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 const char* SkDifferentPixelsMetric::getName() const { | 13 const char* SkDifferentPixelsMetric::getName() const { |
14 return "different_pixels"; | 14 return "different_pixels"; |
15 } | 15 } |
16 | 16 |
17 bool SkDifferentPixelsMetric::diff(SkBitmap* baseline, SkBitmap* test, bool comp
uteMask, | 17 bool SkDifferentPixelsMetric::diff(SkBitmap* baseline, SkBitmap* test, |
| 18 const BitmapsToCreate& bitmapsToCreate, |
18 Result* result) const { | 19 Result* result) const { |
19 double startTime = get_seconds(); | 20 double startTime = get_seconds(); |
20 | 21 |
21 // Ensure the images are comparable | 22 // Ensure the images are comparable |
22 if (baseline->width() != test->width() || baseline->height() != test->height
() || | 23 if (baseline->width() != test->width() || baseline->height() != test->height
() || |
23 baseline->width() <= 0 || baseline->height() <= 0 || | 24 baseline->width() <= 0 || baseline->height() <= 0 || |
24 baseline->colorType() != test->colorType()) { | 25 baseline->colorType() != test->colorType()) { |
| 26 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()); |
25 return false; | 31 return false; |
26 } | 32 } |
27 | 33 |
28 int width = baseline->width(); | 34 int width = baseline->width(); |
29 int height = baseline->height(); | 35 int height = baseline->height(); |
| 36 int maxRedDiff = 0; |
| 37 int maxGreenDiff = 0; |
| 38 int maxBlueDiff = 0; |
30 | 39 |
31 // Prepare the POI alpha mask if needed | 40 // Prepare any bitmaps we will be filling in |
32 if (computeMask) { | 41 if (bitmapsToCreate.alphaMask) { |
33 result->poiAlphaMask.allocPixels(SkImageInfo::MakeA8(width, height)); | 42 result->poiAlphaMask.allocPixels(SkImageInfo::MakeA8(width, height)); |
34 result->poiAlphaMask.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0); | 43 result->poiAlphaMask.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0); |
35 } | 44 } |
| 45 if (bitmapsToCreate.rgbDiff) { |
| 46 result->rgbDiffBitmap.allocPixels(SkImageInfo::Make(width, height, basel
ine->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, he
ight)); |
| 52 result->whiteDiffBitmap.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0); |
| 53 } |
36 | 54 |
37 // Prepare the pixels for comparison | 55 // Prepare the pixels for comparison |
38 result->poiCount = 0; | 56 result->poiCount = 0; |
39 baseline->lockPixels(); | 57 baseline->lockPixels(); |
40 test->lockPixels(); | 58 test->lockPixels(); |
41 for (int y = 0; y < height; y++) { | 59 for (int y = 0; y < height; y++) { |
42 // Grab a row from each image for easy comparison | 60 // Grab a row from each image for easy comparison |
43 unsigned char* baselineRow = (unsigned char*)baseline->getAddr(0, y); | 61 // TODO(epoger): The code below already assumes 4 bytes per pixel, so I
think |
44 unsigned char* testRow = (unsigned char*)test->getAddr(0, y); | 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)); |
45 for (int x = 0; x < width; x++) { | 67 for (int x = 0; x < width; x++) { |
46 // Compare one pixel at a time so each differing pixel can be noted | 68 // Compare one pixel at a time so each differing pixel can be noted |
47 if (memcmp(&baselineRow[x * 4], &testRow[x * 4], 4) != 0) { | 69 // TODO(epoger): This loop looks like a good place to work on perfor
mance, |
| 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) { |
48 result->poiCount++; | 74 result->poiCount++; |
49 if (computeMask) { | 75 |
| 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) { |
50 *result->poiAlphaMask.getAddr8(x,y) = SK_AlphaTRANSPARENT; | 87 *result->poiAlphaMask.getAddr8(x,y) = SK_AlphaTRANSPARENT; |
51 } | 88 } |
| 89 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 } |
52 } | 96 } |
53 } | 97 } |
54 } | 98 } |
55 test->unlockPixels(); | 99 test->unlockPixels(); |
56 baseline->unlockPixels(); | 100 baseline->unlockPixels(); |
57 | 101 |
58 if (computeMask) { | 102 result->maxRedDiff = maxRedDiff; |
| 103 result->maxGreenDiff = maxGreenDiff; |
| 104 result->maxBlueDiff = maxBlueDiff; |
| 105 |
| 106 if (bitmapsToCreate.alphaMask) { |
59 result->poiAlphaMask.unlockPixels(); | 107 result->poiAlphaMask.unlockPixels(); |
60 } | 108 } |
| 109 if (bitmapsToCreate.rgbDiff) { |
| 110 result->rgbDiffBitmap.unlockPixels(); |
| 111 } |
| 112 if (bitmapsToCreate.whiteDiff) { |
| 113 result->whiteDiffBitmap.unlockPixels(); |
| 114 } |
61 | 115 |
62 // Calculates the percentage of identical pixels | 116 // Calculates the percentage of identical pixels |
63 result->result = 1.0 - ((double)result->poiCount / (width * height)); | 117 result->result = 1.0 - ((double)result->poiCount / (width * height)); |
64 result->timeElapsed = get_seconds() - startTime; | 118 result->timeElapsed = get_seconds() - startTime; |
65 | 119 |
66 return true; | 120 return true; |
67 } | 121 } |
OLD | NEW |