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;
}
}
}