Debian Codegraph @

src/filter/tehroxx0r/tehroxx0r.c

raw symbols SCIP
1 #include "frei0r.h"
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <limits.h>
5 #include <string.h>
6
7 /**
8 * This is our instance.
9 * It has a buffer allocated to place a small version of the incoming
10 * frame into.
11 */
12 typedef struct teh_roxx0r
13 {
14 unsigned int width; // frame size in x-dimension
15 unsigned int height; // frame size in y-dimension
16 unsigned int block_size; // x/y size of one block
17
18 double change_speed;
19 double last_time;
20 double time_stack;
21
22 uint32_t* small_block; // buffer to write downscaled frame
23
24 } tehRoxx0r_instance_t;
25
26
27 // returns greatest common divisor of to int numbers
28 int gcd(int a, int b);
29
30 int f0r_init()
31 {
32 return 1;
33 }
34
35 void f0r_deinit()
36 { /* no initialization required */ }
37
38 void f0r_get_plugin_info(f0r_plugin_info_t* tehRoxx0rInfo)
39 {
40 tehRoxx0rInfo->name = "TehRoxx0r";
41 tehRoxx0rInfo->author = "Coma";
42 tehRoxx0rInfo->plugin_type = F0R_PLUGIN_TYPE_FILTER;
43 tehRoxx0rInfo->color_model = F0R_COLOR_MODEL_PACKED32;
44 tehRoxx0rInfo->frei0r_version = FREI0R_MAJOR_VERSION;
45 tehRoxx0rInfo->major_version = 0;
46 tehRoxx0rInfo->minor_version = 9;
47 tehRoxx0rInfo->num_params = 1;
48 tehRoxx0rInfo->explanation = "Something videowall-ish";
49 }
50
51 void f0r_get_param_info(f0r_param_info_t* info, int param_index)
52 {
53
54 info->name = "Interval";
55 info->type = F0R_PARAM_DOUBLE;
56 info->explanation = "Changing speed of small blocks";
57 }
58
59 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
60 {
61 int blocksize;
62 tehRoxx0r_instance_t* inst = (tehRoxx0r_instance_t*)calloc(1, sizeof(*inst));
63 inst->width = width; inst->height = height;
64 inst->change_speed = 0.01;
65
66 // get greatest common divisor
67 blocksize = gcd(width, height);
68 // this will sometimes be to large, so roughly estimate a check
69 if(blocksize >= (width/3) || blocksize >= (height/3))
70 blocksize /= 2;
71
72 inst->block_size = blocksize;
73
74 inst->small_block =
75 (uint32_t*)malloc(sizeof(uint32_t)*inst->block_size*inst->block_size);
76
77 return (f0r_instance_t)inst;
78 }
79
80 void f0r_destruct(f0r_instance_t instance)
81 {
82 tehRoxx0r_instance_t* inst = (tehRoxx0r_instance_t*)instance;
83 free(inst->small_block);
84 free(inst);
85 }
86
87 void f0r_set_param_value(f0r_instance_t instance,
88 f0r_param_t param, int param_index)
89 {
90 tehRoxx0r_instance_t* inst = (tehRoxx0r_instance_t *)instance;
91 // (tehRoxx0r_instance_t*)malloc(sizeof(tehRoxx0r_instance_t));
92
93 switch(param_index)
94 {
95 case 0:
96 inst->change_speed = *((double*)param);
97 break;
98 };
99 }
100
101 void f0r_get_param_value(f0r_instance_t instance,
102 f0r_param_t param, int param_index)
103 {
104 tehRoxx0r_instance_t* inst = (tehRoxx0r_instance_t *)instance;
105 // (tehRoxx0r_instance_t*)malloc(sizeof(tehRoxx0r_instance_t));
106
107 switch(param_index)
108 {
109 case 0:
110 *((double*)param) = inst->change_speed;
111 break;
112 };
113 }
114
115
116 void f0r_update(f0r_instance_t instance, double time,
117 const uint32_t* inframe, uint32_t* outframe)
118 {
119 assert(instance);
120 tehRoxx0r_instance_t* inst = (tehRoxx0r_instance_t*)instance;
121 unsigned int w = inst->width;
122 unsigned int h = inst->height;
123 uint32_t* dst;
124 const uint32_t* src;
125 uint32_t* small_block = inst->small_block;
126 unsigned int x,y;
127 unsigned int small_x, small_y;
128 unsigned int small_w, small_h;
129 double step_x, step_y;
130 unsigned int pos_w, pos_h;
131
132 // get x/y-size of middle block
133 small_w = w-2*inst->block_size;
134 small_h = h-2*inst->block_size;
135
136 // get interpolation step for that
137 step_x = (double)w / (double)small_w;
138 step_y = (double)h / (double)small_h;
139
140 // make background black transparent
141 memset(outframe, 0, w * h * sizeof(uint32_t));
142
143 // copy a downscaled version into the middle of the result frame
144 // (blocksize to x-blocksize and blocksize to y-blocksize)
145 for(y = 0, small_y=inst->block_size;small_y<h-inst->block_size;small_y++,
146 y=step_y*(small_y-inst->block_size))
147 {
148 src = inframe + y*w;
149 dst = outframe + small_y*w + inst->block_size;
150 for(x=0;x<w-2*inst->block_size;x++)
151 {
152 *dst++ = *(src + (int)(x*step_x));
153 }
154 }
155
156 // add elapsed time to timestack
157 inst->time_stack += (time-inst->last_time);
158
159
160 // get interpolation step size
161 step_x = w / inst->block_size;
162 step_y = h / inst->block_size;
163
164 // create a small picture
165 for(y=0,small_y=0; small_y<inst->block_size; small_y++,y+=step_y)
166 {
167 src = inframe + y*w;
168 dst = small_block + small_y*inst->block_size;
169 for(x=0,small_x = 0; small_x<inst->block_size; small_x++)//,x+=step_x)
170 {
171 *dst++ = *src;
172 src += (unsigned int)step_x;
173 }
174 }
175 // do we actually changed anything?
176 if(inst->time_stack > inst->change_speed)
177 {
178 // get random position
179 pos_w = inst->block_size *
180 (unsigned int)(rand()/(double)RAND_MAX * ((w / inst->block_size)));
181 pos_h = inst->block_size *
182 (unsigned int)(rand()/(double)RAND_MAX * ((h / inst->block_size)));
183
184 // now copy to some (random) places along the border of
185 // the incoming frame.....
186 dst = outframe + pos_w;
187 src = small_block;
188 for(x=0; x<inst->block_size;
189 x++, dst += w, src += inst->block_size)
190 memcpy(dst, src, sizeof(int32_t)*inst->block_size);
191
192 dst = outframe + pos_h * w;
193 src = small_block;
194 for(x=0; x<inst->block_size;
195 x++, dst += w, src += inst->block_size)
196 memcpy(dst, src, sizeof(int32_t)*inst->block_size);
197
198 dst = outframe + pos_h* w + w - inst->block_size;
199 src = small_block;
200 for(x=0; x<inst->block_size;
201 x++, dst += w, src += inst->block_size)
202 memcpy(dst, src, sizeof(int32_t)*inst->block_size);
203
204
205 dst = outframe + (h-inst->block_size) *w + pos_w;
206 src = small_block;
207 for(x=0; x<inst->block_size;
208 x++, dst += w, src += inst->block_size)
209 memcpy(dst, src, sizeof(int32_t)*inst->block_size);
210
211
212 // reset timestack
213 inst->time_stack = 0.0;
214 }
215
216
217 inst->last_time = time;
218 }
219
220 // greatest common divisor. this will never become smaller than 8.
221 int gcd(int a, int b)
222 {
223 if(b==0) return a;
224 else return gcd(b, a%b);
225 }