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