-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorModelConversions.h
422 lines (391 loc) · 12 KB
/
ColorModelConversions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/**
* @file ColorModelConversions.h
*
* Declaration and implementation of class ColorModelConversions
*/
#ifndef __ColorModelConversions_h_
#define __ColorModelConversions_h_
#include <iostream>
#include "math.h"
//TODO: solve the include jobs
/**
* A class that defines static conversions between color models for single pixels.
*/
class ColorModelConversions
{
public:
/** Converts an YCbCr pixel into an RGB pixel.
* @param Y The Y channel of the source pixel.
* @param Cb The Cb channel of the source pixel.
* @param Cr The Cr channel of the source pixel.
* @param R The R channel of the target pixel.
* @param G The G channel of the target pixel.
* @param B The B channel of the target pixel.
*/
static void fromYCbCrToRGB(unsigned char Y,
unsigned char Cb,
unsigned char Cr,
unsigned char& R,
unsigned char& G,
unsigned char& B)
{
/* int r = (int)(Y + 1.4021 * (Cr - 128)),
g = (int)(Y - 0.3456 * (Cb - 128) - 0.71448 * (Cr - 128)),
b = (int)(Y + 1.7710 * (Cb - 128));*/
int b = Y + ((1436 * (Cr - 128)) >> 10),
g = Y - ((354 * (Cb - 128) + 732 * (Cr - 128)) >> 10),
r = Y + ((1814 * (Cb - 128)) >> 10);
/*
int C = Y - 16;
int D = Cb - 128;
int E = Cr - 128;
int g = 1.164f * C + 1.596f * E;
int r = 1.164f * C - 0.813f * E - 0.391f * D;
int b = 1.164f * C +2.018 * E;
*/
if(r < 0) r = 0; else if(r > 255) r = 255;
if(g < 0) g = 0; else if(g > 255) g = 255;
if(b < 0) b = 0; else if(b > 255) b = 255;
R = (unsigned char) r;
G = (unsigned char) g;
B = (unsigned char) b;
}
static void fromYCbCrToRGB(unsigned char* sourceImg,unsigned char* destImg, int width,int height)
{
int yuvLen = width * height * 2;// div by four because of the uY1vY2
int rgbIndex=0, yuvIndex=0;
//unsigned char* destImg = new unsigned char[rgbLen];
while(yuvIndex < yuvLen)
{
unsigned char Y1 = sourceImg[yuvIndex++];
unsigned char U = sourceImg[yuvIndex++];
unsigned char Y2 = sourceImg[yuvIndex++];
unsigned char V = sourceImg[yuvIndex++];
fromYCbCrToRGB(Y1,U,V,
destImg[rgbIndex++],//R
destImg[rgbIndex++],//G
destImg[rgbIndex++]);//B
fromYCbCrToRGB(Y2,U,V,
destImg[rgbIndex++],//R
destImg[rgbIndex++],//G
destImg[rgbIndex++]);//B
}
}
/** Converts an RGB pixel into an YCbCr pixel.
* @param R The R channel of the source pixel.
* @param G The G channel of the source pixel.
* @param B The B channel of the source pixel.
* @param Y The Y channel of the target pixel.
* @param Cb The Cb channel of the target pixel.
* @param Cr The Cr channel of the target pixel.
*/
static void fromRGBToYCbCr(unsigned char R,
unsigned char G,
unsigned char B,
unsigned char& Y,
unsigned char& Cb,
unsigned char& Cr)
{
int y = (int)( 0.2990 * R + 0.5870 * G + 0.1140 * B),
cr = 127 + (int)(-0.1687 * R - 0.3313 * G + 0.5000 * B),
cb = 127 + (int)( 0.5000 * R - 0.4187 * G - 0.0813 * B);
if(y < 0) y = 0; else if(y > 255) y = 255;
if(cb < 0) cb = 0; else if(cb > 255) cb = 255;
if(cr < 0) cr = 0; else if(cr > 255) cr = 255;
Y = (unsigned char) y;
Cb = (unsigned char) cb;
Cr = (unsigned char) cr;
}
static double max(double a, double b, double c)
{
if( a > b && a > c)
return a;
if( b > c && b > a)
return b;
if( c > a && c > b)
return c;
}
static double min(double a, double b, double c)
{
if( a < b && a < c)
return a;
if( b < c && b < a)
return b;
if( c < a && c < b)
return c;
}
static void fromRGBtoHSL(unsigned char R,unsigned char G,unsigned char B, int &H,unsigned char &S,unsigned char &L)
{
float r = (float)R / 255, g = (float)G / 255, b = (float)B / 255;
float Max = max(r, g, b);
float Min = min(r, g, b);
float h, s, l = (float)(r+g+b) / 3;
if(Max == Min)
h = s = 0; // achromatic
else
{
float d = (float)Max - Min;
s = l >= 0.5 ? d / (2 - Max - Min) : d / (Max + Min);
if (Max == r)
{h = (g - b) / d + (g < b ? 6 : 0);}
else if(Max == g)
{ h = (b - r) / d + 2;}
else if(Max == b)
{h = (r - g) / d + 4;}
h *= 60;
}
H = (int)h;
S = s*100;
L = l*100;
}
static void fromYUVtoHSL(unsigned char Y,unsigned char U,unsigned char V,int &H,unsigned char &S,unsigned char &L)
{
unsigned char R,G,B;
fromYCbCrToRGB(Y,U,V,R,G,B);
fromRGBtoHSL(R,G,B,H,S,L);
}
/** Converts an YCbCr pixel into an HSI pixel.
* @param Y The Y channel of the source pixel.
* @param Cb The Cb channel of the source pixel.
* @param Cr The Cr channel of the source pixel.
* @param H The H channel of the target pixel.
* @param S The S channel of the target pixel.
* @param I The I channel of the target pixel.
*/
static void fromYCbCrToHSI(unsigned char Y,
unsigned char Cb,
unsigned char Cr,
unsigned char& H,
unsigned char& S,
unsigned char& I)
{
const double sqrt3 = 1.732050808;
unsigned char R,
G,
B;
fromYCbCrToRGB(Y, Cb, Cr, R, G, B);
I = R;
if(G > I) I = G;
if(B > I) I = B;
if(I)
{
S = R;
if(G < S) S = G;
if(B < S) S = B;
S = (unsigned char) (255 - 255 * S / I);
if(S)
{
int h = int(atan2(sqrt3 * (G - B), 2 * R - G - B) / M_PI_2 * 256);
if(h > 255) h -= 256;
else if(h < 0) h += 256;
H = (unsigned char) h;
}
else
H = 0;
}
else
S = H = 0;
}
/** Converts an HSI pixel into an YCbCr pixel.
* @param H The H channel of the source pixel.
* @param S The S channel of the source pixel.
* @param I The I channel of the source pixel.
* @param Y The Y channel of the target pixel.
* @param Cb The Cb channel of the target pixel.
* @param Cr The Cr channel of the target pixel.
*/
static void fromHSIToYCbCr(unsigned char H,
unsigned char S,
unsigned char I,
unsigned char& Y,
unsigned char& Cb,
unsigned char& Cr)
{
double h = 1.0 - H / 255.0,
s = S / 255.0,
r,
g,
b;
if(h < 1.0 / 3.0)
{
g = (1 - s) / 3;
b = (1 + s * cos(M_PI_2 * h) / cos(M_PI_2 * (1.0 / 6.0 - h))) / 3.0;
r = 1 - (g + b);
}
else if(h < 2.0 / 3.0)
{
h -= 1.0 / 3.0;
b = (1 - s) / 3;
r = (1 + s * cos(M_PI_2 * h) / cos(M_PI_2 * (1.0 / 6.0 - h))) / 3.0;
g = 1 - (b + r);
}
else
{
h -= 2.0 / 3.0;
r = (1 - s) / 3;
g = (1 + s * cos(M_PI_2 * h) / cos(M_PI_2 * (1.0 / 6.0 - h))) / 3.0;
b = 1 - (r + g);
}
r *= I * 3;
g *= I * 3;
b *= I * 3;
if(r > 255)
r = 255;
if(g > 255)
g = 255;
if(b > 255)
b = 255;
fromRGBToYCbCr((unsigned char) r,
(unsigned char) g,
(unsigned char) b,
Y, Cb, Cr);
}
/** Converts an YCbCr pixel into a TSL pixel.
* @param Y The Y channel of the source pixel.
* @param Cb The Cb channel of the source pixel.
* @param Cr The Cr channel of the source pixel.
* @param T The T channel of the target pixel.
* @param S The S channel of the target pixel.
* @param L The L channel of the target pixel.
*/
static void fromYCbCrToTSL(unsigned char Y,
unsigned char Cb,
unsigned char Cr,
unsigned char& T,
unsigned char& S,
unsigned char& L)
{
const double pi2_div = 0.15915494309189533576888376337251; /* 1.0 / (PI * 2.0) */
double cb = Cb - 128.0,
cr = Cr - 128.0,
tmp = 1.0 / (4.3403 * Y + 2.0 * cr + cb),
tmp_r = (-0.6697 * cr + 1.6959 * cb) * tmp,
tmp_g = (-1.168 * cr - 1.3626 * cb) * tmp,
tmp_b = ( 1.8613 * cr - 0.331 * cb) * tmp;
double t_out;
if(tmp_g > 0.0)
t_out = (atan2(tmp_r, tmp_g) * pi2_div + 0.25) * 255.0;
else if(tmp_g < 0.0)
t_out = (atan2(-tmp_r, -tmp_g) * pi2_div + 0.75) * 255.0;
else
t_out = 0.0;
double s_out = sqrt(1.8 * (tmp_r * tmp_r + tmp_g * tmp_g + tmp_b * tmp_b)) * 255.0;
if(t_out < 0.0)
t_out = 0.0;
else if(t_out > 255.0)
t_out = 255.0;
if(s_out < 0.0)
s_out = 0.0;
else if(s_out > 255.0)
s_out = 255.0;
T = (unsigned char) t_out;
S = (unsigned char) s_out;
L = Y;
}
/** Converts a TSL pixel into an YCbCr pixel.
* @param T The T channel of the source pixel.
* @param S The S channel of the source pixel.
* @param L The L channel of the source pixel.
* @param Y The Y channel of the target pixel.
* @param Cb The Cr channel of the target pixel.
* @param Cr The Cr channel of the target pixel.
*/
static void fromTSLToYCbCr(unsigned char T,
unsigned char S,
unsigned char L,
unsigned char& Y,
unsigned char& Cb,
unsigned char& Cr)
{
double rad = S * 0.555 / 255.0,
phi = (T * 2 / 255.0 + 0.25) * M_PI,
cb = 128 + rad * cos(phi) * 255.0,
cr = 128 - rad * sin(phi) * 255.0;
Y = L;
if(cb < 0)
cb = 0;
else if(cb > 255)
cb = 255;
if(cr < 0)
cr = 0;
else if(cr > 255)
cr = 255;
Cb = (unsigned char) cb;
Cr = (unsigned char) cr;
}
/** Converts a TSL pixel into an YCbCr pixel.
* @param t The T channel of the source pixel.
* @param s The S channel of the source pixel.
* @param l The L channel of the source pixel.
* @param r The R channel of the target pixel.
* @param g The G channel of the target pixel.
* @param b The B channel of the target pixel.
*/
static void fromTSLToRGB(unsigned char t,
unsigned char s,
unsigned char l,
unsigned char& r,
unsigned char& g,
unsigned char& b)
{
double y1, u1, v1, r1, g1, b1, rad, phi;
/* Convert TSL to YUV */
rad = (((double) s) * 0.555) / 255.0;
phi = (((double) t) * 2.0 * 3.14159) / 255.0;
phi += 0.25 * 3.14159;
y1 = (double) l;
u1 = (rad * cos(phi)) * 255.0;
v1 = -(rad * sin(phi)) * 255.0;
/* Crop UV */
if (u1 < -128.0)
{
u1 = -128.0;
}
else if (u1 > 127.0)
{
u1 = 127.0;
}
if (v1 < -128.0)
{
v1 = -128.0;
}
else if (v1 > 127.0)
{
v1 = 127.0;
}
/* Convert YUV to RGB */
r1 = y1 + 1.371 * v1;
g1 = y1 - 0.336 * u1 - 0.698 * v1;
b1 = y1 + 1.732 * u1;
/* Crop RGB */
if (r1 < 0.0)
{
r1 = 0.0;
}
else if (r1 > 255.0)
{
r1 = 255.0;
}
if (g1 < 0.0)
{
g1 = 0.0;
}
else if (g1 > 255.0)
{
g1 = 255.0;
}
if (b1 < 0.0)
{
b1 = 0.0;
}
else if (b1 > 255.0)
{
b1 = 255.0;
}
/* Return values */
r = (unsigned char) r1;
g = (unsigned char) g1;
b = (unsigned char) b1;
}
};
#endif //__ColorModelConversions_h_