1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  *
19  * The Original Code is: some of this file.
20  *
21  * */
22 
23 /** \file
24  * \ingroup bli
25  */
26 
27 #include "BLI_math_base.h"
28 #include "BLI_math_color.h"
29 #include "BLI_utildefines.h"
30 
31 #include "math.h"
32 
33 #ifndef __MATH_COLOR_INLINE_C__
34 #  define __MATH_COLOR_INLINE_C__
35 
36 /******************************** Color Space ********************************/
37 
38 #  ifdef __SSE2__
39 
srgb_to_linearrgb_v4_simd(const __m128 c)40 MALWAYS_INLINE __m128 srgb_to_linearrgb_v4_simd(const __m128 c)
41 {
42   __m128 cmp = _mm_cmplt_ps(c, _mm_set1_ps(0.04045f));
43   __m128 lt = _mm_max_ps(_mm_mul_ps(c, _mm_set1_ps(1.0f / 12.92f)), _mm_set1_ps(0.0f));
44   __m128 gtebase = _mm_mul_ps(_mm_add_ps(c, _mm_set1_ps(0.055f)),
45                               _mm_set1_ps(1.0f / 1.055f)); /* fma */
46   __m128 gte = _bli_math_fastpow24(gtebase);
47   return _bli_math_blend_sse(cmp, lt, gte);
48 }
49 
linearrgb_to_srgb_v4_simd(const __m128 c)50 MALWAYS_INLINE __m128 linearrgb_to_srgb_v4_simd(const __m128 c)
51 {
52   __m128 cmp = _mm_cmplt_ps(c, _mm_set1_ps(0.0031308f));
53   __m128 lt = _mm_max_ps(_mm_mul_ps(c, _mm_set1_ps(12.92f)), _mm_set1_ps(0.0f));
54   __m128 gte = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(1.055f), _bli_math_fastpow512(c)),
55                           _mm_set1_ps(-0.055f));
56   return _bli_math_blend_sse(cmp, lt, gte);
57 }
58 
srgb_to_linearrgb_v3_v3(float linear[3],const float srgb[3])59 MINLINE void srgb_to_linearrgb_v3_v3(float linear[3], const float srgb[3])
60 {
61   float r[4] = {srgb[0], srgb[1], srgb[2], 1.0f};
62   __m128 *rv = (__m128 *)&r;
63   *rv = srgb_to_linearrgb_v4_simd(*rv);
64   linear[0] = r[0];
65   linear[1] = r[1];
66   linear[2] = r[2];
67 }
68 
linearrgb_to_srgb_v3_v3(float srgb[3],const float linear[3])69 MINLINE void linearrgb_to_srgb_v3_v3(float srgb[3], const float linear[3])
70 {
71   float r[4] = {linear[0], linear[1], linear[2], 1.0f};
72   __m128 *rv = (__m128 *)&r;
73   *rv = linearrgb_to_srgb_v4_simd(*rv);
74   srgb[0] = r[0];
75   srgb[1] = r[1];
76   srgb[2] = r[2];
77 }
78 
79 #  else  /* __SSE2__ */
80 
srgb_to_linearrgb_v3_v3(float linear[3],const float srgb[3])81 MINLINE void srgb_to_linearrgb_v3_v3(float linear[3], const float srgb[3])
82 {
83   linear[0] = srgb_to_linearrgb(srgb[0]);
84   linear[1] = srgb_to_linearrgb(srgb[1]);
85   linear[2] = srgb_to_linearrgb(srgb[2]);
86 }
87 
linearrgb_to_srgb_v3_v3(float srgb[3],const float linear[3])88 MINLINE void linearrgb_to_srgb_v3_v3(float srgb[3], const float linear[3])
89 {
90   srgb[0] = linearrgb_to_srgb(linear[0]);
91   srgb[1] = linearrgb_to_srgb(linear[1]);
92   srgb[2] = linearrgb_to_srgb(linear[2]);
93 }
94 #  endif /* __SSE2__ */
95 
srgb_to_linearrgb_v4(float linear[4],const float srgb[4])96 MINLINE void srgb_to_linearrgb_v4(float linear[4], const float srgb[4])
97 {
98   srgb_to_linearrgb_v3_v3(linear, srgb);
99   linear[3] = srgb[3];
100 }
101 
linearrgb_to_srgb_v4(float srgb[4],const float linear[4])102 MINLINE void linearrgb_to_srgb_v4(float srgb[4], const float linear[4])
103 {
104   linearrgb_to_srgb_v3_v3(srgb, linear);
105   srgb[3] = linear[3];
106 }
107 
linearrgb_to_srgb_uchar3(unsigned char srgb[3],const float linear[3])108 MINLINE void linearrgb_to_srgb_uchar3(unsigned char srgb[3], const float linear[3])
109 {
110   float srgb_f[3];
111 
112   linearrgb_to_srgb_v3_v3(srgb_f, linear);
113   unit_float_to_uchar_clamp_v3(srgb, srgb_f);
114 }
115 
linearrgb_to_srgb_uchar4(unsigned char srgb[4],const float linear[4])116 MINLINE void linearrgb_to_srgb_uchar4(unsigned char srgb[4], const float linear[4])
117 {
118   float srgb_f[4];
119 
120   linearrgb_to_srgb_v4(srgb_f, linear);
121   unit_float_to_uchar_clamp_v4(srgb, srgb_f);
122 }
123 
124 /* predivide versions to work on associated/pre-multiplied alpha. if this should
125  * be done or not depends on the background the image will be composited over,
126  * ideally you would never do color space conversion on an image with alpha
127  * because it is ill defined */
128 
srgb_to_linearrgb_predivide_v4(float linear[4],const float srgb[4])129 MINLINE void srgb_to_linearrgb_predivide_v4(float linear[4], const float srgb[4])
130 {
131   float alpha, inv_alpha;
132 
133   if (srgb[3] == 1.0f || srgb[3] == 0.0f) {
134     alpha = 1.0f;
135     inv_alpha = 1.0f;
136   }
137   else {
138     alpha = srgb[3];
139     inv_alpha = 1.0f / alpha;
140   }
141 
142   linear[0] = srgb[0] * inv_alpha;
143   linear[1] = srgb[1] * inv_alpha;
144   linear[2] = srgb[2] * inv_alpha;
145   linear[3] = srgb[3];
146   srgb_to_linearrgb_v3_v3(linear, linear);
147   linear[0] *= alpha;
148   linear[1] *= alpha;
149   linear[2] *= alpha;
150 }
151 
linearrgb_to_srgb_predivide_v4(float srgb[4],const float linear[4])152 MINLINE void linearrgb_to_srgb_predivide_v4(float srgb[4], const float linear[4])
153 {
154   float alpha, inv_alpha;
155 
156   if (linear[3] == 1.0f || linear[3] == 0.0f) {
157     alpha = 1.0f;
158     inv_alpha = 1.0f;
159   }
160   else {
161     alpha = linear[3];
162     inv_alpha = 1.0f / alpha;
163   }
164 
165   srgb[0] = linear[0] * inv_alpha;
166   srgb[1] = linear[1] * inv_alpha;
167   srgb[2] = linear[2] * inv_alpha;
168   srgb[3] = linear[3];
169   linearrgb_to_srgb_v3_v3(srgb, srgb);
170   srgb[0] *= alpha;
171   srgb[1] *= alpha;
172   srgb[2] *= alpha;
173 }
174 
175 /* LUT accelerated conversions */
176 
177 extern float BLI_color_from_srgb_table[256];
178 extern unsigned short BLI_color_to_srgb_table[0x10000];
179 
to_srgb_table_lookup(const float f)180 MINLINE unsigned short to_srgb_table_lookup(const float f)
181 {
182 
183   union {
184     float f;
185     unsigned short us[2];
186   } tmp;
187   tmp.f = f;
188 #  ifdef __BIG_ENDIAN__
189   return BLI_color_to_srgb_table[tmp.us[0]];
190 #  else
191   return BLI_color_to_srgb_table[tmp.us[1]];
192 #  endif
193 }
194 
linearrgb_to_srgb_ushort4(unsigned short srgb[4],const float linear[4])195 MINLINE void linearrgb_to_srgb_ushort4(unsigned short srgb[4], const float linear[4])
196 {
197   srgb[0] = to_srgb_table_lookup(linear[0]);
198   srgb[1] = to_srgb_table_lookup(linear[1]);
199   srgb[2] = to_srgb_table_lookup(linear[2]);
200   srgb[3] = unit_float_to_ushort_clamp(linear[3]);
201 }
202 
srgb_to_linearrgb_uchar4(float linear[4],const unsigned char srgb[4])203 MINLINE void srgb_to_linearrgb_uchar4(float linear[4], const unsigned char srgb[4])
204 {
205   linear[0] = BLI_color_from_srgb_table[srgb[0]];
206   linear[1] = BLI_color_from_srgb_table[srgb[1]];
207   linear[2] = BLI_color_from_srgb_table[srgb[2]];
208   linear[3] = srgb[3] * (1.0f / 255.0f);
209 }
210 
srgb_to_linearrgb_uchar4_predivide(float linear[4],const unsigned char srgb[4])211 MINLINE void srgb_to_linearrgb_uchar4_predivide(float linear[4], const unsigned char srgb[4])
212 {
213   float fsrgb[4];
214   int i;
215 
216   if (srgb[3] == 255 || srgb[3] == 0) {
217     srgb_to_linearrgb_uchar4(linear, srgb);
218     return;
219   }
220 
221   for (i = 0; i < 4; i++) {
222     fsrgb[i] = srgb[i] * (1.0f / 255.0f);
223   }
224 
225   srgb_to_linearrgb_predivide_v4(linear, fsrgb);
226 }
227 
rgba_uchar_args_set(uchar col[4],const uchar r,const uchar g,const uchar b,const uchar a)228 MINLINE void rgba_uchar_args_set(
229     uchar col[4], const uchar r, const uchar g, const uchar b, const uchar a)
230 {
231   col[0] = r;
232   col[1] = g;
233   col[2] = b;
234   col[3] = a;
235 }
236 
rgba_float_args_set(float col[4],const float r,const float g,const float b,const float a)237 MINLINE void rgba_float_args_set(
238     float col[4], const float r, const float g, const float b, const float a)
239 {
240   col[0] = r;
241   col[1] = g;
242   col[2] = b;
243   col[3] = a;
244 }
245 
rgba_uchar_args_test_set(uchar col[4],const uchar r,const uchar g,const uchar b,const uchar a)246 MINLINE void rgba_uchar_args_test_set(
247     uchar col[4], const uchar r, const uchar g, const uchar b, const uchar a)
248 {
249   if (col[3] == 0) {
250     col[0] = r;
251     col[1] = g;
252     col[2] = b;
253     col[3] = a;
254   }
255 }
256 
cpack_cpy_3ub(unsigned char r_col[3],const unsigned int pack)257 MINLINE void cpack_cpy_3ub(unsigned char r_col[3], const unsigned int pack)
258 {
259   r_col[0] = ((pack) >> 0) & 0xFF;
260   r_col[1] = ((pack) >> 8) & 0xFF;
261   r_col[2] = ((pack) >> 16) & 0xFF;
262 }
263 
264 /** \name RGB/Grayscale Functions
265  *
266  * \warning
267  * These are only an approximation,
268  * in almost _all_ cases, #IMB_colormanagement_get_luminance should be used instead. However for
269  * screen-only colors which don't depend on the currently loaded profile - this is preferred.
270  * Checking theme colors for contrast, etc. Basically anything outside the render pipeline.
271  *
272  * \{ */
273 
274 /**
275  * ITU-R BT.709 primaries
276  * https://en.wikipedia.org/wiki/Relative_luminance
277  *
278  * Real values are:
279  * ``Y = 0.2126390059(R) + 0.7151686788(G) + 0.0721923154(B)``
280  * according to: "Derivation of Basic Television Color Equations", RP 177-1993
281  *
282  * As this sums slightly above 1.0, the document recommends to use:
283  * ``0.2126(R) + 0.7152(G) + 0.0722(B)``, as used here.
284  *
285  * The high precision values are used to calculate the rounded byte weights so they add up to 255:
286  * ``54(R) + 182(G) + 19(B)``
287  */
rgb_to_grayscale(const float rgb[3])288 MINLINE float rgb_to_grayscale(const float rgb[3])
289 {
290   return (0.2126f * rgb[0]) + (0.7152f * rgb[1]) + (0.0722f * rgb[2]);
291 }
292 
rgb_to_grayscale_byte(const unsigned char rgb[3])293 MINLINE unsigned char rgb_to_grayscale_byte(const unsigned char rgb[3])
294 {
295   return (unsigned char)(((54 * (unsigned short)rgb[0]) + (182 * (unsigned short)rgb[1]) +
296                           (19 * (unsigned short)rgb[2])) /
297                          255);
298 }
299 
300 /** \} */
301 
compare_rgb_uchar(const unsigned char col_a[3],const unsigned char col_b[3],const int limit)302 MINLINE int compare_rgb_uchar(const unsigned char col_a[3],
303                               const unsigned char col_b[3],
304                               const int limit)
305 {
306   const int r = (int)col_a[0] - (int)col_b[0];
307   if (abs(r) < limit) {
308     const int g = (int)col_a[1] - (int)col_b[1];
309     if (abs(g) < limit) {
310       const int b = (int)col_a[2] - (int)col_b[2];
311       if (abs(b) < limit) {
312         return 1;
313       }
314     }
315   }
316 
317   return 0;
318 }
319 
320 /* Using a triangle distribution which gives a more final uniform noise.
321  * See Banding in Games:A Noisy Rant(revision 5) Mikkel Gjøl, Playdead (slide 27) */
322 /* Return triangle noise in [-0.5..1.5[ range */
dither_random_value(float s,float t)323 MINLINE float dither_random_value(float s, float t)
324 {
325   /* Original code from https://www.shadertoy.com/view/4t2SDh */
326   /* The noise reshaping technique does not work on CPU.
327    * We generate and merge two distribution instead */
328   /* Uniform noise in [0..1[ range */
329   float nrnd0 = sinf(s * 12.9898f + t * 78.233f) * 43758.5453f;
330   float nrnd1 = sinf(s * 19.9898f + t * 119.233f) * 43798.5453f;
331   nrnd0 -= floorf(nrnd0);
332   nrnd1 -= floorf(nrnd1);
333   /* Convert uniform distribution into triangle-shaped distribution. */
334   return nrnd0 + nrnd1 - 0.5f;
335 }
336 
float_to_byte_dither_v3(unsigned char b[3],const float f[3],float dither,float s,float t)337 MINLINE void float_to_byte_dither_v3(
338     unsigned char b[3], const float f[3], float dither, float s, float t)
339 {
340   float dither_value = dither_random_value(s, t) * 0.0033f * dither;
341 
342   b[0] = unit_float_to_uchar_clamp(dither_value + f[0]);
343   b[1] = unit_float_to_uchar_clamp(dither_value + f[1]);
344   b[2] = unit_float_to_uchar_clamp(dither_value + f[2]);
345 }
346 
347 /**************** Alpha Transformations *****************/
348 
premul_to_straight_v4_v4(float straight[4],const float premul[4])349 MINLINE void premul_to_straight_v4_v4(float straight[4], const float premul[4])
350 {
351   if (premul[3] == 0.0f || premul[3] == 1.0f) {
352     straight[0] = premul[0];
353     straight[1] = premul[1];
354     straight[2] = premul[2];
355     straight[3] = premul[3];
356   }
357   else {
358     const float alpha_inv = 1.0f / premul[3];
359     straight[0] = premul[0] * alpha_inv;
360     straight[1] = premul[1] * alpha_inv;
361     straight[2] = premul[2] * alpha_inv;
362     straight[3] = premul[3];
363   }
364 }
365 
premul_to_straight_v4(float color[4])366 MINLINE void premul_to_straight_v4(float color[4])
367 {
368   premul_to_straight_v4_v4(color, color);
369 }
370 
straight_to_premul_v4_v4(float premul[4],const float straight[4])371 MINLINE void straight_to_premul_v4_v4(float premul[4], const float straight[4])
372 {
373   const float alpha = straight[3];
374   premul[0] = straight[0] * alpha;
375   premul[1] = straight[1] * alpha;
376   premul[2] = straight[2] * alpha;
377   premul[3] = straight[3];
378 }
379 
straight_to_premul_v4(float color[4])380 MINLINE void straight_to_premul_v4(float color[4])
381 {
382   straight_to_premul_v4_v4(color, color);
383 }
384 
straight_uchar_to_premul_float(float result[4],const unsigned char color[4])385 MINLINE void straight_uchar_to_premul_float(float result[4], const unsigned char color[4])
386 {
387   const float alpha = color[3] * (1.0f / 255.0f);
388   const float fac = alpha * (1.0f / 255.0f);
389 
390   result[0] = color[0] * fac;
391   result[1] = color[1] * fac;
392   result[2] = color[2] * fac;
393   result[3] = alpha;
394 }
395 
premul_float_to_straight_uchar(unsigned char * result,const float color[4])396 MINLINE void premul_float_to_straight_uchar(unsigned char *result, const float color[4])
397 {
398   if (color[3] == 0.0f || color[3] == 1.0f) {
399     result[0] = unit_float_to_uchar_clamp(color[0]);
400     result[1] = unit_float_to_uchar_clamp(color[1]);
401     result[2] = unit_float_to_uchar_clamp(color[2]);
402     result[3] = unit_float_to_uchar_clamp(color[3]);
403   }
404   else {
405     const float alpha_inv = 1.0f / color[3];
406 
407     /* hopefully this would be optimized */
408     result[0] = unit_float_to_uchar_clamp(color[0] * alpha_inv);
409     result[1] = unit_float_to_uchar_clamp(color[1] * alpha_inv);
410     result[2] = unit_float_to_uchar_clamp(color[2] * alpha_inv);
411     result[3] = unit_float_to_uchar_clamp(color[3]);
412   }
413 }
414 
415 #endif /* __MATH_COLOR_INLINE_C__ */
416