]> git.sesse.net Git - mlt/blob - src/modules/core/filter_brightness.c
Use NULL instead of magic string to flag unset parameter
[mlt] / src / modules / core / filter_brightness.c
1 /*
2  * filter_brightness.c -- gamma filter
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <math.h>
27 #include <string.h>
28
29 #define CLAMP( x, min, max ) (x) < (min) ? (min) : (x) > (max) ? (max) : (x)
30
31 /** Do it :-).
32 */
33
34 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
35 {
36         mlt_filter filter =  (mlt_filter) mlt_frame_pop_service( frame );
37         mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
38         mlt_position position = mlt_filter_get_position( filter, frame );
39         mlt_position length = mlt_filter_get_length2( filter, frame );
40
41         // Get the image
42         *format = mlt_image_yuv422;
43         int error = mlt_frame_get_image( frame, image, format, width, height, 1 );
44
45         // Only process if we have no error and a valid colour space
46         if ( error == 0 )
47         {
48                 double level = 1.0;
49
50                 // Use animated "level" property only if it has been set since init
51                 char* level_property = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "level" );
52                 if ( level_property != NULL )
53                 {
54                         level = mlt_properties_anim_get_double( properties, "level", position, length );
55                 }
56                 else
57                 {
58                         // Get level using old "start,"end" mechanics
59                         // Get the starting brightness level
60                         level = fabs( mlt_properties_get_double( MLT_FILTER_PROPERTIES( filter ), "start" ) );
61
62                         // If there is an end adjust gain to the range
63                         if ( mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "end" ) != NULL )
64                         {
65                                 // Determine the time position of this frame in the transition duration
66                                 double end = fabs( mlt_properties_get_double( MLT_FILTER_PROPERTIES( filter ), "end" ) );
67                                 level += ( end - level ) * mlt_filter_get_progress( filter, frame );
68                         }
69                 }
70
71                 // Only process if level is something other than 1
72                 if ( level != 1.0 )
73                 {
74                         int i = *width * *height + 1;
75                         uint8_t *p = *image;
76                         int32_t m = level * ( 1 << 16 );
77                         int32_t n = 128 * ( ( 1 << 16 ) - m );
78
79                         while ( --i )
80                         {
81                                 p[0] = CLAMP( (p[0] * m) >> 16, 16, 235 );
82                                 p[1] = CLAMP( (p[1] * m + n) >> 16, 16, 240 );
83                                 p += 2;
84                         }
85                 }
86         }
87
88         return error;
89 }
90
91 /** Filter processing.
92 */
93
94 static mlt_frame filter_process( mlt_filter filter, mlt_frame frame )
95 {
96         mlt_frame_push_service( frame, filter );
97         mlt_frame_push_get_image( frame, filter_get_image );
98
99         return frame;
100 }
101
102 /** Constructor for the filter.
103 */
104
105 mlt_filter filter_brightness_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
106 {
107         mlt_filter filter = mlt_filter_new( );
108         if ( filter != NULL )
109         {
110                 filter->process = filter_process;
111                 mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "start", arg == NULL ? "1" : arg );
112                 mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "level", NULL );
113         }
114         return filter;
115 }
116