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