2 * producer_pixbuf.c -- raster image loader based upon gdk-pixbuf
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "producer_pixbuf.h"
22 #include <framework/mlt_frame.h>
26 #include <gdk-pixbuf/gdk-pixbuf.h>
28 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
29 static void producer_close( mlt_producer parent );
31 mlt_producer producer_pixbuf_init( const char *filename )
33 producer_pixbuf this = calloc( sizeof( struct producer_pixbuf_s ), 1 );
34 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
36 mlt_producer producer = &this->parent;
38 producer->get_frame = producer_get_frame;
39 producer->close = producer_close;
41 this->filename = strdup( filename );
51 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
53 // Obtain properties of frame
54 mlt_properties properties = mlt_frame_properties( this );
56 // May need to know the size of the image to clone it
60 uint8_t *image = mlt_properties_get_data( properties, "image", &size );
62 // Get width and height
63 *width = mlt_properties_get_int( properties, "width" );
64 *height = mlt_properties_get_int( properties, "height" );
70 uint8_t *copy = malloc( size );
71 memcpy( copy, image, size );
73 // We're going to pass the copy on
76 // Now update properties so we free the copy after
77 mlt_properties_set_data( properties, "image", copy, size, free, NULL );
86 uint8_t *producer_get_alpha_mask( mlt_frame this )
88 // Obtain properties of frame
89 mlt_properties properties = mlt_frame_properties( this );
91 // Return the alpha mask
92 return mlt_properties_get_data( properties, "alpha", NULL );
95 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
97 producer_pixbuf this = producer->child;
98 GdkPixbuf *pixbuf = NULL;
102 *frame = mlt_frame_init( );
104 // Obtain properties of frame
105 mlt_properties properties = mlt_frame_properties( *frame );
107 // optimization for subsequent iterations on single picture
108 if ( this->image != NULL )
111 mlt_properties_set_int( properties, "width", this->width );
112 mlt_properties_set_int( properties, "height", this->height );
114 // if picture sequence pass the image and alpha data without destructor
115 mlt_properties_set_data( properties, "image", this->image, 0, NULL, NULL );
116 mlt_properties_set_data( properties, "alpha", this->alpha, 0, NULL, NULL );
118 // Set alpha mask call back
119 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
121 // Stack the get image callback
122 mlt_frame_push_get_image( *frame, producer_get_image );
125 else if ( strchr( this->filename, '%' ) != NULL )
127 // handle picture sequences
130 int current = this->counter;
134 snprintf( filename, 1023, this->filename, this->counter );
135 pixbuf = gdk_pixbuf_new_from_file( filename, &error );
136 // allow discontinuity in frame numbers up to 99
138 } while ( pixbuf == NULL && ( this->counter - current ) < 100 );
142 pixbuf = gdk_pixbuf_new_from_file( this->filename, &error );
145 // If we have a pixbuf
148 // Store width and height
149 this->width = gdk_pixbuf_get_width( pixbuf );
150 this->height = gdk_pixbuf_get_height( pixbuf );
152 // Allocate/define image and alpha
153 uint8_t *image = malloc( this->width * this->height * 2 );
154 uint8_t *alpha = NULL;
156 // Extract YUV422 and alpha
157 if ( gdk_pixbuf_get_has_alpha( pixbuf ) )
159 // Allocate the alpha mask
160 alpha = malloc( this->width * this->height );
163 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
164 this->width, this->height,
165 gdk_pixbuf_get_rowstride( pixbuf ),
170 // No alpha to extract
171 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
172 this->width, this->height,
173 gdk_pixbuf_get_rowstride( pixbuf ),
177 // Finished with pixbuf now
178 g_object_unref( pixbuf );
180 // Set width/height of frame
181 mlt_properties_set_int( properties, "width", this->width );
182 mlt_properties_set_int( properties, "height", this->height );
184 // Pass alpha and image on properties with or without destructor
185 if ( this->counter >= 0 )
187 // if picture sequence pass the image and alpha data with destructor
188 mlt_properties_set_data( properties, "image", image, 0, free, NULL );
189 mlt_properties_set_data( properties, "alpha", alpha, 0, free, NULL );
193 // if single picture, reference the image and alpha in the producer
197 // pass the image and alpha data without destructor
198 mlt_properties_set_data( properties, "image", image, 0, NULL, NULL );
199 mlt_properties_set_data( properties, "alpha", alpha, 0, NULL, NULL );
202 // Set alpha call back
203 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
205 // Push the get_image method
206 mlt_frame_push_get_image( *frame, producer_get_image );
209 // Update timecode on the frame we're creating
210 mlt_frame_set_timecode( *frame, mlt_producer_position( producer ) );
212 // Calculate the next timecode
213 mlt_producer_prepare_next( producer );
218 static void producer_close( mlt_producer parent )
220 producer_pixbuf this = parent->child;
221 if ( this->filename )
222 free( this->filename );
227 parent->close = NULL;
228 mlt_producer_close( parent );