epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 7 | #include "skdiff.h" |
| 8 | #include "skdiff_html.h" |
| 9 | #include "skdiff_utils.h" |
| 10 | #include "SkBitmap.h" |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 11 | #include "SkData.h" |
zachr@google.com | 6ef5a85 | 2013-06-18 21:23:31 +0000 | [diff] [blame] | 12 | #include "SkForceLinking.h" |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 13 | #include "SkImageDecoder.h" |
| 14 | #include "SkImageEncoder.h" |
| 15 | #include "SkOSFile.h" |
| 16 | #include "SkStream.h" |
| 17 | #include "SkTDArray.h" |
| 18 | #include "SkTemplates.h" |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 19 | #include "SkTSearch.h" |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 20 | |
zachr@google.com | 6ef5a85 | 2013-06-18 21:23:31 +0000 | [diff] [blame] | 21 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 22 | |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 23 | /** |
| 24 | * skdiff |
| 25 | * |
| 26 | * Given three directory names, expects to find identically-named files in |
| 27 | * each of the first two; the first are treated as a set of baseline, |
| 28 | * the second a set of variant images, and a diff image is written into the |
| 29 | * third directory for each pair. |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 30 | * 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] | 31 | * pair that does not match exactly. |
epoger@google.com | 71329d8 | 2012-08-16 13:42:13 +0000 | [diff] [blame] | 32 | * Recursively descends directories, unless run with --norecurse. |
epoger@google.com | be6188d | 2012-05-31 15:13:45 +0000 | [diff] [blame] | 33 | * |
| 34 | * Returns zero exit code if all images match across baseDir and comparisonDir. |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 35 | */ |
| 36 | |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 37 | typedef SkTDArray<SkString*> StringArray; |
| 38 | typedef StringArray FileArray; |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 39 | |
reed | 3a3baf6 | 2015-01-06 07:39:55 -0800 | [diff] [blame] | 40 | static void add_unique_basename(StringArray* array, const SkString& filename) { |
| 41 | // trim off dirs |
| 42 | const char* src = filename.c_str(); |
| 43 | const char* trimmed = strrchr(src, SkPATH_SEPARATOR); |
| 44 | if (trimmed) { |
| 45 | trimmed += 1; // skip the separator |
| 46 | } else { |
| 47 | trimmed = src; |
| 48 | } |
| 49 | const char* end = strrchr(trimmed, '.'); |
| 50 | if (!end) { |
| 51 | end = trimmed + strlen(trimmed); |
| 52 | } |
| 53 | SkString result(trimmed, end - trimmed); |
| 54 | |
| 55 | // only add unique entries |
| 56 | for (int i = 0; i < array->count(); ++i) { |
| 57 | if (*array->getAt(i) == result) { |
| 58 | return; |
| 59 | } |
| 60 | } |
| 61 | *array->append() = new SkString(result); |
| 62 | } |
| 63 | |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 64 | struct DiffSummary { |
| 65 | DiffSummary () |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 66 | : fNumMatches(0) |
| 67 | , fNumMismatches(0) |
| 68 | , fMaxMismatchV(0) |
| 69 | , fMaxMismatchPercent(0) { }; |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 70 | |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 71 | ~DiffSummary() { |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 72 | for (int i = 0; i < DiffRecord::kResultCount; ++i) { |
epoger@google.com | 76222c0 | 2012-05-31 15:12:09 +0000 | [diff] [blame] | 73 | fResultsOfType[i].deleteAll(); |
| 74 | } |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 75 | for (int base = 0; base < DiffResource::kStatusCount; ++base) { |
| 76 | for (int comparison = 0; comparison < DiffResource::kStatusCount; ++comparison) { |
| 77 | fStatusOfType[base][comparison].deleteAll(); |
| 78 | } |
skia.committer@gmail.com | 0264fb4 | 2012-12-06 02:01:25 +0000 | [diff] [blame] | 79 | } |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 80 | } |
| 81 | |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 82 | uint32_t fNumMatches; |
| 83 | uint32_t fNumMismatches; |
| 84 | uint32_t fMaxMismatchV; |
| 85 | float fMaxMismatchPercent; |
| 86 | |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 87 | FileArray fResultsOfType[DiffRecord::kResultCount]; |
| 88 | FileArray fStatusOfType[DiffResource::kStatusCount][DiffResource::kStatusCount]; |
| 89 | |
reed | 3a3baf6 | 2015-01-06 07:39:55 -0800 | [diff] [blame] | 90 | StringArray fFailedBaseNames[DiffRecord::kResultCount]; |
| 91 | |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 92 | void printContents(const FileArray& fileArray, |
| 93 | const char* baseStatus, const char* comparisonStatus, |
| 94 | bool listFilenames) { |
| 95 | int n = fileArray.count(); |
| 96 | printf("%d file pairs %s in baseDir and %s in comparisonDir", |
| 97 | n, baseStatus, comparisonStatus); |
| 98 | if (listFilenames) { |
| 99 | printf(": "); |
| 100 | for (int i = 0; i < n; ++i) { |
| 101 | printf("%s ", fileArray[i]->c_str()); |
| 102 | } |
| 103 | } |
| 104 | printf("\n"); |
| 105 | } |
| 106 | |
| 107 | void printStatus(bool listFilenames, |
| 108 | bool failOnStatusType[DiffResource::kStatusCount] |
| 109 | [DiffResource::kStatusCount]) { |
| 110 | typedef DiffResource::Status Status; |
| 111 | |
| 112 | for (int base = 0; base < DiffResource::kStatusCount; ++base) { |
| 113 | Status baseStatus = static_cast<Status>(base); |
| 114 | for (int comparison = 0; comparison < DiffResource::kStatusCount; ++comparison) { |
| 115 | Status comparisonStatus = static_cast<Status>(comparison); |
| 116 | const FileArray& fileArray = fStatusOfType[base][comparison]; |
| 117 | if (fileArray.count() > 0) { |
| 118 | if (failOnStatusType[base][comparison]) { |
| 119 | printf(" [*] "); |
| 120 | } else { |
| 121 | printf(" [_] "); |
| 122 | } |
| 123 | printContents(fileArray, |
| 124 | DiffResource::getStatusDescription(baseStatus), |
| 125 | DiffResource::getStatusDescription(comparisonStatus), |
| 126 | listFilenames); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |
epoger@google.com | 76222c0 | 2012-05-31 15:12:09 +0000 | [diff] [blame] | 131 | |
epoger@google.com | 3af4ff4 | 2012-07-19 17:35:04 +0000 | [diff] [blame] | 132 | // Print a line about the contents of this FileArray to stdout. |
epoger@google.com | 46a4596 | 2012-07-12 18:16:02 +0000 | [diff] [blame] | 133 | void printContents(const FileArray& fileArray, const char* headerText, bool listFilenames) { |
epoger@google.com | 76222c0 | 2012-05-31 15:12:09 +0000 | [diff] [blame] | 134 | int n = fileArray.count(); |
epoger@google.com | 3af4ff4 | 2012-07-19 17:35:04 +0000 | [diff] [blame] | 135 | printf("%d file pairs %s", n, headerText); |
| 136 | if (listFilenames) { |
| 137 | printf(": "); |
| 138 | for (int i = 0; i < n; ++i) { |
| 139 | printf("%s ", fileArray[i]->c_str()); |
epoger@google.com | 76222c0 | 2012-05-31 15:12:09 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
epoger@google.com | 3af4ff4 | 2012-07-19 17:35:04 +0000 | [diff] [blame] | 142 | printf("\n"); |
epoger@google.com | 76222c0 | 2012-05-31 15:12:09 +0000 | [diff] [blame] | 143 | } |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 144 | |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 145 | void print(bool listFilenames, bool failOnResultType[DiffRecord::kResultCount], |
| 146 | bool failOnStatusType[DiffResource::kStatusCount] |
| 147 | [DiffResource::kStatusCount]) { |
epoger@google.com | 3af4ff4 | 2012-07-19 17:35:04 +0000 | [diff] [blame] | 148 | printf("\ncompared %d file pairs:\n", fNumMatches + fNumMismatches); |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 149 | for (int resultInt = 0; resultInt < DiffRecord::kResultCount; ++resultInt) { |
| 150 | DiffRecord::Result result = static_cast<DiffRecord::Result>(resultInt); |
epoger@google.com | 3af4ff4 | 2012-07-19 17:35:04 +0000 | [diff] [blame] | 151 | if (failOnResultType[result]) { |
| 152 | printf("[*] "); |
| 153 | } else { |
| 154 | printf("[_] "); |
| 155 | } |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 156 | printContents(fResultsOfType[result], DiffRecord::getResultDescription(result), |
| 157 | listFilenames); |
| 158 | if (DiffRecord::kCouldNotCompare_Result == result) { |
| 159 | printStatus(listFilenames, failOnStatusType); |
| 160 | } |
epoger@google.com | 46a4596 | 2012-07-12 18:16:02 +0000 | [diff] [blame] | 161 | } |
epoger@google.com | 3af4ff4 | 2012-07-19 17:35:04 +0000 | [diff] [blame] | 162 | printf("(results marked with [*] will cause nonzero return value)\n"); |
| 163 | printf("\nnumber of mismatching file pairs: %d\n", fNumMismatches); |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 164 | if (fNumMismatches > 0) { |
| 165 | printf("Maximum pixel intensity mismatch %d\n", fMaxMismatchV); |
epoger@google.com | 46a4596 | 2012-07-12 18:16:02 +0000 | [diff] [blame] | 166 | printf("Largest area mismatch was %.2f%% of pixels\n",fMaxMismatchPercent); |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 167 | } |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 168 | } |
| 169 | |
reed | 3a3baf6 | 2015-01-06 07:39:55 -0800 | [diff] [blame] | 170 | void printfFailingBaseNames(const char separator[]) { |
| 171 | for (int resultInt = 0; resultInt < DiffRecord::kResultCount; ++resultInt) { |
| 172 | const StringArray& array = fFailedBaseNames[resultInt]; |
| 173 | if (array.count()) { |
| 174 | printf("%s [%d]%s", DiffRecord::ResultNames[resultInt], array.count(), separator); |
| 175 | for (int j = 0; j < array.count(); ++j) { |
| 176 | printf("%s%s", array[j]->c_str(), separator); |
| 177 | } |
| 178 | printf("\n"); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 183 | void add (DiffRecord* drp) { |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 184 | uint32_t mismatchValue; |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 185 | |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 186 | if (drp->fBase.fFilename.equals(drp->fComparison.fFilename)) { |
| 187 | fResultsOfType[drp->fResult].push(new SkString(drp->fBase.fFilename)); |
| 188 | } else { |
| 189 | SkString* blame = new SkString("("); |
| 190 | blame->append(drp->fBase.fFilename); |
| 191 | blame->append(", "); |
| 192 | blame->append(drp->fComparison.fFilename); |
| 193 | blame->append(")"); |
| 194 | fResultsOfType[drp->fResult].push(blame); |
| 195 | } |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 196 | switch (drp->fResult) { |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 197 | case DiffRecord::kEqualBits_Result: |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 198 | fNumMatches++; |
| 199 | break; |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 200 | case DiffRecord::kEqualPixels_Result: |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 201 | fNumMatches++; |
| 202 | break; |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 203 | case DiffRecord::kDifferentSizes_Result: |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 204 | fNumMismatches++; |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 205 | break; |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 206 | case DiffRecord::kDifferentPixels_Result: |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 207 | fNumMismatches++; |
| 208 | if (drp->fFractionDifference * 100 > fMaxMismatchPercent) { |
| 209 | fMaxMismatchPercent = drp->fFractionDifference * 100; |
| 210 | } |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 211 | mismatchValue = MAX3(drp->fMaxMismatchR, drp->fMaxMismatchG, |
| 212 | drp->fMaxMismatchB); |
| 213 | if (mismatchValue > fMaxMismatchV) { |
| 214 | fMaxMismatchV = mismatchValue; |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 215 | } |
epoger@google.com | 292aff6 | 2012-05-16 14:57:28 +0000 | [diff] [blame] | 216 | break; |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 217 | case DiffRecord::kCouldNotCompare_Result: |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 218 | fNumMismatches++; |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 219 | fStatusOfType[drp->fBase.fStatus][drp->fComparison.fStatus].push( |
| 220 | new SkString(drp->fBase.fFilename)); |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 221 | break; |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 222 | case DiffRecord::kUnknown_Result: |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 223 | SkDEBUGFAIL("adding uncategorized DiffRecord"); |
| 224 | break; |
| 225 | default: |
| 226 | SkDEBUGFAIL("adding DiffRecord with unhandled fResult value"); |
| 227 | break; |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 228 | } |
reed | 3a3baf6 | 2015-01-06 07:39:55 -0800 | [diff] [blame] | 229 | |
| 230 | switch (drp->fResult) { |
| 231 | case DiffRecord::kEqualBits_Result: |
| 232 | case DiffRecord::kEqualPixels_Result: |
| 233 | break; |
| 234 | default: |
| 235 | add_unique_basename(&fFailedBaseNames[drp->fResult], drp->fBase.fFilename); |
| 236 | break; |
| 237 | } |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 238 | } |
| 239 | }; |
| 240 | |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 241 | /// Returns true if string contains any of these substrings. |
| 242 | static bool string_contains_any_of(const SkString& string, |
| 243 | const StringArray& substrings) { |
| 244 | for (int i = 0; i < substrings.count(); i++) { |
| 245 | if (string.contains(substrings[i]->c_str())) { |
| 246 | return true; |
| 247 | } |
| 248 | } |
| 249 | return false; |
| 250 | } |
| 251 | |
epoger@google.com | 71329d8 | 2012-08-16 13:42:13 +0000 | [diff] [blame] | 252 | /// Internal (potentially recursive) implementation of get_file_list. |
| 253 | static void get_file_list_subdir(const SkString& rootDir, const SkString& subDir, |
| 254 | const StringArray& matchSubstrings, |
| 255 | const StringArray& nomatchSubstrings, |
| 256 | bool recurseIntoSubdirs, FileArray *files) { |
| 257 | bool isSubDirEmpty = subDir.isEmpty(); |
| 258 | SkString dir(rootDir); |
| 259 | if (!isSubDirEmpty) { |
| 260 | dir.append(PATH_DIV_STR); |
| 261 | dir.append(subDir); |
| 262 | } |
| 263 | |
| 264 | // Iterate over files (not directories) within dir. |
| 265 | SkOSFile::Iter fileIterator(dir.c_str()); |
| 266 | SkString fileName; |
| 267 | while (fileIterator.next(&fileName, false)) { |
| 268 | if (fileName.startsWith(".")) { |
| 269 | continue; |
| 270 | } |
| 271 | SkString pathRelativeToRootDir(subDir); |
| 272 | if (!isSubDirEmpty) { |
| 273 | pathRelativeToRootDir.append(PATH_DIV_STR); |
| 274 | } |
| 275 | pathRelativeToRootDir.append(fileName); |
| 276 | if (string_contains_any_of(pathRelativeToRootDir, matchSubstrings) && |
| 277 | !string_contains_any_of(pathRelativeToRootDir, nomatchSubstrings)) { |
| 278 | files->push(new SkString(pathRelativeToRootDir)); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | // Recurse into any non-ignored subdirectories. |
| 283 | if (recurseIntoSubdirs) { |
| 284 | SkOSFile::Iter dirIterator(dir.c_str()); |
| 285 | SkString dirName; |
| 286 | while (dirIterator.next(&dirName, true)) { |
| 287 | if (dirName.startsWith(".")) { |
| 288 | continue; |
| 289 | } |
| 290 | SkString pathRelativeToRootDir(subDir); |
| 291 | if (!isSubDirEmpty) { |
| 292 | pathRelativeToRootDir.append(PATH_DIV_STR); |
| 293 | } |
| 294 | pathRelativeToRootDir.append(dirName); |
| 295 | if (!string_contains_any_of(pathRelativeToRootDir, nomatchSubstrings)) { |
| 296 | get_file_list_subdir(rootDir, pathRelativeToRootDir, |
| 297 | matchSubstrings, nomatchSubstrings, recurseIntoSubdirs, |
| 298 | files); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /// Iterate over dir and get all files whose filename: |
| 305 | /// - matches any of the substrings in matchSubstrings, but... |
| 306 | /// - DOES NOT match any of the substrings in nomatchSubstrings |
| 307 | /// - DOES NOT start with a dot (.) |
| 308 | /// Adds the matching files to the list in *files. |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 309 | static void get_file_list(const SkString& dir, |
| 310 | const StringArray& matchSubstrings, |
| 311 | const StringArray& nomatchSubstrings, |
epoger@google.com | 71329d8 | 2012-08-16 13:42:13 +0000 | [diff] [blame] | 312 | bool recurseIntoSubdirs, FileArray *files) { |
| 313 | get_file_list_subdir(dir, SkString(""), |
| 314 | matchSubstrings, nomatchSubstrings, recurseIntoSubdirs, |
| 315 | files); |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static void release_file_list(FileArray *files) { |
| 319 | files->deleteAll(); |
| 320 | } |
| 321 | |
| 322 | /// Comparison routines for qsort, sort by file names. |
| 323 | static int compare_file_name_metrics(SkString **lhs, SkString **rhs) { |
| 324 | return strcmp((*lhs)->c_str(), (*rhs)->c_str()); |
| 325 | } |
| 326 | |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 327 | class AutoReleasePixels { |
| 328 | public: |
| 329 | AutoReleasePixels(DiffRecord* drp) |
| 330 | : fDrp(drp) { |
| 331 | SkASSERT(drp != NULL); |
| 332 | } |
| 333 | ~AutoReleasePixels() { |
| 334 | fDrp->fBase.fBitmap.setPixelRef(NULL); |
| 335 | fDrp->fComparison.fBitmap.setPixelRef(NULL); |
| 336 | fDrp->fDifference.fBitmap.setPixelRef(NULL); |
| 337 | fDrp->fWhite.fBitmap.setPixelRef(NULL); |
| 338 | } |
| 339 | |
| 340 | private: |
| 341 | DiffRecord* fDrp; |
| 342 | }; |
| 343 | |
| 344 | static void get_bounds(DiffResource& resource, const char* name) { |
| 345 | if (resource.fBitmap.empty() && !DiffResource::isStatusFailed(resource.fStatus)) { |
| 346 | SkAutoDataUnref fileBits(read_file(resource.fFullPath.c_str())); |
| 347 | if (NULL == fileBits) { |
| 348 | SkDebugf("WARNING: couldn't read %s file <%s>\n", name, resource.fFullPath.c_str()); |
| 349 | resource.fStatus = DiffResource::kCouldNotRead_Status; |
| 350 | } else { |
| 351 | get_bitmap(fileBits, resource, SkImageDecoder::kDecodeBounds_Mode); |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | static void get_bounds(DiffRecord& drp) { |
| 357 | get_bounds(drp.fBase, "base"); |
| 358 | get_bounds(drp.fComparison, "comparison"); |
| 359 | } |
| 360 | |
commit-bot@chromium.org | 93d7bb6 | 2014-05-28 18:26:00 +0000 | [diff] [blame] | 361 | #ifdef SK_OS_WIN |
| 362 | #define ANSI_COLOR_RED "" |
| 363 | #define ANSI_COLOR_GREEN "" |
| 364 | #define ANSI_COLOR_YELLOW "" |
| 365 | #define ANSI_COLOR_RESET "" |
| 366 | #else |
| 367 | #define ANSI_COLOR_RED "\x1b[31m" |
| 368 | #define ANSI_COLOR_GREEN "\x1b[32m" |
| 369 | #define ANSI_COLOR_YELLOW "\x1b[33m" |
| 370 | #define ANSI_COLOR_RESET "\x1b[0m" |
| 371 | #endif |
| 372 | |
| 373 | #define VERBOSE_STATUS(status,color,filename) if (verbose) printf( "[ " color " %10s " ANSI_COLOR_RESET " ] %s\n", status, filename->c_str()) |
| 374 | |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 375 | /// Creates difference images, returns the number that have a 0 metric. |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 376 | /// If outputDir.isEmpty(), don't write out diff files. |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 377 | static void create_diff_images (DiffMetricProc dmp, |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 378 | const int colorThreshold, |
| 379 | RecordArray* differences, |
| 380 | const SkString& baseDir, |
| 381 | const SkString& comparisonDir, |
| 382 | const SkString& outputDir, |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 383 | const StringArray& matchSubstrings, |
| 384 | const StringArray& nomatchSubstrings, |
epoger@google.com | 71329d8 | 2012-08-16 13:42:13 +0000 | [diff] [blame] | 385 | bool recurseIntoSubdirs, |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 386 | bool getBounds, |
commit-bot@chromium.org | 93d7bb6 | 2014-05-28 18:26:00 +0000 | [diff] [blame] | 387 | bool verbose, |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 388 | DiffSummary* summary) { |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 389 | SkASSERT(!baseDir.isEmpty()); |
| 390 | SkASSERT(!comparisonDir.isEmpty()); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 391 | |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 392 | FileArray baseFiles; |
| 393 | FileArray comparisonFiles; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 394 | |
epoger@google.com | 71329d8 | 2012-08-16 13:42:13 +0000 | [diff] [blame] | 395 | get_file_list(baseDir, matchSubstrings, nomatchSubstrings, recurseIntoSubdirs, &baseFiles); |
| 396 | get_file_list(comparisonDir, matchSubstrings, nomatchSubstrings, recurseIntoSubdirs, |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 397 | &comparisonFiles); |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 398 | |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 399 | if (!baseFiles.isEmpty()) { |
reed@google.com | c7a67cb | 2012-05-07 14:52:12 +0000 | [diff] [blame] | 400 | qsort(baseFiles.begin(), baseFiles.count(), sizeof(SkString*), |
| 401 | SkCastForQSort(compare_file_name_metrics)); |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 402 | } |
| 403 | if (!comparisonFiles.isEmpty()) { |
reed@google.com | c7a67cb | 2012-05-07 14:52:12 +0000 | [diff] [blame] | 404 | qsort(comparisonFiles.begin(), comparisonFiles.count(), |
| 405 | sizeof(SkString*), SkCastForQSort(compare_file_name_metrics)); |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 406 | } |
epoger@google.com | 6600852 | 2012-05-16 17:40:57 +0000 | [diff] [blame] | 407 | |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 408 | int i = 0; |
| 409 | int j = 0; |
| 410 | |
| 411 | while (i < baseFiles.count() && |
| 412 | j < comparisonFiles.count()) { |
| 413 | |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 414 | SkString basePath(baseDir); |
| 415 | SkString comparisonPath(comparisonDir); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 416 | |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 417 | DiffRecord *drp = new DiffRecord; |
| 418 | int v = strcmp(baseFiles[i]->c_str(), comparisonFiles[j]->c_str()); |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 419 | |
| 420 | if (v < 0) { |
| 421 | // in baseDir, but not in comparisonDir |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 422 | drp->fResult = DiffRecord::kCouldNotCompare_Result; |
| 423 | |
| 424 | basePath.append(*baseFiles[i]); |
| 425 | comparisonPath.append(*baseFiles[i]); |
| 426 | |
| 427 | drp->fBase.fFilename = *baseFiles[i]; |
| 428 | drp->fBase.fFullPath = basePath; |
| 429 | drp->fBase.fStatus = DiffResource::kExists_Status; |
| 430 | |
| 431 | drp->fComparison.fFilename = *baseFiles[i]; |
| 432 | drp->fComparison.fFullPath = comparisonPath; |
| 433 | drp->fComparison.fStatus = DiffResource::kDoesNotExist_Status; |
| 434 | |
commit-bot@chromium.org | 93d7bb6 | 2014-05-28 18:26:00 +0000 | [diff] [blame] | 435 | VERBOSE_STATUS("MISSING", ANSI_COLOR_YELLOW, baseFiles[i]); |
| 436 | |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 437 | ++i; |
| 438 | } else if (v > 0) { |
| 439 | // in comparisonDir, but not in baseDir |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 440 | drp->fResult = DiffRecord::kCouldNotCompare_Result; |
| 441 | |
| 442 | basePath.append(*comparisonFiles[j]); |
| 443 | comparisonPath.append(*comparisonFiles[j]); |
| 444 | |
| 445 | drp->fBase.fFilename = *comparisonFiles[j]; |
| 446 | drp->fBase.fFullPath = basePath; |
| 447 | drp->fBase.fStatus = DiffResource::kDoesNotExist_Status; |
| 448 | |
| 449 | drp->fComparison.fFilename = *comparisonFiles[j]; |
| 450 | drp->fComparison.fFullPath = comparisonPath; |
| 451 | drp->fComparison.fStatus = DiffResource::kExists_Status; |
| 452 | |
commit-bot@chromium.org | 93d7bb6 | 2014-05-28 18:26:00 +0000 | [diff] [blame] | 453 | VERBOSE_STATUS("MISSING", ANSI_COLOR_YELLOW, comparisonFiles[j]); |
| 454 | |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 455 | ++j; |
| 456 | } else { |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 457 | // Found the same filename in both baseDir and comparisonDir. |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 458 | SkASSERT(DiffRecord::kUnknown_Result == drp->fResult); |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 459 | |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 460 | basePath.append(*baseFiles[i]); |
| 461 | comparisonPath.append(*comparisonFiles[j]); |
| 462 | |
| 463 | drp->fBase.fFilename = *baseFiles[i]; |
| 464 | drp->fBase.fFullPath = basePath; |
| 465 | drp->fBase.fStatus = DiffResource::kExists_Status; |
| 466 | |
| 467 | drp->fComparison.fFilename = *comparisonFiles[j]; |
| 468 | drp->fComparison.fFullPath = comparisonPath; |
| 469 | drp->fComparison.fStatus = DiffResource::kExists_Status; |
| 470 | |
| 471 | SkAutoDataUnref baseFileBits(read_file(drp->fBase.fFullPath.c_str())); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 472 | if (baseFileBits) { |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 473 | drp->fBase.fStatus = DiffResource::kRead_Status; |
| 474 | } |
| 475 | SkAutoDataUnref comparisonFileBits(read_file(drp->fComparison.fFullPath.c_str())); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 476 | if (comparisonFileBits) { |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 477 | drp->fComparison.fStatus = DiffResource::kRead_Status; |
| 478 | } |
| 479 | if (NULL == baseFileBits || NULL == comparisonFileBits) { |
| 480 | if (NULL == baseFileBits) { |
| 481 | drp->fBase.fStatus = DiffResource::kCouldNotRead_Status; |
commit-bot@chromium.org | 93d7bb6 | 2014-05-28 18:26:00 +0000 | [diff] [blame] | 482 | VERBOSE_STATUS("READ FAIL", ANSI_COLOR_RED, baseFiles[i]); |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 483 | } |
| 484 | if (NULL == comparisonFileBits) { |
| 485 | drp->fComparison.fStatus = DiffResource::kCouldNotRead_Status; |
commit-bot@chromium.org | 93d7bb6 | 2014-05-28 18:26:00 +0000 | [diff] [blame] | 486 | VERBOSE_STATUS("READ FAIL", ANSI_COLOR_RED, comparisonFiles[j]); |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 487 | } |
| 488 | drp->fResult = DiffRecord::kCouldNotCompare_Result; |
| 489 | |
| 490 | } else if (are_buffers_equal(baseFileBits, comparisonFileBits)) { |
| 491 | drp->fResult = DiffRecord::kEqualBits_Result; |
commit-bot@chromium.org | 93d7bb6 | 2014-05-28 18:26:00 +0000 | [diff] [blame] | 492 | VERBOSE_STATUS("MATCH", ANSI_COLOR_GREEN, baseFiles[i]); |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 493 | } else { |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 494 | AutoReleasePixels arp(drp); |
| 495 | get_bitmap(baseFileBits, drp->fBase, SkImageDecoder::kDecodePixels_Mode); |
| 496 | get_bitmap(comparisonFileBits, drp->fComparison, |
| 497 | SkImageDecoder::kDecodePixels_Mode); |
commit-bot@chromium.org | 93d7bb6 | 2014-05-28 18:26:00 +0000 | [diff] [blame] | 498 | VERBOSE_STATUS("DIFFERENT", ANSI_COLOR_RED, baseFiles[i]); |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 499 | if (DiffResource::kDecoded_Status == drp->fBase.fStatus && |
| 500 | DiffResource::kDecoded_Status == drp->fComparison.fStatus) { |
| 501 | create_and_write_diff_image(drp, dmp, colorThreshold, |
| 502 | outputDir, drp->fBase.fFilename); |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 503 | } else { |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 504 | drp->fResult = DiffRecord::kCouldNotCompare_Result; |
epoger@google.com | 46256ea | 2012-05-22 13:45:35 +0000 | [diff] [blame] | 505 | } |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 506 | } |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 507 | |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 508 | ++i; |
| 509 | ++j; |
| 510 | } |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 511 | |
| 512 | if (getBounds) { |
| 513 | get_bounds(*drp); |
| 514 | } |
| 515 | SkASSERT(DiffRecord::kUnknown_Result != drp->fResult); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 516 | differences->push(drp); |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 517 | summary->add(drp); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 518 | } |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 519 | |
| 520 | for (; i < baseFiles.count(); ++i) { |
| 521 | // files only in baseDir |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 522 | DiffRecord *drp = new DiffRecord(); |
| 523 | drp->fBase.fFilename = *baseFiles[i]; |
| 524 | drp->fBase.fFullPath = baseDir; |
| 525 | drp->fBase.fFullPath.append(drp->fBase.fFilename); |
| 526 | drp->fBase.fStatus = DiffResource::kExists_Status; |
| 527 | |
| 528 | drp->fComparison.fFilename = *baseFiles[i]; |
| 529 | drp->fComparison.fFullPath = comparisonDir; |
| 530 | drp->fComparison.fFullPath.append(drp->fComparison.fFilename); |
| 531 | drp->fComparison.fStatus = DiffResource::kDoesNotExist_Status; |
| 532 | |
| 533 | drp->fResult = DiffRecord::kCouldNotCompare_Result; |
| 534 | if (getBounds) { |
| 535 | get_bounds(*drp); |
| 536 | } |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 537 | differences->push(drp); |
| 538 | summary->add(drp); |
| 539 | } |
| 540 | |
| 541 | for (; j < comparisonFiles.count(); ++j) { |
| 542 | // files only in comparisonDir |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 543 | DiffRecord *drp = new DiffRecord(); |
| 544 | drp->fBase.fFilename = *comparisonFiles[j]; |
| 545 | drp->fBase.fFullPath = baseDir; |
| 546 | drp->fBase.fFullPath.append(drp->fBase.fFilename); |
| 547 | drp->fBase.fStatus = DiffResource::kDoesNotExist_Status; |
| 548 | |
| 549 | drp->fComparison.fFilename = *comparisonFiles[j]; |
| 550 | drp->fComparison.fFullPath = comparisonDir; |
| 551 | drp->fComparison.fFullPath.append(drp->fComparison.fFilename); |
| 552 | drp->fComparison.fStatus = DiffResource::kExists_Status; |
| 553 | |
| 554 | drp->fResult = DiffRecord::kCouldNotCompare_Result; |
| 555 | if (getBounds) { |
| 556 | get_bounds(*drp); |
| 557 | } |
epoger@google.com | 5fd5385 | 2012-03-22 18:20:06 +0000 | [diff] [blame] | 558 | differences->push(drp); |
| 559 | summary->add(drp); |
| 560 | } |
| 561 | |
| 562 | release_file_list(&baseFiles); |
| 563 | release_file_list(&comparisonFiles); |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 564 | } |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 565 | |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 566 | static void usage (char * argv0) { |
| 567 | SkDebugf("Skia baseline image diff tool\n"); |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 568 | SkDebugf("\n" |
| 569 | "Usage: \n" |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 570 | " %s <baseDir> <comparisonDir> [outputDir] \n", argv0); |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 571 | SkDebugf( |
epoger@google.com | 46a4596 | 2012-07-12 18:16:02 +0000 | [diff] [blame] | 572 | "\nArguments:" |
epoger@google.com | dfbf24e | 2012-07-13 21:22:02 +0000 | [diff] [blame] | 573 | "\n --failonresult <result>: After comparing all file pairs, exit with nonzero" |
| 574 | "\n return code (number of file pairs yielding this" |
| 575 | "\n result) if any file pairs yielded this result." |
| 576 | "\n This flag may be repeated, in which case the" |
| 577 | "\n return code will be the number of fail pairs" |
| 578 | "\n yielding ANY of these results." |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 579 | "\n --failonstatus <baseStatus> <comparisonStatus>: exit with nonzero return" |
| 580 | "\n code if any file pairs yielded this status." |
epoger@google.com | 46a4596 | 2012-07-12 18:16:02 +0000 | [diff] [blame] | 581 | "\n --help: display this info" |
epoger@google.com | 46a4596 | 2012-07-12 18:16:02 +0000 | [diff] [blame] | 582 | "\n --listfilenames: list all filenames for each result type in stdout" |
epoger@google.com | dfbf24e | 2012-07-13 21:22:02 +0000 | [diff] [blame] | 583 | "\n --match <substring>: compare files whose filenames contain this substring;" |
| 584 | "\n if unspecified, compare ALL files." |
| 585 | "\n this flag may be repeated." |
epoger@google.com | 46a4596 | 2012-07-12 18:16:02 +0000 | [diff] [blame] | 586 | "\n --nodiffs: don't write out image diffs or index.html, just generate" |
| 587 | "\n report on stdout" |
epoger@google.com | dfbf24e | 2012-07-13 21:22:02 +0000 | [diff] [blame] | 588 | "\n --nomatch <substring>: regardless of --match, DO NOT compare files whose" |
| 589 | "\n filenames contain this substring." |
| 590 | "\n this flag may be repeated." |
epoger@google.com | 46a4596 | 2012-07-12 18:16:02 +0000 | [diff] [blame] | 591 | "\n --noprintdirs: do not print the directories used." |
epoger@google.com | 71329d8 | 2012-08-16 13:42:13 +0000 | [diff] [blame] | 592 | "\n --norecurse: do not recurse into subdirectories." |
epoger@google.com | 46a4596 | 2012-07-12 18:16:02 +0000 | [diff] [blame] | 593 | "\n --sortbymaxmismatch: sort by worst color channel mismatch;" |
| 594 | "\n break ties with -sortbymismatch" |
| 595 | "\n --sortbymismatch: sort by average color channel mismatch" |
| 596 | "\n --threshold <n>: only report differences > n (per color channel) [default 0]" |
| 597 | "\n --weighted: sort by # pixels different weighted by color difference" |
| 598 | "\n" |
| 599 | "\n baseDir: directory to read baseline images from." |
| 600 | "\n comparisonDir: directory to read comparison images from" |
| 601 | "\n outputDir: directory to write difference images and index.html to;" |
| 602 | "\n defaults to comparisonDir" |
| 603 | "\n" |
| 604 | "\nIf no sort is specified, it will sort by fraction of pixels mismatching." |
| 605 | "\n"); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 606 | } |
| 607 | |
epoger@google.com | 70044cc | 2012-07-12 18:37:55 +0000 | [diff] [blame] | 608 | const int kNoError = 0; |
| 609 | const int kGenericError = -1; |
epoger@google.com | 46a4596 | 2012-07-12 18:16:02 +0000 | [diff] [blame] | 610 | |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 611 | int tool_main(int argc, char** argv); |
| 612 | int tool_main(int argc, char** argv) { |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 613 | DiffMetricProc diffProc = compute_diff_pmcolor; |
epoger@google.com | 28060e7 | 2012-06-28 16:47:34 +0000 | [diff] [blame] | 614 | int (*sortProc)(const void*, const void*) = compare<CompareDiffMetrics>; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 615 | |
| 616 | // Maximum error tolerated in any one color channel in any one pixel before |
| 617 | // a difference is reported. |
| 618 | int colorThreshold = 0; |
| 619 | SkString baseDir; |
| 620 | SkString comparisonDir; |
| 621 | SkString outputDir; |
epoger@google.com | dfbf24e | 2012-07-13 21:22:02 +0000 | [diff] [blame] | 622 | |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 623 | StringArray matchSubstrings; |
| 624 | StringArray nomatchSubstrings; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 625 | |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 626 | bool generateDiffs = true; |
epoger@google.com | 46a4596 | 2012-07-12 18:16:02 +0000 | [diff] [blame] | 627 | bool listFilenames = false; |
epoger@google.com | 71329d8 | 2012-08-16 13:42:13 +0000 | [diff] [blame] | 628 | bool printDirNames = true; |
| 629 | bool recurseIntoSubdirs = true; |
commit-bot@chromium.org | 93d7bb6 | 2014-05-28 18:26:00 +0000 | [diff] [blame] | 630 | bool verbose = false; |
reed | 3a3baf6 | 2015-01-06 07:39:55 -0800 | [diff] [blame] | 631 | bool listFailingBase = false; |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 632 | |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 633 | RecordArray differences; |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 634 | DiffSummary summary; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 635 | |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 636 | bool failOnResultType[DiffRecord::kResultCount]; |
| 637 | for (int i = 0; i < DiffRecord::kResultCount; i++) { |
epoger@google.com | 3af4ff4 | 2012-07-19 17:35:04 +0000 | [diff] [blame] | 638 | failOnResultType[i] = false; |
| 639 | } |
| 640 | |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 641 | bool failOnStatusType[DiffResource::kStatusCount][DiffResource::kStatusCount]; |
| 642 | for (int base = 0; base < DiffResource::kStatusCount; ++base) { |
| 643 | for (int comparison = 0; comparison < DiffResource::kStatusCount; ++comparison) { |
| 644 | failOnStatusType[base][comparison] = false; |
| 645 | } |
| 646 | } |
| 647 | |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 648 | int i; |
| 649 | int numUnflaggedArguments = 0; |
| 650 | for (i = 1; i < argc; i++) { |
epoger@google.com | dfbf24e | 2012-07-13 21:22:02 +0000 | [diff] [blame] | 651 | if (!strcmp(argv[i], "--failonresult")) { |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 652 | if (argc == ++i) { |
| 653 | SkDebugf("failonresult expects one argument.\n"); |
| 654 | continue; |
| 655 | } |
| 656 | DiffRecord::Result type = DiffRecord::getResultByName(argv[i]); |
| 657 | if (type != DiffRecord::kResultCount) { |
| 658 | failOnResultType[type] = true; |
| 659 | } else { |
| 660 | SkDebugf("ignoring unrecognized result <%s>\n", argv[i]); |
| 661 | } |
| 662 | continue; |
| 663 | } |
| 664 | if (!strcmp(argv[i], "--failonstatus")) { |
| 665 | if (argc == ++i) { |
| 666 | SkDebugf("failonstatus missing base status.\n"); |
| 667 | continue; |
| 668 | } |
| 669 | bool baseStatuses[DiffResource::kStatusCount]; |
| 670 | if (!DiffResource::getMatchingStatuses(argv[i], baseStatuses)) { |
| 671 | SkDebugf("unrecognized base status <%s>\n", argv[i]); |
| 672 | } |
| 673 | |
| 674 | if (argc == ++i) { |
| 675 | SkDebugf("failonstatus missing comparison status.\n"); |
| 676 | continue; |
| 677 | } |
| 678 | bool comparisonStatuses[DiffResource::kStatusCount]; |
| 679 | if (!DiffResource::getMatchingStatuses(argv[i], comparisonStatuses)) { |
| 680 | SkDebugf("unrecognized comarison status <%s>\n", argv[i]); |
| 681 | } |
| 682 | |
| 683 | for (int base = 0; base < DiffResource::kStatusCount; ++base) { |
| 684 | for (int comparison = 0; comparison < DiffResource::kStatusCount; ++comparison) { |
| 685 | failOnStatusType[base][comparison] |= |
| 686 | baseStatuses[base] && comparisonStatuses[comparison]; |
| 687 | } |
| 688 | } |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 689 | continue; |
| 690 | } |
epoger@google.com | 46a4596 | 2012-07-12 18:16:02 +0000 | [diff] [blame] | 691 | if (!strcmp(argv[i], "--help")) { |
| 692 | usage(argv[0]); |
epoger@google.com | 70044cc | 2012-07-12 18:37:55 +0000 | [diff] [blame] | 693 | return kNoError; |
epoger@google.com | 46a4596 | 2012-07-12 18:16:02 +0000 | [diff] [blame] | 694 | } |
| 695 | if (!strcmp(argv[i], "--listfilenames")) { |
| 696 | listFilenames = true; |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 697 | continue; |
| 698 | } |
commit-bot@chromium.org | 93d7bb6 | 2014-05-28 18:26:00 +0000 | [diff] [blame] | 699 | if (!strcmp(argv[i], "--verbose")) { |
| 700 | verbose = true; |
| 701 | continue; |
| 702 | } |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 703 | if (!strcmp(argv[i], "--match")) { |
| 704 | matchSubstrings.push(new SkString(argv[++i])); |
| 705 | continue; |
| 706 | } |
epoger@google.com | 46a4596 | 2012-07-12 18:16:02 +0000 | [diff] [blame] | 707 | if (!strcmp(argv[i], "--nodiffs")) { |
| 708 | generateDiffs = false; |
| 709 | continue; |
| 710 | } |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 711 | if (!strcmp(argv[i], "--nomatch")) { |
| 712 | nomatchSubstrings.push(new SkString(argv[++i])); |
| 713 | continue; |
| 714 | } |
epoger@google.com | 46a4596 | 2012-07-12 18:16:02 +0000 | [diff] [blame] | 715 | if (!strcmp(argv[i], "--noprintdirs")) { |
epoger@google.com | 71329d8 | 2012-08-16 13:42:13 +0000 | [diff] [blame] | 716 | printDirNames = false; |
| 717 | continue; |
| 718 | } |
| 719 | if (!strcmp(argv[i], "--norecurse")) { |
| 720 | recurseIntoSubdirs = false; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 721 | continue; |
| 722 | } |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 723 | if (!strcmp(argv[i], "--sortbymaxmismatch")) { |
epoger@google.com | 28060e7 | 2012-06-28 16:47:34 +0000 | [diff] [blame] | 724 | sortProc = compare<CompareDiffMaxMismatches>; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 725 | continue; |
| 726 | } |
epoger@google.com | 46a4596 | 2012-07-12 18:16:02 +0000 | [diff] [blame] | 727 | if (!strcmp(argv[i], "--sortbymismatch")) { |
| 728 | sortProc = compare<CompareDiffMeanMismatches>; |
tomhudson@google.com | 5b32529 | 2011-05-24 19:41:13 +0000 | [diff] [blame] | 729 | continue; |
| 730 | } |
epoger@google.com | 46a4596 | 2012-07-12 18:16:02 +0000 | [diff] [blame] | 731 | if (!strcmp(argv[i], "--threshold")) { |
| 732 | colorThreshold = atoi(argv[++i]); |
| 733 | continue; |
| 734 | } |
| 735 | if (!strcmp(argv[i], "--weighted")) { |
| 736 | sortProc = compare<CompareDiffWeighted>; |
keyar@chromium.org | a631819 | 2012-07-09 21:01:50 +0000 | [diff] [blame] | 737 | continue; |
| 738 | } |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 739 | if (argv[i][0] != '-') { |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 740 | switch (numUnflaggedArguments++) { |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 741 | case 0: |
| 742 | baseDir.set(argv[i]); |
| 743 | continue; |
| 744 | case 1: |
| 745 | comparisonDir.set(argv[i]); |
| 746 | continue; |
| 747 | case 2: |
| 748 | outputDir.set(argv[i]); |
| 749 | continue; |
| 750 | default: |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 751 | SkDebugf("extra unflagged argument <%s>\n", argv[i]); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 752 | usage(argv[0]); |
epoger@google.com | 70044cc | 2012-07-12 18:37:55 +0000 | [diff] [blame] | 753 | return kGenericError; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 754 | } |
| 755 | } |
reed | 3a3baf6 | 2015-01-06 07:39:55 -0800 | [diff] [blame] | 756 | if (!strcmp(argv[i], "--listFailingBase")) { |
| 757 | listFailingBase = true; |
| 758 | continue; |
| 759 | } |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 760 | |
| 761 | SkDebugf("Unrecognized argument <%s>\n", argv[i]); |
| 762 | usage(argv[0]); |
epoger@google.com | 70044cc | 2012-07-12 18:37:55 +0000 | [diff] [blame] | 763 | return kGenericError; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 764 | } |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 765 | |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 766 | if (numUnflaggedArguments == 2) { |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 767 | outputDir = comparisonDir; |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 768 | } else if (numUnflaggedArguments != 3) { |
tomhudson@google.com | 9dc527b | 2011-06-09 15:47:10 +0000 | [diff] [blame] | 769 | usage(argv[0]); |
epoger@google.com | 70044cc | 2012-07-12 18:37:55 +0000 | [diff] [blame] | 770 | return kGenericError; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 771 | } |
| 772 | |
bsalomon@google.com | 1a315fe | 2011-09-23 14:56:37 +0000 | [diff] [blame] | 773 | if (!baseDir.endsWith(PATH_DIV_STR)) { |
| 774 | baseDir.append(PATH_DIV_STR); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 775 | } |
epoger@google.com | 71329d8 | 2012-08-16 13:42:13 +0000 | [diff] [blame] | 776 | if (printDirNames) { |
keyar@chromium.org | a631819 | 2012-07-09 21:01:50 +0000 | [diff] [blame] | 777 | printf("baseDir is [%s]\n", baseDir.c_str()); |
| 778 | } |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 779 | |
bsalomon@google.com | 1a315fe | 2011-09-23 14:56:37 +0000 | [diff] [blame] | 780 | if (!comparisonDir.endsWith(PATH_DIV_STR)) { |
| 781 | comparisonDir.append(PATH_DIV_STR); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 782 | } |
epoger@google.com | 71329d8 | 2012-08-16 13:42:13 +0000 | [diff] [blame] | 783 | if (printDirNames) { |
keyar@chromium.org | a631819 | 2012-07-09 21:01:50 +0000 | [diff] [blame] | 784 | printf("comparisonDir is [%s]\n", comparisonDir.c_str()); |
| 785 | } |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 786 | |
bsalomon@google.com | 1a315fe | 2011-09-23 14:56:37 +0000 | [diff] [blame] | 787 | if (!outputDir.endsWith(PATH_DIV_STR)) { |
| 788 | outputDir.append(PATH_DIV_STR); |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 789 | } |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 790 | if (generateDiffs) { |
epoger@google.com | 71329d8 | 2012-08-16 13:42:13 +0000 | [diff] [blame] | 791 | if (printDirNames) { |
keyar@chromium.org | a631819 | 2012-07-09 21:01:50 +0000 | [diff] [blame] | 792 | printf("writing diffs to outputDir is [%s]\n", outputDir.c_str()); |
| 793 | } |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 794 | } else { |
epoger@google.com | 71329d8 | 2012-08-16 13:42:13 +0000 | [diff] [blame] | 795 | if (printDirNames) { |
keyar@chromium.org | a631819 | 2012-07-09 21:01:50 +0000 | [diff] [blame] | 796 | printf("not writing any diffs to outputDir [%s]\n", outputDir.c_str()); |
| 797 | } |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 798 | outputDir.set(""); |
| 799 | } |
| 800 | |
epoger@google.com | da4af24 | 2012-06-25 18:45:50 +0000 | [diff] [blame] | 801 | // If no matchSubstrings were specified, match ALL strings |
| 802 | // (except for whatever nomatchSubstrings were specified, if any). |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 803 | if (matchSubstrings.isEmpty()) { |
| 804 | matchSubstrings.push(new SkString("")); |
| 805 | } |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 806 | |
epoger@google.com | a611c3e | 2012-05-18 20:10:06 +0000 | [diff] [blame] | 807 | create_diff_images(diffProc, colorThreshold, &differences, |
| 808 | baseDir, comparisonDir, outputDir, |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 809 | matchSubstrings, nomatchSubstrings, recurseIntoSubdirs, generateDiffs, |
commit-bot@chromium.org | 93d7bb6 | 2014-05-28 18:26:00 +0000 | [diff] [blame] | 810 | verbose, &summary); |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 811 | summary.print(listFilenames, failOnResultType, failOnStatusType); |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 812 | |
reed | 3a3baf6 | 2015-01-06 07:39:55 -0800 | [diff] [blame] | 813 | if (listFailingBase) { |
| 814 | summary.printfFailingBaseNames("\n"); |
| 815 | } |
| 816 | |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 817 | if (differences.count()) { |
reed@google.com | c7a67cb | 2012-05-07 14:52:12 +0000 | [diff] [blame] | 818 | qsort(differences.begin(), differences.count(), |
| 819 | sizeof(DiffRecord*), sortProc); |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 820 | } |
epoger@google.com | 6600852 | 2012-05-16 17:40:57 +0000 | [diff] [blame] | 821 | |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 822 | if (generateDiffs) { |
| 823 | print_diff_page(summary.fNumMatches, colorThreshold, differences, |
| 824 | baseDir, comparisonDir, outputDir); |
| 825 | } |
epoger@google.com | 76222c0 | 2012-05-31 15:12:09 +0000 | [diff] [blame] | 826 | |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 827 | for (i = 0; i < differences.count(); i++) { |
| 828 | delete differences[i]; |
| 829 | } |
epoger@google.com | a5f406e | 2012-05-01 13:26:16 +0000 | [diff] [blame] | 830 | matchSubstrings.deleteAll(); |
| 831 | nomatchSubstrings.deleteAll(); |
epoger@google.com | be6188d | 2012-05-31 15:13:45 +0000 | [diff] [blame] | 832 | |
epoger@google.com | dfbf24e | 2012-07-13 21:22:02 +0000 | [diff] [blame] | 833 | int num_failing_results = 0; |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 834 | for (int i = 0; i < DiffRecord::kResultCount; i++) { |
epoger@google.com | 3af4ff4 | 2012-07-19 17:35:04 +0000 | [diff] [blame] | 835 | if (failOnResultType[i]) { |
| 836 | num_failing_results += summary.fResultsOfType[i].count(); |
| 837 | } |
epoger@google.com | 46a4596 | 2012-07-12 18:16:02 +0000 | [diff] [blame] | 838 | } |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 839 | if (!failOnResultType[DiffRecord::kCouldNotCompare_Result]) { |
| 840 | for (int base = 0; base < DiffResource::kStatusCount; ++base) { |
| 841 | for (int comparison = 0; comparison < DiffResource::kStatusCount; ++comparison) { |
| 842 | if (failOnStatusType[base][comparison]) { |
| 843 | num_failing_results += summary.fStatusOfType[base][comparison].count(); |
| 844 | } |
| 845 | } |
| 846 | } |
| 847 | } |
epoger@google.com | 2865988 | 2012-07-16 18:01:06 +0000 | [diff] [blame] | 848 | |
| 849 | // On Linux (and maybe other platforms too), any results outside of the |
| 850 | // range [0...255] are wrapped (mod 256). Do the conversion ourselves, to |
| 851 | // make sure that we only return 0 when there were no failures. |
| 852 | return (num_failing_results > 255) ? 255 : num_failing_results; |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 853 | } |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 854 | |
| 855 | #if !defined SK_BUILD_FOR_IOS |
| 856 | int main(int argc, char * const argv[]) { |
| 857 | return tool_main(argc, (char**) argv); |
| 858 | } |
| 859 | #endif |