Codebase list frei0r / 2bb467c src / filter / alpha0ps / alphaspot.c
2bb467c

Tree @2bb467c (Download .tar.gz)

alphaspot.c @2bb467craw · history · blame

  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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
alphaspot.c

This frei0r plugin draws simple shapes into the alpha channel
Version 0.1	aug 2010

Copyright (C) 2010  Marko Cebokli    http://lea.hamradio.si/~s57uuu


 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/


//compile: gcc -c -fPIC -Wall alphaspot.c -o alphaspot.o
//link: gcc -shared -o alphaspot.so alphaspot.o

//#include <stdio.h>
#include <frei0r.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>



//----------------------------------------
//struktura za instanco efekta
typedef struct
{
int h;
int w;

float pozx,pozy,sizx,sizy,wdt,tilt,min,max;
int shp,op;

uint32_t *gr8;

} inst;


//----------------------------------------------------------
//general (rotated) rectangle with soft border
void gen_rec_s(uint32_t* sl, int w, int h, float siz1, float siz2, float tilt, float pozx, float pozy, float min, float max, float wb)
{
int i,j;
float d1,d2,d,db,st,ct,g,is1,is2;

if ((siz1==0.0)||(siz2==0.0)) return;
st=sinf(tilt);
ct=cosf(tilt);
is1=1.0/siz1;
is2=1.0/siz2;

for (i=0;i<h;i++)
	for (j=0;j<w;j++)
		{
		d1=(i-pozy)*st+(j-pozx)*ct;
		d2=(i-pozy)*ct-(j-pozx)*st;
		d1=fabsf(d1)*is1;
		d2=fabsf(d2)*is2;
		d=(d1<d2)?d2:d1;
		d1=1.0-(1.0-d1)*is2/is1;
		db=(d1<d2)?d2:d1;
		if (fabsf(d)>1.0)
			g=min;
		else
			{
			if (db<=1.004-wb)
				g=max;
			else
				{
				g=min+(1.0-wb-db)/wb*(max-min);
				}
			}
		g=g*255.0;
		sl[i*w+j] = (((uint32_t)g)<<24)&0xFF000000;
		}
}

//----------------------------------------------------------
//general (rotated) ellipse with soft border
void gen_eli_s(uint32_t* sl, int w, int h, float siz1, float siz2, float tilt, float pozx, float pozy, float min, float max, float wb)
{
int i,j;
float d1,d2,d,db,st,ct,is1,is2,g,wbb;

if ((siz1==0.0)||(siz2==0.0)) return;
st=sinf(tilt);
ct=cosf(tilt);
is1=1.0/siz1;
is2=1.0/siz2;
wbb=wb;

for (i=0;i<h;i++)
	for (j=0;j<w;j++)
		{
		d1=(i-pozy)*st+(j-pozx)*ct;
		d2=(i-pozy)*ct-(j-pozx)*st;
		d=hypotf(d1*is1,d2*is2);

		db=d;	//neenakomeren rob!!!

		if (d>1.0)
			g=min;
		else
			{
			if (db<=1.004-wb)
				g=max;
			else
				{
				g=min+(1.0-wb-db)/wb*(max-min);
				}
			}
		g=g*255.0;
		sl[i*w+j] = (((uint32_t)g)<<24)&0xFF000000;
		}
}

//----------------------------------------------------------
//general (rotated) triangle with soft border
void gen_tri_s(uint32_t* sl, int w, int h, float siz1, float siz2, float tilt, float pozx, float pozy, float min, float max, float wb)
{
int i,j;
float d1,d2,d3,d4,d,st,ct,is1,is2,k5,lim,db,g;

if ((siz1==0.0)||(siz2==0.0)) return;
st=sinf(tilt);
ct=cosf(tilt);
is1=1.0/siz1;
is2=1.0/siz2;
k5=1.0/sqrtf(5.0);
lim=0.82;

for (i=0;i<h;i++)
	for (j=0;j<w;j++)
		{
		d1=(i-pozy)*st+(j-pozx)*ct;
		d2=(i-pozy)*ct-(j-pozx)*st;
		d1=d1*is1;
		d2=d2*is2;
		d3=(2.0*d1+d2+1.0)*k5;
		d4=(2.0*d1-d2-1.0)*k5;
		d2=fabsf(d2);
		d3=fabsf(d3);
		d4=fabsf(d4);
		d=d2;
		if (d3>d) d=d3;
		if (d4>d) d=d4;
		db=d;

		if (fabsf(d)>lim)
			g=min;
		else
			{
			if (db<=1.004*lim-wb)
				g=max;
			else
				{
				g=min+(lim-wb-db)/wb*(max-min);
				}
			}
		g=g*255.0;
		sl[i*w+j] = (((uint32_t)g)<<24)&0xFF000000;
		}
}

