+ Changed license of plugins to LGPL
[melted] / src / modules / vmfx / filter_chroma_hold.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 "filter_chroma.h"
22 #include <stdlib.h>
23 #include <framework/mlt_factory.h>
24 #include <framework/mlt_frame.h>
25 #include <framework/mlt_producer.h>
26 #include <framework/mlt_geometry.h>
27
28 static inline int in_range( uint8_t v, uint8_t c, int var )
29 {
30 return ( ( int )v >= c - var ) && ( ( int )v <= c + var );
31 }
32
33 static inline uint8_t alpha_value( uint8_t a, uint8_t *p, uint8_t u, uint8_t v, int var )
34 {
35 return ( in_range( *( p + 1 ), u, var ) && in_range( *( p + 3 ), v, var ) ) ? 0 : a;
36 }
37
38 /** Get the images and map the chroma to the alpha of the frame.
39 */
40
41 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
42 {
43 mlt_filter this = mlt_frame_pop_service( frame );
44 char *key = mlt_properties_get( MLT_FILTER_PROPERTIES( this ), "key" );
45 int variance = 200 * mlt_properties_get_double( MLT_FILTER_PROPERTIES( this ), "variance" );
46 int32_t key_val = strtol( key, &key, 0 );
47 uint8_t b = key_val & 0xff;
48 uint8_t g = ( key_val >> 8 ) & 0xff;
49 uint8_t r = ( key_val >> 16 ) & 0xff;
50 uint8_t y, u, v;
51
52 RGB2YUV( r, g, b, y, u, v );
53
54 if ( mlt_frame_get_image( frame, image, format, width, height, writable ) == 0 )
55 {
56 uint8_t alpha = 0;
57 uint8_t *p = *image;
58 int size = *width * *height / 2;
59 while ( size -- )
60 {
61 alpha = alpha_value( 255, p, u, v, variance );
62 p ++;
63 if ( alpha )
64 *p ++ = 128;
65 else
66 p ++;
67 alpha = alpha_value( 255, p, v, u, variance );
68 p ++;
69 if ( alpha )
70 *p ++ = 128;
71 else
72 p ++;
73 }
74 }
75
76 return 0;
77 }
78
79 /** Filter processing.
80 */
81
82 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
83 {
84 mlt_frame_push_service( frame, this );
85 mlt_frame_push_service( frame, filter_get_image );
86 return frame;
87 }
88
89 /** Constructor for the filter.
90 */
91
92 mlt_filter filter_chroma_hold_init( char *arg )
93 {
94 mlt_filter this = mlt_filter_new( );
95 if ( this != NULL )
96 {
97 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "key", arg == NULL ? "0xc00000" : arg );
98 mlt_properties_set_double( MLT_FILTER_PROPERTIES( this ), "variance", 0.3 );
99 this->process = filter_process;
100 }
101 return this;
102 }
103