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