Blender V5.3
math_color_inline.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#pragma once
10
11#include "BLI_math_base_c.hh"
12#include "BLI_math_color_c.hh"
13
14#include <cmath>
15
16namespace blender {
17
18/* -------------------------------------------------------------------- */
21
22MINLINE void srgb_to_linearrgb_v4(float linear[4], const float srgb[4])
23{
24 srgb_to_linearrgb_v3_v3(linear, srgb);
25 linear[3] = srgb[3];
26}
27
28MINLINE void linearrgb_to_srgb_v4(float srgb[4], const float linear[4])
29{
30 linearrgb_to_srgb_v3_v3(srgb, linear);
31 srgb[3] = linear[3];
32}
33
34MINLINE void linearrgb_to_srgb_uchar3(unsigned char srgb[3], const float linear[3])
35{
36 float srgb_f[3];
37
38 linearrgb_to_srgb_v3_v3(srgb_f, linear);
39 unit_float_to_uchar_clamp_v3(srgb, srgb_f);
40}
41
42MINLINE void linearrgb_to_srgb_uchar4(unsigned char srgb[4], const float linear[4])
43{
44 float srgb_f[4];
45
46 linearrgb_to_srgb_v4(srgb_f, linear);
47 unit_float_to_uchar_clamp_v4(srgb, srgb_f);
48}
49
50/* predivide versions to work on associated/pre-multiplied alpha. if this should
51 * be done or not depends on the background the image will be composited over,
52 * ideally you would never do color space conversion on an image with alpha
53 * because it is ill defined */
54
55MINLINE void srgb_to_linearrgb_predivide_v4(float linear[4], const float srgb[4])
56{
57 float alpha, inv_alpha;
58
59 if (srgb[3] == 1.0f || srgb[3] == 0.0f) {
60 alpha = 1.0f;
61 inv_alpha = 1.0f;
62 }
63 else {
64 alpha = srgb[3];
65 inv_alpha = 1.0f / alpha;
66 }
67
68 linear[0] = srgb[0] * inv_alpha;
69 linear[1] = srgb[1] * inv_alpha;
70 linear[2] = srgb[2] * inv_alpha;
71 linear[3] = srgb[3];
72 srgb_to_linearrgb_v3_v3(linear, linear);
73 linear[0] *= alpha;
74 linear[1] *= alpha;
75 linear[2] *= alpha;
76}
77
78MINLINE void linearrgb_to_srgb_predivide_v4(float srgb[4], const float linear[4])
79{
80 float alpha, inv_alpha;
81
82 if (linear[3] == 1.0f || linear[3] == 0.0f) {
83 alpha = 1.0f;
84 inv_alpha = 1.0f;
85 }
86 else {
87 alpha = linear[3];
88 inv_alpha = 1.0f / alpha;
89 }
90
91 srgb[0] = linear[0] * inv_alpha;
92 srgb[1] = linear[1] * inv_alpha;
93 srgb[2] = linear[2] * inv_alpha;
94 srgb[3] = linear[3];
95 linearrgb_to_srgb_v3_v3(srgb, srgb);
96 srgb[0] *= alpha;
97 srgb[1] *= alpha;
98 srgb[2] *= alpha;
99}
100
101/* LUT accelerated conversions */
102
103extern float BLI_color_from_srgb_table[256];
104
105MINLINE void srgb_to_linearrgb_uchar4(float linear[4], const unsigned char srgb[4])
106{
107 linear[0] = BLI_color_from_srgb_table[srgb[0]];
108 linear[1] = BLI_color_from_srgb_table[srgb[1]];
109 linear[2] = BLI_color_from_srgb_table[srgb[2]];
110 linear[3] = srgb[3] * (1.0f / 255.0f);
111}
112
113MINLINE void srgb_to_linearrgb_uchar4_predivide(float linear[4], const unsigned char srgb[4])
114{
115 float fsrgb[4];
116 int i;
117
118 if (srgb[3] == 255 || srgb[3] == 0) {
119 srgb_to_linearrgb_uchar4(linear, srgb);
120 return;
121 }
122
123 for (i = 0; i < 4; i++) {
124 fsrgb[i] = srgb[i] * (1.0f / 255.0f);
125 }
126
127 srgb_to_linearrgb_predivide_v4(linear, fsrgb);
128}
129
130MINLINE void rgb_uchar_to_float(float r_col[3], const uchar col_ub[3])
131{
132 r_col[0] = float(col_ub[0]) * (1.0f / 255.0f);
133 r_col[1] = float(col_ub[1]) * (1.0f / 255.0f);
134 r_col[2] = float(col_ub[2]) * (1.0f / 255.0f);
135}
136
137MINLINE void rgba_uchar_to_float(float r_col[4], const uchar col_ub[4])
138{
139 r_col[0] = float(col_ub[0]) * (1.0f / 255.0f);
140 r_col[1] = float(col_ub[1]) * (1.0f / 255.0f);
141 r_col[2] = float(col_ub[2]) * (1.0f / 255.0f);
142 r_col[3] = float(col_ub[3]) * (1.0f / 255.0f);
143}
144
145MINLINE void rgb_float_to_uchar(uchar r_col[3], const float col_f[3])
146{
147 unit_float_to_uchar_clamp_v3(r_col, col_f);
148}
149
150MINLINE void rgba_float_to_uchar(uchar r_col[4], const float col_f[4])
151{
152 unit_float_to_uchar_clamp_v4(r_col, col_f);
153}
154
156 uchar col[4], const uchar r, const uchar g, const uchar b, const uchar a)
157{
158 col[0] = r;
159 col[1] = g;
160 col[2] = b;
161 col[3] = a;
162}
163
165 float col[4], const float r, const float g, const float b, const float a)
166{
167 col[0] = r;
168 col[1] = g;
169 col[2] = b;
170 col[3] = a;
171}
172
174 uchar col[4], const uchar r, const uchar g, const uchar b, const uchar a)
175{
176 if (col[3] == 0) {
177 col[0] = r;
178 col[1] = g;
179 col[2] = b;
180 col[3] = a;
181 }
182}
183
184MINLINE void cpack_cpy_3ub(unsigned char r_col[3], const unsigned int pack)
185{
186 r_col[0] = ((pack) >> 0) & 0xFF;
187 r_col[1] = ((pack) >> 8) & 0xFF;
188 r_col[2] = ((pack) >> 16) & 0xFF;
189}
190
192
193/* -------------------------------------------------------------------- */
201
202MINLINE float srgb_to_grayscale(const float rgb[3])
203{
204 /* Real values are:
205 * `Y = 0.2126390059(R) + 0.7151686788(G) + 0.0721923154(B)`
206 * according to: "Derivation of Basic Television Color Equations", RP 177-1993
207 *
208 * As this sums slightly above 1.0, the document recommends to use:
209 * `0.2126(R) + 0.7152(G) + 0.0722(B)`, as used here. */
210 return (0.2126f * rgb[0]) + (0.7152f * rgb[1]) + (0.0722f * rgb[2]);
211}
212
213MINLINE unsigned char srgb_to_grayscale_byte(const unsigned char rgb[3])
214{
215 /* The high precision values are used to calculate the rounded byte weights so they add up to
216 * 255: `54(R) + 182(G) + 19(B)` */
217 return static_cast<unsigned char>(((54 * static_cast<unsigned short>(rgb[0])) +
218 (182 * static_cast<unsigned short>(rgb[1])) +
219 (19 * static_cast<unsigned short>(rgb[2]))) /
220 255);
221}
222
224
225/* -------------------------------------------------------------------- */
228
229MINLINE int compare_rgb_uchar(const unsigned char col_a[3],
230 const unsigned char col_b[3],
231 const int limit)
232{
233 const int r = int(col_a[0]) - int(col_b[0]);
234 if (abs(r) < limit) {
235 const int g = int(col_a[1]) - int(col_b[1]);
236 if (abs(g) < limit) {
237 const int b = int(col_a[2]) - int(col_b[2]);
238 if (abs(b) < limit) {
239 return 1;
240 }
241 }
242 }
243
244 return 0;
245}
246
247/* 2D hash (iqint3) recommended from "Hash Functions for GPU Rendering" JCGT Vol. 9, No. 3, 2020
248 * https://jcgt.org/published/0009/03/02/ */
250{
251 const uint32_t qx = 1103515245u * ((x >> 1u) ^ (y));
252 const uint32_t qy = 1103515245u * ((y >> 1u) ^ (x));
253 const uint32_t n = 1103515245u * ((qx) ^ (qy >> 3u));
254 return float(n) * (1.0f / float(0xffffffffu));
255}
256
258{
259 float v = hash_iqint3_f(x, y);
260 /* Convert uniform distribution into triangle-shaped distribution. Based on
261 * "remap_pdf_tri_unity" from https://www.shadertoy.com/view/WldSRf */
262 v = v * 2.0f - 1.0f;
263 v = signf(v) * (1.0f - sqrtf(1.0f - fabsf(v)));
264 return v;
265}
266
268 unsigned char b[3], const float f[3], float dither, int x, int y)
269{
270 float dither_value = dither_random_value(x, y) * 0.0033f * dither;
271
272 b[0] = unit_float_to_uchar_clamp(dither_value + f[0]);
273 b[1] = unit_float_to_uchar_clamp(dither_value + f[1]);
274 b[2] = unit_float_to_uchar_clamp(dither_value + f[2]);
275}
276
278
279/* -------------------------------------------------------------------- */
282
283MINLINE void premul_to_straight_v4_v4(float straight[4], const float premul[4])
284{
285 if (premul[3] == 0.0f || premul[3] == 1.0f) {
286 straight[0] = premul[0];
287 straight[1] = premul[1];
288 straight[2] = premul[2];
289 straight[3] = premul[3];
290 }
291 else {
292 const float alpha_inv = 1.0f / premul[3];
293 straight[0] = premul[0] * alpha_inv;
294 straight[1] = premul[1] * alpha_inv;
295 straight[2] = premul[2] * alpha_inv;
296 straight[3] = premul[3];
297 }
298}
299
304
305MINLINE void straight_to_premul_v4_v4(float premul[4], const float straight[4])
306{
307 const float alpha = straight[3];
308 premul[0] = straight[0] * alpha;
309 premul[1] = straight[1] * alpha;
310 premul[2] = straight[2] * alpha;
311 premul[3] = straight[3];
312}
313
318
319MINLINE void straight_uchar_to_premul_float(float result[4], const unsigned char color[4])
320{
321 const float alpha = color[3] * (1.0f / 255.0f);
322 const float fac = alpha * (1.0f / 255.0f);
323
324 result[0] = color[0] * fac;
325 result[1] = color[1] * fac;
326 result[2] = color[2] * fac;
327 result[3] = alpha;
328}
329
330MINLINE void premul_float_to_straight_uchar(unsigned char *result, const float color[4])
331{
332 if (color[3] == 0.0f || color[3] == 1.0f) {
337 }
338 else {
339 const float alpha_inv = 1.0f / color[3];
340
341 /* hopefully this would be optimized */
342 result[0] = unit_float_to_uchar_clamp(color[0] * alpha_inv);
343 result[1] = unit_float_to_uchar_clamp(color[1] * alpha_inv);
344 result[2] = unit_float_to_uchar_clamp(color[2] * alpha_inv);
346 }
347}
348
350
351} // namespace blender
#define MINLINE
#define abs
#define unit_float_to_uchar_clamp_v4(v1, v2)
#define unit_float_to_uchar_clamp_v3(v1, v2)
BMesh Iterators.
MINLINE void rgba_uchar_to_float(float r_col[4], const unsigned char col_ub[4])
MINLINE void linearrgb_to_srgb_uchar3(unsigned char srgb[3], const float linear[3])
MINLINE unsigned char unit_float_to_uchar_clamp(float val)
void linearrgb_to_srgb_v3_v3(float srgb[3], const float linear[3])
MINLINE float hash_iqint3_f(const uint32_t x, const uint32_t y)
MINLINE unsigned char srgb_to_grayscale_byte(const unsigned char rgb[3])
MINLINE void linearrgb_to_srgb_v4(float srgb[4], const float linear[4])
MINLINE void srgb_to_linearrgb_uchar4(float linear[4], const unsigned char srgb[4])
void srgb_to_linearrgb_v3_v3(float linear[3], const float srgb[3])
MINLINE void rgba_uchar_args_set(unsigned char col[4], unsigned char r, unsigned char g, unsigned char b, unsigned char a)
MINLINE void srgb_to_linearrgb_v4(float linear[4], const float srgb[4])
MINLINE void straight_to_premul_v4_v4(float premul[4], const float straight[4])
ATTR_WARN_UNUSED_RESULT const BMVert * v
MINLINE void rgb_float_to_uchar(unsigned char r_col[3], const float col_f[3])
MINLINE float srgb_to_grayscale(const float rgb[3])
float BLI_color_from_srgb_table[256]
unsigned int uint32_t
Definition BKE_text.h:81
MINLINE float dither_random_value(int x, int y)
unsigned char uchar
Definition BKE_text.h:58
MINLINE float signf(float f)
MINLINE void premul_to_straight_v4(float color[4])
MINLINE void linearrgb_to_srgb_predivide_v4(float srgb[4], const float linear[4])
MINLINE void float_to_byte_dither_v3(unsigned char b[3], const float f[3], float dither, int x, int y)
MINLINE void cpack_cpy_3ub(unsigned char r_col[3], unsigned int pack)
MINLINE void straight_uchar_to_premul_float(float result[4], const unsigned char color[4])
MINLINE void srgb_to_linearrgb_predivide_v4(float linear[4], const float srgb[4])
MINLINE void rgb_uchar_to_float(float r_col[3], const unsigned char col_ub[3])
MINLINE void linearrgb_to_srgb_uchar4(unsigned char srgb[4], const float linear[4])
MINLINE int compare_rgb_uchar(const unsigned char col_a[3], const unsigned char col_b[3], int limit)
MINLINE void premul_float_to_straight_uchar(unsigned char *result, const float color[4])
MINLINE void rgba_uchar_args_test_set(unsigned char col[4], unsigned char r, unsigned char g, unsigned char b, unsigned char a)
MINLINE void straight_to_premul_v4(float color[4])
MINLINE void srgb_to_linearrgb_uchar4_predivide(float linear[4], const unsigned char srgb[4])
MINLINE void rgba_float_to_uchar(unsigned char r_col[4], const float col_f[4])
MINLINE void premul_to_straight_v4_v4(float straight[4], const float premul[4])
MINLINE void rgba_float_args_set(float col[4], float r, float g, float b, float a)
#define fabsf
#define sqrtf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float