(转)YUV420、YUV422、RGB24转换

//平面YUV422转平面RGB24
static void YUV422p_to_RGB24(unsigned char *yuv422[3], unsigned char *rgb24, int width, int height)
{
 int R,G,B,Y,U,V;
 int x,y;
 int nWidth = width>>1; //色度信号宽度
 for (y=0;y<height;y++)
 {
  for (x=0;x<width;x++)
  {
   Y = *(yuv422[0] + y*width + x);
   U = *(yuv422[1] + y*nWidth + (x>>1));
   V = *(yuv422[2] + y*nWidth + (x>>1));
   R = Y + 1.402*(V-128);
   G = Y - 0.34414*(U-128) - 0.71414*(V-128);
   B = Y + 1.772*(U-128);
   
   //防止越界
   if (R>255)R=255;
   if (R<0)R=0;
   if (G>255)G=255;
   if (G<0)G=0;
   if (B>255)B=255;
   if (B<0)B=0;
   
   *(rgb24 + ((height-y-1)*width + x)*3) = B;
   *(rgb24 + ((height-y-1)*width + x)*3 + 1) = G;
   *(rgb24 + ((height-y-1)*width + x)*3 + 2) = R;  
  }
 }
}

//平面YUV420转平面YUV422
static void YUV420p_to_YUV422p(unsigned char *yuv420[3], unsigned char *yuv422, int width, int height)  
{
 int x, y;
 //亮度信号Y复制
 int Ylen = width*height;
 memcpy(yuv422, yuv420[0], Ylen);
 //色度信号U复制
 unsigned char *pU422 = yuv422 + Ylen; //指向U的位置
 int Uwidth = width>>1; //422色度信号U宽度
 int Uheight = height>>1; //422色度信号U高度 
 for (y = 0; y < Uheight; y++) 
 {         
  memcpy(pU422 + y*width,          yuv420[1] + y*Uwidth, Uwidth);
  memcpy(pU422 + y*width + Uwidth, yuv420[1] + y*Uwidth, Uwidth);
 }
 //色度信号V复制
 unsigned char *pV422 = yuv422 + Ylen + (Ylen>>1); //指向V的位置
 int Vwidth = Uwidth; //422色度信号V宽度
 int Vheight = Uheight; //422色度信号U宽度
 for (y = 0; y < Vheight; y++) 
 {  
  memcpy(pV422 + y*width,          yuv420[2] + y*Vwidth, Vwidth);
  memcpy(pV422 + y*width + Vwidth, yuv420[2] + y*Vwidth, Vwidth);
 } 
}

//平面YUV420转RGB24
static void YUV420p_to_RGB24(unsigned char *yuv420[3], unsigned char *rgb24, int width, int height) 
{
//  int begin = GetTickCount();
 int R,G,B,Y,U,V;
 int x,y;
 int nWidth = width>>1; //色度信号宽度
 for (y=0;y<height;y++)
 {
  for (x=0;x<width;x++)
  {
   Y = *(yuv420[0] + y*width + x);
   U = *(yuv420[1] + ((y>>1)*nWidth) + (x>>1));
   V = *(yuv420[2] + ((y>>1)*nWidth) + (x>>1));
   R = Y + 1.402*(V-128);
   G = Y - 0.34414*(U-128) - 0.71414*(V-128);
   B = Y + 1.772*(U-128);

   //防止越界
   if (R>255)R=255;
   if (R<0)R=0;
   if (G>255)G=255;
   if (G<0)G=0;
   if (B>255)B=255;
   if (B<0)B=0;
   
   *(rgb24 + ((height-y-1)*width + x)*3) = B;
   *(rgb24 + ((height-y-1)*width + x)*3 + 1) = G;
   *(rgb24 + ((height-y-1)*width + x)*3 + 2) = R;
//    *(rgb24 + (y*width + x)*3) = B;
//    *(rgb24 + (y*width + x)*3 + 1) = G;
//    *(rgb24 + (y*width + x)*3 + 2) = R;   
  }
 }
}

来自:http://blog.chinaunix.net/uid-22670933-id-3022818.html

posted @ 2015-04-20 14:28  lihaiping  阅读(1133)  评论(0编辑  收藏  举报