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