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