]> git.sesse.net Git - mlt/blob - src/modules/oldfilm/filter_lines.c
d4a9bdfc79347d2510d6560bd07333b215d699db
[mlt] / src / modules / oldfilm / filter_lines.c
1 /*
2  * filter_lines.c -- lines 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
27 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
28 {
29
30         mlt_filter filter = mlt_frame_pop_service( this );
31         *format = mlt_image_yuv422;
32         int error = mlt_frame_get_image( this, image, format, width, height, 1 );
33
34         if ( error == 0 && *image )
35         {
36                 int h = *height;
37                 int w = *width;
38
39                 int width_line = mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "width" );
40                 int num = mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "num" );
41                 double maxdarker= (double)mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "darker" ) ;
42                 double maxlighter=(double)mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "lighter" ) ;
43                 //int frame = mlt_properties_get_int( this, "_position" );
44                 char buf[256];
45                 char typebuf[256];
46                 
47                 mlt_position in = mlt_filter_get_in( filter );
48                 mlt_position out = mlt_filter_get_out( filter );
49                 mlt_position time = mlt_frame_get_position( this );
50                 double position = ( double )( time - in ) / ( double )( out - in + 1 );
51                 srand(position*10000);
52                 if (!width_line)
53                         return 0;
54                 
55                 mlt_service_lock( MLT_FILTER_SERVICE( filter ) );
56
57                 while (num--){
58                         int type=(rand()%3)+1;
59                         int x1=(double)w*rand()/RAND_MAX;
60                         int dx=rand()%width_line;
61                         int x=0,y=0;
62                         int ystart=rand()%h;
63                         int yend=rand()%h;
64                         
65                         sprintf(buf,"line%d",num);
66                         sprintf(typebuf,"typeline%d",num);
67                         maxlighter+=rand()%30-15;
68                         maxdarker+=rand()%30-15;
69                         
70                         if (mlt_properties_get_int(MLT_FILTER_PROPERTIES( filter ),buf)==0){
71                                 mlt_properties_set_int(MLT_FILTER_PROPERTIES( filter ),buf,x1);
72                         }
73                         
74                         if (mlt_properties_get_int(MLT_FILTER_PROPERTIES( filter ),typebuf)==0 ){
75                                 mlt_properties_set_int(MLT_FILTER_PROPERTIES( filter ),typebuf,type);
76                         }
77
78                         
79                         x1=mlt_properties_get_int(MLT_FILTER_PROPERTIES( filter ),buf);
80                         type=mlt_properties_get_int(MLT_FILTER_PROPERTIES( filter ),typebuf);
81                         if (position!=mlt_properties_get_double(MLT_FILTER_PROPERTIES( filter ),"last_oldfilm_line_pos")){
82                                 x1+=(rand()%11-5);
83                         }
84                 
85                         if (yend<ystart){
86                                 yend=h;
87                         }
88                         
89                         for (x = -dx ; x < dx && dx != 0 ; x++ )
90                                 for(y=ystart;y<yend;y++)
91                                         if (x+x1<w && x+x1>0){
92                                                 uint8_t* pixel=(*image+(y)*w*2+(x+x1)*2);
93                                                 double diff=1.0-fabs(x)/dx;
94                                                 switch(type){
95                                                         case 1: //blackline
96                                                                 *pixel-=((double)*pixel*diff*maxdarker/100.0);
97                                                                 break;
98                                                         case 2: //whiteline
99                                                                 *pixel+=((255.0-(double)*pixel)*diff*maxlighter/100.0);
100                                                                 break;
101                                                         case 3: //greenline
102                                                                 *(pixel+1)-=((*(pixel+1))*diff*maxlighter/100.0);
103                                                         break;
104                                                 }
105                                                         
106                                 }
107                         mlt_properties_set_int(MLT_FILTER_PROPERTIES( filter ),buf,x1);
108                 }
109                 mlt_properties_set_double(MLT_FILTER_PROPERTIES( filter ),"last_oldfilm_line_pos",position);
110                 mlt_service_unlock( MLT_FILTER_SERVICE( filter ) );
111         }
112
113         return error;
114 }
115
116 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
117 {
118
119         mlt_frame_push_service( frame, this );
120         mlt_frame_push_get_image( frame, filter_get_image );
121         return frame;
122 }
123
124 mlt_filter filter_lines_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
125 {
126         mlt_filter this = mlt_filter_new( );
127         if ( this != NULL )
128         {
129                 this->process = filter_process;
130                 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "width", "2" );
131                 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "num", "5" );
132                 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "darker" , 40 ) ;
133                 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "lighter" , 40 ) ;
134         }
135         return this;
136 }
137
138