framework: remove global profile, rather share one mlt_profile across a service netwo...
[melted] / 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 /** Do it :-).
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 // Get the image
34 int error = mlt_frame_get_image( this, image, format, width, height, 1 );
35
36 // Only process if we have no error and a valid colour space
37 if ( error == 0 && *format == mlt_image_yuv422 )
38 {
39 // Get the brightness level
40 double level = mlt_properties_get_double( MLT_FRAME_PROPERTIES( this ), "brightness" );
41
42 // Only process if level is something other than 1
43 if ( level != 1.0 )
44 {
45 uint8_t *p = *image;
46 uint8_t *q = *image + *width * *height * 2;
47 int32_t x = 0;
48 int32_t m = level * ( 1 << 16 );
49
50 while ( p != q )
51 {
52 x = ( *p * m ) >> 16;
53 *p = x < 16 ? 16 : x > 235 ? 235 : x;
54 p += 2;
55 }
56 }
57 }
58
59 return error;
60 }
61
62 /** Filter processing.
63 */
64
65 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
66 {
67 // Get the starting brightness level
68 double level = fabs( mlt_properties_get_double( MLT_FILTER_PROPERTIES( this ), "start" ) );
69
70 // If there is an end adjust gain to the range
71 if ( mlt_properties_get( MLT_FILTER_PROPERTIES( this ), "end" ) != NULL )
72 {
73 // Determine the time position of this frame in the transition duration
74 mlt_position in = mlt_filter_get_in( this );
75 mlt_position out = mlt_filter_get_out( this );
76 mlt_position time = mlt_frame_get_position( frame );
77 double position = ( double )( time - in ) / ( double )( out - in + 1 );
78 double end = fabs( mlt_properties_get_double( MLT_FILTER_PROPERTIES( this ), "end" ) );
79 level += ( end - level ) * position;
80 }
81
82 // Push the frame filter
83 mlt_properties_set_double( MLT_FRAME_PROPERTIES( frame ), "brightness", level );
84 mlt_frame_push_get_image( frame, filter_get_image );
85
86 return frame;
87 }
88
89 /** Constructor for the filter.
90 */
91
92 mlt_filter filter_brightness_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 this->process = filter_process;
98 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "start", arg == NULL ? "1" : arg );
99 }
100 return this;
101 }
102