blob: 23f988a8f1d9f6a005b2aa0669c86b95a0d422d8 [file] [log] [blame]
[email protected]5ad76172011-03-02 19:25:171// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]19478682009-07-13 16:11:082// 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]d91db112011-10-18 20:58:517#include <algorithm>
8
thestig22dfc4012014-09-05 08:29:449#include "base/files/file_util.h"
[email protected]aa82249f2009-07-16 17:23:5810#include "base/md5.h"
[email protected]cb154062014-01-17 03:32:4011#include "base/numerics/safe_conversions.h"
[email protected]896d161f2013-06-11 22:52:2412#include "base/strings/string_number_conversions.h"
[email protected]7d7489902011-04-11 21:54:0613#include "printing/metafile.h"
vitalybukafdd3212ca2014-08-29 03:11:5614#include "printing/pdf_metafile_skia.h"
[email protected]48091b02010-10-21 02:38:1115#include "third_party/skia/include/core/SkColor.h"
[email protected]08397d52011-02-05 01:53:3816#include "ui/gfx/codec/png_codec.h"
[email protected]aa82249f2009-07-16 17:23:5817
18namespace printing {
19
[email protected]79f63882013-02-10 05:15:4520Image::Image(const base::FilePath& path)
[email protected]aa82249f2009-07-16 17:23:5821 : row_length_(0),
22 ignore_alpha_(true) {
[email protected]19478682009-07-13 16:11:0823 std::string data;
[email protected]82f84b92013-08-30 18:23:5024 base::ReadFileToString(path, &data);
[email protected]19478682009-07-13 16:11:0825 bool success = false;
[email protected]63597e4e2010-07-08 17:49:0526 if (path.MatchesExtension(FILE_PATH_LITERAL(".png"))) {
[email protected]19478682009-07-13 16:11:0827 success = LoadPng(data);
[email protected]63597e4e2010-07-08 17:49:0528 } else if (path.MatchesExtension(FILE_PATH_LITERAL(".emf"))) {
[email protected]19478682009-07-13 16:11:0829 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]7d7489902011-04-11 21:54:0640Image::Image(const Metafile& metafile)
[email protected]aa82249f2009-07-16 17:23:5841 : row_length_(0),
42 ignore_alpha_(true) {
43 LoadMetafile(metafile);
44}
45
46Image::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]9b2331d92010-10-04 23:11:1953Image::~Image() {}
54
[email protected]aa82249f2009-07-16 17:23:5855std::string Image::checksum() const {
[email protected]0ad1d2d62011-07-18 16:54:5856 base::MD5Digest digest;
57 base::MD5Sum(&data_[0], data_.size(), &digest);
tfarina0a9c25c2015-04-08 04:44:2558 return base::MD5DigestToBase16(digest);
[email protected]aa82249f2009-07-16 17:23:5859}
60
[email protected]79f63882013-02-10 05:15:4561bool Image::SaveToPng(const base::FilePath& filepath) const {
[email protected]19478682009-07-13 16:11:0862 DCHECK(!data_.empty());
63 std::vector<unsigned char> compressed;
[email protected]468fec92009-10-03 03:12:2064 bool success = gfx::PNGCodec::Encode(&*data_.begin(),
65 gfx::PNGCodec::FORMAT_BGRA,
[email protected]c99fcf52011-03-28 18:24:2166 size_,
[email protected]468fec92009-10-03 03:12:2067 row_length_,
68 true,
[email protected]c99fcf52011-03-28 18:24:2169 std::vector<gfx::PNGCodec::Comment>(),
[email protected]468fec92009-10-03 03:12:2070 &compressed);
[email protected]19478682009-07-13 16:11:0871 DCHECK(success && compressed.size());
72 if (success) {
[email protected]e5c2a22e2014-03-06 20:42:3073 int write_bytes = base::WriteFile(
[email protected]72f966b2009-10-14 20:02:2774 filepath,
[email protected]848c9c2f2013-01-21 17:58:2875 reinterpret_cast<char*>(&*compressed.begin()),
[email protected]cb154062014-01-17 03:32:4076 base::checked_cast<int>(compressed.size()));
[email protected]19478682009-07-13 16:11:0877 success = (write_bytes == static_cast<int>(compressed.size()));
78 DCHECK(success);
79 }
80 return success;
81}
82
[email protected]aa82249f2009-07-16 17:23:5883double Image::PercentageDifferent(const Image& rhs) const {
[email protected]19478682009-07-13 16:11:0884 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) {
thestig707a24b22015-09-14 18:16:3394 uint32_t lhs_pixel = pixel_at(x, y);
95 uint32_t rhs_pixel = rhs.pixel_at(x, y);
[email protected]19478682009-07-13 16:11:0896 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) {
thestig707a24b22015-09-14 18:16:33102 uint32_t lhs_pixel = pixel_at(x, y);
[email protected]19478682009-07-13 16:11:08103 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) {
thestig707a24b22015-09-14 18:16:33109 uint32_t rhs_pixel = rhs.pixel_at(x, y);
[email protected]19478682009-07-13 16:11:08110 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) {
thestig707a24b22015-09-14 18:16:33118 uint32_t lhs_pixel = pixel_at(x, y);
[email protected]19478682009-07-13 16:11:08119 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) {
thestig707a24b22015-09-14 18:16:33127 uint32_t rhs_pixel = rhs.pixel_at(x, y);
[email protected]19478682009-07-13 16:11:08128 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]aa82249f2009-07-16 17:23:58140bool Image::LoadPng(const std::string& compressed) {
[email protected]19478682009-07-13 16:11:08141 int w;
142 int h;
[email protected]468fec92009-10-03 03:12:20143 bool success = gfx::PNGCodec::Decode(
[email protected]19478682009-07-13 16:11:08144 reinterpret_cast<const unsigned char*>(compressed.c_str()),
[email protected]468fec92009-10-03 03:12:20145 compressed.size(), gfx::PNGCodec::FORMAT_BGRA, &data_, &w, &h);
[email protected]19478682009-07-13 16:11:08146 size_.SetSize(w, h);
thestig707a24b22015-09-14 18:16:33147 row_length_ = size_.width() * sizeof(uint32_t);
[email protected]19478682009-07-13 16:11:08148 return success;
149}
150
[email protected]aa82249f2009-07-16 17:23:58151bool Image::LoadMetafile(const std::string& data) {
[email protected]19478682009-07-13 16:11:08152 DCHECK(!data.empty());
vitalybukafdd3212ca2014-08-29 03:11:56153 PdfMetafileSkia metafile;
[email protected]848c9c2f2013-01-21 17:58:28154 if (!metafile.InitFromData(data.data(),
thestig707a24b22015-09-14 18:16:33155 base::checked_cast<uint32_t>(data.size())))
[email protected]67c5be792011-03-26 00:28:36156 return false;
[email protected]7d7489902011-04-11 21:54:06157 return LoadMetafile(metafile);
[email protected]aa82249f2009-07-16 17:23:58158}
159
160} // namespace printing