blob: a3e4a383f2e8ef0edc32882a35958b3af28c2018 [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
8#include <cstring>
9
10#include "SkBitmap.h"
11
12#include "SkDifferentPixelsMetric.h"
13#include "skpdiff_util.h"
14
15struct SkDifferentPixelsMetric::QueuedDiff {
16 bool finished;
17 double result;
18 SkTDArray<SkIPoint>* poi;
djsollen@google.com513a7bf2013-11-07 19:24:06 +000019 SkBitmap poiAlphaMask;
zachr@google.comd6585682013-07-17 19:29:19 +000020};
21
22const char* SkDifferentPixelsMetric::getName() {
23 return "different_pixels";
24}
25
djsollen@google.com513a7bf2013-11-07 19:24:06 +000026bool SkDifferentPixelsMetric::enablePOIAlphaMask() {
27 fPOIAlphaMask = true;
28 return true;
29}
30
zachr@google.comd6585682013-07-17 19:29:19 +000031int SkDifferentPixelsMetric::queueDiff(SkBitmap* baseline, SkBitmap* test) {
32 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
41 // Ensure the images are comparable
42 if (baseline->width() != test->width() || baseline->height() != test->height() ||
43 baseline->width() <= 0 || baseline->height() <= 0 ||
44 baseline->config() != test->config()) {
45 diff->finished = true;
46 return diffID;
47 }
48
49 int width = baseline->width();
50 int height = baseline->height();
51 int differentPixelsCount = 0;
52
djsollen@google.com513a7bf2013-11-07 19:24:06 +000053 // Prepare the POI alpha mask if needed
54 if (fPOIAlphaMask) {
55 diff->poiAlphaMask.setConfig(SkBitmap::kA8_Config, width, height);
56 diff->poiAlphaMask.allocPixels();
57 diff->poiAlphaMask.lockPixels();
58 diff->poiAlphaMask.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0);
59 }
60
zachr@google.comd6585682013-07-17 19:29:19 +000061 // Prepare the pixels for comparison
62 baseline->lockPixels();
63 test->lockPixels();
64 for (int y = 0; y < height; y++) {
65 // Grab a row from each image for easy comparison
66 unsigned char* baselineRow = (unsigned char*)baseline->getAddr(0, y);
67 unsigned char* testRow = (unsigned char*)test->getAddr(0, y);
68 for (int x = 0; x < width; x++) {
69 // 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) {
71 poi->push()->set(x, y);
72 differentPixelsCount++;
djsollen@google.com513a7bf2013-11-07 19:24:06 +000073 if (fPOIAlphaMask) {
74 *diff->poiAlphaMask.getAddr8(x,y) = SK_AlphaTRANSPARENT;
75 }
zachr@google.comd6585682013-07-17 19:29:19 +000076 }
77 }
78 }
79 test->unlockPixels();
80 baseline->unlockPixels();
81
82 // Calculates the percentage of identical pixels
83 diff->result = 1.0 - ((double)differentPixelsCount / (width * height));
84
85 SkDebugf("Time: %f\n", (get_seconds() - startTime));
86
87 return diffID;
88}
89
90void SkDifferentPixelsMetric::deleteDiff(int id) {
91 if (NULL != fQueuedDiffs[id].poi)
92 {
93 delete fQueuedDiffs[id].poi;
94 fQueuedDiffs[id].poi = NULL;
95 }
96}
97
98bool SkDifferentPixelsMetric::isFinished(int id) {
99 return fQueuedDiffs[id].finished;
100}
101
102double SkDifferentPixelsMetric::getResult(int id) {
103 return fQueuedDiffs[id].result;
104}
105
106int SkDifferentPixelsMetric::getPointsOfInterestCount(int id) {
107 return fQueuedDiffs[id].poi->count();
108}
109
110SkIPoint* SkDifferentPixelsMetric::getPointsOfInterest(int id) {
111 return fQueuedDiffs[id].poi->begin();
112}
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000113
114SkBitmap* SkDifferentPixelsMetric::getPointsOfInterestAlphaMask(int id) {
115 if (fQueuedDiffs[id].poiAlphaMask.empty()) {
116 return NULL;
117 }
118 return &fQueuedDiffs[id].poiAlphaMask;
119}