2 * producer_colour.c -- raster image loader based upon gdk-pixbuf
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <framework/mlt_producer.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_pool.h>
35 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
36 static void producer_close( mlt_producer parent );
38 mlt_producer producer_colour_init( mlt_profile profile, mlt_service_type type, const char *id, char *colour )
40 mlt_producer producer = calloc( 1, sizeof( struct mlt_producer_s ) );
41 if ( producer != NULL && mlt_producer_init( producer, NULL ) == 0 )
43 // Get the properties interface
44 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
46 // Callback registration
47 producer->get_frame = producer_get_frame;
48 producer->close = ( mlt_destructor )producer_close;
50 // Set the default properties
51 mlt_properties_set( properties, "resource", colour == NULL ? "0x000000ff" : colour );
52 mlt_properties_set( properties, "_resource", "" );
53 mlt_properties_set_double( properties, "aspect_ratio", 0 );
61 rgba_color parse_color( char *color, unsigned int color_int )
63 rgba_color result = { 0xff, 0xff, 0xff, 0xff };
65 if ( !strcmp( color, "red" ) )
71 else if ( !strcmp( color, "green" ) )
77 else if ( !strcmp( color, "blue" ) )
83 else if ( strcmp( color, "white" ) )
85 result.r = ( color_int >> 24 ) & 0xff;
86 result.g = ( color_int >> 16 ) & 0xff;
87 result.b = ( color_int >> 8 ) & 0xff;
88 result.a = ( color_int ) & 0xff;
94 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
96 // Obtain properties of frame
97 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
99 // Obtain the producer for this frame
100 mlt_producer producer = mlt_properties_get_data( properties, "producer_colour", NULL );
102 // Obtain properties of producer
103 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
105 // Get the current and previous colour strings
106 char *now = mlt_properties_get( producer_props, "resource" );
107 char *then = mlt_properties_get( producer_props, "_resource" );
109 // Get the current image and dimensions cached in the producer
111 uint8_t *image = mlt_properties_get_data( producer_props, "image", &size );
112 int current_width = mlt_properties_get_int( producer_props, "_width" );
113 int current_height = mlt_properties_get_int( producer_props, "_height" );
114 mlt_image_format current_format = mlt_properties_get_int( producer_props, "_format" );
117 if ( now && strchr( now, '/' ) )
119 now = strrchr( now, '/' ) + 1;
120 mlt_properties_set( producer_props, "resource", now );
122 rgba_color color = parse_color( now, mlt_properties_get_int( producer_props, "resource" ) );
124 // See if we need to regenerate
125 if ( strcmp( now, then ) || *width != current_width || *height != current_height || *format != current_format )
128 int i = *width * *height + 1;
133 case mlt_image_rgb24:
136 case mlt_image_rgb24a:
137 case mlt_image_opengl:
142 *format = mlt_image_yuv422;
146 // Allocate the image
147 size = *width * *height * bpp;
148 uint8_t *p = image = mlt_pool_alloc( size );
150 // Update the producer
151 mlt_properties_set_data( producer_props, "image", image, size, mlt_pool_release, NULL );
152 mlt_properties_set_int( producer_props, "_width", *width );
153 mlt_properties_set_int( producer_props, "_height", *height );
154 mlt_properties_set_int( producer_props, "_format", *format );
155 mlt_properties_set( producer_props, "_resource", now );
159 case mlt_image_yuv422:
161 int uneven = *width % 2;
162 int count = ( *width - uneven ) / 2 + 1;
165 RGB2YUV_601_SCALED( color.r, color.g, color.b, y, u, v );
185 case mlt_image_rgb24:
193 case mlt_image_rgb24a:
194 case mlt_image_opengl:
208 // Create the alpha channel
209 int alpha_size = *width * *height;
210 uint8_t *alpha = mlt_pool_alloc( alpha_size );
212 // Initialise the alpha
214 memset( alpha, color.a, alpha_size );
217 *buffer = mlt_pool_alloc( size );
218 memcpy( *buffer, image, size );
220 // Now update properties so we free the copy after
221 mlt_properties_set_data( properties, "image", *buffer, size, mlt_pool_release, NULL );
222 mlt_properties_set_data( properties, "alpha", alpha, alpha_size, mlt_pool_release, NULL );
223 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_props, "aspect_ratio" ) );
224 mlt_properties_set_int( properties, "real_width", *width );
225 mlt_properties_set_int( properties, "real_height", *height );
231 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
234 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
236 if ( *frame != NULL )
238 // Obtain properties of frame and producer
239 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
241 // Obtain properties of producer
242 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
244 // Set the producer on the frame properties
245 mlt_properties_set_data( properties, "producer_colour", producer, 0, NULL, NULL );
247 // Update timecode on the frame we're creating
248 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
250 // Set producer-specific frame properties
251 mlt_properties_set_int( properties, "progressive", 1 );
252 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_props, "aspect_ratio" ) );
254 // colour is an alias for resource
255 if ( mlt_properties_get( producer_props, "colour" ) != NULL )
256 mlt_properties_set( producer_props, "resource", mlt_properties_get( producer_props, "colour" ) );
258 // Push the get_image method
259 mlt_frame_push_get_image( *frame, producer_get_image );
262 // Calculate the next timecode
263 mlt_producer_prepare_next( producer );
268 static void producer_close( mlt_producer producer )
270 producer->close = NULL;
271 mlt_producer_close( producer );