+ QImage module added - default is still GTK2 when available
[melted] / src / modules / qimage / producer_qimage.c
1 /*
2 * producer_image.c -- a QT/QImage based producer for MLT
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
5 * Charles Yates <charles.yates@gmail.com>
6 *
7 * NB: This module is designed to be functionally equivalent to the
8 * gtk2 image loading module so it can be used as replacement.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24
25 #include "producer_qimage.h"
26 #include "qimage_wrapper.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <math.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <unistd.h>
35
36 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
37 static void producer_close( mlt_producer parent );
38
39 mlt_producer producer_qimage_init( char *filename )
40 {
41 producer_qimage this = calloc( sizeof( struct producer_qimage_s ), 1 );
42 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
43 {
44 mlt_producer producer = &this->parent;
45
46 // Get the properties interface
47 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->parent );
48
49 // Callback registration
50 producer->get_frame = producer_get_frame;
51 producer->close = ( mlt_destructor )producer_close;
52
53 // Set the default properties
54 mlt_properties_set( properties, "resource", filename );
55 mlt_properties_set_int( properties, "ttl", 25 );
56 mlt_properties_set_int( properties, "aspect_ratio", 1 );
57 mlt_properties_set_int( properties, "progressive", 1 );
58
59 return producer;
60 }
61 free( this );
62 return NULL;
63 }
64
65 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
66 {
67 // Obtain properties of frame
68 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
69
70 // We need to know the size of the image to clone it
71 int image_size = 0;
72 int alpha_size = 0;
73
74 // Alpha channel
75 uint8_t *alpha = NULL;
76
77 *width = mlt_properties_get_int( properties, "rescale_width" );
78 *height = mlt_properties_get_int( properties, "rescale_height" );
79
80 // Refresh the image
81 refresh_qimage( frame, *width, *height );
82
83 // Get the image
84 *buffer = mlt_properties_get_data( properties, "image", &image_size );
85 alpha = mlt_properties_get_data( properties, "alpha", &alpha_size );
86
87 // Get width and height (may have changed during the refresh)
88 *width = mlt_properties_get_int( properties, "width" );
89 *height = mlt_properties_get_int( properties, "height" );
90
91 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
92 // The fault is not in the design of mlt, but in the implementation of the qimage producer...
93 if ( *buffer != NULL )
94 {
95 // Clone the image and the alpha
96 uint8_t *image_copy = mlt_pool_alloc( image_size );
97 uint8_t *alpha_copy = mlt_pool_alloc( alpha_size );
98
99 memcpy( image_copy, *buffer, image_size );
100
101 // Copy or default the alpha
102 if ( alpha != NULL )
103 memcpy( alpha_copy, alpha, alpha_size );
104 else
105 memset( alpha_copy, 255, alpha_size );
106
107 // Now update properties so we free the copy after
108 mlt_properties_set_data( properties, "image", image_copy, image_size, mlt_pool_release, NULL );
109 mlt_properties_set_data( properties, "alpha", alpha_copy, alpha_size, mlt_pool_release, NULL );
110
111 // We're going to pass the copy on
112 *buffer = image_copy;
113 }
114 else
115 {
116 // TODO: Review all cases of invalid images
117 *buffer = mlt_pool_alloc( 50 * 50 * 2 );
118 mlt_properties_set_data( properties, "image", *buffer, image_size, mlt_pool_release, NULL );
119 *width = 50;
120 *height = 50;
121 }
122
123 return 0;
124 }
125
126 static uint8_t *producer_get_alpha_mask( mlt_frame this )
127 {
128 // Obtain properties of frame
129 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
130
131 // Return the alpha mask
132 return mlt_properties_get_data( properties, "alpha", NULL );
133 }
134
135 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
136 {
137 // Get the real structure for this producer
138 producer_qimage this = producer->child;
139
140 // Fetch the producers properties
141 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
142
143 if ( this->filenames == NULL && mlt_properties_get( producer_properties, "resource" ) != NULL )
144 {
145 char *filename = mlt_properties_get( producer_properties, "resource" );
146 this->filenames = mlt_properties_new( );
147
148 // Read xml string
149 if ( strstr( filename, "<svg" ) )
150 {
151 // Generate a temporary file for the svg
152 char fullname[ 1024 ] = "/tmp/mlt.XXXXXX";
153 int fd = mkstemp( fullname );
154
155 if ( fd > -1 )
156 {
157 // Write the svg into the temp file
158 ssize_t remaining_bytes;
159 char *xml = filename;
160
161 // Strip leading crap
162 while ( xml[0] != '<' )
163 xml++;
164
165 remaining_bytes = strlen( xml );
166 while ( remaining_bytes > 0 )
167 remaining_bytes -= write( fd, xml + strlen( xml ) - remaining_bytes, remaining_bytes );
168 close( fd );
169
170 mlt_properties_set( this->filenames, "0", fullname );
171
172 // Teehe - when the producer closes, delete the temp file and the space allo
173 mlt_properties_set_data( producer_properties, "__temporary_file__", fullname, 0, ( mlt_destructor )unlink, NULL );
174 }
175 }
176 // Obtain filenames
177 else if ( strchr( filename, '%' ) != NULL )
178 {
179 // handle picture sequences
180 int i = mlt_properties_get_int( producer_properties, "begin" );
181 int gap = 0;
182 char full[1024];
183 int keyvalue = 0;
184 char key[ 50 ];
185
186 while ( gap < 100 )
187 {
188 struct stat buf;
189 snprintf( full, 1023, filename, i ++ );
190 if ( stat( full, &buf ) == 0 )
191 {
192 sprintf( key, "%d", keyvalue ++ );
193 mlt_properties_set( this->filenames, "0", full );
194 gap = 0;
195 }
196 else
197 {
198 gap ++;
199 }
200 }
201 }
202 else if ( strstr( filename, "/.all." ) != NULL )
203 {
204 char wildcard[ 1024 ];
205 char *dir_name = strdup( filename );
206 char *extension = strrchr( dir_name, '.' );
207
208 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
209 sprintf( wildcard, "*%s", extension );
210
211 mlt_properties_dir_list( this->filenames, dir_name, wildcard, 1 );
212
213 free( dir_name );
214 }
215 else
216 {
217 mlt_properties_set( this->filenames, "0", filename );
218 }
219
220 this->count = mlt_properties_count( this->filenames );
221 }
222
223 // Generate a frame
224 *frame = mlt_frame_init( );
225
226 if ( *frame != NULL && this->count > 0 )
227 {
228 // Obtain properties of frame and producer
229 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
230
231 // Set the producer on the frame properties
232 mlt_properties_set_data( properties, "producer_qimage", this, 0, NULL, NULL );
233
234 // Update timecode on the frame we're creating
235 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
236
237 // Ensure that we have a way to obtain the position in the get_image
238 mlt_properties_set_position( properties, "qimage_position", mlt_producer_position( producer ) );
239
240 // Refresh the image
241 refresh_qimage( *frame, 0, 0 );
242
243 // Set producer-specific frame properties
244 mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( producer_properties, "progressive" ) );
245 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_properties, "aspect_ratio" ) );
246
247 // Set alpha call back
248 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
249
250 // Push the get_image method
251 mlt_frame_push_get_image( *frame, producer_get_image );
252 }
253
254 // Calculate the next timecode
255 mlt_producer_prepare_next( producer );
256
257 return 0;
258 }
259
260 static void producer_close( mlt_producer parent )
261 {
262 producer_qimage this = parent->child;
263 parent->close = NULL;
264 mlt_producer_close( parent );
265 mlt_properties_close( this->filenames );
266 free( this );
267 }