]> git.sesse.net Git - mlt/blob - src/modules/opengl/filter_movit_white_balance.cpp
f9d7e5a5e3620b43c9cc0546031dfb8e47dae2bf
[mlt] / src / modules / opengl / filter_movit_white_balance.cpp
1 /*
2  * filter_white_balance.cpp
3  * Copyright (C) 2013 Dan Dennedy <dan@dennedy.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <framework/mlt.h>
21 #include <string.h>
22 #include <math.h>
23 #include <assert.h>
24
25 #include "filter_glsl_manager.h"
26 #include <movit/white_balance_effect.h>
27
28 using namespace movit;
29
30 static double srgb8_to_linear(int c)
31 {
32         double x = c / 255.0f;
33         if (x < 0.04045f) {
34                 return (1.0/12.92f) * x;
35         } else {
36                 return pow((x + 0.055) * (1.0/1.055f), 2.4);
37         }
38 }
39
40 static int get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
41 {
42         mlt_filter filter = (mlt_filter) mlt_frame_pop_service( frame );
43         mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
44         GlslManager::get_instance()->lock_service( frame );
45         mlt_position position = mlt_filter_get_position( filter, frame );
46         mlt_position length = mlt_filter_get_length2( filter, frame );
47         int color_int = mlt_properties_anim_get_int( properties, "neutral_color", position, length );
48         RGBTriplet color(
49                 srgb8_to_linear((color_int >> 24) & 0xff),
50                 srgb8_to_linear((color_int >> 16) & 0xff),
51                 srgb8_to_linear((color_int >> 8) & 0xff)
52         );
53         mlt_properties_set_double( properties, "movit.parms.vec3.neutral_color[0]", color.r );
54         mlt_properties_set_double( properties, "movit.parms.vec3.neutral_color[1]", color.g );
55         mlt_properties_set_double( properties, "movit.parms.vec3.neutral_color[2]", color.b );
56         double output_color_temperature =
57                 mlt_properties_anim_get_double( properties, "color_temperature", position, length );
58         mlt_properties_set_double( properties, "movit.parms.float.output_color_temperature",
59                 output_color_temperature );
60         GlslManager::get_instance()->unlock_service( frame );
61         *format = mlt_image_glsl;
62         int error = mlt_frame_get_image( frame, image, format, width, height, writable );
63         GlslManager::set_effect_input( MLT_FILTER_SERVICE( filter ), frame, (mlt_service) *image );
64         GlslManager::set_effect( MLT_FILTER_SERVICE( filter ), frame, new WhiteBalanceEffect );
65         *image = (uint8_t *) MLT_FILTER_SERVICE( filter );
66         return error;
67 }
68
69 static mlt_frame process( mlt_filter filter, mlt_frame frame )
70 {
71         mlt_frame_push_service( frame, filter );
72         mlt_frame_push_get_image( frame, get_image );
73         return frame;
74 }
75
76 extern "C" {
77
78 mlt_filter filter_white_balance_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
79 {
80         mlt_filter filter = NULL;
81         GlslManager* glsl = GlslManager::get_instance();
82
83         if ( glsl && ( filter = mlt_filter_new() ) ) {
84                 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
85                 glsl->add_ref( properties );
86                 mlt_properties_set( properties, "neutral_color", arg? arg : "#7f7f7f" );
87                 mlt_properties_set_double( properties, "color_temperature", 6500.0 );
88                 filter->process = process;
89         }
90         return filter;
91 }
92
93 }