【问题标题】:How to create and write an indexed png image in C++ or C如何在 C++ 或 C 中创建和编写索引 png 图像
【发布时间】:2020-08-04 21:48:38
【问题描述】:

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

【问题讨论】:

  • 你试过image[y][x] = png::index_pixel(whatever);吗? (索引在哪里,在 0-255 范围内)?
  • rici,谢谢,成功了。
  • 好的,回答了。感谢您的确认。

标签: c++ c image png


【解决方案1】:

创建索引图像的方式与创建 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, ...);
    }
}

【讨论】:

    猜你喜欢
    • 2022-01-07
    • 2010-10-27
    • 2016-09-07
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    相关资源
    最近更新 更多