6 #include "white_balance_effect.h"
10 using namespace Eigen;
14 // Temperature is in Kelvin. Formula from http://en.wikipedia.org/wiki/Planckian_locus#Approximation .
15 Vector3d convert_color_temperature_to_xyz(float T)
17 double invT = 1.0 / T;
21 assert(T <= 15000.0f);
24 x = ((-0.2661239e9 * invT - 0.2343580e6) * invT + 0.8776956e3) * invT + 0.179910;
26 x = ((-3.0258469e9 * invT + 2.1070379e6) * invT + 0.2226347e3) * invT + 0.240390;
30 y = ((-1.1063814 * x - 1.34811020) * x + 2.18555832) * x - 0.20219683;
31 } else if (T <= 4000.0f) {
32 y = ((-0.9549476 * x - 1.37418593) * x + 2.09137015) * x - 0.16748867;
34 y = (( 3.0817580 * x - 5.87338670) * x + 3.75112997) * x - 0.37001483;
37 return Vector3d(x, y, 1.0 - x - y);
40 // Assuming sRGB primaries, from Wikipedia.
41 double rgb_to_xyz_matrix[9] = {
42 0.4124, 0.2126, 0.0193,
43 0.3576, 0.7152, 0.1192,
44 0.1805, 0.0722, 0.9505,
48 * There are several different LMS spaces, at least according to Wikipedia.
49 * Through practical testing, I've found most of them (like the CIECAM02 model)
50 * to yield a result that is too reddish in practice, possibly because they
51 * are intended for different illuminants than what sRGB assumes.
53 * This is the RLAB space, normalized to D65, which means that the standard
54 * D65 illuminant (x=0.31271, y=0.32902, z=1-y-x) gives L=M=S under this transformation.
55 * This makes sense because sRGB (which is used to derive those XYZ values
56 * in the first place) assumes the D65 illuminant, and so the D65 illuminant
57 * also gives R=G=B in sRGB.
59 const double xyz_to_lms_matrix[9] = {
62 -0.0808, 0.0457, 0.9182,
66 * For a given reference color (given in XYZ space),
67 * compute scaling factors for L, M and S. What we want at the output is equal L, M and S
68 * for the reference color (making it a neutral illuminant), or sL ref_L = sM ref_M = sS ref_S.
69 * This removes two degrees of freedom for our system, and we only need to find fL.
71 * A reasonable last constraint would be to preserve Y, approximately the brightness,
72 * for the reference color. Since L'=M'=S' and the Y row of the LMS-to-XYZ matrix
73 * sums to unity, we know that Y'=L', and it's easy to find the fL that sets Y'=Y.
75 Vector3d compute_lms_scaling_factors(const Vector3d &xyz)
77 Vector3d lms = Map<const Matrix3d>(xyz_to_lms_matrix) * xyz;
82 double scale_l = xyz[1] / l;
83 double scale_m = scale_l * (l / m);
84 double scale_s = scale_l * (l / s);
86 return Vector3d(scale_l, scale_m, scale_s);
91 WhiteBalanceEffect::WhiteBalanceEffect()
92 : neutral_color(0.5f, 0.5f, 0.5f),
93 output_color_temperature(6500.0f)
95 register_vec3("neutral_color", (float *)&neutral_color);
96 register_float("output_color_temperature", &output_color_temperature);
99 std::string WhiteBalanceEffect::output_fragment_shader()
101 return read_file("white_balance_effect.frag");
104 void WhiteBalanceEffect::set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
106 Vector3d rgb(neutral_color.r, neutral_color.g, neutral_color.b);
107 Vector3d xyz = Map<const Matrix3d>(rgb_to_xyz_matrix) * rgb;
108 Vector3d lms_scale = compute_lms_scaling_factors(xyz);
111 * Now apply the color balance. Simply put, we find the chromacity point
112 * for the desired white temperature, see what LMS scaling factors they
113 * would have given us, and then reverse that transform. For T=6500K,
114 * the default, this gives us nearly an identity transform (but only nearly,
115 * since the D65 illuminant does not exactly match the results of T=6500K);
116 * we normalize so that T=6500K really is a no-op.
118 Vector3d white_xyz = convert_color_temperature_to_xyz(output_color_temperature);
119 Vector3d lms_scale_white = compute_lms_scaling_factors(white_xyz);
121 Vector3d ref_xyz = convert_color_temperature_to_xyz(6500.0f);
122 Vector3d lms_scale_ref = compute_lms_scaling_factors(ref_xyz);
124 lms_scale[0] *= lms_scale_ref[0] / lms_scale_white[0];
125 lms_scale[1] *= lms_scale_ref[1] / lms_scale_white[1];
126 lms_scale[2] *= lms_scale_ref[2] / lms_scale_white[2];
129 * Concatenate all the different linear operations into a single 3x3 matrix.
130 * Note that since we postmultiply our vectors, the order of the matrices
131 * has to be the opposite of the execution order.
133 Matrix3d corr_matrix =
134 Map<const Matrix3d>(rgb_to_xyz_matrix).inverse() *
135 Map<const Matrix3d>(xyz_to_lms_matrix).inverse() *
136 lms_scale.asDiagonal() *
137 Map<const Matrix3d>(xyz_to_lms_matrix) *
138 Map<const Matrix3d>(rgb_to_xyz_matrix);
139 set_uniform_mat3(glsl_program_num, prefix, "correction_matrix", corr_matrix);