6ac7d0de741025184d3001ba1a9e280c11b9bdfd
[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
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <math.h>
26 #define MIN(a,b) (a<b?a:b)
27 #define MAX(a,b) (a<b?b:a)
28
29 #define SIGMOD_STEPS 1000
30 #define POSITION_VALUE(p,s,e) (s+((double)(e-s)*p ))
31 //static double pow2[SIGMOD_STEPS];
32
33 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
34 {
35
36 mlt_filter filter = mlt_frame_pop_service( this );
37 int error = mlt_frame_get_image( this, image, format, width, height, 1 );
38
39 if ( error == 0 && *image && *format == mlt_image_yuv422 )
40 {
41 float smooth_s=80,radius_s=50,x_s=50,y_s=50,opac_s=0;
42 float smooth_e=80,radius_e=50,x_e=50,y_e=50,opac_e=0;
43
44 sscanf(mlt_properties_get(MLT_FILTER_PROPERTIES( filter ), "start" ), "%f:%f,%fx%f,%f",&smooth_s,&radius_s,&x_s,&y_s,&opac_s);
45 if (mlt_properties_get(MLT_FILTER_PROPERTIES( filter ), "end" ) ){
46 sscanf(mlt_properties_get(MLT_FILTER_PROPERTIES( filter ), "end" ), "%f:%f,%fx%f,%f",&smooth_e,&radius_e,&x_e,&y_e,&opac_e);
47 }else{
48 smooth_e=smooth_s;
49 radius_e=radius_s;
50 x_e=x_s;
51 y_e=y_s;
52 opac_e=opac_s;
53 }
54 mlt_position in = mlt_filter_get_in( filter );
55 mlt_position out = mlt_filter_get_out( filter );
56 mlt_position time = mlt_frame_get_position( this );
57 float position = ( double )( time -in ) / ( double )( out - in + 1 ) /100.0;
58
59 float smooth = 2.0*POSITION_VALUE (position, smooth_s, smooth_e) ;
60 float radius = POSITION_VALUE (position, radius_s, radius_e)/100.0* *width;
61
62 double cx = POSITION_VALUE (position, x_s, x_e ) * *width/100;
63 double cy = POSITION_VALUE (position, y_s, y_e ) * *height/100;
64 double opac= POSITION_VALUE(position, opac_s, opac_e) ;
65
66 int video_width = *width;
67 int video_height = *height;
68
69 int x,y;
70 int w2=cx,h2=cy;
71 double delta=1.0;
72 double max_opac=opac/100.0;
73
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 //double sigx=5.0-10.0*(double)(dx-radius+smooth)/(2.0*smooth);//smooth >10 inner area, <-10 in dark area
87 //delta=pow2[((int)((sigx+10.0)*SIGMOD_STEPS/20.0))];//sigmoidal
88 delta = ((double)(radius+smooth-dx)/(2.0*smooth));//linear
89 }
90 delta=MAX(max_opac,delta);
91 *pix=(double)(*pix)*delta;
92 *(pix+1)=((double)(*(pix+1)-127.0)*delta)+127.0;
93 }
94 }
95 // short a, short b, short c, short d
96 // a= gray val pix 1
97 // b: +=blue, -=yellow
98 // c: =gray pix 2
99 // d: +=red,-=green
100 }
101
102 return error;
103 }
104
105 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
106 {
107
108 mlt_frame_push_service( frame, this );
109 mlt_frame_push_get_image( frame, filter_get_image );
110 return frame;
111 }
112
113
114 mlt_filter filter_vignette_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
115 {
116 mlt_filter this = mlt_filter_new( );
117 //int i=0;
118 if ( this != NULL )
119 {
120 /*
121 for (i=-SIGMOD_STEPS/2;i<SIGMOD_STEPS/2;i++){
122 pow2[i+SIGMOD_STEPS/2]=1.0/(1.0+pow(2.0,-((double)i)/((double)SIGMOD_STEPS/20.0)));
123 }
124 */
125
126 this->process = filter_process;
127 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "start", "80:50:50x50:0" );
128 //mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "end", "" );
129
130 }
131 return this;
132 }
133
134