Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Unified Diff: src/core/SkBitmapFilter.cpp

Issue 18978014: Working plumb of image scaling: (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Added a comment about passing the clip Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/core/SkBitmapFilter.cpp
diff --git a/src/core/SkBitmapFilter.cpp b/src/core/SkBitmapFilter.cpp
index 3cbafd743c1eb9943b451f0a8129105b32da3253..8b2ef3ff46e67310861e870143fd38bc1232616b 100644
--- a/src/core/SkBitmapFilter.cpp
+++ b/src/core/SkBitmapFilter.cpp
@@ -22,26 +22,26 @@ 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);
robertphillips 2013/07/12 19:42:31 srcY
humper 2013/07/12 21:12:03 Done.
for (int src_y = y0; src_y <= y1; src_y++) {
robertphillips 2013/07/12 19:42:31 xWeight, yWeight?
humper 2013/07/12 21:12:03 Done.
- SkFixed yweight = s.getBitmapFilter()->lookup((srcPt.fY - src_y));
+ SkScalar yweight = s.getBitmapFilter()->lookupScalar((srcPt.fY - src_y));
robertphillips 2013/07/12 19:42:31 srcX
humper 2013/07/12 21:12:03 Done.
for (int src_x = x0; src_x <= x1 ; src_x++) {
- SkFixed xweight = s.getBitmapFilter()->lookup((srcPt.fX - src_x));
+ SkScalar xweight = s.getBitmapFilter()->lookupScalar((srcPt.fX - src_x));
robertphillips 2013/07/12 19:42:31 combinedWeight
humper 2013/07/12 21:12:03 Done.
- SkFixed combined_weight = SkFixedMul(xweight, yweight);
+ SkScalar combined_weight = SkScalarMul(xweight, yweight);
SkPMColor c = *s.fBitmap->getAddr32(src_x, src_y);
fr += combined_weight * SkGetPackedR32(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,31 +75,31 @@ 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);
robertphillips 2013/07/12 19:42:31 srcY
humper 2013/07/12 21:12:03 Done.
for (int src_y = y0; src_y <= y1; src_y++) {
robertphillips 2013/07/12 19:42:31 xWeight, yWeight?
humper 2013/07/12 21:12:03 Done.
- SkFixed yweight = s.getBitmapFilter()->lookup((srcPt.fY - src_y));
+ SkScalar yweight = s.getBitmapFilter()->lookupScalar((srcPt.fY - src_y));
for (int src_x = x0; src_x <= x1 ; src_x++) {
- SkFixed xweight = s.getBitmapFilter()->lookup((srcPt.fX - src_x));
+ SkScalar xweight = s.getBitmapFilter()->lookupScalar((srcPt.fX - src_x));
robertphillips 2013/07/12 19:42:31 combinedWeight
humper 2013/07/12 21:12:03 Done.
- SkFixed combined_weight = SkFixedMul(xweight, yweight);
+ SkScalar combined_weight = SkScalarMul(xweight, yweight);
SkPMColor c = *s.fBitmap->getAddr32(src_x, src_y);
fr += combined_weight * SkGetPackedR32(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);
robertphillips 2013/07/12 19:42:31 totalWeight
humper 2013/07/12 21:12:03 Done.
- SkFixed total_weight = 0;
- SkFixed fr = 0, fg = 0, fb = 0, fa = 0;
+ SkScalar total_weight = 0;
+ SkScalar fr = 0, fg = 0, fb = 0, fa = 0;
robertphillips 2013/07/12 19:42:31 srcX
humper 2013/07/12 21:12:03 Done.
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);
+ SkScalar weight = filter->lookupScalar(sx - src_x);
+ SkPMColor c = *src->getAddr32(src_x, x);
+ fr += SkScalarMul(weight,SkGetPackedR32(c));
+ fg += SkScalarMul(weight,SkGetPackedG32(c));
+ fb += SkScalarMul(weight,SkGetPackedB32(c));
+ fa += SkScalarMul(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);
+ fr = SkScalarDiv(fr,total_weight);
+ fg = SkScalarDiv(fg,total_weight);
+ fb = SkScalarDiv(fb,total_weight);
+ fa = SkScalarDiv(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);
+ 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<SkFixed> ada1(sums);
- SkAutoTDeleteArray<SkFixed> ada2(weights);
+ SkAutoTDeleteArray<SkScalar> ada1(sums);
+ SkAutoTDeleteArray<SkScalar> 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<SkFixed> ada1(sums);
- SkAutoTDeleteArray<SkFixed> ada2(weights);
+ SkAutoTDeleteArray<SkScalar> ada1(sums);
+ SkAutoTDeleteArray<SkScalar> 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);
@@ -339,27 +301,23 @@ void SkBitmap::scale(SkBitmap *dst) const {
robertphillips 2013/07/12 19:42:31 horizTemp
humper 2013/07/12 21:12:03 Done.
SkBitmap horiz_temp;
- horiz_temp.setConfig(SkBitmap::kARGB_8888_Config, dst->width(), height());
+ horiz_temp.setConfig(SkBitmap::kARGB_8888_Config, height(), dst->width());
horiz_temp.allocPixels();
SkBitmapFilter *filter = allocateBitmapFilter();
robertphillips 2013/07/12 19:42:31 horizScale
humper 2013/07/12 21:12:03 Done.
float horiz_scale = 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);
+ if (horiz_scale >= 1) {
+ upScaleHorizTranspose(this, &horiz_temp, horiz_scale, filter);
} else if (horiz_scale < 1) {
downScaleHoriz(this, &horiz_temp, horiz_scale, filter);
}
robertphillips 2013/07/12 19:42:31 vertScale
humper 2013/07/12 21:12:03 Done.
float vert_scale = 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);
+ if (vert_scale >= 1) {
+ upScaleHorizTranspose(&horiz_temp, dst, vert_scale, filter);
} else if (vert_scale < 1) {
downScaleVert(&horiz_temp, dst, vert_scale, filter);
}

Powered by Google App Engine
This is Rietveld 408576698