2 * qimage_wrapper.cpp -- a QT/QImage based producer for MLT
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
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"
30 #include <framework/mlt_pool.h>
32 static void qimage_delete( void *data
)
34 QImage
*image
= ( QImage
* )data
;
38 static void clear_buffered_image( mlt_properties producer_props
, uint8_t **current_image
, uint8_t **current_alpha
)
40 mlt_events_block( producer_props
, NULL
);
41 mlt_properties_set_data( producer_props
, "_qimage_image", NULL
, 0, NULL
, NULL
);
42 mlt_properties_set_data( producer_props
, "_qimage_alpha", NULL
, 0, NULL
, NULL
);
43 *current_image
= NULL
;
44 *current_alpha
= NULL
;
45 mlt_events_unblock( producer_props
, NULL
);
48 static void assign_buffered_image( mlt_properties producer_props
, uint8_t *current_image
, uint8_t *current_alpha
, int width
, int height
)
50 mlt_events_block( producer_props
, NULL
);
51 mlt_properties_set_data( producer_props
, "_qimage_image", current_image
, 0, mlt_pool_release
, NULL
);
52 mlt_properties_set_data( producer_props
, "_qimage_alpha", current_alpha
, 0, mlt_pool_release
, NULL
);
53 mlt_properties_set_int( producer_props
, "_qimage_width", width
);
54 mlt_properties_set_int( producer_props
, "_qimage_height", height
);
55 mlt_events_unblock( producer_props
, NULL
);
58 void refresh_qimage( mlt_frame frame
, int width
, int height
)
60 // Obtain a previous assigned qimage (if it exists)
61 QImage
*qimage
= ( QImage
* )mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame
), "qimage", NULL
);
63 // Obtain properties of frame
64 mlt_properties properties
= MLT_FRAME_PROPERTIES( frame
);
66 // Obtain the producer for this frame
67 producer_qimage self
= ( producer_qimage
)mlt_properties_get_data( properties
, "producer_qimage", NULL
);
69 // Obtain the producer
70 mlt_producer producer
= &self
->parent
;
72 // Obtain properties of producer
73 mlt_properties producer_props
= MLT_PRODUCER_PROPERTIES( producer
);
75 // Retrieve current info if available
76 uint8_t *current_image
= ( uint8_t * )mlt_properties_get_data( producer_props
, "_qimage_image", NULL
);
77 uint8_t *current_alpha
= ( uint8_t * )mlt_properties_get_data( producer_props
, "_qimage_alpha", NULL
);
78 int current_width
= mlt_properties_get_int( producer_props
, "_qimage_width" );
79 int current_height
= mlt_properties_get_int( producer_props
, "_qimage_height" );
81 // Get the time to live for each frame
82 double ttl
= mlt_properties_get_int( producer_props
, "ttl" );
84 // Get the original position of this frame
85 mlt_position position
= mlt_properties_get_position( properties
, "qimage_position" );
88 int image_idx
= ( int )floor( ( double )position
/ ttl
) % self
->count
;
90 // optimization for subsequent iterations on single picture
91 if ( width
!= 0 && current_image
!= NULL
&& image_idx
== self
->image_idx
)
93 if ( width
!= current_width
|| height
!= current_height
)
95 qimage
= ( QImage
* )mlt_properties_get_data( producer_props
, "_qimage", NULL
);
96 clear_buffered_image( producer_props
, ¤t_image
, ¤t_alpha
);
99 else if ( qimage
== NULL
&& ( current_image
== NULL
|| image_idx
!= self
->image_idx
) )
101 clear_buffered_image( producer_props
, ¤t_image
, ¤t_alpha
);
103 self
->image_idx
= image_idx
;
104 qimage
= new QImage( mlt_properties_get_value( self
->filenames
, image_idx
) );
106 if ( !qimage
->isNull( ) )
108 QImage
*frame_copy
= new QImage( *qimage
);
110 // Store the width/height of the pixbuf
111 current_width
= qimage
->width( );
112 current_height
= qimage
->height( );
114 // Register qimage for destruction and reuse
115 mlt_events_block( producer_props
, NULL
);
116 mlt_properties_set_data( producer_props
, "_qimage", qimage
, 0, ( mlt_destructor
)qimage_delete
, NULL
);
117 mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame
), "qimage", frame_copy
, 0, ( mlt_destructor
)qimage_delete
, NULL
);
118 mlt_properties_set_int( producer_props
, "_real_width", current_width
);
119 mlt_properties_set_int( producer_props
, "_real_height", current_height
);
120 mlt_events_unblock( producer_props
, NULL
);
129 // If we have a pixbuf and this request specifies a valid dimension and we haven't already got a cached version...
130 if ( qimage
&& width
> 0 && current_image
== NULL
)
132 char *interps
= mlt_properties_get( properties
, "rescale.interp" );
135 // QImage has two scaling modes - we'll toggle between them here
136 if ( strcmp( interps
, "tiles" ) == 0 )
138 else if ( strcmp( interps
, "hyper" ) == 0 )
141 // Note - the original qimage is already safe and ready for destruction
142 QImage scaled
= interp
== 0 ? qimage
->scale( width
, height
, QImage
::ScaleFree
) : qimage
->smoothScale( width
, height
, QImage
::ScaleFree
);
143 QImage temp
= scaled
.convertDepth( 32 );
145 // Store width and height
146 current_width
= width
;
147 current_height
= height
;
149 // Allocate/define image
150 current_image
= ( uint8_t * )mlt_pool_alloc( width
* ( height
+ 1 ) * 2 );
152 // Allocate the alpha mask
153 current_alpha
= ( uint8_t * )mlt_pool_alloc( current_width
* current_height
);
156 if ( QImage
::systemByteOrder( ) == QImage
::BigEndian
)
157 mlt_convert_argb_to_yuv422( temp
.bits( ), current_width
, current_height
, temp
.bytesPerLine( ), current_image
, current_alpha
);
159 mlt_convert_bgr24a_to_yuv422( temp
.bits( ), current_width
, current_height
, temp
.bytesPerLine( ), current_image
, current_alpha
);
161 assign_buffered_image( producer_props
, current_image
, current_alpha
, current_width
, current_height
);
164 // Set width/height of frame
165 mlt_properties_set_int( properties
, "width", current_width
);
166 mlt_properties_set_int( properties
, "height", current_height
);
167 mlt_properties_set_int( properties
, "real_width", mlt_properties_get_int( producer_props
, "_real_width" ) );
168 mlt_properties_set_int( properties
, "real_height", mlt_properties_get_int( producer_props
, "_real_height" ) );
170 // pass the image data without destructor
171 mlt_properties_set_data( properties
, "image", current_image
, current_width
* ( current_height
+ 1 ) * 2, NULL
, NULL
);
172 mlt_properties_set_data( properties
, "alpha", current_alpha
, current_width
* current_height
, NULL
, NULL
);