]> git.sesse.net Git - mlt/blob - src/modules/qimage/qimage_wrapper.cpp
producer_pixbuf.c, qimage_wrapper.c: Add "force_reload" option to force image reloadi...
[mlt] / 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 #include <qmutex.h>
29
30 #ifdef USE_KDE
31 #include <kinstance.h>
32 #include <kimageio.h>
33 #endif
34
35 #endif
36
37
38 #ifdef USE_QT4
39 #include <QtGui/QImage>
40 #include <QtCore/QSysInfo>
41 #include <QtCore/QMutex>
42 #endif
43
44
45 #include <cmath>
46
47 extern "C" {
48
49 #include <framework/mlt_pool.h>
50
51 #ifdef USE_KDE
52 static KInstance *instance = 0L;
53 #endif
54
55 static void qimage_delete( void *data )
56 {
57         QImage *image = ( QImage * )data;
58         delete image;
59         image = NULL;
60 #ifdef USE_KDE
61         if (instance) delete instance;
62         instance = 0L;
63 #endif
64 }
65
66 QMutex mutex;
67
68 #ifdef USE_KDE
69 void init_qimage()
70 {
71         if (!instance) {
72             instance = new KInstance("qimage_prod");
73             KImageIO::registerFormats();
74         }
75 }
76 #endif
77
78 void refresh_qimage( mlt_frame frame, int width, int height )
79 {
80         // Obtain a previous assigned qimage (if it exists) 
81         QImage *qimage = static_cast <QImage *>(mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "qimage", NULL ));
82
83         // Obtain properties of frame
84         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
85
86         // Obtain the producer for this frame
87         producer_qimage self = ( producer_qimage )mlt_properties_get_data( properties, "producer_qimage", NULL );
88
89         // Obtain the producer 
90         mlt_producer producer = &self->parent;
91
92         // Obtain properties of producer
93         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
94
95         // Check if user wants us to reload the image
96         if ( mlt_properties_get_int( producer_props, "force_reload" ) ) 
97         {
98                 qimage = NULL;
99                 self->current_image = NULL;
100                 mlt_properties_set_int( producer_props, "force_reload", 0 );
101         }
102
103         // Obtain the cache flag and structure
104         int use_cache = mlt_properties_get_int( producer_props, "cache" );
105         mlt_properties cache = ( mlt_properties )mlt_properties_get_data( producer_props, "_cache", NULL );
106         int update_cache = 0;
107
108         // Get the time to live for each frame
109         double ttl = mlt_properties_get_int( producer_props, "ttl" );
110
111         // Get the original position of this frame
112         mlt_position position = mlt_properties_get_position( properties, "qimage_position" );
113         position += mlt_producer_get_in( producer );
114
115         // Image index
116         int image_idx = ( int )floor( ( double )position / ttl ) % self->count;
117
118         // Key for the cache
119         char image_key[ 10 ];
120         sprintf( image_key, "%d", image_idx );
121
122         mutex.lock();
123
124         // Check if the frame is already loaded
125         if ( use_cache )
126         {
127                 if ( cache == NULL )
128                 {
129                         cache = mlt_properties_new( );
130                         mlt_properties_set_data( producer_props, "_cache", cache, 0, ( mlt_destructor )mlt_properties_close, NULL );
131                 }
132
133                 mlt_frame cached = ( mlt_frame )mlt_properties_get_data( cache, image_key, NULL );
134
135                 if ( cached )
136                 {
137                         self->image_idx = image_idx;
138                         mlt_properties cached_props = MLT_FRAME_PROPERTIES( cached );
139                         self->current_width = mlt_properties_get_int( cached_props, "width" );
140                         self->current_height = mlt_properties_get_int( cached_props, "height" );
141                         mlt_properties_set_int( producer_props, "_real_width", mlt_properties_get_int( cached_props, "real_width" ) );
142                         mlt_properties_set_int( producer_props, "_real_height", mlt_properties_get_int( cached_props, "real_height" ) );
143                         self->current_image = ( uint8_t * )mlt_properties_get_data( cached_props, "image", NULL );
144                         self->current_alpha = ( uint8_t * )mlt_properties_get_data( cached_props, "alpha", NULL );
145
146                         if ( width != 0 && ( width != self->current_width || height != self->current_height ) )
147                                 self->current_image = NULL;
148                 }
149         }
150
151     // optimization for subsequent iterations on single picture
152         if ( width != 0 && self->current_image != NULL && image_idx == self->image_idx )
153         {
154                 if ( width != self->current_width || height != self->current_height )
155                 {
156                         qimage = static_cast<QImage *>(mlt_properties_get_data( producer_props, "_qimage", NULL ));
157                         if ( !use_cache )
158                         {
159                                 mlt_pool_release( self->current_image );
160                                 mlt_pool_release( self->current_alpha );
161                         }
162                         self->current_image = NULL;
163                         self->current_alpha = NULL;
164                 }
165         }
166         else if ( qimage == NULL && ( self->current_image == NULL || image_idx != self->image_idx ) )
167         {
168                 if ( !use_cache )
169                 {
170                         mlt_pool_release( self->current_image );
171                         mlt_pool_release( self->current_alpha );
172                 }
173                 self->current_image = NULL;
174                 self->current_alpha = NULL;
175
176                 self->image_idx = image_idx;
177                 qimage = new QImage( mlt_properties_get_value( self->filenames, image_idx ) );
178
179                 if ( !qimage->isNull( ) )
180                 {
181                         QImage *frame_copy = new QImage( *qimage );
182
183                         // Store the width/height of the pixbuf 
184                         self->current_width = qimage->width( );
185                         self->current_height = qimage->height( );
186
187                         // Register qimage for destruction and reuse
188                         mlt_events_block( producer_props, NULL );
189                         mlt_properties_set_data( producer_props, "_qimage", qimage, 0, ( mlt_destructor )qimage_delete, NULL );
190                         mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame ), "qimage", frame_copy, 0, ( mlt_destructor )qimage_delete, NULL );
191                         mlt_properties_set_int( producer_props, "_real_width", self->current_width );
192                         mlt_properties_set_int( producer_props, "_real_height", self->current_height );
193                         mlt_events_unblock( producer_props, NULL );
194                 }
195                 else
196                 {
197                         delete qimage;
198                         qimage = NULL;
199                 }
200         }
201
202         // If we have a pixbuf and this request specifies a valid dimension and we haven't already got a cached version...
203         if ( qimage && width > 0 && self->current_image == NULL )
204         {
205                 char *interps = mlt_properties_get( properties, "rescale.interp" );
206                 int interp = 0;
207
208                 // QImage has two scaling modes - we'll toggle between them here
209                 if ( strcmp( interps, "tiles" ) == 0 )
210                         interp = 1;
211                 else if ( strcmp( interps, "hyper" ) == 0 )
212                         interp = 1;
213
214 #ifdef USE_QT4
215                 // Note - the original qimage is already safe and ready for destruction
216                 QImage scaled = interp == 0 ? qimage->scaled( QSize( width, height)) : qimage->scaled( QSize(width, height), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
217                 QImage temp;
218                 bool hasAlpha = scaled.hasAlphaChannel();
219                 if (hasAlpha)
220                     temp = scaled.convertToFormat(QImage::Format_ARGB32);
221                 else 
222                     temp = scaled.convertToFormat(QImage::Format_RGB888);
223 #endif
224
225 #ifdef USE_QT3
226                 // Note - the original qimage is already safe and ready for destruction
227                 QImage scaled = interp == 0 ? qimage->scale( width, height, QImage::ScaleFree ) : qimage->smoothScale( width, height, QImage::ScaleFree );
228                 QImage temp = scaled.convertDepth( 32 );
229                 bool hasAlpha = true;
230 #endif
231
232                 // Store width and height
233                 self->current_width = width;
234                 self->current_height = height;
235                 
236                 // Allocate/define image
237                 self->current_image = ( uint8_t * )mlt_pool_alloc( width * ( height + 1 ) * 2 );
238
239
240                 if (!hasAlpha) {
241                         mlt_convert_rgb24_to_yuv422( temp.bits(), self->current_width, self->current_height, temp.bytesPerLine(), self->current_image ); 
242                 }
243                 else {
244                         // Allocate the alpha mask
245                         self->current_alpha = ( uint8_t * )mlt_pool_alloc( self->current_width * self->current_height );
246 #ifdef USE_QT4
247                         if ( QSysInfo::ByteOrder == QSysInfo::BigEndian )
248                                 mlt_convert_argb_to_yuv422( temp.bits( ), self->current_width, self->current_height, temp.bytesPerLine(), self->current_image, self->current_alpha );
249                         else
250                                 mlt_convert_bgr24a_to_yuv422( temp.bits( ), self->current_width, self->current_height, temp.bytesPerLine( ), self->current_image, self->current_alpha );
251 #endif
252
253 #ifdef USE_QT3
254                         // Convert the image
255                         if ( QImage::systemByteOrder( ) == QImage::BigEndian )
256                                 mlt_convert_argb_to_yuv422( temp.bits( ), self->current_width, self->current_height, temp.bytesPerLine( ), self->current_image, self->current_alpha );
257                         else
258                                 mlt_convert_bgr24a_to_yuv422( temp.bits( ), self->current_width, self->current_height, temp.bytesPerLine( ), self->current_image, self->current_alpha );
259 #endif
260                 }
261
262                 // Ensure we update the cache when we need to
263                 update_cache = use_cache;
264         }
265
266         // Set width/height of frame
267         mlt_properties_set_int( properties, "width", self->current_width );
268         mlt_properties_set_int( properties, "height", self->current_height );
269         mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
270         mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
271
272         // pass the image data without destructor
273         mlt_properties_set_data( properties, "image", self->current_image, self->current_width * ( self->current_height + 1 ) * 2, NULL, NULL );
274         mlt_properties_set_data( properties, "alpha", self->current_alpha, self->current_width * self->current_height, NULL, NULL );
275
276         if ( update_cache )
277         {
278                 mlt_frame cached = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
279                 mlt_properties cached_props = MLT_FRAME_PROPERTIES( cached );
280                 mlt_properties_set_int( cached_props, "width", self->current_width );
281                 mlt_properties_set_int( cached_props, "height", self->current_height );
282                 mlt_properties_set_int( cached_props, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
283                 mlt_properties_set_int( cached_props, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
284                 mlt_properties_set_data( cached_props, "image", self->current_image, self->current_width * ( self->current_height + 1 ) * 2, mlt_pool_release, NULL );
285                 mlt_properties_set_data( cached_props, "alpha", self->current_alpha, self->current_width * self->current_height, mlt_pool_release, NULL );
286                 mlt_properties_set_data( cache, image_key, cached, 0, ( mlt_destructor )mlt_frame_close, NULL );
287         }
288         mutex.unlock();
289     }
290 }
291