Initial revision
[melted] / src / modules / gtk2 / producer_pixbuf.c
1 /*
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>
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 #include "producer_pixbuf.h"
22 #include <framework/mlt_frame.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <gdk-pixbuf/gdk-pixbuf.h>
27
28 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
29 static void producer_close( mlt_producer parent );
30
31 mlt_producer producer_pixbuf_init( const char *filename )
32 {
33 producer_pixbuf this = calloc( sizeof( struct producer_pixbuf_s ), 1 );
34 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
35 {
36 mlt_producer producer = &this->parent;
37
38 producer->get_frame = producer_get_frame;
39 producer->close = producer_close;
40
41 this->filename = strdup( filename );
42 this->counter = -1;
43 g_type_init();
44
45 return producer;
46 }
47 free( this );
48 return NULL;
49 }
50
51 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
52 {
53 // Obtain properties of frame
54 mlt_properties properties = mlt_frame_properties( this );
55
56 // May need to know the size of the image to clone it
57 int size = 0;
58
59 // Get the image
60 uint8_t *image = mlt_properties_get_data( properties, "image", &size );
61
62 // Get width and height
63 *width = mlt_properties_get_int( properties, "width" );
64 *height = mlt_properties_get_int( properties, "height" );
65
66 // Clone if necessary
67 if ( writable )
68 {
69 // Clone our image
70 uint8_t *copy = malloc( size );
71 memcpy( copy, image, size );
72
73 // We're going to pass the copy on
74 image = copy;
75
76 // Now update properties so we free the copy after
77 mlt_properties_set_data( properties, "image", copy, size, free, NULL );
78 }
79
80 // Pass on the image
81 *buffer = image;
82
83 return 0;
84 }
85
86 uint8_t *producer_get_alpha_mask( mlt_frame this )
87 {
88 // Obtain properties of frame
89 mlt_properties properties = mlt_frame_properties( this );
90
91 // Return the alpha mask
92 return mlt_properties_get_data( properties, "alpha", NULL );
93 }
94
95 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
96 {
97 producer_pixbuf this = producer->child;
98 GdkPixbuf *pixbuf = NULL;
99 GError *error = NULL;
100
101 // Generate a frame
102 *frame = mlt_frame_init( );
103
104 // Obtain properties of frame
105 mlt_properties properties = mlt_frame_properties( *frame );
106
107 // optimization for subsequent iterations on single picture
108 if ( this->image != NULL )
109 {
110 // Set width/height
111 mlt_properties_set_int( properties, "width", this->width );
112 mlt_properties_set_int( properties, "height", this->height );
113
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 );
117
118 // Set alpha mask call back
119 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
120
121 // Stack the get image callback
122 mlt_frame_push_get_image( *frame, producer_get_image );
123
124 }
125 else if ( strchr( this->filename, '%' ) != NULL )
126 {
127 // handle picture sequences
128 char filename[1024];
129 filename[1023] = 0;
130 int current = this->counter;
131 do
132 {
133 ++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
137 error = NULL;
138 } while ( pixbuf == NULL && ( this->counter - current ) < 100 );
139 }
140 else
141 {
142 pixbuf = gdk_pixbuf_new_from_file( this->filename, &error );
143 }
144
145 // If we have a pixbuf
146 if ( pixbuf )
147 {
148 // Store width and height
149 this->width = gdk_pixbuf_get_width( pixbuf );
150 this->height = gdk_pixbuf_get_height( pixbuf );
151
152 // Allocate/define image and alpha
153 uint8_t *image = malloc( this->width * this->height * 2 );
154 uint8_t *alpha = NULL;
155
156 // Extract YUV422 and alpha
157 if ( gdk_pixbuf_get_has_alpha( pixbuf ) )
158 {
159 // Allocate the alpha mask
160 alpha = malloc( this->width * this->height );
161
162 // Convert the image
163 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
164 this->width, this->height,
165 gdk_pixbuf_get_rowstride( pixbuf ),
166 image, alpha );
167 }
168 else
169 {
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 ),
174 image );
175 }
176
177 // Finished with pixbuf now
178 g_object_unref( pixbuf );
179
180 // Set width/height of frame
181 mlt_properties_set_int( properties, "width", this->width );
182 mlt_properties_set_int( properties, "height", this->height );
183
184 // Pass alpha and image on properties with or without destructor
185 if ( this->counter >= 0 )
186 {
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 );
190 }
191 else
192 {
193 // if single picture, reference the image and alpha in the producer
194 this->image = image;
195 this->alpha = alpha;
196
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 );
200 }
201
202 // Set alpha call back
203 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
204
205 // Push the get_image method
206 mlt_frame_push_get_image( *frame, producer_get_image );
207 }
208
209 // Update timecode on the frame we're creating
210 mlt_frame_set_timecode( *frame, mlt_producer_position( producer ) );
211
212 // Calculate the next timecode
213 mlt_producer_prepare_next( producer );
214
215 return 0;
216 }
217
218 static void producer_close( mlt_producer parent )
219 {
220 producer_pixbuf this = parent->child;
221 if ( this->filename )
222 free( this->filename );
223 if ( this->image )
224 free( this->image );
225 if ( this->alpha )
226 free( this->alpha );
227 parent->close = NULL;
228 mlt_producer_close( parent );
229 free( this );
230 }
231