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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
/*
* Copyright 2012 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 "SkArithmeticMode.h"
#include "SkDevice.h"
#include "SkBitmapSource.h"
#include "SkBlurImageFilter.h"
#include "SkColorFilter.h"
#include "SkColorFilterImageFilter.h"
#include "SkColorMatrixFilter.h"
#include "SkReadBuffer.h"
#include "SkWriteBuffer.h"
#include "SkMergeImageFilter.h"
#include "SkMorphologyImageFilter.h"
#include "SkTestImageFilters.h"
#include "SkXfermodeImageFilter.h"
// More closely models how Blink's OffsetFilter works as of 10/23/13. SkOffsetImageFilter doesn't
// perform a draw and this one does.
class SimpleOffsetFilter : public SkImageFilter {
public:
class Registrar {
public:
Registrar() {
SkFlattenable::Register("SimpleOffsetFilter",
SimpleOffsetFilter::CreateProc,
SimpleOffsetFilter::GetFlattenableType());
}
};
static SkImageFilter* Create(SkScalar dx, SkScalar dy, SkImageFilter* input) {
return SkNEW_ARGS(SimpleOffsetFilter, (dx, dy, input));
}
virtual bool onFilterImage(Proxy* proxy, const SkBitmap& src, const Context& ctx,
SkBitmap* dst, SkIPoint* offset) const SK_OVERRIDE {
SkBitmap source = src;
SkImageFilter* input = getInput(0);
SkIPoint srcOffset = SkIPoint::Make(0, 0);
if (input && !input->filterImage(proxy, src, ctx, &source, &srcOffset)) {
return false;
}
SkIRect bounds;
if (!this->applyCropRect(ctx, proxy, source, &srcOffset, &bounds, &source)) {
return false;
}
SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height()));
SkCanvas canvas(device);
SkPaint paint;
paint.setXfermodeMode(SkXfermode::kSrc_Mode);
canvas.drawBitmap(source, fDX - bounds.left(), fDY - bounds.top(), &paint);
*dst = device->accessBitmap(false);
offset->fX += bounds.left();
offset->fY += bounds.top();
return true;
}
SK_TO_STRING_OVERRIDE()
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SimpleOffsetFilter);
protected:
void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE {
this->INHERITED::flatten(buffer);
buffer.writeScalar(fDX);
buffer.writeScalar(fDY);
}
private:
SimpleOffsetFilter(SkScalar dx, SkScalar dy, SkImageFilter* input)
: SkImageFilter(1, &input), fDX(dx), fDY(dy) {}
SkScalar fDX, fDY;
typedef SkImageFilter INHERITED;
};
static SimpleOffsetFilter::Registrar gReg;
SkFlattenable* SimpleOffsetFilter::CreateProc(SkReadBuffer& buffer) {
SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
SkScalar dx = buffer.readScalar();
SkScalar dy = buffer.readScalar();
return Create(dx, dy, common.getInput(0));
}
#ifndef SK_IGNORE_TO_STRING
void SimpleOffsetFilter::toString(SkString* str) const {
str->appendf("SimpleOffsetFilter: (");
str->append(")");
}
#endif
class ImageFiltersGraphGM : public skiagm::GM {
public:
ImageFiltersGraphGM() {}
protected:
virtual SkString onShortName() {
return SkString("imagefiltersgraph");
}
void make_bitmap() {
fBitmap.allocN32Pixels(100, 100);
SkCanvas canvas(fBitmap);
canvas.clear(0x00000000);
SkPaint paint;
paint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&paint);
paint.setColor(0xFFFFFFFF);
paint.setTextSize(SkIntToScalar(96));
const char* str = "e";
canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint);
}
void drawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, const SkPaint& paint) {
canvas->save();
canvas->clipRect(SkRect::MakeXYWH(0, 0,
SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height())));
canvas->drawBitmap(bitmap, 0, 0, &paint);
canvas->restore();
}
virtual SkISize onISize() { return SkISize::Make(500, 150); }
virtual void onOnceBeforeDraw() {
this->make_bitmap();
}
virtual void onDraw(SkCanvas* canvas) {
canvas->clear(0x00000000);
{
SkAutoTUnref<SkImageFilter> bitmapSource(SkBitmapSource::Create(fBitmap));
SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(SK_ColorRED,
SkXfermode::kSrcIn_Mode));
SkAutoTUnref<SkImageFilter> blur(SkBlurImageFilter::Create(4.0f, 4.0f, bitmapSource));
SkAutoTUnref<SkImageFilter> erode(SkErodeImageFilter::Create(4, 4, blur));
SkAutoTUnref<SkImageFilter> color(SkColorFilterImageFilter::Create(cf, erode));
SkAutoTUnref<SkImageFilter> merge(SkMergeImageFilter::Create(blur, color));
SkPaint paint;
paint.setImageFilter(merge);
canvas->drawPaint(paint);
canvas->translate(SkIntToScalar(100), 0);
}
{
SkAutoTUnref<SkImageFilter> morph(SkDilateImageFilter::Create(5, 5));
SkScalar matrix[20] = { SK_Scalar1, 0, 0, 0, 0,
0, SK_Scalar1, 0, 0, 0,
0, 0, SK_Scalar1, 0, 0,
0, 0, 0, 0.5f, 0 };
SkAutoTUnref<SkColorFilter> matrixFilter(SkColorMatrixFilter::Create(matrix));
SkAutoTUnref<SkImageFilter> colorMorph(SkColorFilterImageFilter::Create(matrixFilter, morph));
SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcOver_Mode));
SkAutoTUnref<SkImageFilter> blendColor(SkXfermodeImageFilter::Create(mode, colorMorph));
SkPaint paint;
paint.setImageFilter(blendColor);
drawClippedBitmap(canvas, fBitmap, paint);
canvas->translate(SkIntToScalar(100), 0);
}
{
SkScalar matrix[20] = { SK_Scalar1, 0, 0, 0, 0,
0, SK_Scalar1, 0, 0, 0,
0, 0, SK_Scalar1, 0, 0,
0, 0, 0, 0.5f, 0 };
SkAutoTUnref<SkColorMatrixFilter> matrixCF(SkColorMatrixFilter::Create(matrix));
SkAutoTUnref<SkImageFilter> matrixFilter(SkColorFilterImageFilter::Create(matrixCF));
SkAutoTUnref<SkImageFilter> offsetFilter(
SimpleOffsetFilter::Create(10.0f, 10.f, matrixFilter));
SkAutoTUnref<SkXfermode> arith(SkArithmeticMode::Create(0, SK_Scalar1, SK_Scalar1, 0));
SkAutoTUnref<SkXfermodeImageFilter> arithFilter(
SkXfermodeImageFilter::Create(arith, matrixFilter, offsetFilter));
SkPaint paint;
paint.setImageFilter(arithFilter);
drawClippedBitmap(canvas, fBitmap, paint);
canvas->translate(SkIntToScalar(100), 0);
}
{
SkAutoTUnref<SkImageFilter> blur(SkBlurImageFilter::Create(
SkIntToScalar(10), SkIntToScalar(10)));
SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcIn_Mode));
SkImageFilter::CropRect cropRect(SkRect::MakeWH(SkIntToScalar(95), SkIntToScalar(100)));
SkAutoTUnref<SkImageFilter> blend(
SkXfermodeImageFilter::Create(mode, blur, NULL, &cropRect));
SkPaint paint;
paint.setImageFilter(blend);
drawClippedBitmap(canvas, fBitmap, paint);
canvas->translate(SkIntToScalar(100), 0);
}
{
// Test that crop offsets are absolute, not relative to the parent's crop rect.
SkAutoTUnref<SkColorFilter> cf1(SkColorFilter::CreateModeFilter(SK_ColorBLUE,
SkXfermode::kSrcIn_Mode));
SkAutoTUnref<SkColorFilter> cf2(SkColorFilter::CreateModeFilter(SK_ColorGREEN,
SkXfermode::kSrcIn_Mode));
SkImageFilter::CropRect outerRect(SkRect::MakeXYWH(SkIntToScalar(10), SkIntToScalar(10),
SkIntToScalar(80), SkIntToScalar(80)));
SkImageFilter::CropRect innerRect(SkRect::MakeXYWH(SkIntToScalar(20), SkIntToScalar(20),
SkIntToScalar(60), SkIntToScalar(60)));
SkAutoTUnref<SkImageFilter> color1(SkColorFilterImageFilter::Create(cf1, NULL, &outerRect));
SkAutoTUnref<SkImageFilter> color2(SkColorFilterImageFilter::Create(cf2, color1, &innerRect));
SkPaint paint;
paint.setImageFilter(color2);
paint.setColor(0xFFFF0000);
canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), paint);
canvas->translate(SkIntToScalar(100), 0);
}
}
private:
typedef GM INHERITED;
SkBitmap fBitmap;
};
///////////////////////////////////////////////////////////////////////////////
static skiagm::GM* MyFactory(void*) { return new ImageFiltersGraphGM; }
static skiagm::GMRegistry reg(MyFactory);
|