1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm.h"
#include "SkAlphaThresholdFilter.h"
#include "SkImageSource.h"
#include "SkOffsetImageFilter.h"
#include "SkRandom.h"
#include "SkRegion.h"
#include "SkSurface.h"
#define WIDTH 500
#define HEIGHT 500
static void draw_rects(SkCanvas* canvas) {
SkPaint rectPaint;
rectPaint.setColor(SK_ColorBLUE);
canvas->drawRect(SkRect::MakeXYWH(0, 0, WIDTH / 2, HEIGHT / 2), rectPaint);
rectPaint.setColor(0xBFFF0000);
canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, 0, WIDTH / 2, HEIGHT / 2), rectPaint);
rectPaint.setColor(0x3F00FF00);
canvas->drawRect(SkRect::MakeXYWH(0, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint);
rectPaint.setColor(SK_ColorTRANSPARENT);
canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint);
}
static SkPaint create_filter_paint(SkImageFilter::CropRect* cropRect = nullptr) {
SkIRect rects[2];
rects[0] = SkIRect::MakeXYWH(0, 150, WIDTH, HEIGHT - 300);
rects[1] = SkIRect::MakeXYWH(150, 0, WIDTH - 300, HEIGHT);
SkRegion region;
region.setRects(rects, 2);
SkPaint paint;
sk_sp<SkImageFilter> offset(SkOffsetImageFilter::Make(25, 25, nullptr));
paint.setImageFilter(SkAlphaThresholdFilter::Make(region, 0.2f, 0.7f, std::move(offset), cropRect));
return paint;
}
class ImageAlphaThresholdGM : public skiagm::GM {
public:
ImageAlphaThresholdGM(bool useCropRect) : fUseCropRect(useCropRect) {
this->setBGColor(SK_ColorWHITE);
}
protected:
SkString onShortName() override {
if (fUseCropRect) {
return SkString("imagealphathreshold_crop");
}
return SkString("imagealphathreshold");
}
SkISize onISize() override {
return SkISize::Make(WIDTH, HEIGHT);
}
void onDraw(SkCanvas* canvas) override {
SkMatrix matrix;
matrix.reset();
matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f);
matrix.postScale(.8f, .8f);
canvas->concat(matrix);
SkRect r = SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100);
SkImageFilter::CropRect cropRect(r);
SkPaint paint = create_filter_paint(fUseCropRect ? &cropRect : nullptr);
canvas->saveLayer(nullptr, &paint);
draw_rects(canvas);
canvas->restore();
}
private:
bool fUseCropRect;
typedef GM INHERITED;
};
// Create a 'width' x 'height' SkSurface that matches the colorType of 'canvas' as
// best we can
static sk_sp<SkSurface> make_color_matching_surface(SkCanvas* canvas, int width, int height,
SkAlphaType alphaType) {
SkColorType ct = canvas->imageInfo().colorType();
sk_sp<SkColorSpace> cs(canvas->imageInfo().refColorSpace());
if (kUnknown_SkColorType == ct) {
// For backends that aren't yet color-space aware we just fallback to N32.
ct = kN32_SkColorType;
cs = nullptr;
}
SkImageInfo info = SkImageInfo::Make(width, height, ct, alphaType, std::move(cs));
sk_sp<SkSurface> result = canvas->makeSurface(info);
if (!result) {
result = SkSurface::MakeRaster(info);
}
return result;
}
class ImageAlphaThresholdSurfaceGM : public skiagm::GM {
public:
ImageAlphaThresholdSurfaceGM() {
this->setBGColor(0xFFFFFFFF);
}
protected:
SkString onShortName() override {
return SkString("imagealphathreshold_surface");
}
SkISize onISize() override {
return SkISize::Make(WIDTH, HEIGHT);
}
void onDraw(SkCanvas* canvas) override {
SkMatrix matrix;
matrix.reset();
matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f);
matrix.postScale(.8f, .8f);
canvas->concat(matrix);
sk_sp<SkSurface> surface(make_color_matching_surface(canvas, WIDTH, HEIGHT,
kPremul_SkAlphaType));
if (!surface) {
return;
}
surface->getCanvas()->clear(SK_ColorTRANSPARENT);
draw_rects(surface->getCanvas());
SkPaint paint = create_filter_paint();
canvas->clipRect(SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100));
canvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, &paint);
}
private:
typedef skiagm::GM INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
DEF_GM(return new ImageAlphaThresholdGM(true);)
DEF_GM(return new ImageAlphaThresholdGM(false);)
DEF_GM(return new ImageAlphaThresholdSurfaceGM();)
//////////////////////////////////////////////////////////////////////////////
static sk_sp<SkImage> make_img() {
SkBitmap bitmap;
bitmap.allocPixels(SkImageInfo::MakeS32(WIDTH, HEIGHT, kPremul_SkAlphaType));
SkCanvas canvas(bitmap);
SkPaint paint;
SkRect rect = SkRect::MakeWH(WIDTH, HEIGHT);
SkRandom rnd;
while (!rect.isEmpty()) {
paint.setColor(rnd.nextU() | (0xFF << 24));
canvas.drawRect(rect, paint);
rect.inset(25, 25);
}
return SkImage::MakeFromBitmap(bitmap);
}
DEF_SIMPLE_GM_BG(imagealphathreshold_image, canvas, WIDTH * 2, HEIGHT, SK_ColorBLACK) {
sk_sp<SkImage> image(make_img());
SkIRect rects[2];
rects[0] = SkIRect::MakeXYWH(0, 150, WIDTH, HEIGHT - 300);
rects[1] = SkIRect::MakeXYWH(150, 0, WIDTH - 300, HEIGHT);
SkRegion region;
region.setRects(rects, 2);
SkPaint filterPaint;
sk_sp<SkImageFilter> imageSource(SkImageSource::Make(image));
filterPaint.setImageFilter(SkAlphaThresholdFilter::Make(region, 0.2f, 0.7f,
std::move(imageSource)));
canvas->saveLayer(nullptr, &filterPaint);
canvas->restore();
canvas->translate(WIDTH, 0);
canvas->drawImage(image, 0, 0);
}
|