yuv420 转换成RGB565函数

做ADNROID 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;
    }
   } /* ..for col */
   if ((line & 1) == 0) { // even line: rewind
    pu -= linewidth;
    pv -= linewidth;
   }
} /* ..for line */
}



再给个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);
}

你可能感兴趣的:(工作)