| 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 class BlendingImageSource : public gfx::ImageSkiaSource { | 61 class BlendingImageSource : public gfx::ImageSkiaSource { |
| 62 public: | 62 public: |
| 63 BlendingImageSource(const ImageSkia& first, | 63 BlendingImageSource(const ImageSkia& first, |
| 64 const ImageSkia& second, | 64 const ImageSkia& second, |
| 65 double alpha) | 65 double alpha) |
| 66 : first_(first), | 66 : first_(first), |
| 67 second_(second), | 67 second_(second), |
| 68 alpha_(alpha) { | 68 alpha_(alpha) { |
| 69 } | 69 } |
| 70 | 70 |
| 71 virtual ~BlendingImageSource() { |
| 72 } |
| 73 |
| 71 // gfx::ImageSkiaSource overrides: | 74 // gfx::ImageSkiaSource overrides: |
| 72 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { | 75 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
| 73 ImageSkiaRep first_rep = first_.GetRepresentation(scale_factor); | 76 ImageSkiaRep first_rep = first_.GetRepresentation(scale_factor); |
| 74 ImageSkiaRep second_rep = second_.GetRepresentation(scale_factor); | 77 ImageSkiaRep second_rep = second_.GetRepresentation(scale_factor); |
| 75 MatchScale(&first_rep, &second_rep); | 78 MatchScale(&first_rep, &second_rep); |
| 76 SkBitmap blended = SkBitmapOperations::CreateBlendedBitmap( | 79 SkBitmap blended = SkBitmapOperations::CreateBlendedBitmap( |
| 77 first_rep.sk_bitmap(), second_rep.sk_bitmap(), alpha_); | 80 first_rep.sk_bitmap(), second_rep.sk_bitmap(), alpha_); |
| 78 return ImageSkiaRep(blended, first_rep.scale_factor()); | 81 return ImageSkiaRep(blended, first_rep.scale_factor()); |
| 79 } | 82 } |
| 80 | 83 |
| 81 private: | 84 private: |
| 82 const ImageSkia first_; | 85 const ImageSkia first_; |
| 83 const ImageSkia second_; | 86 const ImageSkia second_; |
| 84 double alpha_; | 87 double alpha_; |
| 85 | 88 |
| 86 DISALLOW_COPY_AND_ASSIGN(BlendingImageSource); | 89 DISALLOW_COPY_AND_ASSIGN(BlendingImageSource); |
| 87 }; | 90 }; |
| 88 | 91 |
| 89 class MaskedImageSource : public gfx::ImageSkiaSource { | 92 class MaskedImageSource : public gfx::ImageSkiaSource { |
| 90 public: | 93 public: |
| 91 MaskedImageSource(const ImageSkia& rgb, const ImageSkia& alpha) | 94 MaskedImageSource(const ImageSkia& rgb, const ImageSkia& alpha) |
| 92 : rgb_(rgb), | 95 : rgb_(rgb), |
| 93 alpha_(alpha) { | 96 alpha_(alpha) { |
| 94 } | 97 } |
| 95 | 98 |
| 99 virtual ~MaskedImageSource() { |
| 100 } |
| 101 |
| 96 // gfx::ImageSkiaSource overrides: | 102 // gfx::ImageSkiaSource overrides: |
| 97 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { | 103 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
| 98 ImageSkiaRep rgb_rep = rgb_.GetRepresentation(scale_factor); | 104 ImageSkiaRep rgb_rep = rgb_.GetRepresentation(scale_factor); |
| 99 ImageSkiaRep alpha_rep = alpha_.GetRepresentation(scale_factor); | 105 ImageSkiaRep alpha_rep = alpha_.GetRepresentation(scale_factor); |
| 100 MatchScale(&rgb_rep, &alpha_rep); | 106 MatchScale(&rgb_rep, &alpha_rep); |
| 101 return ImageSkiaRep(SkBitmapOperations::CreateMaskedBitmap( | 107 return ImageSkiaRep(SkBitmapOperations::CreateMaskedBitmap( |
| 102 rgb_rep.sk_bitmap(), alpha_rep.sk_bitmap()), | 108 rgb_rep.sk_bitmap(), alpha_rep.sk_bitmap()), |
| 103 rgb_rep.scale_factor()); | 109 rgb_rep.scale_factor()); |
| 104 } | 110 } |
| 105 | 111 |
| 106 private: | 112 private: |
| 107 const ImageSkia rgb_; | 113 const ImageSkia rgb_; |
| 108 const ImageSkia alpha_; | 114 const ImageSkia alpha_; |
| 109 | 115 |
| 110 DISALLOW_COPY_AND_ASSIGN(MaskedImageSource); | 116 DISALLOW_COPY_AND_ASSIGN(MaskedImageSource); |
| 111 }; | 117 }; |
| 112 | 118 |
| 113 class TiledImageSource : public gfx::ImageSkiaSource { | 119 class TiledImageSource : public gfx::ImageSkiaSource { |
| 114 public: | 120 public: |
| 115 TiledImageSource(const ImageSkia& source, | 121 TiledImageSource(const ImageSkia& source, |
| 116 int src_x, int src_y, | 122 int src_x, int src_y, |
| 117 int dst_w, int dst_h) | 123 int dst_w, int dst_h) |
| 118 : source_(source), | 124 : source_(source), |
| 119 src_x_(src_x), | 125 src_x_(src_x), |
| 120 src_y_(src_y), | 126 src_y_(src_y), |
| 121 dst_w_(dst_w), | 127 dst_w_(dst_w), |
| 122 dst_h_(dst_h) { | 128 dst_h_(dst_h) { |
| 123 } | 129 } |
| 124 | 130 |
| 131 virtual ~TiledImageSource() { |
| 132 } |
| 133 |
| 125 // gfx::ImageSkiaSource overrides: | 134 // gfx::ImageSkiaSource overrides: |
| 126 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { | 135 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
| 127 ImageSkiaRep source_rep = source_.GetRepresentation(scale_factor); | 136 ImageSkiaRep source_rep = source_.GetRepresentation(scale_factor); |
| 128 float scale = ui::GetScaleFactorScale(source_rep.scale_factor()); | 137 float scale = ui::GetScaleFactorScale(source_rep.scale_factor()); |
| 129 return ImageSkiaRep( | 138 return ImageSkiaRep( |
| 130 SkBitmapOperations::CreateTiledBitmap( | 139 SkBitmapOperations::CreateTiledBitmap( |
| 131 source_rep.sk_bitmap(), | 140 source_rep.sk_bitmap(), |
| 132 src_x_ * scale, src_y_ * scale, dst_w_ * scale, dst_h_ * scale), | 141 src_x_ * scale, src_y_ * scale, dst_w_ * scale, dst_h_ * scale), |
| 133 source_rep.scale_factor()); | 142 source_rep.scale_factor()); |
| 134 } | 143 } |
| 135 | 144 |
| 136 private: | 145 private: |
| 137 const ImageSkia& source_; | 146 const ImageSkia& source_; |
| 138 const int src_x_; | 147 const int src_x_; |
| 139 const int src_y_; | 148 const int src_y_; |
| 140 const int dst_w_; | 149 const int dst_w_; |
| 141 const int dst_h_; | 150 const int dst_h_; |
| 142 | 151 |
| 143 DISALLOW_COPY_AND_ASSIGN(TiledImageSource); | 152 DISALLOW_COPY_AND_ASSIGN(TiledImageSource); |
| 144 }; | 153 }; |
| 145 | 154 |
| 155 // ImageSkiaSource which uses SkBitmapOperations::CreateButtonBackground |
| 156 // to generate image reps for the target image. |
| 157 class ButtonImageSource: public gfx::ImageSkiaSource { |
| 158 public: |
| 159 ButtonImageSource(SkColor color, |
| 160 const ImageSkia& image, |
| 161 const ImageSkia& mask) |
| 162 : color_(color), |
| 163 image_(image), |
| 164 mask_(mask) { |
| 165 } |
| 166 |
| 167 virtual ~ButtonImageSource() { |
| 168 } |
| 169 |
| 170 // gfx::ImageSkiaSource overrides: |
| 171 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
| 172 ImageSkiaRep image_rep = image_.GetRepresentation(scale_factor); |
| 173 ImageSkiaRep mask_rep = mask_.GetRepresentation(scale_factor); |
| 174 MatchScale(&image_rep, &mask_rep); |
| 175 return ImageSkiaRep( |
| 176 SkBitmapOperations::CreateButtonBackground(color_, |
| 177 image_rep.sk_bitmap(), mask_rep.sk_bitmap()), |
| 178 image_rep.scale_factor()); |
| 179 } |
| 180 |
| 181 private: |
| 182 const SkColor color_; |
| 183 const ImageSkia image_; |
| 184 const ImageSkia mask_; |
| 185 |
| 186 DISALLOW_COPY_AND_ASSIGN(ButtonImageSource); |
| 187 }; |
| 188 |
| 189 // ImageSkiaSource which uses SkBitmap::extractSubset to generate image reps |
| 190 // for the target image. |
| 191 class ExtractSubsetImageSource: public gfx::ImageSkiaSource { |
| 192 public: |
| 193 ExtractSubsetImageSource(const gfx::ImageSkia& image, |
| 194 const gfx::Rect& subset_bounds) |
| 195 : image_(image), |
| 196 subset_bounds_(subset_bounds) { |
| 197 } |
| 198 |
| 199 ~ExtractSubsetImageSource() { |
| 200 } |
| 201 |
| 202 // gfx::ImageSkiaSource overrides: |
| 203 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
| 204 ImageSkiaRep image_rep = image_.GetRepresentation(scale_factor); |
| 205 SkIRect subset_bounds_in_pixel = RectToSkIRect(subset_bounds_.Scale( |
| 206 ui::GetScaleFactorScale(image_rep.scale_factor()))); |
| 207 SkBitmap dst; |
| 208 bool success = image_rep.sk_bitmap().extractSubset(&dst, |
| 209 subset_bounds_in_pixel); |
| 210 DCHECK(success); |
| 211 return gfx::ImageSkiaRep(dst, image_rep.scale_factor()); |
| 212 } |
| 213 |
| 214 private: |
| 215 const gfx::ImageSkia image_; |
| 216 const gfx::Rect subset_bounds_; |
| 217 |
| 218 DISALLOW_COPY_AND_ASSIGN(ExtractSubsetImageSource); |
| 219 }; |
| 220 |
| 146 // ResizeSource resizes relevant image reps in |source| to |target_dip_size| | 221 // ResizeSource resizes relevant image reps in |source| to |target_dip_size| |
| 147 // for requested scale factors. | 222 // for requested scale factors. |
| 148 class ResizeSource : public ImageSkiaSource { | 223 class ResizeSource : public ImageSkiaSource { |
| 149 public: | 224 public: |
| 150 ResizeSource(const ImageSkia& source, | 225 ResizeSource(const ImageSkia& source, |
| 151 const Size& target_dip_size) | 226 const Size& target_dip_size) |
| 152 : source_(source), | 227 : source_(source), |
| 153 target_dip_size_(target_dip_size) { | 228 target_dip_size_(target_dip_size) { |
| 154 } | 229 } |
| 155 virtual ~ResizeSource() {} | 230 virtual ~ResizeSource() {} |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 | 303 |
| 229 // static | 304 // static |
| 230 ImageSkia ImageSkiaOperations::CreateTiledImage(const ImageSkia& source, | 305 ImageSkia ImageSkiaOperations::CreateTiledImage(const ImageSkia& source, |
| 231 int src_x, int src_y, | 306 int src_x, int src_y, |
| 232 int dst_w, int dst_h) { | 307 int dst_w, int dst_h) { |
| 233 return ImageSkia(new TiledImageSource(source, src_x, src_y, dst_w, dst_h), | 308 return ImageSkia(new TiledImageSource(source, src_x, src_y, dst_w, dst_h), |
| 234 gfx::Size(dst_w, dst_h)); | 309 gfx::Size(dst_w, dst_h)); |
| 235 } | 310 } |
| 236 | 311 |
| 237 // static | 312 // static |
| 313 ImageSkia ImageSkiaOperations::CreateButtonBackground(SkColor color, |
| 314 const ImageSkia& image, |
| 315 const ImageSkia& mask) { |
| 316 return ImageSkia(new ButtonImageSource(color, image, mask), mask.size()); |
| 317 } |
| 318 |
| 319 // static |
| 320 ImageSkia ImageSkiaOperations::ExtractSubset(const ImageSkia& image, |
| 321 const Rect& subset_bounds) { |
| 322 gfx::Rect clipped_bounds = subset_bounds.Intersect(gfx::Rect(image.size())); |
| 323 if (image.isNull() || clipped_bounds.IsEmpty()) { |
| 324 return ImageSkia(); |
| 325 } |
| 326 |
| 327 return ImageSkia(new ExtractSubsetImageSource(image, clipped_bounds), |
| 328 clipped_bounds.size()); |
| 329 } |
| 330 |
| 331 // static |
| 238 ImageSkia ImageSkiaOperations::CreateResizedImage(const ImageSkia& source, | 332 ImageSkia ImageSkiaOperations::CreateResizedImage(const ImageSkia& source, |
| 239 const Size& target_dip_size) { | 333 const Size& target_dip_size) { |
| 240 return ImageSkia(new ResizeSource(source, target_dip_size), target_dip_size); | 334 return ImageSkia(new ResizeSource(source, target_dip_size), target_dip_size); |
| 241 } | 335 } |
| 242 | 336 |
| 243 // static | 337 // static |
| 244 ImageSkia ImageSkiaOperations::CreateImageWithDropShadow( | 338 ImageSkia ImageSkiaOperations::CreateImageWithDropShadow( |
| 245 const ImageSkia& source, | 339 const ImageSkia& source, |
| 246 const ShadowValues& shadows) { | 340 const ShadowValues& shadows) { |
| 247 const gfx::Insets shadow_padding = -gfx::ShadowValue::GetMargin(shadows); | 341 const gfx::Insets shadow_padding = -gfx::ShadowValue::GetMargin(shadows); |
| 248 gfx::Size shadow_image_size = source.size(); | 342 gfx::Size shadow_image_size = source.size(); |
| 249 shadow_image_size.Enlarge(shadow_padding.width(), | 343 shadow_image_size.Enlarge(shadow_padding.width(), |
| 250 shadow_padding.height()); | 344 shadow_padding.height()); |
| 251 return ImageSkia(new DropShadowSource(source, shadows), shadow_image_size); | 345 return ImageSkia(new DropShadowSource(source, shadows), shadow_image_size); |
| 252 } | 346 } |
| 253 | 347 |
| 254 } // namespace gfx | 348 } // namespace gfx |
| OLD | NEW |