2 * producer_sdl_image.c -- Image loader which wraps SDL_image
3 * Copyright (C) 2005 Visual Media FX
4 * Author: Charles Yates <charles.yates@gmail.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <framework/mlt_producer.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_pool.h>
28 #include <sys/types.h>
34 #include <SDL_image.h>
36 static int producer_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
38 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
39 SDL_Surface *surface = mlt_properties_get_data( properties, "surface", NULL );
40 SDL_Surface *converted = NULL;
44 int image_size = *width * *height * 3;
46 if ( surface->format->BitsPerPixel != 32 && surface->format->BitsPerPixel != 24 )
49 fmt.BitsPerPixel = 24;
50 fmt.BytesPerPixel = 3;
54 fmt.Rmask = 0xff << 16;
55 fmt.Gmask = 0xff << 8;
57 converted = SDL_ConvertSurface( surface, &fmt, 0 );
60 switch( surface->format->BitsPerPixel )
63 *format = mlt_image_rgb24a;
64 image_size = *width * *height * 4;
65 *image = mlt_pool_alloc( image_size );
66 memcpy( *image, surface->pixels, image_size );
69 *format = mlt_image_rgb24;
70 *image = mlt_pool_alloc( image_size );
71 memcpy( *image, surface->pixels, image_size );
74 *image = mlt_pool_alloc( image_size );
75 memcpy( *image, converted->pixels, image_size );
80 SDL_FreeSurface( converted );
83 mlt_properties_set_data( properties, "image", *image, image_size, mlt_pool_release, NULL );
84 mlt_properties_set_int( properties, "width", *width );
85 mlt_properties_set_int( properties, "height", *height );
90 static int filter_files( const struct dirent *de )
92 return de->d_name[ 0 ] != '.';
95 static mlt_properties parse_file_names( char *resource )
97 mlt_properties properties = mlt_properties_new( );
99 if ( strstr( resource, "/.all." ) != NULL )
101 char *dir_name = strdup( resource );
102 char *extension = strrchr( resource, '.' );
103 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
104 char fullname[ 1024 ];
105 strcpy( fullname, dir_name );
106 struct dirent **de = NULL;
107 int n = scandir( fullname, &de, filter_files, alphasort );
111 for (i = 0; i < n; i++ )
113 snprintf( fullname, 1023, "%s%s", dir_name, de[i]->d_name );
114 if ( strstr( fullname, extension ) && lstat( fullname, &info ) == 0 &&
115 ( S_ISREG( info.st_mode ) || info.st_mode | S_IXUSR ) )
118 sprintf( temp, "%d", i );
119 mlt_properties_set( properties, temp, fullname );
129 mlt_properties_set( properties, "0", resource );
135 static SDL_Surface *load_image( mlt_producer producer )
137 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
138 char *resource = mlt_properties_get( properties, "resource" );
139 char *last_resource = mlt_properties_get( properties, "_last_resource" );
141 char *this_resource = NULL;
142 double ttl = mlt_properties_get_int( properties, "ttl" );
143 mlt_position position = mlt_producer_position( producer );
144 SDL_Surface *surface = mlt_properties_get_data( properties, "_surface", NULL );
145 mlt_properties filenames = mlt_properties_get_data( properties, "_filenames", NULL );
147 if ( filenames == NULL )
149 filenames = parse_file_names( resource );
150 mlt_properties_set_data( properties, "_surface", surface, 0, ( mlt_destructor )SDL_FreeSurface, 0 );
153 if ( mlt_properties_count( filenames ) )
155 image_idx = ( int )floor( ( double )position / ttl ) % mlt_properties_count( filenames );
156 this_resource = mlt_properties_get_value( filenames, image_idx );
158 if ( surface == NULL || last_resource == NULL || strcmp( last_resource, this_resource ) )
160 surface = IMG_Load( this_resource );
161 if ( surface != NULL )
163 surface->refcount ++;
164 mlt_properties_set_data( properties, "_surface", surface, 0, ( mlt_destructor )SDL_FreeSurface, 0 );
165 mlt_properties_set( properties, "_last_resource", this_resource );
166 mlt_properties_set_int( properties, "_real_width", surface->w );
167 mlt_properties_set_int( properties, "_real_height", surface->h );
170 else if ( surface != NULL )
172 surface->refcount ++;
179 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
182 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
184 if ( *frame != NULL )
186 // Create the surface for the current image
187 SDL_Surface *surface = load_image( producer );
189 if ( surface != NULL )
191 // Obtain properties of frame and producer
192 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
194 // Obtain properties of producer
195 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
197 // Update timecode on the frame we're creating
198 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
200 // Set producer-specific frame properties
201 mlt_properties_set_int( properties, "progressive", 1 );
202 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_props, "aspect_ratio" ) );
203 mlt_properties_set_data( properties, "surface", surface, 0, ( mlt_destructor )SDL_FreeSurface, NULL );
204 mlt_properties_set_int( properties, "real_width", surface->w );
205 mlt_properties_set_int( properties, "real_height", surface->h );
207 // Push the get_image method
208 mlt_frame_push_get_image( *frame, producer_get_image );
212 // Calculate the next timecode
213 mlt_producer_prepare_next( producer );
218 static void producer_close( mlt_producer producer )
220 producer->close = NULL;
221 mlt_producer_close( producer );
225 mlt_producer producer_sdl_image_init( mlt_profile profile, mlt_service_type type, const char *id, char *file )
227 mlt_producer producer = calloc( 1, sizeof( struct mlt_producer_s ) );
228 if ( producer != NULL && mlt_producer_init( producer, NULL ) == 0 )
230 // Get the properties interface
231 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
233 // Callback registration
234 producer->get_frame = producer_get_frame;
235 producer->close = ( mlt_destructor )producer_close;
237 // Set the default properties
238 mlt_properties_set( properties, "resource", file );
239 mlt_properties_set( properties, "_resource", "" );
240 mlt_properties_set_double( properties, "aspect_ratio", 1 );
241 mlt_properties_set_int( properties, "ttl", 25 );
242 mlt_properties_set_int( properties, "progressive", 1 );
244 // Validate the resource
245 SDL_Surface *surface = NULL;
246 if ( file && ( surface = load_image( producer ) ) )
248 SDL_FreeSurface( surface );
249 mlt_properties_set_data( properties, "_surface", NULL, 0, NULL, NULL );
253 producer_close( producer );