//----------------------------------------------------------
//general (rotated) diamond shape with soft border
void gen_dia_s(uint32_t* sl, int w, int h, float siz1, float siz2, float tilt, float pozx, float pozy, float min, float max, float wb)
{
int i,j;
float d1,d2,d,db,st,ct,is1,is2,g;

if ((siz1==0.0)||(siz2==0.0)) return;
st=sinf(tilt);
ct=cosf(tilt);
is1=1.0/siz1;
is2=1.0/siz2;

for (i=0;i<h;i++)
	for (j=0;j<w;j++)
		{
		d1=(i-pozy)*st+(j-pozx)*ct;
		d2=(i-pozy)*ct-(j-pozx)*st;
		d=fabsf(d1*is1)+fabsf(d2*is2);
		db=d;
		if (fabsf(d)>1.0)
			g=min;
		else
			{
			if (db<=1.004-wb)
				g=max;
			else
				{
				g=min+(1.0-wb-db)/wb*(max-min);
				}
			}
		g=g*255.0;
		sl[i*w+j] = (((uint32_t)g)<<24)&0xFF000000;
		}
}

//-----------------------------------------------------
void draw(inst *in)
{
switch (in->shp)
	{
	case 0:
		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);
		break;
	case 1:
		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);
		break;
	case 2:
		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);
		break;
	case 3:
		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);
		break;
	default:
		break;
	}
}

//-----------------------------------------------------
//stretch [0...1] to parameter range [min...max] linear
float map_value_forward(double v, float min, float max)
{
return min+(max-min)*v;
}

//-----------------------------------------------------
//collapse from parameter range [min...max] to [0...1] linear
double map_value_backward(float v, float min, float max)
{
return (v-min)/(max-min);
}

//***********************************************
// OBVEZNE FREI0R FUNKCIJE

//-----------------------------------------------
int f0r_init()
{
return 1;
}

//------------------------------------------------
void f0r_deinit()
{
}

//-----------------------------------------------
void f0r_get_plugin_info(f0r_plugin_info_t* info)
{

info->name="alphaspot";
info->author="Marko Cebokli";
info->plugin_type=F0R_PLUGIN_TYPE_FILTER;
info->color_model=F0R_COLOR_MODEL_RGBA8888;
info->frei0r_version=FREI0R_MAJOR_VERSION;
info->major_version=0;
info->minor_version=1;
info->num_params=10;
info->explanation="Draws simple shapes into the alpha channel";
}

//--------------------------------------------------
void f0r_get_param_info(f0r_param_info_t* info, int param_index)
{
switch(param_index)
	{
	case 0:
		info->name = "Shape";
		info->type = F0R_PARAM_DOUBLE;
		info->explanation = "";
		break;
	case 1:
		info->name = "Position X";
		info->type = F0R_PARAM_DOUBLE;
		info->explanation = "";
		break;
	case 2:
		info->name = "Position Y";
		info->type = F0R_PARAM_DOUBLE;
		info->explanation = "";
		break;
	case 3:
		info->name = "Size X";
		info->type = F0R_PARAM_DOUBLE;
		info->explanation = "";
		break;
	case 4:
		info->name = "Size Y";
		info->type = F0R_PARAM_DOUBLE;
		info->explanation = "";
		break;
	case 5:
		info->name = "Tilt";
		info->type = F0R_PARAM_DOUBLE;
		info->explanation = "";
		break;
	case 6:
		info->name = "Transition width";
		info->type = F0R_PARAM_DOUBLE;
		info->explanation = "";
		break;
	case 7:
		info->name = "Min";
		info->type = F0R_PARAM_DOUBLE;
		info->explanation = "";
		break;
	case 8:
		info->name = "Max";
		info->type = F0R_PARAM_DOUBLE;
		info->explanation = "";
		break;
	case 9:
		info->name = "Operation";
		info->type = F0R_PARAM_DOUBLE;
		info->explanation = "";
		break;
	}
}

//----------------------------------------------
f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
{
inst *in;

in=calloc(1,sizeof(inst));
in->w=width;
in->h=height;

in->shp=0;
in->pozx=0.5;
in->pozy=0.5;
in->sizx=0.1;
in->sizy=0.1;
in->wdt=0.2;
in->tilt=0.0;
in->min=0.0;
in->max=1.0;
in->op=0;

in->gr8 = (uint32_t*)calloc(in->w*in->h, sizeof(uint32_t));

draw(in);

return (f0r_instance_t)in;
}

//---------------------------------------------------
void f0r_destruct(f0r_instance_t instance)
{
inst *in;

in=(inst*)instance;

free(in->gr8);
free(instance);
}

