Debian Codegraph @

src/generator/test_pat/test_pat_C.c

raw symbols SCIP
1 /*
2 test_pat_C
3 This frei0r plugin generates cross sections of color spaces
4 Version 0.1 aug 2010
5
6 Copyright (C) 2010 Marko Cebokli http://lea.hamradio.si/~s57uuu
7
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 */
24
25 /***********************************************************
26 Test patterns: cross sections of color spaces
27 *************************************************************/
28
29 //compile: gcc -Wall -c -fPIC test_pat_C.c -o test_pat_C.o
30
31 //link: gcc -lm -shared -o test_pat_C.so test_pat_C.o
32
33 #include <stdlib.h>
34 #include <assert.h>
35 #include <math.h>
36
37 #include "frei0r.h"
38
39
40
41 double PI=3.14159265358979;
42
43 typedef struct
44 {
45 float r;
46 float g;
47 float b;
48 float a;
49 } float_rgba;
50
51 //--------------------------------------------------------------
52 void draw_rectangle(float_rgba *s, int w, int h, float x, float y, float wr, float hr, float_rgba c)
53 {
54 int i,j;
55 int zx,kx,zy,ky;
56
57 zx=x; if (zx<0) zx=0;
58 zy=y; if (zy<0) zy=0;
59 kx=x+wr; if (kx>w) kx=w;
60 ky=y+hr; if (ky>h) ky=h;
61 for (i=zy;i<ky;i++)
62 for (j=zx;j<kx;j++)
63 s[w*i+j]=c;
64
65 }
66
67 //-------------------------------------------------------
68 int inside(float_rgba c)
69 {
70 if (c.r<0.0) return 0;
71 if (c.r>1.0) return 0;
72 if (c.g<0.0) return 0;
73 if (c.g>1.0) return 0;
74 if (c.b<0.0) return 0;
75 if (c.b>1.0) return 0;
76 return 1;
77 }
78
79 //-----------------------------------------------------------
80 //os: 0=RG(B) 1=GB(R) 2=BR(G)
81 //a: value on third axis
82 void risi_presek_rgb(float_rgba *s, int w, int h, float x, float y, float wr, float hr, int os, float a)
83 {
84 int i,j;
85 int zx,kx,zy,ky;
86 float_rgba c;
87 float d1,d2;
88
89 zx=x; if (zx<0) zx=0;
90 zy=y; if (zy<0) zy=0;
91 kx=x+wr; if (kx>w) kx=w;
92 ky=y+hr; if (ky>h) ky=h;
93 switch (os)
94 {
95 case 0:
96 c.b=a;
97 d1=1.0/hr;
98 d2=1.0/wr;
99 c.r=0.0;
100 for (i=zy;i<ky;i++)
101 {
102 c.r=c.r+d1;
103 c.g=0.0;
104 for (j=zx;j<kx;j++)
105 {
106 c.g=c.g+d2;
107 c.a=1.0;
108 s[w*i+j]=c;
109 }
110 }
111 break;
112 case 1:
113 c.r=a;
114 d1=1.0/hr;
115 d2=1.0/wr;
116 c.g=0.0;
117 for (i=zy;i<ky;i++)
118 {
119 c.g=c.g+d1;
120 c.b=0.0;
121 for (j=zx;j<kx;j++)
122 {
123 c.b=c.b+d2;
124 c.a=1.0;
125 s[w*i+j]=c;
126 }
127 }
128 break;
129 case 2:
130 c.g=a;
131 d1=1.0/hr;
132 d2=1.0/wr;
133 c.b=0.0;
134 for (i=zy;i<ky;i++)
135 {
136 c.b=c.b+d1;
137 c.r=0.0;
138 for (j=zx;j<kx;j++)
139 {
140 c.r=c.r+d2;
141 c.a=1.0;
142 s[w*i+j]=c;
143 }
144 }
145 break;
146 default:
147 break;
148 }
149
150 }
151
152 //-----------------------------------------------------------
153 //PAZI!!!! ali so meje -0.5 do 0.5 za Pr in Pb prave?
154 //os: 0=Y'Pr(Pb) 1=PrPb(Y) 2=PbY'(Pr)
155 //a: value on third axis
156 void risi_presek_yprpb601(float_rgba *s, int w, int h, float x, float y, float wr, float hr, int os, float a)
157 {
158 int i,j;
159 int zx,kx,zy,ky;
160 float_rgba c;
161 float d1,d2,yy,pr,pb;
162
163 zx=x; if (zx<0) zx=0;
164 zy=y; if (zy<0) zy=0;
165 kx=x+wr; if (kx>w) kx=w;
166 ky=y+hr; if (ky>h) ky=h;
167 switch (os)
168 {
169 case 0:
170 pb=a-0.5;
171 d1=1.0/hr;
172 d2=1.0/wr;
173 yy=0.0;
174 for (i=zy;i<ky;i++)
175 {
176 yy=yy+d1;
177 pr=-0.5;
178 for (j=zx;j<kx;j++)
179 {
180 pr=pr+d2;
181 c.r=pr+yy;
182 c.b=pb+yy;
183 c.g=(yy-0.3*c.r-0.1*c.b)/0.6;
184 c.a=1.0;
185 if (inside(c)==1) s[w*i+j]=c;
186 }
187 }
188 break;
189 case 1:
190 yy=a;
191 d1=1.0/hr;
192 d2=1.0/wr;
193 pr=-0.5;
194 for (i=zy;i<ky;i++)
195 {
196 pr=pr+d1;
197 pb=-0.5;
198 for (j=zx;j<kx;j++)
199 {
200 pb=pb+d2;
201 c.r=pr+yy;
202 c.b=pb+yy;
203 c.g=(yy-0.3*c.r-0.1*c.b)/0.6;
204 c.a=1.0;
205 if (inside(c)==1) s[w*i+j]=c;
206 }
207 }
208 break;
209 case 2:
210 pr=a-0.5;
211 d1=1.0/hr;
212 d2=1.0/wr;
213 pb=-0.5;
214 for (i=zy;i<ky;i++)
215 {
216 pb=pb+d1;
217 yy=0.0;
218 for (j=zx;j<kx;j++)
219 {
220 yy=yy+d2;
221 c.r=pr+yy;
222 c.b=pb+yy;
223 c.g=(yy-0.3*c.r-0.1*c.b)/0.6;
224 c.a=1.0;
225 if (inside(c)==1) s[w*i+j]=c;
226 }
227 }
228 break;
229 default:
230 break;
231 }
232
233 }
234
235 //-----------------------------------------------------------
236 //PAZI!!!! ali so meje -1.0 do 1.0 za aa in bb prave?
237 //os: 0=AB(I) 1=BI(A) 2=IA(B)
238 //a: value on third axis
239 void risi_presek_abi(float_rgba *s, int w, int h, float x, float y, float wr, float hr, int os, float a)
240 {
241 int i,j;
242 int zx,kx,zy,ky;
243 float_rgba c;
244 float d1,d2,aa,bb,ii,k3,ik3;
245
246 zx=x; if (zx<0) zx=0;
247 zy=y; if (zy<0) zy=0;
248 kx=x+wr; if (kx>w) kx=w;
249 ky=y+hr; if (ky>h) ky=h;
250 k3=sqrtf(3.0)/2.0;
251 ik3=0.5/k3;
252
253 switch (os)
254 {
255 case 0:
256 ii=a;
257 d1=2.0/hr;
258 d2=2.0/wr;
259 aa=-1.0;
260 for (i=zy;i<ky;i++)
261 {
262 aa=aa+d1;
263 bb=-1.0;
264 for (j=zx;j<kx;j++)
265 {
266 bb=bb+d2;
267 c.r=(aa+1.5*ii)*0.666666666;
268 c.b=ii-0.333333*aa-ik3*bb;
269 c.g=bb+k3*c.b;
270 c.a=1.0;
271 if (inside(c)==1) s[w*i+j]=c;
272 }
273 }
274 break;
275 case 1:
276 aa=2.0*a-1.0;
277 d1=2.0/hr;
278 d2=1.0/wr;
279 bb=-1.0;
280 for (i=zy;i<ky;i++)
281 {
282 bb=bb+d1;
283 ii=0.0;
284 for (j=zx;j<kx;j++)
285 {
286 ii=ii+d2;
287 c.r=(aa+1.5*ii)*0.666666666;
288 c.b=ii-0.333333*aa-ik3*bb;
289 c.g=bb+k3*c.b;
290 c.a=1.0;
291 if (inside(c)==1) s[w*i+j]=c;
292 }
293 }
294 break;
295 case 2:
296 bb=2.0*a-1.0;
297 d1=1.0/hr;
298 d2=2.0/wr;
299 ii=0.0;
300 for (i=zy;i<ky;i++)
301 {
302 ii=ii+d1;
303 aa=-1.0;
304 for (j=zx;j<kx;j++)
305 {
306 aa=aa+d2;
307 c.r=(aa+1.5*ii)*0.666666666;
308 c.b=ii-0.333333*aa-ik3*bb;
309 c.g=bb+k3*c.b;
310 c.a=1.0;
311 if (inside(c)==1) s[w*i+j]=c;
312 }
313 }
314 break;
315 default:
316 break;
317 }
318
319 }
320
321 //-----------------------------------------------------------
322 //os: 0=HC(I) 1=CI(H) 2=IH(C)
323 //a: value on third axis
324 void risi_presek_hci(float_rgba *s, int w, int h, float x, float y, float wr, float hr, int os, float a)
325 {
326 int i,j;
327 int zx,kx,zy,ky;
328 float_rgba c;
329 float d1,d2,aa,bb,ii,k3,ik3,hh,cc;
330
331 zx=x; if (zx<0) zx=0;
332 zy=y; if (zy<0) zy=0;
333 kx=x+wr; if (kx>w) kx=w;
334 ky=y+hr; if (ky>h) ky=h;
335 k3=sqrtf(3.0)/2.0;
336 ik3=0.5/k3;
337
338 switch (os)
339 {
340 case 0:
341 ii=a;
342 d1=2.0*PI/hr;
343 d2=1.0/wr;
344 hh=0.0;
345 for (i=zy;i<ky;i++)
346 {
347 hh=hh+d1;
348 cc=0.0;
349 for (j=zx;j<kx;j++)
350 {
351 cc=cc+d2;
352 aa=cc*cos(hh);
353 bb=cc*sin(hh);
354 c.r=(aa+1.5*ii)*0.666666666;
355 c.b=ii-0.333333*aa-ik3*bb;
356 c.g=bb+k3*c.b;
357 c.a=1.0;
358 if (inside(c)==1) s[w*i+j]=c;
359 }
360 }
361 break;
362 case 1:
363 hh=a*2.0*PI;
364 d1=1.0/hr;
365 d2=1.0/wr;
366 cc=0.0;
367 for (i=zy;i<ky;i++)
368 {
369 cc=cc+d1;
370 ii=0.0;
371 for (j=zx;j<kx;j++)
372 {
373 ii=ii+d2;
374 aa=cc*cos(hh);
375 bb=cc*sin(hh);
376 c.r=(aa+1.5*ii)*0.666666666;
377 c.b=ii-0.333333*aa-ik3*bb;
378 c.g=bb+k3*c.b;
379 c.a=1.0;
380 if (inside(c)==1) s[w*i+j]=c;
381 }
382 }
383 break;
384 case 2:
385 cc=a;
386 d1=1.0/hr;
387 d2=2.0*PI/wr;
388 ii=0.0;
389 for (i=zy;i<ky;i++)
390 {
391 ii=ii+d1;
392 hh=0.0;
393 for (j=zx;j<kx;j++)
394 {
395 hh=hh+d2;
396 aa=cc*cos(hh);
397 bb=cc*sin(hh);
398 c.r=(aa+1.5*ii)*0.666666666;
399 c.b=ii-0.333333*aa-ik3*bb;
400 c.g=bb+k3*c.b;
401 c.a=1.0;
402 if (inside(c)==1) s[w*i+j]=c;
403 }
404 }
405 break;
406 default:
407 break;
408 }
409
410 }
411
412 //-----------------------------------------------------
413 //converts the internal RGB float image into
414 //Frei0r rgba8888 color
415 //sets alpha to opaque
416 void floatrgba2color(float_rgba *sl, uint32_t* outframe, int w , int h)
417 {
418 int i;
419 uint32_t p;
420
421 for (i=0;i<w*h;i++)
422 {
423 p=(uint32_t)(255.0*sl[i].b) & 0xFF;
424 p=(p<<8) + ((uint32_t)(255.0*sl[i].g) & 0xFF);
425 p=(p<<8) + ((uint32_t)(255.0*sl[i].r) & 0xFF);
426 outframe[i] = p + 0xFF000000; //no transparency
427 }
428
429 }
430
431 //-----------------------------------------------------
432 //stretch [0...1] to parameter range [min...max] linear
433 float map_value_forward(double v, float min, float max)
434 {
435 return min+(max-min)*v;
436 }
437
438 //-----------------------------------------------------
439 //collapse from parameter range [min...max] to [0...1] linear
440 double map_value_backward(float v, float min, float max)
441 {
442 return (v-min)/(max-min);
443 }
444
445 //-----------------------------------------------------
446 //stretch [0...1] to parameter range [min...max] logarithmic
447 //min and max must be positive!
448 float map_value_forward_log(double v, float min, float max)
449 {
450 float sr,k;
451
452 sr=sqrtf(min*max);
453 k=2.0*log(max/sr);
454 return sr*expf(k*(v-0.5));
455 }
456
457 //-----------------------------------------------------
458 //collapse from parameter range [min...max] to [0...1] logarithmic
459 //min and max must be positive!
460 double map_value_backward_log(float v, float min, float max)
461 {
462 float sr,k;
463
464 sr=sqrtf(min*max);
465 k=2.0*log(max/sr);
466 return logf(v/sr)/k+0.5;
467 }
468
469 //**************************************************
470 //obligatory frei0r stuff follows
471
472 //------------------------------------------------
473 //this structure holds an instance of the test_pat_C plugin
474 typedef struct
475 {
476 unsigned int w;
477 unsigned int h;
478
479 int spc;
480 int cs;
481 float thav;
482 int fs;
483
484 float_rgba *sl;
485
486 } tp_inst_t;
487
488 //----------------------------------------------------
489 int f0r_init()
490 {
491 return 1;
492 }
493
494 //--------------------------------------------------
495 void f0r_deinit()
496 { /* no initialization required */ }
497
498 //--------------------------------------------------
499 void f0r_get_plugin_info(f0r_plugin_info_t* tp_info)
500 {
501 tp_info->name = "test_pat_C";
502 tp_info->author = "Marko Cebokli";
503 tp_info->plugin_type = F0R_PLUGIN_TYPE_SOURCE;
504 // tp_info->plugin_type = F0R_PLUGIN_TYPE_FILTER;
505 tp_info->color_model = F0R_COLOR_MODEL_RGBA8888;
506 tp_info->frei0r_version = FREI0R_MAJOR_VERSION;
507 tp_info->major_version = 0;
508 tp_info->minor_version = 1;
509 tp_info->num_params = 4;
510 tp_info->explanation = "Generates cross sections of color spaces";
511 }
512
513 //--------------------------------------------------
514 void f0r_get_param_info(f0r_param_info_t* info, int param_index)
515 {
516 switch (param_index)
517 {
518 case 0:
519 info->name = "Color space";
520 info->type = F0R_PARAM_DOUBLE;
521 info->explanation = "";
522 break;
523 case 1:
524 info->name ="Cross section";
525 info->type = F0R_PARAM_DOUBLE;
526 info->explanation = "";
527 break;
528 case 2:
529 info->name = "Third axis value";
530 info->type = F0R_PARAM_DOUBLE;
531 info->explanation = "";
532 break;
533 case 3:
534 info->name = "Fullscreen";
535 info->type = F0R_PARAM_BOOL;
536 info->explanation = "";
537 break;
538 }
539 }
540
541 //--------------------------------------------------
542 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
543 {
544 int x0,y0,velx,vely;
545 float_rgba c;
546 tp_inst_t* inst = calloc(1, sizeof(*inst));
547 inst->w = width;
548 inst->h = height;
549
550 inst->spc=0;
551 inst->cs=0;
552 inst->thav=0.5;
553 inst->fs=0;
554
555 inst->sl=(float_rgba*)calloc(width*height,sizeof(float_rgba));
556
557 x0=(inst->w-3*inst->h/4)/2;
558 y0=inst->h/8;
559 velx=3*inst->h/4;
560 vely=3*inst->h/4;
561 c.r=0.5;c.g=0.5;c.b=0.5;c.a=1.0; //gray background
562 draw_rectangle(inst->sl, inst->w, inst->h, 0.0, 0.0, (float)inst->w, (float)inst->h, c);
563 c.r=0.4;c.g=0.4;c.b=0.4;c.a=1.0; //darker gray background
564 draw_rectangle(inst->sl, inst->w, inst->h, x0, y0, velx, vely, c);
565 risi_presek_rgb(inst->sl, inst->w, inst->h, x0, y0, velx, vely, inst->cs, inst->thav);
566
567 return (f0r_instance_t)inst;
568 }
569
570 //--------------------------------------------------
571 void f0r_destruct(f0r_instance_t instance)
572 {
573 tp_inst_t* inst = (tp_inst_t*)instance;
574
575 free(inst->sl);
576 free(inst);
577 }
578
579 //--------------------------------------------------
580 void f0r_set_param_value(f0r_instance_t instance, f0r_param_t param, int param_index)
581 {
582 tp_inst_t* inst = (tp_inst_t*)instance;
583 f0r_param_double* p = (f0r_param_double*) param;
584 int chg,tmpi,x0,y0,velx,vely;
585 float tmpf;
586 float_rgba c;
587
588 chg=0;
589 switch (param_index)
590 {
591 case 0: //color space
592 tmpf=*((double*)p);
593 if (tmpf>=1.0)
594 tmpi=(int)tmpf;
595 else
596 tmpi = map_value_forward(tmpf, 0.0, 3.9999);
597 if ((tmpi<0)||(tmpi>3.0)) break;
598 if (inst->spc != tmpi) chg=1;
599 inst->spc = tmpi;
600 break;
601 case 1: //cross section
602 tmpf=*((double*)p);
603 if (tmpf>=1.0)
604 tmpi=(int)tmpf;
605 else
606 tmpi = map_value_forward(tmpf, 0.0, 2.9999);
607 if ((tmpi<0)||(tmpi>2.0)) break;
608 if (inst->cs != tmpi) chg=1;
609 inst->cs = tmpi;
610 break;
611 case 2: //third axis value
612 tmpf = map_value_forward(*((double*)p), 0.0, 1.0);
613 if (inst->thav != tmpf) chg=1;
614 inst->thav = tmpf;
615 break;
616 case 3: //fullscreen (BOOL)
617 tmpi = map_value_forward(*((double*)p), 0.0, 1.0);
618 if (inst->fs != tmpi) chg=1;
619 inst->fs = tmpi;
620 break;
621 }
622
623 if (chg==0) return;
624
625 if (inst->fs==0)
626 {
627 x0=(inst->w-3*inst->h/4)/2;
628 y0=inst->h/8;
629 velx=3*inst->h/4;
630 vely=3*inst->h/4;
631 }
632 else
633 {
634 x0=0;
635 y0=0;
636 velx=inst->w;
637 vely=inst->h;
638 }
639
640 c.r=0.5;c.g=0.5;c.b=0.5;c.a=1.0; //gray background
641 draw_rectangle(inst->sl, inst->w, inst->h, 0.0, 0.0, (float)inst->w, (float)inst->h, c);
642 c.r=0.4;c.g=0.4;c.b=0.4;c.a=1.0; //darker gray background
643 draw_rectangle(inst->sl, inst->w, inst->h, x0, y0, velx, vely, c);
644
645 switch (inst->spc)
646 {
647 case 0:
648 risi_presek_rgb(inst->sl, inst->w, inst->h, x0, y0, velx, vely, inst->cs, inst->thav);
649 break;
650 case 1:
651 risi_presek_yprpb601(inst->sl, inst->w, inst->h, x0, y0, velx, vely, inst->cs, inst->thav);
652 break;
653 case 2:
654 risi_presek_abi(inst->sl, inst->w, inst->h, x0, y0, velx, vely, inst->cs, inst->thav);
655 break;
656 case 3:
657 risi_presek_hci(inst->sl, inst->w, inst->h, x0, y0, velx, vely, inst->cs, inst->thav);
658 break;
659 default:
660 break;
661 }
662
663 }
664
665 //-------------------------------------------------
666 void f0r_get_param_value(f0r_instance_t instance, f0r_param_t param, int param_index)
667 {
668 tp_inst_t* inst = (tp_inst_t*)instance;
669
670 f0r_param_double* p = (f0r_param_double*) param;
671
672 switch (param_index)
673 {
674 case 0: //color space
675 *p = map_value_backward(inst->spc, 0.0, 3.9999);
676 break;
677 case 1: //cross section
678 *p = map_value_backward(inst->cs, 0.0, 2.9999);
679 break;
680 case 2: //third axis value
681 *p = map_value_backward(inst->thav, 0.0, 1.0);
682 break;
683 case 3: //fullscreen (BOOL)
684 *p = map_value_backward_log(inst->fs, 0.0, 1.0);
685 break;
686 }
687 }
688
689 //---------------------------------------------------
690 void f0r_update(f0r_instance_t instance, double time, const uint32_t* inframe, uint32_t* outframe)
691 {
692
693 assert(instance);
694 tp_inst_t* inst = (tp_inst_t*)instance;
695
696 floatrgba2color(inst->sl, outframe, inst->w , inst->h);
697
698 }