]> git.sesse.net Git - mlt/blob - src/modules/core/filter_brightness.c
Add animated level parameter to brightness
[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 #define UNSET "unset"
31
32 /** Do it :-).
33 */
34
35 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
36 {
37         mlt_filter filter =  (mlt_filter) mlt_frame_pop_service( frame );
38         mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
39         mlt_position position = mlt_filter_get_position( filter, frame );
40         mlt_position length = mlt_filter_get_length2( filter, frame );
41
42         // Get the image
43         *format = mlt_image_yuv422;
44         int error = mlt_frame_get_image( frame, image, format, width, height, 1 );
45
46         // Only process if we have no error and a valid colour space
47         if ( error == 0 )
48         {
49                 double level = 1.0;
50
51                 // Use animated "level" property only if it has been set since init
52                 char* level_property = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "level" );
53                 if ( strcmp( level_property, UNSET ) != 0 )
54                 {
55                         level = mlt_properties_anim_get_double( properties, "level", position, length );
56                 }
57                 else
58                 {
59                         // Get level using old "start,"end" mechanics
60                         // Get the starting brightness level
61                         level = fabs( mlt_properties_get_double( MLT_FILTER_PROPERTIES( filter ), "start" ) );
62
63                         // If there is an end adjust gain to the range
64                         if ( mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "end" ) != NULL )
65                         {
66                                 // Determine the time position of this frame in the transition duration
67                                 double end = fabs( mlt_properties_get_double( MLT_FILTER_PROPERTIES( filter ), "end" ) );
68                                 level += ( end - level ) * mlt_filter_get_progress( filter, frame );
69                         }
70                 }
71
72                 // Only process if level is something other than 1
73                 if ( level != 1.0 )
74                 {
75                         int i = *width * *height + 1;
76                         uint8_t *p = *image;
77                         int32_t m = level * ( 1 << 16 );
78                         int32_t n = 128 * ( ( 1 << 16 ) - m );
79
80                         while ( --i )
81                         {
82                                 p[0] = CLAMP( (p[0] * m) >> 16, 16, 235 );
83                                 p[1] = CLAMP( (p[1] * m + n) >> 16, 16, 240 );
84                                 p += 2;
85                         }
86                 }
87         }
88
89         return error;
90 }
91
92 /** Filter processing.
93 */
94
95 static mlt_frame filter_process( mlt_filter filter, mlt_frame frame )
96 {
97         mlt_frame_push_service( frame, filter );
98         mlt_frame_push_get_image( frame, filter_get_image );
99
100         return frame;
101 }
102
103 /** Constructor for the filter.
104 */
105
106 mlt_filter filter_brightness_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
107 {
108         mlt_filter filter = mlt_filter_new( );
109         if ( filter != NULL )
110         {
111                 filter->process = filter_process;
112                 mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "start", arg == NULL ? "1" : arg );
113                 mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "level", UNSET );
114         }
115         return filter;
116 }
117