Merge ../mlt
[melted] / src / modules / vmfx / filter_mono.c
1 /*
2 * filter_mono.c -- Arbitrary alpha channel shaping
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 <string.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 /** Get the images and apply the luminance of the mask to the alpha of the frame.
29 */
30
31 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
32 {
33 int use_alpha = mlt_deque_pop_back_int( MLT_FRAME_IMAGE_STACK( this ) );
34 int midpoint = mlt_deque_pop_back_int( MLT_FRAME_IMAGE_STACK( this ) );
35 int invert = mlt_deque_pop_back_int( MLT_FRAME_IMAGE_STACK( this ) );
36
37 // Render the frame
38 if ( mlt_frame_get_image( this, image, format, width, height, writable ) == 0 )
39 {
40 uint8_t *p = *image;
41 uint8_t A = invert? 235 : 16;
42 uint8_t B = invert? 16 : 235;
43 int size = *width * *height;
44
45 if ( !use_alpha )
46 {
47 while( size -- )
48 {
49 if ( *p < midpoint )
50 *p ++ = A;
51 else
52 *p ++ = B;
53 *p ++ = 128;
54 }
55 }
56 else
57 {
58 uint8_t *alpha = mlt_frame_get_alpha_mask( this );
59 while( size -- )
60 {
61 if ( *alpha ++ < midpoint )
62 *p ++ = A;
63 else
64 *p ++ = B;
65 *p ++ = 128;
66 }
67 }
68 }
69
70 return 0;
71 }
72
73 /** Filter processing.
74 */
75
76 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
77 {
78 int midpoint = mlt_properties_get_int( MLT_FILTER_PROPERTIES( this ), "midpoint" );
79 int use_alpha = mlt_properties_get_int( MLT_FILTER_PROPERTIES( this ), "use_alpha" );
80 int invert = mlt_properties_get_int( MLT_FILTER_PROPERTIES( this ), "invert" );
81 mlt_deque_push_back_int( MLT_FRAME_IMAGE_STACK( frame ), invert );
82 mlt_deque_push_back_int( MLT_FRAME_IMAGE_STACK( frame ), midpoint );
83 mlt_deque_push_back_int( MLT_FRAME_IMAGE_STACK( frame ), use_alpha );
84 mlt_frame_push_get_image( frame, filter_get_image );
85 return frame;
86 }
87
88 /** Constructor for the filter.
89 */
90
91 mlt_filter filter_mono_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
92 {
93 mlt_filter this = mlt_filter_new( );
94 if ( this != NULL )
95 {
96 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "midpoint", 128 );
97 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "use_alpha", 0 );
98 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "invert", 0 );
99 this->process = filter_process;
100 }
101 return this;
102 }
103