Debian Codegraph @

src/filter/colgate/colgate.c

raw symbols SCIP
1 /* colgate.c
2 * Copyright (C) 2012 Steinar H. Gunderson <[email protected]>
3 *
4 * Color correction in LMS color space.
5 *
6 * This plugin is intended for the same uses as the balanc0r plugin,
7 * but differs from balanc0r in two important aspects:
8 *
9 * 1. It operates in the LMS color space, which is a better approximation
10 * to the human color sensation than RGB is. This allows for more
11 * natural color changes. (Note that this inevitably requires that we
12 * we work in linear color space, not the nonlinear space pixels are
13 * typically stored in.)
14 *
15 * 2. Its choice of neutral color is not limited by the Planckian locus
16 * (typically expressed in Kelvins) like balanc0r is; you can choose
17 * any color, even those that are not typically regarded as “white”.
18 * If you want a color cast (e.g. a warmer scene), this can be
19 * adjusted with a separate control for color temperature.
20 *
21 * Color is a very complex topic, and this plugin makes no claims to
22 * being 100% correct in any way; however, the results appear visually
23 * much more meaningful to me than the balanc0r plugin does, although it
24 * also uses significantly more CPU time.
25 *
26 * frei0r plugins are not given any meaningful information about input or
27 * output color space. We assume sRGB, since that typically matches people's
28 * viewing devices pretty well. sRGB in turn very closely matches ITU-R Rec.
29 * BT.709, the standard color space for HDTV; they share the same RGB
30 * primaries, and have a similar gamma (2.35 or 2.4, versus sRGB's curve that
31 * is approximately a gamma curve at 2.2). SDTV uses a different color space,
32 * ITU-R Rec. BT.601, which has somewhat different primaries and a gamma of
33 * 2.2, but in practice, sRGB should be an okay approximation for this as well.
34 *
35 * The color matrices used are typically from Wikipedia, and the inverses
36 * are computed by Octave if nothing else is mentioned.
37 *
38 *
39 * This program is free software; you can redistribute it and/or modify
40 * it under the terms of the GNU General Public License as published by
41 * the Free Software Foundation; either version 2 of the License, or
42 * (at your option) any later version.
43 *
44 * This program is distributed in the hope that it will be useful,
45 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47 * GNU General Public License for more details.
48 *
49 * You should have received a copy of the GNU General Public License
50 * along with this program; if not, write to the Free Software
51 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
52 */
53
54 /*
55 * We use fixed point, since conversion back and forth to floating-point is
56 * slow. (This also enables us to use LUTs in an efficient way for lookup
57 * to and from sRGB, as opposed to a pow()-based solution, which is very slow.)
58 *
59 * We need to think a bit about the range and precision of the different
60 * elements to get the best results here, since linear RGB can span quite
61 * some range. So:
62 *
63 * - Input pixels (which are in linear RGB, always 0..1) are stored as 1.15.
64 * - Matrix elements are s4.10.
65 *
66 * Matrix elements up to +/- 16 should give us some headroom; extreme color
67 * adjustments typically have elements of 5-6.
68 *
69 * Our standard operation is input_pixel * matrix_element, which becomes s5.25.
70 * We add three of them, which would seem to be able to overflow, but doesn't,
71 * since the largest pixel is 1.0 (represented as 2^15):
72 *
73 * 3 * 2^15 * (2^14 - 1) ~= 3 * 2^29 < 2^31.
74 *
75 * It _is_ larger than 2^30, though, so we don't have any bits to spare here.
76 */
77 #define INPUT_PIXEL_BITScxx . . $ `src/filter/colgate/colgate.c:77:9`! 15
78 #define MATRIX_ELEMENT_FRAC_BITScxx . . $ `src/filter/colgate/colgate.c:78:9`! 10
79 #define MATRIX_ELEMENT_BITScxx . . $ `src/filter/colgate/colgate.c:79:9`! (4 + MATRIX_ELEMENT_FRAC_BITScxx . . $ `src/filter/colgate/colgate.c:78:9`!)
80
81 #include <stdlib.h>
82 #include <assert.h>
83 #include <math.h>
84 #include <stdio.h>
85
86 #ifdef __SSE2__cxx . . $ `<built-in>:383:9`!
87 #include <emmintrin.h>
88 #endif
89
90 #include "frei0r.h"cxx . . $ `<file>/include/frei0r.h`/
91 #include "frei0r/math.h"cxx . . $ `<file>/include/frei0r/math.h`/
92
93 enum ParamIndexcxx . . $ ParamIndex# {
94 NEUTRAL_COLORcxx . . $ NEUTRAL_COLOR.,
95 COLOR_TEMPERATUREcxx . . $ COLOR_TEMPERATURE.,
96 };
97
98 // Row major (opposite of OpenGL).
99 typedef float Matrix3x3cxx . . $ Matrix3x3#[9];
100
101 typedef struct colgate_instancecxx . . $ colgate_instance#
102 {
103 unsigned widthcxx . . $ colgate_instance#width.;
104 unsigned heightcxx . . $ colgate_instance#height.;
105 f0r_param_color_tcxx . . $ f0r_param_color_t# neutral_colorcxx . . $ colgate_instance#neutral_color.;
106 double color_temperaturecxx . . $ colgate_instance#color_temperature.;
107
108 #ifdef __SSE2__cxx . . $ `<built-in>:383:9`!
109 __m128icxx . . $ __m128i# premult_rcxx . . $ colgate_instance#premult_r.[256];
110 __m128icxx . . $ __m128i# premult_gcxx . . $ colgate_instance#premult_g.[256];
111 __m128icxx . . $ __m128i# premult_bcxx . . $ colgate_instance#premult_b.[256];
112 #else
113 int premult_r[256][3];
114 int premult_g[256][3];
115 int premult_b[256][3];
116 #endif
117 } colgate_instance_tcxx . . $ colgate_instance_t#;
118
119 // Assumes input value in [0..255]; output value is normalized.
120 static float convert_srgb_to_linear_rgbcxx . . $ convert_srgb_to_linear_rgb(fbcf3211dfe1bfd1).(float x)
121 {
122 if (x < 255.0f * 0.04045f) {
123 return x * (1.0f / (255.0f * 12.92f));
124 } else {
125 return pow((x + 255.0f * 0.055) * (1.0 / (255.0f * 1.055f)), 2.4);
126 }
127 }
128
129 // Assumes normalized input value; output value in [0..255].
130 static float convert_linear_rgb_to_srgbcxx . . $ convert_linear_rgb_to_srgb(fbcf3211dfe1bfd1).(float x)
131 {
132 if (x < 0.0031308f) {
133 return (255.0f * 12.92f) * x;
134 } else {
135 return ((255.0f * 1.055f) * pow(x, 1.0f / 2.4f)) - (0.055 * 255.0f);
136 }
137 }
138
139 /*
140 * For linear -> sRGB, we need at least 13 bits to be able to distinguish all
141 * input values; we go for 14 to get some extra accuracy. This results in an 16
142 * kB LUT, but we generally don't need the L1 cache for a lot of other things
143 * anyway, so hopefully the LUT can mostly stay in L1 cache.
144 */
145 #define REVERSE_LUT_BITScxx . . $ `src/filter/colgate/colgate.c:145:9`! 14
146 #define REVERSE_LUT_SIZEcxx . . $ `src/filter/colgate/colgate.c:146:9`! (1 << REVERSE_LUT_BITScxx . . $ `src/filter/colgate/colgate.c:145:9`!)
147
148 static uint8_tcxx . . $ uint8_t# linear_rgb_to_srgb_lutcxx . . $ linear_rgb_to_srgb_lut.[REVERSE_LUT_SIZEcxx . . $ `src/filter/colgate/colgate.c:146:9`!];
149
150 static void fill_srgb_lutcxx . . $ fill_srgb_lut(d878ad36e69247c7).()
151 {
152 int i;
153 for (i = 0; i < REVERSE_LUT_SIZEcxx . . $ `src/filter/colgate/colgate.c:146:9`!; ++i) {
154 // Subtract 0.5 to compensate for the fact that we don't round
155 // (which, for our purposes, would entail _adding_ 0.5) at lookup time.
156 float x = (i - 0.5) / (float)(REVERSE_LUT_SIZEcxx . . $ `src/filter/colgate/colgate.c:146:9`!);
157 int srgb = lrintfcxx . . $ lrintf(2b53f6384d78b03a).(convert_linear_rgb_to_srgbcxx . . $ convert_linear_rgb_to_srgb(fbcf3211dfe1bfd1).(x));
158 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(srgb >= 0 && srgb <= 255);
159 linear_rgb_to_srgb_lutcxx . . $ linear_rgb_to_srgb_lut.[i] = srgb;
160 }
161 }
162
163 // Multiply two 3x3 matrices.
164 static void multiply_3x3_matricescxx . . $ multiply_3x3_matrices(e402dac2b8bda508).(const Matrix3x3cxx . . $ Matrix3x3# a, const Matrix3x3cxx . . $ Matrix3x3# b, Matrix3x3cxx . . $ Matrix3x3# result)
165 {
166 result[0] = a[0] * b[0] + a[1] * b[3] + a[2] * b[6];
167 result[3] = a[3] * b[0] + a[4] * b[3] + a[5] * b[6];
168 result[6] = a[6] * b[0] + a[7] * b[3] + a[8] * b[6];
169
170 result[1] = a[0] * b[1] + a[1] * b[4] + a[2] * b[7];
171 result[4] = a[3] * b[1] + a[4] * b[4] + a[5] * b[7];
172 result[7] = a[6] * b[1] + a[7] * b[4] + a[8] * b[7];
173
174 result[2] = a[0] * b[2] + a[1] * b[5] + a[2] * b[8];
175 result[5] = a[3] * b[2] + a[4] * b[5] + a[5] * b[8];
176 result[8] = a[6] * b[2] + a[7] * b[5] + a[8] * b[8];
177 }
178
179 // Multiply a 3x3 matrix with a three-element float vector.
180 static void multiply_3x3_matrix_float3cxx . . $ multiply_3x3_matrix_float3(e799f44730d44e02).(const Matrix3x3cxx . . $ Matrix3x3# M, float x0, float x1, float x2, float *y0, float *y1, float *y2)
181 {
182 *y0 = M[0] * x0 + M[1] * x1 + M[2] * x2;
183 *y1 = M[3] * x0 + M[4] * x1 + M[5] * x2;
184 *y2 = M[6] * x0 + M[7] * x1 + M[8] * x2;
185 }
186
187 // Convert a linear RGB value in s6.25 fixed-point to sRGB (between 0 to 255, inclusive).
188 static inline uint8_tcxx . . $ uint8_t# convert_linear_rgb_to_srgb_fpcxx . . $ convert_linear_rgb_to_srgb_fp(bc0fba5608a0bfe7).(int x)
189 {
190 if (x < 0) {
191 return 0;
192 }
193 if (x >= (REVERSE_LUT_SIZEcxx . . $ `src/filter/colgate/colgate.c:146:9`! << (INPUT_PIXEL_BITScxx . . $ `src/filter/colgate/colgate.c:77:9`! + MATRIX_ELEMENT_FRAC_BITScxx . . $ `src/filter/colgate/colgate.c:78:9`! - REVERSE_LUT_BITScxx . . $ `src/filter/colgate/colgate.c:145:9`!))) {
194 return 255;
195 }
196 return linear_rgb_to_srgb_lutcxx . . $ linear_rgb_to_srgb_lut.[((unsigned)x) >> (INPUT_PIXEL_BITScxx . . $ `src/filter/colgate/colgate.c:77:9`! + MATRIX_ELEMENT_FRAC_BITScxx . . $ `src/filter/colgate/colgate.c:78:9`! - REVERSE_LUT_BITScxx . . $ `src/filter/colgate/colgate.c:145:9`!)];
197 }
198
199 // Temperature is in Kelvin. Formula from http://en.wikipedia.org/wiki/Planckian_locus#Approximation .
200 void convert_color_temperature_to_xyzcxx . . $ convert_color_temperature_to_xyz(9824d325db6993fa).(float T, float *x, float *y, float *z)
201 {
202 double invT = 1.0 / T;
203 double xc, yc;
204
205 if (T <= 4000.0f) {
206 xc = ((-0.2661239e9 * invT - 0.2343580e6) * invT + 0.8776956e3) * invT + 0.179910;
207 } else {
208 xc = ((-3.0258469e9 * invT + 2.1070379e6) * invT + 0.2226347e3) * invT + 0.240390;
209 }
210
211 if (T <= 2222.0f) {
212 yc = ((-1.1063814 * xc - 1.34811020) * xc + 2.18555832) * xc - 0.20219683;
213 } else if (T <= 4000.0f) {
214 yc = ((-0.9549476 * xc - 1.37418593) * xc + 2.09137015) * xc - 0.16748867;
215 } else {
216 yc = (( 3.0817580 * xc - 5.87338670) * xc + 3.75112997) * xc - 0.37001483;
217 }
218
219 *x = xc;
220 *y = yc;
221 *z = 1.0 - xc - yc;
222 }
223
224 // sRGB primaries.
225 static const Matrix3x3cxx . . $ Matrix3x3# rgb_to_xyz_matrixcxx . . $ rgb_to_xyz_matrix. = {
226 0.4124, 0.3576, 0.1805,
227 0.2126, 0.7152, 0.0722,
228 0.0193, 0.1192, 0.9505,
229 };
230 static const Matrix3x3cxx . . $ Matrix3x3# xyz_to_rgb_matrixcxx . . $ xyz_to_rgb_matrix. = {
231 3.240625, -1.537208, -0.498629,
232 -0.968931, 1.875756, 0.041518,
233 0.055710, -0.204021, 1.056996,
234 };
235
236 static void convert_linear_rgb_to_linear_xyzcxx . . $ convert_linear_rgb_to_linear_xyz(7348b25d00bc3e4f).(float r, float g, float b, float *x, float *y, float *z)
237 {
238 multiply_3x3_matrix_float3cxx . . $ multiply_3x3_matrix_float3(e799f44730d44e02).(rgb_to_xyz_matrixcxx . . $ rgb_to_xyz_matrix., r, g, b, x, y, z);
239 }
240
241 static void convert_linear_xyz_to_linear_rgbcxx . . $ convert_linear_xyz_to_linear_rgb(7348b25d00bc3e4f).(float x, float y, float z, float *r, float *g, float *b)
242 {
243 multiply_3x3_matrix_float3cxx . . $ multiply_3x3_matrix_float3(e799f44730d44e02).(xyz_to_rgb_matrixcxx . . $ xyz_to_rgb_matrix., x, y, z, r, g, b);
244 }
245
246 /*
247 * There are several different LMS spaces, at least according to Wikipedia.
248 * Through practical testing, I've found most of them (like the CIECAM02 model)
249 * to yield a result that is too reddish in practice. This is the RLAB space,
250 * normalized to D65, which means that the standard D65 illuminant
251 * (x=0.31271, y=0.32902, z=1-y-x) gives L=M=S under this transformation.
252 * This makes sense because sRGB (which is used to derive those XYZ values
253 * in the first place) assumes the D65 illuminant, and so the D65 illuminant
254 * also gives R=G=B in sRGB.
255 */
256 static const Matrix3x3cxx . . $ Matrix3x3# xyz_to_lms_matrixcxx . . $ xyz_to_lms_matrix. = {
257 0.4002, 0.7076, -0.0808,
258 -0.2263, 1.1653, 0.0457,
259 0.0, 0.0, 0.9182,
260 };
261 static const Matrix3x3cxx . . $ Matrix3x3# lms_to_xyz_matrixcxx . . $ lms_to_xyz_matrix. = {
262 1.86007, -1.12948, 0.21990,
263 0.36122, 0.63880, -0.00001,
264 0.00000, 0.00000, 1.08909,
265 };
266
267 static void convert_linear_xyz_to_linear_lmscxx . . $ convert_linear_xyz_to_linear_lms(7348b25d00bc3e4f).(float x, float y, float z, float *l, float *m, float *s)
268 {
269 multiply_3x3_matrix_float3cxx . . $ multiply_3x3_matrix_float3(e799f44730d44e02).(xyz_to_lms_matrixcxx . . $ xyz_to_lms_matrix., x, y, z, l, m, s);
270 }
271
272 static void convert_linear_lms_to_linear_xyzcxx . . $ convert_linear_lms_to_linear_xyz(7348b25d00bc3e4f).(float l, float m, float s, float *x, float *y, float *z)
273 {
274 multiply_3x3_matrix_float3cxx . . $ multiply_3x3_matrix_float3(e799f44730d44e02).(lms_to_xyz_matrixcxx . . $ lms_to_xyz_matrix., l, m, s, x, y, z);
275 }
276
277 /*
278 * For a given reference color (given in XYZ space),
279 * compute scaling factors for L, M and S. What we want at the output is equal L, M and S
280 * for the reference color (making it a neutral illuminant), or sL ref_L = sM ref_M = sS ref_S.
281 * This removes two degrees of freedom for our system, and we only need to find fL.
282 *
283 * A reasonable last constraint would be to preserve Y, approximately the brightness,
284 * for the reference color. Since L'=M'=S' and the Y row of the LMS-to-XYZ matrix
285 * sums to unity, we know that Y'=L', and it's easy to find the fL that sets Y'=Y.
286 */
287 static void compute_lms_scaling_factorscxx . . $ compute_lms_scaling_factors(7348b25d00bc3e4f).(float x, float y, float z, float *scale_l, float *scale_m, float *scale_s)
288 {
289 float l, m, s;
290 convert_linear_xyz_to_linear_lmscxx . . $ convert_linear_xyz_to_linear_lms(7348b25d00bc3e4f).(x, y, z, &l, &m, &s);
291
292 *scale_l = y / l;
293 *scale_m = *scale_l * (l / m);
294 *scale_s = *scale_l * (l / s);
295 }
296
297 static void compute_correction_matrixcxx . . $ compute_correction_matrix(cd4271b529fe6e1a).(colgate_instance_tcxx . . $ colgate_instance_t# *o)
298 {
299 int i;
300
301 /*
302 * Find out what the given neutral color would be in LMS space,
303 * and use that value to build a correction factor for each component
304 * so that the neutral color really becomes gray (in LMS).
305 */
306 float ref_r = o->neutral_colorcxx . . $ colgate_instance#neutral_color..rcxx . . $ f0r_param_color#r. * 255.0f;
307 float ref_g = o->neutral_colorcxx . . $ colgate_instance#neutral_color..gcxx . . $ f0r_param_color#g. * 255.0f;
308 float ref_b = o->neutral_colorcxx . . $ colgate_instance#neutral_color..bcxx . . $ f0r_param_color#b. * 255.0f;
309
310 float linear_r = convert_srgb_to_linear_rgbcxx . . $ convert_srgb_to_linear_rgb(fbcf3211dfe1bfd1).(ref_r);
311 float linear_g = convert_srgb_to_linear_rgbcxx . . $ convert_srgb_to_linear_rgb(fbcf3211dfe1bfd1).(ref_g);
312 float linear_b = convert_srgb_to_linear_rgbcxx . . $ convert_srgb_to_linear_rgb(fbcf3211dfe1bfd1).(ref_b);
313
314 float x, y, z;
315 convert_linear_rgb_to_linear_xyzcxx . . $ convert_linear_rgb_to_linear_xyz(7348b25d00bc3e4f).(linear_r, linear_g, linear_b, &x, &y, &z);
316
317 float l, m, s;
318 convert_linear_xyz_to_linear_lmscxx . . $ convert_linear_xyz_to_linear_lms(7348b25d00bc3e4f).(x, y, z, &l, &m, &s);
319
320 float l_scale, m_scale, s_scale;
321 compute_lms_scaling_factorscxx . . $ compute_lms_scaling_factors(7348b25d00bc3e4f).(x, y, z, &l_scale, &m_scale, &s_scale);
322
323 /*
324 * Now apply the color balance. Simply put, we find the chromacity point
325 * for the desired white temperature, see what LMS scaling factors they
326 * would have given us, and then reverse that transform. For T=6500K,
327 * the default, this gives us nearly an identity transform (but only nearly,
328 * since the D65 illuminant does not exactly match the results of T=6500K);
329 * we normalize so that T=6500K really is a no-op.
330 */
331 float white_x, white_y, white_z, l_scale_white, m_scale_white, s_scale_white;
332 convert_color_temperature_to_xyzcxx . . $ convert_color_temperature_to_xyz(9824d325db6993fa).(o->color_temperaturecxx . . $ colgate_instance#color_temperature., &white_x, &white_y, &white_z);
333 compute_lms_scaling_factorscxx . . $ compute_lms_scaling_factors(7348b25d00bc3e4f).(white_x, white_y, white_z, &l_scale_white, &m_scale_white, &s_scale_white);
334
335 float ref_x, ref_y, ref_z, l_scale_ref, m_scale_ref, s_scale_ref;
336 convert_color_temperature_to_xyzcxx . . $ convert_color_temperature_to_xyz(9824d325db6993fa).(6500.0f, &ref_x, &ref_y, &ref_z);
337 compute_lms_scaling_factorscxx . . $ compute_lms_scaling_factors(7348b25d00bc3e4f).(ref_x, ref_y, ref_z, &l_scale_ref, &m_scale_ref, &s_scale_ref);
338
339 l_scale *= l_scale_ref / l_scale_white;
340 m_scale *= m_scale_ref / m_scale_white;
341 s_scale *= s_scale_ref / s_scale_white;
342
343 /*
344 * Concatenate all the different linear operations into a single 3x3 matrix.
345 * Note that since we postmultiply our vectors, the order of the matrices
346 * has to be the opposite of the execution order.
347 */
348 Matrix3x3cxx . . $ Matrix3x3# temp, temp2, corr_matrix;
349 Matrix3x3cxx . . $ Matrix3x3# lms_scale_matrix = {
350 l_scale, 0.0f, 0.0f,
351 0.0f, m_scale, 0.0f,
352 0.0f, 0.0f, s_scale,
353 };
354 multiply_3x3_matricescxx . . $ multiply_3x3_matrices(e402dac2b8bda508).(xyz_to_rgb_matrixcxx . . $ xyz_to_rgb_matrix., lms_to_xyz_matrixcxx . . $ lms_to_xyz_matrix., temp);
355 multiply_3x3_matricescxx . . $ multiply_3x3_matrices(e402dac2b8bda508).(temp, lms_scale_matrix, temp2);
356 multiply_3x3_matricescxx . . $ multiply_3x3_matrices(e402dac2b8bda508).(temp2, xyz_to_lms_matrixcxx . . $ xyz_to_lms_matrix., temp);
357 multiply_3x3_matricescxx . . $ multiply_3x3_matrices(e402dac2b8bda508).(temp, rgb_to_xyz_matrixcxx . . $ rgb_to_xyz_matrix., corr_matrix);
358
359 // Scale for fixed-point, and clamp. We clamp the matrix elements
360 // instead of the actual fixed-point numbers below, to make sure
361 // we get consistent results over the entire range.
362 for (i = 0; i < 9; ++i) {
363 corr_matrix[i] *= (float)(1 << MATRIX_ELEMENT_FRAC_BITScxx . . $ `src/filter/colgate/colgate.c:78:9`!);
364 if (corr_matrix[i] < -(1 << MATRIX_ELEMENT_BITScxx . . $ `src/filter/colgate/colgate.c:79:9`!)) {
365 corr_matrix[i] = -(1 << MATRIX_ELEMENT_BITScxx . . $ `src/filter/colgate/colgate.c:79:9`!);
366 }
367 if (corr_matrix[i] > (1 << MATRIX_ELEMENT_BITScxx . . $ `src/filter/colgate/colgate.c:79:9`!) - 1) {
368 corr_matrix[i] = (1 << MATRIX_ELEMENT_BITScxx . . $ `src/filter/colgate/colgate.c:79:9`!) - 1;
369 }
370 }
371
372 // Precompute some of the multiplications (after conversion from sRGB)
373 // to save some time per-pixel later. Each of these contain the given color
374 // converted to linear space and then multiplied by three different factors,
375 // given by the matrix.
376 for (i = 0; i < 256; ++i) {
377 int x = convert_srgb_to_linear_rgbcxx . . $ convert_srgb_to_linear_rgb(fbcf3211dfe1bfd1).(i) * (float)(1 << INPUT_PIXEL_BITScxx . . $ `src/filter/colgate/colgate.c:77:9`!);
378
379 int r0 = lrintfcxx . . $ lrintf(2b53f6384d78b03a).(x * corr_matrix[0]);
380 int r1 = lrintfcxx . . $ lrintf(2b53f6384d78b03a).(x * corr_matrix[3]);
381 int r2 = lrintfcxx . . $ lrintf(2b53f6384d78b03a).(x * corr_matrix[6]);
382
383 int g0 = lrintfcxx . . $ lrintf(2b53f6384d78b03a).(x * corr_matrix[1]);
384 int g1 = lrintfcxx . . $ lrintf(2b53f6384d78b03a).(x * corr_matrix[4]);
385 int g2 = lrintfcxx . . $ lrintf(2b53f6384d78b03a).(x * corr_matrix[7]);
386
387 int b0 = lrintfcxx . . $ lrintf(2b53f6384d78b03a).(x * corr_matrix[2]);
388 int b1 = lrintfcxx . . $ lrintf(2b53f6384d78b03a).(x * corr_matrix[5]);
389 int b2 = lrintfcxx . . $ lrintf(2b53f6384d78b03a).(x * corr_matrix[8]);
390
391 #if __SSE2__cxx . . $ `<built-in>:383:9`!
392 o->premult_rcxx . . $ colgate_instance#premult_r.[i] = _mm_setr_epi32cxx . . $ _mm_setr_epi32(df364411271f454b).(r0, r1, r2, 0);
393 o->premult_gcxx . . $ colgate_instance#premult_g.[i] = _mm_setr_epi32cxx . . $ _mm_setr_epi32(df364411271f454b).(g0, g1, g2, 0);
394 o->premult_bcxx . . $ colgate_instance#premult_b.[i] = _mm_setr_epi32cxx . . $ _mm_setr_epi32(df364411271f454b).(b0, b1, b2, 0);
395 #else
396 o->premult_r[i][0] = r0;
397 o->premult_r[i][1] = r1;
398 o->premult_r[i][2] = r2;
399
400 o->premult_g[i][0] = g0;
401 o->premult_g[i][1] = g1;
402 o->premult_g[i][2] = g2;
403
404 o->premult_b[i][0] = b0;
405 o->premult_b[i][1] = b1;
406 o->premult_b[i][2] = b2;
407 #endif
408 }
409 }
410
411 int f0r_initcxx . . $ f0r_init(b126dc7c1de90089).()
412 {
413 fill_srgb_lutcxx . . $ fill_srgb_lut(d878ad36e69247c7).();
414 return 1;
415 }
416
417 void f0r_deinitcxx . . $ f0r_deinit(49f6e7a06ebc5aa8).()
418 {
419 }
420
421 void f0r_get_plugin_infocxx . . $ f0r_get_plugin_info(35d753456c0ea59c).(f0r_plugin_info_tcxx . . $ f0r_plugin_info_t# *colordistance_info)
422 {
423 colordistance_info->namecxx . . $ f0r_plugin_info#name. = "White Balance (LMS space)";
424 colordistance_info->authorcxx . . $ f0r_plugin_info#author. = "Steinar H. Gunderson";
425 colordistance_info->plugin_typecxx . . $ f0r_plugin_info#plugin_type. = F0R_PLUGIN_TYPE_FILTERcxx . . $ `include/frei0r.h:247:9`!;
426 colordistance_info->color_modelcxx . . $ f0r_plugin_info#color_model. = F0R_COLOR_MODEL_RGBA8888cxx . . $ `include/frei0r.h:308:9`!;
427 colordistance_info->frei0r_versioncxx . . $ f0r_plugin_info#frei0r_version. = FREI0R_MAJOR_VERSIONcxx . . $ `include/frei0r.h:217:9`!;
428 colordistance_info->major_versioncxx . . $ f0r_plugin_info#major_version. = 0;
429 colordistance_info->minor_versioncxx . . $ f0r_plugin_info#minor_version. = 1;
430 colordistance_info->num_paramscxx . . $ f0r_plugin_info#num_params. = 2;
431 colordistance_info->explanationcxx . . $ f0r_plugin_info#explanation. = "Do simple color correction, in a physically meaningful way";
432 }
433
434 void f0r_get_param_infocxx . . $ f0r_get_param_info(a9f9aa7954cc4a37).(f0r_param_info_tcxx . . $ f0r_param_info_t# *info, int param_index)
435 {
436 switch (param_index) {
437 case NEUTRAL_COLORcxx . . $ NEUTRAL_COLOR.:
438 info->namecxx . . $ f0r_param_info#name. = "Neutral Color";
439 info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_COLORcxx . . $ `include/frei0r.h:389:9`!;
440 info->explanationcxx . . $ f0r_param_info#explanation. = "Choose a color from the source image that should be white.";
441 break;
442
443 case COLOR_TEMPERATUREcxx . . $ COLOR_TEMPERATURE.:
444 info->namecxx . . $ f0r_param_info#name. = "Color Temperature";
445 info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_DOUBLEcxx . . $ `include/frei0r.h:383:9`!;
446 info->explanationcxx . . $ f0r_param_info#explanation. = "Choose an output color temperature, if different from 6500 K.";
447 break;
448 }
449
450 }
451
452 f0r_instance_tcxx . . $ f0r_instance_t# f0r_constructcxx . . $ f0r_construct(da3f471cd007a696).(unsigned width, unsigned height)
453 {
454 colgate_instance_tcxx . . $ colgate_instance_t# *inst = (colgate_instance_tcxx . . $ colgate_instance_t# *)calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(*inst));
455 inst->widthcxx . . $ colgate_instance#width. = width;
456 inst->heightcxx . . $ colgate_instance#height. = height;
457 inst->neutral_colorcxx . . $ colgate_instance#neutral_color..rcxx . . $ f0r_param_color#r. = 0.5;
458 inst->neutral_colorcxx . . $ colgate_instance#neutral_color..gcxx . . $ f0r_param_color#g. = 0.5;
459 inst->neutral_colorcxx . . $ colgate_instance#neutral_color..bcxx . . $ f0r_param_color#b. = 0.5;
460 inst->color_temperaturecxx . . $ colgate_instance#color_temperature. = 6500.0;
461 compute_correction_matrixcxx . . $ compute_correction_matrix(cd4271b529fe6e1a).(inst);
462 return (f0r_instance_tcxx . . $ f0r_instance_t#)inst;
463 }
464
465 void f0r_destructcxx . . $ f0r_destruct(5d22bdacc48458e8).(f0r_instance_tcxx . . $ f0r_instance_t# instance)
466 {
467 freecxx . . $ free(5d22bdacc48458e8).(instance);
468 }
469
470 void f0r_set_param_valuecxx . . $ f0r_set_param_value(5be095a8278d2bf5).(f0r_instance_tcxx . . $ f0r_instance_t# instance, f0r_param_tcxx . . $ f0r_param_t# param, int param_index)
471 {
472 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance);
473 colgate_instance_tcxx . . $ colgate_instance_t# *inst = (colgate_instance_tcxx . . $ colgate_instance_t# *)instance;
474
475 switch (param_index) {
476 case NEUTRAL_COLORcxx . . $ NEUTRAL_COLOR.:
477 inst->neutral_colorcxx . . $ colgate_instance#neutral_color. = *((f0r_param_color_tcxx . . $ f0r_param_color_t# *)param);
478 compute_correction_matrixcxx . . $ compute_correction_matrix(cd4271b529fe6e1a).(inst);
479 break;
480
481 case COLOR_TEMPERATUREcxx . . $ COLOR_TEMPERATURE.:
482 // Map frei0r range [0, 1] to temperature range [0, 15000].
483 inst->color_temperaturecxx . . $ colgate_instance#color_temperature. = *((double *)param) * 15000.0;
484 if (inst->color_temperaturecxx . . $ colgate_instance#color_temperature. < 1000.0 || inst->color_temperaturecxx . . $ colgate_instance#color_temperature. > 15000.0) {
485 inst->color_temperaturecxx . . $ colgate_instance#color_temperature. = 6500.0;
486 }
487 compute_correction_matrixcxx . . $ compute_correction_matrix(cd4271b529fe6e1a).(inst);
488 break;
489 }
490 }
491
492 void f0r_get_param_valuecxx . . $ f0r_get_param_value(5be095a8278d2bf5).(f0r_instance_tcxx . . $ f0r_instance_t# instance, f0r_param_tcxx . . $ f0r_param_t# param, int param_index)
493 {
494 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance);
495 colgate_instance_tcxx . . $ colgate_instance_t# *inst = (colgate_instance_tcxx . . $ colgate_instance_t#*)instance;
496
497 switch (param_index) {
498 case NEUTRAL_COLORcxx . . $ NEUTRAL_COLOR.:
499 *((f0r_param_color_tcxx . . $ f0r_param_color_t# *)param) = inst->neutral_colorcxx . . $ colgate_instance#neutral_color.;
500 break;
501
502 case COLOR_TEMPERATUREcxx . . $ COLOR_TEMPERATURE.:
503 // Map temperature range [0, 15000] to frei0r range [0, 1].
504 *((double *)param) = inst->color_temperaturecxx . . $ colgate_instance#color_temperature. / 15000.0;
505 break;
506 }
507 }
508
509 void f0r_updatecxx . . $ f0r_update(96cd7b27f2fca8a7).(f0r_instance_tcxx . . $ f0r_instance_t# instance, double time, const uint32_tcxx . . $ uint32_t# *inframe, uint32_tcxx . . $ uint32_t# *outframe)
510 {
511 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance);
512 colgate_instance_tcxx . . $ colgate_instance_t# *inst = (colgate_instance_tcxx . . $ colgate_instance_t# *)instance;
513 unsigned len = inst->widthcxx . . $ colgate_instance#width. * inst->heightcxx . . $ colgate_instance#height.;
514 unsigned char *dst = (unsigned char *)outframe;
515 const unsigned char *src = (unsigned char *)inframe;
516 unsigned i;
517
518 #ifdef __SSE2__cxx . . $ `<built-in>:383:9`!
519 __m128icxx . . $ __m128i# zero = _mm_setzero_si128cxx . . $ _mm_setzero_si128(3e8ed594d480523e).();
520 __m128icxx . . $ __m128i# max = _mm_set1_epi16cxx . . $ _mm_set1_epi16(5130941c07634c37).(REVERSE_LUT_SIZEcxx . . $ `src/filter/colgate/colgate.c:146:9`! - 1);
521 for (i = 0; i < len; ++i) {
522 __m128icxx . . $ __m128i# l1 = inst->premult_rcxx . . $ colgate_instance#premult_r.[*src++];
523 __m128icxx . . $ __m128i# l2 = inst->premult_gcxx . . $ colgate_instance#premult_g.[*src++];
524 __m128icxx . . $ __m128i# l3 = inst->premult_bcxx . . $ colgate_instance#premult_b.[*src++];
525 __m128icxx . . $ __m128i# result = _mm_add_epi32cxx . . $ _mm_add_epi32(e79ab442e937dade).(l3, _mm_add_epi32cxx . . $ _mm_add_epi32(e79ab442e937dade).(l1, l2));
526
527 // Shift into the right range, and then clamp to [min, max].
528 // We convert to 16-bit values since we have min/max instructions
529 // there (without needing SSE4), and because it allows us
530 // to extract the values with one less SSE shift/move.
531 result = _mm_srai_epi32cxx . . $ _mm_srai_epi32(8c21f12da5b7f84d).(result, INPUT_PIXEL_BITScxx . . $ `src/filter/colgate/colgate.c:77:9`! + MATRIX_ELEMENT_FRAC_BITScxx . . $ `src/filter/colgate/colgate.c:78:9`! - REVERSE_LUT_BITScxx . . $ `src/filter/colgate/colgate.c:145:9`!);
532 result = _mm_packs_epi32cxx . . $ _mm_packs_epi32(e79ab442e937dade).(result, result);
533 result = _mm_max_epi16cxx . . $ _mm_max_epi16(e79ab442e937dade).(result, zero);
534 result = _mm_min_epi16cxx . . $ _mm_min_epi16(e79ab442e937dade).(result, max);
535
536 unsigned new_rg = _mm_cvtsi128_si32cxx . . $ _mm_cvtsi128_si32(ad38f7c246444b6).(result);
537 result = _mm_srli_si128cxx . . $ _mm_srli_si128(8c21f12da5b7f84d).(result, 4);
538 unsigned new_b = _mm_cvtsi128_si32cxx . . $ _mm_cvtsi128_si32(ad38f7c246444b6).(result);
539
540 *dst++ = linear_rgb_to_srgb_lutcxx . . $ linear_rgb_to_srgb_lut.[new_rg & 0xffff];
541 *dst++ = linear_rgb_to_srgb_lutcxx . . $ linear_rgb_to_srgb_lut.[new_rg >> 16];
542 *dst++ = linear_rgb_to_srgb_lutcxx . . $ linear_rgb_to_srgb_lut.[new_b];
543 *dst++ = *src++; // Copy alpha.
544 }
545 #else
546 for (i = 0; i < len; ++i) {
547 unsigned old_r = *src++;
548 unsigned old_g = *src++;
549 unsigned old_b = *src++;
550
551 int new_r = inst->premult_r[old_r][0] + inst->premult_g[old_g][0] + inst->premult_b[old_b][0];
552 int new_g = inst->premult_r[old_r][1] + inst->premult_g[old_g][1] + inst->premult_b[old_b][1];
553 int new_b = inst->premult_r[old_r][2] + inst->premult_g[old_g][2] + inst->premult_b[old_b][2];
554
555 *dst++ = convert_linear_rgb_to_srgb_fp(new_r);
556 *dst++ = convert_linear_rgb_to_srgb_fp(new_g);
557 *dst++ = convert_linear_rgb_to_srgb_fp(new_b);
558 *dst++ = *src++; // Copy alpha.
559 }
560 #endif
561 }