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
3 // found in the LICENSE file.
4
5 #include "ui/gfx/skbitmap_operations.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <algorithm>
11
12 #include "base/logging.h"
13 #include "third_party/skia/include/core/SkBitmap.h"
14 #include "third_party/skia/include/core/SkCanvas.h"
15 #include "third_party/skia/include/core/SkColorFilter.h"
16 #include "third_party/skia/include/core/SkColorPriv.h"
17 #include "third_party/skia/include/core/SkUnPreMultiply.h"
18 #include "third_party/skia/include/effects/SkBlurImageFilter.h"
19 #include "ui/gfx/geometry/insets.h"
20 #include "ui/gfx/geometry/point.h"
21 #include "ui/gfx/geometry/size.h"
22
23 // static
CreateInvertedBitmap(const SkBitmap & image)24 SkBitmap SkBitmapOperations::CreateInvertedBitmap(const SkBitmap& image) {
25 DCHECK(image.colorType() == kN32_SkColorType);
26
27 SkBitmap inverted;
28 inverted.allocN32Pixels(image.width(), image.height());
29
30 for (int y = 0; y < image.height(); ++y) {
31 uint32_t* image_row = image.getAddr32(0, y);
32 uint32_t* dst_row = inverted.getAddr32(0, y);
33
34 for (int x = 0; x < image.width(); ++x) {
35 uint32_t image_pixel = image_row[x];
36 dst_row[x] = (image_pixel & 0xFF000000) |
37 (0x00FFFFFF - (image_pixel & 0x00FFFFFF));
38 }
39 }
40
41 return inverted;
42 }
43
44 // static
CreateBlendedBitmap(const SkBitmap & first,const SkBitmap & second,double alpha)45 SkBitmap SkBitmapOperations::CreateBlendedBitmap(const SkBitmap& first,
46 const SkBitmap& second,
47 double alpha) {
48 DCHECK((alpha >= 0) && (alpha <= 1));
49 DCHECK(first.width() == second.width());
50 DCHECK(first.height() == second.height());
51 DCHECK(first.bytesPerPixel() == second.bytesPerPixel());
52 DCHECK(first.colorType() == kN32_SkColorType);
53
54 // Optimize for case where we won't need to blend anything.
55 static const double alpha_min = 1.0 / 255;
56 static const double alpha_max = 254.0 / 255;
57 if (alpha < alpha_min)
58 return first;
59 else if (alpha > alpha_max)
60 return second;
61
62 SkBitmap blended;
63 blended.allocN32Pixels(first.width(), first.height());
64
65 double first_alpha = 1 - alpha;
66
67 for (int y = 0; y < first.height(); ++y) {
68 uint32_t* first_row = first.getAddr32(0, y);
69 uint32_t* second_row = second.getAddr32(0, y);
70 uint32_t* dst_row = blended.getAddr32(0, y);
71
72 for (int x = 0; x < first.width(); ++x) {
73 uint32_t first_pixel = first_row[x];
74 uint32_t second_pixel = second_row[x];
75
76 int a = static_cast<int>((SkColorGetA(first_pixel) * first_alpha) +
77 (SkColorGetA(second_pixel) * alpha));
78 int r = static_cast<int>((SkColorGetR(first_pixel) * first_alpha) +
79 (SkColorGetR(second_pixel) * alpha));
80 int g = static_cast<int>((SkColorGetG(first_pixel) * first_alpha) +
81 (SkColorGetG(second_pixel) * alpha));
82 int b = static_cast<int>((SkColorGetB(first_pixel) * first_alpha) +
83 (SkColorGetB(second_pixel) * alpha));
84
85 dst_row[x] = SkColorSetARGB(a, r, g, b);
86 }
87 }
88
89 return blended;
90 }
91
92 // static
CreateMaskedBitmap(const SkBitmap & rgb,const SkBitmap & alpha)93 SkBitmap SkBitmapOperations::CreateMaskedBitmap(const SkBitmap& rgb,
94 const SkBitmap& alpha) {
95 DCHECK(rgb.width() == alpha.width());
96 DCHECK(rgb.height() == alpha.height());
97 DCHECK(rgb.bytesPerPixel() == alpha.bytesPerPixel());
98 DCHECK(rgb.colorType() == kN32_SkColorType);
99 DCHECK(alpha.colorType() == kN32_SkColorType);
100
101 SkBitmap masked;
102 masked.allocN32Pixels(rgb.width(), rgb.height());
103
104 for (int y = 0; y < masked.height(); ++y) {
105 uint32_t* rgb_row = rgb.getAddr32(0, y);
106 uint32_t* alpha_row = alpha.getAddr32(0, y);
107 uint32_t* dst_row = masked.getAddr32(0, y);
108
109 for (int x = 0; x < masked.width(); ++x) {
110 unsigned alpha32 = SkGetPackedA32(alpha_row[x]);
111 unsigned scale = SkAlpha255To256(alpha32);
112 dst_row[x] = SkAlphaMulQ(rgb_row[x], scale);
113 }
114 }
115
116 return masked;
117 }
118
119 // static
CreateButtonBackground(SkColor color,const SkBitmap & image,const SkBitmap & mask)120 SkBitmap SkBitmapOperations::CreateButtonBackground(SkColor color,
121 const SkBitmap& image,
122 const SkBitmap& mask) {
123 // Despite this assert, it seems like image is actually unpremultiplied.
124 // The math producing dst_row[x] below is a correct SrcOver when
125 // bg_* are premultiplied and img_* are unpremultiplied.
126 DCHECK(image.colorType() == kN32_SkColorType);
127 DCHECK(mask.colorType() == kN32_SkColorType);
128
129 SkBitmap background;
130 background.allocN32Pixels(mask.width(), mask.height());
131
132 double bg_a = SkColorGetA(color);
133 double bg_r = SkColorGetR(color) * (bg_a / 255.0);
134 double bg_g = SkColorGetG(color) * (bg_a / 255.0);
135 double bg_b = SkColorGetB(color) * (bg_a / 255.0);
136
137 for (int y = 0; y < mask.height(); ++y) {
138 uint32_t* dst_row = background.getAddr32(0, y);
139 uint32_t* image_row = image.getAddr32(0, y % image.height());
140 uint32_t* mask_row = mask.getAddr32(0, y);
141
142 for (int x = 0; x < mask.width(); ++x) {
143 uint32_t image_pixel = image_row[x % image.width()];
144
145 double img_a = SkColorGetA(image_pixel);
146 double img_r = SkColorGetR(image_pixel);
147 double img_g = SkColorGetG(image_pixel);
148 double img_b = SkColorGetB(image_pixel);
149
150 double img_alpha = img_a / 255.0;
151 double img_inv = 1 - img_alpha;
152
153 double mask_a = static_cast<double>(SkColorGetA(mask_row[x])) / 255.0;
154
155 dst_row[x] = SkColorSetARGB(
156 // This is pretty weird; why not the usual SrcOver alpha?
157 static_cast<int>(std::min(255.0, bg_a + img_a) * mask_a),
158 static_cast<int>(((bg_r * img_inv) + (img_r * img_alpha)) * mask_a),
159 static_cast<int>(((bg_g * img_inv) + (img_g * img_alpha)) * mask_a),
160 static_cast<int>(((bg_b * img_inv) + (img_b * img_alpha)) * mask_a));
161 }
162 }
163
164 return background;
165 }
166
167 namespace {
168 namespace HSLShift {
169
170 // TODO(viettrungluu): Some things have yet to be optimized at all.
171
172 // Notes on and conventions used in the following code
173 //
174 // Conventions:
175 // - R, G, B, A = obvious; as variables: |r|, |g|, |b|, |a| (see also below)
176 // - H, S, L = obvious; as variables: |h|, |s|, |l| (see also below)
177 // - variables derived from S, L shift parameters: |sdec| and |sinc| for S
178 // increase and decrease factors, |ldec| and |linc| for L (see also below)
179 //
180 // To try to optimize HSL shifts, we do several things:
181 // - Avoid unpremultiplying (then processing) then premultiplying. This means
182 // that R, G, B values (and also L, but not H and S) should be treated as
183 // having a range of 0..A (where A is alpha).
184 // - Do things in integer/fixed-point. This avoids costly conversions between
185 // floating-point and integer, though I should study the tradeoff more
186 // carefully (presumably, at some point of processing complexity, converting
187 // and processing using simpler floating-point code will begin to win in
188 // performance). Also to be studied is the speed/type of floating point
189 // conversions; see, e.g., <http://www.stereopsis.com/sree/fpu2006.html>.
190 //
191 // Conventions for fixed-point arithmetic
192 // - Each function has a constant denominator (called |den|, which should be a
193 // power of 2), appropriate for the computations done in that function.
194 // - A value |x| is then typically represented by a numerator, named |x_num|,
195 // so that its actual value is |x_num / den| (casting to floating-point
196 // before division).
197 // - To obtain |x_num| from |x|, simply multiply by |den|, i.e., |x_num = x *
198 // den| (casting appropriately).
199 // - When necessary, a value |x| may also be represented as a numerator over
200 // the denominator squared (set |den2 = den * den|). In such a case, the
201 // corresponding variable is called |x_num2| (so that its actual value is
202 // |x_num^2 / den2|.
203 // - The representation of the product of |x| and |y| is be called |x_y_num| if
204 // |x * y == x_y_num / den|, and |xy_num2| if |x * y == x_y_num2 / den2|. In
205 // the latter case, notice that one can calculate |x_y_num2 = x_num * y_num|.
206
207 // Routine used to process a line; typically specialized for specific kinds of
208 // HSL shifts (to optimize).
209 typedef void (*LineProcessor)(const color_utils::HSL&,
210 const SkPMColor*,
211 SkPMColor*,
212 int width);
213
214 enum OperationOnH { kOpHNone = 0, kOpHShift, kNumHOps };
215 enum OperationOnS { kOpSNone = 0, kOpSDec, kOpSInc, kNumSOps };
216 enum OperationOnL { kOpLNone = 0, kOpLDec, kOpLInc, kNumLOps };
217
218 // Epsilon used to judge when shift values are close enough to various critical
219 // values (typically 0.5, which yields a no-op for S and L shifts. 1/256 should
220 // be small enough, but let's play it safe>
221 const double epsilon = 0.0005;
222
223 // Line processor: default/universal (i.e., old-school).
LineProcDefault(const color_utils::HSL & hsl_shift,const SkPMColor * in,SkPMColor * out,int width)224 void LineProcDefault(const color_utils::HSL& hsl_shift,
225 const SkPMColor* in,
226 SkPMColor* out,
227 int width) {
228 for (int x = 0; x < width; x++) {
229 out[x] = SkPreMultiplyColor(color_utils::HSLShift(
230 SkUnPreMultiply::PMColorToColor(in[x]), hsl_shift));
231 }
232 }
233
234 // Line processor: no-op (i.e., copy).
LineProcCopy(const color_utils::HSL & hsl_shift,const SkPMColor * in,SkPMColor * out,int width)235 void LineProcCopy(const color_utils::HSL& hsl_shift,
236 const SkPMColor* in,
237 SkPMColor* out,
238 int width) {
239 DCHECK(hsl_shift.h < 0);
240 DCHECK(hsl_shift.s < 0 || fabs(hsl_shift.s - 0.5) < HSLShift::epsilon);
241 DCHECK(hsl_shift.l < 0 || fabs(hsl_shift.l - 0.5) < HSLShift::epsilon);
242 memcpy(out, in, static_cast<size_t>(width) * sizeof(out[0]));
243 }
244
245 // Line processor: H no-op, S no-op, L decrease.
LineProcHnopSnopLdec(const color_utils::HSL & hsl_shift,const SkPMColor * in,SkPMColor * out,int width)246 void LineProcHnopSnopLdec(const color_utils::HSL& hsl_shift,
247 const SkPMColor* in,
248 SkPMColor* out,
249 int width) {
250 const uint32_t den = 65536;
251
252 DCHECK(hsl_shift.h < 0);
253 DCHECK(hsl_shift.s < 0 || fabs(hsl_shift.s - 0.5) < HSLShift::epsilon);
254 DCHECK(hsl_shift.l <= 0.5 - HSLShift::epsilon && hsl_shift.l >= 0);
255
256 uint32_t ldec_num = static_cast<uint32_t>(hsl_shift.l * 2 * den);
257 for (int x = 0; x < width; x++) {
258 uint32_t a = SkGetPackedA32(in[x]);
259 uint32_t r = SkGetPackedR32(in[x]);
260 uint32_t g = SkGetPackedG32(in[x]);
261 uint32_t b = SkGetPackedB32(in[x]);
262 r = r * ldec_num / den;
263 g = g * ldec_num / den;
264 b = b * ldec_num / den;
265 out[x] = SkPackARGB32(a, r, g, b);
266 }
267 }
268
269 // Line processor: H no-op, S no-op, L increase.
LineProcHnopSnopLinc(const color_utils::HSL & hsl_shift,const SkPMColor * in,SkPMColor * out,int width)270 void LineProcHnopSnopLinc(const color_utils::HSL& hsl_shift,
271 const SkPMColor* in,
272 SkPMColor* out,
273 int width) {
274 const uint32_t den = 65536;
275
276 DCHECK(hsl_shift.h < 0);
277 DCHECK(hsl_shift.s < 0 || fabs(hsl_shift.s - 0.5) < HSLShift::epsilon);
278 DCHECK(hsl_shift.l >= 0.5 + HSLShift::epsilon && hsl_shift.l <= 1);
279
280 uint32_t linc_num = static_cast<uint32_t>((hsl_shift.l - 0.5) * 2 * den);
281 for (int x = 0; x < width; x++) {
282 uint32_t a = SkGetPackedA32(in[x]);
283 uint32_t r = SkGetPackedR32(in[x]);
284 uint32_t g = SkGetPackedG32(in[x]);
285 uint32_t b = SkGetPackedB32(in[x]);
286 r += (a - r) * linc_num / den;
287 g += (a - g) * linc_num / den;
288 b += (a - b) * linc_num / den;
289 out[x] = SkPackARGB32(a, r, g, b);
290 }
291 }
292
293 // Saturation changes modifications in RGB
294 //
295 // (Note that as a further complication, the values we deal in are
296 // premultiplied, so R/G/B values must be in the range 0..A. For mathematical
297 // purposes, one may as well use r=R/A, g=G/A, b=B/A. Without loss of
298 // generality, assume that R/G/B values are in the range 0..1.)
299 //
300 // Let Max = max(R,G,B), Min = min(R,G,B), and Med be the median value. Then L =
301 // (Max+Min)/2. If L is to remain constant, Max+Min must also remain constant.
302 //
303 // For H to remain constant, first, the (numerical) order of R/G/B (from
304 // smallest to largest) must remain the same. Second, all the ratios
305 // (R-G)/(Max-Min), (R-B)/(Max-Min), (G-B)/(Max-Min) must remain constant (of
306 // course, if Max = Min, then S = 0 and no saturation change is well-defined,
307 // since H is not well-defined).
308 //
309 // Let C_max be a colour with value Max, C_min be one with value Min, and C_med
310 // the remaining colour. Increasing saturation (to the maximum) is accomplished
311 // by increasing the value of C_max while simultaneously decreasing C_min and
312 // changing C_med so that the ratios are maintained; for the latter, it suffices
313 // to keep (C_med-C_min)/(C_max-C_min) constant (and equal to
314 // (Med-Min)/(Max-Min)).
315
316 // Line processor: H no-op, S decrease, L no-op.
LineProcHnopSdecLnop(const color_utils::HSL & hsl_shift,const SkPMColor * in,SkPMColor * out,int width)317 void LineProcHnopSdecLnop(const color_utils::HSL& hsl_shift,
318 const SkPMColor* in,
319 SkPMColor* out,
320 int width) {
321 DCHECK(hsl_shift.h < 0);
322 DCHECK(hsl_shift.s >= 0 && hsl_shift.s <= 0.5 - HSLShift::epsilon);
323 DCHECK(hsl_shift.l < 0 || fabs(hsl_shift.l - 0.5) < HSLShift::epsilon);
324
325 const int32_t denom = 65536;
326 int32_t s_numer = static_cast<int32_t>(hsl_shift.s * 2 * denom);
327 for (int x = 0; x < width; x++) {
328 int32_t a = static_cast<int32_t>(SkGetPackedA32(in[x]));
329 int32_t r = static_cast<int32_t>(SkGetPackedR32(in[x]));
330 int32_t g = static_cast<int32_t>(SkGetPackedG32(in[x]));
331 int32_t b = static_cast<int32_t>(SkGetPackedB32(in[x]));
332
333 int32_t vmax, vmin;
334 if (r > g) { // This uses 3 compares rather than 4.
335 vmax = std::max(r, b);
336 vmin = std::min(g, b);
337 } else {
338 vmax = std::max(g, b);
339 vmin = std::min(r, b);
340 }
341
342 // Use denom * L to avoid rounding.
343 int32_t denom_l = (vmax + vmin) * (denom / 2);
344 int32_t s_numer_l = (vmax + vmin) * s_numer / 2;
345
346 r = (denom_l + r * s_numer - s_numer_l) / denom;
347 g = (denom_l + g * s_numer - s_numer_l) / denom;
348 b = (denom_l + b * s_numer - s_numer_l) / denom;
349 out[x] = SkPackARGB32(a, r, g, b);
350 }
351 }
352
353 // Line processor: H no-op, S decrease, L decrease.
LineProcHnopSdecLdec(const color_utils::HSL & hsl_shift,const SkPMColor * in,SkPMColor * out,int width)354 void LineProcHnopSdecLdec(const color_utils::HSL& hsl_shift,
355 const SkPMColor* in,
356 SkPMColor* out,
357 int width) {
358 DCHECK(hsl_shift.h < 0);
359 DCHECK(hsl_shift.s >= 0 && hsl_shift.s <= 0.5 - HSLShift::epsilon);
360 DCHECK(hsl_shift.l >= 0 && hsl_shift.l <= 0.5 - HSLShift::epsilon);
361
362 // Can't be too big since we need room for denom*denom and a bit for sign.
363 const int32_t denom = 1024;
364 int32_t l_numer = static_cast<int32_t>(hsl_shift.l * 2 * denom);
365 int32_t s_numer = static_cast<int32_t>(hsl_shift.s * 2 * denom);
366 for (int x = 0; x < width; x++) {
367 int32_t a = static_cast<int32_t>(SkGetPackedA32(in[x]));
368 int32_t r = static_cast<int32_t>(SkGetPackedR32(in[x]));
369 int32_t g = static_cast<int32_t>(SkGetPackedG32(in[x]));
370 int32_t b = static_cast<int32_t>(SkGetPackedB32(in[x]));
371
372 int32_t vmax, vmin;
373 if (r > g) { // This uses 3 compares rather than 4.
374 vmax = std::max(r, b);
375 vmin = std::min(g, b);
376 } else {
377 vmax = std::max(g, b);
378 vmin = std::min(r, b);
379 }
380
381 // Use denom * L to avoid rounding.
382 int32_t denom_l = (vmax + vmin) * (denom / 2);
383 int32_t s_numer_l = (vmax + vmin) * s_numer / 2;
384
385 r = (denom_l + r * s_numer - s_numer_l) * l_numer / (denom * denom);
386 g = (denom_l + g * s_numer - s_numer_l) * l_numer / (denom * denom);
387 b = (denom_l + b * s_numer - s_numer_l) * l_numer / (denom * denom);
388 out[x] = SkPackARGB32(a, r, g, b);
389 }
390 }
391
392 // Line processor: H no-op, S decrease, L increase.
LineProcHnopSdecLinc(const color_utils::HSL & hsl_shift,const SkPMColor * in,SkPMColor * out,int width)393 void LineProcHnopSdecLinc(const color_utils::HSL& hsl_shift,
394 const SkPMColor* in,
395 SkPMColor* out,
396 int width) {
397 DCHECK(hsl_shift.h < 0);
398 DCHECK(hsl_shift.s >= 0 && hsl_shift.s <= 0.5 - HSLShift::epsilon);
399 DCHECK(hsl_shift.l >= 0.5 + HSLShift::epsilon && hsl_shift.l <= 1);
400
401 // Can't be too big since we need room for denom*denom and a bit for sign.
402 const int32_t denom = 1024;
403 int32_t l_numer = static_cast<int32_t>((hsl_shift.l - 0.5) * 2 * denom);
404 int32_t s_numer = static_cast<int32_t>(hsl_shift.s * 2 * denom);
405 for (int x = 0; x < width; x++) {
406 int32_t a = static_cast<int32_t>(SkGetPackedA32(in[x]));
407 int32_t r = static_cast<int32_t>(SkGetPackedR32(in[x]));
408 int32_t g = static_cast<int32_t>(SkGetPackedG32(in[x]));
409 int32_t b = static_cast<int32_t>(SkGetPackedB32(in[x]));
410
411 int32_t vmax, vmin;
412 if (r > g) { // This uses 3 compares rather than 4.
413 vmax = std::max(r, b);
414 vmin = std::min(g, b);
415 } else {
416 vmax = std::max(g, b);
417 vmin = std::min(r, b);
418 }
419
420 // Use denom * L to avoid rounding.
421 int32_t denom_l = (vmax + vmin) * (denom / 2);
422 int32_t s_numer_l = (vmax + vmin) * s_numer / 2;
423
424 r = denom_l + r * s_numer - s_numer_l;
425 g = denom_l + g * s_numer - s_numer_l;
426 b = denom_l + b * s_numer - s_numer_l;
427
428 r = (r * denom + (a * denom - r) * l_numer) / (denom * denom);
429 g = (g * denom + (a * denom - g) * l_numer) / (denom * denom);
430 b = (b * denom + (a * denom - b) * l_numer) / (denom * denom);
431 out[x] = SkPackARGB32(a, r, g, b);
432 }
433 }
434
435 const LineProcessor kLineProcessors[kNumHOps][kNumSOps][kNumLOps] = {
436 { // H: kOpHNone
437 { // S: kOpSNone
438 LineProcCopy, // L: kOpLNone
439 LineProcHnopSnopLdec, // L: kOpLDec
440 LineProcHnopSnopLinc // L: kOpLInc
441 },
442 { // S: kOpSDec
443 LineProcHnopSdecLnop, // L: kOpLNone
444 LineProcHnopSdecLdec, // L: kOpLDec
445 LineProcHnopSdecLinc // L: kOpLInc
446 },
447 { // S: kOpSInc
448 LineProcDefault, // L: kOpLNone
449 LineProcDefault, // L: kOpLDec
450 LineProcDefault // L: kOpLInc
451 }
452 },
453 { // H: kOpHShift
454 { // S: kOpSNone
455 LineProcDefault, // L: kOpLNone
456 LineProcDefault, // L: kOpLDec
457 LineProcDefault // L: kOpLInc
458 },
459 { // S: kOpSDec
460 LineProcDefault, // L: kOpLNone
461 LineProcDefault, // L: kOpLDec
462 LineProcDefault // L: kOpLInc
463 },
464 { // S: kOpSInc
465 LineProcDefault, // L: kOpLNone
466 LineProcDefault, // L: kOpLDec
467 LineProcDefault // L: kOpLInc
468 }
469 }
470 };
471
472 } // namespace HSLShift
473 } // namespace
474
475 // static
CreateHSLShiftedBitmap(const SkBitmap & bitmap,const color_utils::HSL & hsl_shift)476 SkBitmap SkBitmapOperations::CreateHSLShiftedBitmap(
477 const SkBitmap& bitmap,
478 const color_utils::HSL& hsl_shift) {
479 // Default to NOPs.
480 HSLShift::OperationOnH H_op = HSLShift::kOpHNone;
481 HSLShift::OperationOnS S_op = HSLShift::kOpSNone;
482 HSLShift::OperationOnL L_op = HSLShift::kOpLNone;
483
484 if (hsl_shift.h >= 0 && hsl_shift.h <= 1)
485 H_op = HSLShift::kOpHShift;
486
487 // Saturation shift: 0 -> fully desaturate, 0.5 -> NOP, 1 -> fully saturate.
488 if (hsl_shift.s >= 0 && hsl_shift.s <= (0.5 - HSLShift::epsilon))
489 S_op = HSLShift::kOpSDec;
490 else if (hsl_shift.s >= (0.5 + HSLShift::epsilon))
491 S_op = HSLShift::kOpSInc;
492
493 // Lightness shift: 0 -> black, 0.5 -> NOP, 1 -> white.
494 if (hsl_shift.l >= 0 && hsl_shift.l <= (0.5 - HSLShift::epsilon))
495 L_op = HSLShift::kOpLDec;
496 else if (hsl_shift.l >= (0.5 + HSLShift::epsilon))
497 L_op = HSLShift::kOpLInc;
498
499 HSLShift::LineProcessor line_proc =
500 HSLShift::kLineProcessors[H_op][S_op][L_op];
501
502 DCHECK(bitmap.empty() == false);
503 DCHECK(bitmap.colorType() == kN32_SkColorType);
504
505 SkBitmap shifted;
506 shifted.allocN32Pixels(bitmap.width(), bitmap.height());
507
508 // Loop through the pixels of the original bitmap.
509 for (int y = 0; y < bitmap.height(); ++y) {
510 SkPMColor* pixels = bitmap.getAddr32(0, y);
511 SkPMColor* tinted_pixels = shifted.getAddr32(0, y);
512
513 (*line_proc)(hsl_shift, pixels, tinted_pixels, bitmap.width());
514 }
515
516 return shifted;
517 }
518
519 // static
CreateTiledBitmap(const SkBitmap & source,int src_x,int src_y,int dst_w,int dst_h)520 SkBitmap SkBitmapOperations::CreateTiledBitmap(const SkBitmap& source,
521 int src_x, int src_y,
522 int dst_w, int dst_h) {
523 DCHECK(source.colorType() == kN32_SkColorType);
524
525 SkBitmap cropped;
526 cropped.allocN32Pixels(dst_w, dst_h);
527
528 // Loop through the pixels of the original bitmap.
529 for (int y = 0; y < dst_h; ++y) {
530 int y_pix = (src_y + y) % source.height();
531 while (y_pix < 0)
532 y_pix += source.height();
533
534 uint32_t* source_row = source.getAddr32(0, y_pix);
535 uint32_t* dst_row = cropped.getAddr32(0, y);
536
537 for (int x = 0; x < dst_w; ++x) {
538 int x_pix = (src_x + x) % source.width();
539 while (x_pix < 0)
540 x_pix += source.width();
541
542 dst_row[x] = source_row[x_pix];
543 }
544 }
545
546 return cropped;
547 }
548
549 // static
DownsampleByTwoUntilSize(const SkBitmap & bitmap,int min_w,int min_h)550 SkBitmap SkBitmapOperations::DownsampleByTwoUntilSize(const SkBitmap& bitmap,
551 int min_w, int min_h) {
552 if ((bitmap.width() <= min_w) || (bitmap.height() <= min_h) ||
553 (min_w < 0) || (min_h < 0))
554 return bitmap;
555
556 // Since bitmaps are refcounted, this copy will be fast.
557 SkBitmap current = bitmap;
558 while ((current.width() >= min_w * 2) && (current.height() >= min_h * 2) &&
559 (current.width() > 1) && (current.height() > 1))
560 current = DownsampleByTwo(current);
561 return current;
562 }
563
564 // static
DownsampleByTwo(const SkBitmap & bitmap)565 SkBitmap SkBitmapOperations::DownsampleByTwo(const SkBitmap& bitmap) {
566 // Handle the nop case.
567 if ((bitmap.width() <= 1) || (bitmap.height() <= 1))
568 return bitmap;
569
570 SkBitmap result;
571 result.allocN32Pixels((bitmap.width() + 1) / 2, (bitmap.height() + 1) / 2);
572
573 const int resultLastX = result.width() - 1;
574 const int srcLastX = bitmap.width() - 1;
575
576 for (int dest_y = 0; dest_y < result.height(); ++dest_y) {
577 const int src_y = dest_y << 1;
578 const SkPMColor* SK_RESTRICT cur_src0 = bitmap.getAddr32(0, src_y);
579 const SkPMColor* SK_RESTRICT cur_src1 = cur_src0;
580 if (src_y + 1 < bitmap.height())
581 cur_src1 = bitmap.getAddr32(0, src_y + 1);
582
583 SkPMColor* SK_RESTRICT cur_dst = result.getAddr32(0, dest_y);
584
585 for (int dest_x = 0; dest_x <= resultLastX; ++dest_x) {
586 // This code is based on downsampleby2_proc32 in SkBitmap.cpp. It is very
587 // clever in that it does two channels at once: alpha and green ("ag")
588 // and red and blue ("rb"). Each channel gets averaged across 4 pixels
589 // to get the result.
590 int bump_x = (dest_x << 1) < srcLastX;
591 SkPMColor tmp, ag, rb;
592
593 // Top left pixel of the 2x2 block.
594 tmp = cur_src0[0];
595 ag = (tmp >> 8) & 0xFF00FF;
596 rb = tmp & 0xFF00FF;
597
598 // Top right pixel of the 2x2 block.
599 tmp = cur_src0[bump_x];
600 ag += (tmp >> 8) & 0xFF00FF;
601 rb += tmp & 0xFF00FF;
602
603 // Bottom left pixel of the 2x2 block.
604 tmp = cur_src1[0];
605 ag += (tmp >> 8) & 0xFF00FF;
606 rb += tmp & 0xFF00FF;
607
608 // Bottom right pixel of the 2x2 block.
609 tmp = cur_src1[bump_x];
610 ag += (tmp >> 8) & 0xFF00FF;
611 rb += tmp & 0xFF00FF;
612
613 // Put the channels back together, dividing each by 4 to get the average.
614 // |ag| has the alpha and green channels shifted right by 8 bits from
615 // there they should end up, so shifting left by 6 gives them in the
616 // correct position divided by 4.
617 *cur_dst++ = ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00);
618
619 cur_src0 += 2;
620 cur_src1 += 2;
621 }
622 }
623
624 return result;
625 }
626
627 // static
UnPreMultiply(const SkBitmap & bitmap)628 SkBitmap SkBitmapOperations::UnPreMultiply(const SkBitmap& bitmap) {
629 if (bitmap.isNull())
630 return bitmap;
631 if (bitmap.isOpaque())
632 return bitmap;
633
634 const SkImageInfo& opaque_info =
635 bitmap.info().makeAlphaType(kOpaque_SkAlphaType);
636 SkBitmap opaque_bitmap;
637 opaque_bitmap.allocPixels(opaque_info);
638
639 for (int y = 0; y < opaque_bitmap.height(); y++) {
640 for (int x = 0; x < opaque_bitmap.width(); x++) {
641 uint32_t src_pixel = *bitmap.getAddr32(x, y);
642 uint32_t* dst_pixel = opaque_bitmap.getAddr32(x, y);
643 SkColor unmultiplied = SkUnPreMultiply::PMColorToColor(src_pixel);
644 *dst_pixel = unmultiplied;
645 }
646 }
647
648 return opaque_bitmap;
649 }
650
651 // static
CreateTransposedBitmap(const SkBitmap & image)652 SkBitmap SkBitmapOperations::CreateTransposedBitmap(const SkBitmap& image) {
653 DCHECK(image.colorType() == kN32_SkColorType);
654
655 SkBitmap transposed;
656 transposed.allocN32Pixels(image.height(), image.width());
657
658 for (int y = 0; y < image.height(); ++y) {
659 uint32_t* image_row = image.getAddr32(0, y);
660 for (int x = 0; x < image.width(); ++x) {
661 uint32_t* dst = transposed.getAddr32(y, x);
662 *dst = image_row[x];
663 }
664 }
665
666 return transposed;
667 }
668
669 // static
CreateColorMask(const SkBitmap & bitmap,SkColor c)670 SkBitmap SkBitmapOperations::CreateColorMask(const SkBitmap& bitmap,
671 SkColor c) {
672 DCHECK(bitmap.colorType() == kN32_SkColorType);
673
674 SkBitmap color_mask;
675 color_mask.allocN32Pixels(bitmap.width(), bitmap.height());
676 color_mask.eraseARGB(0, 0, 0, 0);
677
678 SkCanvas canvas(color_mask);
679
680 SkPaint paint;
681 paint.setColorFilter(SkColorFilters::Blend(c, SkBlendMode::kSrcIn));
682 canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0), &paint);
683 return color_mask;
684 }
685
686 // static
CreateDropShadow(const SkBitmap & bitmap,const gfx::ShadowValues & shadows)687 SkBitmap SkBitmapOperations::CreateDropShadow(
688 const SkBitmap& bitmap,
689 const gfx::ShadowValues& shadows) {
690 DCHECK(bitmap.colorType() == kN32_SkColorType);
691
692 // Shadow margin insets are negative values because they grow outside.
693 // Negate them here as grow direction is not important and only pixel value
694 // is of interest here.
695 gfx::Insets shadow_margin = -gfx::ShadowValue::GetMargin(shadows);
696
697 SkBitmap image_with_shadow;
698 image_with_shadow.allocN32Pixels(bitmap.width() + shadow_margin.width(),
699 bitmap.height() + shadow_margin.height());
700 image_with_shadow.eraseARGB(0, 0, 0, 0);
701
702 SkCanvas canvas(image_with_shadow);
703 canvas.translate(SkIntToScalar(shadow_margin.left()),
704 SkIntToScalar(shadow_margin.top()));
705
706 SkPaint paint;
707 for (size_t i = 0; i < shadows.size(); ++i) {
708 const gfx::ShadowValue& shadow = shadows[i];
709 SkBitmap shadow_image = SkBitmapOperations::CreateColorMask(bitmap,
710 shadow.color());
711
712 // The blur is halved to produce a shadow that correctly fits within the
713 // |shadow_margin|.
714 SkScalar sigma = SkDoubleToScalar(shadow.blur() / 2);
715 paint.setImageFilter(SkBlurImageFilter::Make(sigma, sigma, nullptr));
716
717 canvas.saveLayer(0, &paint);
718 canvas.drawBitmap(shadow_image,
719 SkIntToScalar(shadow.x()),
720 SkIntToScalar(shadow.y()));
721 canvas.restore();
722 }
723
724 canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0));
725 return image_with_shadow;
726 }
727
728 // static
Rotate(const SkBitmap & source,RotationAmount rotation)729 SkBitmap SkBitmapOperations::Rotate(const SkBitmap& source,
730 RotationAmount rotation) {
731 // SkCanvas::drawBitmap() fails silently with unpremultiplied SkBitmap.
732 DCHECK_NE(source.info().alphaType(), kUnpremul_SkAlphaType);
733
734 SkBitmap result;
735 SkScalar angle = SkFloatToScalar(0.0f);
736
737 switch (rotation) {
738 case ROTATION_90_CW:
739 angle = SkFloatToScalar(90.0f);
740 result.allocN32Pixels(source.height(), source.width());
741 break;
742 case ROTATION_180_CW:
743 angle = SkFloatToScalar(180.0f);
744 result.allocN32Pixels(source.width(), source.height());
745 break;
746 case ROTATION_270_CW:
747 angle = SkFloatToScalar(270.0f);
748 result.allocN32Pixels(source.height(), source.width());
749 break;
750 }
751
752 SkCanvas canvas(result);
753 canvas.clear(SkColorSetARGB(0, 0, 0, 0));
754
755 canvas.translate(SkFloatToScalar(result.width() * 0.5f),
756 SkFloatToScalar(result.height() * 0.5f));
757 canvas.rotate(angle);
758 canvas.translate(-SkFloatToScalar(source.width() * 0.5f),
759 -SkFloatToScalar(source.height() * 0.5f));
760 canvas.drawBitmap(source, 0, 0);
761 canvas.flush();
762
763 return result;
764 }
765