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 { |
| 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 private: |
| 156 // gfx::ImageSkiaSource overrides: |
| 157 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
| 158 const ImageSkiaRep& image_rep = source_.GetRepresentation(scale_factor); |
| 159 if (image_rep.GetWidth() == target_dip_size_.width() && |
| 160 image_rep.GetHeight() == target_dip_size_.height()) |
| 161 return image_rep; |
| 162 |
| 163 const float scale = image_rep.GetScale(); |
| 164 const Size target_pixel_size(target_dip_size_.Scale(scale)); |
| 165 const SkBitmap resized = skia::ImageOperations::Resize( |
| 166 image_rep.sk_bitmap(), |
| 167 skia::ImageOperations::RESIZE_BEST, |
| 168 target_pixel_size.width(), |
| 169 target_pixel_size.height()); |
| 170 return ImageSkiaRep(resized, image_rep.scale_factor()); |
| 171 } |
| 172 |
| 173 const ImageSkia source_; |
| 174 const Size target_dip_size_; |
| 175 |
| 176 DISALLOW_COPY_AND_ASSIGN(ResizeSource); |
| 177 }; |
| 178 |
| 179 class DropShadowSource : public ImageSkiaSource { |
| 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 private: |
| 189 // gfx::ImageSkiaSource overrides: |
| 190 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
| 191 const ImageSkiaRep& image_rep = source_.GetRepresentation(scale_factor); |
| 192 |
| 193 const float scale = image_rep.GetScale(); |
| 194 ShadowValues shadow_in_pixel; |
| 195 for (size_t i = 0; i < dip_shadows_.size(); ++i) |
| 196 shadow_in_pixel.push_back(dip_shadows_[i].Scale(scale)); |
| 197 |
| 198 const SkBitmap shadow_image = SkBitmapOperations::CreateDropShadow( |
| 199 image_rep.sk_bitmap(), |
| 200 shadow_in_pixel); |
| 201 return ImageSkiaRep(shadow_image, image_rep.scale_factor()); |
| 202 } |
| 203 |
| 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::CreateDropShadowImage( |
| 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 |