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