]> git.sesse.net Git - mlt/blob - src/modules/core/producer_colour.c
0121689ec1b57c86c7080681b8d4d91f66f3478c
[mlt] / src / modules / core / producer_colour.c
1 /*
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>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <framework/mlt_producer.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_pool.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29
30 typedef struct
31 {
32         uint8_t r, g, b, a;
33 } rgba_color;
34
35 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
36 static void producer_close( mlt_producer parent );
37
38 mlt_producer producer_colour_init( mlt_profile profile, mlt_service_type type, const char *id, char *colour )
39 {
40         mlt_producer producer = calloc( 1, sizeof( struct mlt_producer_s ) );
41         if ( producer != NULL && mlt_producer_init( producer, NULL ) == 0 )
42         {
43                 // Get the properties interface
44                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
45         
46                 // Callback registration
47                 producer->get_frame = producer_get_frame;
48                 producer->close = ( mlt_destructor )producer_close;
49
50                 // Set the default properties
51                 mlt_properties_set( properties, "resource", ( !colour || !strcmp( colour, "" ) ) ? "0x000000ff" : colour );
52                 mlt_properties_set( properties, "_resource", "" );
53                 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
54                 
55                 return producer;
56         }
57         free( producer );
58         return NULL;
59 }
60
61 rgba_color parse_color( char *color, unsigned int color_int )
62 {
63         rgba_color result = { 0xff, 0xff, 0xff, 0xff };
64
65         if ( !strcmp( color, "red" ) )
66         {
67                 result.r = 0xff;
68                 result.g = 0x00;
69                 result.b = 0x00;
70         }
71         else if ( !strcmp( color, "green" ) )
72         {
73                 result.r = 0x00;
74                 result.g = 0xff;
75                 result.b = 0x00;
76         }
77         else if ( !strcmp( color, "blue" ) )
78         {
79                 result.r = 0x00;
80                 result.g = 0x00;
81                 result.b = 0xff;
82         }
83         else if ( !strcmp( color, "black" ) )
84         {
85                 result.r = 0x00;
86                 result.g = 0x00;
87                 result.b = 0x00;
88         }
89         else if ( strcmp( color, "white" ) )
90         {
91                 result.r = ( color_int >> 24 ) & 0xff;
92                 result.g = ( color_int >> 16 ) & 0xff;
93                 result.b = ( color_int >> 8 ) & 0xff;
94                 result.a = ( color_int ) & 0xff;
95         }
96
97         return result;
98 }
99
100 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
101 {
102         // Obtain properties of frame
103         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
104
105         // Obtain the producer for this frame
106         mlt_producer producer = mlt_properties_get_data( properties, "producer_colour", NULL );
107
108         mlt_service_lock( MLT_PRODUCER_SERVICE( producer ) );
109
110         // Obtain properties of producer
111         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
112
113         // Get the current and previous colour strings
114         char *now = mlt_properties_get( producer_props, "resource" );
115         char *then = mlt_properties_get( producer_props, "_resource" );
116
117         // Get the current image and dimensions cached in the producer
118         int size = 0;
119         uint8_t *image = mlt_properties_get_data( producer_props, "image", &size );
120         int current_width = mlt_properties_get_int( producer_props, "_width" );
121         int current_height = mlt_properties_get_int( producer_props, "_height" );
122         mlt_image_format current_format = mlt_properties_get_int( producer_props, "_format" );
123
124         // Parse the colour
125         if ( now && strchr( now, '/' ) )
126         {
127                 now = strdup( strrchr( now, '/' ) + 1 );
128                 mlt_properties_set( producer_props, "resource", now );
129                 free( now );
130                 now = mlt_properties_get( producer_props, "resource" );
131         }
132         rgba_color color = parse_color( now, mlt_properties_get_int( producer_props, "resource" ) );
133
134         // Choose suitable out values if nothing specific requested
135         if ( *format == mlt_image_none )
136                 *format = mlt_image_rgb24a;
137         if ( *width <= 0 )
138                 *width = mlt_service_profile( MLT_PRODUCER_SERVICE(producer) )->width;
139         if ( *height <= 0 )
140                 *height = mlt_service_profile( MLT_PRODUCER_SERVICE(producer) )->height;
141
142         // See if we need to regenerate
143         if ( strcmp( now, then ) || *width != current_width || *height != current_height || *format != current_format )
144         {
145                 // Color the image
146                 int i = *width * *height + 1;
147                 int bpp;
148
149                 // Allocate the image
150                 size = mlt_image_format_size( *format, *width, *height, &bpp );
151                 uint8_t *p = image = mlt_pool_alloc( size );
152
153                 // Update the producer
154                 mlt_properties_set_data( producer_props, "image", image, size, mlt_pool_release, NULL );
155                 mlt_properties_set_int( producer_props, "_width", *width );
156                 mlt_properties_set_int( producer_props, "_height", *height );
157                 mlt_properties_set_int( producer_props, "_format", *format );
158                 mlt_properties_set( producer_props, "_resource", now );
159
160                 mlt_service_unlock( MLT_PRODUCER_SERVICE( producer ) );
161
162                 switch ( *format )
163                 {
164                 case mlt_image_yuv422:
165                 {
166                         int uneven = *width % 2;
167                         int count = ( *width - uneven ) / 2 + 1;
168                         uint8_t y, u, v;
169
170                         RGB2YUV_601_SCALED( color.r, color.g, color.b, y, u, v );
171                         i = *height + 1;
172                         while ( --i )
173                         {
174                                 int j = count;
175                                 while ( --j )
176                                 {
177                                         *p ++ = y;
178                                         *p ++ = u;
179                                         *p ++ = y;
180                                         *p ++ = v;
181                                 }
182                                 if ( uneven )
183                                 {
184                                         *p ++ = y;
185                                         *p ++ = u;
186                                 }
187                         }
188                         break;
189                 }
190                 case mlt_image_rgb24:
191                         while ( --i )
192                         {
193                                 *p ++ = color.r;
194                                 *p ++ = color.g;
195                                 *p ++ = color.b;
196                         }
197                         break;
198                 case mlt_image_glsl:
199                 case mlt_image_glsl_texture:
200                         memset(p, 0, size);
201                         break;
202                 default:
203                         *format = mlt_image_rgb24a;
204                         while ( --i )
205                         {
206                                 *p ++ = color.r;
207                                 *p ++ = color.g;
208                                 *p ++ = color.b;
209                                 *p ++ = color.a;
210                         }
211                         break;
212                 }
213         }
214         else
215         {
216                 mlt_service_unlock( MLT_PRODUCER_SERVICE( producer ) );
217         }
218
219         // Create the alpha channel
220         int alpha_size = *width * *height;
221         uint8_t *alpha = mlt_pool_alloc( alpha_size );
222
223         // Initialise the alpha
224         if ( alpha )
225                 memset( alpha, color.a, alpha_size );
226
227         // Clone our image
228         *buffer = mlt_pool_alloc( size );
229         memcpy( *buffer, image, size );
230
231         // Now update properties so we free the copy after
232         mlt_frame_set_image( frame, *buffer, size, mlt_pool_release );
233         mlt_frame_set_alpha( frame, alpha, alpha_size, mlt_pool_release );
234         mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_props, "aspect_ratio" ) );
235         mlt_properties_set_int( properties, "meta.media.width", *width );
236         mlt_properties_set_int( properties, "meta.media.height", *height );
237
238
239         return 0;
240 }
241
242 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
243 {
244         // Generate a frame
245         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
246
247         if ( *frame != NULL )
248         {
249                 // Obtain properties of frame and producer
250                 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
251
252                 // Obtain properties of producer
253                 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
254
255                 // Set the producer on the frame properties
256                 mlt_properties_set_data( properties, "producer_colour", producer, 0, NULL, NULL );
257
258                 // Update timecode on the frame we're creating
259                 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
260
261                 // Set producer-specific frame properties
262                 mlt_properties_set_int( properties, "progressive", 1 );
263                 mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) );
264                 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
265
266                 // colour is an alias for resource
267                 if ( mlt_properties_get( producer_props, "colour" ) != NULL )
268                         mlt_properties_set( producer_props, "resource", mlt_properties_get( producer_props, "colour" ) );
269                 
270                 // Push the get_image method
271                 mlt_frame_push_get_image( *frame, producer_get_image );
272         }
273
274         // Calculate the next timecode
275         mlt_producer_prepare_next( producer );
276
277         return 0;
278 }
279
280 static void producer_close( mlt_producer producer )
281 {
282         producer->close = NULL;
283         mlt_producer_close( producer );
284         free( producer );
285 }