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