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 "SkMorphologyImageFilter.h"
10
11#define WIDTH 700
12#define HEIGHT 560
13
14namespace skiagm {
15
16class MorphologyGM : public GM {
17public:
18    MorphologyGM() {
19        this->setBGColor(0xFF000000);
20        fOnce = false;
21    }
22
23protected:
24    virtual SkString onShortName() {
25        return SkString("morphology");
26    }
27
28    void make_bitmap() {
29        fBitmap.allocN32Pixels(135, 135);
30        SkCanvas canvas(fBitmap);
31        canvas.clear(0x0);
32        SkPaint paint;
33        paint.setAntiAlias(true);
34        const char* str1 = "ABC";
35        const char* str2 = "XYZ";
36        paint.setColor(0xFFFFFFFF);
37        paint.setTextSize(64);
38        canvas.drawText(str1, strlen(str1), 10, 55, paint);
39        canvas.drawText(str2, strlen(str2), 10, 110, paint);
40    }
41
42    virtual SkISize onISize() {
43        return SkISize::Make(WIDTH, HEIGHT);
44    }
45
46    void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) {
47        canvas->save();
48        canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
49        canvas->clipRect(SkRect::MakeWH(
50          SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
51        canvas->drawBitmap(fBitmap, 0, 0, &paint);
52        canvas->restore();
53    }
54
55    virtual void onDraw(SkCanvas* canvas) {
56        if (!fOnce) {
57            make_bitmap();
58            fOnce = true;
59        }
60        struct {
61            int fWidth, fHeight;
62            int fRadiusX, fRadiusY;
63        } samples[] = {
64            { 140, 140,   0,   0 },
65            { 140, 140,   0,   2 },
66            { 140, 140,   2,   0 },
67            { 140, 140,   2,   2 },
68            {  24,  24,  25,  25 },
69        };
70        SkPaint paint;
71        SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(25, 20, 100, 80));
72
73        for (unsigned j = 0; j < 4; ++j) {
74            for (unsigned i = 0; i < SK_ARRAY_COUNT(samples); ++i) {
75                const SkImageFilter::CropRect* cr = j & 0x02 ? &cropRect : NULL;
76                if (j & 0x01) {
77                    paint.setImageFilter(SkErodeImageFilter::Create(
78                        samples[i].fRadiusX,
79                        samples[i].fRadiusY,
80                        NULL,
81                        cr))->unref();
82                } else {
83                    paint.setImageFilter(SkDilateImageFilter::Create(
84                        samples[i].fRadiusX,
85                        samples[i].fRadiusY,
86                        NULL,
87                        cr))->unref();
88                }
89                drawClippedBitmap(canvas, paint, i * 140, j * 140);
90            }
91        }
92    }
93
94private:
95    typedef GM INHERITED;
96    SkBitmap fBitmap;
97    bool fOnce;
98};
99
100//////////////////////////////////////////////////////////////////////////////
101
102static GM* MyFactory(void*) { return new MorphologyGM; }
103static GMRegistry reg(MyFactory);
104
105}
106