aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/skpdiff/SkDifferentPixelsMetric_cpu.cpp
blob: 7194e805bf36dc859a2250f841f8a53a45b4293d (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * 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,
                                   const BitmapsToCreate& bitmapsToCreate,
                                   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->colorType() != test->colorType()) {
        SkASSERT(baseline->width() == test->width());
        SkASSERT(baseline->height() == test->height());
        SkASSERT(baseline->width() > 0);
        SkASSERT(baseline->height() > 0);
        SkASSERT(baseline->colorType() == test->colorType());
        return false;
    }

    int width = baseline->width();
    int height = baseline->height();
    int maxRedDiff = 0;
    int maxGreenDiff = 0;
    int maxBlueDiff = 0;

    // Prepare any bitmaps we will be filling in
    if (bitmapsToCreate.alphaMask) {
        result->poiAlphaMask.allocPixels(SkImageInfo::MakeA8(width, height));
        result->poiAlphaMask.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0);
    }
    if (bitmapsToCreate.rgbDiff) {
        result->rgbDiffBitmap.allocPixels(SkImageInfo::Make(width, height, baseline->colorType(),
                                                            kPremul_SkAlphaType));
        result->rgbDiffBitmap.eraseARGB(SK_AlphaTRANSPARENT, 0, 0, 0);
    }
    if (bitmapsToCreate.whiteDiff) {
        result->whiteDiffBitmap.allocPixels(SkImageInfo::MakeN32Premul(width, height));
        result->whiteDiffBitmap.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
        // TODO(epoger): The code below already assumes 4 bytes per pixel, so I think
        // we could just call getAddr32() to save a little time.
        // OR, if we want to play it safe, call ComputeBytesPerPixel instead
        // of assuming 4 bytes per pixel.
        uint32_t* baselineRow = static_cast<uint32_t *>(baseline->getAddr(0, y));
        uint32_t* testRow = static_cast<uint32_t *>(test->getAddr(0, y));
        for (int x = 0; x < width; x++) {
            // Compare one pixel at a time so each differing pixel can be noted
            // TODO(epoger): This loop looks like a good place to work on performance,
            // but we should run the code through a profiler to be sure.
            uint32_t baselinePixel = baselineRow[x];
            uint32_t testPixel = testRow[x];
            if (baselinePixel != testPixel) {
                result->poiCount++;

                int redDiff = abs(static_cast<int>(SkColorGetR(baselinePixel) -
                                                   SkColorGetR(testPixel)));
                if (redDiff > maxRedDiff) {maxRedDiff = redDiff;}
                int greenDiff = abs(static_cast<int>(SkColorGetG(baselinePixel) -
                                                     SkColorGetG(testPixel)));
                if (greenDiff > maxGreenDiff) {maxGreenDiff = greenDiff;}
                int blueDiff = abs(static_cast<int>(SkColorGetB(baselinePixel) -
                                                    SkColorGetB(testPixel)));
                if (blueDiff > maxBlueDiff) {maxBlueDiff = blueDiff;}

                if (bitmapsToCreate.alphaMask) {
                    *result->poiAlphaMask.getAddr8(x,y) = SK_AlphaTRANSPARENT;
                }
                if (bitmapsToCreate.rgbDiff) {
                    *result->rgbDiffBitmap.getAddr32(x,y) =
                        SkColorSetRGB(redDiff, greenDiff, blueDiff);
                }
                if (bitmapsToCreate.whiteDiff) {
                    *result->whiteDiffBitmap.getAddr32(x,y) = SK_ColorWHITE;
                }
            }
        }
    }
    test->unlockPixels();
    baseline->unlockPixels();

    result->maxRedDiff = maxRedDiff;
    result->maxGreenDiff = maxGreenDiff;
    result->maxBlueDiff = maxBlueDiff;

    if (bitmapsToCreate.alphaMask) {
        result->poiAlphaMask.unlockPixels();
    }
    if (bitmapsToCreate.rgbDiff) {
        result->rgbDiffBitmap.unlockPixels();
    }
    if (bitmapsToCreate.whiteDiff) {
        result->whiteDiffBitmap.unlockPixels();
    }

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

    return true;
}