项目需要,将YUV422图像转换成RGB图像,考虑到效率问题,不能使用除法,不能使用浮点运算。
参考http://blog.csdn.net/housisong/article/details/1859084
不使用浮点数:在应用时,希望避免低速的浮点运算,所以需要整数算法,我们可以将先乘上一定的倍数来实现整数运算算法.
不使用除法:通过移位运算代替除法,避免除法运算带来的效率问题。
原公式:
B= 1.164383 * (Y - 16) + 2.017232*(U - 128);
G= 1.164383 * (Y - 16) - 0.391762*(U - 128) - 0.812968*(V - 128);
R= 1.164383 * (Y - 16) + 1.596027*(V - 128);
实现公式
B= ((1.164383<<16) * (Y - 16) + (2.017232<<16) *(U - 128))>>16;
G= ((1.164383<<16) * (Y - 16) - (0.391762<<16) *(U - 128) - (0.812968<<16) *(V - 128))>>16;
R= ((1.164383<<16) * (Y - 16) + (1.596027<<16) *(V - 128))>>16;
注:这样的算法有少量的精度损失。
实现代码:
void YUV422P_To_RGB24_init() {
int i;
for (i = 0; i < 256 * 3; ++i)
_color_table[i] = border_color(i - 256);
for (i = 0; i < 256; ++i) {
Ym_tableEx[i] = (csY_coeff_16 * (i - 16)) >> 16;
Um_blue_tableEx[i] = (csU_blue_16 * (i - 128)) >> 16;
Um_green_tableEx[i] = (csU_green_16 * (i - 128)) >> 16;
Vm_green_tableEx[i] = (csV_green_16 * (i - 128)) >> 16;
Vm_red_tableEx[i] = (csV_red_16 * (i - 128)) >> 16;
}
}
void YUVToRGB24_Table(BYTE *p, const BYTE Y0, const BYTE Y1,
const BYTE U, const BYTE V) {
int Ye0 = Ym_tableEx[Y0];
int Ye1 = Ym_tableEx[Y1];
int Ue_blue = Um_blue_tableEx[U];
int Ue_green = Um_green_tableEx[U];
int Ve_green = Vm_green_tableEx[V];
int Ve_red = Vm_red_tableEx[V];
int UeVe_green = Ue_green + Ve_green;
*p = color_table[(Ye0 + Ve_red)];
*(p + 1) = color_table[(Ye0 + UeVe_green)];
*(p + 2) = color_table[(Ye0 + Ue_blue)];
*(p + 3) = color_table[(Ye1 + Ve_red)];
*(p + 4) = color_table[(Ye1 + UeVe_green)];
*(p + 5) = color_table[(Ye1 + Ue_blue)];
printf("%d, %d, %d, %d, %d, %d\n", *p,*(p+1), *(p+2), *(p+3), *(p+4), *(p+5));
}
long border_color(long color) {
if (color > 255)
return 255;
else if (color < 0)
return 0;
else
return color;
}
void YUVToRGB24(const BYTE * pYUV, BYTE *pRGB, int width, int height)
{
YUV422P_To_RGB24_init();
int i, j;
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j+=2)
{
printf("%d, %d\n", i, j);
YUVToRGB24_Table(pRGB, pYUV[1], pYUV[3], pYUV[0], pYUV[2]);
pYUV += 4;
pRGB += 3;
}
}
}