Merge ../mlt
[melted] / src / modules / vmfx / filter_chroma.c
1 /*
2 * filter_chroma.c -- Maps a chroma key to the alpha channel
3 * Copyright (C) 2005 Visual Media Fx Inc.
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser 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 <framework/mlt_filter.h>
22 #include <stdlib.h>
23 #include <math.h>
24 #include <framework/mlt_factory.h>
25 #include <framework/mlt_frame.h>
26 #include <framework/mlt_producer.h>
27 #include <framework/mlt_geometry.h>
28
29 static inline int in_range( uint8_t v, uint8_t c, int var )
30 {
31 return ( ( int )v >= c - var ) && ( ( int )v <= c + var );
32 }
33
34 static inline uint8_t alpha_value( uint8_t a, uint8_t *p, uint8_t u, uint8_t v, int var, int odd )
35 {
36 if ( odd == 0 )
37 return ( in_range( *( p + 1 ), u, var ) && in_range( *( p + 3 ), v, var ) ) ? 0 : a;
38 else
39 return ( in_range( ( *( p + 1 ) + *( p + 5 ) ) / 2, u, var ) && in_range( ( *( p + 3 ) + *( p + 7 ) ) / 2, v, var ) ) ? 0 : a;
40 }
41
42 /** Get the images and map the chroma to the alpha of the frame.
43 */
44
45 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
46 {
47 mlt_filter this = mlt_frame_pop_service( frame );
48 int variance = 200 * mlt_properties_get_double( MLT_FILTER_PROPERTIES( this ), "variance" );
49 int32_t key_val = mlt_properties_get_int( MLT_FILTER_PROPERTIES( this ), "key" );
50 uint8_t r = ( key_val >> 24 ) & 0xff;
51 uint8_t g = ( key_val >> 16 ) & 0xff;
52 uint8_t b = ( key_val >> 8 ) & 0xff;
53 uint8_t y, u, v;
54
55 RGB2YUV( r, g, b, y, u, v );
56
57 if ( mlt_frame_get_image( frame, image, format, width, height, writable ) == 0 )
58 {
59 uint8_t *alpha = mlt_frame_get_alpha_mask( frame );
60 uint8_t *p = *image;
61 int size = *width * *height / 2;
62 while ( size -- )
63 {
64 *alpha = alpha_value( *alpha, p, u, v, variance, 0 );
65 alpha ++;
66 *alpha = alpha_value( *alpha, p, u, v, variance, 1 );
67 alpha ++;
68 p += 4;
69 }
70 }
71
72 return 0;
73 }
74
75 /** Filter processing.
76 */
77
78 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
79 {
80 mlt_frame_push_service( frame, this );
81 mlt_frame_push_service( frame, filter_get_image );
82 return frame;
83 }
84
85 /** Constructor for the filter.
86 */
87
88 mlt_filter filter_chroma_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
89 {
90 mlt_filter this = mlt_filter_new( );
91 if ( this != NULL )
92 {
93 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "key", arg == NULL ? "0x0000ff00" : arg );
94 mlt_properties_set_double( MLT_FILTER_PROPERTIES( this ), "variance", 0.15 );
95 this->process = filter_process;
96 }
97 return this;
98 }
99