• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
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 <assert.h>
12 #include <stdint.h>
13 #include <stdio.h>
14 #include <string.h>
15 
16 #include <algorithm>
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/logging.h"
25 #include "third_party/base/numerics/safe_conversions.h"
26 
27 #if defined(OS_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:
Image()42   Image() : w_(0), h_(0) {}
Image(const Image & image)43   Image(const Image& image) : w_(image.w_), h_(image.h_), data_(image.data_) {}
44 
has_image() const45   bool has_image() const { return w_ > 0 && h_ > 0; }
w() const46   int w() const { return w_; }
h() const47   int h() const { return h_; }
span() const48   pdfium::span<const uint8_t> span() const { return data_; }
49 
50   // Creates the image from the given filename on disk, and returns true on
51   // success.
CreateFromFilename(const std::string & path)52   bool CreateFromFilename(const std::string& path) {
53     return CreateFromFilenameImpl(path, /*reverse_byte_order=*/false);
54   }
55 
56   // Same as CreateFromFilename(), but with BGRA instead of RGBA ordering.
CreateFromFilenameWithReverseByteOrder(const std::string & path)57   bool CreateFromFilenameWithReverseByteOrder(const std::string& path) {
58     return CreateFromFilenameImpl(path, /*reverse_byte_order=*/true);
59   }
60 
Clear()61   void Clear() {
62     w_ = h_ = 0;
63     data_.clear();
64   }
65 
66   // Returns the RGBA value of the pixel at the given location
pixel_at(int x,int y) const67   uint32_t pixel_at(int x, int y) const {
68     if (!pixel_in_bounds(x, y))
69       return 0;
70     return *reinterpret_cast<const uint32_t*>(&(data_[pixel_address(x, y)]));
71   }
72 
set_pixel_at(int x,int y,uint32_t color)73   void set_pixel_at(int x, int y, uint32_t color) {
74     if (!pixel_in_bounds(x, y))
75       return;
76 
77     void* addr = &data_[pixel_address(x, y)];
78     *reinterpret_cast<uint32_t*>(addr) = color;
79   }
80 
81  private:
CreateFromFilenameImpl(const std::string & path,bool reverse_byte_order)82   bool CreateFromFilenameImpl(const std::string& path,
83                               bool reverse_byte_order) {
84     FILE* f = fopen(path.c_str(), "rb");
85     if (!f)
86       return false;
87 
88     std::vector<uint8_t> compressed;
89     const size_t kBufSize = 1024;
90     uint8_t buf[kBufSize];
91     size_t num_read = 0;
92     while ((num_read = fread(buf, 1, kBufSize, f)) > 0) {
93       compressed.insert(compressed.end(), buf, buf + num_read);
94     }
95 
96     fclose(f);
97 
98     data_ = image_diff_png::DecodePNG(compressed, reverse_byte_order, &w_, &h_);
99     if (data_.empty()) {
100       Clear();
101       return false;
102     }
103     return true;
104   }
105 
pixel_in_bounds(int x,int y) const106   bool pixel_in_bounds(int x, int y) const {
107     return x >= 0 && x < w_ && y >= 0 && y < h_;
108   }
109 
pixel_address(int x,int y) const110   size_t pixel_address(int x, int y) const { return (y * w_ + x) * 4; }
111 
112   // Pixel dimensions of the image.
113   int w_;
114   int h_;
115 
116   std::vector<uint8_t> data_;
117 };
118 
CalculateDifferencePercentage(const Image & actual,int pixels_different)119 float CalculateDifferencePercentage(const Image& actual, int pixels_different) {
120   // Like the WebKit ImageDiff tool, we define percentage different in terms
121   // of the size of the 'actual' bitmap.
122   float total_pixels =
123       static_cast<float>(actual.w()) * static_cast<float>(actual.h());
124   if (total_pixels == 0) {
125     // When the bitmap is empty, they are 100% different.
126     return 100.0f;
127   }
128   return 100.0f * pixels_different / total_pixels;
129 }
130 
CountImageSizeMismatchAsPixelDifference(const Image & baseline,const Image & actual,int * pixels_different)131 void CountImageSizeMismatchAsPixelDifference(const Image& baseline,
132                                              const Image& actual,
133                                              int* pixels_different) {
134   int w = std::min(baseline.w(), actual.w());
135   int h = std::min(baseline.h(), actual.h());
136 
137   // Count pixels that are a difference in size as also being different.
138   int max_w = std::max(baseline.w(), actual.w());
139   int max_h = std::max(baseline.h(), actual.h());
140   // These pixels are off the right side, not including the lower right corner.
141   *pixels_different += (max_w - w) * h;
142   // These pixels are along the bottom, including the lower right corner.
143   *pixels_different += (max_h - h) * max_w;
144 }
145 
PercentageDifferent(const Image & baseline,const Image & actual)146 float PercentageDifferent(const Image& baseline, const Image& actual) {
147   int w = std::min(baseline.w(), actual.w());
148   int h = std::min(baseline.h(), actual.h());
149 
150   // Compute pixels different in the overlap.
151   int pixels_different = 0;
152   for (int y = 0; y < h; ++y) {
153     for (int x = 0; x < w; ++x) {
154       if (baseline.pixel_at(x, y) != actual.pixel_at(x, y))
155         ++pixels_different;
156     }
157   }
158 
159   CountImageSizeMismatchAsPixelDifference(baseline, actual, &pixels_different);
160   return CalculateDifferencePercentage(actual, pixels_different);
161 }
162 
HistogramPercentageDifferent(const Image & baseline,const Image & actual)163 float HistogramPercentageDifferent(const Image& baseline, const Image& actual) {
164   // TODO(johnme): Consider using a joint histogram instead, as described in
165   // "Comparing Images Using Joint Histograms" by Pass & Zabih
166   // http://www.cs.cornell.edu/~rdz/papers/pz-jms99.pdf
167 
168   int w = std::min(baseline.w(), actual.w());
169   int h = std::min(baseline.h(), actual.h());
170 
171   // Count occurrences of each RGBA pixel value of baseline in the overlap.
172   std::map<uint32_t, int32_t> baseline_histogram;
173   for (int y = 0; y < h; ++y) {
174     for (int x = 0; x < w; ++x) {
175       // hash_map operator[] inserts a 0 (default constructor) if key not found.
176       ++baseline_histogram[baseline.pixel_at(x, y)];
177     }
178   }
179 
180   // Compute pixels different in the histogram of the overlap.
181   int pixels_different = 0;
182   for (int y = 0; y < h; ++y) {
183     for (int x = 0; x < w; ++x) {
184       uint32_t actual_rgba = actual.pixel_at(x, y);
185       auto it = baseline_histogram.find(actual_rgba);
186       if (it != baseline_histogram.end() && it->second > 0)
187         --it->second;
188       else
189         ++pixels_different;
190     }
191   }
192 
193   CountImageSizeMismatchAsPixelDifference(baseline, actual, &pixels_different);
194   return CalculateDifferencePercentage(actual, pixels_different);
195 }
196 
PrintHelp(const std::string & binary_name)197 void PrintHelp(const std::string& binary_name) {
198   fprintf(
199       stderr,
200       "Usage:\n"
201       "  %s OPTIONS <compare file> <reference file>\n"
202       "    Compares two files on disk, returning 0 when they are the same;\n"
203       "    Passing \"--histogram\" additionally calculates a diff of the\n"
204       "    RGBA value histograms. (which is resistant to shifts in layout)\n"
205       "    Passing \"--reverse-byte-order\" additionally assumes the compare\n"
206       "    file has BGRA byte ordering.\n"
207       "  %s --diff <compare file> <reference file> <output file>\n"
208       "    Compares two files on disk, outputs an image that visualizes the\n"
209       "    difference to <output file>\n",
210       binary_name.c_str(), binary_name.c_str());
211 }
212 
CompareImages(const std::string & binary_name,const std::string & file1,const std::string & file2,bool compare_histograms,bool reverse_byte_order)213 int CompareImages(const std::string& binary_name,
214                   const std::string& file1,
215                   const std::string& file2,
216                   bool compare_histograms,
217                   bool reverse_byte_order) {
218   Image actual_image;
219   Image baseline_image;
220 
221   bool actual_load_result =
222       reverse_byte_order
223           ? actual_image.CreateFromFilenameWithReverseByteOrder(file1)
224           : actual_image.CreateFromFilename(file1);
225   if (!actual_load_result) {
226     fprintf(stderr, "%s: Unable to open file \"%s\"\n", binary_name.c_str(),
227             file1.c_str());
228     return kStatusError;
229   }
230   if (!baseline_image.CreateFromFilename(file2)) {
231     fprintf(stderr, "%s: Unable to open file \"%s\"\n", binary_name.c_str(),
232             file2.c_str());
233     return kStatusError;
234   }
235 
236   if (compare_histograms) {
237     float percent = HistogramPercentageDifferent(actual_image, baseline_image);
238     const char* passed = percent > 0.0 ? "failed" : "passed";
239     printf("histogram diff: %01.2f%% %s\n", percent, passed);
240   }
241 
242   const char* const diff_name = compare_histograms ? "exact diff" : "diff";
243   float percent = PercentageDifferent(actual_image, baseline_image);
244   const char* const passed = percent > 0.0 ? "failed" : "passed";
245   printf("%s: %01.2f%% %s\n", diff_name, percent, passed);
246 
247   if (percent > 0.0) {
248     // failure: The WebKit version also writes the difference image to
249     // stdout, which seems excessive for our needs.
250     return kStatusDifferent;
251   }
252   // success
253   return kStatusSame;
254 }
255 
CreateImageDiff(const Image & image1,const Image & image2,Image * out)256 bool CreateImageDiff(const Image& image1, const Image& image2, Image* out) {
257   int w = std::min(image1.w(), image2.w());
258   int h = std::min(image1.h(), image2.h());
259   *out = Image(image1);
260   bool same = (image1.w() == image2.w()) && (image1.h() == image2.h());
261 
262   // TODO(estade): do something with the extra pixels if the image sizes
263   // are different.
264   for (int y = 0; y < h; ++y) {
265     for (int x = 0; x < w; ++x) {
266       uint32_t base_pixel = image1.pixel_at(x, y);
267       if (base_pixel != image2.pixel_at(x, y)) {
268         // Set differing pixels red.
269         out->set_pixel_at(x, y, RGBA_RED | RGBA_ALPHA);
270         same = false;
271       } else {
272         // Set same pixels as faded.
273         uint32_t alpha = base_pixel & RGBA_ALPHA;
274         uint32_t new_pixel = base_pixel - ((alpha / 2) & RGBA_ALPHA);
275         out->set_pixel_at(x, y, new_pixel);
276       }
277     }
278   }
279 
280   return same;
281 }
282 
DiffImages(const std::string & binary_name,const std::string & file1,const std::string & file2,const std::string & out_file)283 int DiffImages(const std::string& binary_name,
284                const std::string& file1,
285                const std::string& file2,
286                const std::string& out_file) {
287   Image actual_image;
288   Image baseline_image;
289 
290   if (!actual_image.CreateFromFilename(file1)) {
291     fprintf(stderr, "%s: Unable to open file \"%s\"\n", binary_name.c_str(),
292             file1.c_str());
293     return kStatusError;
294   }
295   if (!baseline_image.CreateFromFilename(file2)) {
296     fprintf(stderr, "%s: Unable to open file \"%s\"\n", binary_name.c_str(),
297             file2.c_str());
298     return kStatusError;
299   }
300 
301   Image diff_image;
302   bool same = CreateImageDiff(baseline_image, actual_image, &diff_image);
303   if (same)
304     return kStatusSame;
305 
306   std::vector<uint8_t> png_encoding = image_diff_png::EncodeRGBAPNG(
307       diff_image.span(), diff_image.w(), diff_image.h(), diff_image.w() * 4);
308   if (png_encoding.empty())
309     return kStatusError;
310 
311   FILE* f = fopen(out_file.c_str(), "wb");
312   if (!f)
313     return kStatusError;
314 
315   size_t size = png_encoding.size();
316   char* ptr = reinterpret_cast<char*>(&png_encoding.front());
317   if (fwrite(ptr, 1, size, f) != size)
318     return kStatusError;
319 
320   return kStatusDifferent;
321 }
322 
main(int argc,const char * argv[])323 int main(int argc, const char* argv[]) {
324   FXMEM_InitializePartitionAlloc();
325 
326   bool histograms = false;
327   bool produce_diff_image = false;
328   bool reverse_byte_order = false;
329   std::string filename1;
330   std::string filename2;
331   std::string diff_filename;
332 
333   // Strip the path from the first arg
334   const char* last_separator = strrchr(argv[0], PATH_SEPARATOR);
335   std::string binary_name = last_separator ? last_separator + 1 : argv[0];
336 
337   int i;
338   for (i = 1; i < argc; ++i) {
339     const char* arg = argv[i];
340     if (strstr(arg, "--") != arg)
341       break;
342     if (strcmp(arg, "--histogram") == 0) {
343       histograms = true;
344     } else if (strcmp(arg, "--diff") == 0) {
345       produce_diff_image = true;
346     } else if (strcmp(arg, "--reverse-byte-order") == 0) {
347       reverse_byte_order = true;
348     }
349   }
350   if (i < argc)
351     filename1 = argv[i++];
352   if (i < argc)
353     filename2 = argv[i++];
354   if (i < argc)
355     diff_filename = argv[i++];
356 
357   if (produce_diff_image) {
358     if (!diff_filename.empty()) {
359       return DiffImages(binary_name, filename1, filename2, diff_filename);
360     }
361   } else if (!filename2.empty()) {
362     return CompareImages(binary_name, filename1, filename2, histograms,
363                          reverse_byte_order);
364   }
365 
366   PrintHelp(binary_name);
367   return kStatusError;
368 }
369