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