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

Unified Diff: src/core/SkBitmapFilter.cpp

Issue 17381008: More general version of image filtering; reworked to be robust and easier to SSE (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 6 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
new file mode 100644
index 0000000000000000000000000000000000000000..8a7b17828a5f61b50e55410cf047061e5d082827
--- /dev/null
+++ b/src/core/SkBitmapFilter.cpp
@@ -0,0 +1,221 @@
+/*
+ * 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 SkBitmapProcState::buildFilterCoefficients(SkFixed dst[4], float t) const {
+ dst[0] = fFilterTable[ SkTMin(int((1+t) * fBitmapFilter->invWidth() * SKBITMAP_FILTER_TABLE_SIZE), SKBITMAP_FILTER_TABLE_SIZE-1) ];
reed1 2013/06/18 20:21:21 keep line-wrap to 80 or 100 cols
Stephen White 2013/06/18 20:47:50 Mostly just comments from the peanut gallery, but
humper 2013/06/19 20:49:26 deleted this unused method.
humper 2013/06/19 20:49:26 I haven't measured explicitly, but this implementa
+ dst[1] = fFilterTable[ SkTMin(int( (t) * fBitmapFilter->invWidth() * SKBITMAP_FILTER_TABLE_SIZE), SKBITMAP_FILTER_TABLE_SIZE-1) ];
+ dst[2] = fFilterTable[ SkTMin(int((1-t) * fBitmapFilter->invWidth() * SKBITMAP_FILTER_TABLE_SIZE), SKBITMAP_FILTER_TABLE_SIZE-1) ];
+ dst[3] = fFilterTable[ SkTMin(int((2-t) * fBitmapFilter->invWidth() * SKBITMAP_FILTER_TABLE_SIZE), SKBITMAP_FILTER_TABLE_SIZE-1) ];
+
+ SkFixed sum = dst[0] + dst[1] + dst[2] + dst[3];
+ dst[0] = SkFixedDiv( dst[0], sum );
+ dst[1] = SkFixedDiv( dst[1], sum );
+ dst[2] = SkFixedDiv( dst[2], sum );
+ dst[3] = SkFixedDiv( dst[3], sum );
+}
+
+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);
+
+ // s.buildFilterCoefficients(coeffX, fractx);
+ // s.buildFilterCoefficients(coeffY, fracty);
+ // // build_coeff4(coeffX, fractx);
+ // // build_coeff4(coeffY, fracty);
+
+ int sx = SkScalarFloorToInt(srcPt.fX);
+ int sy = SkScalarFloorToInt(srcPt.fY);
+
+ SkFixed weight = 0;
+ SkFixed fr = 0, fg = 0, fb = 0, fa = 0;
+
+ int y0 = SkTMax( 0, int(ceil(sy-s.fBitmapFilter->width() + 0.5f) ));
+ int y1 = SkTMin( maxY, int(floor( sy+s.fBitmapFilter->width() + 0.5f) ));
+ int x0 = SkTMax( 0, int(ceil(sx-s.fBitmapFilter->width() + 0.5f) ));
+ int x1 = SkTMin( maxX, int(floor( sx+s.fBitmapFilter->width() + 0.5f) ));
+
+ for (int src_y = y0; src_y <= y1; src_y++ ) {
+ int filter_idx = int( fabsf( (srcPt.fY - src_y)* s.fBitmapFilter->invWidth() * SKBITMAP_FILTER_TABLE_SIZE) );
+ SkFixed yweight = s.fFilterTable[ SkTMin(filter_idx, SKBITMAP_FILTER_TABLE_SIZE-1) ];
+
+ for (int src_x = x0; src_x <= x1 ; src_x++ ) {
+ filter_idx = int( fabsf( (srcPt.fX - src_x) * s.fBitmapFilter->invWidth() * SKBITMAP_FILTER_TABLE_SIZE) );
+ SkFixed xweight = s.fFilterTable[ SkTMin(filter_idx, SKBITMAP_FILTER_TABLE_SIZE-1) ];
+
+ 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(255, 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 = SkTMax( 0, int(ceil(sy-s.fBitmapFilter->width() + 0.5f) ));
+ int y1 = SkTMin( maxY, int(floor( sy+s.fBitmapFilter->width() + 0.5f) ));
+
+ 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 = SkTMax( 0, int(ceil(sx-s.fBitmapFilter->width() + 0.5f) ));
+ int x1 = SkTMin( maxX, int(floor( sx+s.fBitmapFilter->width() + 0.5f) ));
+
+ for (int src_y = y0; src_y <= y1; src_y++ ) {
+ int filter_idx = int( fabsf( (srcPt.fY - src_y)* s.fBitmapFilter->invWidth() * SKBITMAP_FILTER_TABLE_SIZE) );
+ SkFixed yweight = s.fFilterTable[ SkTMin(filter_idx, SKBITMAP_FILTER_TABLE_SIZE-1) ];
+
+ for (int src_x = x0; src_x <= x1 ; src_x++ ) {
+ filter_idx = int( fabsf( (srcPt.fX - src_x) * s.fBitmapFilter->invWidth() * SKBITMAP_FILTER_TABLE_SIZE) );
+ SkFixed xweight = s.fFilterTable[ SkTMin(filter_idx, SKBITMAP_FILTER_TABLE_SIZE-1) ];
+
+ 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(255, r, g, b);
+
+ x++;
+ }
+}
+
+void SkBitmapProcState::precomputeFilterTable() {
+ SkFixed *ftp = fFilterTable;
+ for (int x = 0; x < SKBITMAP_FILTER_TABLE_SIZE; ++x) {
+ float fx = ((float)x + .5f) * fBitmapFilter->width() / SKBITMAP_FILTER_TABLE_SIZE;
+ float filter_value = fBitmapFilter->Evaluate(fx);
+ *ftp++ = SkFloatToFixed( filter_value );
+ }
+}
+
+SK_CONF_DECLARE( const char *, c_bitmapFilter, "bitmap.filter", "mitchell", "Which bitmap filter to use [mitchell, sinc, gaussian, triangle, box]" );
+
+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)) {
+ if (!strcmp(c_bitmapFilter, "mitchell")) {
+ fBitmapFilter = new SkMitchellFilter(1.f/3.f,1.f/3.f);
+ } else if (!strcmp(c_bitmapFilter, "sinc")) {
+ fBitmapFilter = new SkSincFilter(3);
+ } else if (!strcmp(c_bitmapFilter, "gaussian")) {
+ fBitmapFilter = new SkGaussianFilter(2);
+ } else if (!strcmp(c_bitmapFilter, "triangle")) {
+ fBitmapFilter = new SkTriangleFilter();
+ } else if (!strcmp(c_bitmapFilter, "box")) {
+ fBitmapFilter = new SkBoxFilter();
+ } else {
+ SkASSERT( !!!"Unknown filter type" );
+ }
+ precomputeFilterTable();
+ }
+
+ if (fInvType & SkMatrix::kAffine_Mask) {
+ return highQualityFilter;
+ } else if (fInvType & SkMatrix::kScale_Mask) {
+ return highQualityFilter_ScaleOnly;
+ } else {
+ return NULL;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698