aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/skdiff.cpp
blob: ae6d72cd7a0ee443686de9fdaa8162d2fcfae2ec (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "skdiff.h"
#include "SkBitmap.h"
#include "SkColor.h"
#include "SkColorPriv.h"
#include "SkTypes.h"

/*static*/ char const * const DiffRecord::ResultNames[DiffRecord::kResultCount] = {
    "EqualBits",
    "EqualPixels",
    "DifferentPixels",
    "DifferentSizes",
    "CouldNotCompare",
    "Unknown",
};

DiffRecord::Result DiffRecord::getResultByName(const char *name) {
    for (int result = 0; result < DiffRecord::kResultCount; ++result) {
        if (0 == strcmp(DiffRecord::ResultNames[result], name)) {
            return static_cast<DiffRecord::Result>(result);
        }
    }
    return DiffRecord::kResultCount;
}

static char const * const ResultDescriptions[DiffRecord::kResultCount] = {
    "contain exactly the same bits",
    "contain the same pixel values, but not the same bits",
    "have identical dimensions but some differing pixels",
    "have differing dimensions",
    "could not be compared",
    "not compared yet",
};

const char* DiffRecord::getResultDescription(DiffRecord::Result result) {
    return ResultDescriptions[result];
}

/*static*/ char const * const DiffResource::StatusNames[DiffResource::kStatusCount] = {
    "Decoded",
    "CouldNotDecode",

    "Read",
    "CouldNotRead",

    "Exists",
    "DoesNotExist",

    "Specified",
    "Unspecified",

    "Unknown",
};

DiffResource::Status DiffResource::getStatusByName(const char *name) {
    for (int status = 0; status < DiffResource::kStatusCount; ++status) {
        if (0 == strcmp(DiffResource::StatusNames[status], name)) {
            return static_cast<DiffResource::Status>(status);
        }
    }
    return DiffResource::kStatusCount;
}

static char const * const StatusDescriptions[DiffResource::kStatusCount] = {
    "decoded",
    "could not be decoded",

    "read",
    "could not be read",

    "found",
    "not found",

    "specified",
    "unspecified",

    "unknown",
};

const char* DiffResource::getStatusDescription(DiffResource::Status status) {
    return StatusDescriptions[status];
}

bool DiffResource::isStatusFailed(DiffResource::Status status) {
    return DiffResource::kCouldNotDecode_Status == status ||
           DiffResource::kCouldNotRead_Status == status ||
           DiffResource::kDoesNotExist_Status == status ||
           DiffResource::kUnspecified_Status == status ||
           DiffResource::kUnknown_Status == status;
}

bool DiffResource::getMatchingStatuses(char* selector, bool statuses[kStatusCount]) {
    if (!strcmp(selector, "any")) {
        for (int statusIndex = 0; statusIndex < kStatusCount; ++statusIndex) {
            statuses[statusIndex] = true;
        }
        return true;
    }

    for (int statusIndex = 0; statusIndex < kStatusCount; ++statusIndex) {
        statuses[statusIndex] = false;
    }

    static const char kDelimiterChar = ',';
    bool understood = true;
    while (true) {
        char* delimiterPtr = strchr(selector, kDelimiterChar);

        if (delimiterPtr) {
            *delimiterPtr = '\0';
        }

        if (!strcmp(selector, "failed")) {
            for (int statusIndex = 0; statusIndex < kStatusCount; ++statusIndex) {
                Status status = static_cast<Status>(statusIndex);
                statuses[statusIndex] |= isStatusFailed(status);
            }
        } else {
            Status status = getStatusByName(selector);
            if (status == kStatusCount) {
                understood = false;
            } else {
                statuses[status] = true;
            }
        }

        if (!delimiterPtr) {
            break;
        }

        *delimiterPtr = kDelimiterChar;
        selector = delimiterPtr + 1;
    }
    return understood;
}

static inline bool colors_match_thresholded(SkPMColor c0, SkPMColor c1, const int threshold) {
    int da = SkGetPackedA32(c0) - SkGetPackedA32(c1);
    int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
    int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
    int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);

    return ((SkAbs32(da) <= threshold) &&
            (SkAbs32(dr) <= threshold) &&
            (SkAbs32(dg) <= threshold) &&
            (SkAbs32(db) <= threshold));
}

