码迷,mamicode.com
首页 > 其他好文 > 详细

YUV 和RGB之间转换函数

时间:2014-10-13 15:01:01      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:style   ar   for   sp   on   bs   har   rgb   函数   

void myRgb2YUV(int width, int height,  unsigned char *yuv, unsigned char *rgb)

{

for(int i=0;i<width;++i)

{

for(int j=0;j<height;++j)

{

int B=rgb[j*width*3+i*3];

int G=rgb[j*width*3+i*3+1];

int R=rgb[j*width*3+i*3+2];

int Y=int(16.5+(0.2578*R+0.504*G+0.098*B));

int U=int(128.5+(-0.148*R-0.291*G+0.439*B));

int V=int(128.5+(0.439*R-0.368*G-0.071*B));

yuv[j*width*2+i*2+0]=(unsigned char)Y;

if(i%2==1)

{

//yuv[j*width*2+i*2-1]=(unsigned char)U;

//yuv[j*width*2+i*2+1]=(unsigned char)V;

}else

{

yuv[j*width*2+i*2+1]=(unsigned char)U;

yuv[j*width*2+i*2+3]=(unsigned char)V;

}


}

}

}



void myYuv2Rgb(int width, int height,  const unsigned char *yuv, unsigned char *rgb)

{

for(int i=0;i<width;++i)

{

for(int j=0;j<height;++j)

{

int Y=yuv[j*width*2+i*2+0];

int U=yuv[j*width*2+i*2+1];

int V=yuv[j*width*2+i*2+3];

if(i%2==1)

{

U=yuv[j*width*2+i*2-1];

V=yuv[j*width*2+i*2+1];

}

int R=int(Y+1.4075 *(V-128));

int G=int(Y-0.3455*(U-128)-0.7169*(V-128));

int B=int(Y+1.779*(U-128));

R=R;

R=R>255?255:R;

R=R<0?0:R;

G=G>255?255:G;

G=G<0?0:G;

B=B>255?255:B;

B=B<0?0:B;

rgb[j*width*3+i*3]=B;

rgb[j*width*3+i*3+1]=G;

rgb[j*width*3+i*3+2]=R;

}

}

}


YUV 和RGB之间转换函数

标签:style   ar   for   sp   on   bs   har   rgb   函数   

原文地址:http://my.oschina.net/jingshishengxu/blog/330100

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!