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