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 <cstring> | 8 #include "SkDifferentPixelsMetric.h" |
9 | 9 |
10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
11 | |
12 #include "SkDifferentPixelsMetric.h" | |
13 #include "skpdiff_util.h" | 11 #include "skpdiff_util.h" |
14 | 12 |
15 struct SkDifferentPixelsMetric::QueuedDiff { | |
16 bool finished; | |
17 double result; | |
18 SkTDArray<SkIPoint>* poi; | |
19 SkBitmap poiAlphaMask; | |
20 }; | |
21 | |
22 const char* SkDifferentPixelsMetric::getName() { | 13 const char* SkDifferentPixelsMetric::getName() { |
23 return "different_pixels"; | 14 return "different_pixels"; |
24 } | 15 } |
25 | 16 |
26 bool SkDifferentPixelsMetric::enablePOIAlphaMask() { | 17 bool SkDifferentPixelsMetric::enablePOIAlphaMask() { |
27 fPOIAlphaMask = true; | 18 fPOIAlphaMask = true; |
28 return true; | 19 return true; |
29 } | 20 } |
30 | 21 |
31 int SkDifferentPixelsMetric::queueDiff(SkBitmap* baseline, SkBitmap* test) { | 22 bool SkDifferentPixelsMetric::diff(SkBitmap* baseline, SkBitmap* test, Result* r
esult) { |
32 double startTime = get_seconds(); | 23 double startTime = get_seconds(); |
33 int diffID = fQueuedDiffs.count(); | |
34 QueuedDiff* diff = fQueuedDiffs.push(); | |
35 SkTDArray<SkIPoint>* poi = diff->poi = new SkTDArray<SkIPoint>(); | |
36 | |
37 // If we never end up running the kernel, include some safe defaults in the
result. | |
38 diff->finished = false; | |
39 diff->result = -1; | |
40 | 24 |
41 // Ensure the images are comparable | 25 // Ensure the images are comparable |
42 if (baseline->width() != test->width() || baseline->height() != test->height
() || | 26 if (baseline->width() != test->width() || baseline->height() != test->height
() || |
43 baseline->width() <= 0 || baseline->height() <= 0 || | 27 baseline->width() <= 0 || baseline->height() <= 0 || |
44 baseline->config() != test->config()) { | 28 baseline->config() != test->config()) { |
45 diff->finished = true; | 29 return false; |
46 return diffID; | |
47 } | 30 } |
48 | 31 |
49 int width = baseline->width(); | 32 int width = baseline->width(); |
50 int height = baseline->height(); | 33 int height = baseline->height(); |
51 int differentPixelsCount = 0; | |
52 | 34 |
53 // Prepare the POI alpha mask if needed | 35 // Prepare the POI alpha mask if needed |
54 if (fPOIAlphaMask) { | 36 if (fPOIAlphaMask) { |
55 diff->poiAlphaMask.setConfig(SkBitmap::kA8_Config, width, height); | 37 result->poiAlphaMask.setConfig(SkBitmap::kA8_Config, width, height); |
56 diff->poiAlphaMask.allocPixels(); | 38 result->poiAlphaMask.allocPixels(); |
57 diff->poiAlphaMask.lockPixels(); | 39 result->poiAlphaMask.lockPixels(); |
58 diff->poiAlphaMask.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0); | 40 result->poiAlphaMask.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0); |
59 } | 41 } |
60 | 42 |
61 // Prepare the pixels for comparison | 43 // Prepare the pixels for comparison |
| 44 result->poiCount = 0; |
62 baseline->lockPixels(); | 45 baseline->lockPixels(); |
63 test->lockPixels(); | 46 test->lockPixels(); |
64 for (int y = 0; y < height; y++) { | 47 for (int y = 0; y < height; y++) { |
65 // Grab a row from each image for easy comparison | 48 // Grab a row from each image for easy comparison |
66 unsigned char* baselineRow = (unsigned char*)baseline->getAddr(0, y); | 49 unsigned char* baselineRow = (unsigned char*)baseline->getAddr(0, y); |
67 unsigned char* testRow = (unsigned char*)test->getAddr(0, y); | 50 unsigned char* testRow = (unsigned char*)test->getAddr(0, y); |
68 for (int x = 0; x < width; x++) { | 51 for (int x = 0; x < width; x++) { |
69 // Compare one pixel at a time so each differing pixel can be noted | 52 // Compare one pixel at a time so each differing pixel can be noted |
70 if (std::memcmp(&baselineRow[x * 4], &testRow[x * 4], 4) != 0) { | 53 if (memcmp(&baselineRow[x * 4], &testRow[x * 4], 4) != 0) { |
71 poi->push()->set(x, y); | 54 result->poiCount++; |
72 differentPixelsCount++; | |
73 if (fPOIAlphaMask) { | 55 if (fPOIAlphaMask) { |
74 *diff->poiAlphaMask.getAddr8(x,y) = SK_AlphaTRANSPARENT; | 56 *result->poiAlphaMask.getAddr8(x,y) = SK_AlphaTRANSPARENT; |
75 } | 57 } |
76 } | 58 } |
77 } | 59 } |
78 } | 60 } |
79 test->unlockPixels(); | 61 test->unlockPixels(); |
80 baseline->unlockPixels(); | 62 baseline->unlockPixels(); |
81 | 63 |
| 64 if (fPOIAlphaMask) { |
| 65 result->poiAlphaMask.unlockPixels(); |
| 66 } |
| 67 |
82 // Calculates the percentage of identical pixels | 68 // Calculates the percentage of identical pixels |
83 diff->result = 1.0 - ((double)differentPixelsCount / (width * height)); | 69 result->result = 1.0 - ((double)result->poiCount / (width * height)); |
| 70 result->timeElapsed = get_seconds() - startTime; |
84 | 71 |
85 SkDebugf("Time: %f\n", (get_seconds() - startTime)); | 72 return true; |
86 | |
87 return diffID; | |
88 } | 73 } |
89 | |
90 void SkDifferentPixelsMetric::deleteDiff(int id) { | |
91 if (NULL != fQueuedDiffs[id].poi) | |
92 { | |
93 delete fQueuedDiffs[id].poi; | |
94 fQueuedDiffs[id].poi = NULL; | |
95 } | |
96 } | |
97 | |
98 bool SkDifferentPixelsMetric::isFinished(int id) { | |
99 return fQueuedDiffs[id].finished; | |
100 } | |
101 | |
102 double SkDifferentPixelsMetric::getResult(int id) { | |
103 return fQueuedDiffs[id].result; | |
104 } | |
105 | |
106 int SkDifferentPixelsMetric::getPointsOfInterestCount(int id) { | |
107 return fQueuedDiffs[id].poi->count(); | |
108 } | |
109 | |
110 SkIPoint* SkDifferentPixelsMetric::getPointsOfInterest(int id) { | |
111 return fQueuedDiffs[id].poi->begin(); | |
112 } | |
113 | |
114 SkBitmap* SkDifferentPixelsMetric::getPointsOfInterestAlphaMask(int id) { | |
115 if (fQueuedDiffs[id].poiAlphaMask.empty()) { | |
116 return NULL; | |
117 } | |
118 return &fQueuedDiffs[id].poiAlphaMask; | |
119 } | |
OLD | NEW |