blob: ff6c1bac6c8f2ead90ad4a3529ca93f6ffa71718 [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
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000011#define WIDTH 700
12#define HEIGHT 560
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000013
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() {
reed@google.comeb9a46c2014-01-25 16:46:20 +000029 fBitmap.allocN32Pixels(135, 135);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000030 SkCanvas canvas(fBitmap);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000031 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() {
tfarinaf5393182014-06-09 23:59:03 -070043 return SkISize::Make(WIDTH, HEIGHT);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000044 }
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000045
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.org05054f12012-03-02 21:05:45 +000055 virtual void onDraw(SkCanvas* canvas) {
56 if (!fOnce) {
57 make_bitmap();
58 fOnce = true;
59 }
60 struct {
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000061 int fWidth, fHeight;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000062 int fRadiusX, fRadiusY;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000063 } samples[] = {
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000064 { 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.org05054f12012-03-02 21:05:45 +000069 };
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000070 SkPaint paint;
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +000071 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(25, 20, 100, 80));
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000072
73 for (unsigned j = 0; j < 4; ++j) {
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000074 for (unsigned i = 0; i < SK_ARRAY_COUNT(samples); ++i) {
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +000075 const SkImageFilter::CropRect* cr = j & 0x02 ? &cropRect : NULL;
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000076 if (j & 0x01) {
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000077 paint.setImageFilter(SkErodeImageFilter::Create(
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000078 samples[i].fRadiusX,
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000079 samples[i].fRadiusY,
80 NULL,
81 cr))->unref();
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000082 } else {
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000083 paint.setImageFilter(SkDilateImageFilter::Create(
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000084 samples[i].fRadiusX,
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000085 samples[i].fRadiusY,
86 NULL,
87 cr))->unref();
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000088 }
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000089 drawClippedBitmap(canvas, paint, i * 140, j * 140);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000090 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000091 }
92 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000093
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000094private:
95 typedef GM INHERITED;
96 SkBitmap fBitmap;
97 bool fOnce;
98};
99
100//////////////////////////////////////////////////////////////////////////////
101
102static GM* MyFactory(void*) { return new MorphologyGM; }
103static GMRegistry reg(MyFactory);
104
105}