michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #include "SkBlurMask.h" michael@0: #include "SkMath.h" michael@0: #include "SkTemplates.h" michael@0: #include "SkEndian.h" michael@0: michael@0: michael@0: SkScalar SkBlurMask::ConvertRadiusToSigma(SkScalar radius) { michael@0: // This constant approximates the scaling done in the software path's michael@0: // "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)). michael@0: // IMHO, it actually should be 1: we blur "less" than we should do michael@0: // according to the CSS and canvas specs, simply because Safari does the same. michael@0: // Firefox used to do the same too, until 4.0 where they fixed it. So at some michael@0: // point we should probably get rid of these scaling constants and rebaseline michael@0: // all the blur tests. michael@0: static const SkScalar kBLUR_SIGMA_SCALE = 0.57735f; michael@0: michael@0: return radius ? kBLUR_SIGMA_SCALE * radius + 0.5f : 0.0f; michael@0: } michael@0: michael@0: #define UNROLL_SEPARABLE_LOOPS michael@0: michael@0: /** michael@0: * This function performs a box blur in X, of the given radius. If the michael@0: * "transpose" parameter is true, it will transpose the pixels on write, michael@0: * such that X and Y are swapped. Reads are always performed from contiguous michael@0: * memory in X, for speed. The destination buffer (dst) must be at least michael@0: * (width + leftRadius + rightRadius) * height bytes in size. michael@0: * michael@0: * This is what the inner loop looks like before unrolling, and with the two michael@0: * cases broken out separately (width < diameter, width >= diameter): michael@0: * michael@0: * if (width < diameter) { michael@0: * for (int x = 0; x < width; ++x) { michael@0: * sum += *right++; michael@0: * *dptr = (sum * scale + half) >> 24; michael@0: * dptr += dst_x_stride; michael@0: * } michael@0: * for (int x = width; x < diameter; ++x) { michael@0: * *dptr = (sum * scale + half) >> 24; michael@0: * dptr += dst_x_stride; michael@0: * } michael@0: * for (int x = 0; x < width; ++x) { michael@0: * *dptr = (sum * scale + half) >> 24; michael@0: * sum -= *left++; michael@0: * dptr += dst_x_stride; michael@0: * } michael@0: * } else { michael@0: * for (int x = 0; x < diameter; ++x) { michael@0: * sum += *right++; michael@0: * *dptr = (sum * scale + half) >> 24; michael@0: * dptr += dst_x_stride; michael@0: * } michael@0: * for (int x = diameter; x < width; ++x) { michael@0: * sum += *right++; michael@0: * *dptr = (sum * scale + half) >> 24; michael@0: * sum -= *left++; michael@0: * dptr += dst_x_stride; michael@0: * } michael@0: * for (int x = 0; x < diameter; ++x) { michael@0: * *dptr = (sum * scale + half) >> 24; michael@0: * sum -= *left++; michael@0: * dptr += dst_x_stride; michael@0: * } michael@0: * } michael@0: */ michael@0: static int boxBlur(const uint8_t* src, int src_y_stride, uint8_t* dst, michael@0: int leftRadius, int rightRadius, int width, int height, michael@0: bool transpose) michael@0: { michael@0: int diameter = leftRadius + rightRadius; michael@0: int kernelSize = diameter + 1; michael@0: int border = SkMin32(width, diameter); michael@0: uint32_t scale = (1 << 24) / kernelSize; michael@0: int new_width = width + SkMax32(leftRadius, rightRadius) * 2; michael@0: int dst_x_stride = transpose ? height : 1; michael@0: int dst_y_stride = transpose ? 1 : new_width; michael@0: uint32_t half = 1 << 23; michael@0: for (int y = 0; y < height; ++y) { michael@0: uint32_t sum = 0; michael@0: uint8_t* dptr = dst + y * dst_y_stride; michael@0: const uint8_t* right = src + y * src_y_stride; michael@0: const uint8_t* left = right; michael@0: for (int x = 0; x < rightRadius - leftRadius; x++) { michael@0: *dptr = 0; michael@0: dptr += dst_x_stride; michael@0: } michael@0: #define LEFT_BORDER_ITER \ michael@0: sum += *right++; \ michael@0: *dptr = (sum * scale + half) >> 24; \ michael@0: dptr += dst_x_stride; michael@0: michael@0: int x = 0; michael@0: #ifdef UNROLL_SEPARABLE_LOOPS michael@0: for (; x < border - 16; x += 16) { michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: } michael@0: #endif michael@0: for (; x < border; ++x) { michael@0: LEFT_BORDER_ITER michael@0: } michael@0: #undef LEFT_BORDER_ITER michael@0: #define TRIVIAL_ITER \ michael@0: *dptr = (sum * scale + half) >> 24; \ michael@0: dptr += dst_x_stride; michael@0: x = width; michael@0: #ifdef UNROLL_SEPARABLE_LOOPS michael@0: for (; x < diameter - 16; x += 16) { michael@0: TRIVIAL_ITER michael@0: TRIVIAL_ITER michael@0: TRIVIAL_ITER michael@0: TRIVIAL_ITER michael@0: TRIVIAL_ITER michael@0: TRIVIAL_ITER michael@0: TRIVIAL_ITER michael@0: TRIVIAL_ITER michael@0: TRIVIAL_ITER michael@0: TRIVIAL_ITER michael@0: TRIVIAL_ITER michael@0: TRIVIAL_ITER michael@0: TRIVIAL_ITER michael@0: TRIVIAL_ITER michael@0: TRIVIAL_ITER michael@0: TRIVIAL_ITER michael@0: } michael@0: #endif michael@0: for (; x < diameter; ++x) { michael@0: TRIVIAL_ITER michael@0: } michael@0: #undef TRIVIAL_ITER michael@0: #define CENTER_ITER \ michael@0: sum += *right++; \ michael@0: *dptr = (sum * scale + half) >> 24; \ michael@0: sum -= *left++; \ michael@0: dptr += dst_x_stride; michael@0: michael@0: x = diameter; michael@0: #ifdef UNROLL_SEPARABLE_LOOPS michael@0: for (; x < width - 16; x += 16) { michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: } michael@0: #endif michael@0: for (; x < width; ++x) { michael@0: CENTER_ITER michael@0: } michael@0: #undef CENTER_ITER michael@0: #define RIGHT_BORDER_ITER \ michael@0: *dptr = (sum * scale + half) >> 24; \ michael@0: sum -= *left++; \ michael@0: dptr += dst_x_stride; michael@0: michael@0: x = 0; michael@0: #ifdef UNROLL_SEPARABLE_LOOPS michael@0: for (; x < border - 16; x += 16) { michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: } michael@0: #endif michael@0: for (; x < border; ++x) { michael@0: RIGHT_BORDER_ITER michael@0: } michael@0: #undef RIGHT_BORDER_ITER michael@0: for (int x = 0; x < leftRadius - rightRadius; ++x) { michael@0: *dptr = 0; michael@0: dptr += dst_x_stride; michael@0: } michael@0: SkASSERT(sum == 0); michael@0: } michael@0: return new_width; michael@0: } michael@0: michael@0: /** michael@0: * This variant of the box blur handles blurring of non-integer radii. It michael@0: * keeps two running sums: an outer sum for the rounded-up kernel radius, and michael@0: * an inner sum for the rounded-down kernel radius. For each pixel, it linearly michael@0: * interpolates between them. In float this would be: michael@0: * outer_weight * outer_sum / kernelSize + michael@0: * (1.0 - outer_weight) * innerSum / (kernelSize - 2) michael@0: * michael@0: * This is what the inner loop looks like before unrolling, and with the two michael@0: * cases broken out separately (width < diameter, width >= diameter): michael@0: * michael@0: * if (width < diameter) { michael@0: * for (int x = 0; x < width; x++) { michael@0: * inner_sum = outer_sum; michael@0: * outer_sum += *right++; michael@0: * *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; michael@0: * dptr += dst_x_stride; michael@0: * } michael@0: * for (int x = width; x < diameter; ++x) { michael@0: * *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; michael@0: * dptr += dst_x_stride; michael@0: * } michael@0: * for (int x = 0; x < width; x++) { michael@0: * inner_sum = outer_sum - *left++; michael@0: * *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; michael@0: * dptr += dst_x_stride; michael@0: * outer_sum = inner_sum; michael@0: * } michael@0: * } else { michael@0: * for (int x = 0; x < diameter; x++) { michael@0: * inner_sum = outer_sum; michael@0: * outer_sum += *right++; michael@0: * *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; michael@0: * dptr += dst_x_stride; michael@0: * } michael@0: * for (int x = diameter; x < width; ++x) { michael@0: * inner_sum = outer_sum - *left; michael@0: * outer_sum += *right++; michael@0: * *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; michael@0: * dptr += dst_x_stride; michael@0: * outer_sum -= *left++; michael@0: * } michael@0: * for (int x = 0; x < diameter; x++) { michael@0: * inner_sum = outer_sum - *left++; michael@0: * *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; michael@0: * dptr += dst_x_stride; michael@0: * outer_sum = inner_sum; michael@0: * } michael@0: * } michael@0: * } michael@0: * return new_width; michael@0: */ michael@0: michael@0: static int boxBlurInterp(const uint8_t* src, int src_y_stride, uint8_t* dst, michael@0: int radius, int width, int height, michael@0: bool transpose, uint8_t outer_weight) michael@0: { michael@0: int diameter = radius * 2; michael@0: int kernelSize = diameter + 1; michael@0: int border = SkMin32(width, diameter); michael@0: int inner_weight = 255 - outer_weight; michael@0: outer_weight += outer_weight >> 7; michael@0: inner_weight += inner_weight >> 7; michael@0: uint32_t outer_scale = (outer_weight << 16) / kernelSize; michael@0: uint32_t inner_scale = (inner_weight << 16) / (kernelSize - 2); michael@0: uint32_t half = 1 << 23; michael@0: int new_width = width + diameter; michael@0: int dst_x_stride = transpose ? height : 1; michael@0: int dst_y_stride = transpose ? 1 : new_width; michael@0: for (int y = 0; y < height; ++y) { michael@0: uint32_t outer_sum = 0, inner_sum = 0; michael@0: uint8_t* dptr = dst + y * dst_y_stride; michael@0: const uint8_t* right = src + y * src_y_stride; michael@0: const uint8_t* left = right; michael@0: int x = 0; michael@0: michael@0: #define LEFT_BORDER_ITER \ michael@0: inner_sum = outer_sum; \ michael@0: outer_sum += *right++; \ michael@0: *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; \ michael@0: dptr += dst_x_stride; michael@0: michael@0: #ifdef UNROLL_SEPARABLE_LOOPS michael@0: for (;x < border - 16; x += 16) { michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: LEFT_BORDER_ITER michael@0: } michael@0: #endif michael@0: michael@0: for (;x < border; ++x) { michael@0: LEFT_BORDER_ITER michael@0: } michael@0: #undef LEFT_BORDER_ITER michael@0: for (int x = width; x < diameter; ++x) { michael@0: *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; michael@0: dptr += dst_x_stride; michael@0: } michael@0: x = diameter; michael@0: michael@0: #define CENTER_ITER \ michael@0: inner_sum = outer_sum - *left; \ michael@0: outer_sum += *right++; \ michael@0: *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; \ michael@0: dptr += dst_x_stride; \ michael@0: outer_sum -= *left++; michael@0: michael@0: #ifdef UNROLL_SEPARABLE_LOOPS michael@0: for (; x < width - 16; x += 16) { michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: CENTER_ITER michael@0: } michael@0: #endif michael@0: for (; x < width; ++x) { michael@0: CENTER_ITER michael@0: } michael@0: #undef CENTER_ITER michael@0: michael@0: #define RIGHT_BORDER_ITER \ michael@0: inner_sum = outer_sum - *left++; \ michael@0: *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; \ michael@0: dptr += dst_x_stride; \ michael@0: outer_sum = inner_sum; michael@0: michael@0: x = 0; michael@0: #ifdef UNROLL_SEPARABLE_LOOPS michael@0: for (; x < border - 16; x += 16) { michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: RIGHT_BORDER_ITER michael@0: } michael@0: #endif michael@0: for (; x < border; ++x) { michael@0: RIGHT_BORDER_ITER michael@0: } michael@0: #undef RIGHT_BORDER_ITER michael@0: SkASSERT(outer_sum == 0 && inner_sum == 0); michael@0: } michael@0: return new_width; michael@0: } michael@0: michael@0: static void get_adjusted_radii(SkScalar passRadius, int *loRadius, int *hiRadius) michael@0: { michael@0: *loRadius = *hiRadius = SkScalarCeilToInt(passRadius); michael@0: if (SkIntToScalar(*hiRadius) - passRadius > 0.5f) { michael@0: *loRadius = *hiRadius - 1; michael@0: } michael@0: } michael@0: michael@0: #include "SkColorPriv.h" michael@0: michael@0: static void merge_src_with_blur(uint8_t dst[], int dstRB, michael@0: const uint8_t src[], int srcRB, michael@0: const uint8_t blur[], int blurRB, michael@0: int sw, int sh) { michael@0: dstRB -= sw; michael@0: srcRB -= sw; michael@0: blurRB -= sw; michael@0: while (--sh >= 0) { michael@0: for (int x = sw - 1; x >= 0; --x) { michael@0: *dst = SkToU8(SkAlphaMul(*blur, SkAlpha255To256(*src))); michael@0: dst += 1; michael@0: src += 1; michael@0: blur += 1; michael@0: } michael@0: dst += dstRB; michael@0: src += srcRB; michael@0: blur += blurRB; michael@0: } michael@0: } michael@0: michael@0: static void clamp_with_orig(uint8_t dst[], int dstRowBytes, michael@0: const uint8_t src[], int srcRowBytes, michael@0: int sw, int sh, michael@0: SkBlurMask::Style style) { michael@0: int x; michael@0: while (--sh >= 0) { michael@0: switch (style) { michael@0: case SkBlurMask::kSolid_Style: michael@0: for (x = sw - 1; x >= 0; --x) { michael@0: int s = *src; michael@0: int d = *dst; michael@0: *dst = SkToU8(s + d - SkMulDiv255Round(s, d)); michael@0: dst += 1; michael@0: src += 1; michael@0: } michael@0: break; michael@0: case SkBlurMask::kOuter_Style: michael@0: for (x = sw - 1; x >= 0; --x) { michael@0: if (*src) { michael@0: *dst = SkToU8(SkAlphaMul(*dst, SkAlpha255To256(255 - *src))); michael@0: } michael@0: dst += 1; michael@0: src += 1; michael@0: } michael@0: break; michael@0: default: michael@0: SkDEBUGFAIL("Unexpected blur style here"); michael@0: break; michael@0: } michael@0: dst += dstRowBytes - sw; michael@0: src += srcRowBytes - sw; michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: // we use a local function to wrap the class static method to work around michael@0: // a bug in gcc98 michael@0: void SkMask_FreeImage(uint8_t* image); michael@0: void SkMask_FreeImage(uint8_t* image) { michael@0: SkMask::FreeImage(image); michael@0: } michael@0: michael@0: bool SkBlurMask::BoxBlur(SkMask* dst, const SkMask& src, michael@0: SkScalar sigma, Style style, Quality quality, michael@0: SkIPoint* margin) { michael@0: michael@0: if (src.fFormat != SkMask::kA8_Format) { michael@0: return false; michael@0: } michael@0: michael@0: // Force high quality off for small radii (performance) michael@0: if (sigma <= SkIntToScalar(2)) { michael@0: quality = kLow_Quality; michael@0: } michael@0: michael@0: SkScalar passRadius; michael@0: if (kHigh_Quality == quality) { michael@0: // For the high quality path the 3 pass box blur kernel width is michael@0: // 6*rad+1 while the full Gaussian width is 6*sigma. michael@0: passRadius = sigma - (1/6.0f); michael@0: } else { michael@0: // For the low quality path we only attempt to cover 3*sigma of the michael@0: // Gaussian blur area (1.5*sigma on each side). The single pass box michael@0: // blur's kernel size is 2*rad+1. michael@0: passRadius = 1.5f*sigma - 0.5f; michael@0: } michael@0: michael@0: // highQuality: use three box blur passes as a cheap way michael@0: // to approximate a Gaussian blur michael@0: int passCount = (kHigh_Quality == quality) ? 3 : 1; michael@0: michael@0: int rx = SkScalarCeilToInt(passRadius); michael@0: int outerWeight = 255 - SkScalarRoundToInt((SkIntToScalar(rx) - passRadius) * 255); michael@0: michael@0: SkASSERT(rx >= 0); michael@0: SkASSERT((unsigned)outerWeight <= 255); michael@0: if (rx <= 0) { michael@0: return false; michael@0: } michael@0: michael@0: int ry = rx; // only do square blur for now michael@0: michael@0: int padx = passCount * rx; michael@0: int pady = passCount * ry; michael@0: michael@0: if (margin) { michael@0: margin->set(padx, pady); michael@0: } michael@0: dst->fBounds.set(src.fBounds.fLeft - padx, src.fBounds.fTop - pady, michael@0: src.fBounds.fRight + padx, src.fBounds.fBottom + pady); michael@0: michael@0: dst->fRowBytes = dst->fBounds.width(); michael@0: dst->fFormat = SkMask::kA8_Format; michael@0: dst->fImage = NULL; michael@0: michael@0: if (src.fImage) { michael@0: size_t dstSize = dst->computeImageSize(); michael@0: if (0 == dstSize) { michael@0: return false; // too big to allocate, abort michael@0: } michael@0: michael@0: int sw = src.fBounds.width(); michael@0: int sh = src.fBounds.height(); michael@0: const uint8_t* sp = src.fImage; michael@0: uint8_t* dp = SkMask::AllocImage(dstSize); michael@0: SkAutoTCallVProc autoCall(dp); michael@0: michael@0: // build the blurry destination michael@0: SkAutoTMalloc tmpBuffer(dstSize); michael@0: uint8_t* tp = tmpBuffer.get(); michael@0: int w = sw, h = sh; michael@0: michael@0: if (outerWeight == 255) { michael@0: int loRadius, hiRadius; michael@0: get_adjusted_radii(passRadius, &loRadius, &hiRadius); michael@0: if (kHigh_Quality == quality) { michael@0: // Do three X blurs, with a transpose on the final one. michael@0: w = boxBlur(sp, src.fRowBytes, tp, loRadius, hiRadius, w, h, false); michael@0: w = boxBlur(tp, w, dp, hiRadius, loRadius, w, h, false); michael@0: w = boxBlur(dp, w, tp, hiRadius, hiRadius, w, h, true); michael@0: // Do three Y blurs, with a transpose on the final one. michael@0: h = boxBlur(tp, h, dp, loRadius, hiRadius, h, w, false); michael@0: h = boxBlur(dp, h, tp, hiRadius, loRadius, h, w, false); michael@0: h = boxBlur(tp, h, dp, hiRadius, hiRadius, h, w, true); michael@0: } else { michael@0: w = boxBlur(sp, src.fRowBytes, tp, rx, rx, w, h, true); michael@0: h = boxBlur(tp, h, dp, ry, ry, h, w, true); michael@0: } michael@0: } else { michael@0: if (kHigh_Quality == quality) { michael@0: // Do three X blurs, with a transpose on the final one. michael@0: w = boxBlurInterp(sp, src.fRowBytes, tp, rx, w, h, false, outerWeight); michael@0: w = boxBlurInterp(tp, w, dp, rx, w, h, false, outerWeight); michael@0: w = boxBlurInterp(dp, w, tp, rx, w, h, true, outerWeight); michael@0: // Do three Y blurs, with a transpose on the final one. michael@0: h = boxBlurInterp(tp, h, dp, ry, h, w, false, outerWeight); michael@0: h = boxBlurInterp(dp, h, tp, ry, h, w, false, outerWeight); michael@0: h = boxBlurInterp(tp, h, dp, ry, h, w, true, outerWeight); michael@0: } else { michael@0: w = boxBlurInterp(sp, src.fRowBytes, tp, rx, w, h, true, outerWeight); michael@0: h = boxBlurInterp(tp, h, dp, ry, h, w, true, outerWeight); michael@0: } michael@0: } michael@0: michael@0: dst->fImage = dp; michael@0: // if need be, alloc the "real" dst (same size as src) and copy/merge michael@0: // the blur into it (applying the src) michael@0: if (style == kInner_Style) { michael@0: // now we allocate the "real" dst, mirror the size of src michael@0: size_t srcSize = src.computeImageSize(); michael@0: if (0 == srcSize) { michael@0: return false; // too big to allocate, abort michael@0: } michael@0: dst->fImage = SkMask::AllocImage(srcSize); michael@0: merge_src_with_blur(dst->fImage, src.fRowBytes, michael@0: sp, src.fRowBytes, michael@0: dp + passCount * (rx + ry * dst->fRowBytes), michael@0: dst->fRowBytes, sw, sh); michael@0: SkMask::FreeImage(dp); michael@0: } else if (style != kNormal_Style) { michael@0: clamp_with_orig(dp + passCount * (rx + ry * dst->fRowBytes), michael@0: dst->fRowBytes, sp, src.fRowBytes, sw, sh, style); michael@0: } michael@0: (void)autoCall.detach(); michael@0: } michael@0: michael@0: if (style == kInner_Style) { michael@0: dst->fBounds = src.fBounds; // restore trimmed bounds michael@0: dst->fRowBytes = src.fRowBytes; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: /* Convolving a box with itself three times results in a piecewise michael@0: quadratic function: michael@0: michael@0: 0 x <= -1.5 michael@0: 9/8 + 3/2 x + 1/2 x^2 -1.5 < x <= -.5 michael@0: 3/4 - x^2 -.5 < x <= .5 michael@0: 9/8 - 3/2 x + 1/2 x^2 0.5 < x <= 1.5 michael@0: 0 1.5 < x michael@0: michael@0: Mathematica: michael@0: michael@0: g[x_] := Piecewise [ { michael@0: {9/8 + 3/2 x + 1/2 x^2 , -1.5 < x <= -.5}, michael@0: {3/4 - x^2 , -.5 < x <= .5}, michael@0: {9/8 - 3/2 x + 1/2 x^2 , 0.5 < x <= 1.5} michael@0: }, 0] michael@0: michael@0: To get the profile curve of the blurred step function at the rectangle michael@0: edge, we evaluate the indefinite integral, which is piecewise cubic: michael@0: michael@0: 0 x <= -1.5 michael@0: 9/16 + 9/8 x + 3/4 x^2 + 1/6 x^3 -1.5 < x <= -0.5 michael@0: 1/2 + 3/4 x - 1/3 x^3 -.5 < x <= .5 michael@0: 7/16 + 9/8 x - 3/4 x^2 + 1/6 x^3 .5 < x <= 1.5 michael@0: 1 1.5 < x michael@0: michael@0: in Mathematica code: michael@0: michael@0: gi[x_] := Piecewise[ { michael@0: { 0 , x <= -1.5 }, michael@0: { 9/16 + 9/8 x + 3/4 x^2 + 1/6 x^3, -1.5 < x <= -0.5 }, michael@0: { 1/2 + 3/4 x - 1/3 x^3 , -.5 < x <= .5}, michael@0: { 7/16 + 9/8 x - 3/4 x^2 + 1/6 x^3, .5 < x <= 1.5} michael@0: },1] michael@0: */ michael@0: michael@0: static float gaussianIntegral(float x) { michael@0: if (x > 1.5f) { michael@0: return 0.0f; michael@0: } michael@0: if (x < -1.5f) { michael@0: return 1.0f; michael@0: } michael@0: michael@0: float x2 = x*x; michael@0: float x3 = x2*x; michael@0: michael@0: if ( x > 0.5f ) { michael@0: return 0.5625f - (x3 / 6.0f - 3.0f * x2 * 0.25f + 1.125f * x); michael@0: } michael@0: if ( x > -0.5f ) { michael@0: return 0.5f - (0.75f * x - x3 / 3.0f); michael@0: } michael@0: return 0.4375f + (-x3 / 6.0f - 3.0f * x2 * 0.25f - 1.125f * x); michael@0: } michael@0: michael@0: /* ComputeBlurProfile allocates and fills in an array of floating michael@0: point values between 0 and 255 for the profile signature of michael@0: a blurred half-plane with the given blur radius. Since we're michael@0: going to be doing screened multiplications (i.e., 1 - (1-x)(1-y)) michael@0: all the time, we actually fill in the profile pre-inverted michael@0: (already done 255-x). michael@0: michael@0: It's the responsibility of the caller to delete the michael@0: memory returned in profile_out. michael@0: */ michael@0: michael@0: void SkBlurMask::ComputeBlurProfile(SkScalar sigma, uint8_t **profile_out) { michael@0: int size = SkScalarCeilToInt(6*sigma); michael@0: michael@0: int center = size >> 1; michael@0: uint8_t *profile = SkNEW_ARRAY(uint8_t, size); michael@0: michael@0: float invr = 1.f/(2*sigma); michael@0: michael@0: profile[0] = 255; michael@0: for (int x = 1 ; x < size ; ++x) { michael@0: float scaled_x = (center - x - .5f) * invr; michael@0: float gi = gaussianIntegral(scaled_x); michael@0: profile[x] = 255 - (uint8_t) (255.f * gi); michael@0: } michael@0: michael@0: *profile_out = profile; michael@0: } michael@0: michael@0: // TODO MAYBE: Maintain a profile cache to avoid recomputing this for michael@0: // commonly used radii. Consider baking some of the most common blur radii michael@0: // directly in as static data? michael@0: michael@0: // Implementation adapted from Michael Herf's approach: michael@0: // http://stereopsis.com/shadowrect/ michael@0: michael@0: uint8_t SkBlurMask::ProfileLookup(const uint8_t *profile, int loc, int blurred_width, int sharp_width) { michael@0: int dx = SkAbs32(((loc << 1) + 1) - blurred_width) - sharp_width; // how far are we from the original edge? michael@0: int ox = dx >> 1; michael@0: if (ox < 0) { michael@0: ox = 0; michael@0: } michael@0: michael@0: return profile[ox]; michael@0: } michael@0: michael@0: void SkBlurMask::ComputeBlurredScanline(uint8_t *pixels, const uint8_t *profile, michael@0: unsigned int width, SkScalar sigma) { michael@0: michael@0: unsigned int profile_size = SkScalarCeilToInt(6*sigma); michael@0: SkAutoTMalloc horizontalScanline(width); michael@0: michael@0: unsigned int sw = width - profile_size; michael@0: // nearest odd number less than the profile size represents the center michael@0: // of the (2x scaled) profile michael@0: int center = ( profile_size & ~1 ) - 1; michael@0: michael@0: int w = sw - center; michael@0: michael@0: for (unsigned int x = 0 ; x < width ; ++x) { michael@0: if (profile_size <= sw) { michael@0: pixels[x] = ProfileLookup(profile, x, width, w); michael@0: } else { michael@0: float span = float(sw)/(2*sigma); michael@0: float giX = 1.5f - (x+.5f)/(2*sigma); michael@0: pixels[x] = (uint8_t) (255 * (gaussianIntegral(giX) - gaussianIntegral(giX + span))); michael@0: } michael@0: } michael@0: } michael@0: michael@0: bool SkBlurMask::BlurRect(SkScalar sigma, SkMask *dst, michael@0: const SkRect &src, Style style, michael@0: SkIPoint *margin, SkMask::CreateMode createMode) { michael@0: int profile_size = SkScalarCeilToInt(6*sigma); michael@0: michael@0: int pad = profile_size/2; michael@0: if (margin) { michael@0: margin->set( pad, pad ); michael@0: } michael@0: michael@0: dst->fBounds.set(SkScalarRoundToInt(src.fLeft - pad), michael@0: SkScalarRoundToInt(src.fTop - pad), michael@0: SkScalarRoundToInt(src.fRight + pad), michael@0: SkScalarRoundToInt(src.fBottom + pad)); michael@0: michael@0: dst->fRowBytes = dst->fBounds.width(); michael@0: dst->fFormat = SkMask::kA8_Format; michael@0: dst->fImage = NULL; michael@0: michael@0: int sw = SkScalarFloorToInt(src.width()); michael@0: int sh = SkScalarFloorToInt(src.height()); michael@0: michael@0: if (createMode == SkMask::kJustComputeBounds_CreateMode) { michael@0: if (style == kInner_Style) { michael@0: dst->fBounds.set(SkScalarRoundToInt(src.fLeft), michael@0: SkScalarRoundToInt(src.fTop), michael@0: SkScalarRoundToInt(src.fRight), michael@0: SkScalarRoundToInt(src.fBottom)); // restore trimmed bounds michael@0: dst->fRowBytes = sw; michael@0: } michael@0: return true; michael@0: } michael@0: uint8_t *profile = NULL; michael@0: michael@0: ComputeBlurProfile(sigma, &profile); michael@0: SkAutoTDeleteArray ada(profile); michael@0: michael@0: size_t dstSize = dst->computeImageSize(); michael@0: if (0 == dstSize) { michael@0: return false; // too big to allocate, abort michael@0: } michael@0: michael@0: uint8_t* dp = SkMask::AllocImage(dstSize); michael@0: michael@0: dst->fImage = dp; michael@0: michael@0: int dstHeight = dst->fBounds.height(); michael@0: int dstWidth = dst->fBounds.width(); michael@0: michael@0: uint8_t *outptr = dp; michael@0: michael@0: SkAutoTMalloc horizontalScanline(dstWidth); michael@0: SkAutoTMalloc verticalScanline(dstHeight); michael@0: michael@0: ComputeBlurredScanline(horizontalScanline, profile, dstWidth, sigma); michael@0: ComputeBlurredScanline(verticalScanline, profile, dstHeight, sigma); michael@0: michael@0: for (int y = 0 ; y < dstHeight ; ++y) { michael@0: for (int x = 0 ; x < dstWidth ; x++) { michael@0: unsigned int maskval = SkMulDiv255Round(horizontalScanline[x], verticalScanline[y]); michael@0: *(outptr++) = maskval; michael@0: } michael@0: } michael@0: michael@0: if (style == kInner_Style) { michael@0: // now we allocate the "real" dst, mirror the size of src michael@0: size_t srcSize = (size_t)(src.width() * src.height()); michael@0: if (0 == srcSize) { michael@0: return false; // too big to allocate, abort michael@0: } michael@0: dst->fImage = SkMask::AllocImage(srcSize); michael@0: for (int y = 0 ; y < sh ; y++) { michael@0: uint8_t *blur_scanline = dp + (y+pad)*dstWidth + pad; michael@0: uint8_t *inner_scanline = dst->fImage + y*sw; michael@0: memcpy(inner_scanline, blur_scanline, sw); michael@0: } michael@0: SkMask::FreeImage(dp); michael@0: michael@0: dst->fBounds.set(SkScalarRoundToInt(src.fLeft), michael@0: SkScalarRoundToInt(src.fTop), michael@0: SkScalarRoundToInt(src.fRight), michael@0: SkScalarRoundToInt(src.fBottom)); // restore trimmed bounds michael@0: dst->fRowBytes = sw; michael@0: michael@0: } else if (style == kOuter_Style) { michael@0: for (int y = pad ; y < dstHeight-pad ; y++) { michael@0: uint8_t *dst_scanline = dp + y*dstWidth + pad; michael@0: memset(dst_scanline, 0, sw); michael@0: } michael@0: } else if (style == kSolid_Style) { michael@0: for (int y = pad ; y < dstHeight-pad ; y++) { michael@0: uint8_t *dst_scanline = dp + y*dstWidth + pad; michael@0: memset(dst_scanline, 0xff, sw); michael@0: } michael@0: } michael@0: // normal and solid styles are the same for analytic rect blurs, so don't michael@0: // need to handle solid specially. michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool SkBlurMask::BlurRRect(SkScalar sigma, SkMask *dst, michael@0: const SkRRect &src, Style style, michael@0: SkIPoint *margin, SkMask::CreateMode createMode) { michael@0: // Temporary for now -- always fail, should cause caller to fall back michael@0: // to old path. Plumbing just to land API and parallelize effort. michael@0: michael@0: return false; michael@0: } michael@0: michael@0: // The "simple" blur is a direct implementation of separable convolution with a discrete michael@0: // gaussian kernel. It's "ground truth" in a sense; too slow to be used, but very michael@0: // useful for correctness comparisons. michael@0: michael@0: bool SkBlurMask::BlurGroundTruth(SkScalar sigma, SkMask* dst, const SkMask& src, michael@0: Style style, SkIPoint* margin) { michael@0: michael@0: if (src.fFormat != SkMask::kA8_Format) { michael@0: return false; michael@0: } michael@0: michael@0: float variance = sigma * sigma; michael@0: michael@0: int windowSize = SkScalarCeilToInt(sigma*6); michael@0: // round window size up to nearest odd number michael@0: windowSize |= 1; michael@0: michael@0: SkAutoTMalloc gaussWindow(windowSize); michael@0: michael@0: int halfWindow = windowSize >> 1; michael@0: michael@0: gaussWindow[halfWindow] = 1; michael@0: michael@0: float windowSum = 1; michael@0: for (int x = 1 ; x <= halfWindow ; ++x) { michael@0: float gaussian = expf(-x*x / (2*variance)); michael@0: gaussWindow[halfWindow + x] = gaussWindow[halfWindow-x] = gaussian; michael@0: windowSum += 2*gaussian; michael@0: } michael@0: michael@0: // leave the filter un-normalized for now; we will divide by the normalization michael@0: // sum later; michael@0: michael@0: int pad = halfWindow; michael@0: if (margin) { michael@0: margin->set( pad, pad ); michael@0: } michael@0: michael@0: dst->fBounds = src.fBounds; michael@0: dst->fBounds.outset(pad, pad); michael@0: michael@0: dst->fRowBytes = dst->fBounds.width(); michael@0: dst->fFormat = SkMask::kA8_Format; michael@0: dst->fImage = NULL; michael@0: michael@0: if (src.fImage) { michael@0: michael@0: size_t dstSize = dst->computeImageSize(); michael@0: if (0 == dstSize) { michael@0: return false; // too big to allocate, abort michael@0: } michael@0: michael@0: int srcWidth = src.fBounds.width(); michael@0: int srcHeight = src.fBounds.height(); michael@0: int dstWidth = dst->fBounds.width(); michael@0: michael@0: const uint8_t* srcPixels = src.fImage; michael@0: uint8_t* dstPixels = SkMask::AllocImage(dstSize); michael@0: SkAutoTCallVProc autoCall(dstPixels); michael@0: michael@0: // do the actual blur. First, make a padded copy of the source. michael@0: // use double pad so we never have to check if we're outside anything michael@0: michael@0: int padWidth = srcWidth + 4*pad; michael@0: int padHeight = srcHeight; michael@0: int padSize = padWidth * padHeight; michael@0: michael@0: SkAutoTMalloc padPixels(padSize); michael@0: memset(padPixels, 0, padSize); michael@0: michael@0: for (int y = 0 ; y < srcHeight; ++y) { michael@0: uint8_t* padptr = padPixels + y * padWidth + 2*pad; michael@0: const uint8_t* srcptr = srcPixels + y * srcWidth; michael@0: memcpy(padptr, srcptr, srcWidth); michael@0: } michael@0: michael@0: // blur in X, transposing the result into a temporary floating point buffer. michael@0: // also double-pad the intermediate result so that the second blur doesn't michael@0: // have to do extra conditionals. michael@0: michael@0: int tmpWidth = padHeight + 4*pad; michael@0: int tmpHeight = padWidth - 2*pad; michael@0: int tmpSize = tmpWidth * tmpHeight; michael@0: michael@0: SkAutoTMalloc tmpImage(tmpSize); michael@0: memset(tmpImage, 0, tmpSize*sizeof(tmpImage[0])); michael@0: michael@0: for (int y = 0 ; y < padHeight ; ++y) { michael@0: uint8_t *srcScanline = padPixels + y*padWidth; michael@0: for (int x = pad ; x < padWidth - pad ; ++x) { michael@0: float *outPixel = tmpImage + (x-pad)*tmpWidth + y + 2*pad; // transposed output michael@0: uint8_t *windowCenter = srcScanline + x; michael@0: for (int i = -pad ; i <= pad ; ++i) { michael@0: *outPixel += gaussWindow[pad+i]*windowCenter[i]; michael@0: } michael@0: *outPixel /= windowSum; michael@0: } michael@0: } michael@0: michael@0: // blur in Y; now filling in the actual desired destination. We have to do michael@0: // the transpose again; these transposes guarantee that we read memory in michael@0: // linear order. michael@0: michael@0: for (int y = 0 ; y < tmpHeight ; ++y) { michael@0: float *srcScanline = tmpImage + y*tmpWidth; michael@0: for (int x = pad ; x < tmpWidth - pad ; ++x) { michael@0: float *windowCenter = srcScanline + x; michael@0: float finalValue = 0; michael@0: for (int i = -pad ; i <= pad ; ++i) { michael@0: finalValue += gaussWindow[pad+i]*windowCenter[i]; michael@0: } michael@0: finalValue /= windowSum; michael@0: uint8_t *outPixel = dstPixels + (x-pad)*dstWidth + y; // transposed output michael@0: int integerPixel = int(finalValue + 0.5f); michael@0: *outPixel = SkClampMax( SkClampPos(integerPixel), 255 ); michael@0: } michael@0: } michael@0: michael@0: dst->fImage = dstPixels; michael@0: // if need be, alloc the "real" dst (same size as src) and copy/merge michael@0: // the blur into it (applying the src) michael@0: if (style == kInner_Style) { michael@0: // now we allocate the "real" dst, mirror the size of src michael@0: size_t srcSize = src.computeImageSize(); michael@0: if (0 == srcSize) { michael@0: return false; // too big to allocate, abort michael@0: } michael@0: dst->fImage = SkMask::AllocImage(srcSize); michael@0: merge_src_with_blur(dst->fImage, src.fRowBytes, michael@0: srcPixels, src.fRowBytes, michael@0: dstPixels + pad*dst->fRowBytes + pad, michael@0: dst->fRowBytes, srcWidth, srcHeight); michael@0: SkMask::FreeImage(dstPixels); michael@0: } else if (style != kNormal_Style) { michael@0: clamp_with_orig(dstPixels + pad*dst->fRowBytes + pad, michael@0: dst->fRowBytes, srcPixels, src.fRowBytes, srcWidth, srcHeight, style); michael@0: } michael@0: (void)autoCall.detach(); michael@0: } michael@0: michael@0: if (style == kInner_Style) { michael@0: dst->fBounds = src.fBounds; // restore trimmed bounds michael@0: dst->fRowBytes = src.fRowBytes; michael@0: } michael@0: michael@0: return true; michael@0: }