blob: 59d70205c960b23acf4aaee0b4621c50e5a7f4e3 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
reed@android.com8a1c16f2008-12-17 15:59:43 +00008
9#include "SkBlurMask.h"
Mike Reede602f392018-02-06 10:17:08 -050010#include "SkColorPriv.h"
Mike Reed771ae962017-07-13 17:16:34 +000011#include "SkMaskBlurFilter.h"
tomhudson@google.com889bd8b2011-09-27 17:38:17 +000012#include "SkMath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkTemplates.h"
tomhudson@google.com01224d52011-11-28 18:22:01 +000014#include "SkEndian.h"
15
robertphillips@google.com7ce661d2013-08-27 16:14:03 +000016
reed@google.comdaaafa62014-04-29 15:20:16 +000017// This constant approximates the scaling done in the software path's
18// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
19// IMHO, it actually should be 1: we blur "less" than we should do
20// according to the CSS and canvas specs, simply because Safari does the same.
21// Firefox used to do the same too, until 4.0 where they fixed it. So at some
22// point we should probably get rid of these scaling constants and rebaseline
23// all the blur tests.
24static const SkScalar kBLUR_SIGMA_SCALE = 0.57735f;
robertphillips@google.com7ce661d2013-08-27 16:14:03 +000025
reed@google.comdaaafa62014-04-29 15:20:16 +000026SkScalar SkBlurMask::ConvertRadiusToSigma(SkScalar radius) {
27 return radius > 0 ? kBLUR_SIGMA_SCALE * radius + 0.5f : 0.0f;
28}
29
30SkScalar SkBlurMask::ConvertSigmaToRadius(SkScalar sigma) {
31 return sigma > 0.5f ? (sigma - 0.5f) / kBLUR_SIGMA_SCALE : 0.0f;
robertphillips@google.com7ce661d2013-08-27 16:14:03 +000032}
humper@google.com7c7292c2013-01-04 20:29:03 +000033
reed@android.com8a1c16f2008-12-17 15:59:43 +000034
reed@android.com0e3c6642009-09-18 13:41:56 +000035static void merge_src_with_blur(uint8_t dst[], int dstRB,
36 const uint8_t src[], int srcRB,
37 const uint8_t blur[], int blurRB,
38 int sw, int sh) {
39 dstRB -= sw;
40 srcRB -= sw;
41 blurRB -= sw;
42 while (--sh >= 0) {
43 for (int x = sw - 1; x >= 0; --x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 *dst = SkToU8(SkAlphaMul(*blur, SkAlpha255To256(*src)));
45 dst += 1;
46 src += 1;
47 blur += 1;
48 }
reed@android.com0e3c6642009-09-18 13:41:56 +000049 dst += dstRB;
50 src += srcRB;
51 blur += blurRB;
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 }
53}
54
55static void clamp_with_orig(uint8_t dst[], int dstRowBytes,
reed@android.com0e3c6642009-09-18 13:41:56 +000056 const uint8_t src[], int srcRowBytes,
57 int sw, int sh,
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000058 SkBlurStyle style) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 int x;
reed@android.com0e3c6642009-09-18 13:41:56 +000060 while (--sh >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 switch (style) {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000062 case kSolid_SkBlurStyle:
reed@android.com0e3c6642009-09-18 13:41:56 +000063 for (x = sw - 1; x >= 0; --x) {
64 int s = *src;
65 int d = *dst;
66 *dst = SkToU8(s + d - SkMulDiv255Round(s, d));
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 dst += 1;
68 src += 1;
69 }
70 break;
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000071 case kOuter_SkBlurStyle:
reed@android.com0e3c6642009-09-18 13:41:56 +000072 for (x = sw - 1; x >= 0; --x) {
73 if (*src) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 *dst = SkToU8(SkAlphaMul(*dst, SkAlpha255To256(255 - *src)));
reed@android.com0e3c6642009-09-18 13:41:56 +000075 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 dst += 1;
77 src += 1;
78 }
79 break;
80 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +000081 SkDEBUGFAIL("Unexpected blur style here");
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 break;
83 }
84 dst += dstRowBytes - sw;
reed@android.com0e3c6642009-09-18 13:41:56 +000085 src += srcRowBytes - sw;
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 }
87}
88
reed@google.com03016a32011-08-12 14:59:59 +000089///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000090
bsalomon@google.com33cdbde2013-01-11 20:54:44 +000091// we use a local function to wrap the class static method to work around
reed@android.com8a1c16f2008-12-17 15:59:43 +000092// a bug in gcc98
93void SkMask_FreeImage(uint8_t* image);
reed@google.com03016a32011-08-12 14:59:59 +000094void SkMask_FreeImage(uint8_t* image) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 SkMask::FreeImage(image);
96}
97
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +000098bool SkBlurMask::BoxBlur(SkMask* dst, const SkMask& src,
99 SkScalar sigma, SkBlurStyle style, SkBlurQuality quality,
100 SkIPoint* margin, bool force_quality) {
101
reed@google.com03016a32011-08-12 14:59:59 +0000102 if (src.fFormat != SkMask::kA8_Format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 return false;
reed@google.com03016a32011-08-12 14:59:59 +0000104 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105
Mike Reed771ae962017-07-13 17:16:34 +0000106 SkIPoint border;
107
Mike Reed771ae962017-07-13 17:16:34 +0000108 SkMaskBlurFilter blurFilter{sigma, sigma};
Mike Reeda7ba6e72017-07-21 12:23:12 -0400109 if (blurFilter.hasNoBlur()) {
110 return false;
111 }
Mike Reed771ae962017-07-13 17:16:34 +0000112 border = blurFilter.blur(src, dst);
Herb Derby82e31452017-08-22 16:59:28 -0400113 // If src.fImage is null, then this call is only to calculate the border.
114 if (src.fImage != nullptr && dst->fImage == nullptr) {
115 return false;
116 }
Mike Reed771ae962017-07-13 17:16:34 +0000117
118 if (src.fImage != nullptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 // if need be, alloc the "real" dst (same size as src) and copy/merge
120 // the blur into it (applying the src)
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000121 if (style == kInner_SkBlurStyle) {
reed@android.com0e3c6642009-09-18 13:41:56 +0000122 // now we allocate the "real" dst, mirror the size of src
reed@android.com543ed932009-04-24 12:43:40 +0000123 size_t srcSize = src.computeImageSize();
124 if (0 == srcSize) {
125 return false; // too big to allocate, abort
126 }
Mike Reed771ae962017-07-13 17:16:34 +0000127 auto blur = dst->fImage;
reed@android.com543ed932009-04-24 12:43:40 +0000128 dst->fImage = SkMask::AllocImage(srcSize);
Mike Reed771ae962017-07-13 17:16:34 +0000129 auto blurStart = &blur[border.x() + border.y() * dst->fRowBytes];
reed@android.com0e3c6642009-09-18 13:41:56 +0000130 merge_src_with_blur(dst->fImage, src.fRowBytes,
Mike Reed771ae962017-07-13 17:16:34 +0000131 src.fImage, src.fRowBytes,
132 blurStart,
133 dst->fRowBytes,
134 src.fBounds.width(), src.fBounds.height());
135 SkMask::FreeImage(blur);
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000136 } else if (style != kNormal_SkBlurStyle) {
Mike Reed771ae962017-07-13 17:16:34 +0000137 auto dstStart = &dst->fImage[border.x() + border.y() * dst->fRowBytes];
138 clamp_with_orig(dstStart,
139 dst->fRowBytes, src.fImage, src.fRowBytes,
140 src.fBounds.width(), src.fBounds.height(), style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 }
143
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000144 if (style == kInner_SkBlurStyle) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 dst->fBounds = src.fBounds; // restore trimmed bounds
reed@android.com0e3c6642009-09-18 13:41:56 +0000146 dst->fRowBytes = src.fRowBytes;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 }
148
Mike Reed771ae962017-07-13 17:16:34 +0000149 if (margin != nullptr) {
150 *margin = border;
151 }
152
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 return true;
154}
155
humper@google.com7c7292c2013-01-04 20:29:03 +0000156/* Convolving a box with itself three times results in a piecewise
157 quadratic function:
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000158
humper@google.com7c7292c2013-01-04 20:29:03 +0000159 0 x <= -1.5
humper@google.coma99a92c2013-02-20 16:42:06 +0000160 9/8 + 3/2 x + 1/2 x^2 -1.5 < x <= -.5
humper@google.com7c7292c2013-01-04 20:29:03 +0000161 3/4 - x^2 -.5 < x <= .5
162 9/8 - 3/2 x + 1/2 x^2 0.5 < x <= 1.5
163 0 1.5 < x
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000164
humper@google.coma99a92c2013-02-20 16:42:06 +0000165 Mathematica:
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000166
humper@google.coma99a92c2013-02-20 16:42:06 +0000167 g[x_] := Piecewise [ {
168 {9/8 + 3/2 x + 1/2 x^2 , -1.5 < x <= -.5},
169 {3/4 - x^2 , -.5 < x <= .5},
170 {9/8 - 3/2 x + 1/2 x^2 , 0.5 < x <= 1.5}
171 }, 0]
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000172
humper@google.com7c7292c2013-01-04 20:29:03 +0000173 To get the profile curve of the blurred step function at the rectangle
174 edge, we evaluate the indefinite integral, which is piecewise cubic:
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000175
humper@google.com7c7292c2013-01-04 20:29:03 +0000176 0 x <= -1.5
humper@google.coma99a92c2013-02-20 16:42:06 +0000177 9/16 + 9/8 x + 3/4 x^2 + 1/6 x^3 -1.5 < x <= -0.5
humper@google.com7c7292c2013-01-04 20:29:03 +0000178 1/2 + 3/4 x - 1/3 x^3 -.5 < x <= .5
humper@google.coma99a92c2013-02-20 16:42:06 +0000179 7/16 + 9/8 x - 3/4 x^2 + 1/6 x^3 .5 < x <= 1.5
humper@google.com7c7292c2013-01-04 20:29:03 +0000180 1 1.5 < x
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000181
humper@google.coma99a92c2013-02-20 16:42:06 +0000182 in Mathematica code:
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000183
humper@google.coma99a92c2013-02-20 16:42:06 +0000184 gi[x_] := Piecewise[ {
185 { 0 , x <= -1.5 },
186 { 9/16 + 9/8 x + 3/4 x^2 + 1/6 x^3, -1.5 < x <= -0.5 },
187 { 1/2 + 3/4 x - 1/3 x^3 , -.5 < x <= .5},
188 { 7/16 + 9/8 x - 3/4 x^2 + 1/6 x^3, .5 < x <= 1.5}
189 },1]
humper@google.com7c7292c2013-01-04 20:29:03 +0000190*/
191
humper@google.coma99a92c2013-02-20 16:42:06 +0000192static float gaussianIntegral(float x) {
193 if (x > 1.5f) {
humper@google.com7c7292c2013-01-04 20:29:03 +0000194 return 0.0f;
195 }
humper@google.coma99a92c2013-02-20 16:42:06 +0000196 if (x < -1.5f) {
humper@google.com7c7292c2013-01-04 20:29:03 +0000197 return 1.0f;
198 }
199
200 float x2 = x*x;
201 float x3 = x2*x;
202
jvanverth@google.com9c4e5ac2013-01-07 18:41:28 +0000203 if ( x > 0.5f ) {
humper@google.coma99a92c2013-02-20 16:42:06 +0000204 return 0.5625f - (x3 / 6.0f - 3.0f * x2 * 0.25f + 1.125f * x);
humper@google.com7c7292c2013-01-04 20:29:03 +0000205 }
jvanverth@google.com9c4e5ac2013-01-07 18:41:28 +0000206 if ( x > -0.5f ) {
207 return 0.5f - (0.75f * x - x3 / 3.0f);
humper@google.com7c7292c2013-01-04 20:29:03 +0000208 }
jvanverth@google.com9c4e5ac2013-01-07 18:41:28 +0000209 return 0.4375f + (-x3 / 6.0f - 3.0f * x2 * 0.25f - 1.125f * x);
humper@google.com7c7292c2013-01-04 20:29:03 +0000210}
211
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000212/* ComputeBlurProfile allocates and fills in an array of floating
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000213 point values between 0 and 255 for the profile signature of
humper@google.com7c7292c2013-01-04 20:29:03 +0000214 a blurred half-plane with the given blur radius. Since we're
215 going to be doing screened multiplications (i.e., 1 - (1-x)(1-y))
216 all the time, we actually fill in the profile pre-inverted
217 (already done 255-x).
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000218
humper@google.com7c5d7b72013-03-11 20:16:28 +0000219 It's the responsibility of the caller to delete the
humper@google.com7c7292c2013-01-04 20:29:03 +0000220 memory returned in profile_out.
221*/
222
robertphillips30c4cae2015-09-15 10:20:55 -0700223uint8_t* SkBlurMask::ComputeBlurProfile(SkScalar sigma) {
robertphillips@google.com7ce661d2013-08-27 16:14:03 +0000224 int size = SkScalarCeilToInt(6*sigma);
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000225
robertphillipsd8982d72015-10-04 12:21:33 -0700226 int center = size >> 1;
halcanary385fe4d2015-08-26 13:07:48 -0700227 uint8_t* profile = new uint8_t[size];
humper@google.com7c7292c2013-01-04 20:29:03 +0000228
robertphillips@google.com7ce661d2013-08-27 16:14:03 +0000229 float invr = 1.f/(2*sigma);
humper@google.com7c7292c2013-01-04 20:29:03 +0000230
231 profile[0] = 255;
humper@google.coma99a92c2013-02-20 16:42:06 +0000232 for (int x = 1 ; x < size ; ++x) {
jvanverth@google.comd98df1a2013-02-20 19:02:34 +0000233 float scaled_x = (center - x - .5f) * invr;
humper@google.coma99a92c2013-02-20 16:42:06 +0000234 float gi = gaussianIntegral(scaled_x);
235 profile[x] = 255 - (uint8_t) (255.f * gi);
humper@google.com7c7292c2013-01-04 20:29:03 +0000236 }
237
robertphillips30c4cae2015-09-15 10:20:55 -0700238 return profile;
humper@google.com7c7292c2013-01-04 20:29:03 +0000239}
240
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000241// TODO MAYBE: Maintain a profile cache to avoid recomputing this for
humper@google.com7c7292c2013-01-04 20:29:03 +0000242// commonly used radii. Consider baking some of the most common blur radii
243// directly in as static data?
244
245// Implementation adapted from Michael Herf's approach:
246// http://stereopsis.com/shadowrect/
247
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000248uint8_t SkBlurMask::ProfileLookup(const uint8_t *profile, int loc, int blurred_width, int sharp_width) {
humper@google.coma99a92c2013-02-20 16:42:06 +0000249 int dx = SkAbs32(((loc << 1) + 1) - blurred_width) - sharp_width; // how far are we from the original edge?
250 int ox = dx >> 1;
251 if (ox < 0) {
252 ox = 0;
253 }
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000254
humper@google.coma99a92c2013-02-20 16:42:06 +0000255 return profile[ox];
256}
257
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000258void SkBlurMask::ComputeBlurredScanline(uint8_t *pixels, const uint8_t *profile,
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000259 unsigned int width, SkScalar sigma) {
260
261 unsigned int profile_size = SkScalarCeilToInt(6*sigma);
262 SkAutoTMalloc<uint8_t> horizontalScanline(width);
263
264 unsigned int sw = width - profile_size;
265 // nearest odd number less than the profile size represents the center
266 // of the (2x scaled) profile
267 int center = ( profile_size & ~1 ) - 1;
268
269 int w = sw - center;
270
271 for (unsigned int x = 0 ; x < width ; ++x) {
272 if (profile_size <= sw) {
273 pixels[x] = ProfileLookup(profile, x, width, w);
274 } else {
275 float span = float(sw)/(2*sigma);
276 float giX = 1.5f - (x+.5f)/(2*sigma);
277 pixels[x] = (uint8_t) (255 * (gaussianIntegral(giX) - gaussianIntegral(giX + span)));
278 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000279 }
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000280}
281
skia.committer@gmail.com7bd141d2013-08-28 07:01:18 +0000282bool SkBlurMask::BlurRect(SkScalar sigma, SkMask *dst,
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000283 const SkRect &src, SkBlurStyle style,
robertphillips@google.com7ce661d2013-08-27 16:14:03 +0000284 SkIPoint *margin, SkMask::CreateMode createMode) {
285 int profile_size = SkScalarCeilToInt(6*sigma);
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000286
humper@google.coma99a92c2013-02-20 16:42:06 +0000287 int pad = profile_size/2;
humper@google.com7c7292c2013-01-04 20:29:03 +0000288 if (margin) {
289 margin->set( pad, pad );
290 }
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000291
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000292 dst->fBounds.set(SkScalarRoundToInt(src.fLeft - pad),
293 SkScalarRoundToInt(src.fTop - pad),
294 SkScalarRoundToInt(src.fRight + pad),
humper@google.com68a690c2013-03-11 21:16:20 +0000295 SkScalarRoundToInt(src.fBottom + pad));
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000296
humper@google.com7c7292c2013-01-04 20:29:03 +0000297 dst->fRowBytes = dst->fBounds.width();
298 dst->fFormat = SkMask::kA8_Format;
halcanary96fcdcc2015-08-27 07:41:13 -0700299 dst->fImage = nullptr;
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000300
humper@google.com7c5d7b72013-03-11 20:16:28 +0000301 int sw = SkScalarFloorToInt(src.width());
302 int sh = SkScalarFloorToInt(src.height());
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000303
humper@google.com7c5d7b72013-03-11 20:16:28 +0000304 if (createMode == SkMask::kJustComputeBounds_CreateMode) {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000305 if (style == kInner_SkBlurStyle) {
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000306 dst->fBounds.set(SkScalarRoundToInt(src.fLeft),
307 SkScalarRoundToInt(src.fTop),
308 SkScalarRoundToInt(src.fRight),
humper@google.com68a690c2013-03-11 21:16:20 +0000309 SkScalarRoundToInt(src.fBottom)); // restore trimmed bounds
humper@google.com7c5d7b72013-03-11 20:16:28 +0000310 dst->fRowBytes = sw;
311 }
312 return true;
313 }
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000314
Ben Wagner7ecc5962016-11-02 17:07:33 -0400315 std::unique_ptr<uint8_t[]> profile(ComputeBlurProfile(sigma));
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000316
humper@google.com7c7292c2013-01-04 20:29:03 +0000317 size_t dstSize = dst->computeImageSize();
318 if (0 == dstSize) {
319 return false; // too big to allocate, abort
320 }
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000321
humper@google.com7c7292c2013-01-04 20:29:03 +0000322 uint8_t* dp = SkMask::AllocImage(dstSize);
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000323
humper@google.com7c7292c2013-01-04 20:29:03 +0000324 dst->fImage = dp;
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000325
humper@google.coma99a92c2013-02-20 16:42:06 +0000326 int dstHeight = dst->fBounds.height();
327 int dstWidth = dst->fBounds.width();
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000328
humper@google.com7c7292c2013-01-04 20:29:03 +0000329 uint8_t *outptr = dp;
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000330
humper@google.coma99a92c2013-02-20 16:42:06 +0000331 SkAutoTMalloc<uint8_t> horizontalScanline(dstWidth);
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000332 SkAutoTMalloc<uint8_t> verticalScanline(dstHeight);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000333
robertphillips30c4cae2015-09-15 10:20:55 -0700334 ComputeBlurredScanline(horizontalScanline, profile.get(), dstWidth, sigma);
335 ComputeBlurredScanline(verticalScanline, profile.get(), dstHeight, sigma);
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000336
humper@google.coma99a92c2013-02-20 16:42:06 +0000337 for (int y = 0 ; y < dstHeight ; ++y) {
humper@google.coma99a92c2013-02-20 16:42:06 +0000338 for (int x = 0 ; x < dstWidth ; x++) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000339 unsigned int maskval = SkMulDiv255Round(horizontalScanline[x], verticalScanline[y]);
humper@google.com7c7292c2013-01-04 20:29:03 +0000340 *(outptr++) = maskval;
341 }
342 }
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000343
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000344 if (style == kInner_SkBlurStyle) {
humper@google.coma99a92c2013-02-20 16:42:06 +0000345 // now we allocate the "real" dst, mirror the size of src
jvanverth@google.comd98df1a2013-02-20 19:02:34 +0000346 size_t srcSize = (size_t)(src.width() * src.height());
humper@google.coma99a92c2013-02-20 16:42:06 +0000347 if (0 == srcSize) {
348 return false; // too big to allocate, abort
349 }
350 dst->fImage = SkMask::AllocImage(srcSize);
351 for (int y = 0 ; y < sh ; y++) {
352 uint8_t *blur_scanline = dp + (y+pad)*dstWidth + pad;
353 uint8_t *inner_scanline = dst->fImage + y*sw;
354 memcpy(inner_scanline, blur_scanline, sw);
355 }
356 SkMask::FreeImage(dp);
357
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000358 dst->fBounds.set(SkScalarRoundToInt(src.fLeft),
359 SkScalarRoundToInt(src.fTop),
360 SkScalarRoundToInt(src.fRight),
humper@google.com68a690c2013-03-11 21:16:20 +0000361 SkScalarRoundToInt(src.fBottom)); // restore trimmed bounds
humper@google.coma99a92c2013-02-20 16:42:06 +0000362 dst->fRowBytes = sw;
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000363
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000364 } else if (style == kOuter_SkBlurStyle) {
humper@google.coma99a92c2013-02-20 16:42:06 +0000365 for (int y = pad ; y < dstHeight-pad ; y++) {
366 uint8_t *dst_scanline = dp + y*dstWidth + pad;
367 memset(dst_scanline, 0, sw);
368 }
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000369 } else if (style == kSolid_SkBlurStyle) {
humper@google.comd4d57302013-03-11 22:18:54 +0000370 for (int y = pad ; y < dstHeight-pad ; y++) {
371 uint8_t *dst_scanline = dp + y*dstWidth + pad;
372 memset(dst_scanline, 0xff, sw);
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000373 }
humper@google.coma99a92c2013-02-20 16:42:06 +0000374 }
375 // normal and solid styles are the same for analytic rect blurs, so don't
376 // need to handle solid specially.
377
378 return true;
379}
380
commit-bot@chromium.orga4771542014-03-10 21:42:06 +0000381bool SkBlurMask::BlurRRect(SkScalar sigma, SkMask *dst,
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000382 const SkRRect &src, SkBlurStyle style,
commit-bot@chromium.orga4771542014-03-10 21:42:06 +0000383 SkIPoint *margin, SkMask::CreateMode createMode) {
384 // Temporary for now -- always fail, should cause caller to fall back
385 // to old path. Plumbing just to land API and parallelize effort.
386
387 return false;
robertphillips@google.com7ce661d2013-08-27 16:14:03 +0000388}
commit-bot@chromium.orga4771542014-03-10 21:42:06 +0000389
humper@google.coma99a92c2013-02-20 16:42:06 +0000390// The "simple" blur is a direct implementation of separable convolution with a discrete
391// gaussian kernel. It's "ground truth" in a sense; too slow to be used, but very
392// useful for correctness comparisons.
393
robertphillips@google.com7ce661d2013-08-27 16:14:03 +0000394bool SkBlurMask::BlurGroundTruth(SkScalar sigma, SkMask* dst, const SkMask& src,
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000395 SkBlurStyle style, SkIPoint* margin) {
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000396
humper@google.coma99a92c2013-02-20 16:42:06 +0000397 if (src.fFormat != SkMask::kA8_Format) {
398 return false;
399 }
400
robertphillips@google.com7ce661d2013-08-27 16:14:03 +0000401 float variance = sigma * sigma;
humper@google.coma99a92c2013-02-20 16:42:06 +0000402
reed@google.come1ca7052013-12-17 19:22:07 +0000403 int windowSize = SkScalarCeilToInt(sigma*6);
humper@google.coma99a92c2013-02-20 16:42:06 +0000404 // round window size up to nearest odd number
405 windowSize |= 1;
406
407 SkAutoTMalloc<float> gaussWindow(windowSize);
408
409 int halfWindow = windowSize >> 1;
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000410
humper@google.coma99a92c2013-02-20 16:42:06 +0000411 gaussWindow[halfWindow] = 1;
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000412
humper@google.coma99a92c2013-02-20 16:42:06 +0000413 float windowSum = 1;
414 for (int x = 1 ; x <= halfWindow ; ++x) {
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000415 float gaussian = expf(-x*x / (2*variance));
humper@google.coma99a92c2013-02-20 16:42:06 +0000416 gaussWindow[halfWindow + x] = gaussWindow[halfWindow-x] = gaussian;
417 windowSum += 2*gaussian;
418 }
419
420 // leave the filter un-normalized for now; we will divide by the normalization
421 // sum later;
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000422
humper@google.coma99a92c2013-02-20 16:42:06 +0000423 int pad = halfWindow;
424 if (margin) {
425 margin->set( pad, pad );
426 }
427
428 dst->fBounds = src.fBounds;
429 dst->fBounds.outset(pad, pad);
430
431 dst->fRowBytes = dst->fBounds.width();
432 dst->fFormat = SkMask::kA8_Format;
halcanary96fcdcc2015-08-27 07:41:13 -0700433 dst->fImage = nullptr;
humper@google.coma99a92c2013-02-20 16:42:06 +0000434
435 if (src.fImage) {
436
437 size_t dstSize = dst->computeImageSize();
438 if (0 == dstSize) {
439 return false; // too big to allocate, abort
440 }
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000441
humper@google.coma99a92c2013-02-20 16:42:06 +0000442 int srcWidth = src.fBounds.width();
443 int srcHeight = src.fBounds.height();
444 int dstWidth = dst->fBounds.width();
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000445
humper@google.coma99a92c2013-02-20 16:42:06 +0000446 const uint8_t* srcPixels = src.fImage;
447 uint8_t* dstPixels = SkMask::AllocImage(dstSize);
448 SkAutoTCallVProc<uint8_t, SkMask_FreeImage> autoCall(dstPixels);
449
450 // do the actual blur. First, make a padded copy of the source.
451 // use double pad so we never have to check if we're outside anything
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000452
humper@google.coma99a92c2013-02-20 16:42:06 +0000453 int padWidth = srcWidth + 4*pad;
454 int padHeight = srcHeight;
455 int padSize = padWidth * padHeight;
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000456
humper@google.coma99a92c2013-02-20 16:42:06 +0000457 SkAutoTMalloc<uint8_t> padPixels(padSize);
458 memset(padPixels, 0, padSize);
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000459
humper@google.coma99a92c2013-02-20 16:42:06 +0000460 for (int y = 0 ; y < srcHeight; ++y) {
461 uint8_t* padptr = padPixels + y * padWidth + 2*pad;
462 const uint8_t* srcptr = srcPixels + y * srcWidth;
463 memcpy(padptr, srcptr, srcWidth);
464 }
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000465
humper@google.coma99a92c2013-02-20 16:42:06 +0000466 // blur in X, transposing the result into a temporary floating point buffer.
467 // also double-pad the intermediate result so that the second blur doesn't
468 // have to do extra conditionals.
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000469
humper@google.coma99a92c2013-02-20 16:42:06 +0000470 int tmpWidth = padHeight + 4*pad;
471 int tmpHeight = padWidth - 2*pad;
472 int tmpSize = tmpWidth * tmpHeight;
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000473
humper@google.coma99a92c2013-02-20 16:42:06 +0000474 SkAutoTMalloc<float> tmpImage(tmpSize);
475 memset(tmpImage, 0, tmpSize*sizeof(tmpImage[0]));
476
477 for (int y = 0 ; y < padHeight ; ++y) {
478 uint8_t *srcScanline = padPixels + y*padWidth;
479 for (int x = pad ; x < padWidth - pad ; ++x) {
480 float *outPixel = tmpImage + (x-pad)*tmpWidth + y + 2*pad; // transposed output
481 uint8_t *windowCenter = srcScanline + x;
482 for (int i = -pad ; i <= pad ; ++i) {
483 *outPixel += gaussWindow[pad+i]*windowCenter[i];
484 }
485 *outPixel /= windowSum;
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000486 }
humper@google.coma99a92c2013-02-20 16:42:06 +0000487 }
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000488
humper@google.coma99a92c2013-02-20 16:42:06 +0000489 // blur in Y; now filling in the actual desired destination. We have to do
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000490 // the transpose again; these transposes guarantee that we read memory in
humper@google.coma99a92c2013-02-20 16:42:06 +0000491 // linear order.
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000492
humper@google.coma99a92c2013-02-20 16:42:06 +0000493 for (int y = 0 ; y < tmpHeight ; ++y) {
494 float *srcScanline = tmpImage + y*tmpWidth;
495 for (int x = pad ; x < tmpWidth - pad ; ++x) {
496 float *windowCenter = srcScanline + x;
497 float finalValue = 0;
498 for (int i = -pad ; i <= pad ; ++i) {
499 finalValue += gaussWindow[pad+i]*windowCenter[i];
500 }
501 finalValue /= windowSum;
502 uint8_t *outPixel = dstPixels + (x-pad)*dstWidth + y; // transposed output
503 int integerPixel = int(finalValue + 0.5f);
504 *outPixel = SkClampMax( SkClampPos(integerPixel), 255 );
505 }
506 }
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000507
humper@google.coma99a92c2013-02-20 16:42:06 +0000508 dst->fImage = dstPixels;
509 // if need be, alloc the "real" dst (same size as src) and copy/merge
510 // the blur into it (applying the src)
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000511 if (style == kInner_SkBlurStyle) {
humper@google.coma99a92c2013-02-20 16:42:06 +0000512 // now we allocate the "real" dst, mirror the size of src
513 size_t srcSize = src.computeImageSize();
514 if (0 == srcSize) {
515 return false; // too big to allocate, abort
516 }
517 dst->fImage = SkMask::AllocImage(srcSize);
518 merge_src_with_blur(dst->fImage, src.fRowBytes,
519 srcPixels, src.fRowBytes,
520 dstPixels + pad*dst->fRowBytes + pad,
521 dst->fRowBytes, srcWidth, srcHeight);
522 SkMask::FreeImage(dstPixels);
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000523 } else if (style != kNormal_SkBlurStyle) {
humper@google.coma99a92c2013-02-20 16:42:06 +0000524 clamp_with_orig(dstPixels + pad*dst->fRowBytes + pad,
525 dst->fRowBytes, srcPixels, src.fRowBytes, srcWidth, srcHeight, style);
526 }
mtklein18300a32016-03-16 13:53:35 -0700527 (void)autoCall.release();
humper@google.coma99a92c2013-02-20 16:42:06 +0000528 }
529
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000530 if (style == kInner_SkBlurStyle) {
humper@google.coma99a92c2013-02-20 16:42:06 +0000531 dst->fBounds = src.fBounds; // restore trimmed bounds
532 dst->fRowBytes = src.fRowBytes;
533 }
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000534
humper@google.com7c7292c2013-01-04 20:29:03 +0000535 return true;
536}