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