OLD | NEW |
---|---|
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 "skia/ext/platform_canvas.h" | 10 #include "skia/ext/platform_canvas.h" |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 const Size target_pixel_size(target_size_in_dip_.Scale(scale)); | 82 const Size target_pixel_size(target_size_in_dip_.Scale(scale)); |
83 const SkBitmap resized = skia::ImageOperations::Resize( | 83 const SkBitmap resized = skia::ImageOperations::Resize( |
84 image_rep.sk_bitmap(), | 84 image_rep.sk_bitmap(), |
85 skia::ImageOperations::RESIZE_BEST, | 85 skia::ImageOperations::RESIZE_BEST, |
86 target_pixel_size.width(), | 86 target_pixel_size.width(), |
87 target_pixel_size.height()); | 87 target_pixel_size.height()); |
88 return ImageSkiaRep(resized, image_rep.scale_factor()); | 88 return ImageSkiaRep(resized, image_rep.scale_factor()); |
89 } | 89 } |
90 | 90 |
91 private: | 91 private: |
92 const ImageSkia source_; | 92 const ImageSkia source_; |
sky
2012/07/16 20:56:10
All of these classes roughly follow the same patte
oshima
2012/07/16 21:08:35
My plan is to make ScaleFactor an object which con
| |
93 const Size target_size_in_dip_; | 93 const Size target_size_in_dip_; |
94 | 94 |
95 DISALLOW_COPY_AND_ASSIGN(ResizingImageSource); | 95 DISALLOW_COPY_AND_ASSIGN(ResizingImageSource); |
96 }; | 96 }; |
97 | 97 |
98 class BlendingImageSource : public gfx::ImageSkiaSource { | 98 class BlendingImageSource : public gfx::ImageSkiaSource { |
99 public: | 99 public: |
100 BlendingImageSource(const ImageSkia& first, | 100 BlendingImageSource(const ImageSkia& first, |
101 const ImageSkia& second, | 101 const ImageSkia& second, |
102 double alpha) | 102 double alpha) |
103 : first_(first), | 103 : first_(first), |
104 second_(second), | 104 second_(second), |
105 alpha_(alpha) { | 105 alpha_(alpha) { |
106 } | 106 } |
107 | 107 |
108 virtual ~BlendingImageSource() { | |
109 } | |
110 | |
108 // gfx::ImageSkiaSource overrides: | 111 // gfx::ImageSkiaSource overrides: |
109 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { | 112 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
110 ImageSkiaRep first_rep = first_.GetRepresentation(scale_factor); | 113 ImageSkiaRep first_rep = first_.GetRepresentation(scale_factor); |
111 ImageSkiaRep second_rep = second_.GetRepresentation(scale_factor); | 114 ImageSkiaRep second_rep = second_.GetRepresentation(scale_factor); |
112 MatchScale(&first_rep, &second_rep); | 115 MatchScale(&first_rep, &second_rep); |
113 SkBitmap blended = SkBitmapOperations::CreateBlendedBitmap( | 116 SkBitmap blended = SkBitmapOperations::CreateBlendedBitmap( |
114 first_rep.sk_bitmap(), second_rep.sk_bitmap(), alpha_); | 117 first_rep.sk_bitmap(), second_rep.sk_bitmap(), alpha_); |
115 return ImageSkiaRep(blended, first_rep.scale_factor()); | 118 return ImageSkiaRep(blended, first_rep.scale_factor()); |
116 } | 119 } |
117 | 120 |
118 private: | 121 private: |
119 const ImageSkia first_; | 122 const ImageSkia first_; |
120 const ImageSkia second_; | 123 const ImageSkia second_; |
121 double alpha_; | 124 double alpha_; |
122 | 125 |
123 DISALLOW_COPY_AND_ASSIGN(BlendingImageSource); | 126 DISALLOW_COPY_AND_ASSIGN(BlendingImageSource); |
124 }; | 127 }; |
125 | 128 |
126 class MaskedImageSource : public gfx::ImageSkiaSource { | 129 class MaskedImageSource : public gfx::ImageSkiaSource { |
127 public: | 130 public: |
128 MaskedImageSource(const ImageSkia& rgb, const ImageSkia& alpha) | 131 MaskedImageSource(const ImageSkia& rgb, const ImageSkia& alpha) |
129 : rgb_(rgb), | 132 : rgb_(rgb), |
130 alpha_(alpha) { | 133 alpha_(alpha) { |
131 } | 134 } |
132 | 135 |
136 virtual ~MaskedImageSource() { | |
137 } | |
138 | |
133 // gfx::ImageSkiaSource overrides: | 139 // gfx::ImageSkiaSource overrides: |
134 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { | 140 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
135 ImageSkiaRep rgb_rep = rgb_.GetRepresentation(scale_factor); | 141 ImageSkiaRep rgb_rep = rgb_.GetRepresentation(scale_factor); |
136 ImageSkiaRep alpha_rep = alpha_.GetRepresentation(scale_factor); | 142 ImageSkiaRep alpha_rep = alpha_.GetRepresentation(scale_factor); |
137 MatchScale(&rgb_rep, &alpha_rep); | 143 MatchScale(&rgb_rep, &alpha_rep); |
138 return ImageSkiaRep(SkBitmapOperations::CreateMaskedBitmap( | 144 return ImageSkiaRep(SkBitmapOperations::CreateMaskedBitmap( |
139 rgb_rep.sk_bitmap(), alpha_rep.sk_bitmap()), | 145 rgb_rep.sk_bitmap(), alpha_rep.sk_bitmap()), |
140 rgb_rep.scale_factor()); | 146 rgb_rep.scale_factor()); |
141 } | 147 } |
142 | 148 |
143 private: | 149 private: |
144 const ImageSkia rgb_; | 150 const ImageSkia rgb_; |
145 const ImageSkia alpha_; | 151 const ImageSkia alpha_; |
146 | 152 |
147 DISALLOW_COPY_AND_ASSIGN(MaskedImageSource); | 153 DISALLOW_COPY_AND_ASSIGN(MaskedImageSource); |
148 }; | 154 }; |
149 | 155 |
150 class TiledImageSource : public gfx::ImageSkiaSource { | 156 class TiledImageSource : public gfx::ImageSkiaSource { |
151 public: | 157 public: |
152 TiledImageSource(const ImageSkia& source, | 158 TiledImageSource(const ImageSkia& source, |
153 int src_x, int src_y, | 159 int src_x, int src_y, |
154 int dst_w, int dst_h) | 160 int dst_w, int dst_h) |
155 : source_(source), | 161 : source_(source), |
156 src_x_(src_x), | 162 src_x_(src_x), |
157 src_y_(src_y), | 163 src_y_(src_y), |
158 dst_w_(dst_w), | 164 dst_w_(dst_w), |
159 dst_h_(dst_h) { | 165 dst_h_(dst_h) { |
160 } | 166 } |
161 | 167 |
168 virtual ~TiledImageSource() { | |
169 } | |
170 | |
162 // gfx::ImageSkiaSource overrides: | 171 // gfx::ImageSkiaSource overrides: |
163 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { | 172 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
164 ImageSkiaRep source_rep = source_.GetRepresentation(scale_factor); | 173 ImageSkiaRep source_rep = source_.GetRepresentation(scale_factor); |
165 float scale = ui::GetScaleFactorScale(source_rep.scale_factor()); | 174 float scale = ui::GetScaleFactorScale(source_rep.scale_factor()); |
166 return ImageSkiaRep( | 175 return ImageSkiaRep( |
167 SkBitmapOperations::CreateTiledBitmap( | 176 SkBitmapOperations::CreateTiledBitmap( |
168 source_rep.sk_bitmap(), | 177 source_rep.sk_bitmap(), |
169 src_x_ * scale, src_y_ * scale, dst_w_ * scale, dst_h_ * scale), | 178 src_x_ * scale, src_y_ * scale, dst_w_ * scale, dst_h_ * scale), |
170 source_rep.scale_factor()); | 179 source_rep.scale_factor()); |
171 } | 180 } |
172 | 181 |
173 private: | 182 private: |
174 const ImageSkia& source_; | 183 const ImageSkia& source_; |
175 const int src_x_; | 184 const int src_x_; |
176 const int src_y_; | 185 const int src_y_; |
177 const int dst_w_; | 186 const int dst_w_; |
178 const int dst_h_; | 187 const int dst_h_; |
179 | 188 |
180 DISALLOW_COPY_AND_ASSIGN(TiledImageSource); | 189 DISALLOW_COPY_AND_ASSIGN(TiledImageSource); |
181 }; | 190 }; |
182 | 191 |
192 // ImageSkiaSource which uses SkBitmapOperations::CreateButtonBackground | |
193 // to generate image reps for the target image. | |
194 class ButtonImageSource: public gfx::ImageSkiaSource { | |
195 public: | |
196 ButtonImageSource(SkColor color, | |
197 const ImageSkia& image, | |
198 const ImageSkia& mask) | |
199 : color_(color), | |
200 image_(image), | |
201 mask_(mask) { | |
202 } | |
203 | |
204 virtual ~ButtonImageSource() { | |
205 } | |
206 | |
207 // gfx::ImageSkiaSource overrides: | |
208 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { | |
209 ImageSkiaRep image_rep = image_.GetRepresentation(scale_factor); | |
210 ImageSkiaRep mask_rep = mask_.GetRepresentation(scale_factor); | |
211 MatchScale(&image_rep, &mask_rep); | |
212 return ImageSkiaRep( | |
213 SkBitmapOperations::CreateButtonBackground(color_, | |
214 image_rep.sk_bitmap(), mask_rep.sk_bitmap()), | |
215 image_rep.scale_factor()); | |
216 } | |
217 | |
218 private: | |
219 const SkColor color_; | |
220 const ImageSkia image_; | |
221 const ImageSkia mask_; | |
222 | |
223 DISALLOW_COPY_AND_ASSIGN(ButtonImageSource); | |
224 }; | |
225 | |
226 // ImageSkiaSource which uses SkBitmap::extractSubset to generate image reps | |
227 // for the target image. | |
228 class ExtractSubsetImageSource: public gfx::ImageSkiaSource { | |
229 public: | |
230 ExtractSubsetImageSource(const gfx::ImageSkia& image, | |
231 const gfx::Rect& subset_bounds) | |
232 : image_(image), | |
233 subset_bounds_(subset_bounds) { | |
234 } | |
235 | |
236 ~ExtractSubsetImageSource() { | |
237 } | |
238 | |
239 // gfx::ImageSkiaSource overrides: | |
240 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { | |
241 ImageSkiaRep image_rep = image_.GetRepresentation(scale_factor); | |
242 SkIRect subset_bounds_in_pixel = RectToSkIRect(subset_bounds_.Scale( | |
243 ui::GetScaleFactorScale(image_rep.scale_factor()))); | |
244 SkBitmap dst; | |
245 bool success = image_rep.sk_bitmap().extractSubset(&dst, | |
246 subset_bounds_in_pixel); | |
247 DCHECK(success); | |
248 return gfx::ImageSkiaRep(dst, image_rep.scale_factor()); | |
249 } | |
250 | |
251 private: | |
252 const gfx::ImageSkia image_; | |
253 const gfx::Rect subset_bounds_; | |
254 | |
255 DISALLOW_COPY_AND_ASSIGN(ExtractSubsetImageSource); | |
256 }; | |
257 | |
183 } // namespace; | 258 } // namespace; |
184 | 259 |
185 // static | 260 // static |
186 ImageSkia ImageSkiaOperations::CreateBlendedImage(const ImageSkia& first, | 261 ImageSkia ImageSkiaOperations::CreateBlendedImage(const ImageSkia& first, |
187 const ImageSkia& second, | 262 const ImageSkia& second, |
188 double alpha) { | 263 double alpha) { |
189 return ImageSkia(new BlendingImageSource(first, second, alpha), first.size()); | 264 return ImageSkia(new BlendingImageSource(first, second, alpha), first.size()); |
190 } | 265 } |
191 | 266 |
192 // static | 267 // static |
(...skipping 11 matching lines...) Expand all Loading... | |
204 } | 279 } |
205 | 280 |
206 // static | 281 // static |
207 ImageSkia ImageSkiaOperations::CreateResizedImage( | 282 ImageSkia ImageSkiaOperations::CreateResizedImage( |
208 const ImageSkia& source, | 283 const ImageSkia& source, |
209 const gfx::Size& target_size_in_dip) { | 284 const gfx::Size& target_size_in_dip) { |
210 return ImageSkia(new ResizingImageSource(source, target_size_in_dip), | 285 return ImageSkia(new ResizingImageSource(source, target_size_in_dip), |
211 target_size_in_dip); | 286 target_size_in_dip); |
212 } | 287 } |
213 | 288 |
289 // static | |
290 ImageSkia ImageSkiaOperations::CreateButtonBackground(SkColor color, | |
sky
2012/07/16 21:23:40
This is only called in one place, should it be mov
pkotwicz
2012/07/17 01:15:41
I would rather keep it here, especially as it is p
| |
291 const ImageSkia& image, | |
292 const ImageSkia& mask) { | |
293 return ImageSkia(new ButtonImageSource(color, image, mask), mask.size()); | |
294 } | |
295 | |
296 // static | |
297 ImageSkia ImageSkiaOperations::ExtractSubset(const ImageSkia& image, | |
298 const Rect& subset_bounds) { | |
299 gfx::Rect clipped_bounds = subset_bounds.Intersect(gfx::Rect(image.size())); | |
300 if (image.isNull() || clipped_bounds.IsEmpty()) { | |
301 return ImageSkia(); | |
302 } | |
303 | |
304 return ImageSkia(new ExtractSubsetImageSource(image, clipped_bounds), | |
305 clipped_bounds.size()); | |
306 } | |
307 | |
214 } // namespace gfx | 308 } // namespace gfx |
OLD | NEW |