| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "printing/image.h" | 5 #include "printing/image.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/md5.h" | 12 #include "base/md5.h" |
| 13 #include "base/numerics/safe_conversions.h" | 13 #include "base/numerics/safe_conversions.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "printing/metafile.h" | 15 #include "printing/metafile.h" |
| 16 #include "third_party/skia/include/core/SkColor.h" | 16 #include "third_party/skia/include/core/SkColor.h" |
| 17 #include "ui/gfx/codec/png_codec.h" | 17 #include "ui/gfx/codec/png_codec.h" |
| 18 | 18 |
| 19 namespace printing { | 19 namespace printing { |
| 20 | 20 |
| 21 Image::Image(const Metafile& metafile) | 21 Image::Image(const void* metafile_src_buffer, size_t metafile_src_buffer_size) |
| 22 : row_length_(0), | 22 : row_length_(0), ignore_alpha_(true) { |
| 23 ignore_alpha_(true) { | 23 LoadMetafile(metafile_src_buffer, metafile_src_buffer_size); |
| 24 LoadMetafile(metafile); | |
| 25 } | 24 } |
| 26 | 25 |
| 27 Image::Image(const Image& image) | 26 Image::Image(const Image&) = default; |
| 28 : size_(image.size_), | 27 Image::Image(Image&&) = default; |
| 29 row_length_(image.row_length_), | |
| 30 data_(image.data_), | |
| 31 ignore_alpha_(image.ignore_alpha_) { | |
| 32 } | |
| 33 | 28 |
| 34 Image::~Image() {} | 29 Image::~Image() {} |
| 35 | 30 |
| 36 std::string Image::checksum() const { | 31 std::string Image::checksum() const { |
| 37 base::MD5Digest digest; | 32 base::MD5Digest digest; |
| 38 base::MD5Sum(&data_[0], data_.size(), &digest); | 33 base::MD5Sum(&data_[0], data_.size(), &digest); |
| 39 return base::MD5DigestToBase16(digest); | 34 return base::MD5DigestToBase16(digest); |
| 40 } | 35 } |
| 41 | 36 |
| 42 bool Image::SaveToPng(const base::FilePath& filepath) const { | 37 bool Image::SaveToPng(const base::FilePath& filepath) const { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 53 if (success) { | 48 if (success) { |
| 54 int write_bytes = base::WriteFile( | 49 int write_bytes = base::WriteFile( |
| 55 filepath, | 50 filepath, |
| 56 reinterpret_cast<char*>(&*compressed.begin()), | 51 reinterpret_cast<char*>(&*compressed.begin()), |
| 57 base::checked_cast<int>(compressed.size())); | 52 base::checked_cast<int>(compressed.size())); |
| 58 success = (write_bytes == static_cast<int>(compressed.size())); | 53 success = (write_bytes == static_cast<int>(compressed.size())); |
| 59 DCHECK(success); | 54 DCHECK(success); |
| 60 } | 55 } |
| 61 return success; | 56 return success; |
| 62 } | 57 } |
| 63 | |
| 64 double Image::PercentageDifferent(const Image& rhs) const { | |
| 65 if (size_.width() == 0 || size_.height() == 0 || | |
| 66 rhs.size_.width() == 0 || rhs.size_.height() == 0) | |
| 67 return 100.; | |
| 68 | |
| 69 int width = std::min(size_.width(), rhs.size_.width()); | |
| 70 int height = std::min(size_.height(), rhs.size_.height()); | |
| 71 // Compute pixels different in the overlap | |
| 72 int pixels_different = 0; | |
| 73 for (int y = 0; y < height; ++y) { | |
| 74 for (int x = 0; x < width; ++x) { | |
| 75 uint32_t lhs_pixel = pixel_at(x, y); | |
| 76 uint32_t rhs_pixel = rhs.pixel_at(x, y); | |
| 77 if (lhs_pixel != rhs_pixel) | |
| 78 ++pixels_different; | |
| 79 } | |
| 80 | |
| 81 // Look for extra right lhs pixels. They should be white. | |
| 82 for (int x = width; x < size_.width(); ++x) { | |
| 83 uint32_t lhs_pixel = pixel_at(x, y); | |
| 84 if (lhs_pixel != Color(SK_ColorWHITE)) | |
| 85 ++pixels_different; | |
| 86 } | |
| 87 | |
| 88 // Look for extra right rhs pixels. They should be white. | |
| 89 for (int x = width; x < rhs.size_.width(); ++x) { | |
| 90 uint32_t rhs_pixel = rhs.pixel_at(x, y); | |
| 91 if (rhs_pixel != Color(SK_ColorWHITE)) | |
| 92 ++pixels_different; | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 // Look for extra bottom lhs pixels. They should be white. | |
| 97 for (int y = height; y < size_.height(); ++y) { | |
| 98 for (int x = 0; x < size_.width(); ++x) { | |
| 99 uint32_t lhs_pixel = pixel_at(x, y); | |
| 100 if (lhs_pixel != Color(SK_ColorWHITE)) | |
| 101 ++pixels_different; | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 // Look for extra bottom rhs pixels. They should be white. | |
| 106 for (int y = height; y < rhs.size_.height(); ++y) { | |
| 107 for (int x = 0; x < rhs.size_.width(); ++x) { | |
| 108 uint32_t rhs_pixel = rhs.pixel_at(x, y); | |
| 109 if (rhs_pixel != Color(SK_ColorWHITE)) | |
| 110 ++pixels_different; | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 // Like the WebKit ImageDiff tool, we define percentage different in terms | |
| 115 // of the size of the 'actual' bitmap. | |
| 116 double total_pixels = static_cast<double>(size_.width()) * | |
| 117 static_cast<double>(height); | |
| 118 return static_cast<double>(pixels_different) / total_pixels * 100.; | |
| 119 } | |
| 120 | |
| 121 bool Image::LoadPng(const std::string& compressed) { | |
| 122 int w; | |
| 123 int h; | |
| 124 bool success = gfx::PNGCodec::Decode( | |
| 125 reinterpret_cast<const unsigned char*>(compressed.c_str()), | |
| 126 compressed.size(), gfx::PNGCodec::FORMAT_BGRA, &data_, &w, &h); | |
| 127 size_.SetSize(w, h); | |
| 128 row_length_ = size_.width() * sizeof(uint32_t); | |
| 129 return success; | |
| 130 } | |
| 131 | |
| 132 } // namespace printing | 58 } // namespace printing |
| OLD | NEW |