]> git.sesse.net Git - mlt/blob - src/modules/core/producer_colour.c
f7966ed8322c5a32d8decfcd331a384d746272ea
[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 program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include "producer_colour.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( 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 == NULL ? "0x000000ff" : colour );
52                 
53                 return producer;
54         }
55         free( producer );
56         return NULL;
57 }
58
59 rgba_color parse_color( char *color )
60 {
61         rgba_color result = { 0xff, 0xff, 0xff, 0xff };
62
63         if ( strchr( color, '/' ) )
64                 color = strrchr( color, '/' ) + 1;
65
66         if ( !strncmp( color, "0x", 2 ) )
67         {
68                 unsigned int temp = 0;
69                 sscanf( color + 2, "%x", &temp );
70                 result.r = ( temp >> 24 ) & 0xff;
71                 result.g = ( temp >> 16 ) & 0xff;
72                 result.b = ( temp >> 8 ) & 0xff;
73                 result.a = ( temp ) & 0xff;
74         }
75         else if ( !strcmp( color, "red" ) )
76         {
77                 result.r = 0xff;
78                 result.g = 0x00;
79                 result.b = 0x00;
80         }
81         else if ( !strcmp( color, "green" ) )
82         {
83                 result.r = 0x00;
84                 result.g = 0xff;
85                 result.b = 0x00;
86         }
87         else if ( !strcmp( color, "blue" ) )
88         {
89                 result.r = 0x00;
90                 result.g = 0x00;
91                 result.b = 0xff;
92         }
93         else if ( strcmp( color, "white" ) )
94         {
95                 unsigned int temp = 0;
96                 sscanf( color, "%d", &temp );
97                 result.r = ( temp >> 24 ) & 0xff;
98                 result.g = ( temp >> 16 ) & 0xff;
99                 result.b = ( temp >> 8 ) & 0xff;
100                 result.a = ( temp ) & 0xff;
101         }
102
103         return result;
104 }
105
106 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
107 {
108         // May need to know the size of the image to clone it
109         int size = 0;
110         
111         // Obtain properties of frame
112         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
113
114         // Obtain the producer for this frame
115         mlt_producer producer = mlt_properties_get_data( properties, "producer_colour", NULL );
116
117         // Obtain properties of producer
118         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
119
120         // Parse the colour
121         rgba_color color = parse_color( mlt_properties_get( producer_props, "resource" ) );
122
123         // Get the current image and dimensions cached in the producer
124         uint8_t *image = mlt_properties_get_data( producer_props, "image", &size );
125         int current_width = mlt_properties_get_int( producer_props, "width" );
126         int current_height = mlt_properties_get_int( producer_props, "height" );
127
128         // See if we need to regenerate
129         if ( *width != current_width || *height != current_height )
130         {
131                 // Allocate the image
132                 size = *width * *height * 2;
133                 image = mlt_pool_alloc( size );
134
135                 // Update the producer
136                 mlt_properties_set_data( producer_props, "image", image, size, mlt_pool_release, NULL );
137                 mlt_properties_set_int( producer_props, "width", *width );
138                 mlt_properties_set_int( producer_props, "height", *height );
139
140                 // Color the image
141                 uint8_t y, u, v;
142                 int i = 0;
143                 RGB2YUV( color.r, color.g, color.b, y, u, v );
144
145                 while ( i < size )
146                 {
147                         image[ i ++ ] = y;
148                         image[ i ++ ] = u;
149                         image[ i ++ ] = y;
150                         image[ i ++ ] = v;
151                 }
152         }
153
154         // Update the frame
155         mlt_properties_set_data( properties, "image", image, size, NULL, NULL );
156         mlt_properties_set_int( properties, "width", *width );
157         mlt_properties_set_int( properties, "height", *height );
158         
159         // Clone if necessary
160         if ( writable )
161         {
162                 // Create the alpha channel
163                 uint8_t *alpha = mlt_pool_alloc( size >> 1 );
164
165                 // Clone our image
166                 uint8_t *copy = mlt_pool_alloc( size );
167                 memcpy( copy, image, size );
168
169                 // We're going to pass the copy on
170                 image = copy;
171
172                 // Initialise the alpha
173                 if ( alpha )
174                         memset( alpha, color.a, size >> 1 );
175
176                 // Now update properties so we free the copy after
177                 mlt_properties_set_data( properties, "image", copy, size, mlt_pool_release, NULL );
178                 mlt_properties_set_data( properties, "alpha", alpha, size >> 1, mlt_pool_release, NULL );
179         }
180
181         // Pass on the image
182         *buffer = image;
183         *format = mlt_image_yuv422;
184
185         return 0;
186 }
187
188 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
189 {
190         // Generate a frame
191         *frame = mlt_frame_init( );
192
193         if ( *frame != NULL )
194         {
195                 // Obtain properties of frame and producer
196                 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
197
198                 // Obtain properties of producer
199                 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
200
201                 // Determine if we're producing PAL or NTSC
202                 int is_pal = mlt_properties_get_double( producer_props, "fps" ) == 25.0;
203
204                 // Set the producer on the frame properties
205                 mlt_properties_set_data( properties, "producer_colour", producer, 0, NULL, NULL );
206
207                 // Update timecode on the frame we're creating
208                 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
209
210                 // Set producer-specific frame properties
211                 mlt_properties_set_int( properties, "progressive", 1 );
212                 mlt_properties_set_double( properties, "aspect_ratio", is_pal ? 59.0/54.0 : 10.0/11.0 );
213
214                 // colour is an alias for resource
215                 if ( mlt_properties_get( producer_props, "colour" ) != NULL )
216                         mlt_properties_set( producer_props, "resource", mlt_properties_get( producer_props, "colour" ) );
217                 
218                 // Push the get_image method
219                 mlt_frame_push_get_image( *frame, producer_get_image );
220         }
221
222         // Calculate the next timecode
223         mlt_producer_prepare_next( producer );
224
225         return 0;
226 }
227
228 static void producer_close( mlt_producer producer )
229 {
230         producer->close = NULL;
231         mlt_producer_close( producer );
232         free( producer );
233 }