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