producer_pango.c
[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 <framework/mlt_geometry.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <gdk-pixbuf/gdk-pixbuf.h>
27 #include <pango/pangoft2.h>
28 #include <freetype/freetype.h>
29 #include <iconv.h>
30
31 struct producer_pango_s
32 {
33 struct mlt_producer_s parent;
34 int width;
35 int height;
36 uint8_t *image;
37 uint8_t *alpha;
38 char *fgcolor;
39 char *bgcolor;
40 int align;
41 int pad;
42 char *markup;
43 char *text;
44 char *font;
45 int weight;
46 };
47
48 // special color type used by internal pango routines
49 typedef struct
50 {
51 uint8_t r, g, b, a;
52 } rgba_color;
53
54 // Forward declarations
55 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
56 static void producer_close( mlt_producer parent );
57 static void pango_draw_background( GdkPixbuf *pixbuf, rgba_color bg );
58 static GdkPixbuf *pango_get_pixbuf( const char *markup, const char *text, const char *font,
59 rgba_color fg, rgba_color bg, int pad, int align, int weight, int size );
60
61 /** Return nonzero if the two strings are equal, ignoring case, up to
62 the first n characters.
63 */
64 int strncaseeq(const char *s1, const char *s2, size_t n)
65 {
66 for ( ; n > 0; n--)
67 {
68 if (tolower(*s1++) != tolower(*s2++))
69 return 0;
70 }
71 return 1;
72 }
73
74 /** Parse the alignment property.
75 */
76
77 static int alignment_parse( char* align )
78 {
79 int ret = pango_align_left;
80
81 if ( align == NULL );
82 else if ( isdigit( align[ 0 ] ) )
83 ret = atoi( align );
84 else if ( align[ 0 ] == 'c' || align[ 0 ] == 'm' )
85 ret = pango_align_center;
86 else if ( align[ 0 ] == 'r' )
87 ret = pango_align_right;
88
89 return ret;
90 }
91
92 static PangoFT2FontMap *fontmap = NULL;
93
94 mlt_producer producer_pango_init( const char *filename )
95 {
96 producer_pango this = calloc( sizeof( struct producer_pango_s ), 1 );
97 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
98 {
99 mlt_producer producer = &this->parent;
100
101 // THIS SHOULD BE MUTEXED...
102 if ( fontmap == NULL )
103 fontmap = (PangoFT2FontMap*) pango_ft2_font_map_new();
104
105 producer->get_frame = producer_get_frame;
106 producer->close = ( mlt_destructor )producer_close;
107
108 // This is required to initialise gdk-pixbuf
109 g_type_init();
110
111 // Get the properties interface
112 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->parent );
113
114 // Set the default properties
115 mlt_properties_set( properties, "fgcolour", "0xffffffff" );
116 mlt_properties_set( properties, "bgcolour", "0x00000000" );
117 mlt_properties_set_int( properties, "align", pango_align_left );
118 mlt_properties_set_int( properties, "pad", 0 );
119 mlt_properties_set( properties, "text", "" );
120 mlt_properties_set( properties, "font", "Sans 48" );
121 mlt_properties_set( properties, "encoding", "UTF-8" );
122 mlt_properties_set_int( properties, "weight", PANGO_WEIGHT_NORMAL );
123
124 if ( filename == NULL )
125 {
126 mlt_properties_set( properties, "markup", "" );
127 }
128 else if ( filename[ 0 ] == '+' || strstr( filename, "/+" ) )
129 {
130 char *copy = strdup( filename + 1 );
131 char *markup = copy;
132 if ( strstr( markup, "/+" ) )
133 markup = strstr( markup, "/+" ) + 2;
134 ( *strrchr( markup, '.' ) ) = '\0';
135 while ( strchr( markup, '~' ) )
136 ( *strchr( markup, '~' ) ) = '\n';
137 mlt_properties_set( properties, "resource", ( char * )filename );
138 mlt_properties_set( properties, "markup", markup );
139 free( copy );
140 }
141 else if ( strstr( filename, ".mpl" ) )
142 {
143 int i = 0;
144 mlt_properties contents = mlt_properties_load( filename );
145 mlt_geometry key_frames = mlt_geometry_init( );
146 struct mlt_geometry_item_s item;
147 mlt_properties_set_data( properties, "contents", contents, 0, ( mlt_destructor )mlt_properties_close, NULL );
148 mlt_properties_set_data( properties, "key_frames", key_frames, 0, ( mlt_destructor )mlt_geometry_close, NULL );
149
150 // Make sure we have at least one entry
151 if ( mlt_properties_get( contents, "0" ) == NULL )
152 mlt_properties_set( contents, "0", "" );
153
154 for ( i = 0; i < mlt_properties_count( contents ); i ++ )
155 {
156 char *name = mlt_properties_get_name( contents, i );
157 char *value = mlt_properties_get_value( contents, i );
158 while ( value != NULL && strchr( value, '~' ) )
159 ( *strchr( value, '~' ) ) = '\n';
160 item.frame = atoi( name );
161 mlt_geometry_insert( key_frames, &item );
162 }
163 }
164 else
165 {
166 FILE *f = fopen( filename, "r" );
167 if ( f != NULL )
168 {
169 char line[81];
170 char *markup = NULL;
171 size_t size = 0;
172 line[80] = '\0';
173
174 while ( fgets( line, 80, f ) )
175 {
176 size += strlen( line ) + 1;
177 if ( markup )
178 {
179 markup = realloc( markup, size );
180 strcat( markup, line );
181 }
182 else
183 {
184 markup = strdup( line );
185 }
186 }
187 fclose( f );
188
189 if ( markup[ strlen( markup ) - 1 ] == '\n' )
190 markup[ strlen( markup ) - 1 ] = '\0';
191
192 mlt_properties_set( properties, "resource", ( char * ) filename );
193 mlt_properties_set( properties, "markup", ( char * ) ( markup == NULL ? "" : markup ) );
194 free( markup );
195 }
196 else
197 {
198 mlt_properties_set( properties, "markup", "" );
199 }
200 }
201
202 return producer;
203 }
204 free( this );
205 return NULL;
206 }
207
208 static void set_string( char **string, char *value, char *fallback )
209 {
210 if ( value != NULL )
211 {
212 free( *string );
213 *string = strdup( value );
214 }
215 else if ( *string == NULL && fallback != NULL )
216 {
217 *string = strdup( fallback );
218 }
219 else if ( *string != NULL && fallback == NULL )
220 {
221 free( *string );
222 *string = NULL;
223 }
224 }
225
226 rgba_color parse_color( char *color )
227 {
228 rgba_color result = { 0xff, 0xff, 0xff, 0xff };
229
230 if ( !strncmp( color, "0x", 2 ) )
231 {
232 unsigned int temp = 0;
233 sscanf( color + 2, "%x", &temp );
234 result.r = ( temp >> 24 ) & 0xff;
235 result.g = ( temp >> 16 ) & 0xff;
236 result.b = ( temp >> 8 ) & 0xff;
237 result.a = ( temp ) & 0xff;
238 }
239 else if ( !strcmp( color, "red" ) )
240 {
241 result.r = 0xff;
242 result.g = 0x00;
243 result.b = 0x00;
244 }
245 else if ( !strcmp( color, "green" ) )
246 {
247 result.r = 0x00;
248 result.g = 0xff;
249 result.b = 0x00;
250 }
251 else if ( !strcmp( color, "blue" ) )
252 {
253 result.r = 0x00;
254 result.g = 0x00;
255 result.b = 0xff;
256 }
257 else
258 {
259 unsigned int temp = 0;
260 sscanf( color, "%d", &temp );
261 result.r = ( temp >> 24 ) & 0xff;
262 result.g = ( temp >> 16 ) & 0xff;
263 result.b = ( temp >> 8 ) & 0xff;
264 result.a = ( temp ) & 0xff;
265 }
266
267 return result;
268 }
269
270 /** Convert a string property to UTF-8
271 */
272 static int iconv_utf8( mlt_properties properties, char *prop_name, const char* encoding )
273 {
274 char *text = mlt_properties_get( properties, prop_name );
275 int result = -1;
276
277 iconv_t cd = iconv_open( "UTF-8", encoding );
278 if ( cd != ( iconv_t )-1 )
279 {
280 char *inbuf_p = text;
281 size_t inbuf_n = strlen( text );
282 size_t outbuf_n = inbuf_n * 6;
283 char *outbuf = mlt_pool_alloc( outbuf_n );
284 char *outbuf_p = outbuf;
285
286 memset( outbuf, 0, outbuf_n );
287
288 if ( text != NULL && strcmp( text, "" ) && iconv( cd, &inbuf_p, &inbuf_n, &outbuf_p, &outbuf_n ) != -1 )
289 mlt_properties_set( properties, prop_name, outbuf );
290 else
291 mlt_properties_set( properties, prop_name, "" );
292
293 mlt_pool_release( outbuf );
294 iconv_close( cd );
295 result = 0;
296 }
297 return result;
298 }
299
300 static void refresh_image( mlt_frame frame, int width, int height )
301 {
302 // Pixbuf
303 GdkPixbuf *pixbuf = NULL;
304
305 // Obtain properties of frame
306 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
307
308 // Obtain the producer pango for this frame
309 producer_pango this = mlt_properties_get_data( properties, "producer_pango", NULL );
310
311 // Obtain the producer
312 mlt_producer producer = &this->parent;
313
314 // Obtain the producer properties
315 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
316
317 // Get producer properties
318 char *fg = mlt_properties_get( producer_props, "fgcolour" );
319 char *bg = mlt_properties_get( producer_props, "bgcolour" );
320 int align = alignment_parse( mlt_properties_get( producer_props, "align" ) );
321 int pad = mlt_properties_get_int( producer_props, "pad" );
322 char *markup = mlt_properties_get( producer_props, "markup" );
323 char *text = mlt_properties_get( producer_props, "text" );
324 char *font = mlt_properties_get( producer_props, "font" );
325 char *encoding = mlt_properties_get( producer_props, "encoding" );
326 int weight = mlt_properties_get_int( producer_props, "weight" );
327 int size = mlt_properties_get_int( producer_props, "size" );
328 int property_changed = ( align != this->align );
329
330 // Check for file support
331 int position = mlt_properties_get_position( properties, "pango_position" );
332 mlt_properties contents = mlt_properties_get_data( producer_props, "contents", NULL );
333 mlt_geometry key_frames = mlt_properties_get_data( producer_props, "key_frames", NULL );
334 struct mlt_geometry_item_s item;
335 if ( contents != NULL )
336 {
337 char temp[ 20 ];
338 mlt_geometry_prev_key( key_frames, &item, position );
339 sprintf( temp, "%d", item.frame );
340 markup = mlt_properties_get( contents, temp );
341 }
342
343 // See if any properties changed
344 property_changed = property_changed || ( this->fgcolor == NULL || ( fg && strcmp( fg, this->fgcolor ) ) );
345 property_changed = property_changed || ( this->bgcolor == NULL || ( bg && strcmp( bg, this->bgcolor ) ) );
346 property_changed = property_changed || ( pad != this->pad );
347 property_changed = property_changed || ( markup && this->markup && strcmp( markup, this->markup ) );
348 property_changed = property_changed || ( text && this->text && strcmp( text, this->text ) );
349 property_changed = property_changed || ( font && this->font && strcmp( font, this->font ) );
350 property_changed = property_changed || ( weight != this->weight );
351
352 // Save the properties for next comparison
353 this->align = align;
354 this->pad = pad;
355 set_string( &this->fgcolor, fg, "0xffffffff" );
356 set_string( &this->bgcolor, bg, "0x00000000" );
357 set_string( &this->markup, markup, NULL );
358 set_string( &this->text, text, NULL );
359 set_string( &this->font, font, "Sans 48" );
360 this->weight = weight;
361
362 if ( property_changed )
363 {
364 rgba_color fgcolor = parse_color( this->fgcolor );
365 rgba_color bgcolor = parse_color( this->bgcolor );
366
367 mlt_pool_release( this->image );
368 mlt_pool_release( this->alpha );
369 this->image = NULL;
370 this->alpha = NULL;
371
372 // Convert from specified encoding to UTF-8
373 if ( encoding != NULL && !strncaseeq( encoding, "utf-8", 5 ) && !strncaseeq( encoding, "utf8", 4 ) )
374 {
375 if ( markup != NULL && iconv_utf8( producer_props, "markup", encoding ) != -1 )
376 {
377 markup = mlt_properties_get( producer_props, "markup" );
378 set_string( &this->markup, markup, NULL );
379 }
380 if ( text != NULL && iconv_utf8( producer_props, "text", encoding ) != -1 )
381 {
382 text = mlt_properties_get( producer_props, "text" );
383 set_string( &this->text, text, NULL );
384 }
385 }
386
387 // Render the title
388 pixbuf = pango_get_pixbuf( markup, text, font, fgcolor, bgcolor, pad, align, weight, size );
389
390 if ( pixbuf != NULL )
391 {
392 // Register this pixbuf for destruction and reuse
393 mlt_properties_set_data( producer_props, "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
394
395 mlt_properties_set_int( producer_props, "real_width", gdk_pixbuf_get_width( pixbuf ) );
396 mlt_properties_set_int( producer_props, "real_height", gdk_pixbuf_get_height( pixbuf ) );
397
398 // Store the width/height of the pixbuf temporarily
399 this->width = gdk_pixbuf_get_width( pixbuf );
400 this->height = gdk_pixbuf_get_height( pixbuf );
401 }
402 }
403 else if ( width > 0 && ( this->image == NULL || width != this->width || height != this->height ) )
404 {
405 mlt_pool_release( this->image );
406 mlt_pool_release( this->alpha );
407 this->image = NULL;
408 this->alpha = NULL;
409
410 pixbuf = mlt_properties_get_data( producer_props, "pixbuf", NULL );
411 }
412
413 // If we have a pixbuf and a valid width
414 if ( pixbuf && width > 0 )
415 {
416 char *interps = mlt_properties_get( properties, "rescale.interp" );
417 int interp = GDK_INTERP_BILINEAR;
418
419 if ( strcmp( interps, "nearest" ) == 0 )
420 interp = GDK_INTERP_NEAREST;
421 else if ( strcmp( interps, "tiles" ) == 0 )
422 interp = GDK_INTERP_TILES;
423 else if ( strcmp( interps, "hyper" ) == 0 )
424 interp = GDK_INTERP_HYPER;
425
426 // fprintf( stderr, "SCALING PANGO from %dx%d to %dx%d was %dx%d\n", gdk_pixbuf_get_width( pixbuf ), gdk_pixbuf_get_height( pixbuf ), width, height, this->width, this->height );
427
428 // Note - the original pixbuf is already safe and ready for destruction
429 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
430
431 // Store width and height
432 this->width = width;
433 this->height = height;
434
435 // Allocate/define image
436 this->image = mlt_pool_alloc( width * ( height + 1 ) * 2 );
437 this->alpha = mlt_pool_alloc( this->width * this->height );
438
439 // Convert the image
440 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
441 this->width, this->height,
442 gdk_pixbuf_get_rowstride( pixbuf ),
443 this->image, this->alpha );
444
445 // Finished with pixbuf now
446 g_object_unref( pixbuf );
447 }
448
449 // Set width/height
450 mlt_properties_set_int( properties, "width", this->width );
451 mlt_properties_set_int( properties, "height", this->height );
452 mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "real_width" ) );
453 mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "real_height" ) );
454
455 // pass the image data without destructor
456 mlt_properties_set_data( properties, "image", this->image, this->width * ( this->height + 1 ) * 2, NULL, NULL );
457 mlt_properties_set_data( properties, "alpha", this->alpha, this->width * this->height, NULL, NULL );
458 }
459
460 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
461 {
462 // Obtain properties of frame
463 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
464
465 // We need to know the size of the image to clone it
466 int image_size = 0;
467 int alpha_size = 0;
468
469 // Alpha channel
470 uint8_t *alpha = NULL;
471
472 *width = mlt_properties_get_int( properties, "rescale_width" );
473 *height = mlt_properties_get_int( properties, "rescale_height" );
474
475 // Refresh the image
476 refresh_image( frame, *width, *height );
477
478 // Get the image
479 *buffer = mlt_properties_get_data( properties, "image", &image_size );
480 alpha = mlt_properties_get_data( properties, "alpha", &alpha_size );
481
482 // Get width and height
483 *width = mlt_properties_get_int( properties, "width" );
484 *height = mlt_properties_get_int( properties, "height" );
485
486 // Always clone here to allow 'animated' text
487 if ( *buffer != NULL )
488 {
489 // Clone the image and the alpha
490 uint8_t *image_copy = mlt_pool_alloc( image_size );
491 uint8_t *alpha_copy = mlt_pool_alloc( alpha_size );
492
493 memcpy( image_copy, *buffer, image_size );
494
495 // Copy or default the alpha
496 if ( alpha != NULL )
497 memcpy( alpha_copy, alpha, alpha_size );
498 else
499 memset( alpha_copy, 255, alpha_size );
500
501 // Now update properties so we free the copy after
502 mlt_properties_set_data( properties, "image", image_copy, image_size, mlt_pool_release, NULL );
503 mlt_properties_set_data( properties, "alpha", alpha_copy, alpha_size, mlt_pool_release, NULL );
504
505 // We're going to pass the copy on
506 *buffer = image_copy;
507 }
508 else
509 {
510 // TODO: Review all cases of invalid images
511 *buffer = mlt_pool_alloc( 50 * 50 * 2 );
512 mlt_properties_set_data( properties, "image", *buffer, image_size, mlt_pool_release, NULL );
513 *width = 50;
514 *height = 50;
515 }
516
517 return 0;
518 }
519
520 static uint8_t *producer_get_alpha_mask( mlt_frame this )
521 {
522 // Obtain properties of frame
523 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
524
525 // Return the alpha mask
526 return mlt_properties_get_data( properties, "alpha", NULL );
527 }
528
529 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
530 {
531 producer_pango this = producer->child;
532
533 // Generate a frame
534 *frame = mlt_frame_init( );
535
536 // Obtain properties of frame and producer
537 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
538
539 // Set the producer on the frame properties
540 mlt_properties_set_data( properties, "producer_pango", this, 0, NULL, NULL );
541
542 // Update timecode on the frame we're creating
543 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
544 mlt_properties_set_position( properties, "pango_position", mlt_producer_frame( producer ) );
545
546 // Refresh the pango image
547 refresh_image( *frame, 0, 0 );
548
549 // Set producer-specific frame properties
550 mlt_properties_set_int( properties, "progressive", 1 );
551 mlt_properties_set_double( properties, "aspect_ratio", 0 );
552
553 // Set alpha call back
554 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
555
556 // Stack the get image callback
557 mlt_frame_push_get_image( *frame, producer_get_image );
558
559 // Calculate the next timecode
560 mlt_producer_prepare_next( producer );
561
562 return 0;
563 }
564
565 static void producer_close( mlt_producer parent )
566 {
567 producer_pango this = parent->child;
568 mlt_pool_release( this->image );
569 mlt_pool_release( this->alpha );
570 free( this->fgcolor );
571 free( this->bgcolor );
572 free( this->markup );
573 free( this->text );
574 free( this->font );
575 parent->close = NULL;
576 mlt_producer_close( parent );
577 free( this );
578 }
579
580 static void pango_draw_background( GdkPixbuf *pixbuf, rgba_color bg )
581 {
582 int ww = gdk_pixbuf_get_width( pixbuf );
583 int hh = gdk_pixbuf_get_height( pixbuf );
584 uint8_t *p = gdk_pixbuf_get_pixels( pixbuf );
585 int i, j;
586
587 for ( j = 0; j < hh; j++ )
588 {
589 for ( i = 0; i < ww; i++ )
590 {
591 *p++ = bg.r;
592 *p++ = bg.g;
593 *p++ = bg.b;
594 *p++ = bg.a;
595 }
596 }
597 }
598
599 static GdkPixbuf *pango_get_pixbuf( const char *markup, const char *text, const char *font, rgba_color fg, rgba_color bg, int pad, int align, int weight, int size )
600 {
601 PangoContext *context = pango_ft2_font_map_create_context( fontmap );
602 PangoLayout *layout = pango_layout_new( context );
603 int w, h, x;
604 int i, j;
605 GdkPixbuf *pixbuf = NULL;
606 FT_Bitmap bitmap;
607 uint8_t *src = NULL;
608 uint8_t* dest = NULL;
609 uint8_t *d, *s, a;
610 int stride;
611 PangoFontDescription *desc = pango_font_description_from_string( font );
612
613 pango_ft2_font_map_set_resolution( fontmap, 72, 72 );
614 pango_layout_set_width( layout, -1 ); // set wrapping constraints
615 pango_font_description_set_weight( desc, ( PangoWeight ) weight );
616 if ( size != 0 )
617 pango_font_description_set_size( desc, PANGO_SCALE * size );
618 pango_layout_set_font_description( layout, desc );
619 // pango_layout_set_spacing( layout, space );
620 pango_layout_set_alignment( layout, ( PangoAlignment ) align );
621 if ( markup != NULL && strcmp( markup, "" ) != 0 )
622 pango_layout_set_markup( layout, markup, strlen( markup ) );
623 else if ( text != NULL && strcmp( text, "" ) != 0 )
624 pango_layout_set_text( layout, text, strlen( text ) );
625 else
626 pango_layout_set_text( layout, " ", 2 );
627 pango_layout_get_pixel_size( layout, &w, &h );
628
629 pixbuf = gdk_pixbuf_new( GDK_COLORSPACE_RGB, TRUE /* has alpha */, 8, w + 2 * pad, h + 2 * pad );
630 pango_draw_background( pixbuf, bg );
631
632 stride = gdk_pixbuf_get_rowstride( pixbuf );
633
634 bitmap.width = w;
635 bitmap.pitch = 32 * ( ( w + 31 ) / 31 );
636 bitmap.rows = h;
637 bitmap.buffer = mlt_pool_alloc( h * bitmap.pitch );
638 bitmap.num_grays = 256;
639 bitmap.pixel_mode = ft_pixel_mode_grays;
640
641 memset( bitmap.buffer, 0, h * bitmap.pitch );
642
643 pango_ft2_render_layout( &bitmap, layout, 0, 0 );
644
645 src = bitmap.buffer;
646 x = ( gdk_pixbuf_get_width( pixbuf ) - w - 2 * pad ) * align / 2 + pad;
647 dest = gdk_pixbuf_get_pixels( pixbuf ) + 4 * x + pad * stride;
648 j = h;
649
650 while( j -- )
651 {
652 d = dest;
653 s = src;
654 i = w;
655 while( i -- )
656 {
657 a = *s ++;
658 *d++ = ( a * fg.r + ( 255 - a ) * bg.r ) >> 8;
659 *d++ = ( a * fg.g + ( 255 - a ) * bg.g ) >> 8;
660 *d++ = ( a * fg.b + ( 255 - a ) * bg.b ) >> 8;
661 *d++ = ( a * fg.a + ( 255 - a ) * bg.a ) >> 8;
662 }
663 dest += stride;
664 src += bitmap.pitch;
665 }
666 mlt_pool_release( bitmap.buffer );
667 pango_font_description_free( desc );
668 g_object_unref( layout );
669 g_object_unref( context );
670
671 return pixbuf;
672 }