c++ - 如何用 C++ 或 C 创建和编写索引 png 图像

标签 c++ c image png

我正在尝试使用可移植到 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/

相关文章:

c++ - cin.get() 和 cin.getline() 的区别

c - 从数组循环中获取最小和最大标签

javascript - 如何完全阻止通过 HTTP 提供的资源的图像缓存

image - 裁剪图像而不是拉伸(stretch)图像

java - 在 mousePressed 处更改 2 个图像

c++ - 是否有必要在构造函数初始化列表中调用默认构造函数?

c++ - 如何优雅地从 COM 错误中恢复?

c++ - 仅蚕食右值引用的部分资源

c - 在 C 中,memcpy 如何处理有符号整数参数?

c - 使用 getchar() 读取两个字符串