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