Debian Codegraph @

src/filter/alpha0ps/alpha0ps_alphaspot.c

raw symbols SCIP
1 /*
2 alphaspot.c
3
4 This frei0r plugin draws simple shapes into the alpha channel
5 Version 0.1 aug 2010
6
7 Copyright (C) 2010 Marko Cebokli http://lea.hamradio.si/~s57uuu
8
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 */
25
26
27 //compile: gcc -c -fPIC -Wall alphaspot.c -o alphaspot.o
28 //link: gcc -shared -o alphaspot.so alphaspot.o
29
30 //#include <stdio.h>
31 #include <frei0r.h>
32 #include <frei0r/math.h>
33 #include <stdlib.h>
34 #include <math.h>
35 #include <assert.h>
36 #include <string.h>
37
38
39 //----------------------------------------
40 //struktura za instanco efekta
41 typedef struct
42 {
43 int h;
44 int w;
45
46 float pozx,pozy,sizx,sizy,wdt,tilt,min,max;
47 int shp,op;
48
49 uint8_t *gr8;
50
51 } inst;
52
53
54 //----------------------------------------------------------
55 //general (rotated) rectangle with soft border
56 void gen_rec_s(uint8_t* sl, int w, int h, float siz1, float siz2, float tilt, float pozx, float pozy, float min, float max, float wb)
57 {
58 int i,j;
59 float d1,d2,d,db,st,ct,g,is1,is2;
60
61 if ((siz1 == 0.0f) || (siz2 == 0.0f)) return;
62 st = sinf(tilt);
63 ct = cosf(tilt);
64 is1 = 1.0f/siz1;
65 is2 = 1.0f/siz2;
66
67 for (i = 0; i < h; i++) {
68 for (j = 0; j < w; j++) {
69 d1 = (i-pozy)*st+(j-pozx)*ct;
70 d2 = (i-pozy)*ct-(j-pozx)*st;
71 d1 = fabsf(d1)*is1;
72 d2 = fabsf(d2)*is2;
73 d = (d1<d2)?d2:d1;
74 d1 = 1.0f - (1.0f - d1)*is2/is1;
75 db = (d1<d2)?d2:d1;
76 if (fabsf(d) > 1.0f) {
77 g = min;
78 } else {
79 if (db <= 1.004f - wb) {
80 g = max;
81 } else {
82 g = min + (1.0f - wb-db)/wb*(max-min);
83 }
84 }
85 sl[i*w+j] = lrintf(g * 255.0f);
86 }
87 }
88 }
89
90 //----------------------------------------------------------
91 //general (rotated) ellipse with soft border
92 void gen_eli_s(uint8_t* sl, int w, int h, float siz1, float siz2, float tilt, float pozx, float pozy, float min, float max, float wb)
93 {
94 int i,j;
95 float d1,d2,d,db,st,ct,is1,is2,g;
96
97 if ((siz1 == 0.0f) || (siz2 == 0.0f)) return;
98 st = sinf(tilt);
99 ct = cosf(tilt);
100 is1 = 1.0f/siz1;
101 is2 = 1.0f/siz2;
102
103 for (i = 0; i < h; i++) {
104 for (j = 0; j < w; j++) {
105 d1 = (i-pozy)*st+(j-pozx)*ct;
106 d2 = (i-pozy)*ct-(j-pozx)*st;
107 d = hypotf(d1*is1,d2*is2);
108
109 db = d; //neenakomeren rob!!!
110
111 if (d > 1.0f) {
112 g = min;
113 } else {
114 if (db <= 1.004f - wb) {
115 g = max;
116 } else {
117 g = min + (1.0f - wb-db)/wb*(max-min);
118 }
119 }
120 sl[i*w+j] = lrintf(g * 255.0f);
121 }
122 }
123 }
124
125 //----------------------------------------------------------
126 //general (rotated) triangle with soft border
127 void gen_tri_s(uint8_t* sl, int w, int h, float siz1, float siz2, float tilt, float pozx, float pozy, float min, float max, float wb)
128 {
129 int i,j;
130 float d1,d2,d3,d4,d,st,ct,is1,is2,k5,lim,db,g;
131
132 if ((siz1 == 0.0f) || (siz2 == 0.0f)) return;
133 st =sinf(tilt);
134 ct = cosf(tilt);
135 is1 = 1.0f/siz1;
136 is2 = 1.0f/siz2;
137 k5 = 1.0f/sqrtf(5.0f);
138 lim = 0.82f;
139
140 for (i = 0; i < h; i++) {
141 for (j = 0; j < w; j++) {
142 d1 = (i-pozy)*st+(j-pozx)*ct;
143 d2 = (i-pozy)*ct-(j-pozx)*st;
144 d1 = d1*is1;
145 d2 = d2*is2;
146 d3 = (2.0f * d1+d2 + 1.0f)*k5;
147 d4 = (2.0f* d1-d2 - 1.0f)*k5;
148 d2 = fabsf(d2);
149 d3 = fabsf(d3);
150 d4 = fabsf(d4);
151 d = d2;
152 if (d3 > d) d=d3;
153 if (d4 > d) d=d4;
154 db = d;
155
156 if (fabsf(d) > lim) {
157 g = min;
158 } else {
159 if (db <= 1.004f * lim-wb) {
160 g = max;
161 } else {
162 g = min + (lim-wb-db)/wb*(max-min);
163 }
164 }
165 sl[i*w+j] = lrintf(g * 255.0f);
166 }
167 }
168 }
169
170 //----------------------------------------------------------
171 //general (rotated) diamond shape with soft border
172 void gen_dia_s(uint8_t* sl, int w, int h, float siz1, float siz2, float tilt, float pozx, float pozy, float min, float max, float wb)
173 {
174 int i,j;
175 float d1,d2,d,db,st,ct,is1,is2,g;
176
177 if ((siz1 == 0.0f) || (siz2 == 0.0f)) return;
178 st = sinf(tilt);
179 ct = cosf(tilt);
180 is1 = 1.0f/siz1;
181 is2 = 1.0f/siz2;
182
183 for (i = 0; i < h; i++) {
184 for (j = 0; j < w; j++) {
185 d1 = (i-pozy)*st+(j-pozx)*ct;
186 d2 = (i-pozy)*ct-(j-pozx)*st;
187 d = fabsf(d1*is1)+fabsf(d2*is2);
188 db = d;
189 if (fabsf(d) > 1.0f) {
190 g = min;
191 } else {
192 if (db <= 1.004f - wb) {
193 g = max;
194 } else {
195 g = min + (1.0f - wb-db)/wb*(max-min);
196 }
197 }
198 sl[i*w+j] = lrintf(g * 255.0f);
199 }
200 }
201 }
202
203 //-----------------------------------------------------
204 void draw(inst *in)
205 {
206 switch (in->shp)
207 {
208 case 0:
209 gen_rec_s(in->gr8, in->w, in->h, in->sizx*in->w, in->sizy*in->h, in->tilt, in->pozx*in->w, in->pozy*in->h, in->min, in->max, in->wdt);
210 break;
211 case 1:
212 gen_eli_s(in->gr8, in->w, in->h, in->sizx*in->w, in->sizy*in->h, in->tilt, in->pozx*in->w, in->pozy*in->h, in->min, in->max, in->wdt);
213 break;
214 case 2:
215 gen_tri_s(in->gr8, in->w, in->h, in->sizx*in->w, in->sizy*in->h, in->tilt, in->pozx*in->w, in->pozy*in->h, in->min, in->max, in->wdt);
216 break;
217 case 3:
218 gen_dia_s(in->gr8, in->w, in->h, in->sizx*in->w, in->sizy*in->h, in->tilt, in->pozx*in->w, in->pozy*in->h, in->min, in->max, in->wdt);
219 break;
220 default:
221 break;
222 }
223 }
224
225 //-----------------------------------------------------
226 //stretch [0...1] to parameter range [min...max] linear
227 float map_value_forward(double v, float min, float max)
228 {
229 return min+(max-min)*v;
230 }
231
232 //-----------------------------------------------------
233 //collapse from parameter range [min...max] to [0...1] linear
234 double map_value_backward(float v, float min, float max)
235 {
236 return (v-min)/(max-min);
237 }
238
239 //***********************************************
240 // MANDATORY FREI0R FUNCTIONS
241
242 //-----------------------------------------------
243 int f0r_init()
244 {
245 return 1;
246 }
247
248 //------------------------------------------------
249 void f0r_deinit()
250 {
251 }
252
253 //-----------------------------------------------
254 void f0r_get_plugin_info(f0r_plugin_info_t* info)
255 {
256
257 info->name = "alphaspot";
258 info->author = "Marko Cebokli";
259 info->plugin_type = F0R_PLUGIN_TYPE_FILTER;
260 info->color_model = F0R_COLOR_MODEL_RGBA8888;
261 info->frei0r_version = FREI0R_MAJOR_VERSION;
262 info->major_version = 0;
263 info->minor_version = 2;
264 info->num_params = 10;
265 info->explanation = "Draws simple shapes into the alpha channel";
266 }
267
268 //--------------------------------------------------
269 void f0r_get_param_info(f0r_param_info_t* info, int param_index)
270 {
271 switch(param_index) {
272 case 0:
273 info->name = "Shape";
274 info->type = F0R_PARAM_DOUBLE;
275 info->explanation = "";
276 break;
277 case 1:
278 info->name = "Position X";
279 info->type = F0R_PARAM_DOUBLE;
280 info->explanation = "";
281 break;
282 case 2:
283 info->name = "Position Y";
284 info->type = F0R_PARAM_DOUBLE;
285 info->explanation = "";
286 break;
287 case 3:
288 info->name = "Size X";
289 info->type = F0R_PARAM_DOUBLE;
290 info->explanation = "";
291 break;
292 case 4:
293 info->name = "Size Y";
294 info->type = F0R_PARAM_DOUBLE;
295 info->explanation = "";
296 break;
297 case 5:
298 info->name = "Tilt";
299 info->type = F0R_PARAM_DOUBLE;
300 info->explanation = "";
301 break;
302 case 6:
303 info->name = "Transition width";
304 info->type = F0R_PARAM_DOUBLE;
305 info->explanation = "";
306 break;
307 case 7:
308 info->name = "Min";
309 info->type = F0R_PARAM_DOUBLE;
310 info->explanation = "";
311 break;
312 case 8:
313 info->name = "Max";
314 info->type = F0R_PARAM_DOUBLE;
315 info->explanation = "";
316 break;
317 case 9:
318 info->name = "Operation";
319 info->type = F0R_PARAM_DOUBLE;
320 info->explanation = "";
321 break;
322 }
323 }
324
325 //----------------------------------------------
326 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
327 {
328 inst *in;
329
330 in = calloc(1, sizeof(inst));
331 in->w = width;
332 in->h = height;
333
334 in->shp = 0;
335 in->pozx = 0.5f;
336 in->pozy = 0.5f;
337 in->sizx = 0.1f;
338 in->sizy = 0.1f;
339 in->wdt = 0.2f;
340 in->tilt = 0.0f;
341 in->min = 0.0f;
342 in->max = 1.0f;
343 in->op = 0;
344
345 in->gr8 = calloc(in->w*in->h, sizeof(*in->gr8));
346
347 draw(in);
348
349 return (f0r_instance_t)in;
350 }
351
352 //---------------------------------------------------
353 void f0r_destruct(f0r_instance_t instance)
354 {
355 inst *in;
356
357 in = (inst*) instance;
358
359 free(in->gr8);
360 free(instance);
361 }
362
363 //-----------------------------------------------------
364 void f0r_set_param_value(f0r_instance_t instance, f0r_param_t parm, int param_index)
365 {
366 inst *p;
367 double tmpf;
368 int tmpi,chg;
369
370 p = (inst*) instance;
371
372 chg = 0;
373 switch (param_index) {
374 case 0:
375 tmpi = map_value_forward(*((double*)parm), 0.0f, 3.9999f);
376 if (p->shp != tmpi) chg=1;
377 p->shp = tmpi;
378 break;
379 case 1:
380 tmpf = *(double*)parm;
381 if (tmpf != p->pozx) chg = 1;
382 p->pozx = tmpf;
383 break;
384 case 2:
385 tmpf = *(double*)parm;
386 if (tmpf != p->pozy) chg = 1;
387 p->pozy = tmpf;
388 break;
389 case 3:
390 tmpf = *(double*)parm;
391 if (tmpf != p->sizx) chg = 1;
392 p->sizx = tmpf;
393 break;
394 case 4:
395 tmpf = *(double*)parm;
396 if (tmpf != p->sizy) chg = 1;
397 p->sizy = tmpf;
398 break;
399 case 5:
400 tmpf = map_value_forward(*((double*)parm), -3.15f, 3.15f);
401 if (tmpf != p->tilt) chg = 1;
402 p->tilt = tmpf;
403 break;
404 case 6:
405 tmpf = *(double*)parm;
406 if (tmpf != p->wdt) chg = 1;
407 p->wdt = tmpf;
408 break;
409 case 7:
410 tmpf = *(double*)parm;
411 if (tmpf != p->min) chg = 1;
412 p->min = tmpf;
413 break;
414 case 8:
415 tmpf = *(double*)parm;
416 if (tmpf != p->max) chg = 1;
417 p->max = tmpf;
418 break;
419 case 9:
420 tmpi = map_value_forward(*((double*)parm), 0.0f, 4.9999f);
421 if (p->op != tmpi) chg = 1;
422 p->op = tmpi;
423 break;
424 }
425
426 if (chg == 0) return;
427
428 draw(p);
429
430 }
431
432 //--------------------------------------------------
433 void f0r_get_param_value(f0r_instance_t instance, f0r_param_t param, int param_index)
434 {
435 inst *p;
436
437 p=(inst*)instance;
438
439 switch(param_index) {
440 case 0:
441 *((double*)param) = map_value_backward(p->shp, 0.0f, 3.9999f);
442 break;
443 case 1:
444 *((double*)param) = p->pozx;
445 break;
446 case 2:
447 *((double*)param) = p->pozy;
448 break;
449 case 3:
450 *((double*)param) = p->sizx;
451 break;
452 case 4:
453 *((double*)param) = p->sizy;
454 break;
455 case 5:
456 *((double*)param) = map_value_backward(p->tilt, -3.15f, 3.15f);
457 break;
458 case 6:
459 *((double*)param) = p->wdt;
460 break;
461 case 7:
462 *((double*)param) = p->min;
463 break;
464 case 8:
465 *((double*)param) = p->max;
466 break;
467 case 9:
468 *((double*)param) = map_value_backward(p->op, 0.0f, 4.9999f);
469 break;
470 }
471 }
472
473 //-------------------------------------------------
474 void f0r_update(f0r_instance_t instance, double time, const uint32_t* inframe, uint32_t* outframe)
475 {
476 inst *in;
477 int i;
478 uint8_t *inp = (uint8_t*) inframe;
479 uint8_t *outp = (uint8_t*) outframe;
480
481 assert(instance);
482 in = (inst*)instance;
483 memcpy(outframe, inframe, sizeof(*inframe) * in->w * in->h);
484
485 switch (in->op) {
486 case 0: //write on clear
487 for (i = 0; i < in->h*in->w; i++) {
488 outp[4*i+3] = in->gr8[i];
489 }
490 break;
491 case 1: //max
492 for (i = 0; i < in->h*in->w; i++) {
493 outp[4*i+3] = (inp[4*i+3] > in->gr8[i]) ? inp[4*i+3] : in->gr8[i];
494 }
495 break;
496 case 2: //min
497 for (i = 0; i < in->h*in->w; i++) {
498 outp[4*i+3] = (inp[4*i+3] < in->gr8[i]) ? inp[4*i+3] : in->gr8[i];
499 }
500 break;
501 case 3: //add
502 for (i = 0;i < in->h*in->w; i++) {
503 outp[4*i+3] = MAX255(inp[4*i+3] + in->gr8[i]);
504 }
505 break;
506 case 4: //subtract
507 for (i = 0; i < in->h*in->w; i++) {
508 outp[4*i+3] = (inp[4*i+3] > in->gr8[i]) ? inp[4*i+3] - in->gr8[i] : 0;
509 }
510 break;
511 default:
512 break;
513 }
514 }