• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         auto surf = SkSurface::MakeRasterN32Premul(135, 135);
41 
42         SkFont  font(ToolUtils::create_portable_typeface(), 64.0f);
43         SkPaint paint;
44         paint.setColor(0xFFFFFFFF);
45         surf->getCanvas()->drawString("ABC", 10, 55,  font, paint);
46         surf->getCanvas()->drawString("XYZ", 10, 110, font, paint);
47 
48         fImage = surf->makeImageSnapshot();
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->clipIRect(fImage->bounds());
59         canvas->drawImage(fImage, 0, 0, SkSamplingOptions(), &paint);
60         canvas->restore();
61     }
62 
onDraw(SkCanvas * canvas)63     void onDraw(SkCanvas* canvas) override {
64         struct {
65             int fWidth, fHeight;
66             int fRadiusX, fRadiusY;
67         } samples[] = {
68             { 140, 140,   0,   0 },
69             { 140, 140,   0,   2 },
70             { 140, 140,   2,   0 },
71             { 140, 140,   2,   2 },
72             {  24,  24,  25,  25 },
73         };
74         SkPaint paint;
75         SkIRect cropRect = SkIRect::MakeXYWH(25, 20, 100, 80);
76 
77         for (unsigned j = 0; j < 4; ++j) {
78             for (unsigned i = 0; i < SK_ARRAY_COUNT(samples); ++i) {
79                 const SkIRect* cr = j & 0x02 ? &cropRect : nullptr;
80                 if (j & 0x01) {
81                     paint.setImageFilter(SkImageFilters::Erode(
82                             samples[i].fRadiusX, samples[i].fRadiusY, nullptr, cr));
83                 } else {
84                     paint.setImageFilter(SkImageFilters::Dilate(
85                             samples[i].fRadiusX, samples[i].fRadiusY, nullptr, cr));
86                 }
87                 this->drawClippedBitmap(canvas, paint, i * 140, j * 140);
88             }
89         }
90     }
91 
92 private:
93     sk_sp<SkImage> fImage;
94 
95     using INHERITED = GM;
96 };
97 
98 //////////////////////////////////////////////////////////////////////////////
99 
100 DEF_GM(return new MorphologyGM;)
101 
102 }  // namespace skiagm
103