]> git.sesse.net Git - mlt/blob - src/modules/oldfilm/filter_vignette.c
Move burningtv into plusgpl module.
[mlt] / 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 static float geometry_to_float(char *val, mlt_position pos )
34 {
35     float ret=0.0;
36     struct mlt_geometry_item_s item;
37
38         mlt_geometry geom=mlt_geometry_init();
39     mlt_geometry_parse(geom,val,-1,-1,-1);
40     mlt_geometry_fetch(geom,&item , pos );
41     ret=item.x;
42     mlt_geometry_close(geom);
43
44     return ret;
45 }
46
47 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
48 {
49         
50         mlt_filter filter = mlt_frame_pop_service( this );
51         *format = mlt_image_yuv422;
52         int error = mlt_frame_get_image( this, image, format, width, height, 1 );
53
54         if ( error == 0 && *image )
55         {
56                 float smooth, radius, cx, cy, opac;
57                 mlt_properties filter_props = MLT_FILTER_PROPERTIES( filter ) ;
58                 mlt_position pos = mlt_filter_get_position( filter, this );
59
60                 smooth = geometry_to_float ( mlt_properties_get( filter_props , "smooth" ) , pos ) * 100.0 ;
61                 radius = geometry_to_float ( mlt_properties_get( filter_props , "radius" ) , pos ) * *width;
62                 cx = geometry_to_float ( mlt_properties_get( filter_props , "x" ) , pos ) * *width;
63                 cy = geometry_to_float ( mlt_properties_get( filter_props , "y" ) , pos ) * *height;
64                 opac = geometry_to_float ( mlt_properties_get( filter_props , "opacity" ) , pos );
65                 int mode = mlt_properties_get_int( filter_props , "mode" );
66
67                 int video_width = *width;
68                 int video_height = *height;
69                 
70                 int x,y;
71                 int w2=cx,h2=cy;
72                 double delta=1.0;
73                 double max_opac=opac;
74                 for (y=0;y<video_height;y++){
75                         int h2_pow2=pow(y-h2,2.0);
76                         for (x=0;x<video_width;x++){
77                                 uint8_t *pix=(*image+y*video_width*2+x*2);
78                                 int dx=sqrt(h2_pow2+pow(x-w2,2.0));
79                                 
80                                 if (radius-smooth>dx){  //center, make not darker
81                                         continue;
82                                 }
83                                 else if (radius+smooth<=dx){//max dark after smooth area
84                                         delta=0.0;
85                                 }else{
86                                         // linear pos from of opacity (from 0 to 1)
87                                         delta=(double)(radius+smooth-dx)/(2.0*smooth);
88                                         if (mode==1){
89                                                 //make cos for smoother non linear fade
90                                                 delta = (double)pow(cos(((1.0-delta)*3.14159/2.0)),3.0);
91                                         }
92                                 }
93                                 delta=MAX(max_opac,delta);
94                                 *pix=(double)(*pix)*delta;
95                                 *(pix+1)=((double)(*(pix+1)-127.0)*delta)+127.0;
96                         }
97                 }
98         }
99         
100         return error;
101 }
102
103 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
104 {
105         mlt_frame_push_service( frame, this );
106         mlt_frame_push_get_image( frame, filter_get_image );
107         return frame;
108 }
109
110
111 mlt_filter filter_vignette_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
112 {
113         mlt_filter this = mlt_filter_new( );
114         //int i=0;
115         if ( this != NULL )
116         {
117                 /*
118                 for (i=-SIGMOD_STEPS/2;i<SIGMOD_STEPS/2;i++){
119                         pow2[i+SIGMOD_STEPS/2]=1.0/(1.0+pow(2.0,-((double)i)/((double)SIGMOD_STEPS/20.0)));
120                 }
121                 */
122                 
123                 this->process = filter_process;
124                 mlt_properties_set_double( MLT_FILTER_PROPERTIES( this ), "smooth", 0.8 );
125                 mlt_properties_set_double( MLT_FILTER_PROPERTIES( this ), "radius", 0.5 );
126                 mlt_properties_set_double( MLT_FILTER_PROPERTIES( this ), "x", 0.5 );
127                 mlt_properties_set_double( MLT_FILTER_PROPERTIES( this ), "y", 0.5 );
128                 mlt_properties_set_double( MLT_FILTER_PROPERTIES( this ), "opacity", 0.0 );
129                 mlt_properties_set_double( MLT_FILTER_PROPERTIES( this ), "mode", 0 );
130
131                 //mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "end", "" );
132
133         }
134         return this;
135 }
136
137