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