YUV转RGB,无除法,无浮点运算

项目需要,将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;
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值