bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | #ifndef skdiff_DEFINED |
| 9 | #define skdiff_DEFINED |
| 10 | |
| 11 | #include "SkBitmap.h" |
| 12 | #include "SkColor.h" |
| 13 | #include "SkColorPriv.h" |
| 14 | #include "SkString.h" |
| 15 | #include "SkTDArray.h" |
| 16 | |
| 17 | #if SK_BUILD_FOR_WIN32 |
| 18 | #define PATH_DIV_STR "\\" |
| 19 | #define PATH_DIV_CHAR '\\' |
| 20 | #else |
| 21 | #define PATH_DIV_STR "/" |
| 22 | #define PATH_DIV_CHAR '/' |
| 23 | #endif |
| 24 | |
| 25 | #define MAX2(a,b) (((b) < (a)) ? (a) : (b)) |
| 26 | #define MAX3(a,b,c) (((b) < (a)) ? MAX2((a), (c)) : MAX2((b), (c))) |
| 27 | |
| 28 | |
| 29 | struct DiffResource { |
| 30 | enum Status { |
| 31 | /** The resource was specified, exists, read, and decoded. */ |
| 32 | kDecoded_Status, |
| 33 | /** The resource was specified, exists, read, but could not be decoded. */ |
| 34 | kCouldNotDecode_Status, |
| 35 | |
| 36 | /** The resource was specified, exists, and read. */ |
| 37 | kRead_Status, |
| 38 | /** The resource was specified, exists, but could not be read. */ |
| 39 | kCouldNotRead_Status, |
| 40 | |
| 41 | /** The resource was specified and exists. */ |
| 42 | kExists_Status, |
| 43 | /** The resource was specified, but does not exist. */ |
| 44 | kDoesNotExist_Status, |
| 45 | |
| 46 | /** The resource was specified. */ |
| 47 | kSpecified_Status, |
| 48 | /** The resource was not specified. */ |
| 49 | kUnspecified_Status, |
| 50 | |
| 51 | /** Nothing is yet known about the resource. */ |
| 52 | kUnknown_Status, |
| 53 | |
| 54 | /** NOT A VALID VALUE -- used to set up arrays and to represent an unknown value. */ |
| 55 | kStatusCount |
| 56 | }; |
| 57 | static char const * const StatusNames[DiffResource::kStatusCount]; |
| 58 | |
| 59 | /** Returns the Status with this name. |
| 60 | * If there is no Status with this name, returns kStatusCount. |
| 61 | */ |
| 62 | static Status getStatusByName(const char *name); |
| 63 | |
| 64 | /** Returns a text description of the given Status type. */ |
| 65 | static const char *getStatusDescription(Status status); |
| 66 | |
| 67 | /** Returns true if the Status indicates some kind of failure. */ |
| 68 | static bool isStatusFailed(Status status); |
| 69 | |
| 70 | /** Sets statuses[i] if it is implied by selector, unsets it if not. |
| 71 | * Selector may be a comma delimited list of status names, "any", or "failed". |
| 72 | * Returns true if the selector was entirely understood, false otherwise. |
| 73 | */ |
| 74 | static bool getMatchingStatuses(char* selector, bool statuses[kStatusCount]); |
| 75 | |
| 76 | DiffResource() : fFilename(), fFullPath(), fBitmap(), fStatus(kUnknown_Status) { }; |
| 77 | |
| 78 | /** If isEmpty() indicates no filename available. */ |
| 79 | SkString fFilename; |
| 80 | /** If isEmpty() indicates no path available. */ |
| 81 | SkString fFullPath; |
| 82 | /** If empty() indicates the bitmap could not be created. */ |
| 83 | SkBitmap fBitmap; |
| 84 | Status fStatus; |
| 85 | }; |
| 86 | |
| 87 | struct DiffRecord { |
| 88 | |
| 89 | // Result of comparison for each pair of files. |
| 90 | // Listed from "better" to "worse", for sorting of results. |
| 91 | enum Result { |
| 92 | kEqualBits_Result, |
| 93 | kEqualPixels_Result, |
| 94 | kDifferentPixels_Result, |
| 95 | kDifferentSizes_Result, |
| 96 | kCouldNotCompare_Result, |
| 97 | kUnknown_Result, |
| 98 | |
| 99 | kResultCount // NOT A VALID VALUE--used to set up arrays. Must be last. |
| 100 | }; |
| 101 | static char const * const ResultNames[DiffRecord::kResultCount]; |
| 102 | |
| 103 | /** Returns the Result with this name. |
| 104 | * If there is no Result with this name, returns kResultCount. |
| 105 | */ |
| 106 | static Result getResultByName(const char *name); |
| 107 | |
| 108 | /** Returns a text description of the given Result type. */ |
| 109 | static const char *getResultDescription(Result result); |
| 110 | |
| 111 | DiffRecord() |
| 112 | : fBase() |
| 113 | , fComparison() |
| 114 | , fDifference() |
| 115 | , fWhite() |
| 116 | , fFractionDifference(0) |
| 117 | , fWeightedFraction(0) |
| 118 | , fAverageMismatchR(0) |
| 119 | , fAverageMismatchG(0) |
| 120 | , fAverageMismatchB(0) |
| 121 | , fMaxMismatchR(0) |
| 122 | , fMaxMismatchG(0) |
| 123 | , fMaxMismatchB(0) |
| 124 | , fResult(kUnknown_Result) { |
| 125 | }; |
| 126 | |
| 127 | DiffResource fBase; |
| 128 | DiffResource fComparison; |
| 129 | DiffResource fDifference; |
| 130 | DiffResource fWhite; |
| 131 | |
| 132 | /// Arbitrary floating-point metric to be used to sort images from most |
| 133 | /// to least different from baseline; values of 0 will be omitted from the |
| 134 | /// summary webpage. |
| 135 | float fFractionDifference; |
| 136 | float fWeightedFraction; |
| 137 | |
| 138 | float fAverageMismatchR; |
| 139 | float fAverageMismatchG; |
| 140 | float fAverageMismatchB; |
| 141 | |
| 142 | uint32_t fMaxMismatchR; |
| 143 | uint32_t fMaxMismatchG; |
| 144 | uint32_t fMaxMismatchB; |
| 145 | |
| 146 | /// Which category of diff result. |
| 147 | Result fResult; |
| 148 | }; |
| 149 | |
| 150 | typedef SkTDArray<DiffRecord*> RecordArray; |
| 151 | |
| 152 | /// A wrapper for any sortProc (comparison routine) which applies a first-order |
| 153 | /// sort beforehand, and a tiebreaker if the sortProc returns 0. |
| 154 | template<typename T> static int compare(const void* untyped_lhs, const void* untyped_rhs) { |
| 155 | const DiffRecord* lhs = *reinterpret_cast<DiffRecord* const *>(untyped_lhs); |
| 156 | const DiffRecord* rhs = *reinterpret_cast<DiffRecord* const *>(untyped_rhs); |
| 157 | |
| 158 | // First-order sort... these comparisons should be applied before comparing |
| 159 | // pixel values, no matter what. |
| 160 | if (lhs->fResult != rhs->fResult) { |
| 161 | return (lhs->fResult < rhs->fResult) ? 1 : -1; |
| 162 | } |
| 163 | |
| 164 | // Passed first-order sort, so call the pixel comparison routine. |
| 165 | int result = T::comparePixels(lhs, rhs); |
| 166 | if (result != 0) { |
| 167 | return result; |
| 168 | } |
| 169 | |
| 170 | // Tiebreaker... if we got to this point, we don't really care |
| 171 | // which order they are sorted in, but let's at least be consistent. |
| 172 | return strcmp(lhs->fBase.fFilename.c_str(), rhs->fBase.fFilename.c_str()); |
| 173 | } |
| 174 | |
| 175 | /// Comparison routine for qsort; sorts by fFractionDifference |
| 176 | /// from largest to smallest. |
| 177 | class CompareDiffMetrics { |
| 178 | public: |
| 179 | static int comparePixels(const DiffRecord* lhs, const DiffRecord* rhs) { |
| 180 | if (lhs->fFractionDifference < rhs->fFractionDifference) { |
| 181 | return 1; |
| 182 | } |
| 183 | if (rhs->fFractionDifference < lhs->fFractionDifference) { |
| 184 | return -1; |
| 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | }; |
| 189 | |
| 190 | class CompareDiffWeighted { |
| 191 | public: |
| 192 | static int comparePixels(const DiffRecord* lhs, const DiffRecord* rhs) { |
| 193 | if (lhs->fWeightedFraction < rhs->fWeightedFraction) { |
| 194 | return 1; |
| 195 | } |
| 196 | if (lhs->fWeightedFraction > rhs->fWeightedFraction) { |
| 197 | return -1; |
| 198 | } |
| 199 | return 0; |
| 200 | } |
| 201 | }; |
| 202 | |
| 203 | /// Comparison routine for qsort; sorts by max(fAverageMismatch{RGB}) |
| 204 | /// from largest to smallest. |
| 205 | class CompareDiffMeanMismatches { |
| 206 | public: |
| 207 | static int comparePixels(const DiffRecord* lhs, const DiffRecord* rhs) { |
| 208 | float leftValue = MAX3(lhs->fAverageMismatchR, |
| 209 | lhs->fAverageMismatchG, |
| 210 | lhs->fAverageMismatchB); |
| 211 | float rightValue = MAX3(rhs->fAverageMismatchR, |
| 212 | rhs->fAverageMismatchG, |
| 213 | rhs->fAverageMismatchB); |
| 214 | if (leftValue < rightValue) { |
| 215 | return 1; |
| 216 | } |
| 217 | if (rightValue < leftValue) { |
| 218 | return -1; |
| 219 | } |
| 220 | return 0; |
| 221 | } |
| 222 | }; |
| 223 | |
| 224 | /// Comparison routine for qsort; sorts by max(fMaxMismatch{RGB}) |
| 225 | /// from largest to smallest. |
| 226 | class CompareDiffMaxMismatches { |
| 227 | public: |
| 228 | static int comparePixels(const DiffRecord* lhs, const DiffRecord* rhs) { |
| 229 | uint32_t leftValue = MAX3(lhs->fMaxMismatchR, |
| 230 | lhs->fMaxMismatchG, |
| 231 | lhs->fMaxMismatchB); |
| 232 | uint32_t rightValue = MAX3(rhs->fMaxMismatchR, |
| 233 | rhs->fMaxMismatchG, |
| 234 | rhs->fMaxMismatchB); |
| 235 | if (leftValue < rightValue) { |
| 236 | return 1; |
| 237 | } |
| 238 | if (rightValue < leftValue) { |
| 239 | return -1; |
| 240 | } |
| 241 | |
| 242 | return CompareDiffMeanMismatches::comparePixels(lhs, rhs); |
| 243 | } |
| 244 | }; |
| 245 | |
| 246 | |
| 247 | /// Parameterized routine to compute the color of a pixel in a difference image. |
| 248 | typedef SkPMColor (*DiffMetricProc)(SkPMColor, SkPMColor); |
| 249 | |
| 250 | // from gm |
| 251 | static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) { |
| 252 | int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1); |
| 253 | int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1); |
| 254 | int db = SkGetPackedB32(c0) - SkGetPackedB32(c1); |
| 255 | |
| 256 | return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db)); |
| 257 | } |
| 258 | |
| 259 | /** When finished, dr->fResult should have some value other than kUnknown_Result. |
| 260 | * Expects dr->fWhite.fBitmap and dr->fDifference.fBitmap to have the same bounds as |
| 261 | * dr->fBase.fBitmap and have a valid pixelref. |
| 262 | */ |
| 263 | void compute_diff(DiffRecord* dr, DiffMetricProc diffFunction, const int colorThreshold); |
| 264 | |
| 265 | #endif |