]> git.sesse.net Git - mlt/blob - src/modules/qimage/qimage_wrapper.cpp
Fix distorted frame in slideshow transitions.
[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 #include <QtCore/QtEndian>
43 #endif
44
45 #ifdef USE_EXIF
46 #include <libexif/exif-data.h>
47 #endif
48
49 #include <cmath>
50
51 extern "C" {
52
53 #include <framework/mlt_pool.h>
54 #include <framework/mlt_cache.h>
55
56 #ifdef USE_KDE
57 static KInstance *instance = 0L;
58 #endif
59
60 static void qimage_delete( void *data )
61 {
62         QImage *image = ( QImage * )data;
63         delete image;
64         image = NULL;
65 #ifdef USE_KDE
66         if (instance) delete instance;
67         instance = 0L;
68 #endif
69 }
70
71 static QMutex g_mutex;
72
73 #ifdef USE_KDE
74 void init_qimage()
75 {
76         if (!instance) {
77             instance = new KInstance("qimage_prod");
78             KImageIO::registerFormats();
79         }
80 }
81 #endif
82
83 void refresh_qimage( producer_qimage self, mlt_frame frame, int width, int height )
84 {
85         // Obtain properties of frame
86         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
87
88         // Obtain the producer 
89         mlt_producer producer = &self->parent;
90
91         // Obtain properties of producer
92         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
93
94         // restore QImage
95         pthread_mutex_lock( &self->mutex );
96         mlt_cache_item qimage_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "qimage.qimage" );
97         QImage *qimage = static_cast<QImage*>( mlt_cache_item_data( qimage_cache, NULL ) );
98
99         // restore scaled image
100         self->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "qimage.image" );
101         self->current_image = static_cast<uint8_t*>( mlt_cache_item_data( self->image_cache, NULL ) );
102
103         // Check if user wants us to reload the image
104         if ( mlt_properties_get_int( producer_props, "force_reload" ) )
105         {
106                 qimage = NULL;
107                 self->current_image = NULL;
108                 mlt_properties_set_int( producer_props, "force_reload", 0 );
109         }
110
111         // Obtain the cache flag and structure
112         int use_cache = mlt_properties_get_int( producer_props, "cache" );
113         mlt_properties cache = ( mlt_properties )mlt_properties_get_data( producer_props, "_cache", NULL );
114         int update_cache = 0;
115
116         // Get the time to live for each frame
117         double ttl = mlt_properties_get_int( producer_props, "ttl" );
118
119         // Get the original position of this frame
120         mlt_position position = mlt_properties_get_position( properties, "qimage_position" );
121         position += mlt_producer_get_in( producer );
122
123         // Image index
124         int image_idx = ( int )floor( ( double )position / ttl ) % self->count;
125
126         // Key for the cache
127         char image_key[ 10 ];
128         sprintf( image_key, "%d", image_idx );
129
130         g_mutex.lock();
131
132         // Check if the frame is already loaded
133         if ( use_cache )
134         {
135                 if ( cache == NULL )
136                 {
137                         cache = mlt_properties_new( );
138                         mlt_properties_set_data( producer_props, "_cache", cache, 0, ( mlt_destructor )mlt_properties_close, NULL );
139                 }
140
141                 mlt_frame cached = ( mlt_frame )mlt_properties_get_data( cache, image_key, NULL );
142
143                 if ( cached )
144                 {
145                         self->image_idx = image_idx;
146                         mlt_properties cached_props = MLT_FRAME_PROPERTIES( cached );
147                         self->current_width = mlt_properties_get_int( cached_props, "width" );
148                         self->current_height = mlt_properties_get_int( cached_props, "height" );
149                         mlt_properties_set_int( producer_props, "_real_width", mlt_properties_get_int( cached_props, "real_width" ) );
150                         mlt_properties_set_int( producer_props, "_real_height", mlt_properties_get_int( cached_props, "real_height" ) );
151                         self->current_image = ( uint8_t * )mlt_properties_get_data( cached_props, "image", NULL );
152                         self->has_alpha = mlt_properties_get_int( cached_props, "alpha" );
153
154                         if ( width != 0 && ( width != self->current_width || height != self->current_height ) )
155                                 self->current_image = NULL;
156                 }
157         }
158
159         int disable_exif = mlt_properties_get_int( producer_props, "disable_exif" );
160
161         // optimization for subsequent iterations on single pictur
162         if ( width != 0 && ( image_idx != self->image_idx || width != self->current_width || height != self->current_height ) )
163                 self->current_image = NULL;
164         if ( image_idx != self->qimage_idx )
165                 qimage = NULL;
166
167         if ( !qimage || mlt_properties_get_int( producer_props, "_disable_exif" ) != disable_exif)
168         {
169                 self->current_image = NULL;
170                 qimage = new QImage( mlt_properties_get_value( self->filenames, image_idx ) );
171
172                 if ( !qimage->isNull( ) )
173                 {
174 #ifdef USE_EXIF
175                         // Read the exif value for this file
176                         if ( disable_exif == 0) {
177                                 ExifData *d = exif_data_new_from_file( mlt_properties_get_value( self->filenames, image_idx ) );
178                                 ExifEntry *entry;
179                                 int exif_orientation = 0;
180                                 /* get orientation and rotate image accordingly if necessary */
181                                 if (d) {
182                                         if ( ( entry = exif_content_get_entry ( d->ifd[EXIF_IFD_0], EXIF_TAG_ORIENTATION ) ) )
183                                                 exif_orientation = exif_get_short (entry->data, exif_data_get_byte_order (d));
184                                         
185                                         /* Free the EXIF data */
186                                         exif_data_unref(d);
187                                 }
188
189                                 if ( exif_orientation > 1 )
190                                 {
191                                       // Rotate image according to exif data
192                                       QImage processed;
193                                       QMatrix matrix;
194
195                                       switch ( exif_orientation ) {
196                                           case 2:
197                                               matrix.scale( -1, 1 );
198                                               break;
199                                           case 3:
200                                               matrix.rotate( 180 );
201                                               break;
202                                           case 4:
203                                               matrix.scale( 1, -1 );
204                                               break;
205                                           case 5:
206                                               matrix.rotate( 270 );
207                                               matrix.scale( -1, 1 );
208                                               break;
209                                           case 6:
210                                               matrix.rotate( 90 );
211                                               break;
212                                           case 7:
213                                               matrix.rotate( 90 );
214                                               matrix.scale( -1, 1 );
215                                               break;
216                                           case 8:
217                                               matrix.rotate( 270 );
218                                               break;
219                                       }
220                                       processed = qimage->transformed( matrix );
221                                       delete qimage;
222                                       qimage = new QImage( processed );
223                                 }
224                         }
225 #endif                  
226                         // Store the width/height of the qimage  
227                         self->current_width = qimage->width( );
228                         self->current_height = qimage->height( );
229
230                         // Register qimage for destruction and reuse
231                         mlt_cache_item_close( qimage_cache );
232                         mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "qimage.qimage", qimage, 0, ( mlt_destructor )qimage_delete );
233                         qimage_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "qimage.qimage" );
234                         self->qimage_idx = image_idx;
235
236                         mlt_events_block( producer_props, NULL );
237                         mlt_properties_set_int( producer_props, "_real_width", self->current_width );
238                         mlt_properties_set_int( producer_props, "_real_height", self->current_height );
239                         mlt_properties_set_int( producer_props, "_disable_exif", disable_exif );
240                         mlt_events_unblock( producer_props, NULL );
241                 }
242                 else
243                 {
244                         delete qimage;
245                         qimage = NULL;
246                 }
247         }
248
249         // If we have a pixbuf and this request specifies a valid dimension and we haven't already got a cached version...
250         if ( qimage && width > 0 && !self->current_image )
251         {
252                 char *interps = mlt_properties_get( properties, "rescale.interp" );
253                 int interp = 0;
254
255                 // QImage has two scaling modes - we'll toggle between them here
256                 if ( strcmp( interps, "tiles" ) == 0 )
257                         interp = 1;
258                 else if ( strcmp( interps, "hyper" ) == 0 )
259                         interp = 1;
260
261 #ifdef USE_QT4
262                 // Note - the original qimage is already safe and ready for destruction
263                 QImage scaled = interp == 0 ? qimage->scaled( QSize( width, height ) ) :
264                         qimage->scaled( QSize(width, height), Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
265                 QImage temp;
266                 self->has_alpha = scaled.hasAlphaChannel();
267 #endif
268
269 #ifdef USE_QT3
270                 // Note - the original qimage is already safe and ready for destruction
271                 QImage scaled = interp == 0 ? qimage->scale( width, height, QImage::ScaleFree ) :
272                         qimage->smoothScale( width, height, QImage::ScaleFree );
273                 self->has_alpha = 1;
274 #endif
275
276                 // Store width and height
277                 self->current_width = width;
278                 self->current_height = height;
279
280                 // Allocate/define image
281                 int dst_stride = width * ( self->has_alpha ? 4 : 3 );
282                 int image_size = dst_stride * ( height + 1 );
283                 self->current_image = ( uint8_t * )mlt_pool_alloc( image_size );
284
285                 // Copy the image
286                 int y = self->current_height + 1;
287                 uint8_t *dst = self->current_image;
288                 while ( --y )
289                 {
290                         QRgb *src = (QRgb*) scaled.scanLine( self->current_height - y );
291                         int x = self->current_width + 1;
292                         while ( --x )
293                         {
294                                 *dst++ = qRed(*src);
295                                 *dst++ = qGreen(*src);
296                                 *dst++ = qBlue(*src);
297                                 if ( self->has_alpha ) *dst++ = qAlpha(*src);
298                                 ++src;
299                         }
300                 }
301
302                 // Update the cache
303                 if ( !use_cache )
304                         mlt_cache_item_close( self->image_cache );
305                 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "qimage.image", self->current_image, image_size, mlt_pool_release );
306                 self->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "qimage.image" );
307                 self->image_idx = image_idx;
308
309                 // Ensure we update the cache when we need to
310                 update_cache = use_cache;
311         }
312
313         // release references no longer needed
314         mlt_cache_item_close( qimage_cache );
315         if ( width == 0 )
316         {
317                 pthread_mutex_unlock( &self->mutex );
318                 mlt_cache_item_close( self->image_cache );
319         }
320
321         // Set width/height of frame
322         mlt_properties_set_int( properties, "width", self->current_width );
323         mlt_properties_set_int( properties, "height", self->current_height );
324         mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
325         mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
326
327         if ( update_cache )
328         {
329                 mlt_frame cached = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
330                 mlt_properties cached_props = MLT_FRAME_PROPERTIES( cached );
331                 mlt_properties_set_int( cached_props, "width", self->current_width );
332                 mlt_properties_set_int( cached_props, "height", self->current_height );
333                 mlt_properties_set_int( cached_props, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
334                 mlt_properties_set_int( cached_props, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
335                 mlt_properties_set_data( cached_props, "image", self->current_image,
336                         self->current_width * ( self->current_height + 1 ) * ( self->has_alpha ? 4 : 3 ),
337                         mlt_pool_release, NULL );
338                 mlt_properties_set_int( cached_props, "alpha", self->has_alpha );
339                 mlt_properties_set_data( cache, image_key, cached, 0, ( mlt_destructor )mlt_frame_close, NULL );
340         }
341         g_mutex.unlock();
342 }
343
344 } // extern "C"