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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/md5.h" | 10 #include "base/md5.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 if (size_.width() == 0 || size_.height() == 0 || | 84 if (size_.width() == 0 || size_.height() == 0 || |
85 rhs.size_.width() == 0 || rhs.size_.height() == 0) | 85 rhs.size_.width() == 0 || rhs.size_.height() == 0) |
86 return 100.; | 86 return 100.; |
87 | 87 |
88 int width = std::min(size_.width(), rhs.size_.width()); | 88 int width = std::min(size_.width(), rhs.size_.width()); |
89 int height = std::min(size_.height(), rhs.size_.height()); | 89 int height = std::min(size_.height(), rhs.size_.height()); |
90 // Compute pixels different in the overlap | 90 // Compute pixels different in the overlap |
91 int pixels_different = 0; | 91 int pixels_different = 0; |
92 for (int y = 0; y < height; ++y) { | 92 for (int y = 0; y < height; ++y) { |
93 for (int x = 0; x < width; ++x) { | 93 for (int x = 0; x < width; ++x) { |
94 uint32 lhs_pixel = pixel_at(x, y); | 94 uint32_t lhs_pixel = pixel_at(x, y); |
95 uint32 rhs_pixel = rhs.pixel_at(x, y); | 95 uint32_t rhs_pixel = rhs.pixel_at(x, y); |
96 if (lhs_pixel != rhs_pixel) | 96 if (lhs_pixel != rhs_pixel) |
97 ++pixels_different; | 97 ++pixels_different; |
98 } | 98 } |
99 | 99 |
100 // Look for extra right lhs pixels. They should be white. | 100 // Look for extra right lhs pixels. They should be white. |
101 for (int x = width; x < size_.width(); ++x) { | 101 for (int x = width; x < size_.width(); ++x) { |
102 uint32 lhs_pixel = pixel_at(x, y); | 102 uint32_t lhs_pixel = pixel_at(x, y); |
103 if (lhs_pixel != Color(SK_ColorWHITE)) | 103 if (lhs_pixel != Color(SK_ColorWHITE)) |
104 ++pixels_different; | 104 ++pixels_different; |
105 } | 105 } |
106 | 106 |
107 // Look for extra right rhs pixels. They should be white. | 107 // Look for extra right rhs pixels. They should be white. |
108 for (int x = width; x < rhs.size_.width(); ++x) { | 108 for (int x = width; x < rhs.size_.width(); ++x) { |
109 uint32 rhs_pixel = rhs.pixel_at(x, y); | 109 uint32_t rhs_pixel = rhs.pixel_at(x, y); |
110 if (rhs_pixel != Color(SK_ColorWHITE)) | 110 if (rhs_pixel != Color(SK_ColorWHITE)) |
111 ++pixels_different; | 111 ++pixels_different; |
112 } | 112 } |
113 } | 113 } |
114 | 114 |
115 // Look for extra bottom lhs pixels. They should be white. | 115 // Look for extra bottom lhs pixels. They should be white. |
116 for (int y = height; y < size_.height(); ++y) { | 116 for (int y = height; y < size_.height(); ++y) { |
117 for (int x = 0; x < size_.width(); ++x) { | 117 for (int x = 0; x < size_.width(); ++x) { |
118 uint32 lhs_pixel = pixel_at(x, y); | 118 uint32_t lhs_pixel = pixel_at(x, y); |
119 if (lhs_pixel != Color(SK_ColorWHITE)) | 119 if (lhs_pixel != Color(SK_ColorWHITE)) |
120 ++pixels_different; | 120 ++pixels_different; |
121 } | 121 } |
122 } | 122 } |
123 | 123 |
124 // Look for extra bottom rhs pixels. They should be white. | 124 // Look for extra bottom rhs pixels. They should be white. |
125 for (int y = height; y < rhs.size_.height(); ++y) { | 125 for (int y = height; y < rhs.size_.height(); ++y) { |
126 for (int x = 0; x < rhs.size_.width(); ++x) { | 126 for (int x = 0; x < rhs.size_.width(); ++x) { |
127 uint32 rhs_pixel = rhs.pixel_at(x, y); | 127 uint32_t rhs_pixel = rhs.pixel_at(x, y); |
128 if (rhs_pixel != Color(SK_ColorWHITE)) | 128 if (rhs_pixel != Color(SK_ColorWHITE)) |
129 ++pixels_different; | 129 ++pixels_different; |
130 } | 130 } |
131 } | 131 } |
132 | 132 |
133 // Like the WebKit ImageDiff tool, we define percentage different in terms | 133 // Like the WebKit ImageDiff tool, we define percentage different in terms |
134 // of the size of the 'actual' bitmap. | 134 // of the size of the 'actual' bitmap. |
135 double total_pixels = static_cast<double>(size_.width()) * | 135 double total_pixels = static_cast<double>(size_.width()) * |
136 static_cast<double>(height); | 136 static_cast<double>(height); |
137 return static_cast<double>(pixels_different) / total_pixels * 100.; | 137 return static_cast<double>(pixels_different) / total_pixels * 100.; |
138 } | 138 } |
139 | 139 |
140 bool Image::LoadPng(const std::string& compressed) { | 140 bool Image::LoadPng(const std::string& compressed) { |
141 int w; | 141 int w; |
142 int h; | 142 int h; |
143 bool success = gfx::PNGCodec::Decode( | 143 bool success = gfx::PNGCodec::Decode( |
144 reinterpret_cast<const unsigned char*>(compressed.c_str()), | 144 reinterpret_cast<const unsigned char*>(compressed.c_str()), |
145 compressed.size(), gfx::PNGCodec::FORMAT_BGRA, &data_, &w, &h); | 145 compressed.size(), gfx::PNGCodec::FORMAT_BGRA, &data_, &w, &h); |
146 size_.SetSize(w, h); | 146 size_.SetSize(w, h); |
147 row_length_ = size_.width() * sizeof(uint32); | 147 row_length_ = size_.width() * sizeof(uint32_t); |
148 return success; | 148 return success; |
149 } | 149 } |
150 | 150 |
151 bool Image::LoadMetafile(const std::string& data) { | 151 bool Image::LoadMetafile(const std::string& data) { |
152 DCHECK(!data.empty()); | 152 DCHECK(!data.empty()); |
153 PdfMetafileSkia metafile; | 153 PdfMetafileSkia metafile; |
154 if (!metafile.InitFromData(data.data(), | 154 if (!metafile.InitFromData(data.data(), |
155 base::checked_cast<uint32>(data.size()))) | 155 base::checked_cast<uint32_t>(data.size()))) |
156 return false; | 156 return false; |
157 return LoadMetafile(metafile); | 157 return LoadMetafile(metafile); |
158 } | 158 } |
159 | 159 |
160 } // namespace printing | 160 } // namespace printing |
OLD | NEW |