Debian Codegraph @

src/filter/c0rners/c0rners.c

raw symbols SCIP
1 /*c0rners.c
2 four corners geometry engine
3
4
5 * Copyright (C) 2010 Marko Cebokli http://lea.hamradio.si/~s57uuu
6 * This file is a Frei0r plugin.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 //compile: gcc -Wall -std=c99 -c -fPIC c0rners.c -o c0rners.o
24 //link: gcc -shared -lm -o c0rners.so c0rners.o
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <frei0r.h>
29 #include <string.h>
30 #include <math.h>
31 #include "frei0r/math.h"
32 #include "interp.h"
33
34 //----------------------------------------
35 //structure for Frei0r instance
36 typedef struct
37 {
38 int h;
39 int w;
40 float x1;
41 float y1;
42 float x2;
43 float y2;
44 float x3;
45 float y3;
46 float x4;
47 float y4;
48 int stretchON;
49 float stretchx;
50 float stretchy;
51 int intp;
52 int transb;
53 float feath;
54 int op;
55
56 interpp interp;
57 float *map;
58 unsigned char *amap;
59 int mapIsDirty;
60 } inst;
61
62
63 //2D point
64 typedef struct //tocka v ravnini
65 {
66 float x;
67 float y;
68 } tocka2;
69
70 //2D line
71 typedef struct //premica v ravnini
72 {
73 float a;
74 float b;
75 float c;
76 float sa; //se normalna oblika
77 float ca;
78 float p;
79 } premica2;
80
81 // line through two points in the plane (2D)
82 // calculate a, b, c for the equation of the line: ax + by + c = 0
83 // ps se sin a, cos a and p for normal form
84 // return: (return value)
85 // - 10 if it is not possible to determine p. coincident points
86 // 0 general p. general case
87 // + 1 vertical p. x v * a vertical
88 // + 2 horizontal p. y v * b horizontal
89 // a line through two points
90 int premica2d(tocka2 t1, tocka2 t2, premica2 *p)
91 {
92 float dx,dy,m;
93
94 dx=t2.x-t1.x;
95 dy=t2.y-t1.y;
96 if (dx==0.0) //navpicna
97 {
98 if (dy==0.0) return -10;
99 p->a=1.0;
100 p->b=0.0;
101 p->c=-t1.x;
102 m=1.0/p->a; if (p->c>=0) m=-m;
103 p->sa=m;
104 p->ca=0.0;
105 p->p=m*p->c;
106 return 1;
107 }
108
109 if (dy==0.0) //vodoravna
110 {
111 if (dx==0.0) return -10;
112 p->a=0.0;
113 p->b=1.0;
114 p->c=-t1.y;
115 m=1.0/p->b; if (p->c>=0) m=-m;
116 p->sa=0.0;
117 p->ca=m;
118 p->p=m*p->c;
119 return 2;
120 }
121
122 //posevna
123 p->a=1.0/dx;
124 p->b=-1.0/dy;
125 p->c=t1.y/dy-t1.x/dx;
126 m=1.0/sqrtf(p->a*p->a+p->b*p->b); if (p->c>=0) m=-m;
127 p->sa=m*p->a;
128 p->ca=m*p->b;
129 p->p=m*p->c;
130 return 0;
131 }
132
133 // ------------------------------------------------ -----
134 // point distance from line (for alpha feather)
135 // distance between a point and a line
136 // needed only for alpha feathering
137 float razd_t_p(tocka2 t, premica2 p)
138 {
139 float r;
140
141 r = t.x*p.ca + t.y*p.sa + p.p;
142
143 return r;
144 }
145
146 // ------------------------------------------------ -----
147 // intersection of two lines in a plane (2D)
148 // return:
149 // 0 if everything is OK
150 // - 1 if the lines are parallel
151 // intersection of two lines
152 int presecisce2(premica2 p1, premica2 p2, tocka2 *t)
153 {
154 float d1,d2,d3;
155
156 d1=p1.a*p2.b-p2.a*p1.b;
157 if (d1==0.0) //vzporedni
158 {
159 return -1;
160 }
161 d2=p1.b*p2.c-p2.b*p1.c;
162 d3=p1.c*p2.a-p2.c*p1.a;
163 t->x=d2/d1;
164 t->y=d3/d1;
165 return 0;
166 }
167
168 // ------------------------------------------------ ---------------
169 // generate mapping for a general quadrangle
170 // wi, hi = input image size
171 // wo, ho = output image size
172 // vog [] = the four corners
173 // str: 0 = no stretch 1 = do stretch
174 // strx, stry: stretch values ​​[0 ... 1] 0.5 = no stretch
175 void cetverokotnik4(int wi, int hi, int wo, int ho, tocka2 vog[], int str, float strx, float stry, float *map)
176 {
177 double a,b,c,d,e,f,g,h,a2,b2,c2,u,v,aa,bb,de,sde,v1,v2,u1,u2;
178 tocka2 T;
179 int x,y;
180 float kx,ky,k1,k2;
181
182 de=0.0;v1=1000.0;v2=1000.0; //da compiler ne jamra
183
184 kx=4.0*2.0*fabsl(strx-0.5)+0.00005;
185 k1=1.0-1.0/(kx+1.0);
186 ky=4.0*2.0*fabsl(stry-0.5)+0.00005;
187 k2=1.0-1.0/(ky+1.0);
188
189 for (y=0;y<ho;y++)
190 {
191 for (x=0;x<wo;x++)
192 {
193 T.x=(float)x+0.5;T.y=(float)y+0.5;
194
195 //enacba za Xt, prva moznost
196 a=vog[0].x-T.x; b=vog[1].x-vog[0].x;
197 c=vog[3].x-vog[0].x;
198 d=vog[2].x-vog[1].x-(vog[3].x-vog[0].x);
199 //enacba za Xt, druga moznost
200 // a=vog[0].x-T.x; b=vog[1].x-vog[0].x;
201 // c=vog[2].x-vog[0].x;
202 // d=vog[3].x-vog[2].x-(vog[1].x-vog[0].x);
203
204 //enacba za Yt, prva moznost
205 e=vog[0].y-T.y; f=vog[1].y-vog[0].y;
206 g=vog[3].y-vog[0].y;
207 h=vog[2].y-vog[1].y-(vog[3].y-vog[0].y);
208 //enacba za Yt, druga moznost
209 // e=vog[0].y-T.y; f=vog[1].y-vog[0].y;
210 // g=vog[2].y-vog[0].y;
211 // h=vog[3].y-vog[2].y-(vog[1].y-vog[0].y);
212
213 //resitev za v in u
214 a2=g*d-h*c; b2=e*d-f*c-h*a+g*b; c2=e*b-f*a;
215 //linearni priblizek uporabim, ce je napaka < 1/10 piksla
216 // if (fabsl(a2*c2*c2/(b2*b2*b2))< 0.1/wi)
217 //dodaten pogoj za a2, da ni spranje v konkavnih
218 if ((fabsl(a2*c2*c2/(b2*b2*b2))< 0.1/wi) && (fabsl(a2)<1.0))
219 {
220 v1 = (b2!=0.0) ? -c2/b2 : 1000.0;
221 v2=1000.0;
222 }
223 else
224 {
225 de=b2*b2-4.0*a2*c2;
226 if (de>=0.0)
227 {
228 sde=sqrt(de);
229 v1=(-b2+sde)/2.0/a2;
230 v2=(-b2-sde)/2.0/a2;
231 }
232 else
233 {
234 v1=1001.0; //krneki zunaj
235 v2=1001.0; //krneki zunaj
236 }
237 }
238 aa=b+d*v1; bb=f+h*v1;
239 if (fabs(aa)>fabs(bb))
240 u1 = (aa!=0.0) ? -(a+c*v1)/aa : 1000.0;
241 else
242 u1 = (bb!=0.0) ? -(e+g*v1)/bb : 1000.0;
243 aa=b+d*v2; bb=f+h*v2;
244 if (fabs(aa)>fabs(bb))
245 u2 = (aa!=0.0) ? -(a+c*v2)/aa : 1000.0;
246 else
247 u2 = (bb!=0.0) ? -(e+g*v2)/bb : 1000.0;
248
249 if ((u1>0.0)&&(u1<1.0)&&(v1>0.0)&&(v1<1.0))
250 {
251 u=u1;
252 v=v1;
253 }
254 else
255 {
256 if ((u2>0.0)&&(u2<1.0)&&(v2>0.0)&&(v2<1.0))
257 {
258 u=u2;
259 v=v2;
260 }
261 else
262 {
263 u=1002.0;
264 v=1002.0;
265 }
266 }
267
268 //if requested, apply stretching
269 if (str!=0)
270 {
271 if (strx>0.5)
272 u=(1.0-1.0/(kx*u+1.0))/k1;
273 else
274 u=1.0-(1.0-1.0/(kx*(1.0-u)+1.0))/k1;
275 if (stry>0.5)
276 v=(1.0-1.0/(ky*v+1.0))/k2;
277 else
278 v=1.0-(1.0-1.0/(ky*(1.0-v)+1.0))/k2;
279 }
280
281 //zdaj samo se vpise izracunana (u,v) v map[]
282 if ((u>=0.0)&&(u<=1.0)&&(v>=0.0)&&(v<=1.0))
283 { //ce smo znotraj orig slike
284 map[2*(y*wo+x)]=u*(wi-1);
285 map[2*(y*wo+x)+1]=v*(hi-1);
286 }
287 else
288 {
289 map[2*(y*wo+x)]=-1;
290 map[2*(y*wo+x)+1]=-1;
291 }
292
293 }
294 }
295
296 }
297
298 //---------------------------------------------------------------
299 //generate mapping for a triangle
300 void trikotnik1(int wi, int hi, int wo, int ho, tocka2 vog[], tocka2 R, tocka2 S, premica2 p12, premica2 p23, premica2 p34, premica2 p41, int t12, int t23, int str, float strx, float stry, float *map)
301 {
302 int x,y;
303 tocka2 T,A,B;
304 premica2 p5,p6;
305 float u,v;
306 float kx,ky,k1,k2;
307
308 kx=4.0*2.0*fabsl(strx-0.5)+0.00005;
309 k1=1.0-1.0/(kx+1.0);
310 ky=4.0*2.0*fabsl(stry-0.5)+0.00005;
311 k2=1.0-1.0/(ky+1.0);
312
313 for (y=0;y<ho;y++)
314 {
315 for (x=0;x<wo;x++)
316 {
317 T.x=(float)x+0.5;T.y=(float)y+0.5;
318 premica2d(T,R,&p5);
319 presecisce2(p5,p12,&A);
320 if (t12!=-10) //razlicno od cetverokotnika
321 { //tocka je med preseciscem in r
322 if (fabsf(p12.a)>fabsf(p12.b)) //bolj pokonci
323 u=(A.y-vog[0].y)/(vog[1].y-vog[0].y);
324 else
325 u=(A.x-vog[0].x)/(vog[1].x-vog[0].x);
326 }
327 else
328 {
329 presecisce2(p5,p34,&A);
330 if (fabsf(p34.a)>fabsf(p34.b)) //bolj pokonci
331 u=(A.y-vog[3].y)/(vog[2].y-vog[3].y);
332 else
333 u=(A.x-vog[3].x)/(vog[2].x-vog[3].x);
334 }
335
336 premica2d(T,S,&p6);
337 presecisce2(p6,p23,&B);
338 if (t23!=-10) //razlicno od cetverokotnika
339 {
340 if (fabsf(p23.a)>fabsf(p23.b)) //bolj pokonci
341 v=(B.y-vog[1].y)/(vog[2].y-vog[1].y);
342 else
343 v=(B.x-vog[1].x)/(vog[2].x-vog[1].x);
344 }
345 else
346 {
347 presecisce2(p6,p41,&B);
348 if (fabsf(p41.a)>fabsf(p41.b)) //bolj pokonci
349 v=(B.y-vog[0].y)/(vog[3].y-vog[0].y);
350 else
351 v=(B.x-vog[0].x)/(vog[3].x-vog[0].x);
352 }
353
354 //if requested, apply stretching
355 if (str!=0)
356 {
357 if (strx>0.5)
358 u=(1.0-1.0/(kx*u+1.0))/k1;
359 else
360 u=1.0-(1.0-1.0/(kx*(1.0-u)+1.0))/k1;
361 if (stry>0.5)
362 v=(1.0-1.0/(ky*v+1.0))/k2;
363 else
364 v=1.0-(1.0-1.0/(ky*(1.0-v)+1.0))/k2;
365 }
366
367 //zdaj samo se vpise izracunana (u,v) v map[]
368 if ((u>=0.0)&&(u<=1.0)&&(v>=0.0)&&(v<=1.0))
369 { //ce smo znotraj orig slike
370 map[2*(y*wo+x)]=u*(wi-1);
371 map[2*(y*wo+x)+1]=v*(hi-1);
372 }
373 else
374 {
375 map[2*(y*wo+x)]=-1;
376 map[2*(y*wo+x)+1]=-1;
377 }
378 }
379 }
380 }
381
382 //-------------------------------------------------------
383 //generates a map of alpha values for transparent background
384 //with feathered (soft) border
385 //feath = soft border width 0..max in pixels
386 //amap = generated alpha map
387 //map = map generated by geom4c_b()
388 //nots[] = flags for inner sides
389 //for now it does not feather caustics on concaves an crossed sides
390 void make_alphamap(unsigned char *amap, tocka2 vog[], int wo, int ho, float *map, float feath, int nots[])
391 {
392 float r12, r23, r34, r41, rmin;
393 tocka2 t;
394 int i,j;
395 premica2 p12,p23,p34,p41;
396
397 premica2d(vog[0],vog[1],&p12); // 1-2
398 premica2d(vog[2],vog[3],&p34); // 3-4
399 premica2d(vog[3],vog[0],&p41); // 4-1
400 premica2d(vog[1],vog[2],&p23); // 2-3
401
402 for (i=0;i<ho;i++)
403 for (j=0;j<wo;j++)
404 {
405 t.x=(float)i+0.5; t.y=(float)j+0.5;
406 r12=fabsf(razd_t_p(t,p12));
407 r23=fabsf(razd_t_p(t,p23));
408 r34=fabsf(razd_t_p(t,p34));
409 r41=fabsf(razd_t_p(t,p41));
410 rmin=1.0E22;
411 if ((r12<rmin) && (nots[0]!=1)) rmin=r12;
412 if ((r23<rmin) && (nots[1]!=1)) rmin=r23;
413 if ((r34<rmin) && (nots[2]!=1)) rmin=r34;
414 if ((r41<rmin) && (nots[3]!=1)) rmin=r41;
415 if ((map[2*(i*wo+j)]>=0.0)&&(map[2*(i*wo+j)+1]>=0.0))
416 { //inside
417 if (rmin<=feath) //border area
418 amap[i*wo+j]=255*(rmin/feath);
419 else
420 amap[i*wo+j]=255;
421 }
422 else //outside
423 amap[i*wo+j]=0;
424 }
425 }
426
427 //-------------------------------------------------------
428 void apply_alphamap(uint32_t* frame, int w, int h, unsigned char *amap, int operation)
429 {
430 int i, length;
431 uint32_t t;
432 length = w * h;
433
434 switch (operation)
435 {
436 case 0: //write on clear
437 for (i=0;i<length;i++)
438 {
439 t=((uint32_t)amap[i])<<24;
440 frame[i] = (frame[i]&0x00FFFFFF) | t;
441 }
442 break;
443 case 1: //max
444 for (i=0;i<length;i++)
445 {
446 t=((uint32_t)amap[i])<<24;
447 frame[i] = (frame[i]&0x00FFFFFF) | MAX(frame[i]&0xFF000000, t);
448 }
449 break;
450 case 2: //min
451 for (i=0;i<length;i++)
452 {
453 t=((uint32_t)amap[i])<<24;
454 frame[i] = (frame[i]&0x00FFFFFF) | MIN(frame[i]&0xFF000000, t);
455 }
456 break;
457 case 3: //add
458 for (i=0;i<length;i++)
459 {
460 t=((uint32_t)amap[i])<<24;
461 t=((frame[i]&0xFF000000)>>1)+(t>>1);
462 t = (t>0x7F800000) ? 0xFF000000 : t<<1;
463 frame[i] = (frame[i]&0x00FFFFFF) | t;
464 }
465 break;
466 case 4: //subtract
467 for (i=0;i<length;i++)
468 {
469 t=((uint32_t)amap[i])<<24;
470 t= ((frame[i]&0xFF000000)>t) ? (frame[i]&0xFF000000)-t : 0;
471 frame[i] = (frame[i]&0x00FFFFFF) | t;
472 }
473 break;
474 default:
475 break;
476 }
477 }
478
479 // ------------------------------------------------ ---------------
480 // function for byte fields (char)
481 // generate map from the four corners
482 // first checks for different types of degenerate geometrty ...
483 // wi, hi input image size
484 // wo, ho output image size
485 // vog [] the four corners
486 // nots [] "inner" sides (for alpha feathering)
487 int geom4c_b(int wi, int hi, int wo, int ho, tocka2 vog[], int str, float strx, float stry, float *map, int nots[])
488 {
489 premica2 p12,p23,p34,p41;
490 tocka2 R,S;
491 int r41,r23,s12,s34; //tocki R in S
492 int p1,p2; //paralelnost stranic
493 int t12,t23,t34,t41; //sovpadanje tock
494 int tip; //1=degen trik 2=paral 3=splosni 4=twist 5=konkavni
495 int i;
496
497 for (i=0;i<4;i++) //convert indexes to positions (pixel)
498 {
499 vog[i].x=vog[i].x+0.5;
500 vog[i].y=vog[i].y+0.5;
501 }
502
503 tip=3;
504
505 t12=premica2d(vog[0],vog[1],&p12); // 1-2
506 t34=premica2d(vog[2],vog[3],&p34); // 3-4
507 t41=premica2d(vog[3],vog[0],&p41); // 4-1
508 t23=premica2d(vog[1],vog[2],&p23); // 2-3
509
510 //preveri degeneracijo v crto ali piko
511 //check for degeneration into a line or point
512 if ((t12+t34+t41+t23)<-19)
513 { //vec kot dve sovpadata
514 //daj tu fill with background??
515 return 0;
516 }
517 if (((vog[0].x==vog[2].x)&&(vog[0].y==vog[2].y)) || ((vog[1].x==vog[3].x)&&(vog[1].y==vog[3].y)))
518 { //sovpadata dve diagonalni tocki
519 //daj tu fill with background??
520 return 0;
521 }
522
523 p1=presecisce2(p12,p34,&S); //tocka S
524 p2=presecisce2(p41,p23,&R); //tocka R
525
526 //preveri degeneracijo v trikotnik (sovpadanje nediagonalnih tock)
527 //check for degeneration into triangle (coincident non-diagonal c.)
528 if (t12==-10) {R=vog[0];p12=p34;p1=-1;tip=1;}
529 if (t34==-10) {R=vog[2];p34=p12;p1=-1;tip=1;}
530 if (t41==-10) {S=vog[0];p41=p23;p2=-1;tip=1;}
531 if (t23==-10) {S=vog[2];p23=p41;p2=-1;tip=1;}
532
533 //preveri vzporednost
534 //check parallelity
535 if (p1==-1) //vzporedni 1-2 in 3-4
536 {
537 if (fabsf(p12.a)>fabsf(p12.b)) //bolj pokonci
538 {S.y=1.0E9;S.x=-(p12.b*S.y+p12.c)/p12.a;}
539 else
540 {S.x=1.0E9;S.y=-(p12.a*S.x+p12.c)/p12.b;}
541 }
542 if (p2==-1) //vzporedni 2-3 in 4-1
543 {
544 if (fabsf(p41.a)>fabsf(p41.b)) //bolj pokonci
545 {R.y=1.0E9;R.x=-(p41.b*R.y+p41.c)/p41.a;}
546 else
547 {R.x=1.0E9;R.y=-(p41.a*R.x+p41.c)/p41.b;}
548 }
549
550 //pogleda, ce je priblizno paralelogram
551 //check if approximately parallelogram
552 if (((fabsf(R.x)>1000000.0)||(fabsf(R.y)>1000000.0)) &&
553 ((fabsf(S.x)>1000000.0)||(fabsf(S.y)>1000000.0)))
554 tip=2;
555
556 //preveri, ce je prekrizan ali konkaven (R ali S med ogljici)
557 //check for concave or crossed sides
558 r41=0;r23=0;s12=0;s34=0;
559
560 if (fabsf(p41.a)>fabsf(p41.b)) //bolj pokonci
561 {if (((R.y-vog[3].y)*(R.y-vog[0].y))<0.0) r41=1;}//R na 4-1
562 else
563 {if (((R.x-vog[3].x)*(R.x-vog[0].x))<0.0) r41=1;}//R na 4-1
564
565 if (fabsf(p23.a)>fabsf(p23.b)) //bolj pokonci
566 {if (((R.y-vog[1].y)*(R.y-vog[2].y))<0.0) r23=1;}//R na 2-3
567 else
568 {if (((R.x-vog[1].x)*(R.x-vog[2].x))<0.0) r23=1;}//R na 2-3
569
570 if (fabsf(p12.a)>fabsf(p12.b)) //bolj pokonci
571 {if (((S.y-vog[0].y)*(S.y-vog[1].y))<0.0) s12=1;}//S na 1-2
572 else
573 {if (((S.x-vog[0].x)*(S.x-vog[1].x))<0.0) s12=1;}//S na 1-2
574
575 if (fabsf(p34.a)>fabsf(p34.b)) //bolj pokonci
576 {if (((S.y-vog[2].y)*(S.y-vog[3].y))<0.0) s34=1;}//S na 3-4
577 else
578 {if (((S.x-vog[2].x)*(S.x-vog[3].x))<0.0) s34=1;}//S na 3-4
579
580 if (((r41+r23+s12+s34)>0)&&(tip==3))
581 {
582 if ((r41*r23+s12*s34)==0) //konkaven
583 tip=5;
584 else //prekrizan
585 tip=4;
586 }
587
588 //prepare nots[] flags
589 nots[0]=nots[1]=nots[2]=nots[3]=0;
590 if (tip==4)
591 {
592 nots[0] = (s12==0) ? 0 : 1;
593 nots[1] = (r23==0) ? 0 : 1;
594 nots[2] = (s34==0) ? 0 : 1;
595 nots[3] = (r41==0) ? 0 : 1;
596 }
597 if (tip==5)
598 {
599 nots[2] = (s12==0) ? 0 : 1;
600 nots[3] = (r23==0) ? 0 : 1;
601 nots[0] = (s34==0) ? 0 : 1;
602 nots[1] = (r41==0) ? 0 : 1;
603 }
604
605 //OK, zdaj gremo risat...
606 switch (tip)
607 {
608 case 0: //should never come to here...
609 break;
610 case 1: //triangle
611 trikotnik1(wi, hi, wo, ho, vog, R, S, p12, p23, p34, p41, t12, t23, str, strx, stry, map);
612 break;
613 case 2: //paralelogram
614 //a faster algorithm could be used here...
615 case 3: //general quadrangle
616 case 4: //crossed sides
617 case 5: //concave quadrangle
618 cetverokotnik4(wi, hi, wo, ho, vog, str, strx, stry, map);
619 break;
620 }
621
622 return 0;
623 }
624
625 //-------------------------------------------------------
626 interpp set_intp(inst p)
627 {
628 switch (p.intp) //katero interpolacijo bo uporabil
629 {
630 // case -1:return interpNNpr_b; //nearest neighbor+print
631 case 0: return interpNN_b32; //nearest neighbor
632 case 1: return interpBL_b32; //bilinear
633 case 2: return interpBC_b32; //bicubic smooth
634 case 3: return interpBC2_b32; //bicibic sharp
635 case 4: return interpSP4_b32; //spline 4x4
636 case 5: return interpSP6_b32; //spline 6x6
637 case 6: return interpSC16_b32; //lanczos 8x8
638 default: return NULL;
639 }
640 }
641
642 //-----------------------------------------------------
643 //stretch [0...1] to parameter range [min...max] linear
644 float map_value_forward(double v, float min, float max)
645 {
646 return min+(max-min)*v;
647 }
648
649 //-----------------------------------------------------
650 //collapse from parameter range [min...max] to [0...1] linear
651 double map_value_backward(float v, float min, float max)
652 {
653 return (v-min)/(max-min);
654 }
655
656
657 //***********************************************
658 // OBVEZNE FREI0R FUNKCIJE
659
660 //-----------------------------------------------
661 int f0r_init()
662 {
663 return 1;
664 }
665
666 //------------------------------------------------
667 void f0r_deinit()
668 {
669 }
670
671 //-----------------------------------------------
672 void f0r_get_plugin_info(f0r_plugin_info_t* info)
673 {
674
675 info->name="c0rners";
676 info->author="Marko Cebokli";
677 info->plugin_type=F0R_PLUGIN_TYPE_FILTER;
678 info->color_model=F0R_COLOR_MODEL_RGBA8888;
679 info->frei0r_version=FREI0R_MAJOR_VERSION;
680 info->major_version=0;
681 info->minor_version=2;
682 info->num_params=15;
683 info->explanation="Four corners geometry engine";
684 }
685
686 //--------------------------------------------------
687 void f0r_get_param_info(f0r_param_info_t* info, int param_index)
688 {
689
690 switch(param_index)
691 {
692 case 0:
693 info->name = "Corner 1 X";
694 info->type = F0R_PARAM_DOUBLE;
695 info->explanation = "X coordinate of corner 1";
696 break;
697 case 1:
698 info->name = "Corner 1 Y";
699 info->type = F0R_PARAM_DOUBLE;
700 info->explanation = "Y coordinate of corner 1";
701 break;
702 case 2:
703 info->name = "Corner 2 X";
704 info->type = F0R_PARAM_DOUBLE;
705 info->explanation = "X coordinate of corner 2";
706 break;
707 case 3:
708 info->name = "Corner 2 Y";
709 info->type = F0R_PARAM_DOUBLE;
710 info->explanation = "Y coordinate of corner 2";
711 break;
712 case 4:
713 info->name = "Corner 3 X";
714 info->type = F0R_PARAM_DOUBLE;
715 info->explanation = "X coordinate of corner 3";
716 break;
717 case 5:
718 info->name = "Corner 3 Y";
719 info->type = F0R_PARAM_DOUBLE;
720 info->explanation = "Y coordinate of corner 3";
721 break;
722 case 6:
723 info->name = "Corner 4 X";
724 info->type = F0R_PARAM_DOUBLE;
725 info->explanation = "X coordinate of corner 4";
726 break;
727 case 7:
728 info->name = "Corner 4 Y";
729 info->type = F0R_PARAM_DOUBLE;
730 info->explanation = "Y coordinate of corner 4";
731 break;
732 case 8:
733 info->name = "Enable Stretch";
734 info->type = F0R_PARAM_BOOL;
735 info->explanation = "Enable stretching";
736 break;
737 case 9:
738 info->name = "Stretch X";
739 info->type = F0R_PARAM_DOUBLE;
740 info->explanation = "Amount of stretching in X direction";
741 break;
742 case 10:
743 info->name = "Stretch Y";
744 info->type = F0R_PARAM_DOUBLE;
745 info->explanation = "Amount of stretching in Y direction";
746 break;
747 case 11:
748 info->name = "Interpolator";
749 info->type = F0R_PARAM_DOUBLE;
750 info->explanation = "Quality of interpolation";
751 break;
752 case 12:
753 info->name = "Transparent Background";
754 info->type = F0R_PARAM_BOOL;
755 info->explanation = "Makes background transparent";
756 break;
757 case 13:
758 info->name = "Feather Alpha";
759 info->type = F0R_PARAM_DOUBLE;
760 info->explanation = "Makes smooth transition into transparent";
761 break;
762 case 14:
763 info->name = "Alpha operation";
764 info->type = F0R_PARAM_DOUBLE;
765 info->explanation = "";
766 }
767 }
768
769 //----------------------------------------------
770 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
771 {
772 inst *in;
773
774 in=(inst*)calloc(1, sizeof(inst));
775 in->w=width;
776 in->h=height;
777 in->x1=0.333333;
778 in->y1=0.333333;
779 in->x2=0.666666;
780 in->y2=0.333333;
781 in->x3=0.666666;
782 in->y3=0.666666;
783 in->x4=0.333333;
784 in->y4=0.666666;
785 in->stretchON=0;
786 in->stretchx=0.5;
787 in->stretchy=0.5;
788 in->intp=1;
789 in->transb=0;
790 in->feath=1.0;
791 in->op=0;
792
793 in->map=(float*)calloc(1, sizeof(float)*(in->w*in->h*2+2));
794 in->amap=(unsigned char*)calloc(1, sizeof(char)*(in->w*in->h*2+2));
795 in->interp=set_intp(*in);
796 in->mapIsDirty=1;
797
798 return (f0r_instance_t)in;
799 }
800
801 //---------------------------------------------------
802 void f0r_destruct(f0r_instance_t instance)
803 {
804 inst *p;
805
806 p=(inst*)instance;
807
808 free(p->map);
809 free(p->amap);
810 free(instance);
811 }
812
813 //-----------------------------------------------------
814 void f0r_set_param_value(f0r_instance_t instance, f0r_param_t parm, int param_index)
815 {
816 inst *p;
817 double tmpf;
818 int chg;
819
820 p=(inst*)instance;
821
822 chg=0;
823 switch(param_index)
824 {
825 case 0: //X coordinate of corner 1
826 tmpf=*(double*)parm;
827 if (tmpf!=p->x1) chg=1;
828 p->x1=tmpf;
829 break;
830 case 1: //Y coordinate of corner 1
831 tmpf=*(double*)parm;
832 if (tmpf!=p->y1) chg=1;
833 p->y1=tmpf;
834 break;
835 case 2: //X coordinate of corner 2
836 tmpf=*(double*)parm;
837 if (tmpf!=p->x2) chg=1;
838 p->x2=tmpf;
839 break;
840 case 3: //Y coordinate of corner 2
841 tmpf=*(double*)parm;
842 if (tmpf!=p->y2) chg=1;
843 p->y2=tmpf;
844 break;
845 case 4: //X coordinate of corner 3
846 tmpf=*(double*)parm;
847 if (tmpf!=p->x3) chg=1;
848 p->x3=tmpf;
849 break;
850 case 5: //Y coordinate of corner 3
851 tmpf=*(double*)parm;
852 if (tmpf!=p->y3) chg=1;
853 p->y3=tmpf;
854 break;
855 case 6: //X coordinate of corner 4
856 tmpf=*(double*)parm;
857 if (tmpf!=p->x4) chg=1;
858 p->x4=tmpf;
859 break;
860 case 7: //Y coordinate of corner 4
861 tmpf=*(double*)parm;
862 if (tmpf!=p->y4) chg=1;
863 p->y4=tmpf;
864 break;
865 case 8: //Enable stretching
866 tmpf=map_value_forward(*((double*)parm), 0.0, 1.0);//BOOL!!
867 if (p->stretchON != tmpf) chg=1;
868 p->stretchON = tmpf;
869 break;
870 case 9: //Stretch X
871 tmpf=*(double*)parm;
872 if (tmpf!=p->stretchx) chg=1;
873 p->stretchx=tmpf;
874 break;
875 case 10: //Stretch Y
876 tmpf=*(double*)parm;
877 if (tmpf!=p->stretchy) chg=1;
878 p->stretchy=tmpf;
879 break;
880 case 11: //Interpolation
881 tmpf=map_value_forward(*((double*)parm), 0.0, 6.999);
882 if (p->intp != tmpf) chg=1;
883 p->intp=tmpf;
884 break;
885 case 12: //Transparent Background
886 tmpf=map_value_forward(*((double*)parm), 0.0, 1.0);//BOOL!!
887 // if (p->transb != tmpf) chg=1;
888 p->transb = tmpf;
889 break;
890 case 13: //Feather Alpha
891 tmpf=map_value_forward(*((double*)parm), 0.0, 100.0);
892 if (tmpf!=p->feath) chg=1;
893 p->feath=tmpf;
894 break;
895 case 14: //Alpha operation
896 p->op=map_value_forward(*((double*)parm), 0.0, 4.9999);
897 break;
898 }
899
900 if (chg!=0)
901 {
902 p->interp=set_intp(*p);
903 p->mapIsDirty = 1;
904 }
905
906 }
907
908 //--------------------------------------------------
909 void f0r_get_param_value(f0r_instance_t instance, f0r_param_t param, int param_index)
910 {
911 inst *p;
912 double tmpf;
913
914 p=(inst*)instance;
915
916 switch(param_index)
917 {
918 case 0: //X coordinate of corner 1
919 tmpf=(float)p->x1;
920 *((double*)param)=tmpf;
921 break;
922 case 1: //Y coordinate of corner 1
923 tmpf=(float)p->y1;
924 *((double*)param)=tmpf;
925 break;
926 case 2: //X coordinate of corner 2
927 tmpf=(float)p->x2;
928 *((double*)param)=tmpf;
929 break;
930 case 3: //Y coordinate of corner 2
931 tmpf=(float)p->y2;
932 *((double*)param)=tmpf;
933 break;
934 case 4: //X coordinate of corner 3
935 tmpf=(float)p->x3;
936 *((double*)param)=tmpf;
937 break;
938 case 5: //Y coordinate of corner 3
939 tmpf=(float)p->y3;
940 *((double*)param)=tmpf;
941 break;
942 case 6: //X coordinate of corner 4
943 tmpf=(float)p->x4;
944 *((double*)param)=tmpf;
945 break;
946 case 7: //Y coordinate of corner 4
947 tmpf=(float)p->y4;
948 *((double*)param)=tmpf;
949 break;
950 case 8: //Enable stretching
951 *((double*)param)=map_value_backward(p->stretchON, 0.0, 1.0); //BOOL!!
952 break;
953 case 9: //Stretch X
954 tmpf=(float)p->stretchx;
955 *((double*)param)=tmpf;
956 break;
957 case 10: //Stretch Y
958 tmpf=(float)p->stretchy;
959 *((double*)param)=tmpf;
960 break;
961 case 11: //Interpolation
962 *((double*)param)=map_value_backward(p->intp, 0.0, 6.0); //!!!!!! 6.999 ???? tudi v defish!!!!
963 break;
964 case 12: //Transparent Background
965 *((double*)param)=map_value_backward(p->transb, 0.0, 1.0); //BOOL!!
966 break;
967 case 13: //Feather Alpha
968 *((double*)param)=map_value_backward(p->feath, 0.0, 100.0);
969 break;
970 case 14: //Alpha operation
971 *((double*)param)=map_value_backward(p->op, 0.0, 4.9999);
972 break;
973 }
974 }
975
976 #define EPSILON 1e-5f
977 #define EQUIVALENT_FLOATS(x, y) (fabsf((x) - (y)) < EPSILON)
978
979 //-------------------------------------------------
980 void f0r_update(f0r_instance_t instance, double time, const uint32_t* inframe, uint32_t* outframe)
981 {
982 inst *p;
983 int bkgr;
984
985 p=(inst*)instance;
986
987 if (EQUIVALENT_FLOATS(p->x1, 0.333333f) &&
988 EQUIVALENT_FLOATS(p->y1, 0.333333f) &&
989 EQUIVALENT_FLOATS(p->x2, 0.666666f) &&
990 EQUIVALENT_FLOATS(p->y2, 0.333333f) &&
991 EQUIVALENT_FLOATS(p->x3, 0.666666f) &&
992 EQUIVALENT_FLOATS(p->y3, 0.666666f) &&
993 EQUIVALENT_FLOATS(p->x4, 0.333333f) &&
994 EQUIVALENT_FLOATS(p->y4, 0.666666f) &&
995 (!p->stretchON || (
996 EQUIVALENT_FLOATS(p->stretchx, 0.5f) &&
997 EQUIVALENT_FLOATS(p->stretchy, 0.5f))))
998 {
999 memcpy(outframe, inframe, p->w * p->h * 4);
1000 return;
1001 }
1002
1003 if (p->mapIsDirty) {
1004 tocka2 vog[4];
1005 int nots[4];
1006 vog[0].x=(p->x1*3-1)*p->w;
1007 vog[0].y=(p->y1*3-1)*p->h;
1008 vog[1].x=(p->x2*3-1)*p->w;
1009 vog[1].y=(p->y2*3-1)*p->h;
1010 vog[2].x=(p->x3*3-1)*p->w;
1011 vog[2].y=(p->y3*3-1)*p->h;
1012 vog[3].x=(p->x4*3-1)*p->w;
1013 vog[3].y=(p->y4*3-1)*p->h;
1014 geom4c_b(p->w, p->h, p->w, p->h, vog, p->stretchON, p->stretchx, p->stretchy, p->map, nots);
1015 make_alphamap(p->amap, vog, p->w, p->h, p->map, p->feath, nots);
1016 p->mapIsDirty = 0;
1017 }
1018
1019 //if (p->transb==0) bkgr=0xFF000000; else bkgr=0;
1020 bkgr=0xFF000000;
1021
1022 remap32(p->w, p->h, p->w, p->h, (unsigned char*) inframe, (unsigned char *) outframe, p->map, bkgr, p->interp);
1023
1024 if (p->transb!=0)
1025 apply_alphamap(outframe, p->w, p->h, p->amap, p->op);
1026
1027 }