93
93
void rgb_to_yuv(float r, float g, float b, float *ly, float *lu, float *lv)
96
y= 0.299f*r + 0.587f*g + 0.114f*b;
97
u=-0.147f*r - 0.289f*g + 0.436f*b;
98
v= 0.615f*r - 0.515f*g - 0.100f*b;
96
y = 0.299f * r + 0.587f * g + 0.114f * b;
97
u = -0.147f * r - 0.289f * g + 0.436f * b;
98
v = 0.615f * r - 0.515f * g - 0.100f * b;
105
105
void yuv_to_rgb(float y, float u, float v, float *lr, float *lg, float *lb)
109
g=y-0.394f*u - 0.581f*v;
109
g = y - 0.394f * u - 0.581f * v;
117
/* The RGB inputs are supposed gamma corrected and in the range 0 - 1.0f */
118
/* Output YCC have a range of 16-235 and 16-240 except with JFIF_0_255 where the range is 0-255 */
117
/* The RGB inputs are supposed gamma corrected and in the range 0 - 1.0f
119
* Output YCC have a range of 16-235 and 16-240 except with JFIF_0_255 where the range is 0-255 */
119
120
void rgb_to_ycc(float r, float g, float b, float *ly, float *lcb, float *lcr, int colorspace)
122
123
float y = 128.f, cr = 128.f, cb = 128.f;
128
129
switch (colorspace) {
129
case BLI_YCC_ITU_BT601 :
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;
134
case BLI_YCC_ITU_BT709 :
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;
139
case BLI_YCC_JFIF_0_255 :
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;
145
assert(!"invalid colorspace");
130
case BLI_YCC_ITU_BT601:
131
y = (0.257f * sr) + (0.504f * sg) + (0.098f * sb) + 16.0f;
132
cb = (-0.148f * sr) - (0.291f * sg) + (0.439f * sb) + 128.0f;
133
cr = (0.439f * sr) - (0.368f * sg) - (0.071f * sb) + 128.0f;
135
case BLI_YCC_ITU_BT709:
136
y = (0.183f * sr) + (0.614f * sg) + (0.062f * sb) + 16.0f;
137
cb = (-0.101f * sr) - (0.338f * sg) + (0.439f * sb) + 128.0f;
138
cr = (0.439f * sr) - (0.399f * sg) - (0.040f * sb) + 128.0f;
140
case BLI_YCC_JFIF_0_255:
141
y = (0.299f * sr) + (0.587f * sg) + (0.114f * sb);
142
cb = (-0.16874f * sr) - (0.33126f * sg) + (0.5f * sb) + 128.0f;
143
cr = (0.5f * sr) - (0.41869f * sg) - (0.08131f * sb) + 128.0f;
146
assert(!"invalid colorspace");
154
155
/* YCC input have a range of 16-235 and 16-240 except with JFIF_0_255 where the range is 0-255 */
155
156
/* RGB outputs are in the range 0 - 1.0f */
156
158
/* FIXME comment above must be wrong because BLI_YCC_ITU_BT601 y 16.0 cr 16.0 -> r -0.7009 */
157
159
void ycc_to_rgb(float y, float cb, float cr, float *lr, float *lg, float *lb, int colorspace)
159
161
float r = 128.f, g = 128.f, b = 128.f;
161
163
switch (colorspace) {
162
case BLI_YCC_ITU_BT601 :
163
r=1.164f*(y-16.0f)+1.596f*(cr-128.0f);
164
g=1.164f*(y-16.0f)-0.813f*(cr-128.0f)-0.392f*(cb-128.0f);
165
b=1.164f*(y-16.0f)+2.017f*(cb-128.0f);
167
case BLI_YCC_ITU_BT709 :
168
r=1.164f*(y-16.0f)+1.793f*(cr-128.0f);
169
g=1.164f*(y-16.0f)-0.534f*(cr-128.0f)-0.213f*(cb-128.0f);
170
b=1.164f*(y-16.0f)+2.115f*(cb-128.0f);
172
case BLI_YCC_JFIF_0_255 :
173
r=y+1.402f*cr - 179.456f;
174
g=y-0.34414f*cb - 0.71414f*cr + 135.45984f;
175
b=y+1.772f*cb - 226.816f;
178
assert(!"invalid colorspace");
164
case BLI_YCC_ITU_BT601:
165
r = 1.164f * (y - 16.0f) + 1.596f * (cr - 128.0f);
166
g = 1.164f * (y - 16.0f) - 0.813f * (cr - 128.0f) - 0.392f * (cb - 128.0f);
167
b = 1.164f * (y - 16.0f) + 2.017f * (cb - 128.0f);
169
case BLI_YCC_ITU_BT709:
170
r = 1.164f * (y - 16.0f) + 1.793f * (cr - 128.0f);
171
g = 1.164f * (y - 16.0f) - 0.534f * (cr - 128.0f) - 0.213f * (cb - 128.0f);
172
b = 1.164f * (y - 16.0f) + 2.115f * (cb - 128.0f);
174
case BLI_YCC_JFIF_0_255:
175
r = y + 1.402f * cr - 179.456f;
176
g = y - 0.34414f * cb - 0.71414f * cr + 135.45984f;
177
b = y + 1.772f * cb - 226.816f;
180
assert(!"invalid colorspace");
185
187
void hex_to_rgb(char *hexcol, float *r, float *g, float *b)
213
cmax = (g>cmax ? g:cmax);
214
cmin = (g<cmin ? g:cmin);
215
cmax = (b>cmax ? b:cmax);
216
cmin = (b<cmin ? b:cmin);
215
cmax = (g > cmax ? g : cmax);
216
cmin = (g < cmin ? g : cmin);
217
cmax = (b > cmax ? b : cmax);
218
cmin = (b < cmin ? b : cmin);
218
v = cmax; /* value */
220
v = cmax; /* value */
219
221
if (cmax != 0.0f)
220
s = (cmax - cmin)/cmax;
222
s = (cmax - cmin) / cmax;
228
rc = (cmax-r)/cdelta;
229
gc = (cmax-g)/cdelta;
230
bc = (cmax-b)/cdelta;
229
cdelta = cmax - cmin;
230
rc = (cmax - r) / cdelta;
231
gc = (cmax - g) / cdelta;
232
bc = (cmax - b) / cdelta;
237
else if (g == cmax) {
244
250
*lh = h / 360.0f;
245
if(*lh < 0.0f) *lh= 0.0f;
251
if (*lh < 0.0f) *lh = 0.0f;
249
255
void rgb_to_hsv_compat(float r, float g, float b, float *lh, float *ls, float *lv)
254
260
rgb_to_hsv(r, g, b, lh, ls, lv);
260
266
else if (*ls <= 0.0f) {
264
if(*lh==0.0f && orig_h >= 1.0f) {
270
if (*lh == 0.0f && orig_h >= 1.0f) {
271
277
void xyz_to_rgb(float xc, float yc, float zc, float *r, float *g, float *b, int colorspace)
273
switch (colorspace) {
275
*r = (3.50570f * xc) + (-1.73964f * yc) + (-0.544011f * zc);
276
*g = (-1.06906f * xc) + (1.97781f * yc) + (0.0351720f * zc);
277
*b = (0.0563117f * xc) + (-0.196994f * yc) + (1.05005f * zc);
279
case BLI_XYZ_REC709_SRGB:
280
*r = (3.240476f * xc) + (-1.537150f * yc) + (-0.498535f * zc);
281
*g = (-0.969256f * xc) + (1.875992f * yc) + (0.041556f * zc);
282
*b = (0.055648f * xc) + (-0.204043f * yc) + (1.057311f * zc);
285
*r = (2.28783848734076f * xc) + (-0.833367677835217f * yc) + (-0.454470795871421f * zc);
286
*g = (-0.511651380743862f * xc) + (1.42275837632178f * yc) + (0.0888930017552939f * zc);
287
*b = (0.00572040983140966f * xc) + (-0.0159068485104036f * yc) + (1.0101864083734f * zc);
279
switch (colorspace) {
281
*r = (3.50570f * xc) + (-1.73964f * yc) + (-0.544011f * zc);
282
*g = (-1.06906f * xc) + (1.97781f * yc) + (0.0351720f * zc);
283
*b = (0.0563117f * xc) + (-0.196994f * yc) + (1.05005f * zc);
285
case BLI_XYZ_REC709_SRGB:
286
*r = (3.240476f * xc) + (-1.537150f * yc) + (-0.498535f * zc);
287
*g = (-0.969256f * xc) + (1.875992f * yc) + (0.041556f * zc);
288
*b = (0.055648f * xc) + (-0.204043f * yc) + (1.057311f * zc);
291
*r = (2.28783848734076f * xc) + (-0.833367677835217f * yc) + (-0.454470795871421f * zc);
292
*g = (-0.511651380743862f * xc) + (1.42275837632178f * yc) + (0.0888930017552939f * zc);
293
*b = (0.00572040983140966f * xc) + (-0.0159068485104036f * yc) + (1.0101864083734f * zc);
292
298
/* we define a 'cpack' here as a (3 byte color code) number that can be expressed like 0xFFAA66 or so.
293
for that reason it is sensitive for endianness... with this function it works correctly
299
* for that reason it is sensitive for endianness... with this function it works correctly
296
302
unsigned int hsv_to_cpack(float h, float s, float v)
299
305
float rf, gf, bf;
300
306
unsigned int col;
302
308
hsv_to_rgb(h, s, v, &rf, &gf, &bf);
304
r= (short)(rf*255.0f);
305
g= (short)(gf*255.0f);
306
b= (short)(bf*255.0f);
308
col= ( r + (g*256) + (b*256*256) );
310
r = (short) (rf * 255.0f);
311
g = (short) (gf * 255.0f);
312
b = (short) (bf * 255.0f);
314
col = (r + (g * 256) + (b * 256 * 256));
313
318
unsigned int rgb_to_cpack(float r, float g, float b)
317
ir= (int)floor(255.0f*r);
318
if(ir<0) ir= 0; else if(ir>255) ir= 255;
319
ig= (int)floor(255.0f*g);
320
if(ig<0) ig= 0; else if(ig>255) ig= 255;
321
ib= (int)floor(255.0f*b);
322
if(ib<0) ib= 0; else if(ib>255) ib= 255;
324
return (ir+ (ig*256) + (ib*256*256));
322
ir = (int)floor(255.0f * r);
324
else if (ir > 255) ir = 255;
325
ig = (int)floor(255.0f * g);
327
else if (ig > 255) ig = 255;
328
ib = (int)floor(255.0f * b);
330
else if (ib > 255) ib = 255;
332
return (ir + (ig * 256) + (ib * 256 * 256));
327
335
void cpack_to_rgb(unsigned int col, float *r, float *g, float *b)
330
*r= (float)((col)&0xFF);
338
*r = (float)((col) & 0xFF);
333
*g= (float)(((col)>>8)&0xFF);
341
*g = (float)(((col) >> 8) & 0xFF);
336
*b= (float)(((col)>>16)&0xFF);
344
*b = (float)(((col) >> 16) & 0xFF);
340
348
void rgb_uchar_to_float(float col_r[3], const unsigned char col_ub[3])
342
col_r[0]= ((float)col_ub[0]) / 255.0f;
343
col_r[1]= ((float)col_ub[1]) / 255.0f;
344
col_r[2]= ((float)col_ub[2]) / 255.0f;
350
col_r[0] = ((float)col_ub[0]) / 255.0f;
351
col_r[1] = ((float)col_ub[1]) / 255.0f;
352
col_r[2] = ((float)col_ub[2]) / 255.0f;
347
355
void rgba_uchar_to_float(float col_r[4], const unsigned char col_ub[4])
349
col_r[0]= ((float)col_ub[0]) / 255.0f;
350
col_r[1]= ((float)col_ub[1]) / 255.0f;
351
col_r[2]= ((float)col_ub[2]) / 255.0f;
352
col_r[3]= ((float)col_ub[3]) / 255.0f;
357
col_r[0] = ((float)col_ub[0]) / 255.0f;
358
col_r[1] = ((float)col_ub[1]) / 255.0f;
359
col_r[2] = ((float)col_ub[2]) / 255.0f;
360
col_r[3] = ((float)col_ub[3]) / 255.0f;
355
363
void rgb_float_to_uchar(unsigned char col_r[3], const float col_f[3])
389
397
float srgb_to_linearrgb(float c)
391
399
if (c < 0.04045f)
392
return (c < 0.0f)? 0.0f: c * (1.0f/12.92f);
400
return (c < 0.0f) ? 0.0f : c * (1.0f / 12.92f);
394
return powf((c + 0.055f)*(1.0f/1.055f), 2.4f);
402
return powf((c + 0.055f) * (1.0f / 1.055f), 2.4f);
397
405
float linearrgb_to_srgb(float c)
399
407
if (c < 0.0031308f)
400
return (c < 0.0f)? 0.0f: c * 12.92f;
408
return (c < 0.0f) ? 0.0f : c * 12.92f;
402
return 1.055f * powf(c, 1.0f/2.4f) - 0.055f;
410
return 1.055f * powf(c, 1.0f / 2.4f) - 0.055f;
405
413
void minmax_rgb(short c[])
407
if(c[0]>255) c[0]=255;
408
else if(c[0]<0) c[0]=0;
409
if(c[1]>255) c[1]=255;
410
else if(c[1]<0) c[1]=0;
411
if(c[2]>255) c[2]=255;
412
else if(c[2]<0) c[2]=0;
415
if (c[0] > 255) c[0] = 255;
416
else if (c[0] < 0) c[0] = 0;
417
if (c[1] > 255) c[1] = 255;
418
else if (c[1] < 0) c[1] = 0;
419
if (c[2] > 255) c[2] = 255;
420
else if (c[2] < 0) c[2] = 0;
415
423
/*If the requested RGB shade contains a negative weight for
416
one of the primaries, it lies outside the color gamut
417
accessible from the given triple of primaries. Desaturate
418
it by adding white, equal quantities of R, G, and B, enough
419
to make RGB all positive. The function returns 1 if the
420
components were modified, zero otherwise.*/
424
* one of the primaries, it lies outside the color gamut
425
* accessible from the given triple of primaries. Desaturate
426
* it by adding white, equal quantities of R, G, and B, enough
427
* to make RGB all positive. The function returns 1 if the
428
* components were modified, zero otherwise.*/
421
429
int constrain_rgb(float *r, float *g, float *b)
432
440
/* Add just enough white to make r, g, b all positive. */
435
*r += w; *g += w; *b += w;
436
return 1; /* Color modified to fit RGB gamut */
446
return 1; /* Color modified to fit RGB gamut */
439
return 0; /* Color within RGB gamut */
449
return 0; /* Color within RGB gamut */
442
452
float rgb_to_grayscale(const float rgb[3])
444
return 0.3f*rgb[0] + 0.58f*rgb[1] + 0.12f*rgb[2];
454
return 0.3f * rgb[0] + 0.58f * rgb[1] + 0.12f * rgb[2];
447
457
unsigned char rgb_to_grayscale_byte(const unsigned char rgb[3])
449
return (76*(unsigned short)rgb[0] + 148*(unsigned short)rgb[1] + 31*(unsigned short)rgb[2]) / 255;
459
return (76 * (unsigned short) rgb[0] + 148 * (unsigned short) rgb[1] + 31 * (unsigned short) rgb[2]) / 255;
452
462
float rgb_to_luma(const float rgb[3])
454
return 0.299f*rgb[0] + 0.587f*rgb[1] + 0.114f*rgb[2];
464
return 0.299f * rgb[0] + 0.587f * rgb[1] + 0.114f * rgb[2];
457
467
unsigned char rgb_to_luma_byte(const unsigned char rgb[3])
459
return (76*(unsigned short)rgb[0] + 150*(unsigned short)rgb[1] + 29*(unsigned short)rgb[2]) / 255;
469
return (76 * (unsigned short) rgb[0] + 150 * (unsigned short) rgb[1] + 29 * (unsigned short) rgb[2]) / 255;
462
472
/* ********************************* lift/gamma/gain / ASC-CDL conversion ********************************* */
480
490
void rgb_float_set_hue_float_offset(float rgb[3], float hue_offset)
484
rgb_to_hsv(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
487
if(hsv[0] > 1.0f) hsv[0] -= 1.0f;
488
else if(hsv[0] < 0.0f) hsv[0] += 1.0f;
490
hsv_to_rgb(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2);
494
rgb_to_hsv(rgb[0], rgb[1], rgb[2], hsv, hsv + 1, hsv + 2);
496
hsv[0] += hue_offset;
497
if (hsv[0] > 1.0f) hsv[0] -= 1.0f;
498
else if (hsv[0] < 0.0f) hsv[0] += 1.0f;
500
hsv_to_rgb(hsv[0], hsv[1], hsv[2], rgb, rgb + 1, rgb + 2);
493
503
/* Applies an hue offset to a byte rgb color */
494
504
void rgb_byte_set_hue_float_offset(unsigned char rgb[3], float hue_offset)
496
506
float rgb_float[3];
498
508
rgb_uchar_to_float(rgb_float, rgb);
499
509
rgb_float_set_hue_float_offset(rgb_float, hue_offset);
500
510
rgb_float_to_uchar(rgb, rgb_float);
560
571
/* Fill in the lookup table to convert floats to bytes: */
561
572
for (i = 0; i < 0x10000; i++) {
562
float f = linearrgb_to_srgb(index_to_float(i))*255.0f;
573
float f = linearrgb_to_srgb(index_to_float(i)) * 255.0f;
563
574
if (f <= 0) BLI_color_to_srgb_table[i] = 0;
564
else if (f < 255) BLI_color_to_srgb_table[i] = (unsigned short)(f*0x100+0.5f);
575
else if (f < 255) BLI_color_to_srgb_table[i] = (unsigned short) (f * 0x100 + 0.5f);
565
576
else BLI_color_to_srgb_table[i] = 0xff00;
568
579
/* Fill in the lookup table to convert bytes to float: */
569
580
for (b = 0; b <= 255; b++) {
570
float f = srgb_to_linearrgb(((float)b)*(1.0f/255.0f));
581
float f = srgb_to_linearrgb(((float)b) * (1.0f / 255.0f));
571
582
BLI_color_from_srgb_table[b] = f;
573
584
/* replace entries so byte->float->byte does not change the data: */
574
BLI_color_to_srgb_table[i] = b*0x100;
585
BLI_color_to_srgb_table[i] = b * 0x100;