| 1 | /* | 
|---|
| 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 |  | 
|---|
| 8 | #include "src/core/SkBlurMask.h" | 
|---|
| 9 |  | 
|---|
| 10 | #include "include/core/SkColorPriv.h" | 
|---|
| 11 | #include "include/core/SkMath.h" | 
|---|
| 12 | #include "include/private/SkTemplates.h" | 
|---|
| 13 | #include "include/private/SkTo.h" | 
|---|
| 14 | #include "src/core/SkEndian.h" | 
|---|
| 15 | #include "src/core/SkMaskBlurFilter.h" | 
|---|
| 16 | #include "src/core/SkMathPriv.h" | 
|---|
| 17 |  | 
|---|
| 18 | // This constant approximates the scaling done in the software path's | 
|---|
| 19 | // "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)). | 
|---|
| 20 | // IMHO, it actually should be 1:  we blur "less" than we should do | 
|---|
| 21 | // according to the CSS and canvas specs, simply because Safari does the same. | 
|---|
| 22 | // Firefox used to do the same too, until 4.0 where they fixed it.  So at some | 
|---|
| 23 | // point we should probably get rid of these scaling constants and rebaseline | 
|---|
| 24 | // all the blur tests. | 
|---|
| 25 | static const SkScalar kBLUR_SIGMA_SCALE = 0.57735f; | 
|---|
| 26 |  | 
|---|
| 27 | SkScalar SkBlurMask::ConvertRadiusToSigma(SkScalar radius) { | 
|---|
| 28 | return radius > 0 ? kBLUR_SIGMA_SCALE * radius + 0.5f : 0.0f; | 
|---|
| 29 | } | 
|---|
| 30 |  | 
|---|
| 31 | SkScalar SkBlurMask::ConvertSigmaToRadius(SkScalar sigma) { | 
|---|
| 32 | return sigma > 0.5f ? (sigma - 0.5f) / kBLUR_SIGMA_SCALE : 0.0f; | 
|---|
| 33 | } | 
|---|
| 34 |  | 
|---|
| 35 |  | 
|---|
| 36 | template <typename AlphaIter> | 
|---|
| 37 | static void merge_src_with_blur(uint8_t dst[], int dstRB, | 
|---|
| 38 | AlphaIter src, int srcRB, | 
|---|
| 39 | const uint8_t blur[], int blurRB, | 
|---|
| 40 | int sw, int sh) { | 
|---|
| 41 | dstRB -= sw; | 
|---|
| 42 | blurRB -= sw; | 
|---|
| 43 | while (--sh >= 0) { | 
|---|
| 44 | AlphaIter rowSrc(src); | 
|---|
| 45 | for (int x = sw - 1; x >= 0; --x) { | 
|---|
| 46 | *dst = SkToU8(SkAlphaMul(*blur, SkAlpha255To256(*rowSrc))); | 
|---|
| 47 | ++dst; | 
|---|
| 48 | ++rowSrc; | 
|---|
| 49 | ++blur; | 
|---|
| 50 | } | 
|---|
| 51 | dst += dstRB; | 
|---|
| 52 | src >>= srcRB; | 
|---|
| 53 | blur += blurRB; | 
|---|
| 54 | } | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | template <typename AlphaIter> | 
|---|
| 58 | static void clamp_solid_with_orig(uint8_t dst[], int dstRowBytes, | 
|---|
| 59 | AlphaIter src, int srcRowBytes, | 
|---|
| 60 | int sw, int sh) { | 
|---|
| 61 | int x; | 
|---|
| 62 | while (--sh >= 0) { | 
|---|
| 63 | AlphaIter rowSrc(src); | 
|---|
| 64 | for (x = sw - 1; x >= 0; --x) { | 
|---|
| 65 | int s = *rowSrc; | 
|---|
| 66 | int d = *dst; | 
|---|
| 67 | *dst = SkToU8(s + d - SkMulDiv255Round(s, d)); | 
|---|
| 68 | ++dst; | 
|---|
| 69 | ++rowSrc; | 
|---|
| 70 | } | 
|---|
| 71 | dst += dstRowBytes - sw; | 
|---|
| 72 | src >>= srcRowBytes; | 
|---|
| 73 | } | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | template <typename AlphaIter> | 
|---|
| 77 | static void clamp_outer_with_orig(uint8_t dst[], int dstRowBytes, | 
|---|
| 78 | AlphaIter src, int srcRowBytes, | 
|---|
| 79 | int sw, int sh) { | 
|---|
| 80 | int x; | 
|---|
| 81 | while (--sh >= 0) { | 
|---|
| 82 | AlphaIter rowSrc(src); | 
|---|
| 83 | for (x = sw - 1; x >= 0; --x) { | 
|---|
| 84 | int srcValue = *rowSrc; | 
|---|
| 85 | if (srcValue) { | 
|---|
| 86 | *dst = SkToU8(SkAlphaMul(*dst, SkAlpha255To256(255 - srcValue))); | 
|---|
| 87 | } | 
|---|
| 88 | ++dst; | 
|---|
| 89 | ++rowSrc; | 
|---|
| 90 | } | 
|---|
| 91 | dst += dstRowBytes - sw; | 
|---|
| 92 | src >>= srcRowBytes; | 
|---|
| 93 | } | 
|---|
| 94 | } | 
|---|
| 95 | /////////////////////////////////////////////////////////////////////////////// | 
|---|
| 96 |  | 
|---|
| 97 | // we use a local function to wrap the class static method to work around | 
|---|
| 98 | // a bug in gcc98 | 
|---|
| 99 | void SkMask_FreeImage(uint8_t* image); | 
|---|
| 100 | void SkMask_FreeImage(uint8_t* image) { | 
|---|
| 101 | SkMask::FreeImage(image); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | bool SkBlurMask::BoxBlur(SkMask* dst, const SkMask& src, SkScalar sigma, SkBlurStyle style, | 
|---|
| 105 | SkIPoint* margin) { | 
|---|
| 106 | if (src.fFormat != SkMask::kBW_Format && | 
|---|
| 107 | src.fFormat != SkMask::kA8_Format && | 
|---|
| 108 | src.fFormat != SkMask::kARGB32_Format && | 
|---|
| 109 | src.fFormat != SkMask::kLCD16_Format) | 
|---|
| 110 | { | 
|---|
| 111 | return false; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | SkMaskBlurFilter blurFilter{sigma, sigma}; | 
|---|
| 115 | if (blurFilter.hasNoBlur()) { | 
|---|
| 116 | // If there is no effective blur most styles will just produce the original mask. | 
|---|
| 117 | // However, kOuter_SkBlurStyle will produce an empty mask. | 
|---|
| 118 | if (style == kOuter_SkBlurStyle) { | 
|---|
| 119 | dst->fImage = nullptr; | 
|---|
| 120 | dst->fBounds = SkIRect::MakeEmpty(); | 
|---|
| 121 | dst->fRowBytes = dst->fBounds.width(); | 
|---|
| 122 | dst->fFormat = SkMask::kA8_Format; | 
|---|
| 123 | if (margin != nullptr) { | 
|---|
| 124 | // This filter will disregard the src.fImage completely. | 
|---|
| 125 | // The margin is actually {-(src.fBounds.width() / 2), -(src.fBounds.height() / 2)} | 
|---|
| 126 | // but it is not clear if callers will fall over with negative margins. | 
|---|
| 127 | *margin = SkIPoint{0,0}; | 
|---|
| 128 | } | 
|---|
| 129 | return true; | 
|---|
| 130 | } | 
|---|
| 131 | return false; | 
|---|
| 132 | } | 
|---|
| 133 | const SkIPoint border = blurFilter.blur(src, dst); | 
|---|
| 134 | // If src.fImage is null, then this call is only to calculate the border. | 
|---|
| 135 | if (src.fImage != nullptr && dst->fImage == nullptr) { | 
|---|
| 136 | return false; | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | if (margin != nullptr) { | 
|---|
| 140 | *margin = border; | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | if (src.fImage == nullptr) { | 
|---|
| 144 | if (style == kInner_SkBlurStyle) { | 
|---|
| 145 | dst->fBounds = src.fBounds; // restore trimmed bounds | 
|---|
| 146 | dst->fRowBytes = dst->fBounds.width(); | 
|---|
| 147 | } | 
|---|
| 148 | return true; | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | switch (style) { | 
|---|
| 152 | case kNormal_SkBlurStyle: | 
|---|
| 153 | break; | 
|---|
| 154 | case kSolid_SkBlurStyle: { | 
|---|
| 155 | auto dstStart = &dst->fImage[border.x() + border.y() * dst->fRowBytes]; | 
|---|
| 156 | switch (src.fFormat) { | 
|---|
| 157 | case SkMask::kBW_Format: | 
|---|
| 158 | clamp_solid_with_orig( | 
|---|
| 159 | dstStart, dst->fRowBytes, | 
|---|
| 160 | SkMask::AlphaIter<SkMask::kBW_Format>(src.fImage, 0), src.fRowBytes, | 
|---|
| 161 | src.fBounds.width(), src.fBounds.height()); | 
|---|
| 162 | break; | 
|---|
| 163 | case SkMask::kA8_Format: | 
|---|
| 164 | clamp_solid_with_orig( | 
|---|
| 165 | dstStart, dst->fRowBytes, | 
|---|
| 166 | SkMask::AlphaIter<SkMask::kA8_Format>(src.fImage), src.fRowBytes, | 
|---|
| 167 | src.fBounds.width(), src.fBounds.height()); | 
|---|
| 168 | break; | 
|---|
| 169 | case SkMask::kARGB32_Format: { | 
|---|
| 170 | uint32_t* srcARGB = reinterpret_cast<uint32_t*>(src.fImage); | 
|---|
| 171 | clamp_solid_with_orig( | 
|---|
| 172 | dstStart, dst->fRowBytes, | 
|---|
| 173 | SkMask::AlphaIter<SkMask::kARGB32_Format>(srcARGB), src.fRowBytes, | 
|---|
| 174 | src.fBounds.width(), src.fBounds.height()); | 
|---|
| 175 | } break; | 
|---|
| 176 | case SkMask::kLCD16_Format: { | 
|---|
| 177 | uint16_t* srcLCD = reinterpret_cast<uint16_t*>(src.fImage); | 
|---|
| 178 | clamp_solid_with_orig( | 
|---|
| 179 | dstStart, dst->fRowBytes, | 
|---|
| 180 | SkMask::AlphaIter<SkMask::kLCD16_Format>(srcLCD), src.fRowBytes, | 
|---|
| 181 | src.fBounds.width(), src.fBounds.height()); | 
|---|
| 182 | } break; | 
|---|
| 183 | default: | 
|---|
| 184 | SK_ABORT( "Unhandled format."); | 
|---|
| 185 | } | 
|---|
| 186 | } break; | 
|---|
| 187 | case kOuter_SkBlurStyle: { | 
|---|
| 188 | auto dstStart = &dst->fImage[border.x() + border.y() * dst->fRowBytes]; | 
|---|
| 189 | switch (src.fFormat) { | 
|---|
| 190 | case SkMask::kBW_Format: | 
|---|
| 191 | clamp_outer_with_orig( | 
|---|
| 192 | dstStart, dst->fRowBytes, | 
|---|
| 193 | SkMask::AlphaIter<SkMask::kBW_Format>(src.fImage, 0), src.fRowBytes, | 
|---|
| 194 | src.fBounds.width(), src.fBounds.height()); | 
|---|
| 195 | break; | 
|---|
| 196 | case SkMask::kA8_Format: | 
|---|
| 197 | clamp_outer_with_orig( | 
|---|
| 198 | dstStart, dst->fRowBytes, | 
|---|
| 199 | SkMask::AlphaIter<SkMask::kA8_Format>(src.fImage), src.fRowBytes, | 
|---|
| 200 | src.fBounds.width(), src.fBounds.height()); | 
|---|
| 201 | break; | 
|---|
| 202 | case SkMask::kARGB32_Format: { | 
|---|
| 203 | uint32_t* srcARGB = reinterpret_cast<uint32_t*>(src.fImage); | 
|---|
| 204 | clamp_outer_with_orig( | 
|---|
| 205 | dstStart, dst->fRowBytes, | 
|---|
| 206 | SkMask::AlphaIter<SkMask::kARGB32_Format>(srcARGB), src.fRowBytes, | 
|---|
| 207 | src.fBounds.width(), src.fBounds.height()); | 
|---|
| 208 | } break; | 
|---|
| 209 | case SkMask::kLCD16_Format: { | 
|---|
| 210 | uint16_t* srcLCD = reinterpret_cast<uint16_t*>(src.fImage); | 
|---|
| 211 | clamp_outer_with_orig( | 
|---|
| 212 | dstStart, dst->fRowBytes, | 
|---|
| 213 | SkMask::AlphaIter<SkMask::kLCD16_Format>(srcLCD), src.fRowBytes, | 
|---|
| 214 | src.fBounds.width(), src.fBounds.height()); | 
|---|
| 215 | } break; | 
|---|
| 216 | default: | 
|---|
| 217 | SK_ABORT( "Unhandled format."); | 
|---|
| 218 | } | 
|---|
| 219 | } break; | 
|---|
| 220 | case kInner_SkBlurStyle: { | 
|---|
| 221 | // now we allocate the "real" dst, mirror the size of src | 
|---|
| 222 | SkMask blur = *dst; | 
|---|
| 223 | SkAutoMaskFreeImage autoFreeBlurMask(blur.fImage); | 
|---|
| 224 | dst->fBounds = src.fBounds; | 
|---|
| 225 | dst->fRowBytes = dst->fBounds.width(); | 
|---|
| 226 | size_t dstSize = dst->computeImageSize(); | 
|---|
| 227 | if (0 == dstSize) { | 
|---|
| 228 | return false;   // too big to allocate, abort | 
|---|
| 229 | } | 
|---|
| 230 | dst->fImage = SkMask::AllocImage(dstSize); | 
|---|
| 231 | auto blurStart = &blur.fImage[border.x() + border.y() * blur.fRowBytes]; | 
|---|
| 232 | switch (src.fFormat) { | 
|---|
| 233 | case SkMask::kBW_Format: | 
|---|
| 234 | merge_src_with_blur( | 
|---|
| 235 | dst->fImage, dst->fRowBytes, | 
|---|
| 236 | SkMask::AlphaIter<SkMask::kBW_Format>(src.fImage, 0), src.fRowBytes, | 
|---|
| 237 | blurStart, blur.fRowBytes, | 
|---|
| 238 | src.fBounds.width(), src.fBounds.height()); | 
|---|
| 239 | break; | 
|---|
| 240 | case SkMask::kA8_Format: | 
|---|
| 241 | merge_src_with_blur( | 
|---|
| 242 | dst->fImage, dst->fRowBytes, | 
|---|
| 243 | SkMask::AlphaIter<SkMask::kA8_Format>(src.fImage), src.fRowBytes, | 
|---|
| 244 | blurStart, blur.fRowBytes, | 
|---|
| 245 | src.fBounds.width(), src.fBounds.height()); | 
|---|
| 246 | break; | 
|---|
| 247 | case SkMask::kARGB32_Format: { | 
|---|
| 248 | uint32_t* srcARGB = reinterpret_cast<uint32_t*>(src.fImage); | 
|---|
| 249 | merge_src_with_blur( | 
|---|
| 250 | dst->fImage, dst->fRowBytes, | 
|---|
| 251 | SkMask::AlphaIter<SkMask::kARGB32_Format>(srcARGB), src.fRowBytes, | 
|---|
| 252 | blurStart, blur.fRowBytes, | 
|---|
| 253 | src.fBounds.width(), src.fBounds.height()); | 
|---|
| 254 | } break; | 
|---|
| 255 | case SkMask::kLCD16_Format: { | 
|---|
| 256 | uint16_t* srcLCD = reinterpret_cast<uint16_t*>(src.fImage); | 
|---|
| 257 | merge_src_with_blur( | 
|---|
| 258 | dst->fImage, dst->fRowBytes, | 
|---|
| 259 | SkMask::AlphaIter<SkMask::kLCD16_Format>(srcLCD), src.fRowBytes, | 
|---|
| 260 | blurStart, blur.fRowBytes, | 
|---|
| 261 | src.fBounds.width(), src.fBounds.height()); | 
|---|
| 262 | } break; | 
|---|
| 263 | default: | 
|---|
| 264 | SK_ABORT( "Unhandled format."); | 
|---|
| 265 | } | 
|---|
| 266 | } break; | 
|---|
| 267 | } | 
|---|
| 268 |  | 
|---|
| 269 | return true; | 
|---|
| 270 | } | 
|---|
| 271 |  | 
|---|
| 272 | /* Convolving a box with itself three times results in a piecewise | 
|---|
| 273 | quadratic function: | 
|---|
| 274 |  | 
|---|
| 275 | 0                              x <= -1.5 | 
|---|
| 276 | 9/8 + 3/2 x + 1/2 x^2   -1.5 < x <= -.5 | 
|---|
| 277 | 3/4 - x^2                -.5 < x <= .5 | 
|---|
| 278 | 9/8 - 3/2 x + 1/2 x^2    0.5 < x <= 1.5 | 
|---|
| 279 | 0                        1.5 < x | 
|---|
| 280 |  | 
|---|
| 281 | Mathematica: | 
|---|
| 282 |  | 
|---|
| 283 | g[x_] := Piecewise [ { | 
|---|
| 284 | {9/8 + 3/2 x + 1/2 x^2 ,  -1.5 < x <= -.5}, | 
|---|
| 285 | {3/4 - x^2             ,   -.5 < x <= .5}, | 
|---|
| 286 | {9/8 - 3/2 x + 1/2 x^2 ,   0.5 < x <= 1.5} | 
|---|
| 287 | }, 0] | 
|---|
| 288 |  | 
|---|
| 289 | To get the profile curve of the blurred step function at the rectangle | 
|---|
| 290 | edge, we evaluate the indefinite integral, which is piecewise cubic: | 
|---|
| 291 |  | 
|---|
| 292 | 0                                        x <= -1.5 | 
|---|
| 293 | 9/16 + 9/8 x + 3/4 x^2 + 1/6 x^3   -1.5 < x <= -0.5 | 
|---|
| 294 | 1/2 + 3/4 x - 1/3 x^3              -.5 < x <= .5 | 
|---|
| 295 | 7/16 + 9/8 x - 3/4 x^2 + 1/6 x^3     .5 < x <= 1.5 | 
|---|
| 296 | 1                                  1.5 < x | 
|---|
| 297 |  | 
|---|
| 298 | in Mathematica code: | 
|---|
| 299 |  | 
|---|
| 300 | gi[x_] := Piecewise[ { | 
|---|
| 301 | { 0 , x <= -1.5 }, | 
|---|
| 302 | { 9/16 + 9/8 x + 3/4 x^2 + 1/6 x^3, -1.5 < x <= -0.5 }, | 
|---|
| 303 | { 1/2 + 3/4 x - 1/3 x^3          ,  -.5 < x <= .5}, | 
|---|
| 304 | { 7/16 + 9/8 x - 3/4 x^2 + 1/6 x^3,   .5 < x <= 1.5} | 
|---|
| 305 | },1] | 
|---|
| 306 | */ | 
|---|
| 307 |  | 
|---|
| 308 | static float gaussianIntegral(float x) { | 
|---|
| 309 | if (x > 1.5f) { | 
|---|
| 310 | return 0.0f; | 
|---|
| 311 | } | 
|---|
| 312 | if (x < -1.5f) { | 
|---|
| 313 | return 1.0f; | 
|---|
| 314 | } | 
|---|
| 315 |  | 
|---|
| 316 | float x2 = x*x; | 
|---|
| 317 | float x3 = x2*x; | 
|---|
| 318 |  | 
|---|
| 319 | if ( x > 0.5f ) { | 
|---|
| 320 | return 0.5625f - (x3 / 6.0f - 3.0f * x2 * 0.25f + 1.125f * x); | 
|---|
| 321 | } | 
|---|
| 322 | if ( x > -0.5f ) { | 
|---|
| 323 | return 0.5f - (0.75f * x - x3 / 3.0f); | 
|---|
| 324 | } | 
|---|
| 325 | return 0.4375f + (-x3 / 6.0f - 3.0f * x2 * 0.25f - 1.125f * x); | 
|---|
| 326 | } | 
|---|
| 327 |  | 
|---|
| 328 | /*  ComputeBlurProfile fills in an array of floating | 
|---|
| 329 | point values between 0 and 255 for the profile signature of | 
|---|
| 330 | a blurred half-plane with the given blur radius.  Since we're | 
|---|
| 331 | going to be doing screened multiplications (i.e., 1 - (1-x)(1-y)) | 
|---|
| 332 | all the time, we actually fill in the profile pre-inverted | 
|---|
| 333 | (already done 255-x). | 
|---|
| 334 | */ | 
|---|
| 335 |  | 
|---|
| 336 | void SkBlurMask::ComputeBlurProfile(uint8_t* profile, int size, SkScalar sigma) { | 
|---|
| 337 | SkASSERT(SkScalarCeilToInt(6*sigma) == size); | 
|---|
| 338 |  | 
|---|
| 339 | int center = size >> 1; | 
|---|
| 340 |  | 
|---|
| 341 | float invr = 1.f/(2*sigma); | 
|---|
| 342 |  | 
|---|
| 343 | profile[0] = 255; | 
|---|
| 344 | for (int x = 1 ; x < size ; ++x) { | 
|---|
| 345 | float scaled_x = (center - x - .5f) * invr; | 
|---|
| 346 | float gi = gaussianIntegral(scaled_x); | 
|---|
| 347 | profile[x] = 255 - (uint8_t) (255.f * gi); | 
|---|
| 348 | } | 
|---|
| 349 | } | 
|---|
| 350 |  | 
|---|
| 351 | // TODO MAYBE: Maintain a profile cache to avoid recomputing this for | 
|---|
| 352 | // commonly used radii.  Consider baking some of the most common blur radii | 
|---|
| 353 | // directly in as static data? | 
|---|
| 354 |  | 
|---|
| 355 | // Implementation adapted from Michael Herf's approach: | 
|---|
| 356 | // http://stereopsis.com/shadowrect/ | 
|---|
| 357 |  | 
|---|
| 358 | uint8_t SkBlurMask::ProfileLookup(const uint8_t *profile, int loc, | 
|---|
| 359 | int blurredWidth, int sharpWidth) { | 
|---|
| 360 | // how far are we from the original edge? | 
|---|
| 361 | int dx = SkAbs32(((loc << 1) + 1) - blurredWidth) - sharpWidth; | 
|---|
| 362 | int ox = dx >> 1; | 
|---|
| 363 | if (ox < 0) { | 
|---|
| 364 | ox = 0; | 
|---|
| 365 | } | 
|---|
| 366 |  | 
|---|
| 367 | return profile[ox]; | 
|---|
| 368 | } | 
|---|
| 369 |  | 
|---|
| 370 | void SkBlurMask::ComputeBlurredScanline(uint8_t *pixels, const uint8_t *profile, | 
|---|
| 371 | unsigned int width, SkScalar sigma) { | 
|---|
| 372 |  | 
|---|
| 373 | unsigned int profile_size = SkScalarCeilToInt(6*sigma); | 
|---|
| 374 | SkAutoTMalloc<uint8_t> horizontalScanline(width); | 
|---|
| 375 |  | 
|---|
| 376 | unsigned int sw = width - profile_size; | 
|---|
| 377 | // nearest odd number less than the profile size represents the center | 
|---|
| 378 | // of the (2x scaled) profile | 
|---|
| 379 | int center = ( profile_size & ~1 ) - 1; | 
|---|
| 380 |  | 
|---|
| 381 | int w = sw - center; | 
|---|
| 382 |  | 
|---|
| 383 | for (unsigned int x = 0 ; x < width ; ++x) { | 
|---|
| 384 | if (profile_size <= sw) { | 
|---|
| 385 | pixels[x] = ProfileLookup(profile, x, width, w); | 
|---|
| 386 | } else { | 
|---|
| 387 | float span = float(sw)/(2*sigma); | 
|---|
| 388 | float giX = 1.5f - (x+.5f)/(2*sigma); | 
|---|
| 389 | pixels[x] = (uint8_t) (255 * (gaussianIntegral(giX) - gaussianIntegral(giX + span))); | 
|---|
| 390 | } | 
|---|
| 391 | } | 
|---|
| 392 | } | 
|---|
| 393 |  | 
|---|
| 394 | bool SkBlurMask::BlurRect(SkScalar sigma, SkMask *dst, | 
|---|
| 395 | const SkRect &src, SkBlurStyle style, | 
|---|
| 396 | SkIPoint *margin, SkMask::CreateMode createMode) { | 
|---|
| 397 | int profileSize = SkScalarCeilToInt(6*sigma); | 
|---|
| 398 | if (profileSize <= 0) { | 
|---|
| 399 | return false;   // no blur to compute | 
|---|
| 400 | } | 
|---|
| 401 |  | 
|---|
| 402 | int pad = profileSize/2; | 
|---|
| 403 | if (margin) { | 
|---|
| 404 | margin->set( pad, pad ); | 
|---|
| 405 | } | 
|---|
| 406 |  | 
|---|
| 407 | dst->fBounds.setLTRB(SkScalarRoundToInt(src.fLeft - pad), | 
|---|
| 408 | SkScalarRoundToInt(src.fTop - pad), | 
|---|
| 409 | SkScalarRoundToInt(src.fRight + pad), | 
|---|
| 410 | SkScalarRoundToInt(src.fBottom + pad)); | 
|---|
| 411 |  | 
|---|
| 412 | dst->fRowBytes = dst->fBounds.width(); | 
|---|
| 413 | dst->fFormat = SkMask::kA8_Format; | 
|---|
| 414 | dst->fImage = nullptr; | 
|---|
| 415 |  | 
|---|
| 416 | int             sw = SkScalarFloorToInt(src.width()); | 
|---|
| 417 | int             sh = SkScalarFloorToInt(src.height()); | 
|---|
| 418 |  | 
|---|
| 419 | if (createMode == SkMask::kJustComputeBounds_CreateMode) { | 
|---|
| 420 | if (style == kInner_SkBlurStyle) { | 
|---|
| 421 | dst->fBounds = src.round(); // restore trimmed bounds | 
|---|
| 422 | dst->fRowBytes = sw; | 
|---|
| 423 | } | 
|---|
| 424 | return true; | 
|---|
| 425 | } | 
|---|
| 426 |  | 
|---|
| 427 | SkAutoTMalloc<uint8_t> profile(profileSize); | 
|---|
| 428 |  | 
|---|
| 429 | ComputeBlurProfile(profile, profileSize, sigma); | 
|---|
| 430 |  | 
|---|
| 431 | size_t dstSize = dst->computeImageSize(); | 
|---|
| 432 | if (0 == dstSize) { | 
|---|
| 433 | return false;   // too big to allocate, abort | 
|---|
| 434 | } | 
|---|
| 435 |  | 
|---|
| 436 | uint8_t*        dp = SkMask::AllocImage(dstSize); | 
|---|
| 437 |  | 
|---|
| 438 | dst->fImage = dp; | 
|---|
| 439 |  | 
|---|
| 440 | int dstHeight = dst->fBounds.height(); | 
|---|
| 441 | int dstWidth = dst->fBounds.width(); | 
|---|
| 442 |  | 
|---|
| 443 | uint8_t *outptr = dp; | 
|---|
| 444 |  | 
|---|
| 445 | SkAutoTMalloc<uint8_t> horizontalScanline(dstWidth); | 
|---|
| 446 | SkAutoTMalloc<uint8_t> verticalScanline(dstHeight); | 
|---|
| 447 |  | 
|---|
| 448 | ComputeBlurredScanline(horizontalScanline, profile, dstWidth, sigma); | 
|---|
| 449 | ComputeBlurredScanline(verticalScanline, profile, dstHeight, sigma); | 
|---|
| 450 |  | 
|---|
| 451 | for (int y = 0 ; y < dstHeight ; ++y) { | 
|---|
| 452 | for (int x = 0 ; x < dstWidth ; x++) { | 
|---|
| 453 | unsigned int maskval = SkMulDiv255Round(horizontalScanline[x], verticalScanline[y]); | 
|---|
| 454 | *(outptr++) = maskval; | 
|---|
| 455 | } | 
|---|
| 456 | } | 
|---|
| 457 |  | 
|---|
| 458 | if (style == kInner_SkBlurStyle) { | 
|---|
| 459 | // now we allocate the "real" dst, mirror the size of src | 
|---|
| 460 | size_t srcSize = (size_t)(src.width() * src.height()); | 
|---|
| 461 | if (0 == srcSize) { | 
|---|
| 462 | return false;   // too big to allocate, abort | 
|---|
| 463 | } | 
|---|
| 464 | dst->fImage = SkMask::AllocImage(srcSize); | 
|---|
| 465 | for (int y = 0 ; y < sh ; y++) { | 
|---|
| 466 | uint8_t *blur_scanline = dp + (y+pad)*dstWidth + pad; | 
|---|
| 467 | uint8_t *inner_scanline = dst->fImage + y*sw; | 
|---|
| 468 | memcpy(inner_scanline, blur_scanline, sw); | 
|---|
| 469 | } | 
|---|
| 470 | SkMask::FreeImage(dp); | 
|---|
| 471 |  | 
|---|
| 472 | dst->fBounds = src.round(); // restore trimmed bounds | 
|---|
| 473 | dst->fRowBytes = sw; | 
|---|
| 474 |  | 
|---|
| 475 | } else if (style == kOuter_SkBlurStyle) { | 
|---|
| 476 | for (int y = pad ; y < dstHeight-pad ; y++) { | 
|---|
| 477 | uint8_t *dst_scanline = dp + y*dstWidth + pad; | 
|---|
| 478 | memset(dst_scanline, 0, sw); | 
|---|
| 479 | } | 
|---|
| 480 | } else if (style == kSolid_SkBlurStyle) { | 
|---|
| 481 | for (int y = pad ; y < dstHeight-pad ; y++) { | 
|---|
| 482 | uint8_t *dst_scanline = dp + y*dstWidth + pad; | 
|---|
| 483 | memset(dst_scanline, 0xff, sw); | 
|---|
| 484 | } | 
|---|
| 485 | } | 
|---|
| 486 | // normal and solid styles are the same for analytic rect blurs, so don't | 
|---|
| 487 | // need to handle solid specially. | 
|---|
| 488 |  | 
|---|
| 489 | return true; | 
|---|
| 490 | } | 
|---|
| 491 |  | 
|---|
| 492 | bool SkBlurMask::BlurRRect(SkScalar sigma, SkMask *dst, | 
|---|
| 493 | const SkRRect &src, SkBlurStyle style, | 
|---|
| 494 | SkIPoint *margin, SkMask::CreateMode createMode) { | 
|---|
| 495 | // Temporary for now -- always fail, should cause caller to fall back | 
|---|
| 496 | // to old path.  Plumbing just to land API and parallelize effort. | 
|---|
| 497 |  | 
|---|
| 498 | return false; | 
|---|
| 499 | } | 
|---|
| 500 |  | 
|---|
| 501 | // The "simple" blur is a direct implementation of separable convolution with a discrete | 
|---|
| 502 | // gaussian kernel.  It's "ground truth" in a sense; too slow to be used, but very | 
|---|
| 503 | // useful for correctness comparisons. | 
|---|
| 504 |  | 
|---|
| 505 | bool SkBlurMask::BlurGroundTruth(SkScalar sigma, SkMask* dst, const SkMask& src, | 
|---|
| 506 | SkBlurStyle style, SkIPoint* margin) { | 
|---|
| 507 |  | 
|---|
| 508 | if (src.fFormat != SkMask::kA8_Format) { | 
|---|
| 509 | return false; | 
|---|
| 510 | } | 
|---|
| 511 |  | 
|---|
| 512 | float variance = sigma * sigma; | 
|---|
| 513 |  | 
|---|
| 514 | int windowSize = SkScalarCeilToInt(sigma*6); | 
|---|
| 515 | // round window size up to nearest odd number | 
|---|
| 516 | windowSize |= 1; | 
|---|
| 517 |  | 
|---|
| 518 | SkAutoTMalloc<float> gaussWindow(windowSize); | 
|---|
| 519 |  | 
|---|
| 520 | int halfWindow = windowSize >> 1; | 
|---|
| 521 |  | 
|---|
| 522 | gaussWindow[halfWindow] = 1; | 
|---|
| 523 |  | 
|---|
| 524 | float windowSum = 1; | 
|---|
| 525 | for (int x = 1 ; x <= halfWindow ; ++x) { | 
|---|
| 526 | float gaussian = expf(-x*x / (2*variance)); | 
|---|
| 527 | gaussWindow[halfWindow + x] = gaussWindow[halfWindow-x] = gaussian; | 
|---|
| 528 | windowSum += 2*gaussian; | 
|---|
| 529 | } | 
|---|
| 530 |  | 
|---|
| 531 | // leave the filter un-normalized for now; we will divide by the normalization | 
|---|
| 532 | // sum later; | 
|---|
| 533 |  | 
|---|
| 534 | int pad = halfWindow; | 
|---|
| 535 | if (margin) { | 
|---|
| 536 | margin->set( pad, pad ); | 
|---|
| 537 | } | 
|---|
| 538 |  | 
|---|
| 539 | dst->fBounds = src.fBounds; | 
|---|
| 540 | dst->fBounds.outset(pad, pad); | 
|---|
| 541 |  | 
|---|
| 542 | dst->fRowBytes = dst->fBounds.width(); | 
|---|
| 543 | dst->fFormat = SkMask::kA8_Format; | 
|---|
| 544 | dst->fImage = nullptr; | 
|---|
| 545 |  | 
|---|
| 546 | if (src.fImage) { | 
|---|
| 547 |  | 
|---|
| 548 | size_t dstSize = dst->computeImageSize(); | 
|---|
| 549 | if (0 == dstSize) { | 
|---|
| 550 | return false;   // too big to allocate, abort | 
|---|
| 551 | } | 
|---|
| 552 |  | 
|---|
| 553 | int             srcWidth = src.fBounds.width(); | 
|---|
| 554 | int             srcHeight = src.fBounds.height(); | 
|---|
| 555 | int             dstWidth = dst->fBounds.width(); | 
|---|
| 556 |  | 
|---|
| 557 | const uint8_t*  srcPixels = src.fImage; | 
|---|
| 558 | uint8_t*        dstPixels = SkMask::AllocImage(dstSize); | 
|---|
| 559 | SkAutoMaskFreeImage autoFreeDstPixels(dstPixels); | 
|---|
| 560 |  | 
|---|
| 561 | // do the actual blur.  First, make a padded copy of the source. | 
|---|
| 562 | // use double pad so we never have to check if we're outside anything | 
|---|
| 563 |  | 
|---|
| 564 | int padWidth = srcWidth + 4*pad; | 
|---|
| 565 | int padHeight = srcHeight; | 
|---|
| 566 | int padSize = padWidth * padHeight; | 
|---|
| 567 |  | 
|---|
| 568 | SkAutoTMalloc<uint8_t> padPixels(padSize); | 
|---|
| 569 | memset(padPixels, 0, padSize); | 
|---|
| 570 |  | 
|---|
| 571 | for (int y = 0 ; y < srcHeight; ++y) { | 
|---|
| 572 | uint8_t* padptr = padPixels + y * padWidth + 2*pad; | 
|---|
| 573 | const uint8_t* srcptr = srcPixels + y * srcWidth; | 
|---|
| 574 | memcpy(padptr, srcptr, srcWidth); | 
|---|
| 575 | } | 
|---|
| 576 |  | 
|---|
| 577 | // blur in X, transposing the result into a temporary floating point buffer. | 
|---|
| 578 | // also double-pad the intermediate result so that the second blur doesn't | 
|---|
| 579 | // have to do extra conditionals. | 
|---|
| 580 |  | 
|---|
| 581 | int tmpWidth = padHeight + 4*pad; | 
|---|
| 582 | int tmpHeight = padWidth - 2*pad; | 
|---|
| 583 | int tmpSize = tmpWidth * tmpHeight; | 
|---|
| 584 |  | 
|---|
| 585 | SkAutoTMalloc<float> tmpImage(tmpSize); | 
|---|
| 586 | memset(tmpImage, 0, tmpSize*sizeof(tmpImage[0])); | 
|---|
| 587 |  | 
|---|
| 588 | for (int y = 0 ; y < padHeight ; ++y) { | 
|---|
| 589 | uint8_t *srcScanline = padPixels + y*padWidth; | 
|---|
| 590 | for (int x = pad ; x < padWidth - pad ; ++x) { | 
|---|
| 591 | float *outPixel = tmpImage + (x-pad)*tmpWidth + y + 2*pad; // transposed output | 
|---|
| 592 | uint8_t *windowCenter = srcScanline + x; | 
|---|
| 593 | for (int i = -pad ; i <= pad ; ++i) { | 
|---|
| 594 | *outPixel += gaussWindow[pad+i]*windowCenter[i]; | 
|---|
| 595 | } | 
|---|
| 596 | *outPixel /= windowSum; | 
|---|
| 597 | } | 
|---|
| 598 | } | 
|---|
| 599 |  | 
|---|
| 600 | // blur in Y; now filling in the actual desired destination.  We have to do | 
|---|
| 601 | // the transpose again; these transposes guarantee that we read memory in | 
|---|
| 602 | // linear order. | 
|---|
| 603 |  | 
|---|
| 604 | for (int y = 0 ; y < tmpHeight ; ++y) { | 
|---|
| 605 | float *srcScanline = tmpImage + y*tmpWidth; | 
|---|
| 606 | for (int x = pad ; x < tmpWidth - pad ; ++x) { | 
|---|
| 607 | float *windowCenter = srcScanline + x; | 
|---|
| 608 | float finalValue = 0; | 
|---|
| 609 | for (int i = -pad ; i <= pad ; ++i) { | 
|---|
| 610 | finalValue += gaussWindow[pad+i]*windowCenter[i]; | 
|---|
| 611 | } | 
|---|
| 612 | finalValue /= windowSum; | 
|---|
| 613 | uint8_t *outPixel = dstPixels + (x-pad)*dstWidth + y; // transposed output | 
|---|
| 614 | int integerPixel = int(finalValue + 0.5f); | 
|---|
| 615 | *outPixel = SkTPin(SkClampPos(integerPixel), 0, 255); | 
|---|
| 616 | } | 
|---|
| 617 | } | 
|---|
| 618 |  | 
|---|
| 619 | dst->fImage = dstPixels; | 
|---|
| 620 | switch (style) { | 
|---|
| 621 | case kNormal_SkBlurStyle: | 
|---|
| 622 | break; | 
|---|
| 623 | case kSolid_SkBlurStyle: { | 
|---|
| 624 | clamp_solid_with_orig( | 
|---|
| 625 | dstPixels + pad*dst->fRowBytes + pad, dst->fRowBytes, | 
|---|
| 626 | SkMask::AlphaIter<SkMask::kA8_Format>(srcPixels), src.fRowBytes, | 
|---|
| 627 | srcWidth, srcHeight); | 
|---|
| 628 | } break; | 
|---|
| 629 | case kOuter_SkBlurStyle: { | 
|---|
| 630 | clamp_outer_with_orig( | 
|---|
| 631 | dstPixels + pad*dst->fRowBytes + pad, dst->fRowBytes, | 
|---|
| 632 | SkMask::AlphaIter<SkMask::kA8_Format>(srcPixels), src.fRowBytes, | 
|---|
| 633 | srcWidth, srcHeight); | 
|---|
| 634 | } break; | 
|---|
| 635 | case kInner_SkBlurStyle: { | 
|---|
| 636 | // now we allocate the "real" dst, mirror the size of src | 
|---|
| 637 | size_t srcSize = src.computeImageSize(); | 
|---|
| 638 | if (0 == srcSize) { | 
|---|
| 639 | return false;   // too big to allocate, abort | 
|---|
| 640 | } | 
|---|
| 641 | dst->fImage = SkMask::AllocImage(srcSize); | 
|---|
| 642 | merge_src_with_blur(dst->fImage, src.fRowBytes, | 
|---|
| 643 | SkMask::AlphaIter<SkMask::kA8_Format>(srcPixels), src.fRowBytes, | 
|---|
| 644 | dstPixels + pad*dst->fRowBytes + pad, | 
|---|
| 645 | dst->fRowBytes, srcWidth, srcHeight); | 
|---|
| 646 | SkMask::FreeImage(dstPixels); | 
|---|
| 647 | } break; | 
|---|
| 648 | } | 
|---|
| 649 | autoFreeDstPixels.release(); | 
|---|
| 650 | } | 
|---|
| 651 |  | 
|---|
| 652 | if (style == kInner_SkBlurStyle) { | 
|---|
| 653 | dst->fBounds = src.fBounds; // restore trimmed bounds | 
|---|
| 654 | dst->fRowBytes = src.fRowBytes; | 
|---|
| 655 | } | 
|---|
| 656 |  | 
|---|
| 657 | return true; | 
|---|
| 658 | } | 
|---|
| 659 |  | 
|---|