zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 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 | |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 8 | #include "SkDifferentPixelsMetric.h" |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 9 | |
| 10 | #include "SkBitmap.h" |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 11 | #include "skpdiff_util.h" |
| 12 | |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 13 | const char* SkDifferentPixelsMetric::getName() const { |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 14 | return "different_pixels"; |
| 15 | } |
| 16 | |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame^] | 17 | bool SkDifferentPixelsMetric::diff(SkBitmap* baseline, SkBitmap* test, |
| 18 | const BitmapsToCreate& bitmapsToCreate, |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 19 | Result* result) const { |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 20 | double startTime = get_seconds(); |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 21 | |
| 22 | // Ensure the images are comparable |
| 23 | if (baseline->width() != test->width() || baseline->height() != test->height() || |
| 24 | baseline->width() <= 0 || baseline->height() <= 0 || |
reed | c3b3266 | 2014-06-17 08:38:31 -0700 | [diff] [blame] | 25 | baseline->colorType() != test->colorType()) { |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame^] | 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()); |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 31 | return false; |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | int width = baseline->width(); |
| 35 | int height = baseline->height(); |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame^] | 36 | int maxRedDiff = 0; |
| 37 | int maxGreenDiff = 0; |
| 38 | int maxBlueDiff = 0; |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 39 | |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame^] | 40 | // Prepare any bitmaps we will be filling in |
| 41 | if (bitmapsToCreate.alphaMask) { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 42 | result->poiAlphaMask.allocPixels(SkImageInfo::MakeA8(width, height)); |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 43 | result->poiAlphaMask.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0); |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame] | 44 | } |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame^] | 45 | if (bitmapsToCreate.rgbDiff) { |
| 46 | result->rgbDiffBitmap.allocPixels(SkImageInfo::Make(width, height, baseline->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, height)); |
| 52 | result->whiteDiffBitmap.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0); |
| 53 | } |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame] | 54 | |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 55 | // Prepare the pixels for comparison |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 56 | result->poiCount = 0; |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 57 | baseline->lockPixels(); |
| 58 | test->lockPixels(); |
| 59 | for (int y = 0; y < height; y++) { |
| 60 | // Grab a row from each image for easy comparison |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame^] | 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)); |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 67 | for (int x = 0; x < width; x++) { |
| 68 | // Compare one pixel at a time so each differing pixel can be noted |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame^] | 69 | // TODO(epoger): This loop looks like a good place to work on performance, |
| 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) { |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 74 | result->poiCount++; |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame^] | 75 | |
| 76 | int redDiff = abs(static_cast<int>(SkColorGetR(baselinePixel) - |
| 77 | SkColorGetR(testPixel))); |
| 78 | if (redDiff > maxRedDiff) {maxRedDiff = redDiff;} |
| 79 | int greenDiff = abs(static_cast<int>(SkColorGetG(baselinePixel) - |
| 80 | SkColorGetG(testPixel))); |
| 81 | if (greenDiff > maxGreenDiff) {maxGreenDiff = greenDiff;} |
| 82 | int blueDiff = abs(static_cast<int>(SkColorGetB(baselinePixel) - |
| 83 | SkColorGetB(testPixel))); |
| 84 | if (blueDiff > maxBlueDiff) {maxBlueDiff = blueDiff;} |
| 85 | |
| 86 | if (bitmapsToCreate.alphaMask) { |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 87 | *result->poiAlphaMask.getAddr8(x,y) = SK_AlphaTRANSPARENT; |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame] | 88 | } |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame^] | 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 | } |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | } |
| 99 | test->unlockPixels(); |
| 100 | baseline->unlockPixels(); |
| 101 | |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame^] | 102 | result->maxRedDiff = maxRedDiff; |
| 103 | result->maxGreenDiff = maxGreenDiff; |
| 104 | result->maxBlueDiff = maxBlueDiff; |
| 105 | |
| 106 | if (bitmapsToCreate.alphaMask) { |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 107 | result->poiAlphaMask.unlockPixels(); |
| 108 | } |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame^] | 109 | if (bitmapsToCreate.rgbDiff) { |
| 110 | result->rgbDiffBitmap.unlockPixels(); |
| 111 | } |
| 112 | if (bitmapsToCreate.whiteDiff) { |
| 113 | result->whiteDiffBitmap.unlockPixels(); |
| 114 | } |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 115 | |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 116 | // Calculates the percentage of identical pixels |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 117 | result->result = 1.0 - ((double)result->poiCount / (width * height)); |
| 118 | result->timeElapsed = get_seconds() - startTime; |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 119 | |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 120 | return true; |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame] | 121 | } |