OLD | NEW |
| (Empty) |
1 /* | |
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 | |
8 #include "SkDifferentPixelsMetric.h" | |
9 | |
10 #include "SkBitmap.h" | |
11 #include "skpdiff_util.h" | |
12 | |
13 const char* SkDifferentPixelsMetric::getName() const { | |
14 return "different_pixels"; | |
15 } | |
16 | |
17 bool SkDifferentPixelsMetric::diff(SkBitmap* baseline, SkBitmap* test, | |
18 const BitmapsToCreate& bitmapsToCreate, | |
19 Result* result) const { | |
20 double startTime = get_seconds(); | |
21 | |
22 // Ensure the images are comparable | |
23 if (baseline->width() != test->width() || baseline->height() != test->height
() || | |
24 baseline->width() <= 0 || baseline->height() <= 0 || | |
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()); | |
31 return false; | |
32 } | |
33 | |
34 int width = baseline->width(); | |
35 int height = baseline->height(); | |
36 int maxRedDiff = 0; | |
37 int maxGreenDiff = 0; | |
38 int maxBlueDiff = 0; | |
39 | |
40 // Prepare any bitmaps we will be filling in | |
41 if (bitmapsToCreate.alphaMask) { | |
42 result->poiAlphaMask.allocPixels(SkImageInfo::MakeA8(width, height)); | |
43 result->poiAlphaMask.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0); | |
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 } | |
54 | |
55 // Prepare the pixels for comparison | |
56 result->poiCount = 0; | |
57 baseline->lockPixels(); | |
58 test->lockPixels(); | |
59 for (int y = 0; y < height; y++) { | |
60 // Grab a row from each image for easy comparison | |
61 // 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)); | |
67 for (int x = 0; x < width; x++) { | |
68 // Compare one pixel at a time so each differing pixel can be noted | |
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) { | |
74 result->poiCount++; | |
75 | |
76 int redDiff = SkTAbs(static_cast<int>(SkColorGetR(baselinePixel)
- | |
77 SkColorGetR(testPixel))); | |
78 if (redDiff > maxRedDiff) {maxRedDiff = redDiff;} | |
79 int greenDiff = SkTAbs(static_cast<int>(SkColorGetG(baselinePixe
l) - | |
80 SkColorGetG(testPixel)))
; | |
81 if (greenDiff > maxGreenDiff) {maxGreenDiff = greenDiff;} | |
82 int blueDiff = SkTAbs(static_cast<int>(SkColorGetB(baselinePixel
) - | |
83 SkColorGetB(testPixel))); | |
84 if (blueDiff > maxBlueDiff) {maxBlueDiff = blueDiff;} | |
85 | |
86 if (bitmapsToCreate.alphaMask) { | |
87 *result->poiAlphaMask.getAddr8(x,y) = SK_AlphaTRANSPARENT; | |
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 } | |
96 } | |
97 } | |
98 } | |
99 test->unlockPixels(); | |
100 baseline->unlockPixels(); | |
101 | |
102 result->maxRedDiff = maxRedDiff; | |
103 result->maxGreenDiff = maxGreenDiff; | |
104 result->maxBlueDiff = maxBlueDiff; | |
105 | |
106 if (bitmapsToCreate.alphaMask) { | |
107 result->poiAlphaMask.unlockPixels(); | |
108 } | |
109 if (bitmapsToCreate.rgbDiff) { | |
110 result->rgbDiffBitmap.unlockPixels(); | |
111 } | |
112 if (bitmapsToCreate.whiteDiff) { | |
113 result->whiteDiffBitmap.unlockPixels(); | |
114 } | |
115 | |
116 // Calculates the percentage of identical pixels | |
117 result->result = 1.0 - ((double)result->poiCount / (width * height)); | |
118 result->timeElapsed = get_seconds() - startTime; | |
119 | |
120 return true; | |
121 } | |
OLD | NEW |