| 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 |
| 26 bool SkDifferentPixelsMetric::enablePOIAlphaMask() { |
| 27 fPOIAlphaMask = true; |
| 28 return true; |
| 29 } |
| 30 |
| 25 int SkDifferentPixelsMetric::queueDiff(SkBitmap* baseline, SkBitmap* test) { | 31 int SkDifferentPixelsMetric::queueDiff(SkBitmap* baseline, SkBitmap* test) { |
| 26 double startTime = get_seconds(); | 32 double startTime = get_seconds(); |
| 27 int diffID = fQueuedDiffs.count(); | 33 int diffID = fQueuedDiffs.count(); |
| 28 QueuedDiff* diff = fQueuedDiffs.push(); | 34 QueuedDiff* diff = fQueuedDiffs.push(); |
| 29 SkTDArray<SkIPoint>* poi = diff->poi = new SkTDArray<SkIPoint>(); | 35 SkTDArray<SkIPoint>* poi = diff->poi = new SkTDArray<SkIPoint>(); |
| 30 | 36 |
| 31 // If we never end up running the kernel, include some safe defaults in the
result. | 37 // If we never end up running the kernel, include some safe defaults in the
result. |
| 32 diff->finished = false; | 38 diff->finished = false; |
| 33 diff->result = -1; | 39 diff->result = -1; |
| 34 | 40 |
| 35 // Ensure the images are comparable | 41 // Ensure the images are comparable |
| 36 if (baseline->width() != test->width() || baseline->height() != test->height
() || | 42 if (baseline->width() != test->width() || baseline->height() != test->height
() || |
| 37 baseline->width() <= 0 || baseline->height() <= 0 || | 43 baseline->width() <= 0 || baseline->height() <= 0 || |
| 38 baseline->config() != test->config()) { | 44 baseline->config() != test->config()) { |
| 39 diff->finished = true; | 45 diff->finished = true; |
| 40 return diffID; | 46 return diffID; |
| 41 } | 47 } |
| 42 | 48 |
| 43 int width = baseline->width(); | 49 int width = baseline->width(); |
| 44 int height = baseline->height(); | 50 int height = baseline->height(); |
| 45 int differentPixelsCount = 0; | 51 int differentPixelsCount = 0; |
| 46 | 52 |
| 53 // Prepare the POI alpha mask if needed |
| 54 if (fPOIAlphaMask) { |
| 55 diff->poiAlphaMask.setConfig(SkBitmap::kA8_Config, width, height); |
| 56 diff->poiAlphaMask.allocPixels(); |
| 57 diff->poiAlphaMask.lockPixels(); |
| 58 diff->poiAlphaMask.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0); |
| 59 } |
| 60 |
| 47 // Prepare the pixels for comparison | 61 // Prepare the pixels for comparison |
| 48 baseline->lockPixels(); | 62 baseline->lockPixels(); |
| 49 test->lockPixels(); | 63 test->lockPixels(); |
| 50 for (int y = 0; y < height; y++) { | 64 for (int y = 0; y < height; y++) { |
| 51 // Grab a row from each image for easy comparison | 65 // Grab a row from each image for easy comparison |
| 52 unsigned char* baselineRow = (unsigned char*)baseline->getAddr(0, y); | 66 unsigned char* baselineRow = (unsigned char*)baseline->getAddr(0, y); |
| 53 unsigned char* testRow = (unsigned char*)test->getAddr(0, y); | 67 unsigned char* testRow = (unsigned char*)test->getAddr(0, y); |
| 54 for (int x = 0; x < width; x++) { | 68 for (int x = 0; x < width; x++) { |
| 55 // Compare one pixel at a time so each differing pixel can be noted | 69 // 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) { | 70 if (std::memcmp(&baselineRow[x * 4], &testRow[x * 4], 4) != 0) { |
| 57 poi->push()->set(x, y); | 71 poi->push()->set(x, y); |
| 58 differentPixelsCount++; | 72 differentPixelsCount++; |
| 73 if (fPOIAlphaMask) { |
| 74 *diff->poiAlphaMask.getAddr8(x,y) = SK_AlphaTRANSPARENT; |
| 75 } |
| 59 } | 76 } |
| 60 } | 77 } |
| 61 } | 78 } |
| 62 test->unlockPixels(); | 79 test->unlockPixels(); |
| 63 baseline->unlockPixels(); | 80 baseline->unlockPixels(); |
| 64 | 81 |
| 65 // Calculates the percentage of identical pixels | 82 // Calculates the percentage of identical pixels |
| 66 diff->result = 1.0 - ((double)differentPixelsCount / (width * height)); | 83 diff->result = 1.0 - ((double)differentPixelsCount / (width * height)); |
| 67 | 84 |
| 68 SkDebugf("Time: %f\n", (get_seconds() - startTime)); | 85 SkDebugf("Time: %f\n", (get_seconds() - startTime)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 86 return fQueuedDiffs[id].result; | 103 return fQueuedDiffs[id].result; |
| 87 } | 104 } |
| 88 | 105 |
| 89 int SkDifferentPixelsMetric::getPointsOfInterestCount(int id) { | 106 int SkDifferentPixelsMetric::getPointsOfInterestCount(int id) { |
| 90 return fQueuedDiffs[id].poi->count(); | 107 return fQueuedDiffs[id].poi->count(); |
| 91 } | 108 } |
| 92 | 109 |
| 93 SkIPoint* SkDifferentPixelsMetric::getPointsOfInterest(int id) { | 110 SkIPoint* SkDifferentPixelsMetric::getPointsOfInterest(int id) { |
| 94 return fQueuedDiffs[id].poi->begin(); | 111 return fQueuedDiffs[id].poi->begin(); |
| 95 } | 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 |