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