我正在尝试使用可移植到 IOS 和 Android 的库在 C++ 或 C 中创建和编写索引 png 图像,因此我研究了 png++、opencv 和 libpng。我尝试过使用 png++ 但无法正确生成索引图像。我无法找出 libpng 索引图像,也没有找到示例。 opencv 似乎不处理索引 png 图像。 png++ 似乎更容易使用,但文档省略了如何为索引图像设置自己的值的部分,它只是在该部分中添加了“...”(见下文)。任何帮助将不胜感激。
#include <png++/png.hpp>
//...
png::image< png::index_pixel > image;
png::palette pal(256);
for (size_t i = 0; i < pal.size(); ++i)
{
pal[i] = png::color(i, 255 - i, i);
}
image.set_palette(pal);
...
image.write("palette.png");
最佳答案
创建索引图像的方式与创建 RGB 图像相同,只是像素类型为 png::index_pixel
而不是 png::rgb_pixel
。
否则,它看起来就像文档中的示例:
for (png::uint_32 y = 0; y < image.get_height(); ++y)
{
for (png::uint_32 x = 0; x < image.get_width(); ++x)
{
image[y][x] = png::index_pixel(/* the index value */);
// non-checking equivalent of image.set_pixel(x, y, ...);
}
}
关于c++ - 如何用 C++ 或 C 创建和编写索引 png 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61355551/