能用C语言写一段将YUV图像转换为RGB图像的代码吗
时间: 2023-06-26 09:09:15 浏览: 61
能够用 C 语言写一段将 YUV 图像转换为 RGB 图像的代码。这里是一个示例代码:
```
void yuv2rgb(unsigned char *src, unsigned char *dst, int width, int height)
{
int frameSize = width * height;
int i, j;
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
int y = (0xff & ((int) src[i * width + j]));
int u = (0xff & ((int) src[frameSize + (i >> 1) * width + (j & ~1) + 0]));
int v = (0xff & ((int) src[frameSize + (i >> 1) * width + (j & ~1) + 1]));
y = y < 16 ? 16 : y;
int r = (int) (1.164f * (y - 16) + 1.596f * (v - 128));
int g = (int) (1.164f * (y - 16) - 0.813f * (v - 128) - 0.391f * (u - 128));
int b = (int) (1.164f * (y - 16) + 2.018f * (u - 128));
r = r < 0 ? 0 : (r > 255 ? 255 : r);
g = g < 0 ? 0 : (g > 255 ? 255 : g);
b = b < 0 ? 0 : (b > 255 ? 255 : b);
dst[i * width * 3 + j * 3] = (unsigned char) r;
dst[i * width * 3 + j * 3 + 1] = (unsigned char) g;
dst[i * width * 3 + j * 3 + 2] = (unsigned char) b;
}
}
}
```
在这个代码中,src 是 YUV 格式的图像数据,dst 是转换后的 RGB 格式的图像数据,width 和 height 分别是图像的宽和高。