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