1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gfx/image/image_skia_operations.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "skia/ext/image_operations.h"
#include "skia/ext/platform_canvas.h"
#include "ui/base/layout.h"
#include "ui/base/ui_base_switches.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/image/canvas_image_source.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_rep.h"
#include "ui/gfx/image/image_skia_source.h"
#include "ui/gfx/insets.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/rect_conversions.h"
#include "ui/gfx/size.h"
#include "ui/gfx/size_conversions.h"
#include "ui/gfx/skbitmap_operations.h"
#include "ui/gfx/skia_util.h"
namespace gfx {
namespace {
bool ScalingEnabled() {
static bool scale_images = !CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableScalingInImageSkiaOperations);
return scale_images;
}
// Creates 2x scaled image of the give |source|.
ImageSkiaRep Create2XImageSkiaRep(const ImageSkiaRep& source) {
gfx::Size size(source.GetWidth() * 2.0f, source.GetHeight() * 2.0f);
skia::PlatformCanvas canvas(size.width(), size.height(), false);
SkRect resized_bounds = RectToSkRect(gfx::Rect(size));
canvas.drawBitmapRect(source.sk_bitmap(), NULL, resized_bounds);
SkBitmap resized_bitmap = canvas.getDevice()->accessBitmap(false);
return ImageSkiaRep(resized_bitmap, ui::SCALE_FACTOR_200P);
}
// A utility function to synchronize the scale factor of the two images.
// When the command line option "--disable-scaling-in-image-skia-operation"
// is provided, this function will fail if the scale factors of the two images
// are different. This assumes that the platform only supports
// 1x and 2x scale factors.
// TODO(oshima): Remove and replace this with plain CHECK once
// 2x images for all resources are provided.
void MatchScale(ImageSkiaRep* first, ImageSkiaRep* second) {
if (first->scale_factor() != second->scale_factor()) {
CHECK(ScalingEnabled());
ImageSkiaRep* target = NULL;
if (first->scale_factor() == ui::SCALE_FACTOR_100P) {
target = first;
} else {
target = second;
}
*target = Create2XImageSkiaRep(*target);
}
}
class BlendingImageSource : public gfx::ImageSkiaSource {
public:
BlendingImageSource(const ImageSkia& first,
const ImageSkia& second,
double alpha)
: first_(first),
second_(second),
alpha_(alpha) {
}
virtual ~BlendingImageSource() {
}
// gfx::ImageSkiaSource overrides:
virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE {
ImageSkiaRep first_rep = first_.GetRepresentation(scale_factor);
ImageSkiaRep second_rep = second_.GetRepresentation(scale_factor);
MatchScale(&first_rep, &second_rep);
SkBitmap blended = SkBitmapOperations::CreateBlendedBitmap(
first_rep.sk_bitmap(), second_rep.sk_bitmap(), alpha_);
return ImageSkiaRep(blended, first_rep.scale_factor());
}
private:
const ImageSkia first_;
const ImageSkia second_;
double alpha_;
DISALLOW_COPY_AND_ASSIGN(BlendingImageSource);
};
class SuperimposedImageSource : public gfx::CanvasImageSource {
public:
SuperimposedImageSource(const ImageSkia& first,
const ImageSkia& second)
: gfx::CanvasImageSource(first.size(), false /* is opaque */),
first_(first),
second_(second) {
}
virtual ~SuperimposedImageSource() {}
// gfx::CanvasImageSource override.
virtual void Draw(Canvas* canvas) OVERRIDE {
canvas->DrawImageInt(first_, 0, 0);
canvas->DrawImageInt(second_,
(first_.width() - second_.width()) / 2,
(first_.height() - second_.height()) / 2);
}
private:
const ImageSkia first_;
const ImageSkia second_;
DISALLOW_COPY_AND_ASSIGN(SuperimposedImageSource);
};
class TransparentImageSource : public gfx::ImageSkiaSource {
public:
TransparentImageSource(const ImageSkia& image, double alpha)
: image_(image),
alpha_(alpha) {
}
virtual ~TransparentImageSource() {}
private:
// gfx::ImageSkiaSource overrides:
virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE {
ImageSkiaRep image_rep = image_.GetRepresentation(scale_factor);
SkBitmap alpha;
alpha.setConfig(SkBitmap::kARGB_8888_Config,
image_rep.pixel_width(),
image_rep.pixel_height());
alpha.allocPixels();
alpha.eraseColor(SkColorSetARGB(alpha_ * 255, 0, 0, 0));
return ImageSkiaRep(
SkBitmapOperations::CreateMaskedBitmap(image_rep.sk_bitmap(), alpha),
image_rep.scale_factor());
}
ImageSkia image_;
double alpha_;
DISALLOW_COPY_AND_ASSIGN(TransparentImageSource);
};
class MaskedImageSource : public gfx::ImageSkiaSource {
public:
MaskedImageSource(const ImageSkia& rgb, const ImageSkia& alpha)
: rgb_(rgb),
alpha_(alpha) {
}
virtual ~MaskedImageSource() {
}
// gfx::ImageSkiaSource overrides:
virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE {
ImageSkiaRep rgb_rep = rgb_.GetRepresentation(scale_factor);
ImageSkiaRep alpha_rep = alpha_.GetRepresentation(scale_factor);
MatchScale(&rgb_rep, &alpha_rep);
return ImageSkiaRep(SkBitmapOperations::CreateMaskedBitmap(
rgb_rep.sk_bitmap(), alpha_rep.sk_bitmap()),
rgb_rep.scale_factor());
}
private:
const ImageSkia rgb_;
const ImageSkia alpha_;
DISALLOW_COPY_AND_ASSIGN(MaskedImageSource);
};
class TiledImageSource : public gfx::ImageSkiaSource {
public:
TiledImageSource(const ImageSkia& source,
int src_x, int src_y,
int dst_w, int dst_h)
: source_(source),
src_x_(src_x),
src_y_(src_y),
dst_w_(dst_w),
dst_h_(dst_h) {
}
virtual ~TiledImageSource() {
}
// gfx::ImageSkiaSource overrides:
virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE {
ImageSkiaRep source_rep = source_.GetRepresentation(scale_factor);
float scale = ui::GetScaleFactorScale(source_rep.scale_factor());
return ImageSkiaRep(
SkBitmapOperations::CreateTiledBitmap(
source_rep.sk_bitmap(),
src_x_ * scale, src_y_ * scale, dst_w_ * scale, dst_h_ * scale),
source_rep.scale_factor());
}
private:
const ImageSkia source_;
const int src_x_;
const int src_y_;
const int dst_w_;
const int dst_h_;
DISALLOW_COPY_AND_ASSIGN(TiledImageSource);
};
class HSLImageSource : public gfx::ImageSkiaSource {
public:
HSLImageSource(const ImageSkia& image,
const color_utils::HSL& hsl_shift)
: image_(image),
hsl_shift_(hsl_shift) {
}
virtual ~HSLImageSource() {
}
// gfx::ImageSkiaSource overrides:
virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE {
ImageSkiaRep image_rep = image_.GetRepresentation(scale_factor);
return gfx::ImageSkiaRep(
SkBitmapOperations::CreateHSLShiftedBitmap(image_rep.sk_bitmap(),
hsl_shift_), image_rep.scale_factor());
}
private:
const gfx::ImageSkia image_;
const color_utils::HSL hsl_shift_;
DISALLOW_COPY_AND_ASSIGN(HSLImageSource);
};
// ImageSkiaSource which uses SkBitmapOperations::CreateButtonBackground
// to generate image reps for the target image.
class ButtonImageSource: public gfx::ImageSkiaSource {
public:
ButtonImageSource(SkColor color,
const ImageSkia& image,
const ImageSkia& mask)
: color_(color),
image_(image),
mask_(mask) {
}
virtual ~ButtonImageSource() {
}
// gfx::ImageSkiaSource overrides:
virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE {
ImageSkiaRep image_rep = image_.GetRepresentation(scale_factor);
ImageSkiaRep mask_rep = mask_.GetRepresentation(scale_factor);
MatchScale(&image_rep, &mask_rep);
return ImageSkiaRep(
SkBitmapOperations::CreateButtonBackground(color_,
image_rep.sk_bitmap(), mask_rep.sk_bitmap()),
image_rep.scale_factor());
}
private:
const SkColor color_;
const ImageSkia image_;
const ImageSkia mask_;
DISALLOW_COPY_AND_ASSIGN(ButtonImageSource);
};
// ImageSkiaSource which uses SkBitmap::extractSubset to generate image reps
// for the target image.
class ExtractSubsetImageSource: public gfx::ImageSkiaSource {
public:
ExtractSubsetImageSource(const gfx::ImageSkia& image,
const gfx::Rect& subset_bounds)
: image_(image),
subset_bounds_(subset_bounds) {
}
virtual ~ExtractSubsetImageSource() {
}
// gfx::ImageSkiaSource overrides:
virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE {
ImageSkiaRep image_rep = image_.GetRepresentation(scale_factor);
float scale_to_pixel = ui::GetScaleFactorScale(image_rep.scale_factor());
SkIRect subset_bounds_in_pixel = RectToSkIRect(ToFlooredRectDeprecated(
gfx::ScaleRect(subset_bounds_, scale_to_pixel)));
SkBitmap dst;
bool success = image_rep.sk_bitmap().extractSubset(&dst,
subset_bounds_in_pixel);
DCHECK(success);
return gfx::ImageSkiaRep(dst, image_rep.scale_factor());
}
private:
const gfx::ImageSkia image_;
const gfx::Rect subset_bounds_;
DISALLOW_COPY_AND_ASSIGN(ExtractSubsetImageSource);
};
// ResizeSource resizes relevant image reps in |source| to |target_dip_size|
// for requested scale factors.
class ResizeSource : public ImageSkiaSource {
public:
ResizeSource(const ImageSkia& source,
skia::ImageOperations::ResizeMethod method,
const Size& target_dip_size)
: source_(source),
resize_method_(method),
target_dip_size_(target_dip_size) {
}
virtual ~ResizeSource() {}
// gfx::ImageSkiaSource overrides:
virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE {
const ImageSkiaRep& image_rep = source_.GetRepresentation(scale_factor);
if (image_rep.GetWidth() == target_dip_size_.width() &&
image_rep.GetHeight() == target_dip_size_.height())
return image_rep;
const float scale = ui::GetScaleFactorScale(scale_factor);
const Size target_pixel_size = gfx::ToFlooredSize(
target_dip_size_.Scale(scale));
const SkBitmap resized = skia::ImageOperations::Resize(
image_rep.sk_bitmap(),
resize_method_,
target_pixel_size.width(),
target_pixel_size.height());
return ImageSkiaRep(resized, scale_factor);
}
private:
const ImageSkia source_;
skia::ImageOperations::ResizeMethod resize_method_;
const Size target_dip_size_;
DISALLOW_COPY_AND_ASSIGN(ResizeSource);
};
// DropShadowSource generates image reps with drop shadow for image reps in
// |source| that represent requested scale factors.
class DropShadowSource : public ImageSkiaSource {
public:
DropShadowSource(const ImageSkia& source,
const ShadowValues& shadows_in_dip)
: source_(source),
shaodws_in_dip_(shadows_in_dip) {
}
virtual ~DropShadowSource() {}
// gfx::ImageSkiaSource overrides:
virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE {
const ImageSkiaRep& image_rep = source_.GetRepresentation(scale_factor);
const float scale = image_rep.GetScale();
ShadowValues shaodws_in_pixel;
for (size_t i = 0; i < shaodws_in_dip_.size(); ++i)
shaodws_in_pixel.push_back(shaodws_in_dip_[i].Scale(scale));
const SkBitmap shaodw_bitmap = SkBitmapOperations::CreateDropShadow(
image_rep.sk_bitmap(),
shaodws_in_pixel);
return ImageSkiaRep(shaodw_bitmap, image_rep.scale_factor());
}
private:
const ImageSkia source_;
const ShadowValues shaodws_in_dip_;
DISALLOW_COPY_AND_ASSIGN(DropShadowSource);
};
} // namespace
// static
ImageSkia ImageSkiaOperations::CreateBlendedImage(const ImageSkia& first,
const ImageSkia& second,
double alpha) {
return ImageSkia(new BlendingImageSource(first, second, alpha), first.size());
}
// static
ImageSkia ImageSkiaOperations::CreateSuperimposedImage(
const ImageSkia& first,
const ImageSkia& second) {
return ImageSkia(new SuperimposedImageSource(first, second), first.size());
}
// static
ImageSkia ImageSkiaOperations::CreateTransparentImage(const ImageSkia& image,
double alpha) {
return ImageSkia(new TransparentImageSource(image, alpha), image.size());
}
// static
ImageSkia ImageSkiaOperations::CreateMaskedImage(const ImageSkia& rgb,
const ImageSkia& alpha) {
return ImageSkia(new MaskedImageSource(rgb, alpha), rgb.size());
}
// static
ImageSkia ImageSkiaOperations::CreateTiledImage(const ImageSkia& source,
int src_x, int src_y,
int dst_w, int dst_h) {
return ImageSkia(new TiledImageSource(source, src_x, src_y, dst_w, dst_h),
gfx::Size(dst_w, dst_h));
}
// static
ImageSkia ImageSkiaOperations::CreateHSLShiftedImage(
const ImageSkia& image,
const color_utils::HSL& hsl_shift) {
return ImageSkia(new HSLImageSource(image, hsl_shift), image.size());
}
// static
ImageSkia ImageSkiaOperations::CreateButtonBackground(SkColor color,
const ImageSkia& image,
const ImageSkia& mask) {
return ImageSkia(new ButtonImageSource(color, image, mask), mask.size());
}
// static
ImageSkia ImageSkiaOperations::ExtractSubset(const ImageSkia& image,
const Rect& subset_bounds) {
gfx::Rect clipped_bounds =
gfx::IntersectRects(subset_bounds, gfx::Rect(image.size()));
if (image.isNull() || clipped_bounds.IsEmpty()) {
return ImageSkia();
}
return ImageSkia(new ExtractSubsetImageSource(image, clipped_bounds),
clipped_bounds.size());
}
// static
ImageSkia ImageSkiaOperations::CreateResizedImage(
const ImageSkia& source,
skia::ImageOperations::ResizeMethod method,
const Size& target_dip_size) {
return ImageSkia(new ResizeSource(source, method, target_dip_size),
target_dip_size);
}
// static
ImageSkia ImageSkiaOperations::CreateImageWithDropShadow(
const ImageSkia& source,
const ShadowValues& shadows) {
const gfx::Insets shadow_padding = -gfx::ShadowValue::GetMargin(shadows);
gfx::Size shadow_image_size = source.size();
shadow_image_size.Enlarge(shadow_padding.width(),
shadow_padding.height());
return ImageSkia(new DropShadowSource(source, shadows), shadow_image_size);
}
} // namespace gfx
|