//-----------------------------------------------------
void f0r_set_param_value(f0r_instance_t instance, f0r_param_t parm, int param_index)
{
inst *p;
double tmpf;
int tmpi,chg;

p=(inst*)instance;

chg=0;
switch(param_index)
	{
	case 0:
                tmpi=map_value_forward(*((double*)parm), 0.0, 3.9999);
                if (p->shp != tmpi) chg=1;
                p->shp=tmpi;
		break;
	case 1:
		tmpf=*(double*)parm;
		if (tmpf!=p->pozx) chg=1;
		p->pozx=tmpf;
		break;
	case 2:
		tmpf=*(double*)parm;
		if (tmpf!=p->pozy) chg=1;
		p->pozy=tmpf;
		break;
	case 3:
		tmpf=*(double*)parm;
		if (tmpf!=p->sizx) chg=1;
		p->sizx=tmpf;
		break;
	case 4:
		tmpf=*(double*)parm;
		if (tmpf!=p->sizy) chg=1;
		p->sizy=tmpf;
		break;
	case 5:
                tmpf=map_value_forward(*((double*)parm), -3.15, 3.15);
		if (tmpf!=p->tilt) chg=1;
		p->tilt=tmpf;
		break;
	case 6:
		tmpf=*(double*)parm;
		if (tmpf!=p->wdt) chg=1;
		p->wdt=tmpf;
		break;
	case 7:
		tmpf=*(double*)parm;
		if (tmpf!=p->min) chg=1;
		p->min=tmpf;
		break;
	case 8:
		tmpf=*(double*)parm;
		if (tmpf!=p->max) chg=1;
		p->max=tmpf;
		break;
	case 9:
                tmpi=map_value_forward(*((double*)parm), 0.0, 4.9999);
                if (p->op != tmpi) chg=1;
                p->op=tmpi;
		break;
	}

if (chg==0) return;

draw(p);

}

//--------------------------------------------------
void f0r_get_param_value(f0r_instance_t instance, f0r_param_t param, int param_index)
{
inst *p;
double tmpf;
int tmpi;

p=(inst*)instance;

switch(param_index)
	{
	case 0:
                *((double*)param)=map_value_backward(p->shp, 0.0, 3.9999);
		break;
	case 1:
                *((double*)param)=p->pozx;
		break;
	case 2:
                *((double*)param)=p->pozy;
		break;
	case 3:
                *((double*)param)=p->sizx;
		break;
	case 4:
                *((double*)param)=p->sizy;
		break;
	case 5:
                *((double*)param)=map_value_backward(p->tilt, -3.15, 3.15);
		break;
	case 6:
                *((double*)param)=p->wdt;
		break;
	case 7:
                *((double*)param)=p->min;
		break;
	case 8:
                *((double*)param)=p->max;
		break;
	case 9:
                *((double*)param)=map_value_backward(p->op, 0.0, 4.9999);
		break;
	}
}

//-------------------------------------------------
void f0r_update(f0r_instance_t instance, double time, const uint32_t* inframe, uint32_t* outframe)
{
inst *in;
int i,j;
float a,d,st,ct;
uint32_t t;

assert(instance);
in=(inst*)instance;

switch (in->op)
	{
	case 0:		//write on clear
		for (i=0;i<in->h*in->w;i++)
			outframe[i] = (inframe[i]&0x00FFFFFF) | in->gr8[i];
		break;
	case 1:		//max
		for (i=0;i<in->h*in->w;i++)
			{
			t=((inframe[i]&0xFF000000)>in->gr8[i]) ? inframe[i]&0xFF000000 : in->gr8[i];
			outframe[i] = (inframe[i]&0x00FFFFFF) | t;
			}
		break;
	case 2:		//min
		for (i=0;i<in->h*in->w;i++)
			{
			t=((inframe[i]&0xFF000000)<in->gr8[i]) ? inframe[i]&0xFF000000 : in->gr8[i];
			outframe[i] = (inframe[i]&0x00FFFFFF) | t;
			}
		break;
	case 3:		//add
		for (i=0;i<in->h*in->w;i++)
			{
			t=((inframe[i]&0xFF000000)>>1)+(in->gr8[i]>>1);
			t = (t>0x7F800000) ? 0xFF000000 : t<<1;
			outframe[i] = (inframe[i]&0x00FFFFFF) | t;
			}
		break;
	case 4:		//subtract
		for (i=0;i<in->h*in->w;i++)
			{
			t= ((inframe[i]&0xFF000000)>in->gr8[i]) ? (inframe[i]&0xFF000000)-in->gr8[i] : 0;
			outframe[i] = (inframe[i]&0x00FFFFFF) | t;
			}
		break;
	default:
		break;
	}
}

//**********************************************************