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 | |
| 11 | #define WIDTH 640 |
| 12 | #define HEIGHT 480 |
| 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() { |
| 29 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 135, 135); |
| 30 | fBitmap.allocPixels(); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 31 | SkBitmapDevice device(fBitmap); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 32 | SkCanvas canvas(&device); |
| 33 | canvas.clear(0x0); |
| 34 | SkPaint paint; |
| 35 | paint.setAntiAlias(true); |
| 36 | const char* str1 = "ABC"; |
| 37 | const char* str2 = "XYZ"; |
| 38 | paint.setColor(0xFFFFFFFF); |
| 39 | paint.setTextSize(64); |
| 40 | canvas.drawText(str1, strlen(str1), 10, 55, paint); |
| 41 | canvas.drawText(str2, strlen(str2), 10, 110, paint); |
| 42 | } |
| 43 | |
| 44 | virtual SkISize onISize() { |
| 45 | return make_isize(WIDTH, HEIGHT); |
| 46 | } |
| 47 | virtual void onDraw(SkCanvas* canvas) { |
| 48 | if (!fOnce) { |
| 49 | make_bitmap(); |
| 50 | fOnce = true; |
| 51 | } |
| 52 | struct { |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 53 | int fWidth, fHeight; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 54 | int fRadiusX, fRadiusY; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 55 | } samples[] = { |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 56 | { 140, 140, 0, 0 }, |
| 57 | { 140, 140, 0, 2 }, |
| 58 | { 140, 140, 2, 0 }, |
| 59 | { 140, 140, 2, 2 }, |
| 60 | { 24, 24, 25, 25 }, |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 61 | }; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 62 | SkPaint paint; |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 63 | for (unsigned j = 0; j < 2; ++j) { |
| 64 | for (unsigned i = 0; i < SK_ARRAY_COUNT(samples); ++i) { |
| 65 | SkScalar x = SkIntToScalar(i * 140), y = SkIntToScalar(j * 140); |
| 66 | if (j) { |
| 67 | paint.setImageFilter(new SkErodeImageFilter( |
| 68 | samples[i].fRadiusX, |
| 69 | samples[i].fRadiusY))->unref(); |
| 70 | } else { |
| 71 | paint.setImageFilter(new SkDilateImageFilter( |
| 72 | samples[i].fRadiusX, |
| 73 | samples[i].fRadiusY))->unref(); |
| 74 | } |
| 75 | SkRect bounds = SkRect::MakeXYWH( |
| 76 | x, |
| 77 | y, |
| 78 | SkIntToScalar(samples[i].fWidth), |
| 79 | SkIntToScalar(samples[i].fHeight)); |
| 80 | canvas->saveLayer(&bounds, &paint); |
| 81 | canvas->drawBitmap(fBitmap, x, y); |
| 82 | canvas->restore(); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 83 | } |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 86 | |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 87 | private: |
| 88 | typedef GM INHERITED; |
| 89 | SkBitmap fBitmap; |
| 90 | bool fOnce; |
| 91 | }; |
| 92 | |
| 93 | ////////////////////////////////////////////////////////////////////////////// |
| 94 | |
| 95 | static GM* MyFactory(void*) { return new MorphologyGM; } |
| 96 | static GMRegistry reg(MyFactory); |
| 97 | |
| 98 | } |