blob: 00bcdfcda04687bb0eff69114f10d3ac8608c51d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.come3c8ddf2012-12-05 20:13:12 +00007#include "skdiff.h"
8#include "skdiff_html.h"
9#include "skdiff_utils.h"
10#include "SkBitmap.h"
epoger@google.com46256ea2012-05-22 13:45:35 +000011#include "SkData.h"
zachr@google.com6ef5a852013-06-18 21:23:31 +000012#include "SkForceLinking.h"
tomhudson@google.com4b33d282011-04-27 15:39:30 +000013#include "SkImageDecoder.h"
14#include "SkImageEncoder.h"
15#include "SkOSFile.h"
16#include "SkStream.h"
17#include "SkTDArray.h"
tomhudson@google.com4b33d282011-04-27 15:39:30 +000018#include "SkTSearch.h"
tomhudson@google.com4b33d282011-04-27 15:39:30 +000019
bungeman60e0fee2015-08-26 05:15:46 -070020#include <stdlib.h>
21
zachr@google.com6ef5a852013-06-18 21:23:31 +000022__SK_FORCE_IMAGE_DECODER_LINKING;
23
tomhudson@google.com4b33d282011-04-27 15:39:30 +000024/**
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.com7d042802011-07-14 13:15:55 +000031 * Creates an index.html in the current third directory to compare each
tomhudson@google.com4b33d282011-04-27 15:39:30 +000032 * pair that does not match exactly.
epoger@google.com71329d82012-08-16 13:42:13 +000033 * Recursively descends directories, unless run with --norecurse.
epoger@google.combe6188d2012-05-31 15:13:45 +000034 *
35 * Returns zero exit code if all images match across baseDir and comparisonDir.
tomhudson@google.com4b33d282011-04-27 15:39:30 +000036 */
37
epoger@google.coma5f406e2012-05-01 13:26:16 +000038typedef SkTDArray<SkString*> StringArray;
39typedef StringArray FileArray;
epoger@google.com5fd53852012-03-22 18:20:06 +000040
reed3a3baf62015-01-06 07:39:55 -080041static void add_unique_basename(StringArray* array, const SkString& filename) {
42 // trim off dirs
43 const char* src = filename.c_str();
44 const char* trimmed = strrchr(src, SkPATH_SEPARATOR);
45 if (trimmed) {
46 trimmed += 1; // skip the separator
47 } else {
48 trimmed = src;
49 }
50 const char* end = strrchr(trimmed, '.');
51 if (!end) {
52 end = trimmed + strlen(trimmed);
53 }
54 SkString result(trimmed, end - trimmed);
55
56 // only add unique entries
57 for (int i = 0; i < array->count(); ++i) {
58 if (*array->getAt(i) == result) {
59 return;
60 }
61 }
62 *array->append() = new SkString(result);
63}
64
tomhudson@google.com9dc527b2011-06-09 15:47:10 +000065struct DiffSummary {
66 DiffSummary ()
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000067 : fNumMatches(0)
68 , fNumMismatches(0)
69 , fMaxMismatchV(0)
70 , fMaxMismatchPercent(0) { };
tomhudson@google.com9dc527b2011-06-09 15:47:10 +000071
epoger@google.com5fd53852012-03-22 18:20:06 +000072 ~DiffSummary() {
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000073 for (int i = 0; i < DiffRecord::kResultCount; ++i) {
epoger@google.com76222c02012-05-31 15:12:09 +000074 fResultsOfType[i].deleteAll();
75 }
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000076 for (int base = 0; base < DiffResource::kStatusCount; ++base) {
77 for (int comparison = 0; comparison < DiffResource::kStatusCount; ++comparison) {
78 fStatusOfType[base][comparison].deleteAll();
79 }
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +000080 }
epoger@google.com5fd53852012-03-22 18:20:06 +000081 }
82
tomhudson@google.com9dc527b2011-06-09 15:47:10 +000083 uint32_t fNumMatches;
84 uint32_t fNumMismatches;
85 uint32_t fMaxMismatchV;
86 float fMaxMismatchPercent;
87
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000088 FileArray fResultsOfType[DiffRecord::kResultCount];
89 FileArray fStatusOfType[DiffResource::kStatusCount][DiffResource::kStatusCount];
90
reed3a3baf62015-01-06 07:39:55 -080091 StringArray fFailedBaseNames[DiffRecord::kResultCount];
92
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000093 void printContents(const FileArray& fileArray,
94 const char* baseStatus, const char* comparisonStatus,
95 bool listFilenames) {
96 int n = fileArray.count();
97 printf("%d file pairs %s in baseDir and %s in comparisonDir",
98 n, baseStatus, comparisonStatus);
99 if (listFilenames) {
100 printf(": ");
101 for (int i = 0; i < n; ++i) {
102 printf("%s ", fileArray[i]->c_str());
103 }
104 }
105 printf("\n");
106 }
107
108 void printStatus(bool listFilenames,
109 bool failOnStatusType[DiffResource::kStatusCount]
110 [DiffResource::kStatusCount]) {
111 typedef DiffResource::Status Status;
112
113 for (int base = 0; base < DiffResource::kStatusCount; ++base) {
114 Status baseStatus = static_cast<Status>(base);
115 for (int comparison = 0; comparison < DiffResource::kStatusCount; ++comparison) {
116 Status comparisonStatus = static_cast<Status>(comparison);
117 const FileArray& fileArray = fStatusOfType[base][comparison];
118 if (fileArray.count() > 0) {
119 if (failOnStatusType[base][comparison]) {
120 printf(" [*] ");
121 } else {
122 printf(" [_] ");
123 }
124 printContents(fileArray,
125 DiffResource::getStatusDescription(baseStatus),
126 DiffResource::getStatusDescription(comparisonStatus),
127 listFilenames);
128 }
129 }
130 }
131 }
epoger@google.com76222c02012-05-31 15:12:09 +0000132
epoger@google.com3af4ff42012-07-19 17:35:04 +0000133 // Print a line about the contents of this FileArray to stdout.
epoger@google.com46a45962012-07-12 18:16:02 +0000134 void printContents(const FileArray& fileArray, const char* headerText, bool listFilenames) {
epoger@google.com76222c02012-05-31 15:12:09 +0000135 int n = fileArray.count();
epoger@google.com3af4ff42012-07-19 17:35:04 +0000136 printf("%d file pairs %s", n, headerText);
137 if (listFilenames) {
138 printf(": ");
139 for (int i = 0; i < n; ++i) {
140 printf("%s ", fileArray[i]->c_str());
epoger@google.com76222c02012-05-31 15:12:09 +0000141 }
142 }
epoger@google.com3af4ff42012-07-19 17:35:04 +0000143 printf("\n");
epoger@google.com76222c02012-05-31 15:12:09 +0000144 }
epoger@google.com5fd53852012-03-22 18:20:06 +0000145
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000146 void print(bool listFilenames, bool failOnResultType[DiffRecord::kResultCount],
147 bool failOnStatusType[DiffResource::kStatusCount]
148 [DiffResource::kStatusCount]) {
epoger@google.com3af4ff42012-07-19 17:35:04 +0000149 printf("\ncompared %d file pairs:\n", fNumMatches + fNumMismatches);
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000150 for (int resultInt = 0; resultInt < DiffRecord::kResultCount; ++resultInt) {
151 DiffRecord::Result result = static_cast<DiffRecord::Result>(resultInt);
epoger@google.com3af4ff42012-07-19 17:35:04 +0000152 if (failOnResultType[result]) {
153 printf("[*] ");
154 } else {
155 printf("[_] ");
156 }
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000157 printContents(fResultsOfType[result], DiffRecord::getResultDescription(result),
158 listFilenames);
159 if (DiffRecord::kCouldNotCompare_Result == result) {
160 printStatus(listFilenames, failOnStatusType);
161 }
epoger@google.com46a45962012-07-12 18:16:02 +0000162 }
epoger@google.com3af4ff42012-07-19 17:35:04 +0000163 printf("(results marked with [*] will cause nonzero return value)\n");
164 printf("\nnumber of mismatching file pairs: %d\n", fNumMismatches);
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000165 if (fNumMismatches > 0) {
166 printf("Maximum pixel intensity mismatch %d\n", fMaxMismatchV);
epoger@google.com46a45962012-07-12 18:16:02 +0000167 printf("Largest area mismatch was %.2f%% of pixels\n",fMaxMismatchPercent);
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000168 }
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000169 }
170
reed3a3baf62015-01-06 07:39:55 -0800171 void printfFailingBaseNames(const char separator[]) {
172 for (int resultInt = 0; resultInt < DiffRecord::kResultCount; ++resultInt) {
173 const StringArray& array = fFailedBaseNames[resultInt];
174 if (array.count()) {
175 printf("%s [%d]%s", DiffRecord::ResultNames[resultInt], array.count(), separator);
176 for (int j = 0; j < array.count(); ++j) {
177 printf("%s%s", array[j]->c_str(), separator);
178 }
179 printf("\n");
180 }
181 }
182 }
183
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000184 void add (DiffRecord* drp) {
epoger@google.com46256ea2012-05-22 13:45:35 +0000185 uint32_t mismatchValue;
epoger@google.com292aff62012-05-16 14:57:28 +0000186
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000187 if (drp->fBase.fFilename.equals(drp->fComparison.fFilename)) {
188 fResultsOfType[drp->fResult].push(new SkString(drp->fBase.fFilename));
189 } else {
190 SkString* blame = new SkString("(");
191 blame->append(drp->fBase.fFilename);
192 blame->append(", ");
193 blame->append(drp->fComparison.fFilename);
194 blame->append(")");
195 fResultsOfType[drp->fResult].push(blame);
196 }
epoger@google.com292aff62012-05-16 14:57:28 +0000197 switch (drp->fResult) {
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000198 case DiffRecord::kEqualBits_Result:
epoger@google.com46256ea2012-05-22 13:45:35 +0000199 fNumMatches++;
200 break;
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000201 case DiffRecord::kEqualPixels_Result:
epoger@google.com292aff62012-05-16 14:57:28 +0000202 fNumMatches++;
203 break;
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000204 case DiffRecord::kDifferentSizes_Result:
epoger@google.com5fd53852012-03-22 18:20:06 +0000205 fNumMismatches++;
epoger@google.com292aff62012-05-16 14:57:28 +0000206 break;
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000207 case DiffRecord::kDifferentPixels_Result:
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000208 fNumMismatches++;
209 if (drp->fFractionDifference * 100 > fMaxMismatchPercent) {
210 fMaxMismatchPercent = drp->fFractionDifference * 100;
211 }
epoger@google.com46256ea2012-05-22 13:45:35 +0000212 mismatchValue = MAX3(drp->fMaxMismatchR, drp->fMaxMismatchG,
213 drp->fMaxMismatchB);
214 if (mismatchValue > fMaxMismatchV) {
215 fMaxMismatchV = mismatchValue;
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000216 }
epoger@google.com292aff62012-05-16 14:57:28 +0000217 break;
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000218 case DiffRecord::kCouldNotCompare_Result:
epoger@google.com46256ea2012-05-22 13:45:35 +0000219 fNumMismatches++;
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000220 fStatusOfType[drp->fBase.fStatus][drp->fComparison.fStatus].push(
221 new SkString(drp->fBase.fFilename));
epoger@google.com46256ea2012-05-22 13:45:35 +0000222 break;
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000223 case DiffRecord::kUnknown_Result:
epoger@google.com46256ea2012-05-22 13:45:35 +0000224 SkDEBUGFAIL("adding uncategorized DiffRecord");
225 break;
226 default:
227 SkDEBUGFAIL("adding DiffRecord with unhandled fResult value");
228 break;
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000229 }
reed3a3baf62015-01-06 07:39:55 -0800230
231 switch (drp->fResult) {
232 case DiffRecord::kEqualBits_Result:
233 case DiffRecord::kEqualPixels_Result:
234 break;
235 default:
236 add_unique_basename(&fFailedBaseNames[drp->fResult], drp->fBase.fFilename);
237 break;
238 }
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000239 }
240};
241
epoger@google.coma5f406e2012-05-01 13:26:16 +0000242/// Returns true if string contains any of these substrings.
243static bool string_contains_any_of(const SkString& string,
244 const StringArray& substrings) {
245 for (int i = 0; i < substrings.count(); i++) {
246 if (string.contains(substrings[i]->c_str())) {
247 return true;
248 }
249 }
250 return false;
251}
252
epoger@google.com71329d82012-08-16 13:42:13 +0000253/// Internal (potentially recursive) implementation of get_file_list.
254static void get_file_list_subdir(const SkString& rootDir, const SkString& subDir,
255 const StringArray& matchSubstrings,
256 const StringArray& nomatchSubstrings,
257 bool recurseIntoSubdirs, FileArray *files) {
258 bool isSubDirEmpty = subDir.isEmpty();
259 SkString dir(rootDir);
260 if (!isSubDirEmpty) {
261 dir.append(PATH_DIV_STR);
262 dir.append(subDir);
263 }
264
265 // Iterate over files (not directories) within dir.
266 SkOSFile::Iter fileIterator(dir.c_str());
267 SkString fileName;
268 while (fileIterator.next(&fileName, false)) {
269 if (fileName.startsWith(".")) {
270 continue;
271 }
272 SkString pathRelativeToRootDir(subDir);
273 if (!isSubDirEmpty) {
274 pathRelativeToRootDir.append(PATH_DIV_STR);
275 }
276 pathRelativeToRootDir.append(fileName);
277 if (string_contains_any_of(pathRelativeToRootDir, matchSubstrings) &&
278 !string_contains_any_of(pathRelativeToRootDir, nomatchSubstrings)) {
279 files->push(new SkString(pathRelativeToRootDir));
280 }
281 }
282
283 // Recurse into any non-ignored subdirectories.
284 if (recurseIntoSubdirs) {
285 SkOSFile::Iter dirIterator(dir.c_str());
286 SkString dirName;
287 while (dirIterator.next(&dirName, true)) {
288 if (dirName.startsWith(".")) {
289 continue;
290 }
291 SkString pathRelativeToRootDir(subDir);
292 if (!isSubDirEmpty) {
293 pathRelativeToRootDir.append(PATH_DIV_STR);
294 }
295 pathRelativeToRootDir.append(dirName);
296 if (!string_contains_any_of(pathRelativeToRootDir, nomatchSubstrings)) {
297 get_file_list_subdir(rootDir, pathRelativeToRootDir,
298 matchSubstrings, nomatchSubstrings, recurseIntoSubdirs,
299 files);
300 }
301 }
302 }
303}
304
305/// Iterate over dir and get all files whose filename:
306/// - matches any of the substrings in matchSubstrings, but...
307/// - DOES NOT match any of the substrings in nomatchSubstrings
308/// - DOES NOT start with a dot (.)
309/// Adds the matching files to the list in *files.
epoger@google.coma5f406e2012-05-01 13:26:16 +0000310static void get_file_list(const SkString& dir,
311 const StringArray& matchSubstrings,
312 const StringArray& nomatchSubstrings,
epoger@google.com71329d82012-08-16 13:42:13 +0000313 bool recurseIntoSubdirs, FileArray *files) {
314 get_file_list_subdir(dir, SkString(""),
315 matchSubstrings, nomatchSubstrings, recurseIntoSubdirs,
316 files);
epoger@google.com5fd53852012-03-22 18:20:06 +0000317}
318
319static void release_file_list(FileArray *files) {
320 files->deleteAll();
321}
322
323/// Comparison routines for qsort, sort by file names.
324static int compare_file_name_metrics(SkString **lhs, SkString **rhs) {
325 return strcmp((*lhs)->c_str(), (*rhs)->c_str());
326}
327
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000328class AutoReleasePixels {
329public:
330 AutoReleasePixels(DiffRecord* drp)
331 : fDrp(drp) {
halcanary96fcdcc2015-08-27 07:41:13 -0700332 SkASSERT(drp != nullptr);
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000333 }
334 ~AutoReleasePixels() {
halcanary96fcdcc2015-08-27 07:41:13 -0700335 fDrp->fBase.fBitmap.setPixelRef(nullptr);
336 fDrp->fComparison.fBitmap.setPixelRef(nullptr);
337 fDrp->fDifference.fBitmap.setPixelRef(nullptr);
338 fDrp->fWhite.fBitmap.setPixelRef(nullptr);
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000339 }
340
341private:
342 DiffRecord* fDrp;
343};
344
345static void get_bounds(DiffResource& resource, const char* name) {
346 if (resource.fBitmap.empty() && !DiffResource::isStatusFailed(resource.fStatus)) {
347 SkAutoDataUnref fileBits(read_file(resource.fFullPath.c_str()));
halcanary96fcdcc2015-08-27 07:41:13 -0700348 if (nullptr == fileBits) {
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000349 SkDebugf("WARNING: couldn't read %s file <%s>\n", name, resource.fFullPath.c_str());
350 resource.fStatus = DiffResource::kCouldNotRead_Status;
351 } else {
352 get_bitmap(fileBits, resource, SkImageDecoder::kDecodeBounds_Mode);
353 }
354 }
355}
356
357static void get_bounds(DiffRecord& drp) {
358 get_bounds(drp.fBase, "base");
359 get_bounds(drp.fComparison, "comparison");
360}
361
commit-bot@chromium.org93d7bb62014-05-28 18:26:00 +0000362#ifdef SK_OS_WIN
363#define ANSI_COLOR_RED ""
364#define ANSI_COLOR_GREEN ""
365#define ANSI_COLOR_YELLOW ""
366#define ANSI_COLOR_RESET ""
367#else
368#define ANSI_COLOR_RED "\x1b[31m"
369#define ANSI_COLOR_GREEN "\x1b[32m"
370#define ANSI_COLOR_YELLOW "\x1b[33m"
371#define ANSI_COLOR_RESET "\x1b[0m"
372#endif
373
374#define VERBOSE_STATUS(status,color,filename) if (verbose) printf( "[ " color " %10s " ANSI_COLOR_RESET " ] %s\n", status, filename->c_str())
375
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000376/// Creates difference images, returns the number that have a 0 metric.
epoger@google.coma5f406e2012-05-01 13:26:16 +0000377/// If outputDir.isEmpty(), don't write out diff files.
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000378static void create_diff_images (DiffMetricProc dmp,
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000379 const int colorThreshold,
380 RecordArray* differences,
381 const SkString& baseDir,
382 const SkString& comparisonDir,
383 const SkString& outputDir,
epoger@google.coma5f406e2012-05-01 13:26:16 +0000384 const StringArray& matchSubstrings,
385 const StringArray& nomatchSubstrings,
epoger@google.com71329d82012-08-16 13:42:13 +0000386 bool recurseIntoSubdirs,
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000387 bool getBounds,
commit-bot@chromium.org93d7bb62014-05-28 18:26:00 +0000388 bool verbose,
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000389 DiffSummary* summary) {
epoger@google.coma5f406e2012-05-01 13:26:16 +0000390 SkASSERT(!baseDir.isEmpty());
391 SkASSERT(!comparisonDir.isEmpty());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000392
epoger@google.com5fd53852012-03-22 18:20:06 +0000393 FileArray baseFiles;
394 FileArray comparisonFiles;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000395
epoger@google.com71329d82012-08-16 13:42:13 +0000396 get_file_list(baseDir, matchSubstrings, nomatchSubstrings, recurseIntoSubdirs, &baseFiles);
397 get_file_list(comparisonDir, matchSubstrings, nomatchSubstrings, recurseIntoSubdirs,
epoger@google.coma5f406e2012-05-01 13:26:16 +0000398 &comparisonFiles);
epoger@google.com5fd53852012-03-22 18:20:06 +0000399
epoger@google.coma5f406e2012-05-01 13:26:16 +0000400 if (!baseFiles.isEmpty()) {
reed@google.comc7a67cb2012-05-07 14:52:12 +0000401 qsort(baseFiles.begin(), baseFiles.count(), sizeof(SkString*),
402 SkCastForQSort(compare_file_name_metrics));
epoger@google.coma5f406e2012-05-01 13:26:16 +0000403 }
404 if (!comparisonFiles.isEmpty()) {
reed@google.comc7a67cb2012-05-07 14:52:12 +0000405 qsort(comparisonFiles.begin(), comparisonFiles.count(),
406 sizeof(SkString*), SkCastForQSort(compare_file_name_metrics));
epoger@google.coma5f406e2012-05-01 13:26:16 +0000407 }
epoger@google.com66008522012-05-16 17:40:57 +0000408
epoger@google.com5fd53852012-03-22 18:20:06 +0000409 int i = 0;
410 int j = 0;
411
412 while (i < baseFiles.count() &&
413 j < comparisonFiles.count()) {
414
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000415 SkString basePath(baseDir);
416 SkString comparisonPath(comparisonDir);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000417
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000418 DiffRecord *drp = new DiffRecord;
419 int v = strcmp(baseFiles[i]->c_str(), comparisonFiles[j]->c_str());
epoger@google.com5fd53852012-03-22 18:20:06 +0000420
421 if (v < 0) {
422 // in baseDir, but not in comparisonDir
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000423 drp->fResult = DiffRecord::kCouldNotCompare_Result;
424
425 basePath.append(*baseFiles[i]);
426 comparisonPath.append(*baseFiles[i]);
427
428 drp->fBase.fFilename = *baseFiles[i];
429 drp->fBase.fFullPath = basePath;
430 drp->fBase.fStatus = DiffResource::kExists_Status;
431
432 drp->fComparison.fFilename = *baseFiles[i];
433 drp->fComparison.fFullPath = comparisonPath;
434 drp->fComparison.fStatus = DiffResource::kDoesNotExist_Status;
435
commit-bot@chromium.org93d7bb62014-05-28 18:26:00 +0000436 VERBOSE_STATUS("MISSING", ANSI_COLOR_YELLOW, baseFiles[i]);
437
epoger@google.com5fd53852012-03-22 18:20:06 +0000438 ++i;
439 } else if (v > 0) {
440 // in comparisonDir, but not in baseDir
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000441 drp->fResult = DiffRecord::kCouldNotCompare_Result;
442
443 basePath.append(*comparisonFiles[j]);
444 comparisonPath.append(*comparisonFiles[j]);
445
446 drp->fBase.fFilename = *comparisonFiles[j];
447 drp->fBase.fFullPath = basePath;
448 drp->fBase.fStatus = DiffResource::kDoesNotExist_Status;
449
450 drp->fComparison.fFilename = *comparisonFiles[j];
451 drp->fComparison.fFullPath = comparisonPath;
452 drp->fComparison.fStatus = DiffResource::kExists_Status;
453
commit-bot@chromium.org93d7bb62014-05-28 18:26:00 +0000454 VERBOSE_STATUS("MISSING", ANSI_COLOR_YELLOW, comparisonFiles[j]);
455
epoger@google.com5fd53852012-03-22 18:20:06 +0000456 ++j;
457 } else {
epoger@google.com46256ea2012-05-22 13:45:35 +0000458 // Found the same filename in both baseDir and comparisonDir.
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000459 SkASSERT(DiffRecord::kUnknown_Result == drp->fResult);
epoger@google.com5fd53852012-03-22 18:20:06 +0000460
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000461 basePath.append(*baseFiles[i]);
462 comparisonPath.append(*comparisonFiles[j]);
463
464 drp->fBase.fFilename = *baseFiles[i];
465 drp->fBase.fFullPath = basePath;
466 drp->fBase.fStatus = DiffResource::kExists_Status;
467
468 drp->fComparison.fFilename = *comparisonFiles[j];
469 drp->fComparison.fFullPath = comparisonPath;
470 drp->fComparison.fStatus = DiffResource::kExists_Status;
471
472 SkAutoDataUnref baseFileBits(read_file(drp->fBase.fFullPath.c_str()));
bsalomon49f085d2014-09-05 13:34:00 -0700473 if (baseFileBits) {
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000474 drp->fBase.fStatus = DiffResource::kRead_Status;
475 }
476 SkAutoDataUnref comparisonFileBits(read_file(drp->fComparison.fFullPath.c_str()));
bsalomon49f085d2014-09-05 13:34:00 -0700477 if (comparisonFileBits) {
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000478 drp->fComparison.fStatus = DiffResource::kRead_Status;
479 }
halcanary96fcdcc2015-08-27 07:41:13 -0700480 if (nullptr == baseFileBits || nullptr == comparisonFileBits) {
481 if (nullptr == baseFileBits) {
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000482 drp->fBase.fStatus = DiffResource::kCouldNotRead_Status;
commit-bot@chromium.org93d7bb62014-05-28 18:26:00 +0000483 VERBOSE_STATUS("READ FAIL", ANSI_COLOR_RED, baseFiles[i]);
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000484 }
halcanary96fcdcc2015-08-27 07:41:13 -0700485 if (nullptr == comparisonFileBits) {
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000486 drp->fComparison.fStatus = DiffResource::kCouldNotRead_Status;
commit-bot@chromium.org93d7bb62014-05-28 18:26:00 +0000487 VERBOSE_STATUS("READ FAIL", ANSI_COLOR_RED, comparisonFiles[j]);
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000488 }
489 drp->fResult = DiffRecord::kCouldNotCompare_Result;
490
491 } else if (are_buffers_equal(baseFileBits, comparisonFileBits)) {
492 drp->fResult = DiffRecord::kEqualBits_Result;
commit-bot@chromium.org93d7bb62014-05-28 18:26:00 +0000493 VERBOSE_STATUS("MATCH", ANSI_COLOR_GREEN, baseFiles[i]);
epoger@google.com46256ea2012-05-22 13:45:35 +0000494 } else {
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000495 AutoReleasePixels arp(drp);
496 get_bitmap(baseFileBits, drp->fBase, SkImageDecoder::kDecodePixels_Mode);
497 get_bitmap(comparisonFileBits, drp->fComparison,
498 SkImageDecoder::kDecodePixels_Mode);
commit-bot@chromium.org93d7bb62014-05-28 18:26:00 +0000499 VERBOSE_STATUS("DIFFERENT", ANSI_COLOR_RED, baseFiles[i]);
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000500 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.com46256ea2012-05-22 13:45:35 +0000504 } else {
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000505 drp->fResult = DiffRecord::kCouldNotCompare_Result;
epoger@google.com46256ea2012-05-22 13:45:35 +0000506 }
epoger@google.com5fd53852012-03-22 18:20:06 +0000507 }
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000508
epoger@google.com5fd53852012-03-22 18:20:06 +0000509 ++i;
510 ++j;
511 }
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000512
513 if (getBounds) {
514 get_bounds(*drp);
515 }
516 SkASSERT(DiffRecord::kUnknown_Result != drp->fResult);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000517 differences->push(drp);
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000518 summary->add(drp);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000519 }
epoger@google.com5fd53852012-03-22 18:20:06 +0000520
521 for (; i < baseFiles.count(); ++i) {
522 // files only in baseDir
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000523 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.com5fd53852012-03-22 18:20:06 +0000538 differences->push(drp);
539 summary->add(drp);
540 }
541
542 for (; j < comparisonFiles.count(); ++j) {
543 // files only in comparisonDir
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000544 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.com5fd53852012-03-22 18:20:06 +0000559 differences->push(drp);
560 summary->add(drp);
561 }
562
563 release_file_list(&baseFiles);
564 release_file_list(&comparisonFiles);
tomhudson@google.com7d042802011-07-14 13:15:55 +0000565}
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000566
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000567static void usage (char * argv0) {
568 SkDebugf("Skia baseline image diff tool\n");
epoger@google.coma5f406e2012-05-01 13:26:16 +0000569 SkDebugf("\n"
570"Usage: \n"
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000571" %s <baseDir> <comparisonDir> [outputDir] \n", argv0);
tomhudson@google.com7d042802011-07-14 13:15:55 +0000572 SkDebugf(
epoger@google.com46a45962012-07-12 18:16:02 +0000573"\nArguments:"
epoger@google.comdfbf24e2012-07-13 21:22:02 +0000574"\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.come3c8ddf2012-12-05 20:13:12 +0000580"\n --failonstatus <baseStatus> <comparisonStatus>: exit with nonzero return"
581"\n code if any file pairs yielded this status."
epoger@google.com46a45962012-07-12 18:16:02 +0000582"\n --help: display this info"
epoger@google.com46a45962012-07-12 18:16:02 +0000583"\n --listfilenames: list all filenames for each result type in stdout"
epoger@google.comdfbf24e2012-07-13 21:22:02 +0000584"\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.com46a45962012-07-12 18:16:02 +0000587"\n --nodiffs: don't write out image diffs or index.html, just generate"
588"\n report on stdout"
epoger@google.comdfbf24e2012-07-13 21:22:02 +0000589"\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.com46a45962012-07-12 18:16:02 +0000592"\n --noprintdirs: do not print the directories used."
epoger@google.com71329d82012-08-16 13:42:13 +0000593"\n --norecurse: do not recurse into subdirectories."
epoger@google.com46a45962012-07-12 18:16:02 +0000594"\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.com4b33d282011-04-27 15:39:30 +0000607}
608
epoger@google.com70044cc2012-07-12 18:37:55 +0000609const int kNoError = 0;
610const int kGenericError = -1;
epoger@google.com46a45962012-07-12 18:16:02 +0000611
caryclark@google.com5987f582012-10-02 18:33:14 +0000612int tool_main(int argc, char** argv);
613int tool_main(int argc, char** argv) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000614 DiffMetricProc diffProc = compute_diff_pmcolor;
epoger@google.com28060e72012-06-28 16:47:34 +0000615 int (*sortProc)(const void*, const void*) = compare<CompareDiffMetrics>;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000616
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.comdfbf24e2012-07-13 21:22:02 +0000623
epoger@google.coma5f406e2012-05-01 13:26:16 +0000624 StringArray matchSubstrings;
625 StringArray nomatchSubstrings;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000626
epoger@google.coma5f406e2012-05-01 13:26:16 +0000627 bool generateDiffs = true;
epoger@google.com46a45962012-07-12 18:16:02 +0000628 bool listFilenames = false;
epoger@google.com71329d82012-08-16 13:42:13 +0000629 bool printDirNames = true;
630 bool recurseIntoSubdirs = true;
commit-bot@chromium.org93d7bb62014-05-28 18:26:00 +0000631 bool verbose = false;
reed3a3baf62015-01-06 07:39:55 -0800632 bool listFailingBase = false;
tomhudson@google.com7d042802011-07-14 13:15:55 +0000633
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000634 RecordArray differences;
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000635 DiffSummary summary;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000636
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000637 bool failOnResultType[DiffRecord::kResultCount];
638 for (int i = 0; i < DiffRecord::kResultCount; i++) {
epoger@google.com3af4ff42012-07-19 17:35:04 +0000639 failOnResultType[i] = false;
640 }
641
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000642 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.coma5f406e2012-05-01 13:26:16 +0000649 int i;
650 int numUnflaggedArguments = 0;
651 for (i = 1; i < argc; i++) {
epoger@google.comdfbf24e2012-07-13 21:22:02 +0000652 if (!strcmp(argv[i], "--failonresult")) {
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000653 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.coma5f406e2012-05-01 13:26:16 +0000690 continue;
691 }
epoger@google.com46a45962012-07-12 18:16:02 +0000692 if (!strcmp(argv[i], "--help")) {
693 usage(argv[0]);
epoger@google.com70044cc2012-07-12 18:37:55 +0000694 return kNoError;
epoger@google.com46a45962012-07-12 18:16:02 +0000695 }
696 if (!strcmp(argv[i], "--listfilenames")) {
697 listFilenames = true;
epoger@google.coma5f406e2012-05-01 13:26:16 +0000698 continue;
699 }
commit-bot@chromium.org93d7bb62014-05-28 18:26:00 +0000700 if (!strcmp(argv[i], "--verbose")) {
701 verbose = true;
702 continue;
703 }
epoger@google.coma5f406e2012-05-01 13:26:16 +0000704 if (!strcmp(argv[i], "--match")) {
705 matchSubstrings.push(new SkString(argv[++i]));
706 continue;
707 }
epoger@google.com46a45962012-07-12 18:16:02 +0000708 if (!strcmp(argv[i], "--nodiffs")) {
709 generateDiffs = false;
710 continue;
711 }
epoger@google.coma5f406e2012-05-01 13:26:16 +0000712 if (!strcmp(argv[i], "--nomatch")) {
713 nomatchSubstrings.push(new SkString(argv[++i]));
714 continue;
715 }
epoger@google.com46a45962012-07-12 18:16:02 +0000716 if (!strcmp(argv[i], "--noprintdirs")) {
epoger@google.com71329d82012-08-16 13:42:13 +0000717 printDirNames = false;
718 continue;
719 }
720 if (!strcmp(argv[i], "--norecurse")) {
721 recurseIntoSubdirs = false;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000722 continue;
723 }
tomhudson@google.com7d042802011-07-14 13:15:55 +0000724 if (!strcmp(argv[i], "--sortbymaxmismatch")) {
epoger@google.com28060e72012-06-28 16:47:34 +0000725 sortProc = compare<CompareDiffMaxMismatches>;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000726 continue;
727 }
epoger@google.com46a45962012-07-12 18:16:02 +0000728 if (!strcmp(argv[i], "--sortbymismatch")) {
729 sortProc = compare<CompareDiffMeanMismatches>;
tomhudson@google.com5b325292011-05-24 19:41:13 +0000730 continue;
731 }
epoger@google.com46a45962012-07-12 18:16:02 +0000732 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.orga6318192012-07-09 21:01:50 +0000738 continue;
739 }
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000740 if (argv[i][0] != '-') {
epoger@google.coma5f406e2012-05-01 13:26:16 +0000741 switch (numUnflaggedArguments++) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000742 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.coma5f406e2012-05-01 13:26:16 +0000752 SkDebugf("extra unflagged argument <%s>\n", argv[i]);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000753 usage(argv[0]);
epoger@google.com70044cc2012-07-12 18:37:55 +0000754 return kGenericError;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000755 }
756 }
reed3a3baf62015-01-06 07:39:55 -0800757 if (!strcmp(argv[i], "--listFailingBase")) {
758 listFailingBase = true;
759 continue;
760 }
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000761
762 SkDebugf("Unrecognized argument <%s>\n", argv[i]);
763 usage(argv[0]);
epoger@google.com70044cc2012-07-12 18:37:55 +0000764 return kGenericError;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000765 }
epoger@google.coma5f406e2012-05-01 13:26:16 +0000766
epoger@google.coma5f406e2012-05-01 13:26:16 +0000767 if (numUnflaggedArguments == 2) {
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000768 outputDir = comparisonDir;
epoger@google.coma5f406e2012-05-01 13:26:16 +0000769 } else if (numUnflaggedArguments != 3) {
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000770 usage(argv[0]);
epoger@google.com70044cc2012-07-12 18:37:55 +0000771 return kGenericError;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000772 }
773
bsalomon@google.com1a315fe2011-09-23 14:56:37 +0000774 if (!baseDir.endsWith(PATH_DIV_STR)) {
775 baseDir.append(PATH_DIV_STR);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000776 }
epoger@google.com71329d82012-08-16 13:42:13 +0000777 if (printDirNames) {
keyar@chromium.orga6318192012-07-09 21:01:50 +0000778 printf("baseDir is [%s]\n", baseDir.c_str());
779 }
epoger@google.coma5f406e2012-05-01 13:26:16 +0000780
bsalomon@google.com1a315fe2011-09-23 14:56:37 +0000781 if (!comparisonDir.endsWith(PATH_DIV_STR)) {
782 comparisonDir.append(PATH_DIV_STR);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000783 }
epoger@google.com71329d82012-08-16 13:42:13 +0000784 if (printDirNames) {
keyar@chromium.orga6318192012-07-09 21:01:50 +0000785 printf("comparisonDir is [%s]\n", comparisonDir.c_str());
786 }
epoger@google.coma5f406e2012-05-01 13:26:16 +0000787
bsalomon@google.com1a315fe2011-09-23 14:56:37 +0000788 if (!outputDir.endsWith(PATH_DIV_STR)) {
789 outputDir.append(PATH_DIV_STR);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000790 }
epoger@google.coma5f406e2012-05-01 13:26:16 +0000791 if (generateDiffs) {
epoger@google.com71329d82012-08-16 13:42:13 +0000792 if (printDirNames) {
keyar@chromium.orga6318192012-07-09 21:01:50 +0000793 printf("writing diffs to outputDir is [%s]\n", outputDir.c_str());
794 }
epoger@google.coma5f406e2012-05-01 13:26:16 +0000795 } else {
epoger@google.com71329d82012-08-16 13:42:13 +0000796 if (printDirNames) {
keyar@chromium.orga6318192012-07-09 21:01:50 +0000797 printf("not writing any diffs to outputDir [%s]\n", outputDir.c_str());
798 }
epoger@google.coma5f406e2012-05-01 13:26:16 +0000799 outputDir.set("");
800 }
801
epoger@google.comda4af242012-06-25 18:45:50 +0000802 // If no matchSubstrings were specified, match ALL strings
803 // (except for whatever nomatchSubstrings were specified, if any).
epoger@google.coma5f406e2012-05-01 13:26:16 +0000804 if (matchSubstrings.isEmpty()) {
805 matchSubstrings.push(new SkString(""));
806 }
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000807
epoger@google.coma611c3e2012-05-18 20:10:06 +0000808 create_diff_images(diffProc, colorThreshold, &differences,
809 baseDir, comparisonDir, outputDir,
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000810 matchSubstrings, nomatchSubstrings, recurseIntoSubdirs, generateDiffs,
commit-bot@chromium.org93d7bb62014-05-28 18:26:00 +0000811 verbose, &summary);
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000812 summary.print(listFilenames, failOnResultType, failOnStatusType);
tomhudson@google.com7d042802011-07-14 13:15:55 +0000813
reed3a3baf62015-01-06 07:39:55 -0800814 if (listFailingBase) {
815 summary.printfFailingBaseNames("\n");
816 }
817
tomhudson@google.com7d042802011-07-14 13:15:55 +0000818 if (differences.count()) {
reed@google.comc7a67cb2012-05-07 14:52:12 +0000819 qsort(differences.begin(), differences.count(),
820 sizeof(DiffRecord*), sortProc);
tomhudson@google.com7d042802011-07-14 13:15:55 +0000821 }
epoger@google.com66008522012-05-16 17:40:57 +0000822
epoger@google.coma5f406e2012-05-01 13:26:16 +0000823 if (generateDiffs) {
824 print_diff_page(summary.fNumMatches, colorThreshold, differences,
825 baseDir, comparisonDir, outputDir);
826 }
epoger@google.com76222c02012-05-31 15:12:09 +0000827
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000828 for (i = 0; i < differences.count(); i++) {
829 delete differences[i];
830 }
epoger@google.coma5f406e2012-05-01 13:26:16 +0000831 matchSubstrings.deleteAll();
832 nomatchSubstrings.deleteAll();
epoger@google.combe6188d2012-05-31 15:13:45 +0000833
epoger@google.comdfbf24e2012-07-13 21:22:02 +0000834 int num_failing_results = 0;
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000835 for (int i = 0; i < DiffRecord::kResultCount; i++) {
epoger@google.com3af4ff42012-07-19 17:35:04 +0000836 if (failOnResultType[i]) {
837 num_failing_results += summary.fResultsOfType[i].count();
838 }
epoger@google.com46a45962012-07-12 18:16:02 +0000839 }
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000840 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.com28659882012-07-16 18:01:06 +0000849
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.com4b33d282011-04-27 15:39:30 +0000854}
caryclark@google.com5987f582012-10-02 18:33:14 +0000855
856#if !defined SK_BUILD_FOR_IOS
857int main(int argc, char * const argv[]) {
858 return tool_main(argc, (char**) argv);
859}
860#endif