blob: cb321c6efd36e060277fbef1894da61835ea6623 [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);
Cary Clark992c7b02014-07-31 08:58:44 -040034 sk_tool_utils::set_portable_typeface(&paint);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000035 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() {
tfarinaf5393182014-06-09 23:59:03 -070044 return SkISize::Make(WIDTH, HEIGHT);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000045 }
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000046
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.org05054f12012-03-02 21:05:45 +000056 virtual void onDraw(SkCanvas* canvas) {
57 if (!fOnce) {
58 make_bitmap();
59 fOnce = true;
60 }
61 struct {
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000062 int fWidth, fHeight;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000063 int fRadiusX, fRadiusY;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000064 } samples[] = {
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000065 { 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.org05054f12012-03-02 21:05:45 +000070 };
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000071 SkPaint paint;
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +000072 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(25, 20, 100, 80));
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000073
74 for (unsigned j = 0; j < 4; ++j) {
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000075 for (unsigned i = 0; i < SK_ARRAY_COUNT(samples); ++i) {
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +000076 const SkImageFilter::CropRect* cr = j & 0x02 ? &cropRect : NULL;
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000077 if (j & 0x01) {
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000078 paint.setImageFilter(SkErodeImageFilter::Create(
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000079 samples[i].fRadiusX,
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000080 samples[i].fRadiusY,
81 NULL,
82 cr))->unref();
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000083 } else {
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000084 paint.setImageFilter(SkDilateImageFilter::Create(
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000085 samples[i].fRadiusX,
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000086 samples[i].fRadiusY,
87 NULL,
88 cr))->unref();
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000089 }
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000090 drawClippedBitmap(canvas, paint, i * 140, j * 140);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000091 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000092 }
93 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000094
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000095private:
96 typedef GM INHERITED;
97 SkBitmap fBitmap;
98 bool fOnce;
99};
100
101//////////////////////////////////////////////////////////////////////////////
102
103static GM* MyFactory(void*) { return new MorphologyGM; }
104static GMRegistry reg(MyFactory);
105
106}