diff options
author | sverrir@chromium.org <sverrir@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-13 16:11:08 +0000 |
---|---|---|
committer | sverrir@chromium.org <sverrir@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-13 16:11:08 +0000 |
commit | 1947868ed7d749fb6ee2895ce9e9e5af9d11a085 (patch) | |
tree | 043dc7e69fdce61aa46134dee062f50bb9e27609 | |
parent | 540c149788ab214c1fac850961c78445d17177a1 (diff) | |
download | chromium_src-1947868ed7d749fb6ee2895ce9e9e5af9d11a085.zip chromium_src-1947868ed7d749fb6ee2895ce9e9e5af9d11a085.tar.gz chromium_src-1947868ed7d749fb6ee2895ce9e9e5af9d11a085.tar.bz2 |
Extract Image class so it can be used in more tests.
BUG=none
TEST=none (no functional change).
Review URL: http://codereview.chromium.org/149410
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20491 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/printing/printing_layout_uitest.cc | 178 | ||||
-rw-r--r-- | printing/image.cc | 162 | ||||
-rw-r--r-- | printing/image.h | 75 | ||||
-rw-r--r-- | printing/printing.gyp | 2 |
4 files changed, 242 insertions, 175 deletions
diff --git a/chrome/browser/printing/printing_layout_uitest.cc b/chrome/browser/printing/printing_layout_uitest.cc index 8455c11..d68a78d 100644 --- a/chrome/browser/printing/printing_layout_uitest.cc +++ b/chrome/browser/printing/printing_layout_uitest.cc @@ -5,9 +5,6 @@ #include "base/command_line.h" #include "base/file_util.h" #include "base/gfx/gdi_util.h" -#include "skia/ext/platform_device.h" -#include "base/gfx/png_decoder.h" -#include "base/gfx/png_encoder.h" #include "base/simple_thread.h" #include "base/win_util.h" #include "chrome/common/chrome_paths.h" @@ -16,186 +13,17 @@ #include "chrome/test/automation/window_proxy.h" #include "chrome/test/ui/ui_test.h" #include "net/url_request/url_request_unittest.h" +#include "printing/image.h" #include "printing/printing_test.h" #include "printing/native_metafile.h" namespace { +using printing::Image; + const wchar_t* const kGenerateSwitch = L"print-layout-generate"; const wchar_t kDocRoot[] = L"chrome/test/data"; -// Lightweight raw-bitmap management. The image, once initialized, is immuable. -// It is mainly used for comparison. -class Image { - public: - // Creates the image from the given filename on disk. - Image(const std::wstring& filename) : ignore_alpha_(true) { - std::string data; - file_util::ReadFileToString(filename, &data); - EXPECT_TRUE(data.size()); - std::wstring ext = file_util::GetFileExtensionFromPath(filename); - if (LowerCaseEqualsASCII(ext, "png")) { - LoadPng(data); - } else if (LowerCaseEqualsASCII(ext, "emf")) { - LoadMetafile(data); - } else { - EXPECT_TRUE(false); - } - } - - const gfx::Size& size() const { - return size_; - } - - // Used to create the initial test files. - void SaveToPng(const std::wstring& filename) { - ASSERT_FALSE(data_.empty()); - std::vector<unsigned char> compressed; - ASSERT_TRUE(PNGEncoder::Encode(&*data_.begin(), - PNGEncoder::FORMAT_BGRA, - size_.width(), - size_.height(), - row_length_, - true, - &compressed)); - ASSERT_TRUE(compressed.size()); - ASSERT_EQ(compressed.size(), file_util::WriteFile( - filename, - reinterpret_cast<char*>(&*compressed.begin()), compressed.size())); - } - - double PercentageDifferent(const Image& rhs) const { - if (size_.width() == 0 || size_.height() == 0 || - rhs.size_.width() == 0 || rhs.size_.height() == 0) - return 100.; - - int width = std::min(size_.width(), rhs.size_.width()); - int height = std::min(size_.height(), rhs.size_.height()); - // Compute pixels different in the overlap - int pixels_different = 0; - for (int y = 0; y < height; ++y) { - for (int x = 0; x < width; ++x) { - uint32 lhs_pixel = pixel_at(x, y); - uint32 rhs_pixel = rhs.pixel_at(x, y); - if (lhs_pixel != rhs_pixel) - ++pixels_different; - } - - // Look for extra right lhs pixels. They should be white. - for (int x = width; x < size_.width(); ++x) { - uint32 lhs_pixel = pixel_at(x, y); - if (lhs_pixel != Color(SK_ColorWHITE)) - ++pixels_different; - } - - // Look for extra right rhs pixels. They should be white. - for (int x = width; x < rhs.size_.width(); ++x) { - uint32 rhs_pixel = rhs.pixel_at(x, y); - if (rhs_pixel != Color(SK_ColorWHITE)) - ++pixels_different; - } - } - - // Look for extra bottom lhs pixels. They should be white. - for (int y = height; y < size_.height(); ++y) { - for (int x = 0; x < size_.width(); ++x) { - uint32 lhs_pixel = pixel_at(x, y); - if (lhs_pixel != Color(SK_ColorWHITE)) - ++pixels_different; - } - } - - // Look for extra bottom rhs pixels. They should be white. - for (int y = height; y < rhs.size_.height(); ++y) { - for (int x = 0; x < rhs.size_.width(); ++x) { - uint32 rhs_pixel = rhs.pixel_at(x, y); - if (rhs_pixel != Color(SK_ColorWHITE)) - ++pixels_different; - } - } - - // Like the WebKit ImageDiff tool, we define percentage different in terms - // of the size of the 'actual' bitmap. - double total_pixels = static_cast<double>(size_.width()) * - static_cast<double>(height); - return static_cast<double>(pixels_different) / total_pixels * 100.; - } - - // Returns the 0x0RGB or 0xARGB value of the pixel at the given location, - // depending on ignore_alpha_. - uint32 Color(uint32 color) const { - if (ignore_alpha_) - return color & 0xFFFFFF; // Strip out A. - else - return color; - } - - uint32 pixel_at(int x, int y) const { - EXPECT_TRUE(x >= 0 && x < size_.width()); - EXPECT_TRUE(y >= 0 && y < size_.height()); - const uint32* data = reinterpret_cast<const uint32*>(&*data_.begin()); - const uint32* data_row = data + y * row_length_ / sizeof(uint32); - return Color(data_row[x]); - } - - private: - void LoadPng(const std::string& compressed) { - int w; - int h; - EXPECT_TRUE(PNGDecoder::Decode( - reinterpret_cast<const unsigned char*>(compressed.c_str()), - compressed.size(), PNGDecoder::FORMAT_BGRA, &data_, &w, &h)); - size_.SetSize(w, h); - row_length_ = size_.width() * sizeof(uint32); - } - - void LoadMetafile(const std::string& data) { - ASSERT_FALSE(data.empty()); - printing::NativeMetafile metafile; - metafile.CreateFromData(data.data(), data.size()); - gfx::Rect rect(metafile.GetBounds()); - // Create a temporary HDC and bitmap to retrieve the renderered data. - HDC hdc = CreateCompatibleDC(NULL); - BITMAPV4HEADER hdr; - EXPECT_FALSE(rect.x()); - EXPECT_FALSE(rect.y()); - EXPECT_NE(rect.width(), 0); - EXPECT_NE(rect.height(), 0); - size_ = rect.size(); - gfx::CreateBitmapV4Header(rect.width(), rect.height(), &hdr); - void* bits; - HBITMAP bitmap = CreateDIBSection(hdc, - reinterpret_cast<BITMAPINFO*>(&hdr), 0, - &bits, NULL, 0); - EXPECT_TRUE(bitmap); - EXPECT_TRUE(SelectObject(hdc, bitmap)); - skia::PlatformDevice::InitializeDC(hdc); - EXPECT_TRUE(metafile.Playback(hdc, NULL)); - row_length_ = size_.width() * sizeof(uint32); - size_t bytes = row_length_ * size_.height(); - ASSERT_TRUE(bytes); - data_.resize(bytes); - memcpy(&*data_.begin(), bits, bytes); - DeleteDC(hdc); - DeleteObject(bitmap); - } - - // Pixel dimensions of the image. - gfx::Size size_; - - // Length of a line in bytes. - int row_length_; - - // Actual bitmap data in arrays of RGBAs (so when loaded as uint32, it's - // 0xABGR). - std::vector<unsigned char> data_; - - // Flag to signal if the comparison functions should ignore the alpha channel. - const bool ignore_alpha_; - - DISALLOW_EVIL_CONSTRUCTORS(Image); -}; - class PrintingLayoutTest : public PrintingTest<UITest> { public: PrintingLayoutTest() { diff --git a/printing/image.cc b/printing/image.cc new file mode 100644 index 0000000..8f397d8 --- /dev/null +++ b/printing/image.cc @@ -0,0 +1,162 @@ +// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "printing/image.h" + +#include "base/file_util.h" +#include "base/gfx/png_decoder.h" +#include "base/gfx/png_encoder.h" +#include "base/gfx/rect.h" +#include "base/string_util.h" +#include "printing/native_metafile.h" +#include "skia/ext/platform_device.h" + +#if defined(OS_WIN) +#include "base/gfx/gdi_util.h" // EMF support +#endif + +printing::Image::Image(const std::wstring& filename) : ignore_alpha_(true) { + std::string data; + file_util::ReadFileToString(filename, &data); + std::wstring ext = file_util::GetFileExtensionFromPath(filename); + bool success = false; + if (LowerCaseEqualsASCII(ext, "png")) { + success = LoadPng(data); + } else if (LowerCaseEqualsASCII(ext, "emf")) { + success = LoadMetafile(data); + } else { + DCHECK(false); + } + if (!success) { + size_.SetSize(0, 0); + row_length_ = 0; + data_.clear(); + } +} + +bool printing::Image::SaveToPng(const std::wstring& filename) { + DCHECK(!data_.empty()); + std::vector<unsigned char> compressed; + bool success = PNGEncoder::Encode(&*data_.begin(), + PNGEncoder::FORMAT_BGRA, + size_.width(), + size_.height(), + row_length_, + true, + &compressed); + DCHECK(success && compressed.size()); + if (success) { + int write_bytes = file_util::WriteFile( + filename, + reinterpret_cast<char*>(&*compressed.begin()), compressed.size()); + success = (write_bytes == static_cast<int>(compressed.size())); + DCHECK(success); + } + return success; +} + +double printing::Image::PercentageDifferent(const Image& rhs) const { + if (size_.width() == 0 || size_.height() == 0 || + rhs.size_.width() == 0 || rhs.size_.height() == 0) + return 100.; + + int width = std::min(size_.width(), rhs.size_.width()); + int height = std::min(size_.height(), rhs.size_.height()); + // Compute pixels different in the overlap + int pixels_different = 0; + for (int y = 0; y < height; ++y) { + for (int x = 0; x < width; ++x) { + uint32 lhs_pixel = pixel_at(x, y); + uint32 rhs_pixel = rhs.pixel_at(x, y); + if (lhs_pixel != rhs_pixel) + ++pixels_different; + } + + // Look for extra right lhs pixels. They should be white. + for (int x = width; x < size_.width(); ++x) { + uint32 lhs_pixel = pixel_at(x, y); + if (lhs_pixel != Color(SK_ColorWHITE)) + ++pixels_different; + } + + // Look for extra right rhs pixels. They should be white. + for (int x = width; x < rhs.size_.width(); ++x) { + uint32 rhs_pixel = rhs.pixel_at(x, y); + if (rhs_pixel != Color(SK_ColorWHITE)) + ++pixels_different; + } + } + + // Look for extra bottom lhs pixels. They should be white. + for (int y = height; y < size_.height(); ++y) { + for (int x = 0; x < size_.width(); ++x) { + uint32 lhs_pixel = pixel_at(x, y); + if (lhs_pixel != Color(SK_ColorWHITE)) + ++pixels_different; + } + } + + // Look for extra bottom rhs pixels. They should be white. + for (int y = height; y < rhs.size_.height(); ++y) { + for (int x = 0; x < rhs.size_.width(); ++x) { + uint32 rhs_pixel = rhs.pixel_at(x, y); + if (rhs_pixel != Color(SK_ColorWHITE)) + ++pixels_different; + } + } + + // Like the WebKit ImageDiff tool, we define percentage different in terms + // of the size of the 'actual' bitmap. + double total_pixels = static_cast<double>(size_.width()) * + static_cast<double>(height); + return static_cast<double>(pixels_different) / total_pixels * 100.; +} + +bool printing::Image::LoadPng(const std::string& compressed) { + int w; + int h; + bool success = PNGDecoder::Decode( + reinterpret_cast<const unsigned char*>(compressed.c_str()), + compressed.size(), PNGDecoder::FORMAT_BGRA, &data_, &w, &h); + size_.SetSize(w, h); + row_length_ = size_.width() * sizeof(uint32); + return success; +} + +bool printing::Image::LoadMetafile(const std::string& data) { + DCHECK(!data.empty()); +#if defined(OS_WIN) + printing::NativeMetafile metafile; + metafile.CreateFromData(data.data(), data.size()); + gfx::Rect rect(metafile.GetBounds()); + // Create a temporary HDC and bitmap to retrieve the rendered data. + HDC hdc = CreateCompatibleDC(NULL); + BITMAPV4HEADER hdr; + DCHECK_EQ(rect.x(), 0); + DCHECK_EQ(rect.y(), 0); + DCHECK_GT(rect.width(), 0); + DCHECK_GT(rect.height(), 0); + size_ = rect.size(); + gfx::CreateBitmapV4Header(rect.width(), rect.height(), &hdr); + void* bits; + HBITMAP bitmap = CreateDIBSection(hdc, + reinterpret_cast<BITMAPINFO*>(&hdr), 0, + &bits, NULL, 0); + DCHECK(bitmap); + DCHECK(SelectObject(hdc, bitmap)); + skia::PlatformDevice::InitializeDC(hdc); + bool success = metafile.Playback(hdc, NULL); + row_length_ = size_.width() * sizeof(uint32); + size_t bytes = row_length_ * size_.height(); + DCHECK(bytes); + data_.resize(bytes); + memcpy(&*data_.begin(), bits, bytes); + DeleteDC(hdc); + DeleteObject(bitmap); + return success; +#else + NOTIMPLEMENTED(); + return false; +#endif +} diff --git a/printing/image.h b/printing/image.h new file mode 100644 index 0000000..8ef1007 --- /dev/null +++ b/printing/image.h @@ -0,0 +1,75 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PRINTING_IMAGE_H_ +#define PRINTING_IMAGE_H_ + +#include <string> +#include <vector> + +#include "base/basictypes.h" +#include "base/gfx/size.h" +#include "base/logging.h" + +namespace printing { + +// Lightweight raw-bitmap management. The image, once initialized, is immuable. +// The main purpose is testing image contents. +class Image { + public: + // Creates the image from the given filename on disk. Uses extension to + // defer file type. PNG and EMF (on Windows) currently supported. + // If image loading fails size().IsEmpty() will be true. + explicit Image(const std::wstring& filename); + + const gfx::Size& size() const { + return size_; + } + + // Save image as PNG. + bool SaveToPng(const std::wstring& filename); + + // Returns % of pixels different + double PercentageDifferent(const Image& rhs) const; + + // Returns the 0x0RGB or 0xARGB value of the pixel at the given location. + uint32 Color(uint32 color) const { + if (ignore_alpha_) + return color & 0xFFFFFF; // Strip out A. + else + return color; + } + + uint32 pixel_at(int x, int y) const { + DCHECK(x >= 0 && x < size_.width()); + DCHECK(y >= 0 && y < size_.height()); + const uint32* data = reinterpret_cast<const uint32*>(&*data_.begin()); + const uint32* data_row = data + y * row_length_ / sizeof(uint32); + return Color(data_row[x]); + } + + private: + bool LoadPng(const std::string& compressed); + + bool LoadMetafile(const std::string& data); + + // Pixel dimensions of the image. + gfx::Size size_; + + // Length of a line in bytes. + int row_length_; + + // Actual bitmap data in arrays of RGBAs (so when loaded as uint32, it's + // 0xABGR). + std::vector<unsigned char> data_; + + // Flag to signal if the comparison functions should ignore the alpha channel. + const bool ignore_alpha_; // Currently always true. + + DISALLOW_EVIL_CONSTRUCTORS(Image); +}; + +} // namespace printing + +#endif // PRINTING_IMAGE_H_ diff --git a/printing/printing.gyp b/printing/printing.gyp index 8dd7a59..e199de8 100644 --- a/printing/printing.gyp +++ b/printing/printing.gyp @@ -29,6 +29,8 @@ 'sources': [ 'emf_win.cc', 'emf_win.h', + 'image.cc', + 'image.h', 'native_metafile.h', 'page_number.cc', 'page_number.h', |