[email protected] | 125b6200 | 2012-03-19 14:30:40 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 5 | #include "ui/gfx/skbitmap_operations.h" |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 6 | |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
[email protected] | 6e25ce8 | 2010-04-26 17:12:40 | [diff] [blame] | 9 | #include <string.h> |
tfarina | 655f81d | 2014-12-23 02:38:50 | [diff] [blame] | 10 | #include <algorithm> |
[email protected] | 0fb499a | 2009-11-10 21:03:33 | [diff] [blame] | 11 | |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 13 | #include "third_party/skia/include/core/SkBitmap.h" |
[email protected] | 0fb499a | 2009-11-10 21:03:33 | [diff] [blame] | 14 | #include "third_party/skia/include/core/SkCanvas.h" |
[email protected] | 6cd13aa | 2012-03-23 02:27:16 | [diff] [blame] | 15 | #include "third_party/skia/include/core/SkColorFilter.h" |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 16 | #include "third_party/skia/include/core/SkColorPriv.h" |
| 17 | #include "third_party/skia/include/core/SkUnPreMultiply.h" |
[email protected] | 6cd13aa | 2012-03-23 02:27:16 | [diff] [blame] | 18 | #include "third_party/skia/include/effects/SkBlurImageFilter.h" |
tfarina | 655f81d | 2014-12-23 02:38:50 | [diff] [blame] | 19 | #include "ui/gfx/geometry/insets.h" |
| 20 | #include "ui/gfx/geometry/point.h" |
tfarina | ebe974f0 | 2015-01-03 04:25:32 | [diff] [blame] | 21 | #include "ui/gfx/geometry/size.h" |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 22 | |
| 23 | // static |
[email protected] | 0fb499a | 2009-11-10 21:03:33 | [diff] [blame] | 24 | SkBitmap SkBitmapOperations::CreateInvertedBitmap(const SkBitmap& image) { |
[email protected] | c80330da | 2014-07-01 19:26:27 | [diff] [blame] | 25 | DCHECK(image.colorType() == kN32_SkColorType); |
[email protected] | 0fb499a | 2009-11-10 21:03:33 | [diff] [blame] | 26 | |
| 27 | SkAutoLockPixels lock_image(image); |
| 28 | |
| 29 | SkBitmap inverted; |
[email protected] | f62e57b | 2014-03-12 20:52:02 | [diff] [blame] | 30 | inverted.allocN32Pixels(image.width(), image.height()); |
[email protected] | 0fb499a | 2009-11-10 21:03:33 | [diff] [blame] | 31 | |
| 32 | for (int y = 0; y < image.height(); ++y) { |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 33 | uint32_t* image_row = image.getAddr32(0, y); |
| 34 | uint32_t* dst_row = inverted.getAddr32(0, y); |
[email protected] | 0fb499a | 2009-11-10 21:03:33 | [diff] [blame] | 35 | |
| 36 | for (int x = 0; x < image.width(); ++x) { |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 37 | uint32_t image_pixel = image_row[x]; |
[email protected] | 0fb499a | 2009-11-10 21:03:33 | [diff] [blame] | 38 | dst_row[x] = (image_pixel & 0xFF000000) | |
| 39 | (0x00FFFFFF - (image_pixel & 0x00FFFFFF)); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | return inverted; |
| 44 | } |
| 45 | |
| 46 | // static |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 47 | SkBitmap 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] | c80330da | 2014-07-01 19:26:27 | [diff] [blame] | 54 | DCHECK(first.colorType() == kN32_SkColorType); |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 55 | |
| 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] | f62e57b | 2014-03-12 20:52:02 | [diff] [blame] | 68 | blended.allocN32Pixels(first.width(), first.height()); |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 69 | |
| 70 | double first_alpha = 1 - alpha; |
| 71 | |
| 72 | for (int y = 0; y < first.height(); ++y) { |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 73 | 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] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 76 | |
| 77 | for (int x = 0; x < first.width(); ++x) { |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 78 | uint32_t first_pixel = first_row[x]; |
| 79 | uint32_t second_pixel = second_row[x]; |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 80 | |
| 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 |
| 98 | SkBitmap 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] | c80330da | 2014-07-01 19:26:27 | [diff] [blame] | 103 | DCHECK(rgb.colorType() == kN32_SkColorType); |
| 104 | DCHECK(alpha.colorType() == kN32_SkColorType); |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 105 | |
| 106 | SkBitmap masked; |
[email protected] | f62e57b | 2014-03-12 20:52:02 | [diff] [blame] | 107 | masked.allocN32Pixels(rgb.width(), rgb.height()); |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 108 | |
| 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) { |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 114 | 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] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 117 | |
| 118 | for (int x = 0; x < masked.width(); ++x) { |
moshayedi | 6e936364 | 2015-10-29 20:27:40 | [diff] [blame] | 119 | unsigned alpha = SkGetPackedA32(alpha_row[x]); |
| 120 | unsigned scale = SkAlpha255To256(alpha); |
| 121 | dst_row[x] = SkAlphaMulQ(rgb_row[x], scale); |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | return masked; |
| 126 | } |
| 127 | |
| 128 | // static |
| 129 | SkBitmap SkBitmapOperations::CreateButtonBackground(SkColor color, |
| 130 | const SkBitmap& image, |
| 131 | const SkBitmap& mask) { |
[email protected] | c80330da | 2014-07-01 19:26:27 | [diff] [blame] | 132 | DCHECK(image.colorType() == kN32_SkColorType); |
| 133 | DCHECK(mask.colorType() == kN32_SkColorType); |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 134 | |
| 135 | SkBitmap background; |
[email protected] | f62e57b | 2014-03-12 20:52:02 | [diff] [blame] | 136 | background.allocN32Pixels(mask.width(), mask.height()); |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 137 | |
| 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) { |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 148 | 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] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 151 | |
| 152 | for (int x = 0; x < mask.width(); ++x) { |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 153 | uint32_t image_pixel = image_row[x % image.width()]; |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 154 | |
| 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] | 6e25ce8 | 2010-04-26 17:12:40 | [diff] [blame] | 176 | namespace { |
| 177 | namespace 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] | 80b3f7c | 2011-06-22 02:29:21 | [diff] [blame] | 218 | typedef void (*LineProcessor)(const color_utils::HSL&, |
[email protected] | 6e25ce8 | 2010-04-26 17:12:40 | [diff] [blame] | 219 | const SkPMColor*, |
| 220 | SkPMColor*, |
| 221 | int width); |
| 222 | |
| 223 | enum OperationOnH { kOpHNone = 0, kOpHShift, kNumHOps }; |
| 224 | enum OperationOnS { kOpSNone = 0, kOpSDec, kOpSInc, kNumSOps }; |
| 225 | enum 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> |
| 230 | const double epsilon = 0.0005; |
| 231 | |
| 232 | // Line processor: default/universal (i.e., old-school). |
[email protected] | 80b3f7c | 2011-06-22 02:29:21 | [diff] [blame] | 233 | void LineProcDefault(const color_utils::HSL& hsl_shift, |
| 234 | const SkPMColor* in, |
| 235 | SkPMColor* out, |
| 236 | int width) { |
[email protected] | 6e25ce8 | 2010-04-26 17:12:40 | [diff] [blame] | 237 | 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] | 80b3f7c | 2011-06-22 02:29:21 | [diff] [blame] | 244 | void LineProcCopy(const color_utils::HSL& hsl_shift, |
| 245 | const SkPMColor* in, |
| 246 | SkPMColor* out, |
| 247 | int width) { |
[email protected] | 6e25ce8 | 2010-04-26 17:12:40 | [diff] [blame] | 248 | 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] | 80b3f7c | 2011-06-22 02:29:21 | [diff] [blame] | 255 | void LineProcHnopSnopLdec(const color_utils::HSL& hsl_shift, |
| 256 | const SkPMColor* in, |
| 257 | SkPMColor* out, |
| 258 | int width) { |
[email protected] | 6e25ce8 | 2010-04-26 17:12:40 | [diff] [blame] | 259 | 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] | 80b3f7c | 2011-06-22 02:29:21 | [diff] [blame] | 279 | void LineProcHnopSnopLinc(const color_utils::HSL& hsl_shift, |
| 280 | const SkPMColor* in, |
| 281 | SkPMColor* out, |
| 282 | int width) { |
[email protected] | 6e25ce8 | 2010-04-26 17:12:40 | [diff] [blame] | 283 | 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] | 80b3f7c | 2011-06-22 02:29:21 | [diff] [blame] | 326 | void LineProcHnopSdecLnop(const color_utils::HSL& hsl_shift, |
| 327 | const SkPMColor* in, |
| 328 | SkPMColor* out, |
| 329 | int width) { |
[email protected] | 6e25ce8 | 2010-04-26 17:12:40 | [diff] [blame] | 330 | 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] | 80b3f7c | 2011-06-22 02:29:21 | [diff] [blame] | 363 | void LineProcHnopSdecLdec(const color_utils::HSL& hsl_shift, |
| 364 | const SkPMColor* in, |
| 365 | SkPMColor* out, |
| 366 | int width) { |
[email protected] | 6e25ce8 | 2010-04-26 17:12:40 | [diff] [blame] | 367 | 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] | 80b3f7c | 2011-06-22 02:29:21 | [diff] [blame] | 402 | void LineProcHnopSdecLinc(const color_utils::HSL& hsl_shift, |
| 403 | const SkPMColor* in, |
| 404 | SkPMColor* out, |
| 405 | int width) { |
[email protected] | 6e25ce8 | 2010-04-26 17:12:40 | [diff] [blame] | 406 | 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 | |
| 444 | const 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] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 483 | |
| 484 | // static |
| 485 | SkBitmap SkBitmapOperations::CreateHSLShiftedBitmap( |
| 486 | const SkBitmap& bitmap, |
[email protected] | 37cbcfe | 2011-03-11 20:47:22 | [diff] [blame] | 487 | const color_utils::HSL& hsl_shift) { |
[email protected] | 6e25ce8 | 2010-04-26 17:12:40 | [diff] [blame] | 488 | // 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] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 511 | DCHECK(bitmap.empty() == false); |
[email protected] | c80330da | 2014-07-01 19:26:27 | [diff] [blame] | 512 | DCHECK(bitmap.colorType() == kN32_SkColorType); |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 513 | |
| 514 | SkBitmap shifted; |
[email protected] | f62e57b | 2014-03-12 20:52:02 | [diff] [blame] | 515 | shifted.allocN32Pixels(bitmap.width(), bitmap.height()); |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 516 | |
| 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] | 6e25ce8 | 2010-04-26 17:12:40 | [diff] [blame] | 525 | (*line_proc)(hsl_shift, pixels, tinted_pixels, bitmap.width()); |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | return shifted; |
| 529 | } |
| 530 | |
| 531 | // static |
| 532 | SkBitmap SkBitmapOperations::CreateTiledBitmap(const SkBitmap& source, |
| 533 | int src_x, int src_y, |
| 534 | int dst_w, int dst_h) { |
[email protected] | c80330da | 2014-07-01 19:26:27 | [diff] [blame] | 535 | DCHECK(source.colorType() == kN32_SkColorType); |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 536 | |
| 537 | SkBitmap cropped; |
[email protected] | f62e57b | 2014-03-12 20:52:02 | [diff] [blame] | 538 | cropped.allocN32Pixels(dst_w, dst_h); |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 539 | |
| 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 | |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 549 | uint32_t* source_row = source.getAddr32(0, y_pix); |
| 550 | uint32_t* dst_row = cropped.getAddr32(0, y); |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 551 | |
| 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 |
| 565 | SkBitmap 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 |
| 580 | SkBitmap 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] | f62e57b | 2014-03-12 20:52:02 | [diff] [blame] | 586 | result.allocN32Pixels((bitmap.width() + 1) / 2, (bitmap.height() + 1) / 2); |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 587 | |
| 588 | SkAutoLockPixels lock(bitmap); |
[email protected] | 16d1608c | 2011-09-14 12:16:56 | [diff] [blame] | 589 | |
| 590 | const int resultLastX = result.width() - 1; |
| 591 | const int srcLastX = bitmap.width() - 1; |
| 592 | |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 593 | for (int dest_y = 0; dest_y < result.height(); ++dest_y) { |
[email protected] | 16d1608c | 2011-09-14 12:16:56 | [diff] [blame] | 594 | 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] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 603 | // 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] | 16d1608c | 2011-09-14 12:16:56 | [diff] [blame] | 607 | int bump_x = (dest_x << 1) < srcLastX; |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 608 | SkPMColor tmp, ag, rb; |
| 609 | |
| 610 | // Top left pixel of the 2x2 block. |
[email protected] | 16d1608c | 2011-09-14 12:16:56 | [diff] [blame] | 611 | tmp = cur_src0[0]; |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 612 | ag = (tmp >> 8) & 0xFF00FF; |
| 613 | rb = tmp & 0xFF00FF; |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 614 | |
| 615 | // Top right pixel of the 2x2 block. |
[email protected] | 16d1608c | 2011-09-14 12:16:56 | [diff] [blame] | 616 | tmp = cur_src0[bump_x]; |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 617 | ag += (tmp >> 8) & 0xFF00FF; |
| 618 | rb += tmp & 0xFF00FF; |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 619 | |
| 620 | // Bottom left pixel of the 2x2 block. |
[email protected] | 16d1608c | 2011-09-14 12:16:56 | [diff] [blame] | 621 | tmp = cur_src1[0]; |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 622 | ag += (tmp >> 8) & 0xFF00FF; |
| 623 | rb += tmp & 0xFF00FF; |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 624 | |
| 625 | // Bottom right pixel of the 2x2 block. |
[email protected] | 16d1608c | 2011-09-14 12:16:56 | [diff] [blame] | 626 | tmp = cur_src1[bump_x]; |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 627 | 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] | 16d1608c | 2011-09-14 12:16:56 | [diff] [blame] | 634 | *cur_dst++ = ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00); |
| 635 | |
| 636 | cur_src0 += 2; |
| 637 | cur_src1 += 2; |
[email protected] | cab34d6a | 2009-09-24 01:14:52 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | |
| 641 | return result; |
| 642 | } |
| 643 | |
[email protected] | 3eaf0ecd | 2010-07-15 15:30:55 | [diff] [blame] | 644 | // static |
| 645 | SkBitmap SkBitmapOperations::UnPreMultiply(const SkBitmap& bitmap) { |
| 646 | if (bitmap.isNull()) |
| 647 | return bitmap; |
| 648 | if (bitmap.isOpaque()) |
| 649 | return bitmap; |
| 650 | |
reed | 03a4b534 | 2016-06-28 03:41:32 | [diff] [blame] | 651 | const SkImageInfo& opaque_info = |
| 652 | bitmap.info().makeAlphaType(kOpaque_SkAlphaType); |
[email protected] | 3eaf0ecd | 2010-07-15 15:30:55 | [diff] [blame] | 653 | SkBitmap opaque_bitmap; |
fmalita | b4e5afc | 2015-05-19 13:44:41 | [diff] [blame] | 654 | opaque_bitmap.allocPixels(opaque_info); |
[email protected] | 3eaf0ecd | 2010-07-15 15:30:55 | [diff] [blame] | 655 | |
| 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++) { |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 661 | uint32_t src_pixel = *bitmap.getAddr32(x, y); |
| 662 | uint32_t* dst_pixel = opaque_bitmap.getAddr32(x, y); |
[email protected] | 3eaf0ecd | 2010-07-15 15:30:55 | [diff] [blame] | 663 | SkColor unmultiplied = SkUnPreMultiply::PMColorToColor(src_pixel); |
| 664 | *dst_pixel = unmultiplied; |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | |
[email protected] | 3eaf0ecd | 2010-07-15 15:30:55 | [diff] [blame] | 669 | return opaque_bitmap; |
| 670 | } |
[email protected] | 8d1b864d1 | 2010-10-10 00:04:34 | [diff] [blame] | 671 | |
| 672 | // static |
[email protected] | 673d1ba | 2012-06-18 05:56:00 | [diff] [blame] | 673 | SkBitmap SkBitmapOperations::CreateTransposedBitmap(const SkBitmap& image) { |
[email protected] | c80330da | 2014-07-01 19:26:27 | [diff] [blame] | 674 | DCHECK(image.colorType() == kN32_SkColorType); |
[email protected] | 8d1b864d1 | 2010-10-10 00:04:34 | [diff] [blame] | 675 | |
[email protected] | 8d1b864d1 | 2010-10-10 00:04:34 | [diff] [blame] | 676 | SkBitmap transposed; |
[email protected] | f62e57b | 2014-03-12 20:52:02 | [diff] [blame] | 677 | transposed.allocN32Pixels(image.height(), image.width()); |
[email protected] | 579dacb | 2011-11-08 20:06:36 | [diff] [blame] | 678 | |
| 679 | SkAutoLockPixels lock_image(image); |
| 680 | SkAutoLockPixels lock_transposed(transposed); |
[email protected] | 8d1b864d1 | 2010-10-10 00:04:34 | [diff] [blame] | 681 | |
| 682 | for (int y = 0; y < image.height(); ++y) { |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 683 | uint32_t* image_row = image.getAddr32(0, y); |
[email protected] | 8d1b864d1 | 2010-10-10 00:04:34 | [diff] [blame] | 684 | for (int x = 0; x < image.width(); ++x) { |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 685 | uint32_t* dst = transposed.getAddr32(y, x); |
[email protected] | 8d1b864d1 | 2010-10-10 00:04:34 | [diff] [blame] | 686 | *dst = image_row[x]; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | return transposed; |
| 691 | } |
| 692 | |
[email protected] | 6cd13aa | 2012-03-23 02:27:16 | [diff] [blame] | 693 | // static |
[email protected] | 6cd13aa | 2012-03-23 02:27:16 | [diff] [blame] | 694 | SkBitmap SkBitmapOperations::CreateColorMask(const SkBitmap& bitmap, |
| 695 | SkColor c) { |
[email protected] | c80330da | 2014-07-01 19:26:27 | [diff] [blame] | 696 | DCHECK(bitmap.colorType() == kN32_SkColorType); |
[email protected] | 6cd13aa | 2012-03-23 02:27:16 | [diff] [blame] | 697 | |
| 698 | SkBitmap color_mask; |
[email protected] | f62e57b | 2014-03-12 20:52:02 | [diff] [blame] | 699 | color_mask.allocN32Pixels(bitmap.width(), bitmap.height()); |
[email protected] | 6cd13aa | 2012-03-23 02:27:16 | [diff] [blame] | 700 | color_mask.eraseARGB(0, 0, 0, 0); |
| 701 | |
| 702 | SkCanvas canvas(color_mask); |
| 703 | |
[email protected] | 6cd13aa | 2012-03-23 02:27:16 | [diff] [blame] | 704 | SkPaint paint; |
tomhudson | 1f8d413 | 2016-03-30 16:03:02 | [diff] [blame] | 705 | paint.setColorFilter( |
| 706 | SkColorFilter::MakeModeFilter(c, SkXfermode::kSrcIn_Mode)); |
[email protected] | 6cd13aa | 2012-03-23 02:27:16 | [diff] [blame] | 707 | canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0), &paint); |
| 708 | return color_mask; |
| 709 | } |
| 710 | |
| 711 | // static |
[email protected] | 58e068f | 2012-05-26 00:29:16 | [diff] [blame] | 712 | SkBitmap SkBitmapOperations::CreateDropShadow( |
| 713 | const SkBitmap& bitmap, |
| 714 | const gfx::ShadowValues& shadows) { |
[email protected] | c80330da | 2014-07-01 19:26:27 | [diff] [blame] | 715 | DCHECK(bitmap.colorType() == kN32_SkColorType); |
[email protected] | 6cd13aa | 2012-03-23 02:27:16 | [diff] [blame] | 716 | |
[email protected] | 58e068f | 2012-05-26 00:29:16 | [diff] [blame] | 717 | // 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] | 6cd13aa | 2012-03-23 02:27:16 | [diff] [blame] | 721 | |
| 722 | SkBitmap image_with_shadow; |
[email protected] | f62e57b | 2014-03-12 20:52:02 | [diff] [blame] | 723 | image_with_shadow.allocN32Pixels(bitmap.width() + shadow_margin.width(), |
| 724 | bitmap.height() + shadow_margin.height()); |
[email protected] | 6cd13aa | 2012-03-23 02:27:16 | [diff] [blame] | 725 | image_with_shadow.eraseARGB(0, 0, 0, 0); |
| 726 | |
| 727 | SkCanvas canvas(image_with_shadow); |
[email protected] | 58e068f | 2012-05-26 00:29:16 | [diff] [blame] | 728 | canvas.translate(SkIntToScalar(shadow_margin.left()), |
| 729 | SkIntToScalar(shadow_margin.top())); |
[email protected] | 6cd13aa | 2012-03-23 02:27:16 | [diff] [blame] | 730 | |
| 731 | SkPaint paint; |
[email protected] | 58e068f | 2012-05-26 00:29:16 | [diff] [blame] | 732 | 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] | 6cd13aa | 2012-03-23 02:27:16 | [diff] [blame] | 736 | |
calamity | 31b6277 | 2015-05-21 07:56:26 | [diff] [blame] | 737 | // The blur is halved to produce a shadow that correctly fits within the |
| 738 | // |shadow_margin|. |
| 739 | SkScalar sigma = SkDoubleToScalar(shadow.blur() / 2); |
tomhudson | 399950b | 2016-05-11 11:00:13 | [diff] [blame] | 740 | paint.setImageFilter(SkBlurImageFilter::Make(sigma, sigma, nullptr)); |
[email protected] | 6cd13aa | 2012-03-23 02:27:16 | [diff] [blame] | 741 | |
| 742 | canvas.saveLayer(0, &paint); |
[email protected] | 58e068f | 2012-05-26 00:29:16 | [diff] [blame] | 743 | canvas.drawBitmap(shadow_image, |
| 744 | SkIntToScalar(shadow.x()), |
| 745 | SkIntToScalar(shadow.y())); |
[email protected] | 6cd13aa | 2012-03-23 02:27:16 | [diff] [blame] | 746 | canvas.restore(); |
| 747 | } |
| 748 | |
| 749 | canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0)); |
| 750 | return image_with_shadow; |
| 751 | } |
[email protected] | bb2b60ac | 2012-12-18 02:23:10 | [diff] [blame] | 752 | |
| 753 | // static |
| 754 | SkBitmap SkBitmapOperations::Rotate(const SkBitmap& source, |
| 755 | RotationAmount rotation) { |
kylechar | fd9f15be | 2015-12-21 18:43:01 | [diff] [blame] | 756 | // SkCanvas::drawBitmap() fails silently with unpremultiplied SkBitmap. |
| 757 | DCHECK_NE(source.info().alphaType(), kUnpremul_SkAlphaType); |
| 758 | |
[email protected] | bb2b60ac | 2012-12-18 02:23:10 | [diff] [blame] | 759 | SkBitmap result; |
| 760 | SkScalar angle = SkFloatToScalar(0.0f); |
| 761 | |
| 762 | switch (rotation) { |
| 763 | case ROTATION_90_CW: |
[email protected] | 5b7ab9c | 2013-03-13 05:41:32 | [diff] [blame] | 764 | angle = SkFloatToScalar(90.0f); |
[email protected] | c80330da | 2014-07-01 19:26:27 | [diff] [blame] | 765 | result.allocN32Pixels(source.height(), source.width()); |
[email protected] | bb2b60ac | 2012-12-18 02:23:10 | [diff] [blame] | 766 | break; |
| 767 | case ROTATION_180_CW: |
[email protected] | 5b7ab9c | 2013-03-13 05:41:32 | [diff] [blame] | 768 | angle = SkFloatToScalar(180.0f); |
[email protected] | c80330da | 2014-07-01 19:26:27 | [diff] [blame] | 769 | result.allocN32Pixels(source.width(), source.height()); |
[email protected] | bb2b60ac | 2012-12-18 02:23:10 | [diff] [blame] | 770 | break; |
| 771 | case ROTATION_270_CW: |
[email protected] | 5b7ab9c | 2013-03-13 05:41:32 | [diff] [blame] | 772 | angle = SkFloatToScalar(270.0f); |
[email protected] | c80330da | 2014-07-01 19:26:27 | [diff] [blame] | 773 | result.allocN32Pixels(source.height(), source.width()); |
[email protected] | bb2b60ac | 2012-12-18 02:23:10 | [diff] [blame] | 774 | break; |
| 775 | } |
[email protected] | c80330da | 2014-07-01 19:26:27 | [diff] [blame] | 776 | |
[email protected] | bb2b60ac | 2012-12-18 02:23:10 | [diff] [blame] | 777 | SkCanvas canvas(result); |
[email protected] | 19c0fa9 | 2013-01-08 17:46:01 | [diff] [blame] | 778 | canvas.clear(SkColorSetARGB(0, 0, 0, 0)); |
[email protected] | bb2b60ac | 2012-12-18 02:23:10 | [diff] [blame] | 779 | |
| 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 | } |