如何用C++或C语言创建和编写索引的png图片

问题描述 投票:0回答:1

我正试图用C++或C语言创建和编写一个可移植到IOS和Android的库的索引图片,所以我研究了png++、opencv和libpng。 我尝试过使用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");
c++ c image png
1个回答
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, ...);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.