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 | |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 17 | bool SkDifferentPixelsMetric::diff(SkBitmap* baseline, SkBitmap* test, bool computeMask, |
| 18 | Result* result) const { |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 19 | double startTime = get_seconds(); |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 20 | |
| 21 | // Ensure the images are comparable |
| 22 | if (baseline->width() != test->width() || baseline->height() != test->height() || |
| 23 | baseline->width() <= 0 || baseline->height() <= 0 || |
reed | c3b3266 | 2014-06-17 08:38:31 -0700 | [diff] [blame] | 24 | baseline->colorType() != test->colorType()) { |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 25 | return false; |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | int width = baseline->width(); |
| 29 | int height = baseline->height(); |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 30 | |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame] | 31 | // Prepare the POI alpha mask if needed |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 32 | if (computeMask) { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 33 | result->poiAlphaMask.allocPixels(SkImageInfo::MakeA8(width, height)); |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 34 | result->poiAlphaMask.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0); |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame] | 35 | } |
| 36 | |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 37 | // Prepare the pixels for comparison |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 38 | result->poiCount = 0; |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 39 | baseline->lockPixels(); |
| 40 | test->lockPixels(); |
| 41 | for (int y = 0; y < height; y++) { |
| 42 | // Grab a row from each image for easy comparison |
| 43 | unsigned char* baselineRow = (unsigned char*)baseline->getAddr(0, y); |
| 44 | unsigned char* testRow = (unsigned char*)test->getAddr(0, y); |
| 45 | for (int x = 0; x < width; x++) { |
| 46 | // Compare one pixel at a time so each differing pixel can be noted |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 47 | if (memcmp(&baselineRow[x * 4], &testRow[x * 4], 4) != 0) { |
| 48 | result->poiCount++; |
| 49 | if (computeMask) { |
| 50 | *result->poiAlphaMask.getAddr8(x,y) = SK_AlphaTRANSPARENT; |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame] | 51 | } |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | } |
| 55 | test->unlockPixels(); |
| 56 | baseline->unlockPixels(); |
| 57 | |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 58 | if (computeMask) { |
| 59 | result->poiAlphaMask.unlockPixels(); |
| 60 | } |
| 61 | |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 62 | // Calculates the percentage of identical pixels |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 63 | result->result = 1.0 - ((double)result->poiCount / (width * height)); |
| 64 | result->timeElapsed = get_seconds() - startTime; |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 65 | |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 66 | return true; |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame] | 67 | } |