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
8 #include "SkDifferentPixelsMetric.h"
9
10 #include "SkBitmap.h"
11 #include "skpdiff_util.h"
12
getName() const13 const char* SkDifferentPixelsMetric::getName() const {
14 return "different_pixels";
15 }
16
diff(SkBitmap * baseline,SkBitmap * test,bool computeMask,Result * result) const17 bool SkDifferentPixelsMetric::diff(SkBitmap* baseline, SkBitmap* test, bool computeMask,
18 Result* result) const {
19 double startTime = get_seconds();
20
21 // Ensure the images are comparable
22 if (baseline->width() != test->width() || baseline->height() != test->height() ||
23 baseline->width() <= 0 || baseline->height() <= 0 ||
24 baseline->config() != test->config()) {
25 return false;
26 }
27
28 int width = baseline->width();
29 int height = baseline->height();
30
31 // Prepare the POI alpha mask if needed
32 if (computeMask) {
33 result->poiAlphaMask.setConfig(SkBitmap::kA8_Config, width, height);
34 result->poiAlphaMask.allocPixels();
35 result->poiAlphaMask.lockPixels();
36 result->poiAlphaMask.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0);
37 }
38
39 // Prepare the pixels for comparison
40 result->poiCount = 0;
41 baseline->lockPixels();
42 test->lockPixels();
43 for (int y = 0; y < height; y++) {
44 // Grab a row from each image for easy comparison
45 unsigned char* baselineRow = (unsigned char*)baseline->getAddr(0, y);
46 unsigned char* testRow = (unsigned char*)test->getAddr(0, y);
47 for (int x = 0; x < width; x++) {
48 // Compare one pixel at a time so each differing pixel can be noted
49 if (memcmp(&baselineRow[x * 4], &testRow[x * 4], 4) != 0) {
50 result->poiCount++;
51 if (computeMask) {
52 *result->poiAlphaMask.getAddr8(x,y) = SK_AlphaTRANSPARENT;
53 }
54 }
55 }
56 }
57 test->unlockPixels();
58 baseline->unlockPixels();
59
60 if (computeMask) {
61 result->poiAlphaMask.unlockPixels();
62 }
63
64 // Calculates the percentage of identical pixels
65 result->result = 1.0 - ((double)result->poiCount / (width * height));
66 result->timeElapsed = get_seconds() - startTime;
67
68 return true;
69 }
70