Implements a matrix convolution filter (raster path only). The filtering loop
is templated on the tiling mode for speed: interior pixels are unconditionally
fetched; border pixels apply the appropriate tiling mode before fetching. It
handles target, bias, divisor (as gain), and edge modes (named to be more
skia-like). It does not handle the "preserveAlpha" semantics of
feConvolveMatrix, nor "kernelUnitLength".
git-svn-id: http://skia.googlecode.com/svn/trunk@5592 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gm/matrixconvolution.cpp b/gm/matrixconvolution.cpp
new file mode 100644
index 0000000..6c69fac
--- /dev/null
+++ b/gm/matrixconvolution.cpp
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "gm.h"
+#include "SkMatrixConvolutionImageFilter.h"
+
+namespace skiagm {
+
+class MatrixConvolutionGM : public GM {
+public:
+ MatrixConvolutionGM() : fInitialized(false) {
+ this->setBGColor(0x00000000);
+ }
+
+protected:
+ virtual SkString onShortName() {
+ return SkString("matrixconvolution");
+ }
+
+ void make_bitmap() {
+ fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 80, 80);
+ fBitmap.allocPixels();
+ SkDevice device(fBitmap);
+ SkCanvas canvas(&device);
+ canvas.clear(0x00000000);
+ SkPaint paint;
+ paint.setAntiAlias(true);
+ paint.setColor(0xFFFFFFFF);
+ paint.setTextSize(SkIntToScalar(180));
+ const char* str = "e";
+ canvas.drawText(str, strlen(str), SkIntToScalar(-10), SkIntToScalar(80), paint);
+ }
+
+ virtual SkISize onISize() {
+ return make_isize(300, 300);
+ }
+
+ void draw(SkCanvas* canvas, int x, int y, const SkIPoint& target, SkMatrixConvolutionImageFilter::TileMode tileMode) {
+ SkScalar kernel[9] = {
+ SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
+ SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1),
+ SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
+ };
+ SkISize kernelSize = SkISize::Make(3, 3);
+ SkScalar gain = SkFloatToScalar(0.3f), bias = SkIntToScalar(100);
+ SkPaint paint;
+ SkAutoTUnref<SkImageFilter> filter(SkNEW_ARGS(SkMatrixConvolutionImageFilter, (kernelSize, kernel, gain, bias, target, tileMode)));
+ paint.setImageFilter(filter);
+ canvas->drawSprite(fBitmap, x, y, &paint);
+ }
+
+ virtual void onDraw(SkCanvas* canvas) {
+ if (!fInitialized) {
+ make_bitmap();
+ fInitialized = true;
+ }
+ canvas->clear(0x00000000);
+ SkIPoint target = SkIPoint::Make(1, 1);
+ for (target.fY = 0; target.fY < 3; ++target.fY) {
+ int y = target.fY * 100 + 10;
+ draw(canvas, 10, y, target, SkMatrixConvolutionImageFilter::kClamp_TileMode);
+ draw(canvas, 110, y, target, SkMatrixConvolutionImageFilter::kClampToBlack_TileMode);
+ draw(canvas, 210, y, target, SkMatrixConvolutionImageFilter::kRepeat_TileMode);
+ }
+ }
+
+private:
+ typedef GM INHERITED;
+ SkBitmap fBitmap;
+ bool fInitialized;
+};
+
+//////////////////////////////////////////////////////////////////////////////
+
+static GM* MyFactory(void*) { return new MatrixConvolutionGM; }
+static GMRegistry reg(MyFactory);
+
+}