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