YUV420P和YUV422转RGB565

int convertyuv422torgb565(unsigned char *inbuf,unsigned char *outbuf,int width,int height)
{
  int rows,cols,rowwidth;
  int y,u,v,r,g,b,rdif,invgdif,bdif;
  int size;
  unsigned char *YUVdata,*RGBdata;
  int YPOS,UPOS,VPOS;

  YUVdata = inbuf;
  RGBdata = outbuf;

  rowwidth = width>>1;
  size=width*height*2;
  YPOS=0;
  UPOS=YPOS + size/2;
  VPOS=UPOS + size/4;

  for(rows=0;rows<height;rows++)
  {
    for(cols=0;cols<width;cols++) 
    {
 u = YUVdata[UPOS] - 128;
 v = YUVdata[VPOS] - 128;

 rdif = v + ((v * 103) >> 8);
 invgdif = ((u * 88) >> 8) +((v * 183) >> 8);
 bdif = u +( (u*198) >> 8);

 r = YUVdata[YPOS] + rdif;
 g = YUVdata[YPOS] - invgdif;
 b = YUVdata[YPOS] + bdif;
 r=r>255?255:(r<0?0:r);
 g=g>255?255:(g<0?0:g);
 b=b>255?255:(b<0?0:b);
    
 *(RGBdata++) =( ((g & 0x1C) << 3) | ( b >> 3) );
 *(RGBdata++) =( (r & 0xF8) | ( g >> 5) );

 YPOS++;      
 
 if(cols & 0x01)
 {
    UPOS++;
    VPOS++;      
 } 
    }
    if((rows & 0x01)== 0)
    {
 UPOS -= rowwidth;
 VPOS -= rowwidth;
    }
  }
  return 1;
}

 

************************************************************************************************

很久没写..刚巧做CAMERA.发现很多朋友都在找YUV RGB 转换的函数..在这里给大家共享一个我自己修改了的.可以实际工作的代码.YUV420P ---- >> RGB565

 

static void ccvt_420p_rgb565(int width, int height, const unsigned char *src, __u16 *dst)
{
int line, col, linewidth;
int y, u, v, yy, vr, ug, vg, ub;
int r, g, b;
const unsigned char *py, *pu, *pv;

linewidth = width >> 1;
py = src;
pu = py + (width * height);
pv = pu + (width * height) / 4;

y = *py++;
yy = y << 8;
u = *pu - 128;
ug =   88 * u;
ub = 454 * u;
v = *pv - 128;
vg = 183 * v;
vr = 359 * v;

for (line = 0; line < height; line++) {
   for (col = 0; col < width; col++) {
    r = (yy +      vr) >> 8;
    g = (yy - ug - vg) >> 8;
    b = (yy + ub     ) >> 8;

    if (r < 0)   r = 0;
    if (r > 255) r = 255;
    if (g < 0)   g = 0;
    if (g > 255) g = 255;
    if (b < 0)   b = 0;
    if (b > 255) b = 255;
   *dst++ = (((__u16)r>>3)<<11) | (((__u16)g>>2)<<5) | (((__u16)b>>3)<<0);
  
    y = *py++;
    yy = y << 8;
    if (col & 1) {
     pu++;
     pv++;

     u = *pu - 128;
     ug =   88 * u;
     ub = 454 * u;
     v = *pv - 128;
     vg = 183 * v;
     vr = 359 * v;
    }
   }
   if ((line & 1) == 0) { // even line: rewind
    pu -= linewidth;
    pv -= linewidth;
   }
}
}

参数大家估计都看的懂吧...

再给个YUV422 ---->>RGB565..

#define YCbCrtoR(Y,Cb,Cr) (1000*Y + 1371*(Cr-128))/1000
#define YCbCrtoG(Y,Cb,Cr) (1000*Y - 336*(Cb-128) - 698*(Cr-128))/1000
#define YCbCrtoB(Y,Cb,Cr) (1000*Y + 1732*(Cb-128))/1000
#define min(x1, x2)   (((x1)<(x2))?(x1):(x2))

__u32 Conv_YCbCr_Rgb(__u8 y0, __u8 y1, __u8 cb0, __u8 cr0)
{
// bit order is
// YCbCr = [Cr0 Y1 Cb0 Y0], RGB=[R1,G1,B1,R0,G0,B0].

int r0, g0, b0, r1, g1, b1;
__u16 rgb0, rgb1;
__u32 rgb;

#if 1 // 4 frames/s @192MHz, 12MHz ; 6 frames/s @450MHz, 12MHz
r0 = YCbCrtoR(y0, cb0, cr0);
g0 = YCbCrtoG(y0, cb0, cr0);
b0 = YCbCrtoB(y0, cb0, cr0);
r1 = YCbCrtoR(y1, cb0, cr0);
g1 = YCbCrtoG(y1, cb0, cr0);
b1 = YCbCrtoB(y1, cb0, cr0);
#endif

if (r0>255 ) r0 = 255;
if (r0<0) r0 = 0;
if (g0>255 ) g0 = 255;
if (g0<0) g0 = 0;
if (b0>255 ) b0 = 255;
if (b0<0) b0 = 0;

if (r1>255 ) r1 = 255;
if (r1<0) r1 = 0;
if (g1>255 ) g1 = 255;
if (g1<0) g1 = 0;
if (b1>255 ) b1 = 255;
if (b1<0) b1 = 0;

// 5:6:5 16bit format
rgb0 = (((__u16)r0>>3)<<11) | (((__u16)g0>>2)<<5) | (((__u16)b0>>3)<<0); //RGB565.
rgb1 = (((__u16)r1>>3)<<11) | (((__u16)g1>>2)<<5) | (((__u16)b1>>3)<<0); //RGB565.

rgb = (rgb1<<16) | rgb0;

return(rgb);
}

 

你可能感兴趣的:(RGB565,yuv422)