如何在 C++ 或 C 中创建和编写索引的 png 图像 [英] How to create and write an indexed png image in C++ or C

查看:88
本文介绍了如何在 C++ 或 C 中创建和编写索引的 png 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用可移植到 IOS 和 Android 的库在 C++ 或 C 中创建和编写索引的 png 图像,因此我研究了 png++、opencv 和 libpng.我曾尝试使用 png++ 但无法正确生成索引图像.我无法找出 libpng 索引图像,也没有找到示例.opencv 似乎不能处理索引的 png 图像.png++ 似乎更易于使用,但文档省略了有关如何为索引图像设置自己的值的部分,它只是在该部分中放置了..."(见下文).任何帮助将不胜感激.

I am trying to create and write an indexed png image in C++ or C using a library that is portable to IOS and Android, so I looked into png++, opencv and libpng. I have tried using png++ but could not produce index images correctly. I was not able to figure out libpng indexed images, and did not find an example. opencv does not seem to handle indexed png images. png++ seems to be easier to use but the documentation leaves out the part on how to set your own values for the indexed images, it just puts "..." in that section (see below). Any help would be appreciated.

#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.

You create the indexed image the same way as you would create an rgb image, except that the pixel type is png::index_pixel instead of png::rgb_pixel.

否则,它看起来就像文档中的示例:

Otherwise, it looks just like the example in the documentation:

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 中创建和编写索引的 png 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