]> git.sesse.net Git - mlt/blob - src/modules/vmfx/filter_mono.c
Fix reading binary files on Windows.
[mlt] / 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         *format = mlt_image_yuv422;
39         if ( mlt_frame_get_image( this, image, format, width, height, writable ) == 0 )
40         {
41                 uint8_t *p = *image;
42                 uint8_t A = invert? 235 : 16;
43                 uint8_t B = invert? 16 : 235;
44                 int size = *width * *height;
45
46                 if ( !use_alpha )
47                 {
48                         while( size -- )
49                         {
50                                 if ( *p < midpoint )
51                                         *p ++ = A;
52                                 else
53                                         *p ++ = B;
54                                 *p ++ = 128;
55                         }
56                 }
57                 else
58                 {
59                         uint8_t *alpha = mlt_frame_get_alpha_mask( this );
60                         while( size -- )
61                         {
62                                 if ( *alpha ++ < midpoint )
63                                         *p ++ = A;
64                                 else
65                                         *p ++ = B;
66                                 *p ++ = 128;
67                         }
68                 }
69         }
70
71         return 0;
72 }
73
74 /** Filter processing.
75 */
76
77 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
78 {
79         int midpoint = mlt_properties_get_int( MLT_FILTER_PROPERTIES( this ), "midpoint" );
80         int use_alpha = mlt_properties_get_int( MLT_FILTER_PROPERTIES( this ), "use_alpha" );
81         int invert = mlt_properties_get_int( MLT_FILTER_PROPERTIES( this ), "invert" );
82         mlt_deque_push_back_int( MLT_FRAME_IMAGE_STACK( frame ), invert );
83         mlt_deque_push_back_int( MLT_FRAME_IMAGE_STACK( frame ), midpoint );
84         mlt_deque_push_back_int( MLT_FRAME_IMAGE_STACK( frame ), use_alpha );
85         mlt_frame_push_get_image( frame, filter_get_image );
86         return frame;
87 }
88
89 /** Constructor for the filter.
90 */
91
92 mlt_filter filter_mono_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
93 {
94         mlt_filter this = mlt_filter_new( );
95         if ( this != NULL )
96         {
97                 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "midpoint", 128 );
98                 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "use_alpha", 0 );
99                 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "invert", 0 );
100                 this->process = filter_process;
101         }
102         return this;
103 }
104