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

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: Move CropImageSkiaRep functionality back into ExtractSubsetImageSource. 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::Size DIPToPixelSize(gfx::Size dip_size, float scale) {
30 return ToCeiledSize(ScaleSize(dip_size, scale));
31 }
32
33 gfx::Rect DIPToPixelBounds(gfx::Rect dip_bounds, float scale) {
34 return gfx::Rect(ToFlooredPoint(ScalePoint(dip_bounds.origin(), scale)),
35 DIPToPixelSize(dip_bounds.size(), scale));
36 }
37
27 // Returns an image rep for the ImageSkiaSource to return to visually indicate 38 // Returns an image rep for the ImageSkiaSource to return to visually indicate
28 // an error. 39 // an error.
29 ImageSkiaRep GetErrorImageRep(ui::ScaleFactor scale_factor, 40 ImageSkiaRep GetErrorImageRep(ui::ScaleFactor scale_factor,
30 const gfx::Size& pixel_size) { 41 const gfx::Size& pixel_size) {
31 SkBitmap bitmap; 42 SkBitmap bitmap;
32 bitmap.setConfig( 43 bitmap.setConfig(
33 SkBitmap::kARGB_8888_Config, pixel_size.width(), pixel_size.height()); 44 SkBitmap::kARGB_8888_Config, pixel_size.width(), pixel_size.height());
34 bitmap.allocPixels(); 45 bitmap.allocPixels();
35 bitmap.eraseColor(SK_ColorRED); 46 bitmap.eraseColor(SK_ColorRED);
36 return gfx::ImageSkiaRep(bitmap, scale_factor); 47 return gfx::ImageSkiaRep(bitmap, scale_factor);
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 dst_h_(dst_h) { 220 dst_h_(dst_h) {
210 } 221 }
211 222
212 virtual ~TiledImageSource() { 223 virtual ~TiledImageSource() {
213 } 224 }
214 225
215 // gfx::ImageSkiaSource overrides: 226 // gfx::ImageSkiaSource overrides:
216 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { 227 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE {
217 ImageSkiaRep source_rep = source_.GetRepresentation(scale_factor); 228 ImageSkiaRep source_rep = source_.GetRepresentation(scale_factor);
218 float scale = ui::GetScaleFactorScale(source_rep.scale_factor()); 229 float scale = ui::GetScaleFactorScale(source_rep.scale_factor());
230 gfx::Rect bounds = DIPToPixelBounds(gfx::Rect(src_x_, src_y_, dst_w_,
231 dst_h_), scale);
219 return ImageSkiaRep( 232 return ImageSkiaRep(
220 SkBitmapOperations::CreateTiledBitmap( 233 SkBitmapOperations::CreateTiledBitmap(
221 source_rep.sk_bitmap(), 234 source_rep.sk_bitmap(),
222 src_x_ * scale, src_y_ * scale, dst_w_ * scale, dst_h_ * scale), 235 bounds.x(), bounds.y(), bounds.width(), bounds.height()),
223 source_rep.scale_factor()); 236 source_rep.scale_factor());
224 } 237 }
225 238
226 private: 239 private:
227 const ImageSkia source_; 240 const ImageSkia source_;
228 const int src_x_; 241 const int src_x_;
229 const int src_y_; 242 const int src_y_;
230 const int dst_w_; 243 const int dst_w_;
231 const int dst_h_; 244 const int dst_h_;
232 245
(...skipping 19 matching lines...) Expand all
252 hsl_shift_), image_rep.scale_factor()); 265 hsl_shift_), image_rep.scale_factor());
253 } 266 }
254 267
255 private: 268 private:
256 const gfx::ImageSkia image_; 269 const gfx::ImageSkia image_;
257 const color_utils::HSL hsl_shift_; 270 const color_utils::HSL hsl_shift_;
258 DISALLOW_COPY_AND_ASSIGN(HSLImageSource); 271 DISALLOW_COPY_AND_ASSIGN(HSLImageSource);
259 }; 272 };
260 273
261 // ImageSkiaSource which uses SkBitmapOperations::CreateButtonBackground 274 // ImageSkiaSource which uses SkBitmapOperations::CreateButtonBackground
262 // to generate image reps for the target image. 275 // to generate image reps for the target image. The image and mask can be
276 // diferent sizes (crbug.com/171725).
263 class ButtonImageSource: public gfx::ImageSkiaSource { 277 class ButtonImageSource: public gfx::ImageSkiaSource {
264 public: 278 public:
265 ButtonImageSource(SkColor color, 279 ButtonImageSource(SkColor color,
266 const ImageSkia& image, 280 const ImageSkia& image,
267 const ImageSkia& mask) 281 const ImageSkia& mask)
268 : color_(color), 282 : color_(color),
269 image_(image), 283 image_(image),
270 mask_(mask) { 284 mask_(mask) {
271 } 285 }
272 286
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 subset_bounds_(subset_bounds) { 319 subset_bounds_(subset_bounds) {
306 } 320 }
307 321
308 virtual ~ExtractSubsetImageSource() { 322 virtual ~ExtractSubsetImageSource() {
309 } 323 }
310 324
311 // gfx::ImageSkiaSource overrides: 325 // gfx::ImageSkiaSource overrides:
312 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { 326 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE {
313 ImageSkiaRep image_rep = image_.GetRepresentation(scale_factor); 327 ImageSkiaRep image_rep = image_.GetRepresentation(scale_factor);
314 float scale_to_pixel = ui::GetScaleFactorScale(image_rep.scale_factor()); 328 float scale_to_pixel = ui::GetScaleFactorScale(image_rep.scale_factor());
315 SkIRect subset_bounds_in_pixel = RectToSkIRect(ToFlooredRectDeprecated( 329 SkIRect subset_bounds_in_pixel = RectToSkIRect(
316 gfx::ScaleRect(subset_bounds_, scale_to_pixel))); 330 DIPToPixelBounds(subset_bounds_, scale_to_pixel));
317 SkBitmap dst; 331 SkBitmap dst;
318 bool success = image_rep.sk_bitmap().extractSubset(&dst, 332 bool success = image_rep.sk_bitmap().extractSubset(&dst,
319 subset_bounds_in_pixel); 333 subset_bounds_in_pixel);
320 DCHECK(success); 334 DCHECK(success);
321 return gfx::ImageSkiaRep(dst, image_rep.scale_factor()); 335 return gfx::ImageSkiaRep(dst, image_rep.scale_factor());
322 } 336 }
323 337
324 private: 338 private:
325 const gfx::ImageSkia image_; 339 const gfx::ImageSkia image_;
326 const gfx::Rect subset_bounds_; 340 const gfx::Rect subset_bounds_;
(...skipping 15 matching lines...) Expand all
342 virtual ~ResizeSource() {} 356 virtual ~ResizeSource() {}
343 357
344 // gfx::ImageSkiaSource overrides: 358 // gfx::ImageSkiaSource overrides:
345 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { 359 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE {
346 const ImageSkiaRep& image_rep = source_.GetRepresentation(scale_factor); 360 const ImageSkiaRep& image_rep = source_.GetRepresentation(scale_factor);
347 if (image_rep.GetWidth() == target_dip_size_.width() && 361 if (image_rep.GetWidth() == target_dip_size_.width() &&
348 image_rep.GetHeight() == target_dip_size_.height()) 362 image_rep.GetHeight() == target_dip_size_.height())
349 return image_rep; 363 return image_rep;
350 364
351 const float scale = ui::GetScaleFactorScale(scale_factor); 365 const float scale = ui::GetScaleFactorScale(scale_factor);
352 const Size target_pixel_size = gfx::ToFlooredSize( 366 const Size target_pixel_size = DIPToPixelSize(target_dip_size_, scale);
353 gfx::ScaleSize(target_dip_size_, scale));
354 const SkBitmap resized = skia::ImageOperations::Resize( 367 const SkBitmap resized = skia::ImageOperations::Resize(
355 image_rep.sk_bitmap(), 368 image_rep.sk_bitmap(),
356 resize_method_, 369 resize_method_,
357 target_pixel_size.width(), 370 target_pixel_size.width(),
358 target_pixel_size.height()); 371 target_pixel_size.height());
359 return ImageSkiaRep(resized, scale_factor); 372 return ImageSkiaRep(resized, scale_factor);
360 } 373 }
361 374
362 private: 375 private:
363 const ImageSkia source_; 376 const ImageSkia source_;
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
545 return ImageSkia(); 558 return ImageSkia();
546 559
547 return ImageSkia(new RotatedSource(source, rotation), 560 return ImageSkia(new RotatedSource(source, rotation),
548 SkBitmapOperations::ROTATION_180_CW == rotation ? 561 SkBitmapOperations::ROTATION_180_CW == rotation ?
549 source.size() : 562 source.size() :
550 gfx::Size(source.height(), source.width())); 563 gfx::Size(source.height(), source.width()));
551 564
552 } 565 }
553 566
554 } // namespace gfx 567 } // 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