1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "printing/image.h"
11 #include "base/files/file_util.h"
13 #include "base/numerics/safe_conversions.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "printing/metafile.h"
16 #include "third_party/skia/include/core/SkColor.h"
17 #include "ui/gfx/codec/png_codec.h"
21 Image::Image(const Metafile& metafile)
24 LoadMetafile(metafile);
27 Image::Image(const Image& image) = default;
29 Image::~Image() = default;
31 std::string Image::checksum() const {
32 base::MD5Digest digest;
33 base::MD5Sum(&data_[0], data_.size(), &digest);
34 return base::MD5DigestToBase16(digest);
37 bool Image::SaveToPng(const base::FilePath& filepath) const {
38 DCHECK(!data_.empty());
39 std::vector<unsigned char> compressed;
40 bool success = gfx::PNGCodec::Encode(&*data_.begin(),
41 gfx::PNGCodec::FORMAT_BGRA,
45 std::vector<gfx::PNGCodec::Comment>(),
47 DCHECK(success && compressed.size());
49 int write_bytes = base::WriteFile(
51 reinterpret_cast<char*>(&*compressed.begin()),
52 base::checked_cast<int>(compressed.size()));
53 success = (write_bytes == static_cast<int>(compressed.size()));
59 double Image::PercentageDifferent(const Image& rhs) const {
60 if (size_.width() == 0 || size_.height() == 0 ||
61 rhs.size_.width() == 0 || rhs.size_.height() == 0)
64 int width = std::min(size_.width(), rhs.size_.width());
65 int height = std::min(size_.height(), rhs.size_.height());
66 // Compute pixels different in the overlap
67 int pixels_different = 0;
68 for (int y = 0; y < height; ++y) {
69 for (int x = 0; x < width; ++x) {
70 uint32_t lhs_pixel = pixel_at(x, y);
71 uint32_t rhs_pixel = rhs.pixel_at(x, y);
72 if (lhs_pixel != rhs_pixel)
76 // Look for extra right lhs pixels. They should be white.
77 for (int x = width; x < size_.width(); ++x) {
78 uint32_t lhs_pixel = pixel_at(x, y);
79 if (lhs_pixel != Color(SK_ColorWHITE))
83 // Look for extra right rhs pixels. They should be white.
84 for (int x = width; x < rhs.size_.width(); ++x) {
85 uint32_t rhs_pixel = rhs.pixel_at(x, y);
86 if (rhs_pixel != Color(SK_ColorWHITE))
91 // Look for extra bottom lhs pixels. They should be white.
92 for (int y = height; y < size_.height(); ++y) {
93 for (int x = 0; x < size_.width(); ++x) {
94 uint32_t lhs_pixel = pixel_at(x, y);
95 if (lhs_pixel != Color(SK_ColorWHITE))
100 // Look for extra bottom rhs pixels. They should be white.
101 for (int y = height; y < rhs.size_.height(); ++y) {
102 for (int x = 0; x < rhs.size_.width(); ++x) {
103 uint32_t rhs_pixel = rhs.pixel_at(x, y);
104 if (rhs_pixel != Color(SK_ColorWHITE))
109 // Like the WebKit ImageDiff tool, we define percentage different in terms
110 // of the size of the 'actual' bitmap.
111 double total_pixels = static_cast<double>(size_.width()) *
112 static_cast<double>(height);
113 return static_cast<double>(pixels_different) / total_pixels * 100.;
116 bool Image::LoadPng(const std::string& compressed) {
119 bool success = gfx::PNGCodec::Decode(
120 reinterpret_cast<const unsigned char*>(compressed.c_str()),
121 compressed.size(), gfx::PNGCodec::FORMAT_BGRA, &data_, &w, &h);
123 row_length_ = size_.width() * sizeof(uint32_t);
127 } // namespace printing