qimage module: add support for Qt4 (you can force compile against Qt3 with --force...
[melted] / src / modules / qimage / qimage_wrapper.cpp
1 /*
2 * qimage_wrapper.cpp -- a QT/QImage based producer for MLT
3 * Copyright (C) 2006 Visual Media
4 * Author: Charles Yates <charles.yates@gmail.com>
5 *
6 * NB: This module is designed to be functionally equivalent to the
7 * gtk2 image loading module so it can be used as replacement.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24 #include "qimage_wrapper.h"
25
26 #ifdef USE_QT3
27 #include <qimage.h>
28
29 #ifdef USE_KDE
30 #include <kinstance.h>
31 #include <kimageio.h>
32 #endif
33
34 #endif
35
36
37 #ifdef USE_QT4
38 #include <QtGui/QImage>
39 #include <QtCore/QSysInfo>
40 #endif
41
42
43 #include <cmath>
44
45 extern "C" {
46
47 #include <framework/mlt_pool.h>
48
49 #ifdef USE_KDE
50 static KInstance *instance = 0L;
51 #endif
52
53 static void qimage_delete( void *data )
54 {
55 QImage *image = ( QImage * )data;
56 delete image;
57 #ifdef USE_KDE
58 if (instance) delete instance;
59 instance = 0L;
60 #endif
61 }
62
63 static void clear_buffered_image( mlt_properties producer_props, uint8_t **current_image, uint8_t **current_alpha )
64 {
65 mlt_events_block( producer_props, NULL );
66 mlt_properties_set_data( producer_props, "_qimage_image", NULL, 0, NULL, NULL );
67 mlt_properties_set_data( producer_props, "_qimage_alpha", NULL, 0, NULL, NULL );
68 *current_image = NULL;
69 *current_alpha = NULL;
70 mlt_events_unblock( producer_props, NULL );
71 }
72
73 static void assign_buffered_image( mlt_properties producer_props, uint8_t *current_image, uint8_t *current_alpha, int width, int height )
74 {
75 int use_cache = mlt_properties_get_int( producer_props, "cache" );
76 mlt_destructor destructor = use_cache ? NULL : mlt_pool_release;
77 mlt_events_block( producer_props, NULL );
78 mlt_properties_set_data( producer_props, "_qimage_image", current_image, 0, destructor, NULL );
79 mlt_properties_set_data( producer_props, "_qimage_alpha", current_alpha, 0, destructor, NULL );
80 mlt_properties_set_int( producer_props, "_qimage_width", width );
81 mlt_properties_set_int( producer_props, "_qimage_height", height );
82 mlt_events_unblock( producer_props, NULL );
83 }
84
85 #ifdef USE_KDE
86 void init_qimage()
87 {
88 if (!instance) {
89 instance = new KInstance("qimage_prod");
90 KImageIO::registerFormats();
91 }
92 }
93 #endif
94
95 void refresh_qimage( mlt_frame frame, int width, int height )
96 {
97 // Obtain a previous assigned qimage (if it exists)
98 QImage *qimage = ( QImage * )mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "qimage", NULL );
99
100 // Obtain properties of frame
101 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
102
103 // Obtain the producer for this frame
104 producer_qimage self = ( producer_qimage )mlt_properties_get_data( properties, "producer_qimage", NULL );
105
106 // Obtain the producer
107 mlt_producer producer = &self->parent;
108
109 // Obtain properties of producer
110 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
111
112 // Obtain the cache flag and structure
113 int use_cache = mlt_properties_get_int( producer_props, "cache" );
114 mlt_properties cache = ( mlt_properties )mlt_properties_get_data( producer_props, "_cache", NULL );
115 int update_cache = 0;
116
117 // Retrieve current info if available
118 uint8_t *current_image = ( uint8_t * )mlt_properties_get_data( producer_props, "_qimage_image", NULL );
119 uint8_t *current_alpha = ( uint8_t * )mlt_properties_get_data( producer_props, "_qimage_alpha", NULL );
120 int current_width = mlt_properties_get_int( producer_props, "_qimage_width" );
121 int current_height = mlt_properties_get_int( producer_props, "_qimage_height" );
122
123 // Get the time to live for each frame
124 double ttl = mlt_properties_get_int( producer_props, "ttl" );
125
126 // Get the original position of this frame
127 mlt_position position = mlt_properties_get_position( properties, "qimage_position" );
128 position += mlt_producer_get_in( producer );
129
130 // Image index
131 int image_idx = ( int )floor( ( double )position / ttl ) % self->count;
132
133 // Key for the cache
134 char image_key[ 10 ];
135 sprintf( image_key, "%d", image_idx );
136
137 // Check if the frame is already loaded
138 if ( use_cache )
139 {
140 if ( cache == NULL )
141 {
142 cache = mlt_properties_new( );
143 mlt_properties_set_data( producer_props, "_cache", cache, 0, ( mlt_destructor )mlt_properties_close, NULL );
144 }
145
146 mlt_frame cached = ( mlt_frame )mlt_properties_get_data( cache, image_key, NULL );
147
148 if ( cached )
149 {
150 self->image_idx = image_idx;
151 mlt_properties cached_props = MLT_FRAME_PROPERTIES( cached );
152 current_width = mlt_properties_get_int( cached_props, "width" );
153 current_height = mlt_properties_get_int( cached_props, "height" );
154 mlt_properties_set_int( producer_props, "_real_width", mlt_properties_get_int( cached_props, "real_width" ) );
155 mlt_properties_set_int( producer_props, "_real_height", mlt_properties_get_int( cached_props, "real_height" ) );
156 current_image = ( uint8_t * )mlt_properties_get_data( cached_props, "image", NULL );
157 current_alpha = ( uint8_t * )mlt_properties_get_data( cached_props, "alpha", NULL );
158
159 if ( width != 0 && ( width != current_width || height != current_height ) )
160 current_image = NULL;
161
162 assign_buffered_image( producer_props, current_image, current_alpha, current_width, current_height );
163 }
164 }
165
166 // optimization for subsequent iterations on single picture
167 if ( width != 0 && current_image != NULL && image_idx == self->image_idx )
168 {
169 if ( width != current_width || height != current_height )
170 {
171 qimage = ( QImage * )mlt_properties_get_data( producer_props, "_qimage", NULL );
172 clear_buffered_image( producer_props, &current_image, &current_alpha );
173 }
174 }
175 else if ( qimage == NULL && ( current_image == NULL || image_idx != self->image_idx ) )
176 {
177 clear_buffered_image( producer_props, &current_image, &current_alpha );
178
179 self->image_idx = image_idx;
180 qimage = new QImage( mlt_properties_get_value( self->filenames, image_idx ) );
181
182 if ( !qimage->isNull( ) )
183 {
184 QImage *frame_copy = new QImage( *qimage );
185
186 // Store the width/height of the pixbuf
187 current_width = qimage->width( );
188 current_height = qimage->height( );
189
190 // Register qimage for destruction and reuse
191 mlt_events_block( producer_props, NULL );
192 mlt_properties_set_data( producer_props, "_qimage", qimage, 0, ( mlt_destructor )qimage_delete, NULL );
193 mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame ), "qimage", frame_copy, 0, ( mlt_destructor )qimage_delete, NULL );
194 mlt_properties_set_int( producer_props, "_real_width", current_width );
195 mlt_properties_set_int( producer_props, "_real_height", current_height );
196 mlt_events_unblock( producer_props, NULL );
197 }
198 else
199 {
200 delete qimage;
201 qimage = NULL;
202 }
203 }
204
205 // If we have a pixbuf and this request specifies a valid dimension and we haven't already got a cached version...
206 if ( qimage && width > 0 && current_image == NULL )
207 {
208 char *interps = mlt_properties_get( properties, "rescale.interp" );
209 int interp = 0;
210
211 // QImage has two scaling modes - we'll toggle between them here
212 if ( strcmp( interps, "tiles" ) == 0 )
213 interp = 1;
214 else if ( strcmp( interps, "hyper" ) == 0 )
215 interp = 1;
216
217 #ifdef USE_QT4
218 // Note - the original qimage is already safe and ready for destruction
219 QImage scaled = interp == 0 ? qimage->scaled( QSize( width, height)) : qimage->scaled( QSize(width, height), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
220 QImage temp = scaled.convertToFormat(QImage::Format_ARGB32);
221 #endif
222
223 #ifdef USE_QT3
224 // Note - the original qimage is already safe and ready for destruction
225 QImage scaled = interp == 0 ? qimage->scale( width, height, QImage::ScaleFree ) : qimage->smoothScale( width, height, QImage::ScaleFree );
226 QImage temp = scaled.convertDepth( 32 );
227 #endif
228
229 // Store width and height
230 current_width = width;
231 current_height = height;
232
233 // Allocate/define image
234 current_image = ( uint8_t * )mlt_pool_alloc( width * ( height + 1 ) * 2 );
235
236 // Allocate the alpha mask
237 current_alpha = ( uint8_t * )mlt_pool_alloc( current_width * current_height );
238
239 #ifdef USE_QT4
240 if ( QSysInfo::ByteOrder == QSysInfo::BigEndian )
241 mlt_convert_argb_to_yuv422( temp.bits( ), current_width, current_height, temp.bytesPerLine( ), current_image, current_alpha );
242 else
243 mlt_convert_bgr24a_to_yuv422( temp.bits( ), current_width, current_height, temp.bytesPerLine( ), current_image, current_alpha );
244 #endif
245
246 #ifdef USE_QT3
247 // Convert the image
248 if ( QImage::systemByteOrder( ) == QImage::BigEndian )
249 mlt_convert_argb_to_yuv422( temp.bits( ), current_width, current_height, temp.bytesPerLine( ), current_image, current_alpha );
250 else
251 mlt_convert_bgr24a_to_yuv422( temp.bits( ), current_width, current_height, temp.bytesPerLine( ), current_image, current_alpha );
252 #endif
253 assign_buffered_image( producer_props, current_image, current_alpha, current_width, current_height );
254
255 // Ensure we update the cache when we need to
256 update_cache = use_cache;
257 }
258
259 // Set width/height of frame
260 mlt_properties_set_int( properties, "width", current_width );
261 mlt_properties_set_int( properties, "height", current_height );
262 mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
263 mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
264
265 // pass the image data without destructor
266 mlt_properties_set_data( properties, "image", current_image, current_width * ( current_height + 1 ) * 2, NULL, NULL );
267 mlt_properties_set_data( properties, "alpha", current_alpha, current_width * current_height, NULL, NULL );
268
269 if ( update_cache )
270 {
271 mlt_frame cached = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
272 mlt_properties cached_props = MLT_FRAME_PROPERTIES( cached );
273 mlt_properties_set_int( cached_props, "width", current_width );
274 mlt_properties_set_int( cached_props, "height", current_height );
275 mlt_properties_set_int( cached_props, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
276 mlt_properties_set_int( cached_props, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
277 mlt_properties_set_data( cached_props, "image", current_image, current_width * ( current_height + 1 ) * 2, mlt_pool_release, NULL );
278 mlt_properties_set_data( cached_props, "alpha", current_alpha, current_width * current_height, mlt_pool_release, NULL );
279 mlt_properties_set_data( cache, image_key, cached, 0, ( mlt_destructor )mlt_frame_close, NULL );
280 }
281 }
282
283 }
284