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

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

Issue 10086023: Expose array of bitmaps contained by gfx::Image similar to NSImage (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Changes as requested Created 8 years, 8 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "ui/gfx/image/image_skia.h"
6
7 #include <limits>
8 #include <cmath>
9
10 #include "base/logging.h"
11 #include "base/stl_util.h"
12
13 namespace gfx {
14
15 ImageSkia::ImageSkia(const SkBitmap* bitmap)
16 : size_(bitmap->width(), bitmap->height()) {
17 DCHECK(bitmap);
sky 2012/04/20 19:31:54 Is this DCHECK useful? Seems like this would crash
18 DCHECK(!bitmap->isNull());
19 bitmaps_.push_back(bitmap);
20 }
21
22 ImageSkia::ImageSkia(const std::vector<const SkBitmap*>& bitmaps)
23 : bitmaps_(bitmaps) {
24 // Assume that the smallest bitmap represents 1x scale factor.
25 for (size_t i = 0; i < bitmaps_.size(); ++i) {
26 DCHECK(!bitmaps[i]->isNull());
27 gfx::Size bitmap_size(bitmaps_[i]->width(), bitmaps_[i]->height());
28 if (size_.IsEmpty() || bitmap_size.GetArea() < size_.GetArea())
29 size_ = bitmap_size;
30 }
31 }
32
33 ImageSkia::~ImageSkia() {
34 }
35
36 void ImageSkia::BuildMipMap() {
37 mip_map_build_pending_ = true;
38 }
39
40 void ImageSkia::DrawToCanvasInt(gfx::Canvas* canvas, int x, int y) {
41 SkPaint p;
42 DrawToCanvasInt(canvas, x, y, p);
43 }
44
45 void ImageSkia::DrawToCanvasInt(gfx::Canvas* canvas, int x, int y,
sky 2012/04/20 19:31:54 nit: each param on its own line (x/y can be groupe
46 const SkPaint& paint) {
47
48 if (IsEmpty() || size_.IsEmpty())
49 return;
50
51 SkMatrix m = canvas->sk_canvas()->getTotalMatrix();
52 float scale_x = std::abs(SkScalarToFloat(m.getScaleX()));
53 float scale_y = std::abs(SkScalarToFloat(m.getScaleY()));
54
55 SkBitmap* bitmap = const_cast<SkBitmap*>(GetBitmapForScale(scale_x, scale_y));
56
57 if (mip_map_build_pending_) {
58 bitmap->buildMipMap();
59 mip_map_build_pending_ = false;
60 }
61
62 float bitmap_scale_x = static_cast<float>(bitmap->width()) / width();
63 float bitmap_scale_y = static_cast<float>(bitmap->height()) / height();
64
65 canvas->Save();
66 canvas->sk_canvas()->scale(1.0f / bitmap_scale_x,
67 1.0f / bitmap_scale_y);
68 canvas->sk_canvas()->drawBitmap(*bitmap, SkFloatToScalar(x * bitmap_scale_x),
69 SkFloatToScalar(y * bitmap_scale_y));
70 canvas->Restore();
71 }
72
73 void ImageSkia::DrawToCanvasInt(gfx::Canvas* canvas,
74 int src_x, int src_y, int src_w, int src_h,
75 int dest_x, int dest_y, int dest_w, int dest_h,
76 bool filter) {
77 SkPaint p;
78 DrawToCanvasInt(canvas, src_x, src_y, src_w, src_h, dest_x, dest_y,
79 dest_w, dest_h, filter, p);
80 }
81
82 void ImageSkia::DrawToCanvasInt(gfx::Canvas* canvas,
83 int src_x, int src_y, int src_w, int src_h,
84 int dest_x, int dest_y, int dest_w, int dest_h,
85 bool filter,
86 const SkPaint& paint) {
87 if (IsEmpty() || size_.IsEmpty())
88 return;
89
90 SkMatrix m = canvas->sk_canvas()->getTotalMatrix();
91 float scale_x = std::abs(SkScalarToFloat(m.getScaleX()));
92 float scale_y = std::abs(SkScalarToFloat(m.getScaleY()));
93
94 SkBitmap* bitmap = const_cast<SkBitmap*>(GetBitmapForScale(scale_x, scale_y));
95
96 if (mip_map_build_pending_) {
97 bitmap->buildMipMap();
98 mip_map_build_pending_ = false;
99 }
100
101 float bitmap_scale_x = static_cast<float>(bitmap->width()) / width();
102 float bitmap_scale_y = static_cast<float>(bitmap->height()) / height();
103
104 canvas->Save();
105 canvas->sk_canvas()->scale(1.0f / bitmap_scale_x,
106 1.0f / bitmap_scale_y);
107 canvas->DrawBitmapFloat(*bitmap,
108 src_x * bitmap_scale_x, src_y * bitmap_scale_x,
109 src_w * bitmap_scale_x, src_h * bitmap_scale_y,
110 dest_x * bitmap_scale_x, dest_y * bitmap_scale_y,
111 dest_w * bitmap_scale_x, dest_h * bitmap_scale_y,
112 filter, paint);
113
114 canvas->Restore();
115 }
116
117 bool ImageSkia::IsEmpty() const {
118 return bitmaps_.empty();
119 }
120
121 const SkBitmap* ImageSkia::GetBitmapForScale(float x_scale_factor,
122 float y_scale_factor) const {
sky 2012/04/20 19:31:54 Why did you make this return a const SkBitmap when
pkotwicz 2012/04/20 20:39:55 To address your issue, I am casting the constness
123 // Get the desired bitmap width and height given |x_scale_factor|,
124 // |y_scale_factor| and |size_| at 1x density.
125 float desired_width = size_.width() * x_scale_factor;
126 float desired_height = size_.height() * y_scale_factor;
127
128 size_t closest_index;
129 float smallest_diff = std::numeric_limits<float>::max();
130 for (size_t i = 0; i < bitmaps_.size(); ++i) {
131 float diff = std::abs(bitmaps_[i]->width() - desired_width) +
132 std::abs(bitmaps_[i]->height() - desired_height);
133 if (diff < smallest_diff) {
134 closest_index = i;
135 smallest_diff = diff;
136 }
137 }
138 return bitmaps_[closest_index];
139 }
140
141 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698