blob: 70c74c951c78e139683c7ba3e29725765703c4a2 [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
Dan Sinclairefbc1912016-02-17 16:54:43 -050022#include "samples/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
Tom Sepezaf18cb32015-02-05 15:06:01 -0800169float HistogramPercentageDifferent(const Image& baseline, const Image& actual) {
170 // TODO(johnme): Consider using a joint histogram instead, as described in
171 // "Comparing Images Using Joint Histograms" by Pass & Zabih
172 // http://www.cs.cornell.edu/~rdz/papers/pz-jms99.pdf
173
174 int w = std::min(baseline.w(), actual.w());
175 int h = std::min(baseline.h(), actual.h());
176
177 // Count occurences of each RGBA pixel value of baseline in the overlap.
Tom Sepeze47e0c92017-04-26 10:55:54 -0700178 std::map<uint32_t, int32_t> baseline_histogram;
Lei Zhangcca5b762015-11-05 15:27:18 -0800179 for (int y = 0; y < h; ++y) {
180 for (int x = 0; x < w; ++x) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800181 // hash_map operator[] inserts a 0 (default constructor) if key not found.
Lei Zhangcca5b762015-11-05 15:27:18 -0800182 ++baseline_histogram[baseline.pixel_at(x, y)];
Tom Sepezaf18cb32015-02-05 15:06:01 -0800183 }
184 }
185
186 // Compute pixels different in the histogram of the overlap.
187 int pixels_different = 0;
Lei Zhangcca5b762015-11-05 15:27:18 -0800188 for (int y = 0; y < h; ++y) {
189 for (int x = 0; x < w; ++x) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800190 uint32_t actual_rgba = actual.pixel_at(x, y);
Tom Sepeze47e0c92017-04-26 10:55:54 -0700191 auto it = baseline_histogram.find(actual_rgba);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800192 if (it != baseline_histogram.end() && it->second > 0)
Lei Zhangcca5b762015-11-05 15:27:18 -0800193 --it->second;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800194 else
Lei Zhangcca5b762015-11-05 15:27:18 -0800195 ++pixels_different;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800196 }
197 }
198
Lei Zhangcca5b762015-11-05 15:27:18 -0800199 CountImageSizeMismatchAsPixelDifference(baseline, actual, &pixels_different);
200 return CalculateDifferencePercentage(actual, pixels_different);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800201}
202
203void PrintHelp() {
204 fprintf(stderr,
205 "Usage:\n"
206 " image_diff [--histogram] <compare file> <reference file>\n"
207 " Compares two files on disk, returning 0 when they are the same;\n"
208 " passing \"--histogram\" additionally calculates a diff of the\n"
209 " RGBA value histograms (which is resistant to shifts in layout)\n"
210 " image_diff --diff <compare file> <reference file> <output file>\n"
211 " Compares two files on disk, outputs an image that visualizes the\n"
212 " difference to <output file>\n");
213}
214
215int CompareImages(const std::string& file1,
216 const std::string& file2,
217 bool compare_histograms) {
218 Image actual_image;
219 Image baseline_image;
220
221 if (!actual_image.CreateFromFilename(file1)) {
222 fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file1.c_str());
223 return kStatusError;
224 }
225 if (!baseline_image.CreateFromFilename(file2)) {
226 fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file2.c_str());
227 return kStatusError;
228 }
229
230 if (compare_histograms) {
231 float percent = HistogramPercentageDifferent(actual_image, baseline_image);
232 const char* passed = percent > 0.0 ? "failed" : "passed";
233 printf("histogram diff: %01.2f%% %s\n", percent, passed);
234 }
235
Lei Zhangcca5b762015-11-05 15:27:18 -0800236 const char* const diff_name = compare_histograms ? "exact diff" : "diff";
Tom Sepezaf18cb32015-02-05 15:06:01 -0800237 float percent = PercentageDifferent(actual_image, baseline_image);
Lei Zhangcca5b762015-11-05 15:27:18 -0800238 const char* const passed = percent > 0.0 ? "failed" : "passed";
Tom Sepezaf18cb32015-02-05 15:06:01 -0800239 printf("%s: %01.2f%% %s\n", diff_name, percent, passed);
Lei Zhangcca5b762015-11-05 15:27:18 -0800240
Tom Sepezaf18cb32015-02-05 15:06:01 -0800241 if (percent > 0.0) {
242 // failure: The WebKit version also writes the difference image to
243 // stdout, which seems excessive for our needs.
244 return kStatusDifferent;
245 }
246 // success
247 return kStatusSame;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800248}
249
250bool CreateImageDiff(const Image& image1, const Image& image2, Image* out) {
251 int w = std::min(image1.w(), image2.w());
252 int h = std::min(image1.h(), image2.h());
253 *out = Image(image1);
254 bool same = (image1.w() == image2.w()) && (image1.h() == image2.h());
255
256 // TODO(estade): do something with the extra pixels if the image sizes
257 // are different.
Lei Zhangcca5b762015-11-05 15:27:18 -0800258 for (int y = 0; y < h; ++y) {
259 for (int x = 0; x < w; ++x) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800260 uint32_t base_pixel = image1.pixel_at(x, y);
261 if (base_pixel != image2.pixel_at(x, y)) {
262 // Set differing pixels red.
263 out->set_pixel_at(x, y, RGBA_RED | RGBA_ALPHA);
264 same = false;
265 } else {
266 // Set same pixels as faded.
267 uint32_t alpha = base_pixel & RGBA_ALPHA;
268 uint32_t new_pixel = base_pixel - ((alpha / 2) & RGBA_ALPHA);
269 out->set_pixel_at(x, y, new_pixel);
270 }
271 }
272 }
273
274 return same;
275}
276
277int DiffImages(const std::string& file1,
278 const std::string& file2,
279 const std::string& out_file) {
280 Image actual_image;
281 Image baseline_image;
282
283 if (!actual_image.CreateFromFilename(file1)) {
284 fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file1.c_str());
285 return kStatusError;
286 }
287 if (!baseline_image.CreateFromFilename(file2)) {
288 fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file2.c_str());
289 return kStatusError;
290 }
291
292 Image diff_image;
293 bool same = CreateImageDiff(baseline_image, actual_image, &diff_image);
294 if (same)
295 return kStatusSame;
296
297 std::vector<unsigned char> png_encoding;
298 image_diff_png::EncodeRGBAPNG(
299 diff_image.data(), diff_image.w(), diff_image.h(),
300 diff_image.w() * 4, &png_encoding);
301
Lei Zhangcca5b762015-11-05 15:27:18 -0800302 FILE* f = fopen(out_file.c_str(), "wb");
Tom Sepezaf18cb32015-02-05 15:06:01 -0800303 if (!f)
304 return kStatusError;
305
306 size_t size = png_encoding.size();
Lei Zhangcca5b762015-11-05 15:27:18 -0800307 char* ptr = reinterpret_cast<char*>(&png_encoding.front());
Tom Sepezaf18cb32015-02-05 15:06:01 -0800308 if (fwrite(ptr, 1, size, f) != size)
309 return kStatusError;
310
311 return kStatusDifferent;
312}
313
314int main(int argc, const char* argv[]) {
315 bool histograms = false;
316 bool produce_diff_image = false;
317 std::string filename1;
318 std::string filename2;
319 std::string diff_filename;
320
321 int i;
322 for (i = 1; i < argc; ++i) {
323 const char* arg = argv[i];
324 if (strstr(arg, "--") != arg)
325 break;
326 if (strcmp(arg, "--histogram") == 0) {
327 histograms = true;
328 } else if (strcmp(arg, "--diff") == 0) {
329 produce_diff_image = true;
330 }
331 }
Lei Zhangcca5b762015-11-05 15:27:18 -0800332 if (i < argc)
333 filename1 = argv[i++];
334 if (i < argc)
335 filename2 = argv[i++];
336 if (i < argc)
337 diff_filename = argv[i++];
Tom Sepezaf18cb32015-02-05 15:06:01 -0800338
339 if (produce_diff_image) {
340 if (!diff_filename.empty()) {
341 return DiffImages(filename1, filename2, diff_filename);
342 }
343 } else if (!filename2.empty()) {
344 return CompareImages(filename1, filename2, histograms);
345 }
346
347 PrintHelp();
348 return kStatusError;
349}