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/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkImageFilter.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkSize.h"
17 #include "include/core/SkString.h"
18 #include "include/core/SkTypeface.h"
19 #include "include/core/SkTypes.h"
20 #include "include/effects/SkImageFilters.h"
21 #include "tools/ToolUtils.h"
22 
23 #define WIDTH 700
24 #define HEIGHT 560
25 
26 namespace skiagm {
27 
28 class MorphologyGM : public GM {
29 public:
MorphologyGM()30     MorphologyGM() {
31         this->setBGColor(0xFF000000);
32     }
33 
34 protected:
onShortName()35     SkString onShortName() override {
36         return SkString("morphology");
37     }
38 
onOnceBeforeDraw()39     void onOnceBeforeDraw() override {
40         fBitmap.allocN32Pixels(135, 135);
41         SkCanvas canvas(fBitmap);
42         canvas.clear(0x0);
43 
44         SkFont  font(ToolUtils::create_portable_typeface(), 64.0f);
45         SkPaint paint;
46         paint.setColor(0xFFFFFFFF);
47         canvas.drawString("ABC", 10, 55,  font, paint);
48         canvas.drawString("XYZ", 10, 110, font, paint);
49     }
50 
onISize()51     SkISize onISize() override {
52         return SkISize::Make(WIDTH, HEIGHT);
53     }
54 
drawClippedBitmap(SkCanvas * canvas,const SkPaint & paint,int x,int y)55     void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) {
56         canvas->save();
57         canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
58         canvas->clipRect(SkRect::MakeWH(
59           SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
60         canvas->drawBitmap(fBitmap, 0, 0, &paint);
61         canvas->restore();
62     }
63 
onDraw(SkCanvas * canvas)64     void onDraw(SkCanvas* canvas) override {
65         struct {
66             int fWidth, fHeight;
67             int fRadiusX, fRadiusY;
68         } samples[] = {
69             { 140, 140,   0,   0 },
70             { 140, 140,   0,   2 },
71             { 140, 140,   2,   0 },
72             { 140, 140,   2,   2 },
73             {  24,  24,  25,  25 },
74         };
75         SkPaint paint;
76         SkIRect cropRect = SkIRect::MakeXYWH(25, 20, 100, 80);
77 
78         for (unsigned j = 0; j < 4; ++j) {
79             for (unsigned i = 0; i < SK_ARRAY_COUNT(samples); ++i) {
80                 const SkIRect* cr = j & 0x02 ? &cropRect : nullptr;
81                 if (j & 0x01) {
82                     paint.setImageFilter(SkImageFilters::Erode(
83                             samples[i].fRadiusX, samples[i].fRadiusY, nullptr, cr));
84                 } else {
85                     paint.setImageFilter(SkImageFilters::Dilate(
86                             samples[i].fRadiusX, samples[i].fRadiusY, nullptr, cr));
87                 }
88                 this->drawClippedBitmap(canvas, paint, i * 140, j * 140);
89             }
90         }
91     }
92 
93 private:
94     SkBitmap fBitmap;
95 
96     using INHERITED = GM;
97 };
98 
99 //////////////////////////////////////////////////////////////////////////////
100 
101 DEF_GM(return new MorphologyGM;)
102 
103 }  // namespace skiagm
104