[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "printing/image.h" |
| 6 | |
| 7 | #include "base/file_util.h" |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 8 | #include "base/md5.h" |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 9 | #include "base/string_number_conversions.h" |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 10 | #include "third_party/skia/include/core/SkColor.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame^] | 11 | #include "ui/gfx/codec/png_codec.h" |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 12 | |
| 13 | namespace printing { |
| 14 | |
[email protected] | 63597e4e | 2010-07-08 17:49:05 | [diff] [blame] | 15 | Image::Image(const FilePath& path) |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 16 | : row_length_(0), |
| 17 | ignore_alpha_(true) { |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 18 | std::string data; |
[email protected] | 63597e4e | 2010-07-08 17:49:05 | [diff] [blame] | 19 | file_util::ReadFileToString(path, &data); |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 20 | bool success = false; |
[email protected] | 63597e4e | 2010-07-08 17:49:05 | [diff] [blame] | 21 | if (path.MatchesExtension(FILE_PATH_LITERAL(".png"))) { |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 22 | success = LoadPng(data); |
[email protected] | 63597e4e | 2010-07-08 17:49:05 | [diff] [blame] | 23 | } else if (path.MatchesExtension(FILE_PATH_LITERAL(".emf"))) { |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 24 | success = LoadMetafile(data); |
| 25 | } else { |
| 26 | DCHECK(false); |
| 27 | } |
| 28 | if (!success) { |
| 29 | size_.SetSize(0, 0); |
| 30 | row_length_ = 0; |
| 31 | data_.clear(); |
| 32 | } |
| 33 | } |
| 34 | |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 35 | Image::Image(const NativeMetafile& metafile) |
| 36 | : row_length_(0), |
| 37 | ignore_alpha_(true) { |
| 38 | LoadMetafile(metafile); |
| 39 | } |
| 40 | |
| 41 | Image::Image(const Image& image) |
| 42 | : size_(image.size_), |
| 43 | row_length_(image.row_length_), |
| 44 | data_(image.data_), |
| 45 | ignore_alpha_(image.ignore_alpha_) { |
| 46 | } |
| 47 | |
[email protected] | 9b2331d9 | 2010-10-04 23:11:19 | [diff] [blame] | 48 | Image::~Image() {} |
| 49 | |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 50 | std::string Image::checksum() const { |
| 51 | MD5Digest digest; |
| 52 | MD5Sum(&data_[0], data_.size(), &digest); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 53 | return base::HexEncode(&digest, sizeof(digest)); |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 54 | } |
| 55 | |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 56 | bool Image::SaveToPng(const FilePath& filepath) const { |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 57 | DCHECK(!data_.empty()); |
| 58 | std::vector<unsigned char> compressed; |
[email protected] | 468fec9 | 2009-10-03 03:12:20 | [diff] [blame] | 59 | bool success = gfx::PNGCodec::Encode(&*data_.begin(), |
| 60 | gfx::PNGCodec::FORMAT_BGRA, |
| 61 | size_.width(), |
| 62 | size_.height(), |
| 63 | row_length_, |
| 64 | true, |
| 65 | &compressed); |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 66 | DCHECK(success && compressed.size()); |
| 67 | if (success) { |
| 68 | int write_bytes = file_util::WriteFile( |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 69 | filepath, |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 70 | reinterpret_cast<char*>(&*compressed.begin()), compressed.size()); |
| 71 | success = (write_bytes == static_cast<int>(compressed.size())); |
| 72 | DCHECK(success); |
| 73 | } |
| 74 | return success; |
| 75 | } |
| 76 | |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 77 | double Image::PercentageDifferent(const Image& rhs) const { |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 78 | if (size_.width() == 0 || size_.height() == 0 || |
| 79 | rhs.size_.width() == 0 || rhs.size_.height() == 0) |
| 80 | return 100.; |
| 81 | |
| 82 | int width = std::min(size_.width(), rhs.size_.width()); |
| 83 | int height = std::min(size_.height(), rhs.size_.height()); |
| 84 | // Compute pixels different in the overlap |
| 85 | int pixels_different = 0; |
| 86 | for (int y = 0; y < height; ++y) { |
| 87 | for (int x = 0; x < width; ++x) { |
| 88 | uint32 lhs_pixel = pixel_at(x, y); |
| 89 | uint32 rhs_pixel = rhs.pixel_at(x, y); |
| 90 | if (lhs_pixel != rhs_pixel) |
| 91 | ++pixels_different; |
| 92 | } |
| 93 | |
| 94 | // Look for extra right lhs pixels. They should be white. |
| 95 | for (int x = width; x < size_.width(); ++x) { |
| 96 | uint32 lhs_pixel = pixel_at(x, y); |
| 97 | if (lhs_pixel != Color(SK_ColorWHITE)) |
| 98 | ++pixels_different; |
| 99 | } |
| 100 | |
| 101 | // Look for extra right rhs pixels. They should be white. |
| 102 | for (int x = width; x < rhs.size_.width(); ++x) { |
| 103 | uint32 rhs_pixel = rhs.pixel_at(x, y); |
| 104 | if (rhs_pixel != Color(SK_ColorWHITE)) |
| 105 | ++pixels_different; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Look for extra bottom lhs pixels. They should be white. |
| 110 | for (int y = height; y < size_.height(); ++y) { |
| 111 | for (int x = 0; x < size_.width(); ++x) { |
| 112 | uint32 lhs_pixel = pixel_at(x, y); |
| 113 | if (lhs_pixel != Color(SK_ColorWHITE)) |
| 114 | ++pixels_different; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Look for extra bottom rhs pixels. They should be white. |
| 119 | for (int y = height; y < rhs.size_.height(); ++y) { |
| 120 | for (int x = 0; x < rhs.size_.width(); ++x) { |
| 121 | uint32 rhs_pixel = rhs.pixel_at(x, y); |
| 122 | if (rhs_pixel != Color(SK_ColorWHITE)) |
| 123 | ++pixels_different; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // Like the WebKit ImageDiff tool, we define percentage different in terms |
| 128 | // of the size of the 'actual' bitmap. |
| 129 | double total_pixels = static_cast<double>(size_.width()) * |
| 130 | static_cast<double>(height); |
| 131 | return static_cast<double>(pixels_different) / total_pixels * 100.; |
| 132 | } |
| 133 | |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 134 | bool Image::LoadPng(const std::string& compressed) { |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 135 | int w; |
| 136 | int h; |
[email protected] | 468fec9 | 2009-10-03 03:12:20 | [diff] [blame] | 137 | bool success = gfx::PNGCodec::Decode( |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 138 | reinterpret_cast<const unsigned char*>(compressed.c_str()), |
[email protected] | 468fec9 | 2009-10-03 03:12:20 | [diff] [blame] | 139 | compressed.size(), gfx::PNGCodec::FORMAT_BGRA, &data_, &w, &h); |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 140 | size_.SetSize(w, h); |
| 141 | row_length_ = size_.width() * sizeof(uint32); |
| 142 | return success; |
| 143 | } |
| 144 | |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 145 | bool Image::LoadMetafile(const std::string& data) { |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 146 | DCHECK(!data.empty()); |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 147 | NativeMetafile metafile; |
[email protected] | e8980a5 | 2010-10-07 20:11:24 | [diff] [blame] | 148 | metafile.Init(data.data(), data.size()); |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 149 | return LoadMetafile(metafile); |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | } // namespace printing |