blob: e45789333c76b2fe292ca4f6e7f47619a49cea9f [file] [log] [blame]
[email protected]125b62002012-03-19 14:30:401// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]cab34d6a2009-09-24 01:14:522// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]08397d52011-02-05 01:53:385#include "ui/gfx/skbitmap_operations.h"
[email protected]cab34d6a2009-09-24 01:14:526
avic89eb8d42015-12-23 08:08:187#include <stddef.h>
8#include <stdint.h>
[email protected]6e25ce82010-04-26 17:12:409#include <string.h>
tfarina655f81d2014-12-23 02:38:5010#include <algorithm>
[email protected]0fb499a2009-11-10 21:03:3311
[email protected]cab34d6a2009-09-24 01:14:5212#include "base/logging.h"
[email protected]cab34d6a2009-09-24 01:14:5213#include "third_party/skia/include/core/SkBitmap.h"
[email protected]0fb499a2009-11-10 21:03:3314#include "third_party/skia/include/core/SkCanvas.h"
[email protected]6cd13aa2012-03-23 02:27:1615#include "third_party/skia/include/core/SkColorFilter.h"
[email protected]cab34d6a2009-09-24 01:14:5216#include "third_party/skia/include/core/SkColorPriv.h"
17#include "third_party/skia/include/core/SkUnPreMultiply.h"
[email protected]6cd13aa2012-03-23 02:27:1618#include "third_party/skia/include/effects/SkBlurImageFilter.h"
tfarina655f81d2014-12-23 02:38:5019#include "ui/gfx/geometry/insets.h"
20#include "ui/gfx/geometry/point.h"
tfarinaebe974f02015-01-03 04:25:3221#include "ui/gfx/geometry/size.h"
[email protected]cab34d6a2009-09-24 01:14:5222
23// static
[email protected]0fb499a2009-11-10 21:03:3324SkBitmap SkBitmapOperations::CreateInvertedBitmap(const SkBitmap& image) {
[email protected]c80330da2014-07-01 19:26:2725 DCHECK(image.colorType() == kN32_SkColorType);
[email protected]0fb499a2009-11-10 21:03:3326
27 SkAutoLockPixels lock_image(image);
28
29 SkBitmap inverted;
[email protected]f62e57b2014-03-12 20:52:0230 inverted.allocN32Pixels(image.width(), image.height());
[email protected]0fb499a2009-11-10 21:03:3331
32 for (int y = 0; y < image.height(); ++y) {
avic89eb8d42015-12-23 08:08:1833 uint32_t* image_row = image.getAddr32(0, y);
34 uint32_t* dst_row = inverted.getAddr32(0, y);
[email protected]0fb499a2009-11-10 21:03:3335
36 for (int x = 0; x < image.width(); ++x) {
avic89eb8d42015-12-23 08:08:1837 uint32_t image_pixel = image_row[x];
[email protected]0fb499a2009-11-10 21:03:3338 dst_row[x] = (image_pixel & 0xFF000000) |
39 (0x00FFFFFF - (image_pixel & 0x00FFFFFF));
40 }
41 }
42
43 return inverted;
44}
45
46// static
[email protected]cab34d6a2009-09-24 01:14:5247SkBitmap SkBitmapOperations::CreateBlendedBitmap(const SkBitmap& first,
48 const SkBitmap& second,
49 double alpha) {
50 DCHECK((alpha >= 0) && (alpha <= 1));
51 DCHECK(first.width() == second.width());
52 DCHECK(first.height() == second.height());
53 DCHECK(first.bytesPerPixel() == second.bytesPerPixel());
[email protected]c80330da2014-07-01 19:26:2754 DCHECK(first.colorType() == kN32_SkColorType);
[email protected]cab34d6a2009-09-24 01:14:5255
56 // Optimize for case where we won't need to blend anything.
57 static const double alpha_min = 1.0 / 255;
58 static const double alpha_max = 254.0 / 255;
59 if (alpha < alpha_min)
60 return first;
61 else if (alpha > alpha_max)
62 return second;
63
64 SkAutoLockPixels lock_first(first);
65 SkAutoLockPixels lock_second(second);
66
67 SkBitmap blended;
[email protected]f62e57b2014-03-12 20:52:0268 blended.allocN32Pixels(first.width(), first.height());
[email protected]cab34d6a2009-09-24 01:14:5269
70 double first_alpha = 1 - alpha;
71
72 for (int y = 0; y < first.height(); ++y) {
avic89eb8d42015-12-23 08:08:1873 uint32_t* first_row = first.getAddr32(0, y);
74 uint32_t* second_row = second.getAddr32(0, y);
75 uint32_t* dst_row = blended.getAddr32(0, y);
[email protected]cab34d6a2009-09-24 01:14:5276
77 for (int x = 0; x < first.width(); ++x) {
avic89eb8d42015-12-23 08:08:1878 uint32_t first_pixel = first_row[x];
79 uint32_t second_pixel = second_row[x];
[email protected]cab34d6a2009-09-24 01:14:5280
81 int a = static_cast<int>((SkColorGetA(first_pixel) * first_alpha) +
82 (SkColorGetA(second_pixel) * alpha));
83 int r = static_cast<int>((SkColorGetR(first_pixel) * first_alpha) +
84 (SkColorGetR(second_pixel) * alpha));
85 int g = static_cast<int>((SkColorGetG(first_pixel) * first_alpha) +
86 (SkColorGetG(second_pixel) * alpha));
87 int b = static_cast<int>((SkColorGetB(first_pixel) * first_alpha) +
88 (SkColorGetB(second_pixel) * alpha));
89
90 dst_row[x] = SkColorSetARGB(a, r, g, b);
91 }
92 }
93
94 return blended;
95}
96
97// static
98SkBitmap SkBitmapOperations::CreateMaskedBitmap(const SkBitmap& rgb,
99 const SkBitmap& alpha) {
100 DCHECK(rgb.width() == alpha.width());
101 DCHECK(rgb.height() == alpha.height());
102 DCHECK(rgb.bytesPerPixel() == alpha.bytesPerPixel());
[email protected]c80330da2014-07-01 19:26:27103 DCHECK(rgb.colorType() == kN32_SkColorType);
104 DCHECK(alpha.colorType() == kN32_SkColorType);
[email protected]cab34d6a2009-09-24 01:14:52105
106 SkBitmap masked;
[email protected]f62e57b2014-03-12 20:52:02107 masked.allocN32Pixels(rgb.width(), rgb.height());
[email protected]cab34d6a2009-09-24 01:14:52108
109 SkAutoLockPixels lock_rgb(rgb);
110 SkAutoLockPixels lock_alpha(alpha);
111 SkAutoLockPixels lock_masked(masked);
112
113 for (int y = 0; y < masked.height(); ++y) {
avic89eb8d42015-12-23 08:08:18114 uint32_t* rgb_row = rgb.getAddr32(0, y);
115 uint32_t* alpha_row = alpha.getAddr32(0, y);
116 uint32_t* dst_row = masked.getAddr32(0, y);
[email protected]cab34d6a2009-09-24 01:14:52117
118 for (int x = 0; x < masked.width(); ++x) {
moshayedi6e9363642015-10-29 20:27:40119 unsigned alpha = SkGetPackedA32(alpha_row[x]);
120 unsigned scale = SkAlpha255To256(alpha);
121 dst_row[x] = SkAlphaMulQ(rgb_row[x], scale);
[email protected]cab34d6a2009-09-24 01:14:52122 }
123 }
124
125 return masked;
126}
127
128// static
129SkBitmap SkBitmapOperations::CreateButtonBackground(SkColor color,
130 const SkBitmap& image,
131 const SkBitmap& mask) {
[email protected]c80330da2014-07-01 19:26:27132 DCHECK(image.colorType() == kN32_SkColorType);
133 DCHECK(mask.colorType() == kN32_SkColorType);
[email protected]cab34d6a2009-09-24 01:14:52134
135 SkBitmap background;
[email protected]f62e57b2014-03-12 20:52:02136 background.allocN32Pixels(mask.width(), mask.height());
[email protected]cab34d6a2009-09-24 01:14:52137
138 double bg_a = SkColorGetA(color);
139 double bg_r = SkColorGetR(color);
140 double bg_g = SkColorGetG(color);
141 double bg_b = SkColorGetB(color);
142
143 SkAutoLockPixels lock_mask(mask);
144 SkAutoLockPixels lock_image(image);
145 SkAutoLockPixels lock_background(background);
146
147 for (int y = 0; y < mask.height(); ++y) {
avic89eb8d42015-12-23 08:08:18148 uint32_t* dst_row = background.getAddr32(0, y);
149 uint32_t* image_row = image.getAddr32(0, y % image.height());
150 uint32_t* mask_row = mask.getAddr32(0, y);
[email protected]cab34d6a2009-09-24 01:14:52151
152 for (int x = 0; x < mask.width(); ++x) {
avic89eb8d42015-12-23 08:08:18153 uint32_t image_pixel = image_row[x % image.width()];
[email protected]cab34d6a2009-09-24 01:14:52154
155 double img_a = SkColorGetA(image_pixel);
156 double img_r = SkColorGetR(image_pixel);
157 double img_g = SkColorGetG(image_pixel);
158 double img_b = SkColorGetB(image_pixel);
159
160 double img_alpha = static_cast<double>(img_a) / 255.0;
161 double img_inv = 1 - img_alpha;
162
163 double mask_a = static_cast<double>(SkColorGetA(mask_row[x])) / 255.0;
164
165 dst_row[x] = SkColorSetARGB(
166 static_cast<int>(std::min(255.0, bg_a + img_a) * mask_a),
167 static_cast<int>(((bg_r * img_inv) + (img_r * img_alpha)) * mask_a),
168 static_cast<int>(((bg_g * img_inv) + (img_g * img_alpha)) * mask_a),
169 static_cast<int>(((bg_b * img_inv) + (img_b * img_alpha)) * mask_a));
170 }
171 }
172
173 return background;
174}
175
[email protected]6e25ce82010-04-26 17:12:40176namespace {
177namespace HSLShift {
178
179// TODO(viettrungluu): Some things have yet to be optimized at all.
180
181// Notes on and conventions used in the following code
182//
183// Conventions:
184// - R, G, B, A = obvious; as variables: |r|, |g|, |b|, |a| (see also below)
185// - H, S, L = obvious; as variables: |h|, |s|, |l| (see also below)
186// - variables derived from S, L shift parameters: |sdec| and |sinc| for S
187// increase and decrease factors, |ldec| and |linc| for L (see also below)
188//
189// To try to optimize HSL shifts, we do several things:
190// - Avoid unpremultiplying (then processing) then premultiplying. This means
191// that R, G, B values (and also L, but not H and S) should be treated as
192// having a range of 0..A (where A is alpha).
193// - Do things in integer/fixed-point. This avoids costly conversions between
194// floating-point and integer, though I should study the tradeoff more
195// carefully (presumably, at some point of processing complexity, converting
196// and processing using simpler floating-point code will begin to win in
197// performance). Also to be studied is the speed/type of floating point
198// conversions; see, e.g., <http://www.stereopsis.com/sree/fpu2006.html>.
199//
200// Conventions for fixed-point arithmetic
201// - Each function has a constant denominator (called |den|, which should be a
202// power of 2), appropriate for the computations done in that function.
203// - A value |x| is then typically represented by a numerator, named |x_num|,
204// so that its actual value is |x_num / den| (casting to floating-point
205// before division).
206// - To obtain |x_num| from |x|, simply multiply by |den|, i.e., |x_num = x *
207// den| (casting appropriately).
208// - When necessary, a value |x| may also be represented as a numerator over
209// the denominator squared (set |den2 = den * den|). In such a case, the
210// corresponding variable is called |x_num2| (so that its actual value is
211// |x_num^2 / den2|.
212// - The representation of the product of |x| and |y| is be called |x_y_num| if
213// |x * y == x_y_num / den|, and |xy_num2| if |x * y == x_y_num2 / den2|. In
214// the latter case, notice that one can calculate |x_y_num2 = x_num * y_num|.
215
216// Routine used to process a line; typically specialized for specific kinds of
217// HSL shifts (to optimize).
[email protected]80b3f7c2011-06-22 02:29:21218typedef void (*LineProcessor)(const color_utils::HSL&,
[email protected]6e25ce82010-04-26 17:12:40219 const SkPMColor*,
220 SkPMColor*,
221 int width);
222
223enum OperationOnH { kOpHNone = 0, kOpHShift, kNumHOps };
224enum OperationOnS { kOpSNone = 0, kOpSDec, kOpSInc, kNumSOps };
225enum OperationOnL { kOpLNone = 0, kOpLDec, kOpLInc, kNumLOps };
226
227// Epsilon used to judge when shift values are close enough to various critical
228// values (typically 0.5, which yields a no-op for S and L shifts. 1/256 should
229// be small enough, but let's play it safe>
230const double epsilon = 0.0005;
231
232// Line processor: default/universal (i.e., old-school).
[email protected]80b3f7c2011-06-22 02:29:21233void LineProcDefault(const color_utils::HSL& hsl_shift,
234 const SkPMColor* in,
235 SkPMColor* out,
236 int width) {
[email protected]6e25ce82010-04-26 17:12:40237 for (int x = 0; x < width; x++) {
238 out[x] = SkPreMultiplyColor(color_utils::HSLShift(
239 SkUnPreMultiply::PMColorToColor(in[x]), hsl_shift));
240 }
241}
242
243// Line processor: no-op (i.e., copy).
[email protected]80b3f7c2011-06-22 02:29:21244void LineProcCopy(const color_utils::HSL& hsl_shift,
245 const SkPMColor* in,
246 SkPMColor* out,
247 int width) {
[email protected]6e25ce82010-04-26 17:12:40248 DCHECK(hsl_shift.h < 0);
249 DCHECK(hsl_shift.s < 0 || fabs(hsl_shift.s - 0.5) < HSLShift::epsilon);
250 DCHECK(hsl_shift.l < 0 || fabs(hsl_shift.l - 0.5) < HSLShift::epsilon);
251 memcpy(out, in, static_cast<size_t>(width) * sizeof(out[0]));
252}
253
254// Line processor: H no-op, S no-op, L decrease.
[email protected]80b3f7c2011-06-22 02:29:21255void LineProcHnopSnopLdec(const color_utils::HSL& hsl_shift,
256 const SkPMColor* in,
257 SkPMColor* out,
258 int width) {
[email protected]6e25ce82010-04-26 17:12:40259 const uint32_t den = 65536;
260
261 DCHECK(hsl_shift.h < 0);
262 DCHECK(hsl_shift.s < 0 || fabs(hsl_shift.s - 0.5) < HSLShift::epsilon);
263 DCHECK(hsl_shift.l <= 0.5 - HSLShift::epsilon && hsl_shift.l >= 0);
264
265 uint32_t ldec_num = static_cast<uint32_t>(hsl_shift.l * 2 * den);
266 for (int x = 0; x < width; x++) {
267 uint32_t a = SkGetPackedA32(in[x]);
268 uint32_t r = SkGetPackedR32(in[x]);
269 uint32_t g = SkGetPackedG32(in[x]);
270 uint32_t b = SkGetPackedB32(in[x]);
271 r = r * ldec_num / den;
272 g = g * ldec_num / den;
273 b = b * ldec_num / den;
274 out[x] = SkPackARGB32(a, r, g, b);
275 }
276}
277
278// Line processor: H no-op, S no-op, L increase.
[email protected]80b3f7c2011-06-22 02:29:21279void LineProcHnopSnopLinc(const color_utils::HSL& hsl_shift,
280 const SkPMColor* in,
281 SkPMColor* out,
282 int width) {
[email protected]6e25ce82010-04-26 17:12:40283 const uint32_t den = 65536;
284
285 DCHECK(hsl_shift.h < 0);
286 DCHECK(hsl_shift.s < 0 || fabs(hsl_shift.s - 0.5) < HSLShift::epsilon);
287 DCHECK(hsl_shift.l >= 0.5 + HSLShift::epsilon && hsl_shift.l <= 1);
288
289 uint32_t linc_num = static_cast<uint32_t>((hsl_shift.l - 0.5) * 2 * den);
290 for (int x = 0; x < width; x++) {
291 uint32_t a = SkGetPackedA32(in[x]);
292 uint32_t r = SkGetPackedR32(in[x]);
293 uint32_t g = SkGetPackedG32(in[x]);
294 uint32_t b = SkGetPackedB32(in[x]);
295 r += (a - r) * linc_num / den;
296 g += (a - g) * linc_num / den;
297 b += (a - b) * linc_num / den;
298 out[x] = SkPackARGB32(a, r, g, b);
299 }
300}
301
302// Saturation changes modifications in RGB
303//
304// (Note that as a further complication, the values we deal in are
305// premultiplied, so R/G/B values must be in the range 0..A. For mathematical
306// purposes, one may as well use r=R/A, g=G/A, b=B/A. Without loss of
307// generality, assume that R/G/B values are in the range 0..1.)
308//
309// Let Max = max(R,G,B), Min = min(R,G,B), and Med be the median value. Then L =
310// (Max+Min)/2. If L is to remain constant, Max+Min must also remain constant.
311//
312// For H to remain constant, first, the (numerical) order of R/G/B (from
313// smallest to largest) must remain the same. Second, all the ratios
314// (R-G)/(Max-Min), (R-B)/(Max-Min), (G-B)/(Max-Min) must remain constant (of
315// course, if Max = Min, then S = 0 and no saturation change is well-defined,
316// since H is not well-defined).
317//
318// Let C_max be a colour with value Max, C_min be one with value Min, and C_med
319// the remaining colour. Increasing saturation (to the maximum) is accomplished
320// by increasing the value of C_max while simultaneously decreasing C_min and
321// changing C_med so that the ratios are maintained; for the latter, it suffices
322// to keep (C_med-C_min)/(C_max-C_min) constant (and equal to
323// (Med-Min)/(Max-Min)).
324
325// Line processor: H no-op, S decrease, L no-op.
[email protected]80b3f7c2011-06-22 02:29:21326void LineProcHnopSdecLnop(const color_utils::HSL& hsl_shift,
327 const SkPMColor* in,
328 SkPMColor* out,
329 int width) {
[email protected]6e25ce82010-04-26 17:12:40330 DCHECK(hsl_shift.h < 0);
331 DCHECK(hsl_shift.s >= 0 && hsl_shift.s <= 0.5 - HSLShift::epsilon);
332 DCHECK(hsl_shift.l < 0 || fabs(hsl_shift.l - 0.5) < HSLShift::epsilon);
333
334 const int32_t denom = 65536;
335 int32_t s_numer = static_cast<int32_t>(hsl_shift.s * 2 * denom);
336 for (int x = 0; x < width; x++) {
337 int32_t a = static_cast<int32_t>(SkGetPackedA32(in[x]));
338 int32_t r = static_cast<int32_t>(SkGetPackedR32(in[x]));
339 int32_t g = static_cast<int32_t>(SkGetPackedG32(in[x]));
340 int32_t b = static_cast<int32_t>(SkGetPackedB32(in[x]));
341
342 int32_t vmax, vmin;
343 if (r > g) { // This uses 3 compares rather than 4.
344 vmax = std::max(r, b);
345 vmin = std::min(g, b);
346 } else {
347 vmax = std::max(g, b);
348 vmin = std::min(r, b);
349 }
350
351 // Use denom * L to avoid rounding.
352 int32_t denom_l = (vmax + vmin) * (denom / 2);
353 int32_t s_numer_l = (vmax + vmin) * s_numer / 2;
354
355 r = (denom_l + r * s_numer - s_numer_l) / denom;
356 g = (denom_l + g * s_numer - s_numer_l) / denom;
357 b = (denom_l + b * s_numer - s_numer_l) / denom;
358 out[x] = SkPackARGB32(a, r, g, b);
359 }
360}
361
362// Line processor: H no-op, S decrease, L decrease.
[email protected]80b3f7c2011-06-22 02:29:21363void LineProcHnopSdecLdec(const color_utils::HSL& hsl_shift,
364 const SkPMColor* in,
365 SkPMColor* out,
366 int width) {
[email protected]6e25ce82010-04-26 17:12:40367 DCHECK(hsl_shift.h < 0);
368 DCHECK(hsl_shift.s >= 0 && hsl_shift.s <= 0.5 - HSLShift::epsilon);
369 DCHECK(hsl_shift.l >= 0 && hsl_shift.l <= 0.5 - HSLShift::epsilon);
370
371 // Can't be too big since we need room for denom*denom and a bit for sign.
372 const int32_t denom = 1024;
373 int32_t l_numer = static_cast<int32_t>(hsl_shift.l * 2 * denom);
374 int32_t s_numer = static_cast<int32_t>(hsl_shift.s * 2 * denom);
375 for (int x = 0; x < width; x++) {
376 int32_t a = static_cast<int32_t>(SkGetPackedA32(in[x]));
377 int32_t r = static_cast<int32_t>(SkGetPackedR32(in[x]));
378 int32_t g = static_cast<int32_t>(SkGetPackedG32(in[x]));
379 int32_t b = static_cast<int32_t>(SkGetPackedB32(in[x]));
380
381 int32_t vmax, vmin;
382 if (r > g) { // This uses 3 compares rather than 4.
383 vmax = std::max(r, b);
384 vmin = std::min(g, b);
385 } else {
386 vmax = std::max(g, b);
387 vmin = std::min(r, b);
388 }
389
390 // Use denom * L to avoid rounding.
391 int32_t denom_l = (vmax + vmin) * (denom / 2);
392 int32_t s_numer_l = (vmax + vmin) * s_numer / 2;
393
394 r = (denom_l + r * s_numer - s_numer_l) * l_numer / (denom * denom);
395 g = (denom_l + g * s_numer - s_numer_l) * l_numer / (denom * denom);
396 b = (denom_l + b * s_numer - s_numer_l) * l_numer / (denom * denom);
397 out[x] = SkPackARGB32(a, r, g, b);
398 }
399}
400
401// Line processor: H no-op, S decrease, L increase.
[email protected]80b3f7c2011-06-22 02:29:21402void LineProcHnopSdecLinc(const color_utils::HSL& hsl_shift,
403 const SkPMColor* in,
404 SkPMColor* out,
405 int width) {
[email protected]6e25ce82010-04-26 17:12:40406 DCHECK(hsl_shift.h < 0);
407 DCHECK(hsl_shift.s >= 0 && hsl_shift.s <= 0.5 - HSLShift::epsilon);
408 DCHECK(hsl_shift.l >= 0.5 + HSLShift::epsilon && hsl_shift.l <= 1);
409
410 // Can't be too big since we need room for denom*denom and a bit for sign.
411 const int32_t denom = 1024;
412 int32_t l_numer = static_cast<int32_t>((hsl_shift.l - 0.5) * 2 * denom);
413 int32_t s_numer = static_cast<int32_t>(hsl_shift.s * 2 * denom);
414 for (int x = 0; x < width; x++) {
415 int32_t a = static_cast<int32_t>(SkGetPackedA32(in[x]));
416 int32_t r = static_cast<int32_t>(SkGetPackedR32(in[x]));
417 int32_t g = static_cast<int32_t>(SkGetPackedG32(in[x]));
418 int32_t b = static_cast<int32_t>(SkGetPackedB32(in[x]));
419
420 int32_t vmax, vmin;
421 if (r > g) { // This uses 3 compares rather than 4.
422 vmax = std::max(r, b);
423 vmin = std::min(g, b);
424 } else {
425 vmax = std::max(g, b);
426 vmin = std::min(r, b);
427 }
428
429 // Use denom * L to avoid rounding.
430 int32_t denom_l = (vmax + vmin) * (denom / 2);
431 int32_t s_numer_l = (vmax + vmin) * s_numer / 2;
432
433 r = denom_l + r * s_numer - s_numer_l;
434 g = denom_l + g * s_numer - s_numer_l;
435 b = denom_l + b * s_numer - s_numer_l;
436
437 r = (r * denom + (a * denom - r) * l_numer) / (denom * denom);
438 g = (g * denom + (a * denom - g) * l_numer) / (denom * denom);
439 b = (b * denom + (a * denom - b) * l_numer) / (denom * denom);
440 out[x] = SkPackARGB32(a, r, g, b);
441 }
442}
443
444const LineProcessor kLineProcessors[kNumHOps][kNumSOps][kNumLOps] = {
445 { // H: kOpHNone
446 { // S: kOpSNone
447 LineProcCopy, // L: kOpLNone
448 LineProcHnopSnopLdec, // L: kOpLDec
449 LineProcHnopSnopLinc // L: kOpLInc
450 },
451 { // S: kOpSDec
452 LineProcHnopSdecLnop, // L: kOpLNone
453 LineProcHnopSdecLdec, // L: kOpLDec
454 LineProcHnopSdecLinc // L: kOpLInc
455 },
456 { // S: kOpSInc
457 LineProcDefault, // L: kOpLNone
458 LineProcDefault, // L: kOpLDec
459 LineProcDefault // L: kOpLInc
460 }
461 },
462 { // H: kOpHShift
463 { // S: kOpSNone
464 LineProcDefault, // L: kOpLNone
465 LineProcDefault, // L: kOpLDec
466 LineProcDefault // L: kOpLInc
467 },
468 { // S: kOpSDec
469 LineProcDefault, // L: kOpLNone
470 LineProcDefault, // L: kOpLDec
471 LineProcDefault // L: kOpLInc
472 },
473 { // S: kOpSInc
474 LineProcDefault, // L: kOpLNone
475 LineProcDefault, // L: kOpLDec
476 LineProcDefault // L: kOpLInc
477 }
478 }
479};
480
481} // namespace HSLShift
482} // namespace
[email protected]cab34d6a2009-09-24 01:14:52483
484// static
485SkBitmap SkBitmapOperations::CreateHSLShiftedBitmap(
486 const SkBitmap& bitmap,
[email protected]37cbcfe2011-03-11 20:47:22487 const color_utils::HSL& hsl_shift) {
[email protected]6e25ce82010-04-26 17:12:40488 // Default to NOPs.
489 HSLShift::OperationOnH H_op = HSLShift::kOpHNone;
490 HSLShift::OperationOnS S_op = HSLShift::kOpSNone;
491 HSLShift::OperationOnL L_op = HSLShift::kOpLNone;
492
493 if (hsl_shift.h >= 0 && hsl_shift.h <= 1)
494 H_op = HSLShift::kOpHShift;
495
496 // Saturation shift: 0 -> fully desaturate, 0.5 -> NOP, 1 -> fully saturate.
497 if (hsl_shift.s >= 0 && hsl_shift.s <= (0.5 - HSLShift::epsilon))
498 S_op = HSLShift::kOpSDec;
499 else if (hsl_shift.s >= (0.5 + HSLShift::epsilon))
500 S_op = HSLShift::kOpSInc;
501
502 // Lightness shift: 0 -> black, 0.5 -> NOP, 1 -> white.
503 if (hsl_shift.l >= 0 && hsl_shift.l <= (0.5 - HSLShift::epsilon))
504 L_op = HSLShift::kOpLDec;
505 else if (hsl_shift.l >= (0.5 + HSLShift::epsilon))
506 L_op = HSLShift::kOpLInc;
507
508 HSLShift::LineProcessor line_proc =
509 HSLShift::kLineProcessors[H_op][S_op][L_op];
510
[email protected]cab34d6a2009-09-24 01:14:52511 DCHECK(bitmap.empty() == false);
[email protected]c80330da2014-07-01 19:26:27512 DCHECK(bitmap.colorType() == kN32_SkColorType);
[email protected]cab34d6a2009-09-24 01:14:52513
514 SkBitmap shifted;
[email protected]f62e57b2014-03-12 20:52:02515 shifted.allocN32Pixels(bitmap.width(), bitmap.height());
[email protected]cab34d6a2009-09-24 01:14:52516
517 SkAutoLockPixels lock_bitmap(bitmap);
518 SkAutoLockPixels lock_shifted(shifted);
519
520 // Loop through the pixels of the original bitmap.
521 for (int y = 0; y < bitmap.height(); ++y) {
522 SkPMColor* pixels = bitmap.getAddr32(0, y);
523 SkPMColor* tinted_pixels = shifted.getAddr32(0, y);
524
[email protected]6e25ce82010-04-26 17:12:40525 (*line_proc)(hsl_shift, pixels, tinted_pixels, bitmap.width());
[email protected]cab34d6a2009-09-24 01:14:52526 }
527
528 return shifted;
529}
530
531// static
532SkBitmap SkBitmapOperations::CreateTiledBitmap(const SkBitmap& source,
533 int src_x, int src_y,
534 int dst_w, int dst_h) {
[email protected]c80330da2014-07-01 19:26:27535 DCHECK(source.colorType() == kN32_SkColorType);
[email protected]cab34d6a2009-09-24 01:14:52536
537 SkBitmap cropped;
[email protected]f62e57b2014-03-12 20:52:02538 cropped.allocN32Pixels(dst_w, dst_h);
[email protected]cab34d6a2009-09-24 01:14:52539
540 SkAutoLockPixels lock_source(source);
541 SkAutoLockPixels lock_cropped(cropped);
542
543 // Loop through the pixels of the original bitmap.
544 for (int y = 0; y < dst_h; ++y) {
545 int y_pix = (src_y + y) % source.height();
546 while (y_pix < 0)
547 y_pix += source.height();
548
avic89eb8d42015-12-23 08:08:18549 uint32_t* source_row = source.getAddr32(0, y_pix);
550 uint32_t* dst_row = cropped.getAddr32(0, y);
[email protected]cab34d6a2009-09-24 01:14:52551
552 for (int x = 0; x < dst_w; ++x) {
553 int x_pix = (src_x + x) % source.width();
554 while (x_pix < 0)
555 x_pix += source.width();
556
557 dst_row[x] = source_row[x_pix];
558 }
559 }
560
561 return cropped;
562}
563
564// static
565SkBitmap SkBitmapOperations::DownsampleByTwoUntilSize(const SkBitmap& bitmap,
566 int min_w, int min_h) {
567 if ((bitmap.width() <= min_w) || (bitmap.height() <= min_h) ||
568 (min_w < 0) || (min_h < 0))
569 return bitmap;
570
571 // Since bitmaps are refcounted, this copy will be fast.
572 SkBitmap current = bitmap;
573 while ((current.width() >= min_w * 2) && (current.height() >= min_h * 2) &&
574 (current.width() > 1) && (current.height() > 1))
575 current = DownsampleByTwo(current);
576 return current;
577}
578
579// static
580SkBitmap SkBitmapOperations::DownsampleByTwo(const SkBitmap& bitmap) {
581 // Handle the nop case.
582 if ((bitmap.width() <= 1) || (bitmap.height() <= 1))
583 return bitmap;
584
585 SkBitmap result;
[email protected]f62e57b2014-03-12 20:52:02586 result.allocN32Pixels((bitmap.width() + 1) / 2, (bitmap.height() + 1) / 2);
[email protected]cab34d6a2009-09-24 01:14:52587
588 SkAutoLockPixels lock(bitmap);
[email protected]16d1608c2011-09-14 12:16:56589
590 const int resultLastX = result.width() - 1;
591 const int srcLastX = bitmap.width() - 1;
592
[email protected]cab34d6a2009-09-24 01:14:52593 for (int dest_y = 0; dest_y < result.height(); ++dest_y) {
[email protected]16d1608c2011-09-14 12:16:56594 const int src_y = dest_y << 1;
595 const SkPMColor* SK_RESTRICT cur_src0 = bitmap.getAddr32(0, src_y);
596 const SkPMColor* SK_RESTRICT cur_src1 = cur_src0;
597 if (src_y + 1 < bitmap.height())
598 cur_src1 = bitmap.getAddr32(0, src_y + 1);
599
600 SkPMColor* SK_RESTRICT cur_dst = result.getAddr32(0, dest_y);
601
602 for (int dest_x = 0; dest_x <= resultLastX; ++dest_x) {
[email protected]cab34d6a2009-09-24 01:14:52603 // This code is based on downsampleby2_proc32 in SkBitmap.cpp. It is very
604 // clever in that it does two channels at once: alpha and green ("ag")
605 // and red and blue ("rb"). Each channel gets averaged across 4 pixels
606 // to get the result.
[email protected]16d1608c2011-09-14 12:16:56607 int bump_x = (dest_x << 1) < srcLastX;
[email protected]cab34d6a2009-09-24 01:14:52608 SkPMColor tmp, ag, rb;
609
610 // Top left pixel of the 2x2 block.
[email protected]16d1608c2011-09-14 12:16:56611 tmp = cur_src0[0];
[email protected]cab34d6a2009-09-24 01:14:52612 ag = (tmp >> 8) & 0xFF00FF;
613 rb = tmp & 0xFF00FF;
[email protected]cab34d6a2009-09-24 01:14:52614
615 // Top right pixel of the 2x2 block.
[email protected]16d1608c2011-09-14 12:16:56616 tmp = cur_src0[bump_x];
[email protected]cab34d6a2009-09-24 01:14:52617 ag += (tmp >> 8) & 0xFF00FF;
618 rb += tmp & 0xFF00FF;
[email protected]cab34d6a2009-09-24 01:14:52619
620 // Bottom left pixel of the 2x2 block.
[email protected]16d1608c2011-09-14 12:16:56621 tmp = cur_src1[0];
[email protected]cab34d6a2009-09-24 01:14:52622 ag += (tmp >> 8) & 0xFF00FF;
623 rb += tmp & 0xFF00FF;
[email protected]cab34d6a2009-09-24 01:14:52624
625 // Bottom right pixel of the 2x2 block.
[email protected]16d1608c2011-09-14 12:16:56626 tmp = cur_src1[bump_x];
[email protected]cab34d6a2009-09-24 01:14:52627 ag += (tmp >> 8) & 0xFF00FF;
628 rb += tmp & 0xFF00FF;
629
630 // Put the channels back together, dividing each by 4 to get the average.
631 // |ag| has the alpha and green channels shifted right by 8 bits from
632 // there they should end up, so shifting left by 6 gives them in the
633 // correct position divided by 4.
[email protected]16d1608c2011-09-14 12:16:56634 *cur_dst++ = ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00);
635
636 cur_src0 += 2;
637 cur_src1 += 2;
[email protected]cab34d6a2009-09-24 01:14:52638 }
639 }
640
641 return result;
642}
643
[email protected]3eaf0ecd2010-07-15 15:30:55644// static
645SkBitmap SkBitmapOperations::UnPreMultiply(const SkBitmap& bitmap) {
646 if (bitmap.isNull())
647 return bitmap;
648 if (bitmap.isOpaque())
649 return bitmap;
650
reed03a4b5342016-06-28 03:41:32651 const SkImageInfo& opaque_info =
652 bitmap.info().makeAlphaType(kOpaque_SkAlphaType);
[email protected]3eaf0ecd2010-07-15 15:30:55653 SkBitmap opaque_bitmap;
fmalitab4e5afc2015-05-19 13:44:41654 opaque_bitmap.allocPixels(opaque_info);
[email protected]3eaf0ecd2010-07-15 15:30:55655
656 {
657 SkAutoLockPixels bitmap_lock(bitmap);
658 SkAutoLockPixels opaque_bitmap_lock(opaque_bitmap);
659 for (int y = 0; y < opaque_bitmap.height(); y++) {
660 for (int x = 0; x < opaque_bitmap.width(); x++) {
avic89eb8d42015-12-23 08:08:18661 uint32_t src_pixel = *bitmap.getAddr32(x, y);
662 uint32_t* dst_pixel = opaque_bitmap.getAddr32(x, y);
[email protected]3eaf0ecd2010-07-15 15:30:55663 SkColor unmultiplied = SkUnPreMultiply::PMColorToColor(src_pixel);
664 *dst_pixel = unmultiplied;
665 }
666 }
667 }
668
[email protected]3eaf0ecd2010-07-15 15:30:55669 return opaque_bitmap;
670}
[email protected]8d1b864d12010-10-10 00:04:34671
672// static
[email protected]673d1ba2012-06-18 05:56:00673SkBitmap SkBitmapOperations::CreateTransposedBitmap(const SkBitmap& image) {
[email protected]c80330da2014-07-01 19:26:27674 DCHECK(image.colorType() == kN32_SkColorType);
[email protected]8d1b864d12010-10-10 00:04:34675
[email protected]8d1b864d12010-10-10 00:04:34676 SkBitmap transposed;
[email protected]f62e57b2014-03-12 20:52:02677 transposed.allocN32Pixels(image.height(), image.width());
[email protected]579dacb2011-11-08 20:06:36678
679 SkAutoLockPixels lock_image(image);
680 SkAutoLockPixels lock_transposed(transposed);
[email protected]8d1b864d12010-10-10 00:04:34681
682 for (int y = 0; y < image.height(); ++y) {
avic89eb8d42015-12-23 08:08:18683 uint32_t* image_row = image.getAddr32(0, y);
[email protected]8d1b864d12010-10-10 00:04:34684 for (int x = 0; x < image.width(); ++x) {
avic89eb8d42015-12-23 08:08:18685 uint32_t* dst = transposed.getAddr32(y, x);
[email protected]8d1b864d12010-10-10 00:04:34686 *dst = image_row[x];
687 }
688 }
689
690 return transposed;
691}
692
[email protected]6cd13aa2012-03-23 02:27:16693// static
[email protected]6cd13aa2012-03-23 02:27:16694SkBitmap SkBitmapOperations::CreateColorMask(const SkBitmap& bitmap,
695 SkColor c) {
[email protected]c80330da2014-07-01 19:26:27696 DCHECK(bitmap.colorType() == kN32_SkColorType);
[email protected]6cd13aa2012-03-23 02:27:16697
698 SkBitmap color_mask;
[email protected]f62e57b2014-03-12 20:52:02699 color_mask.allocN32Pixels(bitmap.width(), bitmap.height());
[email protected]6cd13aa2012-03-23 02:27:16700 color_mask.eraseARGB(0, 0, 0, 0);
701
702 SkCanvas canvas(color_mask);
703
[email protected]6cd13aa2012-03-23 02:27:16704 SkPaint paint;
tomhudson1f8d4132016-03-30 16:03:02705 paint.setColorFilter(
706 SkColorFilter::MakeModeFilter(c, SkXfermode::kSrcIn_Mode));
[email protected]6cd13aa2012-03-23 02:27:16707 canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0), &paint);
708 return color_mask;
709}
710
711// static
[email protected]58e068f2012-05-26 00:29:16712SkBitmap SkBitmapOperations::CreateDropShadow(
713 const SkBitmap& bitmap,
714 const gfx::ShadowValues& shadows) {
[email protected]c80330da2014-07-01 19:26:27715 DCHECK(bitmap.colorType() == kN32_SkColorType);
[email protected]6cd13aa2012-03-23 02:27:16716
[email protected]58e068f2012-05-26 00:29:16717 // Shadow margin insets are negative values because they grow outside.
718 // Negate them here as grow direction is not important and only pixel value
719 // is of interest here.
720 gfx::Insets shadow_margin = -gfx::ShadowValue::GetMargin(shadows);
[email protected]6cd13aa2012-03-23 02:27:16721
722 SkBitmap image_with_shadow;
[email protected]f62e57b2014-03-12 20:52:02723 image_with_shadow.allocN32Pixels(bitmap.width() + shadow_margin.width(),
724 bitmap.height() + shadow_margin.height());
[email protected]6cd13aa2012-03-23 02:27:16725 image_with_shadow.eraseARGB(0, 0, 0, 0);
726
727 SkCanvas canvas(image_with_shadow);
[email protected]58e068f2012-05-26 00:29:16728 canvas.translate(SkIntToScalar(shadow_margin.left()),
729 SkIntToScalar(shadow_margin.top()));
[email protected]6cd13aa2012-03-23 02:27:16730
731 SkPaint paint;
[email protected]58e068f2012-05-26 00:29:16732 for (size_t i = 0; i < shadows.size(); ++i) {
733 const gfx::ShadowValue& shadow = shadows[i];
734 SkBitmap shadow_image = SkBitmapOperations::CreateColorMask(bitmap,
735 shadow.color());
[email protected]6cd13aa2012-03-23 02:27:16736
calamity31b62772015-05-21 07:56:26737 // The blur is halved to produce a shadow that correctly fits within the
738 // |shadow_margin|.
739 SkScalar sigma = SkDoubleToScalar(shadow.blur() / 2);
tomhudson399950b2016-05-11 11:00:13740 paint.setImageFilter(SkBlurImageFilter::Make(sigma, sigma, nullptr));
[email protected]6cd13aa2012-03-23 02:27:16741
742 canvas.saveLayer(0, &paint);
[email protected]58e068f2012-05-26 00:29:16743 canvas.drawBitmap(shadow_image,
744 SkIntToScalar(shadow.x()),
745 SkIntToScalar(shadow.y()));
[email protected]6cd13aa2012-03-23 02:27:16746 canvas.restore();
747 }
748
749 canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0));
750 return image_with_shadow;
751}
[email protected]bb2b60ac2012-12-18 02:23:10752
753// static
754SkBitmap SkBitmapOperations::Rotate(const SkBitmap& source,
755 RotationAmount rotation) {
kylecharfd9f15be2015-12-21 18:43:01756 // SkCanvas::drawBitmap() fails silently with unpremultiplied SkBitmap.
757 DCHECK_NE(source.info().alphaType(), kUnpremul_SkAlphaType);
758
[email protected]bb2b60ac2012-12-18 02:23:10759 SkBitmap result;
760 SkScalar angle = SkFloatToScalar(0.0f);
761
762 switch (rotation) {
763 case ROTATION_90_CW:
[email protected]5b7ab9c2013-03-13 05:41:32764 angle = SkFloatToScalar(90.0f);
[email protected]c80330da2014-07-01 19:26:27765 result.allocN32Pixels(source.height(), source.width());
[email protected]bb2b60ac2012-12-18 02:23:10766 break;
767 case ROTATION_180_CW:
[email protected]5b7ab9c2013-03-13 05:41:32768 angle = SkFloatToScalar(180.0f);
[email protected]c80330da2014-07-01 19:26:27769 result.allocN32Pixels(source.width(), source.height());
[email protected]bb2b60ac2012-12-18 02:23:10770 break;
771 case ROTATION_270_CW:
[email protected]5b7ab9c2013-03-13 05:41:32772 angle = SkFloatToScalar(270.0f);
[email protected]c80330da2014-07-01 19:26:27773 result.allocN32Pixels(source.height(), source.width());
[email protected]bb2b60ac2012-12-18 02:23:10774 break;
775 }
[email protected]c80330da2014-07-01 19:26:27776
[email protected]bb2b60ac2012-12-18 02:23:10777 SkCanvas canvas(result);
[email protected]19c0fa92013-01-08 17:46:01778 canvas.clear(SkColorSetARGB(0, 0, 0, 0));
[email protected]bb2b60ac2012-12-18 02:23:10779
780 canvas.translate(SkFloatToScalar(result.width() * 0.5f),
781 SkFloatToScalar(result.height() * 0.5f));
782 canvas.rotate(angle);
783 canvas.translate(-SkFloatToScalar(source.width() * 0.5f),
784 -SkFloatToScalar(source.height() * 0.5f));
785 canvas.drawBitmap(source, 0, 0);
786 canvas.flush();
787
788 return result;
789}