blob: 7194e805bf36dc859a2250f841f8a53a45b4293d [file] [log] [blame]
zachr@google.comd6585682013-07-17 19:29:19 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
djsollen@google.comefc51b72013-11-12 18:29:17 +00008#include "SkDifferentPixelsMetric.h"
zachr@google.comd6585682013-07-17 19:29:19 +00009
10#include "SkBitmap.h"
zachr@google.comd6585682013-07-17 19:29:19 +000011#include "skpdiff_util.h"
12
djsollen@google.comefc51b72013-11-12 18:29:17 +000013const char* SkDifferentPixelsMetric::getName() const {
zachr@google.comd6585682013-07-17 19:29:19 +000014 return "different_pixels";
15}
16
epoger54f1ad82014-07-02 07:43:04 -070017bool SkDifferentPixelsMetric::diff(SkBitmap* baseline, SkBitmap* test,
18 const BitmapsToCreate& bitmapsToCreate,
djsollen@google.comefc51b72013-11-12 18:29:17 +000019 Result* result) const {
zachr@google.comd6585682013-07-17 19:29:19 +000020 double startTime = get_seconds();
zachr@google.comd6585682013-07-17 19:29:19 +000021
22 // Ensure the images are comparable
23 if (baseline->width() != test->width() || baseline->height() != test->height() ||
24 baseline->width() <= 0 || baseline->height() <= 0 ||
reedc3b32662014-06-17 08:38:31 -070025 baseline->colorType() != test->colorType()) {
epoger54f1ad82014-07-02 07:43:04 -070026 SkASSERT(baseline->width() == test->width());
27 SkASSERT(baseline->height() == test->height());
28 SkASSERT(baseline->width() > 0);
29 SkASSERT(baseline->height() > 0);
30 SkASSERT(baseline->colorType() == test->colorType());
djsollen@google.comefc51b72013-11-12 18:29:17 +000031 return false;
zachr@google.comd6585682013-07-17 19:29:19 +000032 }
33
34 int width = baseline->width();
35 int height = baseline->height();
epoger54f1ad82014-07-02 07:43:04 -070036 int maxRedDiff = 0;
37 int maxGreenDiff = 0;
38 int maxBlueDiff = 0;
zachr@google.comd6585682013-07-17 19:29:19 +000039
epoger54f1ad82014-07-02 07:43:04 -070040 // Prepare any bitmaps we will be filling in
41 if (bitmapsToCreate.alphaMask) {
reed6c225732014-06-09 19:52:07 -070042 result->poiAlphaMask.allocPixels(SkImageInfo::MakeA8(width, height));
djsollen@google.comefc51b72013-11-12 18:29:17 +000043 result->poiAlphaMask.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0);
djsollen@google.com513a7bf2013-11-07 19:24:06 +000044 }
epoger54f1ad82014-07-02 07:43:04 -070045 if (bitmapsToCreate.rgbDiff) {
46 result->rgbDiffBitmap.allocPixels(SkImageInfo::Make(width, height, baseline->colorType(),
47 kPremul_SkAlphaType));
48 result->rgbDiffBitmap.eraseARGB(SK_AlphaTRANSPARENT, 0, 0, 0);
49 }
50 if (bitmapsToCreate.whiteDiff) {
51 result->whiteDiffBitmap.allocPixels(SkImageInfo::MakeN32Premul(width, height));
52 result->whiteDiffBitmap.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0);
53 }
djsollen@google.com513a7bf2013-11-07 19:24:06 +000054
zachr@google.comd6585682013-07-17 19:29:19 +000055 // Prepare the pixels for comparison
djsollen@google.comefc51b72013-11-12 18:29:17 +000056 result->poiCount = 0;
zachr@google.comd6585682013-07-17 19:29:19 +000057 baseline->lockPixels();
58 test->lockPixels();
59 for (int y = 0; y < height; y++) {
60 // Grab a row from each image for easy comparison
epoger54f1ad82014-07-02 07:43:04 -070061 // TODO(epoger): The code below already assumes 4 bytes per pixel, so I think
62 // we could just call getAddr32() to save a little time.
63 // OR, if we want to play it safe, call ComputeBytesPerPixel instead
64 // of assuming 4 bytes per pixel.
65 uint32_t* baselineRow = static_cast<uint32_t *>(baseline->getAddr(0, y));
66 uint32_t* testRow = static_cast<uint32_t *>(test->getAddr(0, y));
zachr@google.comd6585682013-07-17 19:29:19 +000067 for (int x = 0; x < width; x++) {
68 // Compare one pixel at a time so each differing pixel can be noted
epoger54f1ad82014-07-02 07:43:04 -070069 // TODO(epoger): This loop looks like a good place to work on performance,
70 // but we should run the code through a profiler to be sure.
71 uint32_t baselinePixel = baselineRow[x];
72 uint32_t testPixel = testRow[x];
73 if (baselinePixel != testPixel) {
djsollen@google.comefc51b72013-11-12 18:29:17 +000074 result->poiCount++;
epoger54f1ad82014-07-02 07:43:04 -070075
76 int redDiff = abs(static_cast<int>(SkColorGetR(baselinePixel) -
77 SkColorGetR(testPixel)));
78 if (redDiff > maxRedDiff) {maxRedDiff = redDiff;}
79 int greenDiff = abs(static_cast<int>(SkColorGetG(baselinePixel) -
80 SkColorGetG(testPixel)));
81 if (greenDiff > maxGreenDiff) {maxGreenDiff = greenDiff;}
82 int blueDiff = abs(static_cast<int>(SkColorGetB(baselinePixel) -
83 SkColorGetB(testPixel)));
84 if (blueDiff > maxBlueDiff) {maxBlueDiff = blueDiff;}
85
86 if (bitmapsToCreate.alphaMask) {
djsollen@google.comefc51b72013-11-12 18:29:17 +000087 *result->poiAlphaMask.getAddr8(x,y) = SK_AlphaTRANSPARENT;
djsollen@google.com513a7bf2013-11-07 19:24:06 +000088 }
epoger54f1ad82014-07-02 07:43:04 -070089 if (bitmapsToCreate.rgbDiff) {
90 *result->rgbDiffBitmap.getAddr32(x,y) =
91 SkColorSetRGB(redDiff, greenDiff, blueDiff);
92 }
93 if (bitmapsToCreate.whiteDiff) {
94 *result->whiteDiffBitmap.getAddr32(x,y) = SK_ColorWHITE;
95 }
zachr@google.comd6585682013-07-17 19:29:19 +000096 }
97 }
98 }
99 test->unlockPixels();
100 baseline->unlockPixels();
101
epoger54f1ad82014-07-02 07:43:04 -0700102 result->maxRedDiff = maxRedDiff;
103 result->maxGreenDiff = maxGreenDiff;
104 result->maxBlueDiff = maxBlueDiff;
105
106 if (bitmapsToCreate.alphaMask) {
djsollen@google.comefc51b72013-11-12 18:29:17 +0000107 result->poiAlphaMask.unlockPixels();
108 }
epoger54f1ad82014-07-02 07:43:04 -0700109 if (bitmapsToCreate.rgbDiff) {
110 result->rgbDiffBitmap.unlockPixels();
111 }
112 if (bitmapsToCreate.whiteDiff) {
113 result->whiteDiffBitmap.unlockPixels();
114 }
djsollen@google.comefc51b72013-11-12 18:29:17 +0000115
zachr@google.comd6585682013-07-17 19:29:19 +0000116 // Calculates the percentage of identical pixels
djsollen@google.comefc51b72013-11-12 18:29:17 +0000117 result->result = 1.0 - ((double)result->poiCount / (width * height));
118 result->timeElapsed = get_seconds() - startTime;
zachr@google.comd6585682013-07-17 19:29:19 +0000119
djsollen@google.comefc51b72013-11-12 18:29:17 +0000120 return true;
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000121}