const SkPMColor PMCOLOR_WHITE = SkPreMultiplyColor(SK_ColorWHITE);
const SkPMColor PMCOLOR_BLACK = SkPreMultiplyColor(SK_ColorBLACK);

void compute_diff(DiffRecord* dr, DiffMetricProc diffFunction, const int colorThreshold) {
    const int w = dr->fComparison.fBitmap.width();
    const int h = dr->fComparison.fBitmap.height();
    if (w != dr->fBase.fBitmap.width() || h != dr->fBase.fBitmap.height()) {
        dr->fResult = DiffRecord::kDifferentSizes_Result;
        return;
    }

    SkAutoLockPixels alpDiff(dr->fDifference.fBitmap);
    SkAutoLockPixels alpWhite(dr->fWhite.fBitmap);
    int mismatchedPixels = 0;
    int totalMismatchA = 0;
    int totalMismatchR = 0;
    int totalMismatchG = 0;
    int totalMismatchB = 0;

    // Accumulate fractionally different pixels, then divide out
    // # of pixels at the end.
    dr->fWeightedFraction = 0;
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            SkPMColor c0 = *dr->fBase.fBitmap.getAddr32(x, y);
            SkPMColor c1 = *dr->fComparison.fBitmap.getAddr32(x, y);
            SkPMColor outputDifference = diffFunction(c0, c1);
            uint32_t thisA = SkAbs32(SkGetPackedA32(c0) - SkGetPackedA32(c1));
            uint32_t thisR = SkAbs32(SkGetPackedR32(c0) - SkGetPackedR32(c1));
            uint32_t thisG = SkAbs32(SkGetPackedG32(c0) - SkGetPackedG32(c1));
            uint32_t thisB = SkAbs32(SkGetPackedB32(c0) - SkGetPackedB32(c1));
            totalMismatchA += thisA;
            totalMismatchR += thisR;
            totalMismatchG += thisG;
            totalMismatchB += thisB;
            // In HSV, value is defined as max RGB component.
            int value = MAX3(thisR, thisG, thisB);
            dr->fWeightedFraction += ((float) value) / 255;
            if (thisA > dr->fMaxMismatchA) {
                dr->fMaxMismatchA = thisA;
            }
            if (thisR > dr->fMaxMismatchR) {
                dr->fMaxMismatchR = thisR;
            }
            if (thisG > dr->fMaxMismatchG) {
                dr->fMaxMismatchG = thisG;
            }
            if (thisB > dr->fMaxMismatchB) {
                dr->fMaxMismatchB = thisB;
            }
            if (!colors_match_thresholded(c0, c1, colorThreshold)) {
                mismatchedPixels++;
                *dr->fDifference.fBitmap.getAddr32(x, y) = outputDifference;
                *dr->fWhite.fBitmap.getAddr32(x, y) = PMCOLOR_WHITE;
            } else {
                *dr->fDifference.fBitmap.getAddr32(x, y) = 0;
                *dr->fWhite.fBitmap.getAddr32(x, y) = PMCOLOR_BLACK;
            }
        }
    }
    if (0 == mismatchedPixels) {
        dr->fResult = DiffRecord::kEqualPixels_Result;
        return;
    }
    dr->fResult = DiffRecord::kDifferentPixels_Result;
    int pixelCount = w * h;
    dr->fFractionDifference = ((float) mismatchedPixels) / pixelCount;
    dr->fWeightedFraction /= pixelCount;
    dr->fTotalMismatchA = totalMismatchA;
    dr->fAverageMismatchA = ((float) totalMismatchA) / pixelCount;
    dr->fAverageMismatchR = ((float) totalMismatchR) / pixelCount;
    dr->fAverageMismatchG = ((float) totalMismatchG) / pixelCount;
    dr->fAverageMismatchB = ((float) totalMismatchB) / pixelCount;
}