bool SkBlurMask::BlurGroundTruth(SkMask* dst, const SkMask& src, SkScalar provided_radius, Style style, SkIPoint* margin) { if (src.fFormat != SkMask::kA8_Format) { return false; } float radius = SkScalarToFloat(SkScalarMul(provided_radius, kBlurRadiusFudgeFactor)); float stddev = SkScalarToFloat(radius) /2.0f; float variance = stddev * stddev; int windowSize = SkScalarCeil(stddev*4); // round window size up to nearest odd number windowSize |= 1; SkAutoTMalloc<float> gaussWindow(windowSize); int halfWindow = windowSize >> 1; gaussWindow[halfWindow] = 1; float windowSum = 1; for (int x = 1 ; x <= halfWindow ; ++x) { float gaussian = expf(-x*x / variance); gaussWindow[halfWindow + x] = gaussWindow[halfWindow-x] = gaussian; windowSum += 2*gaussian; } // leave the filter un-normalized for now; we will divide by the normalization // sum later; int pad = halfWindow; if (margin) { margin->set( pad, pad ); } dst->fBounds = src.fBounds; dst->fBounds.outset(pad, pad); dst->fRowBytes = dst->fBounds.width(); dst->fFormat = SkMask::kA8_Format; dst->fImage = NULL; if (src.fImage) { size_t dstSize = dst->computeImageSize(); if (0 == dstSize) { return false; // too big to allocate, abort } int srcWidth = src.fBounds.width(); int srcHeight = src.fBounds.height(); int dstWidth = dst->fBounds.width(); const uint8_t* srcPixels = src.fImage; uint8_t* dstPixels = SkMask::AllocImage(dstSize); SkAutoTCallVProc<uint8_t, SkMask_FreeImage> autoCall(dstPixels); // do the actual blur. First, make a padded copy of the source. // use double pad so we never have to check if we're outside anything int padWidth = srcWidth + 4*pad; int padHeight = srcHeight; int padSize = padWidth * padHeight; SkAutoTMalloc<uint8_t> padPixels(padSize); memset(padPixels, 0, padSize); for (int y = 0 ; y < srcHeight; ++y) { uint8_t* padptr = padPixels + y * padWidth + 2*pad; const uint8_t* srcptr = srcPixels + y * srcWidth; memcpy(padptr, srcptr, srcWidth); } // blur in X, transposing the result into a temporary floating point buffer. // also double-pad the intermediate result so that the second blur doesn't // have to do extra conditionals. int tmpWidth = padHeight + 4*pad; int tmpHeight = padWidth - 2*pad; int tmpSize = tmpWidth * tmpHeight; SkAutoTMalloc<float> tmpImage(tmpSize); memset(tmpImage, 0, tmpSize*sizeof(tmpImage[0])); for (int y = 0 ; y < padHeight ; ++y) { uint8_t *srcScanline = padPixels + y*padWidth; for (int x = pad ; x < padWidth - pad ; ++x) { float *outPixel = tmpImage + (x-pad)*tmpWidth + y + 2*pad; // transposed output uint8_t *windowCenter = srcScanline + x; for (int i = -pad ; i <= pad ; ++i) { *outPixel += gaussWindow[pad+i]*windowCenter[i]; } *outPixel /= windowSum; } } // blur in Y; now filling in the actual desired destination. We have to do // the transpose again; these transposes guarantee that we read memory in // linear order. for (int y = 0 ; y < tmpHeight ; ++y) { float *srcScanline = tmpImage + y*tmpWidth; for (int x = pad ; x < tmpWidth - pad ; ++x) { float *windowCenter = srcScanline + x; float finalValue = 0; for (int i = -pad ; i <= pad ; ++i) { finalValue += gaussWindow[pad+i]*windowCenter[i]; } finalValue /= windowSum; uint8_t *outPixel = dstPixels + (x-pad)*dstWidth + y; // transposed output int integerPixel = int(finalValue + 0.5f); *outPixel = SkClampMax( SkClampPos(integerPixel), 255 ); } } dst->fImage = dstPixels; // if need be, alloc the "real" dst (same size as src) and copy/merge // the blur into it (applying the src) if (style == kInner_Style) { // now we allocate the "real" dst, mirror the size of src size_t srcSize = src.computeImageSize(); if (0 == srcSize) { return false; // too big to allocate, abort } dst->fImage = SkMask::AllocImage(srcSize); merge_src_with_blur(dst->fImage, src.fRowBytes, srcPixels, src.fRowBytes, dstPixels + pad*dst->fRowBytes + pad, dst->fRowBytes, srcWidth, srcHeight); SkMask::FreeImage(dstPixels); } else if (style != kNormal_Style) { clamp_with_orig(dstPixels + pad*dst->fRowBytes + pad, dst->fRowBytes, srcPixels, src.fRowBytes, srcWidth, srcHeight, style); } (void)autoCall.detach(); } if (style == kInner_Style) { dst->fBounds = src.fBounds; // restore trimmed bounds dst->fRowBytes = src.fRowBytes; } return true; }
bool SkBlurMask::BoxBlur(SkMask* dst, const SkMask& src, SkScalar sigma, SkBlurStyle style, SkBlurQuality quality, SkIPoint* margin, bool force_quality) { if (src.fFormat != SkMask::kA8_Format) { return false; } SkIPoint border; #ifdef SK_SUPPORT_LEGACY_MASK_BLUR auto get_adjusted_radii = [](SkScalar passRadius, int *loRadius, int *hiRadius) { *loRadius = *hiRadius = SkScalarCeilToInt(passRadius); if (SkIntToScalar(*hiRadius) - passRadius > 0.5f) { *loRadius = *hiRadius - 1; } }; // Force high quality off for small radii (performance) if (!force_quality && sigma <= SkIntToScalar(2)) { quality = kLow_SkBlurQuality; } SkScalar passRadius; if (kHigh_SkBlurQuality == quality) { // For the high quality path the 3 pass box blur kernel width is // 6*rad+1 while the full Gaussian width is 6*sigma. passRadius = sigma - (1 / 6.0f); } else { // For the low quality path we only attempt to cover 3*sigma of the // Gaussian blur area (1.5*sigma on each side). The single pass box // blur's kernel size is 2*rad+1. passRadius = 1.5f * sigma - 0.5f; } // highQuality: use three box blur passes as a cheap way // to approximate a Gaussian blur int passCount = (kHigh_SkBlurQuality == quality) ? 3 : 1; int rx = SkScalarCeilToInt(passRadius); int outerWeight = 255 - SkScalarRoundToInt((SkIntToScalar(rx) - passRadius) * 255); SkASSERT(rx >= 0); SkASSERT((unsigned)outerWeight <= 255); if (rx <= 0) { return false; } int ry = rx; // only do square blur for now int padx = passCount * rx; int pady = passCount * ry; border = {padx, pady}; dst->fBounds.set(src.fBounds.fLeft - padx, src.fBounds.fTop - pady, src.fBounds.fRight + padx, src.fBounds.fBottom + pady); dst->fRowBytes = dst->fBounds.width(); dst->fFormat = SkMask::kA8_Format; dst->fImage = nullptr; if (src.fImage) { size_t dstSize = dst->computeImageSize(); if (0 == dstSize) { return false; // too big to allocate, abort } int sw = src.fBounds.width(); int sh = src.fBounds.height(); const uint8_t* sp = src.fImage; uint8_t* dp = SkMask::AllocImage(dstSize); SkAutoTCallVProc<uint8_t, SkMask_FreeImage> autoCall(dp); // build the blurry destination SkAutoTMalloc<uint8_t> tmpBuffer(dstSize); uint8_t* tp = tmpBuffer.get(); int w = sw, h = sh; if (outerWeight == 255) { int loRadius, hiRadius; get_adjusted_radii(passRadius, &loRadius, &hiRadius); if (kHigh_SkBlurQuality == quality) { // Do three X blurs, with a transpose on the final one. w = boxBlur<false>(sp, src.fRowBytes, tp, loRadius, hiRadius, w, h); w = boxBlur<false>(tp, w, dp, hiRadius, loRadius, w, h); w = boxBlur<true>(dp, w, tp, hiRadius, hiRadius, w, h); // Do three Y blurs, with a transpose on the final one. h = boxBlur<false>(tp, h, dp, loRadius, hiRadius, h, w); h = boxBlur<false>(dp, h, tp, hiRadius, loRadius, h, w); h = boxBlur<true>(tp, h, dp, hiRadius, hiRadius, h, w); } else { w = boxBlur<true>(sp, src.fRowBytes, tp, rx, rx, w, h); h = boxBlur<true>(tp, h, dp, ry, ry, h, w); } } else { if (kHigh_SkBlurQuality == quality) { // Do three X blurs, with a transpose on the final one. w = boxBlurInterp<false>(sp, src.fRowBytes, tp, rx, w, h, outerWeight); w = boxBlurInterp<false>(tp, w, dp, rx, w, h, outerWeight); w = boxBlurInterp<true>(dp, w, tp, rx, w, h, outerWeight); // Do three Y blurs, with a transpose on the final one. h = boxBlurInterp<false>(tp, h, dp, ry, h, w, outerWeight); h = boxBlurInterp<false>(dp, h, tp, ry, h, w, outerWeight); h = boxBlurInterp<true>(tp, h, dp, ry, h, w, outerWeight); } else { w = boxBlurInterp<true>(sp, src.fRowBytes, tp, rx, w, h, outerWeight); h = boxBlurInterp<true>(tp, h, dp, ry, h, w, outerWeight); } } dst->fImage = autoCall.release(); } #else SkMaskBlurFilter blurFilter{sigma, sigma}; border = blurFilter.blur(src, dst); #endif // SK_SUPPORT_LEGACY_MASK_BLUR if (src.fImage != nullptr) { // if need be, alloc the "real" dst (same size as src) and copy/merge // the blur into it (applying the src) if (style == kInner_SkBlurStyle) { // now we allocate the "real" dst, mirror the size of src size_t srcSize = src.computeImageSize(); if (0 == srcSize) { return false; // too big to allocate, abort } auto blur = dst->fImage; dst->fImage = SkMask::AllocImage(srcSize); auto blurStart = &blur[border.x() + border.y() * dst->fRowBytes]; merge_src_with_blur(dst->fImage, src.fRowBytes, src.fImage, src.fRowBytes, blurStart, dst->fRowBytes, src.fBounds.width(), src.fBounds.height()); SkMask::FreeImage(blur); } else if (style != kNormal_SkBlurStyle) { auto dstStart = &dst->fImage[border.x() + border.y() * dst->fRowBytes]; clamp_with_orig(dstStart, dst->fRowBytes, src.fImage, src.fRowBytes, src.fBounds.width(), src.fBounds.height(), style); } } if (style == kInner_SkBlurStyle) { dst->fBounds = src.fBounds; // restore trimmed bounds dst->fRowBytes = src.fRowBytes; } if (margin != nullptr) { *margin = border; } return true; }
bool SkBlurMask::Blur(SkMask* dst, const SkMask& src, SkScalar radius, Style style, Quality quality, SkIPoint* margin) { if (src.fFormat != SkMask::kA8_Format) { return false; } // Force high quality off for small radii (performance) if (radius < SkIntToScalar(3)) { quality = kLow_Quality; } // highQuality: use three box blur passes as a cheap way // to approximate a Gaussian blur int passCount = (kHigh_Quality == quality) ? 3 : 1; SkScalar passRadius = (kHigh_Quality == quality) ? SkScalarMul( radius, kBlurRadiusFudgeFactor): radius; int rx = SkScalarCeil(passRadius); int outerWeight = 255 - SkScalarRound((SkIntToScalar(rx) - passRadius) * 255); SkASSERT(rx >= 0); SkASSERT((unsigned)outerWeight <= 255); if (rx <= 0) { return false; } int ry = rx; // only do square blur for now int padx = passCount * rx; int pady = passCount * ry; if (margin) { margin->set(padx, pady); } dst->fBounds.set(src.fBounds.fLeft - padx, src.fBounds.fTop - pady, src.fBounds.fRight + padx, src.fBounds.fBottom + pady); dst->fRowBytes = dst->fBounds.width(); dst->fFormat = SkMask::kA8_Format; dst->fImage = NULL; if (src.fImage) { size_t dstSize = dst->computeImageSize(); if (0 == dstSize) { return false; // too big to allocate, abort } int sw = src.fBounds.width(); int sh = src.fBounds.height(); const uint8_t* sp = src.fImage; uint8_t* dp = SkMask::AllocImage(dstSize); SkAutoTCallVProc<uint8_t, SkMask_FreeImage> autoCall(dp); // build the blurry destination SkAutoTMalloc<uint8_t> tmpBuffer(dstSize); uint8_t* tp = tmpBuffer.get(); int w = sw, h = sh; if (outerWeight == 255) { int loRadius, hiRadius; get_adjusted_radii(passRadius, &loRadius, &hiRadius); if (kHigh_Quality == quality) { // Do three X blurs, with a transpose on the final one. w = boxBlur(sp, src.fRowBytes, tp, loRadius, hiRadius, w, h, false); w = boxBlur(tp, w, dp, hiRadius, loRadius, w, h, false); w = boxBlur(dp, w, tp, hiRadius, hiRadius, w, h, true); // Do three Y blurs, with a transpose on the final one. h = boxBlur(tp, h, dp, loRadius, hiRadius, h, w, false); h = boxBlur(dp, h, tp, hiRadius, loRadius, h, w, false); h = boxBlur(tp, h, dp, hiRadius, hiRadius, h, w, true); } else { w = boxBlur(sp, src.fRowBytes, tp, rx, rx, w, h, true); h = boxBlur(tp, h, dp, ry, ry, h, w, true); } } else { if (kHigh_Quality == quality) { // Do three X blurs, with a transpose on the final one. w = boxBlurInterp(sp, src.fRowBytes, tp, rx, w, h, false, outerWeight); w = boxBlurInterp(tp, w, dp, rx, w, h, false, outerWeight); w = boxBlurInterp(dp, w, tp, rx, w, h, true, outerWeight); // Do three Y blurs, with a transpose on the final one. h = boxBlurInterp(tp, h, dp, ry, h, w, false, outerWeight); h = boxBlurInterp(dp, h, tp, ry, h, w, false, outerWeight); h = boxBlurInterp(tp, h, dp, ry, h, w, true, outerWeight); } else { w = boxBlurInterp(sp, src.fRowBytes, tp, rx, w, h, true, outerWeight); h = boxBlurInterp(tp, h, dp, ry, h, w, true, outerWeight); } } dst->fImage = dp; // if need be, alloc the "real" dst (same size as src) and copy/merge // the blur into it (applying the src) if (style == kInner_Style) { // now we allocate the "real" dst, mirror the size of src size_t srcSize = src.computeImageSize(); if (0 == srcSize) { return false; // too big to allocate, abort } dst->fImage = SkMask::AllocImage(srcSize); merge_src_with_blur(dst->fImage, src.fRowBytes, sp, src.fRowBytes, dp + passCount * (rx + ry * dst->fRowBytes), dst->fRowBytes, sw, sh); SkMask::FreeImage(dp); } else if (style != kNormal_Style) { clamp_with_orig(dp + passCount * (rx + ry * dst->fRowBytes), dst->fRowBytes, sp, src.fRowBytes, sw, sh, style); } (void)autoCall.detach(); } if (style == kInner_Style) { dst->fBounds = src.fBounds; // restore trimmed bounds dst->fRowBytes = src.fRowBytes; } return true; }
bool SkBlurMask::Blur(SkMask* dst, const SkMask& src, SkScalar radius, Style style, Quality quality, SkIPoint* margin) { if (src.fFormat != SkMask::kA8_Format) { return false; } // Force high quality off for small radii (performance) if (radius < SkIntToScalar(3)) quality = kLow_Quality; // highQuality: use three box blur passes as a cheap way to approximate a Gaussian blur int passCount = (quality == kHigh_Quality) ? 3 : 1; SkScalar passRadius = SkScalarDiv(radius, SkScalarSqrt(SkIntToScalar(passCount))); int rx = SkScalarCeil(passRadius); int outer_weight = 255 - SkScalarRound((SkIntToScalar(rx) - passRadius) * 255); SkASSERT(rx >= 0); SkASSERT((unsigned)outer_weight <= 255); if (rx <= 0) { return false; } int ry = rx; // only do square blur for now int padx = passCount * rx; int pady = passCount * ry; if (margin) { margin->set(padx, pady); } dst->fBounds.set(src.fBounds.fLeft - padx, src.fBounds.fTop - pady, src.fBounds.fRight + padx, src.fBounds.fBottom + pady); dst->fRowBytes = dst->fBounds.width(); dst->fFormat = SkMask::kA8_Format; dst->fImage = NULL; if (src.fImage) { size_t dstSize = dst->computeImageSize(); if (0 == dstSize) { return false; // too big to allocate, abort } int sw = src.fBounds.width(); int sh = src.fBounds.height(); const uint8_t* sp = src.fImage; uint8_t* dp = SkMask::AllocImage(dstSize); SkAutoTCallVProc<uint8_t, SkMask_FreeImage> autoCall(dp); // build the blurry destination { const size_t storageW = sw + 2 * (passCount - 1) * rx + 1; const size_t storageH = sh + 2 * (passCount - 1) * ry + 1; SkAutoTMalloc<uint32_t> storage(storageW * storageH); uint32_t* sumBuffer = storage.get(); //pass1: sp is source, dp is destination build_sum_buffer(sumBuffer, sw, sh, sp, src.fRowBytes); if (outer_weight == 255) { apply_kernel(dp, rx, ry, sumBuffer, sw, sh); } else { apply_kernel_interp(dp, rx, ry, sumBuffer, sw, sh, outer_weight); } if (quality == kHigh_Quality) { //pass2: dp is source, tmpBuffer is destination int tmp_sw = sw + 2 * rx; int tmp_sh = sh + 2 * ry; SkAutoTMalloc<uint8_t> tmpBuffer(dstSize); build_sum_buffer(sumBuffer, tmp_sw, tmp_sh, dp, tmp_sw); if (outer_weight == 255) apply_kernel(tmpBuffer.get(), rx, ry, sumBuffer, tmp_sw, tmp_sh); else apply_kernel_interp(tmpBuffer.get(), rx, ry, sumBuffer, tmp_sw, tmp_sh, outer_weight); //pass3: tmpBuffer is source, dp is destination tmp_sw += 2 * rx; tmp_sh += 2 * ry; build_sum_buffer(sumBuffer, tmp_sw, tmp_sh, tmpBuffer.get(), tmp_sw); if (outer_weight == 255) apply_kernel(dp, rx, ry, sumBuffer, tmp_sw, tmp_sh); else apply_kernel_interp(dp, rx, ry, sumBuffer, tmp_sw, tmp_sh, outer_weight); } } dst->fImage = dp; // if need be, alloc the "real" dst (same size as src) and copy/merge // the blur into it (applying the src) if (style == kInner_Style) { // now we allocate the "real" dst, mirror the size of src size_t srcSize = src.computeImageSize(); if (0 == srcSize) { return false; // too big to allocate, abort } dst->fImage = SkMask::AllocImage(srcSize); merge_src_with_blur(dst->fImage, src.fRowBytes, sp, src.fRowBytes, dp + passCount * (rx + ry * dst->fRowBytes), dst->fRowBytes, sw, sh); SkMask::FreeImage(dp); } else if (style != kNormal_Style) { clamp_with_orig(dp + passCount * (rx + ry * dst->fRowBytes), dst->fRowBytes, sp, src.fRowBytes, sw, sh, style); } (void)autoCall.detach(); } if (style == kInner_Style) { dst->fBounds = src.fBounds; // restore trimmed bounds dst->fRowBytes = src.fRowBytes; } return true; }