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 | |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 22 | #include "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 | |
| 169 | // FIXME: Replace with unordered_map when available. |
| 170 | typedef std::map<uint32_t, int32_t> RgbaToCountMap; |
| 171 | |
| 172 | float 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 Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 182 | for (int y = 0; y < h; ++y) { |
| 183 | for (int x = 0; x < w; ++x) { |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 184 | // hash_map operator[] inserts a 0 (default constructor) if key not found. |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 185 | ++baseline_histogram[baseline.pixel_at(x, y)]; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | // Compute pixels different in the histogram of the overlap. |
| 190 | int pixels_different = 0; |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 191 | for (int y = 0; y < h; ++y) { |
| 192 | for (int x = 0; x < w; ++x) { |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 193 | 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 Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 196 | --it->second; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 197 | else |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 198 | ++pixels_different; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 202 | CountImageSizeMismatchAsPixelDifference(baseline, actual, &pixels_different); |
| 203 | return CalculateDifferencePercentage(actual, pixels_different); |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void 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 | |
| 218 | int 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 Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 239 | const char* const diff_name = compare_histograms ? "exact diff" : "diff"; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 240 | float percent = PercentageDifferent(actual_image, baseline_image); |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 241 | const char* const passed = percent > 0.0 ? "failed" : "passed"; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 242 | printf("%s: %01.2f%% %s\n", diff_name, percent, passed); |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 243 | |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 244 | 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 Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | bool 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 Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 261 | for (int y = 0; y < h; ++y) { |
| 262 | for (int x = 0; x < w; ++x) { |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 263 | 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 | |
| 280 | int 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 Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 305 | FILE* f = fopen(out_file.c_str(), "wb"); |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 306 | if (!f) |
| 307 | return kStatusError; |
| 308 | |
| 309 | size_t size = png_encoding.size(); |
Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 310 | char* ptr = reinterpret_cast<char*>(&png_encoding.front()); |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 311 | if (fwrite(ptr, 1, size, f) != size) |
| 312 | return kStatusError; |
| 313 | |
| 314 | return kStatusDifferent; |
| 315 | } |
| 316 | |
| 317 | int 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 Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 335 | 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 Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 341 | |
| 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 | } |