| 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 { | 13 const char* SkDifferentPixelsMetric::getName() const { |
| 16 bool finished; | |
| 17 double result; | |
| 18 SkTDArray<SkIPoint>* poi; | |
| 19 SkBitmap poiAlphaMask; | |
| 20 }; | |
| 21 | |
| 22 const char* SkDifferentPixelsMetric::getName() { | |
| 23 return "different_pixels"; | 14 return "different_pixels"; |
| 24 } | 15 } |
| 25 | 16 |
| 26 bool SkDifferentPixelsMetric::enablePOIAlphaMask() { | 17 bool SkDifferentPixelsMetric::diff(SkBitmap* baseline, SkBitmap* test, bool comp
uteMask, |
| 27 fPOIAlphaMask = true; | 18 Result* result) const { |
| 28 return true; | |
| 29 } | |
| 30 | |
| 31 int SkDifferentPixelsMetric::queueDiff(SkBitmap* baseline, SkBitmap* test) { | |
| 32 double startTime = get_seconds(); | 19 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 | 20 |
| 41 // Ensure the images are comparable | 21 // Ensure the images are comparable |
| 42 if (baseline->width() != test->width() || baseline->height() != test->height
() || | 22 if (baseline->width() != test->width() || baseline->height() != test->height
() || |
| 43 baseline->width() <= 0 || baseline->height() <= 0 || | 23 baseline->width() <= 0 || baseline->height() <= 0 || |
| 44 baseline->config() != test->config()) { | 24 baseline->config() != test->config()) { |
| 45 diff->finished = true; | 25 return false; |
| 46 return diffID; | |
| 47 } | 26 } |
| 48 | 27 |
| 49 int width = baseline->width(); | 28 int width = baseline->width(); |
| 50 int height = baseline->height(); | 29 int height = baseline->height(); |
| 51 int differentPixelsCount = 0; | |
| 52 | 30 |
| 53 // Prepare the POI alpha mask if needed | 31 // Prepare the POI alpha mask if needed |
| 54 if (fPOIAlphaMask) { | 32 if (computeMask) { |
| 55 diff->poiAlphaMask.setConfig(SkBitmap::kA8_Config, width, height); | 33 result->poiAlphaMask.setConfig(SkBitmap::kA8_Config, width, height); |
| 56 diff->poiAlphaMask.allocPixels(); | 34 result->poiAlphaMask.allocPixels(); |
| 57 diff->poiAlphaMask.lockPixels(); | 35 result->poiAlphaMask.lockPixels(); |
| 58 diff->poiAlphaMask.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0); | 36 result->poiAlphaMask.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0); |
| 59 } | 37 } |
| 60 | 38 |
| 61 // Prepare the pixels for comparison | 39 // Prepare the pixels for comparison |
| 40 result->poiCount = 0; |
| 62 baseline->lockPixels(); | 41 baseline->lockPixels(); |
| 63 test->lockPixels(); | 42 test->lockPixels(); |
| 64 for (int y = 0; y < height; y++) { | 43 for (int y = 0; y < height; y++) { |
| 65 // Grab a row from each image for easy comparison | 44 // Grab a row from each image for easy comparison |
| 66 unsigned char* baselineRow = (unsigned char*)baseline->getAddr(0, y); | 45 unsigned char* baselineRow = (unsigned char*)baseline->getAddr(0, y); |
| 67 unsigned char* testRow = (unsigned char*)test->getAddr(0, y); | 46 unsigned char* testRow = (unsigned char*)test->getAddr(0, y); |
| 68 for (int x = 0; x < width; x++) { | 47 for (int x = 0; x < width; x++) { |
| 69 // Compare one pixel at a time so each differing pixel can be noted | 48 // 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) { | 49 if (memcmp(&baselineRow[x * 4], &testRow[x * 4], 4) != 0) { |
| 71 poi->push()->set(x, y); | 50 result->poiCount++; |
| 72 differentPixelsCount++; | 51 if (computeMask) { |
| 73 if (fPOIAlphaMask) { | 52 *result->poiAlphaMask.getAddr8(x,y) = SK_AlphaTRANSPARENT; |
| 74 *diff->poiAlphaMask.getAddr8(x,y) = SK_AlphaTRANSPARENT; | |
| 75 } | 53 } |
| 76 } | 54 } |
| 77 } | 55 } |
| 78 } | 56 } |
| 79 test->unlockPixels(); | 57 test->unlockPixels(); |
| 80 baseline->unlockPixels(); | 58 baseline->unlockPixels(); |
| 81 | 59 |
| 60 if (computeMask) { |
| 61 result->poiAlphaMask.unlockPixels(); |
| 62 } |
| 63 |
| 82 // Calculates the percentage of identical pixels | 64 // Calculates the percentage of identical pixels |
| 83 diff->result = 1.0 - ((double)differentPixelsCount / (width * height)); | 65 result->result = 1.0 - ((double)result->poiCount / (width * height)); |
| 66 result->timeElapsed = get_seconds() - startTime; |
| 84 | 67 |
| 85 SkDebugf("Time: %f\n", (get_seconds() - startTime)); | 68 return true; |
| 86 | |
| 87 return diffID; | |
| 88 } | 69 } |
| 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 |