Debian Codegraph @

src/filter/glitch0r/glitch0r.c

raw symbols SCIP
1 /* glitch0r.c
2 * Copyright (C) 2016 IDENT Software ~ http://identsoft.org
3 * Inspired by the test video on frei0r.dune.org
4 *
5 * This file is a Frei0r plugin.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include <stdlib.h>
23 #include <assert.h>
24 #include <string.h>
25 #include <time.h>
26
27 #include "frei0r.h"cxx . . $ `<file>/include/frei0r.h`/
28 #include "frei0r/math.h"cxx . . $ `<file>/include/frei0r/math.h`/
29
30 /* cheap & fast randomizer (by Fukuchi Kentarou) */
31 static uint32_tcxx . . $ uint32_t# randvalcxx . . $ randval.;
32 inline static uint32_tcxx . . $ uint32_t# fastrandcxx . . $ fastrand(f0be770dcc0b0316).() { return (randvalcxx . . $ randval.=randvalcxx . . $ randval.*1103515245+12345); };
33 static void fastsrandcxx . . $ fastsrand(cb2f890c2fdad230).(uint32_tcxx . . $ uint32_t# seed) { randvalcxx . . $ randval. = seed; };
34
35 struct glitch0r_statecxx . . $ glitch0r_state# // helps to save time when allocating in a loop
36 {
37 unsigned int currentBlockcxx . . $ glitch0r_state#currentBlock.;
38 unsigned int currentPoscxx . . $ glitch0r_state#currentPos.;
39 unsigned int currentYcxx . . $ glitch0r_state#currentY.;
40 unsigned int blkShiftcxx . . $ glitch0r_state#blkShift.;
41
42 uint32_tcxx . . $ uint32_t# distortionSeed1cxx . . $ glitch0r_state#distortionSeed1.;
43 uint32_tcxx . . $ uint32_t# distortionSeed2cxx . . $ glitch0r_state#distortionSeed2.;
44
45 short int howToDistort1cxx . . $ glitch0r_state#howToDistort1.;
46 short int howToDistort2cxx . . $ glitch0r_state#howToDistort2.;
47 short int passThisLinecxx . . $ glitch0r_state#passThisLine.;
48 };
49
50 typedef struct glitch0r_instancecxx . . $ glitch0r_instance#
51 {
52 unsigned int widthcxx . . $ glitch0r_instance#width.;
53 unsigned int heightcxx . . $ glitch0r_instance#height.;
54
55 unsigned int maxBlockSizecxx . . $ glitch0r_instance#maxBlockSize.;
56 unsigned int maxBlockShiftcxx . . $ glitch0r_instance#maxBlockShift.;
57
58 short int colorGlitchIntensitycxx . . $ glitch0r_instance#colorGlitchIntensity.;
59 short int doColorDistortioncxx . . $ glitch0r_instance#doColorDistortion.;
60 short int glitchChancecxx . . $ glitch0r_instance#glitchChance.;
61
62 struct glitch0r_statecxx . . $ glitch0r_state# statecxx . . $ glitch0r_instance#state.; // Instance-specific state
63 } glitch0r_instance_tcxx . . $ glitch0r_instance_t#;
64
65
66 inline static unsigned int rndcxx . . $ rnd(83f10ac8959d5577). (unsigned int min, unsigned int max)
67 {
68 return fastrandcxx . . $ fastrand(f0be770dcc0b0316).() % (max - min + 1) + min;
69 }
70
71 inline static void glitch0r_state_resetcxx . . $ glitch0r_state_reset(a0069cb53603d495).(glitch0r_instance_tcxx . . $ glitch0r_instance_t# *inst)
72 {
73 inst->statecxx . . $ glitch0r_instance#state..currentPoscxx . . $ glitch0r_state#currentPos. = 0;
74 inst->statecxx . . $ glitch0r_instance#state..currentBlockcxx . . $ glitch0r_state#currentBlock. = rndcxx . . $ rnd(83f10ac8959d5577).(1, inst->maxBlockSizecxx . . $ glitch0r_instance#maxBlockSize.);
75 inst->statecxx . . $ glitch0r_instance#state..blkShiftcxx . . $ glitch0r_state#blkShift. = rndcxx . . $ rnd(83f10ac8959d5577).(1, inst->maxBlockShiftcxx . . $ glitch0r_instance#maxBlockShift.);
76 inst->statecxx . . $ glitch0r_instance#state..passThisLinecxx . . $ glitch0r_state#passThisLine. = (inst->glitchChancecxx . . $ glitch0r_instance#glitchChance. < rndcxx . . $ rnd(83f10ac8959d5577).(1, 101)) ? 1 : 0;
77
78 if (inst->doColorDistortioncxx . . $ glitch0r_instance#doColorDistortion.)
79 {
80 inst->statecxx . . $ glitch0r_instance#state..distortionSeed1cxx . . $ glitch0r_state#distortionSeed1. = rndcxx . . $ rnd(83f10ac8959d5577).(0x00000000, 0xfffffffe);
81 inst->statecxx . . $ glitch0r_instance#state..distortionSeed2cxx . . $ glitch0r_state#distortionSeed2. = rndcxx . . $ rnd(83f10ac8959d5577).(0x00000000, 0xfffffffe);
82 inst->statecxx . . $ glitch0r_instance#state..howToDistort1cxx . . $ glitch0r_state#howToDistort1. = rndcxx . . $ rnd(83f10ac8959d5577). (0, inst->colorGlitchIntensitycxx . . $ glitch0r_instance#colorGlitchIntensity.);
83 inst->statecxx . . $ glitch0r_instance#state..howToDistort2cxx . . $ glitch0r_state#howToDistort2. = rndcxx . . $ rnd(83f10ac8959d5577). (0, inst->colorGlitchIntensitycxx . . $ glitch0r_instance#colorGlitchIntensity.);
84 }
85 }
86
87 inline static void glitch0r_pixel_dist0rtcxx . . $ glitch0r_pixel_dist0rt(599a42e9249dffe1). (uint32_tcxx . . $ uint32_t# *pixel,
88 uint32_tcxx . . $ uint32_t# distortionSeed, short int howToDistort)
89 {
90
91 // Save alpha
92 uint8_tcxx . . $ uint8_t# *px = (uint8_tcxx . . $ uint8_t# *)pixel;
93 uint8_tcxx . . $ uint8_t# alpha = px[3];
94
95 // Choose from five levels of madness:
96 switch (howToDistort)
97 {
98 case 0 : return; // ok, let this pixel live (just shift)
99
100 case 1 : // lightest distortion: just invert
101 *(pixel) = ~*(pixel);
102 break;
103
104 case 2 : // add some unneeded colors
105 *(pixel) = distortionSeed | *(pixel);
106 break;
107
108 case 3 : // change some colors
109 *(pixel) = distortionSeed ^ *(pixel);
110 break;
111
112 case 4 : // oh shi...
113 *(pixel) = distortionSeed & *(pixel);
114 break;
115 }
116
117 // Restore alpha
118 px[3] = alpha;
119 }
120
121 int f0r_initcxx . . $ f0r_init(b126dc7c1de90089).()
122 {
123 fastsrandcxx . . $ fastsrand(cb2f890c2fdad230).((uint32_tcxx . . $ uint32_t#)timecxx . . $ time(7f38134db6521d23).(0));
124 return 1;
125 }
126
127 void f0r_deinitcxx . . $ f0r_deinit(49f6e7a06ebc5aa8).()
128 { /* no initialization required */ }
129
130 void f0r_get_plugin_infocxx . . $ f0r_get_plugin_info(35d753456c0ea59c).(f0r_plugin_info_tcxx . . $ f0r_plugin_info_t#* glitch0rInfo)
131 {
132 glitch0rInfo->namecxx . . $ f0r_plugin_info#name. = "Glitch0r";
133 glitch0rInfo->authorcxx . . $ f0r_plugin_info#author. = "IDENT Software";
134 glitch0rInfo->plugin_typecxx . . $ f0r_plugin_info#plugin_type. = F0R_PLUGIN_TYPE_FILTERcxx . . $ `include/frei0r.h:247:9`!;
135 glitch0rInfo->color_modelcxx . . $ f0r_plugin_info#color_model. = F0R_COLOR_MODEL_RGBA8888cxx . . $ `include/frei0r.h:308:9`!;
136 glitch0rInfo->frei0r_versioncxx . . $ f0r_plugin_info#frei0r_version. = FREI0R_MAJOR_VERSIONcxx . . $ `include/frei0r.h:217:9`!;
137 glitch0rInfo->major_versioncxx . . $ f0r_plugin_info#major_version. = 0;
138 glitch0rInfo->minor_versioncxx . . $ f0r_plugin_info#minor_version. = 1;
139 glitch0rInfo->num_paramscxx . . $ f0r_plugin_info#num_params. = 4;
140 glitch0rInfo->explanationcxx . . $ f0r_plugin_info#explanation. = "Adds glitches and block shifting";
141 }
142
143 void f0r_get_param_infocxx . . $ f0r_get_param_info(a9f9aa7954cc4a37).(f0r_param_info_tcxx . . $ f0r_param_info_t#* info, int param_index)
144 {
145 switch(param_index) {
146 case 0:
147 {
148 info->namecxx . . $ f0r_param_info#name. = "Glitch frequency";
149 info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_DOUBLEcxx . . $ `include/frei0r.h:383:9`!;
150 info->explanationcxx . . $ f0r_param_info#explanation. = "How frequently the glitch should be applied";
151 break;
152 }
153
154 case 1:
155 {
156 info->namecxx . . $ f0r_param_info#name. = "Block height";
157 info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_DOUBLEcxx . . $ `include/frei0r.h:383:9`!;
158 info->explanationcxx . . $ f0r_param_info#explanation. = "Height range of the block that will be shifted/glitched";
159 break;
160 }
161
162 case 2:
163 {
164 info->namecxx . . $ f0r_param_info#name. = "Shift intensity";
165 info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_DOUBLEcxx . . $ `include/frei0r.h:383:9`!;
166 info->explanationcxx . . $ f0r_param_info#explanation. = "How much we should move blocks when glitching";
167 break;
168 }
169
170 case 3:
171 {
172 info->namecxx . . $ f0r_param_info#name. = "Color glitching intensity";
173 info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_DOUBLEcxx . . $ `include/frei0r.h:383:9`!;
174 info->explanationcxx . . $ f0r_param_info#explanation. = "How intensive should be color distortion";
175 break;
176 }
177 }
178 }
179
180 f0r_instance_tcxx . . $ f0r_instance_t# f0r_constructcxx . . $ f0r_construct(da3f471cd007a696).(unsigned int width, unsigned int height)
181 {
182 glitch0r_instance_tcxx . . $ glitch0r_instance_t#* inst = (glitch0r_instance_tcxx . . $ glitch0r_instance_t#*)calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(*inst));
183 inst->widthcxx . . $ glitch0r_instance#width. = width; inst->heightcxx . . $ glitch0r_instance#height. = height;
184 inst->maxBlockSizecxx . . $ glitch0r_instance#maxBlockSize. = (unsigned int)MAXcxx . . $ `include/frei0r/math.h:71:9`!(1, inst->height / 2);
185 inst->maxBlockShiftcxx . . $ glitch0r_instance#maxBlockShift. = (unsigned int)MAXcxx . . $ `include/frei0r/math.h:71:9`!(1, inst->width / 2);
186 inst->colorGlitchIntensitycxx . . $ glitch0r_instance#colorGlitchIntensity. = 3;
187 inst->doColorDistortioncxx . . $ glitch0r_instance#doColorDistortion. = 1;
188
189 glitch0r_state_resetcxx . . $ glitch0r_state_reset(a0069cb53603d495).(inst);
190
191 return (f0r_instance_tcxx . . $ f0r_instance_t#)inst;
192 }
193
194 void f0r_destructcxx . . $ f0r_destruct(5d22bdacc48458e8).(f0r_instance_tcxx . . $ f0r_instance_t# instance)
195 {
196 freecxx . . $ free(5d22bdacc48458e8).(instance);
197 }
198
199 void f0r_set_param_valuecxx . . $ f0r_set_param_value(5be095a8278d2bf5).(f0r_instance_tcxx . . $ f0r_instance_t# instance,
200 f0r_param_tcxx . . $ f0r_param_t# param, int param_index)
201 {
202 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance);
203 glitch0r_instance_tcxx . . $ glitch0r_instance_t# * inst = (glitch0r_instance_tcxx . . $ glitch0r_instance_t#*)instance;
204
205 switch (param_index)
206 {
207
208 case 0 : // glitch frequency
209 {
210 double glitchChance = *((double*)param);
211 // convert frei0r range [0, 1] to range between 0 and 100
212 glitchChance = (100 * glitchChance);
213 inst->glitchChancecxx . . $ glitch0r_instance#glitchChance. = (short int)glitchChance;
214 break;
215 }
216
217 case 1 : // block height
218 {
219 double mbHeight = *((double*)param);
220 // convert frei0r range [0, 1] to range between 1 and maximal height
221 mbHeight = (1 + (inst->heightcxx . . $ glitch0r_instance#height. - 1) * mbHeight);
222 inst->maxBlockSizecxx . . $ glitch0r_instance#maxBlockSize. = (unsigned int)mbHeight;
223
224 // zero size is a bad idea
225 if (inst->maxBlockSizecxx . . $ glitch0r_instance#maxBlockSize. == 0)
226 inst->maxBlockSizecxx . . $ glitch0r_instance#maxBlockSize. = (unsigned int)MAXcxx . . $ `include/frei0r/math.h:71:9`!(1, inst->height / 2);
227 break;
228 }
229
230 case 2 : // shift intensity
231 {
232 double intensity = *((double*)param);
233 // convert frei0r range [0, 1] to range between 1 and maximal width
234 intensity = (1 + (inst->widthcxx . . $ glitch0r_instance#width. - 1) * intensity);
235 inst->maxBlockShiftcxx . . $ glitch0r_instance#maxBlockShift. = (unsigned int)intensity;
236
237 // zero shift is a bad idea
238 if (inst->maxBlockShiftcxx . . $ glitch0r_instance#maxBlockShift. == 0)
239 inst->maxBlockShiftcxx . . $ glitch0r_instance#maxBlockShift. = (unsigned int)MAXcxx . . $ `include/frei0r/math.h:71:9`!(1, inst->width / 2);
240 break;
241 }
242
243 case 3 : // color intensity
244 {
245 double intensity = *((double*)param);
246 // convert frei0r range [0, 1] to range between 0 and 5
247 intensity = (5 * intensity);
248 inst->colorGlitchIntensitycxx . . $ glitch0r_instance#colorGlitchIntensity. = (short int)intensity;
249
250 if (inst->colorGlitchIntensitycxx . . $ glitch0r_instance#colorGlitchIntensity. > 0)
251 {
252 inst->doColorDistortioncxx . . $ glitch0r_instance#doColorDistortion. = 1;
253 }
254 else
255 inst->doColorDistortioncxx . . $ glitch0r_instance#doColorDistortion. = 0;
256
257 break;
258 }
259 }
260 }
261
262 void f0r_get_param_valuecxx . . $ f0r_get_param_value(5be095a8278d2bf5).(f0r_instance_tcxx . . $ f0r_instance_t# instance,
263 f0r_param_tcxx . . $ f0r_param_t# param, int param_index)
264 {
265
266 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance);
267 glitch0r_instance_tcxx . . $ glitch0r_instance_t# * inst = (glitch0r_instance_tcxx . . $ glitch0r_instance_t#*)instance;
268
269 switch (param_index)
270 {
271 case 0 : // glitch frequency
272 {
273 // convert plugin's param to frei0r range
274 *((double*)param) = (inst->glitchChancecxx . . $ glitch0r_instance#glitchChance.) / 100;
275 break;
276 }
277
278 case 1 : // block height
279 {
280 // convert plugin's param to frei0r range
281 *((double*)param) = (inst->maxBlockSizecxx . . $ glitch0r_instance#maxBlockSize. - 1.0) / MAXcxx . . $ `include/frei0r/math.h:71:9`!(1, inst->height - 1.0);
282 break;
283 }
284
285 case 2 : // block shift intensity
286 {
287 *((double*)param) = (inst->maxBlockShiftcxx . . $ glitch0r_instance#maxBlockShift. - 1.0) / MAXcxx . . $ `include/frei0r/math.h:71:9`!(1, inst->width - 1.0);
288 break;
289 }
290
291 case 3 : // color glitch intensity
292 {
293 *((double*)param) = (inst->colorGlitchIntensitycxx . . $ glitch0r_instance#colorGlitchIntensity.) / 5; // 5 levels of madness
294 break;
295 }
296 }
297
298 }
299
300 void f0r_updatecxx . . $ f0r_update(96cd7b27f2fca8a7).(f0r_instance_tcxx . . $ f0r_instance_t# instance, double time,
301 const uint32_tcxx . . $ uint32_t#* inframe, uint32_tcxx . . $ uint32_t#* outframe)
302 {
303 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance);
304 glitch0r_instance_tcxx . . $ glitch0r_instance_t#* inst = (glitch0r_instance_tcxx . . $ glitch0r_instance_t#*)instance;
305 unsigned int x, y;
306
307 uint32_tcxx . . $ uint32_t#* dst = outframe;
308 const uint32_tcxx . . $ uint32_t#* src = inframe;
309 uint32_tcxx . . $ uint32_t# *pixel;
310
311 inst->statecxx . . $ glitch0r_instance#state..currentBlockcxx . . $ glitch0r_state#currentBlock. = rndcxx . . $ rnd(83f10ac8959d5577).(1, inst->maxBlockSizecxx . . $ glitch0r_instance#maxBlockSize.);
312
313 for (y = 0; y < inst->heightcxx . . $ glitch0r_instance#height.; y++)
314 {
315
316 if (inst->statecxx . . $ glitch0r_instance#state..currentPoscxx . . $ glitch0r_state#currentPos. > inst->statecxx . . $ glitch0r_instance#state..currentBlockcxx . . $ glitch0r_state#currentBlock.)
317 {
318 glitch0r_state_resetcxx . . $ glitch0r_state_reset(a0069cb53603d495).(inst);
319 }
320 else
321 inst->statecxx . . $ glitch0r_instance#state..currentPoscxx . . $ glitch0r_state#currentPos.++;
322
323 inst->statecxx . . $ glitch0r_instance#state..currentYcxx . . $ glitch0r_state#currentY. = y*inst->widthcxx . . $ glitch0r_instance#width.;
324 pixel = dst + inst->statecxx . . $ glitch0r_instance#state..currentYcxx . . $ glitch0r_state#currentY.;
325
326 if (inst->statecxx . . $ glitch0r_instance#state..passThisLinecxx . . $ glitch0r_state#passThisLine.)
327 {
328 memcpycxx . . $ memcpy(cc817cb175f282d).((uint32_tcxx . . $ uint32_t# *)(dst + inst->statecxx . . $ glitch0r_instance#state..currentYcxx . . $ glitch0r_state#currentY.),
329 (uint32_tcxx . . $ uint32_t# *)(src + inst->statecxx . . $ glitch0r_instance#state..currentYcxx . . $ glitch0r_state#currentY.),
330 (inst->widthcxx . . $ glitch0r_instance#width.) * sizeof(uint32_tcxx . . $ uint32_t#));
331 continue;
332 }
333
334 for (x = inst->statecxx . . $ glitch0r_instance#state..blkShiftcxx . . $ glitch0r_state#blkShift.; x < (inst->widthcxx . . $ glitch0r_instance#width.); x++)
335 {
336 *(pixel) = *(src + inst->statecxx . . $ glitch0r_instance#state..currentYcxx . . $ glitch0r_state#currentY. + x);
337
338 if (inst->doColorDistortioncxx . . $ glitch0r_instance#doColorDistortion.)
339 glitch0r_pixel_dist0rtcxx . . $ glitch0r_pixel_dist0rt(599a42e9249dffe1).(pixel,
340 inst->statecxx . . $ glitch0r_instance#state..distortionSeed1cxx . . $ glitch0r_state#distortionSeed1., inst->statecxx . . $ glitch0r_instance#state..howToDistort1cxx . . $ glitch0r_state#howToDistort1.);
341
342 pixel++;
343 }
344
345 for (x = 0; x < inst->statecxx . . $ glitch0r_instance#state..blkShiftcxx . . $ glitch0r_state#blkShift.; x++)
346 {
347 *(pixel) = *(src + inst->statecxx . . $ glitch0r_instance#state..currentYcxx . . $ glitch0r_state#currentY. + x);
348
349 if (inst->doColorDistortioncxx . . $ glitch0r_instance#doColorDistortion.)
350 glitch0r_pixel_dist0rtcxx . . $ glitch0r_pixel_dist0rt(599a42e9249dffe1).(pixel,
351 inst->statecxx . . $ glitch0r_instance#state..distortionSeed2cxx . . $ glitch0r_state#distortionSeed2., inst->statecxx . . $ glitch0r_instance#state..howToDistort2cxx . . $ glitch0r_state#howToDistort2.);
352
353 pixel++;
354 }
355
356 }
357 }
358