blob: f038b2c183deedd56dd7499ef1381bff8400e673 [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"
9#include "SkImageDecoder.h"
10#include "SkImageEncoder.h"
11#include "SkOSFile.h"
12#include "SkStream.h"
13#include "SkTDArray.h"
14#include "SkTemplates.h"
15#include "SkTime.h"
16#include "SkTSearch.h"
17#include "SkTypes.h"
18
19/**
20 * skdiff
21 *
22 * Given three directory names, expects to find identically-named files in
23 * each of the first two; the first are treated as a set of baseline,
24 * the second a set of variant images, and a diff image is written into the
25 * third directory for each pair.
tomhudson@google.com7d042802011-07-14 13:15:55 +000026 * Creates an index.html in the current third directory to compare each
tomhudson@google.com4b33d282011-04-27 15:39:30 +000027 * pair that does not match exactly.
28 * Does *not* recursively descend directories.
tomhudson@google.com7d042802011-07-14 13:15:55 +000029 *
30 * With the --chromium flag, *does* recursively descend the first directory
31 * named, comparing *-expected.png with *-actual.png and writing diff
32 * images into the second directory, also writing index.html there.
tomhudson@google.com4b33d282011-04-27 15:39:30 +000033 */
34
bsalomon@google.com1a315fe2011-09-23 14:56:37 +000035#if SK_BUILD_FOR_WIN32
36 #define PATH_DIV_STR "\\"
37 #define PATH_DIV_CHAR '\\'
38#else
39 #define PATH_DIV_STR "/"
40 #define PATH_DIV_CHAR '/'
41#endif
42
epoger@google.com292aff62012-05-16 14:57:28 +000043// Result of comparison for each pair of files.
44// TODO: we don't actually use all of these yet.
45enum Result {
46 kEqualBits, // both files in the pair contain exactly the same bits
47 kEqualPixels, // not bitwise equal, but their pixels are exactly the same
48 kDifferentSizes, // both are images we can parse, but of different sizes
49 kDifferentPixels,// both are images we can parse, but with different pixels
50 kDifferentOther, // files have different bits but are not parsable images
51 kBaseMissing, // missing from baseDir
52 kComparisonMissing,// missing from comparisonDir
53 kUnknown
54};
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.com7d042802011-07-14 13:15:55 +000079 // These asserts are valid for GM, but not for --chromium
80 //SkASSERT(basePath.endsWith(filename.c_str()));
81 //SkASSERT(comparisonPath.endsWith(filename.c_str()));
tomhudson@google.com4e305982011-07-13 17:42:46 +000082 };
tomhudson@google.com4b33d282011-04-27 15:39:30 +000083
84 SkString fFilename;
tomhudson@google.com4e305982011-07-13 17:42:46 +000085 SkString fBasePath;
86 SkString fComparisonPath;
tomhudson@google.com4b33d282011-04-27 15:39:30 +000087
tomhudson@google.com9b540ce2011-08-02 14:10:04 +000088 SkBitmap* fBaseBitmap;
89 SkBitmap* fComparisonBitmap;
90 SkBitmap* fDifferenceBitmap;
epoger@google.com25d961c2012-02-02 20:50:36 +000091 SkBitmap* fWhiteBitmap;
tomhudson@google.com9b540ce2011-08-02 14:10:04 +000092
93 int fBaseHeight;
94 int fBaseWidth;
tomhudson@google.com4b33d282011-04-27 15:39:30 +000095
96 /// Arbitrary floating-point metric to be used to sort images from most
97 /// to least different from baseline; values of 0 will be omitted from the
98 /// summary webpage.
tomhudson@google.com9dc527b2011-06-09 15:47:10 +000099 float fFractionDifference;
100 float fWeightedFraction;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000101
102 float fAverageMismatchR;
103 float fAverageMismatchG;
104 float fAverageMismatchB;
105
106 uint32_t fMaxMismatchR;
107 uint32_t fMaxMismatchG;
108 uint32_t fMaxMismatchB;
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +0000109
epoger@google.com292aff62012-05-16 14:57:28 +0000110 /// Which category of diff result.
111 Result fResult;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000112};
113
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000114#define MAX2(a,b) (((b) < (a)) ? (a) : (b))
115#define MAX3(a,b,c) (((b) < (a)) ? MAX2((a), (c)) : MAX2((b), (c)))
116
epoger@google.com25d961c2012-02-02 20:50:36 +0000117const SkPMColor PMCOLOR_WHITE = SkPreMultiplyColor(SK_ColorWHITE);
118const SkPMColor PMCOLOR_BLACK = SkPreMultiplyColor(SK_ColorBLACK);
119
epoger@google.coma5f406e2012-05-01 13:26:16 +0000120typedef SkTDArray<SkString*> StringArray;
121typedef StringArray FileArray;
epoger@google.com5fd53852012-03-22 18:20:06 +0000122
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000123struct DiffSummary {
124 DiffSummary ()
125 : fNumMatches (0)
126 , fNumMismatches (0)
127 , fMaxMismatchV (0)
128 , fMaxMismatchPercent (0) { };
129
epoger@google.com5fd53852012-03-22 18:20:06 +0000130 ~DiffSummary() {
131 fBaseMissing.deleteAll();
132 fComparisonMissing.deleteAll();
133 }
134
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000135 uint32_t fNumMatches;
136 uint32_t fNumMismatches;
137 uint32_t fMaxMismatchV;
138 float fMaxMismatchPercent;
139
epoger@google.com5fd53852012-03-22 18:20:06 +0000140 FileArray fBaseMissing;
141 FileArray fComparisonMissing;
142
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000143 void print () {
epoger@google.com5fd53852012-03-22 18:20:06 +0000144 int n = fBaseMissing.count();
145 if (n > 0) {
146 printf("Missing in baseDir:\n");
147 for (int i = 0; i < n; ++i) {
148 printf("\t%s\n", fBaseMissing[i]->c_str());
149 }
150 }
151 n = fComparisonMissing.count();
152 if (n > 0) {
153 printf("Missing in comparisonDir:\n");
154 for (int i = 0; i < n; ++i) {
155 printf("\t%s\n", fComparisonMissing[i]->c_str());
156 }
157 }
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000158 printf("%d of %d images matched.\n", fNumMatches,
159 fNumMatches + fNumMismatches);
160 if (fNumMismatches > 0) {
161 printf("Maximum pixel intensity mismatch %d\n", fMaxMismatchV);
162 printf("Largest area mismatch was %.2f%% of pixels\n",
163 fMaxMismatchPercent);
164 }
165
166 }
167
168 void add (DiffRecord* drp) {
epoger@google.com292aff62012-05-16 14:57:28 +0000169 // Maintain current (and, I think, incorrect) skdiff behavior:
170 // If we were unable to parse either file in the pair as an image,
171 // treat them as matching.
172 // TODO: Remove this logic and change the results for the better.
173 if (kUnknown == drp->fResult) {
174 drp->fResult = kEqualPixels;
175 }
176
177 switch (drp->fResult) {
178 case kEqualPixels:
179 fNumMatches++;
180 break;
181 case kBaseMissing:
epoger@google.com5fd53852012-03-22 18:20:06 +0000182 fBaseMissing.push(new SkString(drp->fFilename));
183 fNumMismatches++;
epoger@google.com292aff62012-05-16 14:57:28 +0000184 break;
185 case kComparisonMissing:
epoger@google.com5fd53852012-03-22 18:20:06 +0000186 fComparisonMissing.push(new SkString(drp->fFilename));
187 fNumMismatches++;
epoger@google.com292aff62012-05-16 14:57:28 +0000188 break;
189 default:
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000190 fNumMismatches++;
191 if (drp->fFractionDifference * 100 > fMaxMismatchPercent) {
192 fMaxMismatchPercent = drp->fFractionDifference * 100;
193 }
tomhudson@google.com88a0e052011-06-09 18:54:01 +0000194 uint32_t value = MAX3(drp->fMaxMismatchR, drp->fMaxMismatchG,
195 drp->fMaxMismatchB);
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000196 if (value > fMaxMismatchV) {
197 fMaxMismatchV = value;
198 }
epoger@google.com292aff62012-05-16 14:57:28 +0000199 break;
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000200 }
201 }
202};
203
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000204typedef SkTDArray<DiffRecord*> RecordArray;
205
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000206/// Comparison routine for qsort; sorts by fFractionDifference
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000207/// from largest to smallest.
208static int compare_diff_metrics (DiffRecord** lhs, DiffRecord** rhs) {
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000209 if ((*lhs)->fFractionDifference < (*rhs)->fFractionDifference) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000210 return 1;
211 }
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000212 if ((*rhs)->fFractionDifference < (*lhs)->fFractionDifference) {
tomhudson@google.com5b325292011-05-24 19:41:13 +0000213 return -1;
214 }
215 return 0;
216}
217
218static int compare_diff_weighted (DiffRecord** lhs, DiffRecord** rhs) {
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000219 if ((*lhs)->fWeightedFraction < (*rhs)->fWeightedFraction) {
tomhudson@google.com5b325292011-05-24 19:41:13 +0000220 return 1;
221 }
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000222 if ((*lhs)->fWeightedFraction > (*rhs)->fWeightedFraction) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000223 return -1;
224 }
225 return 0;
226}
227
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000228/// Comparison routine for qsort; sorts by max(fAverageMismatch{RGB})
229/// from largest to smallest.
230static int compare_diff_mean_mismatches (DiffRecord** lhs, DiffRecord** rhs) {
231 float leftValue = MAX3((*lhs)->fAverageMismatchR,
232 (*lhs)->fAverageMismatchG,
233 (*lhs)->fAverageMismatchB);
234 float rightValue = MAX3((*rhs)->fAverageMismatchR,
235 (*rhs)->fAverageMismatchG,
236 (*rhs)->fAverageMismatchB);
237 if (leftValue < rightValue) {
238 return 1;
239 }
240 if (rightValue < leftValue) {
241 return -1;
242 }
243 return 0;
244}
245
246/// Comparison routine for qsort; sorts by max(fMaxMismatch{RGB})
247/// from largest to smallest.
248static int compare_diff_max_mismatches (DiffRecord** lhs, DiffRecord** rhs) {
bsalomon@google.com8e06dab2011-10-07 20:03:39 +0000249 uint32_t leftValue = MAX3((*lhs)->fMaxMismatchR,
250 (*lhs)->fMaxMismatchG,
251 (*lhs)->fMaxMismatchB);
252 uint32_t rightValue = MAX3((*rhs)->fMaxMismatchR,
253 (*rhs)->fMaxMismatchG,
254 (*rhs)->fMaxMismatchB);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000255 if (leftValue < rightValue) {
256 return 1;
257 }
258 if (rightValue < leftValue) {
259 return -1;
260 }
261 return compare_diff_mean_mismatches(lhs, rhs);
262}
263
264
265
266/// Parameterized routine to compute the color of a pixel in a difference image.
267typedef SkPMColor (*DiffMetricProc)(SkPMColor, SkPMColor);
268
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +0000269static void expand_and_copy (int width, int height, SkBitmap** dest) {
270 SkBitmap* temp = new SkBitmap ();
271 temp->reset();
272 temp->setConfig((*dest)->config(), width, height);
273 temp->allocPixels();
274 (*dest)->copyPixelsTo(temp->getPixels(), temp->getSize(),
275 temp->rowBytes());
276 *dest = temp;
277}
278
tomhudson@google.com4e305982011-07-13 17:42:46 +0000279static bool get_bitmaps (DiffRecord* diffRecord) {
280 SkFILEStream compareStream(diffRecord->fComparisonPath.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000281 if (!compareStream.isValid()) {
282 SkDebugf("WARNING: couldn't open comparison file <%s>\n",
tomhudson@google.com4e305982011-07-13 17:42:46 +0000283 diffRecord->fComparisonPath.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000284 return false;
285 }
286
tomhudson@google.com4e305982011-07-13 17:42:46 +0000287 SkFILEStream baseStream(diffRecord->fBasePath.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000288 if (!baseStream.isValid()) {
289 SkDebugf("ERROR: couldn't open base file <%s>\n",
tomhudson@google.com4e305982011-07-13 17:42:46 +0000290 diffRecord->fBasePath.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000291 return false;
292 }
293
294 SkImageDecoder* codec = SkImageDecoder::Factory(&baseStream);
295 if (NULL == codec) {
296 SkDebugf("ERROR: no codec found for <%s>\n",
tomhudson@google.com4e305982011-07-13 17:42:46 +0000297 diffRecord->fBasePath.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000298 return false;
299 }
300
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +0000301 // In debug, the DLL will automatically be unloaded when this is deleted,
302 // but that shouldn't be a problem in release mode.
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000303 SkAutoTDelete<SkImageDecoder> ad(codec);
304
305 baseStream.rewind();
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000306 if (!codec->decode(&baseStream, diffRecord->fBaseBitmap,
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000307 SkBitmap::kARGB_8888_Config,
308 SkImageDecoder::kDecodePixels_Mode)) {
309 SkDebugf("ERROR: codec failed for <%s>\n",
tomhudson@google.com4e305982011-07-13 17:42:46 +0000310 diffRecord->fBasePath.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000311 return false;
312 }
313
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000314 diffRecord->fBaseWidth = diffRecord->fBaseBitmap->width();
315 diffRecord->fBaseHeight = diffRecord->fBaseBitmap->height();
316
317 if (!codec->decode(&compareStream, diffRecord->fComparisonBitmap,
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000318 SkBitmap::kARGB_8888_Config,
319 SkImageDecoder::kDecodePixels_Mode)) {
320 SkDebugf("ERROR: codec failed for <%s>\n",
tomhudson@google.com4e305982011-07-13 17:42:46 +0000321 diffRecord->fComparisonPath.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000322 return false;
323 }
324
325 return true;
326}
327
epoger@google.com5fd53852012-03-22 18:20:06 +0000328static bool get_bitmap_height_width(const SkString& path,
329 int *height, int *width) {
330 SkFILEStream stream(path.c_str());
331 if (!stream.isValid()) {
332 SkDebugf("ERROR: couldn't open file <%s>\n",
333 path.c_str());
334 return false;
335 }
336
337 SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
338 if (NULL == codec) {
339 SkDebugf("ERROR: no codec found for <%s>\n",
340 path.c_str());
341 return false;
342 }
343
344 SkAutoTDelete<SkImageDecoder> ad(codec);
345 SkBitmap bm;
346
347 stream.rewind();
348 if (!codec->decode(&stream, &bm,
349 SkBitmap::kARGB_8888_Config,
350 SkImageDecoder::kDecodePixels_Mode)) {
351 SkDebugf("ERROR: codec failed for <%s>\n",
352 path.c_str());
353 return false;
354 }
355
356 *height = bm.height();
357 *width = bm.width();
358
359 return true;
360}
361
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000362// from gm - thanks to PNG, we need to force all pixels 100% opaque
363static void force_all_opaque(const SkBitmap& bitmap) {
364 SkAutoLockPixels lock(bitmap);
365 for (int y = 0; y < bitmap.height(); y++) {
366 for (int x = 0; x < bitmap.width(); x++) {
367 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
368 }
369 }
370}
371
372// from gm
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000373static bool write_bitmap(const SkString& path, const SkBitmap* bitmap) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000374 SkBitmap copy;
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000375 bitmap->copyTo(&copy, SkBitmap::kARGB_8888_Config);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000376 force_all_opaque(copy);
377 return SkImageEncoder::EncodeFile(path.c_str(), copy,
378 SkImageEncoder::kPNG_Type, 100);
379}
380
381// from gm
382static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
383 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
384 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
385 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
386
387 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
388}
389
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000390static inline bool colors_match_thresholded(SkPMColor c0, SkPMColor c1,
391 const int threshold) {
392 int da = SkGetPackedA32(c0) - SkGetPackedA32(c1);
393 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
394 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
395 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
396
397 return ((SkAbs32(da) <= threshold) &&
398 (SkAbs32(dr) <= threshold) &&
399 (SkAbs32(dg) <= threshold) &&
400 (SkAbs32(db) <= threshold));
401}
402
403// based on gm
404static void compute_diff(DiffRecord* dr,
405 DiffMetricProc diffFunction,
406 const int colorThreshold) {
epoger@google.com25d961c2012-02-02 20:50:36 +0000407 SkAutoLockPixels alpDiff(*dr->fDifferenceBitmap);
408 SkAutoLockPixels alpWhite(*dr->fWhiteBitmap);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000409
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000410 const int w = dr->fComparisonBitmap->width();
411 const int h = dr->fComparisonBitmap->height();
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000412 int mismatchedPixels = 0;
413 int totalMismatchR = 0;
414 int totalMismatchG = 0;
415 int totalMismatchB = 0;
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +0000416
417 if (w != dr->fBaseWidth || h != dr->fBaseHeight) {
epoger@google.com292aff62012-05-16 14:57:28 +0000418 dr->fResult = kDifferentSizes;
419 dr->fFractionDifference = 1; // for sorting the diffs later
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +0000420 return;
421 }
tomhudson@google.com5b325292011-05-24 19:41:13 +0000422 // Accumulate fractionally different pixels, then divide out
423 // # of pixels at the end.
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000424 dr->fWeightedFraction = 0;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000425 for (int y = 0; y < h; y++) {
426 for (int x = 0; x < w; x++) {
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000427 SkPMColor c0 = *dr->fBaseBitmap->getAddr32(x, y);
428 SkPMColor c1 = *dr->fComparisonBitmap->getAddr32(x, y);
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000429 SkPMColor trueDifference = compute_diff_pmcolor(c0, c1);
430 SkPMColor outputDifference = diffFunction(c0, c1);
431 uint32_t thisR = SkGetPackedR32(trueDifference);
432 uint32_t thisG = SkGetPackedG32(trueDifference);
433 uint32_t thisB = SkGetPackedB32(trueDifference);
tomhudson@google.com5b325292011-05-24 19:41:13 +0000434 totalMismatchR += thisR;
435 totalMismatchG += thisG;
436 totalMismatchB += thisB;
437 // In HSV, value is defined as max RGB component.
438 int value = MAX3(thisR, thisG, thisB);
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000439 dr->fWeightedFraction += ((float) value) / 255;
tomhudson@google.com5b325292011-05-24 19:41:13 +0000440 if (thisR > dr->fMaxMismatchR) {
441 dr->fMaxMismatchR = thisR;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000442 }
tomhudson@google.com5b325292011-05-24 19:41:13 +0000443 if (thisG > dr->fMaxMismatchG) {
444 dr->fMaxMismatchG = thisG;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000445 }
tomhudson@google.com5b325292011-05-24 19:41:13 +0000446 if (thisB > dr->fMaxMismatchB) {
447 dr->fMaxMismatchB = thisB;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000448 }
449 if (!colors_match_thresholded(c0, c1, colorThreshold)) {
450 mismatchedPixels++;
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000451 *dr->fDifferenceBitmap->getAddr32(x, y) = outputDifference;
epoger@google.com25d961c2012-02-02 20:50:36 +0000452 *dr->fWhiteBitmap->getAddr32(x, y) = PMCOLOR_WHITE;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000453 } else {
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000454 *dr->fDifferenceBitmap->getAddr32(x, y) = 0;
epoger@google.com25d961c2012-02-02 20:50:36 +0000455 *dr->fWhiteBitmap->getAddr32(x, y) = PMCOLOR_BLACK;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000456 }
457 }
458 }
epoger@google.com292aff62012-05-16 14:57:28 +0000459 if (0 == mismatchedPixels) {
460 dr->fResult = kEqualPixels;
461 return;
462 }
463 dr->fResult = kDifferentPixels;
tomhudson@google.com5b325292011-05-24 19:41:13 +0000464 int pixelCount = w * h;
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000465 dr->fFractionDifference = ((float) mismatchedPixels) / pixelCount;
466 dr->fWeightedFraction /= pixelCount;
tomhudson@google.com5b325292011-05-24 19:41:13 +0000467 dr->fAverageMismatchR = ((float) totalMismatchR) / pixelCount;
468 dr->fAverageMismatchG = ((float) totalMismatchG) / pixelCount;
469 dr->fAverageMismatchB = ((float) totalMismatchB) / pixelCount;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000470}
471
epoger@google.com25d961c2012-02-02 20:50:36 +0000472static SkString filename_to_derived_filename (const SkString& filename,
473 const char *suffix) {
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000474 SkString diffName (filename);
475 const char* cstring = diffName.c_str();
476 int dotOffset = strrchr(cstring, '.') - cstring;
477 diffName.remove(dotOffset, diffName.size() - dotOffset);
epoger@google.com25d961c2012-02-02 20:50:36 +0000478 diffName.append(suffix);
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000479 return diffName;
480}
481
epoger@google.com25d961c2012-02-02 20:50:36 +0000482/// Given a image filename, returns the name of the file containing the
483/// associated difference image.
484static SkString filename_to_diff_filename (const SkString& filename) {
485 return filename_to_derived_filename(filename, "-diff.png");
486}
487
488/// Given a image filename, returns the name of the file containing the
489/// "white" difference image.
490static SkString filename_to_white_filename (const SkString& filename) {
491 return filename_to_derived_filename(filename, "-white.png");
492}
493
tomhudson@google.com7d042802011-07-14 13:15:55 +0000494/// Convert a chromium/WebKit LayoutTest "foo-expected.png" to "foo-actual.png"
495static SkString chrome_expected_path_to_actual (const SkString& expected) {
496 SkString actualPath (expected);
497 actualPath.remove(actualPath.size() - 13, 13);
498 actualPath.append("-actual.png");
499 return actualPath;
500}
501
502/// Convert a chromium/WebKit LayoutTest "foo-expected.png" to "foo.png"
503static SkString chrome_expected_name_to_short (const SkString& expected) {
504 SkString shortName (expected);
505 shortName.remove(shortName.size() - 13, 13);
506 shortName.append(".png");
507 return shortName;
508}
509
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000510static void release_bitmaps(DiffRecord* drp) {
511 delete drp->fBaseBitmap;
512 drp->fBaseBitmap = NULL;
513 delete drp->fComparisonBitmap;
514 drp->fComparisonBitmap = NULL;
515 delete drp->fDifferenceBitmap;
516 drp->fDifferenceBitmap = NULL;
epoger@google.com25d961c2012-02-02 20:50:36 +0000517 delete drp->fWhiteBitmap;
518 drp->fWhiteBitmap = NULL;
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000519}
520
tomhudson@google.com7d042802011-07-14 13:15:55 +0000521
epoger@google.coma5f406e2012-05-01 13:26:16 +0000522/// If outputDir.isEmpty(), don't write out diff files.
tomhudson@google.com7d042802011-07-14 13:15:55 +0000523static void create_and_write_diff_image(DiffRecord* drp,
524 DiffMetricProc dmp,
525 const int colorThreshold,
526 const SkString& outputDir,
527 const SkString& filename) {
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000528 const int w = drp->fBaseWidth;
529 const int h = drp->fBaseHeight;
530 drp->fDifferenceBitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h);
531 drp->fDifferenceBitmap->allocPixels();
epoger@google.com25d961c2012-02-02 20:50:36 +0000532 drp->fWhiteBitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h);
533 drp->fWhiteBitmap->allocPixels();
tomhudson@google.com4e305982011-07-13 17:42:46 +0000534 compute_diff(drp, dmp, colorThreshold);
tomhudson@google.com7d042802011-07-14 13:15:55 +0000535
epoger@google.coma5f406e2012-05-01 13:26:16 +0000536 if (!outputDir.isEmpty()) {
537 SkString differencePath (outputDir);
538 differencePath.append(filename_to_diff_filename(filename));
539 write_bitmap(differencePath, drp->fDifferenceBitmap);
540 SkString whitePath (outputDir);
541 whitePath.append(filename_to_white_filename(filename));
542 write_bitmap(whitePath, drp->fWhiteBitmap);
543 }
544
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000545 release_bitmaps(drp);
tomhudson@google.com4e305982011-07-13 17:42:46 +0000546}
547
epoger@google.coma5f406e2012-05-01 13:26:16 +0000548/// Returns true if string contains any of these substrings.
549static bool string_contains_any_of(const SkString& string,
550 const StringArray& substrings) {
551 for (int i = 0; i < substrings.count(); i++) {
552 if (string.contains(substrings[i]->c_str())) {
553 return true;
554 }
555 }
556 return false;
557}
558
559/// Iterate over dir and get all files that:
560/// - match any of the substrings in matchSubstrings, but...
561/// - DO NOT match any of the substrings in nomatchSubstrings
562/// Returns the list of files in *files.
563static void get_file_list(const SkString& dir,
564 const StringArray& matchSubstrings,
565 const StringArray& nomatchSubstrings,
566 FileArray *files) {
epoger@google.com5fd53852012-03-22 18:20:06 +0000567 SkOSFile::Iter it(dir.c_str());
568 SkString filename;
569 while (it.next(&filename)) {
epoger@google.coma5f406e2012-05-01 13:26:16 +0000570 if (string_contains_any_of(filename, matchSubstrings) &&
571 !string_contains_any_of(filename, nomatchSubstrings)) {
572 files->push(new SkString(filename));
epoger@google.com5fd53852012-03-22 18:20:06 +0000573 }
epoger@google.com5fd53852012-03-22 18:20:06 +0000574 }
575}
576
577static void release_file_list(FileArray *files) {
578 files->deleteAll();
579}
580
581/// Comparison routines for qsort, sort by file names.
582static int compare_file_name_metrics(SkString **lhs, SkString **rhs) {
583 return strcmp((*lhs)->c_str(), (*rhs)->c_str());
584}
585
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000586/// Creates difference images, returns the number that have a 0 metric.
epoger@google.coma5f406e2012-05-01 13:26:16 +0000587/// If outputDir.isEmpty(), don't write out diff files.
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000588static void create_diff_images (DiffMetricProc dmp,
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000589 const int colorThreshold,
590 RecordArray* differences,
591 const SkString& baseDir,
592 const SkString& comparisonDir,
593 const SkString& outputDir,
epoger@google.coma5f406e2012-05-01 13:26:16 +0000594 const StringArray& matchSubstrings,
595 const StringArray& nomatchSubstrings,
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000596 DiffSummary* summary) {
epoger@google.coma5f406e2012-05-01 13:26:16 +0000597 SkASSERT(!baseDir.isEmpty());
598 SkASSERT(!comparisonDir.isEmpty());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000599
epoger@google.com5fd53852012-03-22 18:20:06 +0000600 FileArray baseFiles;
601 FileArray comparisonFiles;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000602
epoger@google.coma5f406e2012-05-01 13:26:16 +0000603 get_file_list(baseDir, matchSubstrings, nomatchSubstrings, &baseFiles);
604 get_file_list(comparisonDir, matchSubstrings, nomatchSubstrings,
605 &comparisonFiles);
epoger@google.com5fd53852012-03-22 18:20:06 +0000606
epoger@google.coma5f406e2012-05-01 13:26:16 +0000607 if (!baseFiles.isEmpty()) {
reed@google.comc7a67cb2012-05-07 14:52:12 +0000608 qsort(baseFiles.begin(), baseFiles.count(), sizeof(SkString*),
609 SkCastForQSort(compare_file_name_metrics));
epoger@google.coma5f406e2012-05-01 13:26:16 +0000610 }
611 if (!comparisonFiles.isEmpty()) {
reed@google.comc7a67cb2012-05-07 14:52:12 +0000612 qsort(comparisonFiles.begin(), comparisonFiles.count(),
613 sizeof(SkString*), SkCastForQSort(compare_file_name_metrics));
epoger@google.coma5f406e2012-05-01 13:26:16 +0000614 }
615
epoger@google.com5fd53852012-03-22 18:20:06 +0000616 int i = 0;
617 int j = 0;
618
619 while (i < baseFiles.count() &&
620 j < comparisonFiles.count()) {
621
tomhudson@google.com4e305982011-07-13 17:42:46 +0000622 SkString basePath (baseDir);
epoger@google.com5fd53852012-03-22 18:20:06 +0000623 basePath.append(*baseFiles[i]);
tomhudson@google.com7d042802011-07-14 13:15:55 +0000624 SkString comparisonPath (comparisonDir);
epoger@google.com5fd53852012-03-22 18:20:06 +0000625 comparisonPath.append(*comparisonFiles[j]);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000626
epoger@google.com5fd53852012-03-22 18:20:06 +0000627 DiffRecord *drp = NULL;
628 int v = strcmp(baseFiles[i]->c_str(),
629 comparisonFiles[j]->c_str());
630
631 if (v < 0) {
632 // in baseDir, but not in comparisonDir
epoger@google.com292aff62012-05-16 14:57:28 +0000633 drp = new DiffRecord(*baseFiles[i], basePath, comparisonPath,
634 kComparisonMissing);
epoger@google.com5fd53852012-03-22 18:20:06 +0000635 ++i;
636 } else if (v > 0) {
637 // in comparisonDir, but not in baseDir
epoger@google.com292aff62012-05-16 14:57:28 +0000638 drp = new DiffRecord(*comparisonFiles[j], basePath, comparisonPath,
639 kBaseMissing);
epoger@google.com5fd53852012-03-22 18:20:06 +0000640 ++j;
641 } else {
642 // let's diff!
643 drp = new DiffRecord(*baseFiles[i], basePath, comparisonPath);
644
645 if (get_bitmaps(drp)) {
646 create_and_write_diff_image(drp, dmp, colorThreshold,
647 outputDir, *baseFiles[i]);
648 }
649
650 ++i;
651 ++j;
652 }
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000653
654 differences->push(drp);
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000655 summary->add(drp);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000656 }
epoger@google.com5fd53852012-03-22 18:20:06 +0000657
658 for (; i < baseFiles.count(); ++i) {
659 // files only in baseDir
660 SkString basePath (baseDir);
661 basePath.append(*baseFiles[i]);
662 SkString comparisonPath;
663 DiffRecord *drp = new DiffRecord(*baseFiles[i], basePath,
epoger@google.com292aff62012-05-16 14:57:28 +0000664 comparisonPath, kComparisonMissing);
epoger@google.com5fd53852012-03-22 18:20:06 +0000665 differences->push(drp);
666 summary->add(drp);
667 }
668
669 for (; j < comparisonFiles.count(); ++j) {
670 // files only in comparisonDir
671 SkString basePath;
672 SkString comparisonPath(comparisonDir);
673 comparisonPath.append(*comparisonFiles[j]);
674 DiffRecord *drp = new DiffRecord(*comparisonFiles[j], basePath,
epoger@google.com292aff62012-05-16 14:57:28 +0000675 comparisonPath, kBaseMissing);
epoger@google.com5fd53852012-03-22 18:20:06 +0000676 differences->push(drp);
677 summary->add(drp);
678 }
679
680 release_file_list(&baseFiles);
681 release_file_list(&comparisonFiles);
tomhudson@google.com7d042802011-07-14 13:15:55 +0000682}
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000683
tomhudson@google.com7d042802011-07-14 13:15:55 +0000684static void create_diff_images_chromium (DiffMetricProc dmp,
685 const int colorThreshold,
686 RecordArray* differences,
687 const SkString& dirname,
688 const SkString& outputDir,
689 DiffSummary* summary) {
epoger@google.coma5f406e2012-05-01 13:26:16 +0000690 SkASSERT(!outputDir.isEmpty());
691
tomhudson@google.com7d042802011-07-14 13:15:55 +0000692 SkOSFile::Iter baseIterator (dirname.c_str());
693 SkString filename;
694 while (baseIterator.next(&filename)) {
695 if (filename.endsWith(".pdf")) {
696 continue;
697 }
698 if (filename.endsWith("-expected.png")) {
699 SkString expectedPath (dirname);
700 expectedPath.append(filename);
701 SkString shortName (chrome_expected_name_to_short(filename));
702 SkString actualPath (chrome_expected_path_to_actual(expectedPath));
703 DiffRecord * drp =
704 new DiffRecord (shortName, expectedPath, actualPath);
705 if (!get_bitmaps(drp)) {
706 continue;
707 }
708 create_and_write_diff_image(drp, dmp, colorThreshold,
709 outputDir, shortName);
710
711 differences->push(drp);
712 summary->add(drp);
713 }
714 }
715}
716
717static void analyze_chromium(DiffMetricProc dmp,
718 const int colorThreshold,
719 RecordArray* differences,
720 const SkString& dirname,
721 const SkString& outputDir,
722 DiffSummary* summary) {
epoger@google.coma5f406e2012-05-01 13:26:16 +0000723 SkASSERT(!outputDir.isEmpty());
tomhudson@google.com7d042802011-07-14 13:15:55 +0000724 create_diff_images_chromium(dmp, colorThreshold, differences,
725 dirname, outputDir, summary);
726 SkOSFile::Iter dirIterator(dirname.c_str());
727 SkString newdirname;
728 while (dirIterator.next(&newdirname, true)) {
729 if (newdirname.startsWith(".")) {
730 continue;
731 }
732 SkString fullname (dirname);
733 fullname.append(newdirname);
bsalomon@google.com1a315fe2011-09-23 14:56:37 +0000734 if (!fullname.endsWith(PATH_DIV_STR)) {
735 fullname.append(PATH_DIV_STR);
tomhudson@google.com7d042802011-07-14 13:15:55 +0000736 }
737 analyze_chromium(dmp, colorThreshold, differences,
738 fullname, outputDir, summary);
739 }
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000740}
741
742/// Make layout more consistent by scaling image to 240 height, 360 width,
743/// or natural size, whichever is smallest.
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000744static int compute_image_height (int height, int width) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000745 int retval = 240;
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000746 if (height < retval) {
747 retval = height;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000748 }
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000749 float scale = (float) retval / height;
750 if (width * scale > 360) {
751 scale = (float) 360 / width;
bsalomon@google.com8e06dab2011-10-07 20:03:39 +0000752 retval = static_cast<int>(height * scale);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000753 }
754 return retval;
755}
756
epoger@google.com25d961c2012-02-02 20:50:36 +0000757static void print_table_header (SkFILEWStream* stream,
758 const int matchCount,
759 const int colorThreshold,
760 const RecordArray& differences,
761 const SkString &baseDir,
epoger@google.coma2b793c2012-05-15 14:58:53 +0000762 const SkString &comparisonDir,
763 bool doOutputDate=false) {
epoger@google.com25d961c2012-02-02 20:50:36 +0000764 stream->writeText("<table>\n");
765 stream->writeText("<tr><th>");
epoger@google.coma2b793c2012-05-15 14:58:53 +0000766 if (doOutputDate) {
767 SkTime::DateTime dt;
768 SkTime::GetDateTime(&dt);
769 stream->writeText("SkDiff run at ");
770 stream->writeDecAsText(dt.fHour);
771 stream->writeText(":");
772 if (dt.fMinute < 10) {
773 stream->writeText("0");
774 }
775 stream->writeDecAsText(dt.fMinute);
776 stream->writeText(":");
777 if (dt.fSecond < 10) {
778 stream->writeText("0");
779 }
780 stream->writeDecAsText(dt.fSecond);
781 stream->writeText("<br>");
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000782 }
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000783 stream->writeDecAsText(matchCount);
784 stream->writeText(" of ");
785 stream->writeDecAsText(differences.count());
786 stream->writeText(" images matched ");
787 if (colorThreshold == 0) {
788 stream->writeText("exactly");
789 } else {
790 stream->writeText("within ");
791 stream->writeDecAsText(colorThreshold);
792 stream->writeText(" color units per component");
793 }
794 stream->writeText(".<br>");
epoger@google.com25d961c2012-02-02 20:50:36 +0000795 stream->writeText("</th>\n<th>");
796 stream->writeText("every different pixel shown in white");
797 stream->writeText("</th>\n<th>");
798 stream->writeText("color difference at each pixel");
799 stream->writeText("</th>\n<th>");
800 stream->writeText(baseDir.c_str());
801 stream->writeText("</th>\n<th>");
802 stream->writeText(comparisonDir.c_str());
803 stream->writeText("</th>\n");
804 stream->writeText("</tr>\n");
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000805}
806
807static void print_pixel_count (SkFILEWStream* stream,
808 const DiffRecord& diff) {
809 stream->writeText("<br>(");
bsalomon@google.com8e06dab2011-10-07 20:03:39 +0000810 stream->writeDecAsText(static_cast<int>(diff.fFractionDifference *
811 diff.fBaseWidth *
812 diff.fBaseHeight));
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000813 stream->writeText(" pixels)");
tomhudson@google.com5b325292011-05-24 19:41:13 +0000814/*
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000815 stream->writeDecAsText(diff.fWeightedFraction *
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000816 diff.fBaseWidth *
817 diff.fBaseHeight);
tomhudson@google.com5b325292011-05-24 19:41:13 +0000818 stream->writeText(" weighted pixels)");
819*/
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000820}
821
822static void print_label_cell (SkFILEWStream* stream,
823 const DiffRecord& diff) {
824 stream->writeText("<td>");
825 stream->writeText(diff.fFilename.c_str());
826 stream->writeText("<br>");
epoger@google.com292aff62012-05-16 14:57:28 +0000827 switch (diff.fResult) {
828 case kBaseMissing:
829 // fall through
830 case kComparisonMissing:
epoger@google.com5fd53852012-03-22 18:20:06 +0000831 stream->writeText("</td>");
832 return;
epoger@google.com292aff62012-05-16 14:57:28 +0000833 case kDifferentSizes:
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +0000834 stream->writeText("Image sizes differ");
835 stream->writeText("</td>");
836 return;
epoger@google.com292aff62012-05-16 14:57:28 +0000837 default:
838 break; // continue in this function
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +0000839 }
epoger@google.com292aff62012-05-16 14:57:28 +0000840
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000841 char metricBuf [20];
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000842 sprintf(metricBuf, "%12.4f%%", 100 * diff.fFractionDifference);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000843 stream->writeText(metricBuf);
844 stream->writeText(" of pixels differ");
tomhudson@google.com5b325292011-05-24 19:41:13 +0000845 stream->writeText("\n (");
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000846 sprintf(metricBuf, "%12.4f%%", 100 * diff.fWeightedFraction);
tomhudson@google.com5b325292011-05-24 19:41:13 +0000847 stream->writeText(metricBuf);
848 stream->writeText(" weighted)");
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000849 // Write the actual number of pixels that differ if it's < 1%
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000850 if (diff.fFractionDifference < 0.01) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000851 print_pixel_count(stream, diff);
852 }
853 stream->writeText("<br>Average color mismatch ");
bsalomon@google.com8e06dab2011-10-07 20:03:39 +0000854 stream->writeDecAsText(static_cast<int>(MAX3(diff.fAverageMismatchR,
855 diff.fAverageMismatchG,
856 diff.fAverageMismatchB)));
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000857 stream->writeText("<br>Max color mismatch ");
858 stream->writeDecAsText(MAX3(diff.fMaxMismatchR,
859 diff.fMaxMismatchG,
860 diff.fMaxMismatchB));
861 stream->writeText("</td>");
862}
863
864static void print_image_cell (SkFILEWStream* stream,
tomhudson@google.com4e305982011-07-13 17:42:46 +0000865 const SkString& path,
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000866 int height) {
867 stream->writeText("<td><a href=\"");
tomhudson@google.com4e305982011-07-13 17:42:46 +0000868 stream->writeText(path.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000869 stream->writeText("\"><img src=\"");
tomhudson@google.com4e305982011-07-13 17:42:46 +0000870 stream->writeText(path.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000871 stream->writeText("\" height=\"");
872 stream->writeDecAsText(height);
873 stream->writeText("px\"></a></td>");
874}
875
epoger@google.com01f78702012-04-12 16:32:04 +0000876static void print_text_cell (SkFILEWStream* stream, const char* text) {
877 stream->writeText("<td align=center>");
878 if (NULL != text) {
879 stream->writeText(text);
880 }
881 stream->writeText("</td>");
882}
883
epoger@google.com5fd53852012-03-22 18:20:06 +0000884static void print_diff_with_missing_file(SkFILEWStream* stream,
885 DiffRecord& diff,
886 const SkString& relativePath) {
887 stream->writeText("<tr>\n");
888 print_label_cell(stream, diff);
889 stream->writeText("<td>N/A</td>");
890 stream->writeText("<td>N/A</td>");
epoger@google.com292aff62012-05-16 14:57:28 +0000891 if (kBaseMissing != diff.fResult) {
epoger@google.com5fd53852012-03-22 18:20:06 +0000892 int h, w;
893 if (!get_bitmap_height_width(diff.fBasePath, &h, &w)) {
894 stream->writeText("<td>N/A</td>");
895 } else {
896 int height = compute_image_height(h, w);
897 if (!diff.fBasePath.startsWith(PATH_DIV_STR)) {
898 diff.fBasePath.prepend(relativePath);
899 }
900 print_image_cell(stream, diff.fBasePath, height);
901 }
902 } else {
903 stream->writeText("<td>N/A</td>");
904 }
epoger@google.com292aff62012-05-16 14:57:28 +0000905 if (kComparisonMissing != diff.fResult) {
epoger@google.com5fd53852012-03-22 18:20:06 +0000906 int h, w;
907 if (!get_bitmap_height_width(diff.fComparisonPath, &h, &w)) {
908 stream->writeText("<td>N/A</td>");
909 } else {
910 int height = compute_image_height(h, w);
911 if (!diff.fComparisonPath.startsWith(PATH_DIV_STR)) {
912 diff.fComparisonPath.prepend(relativePath);
913 }
914 print_image_cell(stream, diff.fComparisonPath, height);
915 }
916 } else {
917 stream->writeText("<td>N/A</td>");
918 }
919 stream->writeText("</tr>\n");
920 stream->flush();
921}
922
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000923static void print_diff_page (const int matchCount,
924 const int colorThreshold,
925 const RecordArray& differences,
926 const SkString& baseDir,
927 const SkString& comparisonDir,
928 const SkString& outputDir) {
929
epoger@google.coma5f406e2012-05-01 13:26:16 +0000930 SkASSERT(!baseDir.isEmpty());
931 SkASSERT(!comparisonDir.isEmpty());
932 SkASSERT(!outputDir.isEmpty());
933
tomhudson@google.com5b325292011-05-24 19:41:13 +0000934 SkString outputPath (outputDir);
935 outputPath.append("index.html");
936 //SkFILEWStream outputStream ("index.html");
937 SkFILEWStream outputStream (outputPath.c_str());
938
tomhudson@google.com4e305982011-07-13 17:42:46 +0000939 // Need to convert paths from relative-to-cwd to relative-to-outputDir
tomhudson@google.com5b325292011-05-24 19:41:13 +0000940 // FIXME this doesn't work if there are '..' inside the outputDir
941 unsigned int ui;
942 SkString relativePath;
943 for (ui = 0; ui < outputDir.size(); ui++) {
bsalomon@google.com1a315fe2011-09-23 14:56:37 +0000944 if (outputDir[ui] == PATH_DIV_CHAR) {
945 relativePath.append(".." PATH_DIV_STR);
tomhudson@google.com5b325292011-05-24 19:41:13 +0000946 }
947 }
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000948
949 outputStream.writeText("<html>\n<body>\n");
epoger@google.com25d961c2012-02-02 20:50:36 +0000950 print_table_header(&outputStream, matchCount, colorThreshold, differences,
951 baseDir, comparisonDir);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000952 int i;
953 for (i = 0; i < differences.count(); i++) {
954 DiffRecord* diff = differences[i];
epoger@google.com5fd53852012-03-22 18:20:06 +0000955
epoger@google.com292aff62012-05-16 14:57:28 +0000956 switch (diff->fResult) {
957 case kEqualPixels:
958 continue;
959 case kBaseMissing:
960 // fall through
961 case kComparisonMissing:
epoger@google.com5fd53852012-03-22 18:20:06 +0000962 print_diff_with_missing_file(&outputStream, *diff, relativePath);
963 continue;
epoger@google.com292aff62012-05-16 14:57:28 +0000964 default:
965 break;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000966 }
epoger@google.com5fd53852012-03-22 18:20:06 +0000967
bsalomon@google.com1a315fe2011-09-23 14:56:37 +0000968 if (!diff->fBasePath.startsWith(PATH_DIV_STR)) {
tomhudson@google.com4e305982011-07-13 17:42:46 +0000969 diff->fBasePath.prepend(relativePath);
970 }
bsalomon@google.com1a315fe2011-09-23 14:56:37 +0000971 if (!diff->fComparisonPath.startsWith(PATH_DIV_STR)) {
tomhudson@google.com4e305982011-07-13 17:42:46 +0000972 diff->fComparisonPath.prepend(relativePath);
973 }
epoger@google.com5fd53852012-03-22 18:20:06 +0000974
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000975 int height = compute_image_height(diff->fBaseHeight, diff->fBaseWidth);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000976 outputStream.writeText("<tr>\n");
977 print_label_cell(&outputStream, *diff);
epoger@google.com292aff62012-05-16 14:57:28 +0000978 switch (diff->fResult) {
979 case kDifferentSizes:
epoger@google.com01f78702012-04-12 16:32:04 +0000980 print_text_cell(&outputStream,
981 "[image size mismatch, so no diff to display]");
982 print_text_cell(&outputStream,
983 "[image size mismatch, so no diff to display]");
epoger@google.com292aff62012-05-16 14:57:28 +0000984 break;
985 default:
epoger@google.com01f78702012-04-12 16:32:04 +0000986 print_image_cell(&outputStream,
987 filename_to_white_filename(diff->fFilename), height);
988 print_image_cell(&outputStream,
989 filename_to_diff_filename(diff->fFilename), height);
990 }
epoger@google.com25d961c2012-02-02 20:50:36 +0000991 print_image_cell(&outputStream, diff->fBasePath, height);
tomhudson@google.com4e305982011-07-13 17:42:46 +0000992 print_image_cell(&outputStream, diff->fComparisonPath, height);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000993 outputStream.writeText("</tr>\n");
994 outputStream.flush();
995 }
996 outputStream.writeText("</table>\n");
997 outputStream.writeText("</body>\n</html>\n");
998 outputStream.flush();
999}
1000
1001static void usage (char * argv0) {
1002 SkDebugf("Skia baseline image diff tool\n");
epoger@google.coma5f406e2012-05-01 13:26:16 +00001003 SkDebugf("\n"
1004"Usage: \n"
1005" %s <baseDir> <comparisonDir> [outputDir] \n"
1006"or \n"
1007" %s --chromium-release|--chromium-debug <baseDir> <outputDir> \n",
1008argv0, argv0);
1009 SkDebugf("\n"
1010"Arguments: \n"
1011" --nodiffs: don't write out image diffs or index.html, just generate \n"
1012" report on stdout \n"
1013" --threshold <n>: only report differences > n (per color channel) [default 0]\n"
1014" --match: compare files whose filenames contain this substring; if \n"
1015" unspecified, compare ALL files. \n"
1016" this flag may be repeated to add more matching substrings. \n"
1017" --nomatch: regardless of --match, DO NOT compare files whose filenames \n"
1018" contain this substring. \n"
1019" this flag may be repeated to add more forbidden substrings. \n"
tomhudson@google.com7d042802011-07-14 13:15:55 +00001020" --sortbymismatch: sort by average color channel mismatch\n");
1021 SkDebugf(
epoger@google.coma5f406e2012-05-01 13:26:16 +00001022" --sortbymaxmismatch: sort by worst color channel mismatch;\n"
1023" break ties with -sortbymismatch\n"
1024" [default sort is by fraction of pixels mismatching]\n");
tomhudson@google.com5b325292011-05-24 19:41:13 +00001025 SkDebugf(
tomhudson@google.com7d042802011-07-14 13:15:55 +00001026" --weighted: sort by # pixels different weighted by color difference\n");
1027 SkDebugf(
1028" --chromium-release: process Webkit LayoutTests results instead of gm\n"
1029" --chromium-debug: process Webkit LayoutTests results instead of gm\n");
1030 SkDebugf(
1031" baseDir: directory to read baseline images from,\n"
1032" or chromium/src directory for --chromium.\n");
tomhudson@google.com9dc527b2011-06-09 15:47:10 +00001033 SkDebugf(
epoger@google.coma5f406e2012-05-01 13:26:16 +00001034" comparisonDir: directory to read comparison images from\n");
1035 SkDebugf(
1036" outputDir: directory to write difference images and index.html to; \n"
1037" defaults to comparisonDir when not running --chromium \n");
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001038}
1039
1040int main (int argc, char ** argv) {
1041 DiffMetricProc diffProc = compute_diff_pmcolor;
reed@google.comc7a67cb2012-05-07 14:52:12 +00001042 int (*sortProc)(const void*, const void*) = SkCastForQSort(compare_diff_metrics);
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001043
1044 // Maximum error tolerated in any one color channel in any one pixel before
1045 // a difference is reported.
1046 int colorThreshold = 0;
1047 SkString baseDir;
1048 SkString comparisonDir;
1049 SkString outputDir;
epoger@google.coma5f406e2012-05-01 13:26:16 +00001050 StringArray matchSubstrings;
1051 StringArray nomatchSubstrings;
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001052
tomhudson@google.com7d042802011-07-14 13:15:55 +00001053 bool analyzeChromium = false;
1054 bool chromiumDebug = false;
1055 bool chromiumRelease = false;
epoger@google.coma5f406e2012-05-01 13:26:16 +00001056 bool generateDiffs = true;
tomhudson@google.com7d042802011-07-14 13:15:55 +00001057
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001058 RecordArray differences;
tomhudson@google.com9dc527b2011-06-09 15:47:10 +00001059 DiffSummary summary;
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001060
epoger@google.coma5f406e2012-05-01 13:26:16 +00001061 int i;
1062 int numUnflaggedArguments = 0;
1063 for (i = 1; i < argc; i++) {
tomhudson@google.com7d042802011-07-14 13:15:55 +00001064 if (!strcmp(argv[i], "--help")) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001065 usage(argv[0]);
1066 return 0;
1067 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001068 if (!strcmp(argv[i], "--nodiffs")) {
1069 generateDiffs = false;
1070 continue;
1071 }
1072 if (!strcmp(argv[i], "--threshold")) {
1073 colorThreshold = atoi(argv[++i]);
1074 continue;
1075 }
1076 if (!strcmp(argv[i], "--match")) {
1077 matchSubstrings.push(new SkString(argv[++i]));
1078 continue;
1079 }
1080 if (!strcmp(argv[i], "--nomatch")) {
1081 nomatchSubstrings.push(new SkString(argv[++i]));
1082 continue;
1083 }
tomhudson@google.com7d042802011-07-14 13:15:55 +00001084 if (!strcmp(argv[i], "--sortbymismatch")) {
reed@google.comc7a67cb2012-05-07 14:52:12 +00001085 sortProc = SkCastForQSort(compare_diff_mean_mismatches);
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001086 continue;
1087 }
tomhudson@google.com7d042802011-07-14 13:15:55 +00001088 if (!strcmp(argv[i], "--sortbymaxmismatch")) {
reed@google.comc7a67cb2012-05-07 14:52:12 +00001089 sortProc = SkCastForQSort(compare_diff_max_mismatches);
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001090 continue;
1091 }
tomhudson@google.com7d042802011-07-14 13:15:55 +00001092 if (!strcmp(argv[i], "--weighted")) {
reed@google.comc7a67cb2012-05-07 14:52:12 +00001093 sortProc = SkCastForQSort(compare_diff_weighted);
tomhudson@google.com5b325292011-05-24 19:41:13 +00001094 continue;
1095 }
tomhudson@google.com7d042802011-07-14 13:15:55 +00001096 if (!strcmp(argv[i], "--chromium-release")) {
1097 analyzeChromium = true;
1098 chromiumRelease = true;
1099 continue;
1100 }
1101 if (!strcmp(argv[i], "--chromium-debug")) {
1102 analyzeChromium = true;
1103 chromiumDebug = true;
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001104 continue;
1105 }
1106 if (argv[i][0] != '-') {
epoger@google.coma5f406e2012-05-01 13:26:16 +00001107 switch (numUnflaggedArguments++) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001108 case 0:
1109 baseDir.set(argv[i]);
1110 continue;
1111 case 1:
1112 comparisonDir.set(argv[i]);
1113 continue;
1114 case 2:
1115 outputDir.set(argv[i]);
1116 continue;
1117 default:
epoger@google.coma5f406e2012-05-01 13:26:16 +00001118 SkDebugf("extra unflagged argument <%s>\n", argv[i]);
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001119 usage(argv[0]);
1120 return 0;
1121 }
1122 }
1123
1124 SkDebugf("Unrecognized argument <%s>\n", argv[i]);
1125 usage(argv[0]);
1126 return 0;
1127 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001128
tomhudson@google.com7d042802011-07-14 13:15:55 +00001129 if (analyzeChromium) {
epoger@google.coma5f406e2012-05-01 13:26:16 +00001130 if (numUnflaggedArguments != 2) {
tomhudson@google.com7d042802011-07-14 13:15:55 +00001131 usage(argv[0]);
1132 return 0;
1133 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001134 outputDir = comparisonDir;
tomhudson@google.com7d042802011-07-14 13:15:55 +00001135 if (chromiumRelease && chromiumDebug) {
1136 SkDebugf(
1137"--chromium must be either -release or -debug, not both!\n");
1138 return 0;
1139 }
1140 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001141
1142 if (numUnflaggedArguments == 2) {
tomhudson@google.com9dc527b2011-06-09 15:47:10 +00001143 outputDir = comparisonDir;
epoger@google.coma5f406e2012-05-01 13:26:16 +00001144 } else if (numUnflaggedArguments != 3) {
tomhudson@google.com9dc527b2011-06-09 15:47:10 +00001145 usage(argv[0]);
1146 return 0;
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001147 }
1148
bsalomon@google.com1a315fe2011-09-23 14:56:37 +00001149 if (!baseDir.endsWith(PATH_DIV_STR)) {
1150 baseDir.append(PATH_DIV_STR);
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001151 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001152 printf("baseDir is [%s]\n", baseDir.c_str());
1153
bsalomon@google.com1a315fe2011-09-23 14:56:37 +00001154 if (!comparisonDir.endsWith(PATH_DIV_STR)) {
1155 comparisonDir.append(PATH_DIV_STR);
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001156 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001157 printf("comparisonDir is [%s]\n", comparisonDir.c_str());
1158
bsalomon@google.com1a315fe2011-09-23 14:56:37 +00001159 if (!outputDir.endsWith(PATH_DIV_STR)) {
1160 outputDir.append(PATH_DIV_STR);
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001161 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001162 if (generateDiffs) {
1163 printf("writing diffs to outputDir is [%s]\n", outputDir.c_str());
1164 } else {
1165 printf("not writing any diffs to outputDir [%s]\n", outputDir.c_str());
1166 outputDir.set("");
1167 }
1168
1169 // Default substring matching:
1170 // - No matter what, don't match any PDF files.
1171 // We may want to change this later, but for now this maintains the filter
1172 // that get_file_list() used to always apply.
1173 // - If no matchSubstrings were specified, match ALL strings.
1174 nomatchSubstrings.push(new SkString(".pdf"));
1175 if (matchSubstrings.isEmpty()) {
1176 matchSubstrings.push(new SkString(""));
1177 }
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001178
tomhudson@google.com7d042802011-07-14 13:15:55 +00001179 if (analyzeChromium) {
bsalomon@google.com1a315fe2011-09-23 14:56:37 +00001180 baseDir.append("webkit" PATH_DIV_STR);
tomhudson@google.com7d042802011-07-14 13:15:55 +00001181 if (chromiumRelease) {
bsalomon@google.com1a315fe2011-09-23 14:56:37 +00001182 baseDir.append("Release" PATH_DIV_STR);
tomhudson@google.com7d042802011-07-14 13:15:55 +00001183 }
1184 if (chromiumDebug) {
bsalomon@google.com1a315fe2011-09-23 14:56:37 +00001185 baseDir.append("Debug" PATH_DIV_STR);
tomhudson@google.com7d042802011-07-14 13:15:55 +00001186 }
bsalomon@google.com1a315fe2011-09-23 14:56:37 +00001187 baseDir.append("layout-test-results" PATH_DIV_STR);
tomhudson@google.com7d042802011-07-14 13:15:55 +00001188 analyze_chromium(diffProc, colorThreshold, &differences,
1189 baseDir, outputDir, &summary);
1190 } else {
1191 create_diff_images(diffProc, colorThreshold, &differences,
epoger@google.coma5f406e2012-05-01 13:26:16 +00001192 baseDir, comparisonDir, outputDir,
1193 matchSubstrings, nomatchSubstrings, &summary);
tomhudson@google.com7d042802011-07-14 13:15:55 +00001194 }
tomhudson@google.com9dc527b2011-06-09 15:47:10 +00001195 summary.print();
tomhudson@google.com7d042802011-07-14 13:15:55 +00001196
1197 if (differences.count()) {
reed@google.comc7a67cb2012-05-07 14:52:12 +00001198 qsort(differences.begin(), differences.count(),
1199 sizeof(DiffRecord*), sortProc);
tomhudson@google.com7d042802011-07-14 13:15:55 +00001200 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001201
1202 if (generateDiffs) {
1203 print_diff_page(summary.fNumMatches, colorThreshold, differences,
1204 baseDir, comparisonDir, outputDir);
1205 }
1206
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001207 for (i = 0; i < differences.count(); i++) {
1208 delete differences[i];
1209 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001210 matchSubstrings.deleteAll();
1211 nomatchSubstrings.deleteAll();
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001212}