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