blob: f0930beb96b3be9539525d435685e8acf592bcaf [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
tomhudson@google.com4b33d282011-04-27 15:39:30 +000043struct DiffRecord {
tomhudson@google.com4e305982011-07-13 17:42:46 +000044 DiffRecord (const SkString filename,
45 const SkString basePath,
epoger@google.com5fd53852012-03-22 18:20:06 +000046 const SkString comparisonPath,
47 bool baseMissing = false,
48 bool comparisonMissing = false)
tomhudson@google.com4b33d282011-04-27 15:39:30 +000049 : fFilename (filename)
tomhudson@google.com4e305982011-07-13 17:42:46 +000050 , fBasePath (basePath)
51 , fComparisonPath (comparisonPath)
tomhudson@google.com9b540ce2011-08-02 14:10:04 +000052 , fBaseBitmap (new SkBitmap ())
53 , fComparisonBitmap (new SkBitmap ())
54 , fDifferenceBitmap (new SkBitmap ())
epoger@google.com25d961c2012-02-02 20:50:36 +000055 , fWhiteBitmap (new SkBitmap ())
tomhudson@google.com9b540ce2011-08-02 14:10:04 +000056 , fBaseHeight (0)
57 , fBaseWidth (0)
tomhudson@google.com9dc527b2011-06-09 15:47:10 +000058 , fFractionDifference (0)
59 , fWeightedFraction (0)
tomhudson@google.com4b33d282011-04-27 15:39:30 +000060 , fAverageMismatchR (0)
61 , fAverageMismatchG (0)
62 , fAverageMismatchB (0)
63 , fMaxMismatchR (0)
64 , fMaxMismatchG (0)
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +000065 , fMaxMismatchB (0)
epoger@google.com5fd53852012-03-22 18:20:06 +000066 , fDoImageSizesMismatch (false)
67 , fBaseMissing(baseMissing)
68 , fComparisonMissing(comparisonMissing) {
tomhudson@google.com7d042802011-07-14 13:15:55 +000069 // These asserts are valid for GM, but not for --chromium
70 //SkASSERT(basePath.endsWith(filename.c_str()));
71 //SkASSERT(comparisonPath.endsWith(filename.c_str()));
tomhudson@google.com4e305982011-07-13 17:42:46 +000072 };
tomhudson@google.com4b33d282011-04-27 15:39:30 +000073
74 SkString fFilename;
tomhudson@google.com4e305982011-07-13 17:42:46 +000075 SkString fBasePath;
76 SkString fComparisonPath;
tomhudson@google.com4b33d282011-04-27 15:39:30 +000077
tomhudson@google.com9b540ce2011-08-02 14:10:04 +000078 SkBitmap* fBaseBitmap;
79 SkBitmap* fComparisonBitmap;
80 SkBitmap* fDifferenceBitmap;
epoger@google.com25d961c2012-02-02 20:50:36 +000081 SkBitmap* fWhiteBitmap;
tomhudson@google.com9b540ce2011-08-02 14:10:04 +000082
83 int fBaseHeight;
84 int fBaseWidth;
tomhudson@google.com4b33d282011-04-27 15:39:30 +000085
86 /// Arbitrary floating-point metric to be used to sort images from most
87 /// to least different from baseline; values of 0 will be omitted from the
88 /// summary webpage.
tomhudson@google.com9dc527b2011-06-09 15:47:10 +000089 float fFractionDifference;
90 float fWeightedFraction;
tomhudson@google.com4b33d282011-04-27 15:39:30 +000091
92 float fAverageMismatchR;
93 float fAverageMismatchG;
94 float fAverageMismatchB;
95
96 uint32_t fMaxMismatchR;
97 uint32_t fMaxMismatchG;
98 uint32_t fMaxMismatchB;
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +000099
100 /// By the time we need to report image size mismatch, we've already
101 /// released the bitmaps, so we need to remember it when we detect it.
102 bool fDoImageSizesMismatch;
epoger@google.com5fd53852012-03-22 18:20:06 +0000103
104 bool fBaseMissing;
105 bool fComparisonMissing;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000106};
107
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000108#define MAX2(a,b) (((b) < (a)) ? (a) : (b))
109#define MAX3(a,b,c) (((b) < (a)) ? MAX2((a), (c)) : MAX2((b), (c)))
110
epoger@google.com25d961c2012-02-02 20:50:36 +0000111const SkPMColor PMCOLOR_WHITE = SkPreMultiplyColor(SK_ColorWHITE);
112const SkPMColor PMCOLOR_BLACK = SkPreMultiplyColor(SK_ColorBLACK);
113
epoger@google.coma5f406e2012-05-01 13:26:16 +0000114typedef SkTDArray<SkString*> StringArray;
115typedef StringArray FileArray;
epoger@google.com5fd53852012-03-22 18:20:06 +0000116
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000117struct DiffSummary {
118 DiffSummary ()
119 : fNumMatches (0)
120 , fNumMismatches (0)
121 , fMaxMismatchV (0)
122 , fMaxMismatchPercent (0) { };
123
epoger@google.com5fd53852012-03-22 18:20:06 +0000124 ~DiffSummary() {
125 fBaseMissing.deleteAll();
126 fComparisonMissing.deleteAll();
127 }
128
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000129 uint32_t fNumMatches;
130 uint32_t fNumMismatches;
131 uint32_t fMaxMismatchV;
132 float fMaxMismatchPercent;
133
epoger@google.com5fd53852012-03-22 18:20:06 +0000134 FileArray fBaseMissing;
135 FileArray fComparisonMissing;
136
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000137 void print () {
epoger@google.com5fd53852012-03-22 18:20:06 +0000138 int n = fBaseMissing.count();
139 if (n > 0) {
140 printf("Missing in baseDir:\n");
141 for (int i = 0; i < n; ++i) {
142 printf("\t%s\n", fBaseMissing[i]->c_str());
143 }
144 }
145 n = fComparisonMissing.count();
146 if (n > 0) {
147 printf("Missing in comparisonDir:\n");
148 for (int i = 0; i < n; ++i) {
149 printf("\t%s\n", fComparisonMissing[i]->c_str());
150 }
151 }
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000152 printf("%d of %d images matched.\n", fNumMatches,
153 fNumMatches + fNumMismatches);
154 if (fNumMismatches > 0) {
155 printf("Maximum pixel intensity mismatch %d\n", fMaxMismatchV);
156 printf("Largest area mismatch was %.2f%% of pixels\n",
157 fMaxMismatchPercent);
158 }
159
160 }
161
162 void add (DiffRecord* drp) {
epoger@google.com5fd53852012-03-22 18:20:06 +0000163 if (drp->fBaseMissing) {
164 fBaseMissing.push(new SkString(drp->fFilename));
165 fNumMismatches++;
166 } else if (drp->fComparisonMissing) {
167 fComparisonMissing.push(new SkString(drp->fFilename));
168 fNumMismatches++;
169 } else if (0 == drp->fFractionDifference) {
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000170 fNumMatches++;
171 } else {
172 fNumMismatches++;
173 if (drp->fFractionDifference * 100 > fMaxMismatchPercent) {
174 fMaxMismatchPercent = drp->fFractionDifference * 100;
175 }
tomhudson@google.com88a0e052011-06-09 18:54:01 +0000176 uint32_t value = MAX3(drp->fMaxMismatchR, drp->fMaxMismatchG,
177 drp->fMaxMismatchB);
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000178 if (value > fMaxMismatchV) {
179 fMaxMismatchV = value;
180 }
181 }
182 }
183};
184
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000185typedef SkTDArray<DiffRecord*> RecordArray;
186
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000187/// Comparison routine for qsort; sorts by fFractionDifference
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000188/// from largest to smallest.
189static int compare_diff_metrics (DiffRecord** lhs, DiffRecord** rhs) {
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000190 if ((*lhs)->fFractionDifference < (*rhs)->fFractionDifference) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000191 return 1;
192 }
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000193 if ((*rhs)->fFractionDifference < (*lhs)->fFractionDifference) {
tomhudson@google.com5b325292011-05-24 19:41:13 +0000194 return -1;
195 }
196 return 0;
197}
198
199static int compare_diff_weighted (DiffRecord** lhs, DiffRecord** rhs) {
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000200 if ((*lhs)->fWeightedFraction < (*rhs)->fWeightedFraction) {
tomhudson@google.com5b325292011-05-24 19:41:13 +0000201 return 1;
202 }
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000203 if ((*lhs)->fWeightedFraction > (*rhs)->fWeightedFraction) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000204 return -1;
205 }
206 return 0;
207}
208
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000209/// Comparison routine for qsort; sorts by max(fAverageMismatch{RGB})
210/// from largest to smallest.
211static int compare_diff_mean_mismatches (DiffRecord** lhs, DiffRecord** rhs) {
212 float leftValue = MAX3((*lhs)->fAverageMismatchR,
213 (*lhs)->fAverageMismatchG,
214 (*lhs)->fAverageMismatchB);
215 float rightValue = MAX3((*rhs)->fAverageMismatchR,
216 (*rhs)->fAverageMismatchG,
217 (*rhs)->fAverageMismatchB);
218 if (leftValue < rightValue) {
219 return 1;
220 }
221 if (rightValue < leftValue) {
222 return -1;
223 }
224 return 0;
225}
226
227/// Comparison routine for qsort; sorts by max(fMaxMismatch{RGB})
228/// from largest to smallest.
229static int compare_diff_max_mismatches (DiffRecord** lhs, DiffRecord** rhs) {
bsalomon@google.com8e06dab2011-10-07 20:03:39 +0000230 uint32_t leftValue = MAX3((*lhs)->fMaxMismatchR,
231 (*lhs)->fMaxMismatchG,
232 (*lhs)->fMaxMismatchB);
233 uint32_t rightValue = MAX3((*rhs)->fMaxMismatchR,
234 (*rhs)->fMaxMismatchG,
235 (*rhs)->fMaxMismatchB);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000236 if (leftValue < rightValue) {
237 return 1;
238 }
239 if (rightValue < leftValue) {
240 return -1;
241 }
242 return compare_diff_mean_mismatches(lhs, rhs);
243}
244
245
246
247/// Parameterized routine to compute the color of a pixel in a difference image.
248typedef SkPMColor (*DiffMetricProc)(SkPMColor, SkPMColor);
249
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +0000250static void expand_and_copy (int width, int height, SkBitmap** dest) {
251 SkBitmap* temp = new SkBitmap ();
252 temp->reset();
253 temp->setConfig((*dest)->config(), width, height);
254 temp->allocPixels();
255 (*dest)->copyPixelsTo(temp->getPixels(), temp->getSize(),
256 temp->rowBytes());
257 *dest = temp;
258}
259
tomhudson@google.com4e305982011-07-13 17:42:46 +0000260static bool get_bitmaps (DiffRecord* diffRecord) {
261 SkFILEStream compareStream(diffRecord->fComparisonPath.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000262 if (!compareStream.isValid()) {
263 SkDebugf("WARNING: couldn't open comparison file <%s>\n",
tomhudson@google.com4e305982011-07-13 17:42:46 +0000264 diffRecord->fComparisonPath.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000265 return false;
266 }
267
tomhudson@google.com4e305982011-07-13 17:42:46 +0000268 SkFILEStream baseStream(diffRecord->fBasePath.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000269 if (!baseStream.isValid()) {
270 SkDebugf("ERROR: couldn't open base file <%s>\n",
tomhudson@google.com4e305982011-07-13 17:42:46 +0000271 diffRecord->fBasePath.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000272 return false;
273 }
274
275 SkImageDecoder* codec = SkImageDecoder::Factory(&baseStream);
276 if (NULL == codec) {
277 SkDebugf("ERROR: no codec found for <%s>\n",
tomhudson@google.com4e305982011-07-13 17:42:46 +0000278 diffRecord->fBasePath.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000279 return false;
280 }
281
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +0000282 // In debug, the DLL will automatically be unloaded when this is deleted,
283 // but that shouldn't be a problem in release mode.
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000284 SkAutoTDelete<SkImageDecoder> ad(codec);
285
286 baseStream.rewind();
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000287 if (!codec->decode(&baseStream, diffRecord->fBaseBitmap,
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000288 SkBitmap::kARGB_8888_Config,
289 SkImageDecoder::kDecodePixels_Mode)) {
290 SkDebugf("ERROR: codec failed for <%s>\n",
tomhudson@google.com4e305982011-07-13 17:42:46 +0000291 diffRecord->fBasePath.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000292 return false;
293 }
294
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000295 diffRecord->fBaseWidth = diffRecord->fBaseBitmap->width();
296 diffRecord->fBaseHeight = diffRecord->fBaseBitmap->height();
297
298 if (!codec->decode(&compareStream, diffRecord->fComparisonBitmap,
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000299 SkBitmap::kARGB_8888_Config,
300 SkImageDecoder::kDecodePixels_Mode)) {
301 SkDebugf("ERROR: codec failed for <%s>\n",
tomhudson@google.com4e305982011-07-13 17:42:46 +0000302 diffRecord->fComparisonPath.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000303 return false;
304 }
305
306 return true;
307}
308
epoger@google.com5fd53852012-03-22 18:20:06 +0000309static bool get_bitmap_height_width(const SkString& path,
310 int *height, int *width) {
311 SkFILEStream stream(path.c_str());
312 if (!stream.isValid()) {
313 SkDebugf("ERROR: couldn't open file <%s>\n",
314 path.c_str());
315 return false;
316 }
317
318 SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
319 if (NULL == codec) {
320 SkDebugf("ERROR: no codec found for <%s>\n",
321 path.c_str());
322 return false;
323 }
324
325 SkAutoTDelete<SkImageDecoder> ad(codec);
326 SkBitmap bm;
327
328 stream.rewind();
329 if (!codec->decode(&stream, &bm,
330 SkBitmap::kARGB_8888_Config,
331 SkImageDecoder::kDecodePixels_Mode)) {
332 SkDebugf("ERROR: codec failed for <%s>\n",
333 path.c_str());
334 return false;
335 }
336
337 *height = bm.height();
338 *width = bm.width();
339
340 return true;
341}
342
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000343// from gm - thanks to PNG, we need to force all pixels 100% opaque
344static void force_all_opaque(const SkBitmap& bitmap) {
345 SkAutoLockPixels lock(bitmap);
346 for (int y = 0; y < bitmap.height(); y++) {
347 for (int x = 0; x < bitmap.width(); x++) {
348 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
349 }
350 }
351}
352
353// from gm
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000354static bool write_bitmap(const SkString& path, const SkBitmap* bitmap) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000355 SkBitmap copy;
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000356 bitmap->copyTo(&copy, SkBitmap::kARGB_8888_Config);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000357 force_all_opaque(copy);
358 return SkImageEncoder::EncodeFile(path.c_str(), copy,
359 SkImageEncoder::kPNG_Type, 100);
360}
361
362// from gm
363static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
364 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
365 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
366 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
367
368 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
369}
370
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000371static inline bool colors_match_thresholded(SkPMColor c0, SkPMColor c1,
372 const int threshold) {
373 int da = SkGetPackedA32(c0) - SkGetPackedA32(c1);
374 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
375 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
376 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
377
378 return ((SkAbs32(da) <= threshold) &&
379 (SkAbs32(dr) <= threshold) &&
380 (SkAbs32(dg) <= threshold) &&
381 (SkAbs32(db) <= threshold));
382}
383
384// based on gm
385static void compute_diff(DiffRecord* dr,
386 DiffMetricProc diffFunction,
387 const int colorThreshold) {
epoger@google.com25d961c2012-02-02 20:50:36 +0000388 SkAutoLockPixels alpDiff(*dr->fDifferenceBitmap);
389 SkAutoLockPixels alpWhite(*dr->fWhiteBitmap);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000390
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000391 const int w = dr->fComparisonBitmap->width();
392 const int h = dr->fComparisonBitmap->height();
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000393 int mismatchedPixels = 0;
394 int totalMismatchR = 0;
395 int totalMismatchG = 0;
396 int totalMismatchB = 0;
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +0000397
398 if (w != dr->fBaseWidth || h != dr->fBaseHeight) {
399 dr->fDoImageSizesMismatch = true;
400 dr->fFractionDifference = 1;
401 return;
402 }
tomhudson@google.com5b325292011-05-24 19:41:13 +0000403 // Accumulate fractionally different pixels, then divide out
404 // # of pixels at the end.
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000405 dr->fWeightedFraction = 0;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000406 for (int y = 0; y < h; y++) {
407 for (int x = 0; x < w; x++) {
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000408 SkPMColor c0 = *dr->fBaseBitmap->getAddr32(x, y);
409 SkPMColor c1 = *dr->fComparisonBitmap->getAddr32(x, y);
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000410 SkPMColor trueDifference = compute_diff_pmcolor(c0, c1);
411 SkPMColor outputDifference = diffFunction(c0, c1);
412 uint32_t thisR = SkGetPackedR32(trueDifference);
413 uint32_t thisG = SkGetPackedG32(trueDifference);
414 uint32_t thisB = SkGetPackedB32(trueDifference);
tomhudson@google.com5b325292011-05-24 19:41:13 +0000415 totalMismatchR += thisR;
416 totalMismatchG += thisG;
417 totalMismatchB += thisB;
418 // In HSV, value is defined as max RGB component.
419 int value = MAX3(thisR, thisG, thisB);
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000420 dr->fWeightedFraction += ((float) value) / 255;
tomhudson@google.com5b325292011-05-24 19:41:13 +0000421 if (thisR > dr->fMaxMismatchR) {
422 dr->fMaxMismatchR = thisR;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000423 }
tomhudson@google.com5b325292011-05-24 19:41:13 +0000424 if (thisG > dr->fMaxMismatchG) {
425 dr->fMaxMismatchG = thisG;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000426 }
tomhudson@google.com5b325292011-05-24 19:41:13 +0000427 if (thisB > dr->fMaxMismatchB) {
428 dr->fMaxMismatchB = thisB;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000429 }
430 if (!colors_match_thresholded(c0, c1, colorThreshold)) {
431 mismatchedPixels++;
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000432 *dr->fDifferenceBitmap->getAddr32(x, y) = outputDifference;
epoger@google.com25d961c2012-02-02 20:50:36 +0000433 *dr->fWhiteBitmap->getAddr32(x, y) = PMCOLOR_WHITE;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000434 } else {
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000435 *dr->fDifferenceBitmap->getAddr32(x, y) = 0;
epoger@google.com25d961c2012-02-02 20:50:36 +0000436 *dr->fWhiteBitmap->getAddr32(x, y) = PMCOLOR_BLACK;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000437 }
438 }
439 }
tomhudson@google.com5b325292011-05-24 19:41:13 +0000440 int pixelCount = w * h;
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000441 dr->fFractionDifference = ((float) mismatchedPixels) / pixelCount;
442 dr->fWeightedFraction /= pixelCount;
tomhudson@google.com5b325292011-05-24 19:41:13 +0000443 dr->fAverageMismatchR = ((float) totalMismatchR) / pixelCount;
444 dr->fAverageMismatchG = ((float) totalMismatchG) / pixelCount;
445 dr->fAverageMismatchB = ((float) totalMismatchB) / pixelCount;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000446}
447
epoger@google.com25d961c2012-02-02 20:50:36 +0000448static SkString filename_to_derived_filename (const SkString& filename,
449 const char *suffix) {
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000450 SkString diffName (filename);
451 const char* cstring = diffName.c_str();
452 int dotOffset = strrchr(cstring, '.') - cstring;
453 diffName.remove(dotOffset, diffName.size() - dotOffset);
epoger@google.com25d961c2012-02-02 20:50:36 +0000454 diffName.append(suffix);
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000455 return diffName;
456}
457
epoger@google.com25d961c2012-02-02 20:50:36 +0000458/// Given a image filename, returns the name of the file containing the
459/// associated difference image.
460static SkString filename_to_diff_filename (const SkString& filename) {
461 return filename_to_derived_filename(filename, "-diff.png");
462}
463
464/// Given a image filename, returns the name of the file containing the
465/// "white" difference image.
466static SkString filename_to_white_filename (const SkString& filename) {
467 return filename_to_derived_filename(filename, "-white.png");
468}
469
tomhudson@google.com7d042802011-07-14 13:15:55 +0000470/// Convert a chromium/WebKit LayoutTest "foo-expected.png" to "foo-actual.png"
471static SkString chrome_expected_path_to_actual (const SkString& expected) {
472 SkString actualPath (expected);
473 actualPath.remove(actualPath.size() - 13, 13);
474 actualPath.append("-actual.png");
475 return actualPath;
476}
477
478/// Convert a chromium/WebKit LayoutTest "foo-expected.png" to "foo.png"
479static SkString chrome_expected_name_to_short (const SkString& expected) {
480 SkString shortName (expected);
481 shortName.remove(shortName.size() - 13, 13);
482 shortName.append(".png");
483 return shortName;
484}
485
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000486static void release_bitmaps(DiffRecord* drp) {
487 delete drp->fBaseBitmap;
488 drp->fBaseBitmap = NULL;
489 delete drp->fComparisonBitmap;
490 drp->fComparisonBitmap = NULL;
491 delete drp->fDifferenceBitmap;
492 drp->fDifferenceBitmap = NULL;
epoger@google.com25d961c2012-02-02 20:50:36 +0000493 delete drp->fWhiteBitmap;
494 drp->fWhiteBitmap = NULL;
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000495}
496
tomhudson@google.com7d042802011-07-14 13:15:55 +0000497
epoger@google.coma5f406e2012-05-01 13:26:16 +0000498/// If outputDir.isEmpty(), don't write out diff files.
tomhudson@google.com7d042802011-07-14 13:15:55 +0000499static void create_and_write_diff_image(DiffRecord* drp,
500 DiffMetricProc dmp,
501 const int colorThreshold,
502 const SkString& outputDir,
503 const SkString& filename) {
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000504 const int w = drp->fBaseWidth;
505 const int h = drp->fBaseHeight;
506 drp->fDifferenceBitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h);
507 drp->fDifferenceBitmap->allocPixels();
epoger@google.com25d961c2012-02-02 20:50:36 +0000508 drp->fWhiteBitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h);
509 drp->fWhiteBitmap->allocPixels();
tomhudson@google.com4e305982011-07-13 17:42:46 +0000510 compute_diff(drp, dmp, colorThreshold);
tomhudson@google.com7d042802011-07-14 13:15:55 +0000511
epoger@google.coma5f406e2012-05-01 13:26:16 +0000512 if (!outputDir.isEmpty()) {
513 SkString differencePath (outputDir);
514 differencePath.append(filename_to_diff_filename(filename));
515 write_bitmap(differencePath, drp->fDifferenceBitmap);
516 SkString whitePath (outputDir);
517 whitePath.append(filename_to_white_filename(filename));
518 write_bitmap(whitePath, drp->fWhiteBitmap);
519 }
520
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000521 release_bitmaps(drp);
tomhudson@google.com4e305982011-07-13 17:42:46 +0000522}
523
epoger@google.coma5f406e2012-05-01 13:26:16 +0000524/// Returns true if string contains any of these substrings.
525static bool string_contains_any_of(const SkString& string,
526 const StringArray& substrings) {
527 for (int i = 0; i < substrings.count(); i++) {
528 if (string.contains(substrings[i]->c_str())) {
529 return true;
530 }
531 }
532 return false;
533}
534
535/// Iterate over dir and get all files that:
536/// - match any of the substrings in matchSubstrings, but...
537/// - DO NOT match any of the substrings in nomatchSubstrings
538/// Returns the list of files in *files.
539static void get_file_list(const SkString& dir,
540 const StringArray& matchSubstrings,
541 const StringArray& nomatchSubstrings,
542 FileArray *files) {
epoger@google.com5fd53852012-03-22 18:20:06 +0000543 SkOSFile::Iter it(dir.c_str());
544 SkString filename;
545 while (it.next(&filename)) {
epoger@google.coma5f406e2012-05-01 13:26:16 +0000546 if (string_contains_any_of(filename, matchSubstrings) &&
547 !string_contains_any_of(filename, nomatchSubstrings)) {
548 files->push(new SkString(filename));
epoger@google.com5fd53852012-03-22 18:20:06 +0000549 }
epoger@google.com5fd53852012-03-22 18:20:06 +0000550 }
551}
552
553static void release_file_list(FileArray *files) {
554 files->deleteAll();
555}
556
557/// Comparison routines for qsort, sort by file names.
558static int compare_file_name_metrics(SkString **lhs, SkString **rhs) {
559 return strcmp((*lhs)->c_str(), (*rhs)->c_str());
560}
561
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000562/// Creates difference images, returns the number that have a 0 metric.
epoger@google.coma5f406e2012-05-01 13:26:16 +0000563/// If outputDir.isEmpty(), don't write out diff files.
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000564static void create_diff_images (DiffMetricProc dmp,
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000565 const int colorThreshold,
566 RecordArray* differences,
567 const SkString& baseDir,
568 const SkString& comparisonDir,
569 const SkString& outputDir,
epoger@google.coma5f406e2012-05-01 13:26:16 +0000570 const StringArray& matchSubstrings,
571 const StringArray& nomatchSubstrings,
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000572 DiffSummary* summary) {
epoger@google.coma5f406e2012-05-01 13:26:16 +0000573 SkASSERT(!baseDir.isEmpty());
574 SkASSERT(!comparisonDir.isEmpty());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000575
epoger@google.com5fd53852012-03-22 18:20:06 +0000576 FileArray baseFiles;
577 FileArray comparisonFiles;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000578
epoger@google.coma5f406e2012-05-01 13:26:16 +0000579 get_file_list(baseDir, matchSubstrings, nomatchSubstrings, &baseFiles);
580 get_file_list(comparisonDir, matchSubstrings, nomatchSubstrings,
581 &comparisonFiles);
epoger@google.com5fd53852012-03-22 18:20:06 +0000582
epoger@google.coma5f406e2012-05-01 13:26:16 +0000583 if (!baseFiles.isEmpty()) {
reed@google.comc7a67cb2012-05-07 14:52:12 +0000584 qsort(baseFiles.begin(), baseFiles.count(), sizeof(SkString*),
585 SkCastForQSort(compare_file_name_metrics));
epoger@google.coma5f406e2012-05-01 13:26:16 +0000586 }
587 if (!comparisonFiles.isEmpty()) {
reed@google.comc7a67cb2012-05-07 14:52:12 +0000588 qsort(comparisonFiles.begin(), comparisonFiles.count(),
589 sizeof(SkString*), SkCastForQSort(compare_file_name_metrics));
epoger@google.coma5f406e2012-05-01 13:26:16 +0000590 }
591
epoger@google.com5fd53852012-03-22 18:20:06 +0000592 int i = 0;
593 int j = 0;
594
595 while (i < baseFiles.count() &&
596 j < comparisonFiles.count()) {
597
tomhudson@google.com4e305982011-07-13 17:42:46 +0000598 SkString basePath (baseDir);
epoger@google.com5fd53852012-03-22 18:20:06 +0000599 basePath.append(*baseFiles[i]);
tomhudson@google.com7d042802011-07-14 13:15:55 +0000600 SkString comparisonPath (comparisonDir);
epoger@google.com5fd53852012-03-22 18:20:06 +0000601 comparisonPath.append(*comparisonFiles[j]);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000602
epoger@google.com5fd53852012-03-22 18:20:06 +0000603 DiffRecord *drp = NULL;
604 int v = strcmp(baseFiles[i]->c_str(),
605 comparisonFiles[j]->c_str());
606
607 if (v < 0) {
608 // in baseDir, but not in comparisonDir
609 drp = new DiffRecord(*baseFiles[i],
610 basePath, comparisonPath, false, true);
611 ++i;
612 } else if (v > 0) {
613 // in comparisonDir, but not in baseDir
614 drp = new DiffRecord(*comparisonFiles[j],
615 basePath, comparisonPath, true, false);
616 ++j;
617 } else {
618 // let's diff!
619 drp = new DiffRecord(*baseFiles[i], basePath, comparisonPath);
620
621 if (get_bitmaps(drp)) {
622 create_and_write_diff_image(drp, dmp, colorThreshold,
623 outputDir, *baseFiles[i]);
624 }
625
626 ++i;
627 ++j;
628 }
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000629
630 differences->push(drp);
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000631 summary->add(drp);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000632 }
epoger@google.com5fd53852012-03-22 18:20:06 +0000633
634 for (; i < baseFiles.count(); ++i) {
635 // files only in baseDir
636 SkString basePath (baseDir);
637 basePath.append(*baseFiles[i]);
638 SkString comparisonPath;
639 DiffRecord *drp = new DiffRecord(*baseFiles[i], basePath,
640 comparisonPath, false, true);
641 differences->push(drp);
642 summary->add(drp);
643 }
644
645 for (; j < comparisonFiles.count(); ++j) {
646 // files only in comparisonDir
647 SkString basePath;
648 SkString comparisonPath(comparisonDir);
649 comparisonPath.append(*comparisonFiles[j]);
650 DiffRecord *drp = new DiffRecord(*comparisonFiles[j], basePath,
651 comparisonPath, true, false);
652 differences->push(drp);
653 summary->add(drp);
654 }
655
656 release_file_list(&baseFiles);
657 release_file_list(&comparisonFiles);
tomhudson@google.com7d042802011-07-14 13:15:55 +0000658}
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000659
tomhudson@google.com7d042802011-07-14 13:15:55 +0000660static void create_diff_images_chromium (DiffMetricProc dmp,
661 const int colorThreshold,
662 RecordArray* differences,
663 const SkString& dirname,
664 const SkString& outputDir,
665 DiffSummary* summary) {
epoger@google.coma5f406e2012-05-01 13:26:16 +0000666 SkASSERT(!outputDir.isEmpty());
667
tomhudson@google.com7d042802011-07-14 13:15:55 +0000668 SkOSFile::Iter baseIterator (dirname.c_str());
669 SkString filename;
670 while (baseIterator.next(&filename)) {
671 if (filename.endsWith(".pdf")) {
672 continue;
673 }
674 if (filename.endsWith("-expected.png")) {
675 SkString expectedPath (dirname);
676 expectedPath.append(filename);
677 SkString shortName (chrome_expected_name_to_short(filename));
678 SkString actualPath (chrome_expected_path_to_actual(expectedPath));
679 DiffRecord * drp =
680 new DiffRecord (shortName, expectedPath, actualPath);
681 if (!get_bitmaps(drp)) {
682 continue;
683 }
684 create_and_write_diff_image(drp, dmp, colorThreshold,
685 outputDir, shortName);
686
687 differences->push(drp);
688 summary->add(drp);
689 }
690 }
691}
692
693static void analyze_chromium(DiffMetricProc dmp,
694 const int colorThreshold,
695 RecordArray* differences,
696 const SkString& dirname,
697 const SkString& outputDir,
698 DiffSummary* summary) {
epoger@google.coma5f406e2012-05-01 13:26:16 +0000699 SkASSERT(!outputDir.isEmpty());
tomhudson@google.com7d042802011-07-14 13:15:55 +0000700 create_diff_images_chromium(dmp, colorThreshold, differences,
701 dirname, outputDir, summary);
702 SkOSFile::Iter dirIterator(dirname.c_str());
703 SkString newdirname;
704 while (dirIterator.next(&newdirname, true)) {
705 if (newdirname.startsWith(".")) {
706 continue;
707 }
708 SkString fullname (dirname);
709 fullname.append(newdirname);
bsalomon@google.com1a315fe2011-09-23 14:56:37 +0000710 if (!fullname.endsWith(PATH_DIV_STR)) {
711 fullname.append(PATH_DIV_STR);
tomhudson@google.com7d042802011-07-14 13:15:55 +0000712 }
713 analyze_chromium(dmp, colorThreshold, differences,
714 fullname, outputDir, summary);
715 }
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000716}
717
718/// Make layout more consistent by scaling image to 240 height, 360 width,
719/// or natural size, whichever is smallest.
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000720static int compute_image_height (int height, int width) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000721 int retval = 240;
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000722 if (height < retval) {
723 retval = height;
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000724 }
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000725 float scale = (float) retval / height;
726 if (width * scale > 360) {
727 scale = (float) 360 / width;
bsalomon@google.com8e06dab2011-10-07 20:03:39 +0000728 retval = static_cast<int>(height * scale);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000729 }
730 return retval;
731}
732
epoger@google.com25d961c2012-02-02 20:50:36 +0000733static void print_table_header (SkFILEWStream* stream,
734 const int matchCount,
735 const int colorThreshold,
736 const RecordArray& differences,
737 const SkString &baseDir,
738 const SkString &comparisonDir) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000739 SkTime::DateTime dt;
740 SkTime::GetDateTime(&dt);
epoger@google.com25d961c2012-02-02 20:50:36 +0000741 stream->writeText("<table>\n");
742 stream->writeText("<tr><th>");
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000743 stream->writeText("SkDiff run at ");
744 stream->writeDecAsText(dt.fHour);
745 stream->writeText(":");
746 if (dt.fMinute < 10) {
747 stream->writeText("0");
748 }
749 stream->writeDecAsText(dt.fMinute);
750 stream->writeText(":");
751 if (dt.fSecond < 10) {
752 stream->writeText("0");
753 }
754 stream->writeDecAsText(dt.fSecond);
755 stream->writeText("<br>");
756 stream->writeDecAsText(matchCount);
757 stream->writeText(" of ");
758 stream->writeDecAsText(differences.count());
759 stream->writeText(" images matched ");
760 if (colorThreshold == 0) {
761 stream->writeText("exactly");
762 } else {
763 stream->writeText("within ");
764 stream->writeDecAsText(colorThreshold);
765 stream->writeText(" color units per component");
766 }
767 stream->writeText(".<br>");
epoger@google.com25d961c2012-02-02 20:50:36 +0000768 stream->writeText("</th>\n<th>");
769 stream->writeText("every different pixel shown in white");
770 stream->writeText("</th>\n<th>");
771 stream->writeText("color difference at each pixel");
772 stream->writeText("</th>\n<th>");
773 stream->writeText(baseDir.c_str());
774 stream->writeText("</th>\n<th>");
775 stream->writeText(comparisonDir.c_str());
776 stream->writeText("</th>\n");
777 stream->writeText("</tr>\n");
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000778}
779
780static void print_pixel_count (SkFILEWStream* stream,
781 const DiffRecord& diff) {
782 stream->writeText("<br>(");
bsalomon@google.com8e06dab2011-10-07 20:03:39 +0000783 stream->writeDecAsText(static_cast<int>(diff.fFractionDifference *
784 diff.fBaseWidth *
785 diff.fBaseHeight));
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000786 stream->writeText(" pixels)");
tomhudson@google.com5b325292011-05-24 19:41:13 +0000787/*
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000788 stream->writeDecAsText(diff.fWeightedFraction *
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000789 diff.fBaseWidth *
790 diff.fBaseHeight);
tomhudson@google.com5b325292011-05-24 19:41:13 +0000791 stream->writeText(" weighted pixels)");
792*/
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000793}
794
795static void print_label_cell (SkFILEWStream* stream,
796 const DiffRecord& diff) {
797 stream->writeText("<td>");
798 stream->writeText(diff.fFilename.c_str());
799 stream->writeText("<br>");
epoger@google.com5fd53852012-03-22 18:20:06 +0000800 if (diff.fBaseMissing || diff.fComparisonMissing) {
801 stream->writeText("</td>");
802 return;
803 }
tomhudson@google.com8b08c3e2011-11-30 17:01:00 +0000804 if (diff.fDoImageSizesMismatch) {
805 stream->writeText("Image sizes differ");
806 stream->writeText("</td>");
807 return;
808 }
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000809 char metricBuf [20];
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000810 sprintf(metricBuf, "%12.4f%%", 100 * diff.fFractionDifference);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000811 stream->writeText(metricBuf);
812 stream->writeText(" of pixels differ");
tomhudson@google.com5b325292011-05-24 19:41:13 +0000813 stream->writeText("\n (");
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000814 sprintf(metricBuf, "%12.4f%%", 100 * diff.fWeightedFraction);
tomhudson@google.com5b325292011-05-24 19:41:13 +0000815 stream->writeText(metricBuf);
816 stream->writeText(" weighted)");
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000817 // Write the actual number of pixels that differ if it's < 1%
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000818 if (diff.fFractionDifference < 0.01) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000819 print_pixel_count(stream, diff);
820 }
821 stream->writeText("<br>Average color mismatch ");
bsalomon@google.com8e06dab2011-10-07 20:03:39 +0000822 stream->writeDecAsText(static_cast<int>(MAX3(diff.fAverageMismatchR,
823 diff.fAverageMismatchG,
824 diff.fAverageMismatchB)));
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000825 stream->writeText("<br>Max color mismatch ");
826 stream->writeDecAsText(MAX3(diff.fMaxMismatchR,
827 diff.fMaxMismatchG,
828 diff.fMaxMismatchB));
829 stream->writeText("</td>");
830}
831
832static void print_image_cell (SkFILEWStream* stream,
tomhudson@google.com4e305982011-07-13 17:42:46 +0000833 const SkString& path,
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000834 int height) {
835 stream->writeText("<td><a href=\"");
tomhudson@google.com4e305982011-07-13 17:42:46 +0000836 stream->writeText(path.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000837 stream->writeText("\"><img src=\"");
tomhudson@google.com4e305982011-07-13 17:42:46 +0000838 stream->writeText(path.c_str());
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000839 stream->writeText("\" height=\"");
840 stream->writeDecAsText(height);
841 stream->writeText("px\"></a></td>");
842}
843
epoger@google.com01f78702012-04-12 16:32:04 +0000844static void print_text_cell (SkFILEWStream* stream, const char* text) {
845 stream->writeText("<td align=center>");
846 if (NULL != text) {
847 stream->writeText(text);
848 }
849 stream->writeText("</td>");
850}
851
epoger@google.com5fd53852012-03-22 18:20:06 +0000852static void print_diff_with_missing_file(SkFILEWStream* stream,
853 DiffRecord& diff,
854 const SkString& relativePath) {
855 stream->writeText("<tr>\n");
856 print_label_cell(stream, diff);
857 stream->writeText("<td>N/A</td>");
858 stream->writeText("<td>N/A</td>");
859 if (!diff.fBaseMissing) {
860 int h, w;
861 if (!get_bitmap_height_width(diff.fBasePath, &h, &w)) {
862 stream->writeText("<td>N/A</td>");
863 } else {
864 int height = compute_image_height(h, w);
865 if (!diff.fBasePath.startsWith(PATH_DIV_STR)) {
866 diff.fBasePath.prepend(relativePath);
867 }
868 print_image_cell(stream, diff.fBasePath, height);
869 }
870 } else {
871 stream->writeText("<td>N/A</td>");
872 }
873 if (!diff.fComparisonMissing) {
874 int h, w;
875 if (!get_bitmap_height_width(diff.fComparisonPath, &h, &w)) {
876 stream->writeText("<td>N/A</td>");
877 } else {
878 int height = compute_image_height(h, w);
879 if (!diff.fComparisonPath.startsWith(PATH_DIV_STR)) {
880 diff.fComparisonPath.prepend(relativePath);
881 }
882 print_image_cell(stream, diff.fComparisonPath, height);
883 }
884 } else {
885 stream->writeText("<td>N/A</td>");
886 }
887 stream->writeText("</tr>\n");
888 stream->flush();
889}
890
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000891static void print_diff_page (const int matchCount,
892 const int colorThreshold,
893 const RecordArray& differences,
894 const SkString& baseDir,
895 const SkString& comparisonDir,
896 const SkString& outputDir) {
897
epoger@google.coma5f406e2012-05-01 13:26:16 +0000898 SkASSERT(!baseDir.isEmpty());
899 SkASSERT(!comparisonDir.isEmpty());
900 SkASSERT(!outputDir.isEmpty());
901
tomhudson@google.com5b325292011-05-24 19:41:13 +0000902 SkString outputPath (outputDir);
903 outputPath.append("index.html");
904 //SkFILEWStream outputStream ("index.html");
905 SkFILEWStream outputStream (outputPath.c_str());
906
tomhudson@google.com4e305982011-07-13 17:42:46 +0000907 // Need to convert paths from relative-to-cwd to relative-to-outputDir
tomhudson@google.com5b325292011-05-24 19:41:13 +0000908 // FIXME this doesn't work if there are '..' inside the outputDir
909 unsigned int ui;
910 SkString relativePath;
911 for (ui = 0; ui < outputDir.size(); ui++) {
bsalomon@google.com1a315fe2011-09-23 14:56:37 +0000912 if (outputDir[ui] == PATH_DIV_CHAR) {
913 relativePath.append(".." PATH_DIV_STR);
tomhudson@google.com5b325292011-05-24 19:41:13 +0000914 }
915 }
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000916
917 outputStream.writeText("<html>\n<body>\n");
epoger@google.com25d961c2012-02-02 20:50:36 +0000918 print_table_header(&outputStream, matchCount, colorThreshold, differences,
919 baseDir, comparisonDir);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000920 int i;
921 for (i = 0; i < differences.count(); i++) {
922 DiffRecord* diff = differences[i];
epoger@google.com5fd53852012-03-22 18:20:06 +0000923
924 if (diff->fBaseMissing || diff->fComparisonMissing) {
925 print_diff_with_missing_file(&outputStream, *diff, relativePath);
926 continue;
927 } else if (0 == diff->fFractionDifference) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000928 continue;
929 }
epoger@google.com5fd53852012-03-22 18:20:06 +0000930
bsalomon@google.com1a315fe2011-09-23 14:56:37 +0000931 if (!diff->fBasePath.startsWith(PATH_DIV_STR)) {
tomhudson@google.com4e305982011-07-13 17:42:46 +0000932 diff->fBasePath.prepend(relativePath);
933 }
bsalomon@google.com1a315fe2011-09-23 14:56:37 +0000934 if (!diff->fComparisonPath.startsWith(PATH_DIV_STR)) {
tomhudson@google.com4e305982011-07-13 17:42:46 +0000935 diff->fComparisonPath.prepend(relativePath);
936 }
epoger@google.com5fd53852012-03-22 18:20:06 +0000937
tomhudson@google.com9b540ce2011-08-02 14:10:04 +0000938 int height = compute_image_height(diff->fBaseHeight, diff->fBaseWidth);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000939 outputStream.writeText("<tr>\n");
940 print_label_cell(&outputStream, *diff);
epoger@google.com01f78702012-04-12 16:32:04 +0000941 if (diff->fDoImageSizesMismatch) {
942 print_text_cell(&outputStream,
943 "[image size mismatch, so no diff to display]");
944 print_text_cell(&outputStream,
945 "[image size mismatch, so no diff to display]");
946 } else {
947 print_image_cell(&outputStream,
948 filename_to_white_filename(diff->fFilename), height);
949 print_image_cell(&outputStream,
950 filename_to_diff_filename(diff->fFilename), height);
951 }
epoger@google.com25d961c2012-02-02 20:50:36 +0000952 print_image_cell(&outputStream, diff->fBasePath, height);
tomhudson@google.com4e305982011-07-13 17:42:46 +0000953 print_image_cell(&outputStream, diff->fComparisonPath, height);
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000954 outputStream.writeText("</tr>\n");
955 outputStream.flush();
956 }
957 outputStream.writeText("</table>\n");
958 outputStream.writeText("</body>\n</html>\n");
959 outputStream.flush();
960}
961
962static void usage (char * argv0) {
963 SkDebugf("Skia baseline image diff tool\n");
epoger@google.coma5f406e2012-05-01 13:26:16 +0000964 SkDebugf("\n"
965"Usage: \n"
966" %s <baseDir> <comparisonDir> [outputDir] \n"
967"or \n"
968" %s --chromium-release|--chromium-debug <baseDir> <outputDir> \n",
969argv0, argv0);
970 SkDebugf("\n"
971"Arguments: \n"
972" --nodiffs: don't write out image diffs or index.html, just generate \n"
973" report on stdout \n"
974" --threshold <n>: only report differences > n (per color channel) [default 0]\n"
975" --match: compare files whose filenames contain this substring; if \n"
976" unspecified, compare ALL files. \n"
977" this flag may be repeated to add more matching substrings. \n"
978" --nomatch: regardless of --match, DO NOT compare files whose filenames \n"
979" contain this substring. \n"
980" this flag may be repeated to add more forbidden substrings. \n"
tomhudson@google.com7d042802011-07-14 13:15:55 +0000981" --sortbymismatch: sort by average color channel mismatch\n");
982 SkDebugf(
epoger@google.coma5f406e2012-05-01 13:26:16 +0000983" --sortbymaxmismatch: sort by worst color channel mismatch;\n"
984" break ties with -sortbymismatch\n"
985" [default sort is by fraction of pixels mismatching]\n");
tomhudson@google.com5b325292011-05-24 19:41:13 +0000986 SkDebugf(
tomhudson@google.com7d042802011-07-14 13:15:55 +0000987" --weighted: sort by # pixels different weighted by color difference\n");
988 SkDebugf(
989" --chromium-release: process Webkit LayoutTests results instead of gm\n"
990" --chromium-debug: process Webkit LayoutTests results instead of gm\n");
991 SkDebugf(
992" baseDir: directory to read baseline images from,\n"
993" or chromium/src directory for --chromium.\n");
tomhudson@google.com9dc527b2011-06-09 15:47:10 +0000994 SkDebugf(
epoger@google.coma5f406e2012-05-01 13:26:16 +0000995" comparisonDir: directory to read comparison images from\n");
996 SkDebugf(
997" outputDir: directory to write difference images and index.html to; \n"
998" defaults to comparisonDir when not running --chromium \n");
tomhudson@google.com4b33d282011-04-27 15:39:30 +0000999}
1000
1001int main (int argc, char ** argv) {
1002 DiffMetricProc diffProc = compute_diff_pmcolor;
reed@google.comc7a67cb2012-05-07 14:52:12 +00001003 int (*sortProc)(const void*, const void*) = SkCastForQSort(compare_diff_metrics);
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001004
1005 // Maximum error tolerated in any one color channel in any one pixel before
1006 // a difference is reported.
1007 int colorThreshold = 0;
1008 SkString baseDir;
1009 SkString comparisonDir;
1010 SkString outputDir;
epoger@google.coma5f406e2012-05-01 13:26:16 +00001011 StringArray matchSubstrings;
1012 StringArray nomatchSubstrings;
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001013
tomhudson@google.com7d042802011-07-14 13:15:55 +00001014 bool analyzeChromium = false;
1015 bool chromiumDebug = false;
1016 bool chromiumRelease = false;
epoger@google.coma5f406e2012-05-01 13:26:16 +00001017 bool generateDiffs = true;
tomhudson@google.com7d042802011-07-14 13:15:55 +00001018
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001019 RecordArray differences;
tomhudson@google.com9dc527b2011-06-09 15:47:10 +00001020 DiffSummary summary;
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001021
epoger@google.coma5f406e2012-05-01 13:26:16 +00001022 int i;
1023 int numUnflaggedArguments = 0;
1024 for (i = 1; i < argc; i++) {
tomhudson@google.com7d042802011-07-14 13:15:55 +00001025 if (!strcmp(argv[i], "--help")) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001026 usage(argv[0]);
1027 return 0;
1028 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001029 if (!strcmp(argv[i], "--nodiffs")) {
1030 generateDiffs = false;
1031 continue;
1032 }
1033 if (!strcmp(argv[i], "--threshold")) {
1034 colorThreshold = atoi(argv[++i]);
1035 continue;
1036 }
1037 if (!strcmp(argv[i], "--match")) {
1038 matchSubstrings.push(new SkString(argv[++i]));
1039 continue;
1040 }
1041 if (!strcmp(argv[i], "--nomatch")) {
1042 nomatchSubstrings.push(new SkString(argv[++i]));
1043 continue;
1044 }
tomhudson@google.com7d042802011-07-14 13:15:55 +00001045 if (!strcmp(argv[i], "--sortbymismatch")) {
reed@google.comc7a67cb2012-05-07 14:52:12 +00001046 sortProc = SkCastForQSort(compare_diff_mean_mismatches);
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001047 continue;
1048 }
tomhudson@google.com7d042802011-07-14 13:15:55 +00001049 if (!strcmp(argv[i], "--sortbymaxmismatch")) {
reed@google.comc7a67cb2012-05-07 14:52:12 +00001050 sortProc = SkCastForQSort(compare_diff_max_mismatches);
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001051 continue;
1052 }
tomhudson@google.com7d042802011-07-14 13:15:55 +00001053 if (!strcmp(argv[i], "--weighted")) {
reed@google.comc7a67cb2012-05-07 14:52:12 +00001054 sortProc = SkCastForQSort(compare_diff_weighted);
tomhudson@google.com5b325292011-05-24 19:41:13 +00001055 continue;
1056 }
tomhudson@google.com7d042802011-07-14 13:15:55 +00001057 if (!strcmp(argv[i], "--chromium-release")) {
1058 analyzeChromium = true;
1059 chromiumRelease = true;
1060 continue;
1061 }
1062 if (!strcmp(argv[i], "--chromium-debug")) {
1063 analyzeChromium = true;
1064 chromiumDebug = true;
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001065 continue;
1066 }
1067 if (argv[i][0] != '-') {
epoger@google.coma5f406e2012-05-01 13:26:16 +00001068 switch (numUnflaggedArguments++) {
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001069 case 0:
1070 baseDir.set(argv[i]);
1071 continue;
1072 case 1:
1073 comparisonDir.set(argv[i]);
1074 continue;
1075 case 2:
1076 outputDir.set(argv[i]);
1077 continue;
1078 default:
epoger@google.coma5f406e2012-05-01 13:26:16 +00001079 SkDebugf("extra unflagged argument <%s>\n", argv[i]);
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001080 usage(argv[0]);
1081 return 0;
1082 }
1083 }
1084
1085 SkDebugf("Unrecognized argument <%s>\n", argv[i]);
1086 usage(argv[0]);
1087 return 0;
1088 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001089
tomhudson@google.com7d042802011-07-14 13:15:55 +00001090 if (analyzeChromium) {
epoger@google.coma5f406e2012-05-01 13:26:16 +00001091 if (numUnflaggedArguments != 2) {
tomhudson@google.com7d042802011-07-14 13:15:55 +00001092 usage(argv[0]);
1093 return 0;
1094 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001095 outputDir = comparisonDir;
tomhudson@google.com7d042802011-07-14 13:15:55 +00001096 if (chromiumRelease && chromiumDebug) {
1097 SkDebugf(
1098"--chromium must be either -release or -debug, not both!\n");
1099 return 0;
1100 }
1101 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001102
1103 if (numUnflaggedArguments == 2) {
tomhudson@google.com9dc527b2011-06-09 15:47:10 +00001104 outputDir = comparisonDir;
epoger@google.coma5f406e2012-05-01 13:26:16 +00001105 } else if (numUnflaggedArguments != 3) {
tomhudson@google.com9dc527b2011-06-09 15:47:10 +00001106 usage(argv[0]);
1107 return 0;
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001108 }
1109
bsalomon@google.com1a315fe2011-09-23 14:56:37 +00001110 if (!baseDir.endsWith(PATH_DIV_STR)) {
1111 baseDir.append(PATH_DIV_STR);
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001112 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001113 printf("baseDir is [%s]\n", baseDir.c_str());
1114
bsalomon@google.com1a315fe2011-09-23 14:56:37 +00001115 if (!comparisonDir.endsWith(PATH_DIV_STR)) {
1116 comparisonDir.append(PATH_DIV_STR);
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001117 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001118 printf("comparisonDir is [%s]\n", comparisonDir.c_str());
1119
bsalomon@google.com1a315fe2011-09-23 14:56:37 +00001120 if (!outputDir.endsWith(PATH_DIV_STR)) {
1121 outputDir.append(PATH_DIV_STR);
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001122 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001123 if (generateDiffs) {
1124 printf("writing diffs to outputDir is [%s]\n", outputDir.c_str());
1125 } else {
1126 printf("not writing any diffs to outputDir [%s]\n", outputDir.c_str());
1127 outputDir.set("");
1128 }
1129
1130 // Default substring matching:
1131 // - No matter what, don't match any PDF files.
1132 // We may want to change this later, but for now this maintains the filter
1133 // that get_file_list() used to always apply.
1134 // - If no matchSubstrings were specified, match ALL strings.
1135 nomatchSubstrings.push(new SkString(".pdf"));
1136 if (matchSubstrings.isEmpty()) {
1137 matchSubstrings.push(new SkString(""));
1138 }
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001139
tomhudson@google.com7d042802011-07-14 13:15:55 +00001140 if (analyzeChromium) {
bsalomon@google.com1a315fe2011-09-23 14:56:37 +00001141 baseDir.append("webkit" PATH_DIV_STR);
tomhudson@google.com7d042802011-07-14 13:15:55 +00001142 if (chromiumRelease) {
bsalomon@google.com1a315fe2011-09-23 14:56:37 +00001143 baseDir.append("Release" PATH_DIV_STR);
tomhudson@google.com7d042802011-07-14 13:15:55 +00001144 }
1145 if (chromiumDebug) {
bsalomon@google.com1a315fe2011-09-23 14:56:37 +00001146 baseDir.append("Debug" PATH_DIV_STR);
tomhudson@google.com7d042802011-07-14 13:15:55 +00001147 }
bsalomon@google.com1a315fe2011-09-23 14:56:37 +00001148 baseDir.append("layout-test-results" PATH_DIV_STR);
tomhudson@google.com7d042802011-07-14 13:15:55 +00001149 analyze_chromium(diffProc, colorThreshold, &differences,
1150 baseDir, outputDir, &summary);
1151 } else {
1152 create_diff_images(diffProc, colorThreshold, &differences,
epoger@google.coma5f406e2012-05-01 13:26:16 +00001153 baseDir, comparisonDir, outputDir,
1154 matchSubstrings, nomatchSubstrings, &summary);
tomhudson@google.com7d042802011-07-14 13:15:55 +00001155 }
tomhudson@google.com9dc527b2011-06-09 15:47:10 +00001156 summary.print();
tomhudson@google.com7d042802011-07-14 13:15:55 +00001157
1158 if (differences.count()) {
reed@google.comc7a67cb2012-05-07 14:52:12 +00001159 qsort(differences.begin(), differences.count(),
1160 sizeof(DiffRecord*), sortProc);
tomhudson@google.com7d042802011-07-14 13:15:55 +00001161 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001162
1163 if (generateDiffs) {
1164 print_diff_page(summary.fNumMatches, colorThreshold, differences,
1165 baseDir, comparisonDir, outputDir);
1166 }
1167
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001168 for (i = 0; i < differences.count(); i++) {
1169 delete differences[i];
1170 }
epoger@google.coma5f406e2012-05-01 13:26:16 +00001171 matchSubstrings.deleteAll();
1172 nomatchSubstrings.deleteAll();
tomhudson@google.com4b33d282011-04-27 15:39:30 +00001173}