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 | bda591c | 2014-02-13 15:29:03 +0000 | [diff] [blame^] | 30 | SkBitmapDevice device(fBitmap); |
| 31 | SkCanvas canvas(&device); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 32 | canvas.clear(0x0); |
| 33 | SkPaint paint; |
| 34 | paint.setAntiAlias(true); |
| 35 | const char* str1 = "ABC"; |
| 36 | const char* str2 = "XYZ"; |
| 37 | paint.setColor(0xFFFFFFFF); |
| 38 | paint.setTextSize(64); |
| 39 | canvas.drawText(str1, strlen(str1), 10, 55, paint); |
| 40 | canvas.drawText(str2, strlen(str2), 10, 110, paint); |
| 41 | } |
| 42 | |
| 43 | virtual SkISize onISize() { |
| 44 | return make_isize(WIDTH, HEIGHT); |
| 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 | |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 56 | virtual void onDraw(SkCanvas* canvas) { |
| 57 | if (!fOnce) { |
| 58 | make_bitmap(); |
| 59 | fOnce = true; |
| 60 | } |
| 61 | struct { |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 62 | int fWidth, fHeight; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 63 | int fRadiusX, fRadiusY; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 64 | } samples[] = { |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 65 | { 140, 140, 0, 0 }, |
| 66 | { 140, 140, 0, 2 }, |
| 67 | { 140, 140, 2, 0 }, |
| 68 | { 140, 140, 2, 2 }, |
| 69 | { 24, 24, 25, 25 }, |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 70 | }; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 71 | SkPaint paint; |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 72 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(25, 20, 100, 80)); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 73 | |
| 74 | for (unsigned j = 0; j < 4; ++j) { |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 75 | for (unsigned i = 0; i < SK_ARRAY_COUNT(samples); ++i) { |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 76 | const SkImageFilter::CropRect* cr = j & 0x02 ? &cropRect : NULL; |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 77 | if (j & 0x01) { |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 78 | paint.setImageFilter(new SkErodeImageFilter( |
| 79 | samples[i].fRadiusX, |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 80 | samples[i].fRadiusY, |
| 81 | NULL, |
| 82 | cr))->unref(); |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 83 | } else { |
| 84 | paint.setImageFilter(new SkDilateImageFilter( |
| 85 | samples[i].fRadiusX, |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 86 | samples[i].fRadiusY, |
| 87 | NULL, |
| 88 | cr))->unref(); |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 89 | } |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 90 | drawClippedBitmap(canvas, paint, i * 140, j * 140); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 91 | } |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 94 | |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 95 | private: |
| 96 | typedef GM INHERITED; |
| 97 | SkBitmap fBitmap; |
| 98 | bool fOnce; |
| 99 | }; |
| 100 | |
| 101 | ////////////////////////////////////////////////////////////////////////////// |
| 102 | |
| 103 | static GM* MyFactory(void*) { return new MorphologyGM; } |
| 104 | static GMRegistry reg(MyFactory); |
| 105 | |
| 106 | } |