YUV到RGB颜色空间转换源码

void NV212RGB(unsigned char *inputImageSource,unsigned char *inputRGBimage, int width,int height, int channel,int widthEdge)
{
int i = 0, j = 0;
int widthStep = width * channel;
int edgeWidthStep = widthEdge * channel;
int grayImageSize = widthEdge * height;
unsigned char *pCurSrcY = NULL;
unsigned char *pCurSrcUV = NULL;
unsigned char *pCurSrcRowY = NULL;
unsigned char *pCurSrcRowUV = NULL;
unsigned char *pCurDst = NULL;
unsigned char *pRowDst = NULL;


unsigned char R = 0;
unsigned char G = 0;
unsigned char B = 0;
unsigned char Y = 0;
unsigned char U = 0;
unsigned char V = 0;
unsigned char cb = 0, cr = 0;


pCurSrcY = inputImageSource;
pCurSrcRowY = pCurSrcY;
pCurSrcUV = inputImageSource + grayImageSize;
pCurSrcRowUV = pCurSrcUV;


pCurDst = inputRGBimage;
pRowDst = pCurDst;
for(i = 0; i < height;pRowDst += widthStep, pCurSrcRowY += widthEdge, pCurSrcRowUV += (i%2) * widthEdge, ++i)
{
pCurDst = pRowDst;
pCurSrcY = pCurSrcRowY;
pCurSrcUV = pCurSrcRowUV;
for( j = 0; j < width;  pCurDst += channel,++pCurSrcY,pCurSrcUV+=(j%2) * 2, ++j)
{
Y = pCurSrcY[0];
cb = pCurSrcUV[0];
cr = pCurSrcUV[1];

int temp = (int)(1.164 * Y + 1.596 * cr - 222.912 + 0.5);
if(temp < 0)
temp = 0;
if(temp > 255)
temp = 255;
R = (unsigned char)temp;


temp = (int)(1.164 * Y - 0.391 * cb - 0.813 * cr + 135.488 + 0.5);
if(temp < 0)
temp = 0;
if(temp > 255)
temp = 255;
G = (unsigned char)temp;


temp = (int)(1.164 * Y + 2.018 * cb - 276.928 + 0.5);
if(temp < 0)
temp = 0;
if(temp > 255)
temp = 255;
B = (unsigned char)temp;


pCurDst[0] = B;
pCurDst[1] = G;
pCurDst[2] = R;
}
}
}


void RGB2NV21(unsigned char *YUV420Image,unsigned char *RGBimage, int width,int height, int channel)
{
int i = 0, j = 0;
int widthStep = width * channel;
int grayImageSize = width * height;
unsigned char *pCurDstY = NULL;
unsigned char *pCurDstUV = NULL;
unsigned char *pCurDstRowY = NULL;
unsigned char *pCurDstRowUV = NULL;


unsigned char *pCurSrcRGB = NULL;
unsigned char *pRowSrcRGB = NULL;


unsigned char R = 0;
unsigned char G = 0;
unsigned char B = 0;
unsigned char Y = 0;
unsigned char U = 0;
unsigned char V = 0;
unsigned char cb = 0, cr = 0;


pCurDstY = YUV420Image;
pCurDstRowY = pCurDstY;
pCurDstUV = YUV420Image + grayImageSize;
pCurDstRowUV = pCurDstUV;


pCurSrcRGB = RGBimage;
pRowSrcRGB = pCurSrcRGB;
for(i = 0; i < height;pRowSrcRGB += widthStep, pCurDstRowY += width, pCurDstRowUV += (i%2) * width, ++i)
{
pCurSrcRGB = pRowSrcRGB;
pCurDstY = pCurDstRowY;
pCurDstUV = pCurDstRowUV;
for( j = 0; j < width;  pCurSrcRGB += channel,++pCurDstY,pCurDstUV+=(j%2) * 2, ++j)
{
B = pCurSrcRGB[0];
G = pCurSrcRGB[1];
R = pCurSrcRGB[2];
//Y   = 0.257*R+0.564*G+0.098*B+16
//Cb = -0.148*R-0.291*G+0.439*B+128
//Cr  = 0.439*R-0.368*G-0.071*B+128

int temp = (int)(0.257 * R + 0.564 * G + 0.098 * B + 16);
pCurDstY[0] = (unsigned char)temp;


temp = (int)(-0.148 * R - 0.291 * G + 0.439 * B + 128);
pCurDstUV[0] = (unsigned char)temp;


temp = (int)(0.439 * R - 0.368 * G - 0.071 * B + 128);
pCurDstUV[1] = (unsigned char)temp;
}
}
#if 0
/**/
FILE *fp = fopen("YUVIMAGE.dat","w+");
unsigned int imageSize = width * height*3/2;
fwrite(YUV420Image,1,imageSize, fp);
fclose(fp);
#endif
}


void RGB2HSV(unsigned char *YUV420Image,unsigned char *RGBimage, int width,int height, int channel)
{
int i = 0, j = 0;
int widthStep = width * channel;
int grayImageSize = width * height;
unsigned char *pCurDstY = NULL;
unsigned char *pCurDstUV = NULL;
unsigned char *pCurDstRowY = NULL;
unsigned char *pCurDstRowUV = NULL;


unsigned char *pCurSrcRGB = NULL;
unsigned char *pRowSrcRGB = NULL;


unsigned char R = 0;
unsigned char G = 0;
unsigned char B = 0;
int H = 0;
double S = 0.0;
double V = 0.0;


pCurDstY = YUV420Image;
pCurDstRowY = pCurDstY;
pCurDstUV = YUV420Image + grayImageSize;
pCurDstRowUV = pCurDstUV;


pCurSrcRGB = RGBimage;
pRowSrcRGB = pCurSrcRGB;
for(i = 0; i < height;pRowSrcRGB += widthStep, pCurDstRowY += width, pCurDstRowUV += (i%2) * width, ++i)
{
pCurSrcRGB = pRowSrcRGB;
pCurDstY = pCurDstRowY;
pCurDstUV = pCurDstRowUV;
for( j = 0; j < width;  pCurSrcRGB += channel,++pCurDstY,pCurDstUV+=(j%2) * 2, ++j)
{
B = pCurSrcRGB[0];
G = pCurSrcRGB[1];
R = pCurSrcRGB[2];


}
}
#if 0
/**/
FILE *fp = fopen("YUVIMAGE.dat","w+");
unsigned int imageSize = width * height*3/2;
fwrite(YUV420Image,1,imageSize, fp);
fclose(fp);
#endif
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值