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 "SkColor.h"
10#include "SkMatrixConvolutionImageFilter.h"
11#include "SkGradientShader.h"
12
13namespace skiagm {
14
15class MatrixConvolutionGM : public GM {
16public:
17    MatrixConvolutionGM() : fInitialized(false) {
18        this->setBGColor(0x00000000);
19    }
20
21protected:
22    virtual uint32_t onGetFlags() const SK_OVERRIDE {
23        return kSkipTiled_Flag;
24    }
25
26    virtual SkString onShortName() {
27        return SkString("matrixconvolution");
28    }
29
30    void make_bitmap() {
31        fBitmap.allocN32Pixels(80, 80);
32        SkCanvas canvas(fBitmap);
33        canvas.clear(0x00000000);
34        SkPaint paint;
35        paint.setAntiAlias(true);
36        sk_tool_utils::set_portable_typeface(&paint);
37        paint.setColor(0xFFFFFFFF);
38        paint.setTextSize(SkIntToScalar(180));
39        SkPoint pts[2] = { SkPoint::Make(0, 0),
40                           SkPoint::Make(0, SkIntToScalar(80)) };
41        SkColor colors[2] = { 0xFFFFFFFF, 0x40404040 };
42        SkScalar pos[2] = { 0, SkIntToScalar(80) };
43        paint.setShader(SkGradientShader::CreateLinear(
44            pts, colors, pos, 2, SkShader::kClamp_TileMode))->unref();
45        const char* str = "e";
46        canvas.drawText(str, strlen(str), SkIntToScalar(-10), SkIntToScalar(80), paint);
47    }
48
49    virtual SkISize onISize() {
50        return SkISize::Make(500, 300);
51    }
52
53    void draw(SkCanvas* canvas, int x, int y, const SkIPoint& kernelOffset,
54              SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAlpha,
55              const SkImageFilter::CropRect* cropRect = NULL) {
56        SkScalar kernel[9] = {
57            SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
58            SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1),
59            SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
60        };
61        SkISize kernelSize = SkISize::Make(3, 3);
62        SkScalar gain = 0.3f, bias = SkIntToScalar(100);
63        SkPaint paint;
64        SkAutoTUnref<SkImageFilter> filter(
65            SkMatrixConvolutionImageFilter::Create(kernelSize,
66                                                   kernel,
67                                                   gain,
68                                                   bias,
69                                                   kernelOffset,
70                                                   tileMode,
71                                                   convolveAlpha,
72                                                   NULL,
73                                                   cropRect));
74        paint.setImageFilter(filter);
75        canvas->save();
76        canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
77        canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()),
78                                        SkIntToScalar(fBitmap.height())));
79        canvas->drawBitmap(fBitmap, 0, 0, &paint);
80        canvas->restore();
81    }
82
83    typedef SkMatrixConvolutionImageFilter MCIF;
84
85    virtual void onDraw(SkCanvas* canvas) {
86        if (!fInitialized) {
87            make_bitmap();
88            fInitialized = true;
89        }
90        canvas->clear(0x00000000);
91        SkIPoint kernelOffset = SkIPoint::Make(1, 0);
92        for (int x = 10; x < 310; x += 100) {
93            this->draw(canvas, x, 10, kernelOffset, MCIF::kClamp_TileMode, true);
94            this->draw(canvas, x, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true);
95            this->draw(canvas, x, 210, kernelOffset, MCIF::kRepeat_TileMode, true);
96            kernelOffset.fY++;
97        }
98        kernelOffset.fY = 1;
99        SkImageFilter::CropRect rect(SkRect::MakeXYWH(10, 5, 60, 60));
100        this->draw(canvas, 310, 10, kernelOffset, MCIF::kClamp_TileMode, true, &rect);
101        this->draw(canvas, 310, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true, &rect);
102        this->draw(canvas, 310, 210, kernelOffset, MCIF::kRepeat_TileMode, true, &rect);
103
104        this->draw(canvas, 410, 10, kernelOffset, MCIF::kClamp_TileMode, false);
105        this->draw(canvas, 410, 110, kernelOffset, MCIF::kClampToBlack_TileMode, false);
106        this->draw(canvas, 410, 210, kernelOffset, MCIF::kRepeat_TileMode, false);
107    }
108
109private:
110    typedef GM INHERITED;
111    SkBitmap fBitmap;
112    bool fInitialized;
113};
114
115//////////////////////////////////////////////////////////////////////////////
116
117static GM* MyFactory(void*) { return new MatrixConvolutionGM; }
118static GMRegistry reg(MyFactory);
119
120}
121