blob: 21460f5a62025601bfa7dfd2514c941848720cda [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"
Dan Sinclairefbc1912016-02-17 16:54:43 -050023#include "samples/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:
42 Image() : w_(0), h_(0) {
43 }
44
45 Image(const Image& image)
46 : w_(image.w_),
47 h_(image.h_),
48 data_(image.data_) {
49 }
50
51 bool has_image() const {
52 return w_ > 0 && h_ > 0;
53 }
54
55 int w() const {
56 return w_;
57 }
58
59 int h() const {
60 return h_;
61 }
62
63 const unsigned char* data() const {
64 return &data_.front();
65 }
66
67 // Creates the image from the given filename on disk, and returns true on
68 // success.
69 bool CreateFromFilename(const std::string& path) {
70 FILE* f = fopen(path.c_str(), "rb");
71 if (!f)
72 return false;
73
74 std::vector<unsigned char> compressed;
Lei Zhangcca5b762015-11-05 15:27:18 -080075 const size_t kBufSize = 1024;
76 unsigned char buf[kBufSize];
Tom Sepezaf18cb32015-02-05 15:06:01 -080077 size_t num_read = 0;
Lei Zhangcca5b762015-11-05 15:27:18 -080078 while ((num_read = fread(buf, 1, kBufSize, f)) > 0) {
Tom Sepezaf18cb32015-02-05 15:06:01 -080079 compressed.insert(compressed.end(), buf, buf + num_read);
80 }
81
82 fclose(f);
83
Lei Zhang9ba6a142015-12-22 14:57:45 -080084 if (!image_diff_png::DecodePNG(compressed.data(), compressed.size(), &data_,
85 &w_, &h_)) {
Tom Sepezaf18cb32015-02-05 15:06:01 -080086 Clear();
87 return false;
88 }
89 return true;
90 }
91
92 void Clear() {
93 w_ = h_ = 0;
94 data_.clear();
95 }
96
97 // Returns the RGBA value of the pixel at the given location
98 uint32_t pixel_at(int x, int y) const {
Lei Zhangcca5b762015-11-05 15:27:18 -080099 if (!pixel_in_bounds(x, y))
100 return 0;
101 return *reinterpret_cast<const uint32_t*>(&(data_[pixel_address(x, y)]));
Tom Sepezaf18cb32015-02-05 15:06:01 -0800102 }
103
Lei Zhangcca5b762015-11-05 15:27:18 -0800104 void set_pixel_at(int x, int y, uint32_t color) {
105 if (!pixel_in_bounds(x, y))
106 return;
107
108 void* addr = &data_[pixel_address(x, y)];
109 *reinterpret_cast<uint32_t*>(addr) = color;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800110 }
111
112 private:
Lei Zhangcca5b762015-11-05 15:27:18 -0800113 bool pixel_in_bounds(int x, int y) const {
114 return x >= 0 && x < w_ && y >= 0 && y < h_;
115 }
116
117 size_t pixel_address(int x, int y) const { return (y * w_ + x) * 4; }
118
119 // Pixel dimensions of the image.
120 int w_;
121 int h_;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800122
123 std::vector<unsigned char> data_;
124};
125
Lei Zhangcca5b762015-11-05 15:27:18 -0800126float CalculateDifferencePercentage(const Image& actual, int pixels_different) {
127 // Like the WebKit ImageDiff tool, we define percentage different in terms
128 // of the size of the 'actual' bitmap.
129 float total_pixels =
130 static_cast<float>(actual.w()) * static_cast<float>(actual.h());
131 if (total_pixels == 0) {
132 // When the bitmap is empty, they are 100% different.
133 return 100.0f;
134 }
135 return 100.0f * pixels_different / total_pixels;
136}
137
138void CountImageSizeMismatchAsPixelDifference(const Image& baseline,
139 const Image& actual,
140 int* pixels_different) {
141 int w = std::min(baseline.w(), actual.w());
142 int h = std::min(baseline.h(), actual.h());
143
144 // Count pixels that are a difference in size as also being different.
145 int max_w = std::max(baseline.w(), actual.w());
146 int max_h = std::max(baseline.h(), actual.h());
147 // These pixels are off the right side, not including the lower right corner.
148 *pixels_different += (max_w - w) * h;
149 // These pixels are along the bottom, including the lower right corner.
150 *pixels_different += (max_h - h) * max_w;
151}
152
Tom Sepezaf18cb32015-02-05 15:06:01 -0800153float PercentageDifferent(const Image& baseline, const Image& actual) {
154 int w = std::min(baseline.w(), actual.w());
155 int h = std::min(baseline.h(), actual.h());
156
157 // Compute pixels different in the overlap.
158 int pixels_different = 0;
Lei Zhangcca5b762015-11-05 15:27:18 -0800159 for (int y = 0; y < h; ++y) {
160 for (int x = 0; x < w; ++x) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800161 if (baseline.pixel_at(x, y) != actual.pixel_at(x, y))
Lei Zhangcca5b762015-11-05 15:27:18 -0800162 ++pixels_different;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800163 }
164 }
165
Lei Zhangcca5b762015-11-05 15:27:18 -0800166 CountImageSizeMismatchAsPixelDifference(baseline, actual, &pixels_different);
167 return CalculateDifferencePercentage(actual, pixels_different);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800168}
169
Tom Sepezaf18cb32015-02-05 15:06:01 -0800170float HistogramPercentageDifferent(const Image& baseline, const Image& actual) {
171 // TODO(johnme): Consider using a joint histogram instead, as described in
172 // "Comparing Images Using Joint Histograms" by Pass & Zabih
173 // http://www.cs.cornell.edu/~rdz/papers/pz-jms99.pdf
174
175 int w = std::min(baseline.w(), actual.w());
176 int h = std::min(baseline.h(), actual.h());
177
178 // Count occurences of each RGBA pixel value of baseline in the overlap.
Tom Sepeze47e0c92017-04-26 10:55:54 -0700179 std::map<uint32_t, int32_t> baseline_histogram;
Lei Zhangcca5b762015-11-05 15:27:18 -0800180 for (int y = 0; y < h; ++y) {
181 for (int x = 0; x < w; ++x) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800182 // hash_map operator[] inserts a 0 (default constructor) if key not found.
Lei Zhangcca5b762015-11-05 15:27:18 -0800183 ++baseline_histogram[baseline.pixel_at(x, y)];
Tom Sepezaf18cb32015-02-05 15:06:01 -0800184 }
185 }
186
187 // Compute pixels different in the histogram of the overlap.
188 int pixels_different = 0;
Lei Zhangcca5b762015-11-05 15:27:18 -0800189 for (int y = 0; y < h; ++y) {
190 for (int x = 0; x < w; ++x) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800191 uint32_t actual_rgba = actual.pixel_at(x, y);
Tom Sepeze47e0c92017-04-26 10:55:54 -0700192 auto it = baseline_histogram.find(actual_rgba);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800193 if (it != baseline_histogram.end() && it->second > 0)
Lei Zhangcca5b762015-11-05 15:27:18 -0800194 --it->second;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800195 else
Lei Zhangcca5b762015-11-05 15:27:18 -0800196 ++pixels_different;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800197 }
198 }
199
Lei Zhangcca5b762015-11-05 15:27:18 -0800200 CountImageSizeMismatchAsPixelDifference(baseline, actual, &pixels_different);
201 return CalculateDifferencePercentage(actual, pixels_different);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800202}
203
204void PrintHelp() {
205 fprintf(stderr,
206 "Usage:\n"
207 " image_diff [--histogram] <compare file> <reference file>\n"
208 " Compares two files on disk, returning 0 when they are the same;\n"
209 " passing \"--histogram\" additionally calculates a diff of the\n"
210 " RGBA value histograms (which is resistant to shifts in layout)\n"
211 " image_diff --diff <compare file> <reference file> <output file>\n"
212 " Compares two files on disk, outputs an image that visualizes the\n"
213 " difference to <output file>\n");
214}
215
216int CompareImages(const std::string& file1,
217 const std::string& file2,
218 bool compare_histograms) {
219 Image actual_image;
220 Image baseline_image;
221
222 if (!actual_image.CreateFromFilename(file1)) {
223 fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file1.c_str());
224 return kStatusError;
225 }
226 if (!baseline_image.CreateFromFilename(file2)) {
227 fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file2.c_str());
228 return kStatusError;
229 }
230
231 if (compare_histograms) {
232 float percent = HistogramPercentageDifferent(actual_image, baseline_image);
233 const char* passed = percent > 0.0 ? "failed" : "passed";
234 printf("histogram diff: %01.2f%% %s\n", percent, passed);
235 }
236
Lei Zhangcca5b762015-11-05 15:27:18 -0800237 const char* const diff_name = compare_histograms ? "exact diff" : "diff";
Tom Sepezaf18cb32015-02-05 15:06:01 -0800238 float percent = PercentageDifferent(actual_image, baseline_image);
Lei Zhangcca5b762015-11-05 15:27:18 -0800239 const char* const passed = percent > 0.0 ? "failed" : "passed";
Tom Sepezaf18cb32015-02-05 15:06:01 -0800240 printf("%s: %01.2f%% %s\n", diff_name, percent, passed);
Lei Zhangcca5b762015-11-05 15:27:18 -0800241
Tom Sepezaf18cb32015-02-05 15:06:01 -0800242 if (percent > 0.0) {
243 // failure: The WebKit version also writes the difference image to
244 // stdout, which seems excessive for our needs.
245 return kStatusDifferent;
246 }
247 // success
248 return kStatusSame;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800249}
250
251bool CreateImageDiff(const Image& image1, const Image& image2, Image* out) {
252 int w = std::min(image1.w(), image2.w());
253 int h = std::min(image1.h(), image2.h());
254 *out = Image(image1);
255 bool same = (image1.w() == image2.w()) && (image1.h() == image2.h());
256
257 // TODO(estade): do something with the extra pixels if the image sizes
258 // are different.
Lei Zhangcca5b762015-11-05 15:27:18 -0800259 for (int y = 0; y < h; ++y) {
260 for (int x = 0; x < w; ++x) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800261 uint32_t base_pixel = image1.pixel_at(x, y);
262 if (base_pixel != image2.pixel_at(x, y)) {
263 // Set differing pixels red.
264 out->set_pixel_at(x, y, RGBA_RED | RGBA_ALPHA);
265 same = false;
266 } else {
267 // Set same pixels as faded.
268 uint32_t alpha = base_pixel & RGBA_ALPHA;
269 uint32_t new_pixel = base_pixel - ((alpha / 2) & RGBA_ALPHA);
270 out->set_pixel_at(x, y, new_pixel);
271 }
272 }
273 }
274
275 return same;
276}
277
278int DiffImages(const std::string& file1,
279 const std::string& file2,
280 const std::string& out_file) {
281 Image actual_image;
282 Image baseline_image;
283
284 if (!actual_image.CreateFromFilename(file1)) {
285 fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file1.c_str());
286 return kStatusError;
287 }
288 if (!baseline_image.CreateFromFilename(file2)) {
289 fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file2.c_str());
290 return kStatusError;
291 }
292
293 Image diff_image;
294 bool same = CreateImageDiff(baseline_image, actual_image, &diff_image);
295 if (same)
296 return kStatusSame;
297
298 std::vector<unsigned char> png_encoding;
299 image_diff_png::EncodeRGBAPNG(
300 diff_image.data(), diff_image.w(), diff_image.h(),
301 diff_image.w() * 4, &png_encoding);
302
Lei Zhangcca5b762015-11-05 15:27:18 -0800303 FILE* f = fopen(out_file.c_str(), "wb");
Tom Sepezaf18cb32015-02-05 15:06:01 -0800304 if (!f)
305 return kStatusError;
306
307 size_t size = png_encoding.size();
Lei Zhangcca5b762015-11-05 15:27:18 -0800308 char* ptr = reinterpret_cast<char*>(&png_encoding.front());
Tom Sepezaf18cb32015-02-05 15:06:01 -0800309 if (fwrite(ptr, 1, size, f) != size)
310 return kStatusError;
311
312 return kStatusDifferent;
313}
314
315int main(int argc, const char* argv[]) {
Chris Palmer7726a582017-06-22 10:10:17 -0700316 FXMEM_InitializePartitionAlloc();
317
Tom Sepezaf18cb32015-02-05 15:06:01 -0800318 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}