/* * Copyright 2006 The Android Open Source Project * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file.
*/
// This constant approximates the scaling done in the software path's // "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)). // IMHO, it actually should be 1: we blur "less" than we should do // according to the CSS and canvas specs, simply because Safari does the same. // Firefox used to do the same too, until 4.0 where they fixed it. So at some // point we should probably get rid of these scaling constants and rebaseline // all the blur tests. staticconst SkScalar kBLUR_SIGMA_SCALE = 0.57735f;
template <typename AlphaIter> staticvoid merge_src_with_blur(uint8_t dst[], int dstRB,
AlphaIter src, int srcRB, const uint8_t blur[], int blurRB, int sw, int sh) {
dstRB -= sw;
blurRB -= sw; while (--sh >= 0) {
AlphaIter rowSrc(src); for (int x = sw - 1; x >= 0; --x) {
*dst = SkToU8(SkAlphaMul(*blur, SkAlpha255To256(*rowSrc)));
++dst;
++rowSrc;
++blur;
}
dst += dstRB;
src >>= srcRB;
blur += blurRB;
}
}
template <typename AlphaIter> staticvoid clamp_solid_with_orig(uint8_t dst[], int dstRowBytes,
AlphaIter src, int srcRowBytes, int sw, int sh) { int x; while (--sh >= 0) {
AlphaIter rowSrc(src); for (x = sw - 1; x >= 0; --x) { int s = *rowSrc; int d = *dst;
*dst = SkToU8(s + d - SkMulDiv255Round(s, d));
++dst;
++rowSrc;
}
dst += dstRowBytes - sw;
src >>= srcRowBytes;
}
}
template <typename AlphaIter> staticvoid clamp_outer_with_orig(uint8_t dst[], int dstRowBytes,
AlphaIter src, int srcRowBytes, int sw, int sh) { int x; while (--sh >= 0) {
AlphaIter rowSrc(src); for (x = sw - 1; x >= 0; --x) { int srcValue = *rowSrc; if (srcValue) {
*dst = SkToU8(SkAlphaMul(*dst, SkAlpha255To256(255 - srcValue)));
}
++dst;
++rowSrc;
}
dst += dstRowBytes - sw;
src >>= srcRowBytes;
}
} ///////////////////////////////////////////////////////////////////////////////
// we use a local function to wrap the class static method to work around // a bug in gcc98 void SkMask_FreeImage(uint8_t* image); void SkMask_FreeImage(uint8_t* image) {
SkMaskBuilder::FreeImage(image);
}
SkMaskBlurFilter blurFilter{sigma, sigma}; if (blurFilter.hasNoBlur()) { // If there is no effective blur most styles will just produce the original mask. // However, kOuter_SkBlurStyle will produce an empty mask. if (style == kOuter_SkBlurStyle) {
dst->image() = nullptr;
dst->bounds() = SkIRect::MakeEmpty();
dst->rowBytes() = dst->fBounds.width();
dst->format() = SkMask::kA8_Format; if (margin != nullptr) { // This filter will disregard the src.fImage completely. // The margin is actually {-(src.fBounds.width() / 2), -(src.fBounds.height() / 2)} // but it is not clear if callers will fall over with negative margins.
*margin = SkIPoint{0,0};
} returntrue;
} returnfalse;
} const SkIPoint border = blurFilter.blur(src, dst); // If src.fImage is null, then this call is only to calculate the border. if (src.fImage != nullptr && dst->fImage == nullptr) { returnfalse;
}
/* ComputeBlurProfile fills in an array of floating point values between 0 and 255 for the profile signature of a blurred half-plane with the given blur radius. Since we're going to be doing screened multiplications (i.e., 1 - (1-x)(1-y)) all the time, we actually fill in the profile pre-inverted (already done 255-x).
*/
profile[0] = 255; for (int x = 1 ; x < size ; ++x) { float scaled_x = (center - x - .5f) * invr; float gi = gaussianIntegral(scaled_x);
profile[x] = 255 - (uint8_t) (255.f * gi);
}
}
// TODO MAYBE: Maintain a profile cache to avoid recomputing this for // commonly used radii. Consider baking some of the most common blur radii // directly in as static data?
uint8_t SkBlurMask::ProfileLookup(const uint8_t *profile, int loc, int blurredWidth, int sharpWidth) { // how far are we from the original edge? int dx = SkAbs32(((loc << 1) + 1) - blurredWidth) - sharpWidth; int ox = dx >> 1; if (ox < 0) {
ox = 0;
}
unsignedint sw = width - profile_size; // nearest odd number less than the profile size represents the center // of the (2x scaled) profile int center = ( profile_size & ~1 ) - 1;
for (int y = 0 ; y < dstHeight ; ++y) { for (int x = 0 ; x < dstWidth ; x++) { unsignedint maskval = SkMulDiv255Round(horizontalScanline[x], verticalScanline[y]);
*(outptr++) = maskval;
}
}
if (style == kInner_SkBlurStyle) { // now we allocate the "real" dst, mirror the size of src
size_t srcSize = (size_t)(src.width() * src.height()); if (0 == srcSize) { returnfalse; // too big to allocate, abort
}
dst->image() = SkMaskBuilder::AllocImage(srcSize); for (int y = 0 ; y < sh ; y++) {
uint8_t *blur_scanline = dp + (y+pad)*dstWidth + pad;
uint8_t *inner_scanline = dst->image() + y*sw;
memcpy(inner_scanline, blur_scanline, sw);
}
SkMaskBuilder::FreeImage(dp);
} elseif (style == kOuter_SkBlurStyle) { for (int y = pad ; y < dstHeight-pad ; y++) {
uint8_t *dst_scanline = dp + y*dstWidth + pad;
memset(dst_scanline, 0, sw);
}
} elseif (style == kSolid_SkBlurStyle) { for (int y = pad ; y < dstHeight-pad ; y++) {
uint8_t *dst_scanline = dp + y*dstWidth + pad;
memset(dst_scanline, 0xff, sw);
}
} // normal and solid styles are the same for analytic rect blurs, so don't // need to handle solid specially.
returntrue;
}
bool SkBlurMask::BlurRRect(SkScalar sigma, SkMaskBuilder *dst, const SkRRect &src, SkBlurStyle style,
SkIPoint *margin, SkMaskBuilder::CreateMode createMode) { // Temporary for now -- always fail, should cause caller to fall back // to old path. Plumbing just to land API and parallelize effort.
returnfalse;
}
// The "simple" blur is a direct implementation of separable convolution with a discrete // gaussian kernel. It's "ground truth" in a sense; too slow to be used, but very // useful for correctness comparisons.
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;
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 = SkTPin(SkClampPos(integerPixel), 0, 255);
}
}
dst->image() = dstPixels; switch (style) { case kNormal_SkBlurStyle: break; case kSolid_SkBlurStyle: {
clamp_solid_with_orig(
dstPixels + pad*dst->fRowBytes + pad, dst->fRowBytes,
SkMask::AlphaIter<SkMask::kA8_Format>(srcPixels), src.fRowBytes,
srcWidth, srcHeight);
} break; case kOuter_SkBlurStyle: {
clamp_outer_with_orig(
dstPixels + pad*dst->fRowBytes + pad, dst->fRowBytes,
SkMask::AlphaIter<SkMask::kA8_Format>(srcPixels), src.fRowBytes,
srcWidth, srcHeight);
} break; case kInner_SkBlurStyle: { // now we allocate the "real" dst, mirror the size of src
size_t srcSize = src.computeImageSize(); if (0 == srcSize) { returnfalse; // too big to allocate, abort
}
dst->image() = SkMaskBuilder::AllocImage(srcSize);
merge_src_with_blur(dst->image(), src.fRowBytes,
SkMask::AlphaIter<SkMask::kA8_Format>(srcPixels), src.fRowBytes,
dstPixels + pad*dst->fRowBytes + pad,
dst->fRowBytes, srcWidth, srcHeight);
SkMaskBuilder::FreeImage(dstPixels);
} break;
}
autoFreeDstPixels.release();
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.