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>
6 * NB: This module is designed to be functionally equivalent to the
7 * gtk2 image loading module so it can be used as replacement.
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.
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.
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.
24 #include "qimage_wrapper.h"
31 #include <kinstance.h>
39 #include <QtGui/QImage>
40 #include <QtCore/QSysInfo>
41 #include <QtCore/QMutex>
42 #include <QtCore/QtEndian>
46 #include <exif-data.h>
53 #include <framework/mlt_pool.h>
54 #include <framework/mlt_cache.h>
57 static KInstance *instance = 0L;
60 static void qimage_delete( void *data )
62 QImage *image = ( QImage * )data;
66 if (instance) delete instance;
71 static QMutex g_mutex;
77 instance = new KInstance("qimage_prod");
78 KImageIO::registerFormats();
83 void refresh_qimage( producer_qimage self, mlt_frame frame, int width, int height )
85 // Obtain properties of frame
86 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
88 // Obtain the producer
89 mlt_producer producer = &self->parent;
91 // Obtain properties of producer
92 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
95 pthread_mutex_lock( &self->mutex );
96 mlt_cache_item qimage_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "qimage.qimage" );
97 QImage *qimage = static_cast<QImage*>( mlt_cache_item_data( qimage_cache, NULL ) );
99 // restore scaled image
100 self->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "qimage.image" );
101 self->current_image = static_cast<uint8_t*>( mlt_cache_item_data( self->image_cache, NULL ) );
103 // Check if user wants us to reload the image
104 if ( mlt_properties_get_int( producer_props, "force_reload" ) )
107 self->current_image = NULL;
108 mlt_properties_set_int( producer_props, "force_reload", 0 );
111 // Obtain the cache flag and structure
112 int use_cache = mlt_properties_get_int( producer_props, "cache" );
113 mlt_properties cache = ( mlt_properties )mlt_properties_get_data( producer_props, "_cache", NULL );
114 int update_cache = 0;
116 // Get the time to live for each frame
117 double ttl = mlt_properties_get_int( producer_props, "ttl" );
119 // Get the original position of this frame
120 mlt_position position = mlt_properties_get_position( properties, "qimage_position" );
121 position += mlt_producer_get_in( producer );
124 int image_idx = ( int )floor( ( double )position / ttl ) % self->count;
127 char image_key[ 10 ];
128 sprintf( image_key, "%d", image_idx );
132 // Check if the frame is already loaded
137 cache = mlt_properties_new( );
138 mlt_properties_set_data( producer_props, "_cache", cache, 0, ( mlt_destructor )mlt_properties_close, NULL );
141 mlt_frame cached = ( mlt_frame )mlt_properties_get_data( cache, image_key, NULL );
145 self->image_idx = image_idx;
146 mlt_properties cached_props = MLT_FRAME_PROPERTIES( cached );
147 self->current_width = mlt_properties_get_int( cached_props, "width" );
148 self->current_height = mlt_properties_get_int( cached_props, "height" );
149 mlt_properties_set_int( producer_props, "_real_width", mlt_properties_get_int( cached_props, "real_width" ) );
150 mlt_properties_set_int( producer_props, "_real_height", mlt_properties_get_int( cached_props, "real_height" ) );
151 self->current_image = ( uint8_t * )mlt_properties_get_data( cached_props, "image", NULL );
152 self->has_alpha = mlt_properties_get_int( cached_props, "alpha" );
154 if ( width != 0 && ( width != self->current_width || height != self->current_height ) )
155 self->current_image = NULL;
159 int disable_exif = mlt_properties_get_int( producer_props, "disable_exif" );
161 // optimization for subsequent iterations on single pictur
162 if ( width != 0 && ( image_idx != self->image_idx || width != self->current_width || height != self->current_height ) )
163 self->current_image = NULL;
164 if ( image_idx != self->qimage_idx )
167 if ( ( !qimage && !self->current_image ) || mlt_properties_get_int( producer_props, "_disable_exif" ) != disable_exif)
169 self->current_image = NULL;
170 qimage = new QImage( mlt_properties_get_value( self->filenames, image_idx ) );
172 if ( !qimage->isNull( ) )
175 // Read the exif value for this file
176 if ( disable_exif == 0) {
177 ExifData *d = exif_data_new_from_file( mlt_properties_get_value( self->filenames, image_idx ) );
179 ExifByteOrder byte_order = exif_data_get_byte_order (d);
180 int exif_orientation = 0;
181 /* get orientation and rotate image accordingly if necessary */
182 if ((entry = exif_content_get_entry (d->ifd[EXIF_IFD_0], EXIF_TAG_ORIENTATION)))
184 exif_orientation = exif_get_short (entry->data, byte_order);
186 if ( exif_orientation > 1 )
188 // Rotate image according to exif data
192 switch ( exif_orientation ) {
194 matrix.scale( -1, 1 );
197 matrix.rotate( 180 );
200 matrix.scale( 1, -1 );
203 matrix.rotate( 270 );
204 matrix.scale( -1, 1 );
211 matrix.scale( -1, 1 );
214 matrix.rotate( 270 );
217 processed = qimage->transformed( matrix );
219 qimage = new QImage( processed );
223 // Store the width/height of the qimage
224 self->current_width = qimage->width( );
225 self->current_height = qimage->height( );
227 // Register qimage for destruction and reuse
228 mlt_cache_item_close( qimage_cache );
229 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "qimage.qimage", qimage, 0, ( mlt_destructor )qimage_delete );
230 qimage_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "qimage.qimage" );
231 self->qimage_idx = image_idx;
233 mlt_events_block( producer_props, NULL );
234 mlt_properties_set_int( producer_props, "_real_width", self->current_width );
235 mlt_properties_set_int( producer_props, "_real_height", self->current_height );
236 mlt_properties_set_int( producer_props, "_disable_exif", disable_exif );
237 mlt_events_unblock( producer_props, NULL );
246 // If we have a pixbuf and this request specifies a valid dimension and we haven't already got a cached version...
247 if ( qimage && width > 0 && !self->current_image )
249 char *interps = mlt_properties_get( properties, "rescale.interp" );
252 // QImage has two scaling modes - we'll toggle between them here
253 if ( strcmp( interps, "tiles" ) == 0 )
255 else if ( strcmp( interps, "hyper" ) == 0 )
259 // Note - the original qimage is already safe and ready for destruction
260 QImage scaled = interp == 0 ? qimage->scaled( QSize( width, height ) ) :
261 qimage->scaled( QSize(width, height), Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
263 self->has_alpha = scaled.hasAlphaChannel();
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 );
273 // Store width and height
274 self->current_width = width;
275 self->current_height = height;
277 // Allocate/define image
278 int dst_stride = width * ( self->has_alpha ? 4 : 3 );
279 int image_size = dst_stride * ( height + 1 );
280 self->current_image = ( uint8_t * )mlt_pool_alloc( image_size );
283 int y = self->current_height + 1;
284 uint8_t *dst = self->current_image;
287 QRgb *src = (QRgb*) scaled.scanLine( self->current_height - y );
288 int x = self->current_width + 1;
292 *dst++ = qGreen(*src);
293 *dst++ = qBlue(*src);
294 if ( self->has_alpha ) *dst++ = qAlpha(*src);
301 mlt_cache_item_close( self->image_cache );
302 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "qimage.image", self->current_image, image_size, mlt_pool_release );
303 self->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "qimage.image" );
304 self->image_idx = image_idx;
306 // Ensure we update the cache when we need to
307 update_cache = use_cache;
310 // release references no longer needed
311 mlt_cache_item_close( qimage_cache );
314 pthread_mutex_unlock( &self->mutex );
315 mlt_cache_item_close( self->image_cache );
318 // Set width/height of frame
319 mlt_properties_set_int( properties, "width", self->current_width );
320 mlt_properties_set_int( properties, "height", self->current_height );
321 mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
322 mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
326 mlt_frame cached = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
327 mlt_properties cached_props = MLT_FRAME_PROPERTIES( cached );
328 mlt_properties_set_int( cached_props, "width", self->current_width );
329 mlt_properties_set_int( cached_props, "height", self->current_height );
330 mlt_properties_set_int( cached_props, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
331 mlt_properties_set_int( cached_props, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
332 mlt_properties_set_data( cached_props, "image", self->current_image,
333 self->current_width * ( self->current_height + 1 ) * ( self->has_alpha ? 4 : 3 ),
334 mlt_pool_release, NULL );
335 mlt_properties_set_int( cached_props, "alpha", self->has_alpha );
336 mlt_properties_set_data( cache, image_key, cached, 0, ( mlt_destructor )mlt_frame_close, NULL );