如下代码实现YCbCr(6:4:4:2)到RGB(5:6:6)的转换:
for(i = 0 ; i < 0x10000; i++)
{
count++;
//得到y,cb,cr颜色分量
y = (i&0xFC00)>>10;
u = (i&0x03C0)>>6;
v = (i&0x003C)>>2;
//每个分量为8bits
Y = (double) (y<<2);
Cb = (double) (u<<4);
Cr = (double) (v<<4);
//计算r,g,b颜色分量,每个分量为8bits
r = (int) (Y + 1.40200 * (Cr - 0x80));
g = (int) (Y - 0.34414 * (Cb - 0x80) - 0.71414 * (Cr - 0x80));
b = (int) (Y + 1.77200 * (Cb - 0x80));
r = max(0, min(255, r));
g = max(0, min(255, g));
b = max(0, min(255, b));
//r = (unsigned char)(1.164383*(y- 16) + 0 + 1.596027*(v - 128));
//g = (unsigned char)(1.164383*(y- 16) - 0.391762*(u - 128) - 0.812969*(v - 128));
//b = (unsigned char)(1.164383*(y- 16) + 2.017230*(u - 128) + 0 );
//printf("\033[32m y : [0x%04x] %x u : %x v : %x \t RGB:[0x%02x%02x%02x] \n\033[0m", i, y, u ,v ,r, g, b);
//printf("%x %x %x\n", (((unsigned int) r)&0x00F8)<<8, (((unsigned int) g)&0x00FC)<<3, (((unsigned int) b)&0x00F8)>>3);
//计算5:6:5 RGB
rgb = ((((unsigned int) r)&0x00F8)<<8) + ((((unsigned int) g)&0x00FC)<<3) + ((((unsigned int) b)&0x00F8)>>3);
//printf("565RGB : %x", rgb);
printf("0x%04x, ", rgb);
if(count%8 == 0)
printf("\n");
}