C++ YUV422怎么转化为RAW,怎么实现
时间: 2023-12-10 10:42:37 浏览: 92
将 YUV422 转化为 RAW 需要进行以下步骤:
1. 读取 YUV422 数据并将其存储到内存中。
2. 将 YUV422 数据转换为 RGB 数据。
3. 将 RGB 数据转换为 RAW 数据格式。
下面是一个 C++ 示例代码,可以实现将 YUV422 转换为 RAW:
```c++
#include <stdio.h>
#include <stdlib.h>
typedef struct tagBITMAPFILEHEADER
{
unsigned short bfType;
unsigned int bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits;
}BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER
{
unsigned int biSize;
int biWidth;
int biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
}BITMAPINFOHEADER;
typedef struct tagRGBQUAD
{
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved;
}RGBQUAD;
void YUV2RGB(int y, int u, int v, int &r, int &g, int &b)
{
r = y + 1.402 * (v - 128);
g = y - 0.34414 * (u - 128) - 0.71414 * (v - 128);
b = y + 1.772 * (u - 128);
}
void YUV422ToRGB(unsigned char *yuv, unsigned char *rgb, int width, int height)
{
int y1, y2, u, v;
int r1, g1, b1, r2, g2, b2;
int index, line, column;
for (line = 0; line < height; line++)
{
for (column = 0; column < width; column += 2)
{
index = line * width * 2 + column * 2;
y1 = yuv[index];
u = yuv[index + 1];
y2 = yuv[index + 2];
v = yuv[index + 3];
YUV2RGB(y1, u, v, r1, g1, b1);
YUV2RGB(y2, u, v, r2, g2, b2);
index = line * width * 3 + column * 3;
rgb[index] = r1;
rgb[index + 1] = g1;
rgb[index + 2] = b1;
rgb[index + 3] = r2;
rgb[index + 4] = g2;
rgb[index + 5] = b2;
}
}
}
void RGBToRAW(unsigned char *rgb, unsigned char *raw, int width, int height)
{
int index, line, column;
for (line = 0; line < height; line++)
{
for (column = 0; column < width; column++)
{
index = line * width * 3 + column * 3;
raw[index] = rgb[index + 2];
raw[index + 1] = rgb[index + 1];
raw[index + 2] = rgb[index];
}
}
}
int main()
{
FILE *fp = fopen("yuv422.yuv", "rb");
BITMAPFILEHEADER bmpFileHeader;
BITMAPINFOHEADER bmpInfoHeader;
RGBQUAD bmpQuad;
unsigned char *yuvData = NULL;
unsigned char *rgbData = NULL;
unsigned char *rawData = NULL;
int width = 640;
int height = 480;
int yuvSize = width * height * 2;
int rgbSize = width * height * 3;
int rawSize = width * height * 3;
yuvData = (unsigned char *)malloc(yuvSize);
rgbData = (unsigned char *)malloc(rgbSize);
rawData = (unsigned char *)malloc(rawSize);
fread(yuvData, 1, yuvSize, fp);
fclose(fp);
YUV422ToRGB(yuvData, rgbData, width, height);
RGBToRAW(rgbData, rawData, width, height);
fp = fopen("rawImage.raw", "wb");
fwrite(rawData, 1, rawSize, fp);
fclose(fp);
free(yuvData);
free(rgbData);
free(rawData);
return 0;
}
```
在上述代码中,YUV422 数据被读取,然后被转换为 RGB 数据,最后 RGB 数据被转换为 RAW 数据格式并保存到文件中。
阅读全文