1 /*
2  * Copyright 2014 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 "SkCanvas.h"
10 #include "SkColorFilter.h"
11 #include "SkColorPriv.h"
12 #include "SkShader.h"
13 
14 #include "SkBitmapSource.h"
15 #include "SkBlurImageFilter.h"
16 #include "SkColorMatrixFilter.h"
17 #include "SkDisplacementMapEffect.h"
18 #include "SkDropShadowImageFilter.h"
19 #include "SkGradientShader.h"
20 #include "SkMorphologyImageFilter.h"
21 #include "SkColorFilterImageFilter.h"
22 #include "SkMergeImageFilter.h"
23 #include "SkOffsetImageFilter.h"
24 
25 ///////////////////////////////////////////////////////////////////////////////
26 
27 class ImageFiltersCropExpandGM : public skiagm::GM {
28 public:
ImageFiltersCropExpandGM()29     ImageFiltersCropExpandGM () {}
30 
31 protected:
32 
onShortName()33     virtual SkString onShortName() {
34         return SkString("imagefilterscropexpand");
35     }
36 
onISize()37     virtual SkISize onISize() { return SkISize::Make(570, 650); }
38 
make_checkerboard(SkBitmap * bitmap)39     void make_checkerboard(SkBitmap* bitmap) {
40         bitmap->allocN32Pixels(64, 64);
41         SkCanvas canvas(*bitmap);
42         canvas.clear(0xFFFF0000);
43         SkPaint darkPaint;
44         darkPaint.setColor(0xFF404040);
45         SkPaint lightPaint;
46         lightPaint.setColor(0xFFA0A0A0);
47         for (int y = 8; y < 48; y += 16) {
48             for (int x = 8; x < 48; x += 16) {
49                 canvas.save();
50                 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
51                 canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
52                 canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
53                 canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
54                 canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
55                 canvas.restore();
56             }
57         }
58     }
59 
make_gradient_circle(int width,int height,SkBitmap * bitmap)60     void make_gradient_circle(int width, int height, SkBitmap* bitmap) {
61         SkScalar x = SkIntToScalar(width / 2);
62         SkScalar y = SkIntToScalar(height / 2);
63         SkScalar radius = SkMinScalar(x, y) * 0.8f;
64         bitmap->allocN32Pixels(width, height);
65         SkCanvas canvas(*bitmap);
66         canvas.clear(0x00000000);
67         SkColor colors[2];
68         colors[0] = SK_ColorWHITE;
69         colors[1] = SK_ColorBLACK;
70         SkAutoTUnref<SkShader> shader(
71             SkGradientShader::CreateRadial(SkPoint::Make(x, y), radius, colors, NULL, 2,
72                                            SkShader::kClamp_TileMode)
73         );
74         SkPaint paint;
75         paint.setShader(shader);
76         canvas.drawCircle(x, y, radius, paint);
77     }
78 
draw(SkCanvas * canvas,const SkBitmap & bitmap,const SkRect & rect,SkImageFilter * filter)79     static void draw(SkCanvas* canvas, const SkBitmap& bitmap, const SkRect& rect, SkImageFilter* filter) {
80         SkPaint paint;
81         paint.setImageFilter(filter)->unref();
82         canvas->saveLayer(&rect, &paint);
83         canvas->drawBitmap(bitmap, 0, 0);
84         canvas->restore();
85 
86         SkPaint strokePaint;
87         strokePaint.setColor(0xFFFF0000);
88         strokePaint.setStyle(SkPaint::kStroke_Style);
89         canvas->drawRect(rect, strokePaint);
90 
91         canvas->translate(SkIntToScalar(80), 0);
92     }
93 
onDraw(SkCanvas * canvas)94     virtual void onDraw(SkCanvas* canvas) {
95         SkAutoTUnref<SkColorFilter> cf(
96             SkColorFilter::CreateModeFilter(SK_ColorBLUE, SkXfermode::kSrcIn_Mode));
97         SkImageFilter::CropRect crop_rect(
98             SkRect::Make(SkIRect::MakeXYWH(10, 10, 44, 44)),
99             SkImageFilter::CropRect::kHasAll_CropEdge);
100 
101         SkBitmap gradient_circle, checkerboard;
102         make_gradient_circle(64, 64, &gradient_circle);
103         make_checkerboard(&checkerboard);
104 
105         SkAutoTUnref<SkImageFilter> gradient_circle_source(
106             SkBitmapSource::Create(gradient_circle));
107         SkAutoTUnref<SkImageFilter> noop_cropped(
108             SkOffsetImageFilter::Create(0, 0, NULL, &crop_rect));
109         SkScalar sk255 = SkIntToScalar(255);
110         SkScalar matrix[20] = { 1, 0, 0, 0, 0,
111                                 0, 1, 0, 0, sk255,
112                                 0, 0, 1, 0, 0,
113                                 0, 0, 0, 0, sk255 };
114         SkAutoTUnref<SkColorFilter> cf_alpha_trans(SkColorMatrixFilter::Create(matrix));
115 
116         SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
117         SkScalar MARGIN = SkIntToScalar(12);
118 
119         SkIRect bounds;
120         r.roundOut(&bounds);
121 
122         SkPaint paint;
123         canvas->translate(MARGIN, MARGIN);
124         for (int outset = -15; outset <= 20; outset += 5) {
125             canvas->save();
126             SkRect rect = crop_rect.rect();
127             rect.outset(SkIntToScalar(outset),
128                         SkIntToScalar(outset));
129             SkImageFilter::CropRect big_rect(rect, SkImageFilter::CropRect::kHasAll_CropEdge);
130 
131             draw(canvas, checkerboard, rect, SkColorFilterImageFilter::Create(
132                 cf_alpha_trans, noop_cropped.get(), &big_rect));
133 
134             draw(canvas, checkerboard, rect, SkBlurImageFilter::Create(
135                 8.0f, 8.0f, noop_cropped.get(), &big_rect));
136 
137             draw(canvas, checkerboard, rect, SkDilateImageFilter::Create(
138                 2, 2, noop_cropped.get(), &big_rect));
139 
140             draw(canvas, checkerboard, rect, SkErodeImageFilter::Create(
141                 2, 2, noop_cropped.get(), &big_rect));
142 
143             draw(canvas, checkerboard, rect, SkDropShadowImageFilter::Create(
144                 SkIntToScalar(10), SkIntToScalar(10), SkIntToScalar(3), SkIntToScalar(3),
145                 SK_ColorBLUE, SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
146                 noop_cropped.get(), &big_rect));
147 
148             draw(canvas, checkerboard, rect, SkDisplacementMapEffect::Create(
149                 SkDisplacementMapEffect::kR_ChannelSelectorType,
150                 SkDisplacementMapEffect::kR_ChannelSelectorType,
151                 SkIntToScalar(12),
152                 gradient_circle_source.get(),
153                 noop_cropped.get(),
154                 &big_rect));
155 
156             draw(canvas, checkerboard, rect, SkOffsetImageFilter::Create(
157                 SkIntToScalar(-8), SkIntToScalar(16), noop_cropped.get(), &big_rect));
158 
159             canvas->restore();
160             canvas->translate(0, SkIntToScalar(80));
161         }
162     }
163 
164 private:
165     typedef GM INHERITED;
166 };
167 
168 ///////////////////////////////////////////////////////////////////////////////
169 
MyFactory(void *)170 static skiagm::GM* MyFactory(void*) { return new ImageFiltersCropExpandGM; }
171 static skiagm::GMRegistry reg(MyFactory);
172