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