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 // ResizeSource resizes relevant image reps in |source| to |target_dip_size| |
| 147 // for requested scale factors. |
| 148 class ResizeSource : public ImageSkiaSource { |
| 149 public: |
| 150 ResizeSource(const ImageSkia& source, |
| 151 const Size& target_dip_size) |
| 152 : source_(source), |
| 153 target_dip_size_(target_dip_size) { |
| 154 } |
| 155 virtual ~ResizeSource() {} |
| 156 |
| 157 // gfx::ImageSkiaSource overrides: |
| 158 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
| 159 const ImageSkiaRep& image_rep = source_.GetRepresentation(scale_factor); |
| 160 if (image_rep.GetWidth() == target_dip_size_.width() && |
| 161 image_rep.GetHeight() == target_dip_size_.height()) |
| 162 return image_rep; |
| 163 |
| 164 const float scale = image_rep.GetScale(); |
| 165 const Size target_pixel_size(target_dip_size_.Scale(scale)); |
| 166 const SkBitmap resized = skia::ImageOperations::Resize( |
| 167 image_rep.sk_bitmap(), |
| 168 skia::ImageOperations::RESIZE_BEST, |
| 169 target_pixel_size.width(), |
| 170 target_pixel_size.height()); |
| 171 return ImageSkiaRep(resized, image_rep.scale_factor()); |
| 172 } |
| 173 |
| 174 private: |
| 175 const ImageSkia source_; |
| 176 const Size target_dip_size_; |
| 177 |
| 178 DISALLOW_COPY_AND_ASSIGN(ResizeSource); |
| 179 }; |
| 180 |
| 181 // DropShadowSource generates image reps with drop shadow for image reps in |
| 182 // |source| that represent requested scale factors. |
| 183 class DropShadowSource : public ImageSkiaSource { |
| 184 public: |
| 185 DropShadowSource(const ImageSkia& source, |
| 186 const ShadowValues& shadows_in_dip) |
| 187 : source_(source), |
| 188 shaodws_in_dip_(shadows_in_dip) { |
| 189 } |
| 190 virtual ~DropShadowSource() {} |
| 191 |
| 192 // gfx::ImageSkiaSource overrides: |
| 193 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
| 194 const ImageSkiaRep& image_rep = source_.GetRepresentation(scale_factor); |
| 195 |
| 196 const float scale = image_rep.GetScale(); |
| 197 ShadowValues shaodws_in_pixel; |
| 198 for (size_t i = 0; i < shaodws_in_dip_.size(); ++i) |
| 199 shaodws_in_pixel.push_back(shaodws_in_dip_[i].Scale(scale)); |
| 200 |
| 201 const SkBitmap shaodw_bitmap = SkBitmapOperations::CreateDropShadow( |
| 202 image_rep.sk_bitmap(), |
| 203 shaodws_in_pixel); |
| 204 return ImageSkiaRep(shaodw_bitmap, image_rep.scale_factor()); |
| 205 } |
| 206 |
| 207 private: |
| 208 const ImageSkia source_; |
| 209 const ShadowValues shaodws_in_dip_; |
| 210 |
| 211 DISALLOW_COPY_AND_ASSIGN(DropShadowSource); |
| 212 }; |
| 213 |
144 } // namespace; | 214 } // namespace; |
145 | 215 |
146 // static | 216 // static |
147 ImageSkia ImageSkiaOperations::CreateBlendedImage(const ImageSkia& first, | 217 ImageSkia ImageSkiaOperations::CreateBlendedImage(const ImageSkia& first, |
148 const ImageSkia& second, | 218 const ImageSkia& second, |
149 double alpha) { | 219 double alpha) { |
150 return ImageSkia(new BlendingImageSource(first, second, alpha), first.size()); | 220 return ImageSkia(new BlendingImageSource(first, second, alpha), first.size()); |
151 } | 221 } |
152 | 222 |
153 // static | 223 // static |
154 ImageSkia ImageSkiaOperations::CreateMaskedImage(const ImageSkia& rgb, | 224 ImageSkia ImageSkiaOperations::CreateMaskedImage(const ImageSkia& rgb, |
155 const ImageSkia& alpha) { | 225 const ImageSkia& alpha) { |
156 return ImageSkia(new MaskedImageSource(rgb, alpha), rgb.size()); | 226 return ImageSkia(new MaskedImageSource(rgb, alpha), rgb.size()); |
157 } | 227 } |
158 | 228 |
159 // static | 229 // static |
160 ImageSkia ImageSkiaOperations::CreateTiledImage(const ImageSkia& source, | 230 ImageSkia ImageSkiaOperations::CreateTiledImage(const ImageSkia& source, |
161 int src_x, int src_y, | 231 int src_x, int src_y, |
162 int dst_w, int dst_h) { | 232 int dst_w, int dst_h) { |
163 return ImageSkia(new TiledImageSource(source, src_x, src_y, dst_w, dst_h), | 233 return ImageSkia(new TiledImageSource(source, src_x, src_y, dst_w, dst_h), |
164 gfx::Size(dst_w, dst_h)); | 234 gfx::Size(dst_w, dst_h)); |
165 } | 235 } |
166 | 236 |
| 237 // static |
| 238 ImageSkia ImageSkiaOperations::CreateResizedImage(const ImageSkia& source, |
| 239 const Size& target_dip_size) { |
| 240 return ImageSkia(new ResizeSource(source, target_dip_size), target_dip_size); |
| 241 } |
| 242 |
| 243 // static |
| 244 ImageSkia ImageSkiaOperations::CreateImageWithDropShadow( |
| 245 const ImageSkia& source, |
| 246 const ShadowValues& shadows) { |
| 247 const gfx::Insets shadow_padding = -gfx::ShadowValue::GetMargin(shadows); |
| 248 gfx::Size shadow_image_size = source.size(); |
| 249 shadow_image_size.Enlarge(shadow_padding.width(), |
| 250 shadow_padding.height()); |
| 251 return ImageSkia(new DropShadowSource(source, shadows), shadow_image_size); |
| 252 } |
| 253 |
167 } // namespace gfx | 254 } // namespace gfx |
OLD | NEW |