Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
morphology.cpp
Go to the documentation of this file.
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"
11#include "include/core/SkFont.h"
14#include "include/core/SkRect.h"
16#include "include/core/SkSize.h"
21#include "tools/ToolUtils.h"
23
24#define WIDTH 700
25#define HEIGHT 560
26
27namespace skiagm {
28
29class MorphologyGM : public GM {
30public:
32 this->setBGColor(0xFF000000);
33 }
34
35protected:
36 SkString getName() const override { return SkString("morphology"); }
37
38 void onOnceBeforeDraw() override {
40
43 paint.setColor(0xFFFFFFFF);
44 surf->getCanvas()->drawString("ABC", 10, 55, font, paint);
45 surf->getCanvas()->drawString("XYZ", 10, 110, font, paint);
46
47 fImage = surf->makeImageSnapshot();
48 }
49
50 SkISize getISize() override { return SkISize::Make(WIDTH, HEIGHT); }
51
52 void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) {
53 canvas->save();
55 canvas->clipIRect(fImage->bounds());
56 canvas->drawImage(fImage, 0, 0, SkSamplingOptions(), &paint);
57 canvas->restore();
58 }
59
60 void onDraw(SkCanvas* canvas) override {
61 struct {
62 int fWidth, fHeight;
63 int fRadiusX, fRadiusY;
64 } samples[] = {
65 { 140, 140, 0, 0 },
66 { 140, 140, 0, 2 },
67 { 140, 140, 2, 0 },
68 { 140, 140, 2, 2 },
69 { 24, 24, 25, 25 },
70 };
72 SkIRect cropRect = SkIRect::MakeXYWH(25, 20, 100, 80);
73
74 for (unsigned j = 0; j < 4; ++j) {
75 for (unsigned i = 0; i < std::size(samples); ++i) {
76 const SkIRect* cr = j & 0x02 ? &cropRect : nullptr;
77 if (j & 0x01) {
78 paint.setImageFilter(SkImageFilters::Erode(
79 samples[i].fRadiusX, samples[i].fRadiusY, nullptr, cr));
80 } else {
81 paint.setImageFilter(SkImageFilters::Dilate(
82 samples[i].fRadiusX, samples[i].fRadiusY, nullptr, cr));
83 }
84 this->drawClippedBitmap(canvas, paint, i * 140, j * 140);
85 }
86 }
87 }
88
89private:
90 sk_sp<SkImage> fImage;
91
92 using INHERITED = GM;
93};
94
95//////////////////////////////////////////////////////////////////////////////
96
97DEF_GM(return new MorphologyGM;)
98
99} // namespace skiagm
#define SkIntToScalar(x)
Definition SkScalar.h:57
#define WIDTH
#define HEIGHT
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
void clipIRect(const SkIRect &irect, SkClipOp op=SkClipOp::kIntersect)
Definition SkCanvas.h:991
int save()
Definition SkCanvas.cpp:451
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
static sk_sp< SkImageFilter > Erode(SkScalar radiusX, SkScalar radiusY, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Dilate(SkScalar radiusX, SkScalar radiusY, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
SkIRect bounds() const
Definition SkImage.h:303
void setBGColor(SkColor)
Definition gm.cpp:159
SkISize getISize() override
SkString getName() const override
void onOnceBeforeDraw() override
void drawClippedBitmap(SkCanvas *canvas, const SkPaint &paint, int x, int y)
void onDraw(SkCanvas *canvas) override
const Paint & paint
#define DEF_GM(CODE)
Definition gm.h:40
double y
double x
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
sk_sp< SkTypeface > DefaultPortableTypeface()
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition SkRect.h:104
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static SkImageInfo MakeN32Premul(int width, int height)