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