1 // Copyright 2011 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // This file input format is based loosely on
6 // Tools/DumpRenderTree/ImageDiff.m
7
8 // The exact format of this tool's output to stdout is important, to match
9 // what the run-webkit-tests script expects.
10
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <string.h>
14
15 #include <algorithm>
16 #include <cmath>
17 #include <map>
18 #include <string>
19 #include <vector>
20
21 #include "core/fxcrt/fx_memory.h"
22 #include "testing/image_diff/image_diff_png.h"
23 #include "testing/utils/path_service.h"
24 #include "third_party/base/cxx17_backports.h"
25 #include "third_party/base/numerics/safe_conversions.h"
26
27 #if BUILDFLAG(IS_WIN)
28 #include <windows.h>
29 #endif
30
31 // Return codes used by this utility.
32 constexpr int kStatusSame = 0;
33 constexpr int kStatusDifferent = 1;
34 constexpr int kStatusError = 2;
35
36 // Color codes.
37 constexpr uint32_t RGBA_RED = 0x000000ff;
38 constexpr uint32_t RGBA_ALPHA = 0xff000000;
39
40 class Image {
41 public:
42 Image() = default;
43 Image(const Image& image) = default;
44 Image& operator=(const Image& other) = default;
45
has_image() const46 bool has_image() const { return w_ > 0 && h_ > 0; }
w() const47 int w() const { return w_; }
h() const48 int h() const { return h_; }
span() const49 pdfium::span<const uint8_t> span() const { return data_; }
50
51 // Creates the image from the given filename on disk, and returns true on
52 // success.
CreateFromFilename(const std::string & path)53 bool CreateFromFilename(const std::string& path) {
54 return CreateFromFilenameImpl(path, /*reverse_byte_order=*/false);
55 }
56
57 // Same as CreateFromFilename(), but with BGRA instead of RGBA ordering.
CreateFromFilenameWithReverseByteOrder(const std::string & path)58 bool CreateFromFilenameWithReverseByteOrder(const std::string& path) {
59 return CreateFromFilenameImpl(path, /*reverse_byte_order=*/true);
60 }
61
Clear()62 void Clear() {
63 w_ = h_ = 0;
64 data_.clear();
65 }
66
67 // Returns the RGBA value of the pixel at the given location
pixel_at(int x,int y) const68 uint32_t pixel_at(int x, int y) const {
69 if (!pixel_in_bounds(x, y))
70 return 0;
71 return *reinterpret_cast<const uint32_t*>(&(data_[pixel_address(x, y)]));
72 }
73
set_pixel_at(int x,int y,uint32_t color)74 void set_pixel_at(int x, int y, uint32_t color) {
75 if (!pixel_in_bounds(x, y))
76 return;
77
78 void* addr = &data_[pixel_address(x, y)];
79 *reinterpret_cast<uint32_t*>(addr) = color;
80 }
81
82 private:
CreateFromFilenameImpl(const std::string & path,bool reverse_byte_order)83 bool CreateFromFilenameImpl(const std::string& path,
84 bool reverse_byte_order) {
85 FILE* f = fopen(path.c_str(), "rb");
86 if (!f)
87 return false;
88
89 std::vector<uint8_t> compressed;
90 const size_t kBufSize = 1024;
91 uint8_t buf[kBufSize];
92 size_t num_read = 0;
93 while ((num_read = fread(buf, 1, kBufSize, f)) > 0) {
94 compressed.insert(compressed.end(), buf, buf + num_read);
95 }
96
97 fclose(f);
98
99 data_ = image_diff_png::DecodePNG(compressed, reverse_byte_order, &w_, &h_);
100 if (data_.empty()) {
101 Clear();
102 return false;
103 }
104 return true;
105 }
106
pixel_in_bounds(int x,int y) const107 bool pixel_in_bounds(int x, int y) const {
108 return x >= 0 && x < w_ && y >= 0 && y < h_;
109 }
110
pixel_address(int x,int y) const111 size_t pixel_address(int x, int y) const { return (y * w_ + x) * 4; }
112
113 // Pixel dimensions of the image.
114 int w_ = 0;
115 int h_ = 0;
116
117 std::vector<uint8_t> data_;
118 };
119
CalculateDifferencePercentage(const Image & actual,int pixels_different)120 float CalculateDifferencePercentage(const Image& actual, int pixels_different) {
121 // Like the WebKit ImageDiff tool, we define percentage different in terms
122 // of the size of the 'actual' bitmap.
123 float total_pixels =
124 static_cast<float>(actual.w()) * static_cast<float>(actual.h());
125 if (total_pixels == 0) {
126 // When the bitmap is empty, they are 100% different.
127 return 100.0f;
128 }
129 return 100.0f * pixels_different / total_pixels;
130 }
131
CountImageSizeMismatchAsPixelDifference(const Image & baseline,const Image & actual,int * pixels_different)132 void CountImageSizeMismatchAsPixelDifference(const Image& baseline,
133 const Image& actual,
134 int* pixels_different) {
135 int w = std::min(baseline.w(), actual.w());
136 int h = std::min(baseline.h(), actual.h());
137
138 // Count pixels that are a difference in size as also being different.
139 int max_w = std::max(baseline.w(), actual.w());
140 int max_h = std::max(baseline.h(), actual.h());
141 // These pixels are off the right side, not including the lower right corner.
142 *pixels_different += (max_w - w) * h;
143 // These pixels are along the bottom, including the lower right corner.
144 *pixels_different += (max_h - h) * max_w;
145 }
146
147 struct UnpackedPixel {
UnpackedPixelUnpackedPixel148 explicit UnpackedPixel(uint32_t packed)
149 : red(packed & 0xff),
150 green((packed >> 8) & 0xff),
151 blue((packed >> 16) & 0xff),
152 alpha((packed >> 24) & 0xff) {}
153
154 uint8_t red;
155 uint8_t green;
156 uint8_t blue;
157 uint8_t alpha;
158 };
159
ChannelDelta(uint8_t baseline_channel,uint8_t actual_channel)160 uint8_t ChannelDelta(uint8_t baseline_channel, uint8_t actual_channel) {
161 // No casts are necessary because arithmetic operators implicitly convert
162 // `uint8_t` to `int` first. The final delta is always in the range 0 to 255.
163 return std::abs(baseline_channel - actual_channel);
164 }
165
MaxPixelPerChannelDelta(const UnpackedPixel & baseline_pixel,const UnpackedPixel & actual_pixel)166 uint8_t MaxPixelPerChannelDelta(const UnpackedPixel& baseline_pixel,
167 const UnpackedPixel& actual_pixel) {
168 return std::max({ChannelDelta(baseline_pixel.red, actual_pixel.red),
169 ChannelDelta(baseline_pixel.green, actual_pixel.green),
170 ChannelDelta(baseline_pixel.blue, actual_pixel.blue),
171 ChannelDelta(baseline_pixel.alpha, actual_pixel.alpha)});
172 }
173
PercentageDifferent(const Image & baseline,const Image & actual,uint8_t max_pixel_per_channel_delta)174 float PercentageDifferent(const Image& baseline,
175 const Image& actual,
176 uint8_t max_pixel_per_channel_delta) {
177 int w = std::min(baseline.w(), actual.w());
178 int h = std::min(baseline.h(), actual.h());
179
180 // Compute pixels different in the overlap.
181 int pixels_different = 0;
182 for (int y = 0; y < h; ++y) {
183 for (int x = 0; x < w; ++x) {
184 const uint32_t baseline_pixel = baseline.pixel_at(x, y);
185 const uint32_t actual_pixel = actual.pixel_at(x, y);
186 if (baseline_pixel == actual_pixel) {
187 continue;
188 }
189
190 if (MaxPixelPerChannelDelta(UnpackedPixel(baseline_pixel),
191 UnpackedPixel(actual_pixel)) >
192 max_pixel_per_channel_delta) {
193 ++pixels_different;
194 }
195 }
196 }
197
198 CountImageSizeMismatchAsPixelDifference(baseline, actual, &pixels_different);
199 return CalculateDifferencePercentage(actual, pixels_different);
200 }
201
HistogramPercentageDifferent(const Image & baseline,const Image & actual)202 float HistogramPercentageDifferent(const Image& baseline, const Image& actual) {
203 // TODO(johnme): Consider using a joint histogram instead, as described in
204 // "Comparing Images Using Joint Histograms" by Pass & Zabih
205 // http://www.cs.cornell.edu/~rdz/papers/pz-jms99.pdf
206
207 int w = std::min(baseline.w(), actual.w());
208 int h = std::min(baseline.h(), actual.h());
209
210 // Count occurrences of each RGBA pixel value of baseline in the overlap.
211 std::map<uint32_t, int32_t> baseline_histogram;
212 for (int y = 0; y < h; ++y) {
213 for (int x = 0; x < w; ++x) {
214 // hash_map operator[] inserts a 0 (default constructor) if key not found.
215 ++baseline_histogram[baseline.pixel_at(x, y)];
216 }
217 }
218
219 // Compute pixels different in the histogram of the overlap.
220 int pixels_different = 0;
221 for (int y = 0; y < h; ++y) {
222 for (int x = 0; x < w; ++x) {
223 uint32_t actual_rgba = actual.pixel_at(x, y);
224 auto it = baseline_histogram.find(actual_rgba);
225 if (it != baseline_histogram.end() && it->second > 0)
226 --it->second;
227 else
228 ++pixels_different;
229 }
230 }
231
232 CountImageSizeMismatchAsPixelDifference(baseline, actual, &pixels_different);
233 return CalculateDifferencePercentage(actual, pixels_different);
234 }
235
PrintHelp(const std::string & binary_name)236 void PrintHelp(const std::string& binary_name) {
237 fprintf(
238 stderr,
239 "Usage:\n"
240 " %s OPTIONS <compare_file> <reference_file>\n"
241 " Compares two files on disk, returning 0 when they are the same.\n"
242 " Passing \"--histogram\" additionally calculates a diff of the\n"
243 " RGBA value histograms (which is resistant to shifts in layout).\n"
244 " Passing \"--reverse-byte-order\" additionally assumes the\n"
245 " compare file has BGRA byte ordering.\n"
246 " Passing \"--fuzzy\" additionally allows individual pixels to\n"
247 " differ by at most 1 on each channel.\n\n"
248 " %s --diff <compare_file> <reference_file> <output_file>\n"
249 " Compares two files on disk, and if they differ, outputs an image\n"
250 " to <output_file> that visualizes the differing pixels as red\n"
251 " dots.\n\n"
252 " %s --subtract <compare_file> <reference_file> <output_file>\n"
253 " Compares two files on disk, and if they differ, outputs an image\n"
254 " to <output_file> that visualizes the difference as a scaled\n"
255 " subtraction of pixel values.\n",
256 binary_name.c_str(), binary_name.c_str(), binary_name.c_str());
257 }
258
CompareImages(const std::string & binary_name,const std::string & file1,const std::string & file2,bool compare_histograms,bool reverse_byte_order,uint8_t max_pixel_per_channel_delta)259 int CompareImages(const std::string& binary_name,
260 const std::string& file1,
261 const std::string& file2,
262 bool compare_histograms,
263 bool reverse_byte_order,
264 uint8_t max_pixel_per_channel_delta) {
265 Image actual_image;
266 Image baseline_image;
267
268 bool actual_load_result =
269 reverse_byte_order
270 ? actual_image.CreateFromFilenameWithReverseByteOrder(file1)
271 : actual_image.CreateFromFilename(file1);
272 if (!actual_load_result) {
273 fprintf(stderr, "%s: Unable to open file \"%s\"\n", binary_name.c_str(),
274 file1.c_str());
275 return kStatusError;
276 }
277 if (!baseline_image.CreateFromFilename(file2)) {
278 fprintf(stderr, "%s: Unable to open file \"%s\"\n", binary_name.c_str(),
279 file2.c_str());
280 return kStatusError;
281 }
282
283 if (compare_histograms) {
284 float percent = HistogramPercentageDifferent(actual_image, baseline_image);
285 const char* passed = percent > 0.0 ? "failed" : "passed";
286 printf("histogram diff: %01.2f%% %s\n", percent, passed);
287 }
288
289 const char* const diff_name = compare_histograms ? "exact diff" : "diff";
290 float percent = PercentageDifferent(actual_image, baseline_image,
291 max_pixel_per_channel_delta);
292 const char* const passed = percent > 0.0 ? "failed" : "passed";
293 printf("%s: %01.2f%% %s\n", diff_name, percent, passed);
294
295 if (percent > 0.0) {
296 // failure: The WebKit version also writes the difference image to
297 // stdout, which seems excessive for our needs.
298 return kStatusDifferent;
299 }
300 // success
301 return kStatusSame;
302 }
303
CreateImageDiff(const Image & image1,const Image & image2,Image * out)304 bool CreateImageDiff(const Image& image1, const Image& image2, Image* out) {
305 int w = std::min(image1.w(), image2.w());
306 int h = std::min(image1.h(), image2.h());
307 *out = Image(image1);
308 bool same = (image1.w() == image2.w()) && (image1.h() == image2.h());
309
310 // TODO(estade): do something with the extra pixels if the image sizes
311 // are different.
312 for (int y = 0; y < h; ++y) {
313 for (int x = 0; x < w; ++x) {
314 uint32_t base_pixel = image1.pixel_at(x, y);
315 if (base_pixel != image2.pixel_at(x, y)) {
316 // Set differing pixels red.
317 out->set_pixel_at(x, y, RGBA_RED | RGBA_ALPHA);
318 same = false;
319 } else {
320 // Set same pixels as faded.
321 uint32_t alpha = base_pixel & RGBA_ALPHA;
322 uint32_t new_pixel = base_pixel - ((alpha / 2) & RGBA_ALPHA);
323 out->set_pixel_at(x, y, new_pixel);
324 }
325 }
326 }
327
328 return same;
329 }
330
SubtractImages(const Image & image1,const Image & image2,Image * out)331 bool SubtractImages(const Image& image1, const Image& image2, Image* out) {
332 int w = std::min(image1.w(), image2.w());
333 int h = std::min(image1.h(), image2.h());
334 *out = Image(image1);
335 bool same = (image1.w() == image2.w()) && (image1.h() == image2.h());
336
337 for (int y = 0; y < h; ++y) {
338 for (int x = 0; x < w; ++x) {
339 uint32_t pixel1 = image1.pixel_at(x, y);
340 int32_t r1 = pixel1 & 0xff;
341 int32_t g1 = (pixel1 >> 8) & 0xff;
342 int32_t b1 = (pixel1 >> 16) & 0xff;
343
344 uint32_t pixel2 = image2.pixel_at(x, y);
345 int32_t r2 = pixel2 & 0xff;
346 int32_t g2 = (pixel2 >> 8) & 0xff;
347 int32_t b2 = (pixel2 >> 16) & 0xff;
348
349 int32_t delta_r = r1 - r2;
350 int32_t delta_g = g1 - g2;
351 int32_t delta_b = b1 - b2;
352 same &= (delta_r == 0 && delta_g == 0 && delta_b == 0);
353
354 delta_r = pdfium::clamp(128 + delta_r * 8, 0, 255);
355 delta_g = pdfium::clamp(128 + delta_g * 8, 0, 255);
356 delta_b = pdfium::clamp(128 + delta_b * 8, 0, 255);
357
358 uint32_t new_pixel = RGBA_ALPHA;
359 new_pixel |= delta_r;
360 new_pixel |= (delta_g << 8);
361 new_pixel |= (delta_b << 16);
362 out->set_pixel_at(x, y, new_pixel);
363 }
364 }
365 return same;
366 }
367
DiffImages(const std::string & binary_name,const std::string & file1,const std::string & file2,const std::string & out_file,bool do_subtraction)368 int DiffImages(const std::string& binary_name,
369 const std::string& file1,
370 const std::string& file2,
371 const std::string& out_file,
372 bool do_subtraction) {
373 Image actual_image;
374 Image baseline_image;
375
376 if (!actual_image.CreateFromFilename(file1)) {
377 fprintf(stderr, "%s: Unable to open file \"%s\"\n", binary_name.c_str(),
378 file1.c_str());
379 return kStatusError;
380 }
381 if (!baseline_image.CreateFromFilename(file2)) {
382 fprintf(stderr, "%s: Unable to open file \"%s\"\n", binary_name.c_str(),
383 file2.c_str());
384 return kStatusError;
385 }
386
387 Image diff_image;
388 bool same = do_subtraction
389 ? SubtractImages(baseline_image, actual_image, &diff_image)
390 : CreateImageDiff(baseline_image, actual_image, &diff_image);
391 if (same)
392 return kStatusSame;
393
394 std::vector<uint8_t> png_encoding = image_diff_png::EncodeRGBAPNG(
395 diff_image.span(), diff_image.w(), diff_image.h(), diff_image.w() * 4);
396 if (png_encoding.empty())
397 return kStatusError;
398
399 FILE* f = fopen(out_file.c_str(), "wb");
400 if (!f)
401 return kStatusError;
402
403 size_t size = png_encoding.size();
404 char* ptr = reinterpret_cast<char*>(&png_encoding.front());
405 if (fwrite(ptr, 1, size, f) != size)
406 return kStatusError;
407
408 return kStatusDifferent;
409 }
410
main(int argc,const char * argv[])411 int main(int argc, const char* argv[]) {
412 FX_InitializeMemoryAllocators();
413
414 bool histograms = false;
415 bool produce_diff_image = false;
416 bool produce_image_subtraction = false;
417 bool reverse_byte_order = false;
418 uint8_t max_pixel_per_channel_delta = 0;
419 std::string filename1;
420 std::string filename2;
421 std::string diff_filename;
422
423 // Strip the path from the first arg
424 const char* last_separator = strrchr(argv[0], PATH_SEPARATOR);
425 std::string binary_name = last_separator ? last_separator + 1 : argv[0];
426
427 int i;
428 for (i = 1; i < argc; ++i) {
429 const char* arg = argv[i];
430 if (strstr(arg, "--") != arg)
431 break;
432 if (strcmp(arg, "--histogram") == 0) {
433 histograms = true;
434 } else if (strcmp(arg, "--diff") == 0) {
435 produce_diff_image = true;
436 } else if (strcmp(arg, "--subtract") == 0) {
437 produce_image_subtraction = true;
438 } else if (strcmp(arg, "--reverse-byte-order") == 0) {
439 reverse_byte_order = true;
440 } else if (strcmp(arg, "--fuzzy") == 0) {
441 max_pixel_per_channel_delta = 1;
442 }
443 }
444 if (i < argc)
445 filename1 = argv[i++];
446 if (i < argc)
447 filename2 = argv[i++];
448 if (i < argc)
449 diff_filename = argv[i++];
450
451 if (produce_diff_image || produce_image_subtraction) {
452 if (!diff_filename.empty()) {
453 return DiffImages(binary_name, filename1, filename2, diff_filename,
454 produce_image_subtraction);
455 }
456 } else if (!filename2.empty()) {
457 return CompareImages(binary_name, filename1, filename2, histograms,
458 reverse_byte_order, max_pixel_per_channel_delta);
459 }
460
461 PrintHelp(binary_name);
462 return kStatusError;
463 }
464