Merge ../mlt
[melted] / src / modules / oldfilm / filter_vignette.c
1 /*
2 * filter_vignette.c -- vignette filter
3 * Copyright (c) 2007 Marco Gittler <g.marco@freenet.de>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include <framework/mlt_filter.h>
21 #include <framework/mlt_frame.h>
22 #include <framework/mlt_geometry.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <math.h>
27 #define MIN(a,b) (a<b?a:b)
28 #define MAX(a,b) (a<b?b:a)
29
30 #define SIGMOD_STEPS 1000
31 #define POSITION_VALUE(p,s,e) (s+((double)(e-s)*p ))
32 //static double pow2[SIGMOD_STEPS];
33
34 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
35 {
36
37 mlt_filter filter = mlt_frame_pop_service( this );
38 int error = mlt_frame_get_image( this, image, format, width, height, 1 );
39
40 if ( error == 0 && *image && *format == mlt_image_yuv422 )
41 {
42 mlt_position in = mlt_filter_get_in( filter );
43 //mlt_position out = mlt_filter_get_out( filter );
44 mlt_position time = mlt_frame_get_position( this );
45
46 mlt_geometry geom=mlt_geometry_init();
47 struct mlt_geometry_item_s item;
48 float smooth, radius, cx, cy, opac;
49 char *val=mlt_properties_get(MLT_FILTER_PROPERTIES( filter ), "geometry" );
50 mlt_geometry_parse(geom,val,-1,-1,-1);
51 mlt_geometry_fetch(geom,&item,time-in);
52 smooth=item.x;
53 radius=item.y;
54 cx=item.w;
55 cy=item.h;
56 opac=item.mix;
57 mlt_geometry_close(geom);
58
59 int video_width = *width;
60 int video_height = *height;
61
62 int x,y;
63 int w2=cx,h2=cy;
64 double delta=1.0;
65 double max_opac=opac/100.0;
66
67 for (y=0;y<video_height;y++){
68 int h2_pow2=pow(y-h2,2.0);
69 for (x=0;x<video_width;x++){
70 uint8_t *pix=(*image+y*video_width*2+x*2);
71 int dx=sqrt(h2_pow2+pow(x-w2,2.0));
72
73 if (radius-smooth>dx){ //center, make not darker
74 continue;
75 }
76 else if (radius+smooth<=dx){//max dark after smooth area
77 delta=0.0;
78 }else{
79 //double sigx=5.0-10.0*(double)(dx-radius+smooth)/(2.0*smooth);//smooth >10 inner area, <-10 in dark area
80 //delta=pow2[((int)((sigx+10.0)*SIGMOD_STEPS/20.0))];//sigmoidal
81 delta = ((double)(radius+smooth-dx)/(2.0*smooth));//linear
82 }
83 delta=MAX(max_opac,delta);
84 *pix=(double)(*pix)*delta;
85 *(pix+1)=((double)(*(pix+1)-127.0)*delta)+127.0;
86 }
87 }
88 // short a, short b, short c, short d
89 // a= gray val pix 1
90 // b: +=blue, -=yellow
91 // c: =gray pix 2
92 // d: +=red,-=green
93 }
94
95 return error;
96 }
97
98 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
99 {
100
101 mlt_frame_push_service( frame, this );
102 mlt_frame_push_get_image( frame, filter_get_image );
103 return frame;
104 }
105
106
107 mlt_filter filter_vignette_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
108 {
109 mlt_filter this = mlt_filter_new( );
110 //int i=0;
111 if ( this != NULL )
112 {
113 /*
114 for (i=-SIGMOD_STEPS/2;i<SIGMOD_STEPS/2;i++){
115 pow2[i+SIGMOD_STEPS/2]=1.0/(1.0+pow(2.0,-((double)i)/((double)SIGMOD_STEPS/20.0)));
116 }
117 */
118
119 this->process = filter_process;
120 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "geometry", "80:50%:50%:50%:0" );
121 //mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "end", "" );
122
123 }
124 return this;
125 }
126
127