• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 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/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkImageFilter.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkScalar.h"
18 #include "include/core/SkSize.h"
19 #include "include/core/SkString.h"
20 #include "include/effects/SkImageFilters.h"
21 #include "tools/ToolUtils.h"
22 
23 #include <utility>
24 
25 namespace skiagm {
26 
27 class DisplacementMapGM : public GM {
28 public:
DisplacementMapGM()29     DisplacementMapGM() {
30         this->setBGColor(0xFF000000);
31     }
32 
33 protected:
onShortName()34     SkString onShortName() override {
35         return SkString("displacement");
36     }
37 
onOnceBeforeDraw()38     void onOnceBeforeDraw() override {
39         fImage = ToolUtils::create_string_image(80, 80, 0xFF884422, 15, 55, 96, "g");
40 
41         SkColor c1 = ToolUtils::color_to_565(0xFF244484);
42         SkColor c2 = ToolUtils::color_to_565(0xFF804020);
43 
44         fCheckerboard = ToolUtils::create_checkerboard_image(80, 80, c1, c2, 8);
45         fSmall  = ToolUtils::create_checkerboard_image(64, 64, c1, c2, 8);
46         fLarge  = ToolUtils::create_checkerboard_image(96, 96, c1, c2, 8);
47         fLargeW = ToolUtils::create_checkerboard_image(96, 64, c1, c2, 8);
48         fLargeH = ToolUtils::create_checkerboard_image(64, 96, c1, c2, 8);
49     }
50 
onISize()51     SkISize onISize() override {
52         return SkISize::Make(600, 500);
53     }
54 
drawClippedBitmap(SkCanvas * canvas,int x,int y,const SkPaint & paint) const55     void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) const {
56         canvas->save();
57         canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
58         canvas->clipIRect(fImage->bounds());
59         canvas->drawImage(fImage, 0, 0, SkSamplingOptions(), &paint);
60         canvas->restore();
61     }
62 
onDraw(SkCanvas * canvas)63     void onDraw(SkCanvas* canvas) override {
64         canvas->clear(SK_ColorBLACK);
65         SkPaint paint;
66         sk_sp<SkImageFilter> displ(SkImageFilters::Image(fCheckerboard));
67         paint.setImageFilter(SkImageFilters::DisplacementMap(
68                 SkColorChannel::kR, SkColorChannel::kG, 0.0f, displ, nullptr));
69         this->drawClippedBitmap(canvas, 0, 0, paint);
70         paint.setImageFilter(SkImageFilters::DisplacementMap(
71                 SkColorChannel::kB, SkColorChannel::kA, 16.0f, displ, nullptr));
72         this->drawClippedBitmap(canvas, 100, 0, paint);
73         paint.setImageFilter(SkImageFilters::DisplacementMap(
74                 SkColorChannel::kR, SkColorChannel::kB, 32.0f, displ, nullptr));
75         this->drawClippedBitmap(canvas, 200, 0, paint);
76         paint.setImageFilter(SkImageFilters::DisplacementMap(
77                 SkColorChannel::kG, SkColorChannel::kA, 48.0f, displ, nullptr));
78         this->drawClippedBitmap(canvas, 300, 0, paint);
79         paint.setImageFilter(SkImageFilters::DisplacementMap(
80                 SkColorChannel::kR, SkColorChannel::kA, 64.0f, displ, nullptr));
81         this->drawClippedBitmap(canvas, 400, 0, paint);
82 
83         paint.setImageFilter(SkImageFilters::DisplacementMap(
84                 SkColorChannel::kR, SkColorChannel::kG, 40.0f, displ, nullptr));
85         this->drawClippedBitmap(canvas, 0, 100, paint);
86         paint.setImageFilter(SkImageFilters::DisplacementMap(
87                 SkColorChannel::kB, SkColorChannel::kA, 40.0f, displ, nullptr));
88         this->drawClippedBitmap(canvas, 100, 100, paint);
89         paint.setImageFilter(SkImageFilters::DisplacementMap(
90                 SkColorChannel::kR, SkColorChannel::kB, 40.0f, displ, nullptr));
91         this->drawClippedBitmap(canvas, 200, 100, paint);
92         paint.setImageFilter(SkImageFilters::DisplacementMap(
93                 SkColorChannel::kG, SkColorChannel::kA, 40.0f, displ, nullptr));
94         this->drawClippedBitmap(canvas, 300, 100, paint);
95         paint.setImageFilter(SkImageFilters::DisplacementMap(
96                 SkColorChannel::kR, SkColorChannel::kA, 40.0f, displ, nullptr));
97         this->drawClippedBitmap(canvas, 400, 100, paint);
98 
99         SkIRect cropRect = SkIRect::MakeXYWH(30, 30, 40, 40);
100         paint.setImageFilter(SkImageFilters::DisplacementMap(
101                 SkColorChannel::kR, SkColorChannel::kG, 0.0f, displ, nullptr, &cropRect));
102         this->drawClippedBitmap(canvas, 0, 200, paint);
103         paint.setImageFilter(SkImageFilters::DisplacementMap(
104             SkColorChannel::kB, SkColorChannel::kA, 16.0f, displ, nullptr, &cropRect));
105         this->drawClippedBitmap(canvas, 100, 200, paint);
106         paint.setImageFilter(SkImageFilters::DisplacementMap(
107                 SkColorChannel::kR, SkColorChannel::kB, 32.0f, displ, nullptr, &cropRect));
108         this->drawClippedBitmap(canvas, 200, 200, paint);
109         paint.setImageFilter(SkImageFilters::DisplacementMap(
110                 SkColorChannel::kG, SkColorChannel::kA, 48.0f, displ, nullptr, &cropRect));
111         this->drawClippedBitmap(canvas, 300, 200, paint);
112         paint.setImageFilter(SkImageFilters::DisplacementMap(
113                 SkColorChannel::kR, SkColorChannel::kA, 64.0f, displ, nullptr, &cropRect));
114         this->drawClippedBitmap(canvas, 400, 200, paint);
115 
116         paint.setImageFilter(SkImageFilters::DisplacementMap(
117                 SkColorChannel::kR, SkColorChannel::kG, 40.0f, displ, nullptr, &cropRect));
118         this->drawClippedBitmap(canvas, 0, 300, paint);
119         paint.setImageFilter(SkImageFilters::DisplacementMap(
120                 SkColorChannel::kB, SkColorChannel::kA, 40.0f, displ, nullptr, &cropRect));
121         this->drawClippedBitmap(canvas, 100, 300, paint);
122         paint.setImageFilter(SkImageFilters::DisplacementMap(
123                 SkColorChannel::kR, SkColorChannel::kB, 40.0f, displ, nullptr, &cropRect));
124         this->drawClippedBitmap(canvas, 200, 300, paint);
125         paint.setImageFilter(SkImageFilters::DisplacementMap(
126                 SkColorChannel::kG, SkColorChannel::kA, 40.0f, displ, nullptr, &cropRect));
127         this->drawClippedBitmap(canvas, 300, 300, paint);
128         paint.setImageFilter(SkImageFilters::DisplacementMap(
129                 SkColorChannel::kR, SkColorChannel::kA, 40.0f, displ, nullptr, &cropRect));
130         this->drawClippedBitmap(canvas, 400, 300, paint);
131 
132         // Test for negative scale.
133         paint.setImageFilter(SkImageFilters::DisplacementMap(
134                 SkColorChannel::kG, SkColorChannel::kA, -40.0f, displ, nullptr));
135         this->drawClippedBitmap(canvas, 500, 0, paint);
136 
137         // Tests for images of different sizes
138         displ = SkImageFilters::Image(fSmall);
139         paint.setImageFilter(SkImageFilters::DisplacementMap(
140                 SkColorChannel::kR, SkColorChannel::kG, 40.0f, std::move(displ), nullptr));
141         this->drawClippedBitmap(canvas, 0, 400, paint);
142         displ = SkImageFilters::Image(fLarge);
143         paint.setImageFilter(SkImageFilters::DisplacementMap(
144                 SkColorChannel::kB, SkColorChannel::kA, 40.0f, std::move(displ), nullptr));
145         this->drawClippedBitmap(canvas, 100, 400, paint);
146         displ = SkImageFilters::Image(fLargeW);
147         paint.setImageFilter(SkImageFilters::DisplacementMap(
148                 SkColorChannel::kR, SkColorChannel::kB, 40.0f, std::move(displ), nullptr));
149         this->drawClippedBitmap(canvas, 200, 400, paint);
150         displ = SkImageFilters::Image(fLargeH);
151         paint.setImageFilter(SkImageFilters::DisplacementMap(
152                 SkColorChannel::kG, SkColorChannel::kA, 40.0f, std::move(displ), nullptr));
153         this->drawClippedBitmap(canvas, 300, 400, paint);
154 
155         // Test for no given displacement input. In this case, both displacement
156         // and color should use the same bitmap, given to SkCanvas::drawBitmap()
157         // as an input argument.
158         paint.setImageFilter(SkImageFilters::DisplacementMap(
159                 SkColorChannel::kG, SkColorChannel::kA, 40.0f, nullptr, nullptr));
160         this->drawClippedBitmap(canvas, 400, 400, paint);
161     }
162 
163 private:
164     sk_sp<SkImage> fImage;
165     sk_sp<SkImage> fCheckerboard, fSmall, fLarge, fLargeW, fLargeH;
166 
167     using INHERITED = GM;
168 };
169 
170 //////////////////////////////////////////////////////////////////////////////
171 
172 DEF_GM(return new DisplacementMapGM;)
173 }  // namespace skiagm
174