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