[email protected] | 5ad7617 | 2011-03-02 19:25:17 | [diff] [blame] | 1 | // Copyright (c) 2011 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 | |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
thestig | 22dfc401 | 2014-09-05 08:29:44 | [diff] [blame] | 9 | #include "base/files/file_util.h" |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 10 | #include "base/md5.h" |
[email protected] | cb15406 | 2014-01-17 03:32:40 | [diff] [blame] | 11 | #include "base/numerics/safe_conversions.h" |
[email protected] | 896d161f | 2013-06-11 22:52:24 | [diff] [blame] | 12 | #include "base/strings/string_number_conversions.h" |
[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 13 | #include "printing/metafile.h" |
vitalybuka | fdd3212ca | 2014-08-29 03:11:56 | [diff] [blame] | 14 | #include "printing/pdf_metafile_skia.h" |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 15 | #include "third_party/skia/include/core/SkColor.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 16 | #include "ui/gfx/codec/png_codec.h" |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 17 | |
| 18 | namespace printing { |
| 19 | |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 20 | Image::Image(const base::FilePath& path) |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 21 | : row_length_(0), |
| 22 | ignore_alpha_(true) { |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 23 | std::string data; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 24 | base::ReadFileToString(path, &data); |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 25 | bool success = false; |
[email protected] | 63597e4e | 2010-07-08 17:49:05 | [diff] [blame] | 26 | if (path.MatchesExtension(FILE_PATH_LITERAL(".png"))) { |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 27 | success = LoadPng(data); |
[email protected] | 63597e4e | 2010-07-08 17:49:05 | [diff] [blame] | 28 | } else if (path.MatchesExtension(FILE_PATH_LITERAL(".emf"))) { |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 29 | success = LoadMetafile(data); |
| 30 | } else { |
| 31 | DCHECK(false); |
| 32 | } |
| 33 | if (!success) { |
| 34 | size_.SetSize(0, 0); |
| 35 | row_length_ = 0; |
| 36 | data_.clear(); |
| 37 | } |
| 38 | } |
| 39 | |
[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 40 | Image::Image(const Metafile& metafile) |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 41 | : row_length_(0), |
| 42 | ignore_alpha_(true) { |
| 43 | LoadMetafile(metafile); |
| 44 | } |
| 45 | |
| 46 | Image::Image(const Image& image) |
| 47 | : size_(image.size_), |
| 48 | row_length_(image.row_length_), |
| 49 | data_(image.data_), |
| 50 | ignore_alpha_(image.ignore_alpha_) { |
| 51 | } |
| 52 | |
[email protected] | 9b2331d9 | 2010-10-04 23:11:19 | [diff] [blame] | 53 | Image::~Image() {} |
| 54 | |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 55 | std::string Image::checksum() const { |
[email protected] | 0ad1d2d6 | 2011-07-18 16:54:58 | [diff] [blame] | 56 | base::MD5Digest digest; |
| 57 | base::MD5Sum(&data_[0], data_.size(), &digest); |
tfarina | 0a9c25c | 2015-04-08 04:44:25 | [diff] [blame] | 58 | return base::MD5DigestToBase16(digest); |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 59 | } |
| 60 | |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 61 | bool Image::SaveToPng(const base::FilePath& filepath) const { |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 62 | DCHECK(!data_.empty()); |
| 63 | std::vector<unsigned char> compressed; |
[email protected] | 468fec9 | 2009-10-03 03:12:20 | [diff] [blame] | 64 | bool success = gfx::PNGCodec::Encode(&*data_.begin(), |
| 65 | gfx::PNGCodec::FORMAT_BGRA, |
[email protected] | c99fcf5 | 2011-03-28 18:24:21 | [diff] [blame] | 66 | size_, |
[email protected] | 468fec9 | 2009-10-03 03:12:20 | [diff] [blame] | 67 | row_length_, |
| 68 | true, |
[email protected] | c99fcf5 | 2011-03-28 18:24:21 | [diff] [blame] | 69 | std::vector<gfx::PNGCodec::Comment>(), |
[email protected] | 468fec9 | 2009-10-03 03:12:20 | [diff] [blame] | 70 | &compressed); |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 71 | DCHECK(success && compressed.size()); |
| 72 | if (success) { |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 73 | int write_bytes = base::WriteFile( |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 74 | filepath, |
[email protected] | 848c9c2f | 2013-01-21 17:58:28 | [diff] [blame] | 75 | reinterpret_cast<char*>(&*compressed.begin()), |
[email protected] | cb15406 | 2014-01-17 03:32:40 | [diff] [blame] | 76 | base::checked_cast<int>(compressed.size())); |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 77 | success = (write_bytes == static_cast<int>(compressed.size())); |
| 78 | DCHECK(success); |
| 79 | } |
| 80 | return success; |
| 81 | } |
| 82 | |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 83 | double Image::PercentageDifferent(const Image& rhs) const { |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 84 | if (size_.width() == 0 || size_.height() == 0 || |
| 85 | rhs.size_.width() == 0 || rhs.size_.height() == 0) |
| 86 | return 100.; |
| 87 | |
| 88 | int width = std::min(size_.width(), rhs.size_.width()); |
| 89 | int height = std::min(size_.height(), rhs.size_.height()); |
| 90 | // Compute pixels different in the overlap |
| 91 | int pixels_different = 0; |
| 92 | for (int y = 0; y < height; ++y) { |
| 93 | for (int x = 0; x < width; ++x) { |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 94 | uint32_t lhs_pixel = pixel_at(x, y); |
| 95 | uint32_t rhs_pixel = rhs.pixel_at(x, y); |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 96 | if (lhs_pixel != rhs_pixel) |
| 97 | ++pixels_different; |
| 98 | } |
| 99 | |
| 100 | // Look for extra right lhs pixels. They should be white. |
| 101 | for (int x = width; x < size_.width(); ++x) { |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 102 | uint32_t lhs_pixel = pixel_at(x, y); |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 103 | if (lhs_pixel != Color(SK_ColorWHITE)) |
| 104 | ++pixels_different; |
| 105 | } |
| 106 | |
| 107 | // Look for extra right rhs pixels. They should be white. |
| 108 | for (int x = width; x < rhs.size_.width(); ++x) { |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 109 | uint32_t rhs_pixel = rhs.pixel_at(x, y); |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 110 | if (rhs_pixel != Color(SK_ColorWHITE)) |
| 111 | ++pixels_different; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // Look for extra bottom lhs pixels. They should be white. |
| 116 | for (int y = height; y < size_.height(); ++y) { |
| 117 | for (int x = 0; x < size_.width(); ++x) { |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 118 | uint32_t lhs_pixel = pixel_at(x, y); |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 119 | if (lhs_pixel != Color(SK_ColorWHITE)) |
| 120 | ++pixels_different; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // Look for extra bottom rhs pixels. They should be white. |
| 125 | for (int y = height; y < rhs.size_.height(); ++y) { |
| 126 | for (int x = 0; x < rhs.size_.width(); ++x) { |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 127 | uint32_t rhs_pixel = rhs.pixel_at(x, y); |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 128 | if (rhs_pixel != Color(SK_ColorWHITE)) |
| 129 | ++pixels_different; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // Like the WebKit ImageDiff tool, we define percentage different in terms |
| 134 | // of the size of the 'actual' bitmap. |
| 135 | double total_pixels = static_cast<double>(size_.width()) * |
| 136 | static_cast<double>(height); |
| 137 | return static_cast<double>(pixels_different) / total_pixels * 100.; |
| 138 | } |
| 139 | |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 140 | bool Image::LoadPng(const std::string& compressed) { |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 141 | int w; |
| 142 | int h; |
[email protected] | 468fec9 | 2009-10-03 03:12:20 | [diff] [blame] | 143 | bool success = gfx::PNGCodec::Decode( |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 144 | reinterpret_cast<const unsigned char*>(compressed.c_str()), |
[email protected] | 468fec9 | 2009-10-03 03:12:20 | [diff] [blame] | 145 | compressed.size(), gfx::PNGCodec::FORMAT_BGRA, &data_, &w, &h); |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 146 | size_.SetSize(w, h); |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 147 | row_length_ = size_.width() * sizeof(uint32_t); |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 148 | return success; |
| 149 | } |
| 150 | |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 151 | bool Image::LoadMetafile(const std::string& data) { |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 152 | DCHECK(!data.empty()); |
vitalybuka | fdd3212ca | 2014-08-29 03:11:56 | [diff] [blame] | 153 | PdfMetafileSkia metafile; |
[email protected] | 848c9c2f | 2013-01-21 17:58:28 | [diff] [blame] | 154 | if (!metafile.InitFromData(data.data(), |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 155 | base::checked_cast<uint32_t>(data.size()))) |
[email protected] | 67c5be79 | 2011-03-26 00:28:36 | [diff] [blame] | 156 | return false; |
[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 157 | return LoadMetafile(metafile); |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | } // namespace printing |