Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: ui/gfx/image/image_skia_operations.cc

Issue 12730010: Fix rounding rules for skia operations to work with non-integer scaling factors. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fix nits. Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gfx/canvas.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "ui/gfx/image/image_skia_operations.h" 5 #include "ui/gfx/image/image_skia_operations.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "skia/ext/image_operations.h" 9 #include "skia/ext/image_operations.h"
10 #include "ui/base/layout.h" 10 #include "ui/base/layout.h"
11 #include "ui/gfx/canvas.h" 11 #include "ui/gfx/canvas.h"
12 #include "ui/gfx/image/canvas_image_source.h" 12 #include "ui/gfx/image/canvas_image_source.h"
13 #include "ui/gfx/image/image_skia.h" 13 #include "ui/gfx/image/image_skia.h"
14 #include "ui/gfx/image/image_skia_rep.h" 14 #include "ui/gfx/image/image_skia_rep.h"
15 #include "ui/gfx/image/image_skia_source.h" 15 #include "ui/gfx/image/image_skia_source.h"
16 #include "ui/gfx/insets.h" 16 #include "ui/gfx/insets.h"
17 #include "ui/gfx/point.h"
18 #include "ui/gfx/point_conversions.h"
17 #include "ui/gfx/rect.h" 19 #include "ui/gfx/rect.h"
18 #include "ui/gfx/rect_conversions.h" 20 #include "ui/gfx/rect_conversions.h"
19 #include "ui/gfx/size.h" 21 #include "ui/gfx/size.h"
20 #include "ui/gfx/size_conversions.h" 22 #include "ui/gfx/size_conversions.h"
21 #include "ui/gfx/skbitmap_operations.h" 23 #include "ui/gfx/skbitmap_operations.h"
22 #include "ui/gfx/skia_util.h" 24 #include "ui/gfx/skia_util.h"
23 25
24 namespace gfx { 26 namespace gfx {
25 namespace { 27 namespace {
26 28
29 gfx::Rect DIPToPixelBounds(gfx::Rect dip_bounds, float scale) {
30 return ToEnclosingRect(ScaleRect(dip_bounds, scale));
31 }
32
33 gfx::Size DIPToPixelSize(gfx::Size dip_size, float scale) {
34 return ToCeiledSize(ScaleSize(dip_size, scale));
35 }
36
37 ImageSkiaRep CropImageSkiaRep(ImageSkiaRep& source,
38 gfx::Rect& bounds_in_pixel) {
39 SkBitmap result;
40 bool success = source.sk_bitmap().extractSubset(&result,
41 RectToSkIRect(bounds_in_pixel));
42 DCHECK(success);
43 return ImageSkiaRep(result, source.scale_factor());
44 }
45
27 // Returns an image rep for the ImageSkiaSource to return to visually indicate 46 // Returns an image rep for the ImageSkiaSource to return to visually indicate
28 // an error. 47 // an error.
29 ImageSkiaRep GetErrorImageRep(ui::ScaleFactor scale_factor, 48 ImageSkiaRep GetErrorImageRep(ui::ScaleFactor scale_factor,
30 const gfx::Size& pixel_size) { 49 const gfx::Size& pixel_size) {
31 SkBitmap bitmap; 50 SkBitmap bitmap;
32 bitmap.setConfig( 51 bitmap.setConfig(
33 SkBitmap::kARGB_8888_Config, pixel_size.width(), pixel_size.height()); 52 SkBitmap::kARGB_8888_Config, pixel_size.width(), pixel_size.height());
34 bitmap.allocPixels(); 53 bitmap.allocPixels();
35 bitmap.eraseColor(SK_ColorRED); 54 bitmap.eraseColor(SK_ColorRED);
36 return gfx::ImageSkiaRep(bitmap, scale_factor); 55 return gfx::ImageSkiaRep(bitmap, scale_factor);
(...skipping 10 matching lines...) Expand all
47 second_(second), 66 second_(second),
48 source_name_(source_name) { 67 source_name_(source_name) {
49 } 68 }
50 virtual ~BinaryImageSource() { 69 virtual ~BinaryImageSource() {
51 } 70 }
52 71
53 // gfx::ImageSkiaSource overrides: 72 // gfx::ImageSkiaSource overrides:
54 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { 73 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE {
55 ImageSkiaRep first_rep = first_.GetRepresentation(scale_factor); 74 ImageSkiaRep first_rep = first_.GetRepresentation(scale_factor);
56 ImageSkiaRep second_rep = second_.GetRepresentation(scale_factor); 75 ImageSkiaRep second_rep = second_.GetRepresentation(scale_factor);
57 if (first_rep.pixel_size() != second_rep.pixel_size()) { 76 // Allow for maximum of 1 pixel mismatch due to rounding errors when using
77 // fractional scale factors.
78 int dw = first_rep.pixel_width() - second_rep.pixel_width();
79 int dh = first_rep.pixel_height() - second_rep.pixel_height();
oshima 2013/03/13 14:47:52 drive-by question: where / in which process the ro
80 if (abs(dw) > 1 || abs(dh) > 1) {
58 DCHECK_NE(first_rep.scale_factor(), second_rep.scale_factor()); 81 DCHECK_NE(first_rep.scale_factor(), second_rep.scale_factor());
59 if (first_rep.scale_factor() == second_rep.scale_factor()) { 82 if (first_rep.scale_factor() == second_rep.scale_factor()) {
60 LOG(ERROR) << "ImageSkiaRep size mismatch in " << source_name_; 83 LOG(ERROR) << "ImageSkiaRep size mismatch in " << source_name_;
61 return GetErrorImageRep(first_rep.scale_factor(), 84 return GetErrorImageRep(first_rep.scale_factor(),
62 first_rep.pixel_size()); 85 first_rep.pixel_size());
63 } 86 }
64 first_rep = first_.GetRepresentation(ui::SCALE_FACTOR_100P); 87 first_rep = first_.GetRepresentation(ui::SCALE_FACTOR_100P);
65 second_rep = second_.GetRepresentation(ui::SCALE_FACTOR_100P); 88 second_rep = second_.GetRepresentation(ui::SCALE_FACTOR_100P);
66 DCHECK_EQ(first_rep.pixel_width(), second_rep.pixel_width()); 89 dw = first_rep.pixel_width() - second_rep.pixel_width();
67 DCHECK_EQ(first_rep.pixel_height(), second_rep.pixel_height()); 90 dh = first_rep.pixel_height() - second_rep.pixel_height();
68 if (first_rep.pixel_size() != second_rep.pixel_size()) { 91 DCHECK_LE(abs(dw), 1);
92 DCHECK_LE(abs(dh), 1);
93 if (abs(dw) > 1 || abs(dh) > 1) {
69 LOG(ERROR) << "ImageSkiaRep size mismatch in " << source_name_; 94 LOG(ERROR) << "ImageSkiaRep size mismatch in " << source_name_;
70 return GetErrorImageRep(first_rep.scale_factor(), 95 return GetErrorImageRep(first_rep.scale_factor(),
71 first_rep.pixel_size()); 96 first_rep.pixel_size());
72 } 97 }
73 } else { 98 } else {
74 DCHECK_EQ(first_rep.scale_factor(), second_rep.scale_factor()); 99 DCHECK_EQ(first_rep.scale_factor(), second_rep.scale_factor());
75 } 100 }
101 if (dw || dh) {
102 gfx::Rect bounds = IntersectRects(Rect(first_rep.pixel_size()),
103 Rect(second_rep.pixel_size()));
104 first_rep = CropImageSkiaRep(first_rep, bounds);
105 second_rep = CropImageSkiaRep(second_rep, bounds);
106 }
76 return CreateImageSkiaRep(first_rep, second_rep); 107 return CreateImageSkiaRep(first_rep, second_rep);
77 } 108 }
78 109
79 // Creates a final image from two ImageSkiaReps. The pixel size of 110 // Creates a final image from two ImageSkiaReps. The pixel size of
80 // the two images are guaranteed to be the same. 111 // the two images are guaranteed to be the same.
81 virtual ImageSkiaRep CreateImageSkiaRep( 112 virtual ImageSkiaRep CreateImageSkiaRep(
82 const ImageSkiaRep& first_rep, 113 const ImageSkiaRep& first_rep,
83 const ImageSkiaRep& second_rep) const = 0; 114 const ImageSkiaRep& second_rep) const = 0;
84 115
85 private: 116 private:
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 dst_h_(dst_h) { 240 dst_h_(dst_h) {
210 } 241 }
211 242
212 virtual ~TiledImageSource() { 243 virtual ~TiledImageSource() {
213 } 244 }
214 245
215 // gfx::ImageSkiaSource overrides: 246 // gfx::ImageSkiaSource overrides:
216 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { 247 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE {
217 ImageSkiaRep source_rep = source_.GetRepresentation(scale_factor); 248 ImageSkiaRep source_rep = source_.GetRepresentation(scale_factor);
218 float scale = ui::GetScaleFactorScale(source_rep.scale_factor()); 249 float scale = ui::GetScaleFactorScale(source_rep.scale_factor());
250 gfx::Rect bounds = DIPToPixelBounds(gfx::Rect(src_x_, src_y_, dst_w_,
251 dst_h_), scale);
219 return ImageSkiaRep( 252 return ImageSkiaRep(
220 SkBitmapOperations::CreateTiledBitmap( 253 SkBitmapOperations::CreateTiledBitmap(
221 source_rep.sk_bitmap(), 254 source_rep.sk_bitmap(),
222 src_x_ * scale, src_y_ * scale, dst_w_ * scale, dst_h_ * scale), 255 bounds.x(), bounds.y(), bounds.width(), bounds.height()),
223 source_rep.scale_factor()); 256 source_rep.scale_factor());
224 } 257 }
225 258
226 private: 259 private:
227 const ImageSkia source_; 260 const ImageSkia source_;
228 const int src_x_; 261 const int src_x_;
229 const int src_y_; 262 const int src_y_;
230 const int dst_w_; 263 const int dst_w_;
231 const int dst_h_; 264 const int dst_h_;
232 265
(...skipping 20 matching lines...) Expand all
253 } 286 }
254 287
255 private: 288 private:
256 const gfx::ImageSkia image_; 289 const gfx::ImageSkia image_;
257 const color_utils::HSL hsl_shift_; 290 const color_utils::HSL hsl_shift_;
258 DISALLOW_COPY_AND_ASSIGN(HSLImageSource); 291 DISALLOW_COPY_AND_ASSIGN(HSLImageSource);
259 }; 292 };
260 293
261 // ImageSkiaSource which uses SkBitmapOperations::CreateButtonBackground 294 // ImageSkiaSource which uses SkBitmapOperations::CreateButtonBackground
262 // to generate image reps for the target image. 295 // to generate image reps for the target image.
263 class ButtonImageSource: public gfx::ImageSkiaSource { 296 class ButtonImageSource: public BinaryImageSource {
264 public: 297 public:
265 ButtonImageSource(SkColor color, 298 ButtonImageSource(SkColor color,
266 const ImageSkia& image, 299 const ImageSkia& image,
267 const ImageSkia& mask) 300 const ImageSkia& mask)
268 : color_(color), 301 : BinaryImageSource(image, mask, "ButtonImageSource"),
269 image_(image), 302 color_(color) {
270 mask_(mask) {
271 } 303 }
272 304
273 virtual ~ButtonImageSource() { 305 virtual ~ButtonImageSource() {
274 } 306 }
275 307
276 // gfx::ImageSkiaSource overrides: 308 // BinaryImageSource overrides:
277 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { 309 virtual ImageSkiaRep CreateImageSkiaRep(
278 ImageSkiaRep image_rep = image_.GetRepresentation(scale_factor); 310 const ImageSkiaRep& first_rep,
279 ImageSkiaRep mask_rep = mask_.GetRepresentation(scale_factor); 311 const ImageSkiaRep& second_rep) const OVERRIDE {
280 if (image_rep.scale_factor() != mask_rep.scale_factor()) {
281 image_rep = image_.GetRepresentation(ui::SCALE_FACTOR_100P);
282 mask_rep = mask_.GetRepresentation(ui::SCALE_FACTOR_100P);
283 }
284 return gfx::ImageSkiaRep( 312 return gfx::ImageSkiaRep(
285 SkBitmapOperations::CreateButtonBackground(color_, 313 SkBitmapOperations::CreateButtonBackground(color_,
286 image_rep.sk_bitmap(), mask_rep.sk_bitmap()), 314 first_rep.sk_bitmap(), second_rep.sk_bitmap()),
287 image_rep.scale_factor()); 315 first_rep.scale_factor());
288 } 316 }
289 317
290 private: 318 private:
291 const SkColor color_; 319 const SkColor color_;
292 const ImageSkia image_;
293 const ImageSkia mask_;
294 320
295 DISALLOW_COPY_AND_ASSIGN(ButtonImageSource); 321 DISALLOW_COPY_AND_ASSIGN(ButtonImageSource);
296 }; 322 };
297 323
298 // ImageSkiaSource which uses SkBitmap::extractSubset to generate image reps 324 // ImageSkiaSource which uses SkBitmap::extractSubset to generate image reps
299 // for the target image. 325 // for the target image.
300 class ExtractSubsetImageSource: public gfx::ImageSkiaSource { 326 class ExtractSubsetImageSource: public gfx::ImageSkiaSource {
301 public: 327 public:
302 ExtractSubsetImageSource(const gfx::ImageSkia& image, 328 ExtractSubsetImageSource(const gfx::ImageSkia& image,
303 const gfx::Rect& subset_bounds) 329 const gfx::Rect& subset_bounds)
304 : image_(image), 330 : image_(image),
305 subset_bounds_(subset_bounds) { 331 subset_bounds_(subset_bounds) {
306 } 332 }
307 333
308 virtual ~ExtractSubsetImageSource() { 334 virtual ~ExtractSubsetImageSource() {
309 } 335 }
310 336
311 // gfx::ImageSkiaSource overrides: 337 // gfx::ImageSkiaSource overrides:
312 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { 338 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE {
313 ImageSkiaRep image_rep = image_.GetRepresentation(scale_factor); 339 ImageSkiaRep image_rep = image_.GetRepresentation(scale_factor);
314 float scale_to_pixel = ui::GetScaleFactorScale(image_rep.scale_factor()); 340 float scale_to_pixel = ui::GetScaleFactorScale(image_rep.scale_factor());
315 SkIRect subset_bounds_in_pixel = RectToSkIRect(ToFlooredRectDeprecated( 341 Rect subset_bounds_in_pixel = DIPToPixelBounds(subset_bounds_,
316 gfx::ScaleRect(subset_bounds_, scale_to_pixel))); 342 scale_to_pixel);
317 SkBitmap dst; 343 return CropImageSkiaRep(image_rep, subset_bounds_in_pixel);
318 bool success = image_rep.sk_bitmap().extractSubset(&dst,
319 subset_bounds_in_pixel);
320 DCHECK(success);
321 return gfx::ImageSkiaRep(dst, image_rep.scale_factor());
322 } 344 }
323 345
324 private: 346 private:
325 const gfx::ImageSkia image_; 347 const gfx::ImageSkia image_;
326 const gfx::Rect subset_bounds_; 348 const gfx::Rect subset_bounds_;
327 349
328 DISALLOW_COPY_AND_ASSIGN(ExtractSubsetImageSource); 350 DISALLOW_COPY_AND_ASSIGN(ExtractSubsetImageSource);
329 }; 351 };
330 352
331 // ResizeSource resizes relevant image reps in |source| to |target_dip_size| 353 // ResizeSource resizes relevant image reps in |source| to |target_dip_size|
(...skipping 10 matching lines...) Expand all
342 virtual ~ResizeSource() {} 364 virtual ~ResizeSource() {}
343 365
344 // gfx::ImageSkiaSource overrides: 366 // gfx::ImageSkiaSource overrides:
345 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { 367 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE {
346 const ImageSkiaRep& image_rep = source_.GetRepresentation(scale_factor); 368 const ImageSkiaRep& image_rep = source_.GetRepresentation(scale_factor);
347 if (image_rep.GetWidth() == target_dip_size_.width() && 369 if (image_rep.GetWidth() == target_dip_size_.width() &&
348 image_rep.GetHeight() == target_dip_size_.height()) 370 image_rep.GetHeight() == target_dip_size_.height())
349 return image_rep; 371 return image_rep;
350 372
351 const float scale = ui::GetScaleFactorScale(scale_factor); 373 const float scale = ui::GetScaleFactorScale(scale_factor);
352 const Size target_pixel_size = gfx::ToFlooredSize( 374 const Size target_pixel_size = DIPToPixelSize(target_dip_size_, scale);
353 gfx::ScaleSize(target_dip_size_, scale));
354 const SkBitmap resized = skia::ImageOperations::Resize( 375 const SkBitmap resized = skia::ImageOperations::Resize(
355 image_rep.sk_bitmap(), 376 image_rep.sk_bitmap(),
356 resize_method_, 377 resize_method_,
357 target_pixel_size.width(), 378 target_pixel_size.width(),
358 target_pixel_size.height()); 379 target_pixel_size.height());
359 return ImageSkiaRep(resized, scale_factor); 380 return ImageSkiaRep(resized, scale_factor);
360 } 381 }
361 382
362 private: 383 private:
363 const ImageSkia source_; 384 const ImageSkia source_;
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
545 return ImageSkia(); 566 return ImageSkia();
546 567
547 return ImageSkia(new RotatedSource(source, rotation), 568 return ImageSkia(new RotatedSource(source, rotation),
548 SkBitmapOperations::ROTATION_180_CW == rotation ? 569 SkBitmapOperations::ROTATION_180_CW == rotation ?
549 source.size() : 570 source.size() :
550 gfx::Size(source.height(), source.width())); 571 gfx::Size(source.height(), source.width()));
551 572
552 } 573 }
553 574
554 } // namespace gfx 575 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/canvas.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698