149
149
srgb[3] = FTOUSHORT(linear[3]);
152
MINLINE void linearrgb_to_srgb_ushort4_predivide(unsigned short srgb[4], const float linear[4])
154
float alpha, inv_alpha, t;
157
if (linear[3] == 1.0f || linear[3] == 0.0f) {
158
linearrgb_to_srgb_ushort4(srgb, linear);
163
inv_alpha = 1.0f / alpha;
165
for (i = 0; i < 3; ++i) {
166
t = linear[i] * inv_alpha;
167
srgb[i] = (t < 1.0f) ? (unsigned short) (to_srgb_table_lookup(t) * alpha) : FTOUSHORT(linearrgb_to_srgb(t) * alpha);
170
srgb[3] = FTOUSHORT(linear[3]);
173
152
MINLINE void srgb_to_linearrgb_uchar4(float linear[4], const unsigned char srgb[4])
175
154
linear[0] = BLI_color_from_srgb_table[srgb[0]];
197
176
/* color macros for themes */
198
#define rgba_char_args_set_fl(col, r, g, b, a) rgba_char_args_set(col, r * 255, g * 255, b * 255, a * 255)
177
#define rgba_char_args_set_fl(col, r, g, b, a) \
178
rgba_char_args_set(col, (r) * 255, (g) * 255, (b) * 255, (a) * 255)
200
180
MINLINE void rgba_char_args_set(char col[4], const char r, const char g, const char b, const char a)
198
MINLINE void cpack_cpy_3ub(unsigned char r_col[3], const unsigned int pack)
200
r_col[0] = ((pack) >> 0) & 0xFF;
201
r_col[1] = ((pack) >> 8) & 0xFF;
202
r_col[2] = ((pack) >> 16) & 0xFF;
207
* regarding #rgb_to_bw vs #rgb_to_grayscale,
208
* it seems nobody knows why we have both functions which convert color to grays
209
* but with different influences, this is quite stupid, and should be resolved
210
* by someone who knows this stuff: see this thread
211
* http://lists.blender.org/pipermail/bf-committers/2012-June/037180.html
213
* Only conclusion is that rgb_to_grayscale is used more for compositing.
215
MINLINE float rgb_to_bw(const float rgb[3])
217
return 0.35f * rgb[0] + 0.45f * rgb[1] + 0.2f * rgb[2];
220
/* non-linear luma from ITU-R BT.601-2
221
* see: http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html#RTFToC11
222
* note: the values used for are not exact matches to those documented above,
223
* but they are from the same */
224
MINLINE float rgb_to_grayscale(const float rgb[3])
226
return 0.3f * rgb[0] + 0.58f * rgb[1] + 0.12f * rgb[2];
229
MINLINE unsigned char rgb_to_grayscale_byte(const unsigned char rgb[3])
231
return (76 * (unsigned short) rgb[0] + 148 * (unsigned short) rgb[1] + 31 * (unsigned short) rgb[2]) / 255;
234
/* luma from defined by 'YCC_JFIF', see #rgb_to_ycc */
235
MINLINE float rgb_to_luma(const float rgb[3])
237
return 0.299f * rgb[0] + 0.587f * rgb[1] + 0.114f * rgb[2];
240
MINLINE unsigned char rgb_to_luma_byte(const unsigned char rgb[3])
242
return (76 * (unsigned short) rgb[0] + 150 * (unsigned short) rgb[1] + 29 * (unsigned short) rgb[2]) / 255;
245
/* gamma-corrected RGB --> CIE XYZ
246
* for this function we only get the Y component
247
* see: http://software.intel.com/sites/products/documentation/hpc/ipp/ippi/ippi_ch6/ch6_color_models.html
250
* luminance rec. 709 */
251
MINLINE float rgb_to_luma_y(const float rgb[3])
253
return 0.212671f * rgb[0] + 0.71516f * rgb[1] + 0.072169f * rgb[2];
256
MINLINE int compare_rgb_uchar(const unsigned char col_a[3], const unsigned char col_b[3], const int limit)
258
int r = (int)col_a[0] - (int)col_b[0];
259
if (ABS(r) < limit) {
260
int g = (int)col_a[1] - (int)col_b[1];
261
if (ABS(g) < limit) {
262
int b = (int)col_a[2] - (int)col_b[2];
263
if (ABS(b) < limit) {
272
/**************** Alpha Transformations *****************/
274
MINLINE void premul_to_straight_v4_v4(float straight[4], const float premul[4])
276
if (premul[3] == 0.0f || premul[3] == 1.0f) {
277
straight[0] = premul[0];
278
straight[1] = premul[1];
279
straight[2] = premul[2];
280
straight[3] = premul[3];
283
float alpha_inv = 1.0f / premul[3];
284
straight[0] = premul[0] * alpha_inv;
285
straight[1] = premul[1] * alpha_inv;
286
straight[2] = premul[2] * alpha_inv;
287
straight[3] = premul[3];
291
MINLINE void premul_to_straight_v4(float color[4])
293
premul_to_straight_v4_v4(color, color);
296
MINLINE void straight_to_premul_v4_v4(float premul[4], const float straight[4])
298
float alpha = straight[3];
299
premul[0] = straight[0] * alpha;
300
premul[1] = straight[1] * alpha;
301
premul[2] = straight[2] * alpha;
302
premul[3] = straight[3];
305
MINLINE void straight_to_premul_v4(float color[4])
307
straight_to_premul_v4_v4(color, color);
310
MINLINE void straight_uchar_to_premul_float(float result[4], const unsigned char color[4])
312
float alpha = color[3] / 255.0f;
313
float fac = alpha / 255.0f;
315
result[0] = color[0] * fac;
316
result[1] = color[1] * fac;
317
result[2] = color[2] * fac;
321
MINLINE void premul_float_to_straight_uchar(unsigned char *result, const float color[4])
323
if (color[3] == 0.0f || color[3] == 1.0f) {
324
result[0] = FTOCHAR(color[0]);
325
result[1] = FTOCHAR(color[1]);
326
result[2] = FTOCHAR(color[2]);
327
result[3] = FTOCHAR(color[3]);
330
float alpha_inv = 1.0f / color[3];
332
/* hopefully this would be optimized */
333
result[0] = FTOCHAR(color[0] * alpha_inv);
334
result[1] = FTOCHAR(color[1] * alpha_inv);
335
result[2] = FTOCHAR(color[2] * alpha_inv);
336
result[3] = FTOCHAR(color[3]);
218
340
#endif /* __MATH_COLOR_INLINE_C__ */