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