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