aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/skpdiff/SkDifferentPixelsMetric_cpu.cpp
blob: 27c7a135d7d094ee757de452c44e4c727330e85a (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
/*
 * 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 "SkDifferentPixelsMetric.h"

#include "SkBitmap.h"
#include "skpdiff_util.h"

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

bool SkDifferentPixelsMetric::diff(SkBitmap* baseline, SkBitmap* test, bool computeMask,
                                   Result* result) const {
    double startTime = get_seconds();

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

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

    // Prepare the POI alpha mask if needed
    if (computeMask) {
        result->poiAlphaMask.setConfig(SkBitmap::kA8_Config, width, height);
        result->poiAlphaMask.allocPixels();
        result->poiAlphaMask.lockPixels();
        result->poiAlphaMask.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0);
    }

    // Prepare the pixels for comparison
    result->poiCount = 0;
    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 (memcmp(&baselineRow[x * 4], &testRow[x * 4], 4) != 0) {
                result->poiCount++;
                if (computeMask) {
                    *result->poiAlphaMask.getAddr8(x,y) = SK_AlphaTRANSPARENT;
                }
            }
        }
    }
    test->unlockPixels();
    baseline->unlockPixels();

    if (computeMask) {
        result->poiAlphaMask.unlockPixels();
    }

    // Calculates the percentage of identical pixels
    result->result = 1.0 - ((double)result->poiCount / (width * height));
    result->timeElapsed = get_seconds() - startTime;

    return true;
}