]> git.sesse.net Git - mlt/blob - src/modules/oldfilm/filter_dust.c
d48ad87874779f49e3b9a2060f70f33eb5225d62
[mlt] / src / modules / oldfilm / filter_dust.c
1 /*
2  * filter_dust.c -- dust 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.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <math.h>
27 #include <string.h>
28 #include <time.h>
29
30 static void overlay_image(uint8_t *src, int src_width, int src_height , uint8_t * overlay, int overlay_width, int overlay_height, uint8_t * alpha , int xpos, int ypos, int upsidedown , int mirror ){
31         int x,y;
32
33         for (y=ypos;y<src_height ; y++){
34                 if ( y>=0 && (y-ypos)<overlay_height ){
35                         uint8_t *scanline_image=src+src_width*y*2;
36                         int overlay_y = upsidedown ?  ( overlay_height - ( y - ypos ) - 1 ) : ( y - ypos  );
37                         uint8_t *scanline_overlay=overlay + overlay_width * 2 * overlay_y;
38                         
39                         for ( x = xpos  ;  x < src_width  && x-xpos < overlay_width ;x++){
40                                 if ( x>0  ){
41                                         int overlay_x = mirror ? overlay_width - ( x - xpos ) -1 : ( x - xpos );
42                                         double alp=(double)*(alpha+ overlay_width * overlay_y + overlay_x )/255.0;
43                                         uint8_t* image_pixel = scanline_image + x * 2;
44                                         uint8_t* overlay_pixel = scanline_overlay + overlay_x * 2;
45                                         
46                                         *image_pixel=(double)(*overlay_pixel)*alp+ (double)*image_pixel*(1.0-alp) ;
47                                         if (xpos%2==0)
48                                                 image_pixel++;
49                                         else
50                                                 image_pixel+=3;
51
52                                         mirror? overlay_pixel-- : overlay_pixel++;
53
54                                         *image_pixel=(double)(*(overlay_pixel))*alp + (double)(*image_pixel )*(1.0-alp) ;
55                                 }
56                 
57                         }
58                 }
59         }
60 }
61
62 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
63 {
64         mlt_filter filter = mlt_frame_pop_service( this );
65         
66         int maxdia = mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "maxdiameter" );
67         int maxcount = mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "maxcount" );
68
69         mlt_position in = mlt_filter_get_in( filter );
70         mlt_position out = mlt_filter_get_out( filter );
71         mlt_position time = mlt_frame_get_position( this );
72         double position = ( double )( time - in ) / ( double )( out - in + 1 );
73         
74         *format = mlt_image_yuv422;
75         int error = mlt_frame_get_image( this, image, format, width, height, 1 );       
76         // load svg
77         mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
78         char *factory = mlt_properties_get( properties, "factory" );
79         char temp[1204]="";
80         sprintf( temp, "%s/oldfilm/", mlt_environment( "MLT_DATA" ) );
81         
82         mlt_properties direntries=mlt_properties_new();
83         mlt_properties_dir_list(direntries,temp,"dust*.svg",1);
84         
85         if (!maxcount)
86                 return 0;
87         srand(position*10000);
88
89         mlt_service_lock( MLT_FILTER_SERVICE( filter ) );
90
91         int im=rand()%maxcount;
92         int piccount=mlt_properties_count(direntries);
93         while (im-- && piccount){
94         
95                 int picnum=rand()%piccount;
96                 
97                 int y1=rand()%*height;
98                 int x1=rand()%*width;
99                 char resource[1024]="";
100                 char savename[1024]="",savename1[1024]="", cachedy[100];
101                 int dx=(*width*maxdia/100);
102                 int luma_width,luma_height;
103                 uint8_t *luma_image = NULL;
104                 uint8_t *alpha =NULL;
105                 int updown= rand()%2;
106                 int mirror=rand()%2;
107                 
108                 sprintf(resource,"%s",mlt_properties_get_value(direntries,picnum));
109                 sprintf(savename,"cache-%d-%d",picnum,dx);
110                 sprintf(savename1,"cache-alpha-%d-%d",picnum,dx);
111                 sprintf(cachedy,"cache-dy-%d-%d",picnum,dx);
112                 
113                 luma_image= mlt_properties_get_data( properties , savename , NULL );
114                 alpha= mlt_properties_get_data( properties , savename1 , NULL );
115                 
116                 if (luma_image == NULL || alpha == NULL ){
117                         mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( filter ) );
118                         mlt_producer producer = mlt_factory_producer( profile, factory, resource );
119                 
120                         if ( producer != NULL )
121                         {
122                                 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
123                                 
124                                 mlt_properties_set( producer_properties, "eof", "loop" );
125                                 mlt_frame luma_frame = NULL;
126                                 
127                                 if ( mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &luma_frame, 0 ) == 0 ){
128                 
129                                         mlt_properties_set_double ( MLT_FRAME_PROPERTIES ( luma_frame ) , "consumer_aspect_ratio" , 1.0 );
130                                         mlt_image_format luma_format = mlt_image_yuv422;
131                                         luma_width = dx;
132                                         luma_height = luma_width * mlt_properties_get_int( MLT_FRAME_PROPERTIES ( luma_frame ) , "height" ) / mlt_properties_get_int( MLT_FRAME_PROPERTIES ( luma_frame ) , "width" );
133
134                                         mlt_properties_set( MLT_FRAME_PROPERTIES( luma_frame ), "rescale.interp", "best" );// none/nearest/tiles/hyper
135                                         
136                                         mlt_frame_get_image( luma_frame, &luma_image, &luma_format, &luma_width, &luma_height, 0 );
137                                         alpha =mlt_frame_get_alpha_mask(luma_frame);
138                                         
139                                         uint8_t* savealpha=mlt_pool_alloc ( luma_width * luma_height );
140                                         uint8_t* savepic=mlt_pool_alloc ( luma_width * luma_height * 2);
141                                         
142                                         if (savealpha && savepic ){
143                                                 memcpy (savealpha, alpha , luma_width * luma_height );
144                                                 memcpy (savepic, luma_image , luma_width * luma_height * 2);
145                                                 
146                                                 mlt_properties_set_data ( properties , savename , savepic , sizeof(uint8_t*) , mlt_pool_release, NULL );
147                                                 mlt_properties_set_data ( properties , savename1 , savealpha , sizeof(uint8_t*)  ,  mlt_pool_release, NULL );
148                                                 mlt_properties_set_int ( properties , cachedy , luma_height );
149                                                 
150                                                 overlay_image(*image,*width,*height,luma_image,luma_width,luma_height, alpha, x1, y1 , updown , mirror );
151                                         }
152                                         mlt_frame_close( luma_frame );  
153                                 }
154                                 mlt_producer_close( producer ); 
155                         }
156                 }else {
157                         overlay_image ( *image , *width, *height , luma_image , dx , mlt_properties_get_int ( properties , cachedy ) , alpha , x1 , y1 , updown , mirror );
158                 }
159         }
160
161         mlt_service_unlock( MLT_FILTER_SERVICE( filter ) );
162
163         if (piccount>0 )
164                 return 0;
165         if ( error == 0 && *image )
166         {
167
168                 int h = *height;
169                 int w = *width;
170                 if (maxcount==0)
171                         return 0;
172
173                 int im=rand()%maxcount;
174                 
175                 while (im-- ){
176                         int type=im%2;
177                         int y1=rand()%h;
178                         int x1=rand()%w;
179                         int dx=rand()%maxdia;
180                         int dy=rand()%maxdia;
181                         int x=0,y=0;//,v=0;
182                         double v=0.0;
183                         for ( x = -dx ; x < dx ; x++ )
184                                 for ( y = -dy ; y < dy ; y++ ) {
185                                         if ( x1+x < w && x1+x > 0 && y1+y < h && y1+y > 0 ){
186                                                 uint8_t *pix=*image+(y+y1)*w*2+(x+x1)*2;
187                                                 //v=(1.0-fabs(x)/dx)*(1.0-fabs(y)/dy);
188                                                 v=pow((double)x/(double)dx*5.0,2.0)+pow((double)y/(double)dy*5.0,2.0);
189                                                 if (v>10)
190                                                         v=10;
191                                                 v=1.0-(v/10.0);
192
193                                                 switch(type){
194                                                         case 0:
195                                                                 *pix-=(*pix)*v;
196                                                                 break;
197                                                         case 1:
198                                                                 *pix+=(255-*pix)*v;
199                                                                 break;
200                                                 }
201                                         }
202                                 }
203                 }
204         }
205
206         return error;
207 }
208
209 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
210 {
211
212         mlt_frame_push_service( frame, this );
213         mlt_frame_push_get_image( frame, filter_get_image );
214         return frame;
215 }
216
217
218 mlt_filter filter_dust_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
219 {
220         mlt_filter this = mlt_filter_new( );
221         if ( this != NULL )
222         {
223                 this->process = filter_process;
224                 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "maxdiameter", "2" );
225                 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "maxcount", "10" );
226         }
227         return this;
228 }
229
230