Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 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> |
Tom Sepez | 49d805b | 2015-03-16 14:58:06 -0700 | [diff] [blame] | 12 | #include <stdint.h> |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 13 | #include <stdio.h> |
| 14 | #include <string.h> |
| 15 | |
| 16 | #include <algorithm> |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 17 | #include <iostream> |
| 18 | #include <map> |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
Dan Sinclair | efbc191 | 2016-02-17 16:54:43 -0500 | [diff] [blame] | 22 | #include "samples/image_diff_png.h" |
Lei Zhang | 8241df7 | 2015-11-06 14:38:48 -0800 | [diff] [blame] | 23 | #include "third_party/base/logging.h" |
| 24 | #include "third_party/base/numerics/safe_conversions.h" |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 25 | |
| 26 | #if defined(OS_WIN) |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 27 | #include <windows.h> |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 28 | #endif |
| 29 | |
| 30 | // Return codes used by this utility. |
| 31 | static const int kStatusSame = 0; |
| 32 | static const int kStatusDifferent = 1; |
| 33 | static const int kStatusError = 2; |
| 34 | |
| 35 | // Color codes. |
| 36 | static const uint32_t RGBA_RED = 0x000000ff; |
| 37 | static const uint32_t RGBA_ALPHA = 0xff000000; |
| 38 | |
| 39 | class 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 Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 74 | const size_t kBufSize = 1024; |
| 75 | unsigned char buf[kBufSize]; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 76 | size_t num_read = 0; |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 77 | while ((num_read = fread(buf, 1, kBufSize, f)) > 0) { |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 78 | compressed.insert(compressed.end(), buf, buf + num_read); |
| 79 | } |
| 80 | |
| 81 | fclose(f); |
| 82 | |
Lei Zhang | 9ba6a14 | 2015-12-22 14:57:45 -0800 | [diff] [blame] | 83 | if (!image_diff_png::DecodePNG(compressed.data(), compressed.size(), &data_, |
| 84 | &w_, &h_)) { |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 85 | 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 Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 98 | if (!pixel_in_bounds(x, y)) |
| 99 | return 0; |
| 100 | return *reinterpret_cast<const uint32_t*>(&(data_[pixel_address(x, y)])); |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 103 | 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 Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | private: |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 112 | 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 Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 121 | |
| 122 | std::vector<unsigned char> data_; |
| 123 | }; |
| 124 | |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 125 | float 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 | |
| 137 | void 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 Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 152 | float 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 Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 158 | for (int y = 0; y < h; ++y) { |
| 159 | for (int x = 0; x < w; ++x) { |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 160 | if (baseline.pixel_at(x, y) != actual.pixel_at(x, y)) |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 161 | ++pixels_different; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 165 | CountImageSizeMismatchAsPixelDifference(baseline, actual, &pixels_different); |
| 166 | return CalculateDifferencePercentage(actual, pixels_different); |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 169 | float 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 Sepez | e47e0c9 | 2017-04-26 10:55:54 -0700 | [diff] [blame] | 178 | std::map<uint32_t, int32_t> baseline_histogram; |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 179 | for (int y = 0; y < h; ++y) { |
| 180 | for (int x = 0; x < w; ++x) { |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 181 | // hash_map operator[] inserts a 0 (default constructor) if key not found. |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 182 | ++baseline_histogram[baseline.pixel_at(x, y)]; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | // Compute pixels different in the histogram of the overlap. |
| 187 | int pixels_different = 0; |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 188 | for (int y = 0; y < h; ++y) { |
| 189 | for (int x = 0; x < w; ++x) { |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 190 | uint32_t actual_rgba = actual.pixel_at(x, y); |
Tom Sepez | e47e0c9 | 2017-04-26 10:55:54 -0700 | [diff] [blame] | 191 | auto it = baseline_histogram.find(actual_rgba); |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 192 | if (it != baseline_histogram.end() && it->second > 0) |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 193 | --it->second; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 194 | else |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 195 | ++pixels_different; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 199 | CountImageSizeMismatchAsPixelDifference(baseline, actual, &pixels_different); |
| 200 | return CalculateDifferencePercentage(actual, pixels_different); |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void 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 | |
| 215 | int 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 Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 236 | const char* const diff_name = compare_histograms ? "exact diff" : "diff"; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 237 | float percent = PercentageDifferent(actual_image, baseline_image); |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 238 | const char* const passed = percent > 0.0 ? "failed" : "passed"; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 239 | printf("%s: %01.2f%% %s\n", diff_name, percent, passed); |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 240 | |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 241 | 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 Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | bool 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 Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 258 | for (int y = 0; y < h; ++y) { |
| 259 | for (int x = 0; x < w; ++x) { |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 260 | 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 | |
| 277 | int 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 Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 302 | FILE* f = fopen(out_file.c_str(), "wb"); |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 303 | if (!f) |
| 304 | return kStatusError; |
| 305 | |
| 306 | size_t size = png_encoding.size(); |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 307 | char* ptr = reinterpret_cast<char*>(&png_encoding.front()); |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 308 | if (fwrite(ptr, 1, size, f) != size) |
| 309 | return kStatusError; |
| 310 | |
| 311 | return kStatusDifferent; |
| 312 | } |
| 313 | |
| 314 | int 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 Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 332 | 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 Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 338 | |
| 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 | } |