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); |
| 20 | fOnce = false; |
| 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: |
| 24 | virtual SkString onShortName() { |
| 25 | return SkString("morphology"); |
| 26 | } |
| 27 | |
| 28 | void make_bitmap() { |
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); |
| 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 | |
| 42 | virtual SkISize onISize() { |
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 | |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 55 | virtual void onDraw(SkCanvas* canvas) { |
| 56 | if (!fOnce) { |
| 57 | make_bitmap(); |
| 58 | fOnce = true; |
| 59 | } |
| 60 | struct { |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 61 | int fWidth, fHeight; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 62 | int fRadiusX, fRadiusY; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 63 | } samples[] = { |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 64 | { 140, 140, 0, 0 }, |
| 65 | { 140, 140, 0, 2 }, |
| 66 | { 140, 140, 2, 0 }, |
| 67 | { 140, 140, 2, 2 }, |
| 68 | { 24, 24, 25, 25 }, |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 69 | }; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 70 | SkPaint paint; |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 71 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(25, 20, 100, 80)); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 72 | |
| 73 | for (unsigned j = 0; j < 4; ++j) { |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 74 | for (unsigned i = 0; i < SK_ARRAY_COUNT(samples); ++i) { |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 75 | const SkImageFilter::CropRect* cr = j & 0x02 ? &cropRect : NULL; |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 76 | if (j & 0x01) { |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 77 | paint.setImageFilter(SkErodeImageFilter::Create( |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 78 | samples[i].fRadiusX, |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 79 | samples[i].fRadiusY, |
| 80 | NULL, |
| 81 | cr))->unref(); |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 82 | } else { |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 83 | paint.setImageFilter(SkDilateImageFilter::Create( |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 84 | samples[i].fRadiusX, |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 85 | samples[i].fRadiusY, |
| 86 | NULL, |
| 87 | cr))->unref(); |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 88 | } |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 89 | drawClippedBitmap(canvas, paint, i * 140, j * 140); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 90 | } |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 93 | |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 94 | private: |
| 95 | typedef GM INHERITED; |
| 96 | SkBitmap fBitmap; |
| 97 | bool fOnce; |
| 98 | }; |
| 99 | |
| 100 | ////////////////////////////////////////////////////////////////////////////// |
| 101 | |
| 102 | static GM* MyFactory(void*) { return new MorphologyGM; } |
| 103 | static GMRegistry reg(MyFactory); |
| 104 | |
| 105 | } |