aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/skpdiff/SkDifferentPixelsMetric_cpu.cpp
blob: 4e5e93969c3f743ecc6024bbba9576a06a3791ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Copyright 2013 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <cstring>

#include "SkBitmap.h"

#include "SkDifferentPixelsMetric.h"
#include "skpdiff_util.h"

struct SkDifferentPixelsMetric::QueuedDiff {
    bool finished;
    double result;
    SkTDArray<SkIPoint>* poi;
};

const char* SkDifferentPixelsMetric::getName() {
    return "different_pixels";
}

int SkDifferentPixelsMetric::queueDiff(SkBitmap* baseline, SkBitmap* test) {
    double startTime = get_seconds();
    int diffID = fQueuedDiffs.count();
    QueuedDiff* diff = fQueuedDiffs.push();
    SkTDArray<SkIPoint>* poi = diff->poi = new SkTDArray<SkIPoint>();

    // If we never end up running the kernel, include some safe defaults in the result.
    diff->finished = false;
    diff->result = -1;

    // Ensure the images are comparable
    if (baseline->width() != test->width() || baseline->height() != test->height() ||
        baseline->width() <= 0 || baseline->height() <= 0 ||
        baseline->config() != test->config()) {
        diff->finished = true;
        return diffID;
    }

    int width = baseline->width();
    int height = baseline->height();
    int differentPixelsCount = 0;

    // Prepare the pixels for comparison
    baseline->lockPixels();
    test->lockPixels();
    for (int y = 0; y < height; y++) {
        // Grab a row from each image for easy comparison
        unsigned char* baselineRow = (unsigned char*)baseline->getAddr(0, y);
        unsigned char* testRow = (unsigned char*)test->getAddr(0, y);
        for (int x = 0; x < width; x++) {
            // Compare one pixel at a time so each differing pixel can be noted
            if (std::memcmp(&baselineRow[x * 4], &testRow[x * 4], 4) != 0) {
                poi->push()->set(x, y);
                differentPixelsCount++;
            }
        }
    }
    test->unlockPixels();
    baseline->unlockPixels();

    // Calculates the percentage of identical pixels
    diff->result = 1.0 - ((double)differentPixelsCount / (width * height));

    SkDebugf("Time: %f\n", (get_seconds() - startTime));

    return diffID;
}

void SkDifferentPixelsMetric::deleteDiff(int id) {
    if (NULL != fQueuedDiffs[id].poi)
    {
        delete fQueuedDiffs[id].poi;
        fQueuedDiffs[id].poi = NULL;
    }
}

bool SkDifferentPixelsMetric::isFinished(int id) {
    return fQueuedDiffs[id].finished;
}

double SkDifferentPixelsMetric::getResult(int id) {
    return fQueuedDiffs[id].result;
}

int SkDifferentPixelsMetric::getPointsOfInterestCount(int id) {
    return fQueuedDiffs[id].poi->count();
}

SkIPoint* SkDifferentPixelsMetric::getPointsOfInterest(int id) {
    return fQueuedDiffs[id].poi->begin();
}