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