senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "gm.h" |
| 9 | #include "SkMatrixConvolutionImageFilter.h" |
| 10 | |
| 11 | namespace skiagm { |
| 12 | |
| 13 | class MatrixConvolutionGM : public GM { |
| 14 | public: |
| 15 | MatrixConvolutionGM() : fInitialized(false) { |
| 16 | this->setBGColor(0x00000000); |
| 17 | } |
| 18 | |
| 19 | protected: |
| 20 | virtual SkString onShortName() { |
| 21 | return SkString("matrixconvolution"); |
| 22 | } |
| 23 | |
| 24 | void make_bitmap() { |
| 25 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 80, 80); |
| 26 | fBitmap.allocPixels(); |
| 27 | SkDevice device(fBitmap); |
| 28 | SkCanvas canvas(&device); |
| 29 | canvas.clear(0x00000000); |
| 30 | SkPaint paint; |
| 31 | paint.setAntiAlias(true); |
| 32 | paint.setColor(0xFFFFFFFF); |
| 33 | paint.setTextSize(SkIntToScalar(180)); |
| 34 | const char* str = "e"; |
| 35 | canvas.drawText(str, strlen(str), SkIntToScalar(-10), SkIntToScalar(80), paint); |
| 36 | } |
| 37 | |
| 38 | virtual SkISize onISize() { |
| 39 | return make_isize(300, 300); |
| 40 | } |
| 41 | |
| 42 | void draw(SkCanvas* canvas, int x, int y, const SkIPoint& target, SkMatrixConvolutionImageFilter::TileMode tileMode) { |
| 43 | SkScalar kernel[9] = { |
| 44 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 45 | SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1), |
| 46 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 47 | }; |
| 48 | SkISize kernelSize = SkISize::Make(3, 3); |
| 49 | SkScalar gain = SkFloatToScalar(0.3f), bias = SkIntToScalar(100); |
| 50 | SkPaint paint; |
| 51 | SkAutoTUnref<SkImageFilter> filter(SkNEW_ARGS(SkMatrixConvolutionImageFilter, (kernelSize, kernel, gain, bias, target, tileMode))); |
| 52 | paint.setImageFilter(filter); |
| 53 | canvas->drawSprite(fBitmap, x, y, &paint); |
| 54 | } |
| 55 | |
| 56 | virtual void onDraw(SkCanvas* canvas) { |
| 57 | if (!fInitialized) { |
| 58 | make_bitmap(); |
| 59 | fInitialized = true; |
| 60 | } |
| 61 | canvas->clear(0x00000000); |
| 62 | SkIPoint target = SkIPoint::Make(1, 1); |
| 63 | for (target.fY = 0; target.fY < 3; ++target.fY) { |
| 64 | int y = target.fY * 100 + 10; |
| 65 | draw(canvas, 10, y, target, SkMatrixConvolutionImageFilter::kClamp_TileMode); |
| 66 | draw(canvas, 110, y, target, SkMatrixConvolutionImageFilter::kClampToBlack_TileMode); |
| 67 | draw(canvas, 210, y, target, SkMatrixConvolutionImageFilter::kRepeat_TileMode); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | private: |
| 72 | typedef GM INHERITED; |
| 73 | SkBitmap fBitmap; |
| 74 | bool fInitialized; |
| 75 | }; |
| 76 | |
| 77 | ////////////////////////////////////////////////////////////////////////////// |
| 78 | |
| 79 | static GM* MyFactory(void*) { return new MatrixConvolutionGM; } |
| 80 | static GMRegistry reg(MyFactory); |
| 81 | |
| 82 | } |