blob: ef2174e82e7428806fbb2a3ba7280bafd16b275e [file] [log] [blame]
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/effects/SkMorphologyImageFilter.h"
21#include "tools/ToolUtils.h"
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000022
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000023#define WIDTH 700
24#define HEIGHT 560
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000025
26namespace skiagm {
27
28class MorphologyGM : public GM {
29public:
30 MorphologyGM() {
31 this->setBGColor(0xFF000000);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000032 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000033
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000034protected:
robertphillipsf299e712016-03-25 04:49:22 -070035 SkString onShortName() override {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000036 return SkString("morphology");
37 }
38
robertphillipsf299e712016-03-25 04:49:22 -070039 void onOnceBeforeDraw() override {
reed@google.comeb9a46c2014-01-25 16:46:20 +000040 fBitmap.allocN32Pixels(135, 135);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000041 SkCanvas canvas(fBitmap);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000042 canvas.clear(0x0);
Hal Canarydf2d27e2019-01-08 09:38:02 -050043
Mike Kleinea3f0142019-03-20 11:12:10 -050044 SkFont font(ToolUtils::create_portable_typeface(), 64.0f);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000045 SkPaint paint;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000046 paint.setColor(0xFFFFFFFF);
Hal Canarydf2d27e2019-01-08 09:38:02 -050047 canvas.drawString("ABC", 10, 55, font, paint);
48 canvas.drawString("XYZ", 10, 110, font, paint);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000049 }
50
robertphillipsf299e712016-03-25 04:49:22 -070051 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070052 return SkISize::Make(WIDTH, HEIGHT);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000053 }
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000054
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
robertphillipsf299e712016-03-25 04:49:22 -070064 void onDraw(SkCanvas* canvas) override {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000065 struct {
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000066 int fWidth, fHeight;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000067 int fRadiusX, fRadiusY;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000068 } samples[] = {
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000069 { 140, 140, 0, 0 },
70 { 140, 140, 0, 2 },
71 { 140, 140, 2, 0 },
72 { 140, 140, 2, 2 },
73 { 24, 24, 25, 25 },
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000074 };
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000075 SkPaint paint;
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +000076 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(25, 20, 100, 80));
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000077
78 for (unsigned j = 0; j < 4; ++j) {
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000079 for (unsigned i = 0; i < SK_ARRAY_COUNT(samples); ++i) {
halcanary96fcdcc2015-08-27 07:41:13 -070080 const SkImageFilter::CropRect* cr = j & 0x02 ? &cropRect : nullptr;
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000081 if (j & 0x01) {
robertphillipsfc11b0a2016-04-05 09:09:36 -070082 paint.setImageFilter(SkErodeImageFilter::Make(samples[i].fRadiusX,
83 samples[i].fRadiusY,
84 nullptr,
85 cr));
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000086 } else {
robertphillipsfc11b0a2016-04-05 09:09:36 -070087 paint.setImageFilter(SkDilateImageFilter::Make(samples[i].fRadiusX,
88 samples[i].fRadiusY,
89 nullptr,
90 cr));
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000091 }
robertphillipsf299e712016-03-25 04:49:22 -070092 this->drawClippedBitmap(canvas, paint, i * 140, j * 140);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000093 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000094 }
95 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000096
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000097private:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000098 SkBitmap fBitmap;
robertphillipsf299e712016-03-25 04:49:22 -070099
100 typedef GM INHERITED;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000101};
102
103//////////////////////////////////////////////////////////////////////////////
104
robertphillipsf299e712016-03-25 04:49:22 -0700105DEF_GM(return new MorphologyGM;)
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000106
107}