blob: 83de758a9b32a716c435261c06760cd5cef1108f [file] [log] [blame]
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001/*
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
14namespace skiagm {
15
16class MorphologyGM : public GM {
17public:
18 MorphologyGM() {
19 this->setBGColor(0xFF000000);
20 fOnce = false;
21 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000022
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000023protected:
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();
31 SkDevice device(fBitmap);
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.org56dd6302012-04-10 17:25:44 +000053 int fWidth, fHeight;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000054 int fRadiusX, fRadiusY;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000055 } samples[] = {
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000056 { 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.org05054f12012-03-02 21:05:45 +000061 };
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000062 SkPaint paint;
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000063 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.org05054f12012-03-02 21:05:45 +000083 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000084 }
85 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000086
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000087private:
88 typedef GM INHERITED;
89 SkBitmap fBitmap;
90 bool fOnce;
91};
92
93//////////////////////////////////////////////////////////////////////////////
94
95static GM* MyFactory(void*) { return new MorphologyGM; }
96static GMRegistry reg(MyFactory);
97
98}