blob: f60079b873f2a0c514c6dd46a74ec10eca1e7d1e [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);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000020 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000021
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000022protected:
robertphillipsf299e712016-03-25 04:49:22 -070023 SkString onShortName() override {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000024 return SkString("morphology");
25 }
26
robertphillipsf299e712016-03-25 04:49:22 -070027 void onOnceBeforeDraw() override {
reed@google.comeb9a46c2014-01-25 16:46:20 +000028 fBitmap.allocN32Pixels(135, 135);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000029 SkCanvas canvas(fBitmap);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000030 canvas.clear(0x0);
31 SkPaint paint;
32 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070033 sk_tool_utils::set_portable_typeface(&paint);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000034 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
robertphillipsf299e712016-03-25 04:49:22 -070042 SkISize onISize() override {
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
robertphillipsf299e712016-03-25 04:49:22 -070055 void onDraw(SkCanvas* canvas) override {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000056 struct {
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000057 int fWidth, fHeight;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000058 int fRadiusX, fRadiusY;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000059 } samples[] = {
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000060 { 140, 140, 0, 0 },
61 { 140, 140, 0, 2 },
62 { 140, 140, 2, 0 },
63 { 140, 140, 2, 2 },
64 { 24, 24, 25, 25 },
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000065 };
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000066 SkPaint paint;
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +000067 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(25, 20, 100, 80));
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000068
69 for (unsigned j = 0; j < 4; ++j) {
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000070 for (unsigned i = 0; i < SK_ARRAY_COUNT(samples); ++i) {
halcanary96fcdcc2015-08-27 07:41:13 -070071 const SkImageFilter::CropRect* cr = j & 0x02 ? &cropRect : nullptr;
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +000072 if (j & 0x01) {
robertphillipsfc11b0a2016-04-05 09:09:36 -070073 paint.setImageFilter(SkErodeImageFilter::Make(samples[i].fRadiusX,
74 samples[i].fRadiusY,
75 nullptr,
76 cr));
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000077 } else {
robertphillipsfc11b0a2016-04-05 09:09:36 -070078 paint.setImageFilter(SkDilateImageFilter::Make(samples[i].fRadiusX,
79 samples[i].fRadiusY,
80 nullptr,
81 cr));
senorblanco@chromium.org56dd6302012-04-10 17:25:44 +000082 }
robertphillipsf299e712016-03-25 04:49:22 -070083 this->drawClippedBitmap(canvas, paint, i * 140, j * 140);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000084 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000085 }
86 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000087
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000088private:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000089 SkBitmap fBitmap;
robertphillipsf299e712016-03-25 04:49:22 -070090
91 typedef GM INHERITED;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000092};
93
94//////////////////////////////////////////////////////////////////////////////
95
robertphillipsf299e712016-03-25 04:49:22 -070096DEF_GM(return new MorphologyGM;)
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000097
98}