gpt4 book ai didi

c - 如何从 AV_PIX_FMT_BGRA 转换为 PIX_FMT_PAL8?

转载 作者:太空宇宙 更新时间:2023-11-04 00:05:55 25 4
gpt4 key购买 nike

我很难将图像从 AV_PIX_FMT_BGRA 转换为 PIX_FMT_PAL8。不幸的是,sws_getCachedContext 不支持转换为 PIX_FMT_PAL8

我想做的是将我的图像转换为具有更高质量输出的 GIF 视频。 PIX_FMT_PAL8 似乎可以提供我正在寻找的更高质量的输出。

根据 this documentation我需要对像素数据进行调色,但我不知道该怎么做。

When the pixel format is palettized RGB (PIX_FMT_PAL8), the palettized image data is stored in AVFrame.data[0]. The palette is transported in AVFrame.data[1], is 1024 bytes long (256 4-byte entries) and is formatted the same as in PIX_FMT_RGB32 described above (i.e., it is also endian-specific). Note also that the individual RGB palette components stored in AVFrame.data[1] should be in the range 0..255. This is important as many custom PAL8 video codecs that were designed to run on the IBM VGA graphics adapter use 6-bit palette components.

如有任何帮助或指导,我们将不胜感激。

最佳答案

下面的代码改编 self 自己的导出到 ARGB/RGB(A) 代码。我没有测试过,但主要思想应该足够清楚。

为了使颜色转换更具可读性,修改了下面的代码,可能有更快的方法来做到这一点。

uint32_t* pal8_to_bgra(AVPicture* pict, int width, int height)
{
size_t size = width * height * 4; /* times 4 because 4 bytes per pixel */
uint32_t colours[255];
uint32_t *buff = NULL;

buff = malloc(size);
if (buff == NULL) {
fprintf(stderr,
"Error allocating memory for subtitle bitmap.\n");
return NULL;
}

for (int i = 0; i < 256; ++i) {
/* Colour conversion. */
int idx = i * 4; /* again, 4 bytes per pixel */
uint8_t r = pict->data[1][idx],
g = pict->data[1][idx + 1],
b = pict->data[1][idx + 2],
a = pict->data[1][idx + 3];
colours[i] = (b << 24) | (g << 16) | (r << 8) | a;
}

for (int y = 0; y < rect->h; ++y) {
for (int x = 0; x < rect->w; ++x) {
/* 1 byte per pixel */
int coordinate = x + y * pict->linesize[0];
/* 32bpp color table */
int idx = pict->data[0][coordinate];
buff[x + (y * rect->w)] = colours[idx];
}
}

return buff;
}

关于c - 如何从 AV_PIX_FMT_BGRA 转换为 PIX_FMT_PAL8?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24297407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com