blob: c0541d980c04da8512baa38480bf004a4caa89a6 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
tomhudson@google.com4b33d282011-04-27 15:39:30 +00008#include "SkColorPriv.h"
epoger@google.com46256ea2012-05-22 13:45:35 +00009#include "SkData.h"
tomhudson@google.com4b33d282011-04-27 15:39:30 +000010#include "SkImageDecoder.h"
11#include "SkImageEncoder.h"
12#include "SkOSFile.h"
13#include "SkStream.h"
14#include "SkTDArray.h"
15#include "SkTemplates.h"
16#include "SkTime.h"
17#include "SkTSearch.h"
18#include "SkTypes.h"
19
20/**
21 * skdiff
22 *
23 * Given three directory names, expects to find identically-named files in
24 * each of the first two; the first are treated as a set of baseline,
25 * the second a set of variant images, and a diff image is written into the
26 * third directory for each pair.
tomhudson@google.com7d042802011-07-14 13:15:55 +000027 * Creates an index.html in the current third directory to compare each
tomhudson@google.com4b33d282011-04-27 15:39:30 +000028 * pair that does not match exactly.
29 * Does *not* recursively descend directories.
epoger@google.combe6188d2012-05-31 15:13:45 +000030 *
31 * Returns zero exit code if all images match across baseDir and comparisonDir.
tomhudson@google.com4b33d282011-04-27 15:39:30 +000032 */
33
bsalomon@google.com1a315fe2011-09-23 14:56:37 +000034#if SK_BUILD_FOR_WIN32
35 #define PATH_DIV_STR "\\"
36 #define PATH_DIV_CHAR '\\'
37#else
38 #define PATH_DIV_STR "/"
39 #define PATH_DIV_CHAR '/'
40#endif
41
epoger@google.com292aff62012-05-16 14:57:28 +000042// Result of comparison for each pair of files.
epoger@google.com292aff62012-05-16 14:57:28 +000043enum Result {
44 kEqualBits, // both files in the pair contain exactly the same bits
45 kEqualPixels, // not bitwise equal, but their pixels are exactly the same
46 kDifferentSizes, // both are images we can parse, but of different sizes
47 kDifferentPixels,// both are images we can parse, but with different pixels
48 kDifferentOther, // files have different bits but are not parsable images
49 kBaseMissing, // missing from baseDir
50 kComparisonMissing,// missing from comparisonDir
epoger@google.com76222c02012-05-31 15:12:09 +000051 kUnknown, // not compared yet
52 //
53 kNumResultTypes // NOT A VALID VALUE--used to set up arrays. Must be last.
epoger@google.com292aff62012-05-16 14:57:28 +000054};
55
tomhudson@google.com4b33d282011-04-27 15:39:30 +000056struct DiffRecord {
tomhudson@google.com4e305982011-07-13 17:42:46 +000057 DiffRecord (const SkString filename,
58 const SkString basePath,
epoger@google.com5fd53852012-03-22 18:20:06 +000059 const SkString comparisonPath,
epoger@google.com292aff62012-05-16 14:57:28 +000060 const Result result = kUnknown)
tomhudson@google.com4b33d282011-04-27 15:39:30 +000061 : fFilename (filename)
tomhudson@google.com4e305982011-07-13 17:42:46 +000062 , fBasePath (basePath)
63 , fComparisonPath (comparisonPath)
tomhudson@google.com9b540ce2011-08-02 14:10:04 +000064 , fBaseBitmap (new SkBitmap ())
65 , fComparisonBitmap (new SkBitmap ())
66 , fDifferenceBitmap (new SkBitmap ())
epoger@google.com25d961c2012-02-02 20:50:36 +000067 , fWhiteBitmap (new SkBitmap ())
tomhudson@google.com9b540ce2011-08-02 14:10:04 +000068 , fBaseHeight (0)
69 , fBaseWidth (0)
tomhudson@google.com9dc527b2011-06-09 15:47:10 +000070 , fFractionDifference (0)
71 , fWeightedFraction (0)
tomhudson@google.com4b33d282011-04-27 15:39:30 +000072 , fAverageMismatchR (0)
73 , fAverageMismatchG (0)
74 , fAverageMismatchB (0)
75 , fMaxMismatchR (0)
76 , fMaxMismatchG (0)
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +000077 , fMaxMismatchB (0)
epoger@google.com292aff62012-05-16 14:57:28 +000078 , fResult(result) {
tomhudson@google.com4e305982011-07-13 17:42:46 +000079 };
tomhudson@google.com4b33d282011-04-27 15:39:30 +000080
81 SkString fFilename;
tomhudson@google.com4e305982011-07-13 17:42:46 +000082 SkString fBasePath;
83 SkString fComparisonPath;
tomhudson@google.com4b33d282011-04-27 15:39:30 +000084
tomhudson@google.com9b540ce2011-08-02 14:10:04 +000085 SkBitmap* fBaseBitmap;
86 SkBitmap* fComparisonBitmap;
87 SkBitmap* fDifferenceBitmap;
epoger@google.com25d961c2012-02-02 20:50:36 +000088 SkBitmap* fWhiteBitmap;
tomhudson@google.com9b540ce2011-08-02 14:10:04 +000089
90 int fBaseHeight;
91 int fBaseWidth;
tomhudson@google.com4b33d282011-04-27 15:39:30 +000092
93 /// Arbitrary floating-point metric to be used to sort images from most
94 /// to least different from baseline; values of 0 will be omitted from the
95 /// summary webpage.
tomhudson@google.com9dc527b2011-06-09 15:47:10 +000096 float fFractionDifference;
97 float fWeightedFraction;
tomhudson@google.com4b33d282011-04-27 15:39:30 +000098
99 float fAverageMismatchR;
100 float fAverageMismatchG;
101 float fAverageMismatchB;
102
103 uint32_t fMaxMismatchR;
104 uint32_t fMaxMismatchG;
105 uint32_t fMaxMismatchB;
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +0000106
epoger@google.com292aff62012-05-16 14:57:28 +0000107 /// Which category of diff result.
108 Result fResult;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000109};
110
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000111#define MAX2(a,b) (((b) < (a)) ? (a) : (b))
112#define MAX3(a,b,c) (((b) < (a)) ? MAX2((a), (c)) : MAX2((b), (c)))
113
epoger@google.com25d961c2012-02-02 20:50:36 +0000114const SkPMColor PMCOLOR_WHITE = SkPreMultiplyColor(SK_ColorWHITE);
115const SkPMColor PMCOLOR_BLACK = SkPreMultiplyColor(SK_ColorBLACK);
116
epoger@google.coma5f406e2012-05-01 13:26:16 +0000117typedef SkTDArray<SkString*> StringArray;
118typedef StringArray FileArray;
epoger@google.com5fd53852012-03-22 18:20:06 +0000119
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000120struct DiffSummary {
121 DiffSummary ()
122 : fNumMatches (0)
123 , fNumMismatches (0)
124 , fMaxMismatchV (0)
125 , fMaxMismatchPercent (0) { };
126
epoger@google.com5fd53852012-03-22 18:20:06 +0000127 ~DiffSummary() {
epoger@google.com76222c02012-05-31 15:12:09 +0000128 for (int i = 0; i < kNumResultTypes; i++) {
129 fResultsOfType[i].deleteAll();
130 }
epoger@google.com5fd53852012-03-22 18:20:06 +0000131 }
132
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000133 uint32_t fNumMatches;
134 uint32_t fNumMismatches;
135 uint32_t fMaxMismatchV;
136 float fMaxMismatchPercent;
137
epoger@google.com76222c02012-05-31 15:12:09 +0000138 FileArray fResultsOfType[kNumResultTypes];
139
140 // Print the contents of this FileArray, if any, to stdout.
141 // (If the FileArray is empty, print nothing.)
142 void printContents(const FileArray& fileArray, const char* headerText) {
143 int n = fileArray.count();
144 if (n > 0) {
145 printf("%s:\n", headerText);
146 for (int i = 0; i < n; ++i) {
147 printf("\t%s\n", fileArray[i]->c_str());
148 }
149 }
150 }
epoger@google.com5fd53852012-03-22 18:20:06 +0000151
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000152 void print () {
epoger@google.com76222c02012-05-31 15:12:09 +0000153 printContents(fResultsOfType[kBaseMissing],
154 "Missing in baseDir");
155 printContents(fResultsOfType[kComparisonMissing],
156 "Missing in comparisonDir");
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000157 printf("%d of %d images matched.\n", fNumMatches,
158 fNumMatches + fNumMismatches);
159 if (fNumMismatches > 0) {
160 printf("Maximum pixel intensity mismatch %d\n", fMaxMismatchV);
161 printf("Largest area mismatch was %.2f%% of pixels\n",
162 fMaxMismatchPercent);
163 }
164
165 }
166
167 void add (DiffRecord* drp) {
epoger@google.com46256ea2012-05-22 13:45:35 +0000168 uint32_t mismatchValue;
epoger@google.com292aff62012-05-16 14:57:28 +0000169
epoger@google.com76222c02012-05-31 15:12:09 +0000170 fResultsOfType[drp->fResult].push(new SkString(drp->fFilename));
epoger@google.com292aff62012-05-16 14:57:28 +0000171 switch (drp->fResult) {
epoger@google.com46256ea2012-05-22 13:45:35 +0000172 case kEqualBits:
173 fNumMatches++;
174 break;
epoger@google.com292aff62012-05-16 14:57:28 +0000175 case kEqualPixels:
176 fNumMatches++;
177 break;
epoger@google.com46256ea2012-05-22 13:45:35 +0000178 case kDifferentSizes:
epoger@google.com5fd53852012-03-22 18:20:06 +0000179 fNumMismatches++;
caryclark@google.com3dd45912012-06-06 12:11:10 +0000180 drp->fFractionDifference = 2;// sort as if 200% of pixels differed
epoger@google.com292aff62012-05-16 14:57:28 +0000181 break;
epoger@google.com46256ea2012-05-22 13:45:35 +0000182 case kDifferentPixels:
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000183 fNumMismatches++;
184 if (drp->fFractionDifference * 100 > fMaxMismatchPercent) {
185 fMaxMismatchPercent = drp->fFractionDifference * 100;
186 }
epoger@google.com46256ea2012-05-22 13:45:35 +0000187 mismatchValue = MAX3(drp->fMaxMismatchR, drp->fMaxMismatchG,
188 drp->fMaxMismatchB);
189 if (mismatchValue > fMaxMismatchV) {
190 fMaxMismatchV = mismatchValue;
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000191 }
epoger@google.com292aff62012-05-16 14:57:28 +0000192 break;
epoger@google.com46256ea2012-05-22 13:45:35 +0000193 case kDifferentOther:
194 fNumMismatches++;
caryclark@google.com3dd45912012-06-06 12:11:10 +0000195 drp->fFractionDifference = 3;// sort as if 300% of pixels differed
epoger@google.com46256ea2012-05-22 13:45:35 +0000196 break;
197 case kBaseMissing:
198 fNumMismatches++;
epoger@google.com46256ea2012-05-22 13:45:35 +0000199 break;
200 case kComparisonMissing:
201 fNumMismatches++;
epoger@google.com46256ea2012-05-22 13:45:35 +0000202 break;
203 case kUnknown:
204 SkDEBUGFAIL("adding uncategorized DiffRecord");
205 break;
206 default:
207 SkDEBUGFAIL("adding DiffRecord with unhandled fResult value");
208 break;
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000209 }
210 }
211};
212
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000213typedef SkTDArray<DiffRecord*> RecordArray;
214
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000215/// Comparison routine for qsort; sorts by fFractionDifference
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000216/// from largest to smallest.
217static int compare_diff_metrics (DiffRecord** lhs, DiffRecord** rhs) {
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000218 if ((*lhs)->fFractionDifference < (*rhs)->fFractionDifference) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000219 return 1;
220 }
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000221 if ((*rhs)->fFractionDifference < (*lhs)->fFractionDifference) {
tomhudson@google.com5b325292011-05-24 19:41:13 +0000222 return -1;
223 }
224 return 0;
225}
226
227static int compare_diff_weighted (DiffRecord** lhs, DiffRecord** rhs) {
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000228 if ((*lhs)->fWeightedFraction < (*rhs)->fWeightedFraction) {
tomhudson@google.com5b325292011-05-24 19:41:13 +0000229 return 1;
230 }
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000231 if ((*lhs)->fWeightedFraction > (*rhs)->fWeightedFraction) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000232 return -1;
233 }
234 return 0;
235}
236
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000237/// Comparison routine for qsort; sorts by max(fAverageMismatch{RGB})
238/// from largest to smallest.
239static int compare_diff_mean_mismatches (DiffRecord** lhs, DiffRecord** rhs) {
240 float leftValue = MAX3((*lhs)->fAverageMismatchR,
241 (*lhs)->fAverageMismatchG,
242 (*lhs)->fAverageMismatchB);
243 float rightValue = MAX3((*rhs)->fAverageMismatchR,
244 (*rhs)->fAverageMismatchG,
245 (*rhs)->fAverageMismatchB);
246 if (leftValue < rightValue) {
247 return 1;
248 }
249 if (rightValue < leftValue) {
250 return -1;
251 }
252 return 0;
253}
254
255/// Comparison routine for qsort; sorts by max(fMaxMismatch{RGB})
256/// from largest to smallest.
257static int compare_diff_max_mismatches (DiffRecord** lhs, DiffRecord** rhs) {
bsalomon@google.com8e06dab2011-10-07 20:03:39 +0000258 uint32_t leftValue = MAX3((*lhs)->fMaxMismatchR,
259 (*lhs)->fMaxMismatchG,
260 (*lhs)->fMaxMismatchB);
261 uint32_t rightValue = MAX3((*rhs)->fMaxMismatchR,
262 (*rhs)->fMaxMismatchG,
263 (*rhs)->fMaxMismatchB);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000264 if (leftValue < rightValue) {
265 return 1;
266 }
267 if (rightValue < leftValue) {
268 return -1;
269 }
270 return compare_diff_mean_mismatches(lhs, rhs);
271}
272
273
274
275/// Parameterized routine to compute the color of a pixel in a difference image.
276typedef SkPMColor (*DiffMetricProc)(SkPMColor, SkPMColor);
277
caryclark@google.com3dd45912012-06-06 12:11:10 +0000278#if 0 // UNUSED
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +0000279static void expand_and_copy (int width, int height, SkBitmap** dest) {
280 SkBitmap* temp = new SkBitmap ();
281 temp->reset();
282 temp->setConfig((*dest)->config(), width, height);
283 temp->allocPixels();
284 (*dest)->copyPixelsTo(temp->getPixels(), temp->getSize(),
285 temp->rowBytes());
286 *dest = temp;
287}
caryclark@google.com3dd45912012-06-06 12:11:10 +0000288#endif
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +0000289
epoger@google.com46256ea2012-05-22 13:45:35 +0000290/// Returns true if the two buffers passed in are both non-NULL, and include
291/// exactly the same byte values (and identical lengths).
292static bool are_buffers_equal(SkData* skdata1, SkData* skdata2) {
293 if ((NULL == skdata1) || (NULL == skdata2)) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000294 return false;
295 }
epoger@google.com46256ea2012-05-22 13:45:35 +0000296 if (skdata1->size() != skdata2->size()) {
297 return false;
298 }
299 return (0 == memcmp(skdata1->data(), skdata2->data(), skdata1->size()));
300}
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000301
epoger@google.com46256ea2012-05-22 13:45:35 +0000302/// Reads the file at the given path and returns its complete contents as an
303/// SkData object (or returns NULL on error).
304static SkData* read_file(const char* file_path) {
305 SkFILEStream fileStream(file_path);
306 if (!fileStream.isValid()) {
307 SkDebugf("WARNING: could not open file <%s> for reading\n", file_path);
308 return NULL;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000309 }
epoger@google.com46256ea2012-05-22 13:45:35 +0000310 size_t bytesInFile = fileStream.getLength();
311 size_t bytesLeftToRead = bytesInFile;
312
313 void* bufferStart = sk_malloc_throw(bytesInFile);
314 char* bufferPointer = (char*)bufferStart;
315 while (bytesLeftToRead > 0) {
316 size_t bytesReadThisTime = fileStream.read(
317 bufferPointer, bytesLeftToRead);
318 if (0 == bytesReadThisTime) {
319 SkDebugf("WARNING: error reading from <%s>\n", file_path);
320 sk_free(bufferStart);
321 return NULL;
322 }
323 bytesLeftToRead -= bytesReadThisTime;
324 bufferPointer += bytesReadThisTime;
325 }
326 return SkData::NewFromMalloc(bufferStart, bytesInFile);
327}
328
329/// Decodes binary contents of baseFile and comparisonFile into
330/// diffRecord->fBaseBitmap and diffRecord->fComparisonBitmap.
331/// Returns true if that succeeds.
332static bool get_bitmaps (SkData* baseFileContents,
333 SkData* comparisonFileContents,
334 DiffRecord* diffRecord) {
335 SkMemoryStream compareStream(comparisonFileContents->data(),
336 comparisonFileContents->size());
337 SkMemoryStream baseStream(baseFileContents->data(),
338 baseFileContents->size());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000339
340 SkImageDecoder* codec = SkImageDecoder::Factory(&baseStream);
341 if (NULL == codec) {
epoger@google.com46256ea2012-05-22 13:45:35 +0000342 SkDebugf("ERROR: no codec found for basePath <%s>\n",
tomhudson@google.com4e305982011-07-13 17:42:46 +0000343 diffRecord->fBasePath.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000344 return false;
345 }
346
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +0000347 // In debug, the DLL will automatically be unloaded when this is deleted,
348 // but that shouldn't be a problem in release mode.
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000349 SkAutoTDelete<SkImageDecoder> ad(codec);
350
351 baseStream.rewind();
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000352 if (!codec->decode(&baseStream, diffRecord->fBaseBitmap,
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000353 SkBitmap::kARGB_8888_Config,
354 SkImageDecoder::kDecodePixels_Mode)) {
epoger@google.com46256ea2012-05-22 13:45:35 +0000355 SkDebugf("ERROR: codec failed for basePath <%s>\n",
tomhudson@google.com4e305982011-07-13 17:42:46 +0000356 diffRecord->fBasePath.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000357 return false;
358 }
359
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000360 diffRecord->fBaseWidth = diffRecord->fBaseBitmap->width();
361 diffRecord->fBaseHeight = diffRecord->fBaseBitmap->height();
362
363 if (!codec->decode(&compareStream, diffRecord->fComparisonBitmap,
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000364 SkBitmap::kARGB_8888_Config,
365 SkImageDecoder::kDecodePixels_Mode)) {
epoger@google.com46256ea2012-05-22 13:45:35 +0000366 SkDebugf("ERROR: codec failed for comparisonPath <%s>\n",
tomhudson@google.com4e305982011-07-13 17:42:46 +0000367 diffRecord->fComparisonPath.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000368 return false;
369 }
370
371 return true;
372}
373
epoger@google.com5fd53852012-03-22 18:20:06 +0000374static bool get_bitmap_height_width(const SkString& path,
375 int *height, int *width) {
376 SkFILEStream stream(path.c_str());
377 if (!stream.isValid()) {
378 SkDebugf("ERROR: couldn't open file <%s>\n",
379 path.c_str());
380 return false;
381 }
382
383 SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
384 if (NULL == codec) {
385 SkDebugf("ERROR: no codec found for <%s>\n",
386 path.c_str());
387 return false;
388 }
389
390 SkAutoTDelete<SkImageDecoder> ad(codec);
391 SkBitmap bm;
392
393 stream.rewind();
394 if (!codec->decode(&stream, &bm,
395 SkBitmap::kARGB_8888_Config,
396 SkImageDecoder::kDecodePixels_Mode)) {
397 SkDebugf("ERROR: codec failed for <%s>\n",
398 path.c_str());
399 return false;
400 }
401
402 *height = bm.height();
403 *width = bm.width();
404
405 return true;
406}
407
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000408// from gm - thanks to PNG, we need to force all pixels 100% opaque
409static void force_all_opaque(const SkBitmap& bitmap) {
410 SkAutoLockPixels lock(bitmap);
411 for (int y = 0; y < bitmap.height(); y++) {
412 for (int x = 0; x < bitmap.width(); x++) {
413 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
414 }
415 }
416}
417
418// from gm
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000419static bool write_bitmap(const SkString& path, const SkBitmap* bitmap) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000420 SkBitmap copy;
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000421 bitmap->copyTo(&copy, SkBitmap::kARGB_8888_Config);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000422 force_all_opaque(copy);
423 return SkImageEncoder::EncodeFile(path.c_str(), copy,
424 SkImageEncoder::kPNG_Type, 100);
425}
426
427// from gm
428static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
429 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
430 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
431 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
432
433 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
434}
435
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000436static inline bool colors_match_thresholded(SkPMColor c0, SkPMColor c1,
437 const int threshold) {
438 int da = SkGetPackedA32(c0) - SkGetPackedA32(c1);
439 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
440 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
441 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
442
443 return ((SkAbs32(da) <= threshold) &&
444 (SkAbs32(dr) <= threshold) &&
445 (SkAbs32(dg) <= threshold) &&
446 (SkAbs32(db) <= threshold));
447}
448
449// based on gm
epoger@google.com66008522012-05-16 17:40:57 +0000450// Postcondition: when we exit this method, dr->fResult should have some value
451// other than kUnknown.
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000452static void compute_diff(DiffRecord* dr,
453 DiffMetricProc diffFunction,
454 const int colorThreshold) {
epoger@google.com25d961c2012-02-02 20:50:36 +0000455 SkAutoLockPixels alpDiff(*dr->fDifferenceBitmap);
456 SkAutoLockPixels alpWhite(*dr->fWhiteBitmap);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000457
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000458 const int w = dr->fComparisonBitmap->width();
459 const int h = dr->fComparisonBitmap->height();
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000460 int mismatchedPixels = 0;
461 int totalMismatchR = 0;
462 int totalMismatchG = 0;
463 int totalMismatchB = 0;
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +0000464
465 if (w != dr->fBaseWidth || h != dr->fBaseHeight) {
epoger@google.com292aff62012-05-16 14:57:28 +0000466 dr->fResult = kDifferentSizes;
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +0000467 return;
468 }
tomhudson@google.com5b325292011-05-24 19:41:13 +0000469 // Accumulate fractionally different pixels, then divide out
470 // # of pixels at the end.
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000471 dr->fWeightedFraction = 0;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000472 for (int y = 0; y < h; y++) {
473 for (int x = 0; x < w; x++) {
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000474 SkPMColor c0 = *dr->fBaseBitmap->getAddr32(x, y);
475 SkPMColor c1 = *dr->fComparisonBitmap->getAddr32(x, y);
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000476 SkPMColor trueDifference = compute_diff_pmcolor(c0, c1);
477 SkPMColor outputDifference = diffFunction(c0, c1);
478 uint32_t thisR = SkGetPackedR32(trueDifference);
479 uint32_t thisG = SkGetPackedG32(trueDifference);
480 uint32_t thisB = SkGetPackedB32(trueDifference);
tomhudson@google.com5b325292011-05-24 19:41:13 +0000481 totalMismatchR += thisR;
482 totalMismatchG += thisG;
483 totalMismatchB += thisB;
484 // In HSV, value is defined as max RGB component.
485 int value = MAX3(thisR, thisG, thisB);
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000486 dr->fWeightedFraction += ((float) value) / 255;
tomhudson@google.com5b325292011-05-24 19:41:13 +0000487 if (thisR > dr->fMaxMismatchR) {
488 dr->fMaxMismatchR = thisR;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000489 }
tomhudson@google.com5b325292011-05-24 19:41:13 +0000490 if (thisG > dr->fMaxMismatchG) {
491 dr->fMaxMismatchG = thisG;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000492 }
tomhudson@google.com5b325292011-05-24 19:41:13 +0000493 if (thisB > dr->fMaxMismatchB) {
494 dr->fMaxMismatchB = thisB;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000495 }
496 if (!colors_match_thresholded(c0, c1, colorThreshold)) {
497 mismatchedPixels++;
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000498 *dr->fDifferenceBitmap->getAddr32(x, y) = outputDifference;
epoger@google.com25d961c2012-02-02 20:50:36 +0000499 *dr->fWhiteBitmap->getAddr32(x, y) = PMCOLOR_WHITE;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000500 } else {
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000501 *dr->fDifferenceBitmap->getAddr32(x, y) = 0;
epoger@google.com25d961c2012-02-02 20:50:36 +0000502 *dr->fWhiteBitmap->getAddr32(x, y) = PMCOLOR_BLACK;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000503 }
504 }
505 }
epoger@google.com292aff62012-05-16 14:57:28 +0000506 if (0 == mismatchedPixels) {
507 dr->fResult = kEqualPixels;
508 return;
509 }
510 dr->fResult = kDifferentPixels;
tomhudson@google.com5b325292011-05-24 19:41:13 +0000511 int pixelCount = w * h;
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000512 dr->fFractionDifference = ((float) mismatchedPixels) / pixelCount;
513 dr->fWeightedFraction /= pixelCount;
tomhudson@google.com5b325292011-05-24 19:41:13 +0000514 dr->fAverageMismatchR = ((float) totalMismatchR) / pixelCount;
515 dr->fAverageMismatchG = ((float) totalMismatchG) / pixelCount;
516 dr->fAverageMismatchB = ((float) totalMismatchB) / pixelCount;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000517}
518
epoger@google.com25d961c2012-02-02 20:50:36 +0000519static SkString filename_to_derived_filename (const SkString& filename,
520 const char *suffix) {
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000521 SkString diffName (filename);
522 const char* cstring = diffName.c_str();
523 int dotOffset = strrchr(cstring, '.') - cstring;
524 diffName.remove(dotOffset, diffName.size() - dotOffset);
epoger@google.com25d961c2012-02-02 20:50:36 +0000525 diffName.append(suffix);
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000526 return diffName;
527}
528
epoger@google.com25d961c2012-02-02 20:50:36 +0000529/// Given a image filename, returns the name of the file containing the
530/// associated difference image.
531static SkString filename_to_diff_filename (const SkString& filename) {
532 return filename_to_derived_filename(filename, "-diff.png");
533}
534
535/// Given a image filename, returns the name of the file containing the
536/// "white" difference image.
537static SkString filename_to_white_filename (const SkString& filename) {
538 return filename_to_derived_filename(filename, "-white.png");
539}
540
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000541static void release_bitmaps(DiffRecord* drp) {
542 delete drp->fBaseBitmap;
543 drp->fBaseBitmap = NULL;
544 delete drp->fComparisonBitmap;
545 drp->fComparisonBitmap = NULL;
546 delete drp->fDifferenceBitmap;
547 drp->fDifferenceBitmap = NULL;
epoger@google.com25d961c2012-02-02 20:50:36 +0000548 delete drp->fWhiteBitmap;
549 drp->fWhiteBitmap = NULL;
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000550}
551
tomhudson@google.com7d042802011-07-14 13:15:55 +0000552
epoger@google.coma5f406e2012-05-01 13:26:16 +0000553/// If outputDir.isEmpty(), don't write out diff files.
tomhudson@google.com7d042802011-07-14 13:15:55 +0000554static void create_and_write_diff_image(DiffRecord* drp,
555 DiffMetricProc dmp,
556 const int colorThreshold,
557 const SkString& outputDir,
558 const SkString& filename) {
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000559 const int w = drp->fBaseWidth;
560 const int h = drp->fBaseHeight;
561 drp->fDifferenceBitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h);
562 drp->fDifferenceBitmap->allocPixels();
epoger@google.com25d961c2012-02-02 20:50:36 +0000563 drp->fWhiteBitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h);
564 drp->fWhiteBitmap->allocPixels();
tomhudson@google.com7d042802011-07-14 13:15:55 +0000565
epoger@google.com66008522012-05-16 17:40:57 +0000566 SkASSERT(kUnknown == drp->fResult);
567 compute_diff(drp, dmp, colorThreshold);
568 SkASSERT(kUnknown != drp->fResult);
569
570 if ((kDifferentPixels == drp->fResult) && !outputDir.isEmpty()) {
epoger@google.coma5f406e2012-05-01 13:26:16 +0000571 SkString differencePath (outputDir);
572 differencePath.append(filename_to_diff_filename(filename));
573 write_bitmap(differencePath, drp->fDifferenceBitmap);
574 SkString whitePath (outputDir);
575 whitePath.append(filename_to_white_filename(filename));
576 write_bitmap(whitePath, drp->fWhiteBitmap);
577 }
epoger@google.com66008522012-05-16 17:40:57 +0000578
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000579 release_bitmaps(drp);
tomhudson@google.com4e305982011-07-13 17:42:46 +0000580}
581
epoger@google.coma5f406e2012-05-01 13:26:16 +0000582/// Returns true if string contains any of these substrings.
583static bool string_contains_any_of(const SkString& string,
584 const StringArray& substrings) {
585 for (int i = 0; i < substrings.count(); i++) {
586 if (string.contains(substrings[i]->c_str())) {
587 return true;
588 }
589 }
590 return false;
591}
592
593/// Iterate over dir and get all files that:
594/// - match any of the substrings in matchSubstrings, but...
595/// - DO NOT match any of the substrings in nomatchSubstrings
596/// Returns the list of files in *files.
597static void get_file_list(const SkString& dir,
598 const StringArray& matchSubstrings,
599 const StringArray& nomatchSubstrings,
600 FileArray *files) {
epoger@google.com5fd53852012-03-22 18:20:06 +0000601 SkOSFile::Iter it(dir.c_str());
602 SkString filename;
603 while (it.next(&filename)) {
epoger@google.coma5f406e2012-05-01 13:26:16 +0000604 if (string_contains_any_of(filename, matchSubstrings) &&
605 !string_contains_any_of(filename, nomatchSubstrings)) {
606 files->push(new SkString(filename));
epoger@google.com5fd53852012-03-22 18:20:06 +0000607 }
epoger@google.com5fd53852012-03-22 18:20:06 +0000608 }
609}
610
611static void release_file_list(FileArray *files) {
612 files->deleteAll();
613}
614
615/// Comparison routines for qsort, sort by file names.
616static int compare_file_name_metrics(SkString **lhs, SkString **rhs) {
617 return strcmp((*lhs)->c_str(), (*rhs)->c_str());
618}
619
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000620/// Creates difference images, returns the number that have a 0 metric.
epoger@google.coma5f406e2012-05-01 13:26:16 +0000621/// If outputDir.isEmpty(), don't write out diff files.
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000622static void create_diff_images (DiffMetricProc dmp,
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000623 const int colorThreshold,
624 RecordArray* differences,
625 const SkString& baseDir,
626 const SkString& comparisonDir,
627 const SkString& outputDir,
epoger@google.coma5f406e2012-05-01 13:26:16 +0000628 const StringArray& matchSubstrings,
629 const StringArray& nomatchSubstrings,
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000630 DiffSummary* summary) {
epoger@google.coma5f406e2012-05-01 13:26:16 +0000631 SkASSERT(!baseDir.isEmpty());
632 SkASSERT(!comparisonDir.isEmpty());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000633
epoger@google.com5fd53852012-03-22 18:20:06 +0000634 FileArray baseFiles;
635 FileArray comparisonFiles;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000636
epoger@google.coma5f406e2012-05-01 13:26:16 +0000637 get_file_list(baseDir, matchSubstrings, nomatchSubstrings, &baseFiles);
638 get_file_list(comparisonDir, matchSubstrings, nomatchSubstrings,
639 &comparisonFiles);
epoger@google.com5fd53852012-03-22 18:20:06 +0000640
epoger@google.coma5f406e2012-05-01 13:26:16 +0000641 if (!baseFiles.isEmpty()) {
reed@google.comc7a67cb2012-05-07 14:52:12 +0000642 qsort(baseFiles.begin(), baseFiles.count(), sizeof(SkString*),
643 SkCastForQSort(compare_file_name_metrics));
epoger@google.coma5f406e2012-05-01 13:26:16 +0000644 }
645 if (!comparisonFiles.isEmpty()) {
reed@google.comc7a67cb2012-05-07 14:52:12 +0000646 qsort(comparisonFiles.begin(), comparisonFiles.count(),
647 sizeof(SkString*), SkCastForQSort(compare_file_name_metrics));
epoger@google.coma5f406e2012-05-01 13:26:16 +0000648 }
epoger@google.com66008522012-05-16 17:40:57 +0000649
epoger@google.com5fd53852012-03-22 18:20:06 +0000650 int i = 0;
651 int j = 0;
652
653 while (i < baseFiles.count() &&
654 j < comparisonFiles.count()) {
655
tomhudson@google.com4e305982011-07-13 17:42:46 +0000656 SkString basePath (baseDir);
epoger@google.com5fd53852012-03-22 18:20:06 +0000657 basePath.append(*baseFiles[i]);
tomhudson@google.com7d042802011-07-14 13:15:55 +0000658 SkString comparisonPath (comparisonDir);
epoger@google.com5fd53852012-03-22 18:20:06 +0000659 comparisonPath.append(*comparisonFiles[j]);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000660
epoger@google.com5fd53852012-03-22 18:20:06 +0000661 DiffRecord *drp = NULL;
662 int v = strcmp(baseFiles[i]->c_str(),
663 comparisonFiles[j]->c_str());
664
665 if (v < 0) {
666 // in baseDir, but not in comparisonDir
epoger@google.com292aff62012-05-16 14:57:28 +0000667 drp = new DiffRecord(*baseFiles[i], basePath, comparisonPath,
668 kComparisonMissing);
epoger@google.com5fd53852012-03-22 18:20:06 +0000669 ++i;
670 } else if (v > 0) {
671 // in comparisonDir, but not in baseDir
epoger@google.com292aff62012-05-16 14:57:28 +0000672 drp = new DiffRecord(*comparisonFiles[j], basePath, comparisonPath,
673 kBaseMissing);
epoger@google.com5fd53852012-03-22 18:20:06 +0000674 ++j;
675 } else {
epoger@google.com46256ea2012-05-22 13:45:35 +0000676 // Found the same filename in both baseDir and comparisonDir.
epoger@google.com5fd53852012-03-22 18:20:06 +0000677 drp = new DiffRecord(*baseFiles[i], basePath, comparisonPath);
epoger@google.com46256ea2012-05-22 13:45:35 +0000678 SkASSERT(kUnknown == drp->fResult);
epoger@google.com5fd53852012-03-22 18:20:06 +0000679
epoger@google.com46256ea2012-05-22 13:45:35 +0000680 SkData* baseFileBits;
681 SkData* comparisonFileBits;
682 if (NULL == (baseFileBits = read_file(basePath.c_str()))) {
683 SkDebugf("WARNING: couldn't read base file <%s>\n",
684 basePath.c_str());
685 drp->fResult = kBaseMissing;
686 } else if (NULL == (comparisonFileBits = read_file(
687 comparisonPath.c_str()))) {
688 SkDebugf("WARNING: couldn't read comparison file <%s>\n",
689 comparisonPath.c_str());
690 drp->fResult = kComparisonMissing;
691 } else {
692 if (are_buffers_equal(baseFileBits, comparisonFileBits)) {
693 drp->fResult = kEqualBits;
694 } else if (get_bitmaps(baseFileBits, comparisonFileBits, drp)) {
695 create_and_write_diff_image(drp, dmp, colorThreshold,
696 outputDir, *baseFiles[i]);
697 } else {
698 drp->fResult = kDifferentOther;
699 }
epoger@google.com5fd53852012-03-22 18:20:06 +0000700 }
epoger@google.com46256ea2012-05-22 13:45:35 +0000701 if (baseFileBits) {
702 baseFileBits->unref();
703 }
704 if (comparisonFileBits) {
705 comparisonFileBits->unref();
706 }
epoger@google.com5fd53852012-03-22 18:20:06 +0000707 ++i;
708 ++j;
709 }
epoger@google.com46256ea2012-05-22 13:45:35 +0000710 SkASSERT(kUnknown != drp->fResult);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000711 differences->push(drp);
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000712 summary->add(drp);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000713 }
epoger@google.com5fd53852012-03-22 18:20:06 +0000714
715 for (; i < baseFiles.count(); ++i) {
716 // files only in baseDir
717 SkString basePath (baseDir);
718 basePath.append(*baseFiles[i]);
719 SkString comparisonPath;
720 DiffRecord *drp = new DiffRecord(*baseFiles[i], basePath,
epoger@google.com292aff62012-05-16 14:57:28 +0000721 comparisonPath, kComparisonMissing);
epoger@google.com5fd53852012-03-22 18:20:06 +0000722 differences->push(drp);
723 summary->add(drp);
724 }
725
726 for (; j < comparisonFiles.count(); ++j) {
727 // files only in comparisonDir
728 SkString basePath;
729 SkString comparisonPath(comparisonDir);
730 comparisonPath.append(*comparisonFiles[j]);
731 DiffRecord *drp = new DiffRecord(*comparisonFiles[j], basePath,
epoger@google.com292aff62012-05-16 14:57:28 +0000732 comparisonPath, kBaseMissing);
epoger@google.com5fd53852012-03-22 18:20:06 +0000733 differences->push(drp);
734 summary->add(drp);
735 }
736
737 release_file_list(&baseFiles);
738 release_file_list(&comparisonFiles);
tomhudson@google.com7d042802011-07-14 13:15:55 +0000739}
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000740
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000741/// Make layout more consistent by scaling image to 240 height, 360 width,
742/// or natural size, whichever is smallest.
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000743static int compute_image_height (int height, int width) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000744 int retval = 240;
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000745 if (height < retval) {
746 retval = height;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000747 }
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000748 float scale = (float) retval / height;
749 if (width * scale > 360) {
750 scale = (float) 360 / width;
bsalomon@google.com8e06dab2011-10-07 20:03:39 +0000751 retval = static_cast<int>(height * scale);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000752 }
753 return retval;
754}
755
epoger@google.com25d961c2012-02-02 20:50:36 +0000756static void print_table_header (SkFILEWStream* stream,
757 const int matchCount,
758 const int colorThreshold,
759 const RecordArray& differences,
760 const SkString &baseDir,
epoger@google.coma2b793c2012-05-15 14:58:53 +0000761 const SkString &comparisonDir,
762 bool doOutputDate=false) {
epoger@google.com25d961c2012-02-02 20:50:36 +0000763 stream->writeText("<table>\n");
764 stream->writeText("<tr><th>");
epoger@google.coma2b793c2012-05-15 14:58:53 +0000765 if (doOutputDate) {
766 SkTime::DateTime dt;
767 SkTime::GetDateTime(&dt);
768 stream->writeText("SkDiff run at ");
769 stream->writeDecAsText(dt.fHour);
770 stream->writeText(":");
771 if (dt.fMinute < 10) {
772 stream->writeText("0");
773 }
774 stream->writeDecAsText(dt.fMinute);
775 stream->writeText(":");
776 if (dt.fSecond < 10) {
777 stream->writeText("0");
778 }
779 stream->writeDecAsText(dt.fSecond);
780 stream->writeText("<br>");
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000781 }
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000782 stream->writeDecAsText(matchCount);
783 stream->writeText(" of ");
784 stream->writeDecAsText(differences.count());
785 stream->writeText(" images matched ");
786 if (colorThreshold == 0) {
787 stream->writeText("exactly");
788 } else {
789 stream->writeText("within ");
790 stream->writeDecAsText(colorThreshold);
791 stream->writeText(" color units per component");
792 }
793 stream->writeText(".<br>");
epoger@google.com25d961c2012-02-02 20:50:36 +0000794 stream->writeText("</th>\n<th>");
795 stream->writeText("every different pixel shown in white");
796 stream->writeText("</th>\n<th>");
797 stream->writeText("color difference at each pixel");
798 stream->writeText("</th>\n<th>");
799 stream->writeText(baseDir.c_str());
800 stream->writeText("</th>\n<th>");
801 stream->writeText(comparisonDir.c_str());
802 stream->writeText("</th>\n");
803 stream->writeText("</tr>\n");
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000804}
805
806static void print_pixel_count (SkFILEWStream* stream,
807 const DiffRecord& diff) {
808 stream->writeText("<br>(");
bsalomon@google.com8e06dab2011-10-07 20:03:39 +0000809 stream->writeDecAsText(static_cast<int>(diff.fFractionDifference *
810 diff.fBaseWidth *
811 diff.fBaseHeight));
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000812 stream->writeText(" pixels)");
tomhudson@google.com5b325292011-05-24 19:41:13 +0000813/*
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000814 stream->writeDecAsText(diff.fWeightedFraction *
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000815 diff.fBaseWidth *
816 diff.fBaseHeight);
tomhudson@google.com5b325292011-05-24 19:41:13 +0000817 stream->writeText(" weighted pixels)");
818*/
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000819}
820
821static void print_label_cell (SkFILEWStream* stream,
822 const DiffRecord& diff) {
epoger@google.com46256ea2012-05-22 13:45:35 +0000823 char metricBuf [20];
824
825 stream->writeText("<td><b>");
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000826 stream->writeText(diff.fFilename.c_str());
epoger@google.com46256ea2012-05-22 13:45:35 +0000827 stream->writeText("</b><br>");
epoger@google.com292aff62012-05-16 14:57:28 +0000828 switch (diff.fResult) {
epoger@google.com46256ea2012-05-22 13:45:35 +0000829 case kEqualBits:
830 SkDEBUGFAIL("should not encounter DiffRecord with kEqualBits here");
831 return;
832 case kEqualPixels:
833 SkDEBUGFAIL("should not encounter DiffRecord with kEqualPixels here");
epoger@google.com5fd53852012-03-22 18:20:06 +0000834 return;
epoger@google.com292aff62012-05-16 14:57:28 +0000835 case kDifferentSizes:
epoger@google.com46256ea2012-05-22 13:45:35 +0000836 stream->writeText("Image sizes differ</td>");
837 return;
838 case kDifferentPixels:
839 sprintf(metricBuf, "%12.4f%%", 100 * diff.fFractionDifference);
840 stream->writeText(metricBuf);
841 stream->writeText(" of pixels differ");
842 stream->writeText("\n (");
843 sprintf(metricBuf, "%12.4f%%", 100 * diff.fWeightedFraction);
844 stream->writeText(metricBuf);
845 stream->writeText(" weighted)");
846 // Write the actual number of pixels that differ if it's < 1%
847 if (diff.fFractionDifference < 0.01) {
848 print_pixel_count(stream, diff);
849 }
850 stream->writeText("<br>Average color mismatch ");
851 stream->writeDecAsText(static_cast<int>(MAX3(diff.fAverageMismatchR,
852 diff.fAverageMismatchG,
853 diff.fAverageMismatchB)));
854 stream->writeText("<br>Max color mismatch ");
855 stream->writeDecAsText(MAX3(diff.fMaxMismatchR,
856 diff.fMaxMismatchG,
857 diff.fMaxMismatchB));
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +0000858 stream->writeText("</td>");
epoger@google.com46256ea2012-05-22 13:45:35 +0000859 break;
860 case kDifferentOther:
861 stream->writeText("Files differ; unable to parse one or both files</td>");
862 return;
863 case kBaseMissing:
864 stream->writeText("Missing from baseDir</td>");
865 return;
866 case kComparisonMissing:
867 stream->writeText("Missing from comparisonDir</td>");
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +0000868 return;
epoger@google.com292aff62012-05-16 14:57:28 +0000869 default:
epoger@google.com46256ea2012-05-22 13:45:35 +0000870 SkDEBUGFAIL("encountered DiffRecord with unknown result type");
871 return;
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +0000872 }
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000873}
874
875static void print_image_cell (SkFILEWStream* stream,
tomhudson@google.com4e305982011-07-13 17:42:46 +0000876 const SkString& path,
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000877 int height) {
878 stream->writeText("<td><a href=\"");
tomhudson@google.com4e305982011-07-13 17:42:46 +0000879 stream->writeText(path.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000880 stream->writeText("\"><img src=\"");
tomhudson@google.com4e305982011-07-13 17:42:46 +0000881 stream->writeText(path.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000882 stream->writeText("\" height=\"");
883 stream->writeDecAsText(height);
884 stream->writeText("px\"></a></td>");
885}
886
caryclark@google.com3dd45912012-06-06 12:11:10 +0000887#if 0 // UNUSED
epoger@google.com01f78702012-04-12 16:32:04 +0000888static void print_text_cell (SkFILEWStream* stream, const char* text) {
889 stream->writeText("<td align=center>");
890 if (NULL != text) {
891 stream->writeText(text);
892 }
893 stream->writeText("</td>");
894}
caryclark@google.com3dd45912012-06-06 12:11:10 +0000895#endif
epoger@google.com01f78702012-04-12 16:32:04 +0000896
epoger@google.com5fd53852012-03-22 18:20:06 +0000897static void print_diff_with_missing_file(SkFILEWStream* stream,
898 DiffRecord& diff,
899 const SkString& relativePath) {
900 stream->writeText("<tr>\n");
901 print_label_cell(stream, diff);
902 stream->writeText("<td>N/A</td>");
903 stream->writeText("<td>N/A</td>");
epoger@google.com292aff62012-05-16 14:57:28 +0000904 if (kBaseMissing != diff.fResult) {
epoger@google.com5fd53852012-03-22 18:20:06 +0000905 int h, w;
906 if (!get_bitmap_height_width(diff.fBasePath, &h, &w)) {
907 stream->writeText("<td>N/A</td>");
908 } else {
909 int height = compute_image_height(h, w);
910 if (!diff.fBasePath.startsWith(PATH_DIV_STR)) {
911 diff.fBasePath.prepend(relativePath);
912 }
913 print_image_cell(stream, diff.fBasePath, height);
914 }
915 } else {
916 stream->writeText("<td>N/A</td>");
917 }
epoger@google.com292aff62012-05-16 14:57:28 +0000918 if (kComparisonMissing != diff.fResult) {
epoger@google.com5fd53852012-03-22 18:20:06 +0000919 int h, w;
920 if (!get_bitmap_height_width(diff.fComparisonPath, &h, &w)) {
921 stream->writeText("<td>N/A</td>");
922 } else {
923 int height = compute_image_height(h, w);
924 if (!diff.fComparisonPath.startsWith(PATH_DIV_STR)) {
925 diff.fComparisonPath.prepend(relativePath);
926 }
927 print_image_cell(stream, diff.fComparisonPath, height);
928 }
929 } else {
930 stream->writeText("<td>N/A</td>");
931 }
932 stream->writeText("</tr>\n");
933 stream->flush();
934}
935
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000936static void print_diff_page (const int matchCount,
937 const int colorThreshold,
938 const RecordArray& differences,
939 const SkString& baseDir,
940 const SkString& comparisonDir,
941 const SkString& outputDir) {
942
epoger@google.coma5f406e2012-05-01 13:26:16 +0000943 SkASSERT(!baseDir.isEmpty());
944 SkASSERT(!comparisonDir.isEmpty());
945 SkASSERT(!outputDir.isEmpty());
946
tomhudson@google.com5b325292011-05-24 19:41:13 +0000947 SkString outputPath (outputDir);
948 outputPath.append("index.html");
949 //SkFILEWStream outputStream ("index.html");
950 SkFILEWStream outputStream (outputPath.c_str());
951
tomhudson@google.com4e305982011-07-13 17:42:46 +0000952 // Need to convert paths from relative-to-cwd to relative-to-outputDir
tomhudson@google.com5b325292011-05-24 19:41:13 +0000953 // FIXME this doesn't work if there are '..' inside the outputDir
954 unsigned int ui;
955 SkString relativePath;
956 for (ui = 0; ui < outputDir.size(); ui++) {
bsalomon@google.com1a315fe2011-09-23 14:56:37 +0000957 if (outputDir[ui] == PATH_DIV_CHAR) {
958 relativePath.append(".." PATH_DIV_STR);
tomhudson@google.com5b325292011-05-24 19:41:13 +0000959 }
960 }
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000961
962 outputStream.writeText("<html>\n<body>\n");
epoger@google.com25d961c2012-02-02 20:50:36 +0000963 print_table_header(&outputStream, matchCount, colorThreshold, differences,
964 baseDir, comparisonDir);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000965 int i;
966 for (i = 0; i < differences.count(); i++) {
967 DiffRecord* diff = differences[i];
epoger@google.com5fd53852012-03-22 18:20:06 +0000968
epoger@google.com292aff62012-05-16 14:57:28 +0000969 switch (diff->fResult) {
epoger@google.com46256ea2012-05-22 13:45:35 +0000970 // Cases in which there is no diff to report.
971 case kEqualBits:
epoger@google.com292aff62012-05-16 14:57:28 +0000972 case kEqualPixels:
973 continue;
epoger@google.com46256ea2012-05-22 13:45:35 +0000974 // Cases in which we want a detailed pixel diff.
975 case kDifferentPixels:
976 break;
977 // Cases in which the files differed, but we can't display the diff.
978 case kDifferentSizes:
979 case kDifferentOther:
epoger@google.com292aff62012-05-16 14:57:28 +0000980 case kBaseMissing:
epoger@google.com292aff62012-05-16 14:57:28 +0000981 case kComparisonMissing:
epoger@google.com5fd53852012-03-22 18:20:06 +0000982 print_diff_with_missing_file(&outputStream, *diff, relativePath);
983 continue;
epoger@google.com292aff62012-05-16 14:57:28 +0000984 default:
epoger@google.com46256ea2012-05-22 13:45:35 +0000985 SkDEBUGFAIL("encountered DiffRecord with unknown result type");
986 continue;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000987 }
epoger@google.com5fd53852012-03-22 18:20:06 +0000988
bsalomon@google.com1a315fe2011-09-23 14:56:37 +0000989 if (!diff->fBasePath.startsWith(PATH_DIV_STR)) {
tomhudson@google.com4e305982011-07-13 17:42:46 +0000990 diff->fBasePath.prepend(relativePath);
991 }
bsalomon@google.com1a315fe2011-09-23 14:56:37 +0000992 if (!diff->fComparisonPath.startsWith(PATH_DIV_STR)) {
tomhudson@google.com4e305982011-07-13 17:42:46 +0000993 diff->fComparisonPath.prepend(relativePath);
994 }
epoger@google.com5fd53852012-03-22 18:20:06 +0000995
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000996 int height = compute_image_height(diff->fBaseHeight, diff->fBaseWidth);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000997 outputStream.writeText("<tr>\n");
998 print_label_cell(&outputStream, *diff);
epoger@google.com46256ea2012-05-22 13:45:35 +0000999 print_image_cell(&outputStream,
1000 filename_to_white_filename(diff->fFilename), height);
1001 print_image_cell(&outputStream,
1002 filename_to_diff_filename(diff->fFilename), height);
epoger@google.com25d961c2012-02-02 20:50:36 +00001003 print_image_cell(&outputStream, diff->fBasePath, height);
tomhudson@google.com4e305982011-07-13 17:42:46 +00001004 print_image_cell(&outputStream, diff->fComparisonPath, height);
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001005 outputStream.writeText("</tr>\n");
1006 outputStream.flush();
1007 }
1008 outputStream.writeText("</table>\n");
1009 outputStream.writeText("</body>\n</html>\n");
1010 outputStream.flush();
1011}
1012
1013static void usage (char * argv0) {
1014 SkDebugf("Skia baseline image diff tool\n");
epoger@google.coma5f406e2012-05-01 13:26:16 +00001015 SkDebugf("\n"
1016"Usage: \n"
1017" %s <baseDir> <comparisonDir> [outputDir] \n"
epoger@google.coma611c3e2012-05-18 20:10:06 +00001018, argv0, argv0);
epoger@google.coma5f406e2012-05-01 13:26:16 +00001019 SkDebugf("\n"
1020"Arguments: \n"
1021" --nodiffs: don't write out image diffs or index.html, just generate \n"
1022" report on stdout \n"
1023" --threshold <n>: only report differences > n (per color channel) [default 0]\n"
1024" --match: compare files whose filenames contain this substring; if \n"
1025" unspecified, compare ALL files. \n"
1026" this flag may be repeated to add more matching substrings. \n"
1027" --nomatch: regardless of --match, DO NOT compare files whose filenames \n"
1028" contain this substring. \n"
1029" this flag may be repeated to add more forbidden substrings. \n"
tomhudson@google.com7d042802011-07-14 13:15:55 +00001030" --sortbymismatch: sort by average color channel mismatch\n");
1031 SkDebugf(
epoger@google.coma5f406e2012-05-01 13:26:16 +00001032" --sortbymaxmismatch: sort by worst color channel mismatch;\n"
1033" break ties with -sortbymismatch\n"
1034" [default sort is by fraction of pixels mismatching]\n");
tomhudson@google.com5b325292011-05-24 19:41:13 +00001035 SkDebugf(
tomhudson@google.com7d042802011-07-14 13:15:55 +00001036" --weighted: sort by # pixels different weighted by color difference\n");
1037 SkDebugf(
epoger@google.coma611c3e2012-05-18 20:10:06 +00001038" baseDir: directory to read baseline images from.\n");
tomhudson@google.com9dc527b2011-06-09 15:47:10 +00001039 SkDebugf(
epoger@google.coma5f406e2012-05-01 13:26:16 +00001040" comparisonDir: directory to read comparison images from\n");
1041 SkDebugf(
1042" outputDir: directory to write difference images and index.html to; \n"
epoger@google.coma611c3e2012-05-18 20:10:06 +00001043" defaults to comparisonDir \n");
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001044}
1045
1046int main (int argc, char ** argv) {
1047 DiffMetricProc diffProc = compute_diff_pmcolor;
reed@google.comc7a67cb2012-05-07 14:52:12 +00001048 int (*sortProc)(const void*, const void*) = SkCastForQSort(compare_diff_metrics);
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001049
1050 // Maximum error tolerated in any one color channel in any one pixel before
1051 // a difference is reported.
1052 int colorThreshold = 0;
1053 SkString baseDir;
1054 SkString comparisonDir;
1055 SkString outputDir;
epoger@google.coma5f406e2012-05-01 13:26:16 +00001056 StringArray matchSubstrings;
1057 StringArray nomatchSubstrings;
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001058
epoger@google.coma5f406e2012-05-01 13:26:16 +00001059 bool generateDiffs = true;
tomhudson@google.com7d042802011-07-14 13:15:55 +00001060
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001061 RecordArray differences;
tomhudson@google.com9dc527b2011-06-09 15:47:10 +00001062 DiffSummary summary;
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001063
epoger@google.coma5f406e2012-05-01 13:26:16 +00001064 int i;
1065 int numUnflaggedArguments = 0;
1066 for (i = 1; i < argc; i++) {
tomhudson@google.com7d042802011-07-14 13:15:55 +00001067 if (!strcmp(argv[i], "--help")) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001068 usage(argv[0]);
1069 return 0;
1070 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001071 if (!strcmp(argv[i], "--nodiffs")) {
1072 generateDiffs = false;
1073 continue;
1074 }
1075 if (!strcmp(argv[i], "--threshold")) {
1076 colorThreshold = atoi(argv[++i]);
1077 continue;
1078 }
1079 if (!strcmp(argv[i], "--match")) {
1080 matchSubstrings.push(new SkString(argv[++i]));
1081 continue;
1082 }
1083 if (!strcmp(argv[i], "--nomatch")) {
1084 nomatchSubstrings.push(new SkString(argv[++i]));
1085 continue;
1086 }
tomhudson@google.com7d042802011-07-14 13:15:55 +00001087 if (!strcmp(argv[i], "--sortbymismatch")) {
reed@google.comc7a67cb2012-05-07 14:52:12 +00001088 sortProc = SkCastForQSort(compare_diff_mean_mismatches);
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001089 continue;
1090 }
tomhudson@google.com7d042802011-07-14 13:15:55 +00001091 if (!strcmp(argv[i], "--sortbymaxmismatch")) {
reed@google.comc7a67cb2012-05-07 14:52:12 +00001092 sortProc = SkCastForQSort(compare_diff_max_mismatches);
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001093 continue;
1094 }
tomhudson@google.com7d042802011-07-14 13:15:55 +00001095 if (!strcmp(argv[i], "--weighted")) {
reed@google.comc7a67cb2012-05-07 14:52:12 +00001096 sortProc = SkCastForQSort(compare_diff_weighted);
tomhudson@google.com5b325292011-05-24 19:41:13 +00001097 continue;
1098 }
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001099 if (argv[i][0] != '-') {
epoger@google.coma5f406e2012-05-01 13:26:16 +00001100 switch (numUnflaggedArguments++) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001101 case 0:
1102 baseDir.set(argv[i]);
1103 continue;
1104 case 1:
1105 comparisonDir.set(argv[i]);
1106 continue;
1107 case 2:
1108 outputDir.set(argv[i]);
1109 continue;
1110 default:
epoger@google.coma5f406e2012-05-01 13:26:16 +00001111 SkDebugf("extra unflagged argument <%s>\n", argv[i]);
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001112 usage(argv[0]);
1113 return 0;
1114 }
1115 }
1116
1117 SkDebugf("Unrecognized argument <%s>\n", argv[i]);
1118 usage(argv[0]);
1119 return 0;
1120 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001121
epoger@google.coma5f406e2012-05-01 13:26:16 +00001122 if (numUnflaggedArguments == 2) {
tomhudson@google.com9dc527b2011-06-09 15:47:10 +00001123 outputDir = comparisonDir;
epoger@google.coma5f406e2012-05-01 13:26:16 +00001124 } else if (numUnflaggedArguments != 3) {
tomhudson@google.com9dc527b2011-06-09 15:47:10 +00001125 usage(argv[0]);
1126 return 0;
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001127 }
1128
bsalomon@google.com1a315fe2011-09-23 14:56:37 +00001129 if (!baseDir.endsWith(PATH_DIV_STR)) {
1130 baseDir.append(PATH_DIV_STR);
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001131 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001132 printf("baseDir is [%s]\n", baseDir.c_str());
1133
bsalomon@google.com1a315fe2011-09-23 14:56:37 +00001134 if (!comparisonDir.endsWith(PATH_DIV_STR)) {
1135 comparisonDir.append(PATH_DIV_STR);
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001136 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001137 printf("comparisonDir is [%s]\n", comparisonDir.c_str());
1138
bsalomon@google.com1a315fe2011-09-23 14:56:37 +00001139 if (!outputDir.endsWith(PATH_DIV_STR)) {
1140 outputDir.append(PATH_DIV_STR);
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001141 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001142 if (generateDiffs) {
1143 printf("writing diffs to outputDir is [%s]\n", outputDir.c_str());
1144 } else {
1145 printf("not writing any diffs to outputDir [%s]\n", outputDir.c_str());
1146 outputDir.set("");
1147 }
1148
1149 // Default substring matching:
1150 // - No matter what, don't match any PDF files.
1151 // We may want to change this later, but for now this maintains the filter
1152 // that get_file_list() used to always apply.
1153 // - If no matchSubstrings were specified, match ALL strings.
1154 nomatchSubstrings.push(new SkString(".pdf"));
1155 if (matchSubstrings.isEmpty()) {
1156 matchSubstrings.push(new SkString(""));
1157 }
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001158
epoger@google.coma611c3e2012-05-18 20:10:06 +00001159 create_diff_images(diffProc, colorThreshold, &differences,
1160 baseDir, comparisonDir, outputDir,
1161 matchSubstrings, nomatchSubstrings, &summary);
tomhudson@google.com9dc527b2011-06-09 15:47:10 +00001162 summary.print();
tomhudson@google.com7d042802011-07-14 13:15:55 +00001163
1164 if (differences.count()) {
reed@google.comc7a67cb2012-05-07 14:52:12 +00001165 qsort(differences.begin(), differences.count(),
1166 sizeof(DiffRecord*), sortProc);
tomhudson@google.com7d042802011-07-14 13:15:55 +00001167 }
epoger@google.com66008522012-05-16 17:40:57 +00001168
epoger@google.coma5f406e2012-05-01 13:26:16 +00001169 if (generateDiffs) {
1170 print_diff_page(summary.fNumMatches, colorThreshold, differences,
1171 baseDir, comparisonDir, outputDir);
1172 }
epoger@google.com76222c02012-05-31 15:12:09 +00001173
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001174 for (i = 0; i < differences.count(); i++) {
1175 delete differences[i];
1176 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001177 matchSubstrings.deleteAll();
1178 nomatchSubstrings.deleteAll();
epoger@google.combe6188d2012-05-31 15:13:45 +00001179
1180 return summary.fNumMismatches;
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001181}