//进行YUV->RGB转换的函数 unsigned char *clp = NULL; unsigned char *clp1; long int crv_tab[256]; long int cbu_tab[256]; long int cgu_tab[256]; long int cgv_tab[256]; long int tab_76309[256]; ///////////////// edit by dxl //////////////////// char filename[100]; //////////////// end /////////////////////////// void init_dither_tab () { long int crv, cbu, cgu, cgv; int i; crv = 104597; cbu = 132201; /* fra matrise i global.h */ cgu = 25675; cgv = 53279; for (i = 0; i < 256; i++) { crv_tab[i] = (i - 128) * crv; cbu_tab[i] = (i - 128) * cbu; cgu_tab[i] = (i - 128) * cgu; cgv_tab[i] = (i - 128) * cgv; tab_76309[i] = 76309 * (i - 16); } if (!(clp = (unsigned char *)malloc(sizeof(unsigned char)*1024))) { AfxMessageBox("安排解码码表内存失败!"); } clp1 = clp; clp += 384; for (i = -384; i < 640; i++) clp[i] = (i < 0) ? 0 : ((i > 255) ? 255 : i); } void ConvertYUVtoRGB (unsigned char *src0, unsigned char *src1, unsigned char *src2, unsigned char *dst_ori,int width,int height) { int y11, y21; int y12, y22; int y13, y23; int y14, y24; int u, v; int i, j; int c11, c21, c31, c41; int c12, c22, c32, c42; unsigned int DW; unsigned int *id1, *id2; unsigned char *py1, *py2, *pu, *pv; unsigned char *d1, *d2; d1 = dst_ori; d1 += width * height * 3 - width * 3; d2 = d1 - width * 3; py1 = src0; pu = src1; pv = src2; py2 = py1 + width; id1 = (unsigned int *) d1; id2 = (unsigned int *) d2; for (j = 0; j < height; j += 2) { /* line j + 0 */ for (i = 0; i < width; i += 4) { u = *pu++; v = *pv++; c11 = crv_tab[v]; c21 = cgu_tab[u]; c31 = cgv_tab[v]; c41 = cbu_tab[u]; u = *pu++; v = *pv++; c12 = crv_tab[v]; c22 = cgu_tab[u]; c32 = cgv_tab[v]; c42 = cbu_tab[u]; y11 = tab_76309[*py1++]; /* (255/219)*65536 */ y12 = tab_76309[*py1++]; y13 = tab_76309[*py1++]; /* (255/219)*65536 */ y14 = tab_76309[*py1++]; y21 = tab_76309[*py2++]; y22 = tab_76309[*py2++]; y23 = tab_76309[*py2++]; y24 = tab_76309[*py2++]; /* RGBR */ DW = ((clp[(y11 + c41) >> 16])) | ((clp[(y11 - c21 - c31) >> 16]) << 8) | ((clp[(y11 + c11) >> 16]) << 16) | ((clp[(y12 + c41) >> 16]) << 24); *id1++ = DW; /* GBRG */ DW = ((clp[(y12 - c21 - c31) >> 16])) | ((clp[(y12 + c11) >> 16]) << 8) | ((clp[(y13 + c42) >> 16]) << 16) | ((clp[(y13 - c22 - c32) >> 16]) << 24); *id1++ = DW; /* BRGB */ DW = ((clp[(y13 + c12) >> 16])) | ((clp[(y14 + c42) >> 16]) << 8) | ((clp[(y14 - c22 - c32) >> 16]) << 16) | ((clp[(y14 + c12) >> 16]) << 24); *id1++ = DW; /* RGBR */ DW = ((clp[(y21 + c41) >> 16])) | ((clp[(y21 - c21 - c31) >> 16]) << 8) | ((clp[(y21 + c11) >> 16]) << 16) | ((clp[(y22 + c41) >> 16]) << 24); *id2++ = DW; /* GBRG */ DW = ((clp[(y22 - c21 - c31) >> 16])) | ((clp[(y22 + c11) >> 16]) << 8) | ((clp[(y23 + c42) >> 16]) << 16) | ((clp[(y23 - c22 - c32) >> 16]) << 24); *id2++ = DW; /* BRGB */ DW = ((clp[(y23 + c12) >> 16])) | ((clp[(y24 + c42) >> 16]) << 8) | ((clp[(y24 - c22 - c32) >> 16]) << 16) | ((clp[(y24 + c12) >> 16]) << 24); *id2++ = DW; } id1 -= (9 * width) >> 2; id2 -= (9 * width) >> 2; py1 += width; py2 += width; } } int convert_yuv_to_rgb_pixel(int y, int u, int v) { unsigned int pixel32 = 0; unsigned char *pixel = (unsigned char *)&pixel32; int r, g, b; r = y + (1.370705 * (v-128)); g = y - (0.698001 * (v-128)) - (0.337633 * (u-128)); b = y + (1.732446 * (u-128)); if(r > 255) r = 255; if(g > 255) g = 255; if(b > 255) b = 255; if(r < 0) r = 0; if(g < 0) g = 0; if(b < 0) b = 0; pixel[0] = r ; pixel[1] = g ; pixel[2] = b ; return pixel32; } //422torgb int convert_yuv_to_rgb_buffer(unsigned char *yuv, unsigned char *rgb, unsigned int width, unsigned int height) { unsigned int in, out = 0; unsigned int pixel_16; unsigned char pixel_24[3]; unsigned int pixel32; int y0, u, y1, v; for(in = 0; in < width * height * 2; in += 4) { pixel_16 = yuv[in + 3] << 24 | yuv[in + 2] << 16 | yuv[in + 1] << 8 | yuv[in + 0];//转化为32位 y0 = (pixel_16 & 0x000000ff); u = (pixel_16 & 0x0000ff00) >> 8; y1 = (pixel_16 & 0x00ff0000) >> 16; v = (pixel_16 & 0xff000000) >> 24; pixel32 = convert_yuv_to_rgb_pixel(y0, u, v); pixel_24[0] = (pixel32 & 0x000000ff); pixel_24[1] = (pixel32 & 0x0000ff00) >> 8; pixel_24[2] = (pixel32 & 0x00ff0000) >> 16; rgb[out++] = pixel_24[0]; rgb[out++] = pixel_24[1]; rgb[out++] = pixel_24[2]; pixel32 = convert_yuv_to_rgb_pixel(y1, u, v); pixel_24[0] = (pixel32 & 0x000000ff); pixel_24[1] = (pixel32 & 0x0000ff00) >> 8; pixel_24[2] = (pixel32 & 0x00ff0000) >> 16; rgb[out++] = pixel_24[0]; rgb[out++] = pixel_24[1]; rgb[out++] = pixel_24[2]; } return 0; } |
|
来自: 昵称12110172 > 《DSP学习》