blob: 99603c1ebc65bc0964998571a38a754445f1b6b1 [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
8#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000010#include "SkMorphologyImageFilter.h"
11
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000012#define WIDTH 700
13#define HEIGHT 560
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000014
15namespace skiagm {
16
17class MorphologyGM : public GM {
18public:
19 MorphologyGM() {
20 this->setBGColor(0xFF000000);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000021 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000022
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000023protected:
robertphillipsf299e712016-03-25 04:49:22 -070024 SkString onShortName() override {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000025 return SkString("morphology");
26 }
27
robertphillipsf299e712016-03-25 04:49:22 -070028 void onOnceBeforeDraw() override {
reed@google.comeb9a46c2014-01-25 16:46:20 +000029 fBitmap.allocN32Pixels(135, 135);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000030 SkCanvas canvas(fBitmap);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000031 canvas.clear(0x0);
32 SkPaint paint;
33 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070034 sk_tool_utils::set_portable_typeface(&paint);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000035 const char* str1 = "ABC";
36 const char* str2 = "XYZ";
37 paint.setColor(0xFFFFFFFF);
38 paint.setTextSize(64);
Cary Clark2a475ea2017-04-28 15:35:12 -040039 canvas.drawString(str1, 10, 55, paint);
40 canvas.drawString(str2, 10, 110, paint);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000041 }
42
robertphillipsf299e712016-03-25 04:49:22 -070043 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070044 return SkISize::Make(WIDTH, HEIGHT);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000045 }
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000046
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
robertphillipsf299e712016-03-25 04:49:22 -070056 void onDraw(SkCanvas* canvas) override {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000057 struct {
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000058 int fWidth, fHeight;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000059 int fRadiusX, fRadiusY;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000060 } samples[] = {
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000061 { 140, 140, 0, 0 },
62 { 140, 140, 0, 2 },
63 { 140, 140, 2, 0 },
64 { 140, 140, 2, 2 },
65 { 24, 24, 25, 25 },
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000066 };
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000067 SkPaint paint;
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +000068 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(25, 20, 100, 80));
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000069
70 for (unsigned j = 0; j < 4; ++j) {
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000071 for (unsigned i = 0; i < SK_ARRAY_COUNT(samples); ++i) {
halcanary96fcdcc2015-08-27 07:41:13 -070072 const SkImageFilter::CropRect* cr = j & 0x02 ? &cropRect : nullptr;
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000073 if (j & 0x01) {
robertphillipsfc11b0a2016-04-05 09:09:36 -070074 paint.setImageFilter(SkErodeImageFilter::Make(samples[i].fRadiusX,
75 samples[i].fRadiusY,
76 nullptr,
77 cr));
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000078 } else {
robertphillipsfc11b0a2016-04-05 09:09:36 -070079 paint.setImageFilter(SkDilateImageFilter::Make(samples[i].fRadiusX,
80 samples[i].fRadiusY,
81 nullptr,
82 cr));
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000083 }
robertphillipsf299e712016-03-25 04:49:22 -070084 this->drawClippedBitmap(canvas, paint, i * 140, j * 140);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000085 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000086 }
87 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000088
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000089private:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000090 SkBitmap fBitmap;
robertphillipsf299e712016-03-25 04:49:22 -070091
92 typedef GM INHERITED;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000093};
94
95//////////////////////////////////////////////////////////////////////////////
96
robertphillipsf299e712016-03-25 04:49:22 -070097DEF_GM(return new MorphologyGM;)
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000098
99}