epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 8 | #include "SkColorPriv.h" |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 9 | #include "SkData.h" |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 10 | #include "SkImageDecoder.h" |
| 11 | #include "SkImageEncoder.h" |
| 12 | #include "SkOSFile.h" |
| 13 | #include "SkStream.h" |
| 14 | #include "SkTDArray.h" |
| 15 | #include "SkTemplates.h" |
| 16 | #include "SkTime.h" |
| 17 | #include "SkTSearch.h" |
| 18 | #include "SkTypes.h" |
| 19 | |
| 20 | /** |
| 21 | * skdiff |
| 22 | * |
| 23 | * Given three directory names, expects to find identically-named files in |
| 24 | * each of the first two; the first are treated as a set of baseline, |
| 25 | * the second a set of variant images, and a diff image is written into the |
| 26 | * third directory for each pair. |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 27 | * Creates an index.html in the current third directory to compare each |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 28 | * pair that does not match exactly. |
| 29 | * Does *not* recursively descend directories. |
epoger@google.com | be6188d | 2012-05-31 15:13:45 +0000 | [diff] [blame] | 30 | * |
| 31 | * Returns zero exit code if all images match across baseDir and comparisonDir. |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 32 | */ |
| 33 | |
bsalomon@google.com | 1a315fe | 2011-09-23 14:56:37 +0000 | [diff] [blame] | 34 | #if SK_BUILD_FOR_WIN32 |
| 35 | #define PATH_DIV_STR "\\" |
| 36 | #define PATH_DIV_CHAR '\\' |
| 37 | #else |
| 38 | #define PATH_DIV_STR "/" |
| 39 | #define PATH_DIV_CHAR '/' |
| 40 | #endif |
| 41 | |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 42 | // Result of comparison for each pair of files. |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 43 | enum Result { |
| 44 | kEqualBits, // both files in the pair contain exactly the same bits |
| 45 | kEqualPixels, // not bitwise equal, but their pixels are exactly the same |
| 46 | kDifferentSizes, // both are images we can parse, but of different sizes |
| 47 | kDifferentPixels,// both are images we can parse, but with different pixels |
| 48 | kDifferentOther, // files have different bits but are not parsable images |
| 49 | kBaseMissing, // missing from baseDir |
| 50 | kComparisonMissing,// missing from comparisonDir |
epoger@google.com | 76222c0 | 2012-05-31 15:12:09 +0000 | [diff] [blame] | 51 | kUnknown, // not compared yet |
| 52 | // |
| 53 | kNumResultTypes // NOT A VALID VALUE--used to set up arrays. Must be last. |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 56 | struct DiffRecord { |
tomhudson@google.com | 4e30598 | 2011-07-13 17:42:46 +0000 | [diff] [blame] | 57 | DiffRecord (const SkString filename, |
| 58 | const SkString basePath, |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 59 | const SkString comparisonPath, |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 60 | const Result result = kUnknown) |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 61 | : fFilename (filename) |
tomhudson@google.com | 4e30598 | 2011-07-13 17:42:46 +0000 | [diff] [blame] | 62 | , fBasePath (basePath) |
| 63 | , fComparisonPath (comparisonPath) |
tomhudson@google.com | 9b540ce | 2011-08-02 14:10:04 +0000 | [diff] [blame] | 64 | , fBaseBitmap (new SkBitmap ()) |
| 65 | , fComparisonBitmap (new SkBitmap ()) |
| 66 | , fDifferenceBitmap (new SkBitmap ()) |
epoger@google.com | 25d961c | 2012-02-02 20:50:36 +0000 | [diff] [blame] | 67 | , fWhiteBitmap (new SkBitmap ()) |
tomhudson@google.com | 9b540ce | 2011-08-02 14:10:04 +0000 | [diff] [blame] | 68 | , fBaseHeight (0) |
| 69 | , fBaseWidth (0) |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 70 | , fFractionDifference (0) |
| 71 | , fWeightedFraction (0) |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 72 | , fAverageMismatchR (0) |
| 73 | , fAverageMismatchG (0) |
| 74 | , fAverageMismatchB (0) |
| 75 | , fMaxMismatchR (0) |
| 76 | , fMaxMismatchG (0) |
tomhudson@google.com | 8b08c3e | 2011-11-30 17:01:00 +0000 | [diff] [blame] | 77 | , fMaxMismatchB (0) |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 78 | , fResult(result) { |
tomhudson@google.com | 4e30598 | 2011-07-13 17:42:46 +0000 | [diff] [blame] | 79 | }; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 80 | |
| 81 | SkString fFilename; |
tomhudson@google.com | 4e30598 | 2011-07-13 17:42:46 +0000 | [diff] [blame] | 82 | SkString fBasePath; |
| 83 | SkString fComparisonPath; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 84 | |
tomhudson@google.com | 9b540ce | 2011-08-02 14:10:04 +0000 | [diff] [blame] | 85 | SkBitmap* fBaseBitmap; |
| 86 | SkBitmap* fComparisonBitmap; |
| 87 | SkBitmap* fDifferenceBitmap; |
epoger@google.com | 25d961c | 2012-02-02 20:50:36 +0000 | [diff] [blame] | 88 | SkBitmap* fWhiteBitmap; |
tomhudson@google.com | 9b540ce | 2011-08-02 14:10:04 +0000 | [diff] [blame] | 89 | |
| 90 | int fBaseHeight; |
| 91 | int fBaseWidth; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 92 | |
| 93 | /// Arbitrary floating-point metric to be used to sort images from most |
| 94 | /// to least different from baseline; values of 0 will be omitted from the |
| 95 | /// summary webpage. |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 96 | float fFractionDifference; |
| 97 | float fWeightedFraction; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 98 | |
| 99 | float fAverageMismatchR; |
| 100 | float fAverageMismatchG; |
| 101 | float fAverageMismatchB; |
| 102 | |
| 103 | uint32_t fMaxMismatchR; |
| 104 | uint32_t fMaxMismatchG; |
| 105 | uint32_t fMaxMismatchB; |
tomhudson@google.com | 8b08c3e | 2011-11-30 17:01:00 +0000 | [diff] [blame] | 106 | |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 107 | /// Which category of diff result. |
| 108 | Result fResult; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 111 | #define MAX2(a,b) (((b) < (a)) ? (a) : (b)) |
| 112 | #define MAX3(a,b,c) (((b) < (a)) ? MAX2((a), (c)) : MAX2((b), (c))) |
| 113 | |
epoger@google.com | 25d961c | 2012-02-02 20:50:36 +0000 | [diff] [blame] | 114 | const SkPMColor PMCOLOR_WHITE = SkPreMultiplyColor(SK_ColorWHITE); |
| 115 | const SkPMColor PMCOLOR_BLACK = SkPreMultiplyColor(SK_ColorBLACK); |
| 116 | |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 117 | typedef SkTDArray<SkString*> StringArray; |
| 118 | typedef StringArray FileArray; |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 119 | |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 120 | struct DiffSummary { |
| 121 | DiffSummary () |
| 122 | : fNumMatches (0) |
| 123 | , fNumMismatches (0) |
| 124 | , fMaxMismatchV (0) |
| 125 | , fMaxMismatchPercent (0) { }; |
| 126 | |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 127 | ~DiffSummary() { |
epoger@google.com | 76222c0 | 2012-05-31 15:12:09 +0000 | [diff] [blame] | 128 | for (int i = 0; i < kNumResultTypes; i++) { |
| 129 | fResultsOfType[i].deleteAll(); |
| 130 | } |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 131 | } |
| 132 | |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 133 | uint32_t fNumMatches; |
| 134 | uint32_t fNumMismatches; |
| 135 | uint32_t fMaxMismatchV; |
| 136 | float fMaxMismatchPercent; |
| 137 | |
epoger@google.com | 76222c0 | 2012-05-31 15:12:09 +0000 | [diff] [blame] | 138 | FileArray fResultsOfType[kNumResultTypes]; |
| 139 | |
| 140 | // Print the contents of this FileArray, if any, to stdout. |
| 141 | // (If the FileArray is empty, print nothing.) |
| 142 | void printContents(const FileArray& fileArray, const char* headerText) { |
| 143 | int n = fileArray.count(); |
| 144 | if (n > 0) { |
| 145 | printf("%s:\n", headerText); |
| 146 | for (int i = 0; i < n; ++i) { |
| 147 | printf("\t%s\n", fileArray[i]->c_str()); |
| 148 | } |
| 149 | } |
| 150 | } |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 151 | |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 152 | void print () { |
epoger@google.com | 76222c0 | 2012-05-31 15:12:09 +0000 | [diff] [blame] | 153 | printContents(fResultsOfType[kBaseMissing], |
| 154 | "Missing in baseDir"); |
| 155 | printContents(fResultsOfType[kComparisonMissing], |
| 156 | "Missing in comparisonDir"); |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 157 | printf("%d of %d images matched.\n", fNumMatches, |
| 158 | fNumMatches + fNumMismatches); |
| 159 | if (fNumMismatches > 0) { |
| 160 | printf("Maximum pixel intensity mismatch %d\n", fMaxMismatchV); |
| 161 | printf("Largest area mismatch was %.2f%% of pixels\n", |
| 162 | fMaxMismatchPercent); |
| 163 | } |
| 164 | |
| 165 | } |
| 166 | |
| 167 | void add (DiffRecord* drp) { |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 168 | uint32_t mismatchValue; |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 169 | |
epoger@google.com | 76222c0 | 2012-05-31 15:12:09 +0000 | [diff] [blame] | 170 | fResultsOfType[drp->fResult].push(new SkString(drp->fFilename)); |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 171 | switch (drp->fResult) { |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 172 | case kEqualBits: |
| 173 | fNumMatches++; |
| 174 | break; |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 175 | case kEqualPixels: |
| 176 | fNumMatches++; |
| 177 | break; |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 178 | case kDifferentSizes: |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 179 | fNumMismatches++; |
caryclark@google.com | 3dd4591 | 2012-06-06 12:11:10 +0000 | [diff] [blame^] | 180 | drp->fFractionDifference = 2;// sort as if 200% of pixels differed |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 181 | break; |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 182 | case kDifferentPixels: |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 183 | fNumMismatches++; |
| 184 | if (drp->fFractionDifference * 100 > fMaxMismatchPercent) { |
| 185 | fMaxMismatchPercent = drp->fFractionDifference * 100; |
| 186 | } |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 187 | mismatchValue = MAX3(drp->fMaxMismatchR, drp->fMaxMismatchG, |
| 188 | drp->fMaxMismatchB); |
| 189 | if (mismatchValue > fMaxMismatchV) { |
| 190 | fMaxMismatchV = mismatchValue; |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 191 | } |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 192 | break; |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 193 | case kDifferentOther: |
| 194 | fNumMismatches++; |
caryclark@google.com | 3dd4591 | 2012-06-06 12:11:10 +0000 | [diff] [blame^] | 195 | drp->fFractionDifference = 3;// sort as if 300% of pixels differed |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 196 | break; |
| 197 | case kBaseMissing: |
| 198 | fNumMismatches++; |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 199 | break; |
| 200 | case kComparisonMissing: |
| 201 | fNumMismatches++; |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 202 | break; |
| 203 | case kUnknown: |
| 204 | SkDEBUGFAIL("adding uncategorized DiffRecord"); |
| 205 | break; |
| 206 | default: |
| 207 | SkDEBUGFAIL("adding DiffRecord with unhandled fResult value"); |
| 208 | break; |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | }; |
| 212 | |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 213 | typedef SkTDArray<DiffRecord*> RecordArray; |
| 214 | |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 215 | /// Comparison routine for qsort; sorts by fFractionDifference |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 216 | /// from largest to smallest. |
| 217 | static int compare_diff_metrics (DiffRecord** lhs, DiffRecord** rhs) { |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 218 | if ((*lhs)->fFractionDifference < (*rhs)->fFractionDifference) { |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 219 | return 1; |
| 220 | } |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 221 | if ((*rhs)->fFractionDifference < (*lhs)->fFractionDifference) { |
tomhudson@google.com | 5b32529 | 2011-05-24 19:41:13 +0000 | [diff] [blame] | 222 | return -1; |
| 223 | } |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | static int compare_diff_weighted (DiffRecord** lhs, DiffRecord** rhs) { |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 228 | if ((*lhs)->fWeightedFraction < (*rhs)->fWeightedFraction) { |
tomhudson@google.com | 5b32529 | 2011-05-24 19:41:13 +0000 | [diff] [blame] | 229 | return 1; |
| 230 | } |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 231 | if ((*lhs)->fWeightedFraction > (*rhs)->fWeightedFraction) { |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 232 | return -1; |
| 233 | } |
| 234 | return 0; |
| 235 | } |
| 236 | |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 237 | /// Comparison routine for qsort; sorts by max(fAverageMismatch{RGB}) |
| 238 | /// from largest to smallest. |
| 239 | static int compare_diff_mean_mismatches (DiffRecord** lhs, DiffRecord** rhs) { |
| 240 | float leftValue = MAX3((*lhs)->fAverageMismatchR, |
| 241 | (*lhs)->fAverageMismatchG, |
| 242 | (*lhs)->fAverageMismatchB); |
| 243 | float rightValue = MAX3((*rhs)->fAverageMismatchR, |
| 244 | (*rhs)->fAverageMismatchG, |
| 245 | (*rhs)->fAverageMismatchB); |
| 246 | if (leftValue < rightValue) { |
| 247 | return 1; |
| 248 | } |
| 249 | if (rightValue < leftValue) { |
| 250 | return -1; |
| 251 | } |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | /// Comparison routine for qsort; sorts by max(fMaxMismatch{RGB}) |
| 256 | /// from largest to smallest. |
| 257 | static int compare_diff_max_mismatches (DiffRecord** lhs, DiffRecord** rhs) { |
bsalomon@google.com | 8e06dab | 2011-10-07 20:03:39 +0000 | [diff] [blame] | 258 | uint32_t leftValue = MAX3((*lhs)->fMaxMismatchR, |
| 259 | (*lhs)->fMaxMismatchG, |
| 260 | (*lhs)->fMaxMismatchB); |
| 261 | uint32_t rightValue = MAX3((*rhs)->fMaxMismatchR, |
| 262 | (*rhs)->fMaxMismatchG, |
| 263 | (*rhs)->fMaxMismatchB); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 264 | if (leftValue < rightValue) { |
| 265 | return 1; |
| 266 | } |
| 267 | if (rightValue < leftValue) { |
| 268 | return -1; |
| 269 | } |
| 270 | return compare_diff_mean_mismatches(lhs, rhs); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | |
| 275 | /// Parameterized routine to compute the color of a pixel in a difference image. |
| 276 | typedef SkPMColor (*DiffMetricProc)(SkPMColor, SkPMColor); |
| 277 | |
caryclark@google.com | 3dd4591 | 2012-06-06 12:11:10 +0000 | [diff] [blame^] | 278 | #if 0 // UNUSED |
tomhudson@google.com | 8b08c3e | 2011-11-30 17:01:00 +0000 | [diff] [blame] | 279 | static void expand_and_copy (int width, int height, SkBitmap** dest) { |
| 280 | SkBitmap* temp = new SkBitmap (); |
| 281 | temp->reset(); |
| 282 | temp->setConfig((*dest)->config(), width, height); |
| 283 | temp->allocPixels(); |
| 284 | (*dest)->copyPixelsTo(temp->getPixels(), temp->getSize(), |
| 285 | temp->rowBytes()); |
| 286 | *dest = temp; |
| 287 | } |
caryclark@google.com | 3dd4591 | 2012-06-06 12:11:10 +0000 | [diff] [blame^] | 288 | #endif |
tomhudson@google.com | 8b08c3e | 2011-11-30 17:01:00 +0000 | [diff] [blame] | 289 | |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 290 | /// Returns true if the two buffers passed in are both non-NULL, and include |
| 291 | /// exactly the same byte values (and identical lengths). |
| 292 | static bool are_buffers_equal(SkData* skdata1, SkData* skdata2) { |
| 293 | if ((NULL == skdata1) || (NULL == skdata2)) { |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 294 | return false; |
| 295 | } |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 296 | if (skdata1->size() != skdata2->size()) { |
| 297 | return false; |
| 298 | } |
| 299 | return (0 == memcmp(skdata1->data(), skdata2->data(), skdata1->size())); |
| 300 | } |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 301 | |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 302 | /// Reads the file at the given path and returns its complete contents as an |
| 303 | /// SkData object (or returns NULL on error). |
| 304 | static SkData* read_file(const char* file_path) { |
| 305 | SkFILEStream fileStream(file_path); |
| 306 | if (!fileStream.isValid()) { |
| 307 | SkDebugf("WARNING: could not open file <%s> for reading\n", file_path); |
| 308 | return NULL; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 309 | } |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 310 | size_t bytesInFile = fileStream.getLength(); |
| 311 | size_t bytesLeftToRead = bytesInFile; |
| 312 | |
| 313 | void* bufferStart = sk_malloc_throw(bytesInFile); |
| 314 | char* bufferPointer = (char*)bufferStart; |
| 315 | while (bytesLeftToRead > 0) { |
| 316 | size_t bytesReadThisTime = fileStream.read( |
| 317 | bufferPointer, bytesLeftToRead); |
| 318 | if (0 == bytesReadThisTime) { |
| 319 | SkDebugf("WARNING: error reading from <%s>\n", file_path); |
| 320 | sk_free(bufferStart); |
| 321 | return NULL; |
| 322 | } |
| 323 | bytesLeftToRead -= bytesReadThisTime; |
| 324 | bufferPointer += bytesReadThisTime; |
| 325 | } |
| 326 | return SkData::NewFromMalloc(bufferStart, bytesInFile); |
| 327 | } |
| 328 | |
| 329 | /// Decodes binary contents of baseFile and comparisonFile into |
| 330 | /// diffRecord->fBaseBitmap and diffRecord->fComparisonBitmap. |
| 331 | /// Returns true if that succeeds. |
| 332 | static bool get_bitmaps (SkData* baseFileContents, |
| 333 | SkData* comparisonFileContents, |
| 334 | DiffRecord* diffRecord) { |
| 335 | SkMemoryStream compareStream(comparisonFileContents->data(), |
| 336 | comparisonFileContents->size()); |
| 337 | SkMemoryStream baseStream(baseFileContents->data(), |
| 338 | baseFileContents->size()); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 339 | |
| 340 | SkImageDecoder* codec = SkImageDecoder::Factory(&baseStream); |
| 341 | if (NULL == codec) { |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 342 | SkDebugf("ERROR: no codec found for basePath <%s>\n", |
tomhudson@google.com | 4e30598 | 2011-07-13 17:42:46 +0000 | [diff] [blame] | 343 | diffRecord->fBasePath.c_str()); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 344 | return false; |
| 345 | } |
| 346 | |
tomhudson@google.com | 8b08c3e | 2011-11-30 17:01:00 +0000 | [diff] [blame] | 347 | // In debug, the DLL will automatically be unloaded when this is deleted, |
| 348 | // but that shouldn't be a problem in release mode. |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 349 | SkAutoTDelete<SkImageDecoder> ad(codec); |
| 350 | |
| 351 | baseStream.rewind(); |
tomhudson@google.com | 9b540ce | 2011-08-02 14:10:04 +0000 | [diff] [blame] | 352 | if (!codec->decode(&baseStream, diffRecord->fBaseBitmap, |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 353 | SkBitmap::kARGB_8888_Config, |
| 354 | SkImageDecoder::kDecodePixels_Mode)) { |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 355 | SkDebugf("ERROR: codec failed for basePath <%s>\n", |
tomhudson@google.com | 4e30598 | 2011-07-13 17:42:46 +0000 | [diff] [blame] | 356 | diffRecord->fBasePath.c_str()); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 357 | return false; |
| 358 | } |
| 359 | |
tomhudson@google.com | 9b540ce | 2011-08-02 14:10:04 +0000 | [diff] [blame] | 360 | diffRecord->fBaseWidth = diffRecord->fBaseBitmap->width(); |
| 361 | diffRecord->fBaseHeight = diffRecord->fBaseBitmap->height(); |
| 362 | |
| 363 | if (!codec->decode(&compareStream, diffRecord->fComparisonBitmap, |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 364 | SkBitmap::kARGB_8888_Config, |
| 365 | SkImageDecoder::kDecodePixels_Mode)) { |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 366 | SkDebugf("ERROR: codec failed for comparisonPath <%s>\n", |
tomhudson@google.com | 4e30598 | 2011-07-13 17:42:46 +0000 | [diff] [blame] | 367 | diffRecord->fComparisonPath.c_str()); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 368 | return false; |
| 369 | } |
| 370 | |
| 371 | return true; |
| 372 | } |
| 373 | |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 374 | static bool get_bitmap_height_width(const SkString& path, |
| 375 | int *height, int *width) { |
| 376 | SkFILEStream stream(path.c_str()); |
| 377 | if (!stream.isValid()) { |
| 378 | SkDebugf("ERROR: couldn't open file <%s>\n", |
| 379 | path.c_str()); |
| 380 | return false; |
| 381 | } |
| 382 | |
| 383 | SkImageDecoder* codec = SkImageDecoder::Factory(&stream); |
| 384 | if (NULL == codec) { |
| 385 | SkDebugf("ERROR: no codec found for <%s>\n", |
| 386 | path.c_str()); |
| 387 | return false; |
| 388 | } |
| 389 | |
| 390 | SkAutoTDelete<SkImageDecoder> ad(codec); |
| 391 | SkBitmap bm; |
| 392 | |
| 393 | stream.rewind(); |
| 394 | if (!codec->decode(&stream, &bm, |
| 395 | SkBitmap::kARGB_8888_Config, |
| 396 | SkImageDecoder::kDecodePixels_Mode)) { |
| 397 | SkDebugf("ERROR: codec failed for <%s>\n", |
| 398 | path.c_str()); |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | *height = bm.height(); |
| 403 | *width = bm.width(); |
| 404 | |
| 405 | return true; |
| 406 | } |
| 407 | |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 408 | // from gm - thanks to PNG, we need to force all pixels 100% opaque |
| 409 | static void force_all_opaque(const SkBitmap& bitmap) { |
| 410 | SkAutoLockPixels lock(bitmap); |
| 411 | for (int y = 0; y < bitmap.height(); y++) { |
| 412 | for (int x = 0; x < bitmap.width(); x++) { |
| 413 | *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | // from gm |
tomhudson@google.com | 9b540ce | 2011-08-02 14:10:04 +0000 | [diff] [blame] | 419 | static bool write_bitmap(const SkString& path, const SkBitmap* bitmap) { |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 420 | SkBitmap copy; |
tomhudson@google.com | 9b540ce | 2011-08-02 14:10:04 +0000 | [diff] [blame] | 421 | bitmap->copyTo(©, SkBitmap::kARGB_8888_Config); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 422 | force_all_opaque(copy); |
| 423 | return SkImageEncoder::EncodeFile(path.c_str(), copy, |
| 424 | SkImageEncoder::kPNG_Type, 100); |
| 425 | } |
| 426 | |
| 427 | // from gm |
| 428 | static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) { |
| 429 | int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1); |
| 430 | int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1); |
| 431 | int db = SkGetPackedB32(c0) - SkGetPackedB32(c1); |
| 432 | |
| 433 | return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db)); |
| 434 | } |
| 435 | |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 436 | static inline bool colors_match_thresholded(SkPMColor c0, SkPMColor c1, |
| 437 | const int threshold) { |
| 438 | int da = SkGetPackedA32(c0) - SkGetPackedA32(c1); |
| 439 | int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1); |
| 440 | int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1); |
| 441 | int db = SkGetPackedB32(c0) - SkGetPackedB32(c1); |
| 442 | |
| 443 | return ((SkAbs32(da) <= threshold) && |
| 444 | (SkAbs32(dr) <= threshold) && |
| 445 | (SkAbs32(dg) <= threshold) && |
| 446 | (SkAbs32(db) <= threshold)); |
| 447 | } |
| 448 | |
| 449 | // based on gm |
epoger@google.com | 6600852 | 2012-05-16 17:40:57 +0000 | [diff] [blame] | 450 | // Postcondition: when we exit this method, dr->fResult should have some value |
| 451 | // other than kUnknown. |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 452 | static void compute_diff(DiffRecord* dr, |
| 453 | DiffMetricProc diffFunction, |
| 454 | const int colorThreshold) { |
epoger@google.com | 25d961c | 2012-02-02 20:50:36 +0000 | [diff] [blame] | 455 | SkAutoLockPixels alpDiff(*dr->fDifferenceBitmap); |
| 456 | SkAutoLockPixels alpWhite(*dr->fWhiteBitmap); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 457 | |
tomhudson@google.com | 9b540ce | 2011-08-02 14:10:04 +0000 | [diff] [blame] | 458 | const int w = dr->fComparisonBitmap->width(); |
| 459 | const int h = dr->fComparisonBitmap->height(); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 460 | int mismatchedPixels = 0; |
| 461 | int totalMismatchR = 0; |
| 462 | int totalMismatchG = 0; |
| 463 | int totalMismatchB = 0; |
tomhudson@google.com | 8b08c3e | 2011-11-30 17:01:00 +0000 | [diff] [blame] | 464 | |
| 465 | if (w != dr->fBaseWidth || h != dr->fBaseHeight) { |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 466 | dr->fResult = kDifferentSizes; |
tomhudson@google.com | 8b08c3e | 2011-11-30 17:01:00 +0000 | [diff] [blame] | 467 | return; |
| 468 | } |
tomhudson@google.com | 5b32529 | 2011-05-24 19:41:13 +0000 | [diff] [blame] | 469 | // Accumulate fractionally different pixels, then divide out |
| 470 | // # of pixels at the end. |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 471 | dr->fWeightedFraction = 0; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 472 | for (int y = 0; y < h; y++) { |
| 473 | for (int x = 0; x < w; x++) { |
tomhudson@google.com | 9b540ce | 2011-08-02 14:10:04 +0000 | [diff] [blame] | 474 | SkPMColor c0 = *dr->fBaseBitmap->getAddr32(x, y); |
| 475 | SkPMColor c1 = *dr->fComparisonBitmap->getAddr32(x, y); |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 476 | SkPMColor trueDifference = compute_diff_pmcolor(c0, c1); |
| 477 | SkPMColor outputDifference = diffFunction(c0, c1); |
| 478 | uint32_t thisR = SkGetPackedR32(trueDifference); |
| 479 | uint32_t thisG = SkGetPackedG32(trueDifference); |
| 480 | uint32_t thisB = SkGetPackedB32(trueDifference); |
tomhudson@google.com | 5b32529 | 2011-05-24 19:41:13 +0000 | [diff] [blame] | 481 | totalMismatchR += thisR; |
| 482 | totalMismatchG += thisG; |
| 483 | totalMismatchB += thisB; |
| 484 | // In HSV, value is defined as max RGB component. |
| 485 | int value = MAX3(thisR, thisG, thisB); |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 486 | dr->fWeightedFraction += ((float) value) / 255; |
tomhudson@google.com | 5b32529 | 2011-05-24 19:41:13 +0000 | [diff] [blame] | 487 | if (thisR > dr->fMaxMismatchR) { |
| 488 | dr->fMaxMismatchR = thisR; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 489 | } |
tomhudson@google.com | 5b32529 | 2011-05-24 19:41:13 +0000 | [diff] [blame] | 490 | if (thisG > dr->fMaxMismatchG) { |
| 491 | dr->fMaxMismatchG = thisG; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 492 | } |
tomhudson@google.com | 5b32529 | 2011-05-24 19:41:13 +0000 | [diff] [blame] | 493 | if (thisB > dr->fMaxMismatchB) { |
| 494 | dr->fMaxMismatchB = thisB; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 495 | } |
| 496 | if (!colors_match_thresholded(c0, c1, colorThreshold)) { |
| 497 | mismatchedPixels++; |
tomhudson@google.com | 9b540ce | 2011-08-02 14:10:04 +0000 | [diff] [blame] | 498 | *dr->fDifferenceBitmap->getAddr32(x, y) = outputDifference; |
epoger@google.com | 25d961c | 2012-02-02 20:50:36 +0000 | [diff] [blame] | 499 | *dr->fWhiteBitmap->getAddr32(x, y) = PMCOLOR_WHITE; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 500 | } else { |
tomhudson@google.com | 9b540ce | 2011-08-02 14:10:04 +0000 | [diff] [blame] | 501 | *dr->fDifferenceBitmap->getAddr32(x, y) = 0; |
epoger@google.com | 25d961c | 2012-02-02 20:50:36 +0000 | [diff] [blame] | 502 | *dr->fWhiteBitmap->getAddr32(x, y) = PMCOLOR_BLACK; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | } |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 506 | if (0 == mismatchedPixels) { |
| 507 | dr->fResult = kEqualPixels; |
| 508 | return; |
| 509 | } |
| 510 | dr->fResult = kDifferentPixels; |
tomhudson@google.com | 5b32529 | 2011-05-24 19:41:13 +0000 | [diff] [blame] | 511 | int pixelCount = w * h; |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 512 | dr->fFractionDifference = ((float) mismatchedPixels) / pixelCount; |
| 513 | dr->fWeightedFraction /= pixelCount; |
tomhudson@google.com | 5b32529 | 2011-05-24 19:41:13 +0000 | [diff] [blame] | 514 | dr->fAverageMismatchR = ((float) totalMismatchR) / pixelCount; |
| 515 | dr->fAverageMismatchG = ((float) totalMismatchG) / pixelCount; |
| 516 | dr->fAverageMismatchB = ((float) totalMismatchB) / pixelCount; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 517 | } |
| 518 | |
epoger@google.com | 25d961c | 2012-02-02 20:50:36 +0000 | [diff] [blame] | 519 | static SkString filename_to_derived_filename (const SkString& filename, |
| 520 | const char *suffix) { |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 521 | SkString diffName (filename); |
| 522 | const char* cstring = diffName.c_str(); |
| 523 | int dotOffset = strrchr(cstring, '.') - cstring; |
| 524 | diffName.remove(dotOffset, diffName.size() - dotOffset); |
epoger@google.com | 25d961c | 2012-02-02 20:50:36 +0000 | [diff] [blame] | 525 | diffName.append(suffix); |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 526 | return diffName; |
| 527 | } |
| 528 | |
epoger@google.com | 25d961c | 2012-02-02 20:50:36 +0000 | [diff] [blame] | 529 | /// Given a image filename, returns the name of the file containing the |
| 530 | /// associated difference image. |
| 531 | static SkString filename_to_diff_filename (const SkString& filename) { |
| 532 | return filename_to_derived_filename(filename, "-diff.png"); |
| 533 | } |
| 534 | |
| 535 | /// Given a image filename, returns the name of the file containing the |
| 536 | /// "white" difference image. |
| 537 | static SkString filename_to_white_filename (const SkString& filename) { |
| 538 | return filename_to_derived_filename(filename, "-white.png"); |
| 539 | } |
| 540 | |
tomhudson@google.com | 9b540ce | 2011-08-02 14:10:04 +0000 | [diff] [blame] | 541 | static void release_bitmaps(DiffRecord* drp) { |
| 542 | delete drp->fBaseBitmap; |
| 543 | drp->fBaseBitmap = NULL; |
| 544 | delete drp->fComparisonBitmap; |
| 545 | drp->fComparisonBitmap = NULL; |
| 546 | delete drp->fDifferenceBitmap; |
| 547 | drp->fDifferenceBitmap = NULL; |
epoger@google.com | 25d961c | 2012-02-02 20:50:36 +0000 | [diff] [blame] | 548 | delete drp->fWhiteBitmap; |
| 549 | drp->fWhiteBitmap = NULL; |
tomhudson@google.com | 9b540ce | 2011-08-02 14:10:04 +0000 | [diff] [blame] | 550 | } |
| 551 | |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 552 | |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 553 | /// If outputDir.isEmpty(), don't write out diff files. |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 554 | static void create_and_write_diff_image(DiffRecord* drp, |
| 555 | DiffMetricProc dmp, |
| 556 | const int colorThreshold, |
| 557 | const SkString& outputDir, |
| 558 | const SkString& filename) { |
tomhudson@google.com | 9b540ce | 2011-08-02 14:10:04 +0000 | [diff] [blame] | 559 | const int w = drp->fBaseWidth; |
| 560 | const int h = drp->fBaseHeight; |
| 561 | drp->fDifferenceBitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 562 | drp->fDifferenceBitmap->allocPixels(); |
epoger@google.com | 25d961c | 2012-02-02 20:50:36 +0000 | [diff] [blame] | 563 | drp->fWhiteBitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 564 | drp->fWhiteBitmap->allocPixels(); |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 565 | |
epoger@google.com | 6600852 | 2012-05-16 17:40:57 +0000 | [diff] [blame] | 566 | SkASSERT(kUnknown == drp->fResult); |
| 567 | compute_diff(drp, dmp, colorThreshold); |
| 568 | SkASSERT(kUnknown != drp->fResult); |
| 569 | |
| 570 | if ((kDifferentPixels == drp->fResult) && !outputDir.isEmpty()) { |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 571 | SkString differencePath (outputDir); |
| 572 | differencePath.append(filename_to_diff_filename(filename)); |
| 573 | write_bitmap(differencePath, drp->fDifferenceBitmap); |
| 574 | SkString whitePath (outputDir); |
| 575 | whitePath.append(filename_to_white_filename(filename)); |
| 576 | write_bitmap(whitePath, drp->fWhiteBitmap); |
| 577 | } |
epoger@google.com | 6600852 | 2012-05-16 17:40:57 +0000 | [diff] [blame] | 578 | |
tomhudson@google.com | 9b540ce | 2011-08-02 14:10:04 +0000 | [diff] [blame] | 579 | release_bitmaps(drp); |
tomhudson@google.com | 4e30598 | 2011-07-13 17:42:46 +0000 | [diff] [blame] | 580 | } |
| 581 | |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 582 | /// Returns true if string contains any of these substrings. |
| 583 | static bool string_contains_any_of(const SkString& string, |
| 584 | const StringArray& substrings) { |
| 585 | for (int i = 0; i < substrings.count(); i++) { |
| 586 | if (string.contains(substrings[i]->c_str())) { |
| 587 | return true; |
| 588 | } |
| 589 | } |
| 590 | return false; |
| 591 | } |
| 592 | |
| 593 | /// Iterate over dir and get all files that: |
| 594 | /// - match any of the substrings in matchSubstrings, but... |
| 595 | /// - DO NOT match any of the substrings in nomatchSubstrings |
| 596 | /// Returns the list of files in *files. |
| 597 | static void get_file_list(const SkString& dir, |
| 598 | const StringArray& matchSubstrings, |
| 599 | const StringArray& nomatchSubstrings, |
| 600 | FileArray *files) { |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 601 | SkOSFile::Iter it(dir.c_str()); |
| 602 | SkString filename; |
| 603 | while (it.next(&filename)) { |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 604 | if (string_contains_any_of(filename, matchSubstrings) && |
| 605 | !string_contains_any_of(filename, nomatchSubstrings)) { |
| 606 | files->push(new SkString(filename)); |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 607 | } |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
| 611 | static void release_file_list(FileArray *files) { |
| 612 | files->deleteAll(); |
| 613 | } |
| 614 | |
| 615 | /// Comparison routines for qsort, sort by file names. |
| 616 | static int compare_file_name_metrics(SkString **lhs, SkString **rhs) { |
| 617 | return strcmp((*lhs)->c_str(), (*rhs)->c_str()); |
| 618 | } |
| 619 | |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 620 | /// Creates difference images, returns the number that have a 0 metric. |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 621 | /// If outputDir.isEmpty(), don't write out diff files. |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 622 | static void create_diff_images (DiffMetricProc dmp, |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 623 | const int colorThreshold, |
| 624 | RecordArray* differences, |
| 625 | const SkString& baseDir, |
| 626 | const SkString& comparisonDir, |
| 627 | const SkString& outputDir, |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 628 | const StringArray& matchSubstrings, |
| 629 | const StringArray& nomatchSubstrings, |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 630 | DiffSummary* summary) { |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 631 | SkASSERT(!baseDir.isEmpty()); |
| 632 | SkASSERT(!comparisonDir.isEmpty()); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 633 | |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 634 | FileArray baseFiles; |
| 635 | FileArray comparisonFiles; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 636 | |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 637 | get_file_list(baseDir, matchSubstrings, nomatchSubstrings, &baseFiles); |
| 638 | get_file_list(comparisonDir, matchSubstrings, nomatchSubstrings, |
| 639 | &comparisonFiles); |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 640 | |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 641 | if (!baseFiles.isEmpty()) { |
reed@google.com | c7a67cb | 2012-05-07 14:52:12 +0000 | [diff] [blame] | 642 | qsort(baseFiles.begin(), baseFiles.count(), sizeof(SkString*), |
| 643 | SkCastForQSort(compare_file_name_metrics)); |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 644 | } |
| 645 | if (!comparisonFiles.isEmpty()) { |
reed@google.com | c7a67cb | 2012-05-07 14:52:12 +0000 | [diff] [blame] | 646 | qsort(comparisonFiles.begin(), comparisonFiles.count(), |
| 647 | sizeof(SkString*), SkCastForQSort(compare_file_name_metrics)); |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 648 | } |
epoger@google.com | 6600852 | 2012-05-16 17:40:57 +0000 | [diff] [blame] | 649 | |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 650 | int i = 0; |
| 651 | int j = 0; |
| 652 | |
| 653 | while (i < baseFiles.count() && |
| 654 | j < comparisonFiles.count()) { |
| 655 | |
tomhudson@google.com | 4e30598 | 2011-07-13 17:42:46 +0000 | [diff] [blame] | 656 | SkString basePath (baseDir); |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 657 | basePath.append(*baseFiles[i]); |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 658 | SkString comparisonPath (comparisonDir); |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 659 | comparisonPath.append(*comparisonFiles[j]); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 660 | |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 661 | DiffRecord *drp = NULL; |
| 662 | int v = strcmp(baseFiles[i]->c_str(), |
| 663 | comparisonFiles[j]->c_str()); |
| 664 | |
| 665 | if (v < 0) { |
| 666 | // in baseDir, but not in comparisonDir |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 667 | drp = new DiffRecord(*baseFiles[i], basePath, comparisonPath, |
| 668 | kComparisonMissing); |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 669 | ++i; |
| 670 | } else if (v > 0) { |
| 671 | // in comparisonDir, but not in baseDir |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 672 | drp = new DiffRecord(*comparisonFiles[j], basePath, comparisonPath, |
| 673 | kBaseMissing); |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 674 | ++j; |
| 675 | } else { |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 676 | // Found the same filename in both baseDir and comparisonDir. |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 677 | drp = new DiffRecord(*baseFiles[i], basePath, comparisonPath); |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 678 | SkASSERT(kUnknown == drp->fResult); |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 679 | |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 680 | SkData* baseFileBits; |
| 681 | SkData* comparisonFileBits; |
| 682 | if (NULL == (baseFileBits = read_file(basePath.c_str()))) { |
| 683 | SkDebugf("WARNING: couldn't read base file <%s>\n", |
| 684 | basePath.c_str()); |
| 685 | drp->fResult = kBaseMissing; |
| 686 | } else if (NULL == (comparisonFileBits = read_file( |
| 687 | comparisonPath.c_str()))) { |
| 688 | SkDebugf("WARNING: couldn't read comparison file <%s>\n", |
| 689 | comparisonPath.c_str()); |
| 690 | drp->fResult = kComparisonMissing; |
| 691 | } else { |
| 692 | if (are_buffers_equal(baseFileBits, comparisonFileBits)) { |
| 693 | drp->fResult = kEqualBits; |
| 694 | } else if (get_bitmaps(baseFileBits, comparisonFileBits, drp)) { |
| 695 | create_and_write_diff_image(drp, dmp, colorThreshold, |
| 696 | outputDir, *baseFiles[i]); |
| 697 | } else { |
| 698 | drp->fResult = kDifferentOther; |
| 699 | } |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 700 | } |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 701 | if (baseFileBits) { |
| 702 | baseFileBits->unref(); |
| 703 | } |
| 704 | if (comparisonFileBits) { |
| 705 | comparisonFileBits->unref(); |
| 706 | } |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 707 | ++i; |
| 708 | ++j; |
| 709 | } |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 710 | SkASSERT(kUnknown != drp->fResult); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 711 | differences->push(drp); |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 712 | summary->add(drp); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 713 | } |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 714 | |
| 715 | for (; i < baseFiles.count(); ++i) { |
| 716 | // files only in baseDir |
| 717 | SkString basePath (baseDir); |
| 718 | basePath.append(*baseFiles[i]); |
| 719 | SkString comparisonPath; |
| 720 | DiffRecord *drp = new DiffRecord(*baseFiles[i], basePath, |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 721 | comparisonPath, kComparisonMissing); |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 722 | differences->push(drp); |
| 723 | summary->add(drp); |
| 724 | } |
| 725 | |
| 726 | for (; j < comparisonFiles.count(); ++j) { |
| 727 | // files only in comparisonDir |
| 728 | SkString basePath; |
| 729 | SkString comparisonPath(comparisonDir); |
| 730 | comparisonPath.append(*comparisonFiles[j]); |
| 731 | DiffRecord *drp = new DiffRecord(*comparisonFiles[j], basePath, |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 732 | comparisonPath, kBaseMissing); |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 733 | differences->push(drp); |
| 734 | summary->add(drp); |
| 735 | } |
| 736 | |
| 737 | release_file_list(&baseFiles); |
| 738 | release_file_list(&comparisonFiles); |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 739 | } |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 740 | |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 741 | /// Make layout more consistent by scaling image to 240 height, 360 width, |
| 742 | /// or natural size, whichever is smallest. |
tomhudson@google.com | 9b540ce | 2011-08-02 14:10:04 +0000 | [diff] [blame] | 743 | static int compute_image_height (int height, int width) { |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 744 | int retval = 240; |
tomhudson@google.com | 9b540ce | 2011-08-02 14:10:04 +0000 | [diff] [blame] | 745 | if (height < retval) { |
| 746 | retval = height; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 747 | } |
tomhudson@google.com | 9b540ce | 2011-08-02 14:10:04 +0000 | [diff] [blame] | 748 | float scale = (float) retval / height; |
| 749 | if (width * scale > 360) { |
| 750 | scale = (float) 360 / width; |
bsalomon@google.com | 8e06dab | 2011-10-07 20:03:39 +0000 | [diff] [blame] | 751 | retval = static_cast<int>(height * scale); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 752 | } |
| 753 | return retval; |
| 754 | } |
| 755 | |
epoger@google.com | 25d961c | 2012-02-02 20:50:36 +0000 | [diff] [blame] | 756 | static void print_table_header (SkFILEWStream* stream, |
| 757 | const int matchCount, |
| 758 | const int colorThreshold, |
| 759 | const RecordArray& differences, |
| 760 | const SkString &baseDir, |
epoger@google.com | a2b793c | 2012-05-15 14:58:53 +0000 | [diff] [blame] | 761 | const SkString &comparisonDir, |
| 762 | bool doOutputDate=false) { |
epoger@google.com | 25d961c | 2012-02-02 20:50:36 +0000 | [diff] [blame] | 763 | stream->writeText("<table>\n"); |
| 764 | stream->writeText("<tr><th>"); |
epoger@google.com | a2b793c | 2012-05-15 14:58:53 +0000 | [diff] [blame] | 765 | if (doOutputDate) { |
| 766 | SkTime::DateTime dt; |
| 767 | SkTime::GetDateTime(&dt); |
| 768 | stream->writeText("SkDiff run at "); |
| 769 | stream->writeDecAsText(dt.fHour); |
| 770 | stream->writeText(":"); |
| 771 | if (dt.fMinute < 10) { |
| 772 | stream->writeText("0"); |
| 773 | } |
| 774 | stream->writeDecAsText(dt.fMinute); |
| 775 | stream->writeText(":"); |
| 776 | if (dt.fSecond < 10) { |
| 777 | stream->writeText("0"); |
| 778 | } |
| 779 | stream->writeDecAsText(dt.fSecond); |
| 780 | stream->writeText("<br>"); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 781 | } |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 782 | stream->writeDecAsText(matchCount); |
| 783 | stream->writeText(" of "); |
| 784 | stream->writeDecAsText(differences.count()); |
| 785 | stream->writeText(" images matched "); |
| 786 | if (colorThreshold == 0) { |
| 787 | stream->writeText("exactly"); |
| 788 | } else { |
| 789 | stream->writeText("within "); |
| 790 | stream->writeDecAsText(colorThreshold); |
| 791 | stream->writeText(" color units per component"); |
| 792 | } |
| 793 | stream->writeText(".<br>"); |
epoger@google.com | 25d961c | 2012-02-02 20:50:36 +0000 | [diff] [blame] | 794 | stream->writeText("</th>\n<th>"); |
| 795 | stream->writeText("every different pixel shown in white"); |
| 796 | stream->writeText("</th>\n<th>"); |
| 797 | stream->writeText("color difference at each pixel"); |
| 798 | stream->writeText("</th>\n<th>"); |
| 799 | stream->writeText(baseDir.c_str()); |
| 800 | stream->writeText("</th>\n<th>"); |
| 801 | stream->writeText(comparisonDir.c_str()); |
| 802 | stream->writeText("</th>\n"); |
| 803 | stream->writeText("</tr>\n"); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | static void print_pixel_count (SkFILEWStream* stream, |
| 807 | const DiffRecord& diff) { |
| 808 | stream->writeText("<br>("); |
bsalomon@google.com | 8e06dab | 2011-10-07 20:03:39 +0000 | [diff] [blame] | 809 | stream->writeDecAsText(static_cast<int>(diff.fFractionDifference * |
| 810 | diff.fBaseWidth * |
| 811 | diff.fBaseHeight)); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 812 | stream->writeText(" pixels)"); |
tomhudson@google.com | 5b32529 | 2011-05-24 19:41:13 +0000 | [diff] [blame] | 813 | /* |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 814 | stream->writeDecAsText(diff.fWeightedFraction * |
tomhudson@google.com | 9b540ce | 2011-08-02 14:10:04 +0000 | [diff] [blame] | 815 | diff.fBaseWidth * |
| 816 | diff.fBaseHeight); |
tomhudson@google.com | 5b32529 | 2011-05-24 19:41:13 +0000 | [diff] [blame] | 817 | stream->writeText(" weighted pixels)"); |
| 818 | */ |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | static void print_label_cell (SkFILEWStream* stream, |
| 822 | const DiffRecord& diff) { |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 823 | char metricBuf [20]; |
| 824 | |
| 825 | stream->writeText("<td><b>"); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 826 | stream->writeText(diff.fFilename.c_str()); |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 827 | stream->writeText("</b><br>"); |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 828 | switch (diff.fResult) { |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 829 | case kEqualBits: |
| 830 | SkDEBUGFAIL("should not encounter DiffRecord with kEqualBits here"); |
| 831 | return; |
| 832 | case kEqualPixels: |
| 833 | SkDEBUGFAIL("should not encounter DiffRecord with kEqualPixels here"); |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 834 | return; |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 835 | case kDifferentSizes: |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 836 | stream->writeText("Image sizes differ</td>"); |
| 837 | return; |
| 838 | case kDifferentPixels: |
| 839 | sprintf(metricBuf, "%12.4f%%", 100 * diff.fFractionDifference); |
| 840 | stream->writeText(metricBuf); |
| 841 | stream->writeText(" of pixels differ"); |
| 842 | stream->writeText("\n ("); |
| 843 | sprintf(metricBuf, "%12.4f%%", 100 * diff.fWeightedFraction); |
| 844 | stream->writeText(metricBuf); |
| 845 | stream->writeText(" weighted)"); |
| 846 | // Write the actual number of pixels that differ if it's < 1% |
| 847 | if (diff.fFractionDifference < 0.01) { |
| 848 | print_pixel_count(stream, diff); |
| 849 | } |
| 850 | stream->writeText("<br>Average color mismatch "); |
| 851 | stream->writeDecAsText(static_cast<int>(MAX3(diff.fAverageMismatchR, |
| 852 | diff.fAverageMismatchG, |
| 853 | diff.fAverageMismatchB))); |
| 854 | stream->writeText("<br>Max color mismatch "); |
| 855 | stream->writeDecAsText(MAX3(diff.fMaxMismatchR, |
| 856 | diff.fMaxMismatchG, |
| 857 | diff.fMaxMismatchB)); |
tomhudson@google.com | 8b08c3e | 2011-11-30 17:01:00 +0000 | [diff] [blame] | 858 | stream->writeText("</td>"); |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 859 | break; |
| 860 | case kDifferentOther: |
| 861 | stream->writeText("Files differ; unable to parse one or both files</td>"); |
| 862 | return; |
| 863 | case kBaseMissing: |
| 864 | stream->writeText("Missing from baseDir</td>"); |
| 865 | return; |
| 866 | case kComparisonMissing: |
| 867 | stream->writeText("Missing from comparisonDir</td>"); |
tomhudson@google.com | 8b08c3e | 2011-11-30 17:01:00 +0000 | [diff] [blame] | 868 | return; |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 869 | default: |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 870 | SkDEBUGFAIL("encountered DiffRecord with unknown result type"); |
| 871 | return; |
tomhudson@google.com | 8b08c3e | 2011-11-30 17:01:00 +0000 | [diff] [blame] | 872 | } |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | static void print_image_cell (SkFILEWStream* stream, |
tomhudson@google.com | 4e30598 | 2011-07-13 17:42:46 +0000 | [diff] [blame] | 876 | const SkString& path, |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 877 | int height) { |
| 878 | stream->writeText("<td><a href=\""); |
tomhudson@google.com | 4e30598 | 2011-07-13 17:42:46 +0000 | [diff] [blame] | 879 | stream->writeText(path.c_str()); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 880 | stream->writeText("\"><img src=\""); |
tomhudson@google.com | 4e30598 | 2011-07-13 17:42:46 +0000 | [diff] [blame] | 881 | stream->writeText(path.c_str()); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 882 | stream->writeText("\" height=\""); |
| 883 | stream->writeDecAsText(height); |
| 884 | stream->writeText("px\"></a></td>"); |
| 885 | } |
| 886 | |
caryclark@google.com | 3dd4591 | 2012-06-06 12:11:10 +0000 | [diff] [blame^] | 887 | #if 0 // UNUSED |
epoger@google.com | 01f7870 | 2012-04-12 16:32:04 +0000 | [diff] [blame] | 888 | static void print_text_cell (SkFILEWStream* stream, const char* text) { |
| 889 | stream->writeText("<td align=center>"); |
| 890 | if (NULL != text) { |
| 891 | stream->writeText(text); |
| 892 | } |
| 893 | stream->writeText("</td>"); |
| 894 | } |
caryclark@google.com | 3dd4591 | 2012-06-06 12:11:10 +0000 | [diff] [blame^] | 895 | #endif |
epoger@google.com | 01f7870 | 2012-04-12 16:32:04 +0000 | [diff] [blame] | 896 | |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 897 | static void print_diff_with_missing_file(SkFILEWStream* stream, |
| 898 | DiffRecord& diff, |
| 899 | const SkString& relativePath) { |
| 900 | stream->writeText("<tr>\n"); |
| 901 | print_label_cell(stream, diff); |
| 902 | stream->writeText("<td>N/A</td>"); |
| 903 | stream->writeText("<td>N/A</td>"); |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 904 | if (kBaseMissing != diff.fResult) { |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 905 | int h, w; |
| 906 | if (!get_bitmap_height_width(diff.fBasePath, &h, &w)) { |
| 907 | stream->writeText("<td>N/A</td>"); |
| 908 | } else { |
| 909 | int height = compute_image_height(h, w); |
| 910 | if (!diff.fBasePath.startsWith(PATH_DIV_STR)) { |
| 911 | diff.fBasePath.prepend(relativePath); |
| 912 | } |
| 913 | print_image_cell(stream, diff.fBasePath, height); |
| 914 | } |
| 915 | } else { |
| 916 | stream->writeText("<td>N/A</td>"); |
| 917 | } |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 918 | if (kComparisonMissing != diff.fResult) { |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 919 | int h, w; |
| 920 | if (!get_bitmap_height_width(diff.fComparisonPath, &h, &w)) { |
| 921 | stream->writeText("<td>N/A</td>"); |
| 922 | } else { |
| 923 | int height = compute_image_height(h, w); |
| 924 | if (!diff.fComparisonPath.startsWith(PATH_DIV_STR)) { |
| 925 | diff.fComparisonPath.prepend(relativePath); |
| 926 | } |
| 927 | print_image_cell(stream, diff.fComparisonPath, height); |
| 928 | } |
| 929 | } else { |
| 930 | stream->writeText("<td>N/A</td>"); |
| 931 | } |
| 932 | stream->writeText("</tr>\n"); |
| 933 | stream->flush(); |
| 934 | } |
| 935 | |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 936 | static void print_diff_page (const int matchCount, |
| 937 | const int colorThreshold, |
| 938 | const RecordArray& differences, |
| 939 | const SkString& baseDir, |
| 940 | const SkString& comparisonDir, |
| 941 | const SkString& outputDir) { |
| 942 | |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 943 | SkASSERT(!baseDir.isEmpty()); |
| 944 | SkASSERT(!comparisonDir.isEmpty()); |
| 945 | SkASSERT(!outputDir.isEmpty()); |
| 946 | |
tomhudson@google.com | 5b32529 | 2011-05-24 19:41:13 +0000 | [diff] [blame] | 947 | SkString outputPath (outputDir); |
| 948 | outputPath.append("index.html"); |
| 949 | //SkFILEWStream outputStream ("index.html"); |
| 950 | SkFILEWStream outputStream (outputPath.c_str()); |
| 951 | |
tomhudson@google.com | 4e30598 | 2011-07-13 17:42:46 +0000 | [diff] [blame] | 952 | // Need to convert paths from relative-to-cwd to relative-to-outputDir |
tomhudson@google.com | 5b32529 | 2011-05-24 19:41:13 +0000 | [diff] [blame] | 953 | // FIXME this doesn't work if there are '..' inside the outputDir |
| 954 | unsigned int ui; |
| 955 | SkString relativePath; |
| 956 | for (ui = 0; ui < outputDir.size(); ui++) { |
bsalomon@google.com | 1a315fe | 2011-09-23 14:56:37 +0000 | [diff] [blame] | 957 | if (outputDir[ui] == PATH_DIV_CHAR) { |
| 958 | relativePath.append(".." PATH_DIV_STR); |
tomhudson@google.com | 5b32529 | 2011-05-24 19:41:13 +0000 | [diff] [blame] | 959 | } |
| 960 | } |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 961 | |
| 962 | outputStream.writeText("<html>\n<body>\n"); |
epoger@google.com | 25d961c | 2012-02-02 20:50:36 +0000 | [diff] [blame] | 963 | print_table_header(&outputStream, matchCount, colorThreshold, differences, |
| 964 | baseDir, comparisonDir); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 965 | int i; |
| 966 | for (i = 0; i < differences.count(); i++) { |
| 967 | DiffRecord* diff = differences[i]; |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 968 | |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 969 | switch (diff->fResult) { |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 970 | // Cases in which there is no diff to report. |
| 971 | case kEqualBits: |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 972 | case kEqualPixels: |
| 973 | continue; |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 974 | // Cases in which we want a detailed pixel diff. |
| 975 | case kDifferentPixels: |
| 976 | break; |
| 977 | // Cases in which the files differed, but we can't display the diff. |
| 978 | case kDifferentSizes: |
| 979 | case kDifferentOther: |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 980 | case kBaseMissing: |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 981 | case kComparisonMissing: |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 982 | print_diff_with_missing_file(&outputStream, *diff, relativePath); |
| 983 | continue; |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 984 | default: |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 985 | SkDEBUGFAIL("encountered DiffRecord with unknown result type"); |
| 986 | continue; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 987 | } |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 988 | |
bsalomon@google.com | 1a315fe | 2011-09-23 14:56:37 +0000 | [diff] [blame] | 989 | if (!diff->fBasePath.startsWith(PATH_DIV_STR)) { |
tomhudson@google.com | 4e30598 | 2011-07-13 17:42:46 +0000 | [diff] [blame] | 990 | diff->fBasePath.prepend(relativePath); |
| 991 | } |
bsalomon@google.com | 1a315fe | 2011-09-23 14:56:37 +0000 | [diff] [blame] | 992 | if (!diff->fComparisonPath.startsWith(PATH_DIV_STR)) { |
tomhudson@google.com | 4e30598 | 2011-07-13 17:42:46 +0000 | [diff] [blame] | 993 | diff->fComparisonPath.prepend(relativePath); |
| 994 | } |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 995 | |
tomhudson@google.com | 9b540ce | 2011-08-02 14:10:04 +0000 | [diff] [blame] | 996 | int height = compute_image_height(diff->fBaseHeight, diff->fBaseWidth); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 997 | outputStream.writeText("<tr>\n"); |
| 998 | print_label_cell(&outputStream, *diff); |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 999 | print_image_cell(&outputStream, |
| 1000 | filename_to_white_filename(diff->fFilename), height); |
| 1001 | print_image_cell(&outputStream, |
| 1002 | filename_to_diff_filename(diff->fFilename), height); |
epoger@google.com | 25d961c | 2012-02-02 20:50:36 +0000 | [diff] [blame] | 1003 | print_image_cell(&outputStream, diff->fBasePath, height); |
tomhudson@google.com | 4e30598 | 2011-07-13 17:42:46 +0000 | [diff] [blame] | 1004 | print_image_cell(&outputStream, diff->fComparisonPath, height); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 1005 | outputStream.writeText("</tr>\n"); |
| 1006 | outputStream.flush(); |
| 1007 | } |
| 1008 | outputStream.writeText("</table>\n"); |
| 1009 | outputStream.writeText("</body>\n</html>\n"); |
| 1010 | outputStream.flush(); |
| 1011 | } |
| 1012 | |
| 1013 | static void usage (char * argv0) { |
| 1014 | SkDebugf("Skia baseline image diff tool\n"); |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 1015 | SkDebugf("\n" |
| 1016 | "Usage: \n" |
| 1017 | " %s <baseDir> <comparisonDir> [outputDir] \n" |
epoger@google.com | a611c3e | 2012-05-18 20:10:06 +0000 | [diff] [blame] | 1018 | , argv0, argv0); |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 1019 | SkDebugf("\n" |
| 1020 | "Arguments: \n" |
| 1021 | " --nodiffs: don't write out image diffs or index.html, just generate \n" |
| 1022 | " report on stdout \n" |
| 1023 | " --threshold <n>: only report differences > n (per color channel) [default 0]\n" |
| 1024 | " --match: compare files whose filenames contain this substring; if \n" |
| 1025 | " unspecified, compare ALL files. \n" |
| 1026 | " this flag may be repeated to add more matching substrings. \n" |
| 1027 | " --nomatch: regardless of --match, DO NOT compare files whose filenames \n" |
| 1028 | " contain this substring. \n" |
| 1029 | " this flag may be repeated to add more forbidden substrings. \n" |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 1030 | " --sortbymismatch: sort by average color channel mismatch\n"); |
| 1031 | SkDebugf( |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 1032 | " --sortbymaxmismatch: sort by worst color channel mismatch;\n" |
| 1033 | " break ties with -sortbymismatch\n" |
| 1034 | " [default sort is by fraction of pixels mismatching]\n"); |
tomhudson@google.com | 5b32529 | 2011-05-24 19:41:13 +0000 | [diff] [blame] | 1035 | SkDebugf( |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 1036 | " --weighted: sort by # pixels different weighted by color difference\n"); |
| 1037 | SkDebugf( |
epoger@google.com | a611c3e | 2012-05-18 20:10:06 +0000 | [diff] [blame] | 1038 | " baseDir: directory to read baseline images from.\n"); |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 1039 | SkDebugf( |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 1040 | " comparisonDir: directory to read comparison images from\n"); |
| 1041 | SkDebugf( |
| 1042 | " outputDir: directory to write difference images and index.html to; \n" |
epoger@google.com | a611c3e | 2012-05-18 20:10:06 +0000 | [diff] [blame] | 1043 | " defaults to comparisonDir \n"); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | int main (int argc, char ** argv) { |
| 1047 | DiffMetricProc diffProc = compute_diff_pmcolor; |
reed@google.com | c7a67cb | 2012-05-07 14:52:12 +0000 | [diff] [blame] | 1048 | int (*sortProc)(const void*, const void*) = SkCastForQSort(compare_diff_metrics); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 1049 | |
| 1050 | // Maximum error tolerated in any one color channel in any one pixel before |
| 1051 | // a difference is reported. |
| 1052 | int colorThreshold = 0; |
| 1053 | SkString baseDir; |
| 1054 | SkString comparisonDir; |
| 1055 | SkString outputDir; |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 1056 | StringArray matchSubstrings; |
| 1057 | StringArray nomatchSubstrings; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 1058 | |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 1059 | bool generateDiffs = true; |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 1060 | |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 1061 | RecordArray differences; |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 1062 | DiffSummary summary; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 1063 | |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 1064 | int i; |
| 1065 | int numUnflaggedArguments = 0; |
| 1066 | for (i = 1; i < argc; i++) { |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 1067 | if (!strcmp(argv[i], "--help")) { |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 1068 | usage(argv[0]); |
| 1069 | return 0; |
| 1070 | } |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 1071 | if (!strcmp(argv[i], "--nodiffs")) { |
| 1072 | generateDiffs = false; |
| 1073 | continue; |
| 1074 | } |
| 1075 | if (!strcmp(argv[i], "--threshold")) { |
| 1076 | colorThreshold = atoi(argv[++i]); |
| 1077 | continue; |
| 1078 | } |
| 1079 | if (!strcmp(argv[i], "--match")) { |
| 1080 | matchSubstrings.push(new SkString(argv[++i])); |
| 1081 | continue; |
| 1082 | } |
| 1083 | if (!strcmp(argv[i], "--nomatch")) { |
| 1084 | nomatchSubstrings.push(new SkString(argv[++i])); |
| 1085 | continue; |
| 1086 | } |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 1087 | if (!strcmp(argv[i], "--sortbymismatch")) { |
reed@google.com | c7a67cb | 2012-05-07 14:52:12 +0000 | [diff] [blame] | 1088 | sortProc = SkCastForQSort(compare_diff_mean_mismatches); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 1089 | continue; |
| 1090 | } |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 1091 | if (!strcmp(argv[i], "--sortbymaxmismatch")) { |
reed@google.com | c7a67cb | 2012-05-07 14:52:12 +0000 | [diff] [blame] | 1092 | sortProc = SkCastForQSort(compare_diff_max_mismatches); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 1093 | continue; |
| 1094 | } |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 1095 | if (!strcmp(argv[i], "--weighted")) { |
reed@google.com | c7a67cb | 2012-05-07 14:52:12 +0000 | [diff] [blame] | 1096 | sortProc = SkCastForQSort(compare_diff_weighted); |
tomhudson@google.com | 5b32529 | 2011-05-24 19:41:13 +0000 | [diff] [blame] | 1097 | continue; |
| 1098 | } |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 1099 | if (argv[i][0] != '-') { |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 1100 | switch (numUnflaggedArguments++) { |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 1101 | case 0: |
| 1102 | baseDir.set(argv[i]); |
| 1103 | continue; |
| 1104 | case 1: |
| 1105 | comparisonDir.set(argv[i]); |
| 1106 | continue; |
| 1107 | case 2: |
| 1108 | outputDir.set(argv[i]); |
| 1109 | continue; |
| 1110 | default: |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 1111 | SkDebugf("extra unflagged argument <%s>\n", argv[i]); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 1112 | usage(argv[0]); |
| 1113 | return 0; |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | SkDebugf("Unrecognized argument <%s>\n", argv[i]); |
| 1118 | usage(argv[0]); |
| 1119 | return 0; |
| 1120 | } |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 1121 | |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 1122 | if (numUnflaggedArguments == 2) { |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 1123 | outputDir = comparisonDir; |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 1124 | } else if (numUnflaggedArguments != 3) { |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 1125 | usage(argv[0]); |
| 1126 | return 0; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
bsalomon@google.com | 1a315fe | 2011-09-23 14:56:37 +0000 | [diff] [blame] | 1129 | if (!baseDir.endsWith(PATH_DIV_STR)) { |
| 1130 | baseDir.append(PATH_DIV_STR); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 1131 | } |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 1132 | printf("baseDir is [%s]\n", baseDir.c_str()); |
| 1133 | |
bsalomon@google.com | 1a315fe | 2011-09-23 14:56:37 +0000 | [diff] [blame] | 1134 | if (!comparisonDir.endsWith(PATH_DIV_STR)) { |
| 1135 | comparisonDir.append(PATH_DIV_STR); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 1136 | } |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 1137 | printf("comparisonDir is [%s]\n", comparisonDir.c_str()); |
| 1138 | |
bsalomon@google.com | 1a315fe | 2011-09-23 14:56:37 +0000 | [diff] [blame] | 1139 | if (!outputDir.endsWith(PATH_DIV_STR)) { |
| 1140 | outputDir.append(PATH_DIV_STR); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 1141 | } |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 1142 | if (generateDiffs) { |
| 1143 | printf("writing diffs to outputDir is [%s]\n", outputDir.c_str()); |
| 1144 | } else { |
| 1145 | printf("not writing any diffs to outputDir [%s]\n", outputDir.c_str()); |
| 1146 | outputDir.set(""); |
| 1147 | } |
| 1148 | |
| 1149 | // Default substring matching: |
| 1150 | // - No matter what, don't match any PDF files. |
| 1151 | // We may want to change this later, but for now this maintains the filter |
| 1152 | // that get_file_list() used to always apply. |
| 1153 | // - If no matchSubstrings were specified, match ALL strings. |
| 1154 | nomatchSubstrings.push(new SkString(".pdf")); |
| 1155 | if (matchSubstrings.isEmpty()) { |
| 1156 | matchSubstrings.push(new SkString("")); |
| 1157 | } |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 1158 | |
epoger@google.com | a611c3e | 2012-05-18 20:10:06 +0000 | [diff] [blame] | 1159 | create_diff_images(diffProc, colorThreshold, &differences, |
| 1160 | baseDir, comparisonDir, outputDir, |
| 1161 | matchSubstrings, nomatchSubstrings, &summary); |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 1162 | summary.print(); |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 1163 | |
| 1164 | if (differences.count()) { |
reed@google.com | c7a67cb | 2012-05-07 14:52:12 +0000 | [diff] [blame] | 1165 | qsort(differences.begin(), differences.count(), |
| 1166 | sizeof(DiffRecord*), sortProc); |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 1167 | } |
epoger@google.com | 6600852 | 2012-05-16 17:40:57 +0000 | [diff] [blame] | 1168 | |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 1169 | if (generateDiffs) { |
| 1170 | print_diff_page(summary.fNumMatches, colorThreshold, differences, |
| 1171 | baseDir, comparisonDir, outputDir); |
| 1172 | } |
epoger@google.com | 76222c0 | 2012-05-31 15:12:09 +0000 | [diff] [blame] | 1173 | |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 1174 | for (i = 0; i < differences.count(); i++) { |
| 1175 | delete differences[i]; |
| 1176 | } |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 1177 | matchSubstrings.deleteAll(); |
| 1178 | nomatchSubstrings.deleteAll(); |
epoger@google.com | be6188d | 2012-05-31 15:13:45 +0000 | [diff] [blame] | 1179 | |
| 1180 | return summary.fNumMismatches; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 1181 | } |