diff options
author | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-15 21:13:03 +0000 |
---|---|---|
committer | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-15 21:13:03 +0000 |
commit | 022f42453b737577e9175eb279d507981ef40b30 (patch) | |
tree | b1daf3f481988fe6612a5883b502987f8819a85d /chrome | |
parent | 48fdb18832b43df6a3b6e2e66a44fa6ab583e98e (diff) | |
download | chromium_src-022f42453b737577e9175eb279d507981ef40b30.zip chromium_src-022f42453b737577e9175eb279d507981ef40b30.tar.gz chromium_src-022f42453b737577e9175eb279d507981ef40b30.tar.bz2 |
Add an image diff link to failed layout tests.
Review URL: http://codereview.chromium.org/18023
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8119 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/tools/test/image_diff/image_diff.cc | 106 |
1 files changed, 101 insertions, 5 deletions
diff --git a/chrome/tools/test/image_diff/image_diff.cc b/chrome/tools/test/image_diff/image_diff.cc index 7fa6f48..c21003d 100644 --- a/chrome/tools/test/image_diff/image_diff.cc +++ b/chrome/tools/test/image_diff/image_diff.cc @@ -17,25 +17,41 @@ #include "base/command_line.h" #include "base/file_util.h" #include "base/gfx/png_decoder.h" +#include "base/gfx/png_encoder.h" #include "base/logging.h" #include "base/process_util.h" #include "base/scoped_ptr.h" #include "base/string_util.h" +#if defined(OS_WIN) +#include "windows.h" +#endif + // Causes the app to remain open, waiting for pairs of filenames on stdin. // The caller is then responsible for terminating this app. static const wchar_t kOptionPollStdin[] = L"use-stdin"; +static const wchar_t kOptionGenerateDiff[] = L"diff"; // Return codes used by this utility. static const int kStatusSame = 0; static const int kStatusDifferent = 1; static const int kStatusError = 2; +// Color codes. +static const uint32 RGBA_RED = 0x000000ff; +static const uint32 RGBA_ALPHA = 0xff000000; + class Image { public: Image() : w_(0), h_(0) { } + Image(const Image& image) + : w_(image.w_), + h_(image.h_), + data_(image.data_) { + } + bool has_image() const { return w_ > 0 && h_ > 0; } @@ -48,6 +64,10 @@ class Image { return h_; } + const unsigned char* data() const { + return &data_.front(); + } + // Creates the image from stdin with the given data length. On success, it // will return true. On failure, no other methods should be accessed. bool CreateFromStdin(size_t byte_length) { @@ -97,10 +117,17 @@ class Image { } // Returns the RGBA value of the pixel at the given location - uint32 pixel_at(int x, int y) const { + const uint32 pixel_at(int x, int y) const { + DCHECK(x >= 0 && x < w_); + DCHECK(y >= 0 && y < h_); + return *reinterpret_cast<const uint32*>(&(data_[(y * w_ + x) * 4])); + } + + void set_pixel_at(int x, int y, uint32 color) const { DCHECK(x >= 0 && x < w_); DCHECK(y >= 0 && y < h_); - return data_[(y * w_ + x) * 4]; + void* addr = &const_cast<unsigned char*>(&data_.front())[(y * w_ + x) * 4]; + *reinterpret_cast<uint32*>(addr) = color; } private: @@ -149,7 +176,10 @@ void PrintHelp() { " Compares two files on disk, returning 0 when they are the same\n" " image_diff --use-stdin\n" " Stays open reading pairs of filenames from stdin, comparing them,\n" - " and sending 0 to stdout when they are the same\n"); + " and sending 0 to stdout when they are the same\n" + " image_diff --diff <compare file> <reference file> <output file>\n" + " Compares two files on disk, outputs an image that visualizes the" + " difference to <output file>\n"); /* For unfinished webkit-like-mode (see below) "\n" " image_diff -s\n" @@ -232,10 +262,68 @@ int CompareImages(const char* file1, const char* file2) { */ } +bool CreateImageDiff(const Image& image1, const Image& image2, Image* out) { + int w = std::min(image1.w(), image2.w()); + int h = std::min(image1.h(), image2.h()); + *out = Image(image1); + bool same = (image1.w() == image2.w()) && (image1.h() == image2.h()); + + // TODO(estade): do something with the extra pixels if the image sizes + // are different. + for (int y = 0; y < h; y++) { + for (int x = 0; x < w; x++) { + uint32 base_pixel = image1.pixel_at(x, y); + if (base_pixel != image2.pixel_at(x, y)) { + // Set differing pixels red. + out->set_pixel_at(x, y, RGBA_RED | RGBA_ALPHA); + same = false; + } else { + // Set same pixels as faded. + uint32 alpha = base_pixel & RGBA_ALPHA; + uint32 new_pixel = base_pixel - ((alpha / 2) & RGBA_ALPHA); + out->set_pixel_at(x, y, new_pixel); + } + } + } + + return same; +} + +int DiffImages(const char* file1, const char* file2, const char* out_file) { + Image actual_image; + Image baseline_image; + + if (!actual_image.CreateFromFilename(file1)) { + fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file1); + return kStatusError; + } + if (!baseline_image.CreateFromFilename(file2)) { + fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file2); + return kStatusError; + } + + Image diff_image; + bool same = CreateImageDiff(baseline_image, actual_image, &diff_image); + if (same) + return kStatusSame; + + std::vector<unsigned char> png_encoding; + PNGEncoder::Encode(diff_image.data(), PNGEncoder::FORMAT_RGBA, + diff_image.w(), diff_image.h(), diff_image.w() * 4, + false, &png_encoding); + if (file_util::WriteFile(UTF8ToWide(out_file), + reinterpret_cast<char*>(&png_encoding.front()), png_encoding.size()) < 0) + return kStatusError; + + return kStatusDifferent; +} + int main(int argc, const char* argv[]) { base::EnableTerminationOnHeapCorruption(); + // TODO(estade): why does using the default constructor (command line + // singleton) cause an exception when run in debug mode? #if defined(OS_WIN) - CommandLine parsed_command_line; + CommandLine parsed_command_line(::GetCommandLine()); #elif defined(OS_POSIX) CommandLine parsed_command_line(argc, argv); #endif @@ -265,7 +353,15 @@ int main(int argc, const char* argv[]) { return 0; } - if (argc == 3) { + if (parsed_command_line.HasSwitch(kOptionGenerateDiff)) { + if (3 == parsed_command_line.GetLooseValueCount()) { + CommandLine::LooseValueIterator iter = + parsed_command_line.GetLooseValuesBegin(); + return DiffImages(WideToUTF8(*iter).c_str(), + WideToUTF8(*(iter + 1)).c_str(), + WideToUTF8(*(iter + 2)).c_str()); + } + } else if (2 == parsed_command_line.GetLooseValueCount()) { return CompareImages(argv[1], argv[2]); } |