如何将256色调色RGBA图像数据转换为NSImage
我是一名新手开发人员,我需要您的帮助来完成一些对您来说可能微不足道的事情。
我有一个这种像素格式的图像数据:256 色调色 RGBA。它来自 FFmpeg (PIX_FMT_PAL8)
,解释如下:
PIX_FMT_RGB32 以特定于字节序的方式处理。 RGBA 颜色组合在一起为:
(A << 24)| (R<<16)| (G<<8)|
这在小端 CPU 架构上存储为 BGRA,在大端 CPU 上存储为 ARGB。
当像素格式为调色板 RGB (PIX_FMT_PAL8) 时,调色板图像数据存储在 AVFrame.data[0] 中。
调色板在 AVFrame.data[1] 中传输,长度为 1024 字节(256 个 4 字节条目),其格式与上述 PIX_FMT_RGB32 中的格式相同(即,它也是特定于字节序的)。另请注意,存储在 AVFrame.data[1] 中的各个 RGB 调色板组件应在 0..255 范围内。
AVFrame 结构体包含 uint8_t *data[4]
和 int linesize[4]
,它们的描述如下:
uint8_t *data[4]
= 指向图像平面的指针- 仅给出四个组件。
- 最后一个组件是 alpha
int linesize[4]
= 每行字节数
我有 AVFrame 结构体,其中包含所有需要的数据,但我不知道如何处理它。 我需要从这个图像数据创建一个 NSImage。
我该怎么做?
I'm a newbie developer and I need your help with something that is probably trivial for you.
I have an image data in this pixel format: 256 colors palettized RGBA. It comes from FFmpeg (PIX_FMT_PAL8)
and it's explained this way:
PIX_FMT_RGB32 is handled in an endian-specific manner. An RGBA
color is put together as:(A << 24) | (R << 16) | (G << 8) | B
This is stored as BGRA on little-endian CPU architectures and ARGB on big-endian CPUs.
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.
AVFrame struct contains uint8_t *data[4]
and int linesize[4]
and they are described simply with:
uint8_t *data[4]
= pointer to the picture planes- four components are given, that's all.
- the last component is alpha
int linesize[4]
= number of bytes per line
I have the AVFrame struct with all the needed data but I don't know how to handle it.
I need to create a NSImage from this image data.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于调色板图像,像素包含一个字节值,它是调色板的索引。该调色板有 256 个条目。
像素从地址 AVFrame.data[0] 开始存储;调色板存储在地址 AVFrame.data[1] 处。
因此,要获取 (X, Y) 处像素的 4 字节像素值,您可以首先使用:
将索引获取到调色板中,然后
获取 RGBA 编码值。
In case of a palettized image, the pixels contain a one byte value, which is an index into the palette. The palette has 256 entries.
Pixels are stored starting at address AVFrame.data[0]; the palette is stored starting at address AVFrame.data[1].
So to get the 4 bytes pixel value for the pixel at (X, Y), you can use first:
to get the index into the palette, and then
to get the RGBA encoded value.