YUYV是YUV的一种一个像素占用两个字节,存放顺序为[Y0 U0][Y1 V0][Y2 U1][Y3 V1].....
Y表示亮度,UV是色差信号奇数(在前)和偶数(在后)像素共用色差信号UV
下面是用查表法转换YUYV(YUV422)为RGB32位的代码
int y_table[256];//查表法 int v_r_table[256]; int v_g_table[256]; int u_g_table[256]; int u_b_table[256]; unsigned int limit_table[256*3]; __forceinline unsigned int LimitToUnsigned8Bits(int value) { if (value > 255) return 255; else if (value < 0) return 0; else return value; } void InitTable() { for(int i=0;i<=255;i++) { y_table[i]=(i-16)*1192; v_r_table[i]=(i-128)*1634; v_g_table[i]=(i-128)*833; u_g_table[i]=(i-128)*401; u_b_table[i]=(i-128)*2065; } for (int i=0;i<256*3;i++) { limit_table[i]=LimitToUnsigned8Bits(i-256); } } __forceinline unsigned int ConvertYUVPixelToRGB(unsigned char yComponent,unsigned char uComponent,unsigned char vComponent) { //转换公式 /* B= 1.164*(Y-16) + 2.018*(U-128); 为了提高速度,放大后使用整数计算,同时移位10位相当于放大缩小1024倍 G= 1.164*(Y-16) - 0.380*(U-128) - 0.813*(V-128); R= 1.164*(Y-16) + 1.159*(V-128); */ //原版代码 //int y = yComponent - 16; //int u = uComponent - 128; //int v = vComponent - 128; //int r = LimitToUnsigned8Bits((1192 * y + 1634 * v) >> 10); //int g = LimitToUnsigned8Bits((1192 * y- 833 * v - 401 * u) >> 10); //int b = LimitToUnsigned8Bits((1192 * y + 2065 * u) >> 10); //return (255 << 24) | (r << 16) | (g << 8) | b; int y1192=y_table[yComponent]; int r = limit_table[((y1192 + v_r_table[vComponent]) >> 10)+256];//可能小于0,先取正 int g = limit_table[((y1192 - v_g_table[vComponent] - u_g_table[uComponent]) >> 10)+256]; int b = limit_table[((y1192 + u_b_table[uComponent]) >> 10)+256]; return (255 << 24) | (r << 16) | (g << 8) | b; } void ConvertBitmapFromYUYV(int width, int height, unsigned char *bufferIn,unsigned int *bufferOut) { for (int pixel = 0; pixel < width * height; pixel += 2) { unsigned char y1 = *bufferIn++; unsigned char u = *bufferIn++; unsigned char y2 = *bufferIn++; unsigned char v = *bufferIn++; //unsigned int a=ConvertYUVPixelToRGB(y1, u, v); *bufferOut++ = ConvertYUVPixelToRGB(y1, u, v); *bufferOut++ = ConvertYUVPixelToRGB(y2, u, v); } }
YUYV转RGB
时间: 2024-10-11 20:38:43