blob: e528b78f4fa850746177e254d4cbf148bccdc240 [file] [log] [blame]
zachr@google.comd6585682013-07-17 19:29:19 +00001/*
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.comefc51b72013-11-12 18:29:17 +00008#include "SkDifferentPixelsMetric.h"
zachr@google.comd6585682013-07-17 19:29:19 +00009
10#include "SkBitmap.h"
zachr@google.comd6585682013-07-17 19:29:19 +000011#include "skpdiff_util.h"
12
djsollen@google.comefc51b72013-11-12 18:29:17 +000013const char* SkDifferentPixelsMetric::getName() const {
zachr@google.comd6585682013-07-17 19:29:19 +000014 return "different_pixels";
15}
16
djsollen@google.comefc51b72013-11-12 18:29:17 +000017bool SkDifferentPixelsMetric::diff(SkBitmap* baseline, SkBitmap* test, bool computeMask,
18 Result* result) const {
zachr@google.comd6585682013-07-17 19:29:19 +000019 double startTime = get_seconds();
zachr@google.comd6585682013-07-17 19:29:19 +000020
21 // Ensure the images are comparable
22 if (baseline->width() != test->width() || baseline->height() != test->height() ||
23 baseline->width() <= 0 || baseline->height() <= 0 ||
reedc3b32662014-06-17 08:38:31 -070024 baseline->colorType() != test->colorType()) {
djsollen@google.comefc51b72013-11-12 18:29:17 +000025 return false;
zachr@google.comd6585682013-07-17 19:29:19 +000026 }
27
28 int width = baseline->width();
29 int height = baseline->height();
zachr@google.comd6585682013-07-17 19:29:19 +000030
djsollen@google.com513a7bf2013-11-07 19:24:06 +000031 // Prepare the POI alpha mask if needed
djsollen@google.comefc51b72013-11-12 18:29:17 +000032 if (computeMask) {
reed6c225732014-06-09 19:52:07 -070033 result->poiAlphaMask.allocPixels(SkImageInfo::MakeA8(width, height));
djsollen@google.comefc51b72013-11-12 18:29:17 +000034 result->poiAlphaMask.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0);
djsollen@google.com513a7bf2013-11-07 19:24:06 +000035 }
36
zachr@google.comd6585682013-07-17 19:29:19 +000037 // Prepare the pixels for comparison
djsollen@google.comefc51b72013-11-12 18:29:17 +000038 result->poiCount = 0;
zachr@google.comd6585682013-07-17 19:29:19 +000039 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.comefc51b72013-11-12 18:29:17 +000047 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.com513a7bf2013-11-07 19:24:06 +000051 }
zachr@google.comd6585682013-07-17 19:29:19 +000052 }
53 }
54 }
55 test->unlockPixels();
56 baseline->unlockPixels();
57
djsollen@google.comefc51b72013-11-12 18:29:17 +000058 if (computeMask) {
59 result->poiAlphaMask.unlockPixels();
60 }
61
zachr@google.comd6585682013-07-17 19:29:19 +000062 // Calculates the percentage of identical pixels
djsollen@google.comefc51b72013-11-12 18:29:17 +000063 result->result = 1.0 - ((double)result->poiCount / (width * height));
64 result->timeElapsed = get_seconds() - startTime;
zachr@google.comd6585682013-07-17 19:29:19 +000065
djsollen@google.comefc51b72013-11-12 18:29:17 +000066 return true;
djsollen@google.com513a7bf2013-11-07 19:24:06 +000067}