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 | |
| 22 | #include "../third_party/base/logging.h" |
| 23 | #include "../third_party/base/numerics/safe_conversions.h" |
| 24 | #include "image_diff_png.h" |
| 25 | |
| 26 | #if defined(OS_WIN) |
| 27 | #include "windows.h" |
| 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; |
| 74 | const int buf_size = 1024; |
| 75 | unsigned char buf[buf_size]; |
| 76 | size_t num_read = 0; |
| 77 | while ((num_read = fread(buf, 1, buf_size, f)) > 0) { |
| 78 | compressed.insert(compressed.end(), buf, buf + num_read); |
| 79 | } |
| 80 | |
| 81 | fclose(f); |
| 82 | |
| 83 | if (!image_diff_png::DecodePNG(&compressed[0], compressed.size(), |
| 84 | &data_, &w_, &h_)) { |
| 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 { |
| 98 | if (x >= 0 && x < w_ && y >= 0 && y < h_) |
| 99 | return *reinterpret_cast<const uint32_t*>(&(data_[(y * w_ + x) * 4])); |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | void set_pixel_at(int x, int y, uint32_t color) const { |
| 104 | if (x >= 0 && x < w_ && y >= 0 && y < h_) { |
| 105 | void* addr = &const_cast<unsigned char*>(&data_.front())[(y * w_ + x) * 4]; |
| 106 | *reinterpret_cast<uint32_t*>(addr) = color; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | // pixel dimensions of the image |
| 112 | int w_, h_; |
| 113 | |
| 114 | std::vector<unsigned char> data_; |
| 115 | }; |
| 116 | |
| 117 | float PercentageDifferent(const Image& baseline, const Image& actual) { |
| 118 | int w = std::min(baseline.w(), actual.w()); |
| 119 | int h = std::min(baseline.h(), actual.h()); |
| 120 | |
| 121 | // Compute pixels different in the overlap. |
| 122 | int pixels_different = 0; |
| 123 | for (int y = 0; y < h; y++) { |
| 124 | for (int x = 0; x < w; x++) { |
| 125 | if (baseline.pixel_at(x, y) != actual.pixel_at(x, y)) |
| 126 | pixels_different++; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // Count pixels that are a difference in size as also being different. |
| 131 | int max_w = std::max(baseline.w(), actual.w()); |
| 132 | int max_h = std::max(baseline.h(), actual.h()); |
| 133 | // These pixels are off the right side, not including the lower right corner. |
| 134 | pixels_different += (max_w - w) * h; |
| 135 | // These pixels are along the bottom, including the lower right corner. |
| 136 | pixels_different += (max_h - h) * max_w; |
| 137 | |
| 138 | // Like the WebKit ImageDiff tool, we define percentage different in terms |
| 139 | // of the size of the 'actual' bitmap. |
| 140 | float total_pixels = static_cast<float>(actual.w()) * |
| 141 | static_cast<float>(actual.h()); |
| 142 | if (total_pixels == 0) { |
| 143 | // When the bitmap is empty, they are 100% different. |
| 144 | return 100.0f; |
| 145 | } |
| 146 | return 100.0f * pixels_different / total_pixels; |
| 147 | } |
| 148 | |
| 149 | // FIXME: Replace with unordered_map when available. |
| 150 | typedef std::map<uint32_t, int32_t> RgbaToCountMap; |
| 151 | |
| 152 | float HistogramPercentageDifferent(const Image& baseline, const Image& actual) { |
| 153 | // TODO(johnme): Consider using a joint histogram instead, as described in |
| 154 | // "Comparing Images Using Joint Histograms" by Pass & Zabih |
| 155 | // http://www.cs.cornell.edu/~rdz/papers/pz-jms99.pdf |
| 156 | |
| 157 | int w = std::min(baseline.w(), actual.w()); |
| 158 | int h = std::min(baseline.h(), actual.h()); |
| 159 | |
| 160 | // Count occurences of each RGBA pixel value of baseline in the overlap. |
| 161 | RgbaToCountMap baseline_histogram; |
| 162 | for (int y = 0; y < h; y++) { |
| 163 | for (int x = 0; x < w; x++) { |
| 164 | // hash_map operator[] inserts a 0 (default constructor) if key not found. |
| 165 | baseline_histogram[baseline.pixel_at(x, y)]++; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // Compute pixels different in the histogram of the overlap. |
| 170 | int pixels_different = 0; |
| 171 | for (int y = 0; y < h; y++) { |
| 172 | for (int x = 0; x < w; x++) { |
| 173 | uint32_t actual_rgba = actual.pixel_at(x, y); |
| 174 | RgbaToCountMap::iterator it = baseline_histogram.find(actual_rgba); |
| 175 | if (it != baseline_histogram.end() && it->second > 0) |
| 176 | it->second--; |
| 177 | else |
| 178 | pixels_different++; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // Count pixels that are a difference in size as also being different. |
| 183 | int max_w = std::max(baseline.w(), actual.w()); |
| 184 | int max_h = std::max(baseline.h(), actual.h()); |
| 185 | // These pixels are off the right side, not including the lower right corner. |
| 186 | pixels_different += (max_w - w) * h; |
| 187 | // These pixels are along the bottom, including the lower right corner. |
| 188 | pixels_different += (max_h - h) * max_w; |
| 189 | |
| 190 | // Like the WebKit ImageDiff tool, we define percentage different in terms |
| 191 | // of the size of the 'actual' bitmap. |
| 192 | float total_pixels = static_cast<float>(actual.w()) * |
| 193 | static_cast<float>(actual.h()); |
| 194 | if (total_pixels == 0) { |
| 195 | // When the bitmap is empty, they are 100% different. |
| 196 | return 100.0f; |
| 197 | } |
| 198 | return 100.0f * pixels_different / total_pixels; |
| 199 | } |
| 200 | |
| 201 | void PrintHelp() { |
| 202 | fprintf(stderr, |
| 203 | "Usage:\n" |
| 204 | " image_diff [--histogram] <compare file> <reference file>\n" |
| 205 | " Compares two files on disk, returning 0 when they are the same;\n" |
| 206 | " passing \"--histogram\" additionally calculates a diff of the\n" |
| 207 | " RGBA value histograms (which is resistant to shifts in layout)\n" |
| 208 | " image_diff --diff <compare file> <reference file> <output file>\n" |
| 209 | " Compares two files on disk, outputs an image that visualizes the\n" |
| 210 | " difference to <output file>\n"); |
| 211 | } |
| 212 | |
| 213 | int CompareImages(const std::string& file1, |
| 214 | const std::string& file2, |
| 215 | bool compare_histograms) { |
| 216 | Image actual_image; |
| 217 | Image baseline_image; |
| 218 | |
| 219 | if (!actual_image.CreateFromFilename(file1)) { |
| 220 | fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file1.c_str()); |
| 221 | return kStatusError; |
| 222 | } |
| 223 | if (!baseline_image.CreateFromFilename(file2)) { |
| 224 | fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file2.c_str()); |
| 225 | return kStatusError; |
| 226 | } |
| 227 | |
| 228 | if (compare_histograms) { |
| 229 | float percent = HistogramPercentageDifferent(actual_image, baseline_image); |
| 230 | const char* passed = percent > 0.0 ? "failed" : "passed"; |
| 231 | printf("histogram diff: %01.2f%% %s\n", percent, passed); |
| 232 | } |
| 233 | |
| 234 | const char* diff_name = compare_histograms ? "exact diff" : "diff"; |
| 235 | float percent = PercentageDifferent(actual_image, baseline_image); |
| 236 | const char* passed = percent > 0.0 ? "failed" : "passed"; |
| 237 | printf("%s: %01.2f%% %s\n", diff_name, percent, passed); |
| 238 | if (percent > 0.0) { |
| 239 | // failure: The WebKit version also writes the difference image to |
| 240 | // stdout, which seems excessive for our needs. |
| 241 | return kStatusDifferent; |
| 242 | } |
| 243 | // success |
| 244 | return kStatusSame; |
| 245 | |
| 246 | /* Untested mode that acts like WebKit's image comparator. I wrote this but |
| 247 | decided it's too complicated. We may use it in the future if it looks useful |
| 248 | |
| 249 | char buffer[2048]; |
| 250 | while (fgets(buffer, sizeof(buffer), stdin)) { |
| 251 | |
| 252 | if (strncmp("Content-length: ", buffer, 16) == 0) { |
| 253 | char* context; |
| 254 | strtok_s(buffer, " ", &context); |
| 255 | int image_size = strtol(strtok_s(NULL, " ", &context), NULL, 10); |
| 256 | |
| 257 | bool success = false; |
| 258 | if (image_size > 0 && actual_image.has_image() == 0) { |
| 259 | if (!actual_image.CreateFromStdin(image_size)) { |
| 260 | fputs("Error, input image can't be decoded.\n", stderr); |
| 261 | return 1; |
| 262 | } |
| 263 | } else if (image_size > 0 && baseline_image.has_image() == 0) { |
| 264 | if (!baseline_image.CreateFromStdin(image_size)) { |
| 265 | fputs("Error, baseline image can't be decoded.\n", stderr); |
| 266 | return 1; |
| 267 | } |
| 268 | } else { |
| 269 | fputs("Error, image size must be specified.\n", stderr); |
| 270 | return 1; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | if (actual_image.has_image() && baseline_image.has_image()) { |
| 275 | float percent = PercentageDifferent(actual_image, baseline_image); |
| 276 | if (percent > 0.0) { |
| 277 | // failure: The WebKit version also writes the difference image to |
| 278 | // stdout, which seems excessive for our needs. |
| 279 | printf("diff: %01.2f%% failed\n", percent); |
| 280 | } else { |
| 281 | // success |
| 282 | printf("diff: %01.2f%% passed\n", percent); |
| 283 | } |
| 284 | actual_image.Clear(); |
| 285 | baseline_image.Clear(); |
| 286 | } |
| 287 | |
| 288 | fflush(stdout); |
| 289 | } |
| 290 | */ |
| 291 | } |
| 292 | |
| 293 | bool CreateImageDiff(const Image& image1, const Image& image2, Image* out) { |
| 294 | int w = std::min(image1.w(), image2.w()); |
| 295 | int h = std::min(image1.h(), image2.h()); |
| 296 | *out = Image(image1); |
| 297 | bool same = (image1.w() == image2.w()) && (image1.h() == image2.h()); |
| 298 | |
| 299 | // TODO(estade): do something with the extra pixels if the image sizes |
| 300 | // are different. |
| 301 | for (int y = 0; y < h; y++) { |
| 302 | for (int x = 0; x < w; x++) { |
| 303 | uint32_t base_pixel = image1.pixel_at(x, y); |
| 304 | if (base_pixel != image2.pixel_at(x, y)) { |
| 305 | // Set differing pixels red. |
| 306 | out->set_pixel_at(x, y, RGBA_RED | RGBA_ALPHA); |
| 307 | same = false; |
| 308 | } else { |
| 309 | // Set same pixels as faded. |
| 310 | uint32_t alpha = base_pixel & RGBA_ALPHA; |
| 311 | uint32_t new_pixel = base_pixel - ((alpha / 2) & RGBA_ALPHA); |
| 312 | out->set_pixel_at(x, y, new_pixel); |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | return same; |
| 318 | } |
| 319 | |
| 320 | int DiffImages(const std::string& file1, |
| 321 | const std::string& file2, |
| 322 | const std::string& out_file) { |
| 323 | Image actual_image; |
| 324 | Image baseline_image; |
| 325 | |
| 326 | if (!actual_image.CreateFromFilename(file1)) { |
| 327 | fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file1.c_str()); |
| 328 | return kStatusError; |
| 329 | } |
| 330 | if (!baseline_image.CreateFromFilename(file2)) { |
| 331 | fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file2.c_str()); |
| 332 | return kStatusError; |
| 333 | } |
| 334 | |
| 335 | Image diff_image; |
| 336 | bool same = CreateImageDiff(baseline_image, actual_image, &diff_image); |
| 337 | if (same) |
| 338 | return kStatusSame; |
| 339 | |
| 340 | std::vector<unsigned char> png_encoding; |
| 341 | image_diff_png::EncodeRGBAPNG( |
| 342 | diff_image.data(), diff_image.w(), diff_image.h(), |
| 343 | diff_image.w() * 4, &png_encoding); |
| 344 | |
| 345 | FILE *f = fopen(out_file.c_str(), "wb"); |
| 346 | if (!f) |
| 347 | return kStatusError; |
| 348 | |
| 349 | size_t size = png_encoding.size(); |
| 350 | char *ptr = reinterpret_cast<char*>(&png_encoding.front()); |
| 351 | if (fwrite(ptr, 1, size, f) != size) |
| 352 | return kStatusError; |
| 353 | |
| 354 | return kStatusDifferent; |
| 355 | } |
| 356 | |
| 357 | int main(int argc, const char* argv[]) { |
| 358 | bool histograms = false; |
| 359 | bool produce_diff_image = false; |
| 360 | std::string filename1; |
| 361 | std::string filename2; |
| 362 | std::string diff_filename; |
| 363 | |
| 364 | int i; |
| 365 | for (i = 1; i < argc; ++i) { |
| 366 | const char* arg = argv[i]; |
| 367 | if (strstr(arg, "--") != arg) |
| 368 | break; |
| 369 | if (strcmp(arg, "--histogram") == 0) { |
| 370 | histograms = true; |
| 371 | } else if (strcmp(arg, "--diff") == 0) { |
| 372 | produce_diff_image = true; |
| 373 | } |
| 374 | } |
| 375 | if (i < argc) { |
| 376 | filename1 = argv[i]; |
| 377 | ++i; |
| 378 | } |
| 379 | if (i < argc) { |
| 380 | filename2 = argv[i]; |
| 381 | ++i; |
| 382 | } |
| 383 | if (i < argc) { |
| 384 | diff_filename = argv[i]; |
| 385 | ++i; |
| 386 | } |
| 387 | |
| 388 | if (produce_diff_image) { |
| 389 | if (!diff_filename.empty()) { |
| 390 | return DiffImages(filename1, filename2, diff_filename); |
| 391 | } |
| 392 | } else if (!filename2.empty()) { |
| 393 | return CompareImages(filename1, filename2, histograms); |
| 394 | } |
| 395 | |
| 396 | PrintHelp(); |
| 397 | return kStatusError; |
| 398 | } |