| 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 | |
| Chris Palmer | 7726a58 | 2017-06-22 10:10:17 -0700 | [diff] [blame] | 22 | #include "core/fxcrt/fx_memory.h" |
| Lei Zhang | 143959d | 2017-06-22 12:20:58 -0700 | [diff] [blame^] | 23 | #include "testing/image_diff/image_diff_png.h" |
| Lei Zhang | 8241df7 | 2015-11-06 14:38:48 -0800 | [diff] [blame] | 24 | #include "third_party/base/logging.h" |
| 25 | #include "third_party/base/numerics/safe_conversions.h" |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 26 | |
| 27 | #if defined(OS_WIN) |
| Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 28 | #include <windows.h> |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 29 | #endif |
| 30 | |
| 31 | // Return codes used by this utility. |
| 32 | static const int kStatusSame = 0; |
| 33 | static const int kStatusDifferent = 1; |
| 34 | static const int kStatusError = 2; |
| 35 | |
| 36 | // Color codes. |
| 37 | static const uint32_t RGBA_RED = 0x000000ff; |
| 38 | static const uint32_t RGBA_ALPHA = 0xff000000; |
| 39 | |
| 40 | class Image { |
| 41 | public: |
| Lei Zhang | 143959d | 2017-06-22 12:20:58 -0700 | [diff] [blame^] | 42 | Image() : w_(0), h_(0) {} |
| 43 | Image(const Image& image) : w_(image.w_), h_(image.h_), data_(image.data_) {} |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 44 | |
| Lei Zhang | 143959d | 2017-06-22 12:20:58 -0700 | [diff] [blame^] | 45 | 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 Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 49 | |
| 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 Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 58 | const size_t kBufSize = 1024; |
| 59 | unsigned char buf[kBufSize]; |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 60 | size_t num_read = 0; |
| Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 61 | while ((num_read = fread(buf, 1, kBufSize, f)) > 0) { |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 62 | compressed.insert(compressed.end(), buf, buf + num_read); |
| 63 | } |
| 64 | |
| 65 | fclose(f); |
| 66 | |
| Lei Zhang | 9ba6a14 | 2015-12-22 14:57:45 -0800 | [diff] [blame] | 67 | if (!image_diff_png::DecodePNG(compressed.data(), compressed.size(), &data_, |
| 68 | &w_, &h_)) { |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 69 | 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 Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 82 | if (!pixel_in_bounds(x, y)) |
| 83 | return 0; |
| 84 | return *reinterpret_cast<const uint32_t*>(&(data_[pixel_address(x, y)])); |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 87 | 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 Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | private: |
| Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 96 | 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 Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 105 | |
| 106 | std::vector<unsigned char> data_; |
| 107 | }; |
| 108 | |
| Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 109 | float 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 | |
| 121 | void 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 Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 136 | float 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 Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 142 | for (int y = 0; y < h; ++y) { |
| 143 | for (int x = 0; x < w; ++x) { |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 144 | if (baseline.pixel_at(x, y) != actual.pixel_at(x, y)) |
| Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 145 | ++pixels_different; |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 149 | CountImageSizeMismatchAsPixelDifference(baseline, actual, &pixels_different); |
| 150 | return CalculateDifferencePercentage(actual, pixels_different); |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 153 | float 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 Sepez | e47e0c9 | 2017-04-26 10:55:54 -0700 | [diff] [blame] | 162 | std::map<uint32_t, int32_t> baseline_histogram; |
| Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 163 | for (int y = 0; y < h; ++y) { |
| 164 | for (int x = 0; x < w; ++x) { |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 165 | // hash_map operator[] inserts a 0 (default constructor) if key not found. |
| Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 166 | ++baseline_histogram[baseline.pixel_at(x, y)]; |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | // Compute pixels different in the histogram of the overlap. |
| 171 | int pixels_different = 0; |
| Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 172 | for (int y = 0; y < h; ++y) { |
| 173 | for (int x = 0; x < w; ++x) { |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 174 | uint32_t actual_rgba = actual.pixel_at(x, y); |
| Tom Sepez | e47e0c9 | 2017-04-26 10:55:54 -0700 | [diff] [blame] | 175 | auto it = baseline_histogram.find(actual_rgba); |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 176 | if (it != baseline_histogram.end() && it->second > 0) |
| Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 177 | --it->second; |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 178 | else |
| Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 179 | ++pixels_different; |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 183 | CountImageSizeMismatchAsPixelDifference(baseline, actual, &pixels_different); |
| 184 | return CalculateDifferencePercentage(actual, pixels_different); |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void PrintHelp() { |
| Lei Zhang | 143959d | 2017-06-22 12:20:58 -0700 | [diff] [blame^] | 188 | 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 Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | int 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 Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 221 | const char* const diff_name = compare_histograms ? "exact diff" : "diff"; |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 222 | float percent = PercentageDifferent(actual_image, baseline_image); |
| Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 223 | const char* const passed = percent > 0.0 ? "failed" : "passed"; |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 224 | printf("%s: %01.2f%% %s\n", diff_name, percent, passed); |
| Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 225 | |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 226 | 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 Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | bool 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 Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 243 | for (int y = 0; y < h; ++y) { |
| 244 | for (int x = 0; x < w; ++x) { |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 245 | 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 | |
| 262 | int 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 Zhang | 143959d | 2017-06-22 12:20:58 -0700 | [diff] [blame^] | 283 | image_diff_png::EncodeRGBAPNG(diff_image.data(), diff_image.w(), |
| 284 | diff_image.h(), diff_image.w() * 4, |
| 285 | &png_encoding); |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 286 | |
| Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 287 | FILE* f = fopen(out_file.c_str(), "wb"); |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 288 | if (!f) |
| 289 | return kStatusError; |
| 290 | |
| 291 | size_t size = png_encoding.size(); |
| Lei Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 292 | char* ptr = reinterpret_cast<char*>(&png_encoding.front()); |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 293 | if (fwrite(ptr, 1, size, f) != size) |
| 294 | return kStatusError; |
| 295 | |
| 296 | return kStatusDifferent; |
| 297 | } |
| 298 | |
| 299 | int main(int argc, const char* argv[]) { |
| Chris Palmer | 7726a58 | 2017-06-22 10:10:17 -0700 | [diff] [blame] | 300 | FXMEM_InitializePartitionAlloc(); |
| 301 | |
| Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 302 | 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 Zhang | cca5b76 | 2015-11-05 15:27:18 -0800 | [diff] [blame] | 319 | 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 Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 325 | |
| 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 | } |