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