From d64d8a905a044204c5960e29cdf8adf1f130edce Mon Sep 17 00:00:00 2001 From: "humper@google.com" Date: Fri, 12 Jul 2013 21:14:33 +0000 Subject: Working plumb of image scaling: 1) always generate mipmaps if we detect that we are downsampling. 2) pre-scale the image if we detect that we are upsampling (currently valid for scale+translate only) 3) A few miscellaneous bug fixes related to image scaling. Still need SSE/Neon versions of these image scalers. BUG= R=bsalomon@google.com, robertphillips@google.com Review URL: https://codereview.chromium.org/18978014 git-svn-id: http://skia.googlecode.com/svn/trunk@10056 2bbb7eff-a529-9590-31e7-b0007b416f81 --- src/core/SkBitmapFilter.cpp | 260 +++++++++++++++++++------------------------- 1 file changed, 109 insertions(+), 151 deletions(-) (limited to 'src/core/SkBitmapFilter.cpp') diff --git a/src/core/SkBitmapFilter.cpp b/src/core/SkBitmapFilter.cpp index 3cbafd743c..91c2beae40 100644 --- a/src/core/SkBitmapFilter.cpp +++ b/src/core/SkBitmapFilter.cpp @@ -22,28 +22,28 @@ void highQualityFilter(const SkBitmapProcState& s, int x, int y, while (count-- > 0) { SkPoint srcPt; - s.fInvProc(*s.fInvMatrix, SkFloatToScalar(x + 0.5f), + s.fInvProc(s.fInvMatrix, SkFloatToScalar(x + 0.5f), SkFloatToScalar(y + 0.5f), &srcPt); srcPt.fX -= SK_ScalarHalf; srcPt.fY -= SK_ScalarHalf; - SkFixed weight = 0; - SkFixed fr = 0, fg = 0, fb = 0, fa = 0; + SkScalar weight = 0; + SkScalar fr = 0, fg = 0, fb = 0, fa = 0; - int y0 = SkClampMax(sk_float_ceil2int(SkScalarToFloat(srcPt.fY)-s.getBitmapFilter()->width()), maxY); - int y1 = SkClampMax(sk_float_floor2int(SkScalarToFloat(srcPt.fY)+s.getBitmapFilter()->width()), maxY); - int x0 = SkClampMax(sk_float_ceil2int(SkScalarToFloat(srcPt.fX)-s.getBitmapFilter()->width()), maxX); - int x1 = SkClampMax(sk_float_floor2int(SkScalarToFloat(srcPt.fX)+s.getBitmapFilter()->width()), maxX); + int y0 = SkClampMax(SkScalarCeilToInt(srcPt.fY-s.getBitmapFilter()->width()), maxY); + int y1 = SkClampMax(SkScalarFloorToInt(srcPt.fY+s.getBitmapFilter()->width()), maxY); + int x0 = SkClampMax(SkScalarCeilToInt(srcPt.fX-s.getBitmapFilter()->width()), maxX); + int x1 = SkClampMax(SkScalarFloorToInt(srcPt.fX+s.getBitmapFilter()->width()), maxX); - for (int src_y = y0; src_y <= y1; src_y++) { - SkFixed yweight = s.getBitmapFilter()->lookup((srcPt.fY - src_y)); + for (int srcY = y0; srcY <= y1; srcY++) { + SkScalar yWeight = s.getBitmapFilter()->lookupScalar((srcPt.fY - srcY)); - for (int src_x = x0; src_x <= x1 ; src_x++) { - SkFixed xweight = s.getBitmapFilter()->lookup((srcPt.fX - src_x)); + for (int srcX = x0; srcX <= x1 ; srcX++) { + SkScalar xWeight = s.getBitmapFilter()->lookupScalar((srcPt.fX - srcX)); - SkFixed combined_weight = SkFixedMul(xweight, yweight); + SkScalar combined_weight = SkScalarMul(xWeight, yWeight); - SkPMColor c = *s.fBitmap->getAddr32(src_x, src_y); + SkPMColor c = *s.fBitmap->getAddr32(srcX, srcY); fr += combined_weight * SkGetPackedR32(c); fg += combined_weight * SkGetPackedG32(c); fb += combined_weight * SkGetPackedB32(c); @@ -52,15 +52,15 @@ void highQualityFilter(const SkBitmapProcState& s, int x, int y, } } - fr = SkFixedDiv(fr, weight); - fg = SkFixedDiv(fg, weight); - fb = SkFixedDiv(fb, weight); - fa = SkFixedDiv(fa, weight); + fr = SkScalarDiv(fr, weight); + fg = SkScalarDiv(fg, weight); + fb = SkScalarDiv(fb, weight); + fa = SkScalarDiv(fa, weight); - int a = SkClampMax(SkFixedRoundToInt(fa), 255); - int r = SkClampMax(SkFixedRoundToInt(fr), a); - int g = SkClampMax(SkFixedRoundToInt(fg), a); - int b = SkClampMax(SkFixedRoundToInt(fb), a); + int a = SkClampMax(SkScalarRoundToInt(fa), 255); + int r = SkClampMax(SkScalarRoundToInt(fr), a); + int g = SkClampMax(SkScalarRoundToInt(fg), a); + int b = SkClampMax(SkScalarRoundToInt(fb), a); *colors++ = SkPackARGB32(a, r, g, b); @@ -75,33 +75,33 @@ void highQualityFilter_ScaleOnly(const SkBitmapProcState &s, int x, int y, SkPoint srcPt; - s.fInvProc(*s.fInvMatrix, SkFloatToScalar(x + 0.5f), + s.fInvProc(s.fInvMatrix, SkFloatToScalar(x + 0.5f), SkFloatToScalar(y + 0.5f), &srcPt); srcPt.fY -= SK_ScalarHalf; - int y0 = SkClampMax(sk_float_ceil2int(SkScalarToFloat(srcPt.fY)-s.getBitmapFilter()->width()), maxY); - int y1 = SkClampMax(sk_float_floor2int(SkScalarToFloat(srcPt.fY)+s.getBitmapFilter()->width()), maxY); + int y0 = SkClampMax(SkScalarCeilToInt(srcPt.fY-s.getBitmapFilter()->width()), maxY); + int y1 = SkClampMax(SkScalarFloorToInt(srcPt.fY+s.getBitmapFilter()->width()), maxY); while (count-- > 0) { - s.fInvProc(*s.fInvMatrix, SkFloatToScalar(x + 0.5f), + s.fInvProc(s.fInvMatrix, SkFloatToScalar(x + 0.5f), SkFloatToScalar(y + 0.5f), &srcPt); srcPt.fX -= SK_ScalarHalf; srcPt.fY -= SK_ScalarHalf; - SkFixed weight = 0; - SkFixed fr = 0, fg = 0, fb = 0, fa = 0; + SkScalar weight = 0; + SkScalar fr = 0, fg = 0, fb = 0, fa = 0; - int x0 = SkClampMax(sk_float_ceil2int(SkScalarToFloat(srcPt.fX)-s.getBitmapFilter()->width()), maxX); - int x1 = SkClampMax(sk_float_floor2int(SkScalarToFloat(srcPt.fX)+s.getBitmapFilter()->width()), maxX); + int x0 = SkClampMax(SkScalarCeilToInt(srcPt.fX-s.getBitmapFilter()->width()), maxX); + int x1 = SkClampMax(SkScalarFloorToInt(srcPt.fX+s.getBitmapFilter()->width()), maxX); - for (int src_y = y0; src_y <= y1; src_y++) { - SkFixed yweight = s.getBitmapFilter()->lookup((srcPt.fY - src_y)); + for (int srcY = y0; srcY <= y1; srcY++) { + SkScalar yWeight = s.getBitmapFilter()->lookupScalar((srcPt.fY - srcY)); - for (int src_x = x0; src_x <= x1 ; src_x++) { - SkFixed xweight = s.getBitmapFilter()->lookup((srcPt.fX - src_x)); + for (int srcX = x0; srcX <= x1 ; srcX++) { + SkScalar xWeight = s.getBitmapFilter()->lookupScalar((srcPt.fX - srcX)); - SkFixed combined_weight = SkFixedMul(xweight, yweight); + SkScalar combined_weight = SkScalarMul(xWeight, yWeight); - SkPMColor c = *s.fBitmap->getAddr32(src_x, src_y); + SkPMColor c = *s.fBitmap->getAddr32(srcX, srcY); fr += combined_weight * SkGetPackedR32(c); fg += combined_weight * SkGetPackedG32(c); fb += combined_weight * SkGetPackedB32(c); @@ -110,15 +110,15 @@ void highQualityFilter_ScaleOnly(const SkBitmapProcState &s, int x, int y, } } - fr = SkFixedDiv(fr, weight); - fg = SkFixedDiv(fg, weight); - fb = SkFixedDiv(fb, weight); - fa = SkFixedDiv(fa, weight); + fr = SkScalarDiv(fr, weight); + fg = SkScalarDiv(fg, weight); + fb = SkScalarDiv(fb, weight); + fa = SkScalarDiv(fa, weight); - int a = SkClampMax(SkFixedRoundToInt(fa), 255); - int r = SkClampMax(SkFixedRoundToInt(fr), a); - int g = SkClampMax(SkFixedRoundToInt(fg), a); - int b = SkClampMax(SkFixedRoundToInt(fb), a); + int a = SkClampMax(SkScalarRoundToInt(fa), 255); + int r = SkClampMax(SkScalarRoundToInt(fr), a); + int g = SkClampMax(SkScalarRoundToInt(fg), a); + int b = SkClampMax(SkScalarRoundToInt(fb), a); *colors++ = SkPackARGB32(a, r, g, b); @@ -147,12 +147,13 @@ static SkBitmapFilter *allocateBitmapFilter() { } SkBitmapProcState::ShaderProc32 -SkBitmapProcState::chooseBitmapFilterProc(const SkPaint& paint) { - // we need to be requested - uint32_t mask = SkPaint::kFilterBitmap_Flag - | SkPaint::kHighQualityFilterBitmap_Flag - ; - if ((paint.getFlags() & mask) != mask) { +SkBitmapProcState::chooseBitmapFilterProc() { + + if (fFilterQuality != kHQ_BitmapFilter) { + return NULL; + } + + if (fAlphaScale != 256) { return NULL; } @@ -166,11 +167,6 @@ SkBitmapProcState::chooseBitmapFilterProc(const SkPaint& paint) { return NULL; } - // TODO: support blending inside our procs - if (0xFF != paint.getAlpha()) { - return NULL; - } - if (fInvType & (SkMatrix::kAffine_Mask | SkMatrix::kScale_Mask)) { fBitmapFilter = allocateBitmapFilter(); } @@ -184,66 +180,66 @@ SkBitmapProcState::chooseBitmapFilterProc(const SkPaint& paint) { } } -static void divideByWeights(SkFixed *sums, SkFixed *weights, SkBitmap *dst) { +static void divideByWeights(SkScalar *sums, SkScalar *weights, SkBitmap *dst) { for (int y = 0 ; y < dst->height() ; y++) { for (int x = 0 ; x < dst->width() ; x++) { - SkFixed fr = SkFixedDiv(sums[4*(y*dst->width() + x) + 0], weights[y*dst->width() + x]); - SkFixed fg = SkFixedDiv(sums[4*(y*dst->width() + x) + 1], weights[y*dst->width() + x]); - SkFixed fb = SkFixedDiv(sums[4*(y*dst->width() + x) + 2], weights[y*dst->width() + x]); - SkFixed fa = SkFixedDiv(sums[4*(y*dst->width() + x) + 3], weights[y*dst->width() + x]); - int a = SkClampMax(SkFixedRoundToInt(fa), 255); - int r = SkClampMax(SkFixedRoundToInt(fr), a); - int g = SkClampMax(SkFixedRoundToInt(fg), a); - int b = SkClampMax(SkFixedRoundToInt(fb), a); + SkScalar fr = SkScalarDiv(sums[4*(y*dst->width() + x) + 0], weights[y*dst->width() + x]); + SkScalar fg = SkScalarDiv(sums[4*(y*dst->width() + x) + 1], weights[y*dst->width() + x]); + SkScalar fb = SkScalarDiv(sums[4*(y*dst->width() + x) + 2], weights[y*dst->width() + x]); + SkScalar fa = SkScalarDiv(sums[4*(y*dst->width() + x) + 3], weights[y*dst->width() + x]); + int a = SkClampMax(SkScalarRoundToInt(fa), 255); + int r = SkClampMax(SkScalarRoundToInt(fr), a); + int g = SkClampMax(SkScalarRoundToInt(fg), a); + int b = SkClampMax(SkScalarRoundToInt(fb), a); *dst->getAddr32(x,y) = SkPackARGB32(a, r, g, b); } } } -static void upScaleHoriz(const SkBitmap *src, SkBitmap *dst, float scale, SkBitmapFilter *filter) { - for (int y = 0 ; y < src->height() ; y++) { +static void upScaleHorizTranspose(const SkBitmap *src, SkBitmap *dst, float scale, SkBitmapFilter *filter) { + for (int y = 0 ; y < dst->height() ; y++) { for (int x = 0 ; x < dst->width() ; x++) { - float sx = (x + 0.5f) / scale - 0.5f; + float sx = (y + 0.5f) / scale - 0.5f; int x0 = SkClampMax(sk_float_ceil2int(sx-filter->width()), src->width()-1); int x1 = SkClampMax(sk_float_floor2int(sx+filter->width()), src->width()-1); - SkFixed total_weight = 0; - SkFixed fr = 0, fg = 0, fb = 0, fa = 0; - - for (int src_x = x0 ; src_x <= x1 ; src_x++) { - SkFixed weight = filter->lookup(sx - src_x); - SkPMColor c = *src->getAddr32(src_x,y); - fr += weight * SkGetPackedR32(c); - fg += weight * SkGetPackedG32(c); - fb += weight * SkGetPackedB32(c); - fa += weight * SkGetPackedA32(c); - total_weight += weight; + SkScalar totalWeight = 0; + SkScalar fr = 0, fg = 0, fb = 0, fa = 0; + + for (int srcX = x0 ; srcX <= x1 ; srcX++) { + SkScalar weight = filter->lookupScalar(sx - srcX); + SkPMColor c = *src->getAddr32(srcX, x); + fr += SkScalarMul(weight,SkGetPackedR32(c)); + fg += SkScalarMul(weight,SkGetPackedG32(c)); + fb += SkScalarMul(weight,SkGetPackedB32(c)); + fa += SkScalarMul(weight,SkGetPackedA32(c)); + totalWeight += weight; } - fr = SkFixedDiv(fr, total_weight); - fg = SkFixedDiv(fg, total_weight); - fb = SkFixedDiv(fb, total_weight); - fa = SkFixedDiv(fa, total_weight); + fr = SkScalarDiv(fr,totalWeight); + fg = SkScalarDiv(fg,totalWeight); + fb = SkScalarDiv(fb,totalWeight); + fa = SkScalarDiv(fa,totalWeight); - int a = SkClampMax(SkFixedRoundToInt(fa), 255); - int r = SkClampMax(SkFixedRoundToInt(fr), a); - int g = SkClampMax(SkFixedRoundToInt(fg), a); - int b = SkClampMax(SkFixedRoundToInt(fb), a); + int a = SkClampMax(SkScalarRoundToInt(fa), 255); + int r = SkClampMax(SkScalarRoundToInt(fr), a); + int g = SkClampMax(SkScalarRoundToInt(fg), a); + int b = SkClampMax(SkScalarRoundToInt(fb), a); - *dst->getAddr32(x,y) = SkPackARGB32(a, r, g, b); + *dst->getAddr32(x,y) = SkPackARGB32(a, r, g, b); } } } static void downScaleHoriz(const SkBitmap *src, SkBitmap *dst, float scale, SkBitmapFilter *filter) { - SkFixed *sums = SkNEW_ARRAY(SkFixed, dst->width() * dst->height() * 4); - SkFixed *weights = SkNEW_ARRAY(SkFixed, dst->width() * dst->height()); + SkScalar *sums = SkNEW_ARRAY(SkScalar, dst->width() * dst->height() * 4); + SkScalar *weights = SkNEW_ARRAY(SkScalar, dst->width() * dst->height()); - SkAutoTDeleteArray ada1(sums); - SkAutoTDeleteArray ada2(weights); + SkAutoTDeleteArray ada1(sums); + SkAutoTDeleteArray ada2(weights); - memset(sums, 0, dst->width() * dst->height() * sizeof(SkFixed) * 4); - memset(weights, 0, dst->width() * dst->height() * sizeof(SkFixed)); + memset(sums, 0, dst->width() * dst->height() * sizeof(SkScalar) * 4); + memset(weights, 0, dst->width() * dst->height() * sizeof(SkScalar)); for (int y = 0 ; y < src->height() ; y++) { for (int x = 0 ; x < src->width() ; x++) { @@ -255,7 +251,7 @@ static void downScaleHoriz(const SkBitmap *src, SkBitmap *dst, float scale, SkBi SkPMColor c = *src->getAddr32(x,y); for (int dst_x = x0 ; dst_x <= x1 ; dst_x++) { - SkFixed weight = filter->lookup(dx - dst_x); + SkScalar weight = filter->lookup(dx - dst_x); sums[4*(y*dst->width() + dst_x) + 0] += weight*SkGetPackedR32(c); sums[4*(y*dst->width() + dst_x) + 1] += weight*SkGetPackedG32(c); sums[4*(y*dst->width() + dst_x) + 2] += weight*SkGetPackedB32(c); @@ -268,49 +264,15 @@ static void downScaleHoriz(const SkBitmap *src, SkBitmap *dst, float scale, SkBi divideByWeights(sums, weights, dst); } -static void upScaleVert(const SkBitmap *src, SkBitmap *dst, float scale, SkBitmapFilter *filter) { - for (int y = 0 ; y < dst->height() ; y++) { - for (int x = 0 ; x < dst->width() ; x++) { - float sy = (y + 0.5f) / scale - 0.5f; - int y0 = SkClampMax(sk_float_ceil2int(sy-filter->width()), src->height()-1); - int y1 = SkClampMax(sk_float_floor2int(sy+filter->width()), src->height()-1); - - SkFixed total_weight = 0; - SkFixed fr = 0, fg = 0, fb = 0, fa = 0; - - for (int src_y = y0 ; src_y <= y1 ; src_y++) { - SkFixed weight = filter->lookup(sy - src_y); - SkPMColor c = *src->getAddr32(x,src_y); - fr += weight * SkGetPackedR32(c); - fg += weight * SkGetPackedG32(c); - fb += weight * SkGetPackedB32(c); - fa += weight * SkGetPackedA32(c); - total_weight += weight; - } - fr = SkFixedDiv(fr, total_weight); - fg = SkFixedDiv(fg, total_weight); - fb = SkFixedDiv(fb, total_weight); - fa = SkFixedDiv(fa, total_weight); - - int a = SkClampMax(SkFixedRoundToInt(fa), 255); - int r = SkClampMax(SkFixedRoundToInt(fr), a); - int g = SkClampMax(SkFixedRoundToInt(fg), a); - int b = SkClampMax(SkFixedRoundToInt(fb), a); - - *dst->getAddr32(x,y) = SkPackARGB32(a, r, g, b); - } - } -} - static void downScaleVert(const SkBitmap *src, SkBitmap *dst, float scale, SkBitmapFilter *filter) { - SkFixed *sums = SkNEW_ARRAY(SkFixed, dst->width() * dst->height() * 4); - SkFixed *weights = SkNEW_ARRAY(SkFixed, dst->width() * dst->height()); + SkScalar *sums = SkNEW_ARRAY(SkScalar, dst->width() * dst->height() * 4); + SkScalar *weights = SkNEW_ARRAY(SkScalar, dst->width() * dst->height()); - SkAutoTDeleteArray ada1(sums); - SkAutoTDeleteArray ada2(weights); + SkAutoTDeleteArray ada1(sums); + SkAutoTDeleteArray ada2(weights); - memset(sums, 0, dst->width() * dst->height() * sizeof(SkFixed) * 4); - memset(weights, 0, dst->width() * dst->height() * sizeof(SkFixed)); + memset(sums, 0, dst->width() * dst->height() * sizeof(SkScalar) * 4); + memset(weights, 0, dst->width() * dst->height() * sizeof(SkScalar)); for (int y = 0 ; y < src->height() ; y++) { for (int x = 0 ; x < src->width() ; x++) { @@ -322,7 +284,7 @@ static void downScaleVert(const SkBitmap *src, SkBitmap *dst, float scale, SkBit SkPMColor c = *src->getAddr32(x,y); for (int dst_y = y0 ; dst_y <= y1 ; dst_y++) { - SkFixed weight = filter->lookup(dy - dst_y); + SkScalar weight = filter->lookupScalar(dy - dst_y); sums[4*(dst_y*dst->width() + x) + 0] += weight*SkGetPackedR32(c); sums[4*(dst_y*dst->width() + x) + 1] += weight*SkGetPackedG32(c); sums[4*(dst_y*dst->width() + x) + 2] += weight*SkGetPackedB32(c); @@ -337,31 +299,27 @@ static void downScaleVert(const SkBitmap *src, SkBitmap *dst, float scale, SkBit void SkBitmap::scale(SkBitmap *dst) const { - SkBitmap horiz_temp; + SkBitmap horizTemp; - horiz_temp.setConfig(SkBitmap::kARGB_8888_Config, dst->width(), height()); - horiz_temp.allocPixels(); + horizTemp.setConfig(SkBitmap::kARGB_8888_Config, height(), dst->width()); + horizTemp.allocPixels(); SkBitmapFilter *filter = allocateBitmapFilter(); - float horiz_scale = float(dst->width()) / width(); + float horizScale = float(dst->width()) / width(); - if (horiz_scale == 1) { - this->copyPixelsTo(horiz_temp.getPixels(), getSize()); - } else if (horiz_scale > 1) { - upScaleHoriz(this, &horiz_temp, horiz_scale, filter); - } else if (horiz_scale < 1) { - downScaleHoriz(this, &horiz_temp, horiz_scale, filter); + if (horizScale >= 1) { + upScaleHorizTranspose(this, &horizTemp, horizScale, filter); + } else if (horizScale < 1) { + downScaleHoriz(this, &horizTemp, horizScale, filter); } - float vert_scale = float(dst->height()) / height(); + float vertScale = float(dst->height()) / height(); - if (vert_scale == 1) { - horiz_temp.copyPixelsTo(dst->getPixels(), dst->getSize()); - } else if (vert_scale > 1) { - upScaleVert(&horiz_temp, dst, vert_scale, filter); - } else if (vert_scale < 1) { - downScaleVert(&horiz_temp, dst, vert_scale, filter); + if (vertScale >= 1) { + upScaleHorizTranspose(&horizTemp, dst, vertScale, filter); + } else if (vertScale < 1) { + downScaleVert(&horizTemp, dst, vertScale, filter); } SkDELETE(filter); -- cgit v1.2.3