一个简单的PNG包装器.任何人都有可以分享的片段

A simple PNG wrapper that works. Anybody have a snippet to share?

本文关键字:任何人 片段 分享 包装 一个 PNG 简单      更新时间:2023-10-16

我正在寻找一种将图像数据缓冲区放入PNG文件的方法,以及将PNG文件放入缓冲区的方法。

我只想做这两件事。

这将是一个使用png.h的非常简单的包装器。好吧,由于极其复杂的libpng API,这并不完全简单,但它的概念是。

我以前试过DevIL。它比libpng更容易使用。尽管如此,我还是遇到了问题。此外,DevIL做了太多。我只需要精简和吝啬的基本PNG格式支持,而不是其他20种格式。

然后我找到这一页。我称赞像素精灵和全能的谷歌给了我一个银盘上的实现。。。事实证明,这会破坏图像:在处理后的图像中,每条扫描线的每四个像素都会丢失。从阅读消息来源中我可以相当肯定,这是不应该发生的!它应该将红色调零,并将绿色设置为蓝色。这也没有发生。

我也尝试过png++。我遇到的问题是,我无法以兼容OpenGL的格式从PNG中获取数据,我必须构建另一个缓冲区。它看起来很难看,但在我考虑再给DevIL一次机会之前,我肯定会再次尝试png++。因为png++至少起了作用。它还实现了只有头的方面。不过,它确实产生了一堆编译器警告。

还有其他竞争者吗?任何直接使用过libpng的人都知道如何实现我所要求的:一个函数,它接受一个文件名,填充一个32bpp的缓冲区,并设置两个分辨率整数;一个函数需要一个32bpp的缓冲区、两个分辨率整数和一个文件名。

更新编辑:我找到了这个。可能有什么东西。

本教程似乎有您想要的。

来自链接:

 //Here's one of the pointers we've defined in the error handler section:
    //Array of row pointers. One for every row.
    rowPtrs = new png_bytep[imgHeight];
    //Alocate a buffer with enough space.
    //(Don't use the stack, these blocks get big easilly)
    //This pointer was also defined in the error handling section, so we can clean it up on error.
    data = new char[imgWidth * imgHeight * bitdepth * channels / 8];
    //This is the length in bytes, of one row.
    const unsigned int stride = imgWidth * bitdepth * channels / 8;
    //A little for-loop here to set all the row pointers to the starting
    //Adresses for every row in the buffer
    for (size_t i = 0; i < imgHeight; i++) {
        //Set the pointer to the data pointer + i times the row stride.
        //Notice that the row order is reversed with q.
        //This is how at least OpenGL expects it,
        //and how many other image loaders present the data.
        png_uint_32 q = (imgHeight- i - 1) * stride;
        rowPtrs[i] = (png_bytep)data + q;
    }
    //And here it is! The actuall reading of the image!
    //Read the imagedata and write it to the adresses pointed to
    //by rowptrs (in other words: our image databuffer)
    png_read_image(pngPtr, rowPtrs);

我会将CImg添加到选项列表中。虽然它是一个图像库,但API并不像大多数(devil/imagemagik/freeimage/GIL)那样高。它也是仅标题。

image类具有简单的宽度-高度和具有公共访问权限的数据成员。在后台,它使用libpng(如果你用预处理器指令告诉它)。数据将强制转换为您为模板图像对象选择的任何类型。

CImg<uint8_t>myRGBA("fname.png");
myRGBA._data[0] = 255; //set red value of first pixel

Sean Barrett编写了两个用于PNG图像读写的公共域文件。