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