]> git.sesse.net Git - movit/blob - white_balance_effect.cpp
Be more consistent about how we handle textures and bouncing; either bounce them...
[movit] / white_balance_effect.cpp
1 #include <math.h>
2 #include <assert.h>
3
4 #include "white_balance_effect.h"
5 #include "util.h"
6 #include "opengl.h"
7
8 namespace {
9
10 // Temperature is in Kelvin. Formula from http://en.wikipedia.org/wiki/Planckian_locus#Approximation .
11 void convert_color_temperature_to_xyz(float T, float *x, float *y, float *z)
12 {
13         double invT = 1.0 / T;
14         double xc, yc;
15
16         assert(T >= 1000.0f);
17         assert(T <= 15000.0f);
18
19         if (T <= 4000.0f) {
20                 xc = ((-0.2661239e9 * invT - 0.2343580e6) * invT + 0.8776956e3) * invT + 0.179910;
21         } else {
22                 xc = ((-3.0258469e9 * invT + 2.1070379e6) * invT + 0.2226347e3) * invT + 0.240390;
23         }
24
25         if (T <= 2222.0f) {
26                 yc = ((-1.1063814 * xc - 1.34811020) * xc + 2.18555832) * xc - 0.20219683;
27         } else if (T <= 4000.0f) {
28                 yc = ((-0.9549476 * xc - 1.37418593) * xc + 2.09137015) * xc - 0.16748867;
29         } else {
30                 yc = (( 3.0817580 * xc - 5.87338670) * xc + 3.75112997) * xc - 0.37001483;
31         }
32
33         *x = xc;
34         *y = yc;
35         *z = 1.0 - xc - yc;
36 }
37
38 // Assuming sRGB primaries, from Wikipedia.
39 static const Matrix3x3 rgb_to_xyz_matrix = {
40         0.4124, 0.2126, 0.0193, 
41         0.3576, 0.7152, 0.1192,
42         0.1805, 0.0722, 0.9505,
43 };
44
45 /*
46  * There are several different LMS spaces, at least according to Wikipedia.
47  * Through practical testing, I've found most of them (like the CIECAM02 model)
48  * to yield a result that is too reddish in practice, possibly because they
49  * are intended for different illuminants than what sRGB assumes. 
50  *
51  * This is the RLAB space, normalized to D65, which means that the standard
52  * D65 illuminant (x=0.31271, y=0.32902, z=1-y-x) gives L=M=S under this transformation.
53  * This makes sense because sRGB (which is used to derive those XYZ values
54  * in the first place) assumes the D65 illuminant, and so the D65 illuminant
55  * also gives R=G=B in sRGB.
56  */
57 static const Matrix3x3 xyz_to_lms_matrix = {
58          0.4002, -0.2263,    0.0,
59          0.7076,  1.1653,    0.0,
60         -0.0808,  0.0457, 0.9182,
61 };
62
63 /*
64  * For a given reference color (given in XYZ space),
65  * compute scaling factors for L, M and S. What we want at the output is equal L, M and S
66  * for the reference color (making it a neutral illuminant), or sL ref_L = sM ref_M = sS ref_S.
67  * This removes two degrees of freedom for our system, and we only need to find fL.
68  *
69  * A reasonable last constraint would be to preserve Y, approximately the brightness,
70  * for the reference color. Since L'=M'=S' and the Y row of the LMS-to-XYZ matrix
71  * sums to unity, we know that Y'=L', and it's easy to find the fL that sets Y'=Y.
72  */
73 static void compute_lms_scaling_factors(float x, float y, float z, float *scale_l, float *scale_m, float *scale_s)
74 {
75         Matrix3x3 xyz_to_rgb_matrix;
76         invert_3x3_matrix(rgb_to_xyz_matrix, xyz_to_rgb_matrix);
77
78         float l, m, s;
79         multiply_3x3_matrix_float3(xyz_to_rgb_matrix, x, y, z, &l, &m, &s);
80
81         *scale_l = y / l;
82         *scale_m = *scale_l * (l / m);
83         *scale_s = *scale_l * (l / s);
84 }
85
86 }  // namespace
87
88 WhiteBalanceEffect::WhiteBalanceEffect()
89         : neutral_color(0.5f, 0.5f, 0.5f),
90           output_color_temperature(6500.0f)
91 {
92         register_vec3("neutral_color", (float *)&neutral_color);
93         register_float("output_color_temperature", &output_color_temperature);
94 }
95
96 std::string WhiteBalanceEffect::output_fragment_shader()
97 {
98         return read_file("white_balance_effect.frag");
99 }
100
101 void WhiteBalanceEffect::set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
102 {
103         float x, y, z;
104         multiply_3x3_matrix_float3(rgb_to_xyz_matrix, neutral_color.r, neutral_color.g, neutral_color.b, &x, &y, &z);
105
106         float l, m, s;
107         multiply_3x3_matrix_float3(xyz_to_lms_matrix, x, y, z, &l, &m, &s);
108
109         float l_scale, m_scale, s_scale;
110         compute_lms_scaling_factors(x, y, z, &l_scale, &m_scale, &s_scale);
111
112         /*
113          * Now apply the color balance. Simply put, we find the chromacity point
114          * for the desired white temperature, see what LMS scaling factors they
115          * would have given us, and then reverse that transform. For T=6500K,
116          * the default, this gives us nearly an identity transform (but only nearly,
117          * since the D65 illuminant does not exactly match the results of T=6500K);
118          * we normalize so that T=6500K really is a no-op.
119          */
120         float white_x, white_y, white_z, l_scale_white, m_scale_white, s_scale_white;
121         convert_color_temperature_to_xyz(output_color_temperature, &white_x, &white_y, &white_z);
122         compute_lms_scaling_factors(white_x, white_y, white_z, &l_scale_white, &m_scale_white, &s_scale_white);
123         
124         float ref_x, ref_y, ref_z, l_scale_ref, m_scale_ref, s_scale_ref;
125         convert_color_temperature_to_xyz(6500.0f, &ref_x, &ref_y, &ref_z);
126         compute_lms_scaling_factors(ref_x, ref_y, ref_z, &l_scale_ref, &m_scale_ref, &s_scale_ref);
127         
128         l_scale *= l_scale_ref / l_scale_white;
129         m_scale *= m_scale_ref / m_scale_white;
130         s_scale *= s_scale_ref / s_scale_white;
131         
132         /*
133          * Concatenate all the different linear operations into a single 3x3 matrix.
134          * Note that since we postmultiply our vectors, the order of the matrices
135          * has to be the opposite of the execution order.
136          */
137         Matrix3x3 lms_to_xyz_matrix, xyz_to_rgb_matrix;
138         invert_3x3_matrix(xyz_to_lms_matrix, lms_to_xyz_matrix);
139         invert_3x3_matrix(rgb_to_xyz_matrix, xyz_to_rgb_matrix);
140
141         Matrix3x3 temp, temp2, corr_matrix;
142         Matrix3x3 lms_scale_matrix = {
143                 l_scale,    0.0f,    0.0f,
144                    0.0f, m_scale,    0.0f,
145                    0.0f,    0.0f, s_scale,
146         };
147         multiply_3x3_matrices(xyz_to_rgb_matrix, lms_to_xyz_matrix, temp);
148         multiply_3x3_matrices(temp, lms_scale_matrix, temp2);
149         multiply_3x3_matrices(temp2, xyz_to_lms_matrix, temp);
150         multiply_3x3_matrices(temp, rgb_to_xyz_matrix, corr_matrix);
151
152         set_uniform_mat3(glsl_program_num, prefix, "correction_matrix", corr_matrix);
153 }