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