OLD | NEW |
---|---|
(Empty) | |
1 | |
2 /* | |
3 * Copyright 2011 Google Inc. | |
scroggo
2013/08/21 23:25:27
2013
sugoi1
2013/08/22 15:41:00
Done.
| |
4 * | |
5 * Use of this source code is governed by a BSD-style license that can be | |
6 * found in the LICENSE file. | |
7 */ | |
8 #include "SampleCode.h" | |
9 #include "SkView.h" | |
10 #include "SkCanvas.h" | |
11 #include "SkDevice.h" | |
12 #include "SkRandom.h" | |
13 #include "SkFlattenableSerialization.h" | |
14 #include "SkBicubicImageFilter.h" | |
15 #include "SkMergeImageFilter.h" | |
16 #include "SkColorFilter.h" | |
17 #include "SkColorFilterImageFilter.h" | |
18 #include "SkBlurImageFilter.h" | |
19 #include "SkMagnifierImageFilter.h" | |
20 #include "SkXfermodeImageFilter.h" | |
21 #include "SkOffsetImageFilter.h" | |
22 #include "SkComposeImageFilter.h" | |
23 #include "SkLightingImageFilter.h" | |
24 #include "SkPerlinNoiseShader.h" | |
25 #include "SkRectShaderImageFilter.h" | |
26 #include "SkDropShadowImageFilter.h" | |
27 #include "SkMorphologyImageFilter.h" | |
28 #include "SkBitmapSource.h" | |
29 #include "SkDisplacementMapEffect.h" | |
30 | |
31 static const uint32_t kSeed = 1; | |
32 static SkRandom gRand(kSeed); | |
33 static bool return_large = false; | |
Stephen White
2013/08/21 20:49:26
Shouldn't we also be randomizing these values, or
sugoi
2013/08/21 21:12:09
I'd like to add more "randomness" in the next vers
| |
34 static bool return_undef = false; | |
35 | |
36 static const int kBitmapSize = 24; | |
37 | |
38 static int R(float x) { | |
39 return (int)floor(SkScalarToFloat(gRand.nextUScalar1()) * x); | |
40 } | |
41 | |
42 #if defined _WIN32 | |
43 #pragma warning ( push ) | |
44 // we are intentionally causing an overflow here | |
45 // (warning C4756: overflow in constant arithmetic) | |
46 #pragma warning ( disable : 4756 ) | |
47 #endif | |
48 | |
49 static float huge() { | |
50 double d = 1e100; | |
51 float f = (float)d; | |
52 return f; | |
53 } | |
54 | |
55 #if defined _WIN32 | |
56 #pragma warning ( pop ) | |
57 #endif | |
58 | |
59 static float make_number(bool positiveOnly) { | |
60 float f = positiveOnly ? 1 : 0; | |
61 float v = f; | |
62 int sel; | |
63 | |
64 if (return_large) sel = R(6); else sel = R(4); | |
65 if (!return_undef && sel == 0) sel = 1; | |
66 | |
67 if (R(2) == 1) v = (float)(R(100)+f); else | |
68 | |
69 switch (sel) { | |
70 case 0: break; | |
71 case 1: v = f; break; | |
72 case 2: v = 0.000001f; break; | |
73 case 3: v = 10000; break; | |
74 case 4: v = 2000000000; break; | |
75 case 5: v = huge(); break; | |
76 } | |
77 | |
78 if (!positiveOnly && (R(4) == 1)) v = -v; | |
79 return v; | |
80 } | |
81 | |
82 static SkScalar make_scalar(bool positiveOnly = false) { | |
83 return SkFloatToScalar(make_number(positiveOnly)); | |
84 } | |
85 | |
86 static SkRect make_rect(int offset = 1) { | |
87 return SkRect::MakeWH(SkIntToScalar(R(kBitmapSize)+offset), | |
88 SkIntToScalar(R(kBitmapSize)+offset)); | |
89 } | |
90 | |
91 static SkXfermode::Mode make_xfermode() { | |
92 return static_cast<SkXfermode::Mode>(R(SkXfermode::kLastMode+1)); | |
93 } | |
94 | |
95 static SkColor make_color() { | |
96 return (R(2) == 1) ? 0xFFC0F0A0 : 0xFF000090; | |
97 } | |
98 | |
99 static SkPoint3 make_point() { | |
100 return SkPoint3(make_scalar(), make_scalar(), make_scalar(true)); | |
101 } | |
102 | |
103 static SkDisplacementMapEffect::ChannelSelectorType make_channel_selector_type() { | |
104 return static_cast<SkDisplacementMapEffect::ChannelSelectorType>(R(4)+1); | |
105 } | |
106 | |
107 static void make_g_bitmap(SkBitmap& bitmap) { | |
108 bitmap.setConfig(SkBitmap::kARGB_8888_Config, kBitmapSize, kBitmapSize); | |
109 bitmap.allocPixels(); | |
110 SkDevice device(bitmap); | |
111 SkCanvas canvas(&device); | |
112 canvas.clear(0x00000000); | |
113 SkPaint paint; | |
114 paint.setAntiAlias(true); | |
115 paint.setColor(0xFF884422); | |
116 paint.setTextSize(SkIntToScalar(kBitmapSize/2)); | |
117 const char* str = "g"; | |
118 canvas.drawText(str, strlen(str), SkIntToScalar(kBitmapSize/8), SkIntToScala r(kBitmapSize/4), paint); | |
scroggo
2013/08/21 23:25:27
100 chars
sugoi1
2013/08/22 15:41:00
Done.
| |
119 } | |
120 | |
121 static void make_checkerboard_bitmap(SkBitmap& bitmap) { | |
122 bitmap.setConfig(SkBitmap::kARGB_8888_Config, kBitmapSize, kBitmapSize); | |
123 bitmap.allocPixels(); | |
124 SkDevice device(bitmap); | |
125 SkCanvas canvas(&device); | |
126 canvas.clear(0x00000000); | |
127 SkPaint darkPaint; | |
128 darkPaint.setColor(0xFF804020); | |
129 SkPaint lightPaint; | |
130 lightPaint.setColor(0xFF244484); | |
131 const int i = kBitmapSize / 8; | |
132 for (int y = 0; y < kBitmapSize; y += i) { | |
133 for (int x = 0; x < kBitmapSize; x += i) { | |
134 canvas.save(); | |
135 canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); | |
136 canvas.drawRect(SkRect::MakeXYWH(0, 0, i, i), darkPaint); | |
137 canvas.drawRect(SkRect::MakeXYWH(i, 0, i, i), lightPaint); | |
138 canvas.drawRect(SkRect::MakeXYWH(0, i, i, i), lightPaint); | |
139 canvas.drawRect(SkRect::MakeXYWH(i, i, i, i), darkPaint); | |
140 canvas.restore(); | |
141 } | |
142 } | |
143 } | |
144 | |
145 static const SkBitmap& make_bitmap() { | |
146 static SkBitmap bitmap[2]; | |
147 static bool initialized = false; | |
148 if (!initialized) { | |
149 make_g_bitmap(bitmap[0]); | |
150 make_checkerboard_bitmap(bitmap[1]); | |
151 initialized = true; | |
152 } | |
153 return bitmap[R(2)]; | |
154 } | |
155 | |
156 static SkImageFilter* make_image_filter(bool canBeNull = true) { | |
157 SkImageFilter* filter = 0; | |
158 | |
159 // Add a 1 in 3 chance to get a NULL input | |
160 if (canBeNull && (R(3) == 1)) { return filter; } | |
161 | |
162 enum { BICUBIC, MERGE, COLOR, BLUR, MAGNIFIER, XFERMODE, OFFSET, COMPOSE, | |
163 DISTANT_LIGHT, POINT_LIGHT, SPOT_LIGHT, NOISE, DROP_SHADOW, | |
164 MORPHOLOGY, BITMAP, DISPLACE, NUM_FILTERS }; | |
165 | |
166 switch (R(NUM_FILTERS)) { | |
167 case BICUBIC: | |
168 // Scale is set to 1 here so that it can fit in the DAG without resizing the output | |
169 filter = SkBicubicImageFilter::CreateMitchell(SkSize::Make(1, 1), make_i mage_filter()); | |
170 break; | |
171 case MERGE: | |
172 filter = new SkMergeImageFilter(make_image_filter(), make_image_filter() , make_xfermode()); | |
173 break; | |
174 case COLOR: | |
175 { | |
176 SkAutoTUnref<SkColorFilter> cf((R(2) == 1) ? | |
177 SkColorFilter::CreateModeFilter(make_color(), make_xfermode()) : | |
178 SkColorFilter::CreateLightingFilter(make_color(), make_color()) ); | |
179 filter = cf.get() ? SkColorFilterImageFilter::Create(cf, make_image_filt er()) : 0; | |
180 } | |
181 break; | |
182 case BLUR: | |
183 filter = new SkBlurImageFilter(make_scalar(true), make_scalar(true), mak e_image_filter()); | |
184 break; | |
185 case MAGNIFIER: | |
186 filter = new SkMagnifierImageFilter(make_rect(0), make_scalar(true)); | |
187 break; | |
188 case XFERMODE: | |
189 { | |
190 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(make_xfermode())); | |
191 filter = new SkXfermodeImageFilter(mode, make_image_filter(), make_image _filter()); | |
192 } | |
193 break; | |
194 case OFFSET: | |
195 filter = new SkOffsetImageFilter(make_scalar(), make_scalar(), make_imag e_filter()); | |
196 break; | |
197 case COMPOSE: | |
198 filter = new SkComposeImageFilter(make_image_filter(), make_image_filter ()); | |
199 break; | |
200 case DISTANT_LIGHT: | |
201 filter = (R(2) == 1) ? | |
202 SkLightingImageFilter::CreateDistantLitDiffuse(make_point(), | |
203 make_color(), make_scalar(), make_scalar(), make_image_filter() ) : | |
204 SkLightingImageFilter::CreateDistantLitSpecular(make_point(), | |
205 make_color(), make_scalar(), make_scalar(), SkIntToScalar(R(10) ), | |
206 make_image_filter()); | |
207 break; | |
208 case POINT_LIGHT: | |
209 filter = (R(2) == 1) ? | |
210 SkLightingImageFilter::CreatePointLitDiffuse(make_point(), | |
211 make_color(), make_scalar(), make_scalar(), make_image_filter() ) : | |
212 SkLightingImageFilter::CreatePointLitSpecular(make_point(), | |
213 make_color(), make_scalar(), make_scalar(), SkIntToScalar(R(10) ), | |
214 make_image_filter()); | |
215 break; | |
216 case SPOT_LIGHT: | |
217 filter = (R(2) == 1) ? | |
218 SkLightingImageFilter::CreateSpotLitDiffuse(SkPoint3(0, 0, 0), | |
Stephen White
2013/08/21 20:49:26
Maybe we should have a make_point3 that we could u
sugoi
2013/08/21 21:12:09
I had some issue with having this value randomized
| |
219 make_point(), make_scalar(), make_scalar(), make_color(), | |
220 make_scalar(), make_scalar(), make_image_filter()) : | |
221 SkLightingImageFilter::CreateSpotLitSpecular(SkPoint3(0, 0, 0), | |
222 make_point(), make_scalar(), make_scalar(), make_color(), | |
223 make_scalar(), make_scalar(), SkIntToScalar(R(10)), make_image_ filter()); | |
224 break; | |
225 case NOISE: | |
226 { | |
227 SkAutoTUnref<SkShader> shader((R(2) == 1) ? | |
228 SkPerlinNoiseShader::CreateFractalNoise(make_scalar(true), make_scal ar(true), R(10), make_scalar()) : | |
scroggo
2013/08/21 23:25:27
100 chars
sugoi1
2013/08/22 15:41:00
Done.
| |
229 SkPerlinNoiseShader::CreateTubulence(make_scalar(true), make_scalar( true), R(10), make_scalar())); | |
scroggo
2013/08/21 23:25:27
100 chars
sugoi1
2013/08/22 15:41:00
Done.
| |
230 filter = SkRectShaderImageFilter::Create(shader, SkRect::MakeWH(kBitmapS ize, kBitmapSize)); | |
231 } | |
232 break; | |
233 case DROP_SHADOW: | |
234 filter = new SkDropShadowImageFilter(make_scalar(), make_scalar(), | |
235 make_scalar(true), make_color(), make_image_filter()); | |
236 break; | |
237 case MORPHOLOGY: | |
238 if (R(2) == 1) | |
239 filter = new SkDilateImageFilter(R(kBitmapSize), R(kBitmapSize), mak e_image_filter()); | |
240 else | |
241 filter = new SkErodeImageFilter(R(kBitmapSize), R(kBitmapSize), make _image_filter()); | |
242 break; | |
243 case BITMAP: | |
244 filter = new SkBitmapSource(make_bitmap()); | |
245 break; | |
246 case DISPLACE: | |
247 filter = new SkDisplacementMapEffect(make_channel_selector_type(), | |
248 make_channel_selector_type(), make_scalar(), | |
249 make_image_filter(false), make_image_filter()); | |
250 break; | |
251 default: | |
252 break; | |
253 } | |
254 return (filter || canBeNull) ? filter : make_image_filter(canBeNull); | |
255 } | |
256 | |
257 void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) { | |
258 canvas->save(); | |
259 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), | |
260 SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize))); | |
261 canvas->drawBitmap(make_bitmap(), SkIntToScalar(x), SkIntToScalar(y), &paint ); | |
262 canvas->restore(); | |
263 } | |
264 | |
265 static void do_fuzz(SkCanvas* canvas) { | |
266 SkPaint paint; | |
267 paint.setImageFilter(make_image_filter()); | |
268 drawClippedBitmap(canvas, 0, 0, paint); | |
269 } | |
270 | |
271 ////////////////////////////////////////////////////////////////////////////// | |
272 | |
273 class ImageFilterFuzzView : public SampleView { | |
274 public: | |
275 ImageFilterFuzzView() { | |
276 this->setBGColor(0xFFDDDDDD); | |
277 } | |
278 | |
279 protected: | |
280 // overrides from SkEventSink | |
281 virtual bool onQuery(SkEvent* evt) { | |
282 if (SampleCode::TitleQ(*evt)) { | |
283 SampleCode::TitleR(evt, "ImageFilterFuzzer"); | |
284 return true; | |
285 } | |
286 return this->INHERITED::onQuery(evt); | |
287 } | |
288 | |
289 void drawBG(SkCanvas* canvas) { | |
290 canvas->drawColor(0xFFDDDDDD); | |
291 } | |
292 | |
293 virtual void onDrawContent(SkCanvas* canvas) { | |
294 do_fuzz(canvas); | |
295 this->inval(0); | |
296 } | |
297 | |
298 private: | |
299 typedef SkView INHERITED; | |
300 }; | |
301 | |
302 ////////////////////////////////////////////////////////////////////////////// | |
303 | |
304 static SkView* MyFactory() { return new ImageFilterFuzzView; } | |
305 static SkViewRegister reg(MyFactory); | |
OLD | NEW |