Debian Codegraph @

src/filter/medians/medians.c

raw symbols SCIP
1 /*
2 medians.c implements several median-type filters
3
4 This frei0r plugin implements several median-type filters
5
6 Version 0.1 jan 2013
7
8 Copyright (C) 2013 Marko Cebokli http://lea.hamradio.si/~s57uuu
9
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 */
26
27
28 //compile: gcc -c -fPIC -Wall medians.c -o medians.o
29 //link: gcc -shared -o medians.so medians.o
30
31 #include <stdio.h>
32 #include <frei0r.h>
33 #include <stdlib.h>
34 #include <math.h>
35 #include <assert.h>
36 #include <inttypes.h>
37 #include <string.h>
38 #include <stdio.h>
39
40 #include "small_medians.h"
41 #include "ctmf.h"
42
43
44 /* ******************************************
45 //The following functions implement these median type filters:
46
47 X
48 cross5: XXX
49 X
50
51 XXX
52 square3x3: XXX
53 XXX
54
55
56 Bilevel:
57
58 X
59 XXX
60 dia3x3: XXXXX
61 XXX
62 X
63
64 Arce BI: multilevel spatio-temporal, see [1]
65
66 Arp ML3D: multilevel spatio-temporal, see [1]
67
68 ML3DEX: multilevel spatio-temporal, see [1]
69
70
71 [1] Anil Christopher Kokaram: Motion Picture Restoration
72 phd dissertation
73
74 ****************************************** */
75
76 //------------------------------------------------------------
77 //cross5 packed char RGB image (uint32_t)
78 //vs = input image
79 //is = output image
80 void cross5(const uint32_t *vs, int w, int h, uint32_t *is)
81 {
82 int i,j,p;
83 uint32_t m[8];
84
85 for (i=1;i<h-1;i++)
86 for (j=1;j<w-1;j++)
87 {
88 p=i*w+j;
89
90 m[0]=vs[p-w]; m[1]=vs[p-1]; m[2]=vs[p];
91 m[3]=vs[p+1]; m[4]=vs[p+w];
92
93 is[p]=median5(m);
94 }
95 }
96
97 //------------------------------------------------------------
98 //square 3x3 packed char RGB image (uint32_t)
99 //vs = input image
100 //is = output image
101 void sq3x3(const uint32_t *vs, int w, int h, uint32_t *is)
102 {
103 int i,j,p;
104 uint32_t m[16];
105
106 for (i=1;i<h-1;i++)
107 for (j=1;j<w-1;j++)
108 {
109 p=i*w+j;
110
111 m[0]=vs[p-w-1]; m[1]=vs[p-w]; m[2]=vs[p-w+1];
112 m[3]=vs[p-1]; m[4]=vs[p]; m[5]=vs[p+1];
113 m[6]=vs[p+w-1]; m[7]=vs[p+w]; m[8]=vs[p+w+1];
114
115 is[p]=median9(m);
116 }
117 }
118
119 //------------------------------------------------------------
120 //bilevel packed char RGB image (uint32_t)
121 //vs = input image
122 //is = output image
123 void bilevel(const uint32_t *vs, int w, int h, uint32_t *is)
124 {
125 int i,j,p;
126 uint32_t m[8],mm[4];
127
128 for (i=1;i<h-1;i++)
129 for (j=1;j<w-1;j++)
130 {
131 p=i*w+j;
132
133 m[0]=vs[p-w-1]; m[1]=vs[p-w+1]; m[2]=vs[p];
134 m[3]=vs[p+w-1]; m[4]=vs[p+w+1];
135 mm[0]=median5(m);
136 mm[1]=vs[p];
137 m[0]=vs[p-w]; m[1]=vs[p-1]; m[2]=vs[p];
138 m[3]=vs[p+1]; m[4]=vs[p+w];
139 mm[2]=median5(m);
140
141 is[p]=median3(mm);
142 }
143 }
144
145 //------------------------------------------------------------
146 //diamond 3x3 packed char RGB image (uint32_t)
147 //vs = input image
148 //is = output image
149 void dia3x3(const uint32_t *vs, int w, int h, uint32_t *is)
150 {
151 int i,j,p;
152 uint32_t m[16];
153
154 for (i=2;i<h-2;i++)
155 for (j=2;j<w-2;j++)
156 {
157 p=i*w+j;
158 m[0]=vs[p-2*w]; m[1]=vs[p-w-1]; m[2]=vs[p-w];
159 m[3]=vs[p-w+1]; m[4]=vs[p-2]; m[5]=vs[p-1];
160 m[6]=vs[p]; m[7]=vs[p+1]; m[8]=vs[p+2];
161 m[9]=vs[p+w-1]; m[10]=vs[p+w]; m[11]=vs[p+w+1];
162 m[12]=vs[p+2*w];
163
164 is[p]=median13(m);
165 }
166 }
167
168 //------------------------------------------------------------
169 //square 5x5 packed char RGB image (uint32_t)
170 //vs = input image
171 //is = output image
172 void sq5x5(const uint32_t *vs, int w, int h, uint32_t *is)
173 {
174 int i,j,p;
175 uint32_t m[32];
176
177 for (i=2;i<h-2;i++)
178 for (j=2;j<w-2;j++)
179 {
180 p=i*w+j;
181
182 m[0]=vs[p-2*w-2]; m[1]=vs[p-2*w-1]; m[2]=vs[p-2*w];
183 m[3]=vs[p-2*w+1]; m[4]=vs[p-2*w+2]; m[5]=vs[p-w-2];
184 m[6]=vs[p-w-1]; m[7]=vs[p-w]; m[8]=vs[p-w+1];
185 m[9]=vs[p-w+2]; m[10]=vs[p-2]; m[11]=vs[p-1];
186 m[12]=vs[p]; m[13]=vs[p+1]; m[14]=vs[p+2];
187 m[15]=vs[p+w-2]; m[16]=vs[p+w-1]; m[17]=vs[p+w];
188 m[18]=vs[p+w+1]; m[19]=vs[p+w+2]; m[20]=vs[p+2*w-2];
189 m[21]=vs[p+2*w-1];m[22]=vs[p+2*w]; m[23]=vs[p+2*w+1];
190 m[24]=vs[p+2*w+2];
191
192 is[p]=median25(m);
193 }
194 }
195
196
197 //--------------------------------------------------------
198 //temporal 3 frames
199 void temp3(uint32_t *s1, uint32_t *s2, uint32_t *s3, int w, int h, uint32_t *is)
200 {
201 int i;
202 uint32_t m[32];
203
204 for (i=0;i<w*h;i++)
205 {
206 m[0]=s1[i]; m[1]=s2[i]; m[2]=s3[i];
207 is[i]=median3(m);
208 }
209 }
210
211
212 //--------------------------------------------------------
213 //temporal 5 frames
214 void temp5(uint32_t *s1, uint32_t *s2, uint32_t *s3, uint32_t *s4, uint32_t *s5, int w, int h, uint32_t *is)
215 {
216 int i;
217 uint32_t m[32];
218
219 for (i=0;i<w*h;i++)
220 {
221 m[0]=s1[i]; m[1]=s2[i]; m[2]=s3[i]; m[3]=s4[i]; m[4]=s5[i];
222 is[i]=median5(m);
223 }
224
225 }
226
227
228 //------------------------------------------------------------
229 //Arce BI packed char RGB image (uint32_t)
230 //s1,s2,s3 = previous, current, next frame
231 //is = output image
232 void ArceBI(uint32_t *s1, uint32_t *s2, uint32_t *s3, int w, int h, uint32_t *is)
233 {
234 int i,j,p;
235 uint32_t mm[8],m[16];
236
237 for (i=1;i<h-1;i++)
238 for (j=1;j<w-1;j++)
239 {
240 p=i*w+j;
241 //grupa C
242 mm[0]=s1[p];
243 //GRUPA W1
244 m[0]=s1[p]; m[1]=s2[p-w-1]; m[2]=s2[p];
245 m[3]=s2[p+w+1]; m[4]=s3[p];
246 mm[3]=median5(m);
247 //grupa W2
248 m[0]=s1[p]; m[1]=s2[p-w]; m[2]=s2[p];
249 m[3]=s2[p+w]; m[4]=s3[p];
250 mm[4]=median5(m);
251 //grupa W3
252 m[0]=s1[p]; m[1]=s2[p-1]; m[2]=s2[p];
253 m[3]=s2[p+1]; m[4]=s3[p];
254 mm[5]=median5(m);
255 //grupa W4
256 m[0]=s1[p]; m[1]=s2[p-w+1]; m[2]=s2[p];
257 m[3]=s2[p+w-1]; m[4]=s3[p];
258 mm[6]=median5(m);
259 //max
260 mm[1]=mm[3]; if (mm[4]>mm[1]) mm[1]=mm[4];
261 if (mm[5]>mm[1]) mm[1]=mm[5];
262 if (mm[6]>mm[1]) mm[1]=mm[6];
263 //min
264 mm[2]=mm[3]; if (mm[4]<mm[2]) mm[2]=mm[4];
265 if (mm[5]<mm[2]) mm[2]=mm[5];
266 if (mm[6]<mm[2]) mm[2]=mm[6];
267 //izhod
268 is[p]=median3(mm);
269 }
270 }
271
272
273 //------------------------------------------------------------
274 //Arp ML3D packed char RGB image (uint32_t)
275 //s1,s2,s3 = previous, current, next frame
276 //is = output image
277 void ml3d(uint32_t *s1, uint32_t *s2, uint32_t *s3, int w, int h, uint32_t *is)
278 {
279 int i,j,p;
280 uint32_t mm[8],m[16];
281
282 for (i=1;i<h-1;i++)
283 for (j=1;j<w-1;j++)
284 {
285 p=i*w+j;
286 //grupa C
287 mm[0]=s1[p];
288 //grupa W6
289 m[0]=s1[p]; m[1]=s2[p-w-1]; m[2]=s2[p-w+1];
290 m[3]=s2[p]; m[4]=s2[p+w-1]; m[5]=s2[p+w+1];
291 m[6]=s3[p];
292 mm[1]=median7(m);
293 //grupa W5
294 m[0]=s1[p]; m[1]=s2[p-w]; m[2]=s2[p-1];
295 m[3]=s2[p]; m[4]=s2[p+1]; m[5]=s2[p+w];
296 m[6]=s3[p];
297 mm[2]=median7(m);
298 //izhod = median medianov
299 is[p]=median3(mm);
300 }
301 }
302
303 //------------------------------------------------------------
304 //Kokaram ML3Dex packed char RGB image (uint32_t)
305 //s1,s2,s3 = previous, current, next frame
306 //is = output image
307 void ml3dex(uint32_t *s1, uint32_t *s2, uint32_t *s3, int w, int h, uint32_t *is)
308 {
309 int i,j,p;
310 uint32_t mm[8],m[16];
311
312 for (i=1;i<h-1;i++)
313 for (j=1;j<w-1;j++)
314 {
315 p=i*w+j;
316 //grupa W9
317 m[0]=s1[p-w-1]; m[1]=s1[p-w+1]; m[2]=s1[p];
318 m[3]=s1[p+w-1]; m[4]=s1[p+w+1]; m[5]=s2[p];
319 m[6]=s3[p-w-1]; m[7]=s3[p-w+1]; m[8]=s3[p];
320 m[9]=s3[p+w-1]; m[10]=s3[p+w+1];
321 mm[0]=median11(m);
322 //grupa W8
323 m[0]=s1[p-w]; m[1]=s1[p-1]; m[2]=s1[p];
324 m[3]=s1[p+w]; m[4]=s1[p+1]; m[5]=s2[p];
325 m[6]=s3[p-w]; m[7]=s3[p-1]; m[8]=s3[p];
326 m[9]=s3[p+w]; m[10]=s3[p+1];
327 mm[1]=median11(m);
328 //grupa W7
329 m[0]=s1[p]; m[1]=s2[p]; m[2]=s3[p];
330 mm[2]=median3(m);
331 //grupa W6
332 m[0]=s1[p]; m[1]=s2[p-w-1]; m[2]=s2[p-w+1];
333 m[3]=s2[p]; m[4]=s2[p+w-1]; m[5]=s2[p+w+1];
334 m[6]=s3[p];
335 mm[3]=median7(m);
336 //grupa W5
337 m[0]=s1[p]; m[1]=s2[p-w]; m[2]=s2[p-1];
338 m[3]=s2[p]; m[4]=s2[p+1]; m[5]=s2[p+w];
339 m[6]=s3[p];
340 mm[4]=median7(m);
341 //izhod = median medianov
342 is[p]=median5(mm);
343 }
344 }
345
346 //****************************************************
347
348 //----------------------------------------
349 //struktura za instanco efekta
350 typedef struct
351 {
352 int h;
353 int w;
354
355 //parameters
356 int type;
357 int size;
358
359 //internal variables
360 uint32_t *ppf,*pf,*cf,*nf,*nnf;
361
362 //image buffers
363 uint32_t *f1;
364 uint32_t *f2;
365 uint32_t *f3;
366 uint32_t *f4;
367 uint32_t *f5;
368
369
370 char *liststr;
371 } inst;
372
373
374 //-----------------------------------------------------
375 //stretch [0...1] to parameter range [min...max] linear
376 float map_value_forward(double v, float min, float max)
377 {
378 return min+(max-min)*v;
379 }
380
381 //-----------------------------------------------------
382 //collapse from parameter range [min...max] to [0...1] linear
383 double map_value_backward(float v, float min, float max)
384 {
385 return (v-min)/(max-min);
386 }
387
388 //***********************************************
389 // OBVEZNE FREI0R FUNKCIJE
390
391 //-----------------------------------------------
392 int f0r_init()
393 {
394 return 1;
395 }
396
397 //------------------------------------------------
398 void f0r_deinit()
399 {
400 }
401
402 //-----------------------------------------------
403 void f0r_get_plugin_info(f0r_plugin_info_t* info)
404 {
405
406 info->name="Medians";
407 info->author="Marko Cebokli";
408 info->plugin_type=F0R_PLUGIN_TYPE_FILTER;
409 info->color_model=F0R_COLOR_MODEL_RGBA8888;
410 info->frei0r_version=FREI0R_MAJOR_VERSION;
411 info->major_version=0;
412 info->minor_version=1;
413 info->num_params=2;
414 info->explanation="Implements several median-type filters";
415 }
416
417 //--------------------------------------------------
418 void f0r_get_param_info(f0r_param_info_t* info, int param_index)
419 {
420 switch(param_index)
421 {
422 case 0:
423 info->name = "Type";
424 info->type = F0R_PARAM_STRING;
425 info->explanation = "Choose type of median: Cross5, Square3x3, Bilevel, Diamond3x3, Square5x5, Temp3, Temp5, ArceBI, ML3D, ML3dEX, VarSize";
426 break;
427 case 1:
428 info->name = "Size";
429 info->type = F0R_PARAM_DOUBLE;
430 info->explanation = "Size for 'var size' type filter";
431 break;
432 case 2:
433 info->name = "";
434 info->type = F0R_PARAM_BOOL;
435 info->explanation = "";
436 break;
437 }
438 }
439
440 //----------------------------------------------
441 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
442 {
443 inst *in;
444
445 in=calloc(1,sizeof(inst));
446 in->w=width;
447 in->h=height;
448
449 in->type=1;
450 in->liststr=calloc(1,strlen("Square3x3")+1);
451 strcpy(in->liststr,"Square3x3");
452 in->size=5;
453
454 in->f1=calloc(in->w*in->h,sizeof(uint32_t));
455 in->f2=calloc(in->w*in->h,sizeof(uint32_t));
456 in->f3=calloc(in->w*in->h,sizeof(uint32_t));
457 in->f4=calloc(in->w*in->h,sizeof(uint32_t));
458 in->f5=calloc(in->w*in->h,sizeof(uint32_t));
459
460 in->ppf=in->f1;
461 in->pf=in->f2;
462 in->cf=in->f3;
463 in->nf=in->f4;
464 in->nnf=in->f5;
465
466 return (f0r_instance_t)in;
467 }
468
469 //---------------------------------------------------
470 void f0r_destruct(f0r_instance_t instance)
471 {
472 inst *in;
473
474 in=(inst*)instance;
475
476 free(in->f1);
477 free(in->f2);
478 free(in->f3);
479 free(in->f4);
480 free(in->f5);
481
482 free(in->liststr);
483 free(instance);
484 }
485
486 //-----------------------------------------------------
487 void f0r_set_param_value(f0r_instance_t instance, f0r_param_t parm, int param_index)
488 {
489 inst *p;
490 double tmpf;
491 int chg;
492 char *tmpch;
493 char list1[][11]={"Cross5", "Square3x3", "Bilevel", "Diamond3x3", "Square5x5", "Temp3", "Temp5", "ArceBI", "ML3D", "ML3dEX", "VarSize"};
494
495 p=(inst*)instance;
496
497 chg=0;
498 switch(param_index)
499 {
500 case 0: //(string based list)
501 tmpch = (*(char**)parm);
502 p->liststr = (char*)realloc( p->liststr, strlen(tmpch) + 1 );
503 strcpy( p->liststr, tmpch );
504 p->type=0;
505 while ((strcmp(p->liststr,list1[p->type])!=0)&&(p->type<10)) p->type++;
506 break;
507 case 1:
508 tmpf=map_value_forward(*((double*)parm), 0.0, 50);
509 if (tmpf!=p->size) chg=1;
510 p->size=tmpf;
511 break;
512 }
513
514 if (chg==0) return;
515
516 }
517
518 //--------------------------------------------------
519 void f0r_get_param_value(f0r_instance_t instance, f0r_param_t param, int param_index)
520 {
521 inst *p;
522
523 p=(inst*)instance;
524
525 switch(param_index)
526 {
527 case 0: //(string based list)
528 *((char**)param) = p->liststr;
529 break;
530 case 1:
531 *((double*)param)=map_value_backward(p->size, 0.0, 50);
532 break;
533 }
534 }
535
536 //-------------------------------------------------
537 void f0r_update(f0r_instance_t instance, double time, const uint32_t* inframe, uint32_t* outframe)
538 {
539 inst *in;
540
541 assert(instance);
542 in=(inst*)instance;
543 uint32_t *tmpp;
544 uint8_t *cin,*cout;
545 int step,i;
546
547 memcpy(in->ppf, inframe, 4*in->w*in->h);
548 tmpp=in->nnf;
549 in->nnf=in->ppf;
550 in->ppf=in->pf;
551 in->pf=in->cf;
552 in->cf=in->nf;
553 in->nf=tmpp;
554
555 cin=(uint8_t*)inframe;
556 cout=(uint8_t*)outframe;
557
558 switch (in->type)
559 {
560 case 0:
561 cross5(inframe, in->w, in->h, outframe);
562 break;
563 case 1:
564 sq3x3(inframe, in->w, in->h, outframe);
565 break;
566 case 2:
567 bilevel(inframe, in->w, in->h, outframe);
568 break;
569 case 3:
570 dia3x3(inframe, in->w, in->h, outframe);
571 break;
572 case 4:
573 sq5x5(inframe, in->w, in->h, outframe);
574 break;
575 case 5:
576 temp3(in->cf, in->nf, in->nnf, in->w, in->h, outframe);
577 break;
578 case 6:
579 temp5(in->ppf, in->pf, in->cf, in->nf, in->nnf, in->w, in->h, outframe);
580 break;
581 case 7:
582 ArceBI(in->cf, in->nf, in->nnf, in->w, in->h, outframe);
583 break;
584 case 8:
585 ml3d(in->cf, in->nf, in->nnf, in->w, in->h, outframe);
586 break;
587 case 9:
588 ml3dex(in->cf, in->nf, in->nnf, in->w, in->h, outframe);
589 break;
590 case 10:
591 //varsize
592 step=in->w*4;
593 ctmf(cin,cout,in->w,in->h,step,step,in->size,4,512*1024);
594 break;
595 default:
596 break;
597 }
598 //COPY ALPHA
599 for (i = 3; i < 4 * in->w * in->h; i += 4)
600 cout[i]=cin[i];
601
602 }
603