senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/effects/SkMorphologyImageFilter.h" |
| 21 | #include "tools/ToolUtils.h" |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 22 | |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 23 | #define WIDTH 700 |
| 24 | #define HEIGHT 560 |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 25 | |
| 26 | namespace skiagm { |
| 27 | |
| 28 | class MorphologyGM : public GM { |
| 29 | public: |
| 30 | MorphologyGM() { |
| 31 | this->setBGColor(0xFF000000); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 32 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 33 | |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 34 | protected: |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 35 | SkString onShortName() override { |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 36 | return SkString("morphology"); |
| 37 | } |
| 38 | |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 39 | void onOnceBeforeDraw() override { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 40 | fBitmap.allocN32Pixels(135, 135); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 41 | SkCanvas canvas(fBitmap); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 42 | canvas.clear(0x0); |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 43 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 44 | SkFont font(ToolUtils::create_portable_typeface(), 64.0f); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 45 | SkPaint paint; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 46 | paint.setColor(0xFFFFFFFF); |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 47 | canvas.drawString("ABC", 10, 55, font, paint); |
| 48 | canvas.drawString("XYZ", 10, 110, font, paint); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 49 | } |
| 50 | |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 51 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 52 | return SkISize::Make(WIDTH, HEIGHT); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 53 | } |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 54 | |
| 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 | |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 64 | void onDraw(SkCanvas* canvas) override { |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 65 | struct { |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 66 | int fWidth, fHeight; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 67 | int fRadiusX, fRadiusY; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 68 | } samples[] = { |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 69 | { 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.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 74 | }; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 75 | SkPaint paint; |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 76 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(25, 20, 100, 80)); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 77 | |
| 78 | for (unsigned j = 0; j < 4; ++j) { |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 79 | for (unsigned i = 0; i < SK_ARRAY_COUNT(samples); ++i) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 80 | const SkImageFilter::CropRect* cr = j & 0x02 ? &cropRect : nullptr; |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 81 | if (j & 0x01) { |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 82 | paint.setImageFilter(SkErodeImageFilter::Make(samples[i].fRadiusX, |
| 83 | samples[i].fRadiusY, |
| 84 | nullptr, |
| 85 | cr)); |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 86 | } else { |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 87 | paint.setImageFilter(SkDilateImageFilter::Make(samples[i].fRadiusX, |
| 88 | samples[i].fRadiusY, |
| 89 | nullptr, |
| 90 | cr)); |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 91 | } |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 92 | this->drawClippedBitmap(canvas, paint, i * 140, j * 140); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 93 | } |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 96 | |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 97 | private: |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 98 | SkBitmap fBitmap; |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 99 | |
| 100 | typedef GM INHERITED; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | ////////////////////////////////////////////////////////////////////////////// |
| 104 | |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 105 | DEF_GM(return new MorphologyGM;) |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 106 | |
| 107 | } |