fixup and optimize edge conditions of composite; updated property handling of produce...
[melted] / src / modules / gtk2 / producer_pango.c
1 /*
2 * producer_pango.c -- a pango-based titler
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_pango.h"
22 #include <framework/mlt_frame.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <gdk-pixbuf/gdk-pixbuf.h>
26 #include <pango/pangoft2.h>
27 #include <freetype/freetype.h>
28
29 struct producer_pango_s
30 {
31 struct mlt_producer_s parent;
32 int width;
33 int height;
34 uint8_t *image;
35 uint8_t *alpha;
36 int fgcolor;
37 int bgcolor;
38 int align;
39 int pad;
40 char *markup;
41 char *text;
42 char *font;
43 };
44
45 // special color type used by internal pango routines
46 typedef struct
47 {
48 uint8_t r, g, b, a;
49 } rgba_color;
50
51 // Forward declarations
52 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
53 static void producer_close( mlt_producer parent );
54 static void pango_draw_background( GdkPixbuf *pixbuf, rgba_color bg );
55 static GdkPixbuf *pango_get_pixbuf( const char *markup, const char *text, const char *font,
56 rgba_color fg, rgba_color bg, int pad, int align );
57
58 mlt_producer producer_pango_init( const char *markup )
59 {
60 producer_pango this = calloc( sizeof( struct producer_pango_s ), 1 );
61 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
62 {
63 mlt_producer producer = &this->parent;
64
65 producer->get_frame = producer_get_frame;
66 producer->close = producer_close;
67
68 // This is required to initialise gdk-pixbuf
69 g_type_init();
70
71 // Get the properties interface
72 mlt_properties properties = mlt_producer_properties( &this->parent );
73
74 // Set the default properties
75 mlt_properties_set_int( properties, "video_standard", mlt_video_standard_pal );
76 mlt_properties_set_int( properties, "fgcolor", 0xffffffff );
77 mlt_properties_set_int( properties, "bgcolor", 0x00000000 );
78 mlt_properties_set_int( properties, "align", pango_align_left );
79 mlt_properties_set_int( properties, "pad", 0 );
80 mlt_properties_set( properties, "markup", ( char * ) ( markup == NULL ? "" : markup ) );
81 mlt_properties_set( properties, "text", "" );
82 mlt_properties_set( properties, "font", "Sans 48" );
83 mlt_properties_set_int( properties, "x", 0 );
84 mlt_properties_set_int( properties, "y", 0 );
85 mlt_properties_set_double( properties, "mix", 1.0 );
86
87 return producer;
88 }
89 free( this );
90 return NULL;
91 }
92
93 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
94 {
95 // Obtain properties of frame
96 mlt_properties properties = mlt_frame_properties( this );
97
98 // May need to know the size of the image to clone it
99 int size = 0;
100
101 // Get the image
102 uint8_t *image = mlt_properties_get_data( properties, "image", &size );
103
104 // Get width and height
105 *width = mlt_properties_get_int( properties, "width" );
106 *height = mlt_properties_get_int( properties, "height" );
107
108 // Clone if necessary
109 if ( writable )
110 {
111 // Clone our image
112 uint8_t *copy = malloc( size );
113 memcpy( copy, image, size );
114
115 // We're going to pass the copy on
116 image = copy;
117
118 // Now update properties so we free the copy after
119 mlt_properties_set_data( properties, "image", copy, size, free, NULL );
120 }
121
122 // Pass on the image
123 *buffer = image;
124
125 return 0;
126 }
127
128 static uint8_t *producer_get_alpha_mask( mlt_frame this )
129 {
130 // Obtain properties of frame
131 mlt_properties properties = mlt_frame_properties( this );
132
133 // Return the alpha mask
134 return mlt_properties_get_data( properties, "alpha", NULL );
135 }
136
137 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
138 {
139 producer_pango this = producer->child;
140 GdkPixbuf *pixbuf = NULL;
141
142 // Generate a frame
143 *frame = mlt_frame_init( );
144
145 // Obtain properties of frame and producer
146 mlt_properties properties = mlt_frame_properties( *frame );
147 mlt_properties producer_props = mlt_producer_properties( producer );
148
149 // Get producer properties
150 int fg = mlt_properties_get_int( producer_props, "fgcolor" );
151 int bg = mlt_properties_get_int( producer_props, "bgcolor" );
152 int align = mlt_properties_get_int( producer_props, "align" );
153 int pad = mlt_properties_get_int( producer_props, "pad" );
154 char *markup = mlt_properties_get( producer_props, "markup" );
155 char *text = mlt_properties_get( producer_props, "text" );
156 char *font = mlt_properties_get( producer_props, "font" );
157
158 // See if any properties changed
159 int property_changed = ( fg != this->fgcolor );
160 property_changed = property_changed || ( bg != this->bgcolor );
161 property_changed = property_changed || ( align != this->align );
162 property_changed = property_changed || ( pad != this->pad );
163 property_changed = property_changed || ( markup && this->markup && strcmp( markup, this->markup ) );
164 property_changed = property_changed || ( text && this->text && strcmp( text, this->text ) );
165 property_changed = property_changed || ( font && this->font && strcmp( font, this->font ) );
166
167 // Save the properties for next comparison
168 this->fgcolor = fg;
169 this->bgcolor = bg;
170 this->align = align;
171 this->pad = pad;
172 if ( markup != NULL )
173 {
174 if ( this->markup != NULL )
175 free( this->markup );
176 this->markup = strdup( markup );
177 }
178 if ( text != NULL )
179 {
180 if ( this->text != NULL )
181 free( this->text );
182 this->text = strdup( text );
183 }
184 if ( font != NULL )
185 {
186 if ( this->font != NULL )
187 free( this->font );
188 this->font = strdup( font );
189 }
190
191 if ( property_changed )
192 {
193 rgba_color fgcolor =
194 {
195 ( fg & 0xff000000 ) >> 24,
196 ( fg & 0x00ff0000 ) >> 16,
197 ( fg & 0x0000ff00 ) >> 8,
198 ( fg & 0x000000ff )
199 };
200 rgba_color bgcolor =
201 {
202 ( bg & 0xff000000 ) >> 24,
203 ( bg & 0x00ff0000 ) >> 16,
204 ( bg & 0x0000ff00 ) >> 8,
205 ( bg & 0x000000ff )
206 };
207
208 // Render the title
209 pixbuf = pango_get_pixbuf( markup, text, font, fgcolor, bgcolor, pad, align );
210 }
211
212 // If we have a pixbuf
213 if ( pixbuf )
214 {
215 // Scale to adjust for sample aspect ratio
216 if ( mlt_properties_get_int( properties, "video_standard" ) == mlt_video_standard_pal )
217 {
218 GdkPixbuf *temp = pixbuf;
219 GdkPixbuf *scaled = gdk_pixbuf_scale_simple( pixbuf,
220 (gint) ( (float) gdk_pixbuf_get_width( pixbuf ) * 54.0/59.0),
221 gdk_pixbuf_get_height( pixbuf ), GDK_INTERP_HYPER );
222 pixbuf = scaled;
223 g_object_unref( temp );
224 }
225 else
226 {
227 GdkPixbuf *temp = pixbuf;
228 GdkPixbuf *scaled = gdk_pixbuf_scale_simple( pixbuf,
229 (gint) ( (float) gdk_pixbuf_get_width( pixbuf ) * 11.0/10.0 ),
230 gdk_pixbuf_get_height( pixbuf ), GDK_INTERP_HYPER );
231 pixbuf = scaled;
232 g_object_unref( temp );
233 }
234
235 // Store width and height
236 this->width = gdk_pixbuf_get_width( pixbuf );
237 this->height = gdk_pixbuf_get_height( pixbuf );
238
239 // Allocate/define image and alpha
240 uint8_t *image = malloc( this->width * this->height * 2 );
241 uint8_t *alpha = NULL;
242
243 // Extract YUV422 and alpha
244 if ( gdk_pixbuf_get_has_alpha( pixbuf ) )
245 {
246 // Allocate the alpha mask
247 alpha = malloc( this->width * this->height );
248
249 // Convert the image
250 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
251 this->width, this->height,
252 gdk_pixbuf_get_rowstride( pixbuf ),
253 image, alpha );
254 }
255 else
256 {
257 // No alpha to extract
258 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
259 this->width, this->height,
260 gdk_pixbuf_get_rowstride( pixbuf ),
261 image );
262 }
263
264 // Finished with pixbuf now
265 g_object_unref( pixbuf );
266
267 // if single picture, reference the image and alpha in the producer
268 if ( this->image != NULL )
269 free( this->image );
270 this->image = image;
271 if ( this->alpha != NULL )
272 free( this->alpha );
273 this->alpha = alpha;
274
275 }
276
277 if ( this->image != NULL )
278 {
279 // Set width/height
280 mlt_properties_set_int( properties, "width", this->width );
281 mlt_properties_set_int( properties, "height", this->height );
282
283 // Set the compositing properties
284 mlt_properties_set_int( properties, "x", mlt_properties_get_int( producer_props, "x" ) );
285 mlt_properties_set_int( properties, "y", mlt_properties_get_int( producer_props, "y" ) );
286 mlt_properties_set_double( properties, "mix", mlt_properties_get_double( producer_props, "mix" ) );
287
288 // if picture sequence pass the image and alpha data without destructor
289 mlt_properties_set_data( properties, "image", this->image, 0, NULL, NULL );
290 mlt_properties_set_data( properties, "alpha", this->alpha, 0, NULL, NULL );
291
292 // Set alpha mask call back
293 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
294
295 // Stack the get image callback
296 mlt_frame_push_get_image( *frame, producer_get_image );
297 }
298
299 // Update timecode on the frame we're creating
300 mlt_frame_set_timecode( *frame, mlt_producer_position( producer ) );
301
302 // Calculate the next timecode
303 mlt_producer_prepare_next( producer );
304
305 return 0;
306 }
307
308 static void producer_close( mlt_producer parent )
309 {
310 producer_pango this = parent->child;
311 if ( this->image != NULL )
312 free( this->image );
313 if ( this->alpha != NULL )
314 free( this->alpha );
315 if ( this->markup != NULL )
316 free( this->markup );
317 if ( this->text != NULL )
318 free( this->text );
319 if ( this->font != NULL )
320 free( this->font );
321 parent->close = NULL;
322 mlt_producer_close( parent );
323 free( this );
324 }
325
326 static void pango_draw_background( GdkPixbuf *pixbuf, rgba_color bg )
327 {
328 int ww = gdk_pixbuf_get_width( pixbuf );
329 int hh = gdk_pixbuf_get_height( pixbuf );
330 uint8_t *p = gdk_pixbuf_get_pixels( pixbuf );
331 int i, j;
332
333 for ( j = 0; j < hh; j++ )
334 {
335 for ( i = 0; i < ww; i++ )
336 {
337 *p++ = bg.r;
338 *p++ = bg.g;
339 *p++ = bg.b;
340 *p++ = bg.a;
341 }
342 }
343 }
344
345 static GdkPixbuf *pango_get_pixbuf( const char *markup, const char *text, const char *font, rgba_color fg, rgba_color bg, int pad, int align )
346 {
347 PangoFT2FontMap *fontmap = (PangoFT2FontMap*) pango_ft2_font_map_new();
348 PangoContext *context = pango_ft2_font_map_create_context( fontmap );
349 PangoLayout *layout = pango_layout_new( context );
350 int w, h, x;
351 int i, j;
352 GdkPixbuf *pixbuf = NULL;
353 FT_Bitmap bitmap;
354 uint8_t *src = NULL;
355 uint8_t* dest = NULL;
356 int stride;
357
358 pango_ft2_font_map_set_resolution( fontmap, 72, 72 );
359 pango_layout_set_width( layout, -1 ); // set wrapping constraints
360 pango_layout_set_font_description( layout, pango_font_description_from_string( font ) );
361 // pango_layout_set_spacing( layout, space );
362 pango_layout_set_alignment( layout, ( PangoAlignment ) align );
363 if ( markup != NULL && strcmp( markup, "" ) != 0 )
364 pango_layout_set_markup( layout, markup, strlen( markup ) );
365 else if ( text != NULL && strcmp( text, "" ) != 0 )
366 pango_layout_set_text( layout, text, strlen( text ) );
367 else
368 return NULL;
369 pango_layout_get_pixel_size( layout, &w, &h );
370
371 pixbuf = gdk_pixbuf_new( GDK_COLORSPACE_RGB, TRUE /* has alpha */, 8, w + 2 * pad, h + 2 * pad );
372 pango_draw_background( pixbuf, bg );
373
374 stride = gdk_pixbuf_get_rowstride( pixbuf );
375
376 bitmap.width = w;
377 bitmap.pitch = 32 * ( ( w + 31 ) / 31 );
378 bitmap.rows = h;
379 bitmap.buffer = ( unsigned char * ) calloc( 1, h * bitmap.pitch );
380 bitmap.num_grays = 256;
381 bitmap.pixel_mode = ft_pixel_mode_grays;
382
383 pango_ft2_render_layout( &bitmap, layout, 0, 0 );
384
385 src = bitmap.buffer;
386 x = ( gdk_pixbuf_get_width( pixbuf ) - w - 2 * pad ) * align / 2 + pad;
387 dest = gdk_pixbuf_get_pixels( pixbuf ) + 4 * x + pad * stride;
388 for ( j = 0; j < h; j++ )
389 {
390 uint8_t *d = dest;
391 for ( i = 0; i < w; i++ )
392 {
393 float a = ( float ) bitmap.buffer[ j * bitmap.pitch + i ] / 255.0;
394 *d++ = ( int ) ( a * fg.r + ( 1 - a ) * bg.r );
395 *d++ = ( int ) ( a * fg.g + ( 1 - a ) * bg.g );
396 *d++ = ( int ) ( a * fg.b + ( 1 - a ) * bg.b );
397 *d++ = ( int ) ( a * fg.a + ( 1 - a ) * bg.a );
398 }
399 dest += stride;
400 }
401 free( bitmap.buffer );
402
403 g_object_unref( layout );
404 g_object_unref( context );
405 g_object_unref( fontmap );
406
407 return pixbuf;
408 }
409