blob: d24652910fc3db53fd5eae20b8c977eb4f37a314 [file] [log] [blame]
Tom Sepezaf18cb32015-02-05 15:06:01 -08001// 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>
Tom Sepez49d805b2015-03-16 14:58:06 -070012#include <stdint.h>
Tom Sepezaf18cb32015-02-05 15:06:01 -080013#include <stdio.h>
14#include <string.h>
15
16#include <algorithm>
Tom Sepezaf18cb32015-02-05 15:06:01 -080017#include <iostream>
18#include <map>
19#include <string>
20#include <vector>
21
Tom Sepezaf18cb32015-02-05 15:06:01 -080022#include "image_diff_png.h"
Lei Zhang8241df72015-11-06 14:38:48 -080023#include "third_party/base/logging.h"
24#include "third_party/base/numerics/safe_conversions.h"
Tom Sepezaf18cb32015-02-05 15:06:01 -080025
26#if defined(OS_WIN)
Lei Zhangcca5b762015-11-05 15:27:18 -080027#include <windows.h>
Tom Sepezaf18cb32015-02-05 15:06:01 -080028#endif
29
30// Return codes used by this utility.
31static const int kStatusSame = 0;
32static const int kStatusDifferent = 1;
33static const int kStatusError = 2;
34
35// Color codes.
36static const uint32_t RGBA_RED = 0x000000ff;
37static const uint32_t RGBA_ALPHA = 0xff000000;
38
39class Image {
40 public:
41 Image() : w_(0), h_(0) {
42 }
43
44 Image(const Image& image)
45 : w_(image.w_),
46 h_(image.h_),
47 data_(image.data_) {
48 }
49
50 bool has_image() const {
51 return w_ > 0 && h_ > 0;
52 }
53
54 int w() const {
55 return w_;
56 }
57
58 int h() const {
59 return h_;
60 }
61
62 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.
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;
Lei Zhangcca5b762015-11-05 15:27:18 -080074 const size_t kBufSize = 1024;
75 unsigned char buf[kBufSize];
Tom Sepezaf18cb32015-02-05 15:06:01 -080076 size_t num_read = 0;
Lei Zhangcca5b762015-11-05 15:27:18 -080077 while ((num_read = fread(buf, 1, kBufSize, f)) > 0) {
Tom Sepezaf18cb32015-02-05 15:06:01 -080078 compressed.insert(compressed.end(), buf, buf + num_read);
79 }
80
81 fclose(f);
82
Lei Zhang9ba6a142015-12-22 14:57:45 -080083 if (!image_diff_png::DecodePNG(compressed.data(), compressed.size(), &data_,
84 &w_, &h_)) {
Tom Sepezaf18cb32015-02-05 15:06:01 -080085 Clear();
86 return false;
87 }
88 return true;
89 }
90
91 void Clear() {
92 w_ = h_ = 0;
93 data_.clear();
94 }
95
96 // Returns the RGBA value of the pixel at the given location
97 uint32_t pixel_at(int x, int y) const {
Lei Zhangcca5b762015-11-05 15:27:18 -080098 if (!pixel_in_bounds(x, y))
99 return 0;
100 return *reinterpret_cast<const uint32_t*>(&(data_[pixel_address(x, y)]));
Tom Sepezaf18cb32015-02-05 15:06:01 -0800101 }
102
Lei Zhangcca5b762015-11-05 15:27:18 -0800103 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;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800109 }
110
111 private:
Lei Zhangcca5b762015-11-05 15:27:18 -0800112 bool pixel_in_bounds(int x, int y) const {
113 return x >= 0 && x < w_ && y >= 0 && y < h_;
114 }
115
116 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_;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800121
122 std::vector<unsigned char> data_;
123};
124
Lei Zhangcca5b762015-11-05 15:27:18 -0800125float 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
137void 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
Tom Sepezaf18cb32015-02-05 15:06:01 -0800152float 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;
Lei Zhangcca5b762015-11-05 15:27:18 -0800158 for (int y = 0; y < h; ++y) {
159 for (int x = 0; x < w; ++x) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800160 if (baseline.pixel_at(x, y) != actual.pixel_at(x, y))
Lei Zhangcca5b762015-11-05 15:27:18 -0800161 ++pixels_different;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800162 }
163 }
164
Lei Zhangcca5b762015-11-05 15:27:18 -0800165 CountImageSizeMismatchAsPixelDifference(baseline, actual, &pixels_different);
166 return CalculateDifferencePercentage(actual, pixels_different);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800167}
168
169// FIXME: Replace with unordered_map when available.
170typedef std::map<uint32_t, int32_t> RgbaToCountMap;
171
172float 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;
Lei Zhangcca5b762015-11-05 15:27:18 -0800182 for (int y = 0; y < h; ++y) {
183 for (int x = 0; x < w; ++x) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800184 // hash_map operator[] inserts a 0 (default constructor) if key not found.
Lei Zhangcca5b762015-11-05 15:27:18 -0800185 ++baseline_histogram[baseline.pixel_at(x, y)];
Tom Sepezaf18cb32015-02-05 15:06:01 -0800186 }
187 }
188
189 // Compute pixels different in the histogram of the overlap.
190 int pixels_different = 0;
Lei Zhangcca5b762015-11-05 15:27:18 -0800191 for (int y = 0; y < h; ++y) {
192 for (int x = 0; x < w; ++x) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800193 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)
Lei Zhangcca5b762015-11-05 15:27:18 -0800196 --it->second;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800197 else
Lei Zhangcca5b762015-11-05 15:27:18 -0800198 ++pixels_different;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800199 }
200 }
201
Lei Zhangcca5b762015-11-05 15:27:18 -0800202 CountImageSizeMismatchAsPixelDifference(baseline, actual, &pixels_different);
203 return CalculateDifferencePercentage(actual, pixels_different);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800204}
205
206void 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
218int 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
Lei Zhangcca5b762015-11-05 15:27:18 -0800239 const char* const diff_name = compare_histograms ? "exact diff" : "diff";
Tom Sepezaf18cb32015-02-05 15:06:01 -0800240 float percent = PercentageDifferent(actual_image, baseline_image);
Lei Zhangcca5b762015-11-05 15:27:18 -0800241 const char* const passed = percent > 0.0 ? "failed" : "passed";
Tom Sepezaf18cb32015-02-05 15:06:01 -0800242 printf("%s: %01.2f%% %s\n", diff_name, percent, passed);
Lei Zhangcca5b762015-11-05 15:27:18 -0800243
Tom Sepezaf18cb32015-02-05 15:06:01 -0800244 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;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800251}
252
253bool 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.
Lei Zhangcca5b762015-11-05 15:27:18 -0800261 for (int y = 0; y < h; ++y) {
262 for (int x = 0; x < w; ++x) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800263 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
280int 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
Lei Zhangcca5b762015-11-05 15:27:18 -0800305 FILE* f = fopen(out_file.c_str(), "wb");
Tom Sepezaf18cb32015-02-05 15:06:01 -0800306 if (!f)
307 return kStatusError;
308
309 size_t size = png_encoding.size();
Lei Zhangcca5b762015-11-05 15:27:18 -0800310 char* ptr = reinterpret_cast<char*>(&png_encoding.front());
Tom Sepezaf18cb32015-02-05 15:06:01 -0800311 if (fwrite(ptr, 1, size, f) != size)
312 return kStatusError;
313
314 return kStatusDifferent;
315}
316
317int 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 }
Lei Zhangcca5b762015-11-05 15:27:18 -0800335 if (i < argc)
336 filename1 = argv[i++];
337 if (i < argc)
338 filename2 = argv[i++];
339 if (i < argc)
340 diff_filename = argv[i++];
Tom Sepezaf18cb32015-02-05 15:06:01 -0800341
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}