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