首页 > 代码库 > YCbCr to RGB and RGB toYCbCr

YCbCr to RGB and RGB toYCbCr

RGB => YCbCr:

Y = 0.299R + 0.587G + 0.114B
Cb = -0.1726R - 0.3388G + 0.5114B + 128
Cr = 0.5114R - 0.4282G – 0.0832B + 128

Then we get YCbCr => RGB:

R = Y + 1.371(Cr – 128)
G = Y – 0.6982(Cr – 128) – 0.3365(Cb – 128)
B = Y + 1.732(Cb – 128)

8bit conversion equation
Y = (77R + 150G + 29B)>>8
Cb = (-44R – 87G + 131B)>>8 + 128
Cr = (131R – 110G – 21B)>>8 + 128

R = Y + (351(Cr – 128))>>8
G = Y – (179(Cr – 128) + 86(Cb - 128))>>8
= Y – (179Cr + 86Cb)>>8 + ((179 + 86) * 128)>>8
= Y + 133 – (128 + 179Cr + 86Cb)>>8
B = Y + (443(Cb - 128))>>8

To use look-up table, we can change to following format:

RGB => YCbCr:
We adjust a little of the factors to avoid saturation operation to improve performance
Y = (77R + 150G + 29B)>>8
Cb = (128B – 43R – 85G)>>8 + 128 = (32768 + 128B – 43R – 85G)>>8
Cr = (128R – 107G – 21B)>>8 + 128 = (32768 + 128R – 107G – 21B)>>8

It needs 8 look-up tables:
T_77 [256], T_150 [256], T_29 [256], T_128 [256],
T_43 [256], T_85 [256], T_107 [256], T_21 [256]

YCbCr => RGB
R = SATURATION (Y + 1.371(Cr – 128))  
G = SATURATION (Y – 0.6982(Cr – 128) – 0.3365(Cb – 128))
B = SATURATION (Y + 1.732(Cb – 128))
Here, “SATURATION (A)” means:
If (A > 255)
A = 255
Else if (A < 0)
A = 0

It needs 4 tables:
Cr_1371 [256], Cr_06982 [256], Cb_03365 [256], Cb_1732 [256]

YCbCr to RGB and RGB toYCbCr