aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core
diff options
context:
space:
mode:
authorGravatar humper@google.com <humper@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-07-09 17:48:15 +0000
committerGravatar humper@google.com <humper@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-07-09 17:48:15 +0000
commit25fc6b9bfb3f9c2839b463b69b86ce9ed206c763 (patch)
treef4c6f78e86f69872343f937c15c1747538531976 /src/core
parent64527e9caa4d38c0c113ce3de3ccf3c6e40eef05 (diff)
More general image filter interface; tested implementation of standalone
image scaler (not yet plumbed). High quality downsampler. Fast SSE resampler. BUG= R=reed@google.com Review URL: https://codereview.chromium.org/17381008 git-svn-id: http://skia.googlecode.com/svn/trunk@9936 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/core')
-rw-r--r--src/core/SkBitmapFilter.cpp378
-rw-r--r--src/core/SkBitmapFilter.h150
-rw-r--r--src/core/SkBitmapProcBicubic.cpp187
-rw-r--r--src/core/SkBitmapProcState.cpp8
-rw-r--r--src/core/SkBitmapProcState.h18
5 files changed, 545 insertions, 196 deletions
diff --git a/src/core/SkBitmapFilter.cpp b/src/core/SkBitmapFilter.cpp
new file mode 100644
index 0000000000..cc6ad95d1e
--- /dev/null
+++ b/src/core/SkBitmapFilter.cpp
@@ -0,0 +1,378 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkBitmapProcState.h"
+#include "SkBitmap.h"
+#include "SkColor.h"
+#include "SkColorPriv.h"
+#include "SkUnPreMultiply.h"
+#include "SkShader.h"
+#include "SkRTConf.h"
+#include "SkMath.h"
+
+void highQualityFilter(const SkBitmapProcState& s, int x, int y,
+ SkPMColor* SK_RESTRICT colors, int count) {
+
+ const int maxX = s.fBitmap->width() - 1;
+ const int maxY = s.fBitmap->height() - 1;
+
+ while (count-- > 0) {
+ SkPoint srcPt;
+ s.fInvProc(*s.fInvMatrix, SkIntToScalar(x),
+ SkIntToScalar(y), &srcPt);
+ srcPt.fX -= SK_ScalarHalf;
+ srcPt.fY -= SK_ScalarHalf;
+ SkScalar fractx = srcPt.fX - SkScalarFloorToScalar(srcPt.fX);
+ SkScalar fracty = srcPt.fY - SkScalarFloorToScalar(srcPt.fY);
+
+ int sx = SkScalarFloorToInt(srcPt.fX);
+ int sy = SkScalarFloorToInt(srcPt.fY);
+
+ SkFixed weight = 0;
+ SkFixed fr = 0, fg = 0, fb = 0, fa = 0;
+
+ int y0 = SkClampMax(int(ceil(sy-s.getBitmapFilter()->width() + 0.5f)), maxY);
+ int y1 = SkClampMax(int(floor(sy+s.getBitmapFilter()->width() + 0.5f)), maxY);
+ int x0 = SkClampMax(int(ceil(sx-s.getBitmapFilter()->width() + 0.5f)), maxX);
+ int x1 = SkClampMax(int(floor(sx+s.getBitmapFilter()->width() + 0.5f)), maxX);
+
+ for (int src_y = y0; src_y <= y1; src_y++) {
+ SkFixed yweight = s.getBitmapFilter()->lookup((srcPt.fY - src_y));
+
+ for (int src_x = x0; src_x <= x1 ; src_x++) {
+ SkFixed xweight = s.getBitmapFilter()->lookup((srcPt.fX - src_x));
+
+ SkFixed combined_weight = SkFixedMul(xweight, yweight);
+
+ SkPMColor c = *s.fBitmap->getAddr32(src_x, src_y);
+ fr += combined_weight * SkGetPackedR32(c);
+ fg += combined_weight * SkGetPackedG32(c);
+ fb += combined_weight * SkGetPackedB32(c);
+ fa += combined_weight * SkGetPackedA32(c);
+ weight += combined_weight;
+ }
+ }
+
+ fr = SkFixedDiv(fr, weight);
+ fg = SkFixedDiv(fg, weight);
+ fb = SkFixedDiv(fb, weight);
+ fa = SkFixedDiv(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);
+
+ *colors++ = SkPackARGB32(a, r, g, b);
+
+ x++;
+ }
+}
+
+void highQualityFilter_ScaleOnly(const SkBitmapProcState &s, int x, int y,
+ SkPMColor *SK_RESTRICT colors, int count) {
+ const int maxX = s.fBitmap->width() - 1;
+ const int maxY = s.fBitmap->height() - 1;
+
+ SkPoint srcPt;
+
+ s.fInvProc(*s.fInvMatrix, SkIntToScalar(x),
+ SkIntToScalar(y), &srcPt);
+ srcPt.fY -= SK_ScalarHalf;
+ SkScalar fracty = srcPt.fY - SkScalarFloorToScalar(srcPt.fY);
+ int sy = SkScalarFloorToInt(srcPt.fY);
+ int y0 = SkClampMax(int(ceil(sy-s.getBitmapFilter()->width() + 0.5f)), maxY);
+ int y1 = SkClampMax(int(floor(sy+s.getBitmapFilter()->width() + 0.5f)), maxY);
+
+ while (count-- > 0) {
+ s.fInvProc(*s.fInvMatrix, SkIntToScalar(x),
+ SkIntToScalar(y), &srcPt);
+ srcPt.fX -= SK_ScalarHalf;
+ srcPt.fY -= SK_ScalarHalf;
+ SkScalar fractx = srcPt.fX - SkScalarFloorToScalar(srcPt.fX);
+
+ int sx = SkScalarFloorToInt(srcPt.fX);
+
+ SkFixed weight = 0;
+ SkFixed fr = 0, fg = 0, fb = 0, fa = 0;
+
+ int x0 = SkClampMax(int(ceil(sx-s.getBitmapFilter()->width() + 0.5f)), maxX);
+ int x1 = SkClampMax(int(floor(sx+s.getBitmapFilter()->width() + 0.5f)), maxX);
+
+ for (int src_y = y0; src_y <= y1; src_y++) {
+ SkFixed yweight = s.getBitmapFilter()->lookup((srcPt.fY - src_y));
+
+ for (int src_x = x0; src_x <= x1 ; src_x++) {
+ SkFixed xweight = s.getBitmapFilter()->lookup((srcPt.fX - src_x));
+
+ SkFixed combined_weight = SkFixedMul(xweight, yweight);
+
+ SkPMColor c = *s.fBitmap->getAddr32(src_x, src_y);
+ fr += combined_weight * SkGetPackedR32(c);
+ fg += combined_weight * SkGetPackedG32(c);
+ fb += combined_weight * SkGetPackedB32(c);
+ fa += combined_weight * SkGetPackedA32(c);
+ weight += combined_weight;
+ }
+ }
+
+ fr = SkFixedDiv(fr, weight);
+ fg = SkFixedDiv(fg, weight);
+ fb = SkFixedDiv(fb, weight);
+ fa = SkFixedDiv(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);
+
+ *colors++ = SkPackARGB32(a, r, g, b);
+
+ x++;
+ }
+}
+
+SK_CONF_DECLARE(const char *, c_bitmapFilter, "bitmap.filter", "mitchell", "Which bitmap filter to use [mitchell, sinc, gaussian, triangle, box]");
+
+static SkBitmapFilter *allocateBitmapFilter() {
+ if (!strcmp(c_bitmapFilter, "mitchell")) {
+ return SkNEW_ARGS(SkMitchellFilter,(1.f/3.f,1.f/3.f));
+ } else if (!strcmp(c_bitmapFilter, "sinc")) {
+ return SkNEW_ARGS(SkSincFilter,(3));
+ } else if (!strcmp(c_bitmapFilter, "gaussian")) {
+ return SkNEW_ARGS(SkGaussianFilter,(2));
+ } else if (!strcmp(c_bitmapFilter, "triangle")) {
+ return SkNEW(SkTriangleFilter);
+ } else if (!strcmp(c_bitmapFilter, "box")) {
+ return SkNEW(SkBoxFilter);
+ } else {
+ SkASSERT(!!!"Unknown filter type");
+ }
+
+ return NULL;
+}
+
+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) {
+ return NULL;
+ }
+
+ // TODO: consider supporting other configs (e.g. 565, A8)
+ if (fBitmap->config() != SkBitmap::kARGB_8888_Config) {
+ return NULL;
+ }
+
+ // TODO: consider supporting repeat and mirror
+ if (SkShader::kClamp_TileMode != fTileModeX || SkShader::kClamp_TileMode != fTileModeY) {
+ return NULL;
+ }
+
+ // TODO: support blending inside our procs
+ if (0xFF != paint.getAlpha()) {
+ return NULL;
+ }
+
+ if (fInvType & (SkMatrix::kAffine_Mask | SkMatrix::kScale_Mask)) {
+ fBitmapFilter = allocateBitmapFilter();
+ }
+
+ if (fInvType & SkMatrix::kAffine_Mask) {
+ return highQualityFilter;
+ } else if (fInvType & SkMatrix::kScale_Mask) {
+ return highQualityFilter_ScaleOnly;
+ } else {
+ return NULL;
+ }
+}
+
+static void divideByWeights(SkFixed *sums, SkFixed *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);
+
+ *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++) {
+ for (int x = 0 ; x < dst->width() ; x++) {
+ float sx = x / scale - 0.5f;
+ int x0 = SkClampMax(int(ceil(sx-filter->width() + 0.5f)), src->width()-1);
+ int x1 = SkClampMax(int(floor(sx+filter->width() + 0.5f)), 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;
+ }
+ 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 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());
+
+ SkAutoTDeleteArray<SkFixed> ada1(sums);
+ SkAutoTDeleteArray<SkFixed> ada2(weights);
+
+ memset(sums, 0, dst->width() * dst->height() * sizeof(SkFixed) * 4);
+ memset(weights, 0, dst->width() * dst->height() * sizeof(SkFixed));
+
+ for (int y = 0 ; y < src->height() ; y++) {
+ for (int x = 0 ; x < src->width() ; x++) {
+ // splat each source pixel into the destination image
+ float dx = (x + 0.5f) * scale;
+ int x0 = SkClampMax(int(ceil(dx-filter->width() + 0.5f)), dst->width()-1);
+ int x1 = SkClampMax(int(floor(dx+filter->width() + 0.5f)), dst->width()-1);
+
+ SkPMColor c = *src->getAddr32(x,y);
+
+ for (int dst_x = x0 ; dst_x <= x1 ; dst_x++) {
+ SkFixed 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);
+ sums[4*(y*dst->width() + dst_x) + 3] += weight*SkGetPackedA32(c);
+ weights[y*dst->width() + dst_x] += weight;
+ }
+ }
+ }
+
+ 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 / scale - 0.5f;
+ int y0 = SkClampMax(int(ceil(sy-filter->width() + 0.5f)), src->height()-1);
+ int y1 = SkClampMax(int(floor(sy+filter->width() + 0.5f)), 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());
+
+ SkAutoTDeleteArray<SkFixed> ada1(sums);
+ SkAutoTDeleteArray<SkFixed> ada2(weights);
+
+ memset(sums, 0, dst->width() * dst->height() * sizeof(SkFixed) * 4);
+ memset(weights, 0, dst->width() * dst->height() * sizeof(SkFixed));
+
+ for (int y = 0 ; y < src->height() ; y++) {
+ for (int x = 0 ; x < src->width() ; x++) {
+ // splat each source pixel into the destination image
+ float dy = (y + 0.5f) * scale;
+ int y0 = SkClampMax(int(ceil(dy-filter->width() + 0.5f)), dst->height()-1);
+ int y1 = SkClampMax(int(floor(dy+filter->width() + 0.5f)), dst->height()-1);
+
+ SkPMColor c = *src->getAddr32(x,y);
+
+ for (int dst_y = y0 ; dst_y <= y1 ; dst_y++) {
+ SkFixed weight = filter->lookup(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);
+ sums[4*(dst_y*dst->width() + x) + 3] += weight*SkGetPackedA32(c);
+ weights[dst_y*dst->width() + x] += weight;
+ }
+ }
+ }
+
+ divideByWeights(sums, weights, dst);
+}
+
+void SkBitmap::scale(SkBitmap *dst) const {
+
+ SkBitmap horiz_temp;
+
+ horiz_temp.setConfig(SkBitmap::kARGB_8888_Config, dst->width(), height());
+ horiz_temp.allocPixels();
+
+ SkBitmapFilter *filter = allocateBitmapFilter();
+
+ 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);
+ } else if (horiz_scale < 1) {
+ downScaleHoriz(this, &horiz_temp, horiz_scale, filter);
+ }
+
+ 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);
+ } else if (vert_scale < 1) {
+ downScaleVert(&horiz_temp, dst, vert_scale, filter);
+ }
+
+ SkDELETE(filter);
+}
diff --git a/src/core/SkBitmapFilter.h b/src/core/SkBitmapFilter.h
new file mode 100644
index 0000000000..4e4778876c
--- /dev/null
+++ b/src/core/SkBitmapFilter.h
@@ -0,0 +1,150 @@
+
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+
+#ifndef SkBitmapFilter_DEFINED
+#define SkBitmapFilter_DEFINED
+
+#include "SkMath.h"
+
+// size of the precomputed bitmap filter tables for high quality filtering.
+// Used to precompute the shape of the filter kernel.
+// Table size chosen from experiments to see where I could start to see a difference.
+
+#define SKBITMAP_FILTER_TABLE_SIZE 32
+
+class SkBitmapFilter {
+ public:
+ SkBitmapFilter(float width)
+ : fWidth(width), fInvWidth(1.f/width) {
+ precomputed = false;
+ }
+
+ SkFixed lookup( float x ) const {
+ if (!precomputed) {
+ precomputeTable();
+ }
+ int filter_idx = int(fabsf(x * invWidth() * SKBITMAP_FILTER_TABLE_SIZE));
+ return fFilterTable[ SkTMin(filter_idx, SKBITMAP_FILTER_TABLE_SIZE-1) ];
+ }
+
+ SkFixed lookupFloat( float x ) const {
+ if (!precomputed) {
+ precomputeTable();
+ }
+ int filter_idx = int(fabsf(x * invWidth() * SKBITMAP_FILTER_TABLE_SIZE));
+ return fFilterTableFloat[ SkTMin(filter_idx, SKBITMAP_FILTER_TABLE_SIZE-1) ];
+ }
+
+ float width() const { return fWidth; }
+ float invWidth() const { return fInvWidth; }
+ virtual float evaluate(float x) const = 0;
+ protected:
+ float fWidth;
+ float fInvWidth;
+
+ mutable bool precomputed;
+ mutable SkFixed fFilterTable[SKBITMAP_FILTER_TABLE_SIZE];
+ mutable float fFilterTableFloat[SKBITMAP_FILTER_TABLE_SIZE];
+ private:
+ void precomputeTable() const {
+ precomputed = true;
+ SkFixed *ftp = fFilterTable;
+ float *ftp_float = fFilterTableFloat;
+ for (int x = 0; x < SKBITMAP_FILTER_TABLE_SIZE; ++x) {
+ float fx = ((float)x + .5f) * this->width() / SKBITMAP_FILTER_TABLE_SIZE;
+ float filter_value = evaluate(fx);
+ *ftp_float++ = filter_value;
+ *ftp++ = SkFloatToFixed(filter_value);
+ }
+ }
+};
+
+class SkMitchellFilter: public SkBitmapFilter {
+ public:
+ SkMitchellFilter(float b, float c, float width=2.0f)
+ : SkBitmapFilter(width), B(b), C(c) {
+ }
+
+ virtual float evaluate(float x) const SK_OVERRIDE {
+ x = fabsf(x);
+ float ret;
+ if (x > 2.f) {
+ return 0;
+ } else if (x > 1.f) {
+ return ((-B - 6*C) * x*x*x + (6*B + 30*C) * x*x +
+ (-12*B - 48*C) * x + (8*B + 24*C)) * (1.f/6.f);
+ } else {
+ return ((12 - 9*B - 6*C) * x*x*x +
+ (-18 + 12*B + 6*C) * x*x +
+ (6 - 2*B)) * (1.f/6.f);
+ }
+ }
+ protected:
+ float B, C;
+};
+
+class SkGaussianFilter: public SkBitmapFilter {
+ public:
+ SkGaussianFilter(float a, float width=2.0f)
+ : SkBitmapFilter(width), alpha(a), expWidth(expf(-alpha * width * width)) {
+ }
+
+ virtual float evaluate(float x) const SK_OVERRIDE {
+ return SkTMax(0.f, float(expf(-alpha*x*x) - expWidth));
+ }
+ protected:
+ float alpha, expWidth;
+};
+
+class SkTriangleFilter: public SkBitmapFilter {
+ public:
+ SkTriangleFilter(float width=1)
+ : SkBitmapFilter(width) {
+ }
+
+ virtual float evaluate(float x) const SK_OVERRIDE {
+ return SkTMax(0.f, fWidth - fabsf(x));
+ }
+ protected:
+};
+
+class SkBoxFilter: public SkBitmapFilter {
+ public:
+ SkBoxFilter(float width=0.5f)
+ : SkBitmapFilter(width) {
+ }
+
+ virtual float evaluate(float x) const SK_OVERRIDE {
+ return 1;
+ }
+ protected:
+};
+
+
+class SkSincFilter: public SkBitmapFilter {
+ public:
+ SkSincFilter(float t, float width=3.f)
+ : SkBitmapFilter(width), tau(t) {
+ }
+
+ virtual float evaluate(float x) const SK_OVERRIDE {
+ x = sk_float_abs(x * fInvWidth);
+ if (x < 1e-5f) return 1.f;
+ if (x > 1.f) return 0.f;
+ x *= M_PI;
+ float sinc = sk_float_sin(x) / x;
+ float lanczos = sk_float_sin(x * tau) / (x * tau);
+ return sinc * lanczos;
+ }
+ protected:
+ float tau;
+};
+
+
+#endif
diff --git a/src/core/SkBitmapProcBicubic.cpp b/src/core/SkBitmapProcBicubic.cpp
deleted file mode 100644
index f2cd0e30e7..0000000000
--- a/src/core/SkBitmapProcBicubic.cpp
+++ /dev/null
@@ -1,187 +0,0 @@
-/*
- * Copyright 2013 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include "SkBitmapProcState.h"
-#include "SkBitmap.h"
-#include "SkColor.h"
-#include "SkColorPriv.h"
-#include "SkUnPreMultiply.h"
-#include "SkRTConf.h"
-#include "SkShader.h"
-
-#define DS(x) SkDoubleToScalar(x)
-
-#define MUL(a, b) ((a) * (b))
-
-static inline SkPMColor cubicBlend(const SkFixed cc[4], SkPMColor c0, SkPMColor c1, SkPMColor c2, SkPMColor c3) {
- SkFixed fa = MUL(cc[0], SkGetPackedA32(c0)) + MUL(cc[1], SkGetPackedA32(c1)) + MUL(cc[2], SkGetPackedA32(c2)) + MUL(cc[3], SkGetPackedA32(c3));
- SkFixed fr = MUL(cc[0], SkGetPackedR32(c0)) + MUL(cc[1], SkGetPackedR32(c1)) + MUL(cc[2], SkGetPackedR32(c2)) + MUL(cc[3], SkGetPackedR32(c3));
- SkFixed fg = MUL(cc[0], SkGetPackedG32(c0)) + MUL(cc[1], SkGetPackedG32(c1)) + MUL(cc[2], SkGetPackedG32(c2)) + MUL(cc[3], SkGetPackedG32(c3));
- SkFixed fb = MUL(cc[0], SkGetPackedB32(c0)) + MUL(cc[1], SkGetPackedB32(c1)) + MUL(cc[2], SkGetPackedB32(c2)) + MUL(cc[3], SkGetPackedB32(c3));
-
- int a = SkClampMax(SkFixedRoundToInt(fa), 255);
- int r = SkClampMax(SkFixedRoundToInt(fr), a);
- int g = SkClampMax(SkFixedRoundToInt(fg), a);
- int b = SkClampMax(SkFixedRoundToInt(fb), a);
-
- return SkPackARGB32(a, r, g, b);
-}
-
-static float poly_eval(const float cc[4], float t) {
- return cc[0] + t * (cc[1] + t * (cc[2] + t * cc[3]));
-}
-
-static void build_coeff4(SkFixed dst[4], float t) {
- static const SkScalar coefficients[16] = {
- DS( 1.0 / 18.0), DS(-9.0 / 18.0), DS( 15.0 / 18.0), DS( -7.0 / 18.0),
- DS(16.0 / 18.0), DS( 0.0 / 18.0), DS(-36.0 / 18.0), DS( 21.0 / 18.0),
- DS( 1.0 / 18.0), DS( 9.0 / 18.0), DS( 27.0 / 18.0), DS(-21.0 / 18.0),
- DS( 0.0 / 18.0), DS( 0.0 / 18.0), DS( -6.0 / 18.0), DS( 7.0 / 18.0),
- };
-
- dst[0] = SkFloatToFixed(poly_eval(&coefficients[ 0], t));
- dst[1] = SkFloatToFixed(poly_eval(&coefficients[ 4], t));
- dst[2] = SkFloatToFixed(poly_eval(&coefficients[ 8], t));
- dst[3] = SkFloatToFixed(poly_eval(&coefficients[12], t));
-}
-
-static SkPMColor doBicubicFilter(const SkBitmap *bm, SkFixed coeffX[4], SkFixed coeffY[4],
- int x0, int x1, int x2, int x3,
- int y0, int y1, int y2, int y3 )
-{
- SkPMColor s00 = *bm->getAddr32(x0, y0);
- SkPMColor s10 = *bm->getAddr32(x1, y0);
- SkPMColor s20 = *bm->getAddr32(x2, y0);
- SkPMColor s30 = *bm->getAddr32(x3, y0);
- SkPMColor s0 = cubicBlend(coeffX, s00, s10, s20, s30);
- SkPMColor s01 = *bm->getAddr32(x0, y1);
- SkPMColor s11 = *bm->getAddr32(x1, y1);
- SkPMColor s21 = *bm->getAddr32(x2, y1);
- SkPMColor s31 = *bm->getAddr32(x3, y1);
- SkPMColor s1 = cubicBlend(coeffX, s01, s11, s21, s31);
- SkPMColor s02 = *bm->getAddr32(x0, y2);
- SkPMColor s12 = *bm->getAddr32(x1, y2);
- SkPMColor s22 = *bm->getAddr32(x2, y2);
- SkPMColor s32 = *bm->getAddr32(x3, y2);
- SkPMColor s2 = cubicBlend(coeffX, s02, s12, s22, s32);
- SkPMColor s03 = *bm->getAddr32(x0, y3);
- SkPMColor s13 = *bm->getAddr32(x1, y3);
- SkPMColor s23 = *bm->getAddr32(x2, y3);
- SkPMColor s33 = *bm->getAddr32(x3, y3);
- SkPMColor s3 = cubicBlend(coeffX, s03, s13, s23, s33);
- return cubicBlend(coeffY, s0, s1, s2, s3);
-}
-
-static void bicubicFilter(const SkBitmapProcState& s, int x, int y,
- SkPMColor* SK_RESTRICT colors, int count) {
-
- const int maxX = s.fBitmap->width() - 1;
- const int maxY = s.fBitmap->height() - 1;
-
- while (count-- > 0) {
- SkPoint srcPt;
- s.fInvProc(*s.fInvMatrix, SkIntToScalar(x),
- SkIntToScalar(y), &srcPt);
- srcPt.fX -= SK_ScalarHalf;
- srcPt.fY -= SK_ScalarHalf;
- SkScalar fractx = srcPt.fX - SkScalarFloorToScalar(srcPt.fX);
- SkScalar fracty = srcPt.fY - SkScalarFloorToScalar(srcPt.fY);
-
- SkFixed coeffX[4], coeffY[4];
- build_coeff4(coeffX, fractx);
- build_coeff4(coeffY, fracty);
-
- int sx = SkScalarFloorToInt(srcPt.fX);
- int sy = SkScalarFloorToInt(srcPt.fY);
-
- // Here is where we can support other tile modes (e.g. repeat or mirror)
- int x0 = SkClampMax(sx - 1, maxX);
- int x1 = SkClampMax(sx , maxX);
- int x2 = SkClampMax(sx + 1, maxX);
- int x3 = SkClampMax(sx + 2, maxX);
- int y0 = SkClampMax(sy - 1, maxY);
- int y1 = SkClampMax(sy , maxY);
- int y2 = SkClampMax(sy + 1, maxY);
- int y3 = SkClampMax(sy + 2, maxY);
-
- *colors++ = doBicubicFilter( s.fBitmap, coeffX, coeffY, x0, x1, x2, x3, y0, y1, y2, y3 );
-
- x++;
- }
-}
-
-static void bicubicFilter_ScaleOnly(const SkBitmapProcState &s, int x, int y,
- SkPMColor *SK_RESTRICT colors, int count) {
- const int maxX = s.fBitmap->width() - 1;
- const int maxY = s.fBitmap->height() - 1;
-
- SkPoint srcPt;
- s.fInvProc(*s.fInvMatrix, SkIntToScalar(x), SkIntToScalar(y), &srcPt);
- srcPt.fY -= SK_ScalarHalf;
- SkScalar fracty = srcPt.fY - SkScalarFloorToScalar(srcPt.fY);
- SkFixed coeffX[4], coeffY[4];
- build_coeff4(coeffY, fracty);
- int sy = SkScalarFloorToInt(srcPt.fY);
- int y0 = SkClampMax(sy - 1, maxY);
- int y1 = SkClampMax(sy , maxY);
- int y2 = SkClampMax(sy + 1, maxY);
- int y3 = SkClampMax(sy + 2, maxY);
-
- while (count-- > 0) {
- s.fInvProc(*s.fInvMatrix, SkIntToScalar(x), SkIntToScalar(y), &srcPt);
- srcPt.fX -= SK_ScalarHalf;
- SkScalar fractx = srcPt.fX - SkScalarFloorToScalar(srcPt.fX);
-
- build_coeff4(coeffX, fractx);
-
- int sx = SkScalarFloorToInt(srcPt.fX);
-
- // Here is where we can support other tile modes (e.g. repeat or mirror)
- int x0 = SkClampMax(sx - 1, maxX);
- int x1 = SkClampMax(sx , maxX);
- int x2 = SkClampMax(sx + 1, maxX);
- int x3 = SkClampMax(sx + 2, maxX);
-
- *colors++ = doBicubicFilter( s.fBitmap, coeffX, coeffY, x0, x1, x2, x3, y0, y1, y2, y3 );
-
- x++;
- }
-}
-
-SkBitmapProcState::ShaderProc32
-SkBitmapProcState::chooseBicubicFilterProc(const SkPaint& paint) {
- // we need to be requested
- uint32_t mask = SkPaint::kFilterBitmap_Flag
- | SkPaint::kBicubicFilterBitmap_Flag
- ;
- if ((paint.getFlags() & mask) != mask) {
- return NULL;
- }
-
- // TODO: consider supporting other configs (e.g. 565, A8)
- if (fBitmap->config() != SkBitmap::kARGB_8888_Config) {
- return NULL;
- }
-
- // TODO: consider supporting repeat and mirror
- if (SkShader::kClamp_TileMode != fTileModeX || SkShader::kClamp_TileMode != fTileModeY) {
- return NULL;
- }
-
- // TODO: support blending inside our procs
- if (0xFF != paint.getAlpha()) {
- return NULL;
- }
-
- if (fInvType & SkMatrix::kAffine_Mask) {
- return bicubicFilter;
- } else if (fInvType & SkMatrix::kScale_Mask) {
- return bicubicFilter_ScaleOnly;
- } else {
- return NULL;
- }
-}
diff --git a/src/core/SkBitmapProcState.cpp b/src/core/SkBitmapProcState.cpp
index 2a79f2f490..a2c7f990c2 100644
--- a/src/core/SkBitmapProcState.cpp
+++ b/src/core/SkBitmapProcState.cpp
@@ -302,13 +302,13 @@ bool SkBitmapProcState::chooseProcs(const SkMatrix& inv, const SkPaint& paint) {
fShaderProc32 = this->chooseShaderProc32();
}
- // see if our platform has any accelerated overrides
- this->platformProcs();
-
if (NULL == fShaderProc32) {
- fShaderProc32 = this->chooseBicubicFilterProc(paint);
+ fShaderProc32 = this->chooseBitmapFilterProc(paint);
}
+ // see if our platform has any accelerated overrides
+ this->platformProcs();
+
return true;
}
diff --git a/src/core/SkBitmapProcState.h b/src/core/SkBitmapProcState.h
index 7d754fe509..69de2ca86a 100644
--- a/src/core/SkBitmapProcState.h
+++ b/src/core/SkBitmapProcState.h
@@ -11,6 +11,7 @@
#define SkBitmapProcState_DEFINED
#include "SkBitmap.h"
+#include "SkBitmapFilter.h"
#include "SkMatrix.h"
#define FractionalInt_IS_64BIT
@@ -113,6 +114,8 @@ struct SkBitmapProcState {
// are ignored
ShaderProc32 getShaderProc32() const { return fShaderProc32; }
ShaderProc16 getShaderProc16() const { return fShaderProc16; }
+
+ SkBitmapFilter* getBitmapFilter() const { return fBitmapFilter; }
#ifdef SK_DEBUG
MatrixProc getMatrixProc() const;
@@ -139,12 +142,11 @@ private:
MatrixProc chooseMatrixProc(bool trivial_matrix);
bool chooseProcs(const SkMatrix& inv, const SkPaint&);
ShaderProc32 chooseShaderProc32();
+
+ void buildFilterCoefficients(SkFixed dst[4], float t) const;
+ SkBitmapFilter *fBitmapFilter;
-
- /** test method for choosing a bicubic shading filter
- */
-
- ShaderProc32 chooseBicubicFilterProc(const SkPaint &paint);
+ ShaderProc32 chooseBitmapFilterProc(const SkPaint &paint);
// Return false if we failed to setup for fast translate (e.g. overflow)
bool setupForTranslate();
@@ -200,4 +202,10 @@ void ClampX_ClampY_nofilter_affine(const SkBitmapProcState& s,
void S32_D16_filter_DX(const SkBitmapProcState& s,
const uint32_t* xy, int count, uint16_t* colors);
+void highQualityFilter_ScaleOnly(const SkBitmapProcState &s, int x, int y,
+ SkPMColor *SK_RESTRICT colors, int count);
+void highQualityFilter(const SkBitmapProcState &s, int x, int y,
+ SkPMColor *SK_RESTRICT colors, int count);
+
+
#endif