• 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         SkPaint paint;
33         paint.setAntiAlias(true);
34         sk_tool_utils::set_portable_typeface(&paint);
35         const char* str1 = "ABC";
36         const char* str2 = "XYZ";
37         paint.setColor(0xFFFFFFFF);
38         paint.setTextSize(64);
39         canvas.drawString(str1, 10, 55, paint);
40         canvas.drawString(str2, 10, 110, paint);
41     }
42 
onISize()43     SkISize onISize() override {
44         return SkISize::Make(WIDTH, HEIGHT);
45     }
46 
drawClippedBitmap(SkCanvas * canvas,const SkPaint & paint,int x,int y)47     void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) {
48         canvas->save();
49         canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
50         canvas->clipRect(SkRect::MakeWH(
51           SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
52         canvas->drawBitmap(fBitmap, 0, 0, &paint);
53         canvas->restore();
54     }
55 
onDraw(SkCanvas * canvas)56     void onDraw(SkCanvas* canvas) override {
57         struct {
58             int fWidth, fHeight;
59             int fRadiusX, fRadiusY;
60         } samples[] = {
61             { 140, 140,   0,   0 },
62             { 140, 140,   0,   2 },
63             { 140, 140,   2,   0 },
64             { 140, 140,   2,   2 },
65             {  24,  24,  25,  25 },
66         };
67         SkPaint paint;
68         SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(25, 20, 100, 80));
69 
70         for (unsigned j = 0; j < 4; ++j) {
71             for (unsigned i = 0; i < SK_ARRAY_COUNT(samples); ++i) {
72                 const SkImageFilter::CropRect* cr = j & 0x02 ? &cropRect : nullptr;
73                 if (j & 0x01) {
74                     paint.setImageFilter(SkErodeImageFilter::Make(samples[i].fRadiusX,
75                                                                   samples[i].fRadiusY,
76                                                                   nullptr,
77                                                                   cr));
78                 } else {
79                     paint.setImageFilter(SkDilateImageFilter::Make(samples[i].fRadiusX,
80                                                                    samples[i].fRadiusY,
81                                                                    nullptr,
82                                                                    cr));
83                 }
84                 this->drawClippedBitmap(canvas, paint, i * 140, j * 140);
85             }
86         }
87     }
88 
89 private:
90     SkBitmap fBitmap;
91 
92     typedef GM INHERITED;
93 };
94 
95 //////////////////////////////////////////////////////////////////////////////
96 
97 DEF_GM(return new MorphologyGM;)
98 
99 }
100