Blender V5.3
math_color.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_math_color.hh"
10#include "BLI_math_color_c.hh"
11#include "BLI_math_matrix.hh"
12#include "BLI_math_matrix_c.hh"
13#include "BLI_math_vector.hh"
14#include "BLI_math_vector_c.hh"
15#include "BLI_simd.hh"
16#include "BLI_utildefines.hh"
17
18#include <algorithm>
19#include <cstring>
20
21#include "BLI_strict_flags.hh" /* IWYU pragma: keep. Keep last. */
22
23namespace blender {
24
25void hsv_to_rgb(float h, float s, float v, float *r_r, float *r_g, float *r_b)
26{
27 float nr, ng, nb;
28
29 nr = fabsf(h * 6.0f - 3.0f) - 1.0f;
30 ng = 2.0f - fabsf(h * 6.0f - 2.0f);
31 nb = 2.0f - fabsf(h * 6.0f - 4.0f);
32
33 CLAMP(nr, 0.0f, 1.0f);
34 CLAMP(nb, 0.0f, 1.0f);
35 CLAMP(ng, 0.0f, 1.0f);
36
37 *r_r = ((nr - 1.0f) * s + 1.0f) * v;
38 *r_g = ((ng - 1.0f) * s + 1.0f) * v;
39 *r_b = ((nb - 1.0f) * s + 1.0f) * v;
40}
41
42void hsl_to_rgb(float h, float s, float l, float *r_r, float *r_g, float *r_b)
43{
44 float nr, ng, nb, chroma;
45
46 nr = fabsf(h * 6.0f - 3.0f) - 1.0f;
47 ng = 2.0f - fabsf(h * 6.0f - 2.0f);
48 nb = 2.0f - fabsf(h * 6.0f - 4.0f);
49
50 CLAMP(nr, 0.0f, 1.0f);
51 CLAMP(nb, 0.0f, 1.0f);
52 CLAMP(ng, 0.0f, 1.0f);
53
54 chroma = (1.0f - fabsf(2.0f * l - 1.0f)) * s;
55
56 *r_r = (nr - 0.5f) * chroma + l;
57 *r_g = (ng - 0.5f) * chroma + l;
58 *r_b = (nb - 0.5f) * chroma + l;
59}
60
61void hsv_to_rgb_v(const float hsv[3], float r_rgb[3])
62{
63 hsv_to_rgb(hsv[0], hsv[1], hsv[2], &r_rgb[0], &r_rgb[1], &r_rgb[2]);
64}
65
66void hsl_to_rgb_v(const float hsl[3], float r_rgb[3])
67{
68 hsl_to_rgb(hsl[0], hsl[1], hsl[2], &r_rgb[0], &r_rgb[1], &r_rgb[2]);
69}
70
71void rgb_to_yuv(float r, float g, float b, float *r_y, float *r_u, float *r_v, int colorspace)
72{
73 float y, u, v;
74
75 switch (colorspace) {
77 y = 0.299f * r + 0.587f * g + 0.114f * b;
78 u = -0.147f * r - 0.289f * g + 0.436f * b;
79 v = 0.615f * r - 0.515f * g - 0.100f * b;
80 break;
82 default:
84 y = 0.2126f * r + 0.7152f * g + 0.0722f * b;
85 u = -0.09991f * r - 0.33609f * g + 0.436f * b;
86 v = 0.615f * r - 0.55861f * g - 0.05639f * b;
87 break;
88 }
89
90 *r_y = y;
91 *r_u = u;
92 *r_v = v;
93}
94
95void yuv_to_rgb(float y, float u, float v, float *r_r, float *r_g, float *r_b, int colorspace)
96{
97 float r, g, b;
98
99 switch (colorspace) {
101 r = y + 1.140f * v;
102 g = y - 0.394f * u - 0.581f * v;
103 b = y + 2.032f * u;
104 break;
106 default:
108 r = y + 1.28033f * v;
109 g = y - 0.21482f * u - 0.38059f * v;
110 b = y + 2.12798f * u;
111 break;
112 }
113
114 *r_r = r;
115 *r_g = g;
116 *r_b = b;
117}
118
119void rgb_to_ycc(float r, float g, float b, float *r_y, float *r_cb, float *r_cr, int colorspace)
120{
121 float sr, sg, sb;
122 float y = 128.0f, cr = 128.0f, cb = 128.0f;
123
124 sr = 255.0f * r;
125 sg = 255.0f * g;
126 sb = 255.0f * b;
127
128 switch (colorspace) {
130 y = (0.257f * sr) + (0.504f * sg) + (0.098f * sb) + 16.0f;
131 cb = (-0.148f * sr) - (0.291f * sg) + (0.439f * sb) + 128.0f;
132 cr = (0.439f * sr) - (0.368f * sg) - (0.071f * sb) + 128.0f;
133 break;
135 y = (0.183f * sr) + (0.614f * sg) + (0.062f * sb) + 16.0f;
136 cb = (-0.101f * sr) - (0.338f * sg) + (0.439f * sb) + 128.0f;
137 cr = (0.439f * sr) - (0.399f * sg) - (0.040f * sb) + 128.0f;
138 break;
140 y = (0.299f * sr) + (0.587f * sg) + (0.114f * sb);
141 cb = (-0.16874f * sr) - (0.33126f * sg) + (0.5f * sb) + 128.0f;
142 cr = (0.5f * sr) - (0.41869f * sg) - (0.08131f * sb) + 128.0f;
143 break;
144 default:
145 BLI_assert_msg(0, "invalid colorspace");
146 break;
147 }
148
149 *r_y = y;
150 *r_cb = cb;
151 *r_cr = cr;
152}
153
154void ycc_to_rgb(float y, float cb, float cr, float *r_r, float *r_g, float *r_b, int colorspace)
155{
156 /* FIXME the following comment must be wrong because:
157 * BLI_YCC_ITU_BT601 y 16.0 cr 16.0 -> r -0.7009. */
158
159 /* YCC input have a range of 16-235 and 16-240 except with JFIF_0_255 where the range is 0-255
160 * RGB outputs are in the range 0 - 1.0f. */
161
162 float r = 128.0f, g = 128.0f, b = 128.0f;
163
164 switch (colorspace) {
166 r = 1.164f * (y - 16.0f) + 1.596f * (cr - 128.0f);
167 g = 1.164f * (y - 16.0f) - 0.813f * (cr - 128.0f) - 0.392f * (cb - 128.0f);
168 b = 1.164f * (y - 16.0f) + 2.017f * (cb - 128.0f);
169 break;
171 r = 1.164f * (y - 16.0f) + 1.793f * (cr - 128.0f);
172 g = 1.164f * (y - 16.0f) - 0.534f * (cr - 128.0f) - 0.213f * (cb - 128.0f);
173 b = 1.164f * (y - 16.0f) + 2.115f * (cb - 128.0f);
174 break;
176 r = y + 1.402f * cr - 179.456f;
177 g = y - 0.34414f * cb - 0.71414f * cr + 135.45984f;
178 b = y + 1.772f * cb - 226.816f;
179 break;
180 default:
182 break;
183 }
184 *r_r = r / 255.0f;
185 *r_g = g / 255.0f;
186 *r_b = b / 255.0f;
187}
188
189void hex_to_rgb(const char *hexcol, float *r_r, float *r_g, float *r_b)
190{
191 hex_to_rgba(hexcol, r_r, r_g, r_b, nullptr);
192}
193
204bool hex_to_rgba(const char *hexcol, float *r_r, float *r_g, float *r_b, float *r_a)
205{
206 if (hexcol[0] == '#') {
207 hexcol++;
208 }
209
210 size_t hex_len = strlen(hexcol);
211 uint r, g, b, a;
212 bool alpha_parsed = false;
213
214 switch (hex_len) {
215 case 8: /* #RRGGBBAA */
216 if (sscanf(hexcol, "%2x%2x%2x%2x", &r, &g, &b, &a) != 4) {
217 return false;
218 }
219 alpha_parsed = true;
220 break;
221 case 7: /* #RRGGBBA -> #RRGGBBA0 */
222 if (sscanf(hexcol, "%2x%2x%2x%1x", &r, &g, &b, &a) != 4) {
223 return false;
224 }
225 a <<= 4;
226 alpha_parsed = true;
227 break;
228 case 6: /* #RRGGBB */
229 if (sscanf(hexcol, "%2x%2x%2x", &r, &g, &b) != 3) {
230 return false;
231 }
232 break;
233 case 5: /* #RGBAA -> #RRGGBBAA */
234 if (sscanf(hexcol, "%1x%1x%1x%2x", &r, &g, &b, &a) != 4) {
235 return false;
236 }
237 r = (r << 4) | r;
238 g = (g << 4) | g;
239 b = (b << 4) | b;
240 alpha_parsed = true;
241 break;
242 case 4: /* #RGBA -> #RRGGBBAA */
243 if (sscanf(hexcol, "%1x%1x%1x%1x", &r, &g, &b, &a) != 4) {
244 return false;
245 }
246 r = (r << 4) | r;
247 g = (g << 4) | g;
248 b = (b << 4) | b;
249 a = (a << 4) | a;
250 alpha_parsed = true;
251 break;
252 case 3: /* #RGB -> #RRGGBB */
253 if (sscanf(hexcol, "%1x%1x%1x", &r, &g, &b) != 3) {
254 return false;
255 }
256 r = (r << 4) | r;
257 g = (g << 4) | g;
258 b = (b << 4) | b;
259 break;
260 case 2: /* #AB -> #ABABAB */
261 if (sscanf(hexcol, "%2x", &r) != 1) {
262 return false;
263 }
264 g = r;
265 b = r;
266 break;
267 case 1: /* #A -> #AAAAAA */
268 if (sscanf(hexcol, "%1x", &r) != 1) {
269 return false;
270 }
271 r = (r << 4) | r;
272 g = r;
273 b = r;
274 break;
275 default:
276 /* Invalid hex color length - leave color unchanged. */
277 return false;
278 }
279
280 /* Convert integer color channels to float. */
281 const float scale = 1.0f / 255.0f;
282 *r_r = float(r) * scale;
283 *r_g = float(g) * scale;
284 *r_b = float(b) * scale;
285
286 CLAMP(*r_r, 0.0f, 1.0f);
287 CLAMP(*r_g, 0.0f, 1.0f);
288 CLAMP(*r_b, 0.0f, 1.0f);
289
290 /* Assign alpha if present. */
291 if (r_a && alpha_parsed) {
292 *r_a = float(a) * scale;
293 CLAMP(*r_a, 0.0f, 1.0f);
294 }
295 return true;
296}
297
298void rgb_to_hsv(float r, float g, float b, float *r_h, float *r_s, float *r_v)
299{
300 float k = 0.0f;
301 float chroma;
302 float min_gb;
303
304 if (g < b) {
305 SWAP(float, g, b);
306 k = -1.0f;
307 }
308 min_gb = b;
309 if (r < g) {
310 SWAP(float, r, g);
311 k = -2.0f / 6.0f - k;
312 min_gb = min_ff(g, b);
313 }
314
315 chroma = r - min_gb;
316
317 *r_h = fabsf(k + (g - b) / (6.0f * chroma + 1e-20f));
318 *r_s = chroma / (r + 1e-20f);
319 *r_v = r;
320}
321
322void rgb_to_hsv_v(const float rgb[3], float r_hsv[3])
323{
324 rgb_to_hsv(rgb[0], rgb[1], rgb[2], &r_hsv[0], &r_hsv[1], &r_hsv[2]);
325}
326
327void rgb_to_hsl(float r, float g, float b, float *r_h, float *r_s, float *r_l)
328{
329 const float cmax = std::max({r, g, b});
330 const float cmin = std::min({r, g, b});
331 float h, s, l = min_ff(1.0f, (cmax + cmin) / 2.0f);
332
333 if (cmax == cmin) {
334 h = s = 0.0f; /* achromatic */
335 }
336 else {
337 float d = cmax - cmin;
338 s = l > 0.5f ? d / (2.0f - cmax - cmin) : d / (cmax + cmin);
339 if (cmax == r) {
340 h = (g - b) / d + (g < b ? 6.0f : 0.0f);
341 }
342 else if (cmax == g) {
343 h = (b - r) / d + 2.0f;
344 }
345 else {
346 h = (r - g) / d + 4.0f;
347 }
348 }
349 h /= 6.0f;
350
351 *r_h = h;
352 *r_s = s;
353 *r_l = l;
354}
355
356void rgb_to_hsl_compat(float r, float g, float b, float *r_h, float *r_s, float *r_l)
357{
358 /* Convert RGB to HSL, while staying as close as possible to existing HSL values.
359 * Uses a threshold as there can be small errors introduced by color space conversions
360 * or other operations. */
361 const float orig_s = *r_s;
362 const float orig_h = *r_h;
363 const float threshold = 1e-5f;
364
365 rgb_to_hsl(r, g, b, r_h, r_s, r_l);
366
367 /* For (near) zero lightness or saturation, keep the other values unchanged,
368 * as they are either undefined or very sensitive to small lightness changes. */
369 if (*r_l <= threshold) {
370 *r_h = orig_h;
371 *r_s = orig_s;
372 }
373 else if (*r_s <= threshold) {
374 *r_h = orig_h;
375 *r_s = orig_s;
376 }
377
378 /* Hue wraps around, keep it on the same side. */
379 if (fabsf(*r_h) <= threshold && fabsf(orig_h - 1.0f) <= threshold) {
380 *r_h = 1.0f;
381 }
382 else if (fabsf(*r_h - 1.0f) <= threshold && fabsf(orig_h) <= threshold) {
383 *r_h = 0.0f;
384 }
385}
386
387void rgb_to_hsl_compat_v(const float rgb[3], float r_hsl[3])
388{
389 rgb_to_hsl_compat(rgb[0], rgb[1], rgb[2], &r_hsl[0], &r_hsl[1], &r_hsl[2]);
390}
391
392void rgb_to_hsl_v(const float rgb[3], float r_hsl[3])
393{
394 rgb_to_hsl(rgb[0], rgb[1], rgb[2], &r_hsl[0], &r_hsl[1], &r_hsl[2]);
395}
396
397void rgb_to_hsv_compat(float r, float g, float b, float *r_h, float *r_s, float *r_v)
398{
399 /* Convert RGB to HSV, while staying as close as possible to existing HSV values.
400 * Uses a threshold as there can be small errors introduced by color space conversions
401 * or other operations. */
402 const float orig_h = *r_h;
403 const float orig_s = *r_s;
404 const float threshold = 1e-5f;
405
406 rgb_to_hsv(r, g, b, r_h, r_s, r_v);
407
408 /* For (near) zero values or saturation, keep the other values unchanged,
409 * as they are either undefined or very sensitive to small value changes. */
410 if (*r_v <= threshold) {
411 *r_h = orig_h;
412 *r_s = orig_s;
413 }
414 else if (*r_s <= threshold) {
415 *r_h = orig_h;
416 }
417
418 /* Hue wraps around, keep it on the same side. */
419 if (fabsf(*r_h) <= threshold && fabsf(orig_h - 1.0f) <= threshold) {
420 *r_h = 1.0f;
421 }
422 else if (fabsf(*r_h - 1.0f) <= threshold && fabsf(orig_h) <= threshold) {
423 *r_h = 0.0f;
424 }
425}
426
427void rgb_to_hsv_compat_v(const float rgb[3], float r_hsv[3])
428{
429 rgb_to_hsv_compat(rgb[0], rgb[1], rgb[2], &r_hsv[0], &r_hsv[1], &r_hsv[2]);
430}
431
432void hsv_clamp_v(float hsv[3], float v_max)
433{
434 if (hsv[0] < 0.0f || hsv[0] > 1.0f) [[unlikely]] {
435 hsv[0] = hsv[0] - floorf(hsv[0]);
436 }
437 CLAMP(hsv[1], 0.0f, 1.0f);
438 CLAMP(hsv[2], 0.0f, v_max);
439}
440
441uint hsv_to_cpack(float h, float s, float v)
442{
443 uint r, g, b;
444 float rf, gf, bf;
445 uint col;
446
447 hsv_to_rgb(h, s, v, &rf, &gf, &bf);
448
449 r = uint(rf * 255.0f);
450 g = uint(gf * 255.0f);
451 b = uint(bf * 255.0f);
452
453 col = (r + (g * 256) + (b * 256 * 256));
454 return col;
455}
456
457uint rgb_to_cpack(float r, float g, float b)
458{
459 uint ir, ig, ib;
460
461 ir = uint(floorf(255.0f * max_ff(r, 0.0f)));
462 ig = uint(floorf(255.0f * max_ff(g, 0.0f)));
463 ib = uint(floorf(255.0f * max_ff(b, 0.0f)));
464
465 ir = std::min<uint>(ir, 255);
466 ig = std::min<uint>(ig, 255);
467 ib = std::min<uint>(ib, 255);
468
469 return (ir + (ig * 256) + (ib * 256 * 256));
470}
471
472void cpack_to_rgb(uint col, float *r_r, float *r_g, float *r_b)
473{
474 *r_r = float(col & 0xFF) * (1.0f / 255.0f);
475 *r_g = float((col >> 8) & 0xFF) * (1.0f / 255.0f);
476 *r_b = float((col >> 16) & 0xFF) * (1.0f / 255.0f);
477}
478
479/* -------------------------------------------------------------------- */
482
483float srgb_to_linearrgb(float c)
484{
485 if (c < 0.04045f) {
486 return (c < 0.0f) ? 0.0f : c * (1.0f / 12.92f);
487 }
488
489 return powf((c + 0.055f) * (1.0f / 1.055f), 2.4f);
490}
491
492float linearrgb_to_srgb(float c)
493{
494 if (c < 0.0031308f) {
495 return (c < 0.0f) ? 0.0f : c * 12.92f;
496 }
497
498 return 1.055f * powf(c, 1.0f / 2.4f) - 0.055f;
499}
500
501/* SIMD code path, with pow 2.4 and 1/2.4 approximations. */
502#if BLI_HAVE_SSE2
503
514
519MALWAYS_INLINE __m128i _bli_float_to_int_nearest(const __m128 a)
520{
521# if BLI_HAVE_ARM_NEON
522 return vreinterpretq_m128i_s32(vcvtnq_s32_f32(a));
523# else
524 return _mm_cvtps_epi32(a);
525# endif
526}
527
528MALWAYS_INLINE __m128 _bli_math_fastpow(const int exp, const int e2coeff, const __m128 arg)
529{
530 __m128 ret;
531 ret = _mm_mul_ps(arg, _mm_castsi128_ps(_mm_set1_epi32(e2coeff)));
532 ret = _mm_cvtepi32_ps(_mm_castps_si128(ret));
533 ret = _mm_mul_ps(ret, _mm_castsi128_ps(_mm_set1_epi32(exp)));
534 ret = _mm_castsi128_ps(_bli_float_to_int_nearest(ret));
535 return ret;
536}
537
539MALWAYS_INLINE __m128 _bli_math_improve_5throot_solution(const __m128 old_result, const __m128 x)
540{
541 __m128 approx2 = _mm_mul_ps(old_result, old_result);
542 __m128 approx4 = _mm_mul_ps(approx2, approx2);
543 __m128 t = _mm_div_ps(x, approx4);
544 __m128 summ = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(4.0f), old_result), t); /* FMA. */
545 return _mm_mul_ps(summ, _mm_set1_ps(1.0f / 5.0f));
546}
547
549MALWAYS_INLINE __m128 _bli_math_fastpow24(const __m128 arg)
550{
551 /* max, avg and |avg| errors were calculated in GCC without FMA instructions
552 * The final precision should be better than `powf` in GLIBC. */
553
554 /* Calculate x^4/5, coefficient 0.994 was constructed manually to minimize
555 * avg error.
556 */
557 /* 0x3F4CCCCD = 4/5 */
558 /* 0x4F55A7FB = 2^(127/(4/5) - 127) * 0.994^(1/(4/5)) */
559 /* error max = 0.17, avg = 0.0018, |avg| = 0.05 */
560 __m128 x = _bli_math_fastpow(0x3F4CCCCD, 0x4F55A7FB, arg);
561 __m128 arg2 = _mm_mul_ps(arg, arg);
562 __m128 arg4 = _mm_mul_ps(arg2, arg2);
563 /* error max = 0.018 avg = 0.0031 |avg| = 0.0031 */
565 /* error max = 0.00021 avg = 1.6e-05 |avg| = 1.6e-05 */
567 /* error max = 6.1e-07 avg = 5.2e-08 |avg| = 1.1e-07 */
569 return _mm_mul_ps(x, _mm_mul_ps(x, x));
570}
571
572MALWAYS_INLINE __m128 _bli_math_rsqrt(__m128 in)
573{
574 __m128 r = _mm_rsqrt_ps(in);
575 /* Only do additional Newton-Raphson iterations when using actual SSE
576 * code path. When we are emulating SSE on NEON via sse2neon, the
577 * additional NR iterations are already done inside _mm_rsqrt_ps
578 * emulation. */
579# if defined(__SSE2__)
580 r = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(1.5f), r),
581 _mm_mul_ps(_mm_mul_ps(_mm_mul_ps(in, _mm_set1_ps(-0.5f)), r), _mm_mul_ps(r, r)));
582# endif
583 return r;
584}
585
586/* Calculate `powf(x, 1.0f / 2.4)`. */
587MALWAYS_INLINE __m128 _bli_math_fastpow512(const __m128 arg)
588{
589 /* 5/12 is too small, so compute the 4th root of 20/12 instead.
590 * 20/12 = 5/3 = 1 + 2/3 = 2 - 1/3. 2/3 is a suitable argument for fastpow.
591 * weighting coefficient: a^-1/2 = 2 a; a = 2^-2/3
592 */
593 __m128 xf = _bli_math_fastpow(0x3f2aaaab, 0x5eb504f3, arg);
594 __m128 xover = _mm_mul_ps(arg, xf);
595 __m128 xfm1 = _bli_math_rsqrt(xf);
596 __m128 x2 = _mm_mul_ps(arg, arg);
597 __m128 xunder = _mm_mul_ps(x2, xfm1);
598 /* sqrt2 * over + 2 * sqrt2 * under */
599 __m128 xavg = _mm_mul_ps(_mm_set1_ps(1.0f / (3.0f * 0.629960524947437f) * 0.999852f),
600 _mm_add_ps(xover, xunder));
601 xavg = _mm_mul_ps(xavg, _bli_math_rsqrt(xavg));
602 xavg = _mm_mul_ps(xavg, _bli_math_rsqrt(xavg));
603 return xavg;
604}
605
606MALWAYS_INLINE __m128 _bli_math_blend_sse(const __m128 mask, const __m128 a, const __m128 b)
607{
608# if BLI_HAVE_SSE4
609 return _mm_blendv_ps(b, a, mask);
610# else
611 return _mm_or_ps(_mm_and_ps(mask, a), _mm_andnot_ps(mask, b));
612# endif
613}
614
615MALWAYS_INLINE __m128 srgb_to_linearrgb_v4_simd(const __m128 c)
616{
617 __m128 cmp = _mm_cmplt_ps(c, _mm_set1_ps(0.04045f));
618 __m128 lt = _mm_max_ps(_mm_mul_ps(c, _mm_set1_ps(1.0f / 12.92f)), _mm_set1_ps(0.0f));
619 __m128 gtebase = _mm_mul_ps(_mm_add_ps(c, _mm_set1_ps(0.055f)),
620 _mm_set1_ps(1.0f / 1.055f)); /* FMA. */
621 __m128 gte = _bli_math_fastpow24(gtebase);
622 return _bli_math_blend_sse(cmp, lt, gte);
623}
624
625MALWAYS_INLINE __m128 linearrgb_to_srgb_v4_simd(const __m128 c)
626{
627 __m128 cmp = _mm_cmplt_ps(c, _mm_set1_ps(0.0031308f));
628 __m128 lt = _mm_max_ps(_mm_mul_ps(c, _mm_set1_ps(12.92f)), _mm_set1_ps(0.0f));
629 __m128 gte = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(1.055f), _bli_math_fastpow512(c)),
630 _mm_set1_ps(-0.055f));
631 return _bli_math_blend_sse(cmp, lt, gte);
632}
633
634void srgb_to_linearrgb_v3_v3(float linear[3], const float srgb[3])
635{
636 float r[4] = {srgb[0], srgb[1], srgb[2], 1.0f};
637 __m128 *rv = reinterpret_cast<__m128 *>(&r);
638 *rv = srgb_to_linearrgb_v4_simd(*rv);
639 linear[0] = r[0];
640 linear[1] = r[1];
641 linear[2] = r[2];
642}
643
644void linearrgb_to_srgb_v3_v3(float srgb[3], const float linear[3])
645{
646 float r[4] = {linear[0], linear[1], linear[2], 1.0f};
647 __m128 *rv = reinterpret_cast<__m128 *>(&r);
648 *rv = linearrgb_to_srgb_v4_simd(*rv);
649 srgb[0] = r[0];
650 srgb[1] = r[1];
651 srgb[2] = r[2];
652}
653
655MALWAYS_INLINE __m128i _bli_float_to_uchar_simd(const __m128 v)
656{
657 const __m128 clamped = _mm_min_ps(_mm_max_ps(v, _mm_setzero_ps()), _mm_set1_ps(1.0f));
658 const __m128 scaled = _mm_add_ps(_mm_mul_ps(clamped, _mm_set1_ps(255.0f)), _mm_set1_ps(0.5f));
659 return _mm_cvttps_epi32(scaled);
660}
661
663MALWAYS_INLINE __m128i linearrgb_to_srgb_uchar4_block(const float (*linear)[4],
664 const float (*matrix)[3])
665{
666 __m128 c0 = _mm_loadu_ps(linear[0]);
667 __m128 c1 = _mm_loadu_ps(linear[1]);
668 __m128 c2 = _mm_loadu_ps(linear[2]);
669 __m128 c3 = _mm_loadu_ps(linear[3]);
670
671 /* Transpose from AoS to SoA. */
672 _MM_TRANSPOSE4_PS(c0, c1, c2, c3);
673
674 if (matrix) {
675 /* Apply matrix to RGB channels. */
676 const __m128 r = c0, g = c1, b = c2;
677 c0 = _mm_add_ps(_mm_add_ps(_mm_mul_ps(_mm_set1_ps(matrix[0][0]), r),
678 _mm_mul_ps(_mm_set1_ps(matrix[1][0]), g)),
679 _mm_mul_ps(_mm_set1_ps(matrix[2][0]), b));
680 c1 = _mm_add_ps(_mm_add_ps(_mm_mul_ps(_mm_set1_ps(matrix[0][1]), r),
681 _mm_mul_ps(_mm_set1_ps(matrix[1][1]), g)),
682 _mm_mul_ps(_mm_set1_ps(matrix[2][1]), b));
683 c2 = _mm_add_ps(_mm_add_ps(_mm_mul_ps(_mm_set1_ps(matrix[0][2]), r),
684 _mm_mul_ps(_mm_set1_ps(matrix[1][2]), g)),
685 _mm_mul_ps(_mm_set1_ps(matrix[2][2]), b));
686 }
687
688 /* Convert RGB channels. */
689 c0 = linearrgb_to_srgb_v4_simd(c0);
690 c1 = linearrgb_to_srgb_v4_simd(c1);
691 c2 = linearrgb_to_srgb_v4_simd(c2);
692
693 /* Transpose from SoA to AoS. */
694 _MM_TRANSPOSE4_PS(c0, c1, c2, c3);
695
696 /* Float to byte. */
697 const __m128i lo = _mm_packs_epi32(_bli_float_to_uchar_simd(c0), _bli_float_to_uchar_simd(c1));
698 const __m128i hi = _mm_packs_epi32(_bli_float_to_uchar_simd(c2), _bli_float_to_uchar_simd(c3));
699 return _mm_packus_epi16(lo, hi);
700}
701
702#else /* BLI_HAVE_SSE2 */
703
704/* Non-SIMD code path, with the same pow approximations as SIMD one. */
705
707{
708 float r;
709 memcpy(&r, &v, sizeof(v));
710 return r;
711}
712
714{
715 int32_t r;
716 memcpy(&r, &v, sizeof(v));
717 return r;
718}
719
720MALWAYS_INLINE float _bli_math_fastpow(const int exp, const int e2coeff, const float arg)
721{
722 float ret = arg * int_as_float(e2coeff);
724 ret = ret * int_as_float(exp);
725 ret = int_as_float(int(ret));
726 return ret;
727}
728
729MALWAYS_INLINE float _bli_math_improve_5throot_solution(const float old_result, const float x)
730{
731 float approx2 = old_result * old_result;
732 float approx4 = approx2 * approx2;
733 float t = x / approx4;
734 float summ = 4.0f * old_result + t;
735 return summ * (1.0f / 5.0f);
736}
737
739{
740 float x = _bli_math_fastpow(0x3F4CCCCD, 0x4F55A7FB, arg);
741 float arg2 = arg * arg;
742 float arg4 = arg2 * arg2;
746 return x * x * x;
747}
748
750{
751 return 1.0f / sqrtf(in);
752}
753
755{
756 float xf = _bli_math_fastpow(0x3f2aaaab, 0x5eb504f3, arg);
757 float xover = arg * xf;
758 float xfm1 = _bli_math_rsqrt(xf);
759 float x2 = arg * arg;
760 float xunder = x2 * xfm1;
761 float xavg = (1.0f / (3.0f * 0.629960524947437f) * 0.999852f) * (xover + xunder);
762 xavg = xavg * _bli_math_rsqrt(xavg);
763 xavg = xavg * _bli_math_rsqrt(xavg);
764 return xavg;
765}
766
768{
769 if (c < 0.04045f) {
770 return (c < 0.0f) ? 0.0f : c * (1.0f / 12.92f);
771 }
772
773 return _bli_math_fastpow24((c + 0.055f) * (1.0f / 1.055f));
774}
775
777{
778 if (c < 0.0031308f) {
779 return (c < 0.0f) ? 0.0f : c * 12.92f;
780 }
781
782 return 1.055f * _bli_math_fastpow512(c) - 0.055f;
783}
784
785void srgb_to_linearrgb_v3_v3(float linear[3], const float srgb[3])
786{
787 linear[0] = srgb_to_linearrgb_approx(srgb[0]);
788 linear[1] = srgb_to_linearrgb_approx(srgb[1]);
789 linear[2] = srgb_to_linearrgb_approx(srgb[2]);
790}
791
792void linearrgb_to_srgb_v3_v3(float srgb[3], const float linear[3])
793{
794 srgb[0] = linearrgb_to_srgb_approx(linear[0]);
795 srgb[1] = linearrgb_to_srgb_approx(linear[1]);
796 srgb[2] = linearrgb_to_srgb_approx(linear[2]);
797}
798
799#endif /* BLI_HAVE_SSE2 */
800
801void linearrgb_to_srgb_uchar4_n(uchar (*__restrict srgb)[4],
802 const float (*__restrict linear)[4],
803 const int size,
804 const float (*__restrict matrix)[3])
805{
806#if BLI_HAVE_SSE2
807 if (size >= 4) {
808 /* Process 4 pixels per SIMD operation. */
809 int i = 0;
810 for (; i + 4 <= size; i += 4) {
811 const __m128i bytes = linearrgb_to_srgb_uchar4_block(&linear[i], matrix);
812 _mm_storeu_si128(reinterpret_cast<__m128i *>(srgb[i]), bytes);
813 }
814 /* Remainder with SIMD too, knowing we can safely overwrite already processed pixels again. */
815 if (i < size) {
816 const int remainder = size - 4;
817 const __m128i bytes = linearrgb_to_srgb_uchar4_block(&linear[remainder], matrix);
818 _mm_storeu_si128(reinterpret_cast<__m128i *>(srgb[remainder]), bytes);
819 }
820 return;
821 }
822#endif
823
824 /* Scalar path. */
825 if (matrix) {
826 for (int i = 0; i < size; i++) {
827 float s[4];
828 mul_v3_m3v3(s, matrix, linear[i]);
830 s[3] = linear[i][3];
831 rgba_float_to_uchar(srgb[i], s);
832 }
833 }
834 else {
835 for (int i = 0; i < size; i++) {
836 float s[4];
837 linearrgb_to_srgb_v3_v3(s, linear[i]);
838 s[3] = linear[i][3];
839 rgba_float_to_uchar(srgb[i], s);
840 }
841 }
842}
843
845
846/* -------------------------------------------------------------------- */
849
850void rgb_float_set_hue_float_offset(float rgb[3], float hue_offset)
851{
852 float hsv[3];
853
854 rgb_to_hsv(rgb[0], rgb[1], rgb[2], hsv, hsv + 1, hsv + 2);
855
856 hsv[0] += hue_offset;
857 if (hsv[0] > 1.0f) {
858 hsv[0] -= 1.0f;
859 }
860 else if (hsv[0] < 0.0f) {
861 hsv[0] += 1.0f;
862 }
863
864 hsv_to_rgb(hsv[0], hsv[1], hsv[2], rgb, rgb + 1, rgb + 2);
865}
866
867void rgb_byte_set_hue_float_offset(uchar rgb[3], float hue_offset)
868{
869 float rgb_float[3];
870
871 rgb_uchar_to_float(rgb_float, rgb);
872 rgb_float_set_hue_float_offset(rgb_float, hue_offset);
873 rgb_float_to_uchar(rgb, rgb_float);
874}
875
877
879{
880 static bool initialized = false;
881 if (initialized) {
882 return;
883 }
884 initialized = true;
885
886 /* Fill in the lookup table to convert bytes to float: */
887 for (int b = 0; b <= 255; b++) {
888 float f = srgb_to_linearrgb(float(b) * (1.0f / 255.0f));
890 }
891}
892
893namespace math {
894
896 float mired; /* Inverse temperature */
897 float2 uv; /* CIE 1960 uv coordinates */
898 float t; /* Isotherm parameter */
899 float dist(const float2 p) const
900 {
901 const float2 diff = p - uv;
902 return diff.y - t * diff.x;
903 }
904};
905
906/* Tabulated approximation of the Planckian locus.
907 * Based on http://www.brucelindbloom.com/Eqn_XYZ_to_T.html.
908 * Original source:
909 * "Color Science: Concepts and Methods, Quantitative Data and Formulae", Second Edition,
910 * Gunter Wyszecki and W. S. Stiles, John Wiley & Sons, 1982, pp. 227, 228. */
911static const std::array<locus_entry_t, 31> planck_locus{{
912 {0.0f, {0.18006f, 0.26352f}, -0.24341f}, {10.0f, {0.18066f, 0.26589f}, -0.25479f},
913 {20.0f, {0.18133f, 0.26846f}, -0.26876f}, {30.0f, {0.18208f, 0.27119f}, -0.28539f},
914 {40.0f, {0.18293f, 0.27407f}, -0.30470f}, {50.0f, {0.18388f, 0.27709f}, -0.32675f},
915 {60.0f, {0.18494f, 0.28021f}, -0.35156f}, {70.0f, {0.18611f, 0.28342f}, -0.37915f},
916 {80.0f, {0.18740f, 0.28668f}, -0.40955f}, {90.0f, {0.18880f, 0.28997f}, -0.44278f},
917 {100.0f, {0.19032f, 0.29326f}, -0.47888f}, {125.0f, {0.19462f, 0.30141f}, -0.58204f},
918 {150.0f, {0.19962f, 0.30921f}, -0.70471f}, {175.0f, {0.20525f, 0.31647f}, -0.84901f},
919 {200.0f, {0.21142f, 0.32312f}, -1.0182f}, {225.0f, {0.21807f, 0.32909f}, -1.2168f},
920 {250.0f, {0.22511f, 0.33439f}, -1.4512f}, {275.0f, {0.23247f, 0.33904f}, -1.7298f},
921 {300.0f, {0.24010f, 0.34308f}, -2.0637f}, {325.0f, {0.24792f, 0.34655f}, -2.4681f},
922 {350.0f, {0.25591f, 0.34951f}, -2.9641f}, {375.0f, {0.26400f, 0.35200f}, -3.5814f},
923 {400.0f, {0.27218f, 0.35407f}, -4.3633f}, {425.0f, {0.28039f, 0.35577f}, -5.3762f},
924 {450.0f, {0.28863f, 0.35714f}, -6.7262f}, {475.0f, {0.29685f, 0.35823f}, -8.5955f},
925 {500.0f, {0.30505f, 0.35907f}, -11.324f}, {525.0f, {0.31320f, 0.35968f}, -15.628f},
926 {550.0f, {0.32129f, 0.36011f}, -23.325f}, {575.0f, {0.32931f, 0.36038f}, -40.770f},
927 {600.0f, {0.33724f, 0.36051f}, -116.45f},
928}};
929
930bool whitepoint_to_temp_tint(const float3 &white, float &temperature, float &tint)
931{
932 /* Convert XYZ -> CIE 1960 uv. */
933 const float2 uv = float2{4.0f * white.x, 6.0f * white.y} / dot(white, {1.0f, 15.0f, 3.0f});
934
935 /* Find first entry that's "to the right" of the white point. */
936 auto check = [uv](const float val, const locus_entry_t &entry) { return entry.dist(uv) < val; };
937 std::array<locus_entry_t, 31>::const_iterator entry = std::upper_bound(
938 planck_locus.begin(), planck_locus.end(), 0.0f, check);
939 if (entry == planck_locus.begin() || entry == planck_locus.end()) {
940 return false;
941 }
942 const size_t i = size_t(entry - planck_locus.begin());
943 const locus_entry_t &low = planck_locus[i - 1], high = planck_locus[i];
944
945 /* Find closest point on locus. */
946 const float d_low = low.dist(uv) / sqrtf(1.0f + low.t * low.t);
947 const float d_high = high.dist(uv) / sqrtf(1.0f + high.t * high.t);
948 const float f = d_low / (d_low - d_high);
949
950 /* Find tint based on distance to closest point on locus. */
951 const float2 uv_temp = interpolate(low.uv, high.uv, f);
952 const float abs_tint = length(uv - uv_temp) * 3000.0f;
953 if (abs_tint > 150.0f) {
954 return false;
955 }
956
957 temperature = 1e6f / interpolate(low.mired, high.mired, f);
958 tint = abs_tint * ((uv.x < uv_temp.x) ? 1.0f : -1.0f);
959 return true;
960}
961
962float3 whitepoint_from_temp_tint(const float temperature, const float tint)
963{
964 /* Find table entry. */
965 const float mired = clamp(
966 1e6f / temperature, planck_locus[0].mired, planck_locus[planck_locus.size() - 1].mired);
967 auto check = [](const locus_entry_t &entry, const float val) { return entry.mired < val; };
968 std::array<locus_entry_t, 31>::const_iterator entry = std::lower_bound(
969 planck_locus.begin(), planck_locus.end(), mired, check);
970 const size_t i = size_t(entry - planck_locus.begin());
971 const locus_entry_t &low = planck_locus[i - 1], high = planck_locus[i];
972
973 /* Find interpolation factor. */
974 const float f = (mired - low.mired) / (high.mired - low.mired);
975
976 /* Interpolate point along Planckian locus. */
977 float2 uv = interpolate(low.uv, high.uv, f);
978
979 /* Compute and interpolate isotherm. */
980 const float2 isotherm0 = normalize(float2(1.0f, low.t));
981 const float2 isotherm1 = normalize(float2(1.0f, high.t));
982 const float2 isotherm = normalize(interpolate(isotherm0, isotherm1, f));
983
984 /* Offset away from the Planckian locus according to the tint.
985 * Tint is parameterized such that +-3000 tint corresponds to +-1 delta UV. */
986 uv -= isotherm * tint / 3000.0f;
987
988 /* Convert CIE 1960 uv -> xyY. */
989 const float x = 3.0f * uv.x / (2.0f * uv.x - 8.0f * uv.y + 4.0f);
990 const float y = 2.0f * uv.y / (2.0f * uv.x - 8.0f * uv.y + 4.0f);
991
992 /* Convert xyY -> XYZ (assuming Y=1). */
993 return float3{x / y, 1.0f, (1.0f - x - y) / y};
994}
995
996float3x3 chromatic_adaption_matrix(const float3 &from_XYZ, const float3 &to_XYZ)
997{
998 /* Bradford transformation matrix (XYZ -> LMS). */
999 static const float3x3 bradford{
1000 {0.8951f, -0.7502f, 0.0389f},
1001 {0.2664f, 1.7135f, -0.0685f},
1002 {-0.1614f, 0.0367f, 1.0296f},
1003 };
1004
1005 /* Compute white points in LMS space. */
1006 const float3 from_LMS = bradford * from_XYZ / from_XYZ.y;
1007 const float3 to_LMS = bradford * to_XYZ / to_XYZ.y;
1008
1009 /* Assemble full transform: XYZ -> LMS -> adapted LMS -> adapted XYZ. */
1010 return invert(bradford) * from_scale<float3x3>(to_LMS / from_LMS) * bradford;
1011}
1012
1014
1015} // namespace math
1016} // namespace blender
#define BLI_YUV_ITU_BT709
#define BLI_YCC_JFIF_0_255
#define BLI_YCC_ITU_BT601
#define BLI_YUV_ITU_BT601
#define BLI_YCC_ITU_BT709
#define MALWAYS_INLINE
#define CLAMP(a, b, c)
#define BLI_assert_unreachable()
#define SWAP(type, a, b)
#define BLI_assert_msg(a, msg)
btMatrix3x3 scaled(const btVector3 &s) const
Create a scaled copy of the matrix.
double y() const
Definition frames.hpp:85
double x() const
Definition frames.hpp:84
#define powf(x, y)
IMETHOD Vector diff(const Vector &a, const Vector &b, double dt)
Definition frames.inl:1166
#define exp
float3x3 scale(float3x3 mat, float2 scale)
T clamp(const T &a, const T &min, const T &max)
float3 whitepoint_from_temp_tint(float temperature, float tint)
T length(const VecBase< T, Size > &a)
bool whitepoint_to_temp_tint(const float3 &white, float &temperature, float &tint)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
static const std::array< locus_entry_t, 31 > planck_locus
CartesianBasis invert(const CartesianBasis &basis)
MatT from_scale(const VecBase< typename MatT::base_type, ScaleDim > &scale)
T interpolate(const T &a, const T &b, const FactorT &t)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
float3x3 chromatic_adaption_matrix(const float3 &from_XYZ, const float3 &to_XYZ)
BMesh Iterators.
MINLINE float min_ff(float a, float b)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MALWAYS_INLINE float _bli_math_fastpow(const int exp, const int e2coeff, const float arg)
void linearrgb_to_srgb_v3_v3(float srgb[3], const float linear[3])
void rgb_float_set_hue_float_offset(float rgb[3], float hue_offset)
unsigned int uint
Definition BKE_text.h:55
void hsv_to_rgb(float h, float s, float v, float *r_r, float *r_g, float *r_b)
Definition math_color.cc:25
MALWAYS_INLINE float srgb_to_linearrgb_approx(float c)
MALWAYS_INLINE float linearrgb_to_srgb_approx(float c)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
void cpack_to_rgb(unsigned int col, float *r_r, float *r_g, float *r_b)
float srgb_to_linearrgb(float c)
void rgb_to_hsl(float r, float g, float b, float *r_h, float *r_s, float *r_l)
void ycc_to_rgb(float y, float cb, float cr, float *r_r, float *r_g, float *r_b, int colorspace)
void rgb_byte_set_hue_float_offset(unsigned char rgb[3], float hue_offset)
void srgb_to_linearrgb_v3_v3(float linear[3], const float srgb[3])
void rgb_to_hsl_v(const float rgb[3], float r_hsl[3])
void rgb_to_hsv_v(const float rgb[3], float r_hsv[3])
void BLI_init_srgb_conversion()
void rgb_to_hsv_compat_v(const float rgb[3], float r_hsv[3])
unsigned int hsv_to_cpack(float h, float s, float v)
void rgb_to_hsv(float r, float g, float b, float *r_h, float *r_s, float *r_v)
ATTR_WARN_UNUSED_RESULT const BMVert * v
unsigned int rgb_to_cpack(float r, float g, float b)
MINLINE void rgb_float_to_uchar(unsigned char r_col[3], const float col_f[3])
void rgb_to_ycc(float r, float g, float b, float *r_y, float *r_cb, float *r_cr, int colorspace)
float BLI_color_from_srgb_table[256]
VecBase< float, 2 > float2
unsigned char uchar
Definition BKE_text.h:58
void hsv_to_rgb_v(const float hsv[3], float r_rgb[3])
Definition math_color.cc:61
void hsv_clamp_v(float hsv[3], float v_max)
MINLINE float int_as_float(int i)
void rgb_to_hsl_compat_v(const float rgb[3], float r_hsl[3])
void rgb_to_yuv(float r, float g, float b, float *r_y, float *r_u, float *r_v, int colorspace)
Definition math_color.cc:71
MatBase< float, 3, 3 > float3x3
void hsl_to_rgb(float h, float s, float l, float *r_r, float *r_g, float *r_b)
Definition math_color.cc:42
void hsl_to_rgb_v(const float hsl[3], float r_rgb[3])
Definition math_color.cc:66
MALWAYS_INLINE float _bli_math_fastpow512(const float arg)
void yuv_to_rgb(float y, float u, float v, float *r_r, float *r_g, float *r_b, int colorspace)
Definition math_color.cc:95
bool hex_to_rgba(const char *hexcol, float *r_r, float *r_g, float *r_b, float *r_a)
MINLINE float max_ff(float a, float b)
void rgb_to_hsl_compat(float r, float g, float b, float *r_h, float *r_s, float *r_l)
void hex_to_rgb(const char *hexcol, float *r_r, float *r_g, float *r_b)
MALWAYS_INLINE float _bli_math_improve_5throot_solution(const float old_result, const float x)
signed int int32_t
Definition BKE_text.h:78
MINLINE void rgb_uchar_to_float(float r_col[3], const unsigned char col_ub[3])
void rgb_to_hsv_compat(float r, float g, float b, float *r_h, float *r_s, float *r_v)
VecBase< float, 3 > float3
MALWAYS_INLINE float _bli_math_rsqrt(float in)
MALWAYS_INLINE float _bli_math_fastpow24(const float arg)
void mul_v3_m3v3(float r[3], const float M[3][3], const float a[3])
float linearrgb_to_srgb(float c)
void linearrgb_to_srgb_uchar4_n(unsigned char(*srgb)[4], const float(*linear)[4], int size, const float(*matrix)[3]=nullptr)
MINLINE int float_as_int(float f)
MINLINE void rgba_float_to_uchar(unsigned char r_col[4], const float col_f[4])
#define floorf
#define fabsf
#define sqrtf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float mired
float dist(const float2 p) const
float2 uv
float t