OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkDifferentPixelsMetric.h" | 8 #include "SkDifferentPixelsMetric.h" |
9 | 9 |
10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
11 #include "skpdiff_util.h" | 11 #include "skpdiff_util.h" |
12 | 12 |
13 /* | |
14 * TODO: It would be more flexible, and probably more efficient, if a diffing | |
15 * module could return an arbitrary number of results, each one as a name/value | |
16 * pair. | |
djsollen
2014/06/12 13:27:00
isn't that the same as adding them explicitly to t
epoger
2014/06/12 14:02:34
Yeah, that seems quite reasonable, thanks for the
| |
17 * | |
18 * So, this one could return: | |
19 * "num_differing_pixels": 1111 | |
20 * "ratio_differing_pixels": 0.3984 | |
21 * "max_red_diff": 32 | |
22 * "max_green_diff": 121 | |
23 * "max_blue_diff": 84 | |
24 */ | |
25 | |
13 const char* SkDifferentPixelsMetric::getName() const { | 26 const char* SkDifferentPixelsMetric::getName() const { |
14 return "different_pixels"; | 27 return "different_pixels"; |
15 } | 28 } |
16 | 29 |
17 bool SkDifferentPixelsMetric::diff(SkBitmap* baseline, SkBitmap* test, bool comp uteMask, | 30 bool SkDifferentPixelsMetric::diff(SkBitmap* baseline, SkBitmap* test, bool comp uteAlphaMask, |
31 bool computeRgbDiff, bool computeWhiteDiff, | |
18 Result* result) const { | 32 Result* result) const { |
19 double startTime = get_seconds(); | 33 double startTime = get_seconds(); |
20 | 34 |
21 // Ensure the images are comparable | 35 // Ensure the images are comparable |
22 if (baseline->width() != test->width() || baseline->height() != test->height () || | 36 if (baseline->width() != test->width() || baseline->height() != test->height () || |
23 baseline->width() <= 0 || baseline->height() <= 0 || | 37 baseline->width() <= 0 || baseline->height() <= 0 || |
24 baseline->config() != test->config()) { | 38 baseline->config() != test->config()) { |
39 SkASSERT(baseline->width() == test->width()); | |
40 SkASSERT(baseline->height() == test->height()); | |
41 SkASSERT(baseline->width() > 0); | |
42 SkASSERT(baseline->height() > 0); | |
43 SkASSERT(baseline->config() == test->config()); | |
25 return false; | 44 return false; |
26 } | 45 } |
27 | 46 |
28 int width = baseline->width(); | 47 int width = baseline->width(); |
29 int height = baseline->height(); | 48 int height = baseline->height(); |
49 uint32_t maxRedDiff = 0; | |
50 uint32_t maxGreenDiff = 0; | |
51 uint32_t maxBlueDiff = 0; | |
30 | 52 |
31 // Prepare the POI alpha mask if needed | 53 // Prepare any bitmaps we will be filling in |
32 if (computeMask) { | 54 if (computeAlphaMask) { |
33 result->poiAlphaMask.allocPixels(SkImageInfo::MakeA8(width, height)); | 55 result->poiAlphaMask.allocPixels(SkImageInfo::MakeA8(width, height)); |
34 result->poiAlphaMask.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0); | 56 result->poiAlphaMask.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0); |
35 } | 57 } |
58 if (computeRgbDiff) { | |
59 result->rgbDiffBitmap.allocPixels(SkImageInfo::Make(width, height, basel ine->colorType(), | |
60 kPremul_SkAlphaType) ); | |
61 result->rgbDiffBitmap.eraseARGB(SK_AlphaTRANSPARENT, 0, 0, 0); | |
62 } | |
63 if (computeWhiteDiff) { | |
64 result->whiteDiffBitmap.allocPixels(SkImageInfo::MakeN32Premul(width, he ight)); | |
65 result->whiteDiffBitmap.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0); | |
66 } | |
36 | 67 |
37 // Prepare the pixels for comparison | 68 // Prepare the pixels for comparison |
38 result->poiCount = 0; | 69 result->poiCount = 0; |
39 baseline->lockPixels(); | 70 baseline->lockPixels(); |
40 test->lockPixels(); | 71 test->lockPixels(); |
41 for (int y = 0; y < height; y++) { | 72 for (int y = 0; y < height; y++) { |
42 // Grab a row from each image for easy comparison | 73 // Grab a row from each image for easy comparison |
43 unsigned char* baselineRow = (unsigned char*)baseline->getAddr(0, y); | 74 // TODO: The code below already assumes 4 bytes per pixel, so I think |
44 unsigned char* testRow = (unsigned char*)test->getAddr(0, y); | 75 // we could just call getAddr32() to save a little time. |
76 // OR, if we want to play it safe, call ComputeBytesPerPixel instead | |
77 // of assuming 4 bytes per pixel. | |
78 uint32_t* baselineRow = static_cast<uint32_t *>(baseline->getAddr(0, y)) ; | |
79 uint32_t* testRow = static_cast<uint32_t *>(test->getAddr(0, y)); | |
45 for (int x = 0; x < width; x++) { | 80 for (int x = 0; x < width; x++) { |
46 // Compare one pixel at a time so each differing pixel can be noted | 81 // Compare one pixel at a time so each differing pixel can be noted |
47 if (memcmp(&baselineRow[x * 4], &testRow[x * 4], 4) != 0) { | 82 uint32_t baselinePixel = baselineRow[x]; |
83 uint32_t testPixel = testRow[x]; | |
84 if (baselinePixel != testPixel) { | |
48 result->poiCount++; | 85 result->poiCount++; |
49 if (computeMask) { | 86 |
87 uint32_t redDiff = abs(SkColorGetR(baselinePixel) - SkColorGetR( testPixel)); | |
epoger
2014/06/12 07:02:07
This section of the code seems like a good place t
| |
88 if (redDiff > maxRedDiff) {maxRedDiff = redDiff;} | |
89 uint32_t greenDiff = abs(SkColorGetG(baselinePixel) - SkColorGet G(testPixel)); | |
90 if (greenDiff > maxGreenDiff) {maxGreenDiff = greenDiff;} | |
91 uint32_t blueDiff = abs(SkColorGetB(baselinePixel) - SkColorGetB (testPixel)); | |
92 if (blueDiff > maxBlueDiff) {maxBlueDiff = blueDiff;} | |
93 | |
94 if (computeAlphaMask) { | |
50 *result->poiAlphaMask.getAddr8(x,y) = SK_AlphaTRANSPARENT; | 95 *result->poiAlphaMask.getAddr8(x,y) = SK_AlphaTRANSPARENT; |
51 } | 96 } |
97 if (computeRgbDiff) { | |
98 *result->rgbDiffBitmap.getAddr32(x,y) = | |
99 SkColorSetRGB(redDiff, greenDiff, blueDiff); | |
100 } | |
101 if (computeWhiteDiff) { | |
102 *result->whiteDiffBitmap.getAddr32(x,y) = SK_ColorWHITE; | |
103 } | |
52 } | 104 } |
53 } | 105 } |
54 } | 106 } |
55 test->unlockPixels(); | 107 test->unlockPixels(); |
56 baseline->unlockPixels(); | 108 baseline->unlockPixels(); |
57 | 109 |
58 if (computeMask) { | 110 result->maxRedDiff = maxRedDiff; |
111 result->maxGreenDiff = maxGreenDiff; | |
112 result->maxBlueDiff = maxBlueDiff; | |
113 | |
114 if (computeAlphaMask) { | |
59 result->poiAlphaMask.unlockPixels(); | 115 result->poiAlphaMask.unlockPixels(); |
60 } | 116 } |
117 if (computeRgbDiff) { | |
118 result->rgbDiffBitmap.unlockPixels(); | |
119 } | |
120 if (computeWhiteDiff) { | |
121 result->whiteDiffBitmap.unlockPixels(); | |
122 } | |
61 | 123 |
62 // Calculates the percentage of identical pixels | 124 // Calculates the percentage of identical pixels |
63 result->result = 1.0 - ((double)result->poiCount / (width * height)); | 125 result->result = 1.0 - ((double)result->poiCount / (width * height)); |
64 result->timeElapsed = get_seconds() - startTime; | 126 result->timeElapsed = get_seconds() - startTime; |
65 | 127 |
66 return true; | 128 return true; |
67 } | 129 } |
OLD | NEW |