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/platform_canvas.h" | 10 #include "skia/ext/platform_canvas.h" |
10 #include "ui/base/layout.h" | 11 #include "ui/base/layout.h" |
11 #include "ui/base/ui_base_switches.h" | 12 #include "ui/base/ui_base_switches.h" |
12 #include "ui/gfx/image/image_skia.h" | 13 #include "ui/gfx/image/image_skia.h" |
13 #include "ui/gfx/image/image_skia_rep.h" | 14 #include "ui/gfx/image/image_skia_rep.h" |
14 #include "ui/gfx/image/image_skia_source.h" | 15 #include "ui/gfx/image/image_skia_source.h" |
16 #include "ui/gfx/insets.h" | |
15 #include "ui/gfx/rect.h" | 17 #include "ui/gfx/rect.h" |
16 #include "ui/gfx/size.h" | 18 #include "ui/gfx/size.h" |
17 #include "ui/gfx/skbitmap_operations.h" | 19 #include "ui/gfx/skbitmap_operations.h" |
18 #include "ui/gfx/skia_util.h" | 20 #include "ui/gfx/skia_util.h" |
19 | 21 |
20 namespace gfx { | 22 namespace gfx { |
21 namespace { | 23 namespace { |
22 | 24 |
23 bool ScalingEnabled() { | 25 bool ScalingEnabled() { |
24 static bool scale_images = !CommandLine::ForCurrentProcess()->HasSwitch( | 26 static bool scale_images = !CommandLine::ForCurrentProcess()->HasSwitch( |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
134 private: | 136 private: |
135 const ImageSkia& source_; | 137 const ImageSkia& source_; |
136 const int src_x_; | 138 const int src_x_; |
137 const int src_y_; | 139 const int src_y_; |
138 const int dst_w_; | 140 const int dst_w_; |
139 const int dst_h_; | 141 const int dst_h_; |
140 | 142 |
141 DISALLOW_COPY_AND_ASSIGN(TiledImageSource); | 143 DISALLOW_COPY_AND_ASSIGN(TiledImageSource); |
142 }; | 144 }; |
143 | 145 |
146 class ResizeSource : public ImageSkiaSource { | |
sky
2012/07/16 16:49:39
Add a description.
xiyuan
2012/07/16 21:45:39
Done.
| |
147 public: | |
148 ResizeSource(const ImageSkia& source, | |
149 const Size& target_dip_size) | |
150 : source_(source), | |
151 target_dip_size_(target_dip_size) { | |
152 } | |
153 virtual ~ResizeSource() {} | |
154 | |
155 // gfx::ImageSkiaSource overrides: | |
156 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { | |
157 const ImageSkiaRep& image_rep = source_.GetRepresentation(scale_factor); | |
158 if (image_rep.GetWidth() == target_dip_size_.width() && | |
159 image_rep.GetHeight() == target_dip_size_.height()) | |
160 return image_rep; | |
161 | |
162 const float scale = image_rep.GetScale(); | |
163 const Size target_pixel_size(target_dip_size_.Scale(scale)); | |
164 const SkBitmap resized = skia::ImageOperations::Resize( | |
165 image_rep.sk_bitmap(), | |
166 skia::ImageOperations::RESIZE_BEST, | |
167 target_pixel_size.width(), | |
168 target_pixel_size.height()); | |
169 return ImageSkiaRep(resized, image_rep.scale_factor()); | |
170 } | |
171 | |
172 private: | |
173 const ImageSkia source_; | |
174 const Size target_dip_size_; | |
175 | |
176 DISALLOW_COPY_AND_ASSIGN(ResizeSource); | |
177 }; | |
178 | |
179 class DropShadowSource : public ImageSkiaSource { | |
sky
2012/07/16 16:49:39
Add Description.
xiyuan
2012/07/16 21:45:39
Done.
| |
180 public: | |
181 DropShadowSource(const ImageSkia& source, | |
182 const ShadowValues& dip_shadows) | |
183 : source_(source), | |
184 dip_shadows_(dip_shadows) { | |
185 } | |
186 virtual ~DropShadowSource() {} | |
187 | |
188 // gfx::ImageSkiaSource overrides: | |
189 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { | |
190 const ImageSkiaRep& image_rep = source_.GetRepresentation(scale_factor); | |
191 | |
192 const float scale = image_rep.GetScale(); | |
193 ShadowValues shadow_in_pixel; | |
194 for (size_t i = 0; i < dip_shadows_.size(); ++i) | |
195 shadow_in_pixel.push_back(dip_shadows_[i].Scale(scale)); | |
196 | |
197 const SkBitmap shadow_image = SkBitmapOperations::CreateDropShadow( | |
198 image_rep.sk_bitmap(), | |
199 shadow_in_pixel); | |
200 return ImageSkiaRep(shadow_image, image_rep.scale_factor()); | |
201 } | |
202 | |
203 private: | |
204 const ImageSkia source_; | |
205 const ShadowValues dip_shadows_; | |
206 | |
207 DISALLOW_COPY_AND_ASSIGN(DropShadowSource); | |
208 }; | |
209 | |
144 } // namespace; | 210 } // namespace; |
145 | 211 |
146 // static | 212 // static |
147 ImageSkia ImageSkiaOperations::CreateBlendedImage(const ImageSkia& first, | 213 ImageSkia ImageSkiaOperations::CreateBlendedImage(const ImageSkia& first, |
148 const ImageSkia& second, | 214 const ImageSkia& second, |
149 double alpha) { | 215 double alpha) { |
150 return ImageSkia(new BlendingImageSource(first, second, alpha), first.size()); | 216 return ImageSkia(new BlendingImageSource(first, second, alpha), first.size()); |
151 } | 217 } |
152 | 218 |
153 // static | 219 // static |
154 ImageSkia ImageSkiaOperations::CreateMaskedImage(const ImageSkia& rgb, | 220 ImageSkia ImageSkiaOperations::CreateMaskedImage(const ImageSkia& rgb, |
155 const ImageSkia& alpha) { | 221 const ImageSkia& alpha) { |
156 return ImageSkia(new MaskedImageSource(rgb, alpha), rgb.size()); | 222 return ImageSkia(new MaskedImageSource(rgb, alpha), rgb.size()); |
157 } | 223 } |
158 | 224 |
159 // static | 225 // static |
160 ImageSkia ImageSkiaOperations::CreateTiledImage(const ImageSkia& source, | 226 ImageSkia ImageSkiaOperations::CreateTiledImage(const ImageSkia& source, |
161 int src_x, int src_y, | 227 int src_x, int src_y, |
162 int dst_w, int dst_h) { | 228 int dst_w, int dst_h) { |
163 return ImageSkia(new TiledImageSource(source, src_x, src_y, dst_w, dst_h), | 229 return ImageSkia(new TiledImageSource(source, src_x, src_y, dst_w, dst_h), |
164 gfx::Size(dst_w, dst_h)); | 230 gfx::Size(dst_w, dst_h)); |
165 } | 231 } |
166 | 232 |
233 // static | |
234 ImageSkia ImageSkiaOperations::CreateResizedImage(const ImageSkia& source, | |
235 const Size& target_dip_size) { | |
236 return ImageSkia(new ResizeSource(source, target_dip_size), target_dip_size); | |
237 } | |
238 | |
239 // static | |
240 ImageSkia ImageSkiaOperations::CreateImageWithDropShadow( | |
241 const ImageSkia& source, | |
242 const ShadowValues& shadows) { | |
243 const gfx::Insets shadow_padding = -gfx::ShadowValue::GetMargin(shadows); | |
244 gfx::Size shadow_image_size = source.size(); | |
245 shadow_image_size.Enlarge(shadow_padding.width(), | |
246 shadow_padding.height()); | |
247 return ImageSkia(new DropShadowSource(source, shadows), shadow_image_size); | |
248 } | |
249 | |
167 } // namespace gfx | 250 } // namespace gfx |
OLD | NEW |