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