blob: 806e0c84d510eeb8dc8ed18cba35a0721b2ac802 [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
Chris Palmer7726a582017-06-22 10:10:17 -070022#include "core/fxcrt/fx_memory.h"
Lei Zhang143959d2017-06-22 12:20:58 -070023#include "testing/image_diff/image_diff_png.h"
Lei Zhang8241df72015-11-06 14:38:48 -080024#include "third_party/base/logging.h"
25#include "third_party/base/numerics/safe_conversions.h"
Tom Sepezaf18cb32015-02-05 15:06:01 -080026
27#if defined(OS_WIN)
Lei Zhangcca5b762015-11-05 15:27:18 -080028#include <windows.h>
Tom Sepezaf18cb32015-02-05 15:06:01 -080029#endif
30
31// Return codes used by this utility.
32static const int kStatusSame = 0;
33static const int kStatusDifferent = 1;
34static const int kStatusError = 2;
35
36// Color codes.
37static const uint32_t RGBA_RED = 0x000000ff;
38static const uint32_t RGBA_ALPHA = 0xff000000;
39
40class Image {
41 public:
Lei Zhang143959d2017-06-22 12:20:58 -070042 Image() : w_(0), h_(0) {}
43 Image(const Image& image) : w_(image.w_), h_(image.h_), data_(image.data_) {}
Tom Sepezaf18cb32015-02-05 15:06:01 -080044
Lei Zhang143959d2017-06-22 12:20:58 -070045 bool has_image() const { return w_ > 0 && h_ > 0; }
46 int w() const { return w_; }
47 int h() const { return h_; }
48 const unsigned char* data() const { return &data_.front(); }
Tom Sepezaf18cb32015-02-05 15:06:01 -080049
50 // Creates the image from the given filename on disk, and returns true on
51 // success.
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;
Lei Zhangcca5b762015-11-05 15:27:18 -080058 const size_t kBufSize = 1024;
59 unsigned char buf[kBufSize];
Tom Sepezaf18cb32015-02-05 15:06:01 -080060 size_t num_read = 0;
Lei Zhangcca5b762015-11-05 15:27:18 -080061 while ((num_read = fread(buf, 1, kBufSize, f)) > 0) {
Tom Sepezaf18cb32015-02-05 15:06:01 -080062 compressed.insert(compressed.end(), buf, buf + num_read);
63 }
64
65 fclose(f);
66
Lei Zhang9ba6a142015-12-22 14:57:45 -080067 if (!image_diff_png::DecodePNG(compressed.data(), compressed.size(), &data_,
68 &w_, &h_)) {
Tom Sepezaf18cb32015-02-05 15:06:01 -080069 Clear();
70 return false;
71 }
72 return true;
73 }
74
75 void Clear() {
76 w_ = h_ = 0;
77 data_.clear();
78 }
79
80 // Returns the RGBA value of the pixel at the given location
81 uint32_t pixel_at(int x, int y) const {
Lei Zhangcca5b762015-11-05 15:27:18 -080082 if (!pixel_in_bounds(x, y))
83 return 0;
84 return *reinterpret_cast<const uint32_t*>(&(data_[pixel_address(x, y)]));
Tom Sepezaf18cb32015-02-05 15:06:01 -080085 }
86
Lei Zhangcca5b762015-11-05 15:27:18 -080087 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;
Tom Sepezaf18cb32015-02-05 15:06:01 -080093 }
94
95 private:
Lei Zhangcca5b762015-11-05 15:27:18 -080096 bool pixel_in_bounds(int x, int y) const {
97 return x >= 0 && x < w_ && y >= 0 && y < h_;
98 }
99
100 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_;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800105
106 std::vector<unsigned char> data_;
107};
108
Lei Zhangcca5b762015-11-05 15:27:18 -0800109float 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
121void 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
Tom Sepezaf18cb32015-02-05 15:06:01 -0800136float 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;
Lei Zhangcca5b762015-11-05 15:27:18 -0800142 for (int y = 0; y < h; ++y) {
143 for (int x = 0; x < w; ++x) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800144 if (baseline.pixel_at(x, y) != actual.pixel_at(x, y))
Lei Zhangcca5b762015-11-05 15:27:18 -0800145 ++pixels_different;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800146 }
147 }
148
Lei Zhangcca5b762015-11-05 15:27:18 -0800149 CountImageSizeMismatchAsPixelDifference(baseline, actual, &pixels_different);
150 return CalculateDifferencePercentage(actual, pixels_different);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800151}
152
Tom Sepezaf18cb32015-02-05 15:06:01 -0800153float 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.
Tom Sepeze47e0c92017-04-26 10:55:54 -0700162 std::map<uint32_t, int32_t> baseline_histogram;
Lei Zhangcca5b762015-11-05 15:27:18 -0800163 for (int y = 0; y < h; ++y) {
164 for (int x = 0; x < w; ++x) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800165 // hash_map operator[] inserts a 0 (default constructor) if key not found.
Lei Zhangcca5b762015-11-05 15:27:18 -0800166 ++baseline_histogram[baseline.pixel_at(x, y)];
Tom Sepezaf18cb32015-02-05 15:06:01 -0800167 }
168 }
169
170 // Compute pixels different in the histogram of the overlap.
171 int pixels_different = 0;
Lei Zhangcca5b762015-11-05 15:27:18 -0800172 for (int y = 0; y < h; ++y) {
173 for (int x = 0; x < w; ++x) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800174 uint32_t actual_rgba = actual.pixel_at(x, y);
Tom Sepeze47e0c92017-04-26 10:55:54 -0700175 auto it = baseline_histogram.find(actual_rgba);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800176 if (it != baseline_histogram.end() && it->second > 0)
Lei Zhangcca5b762015-11-05 15:27:18 -0800177 --it->second;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800178 else
Lei Zhangcca5b762015-11-05 15:27:18 -0800179 ++pixels_different;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800180 }
181 }
182
Lei Zhangcca5b762015-11-05 15:27:18 -0800183 CountImageSizeMismatchAsPixelDifference(baseline, actual, &pixels_different);
184 return CalculateDifferencePercentage(actual, pixels_different);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800185}
186
187void PrintHelp() {
Lei Zhang143959d2017-06-22 12:20:58 -0700188 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");
Tom Sepezaf18cb32015-02-05 15:06:01 -0800198}
199
200int 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
Lei Zhangcca5b762015-11-05 15:27:18 -0800221 const char* const diff_name = compare_histograms ? "exact diff" : "diff";
Tom Sepezaf18cb32015-02-05 15:06:01 -0800222 float percent = PercentageDifferent(actual_image, baseline_image);
Lei Zhangcca5b762015-11-05 15:27:18 -0800223 const char* const passed = percent > 0.0 ? "failed" : "passed";
Tom Sepezaf18cb32015-02-05 15:06:01 -0800224 printf("%s: %01.2f%% %s\n", diff_name, percent, passed);
Lei Zhangcca5b762015-11-05 15:27:18 -0800225
Tom Sepezaf18cb32015-02-05 15:06:01 -0800226 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;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800233}
234
235bool 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.
Lei Zhangcca5b762015-11-05 15:27:18 -0800243 for (int y = 0; y < h; ++y) {
244 for (int x = 0; x < w; ++x) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800245 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
262int 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;
Lei Zhang143959d2017-06-22 12:20:58 -0700283 image_diff_png::EncodeRGBAPNG(diff_image.data(), diff_image.w(),
284 diff_image.h(), diff_image.w() * 4,
285 &png_encoding);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800286
Lei Zhangcca5b762015-11-05 15:27:18 -0800287 FILE* f = fopen(out_file.c_str(), "wb");
Tom Sepezaf18cb32015-02-05 15:06:01 -0800288 if (!f)
289 return kStatusError;
290
291 size_t size = png_encoding.size();
Lei Zhangcca5b762015-11-05 15:27:18 -0800292 char* ptr = reinterpret_cast<char*>(&png_encoding.front());
Tom Sepezaf18cb32015-02-05 15:06:01 -0800293 if (fwrite(ptr, 1, size, f) != size)
294 return kStatusError;
295
296 return kStatusDifferent;
297}
298
299int main(int argc, const char* argv[]) {
Chris Palmer7726a582017-06-22 10:10:17 -0700300 FXMEM_InitializePartitionAlloc();
301
Tom Sepezaf18cb32015-02-05 15:06:01 -0800302 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 }
Lei Zhangcca5b762015-11-05 15:27:18 -0800319 if (i < argc)
320 filename1 = argv[i++];
321 if (i < argc)
322 filename2 = argv[i++];
323 if (i < argc)
324 diff_filename = argv[i++];
Tom Sepezaf18cb32015-02-05 15:06:01 -0800325
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}