一个简单的PNG包装器.任何人都有可以分享的片段
A simple PNG wrapper that works. Anybody have a snippet to share?
我正在寻找一种将图像数据缓冲区放入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图像读写的公共域文件。
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- Opengl 3.1 GLSL 140 在 C++ 年输出白色在片段着色器中
- 这两个代码片段相似,但显示的结果不同
- 如何替换此示例代码片段中已弃用的handler_type_t或 boost::asio::handler_type?
- 我需要解释给定片段的输出
- 任何人都可以告诉我我的 C++ 代码出了什么问题?
- 如果我在下面的代码片段中添加"delete[] d;",为什么我得到零?
- 任何人都可以弄清楚这段代码如何显示运行错误?它打印无限时间 -1 以及正确答案
- 我遇到了这个代码片段,不明白. 它递归检查 C++ 字符串中是否存在大写字符
- 任何人都可以解释一下我是否需要 & 在第一个代码片段中
- OpenGL 片段着色器未在英特尔 HD 4000 显卡上编译
- 任何人都可以查明我的递归函数中的错误吗?
- 帧缓冲纹理变为白色(片段着色器不会影响它)
- 如何在 c++ 中检查连续片段中数字被 11 整除
- 提取狮身人面像文档中的C++代码片段
- 为什么此代码片段有效?如何取消引用空点?
- 将顶点位置从顶点传递到片段着色器 - 仅在使用 Nsight 进行调试时有效
- 无法在这个基本的Qt代码片段中找到错误,但我被告知它肯定存在?
- 使用片段着色器写入 1D 纹理后从 1D 纹理读回不起作用
- 一个简单的PNG包装器.任何人都有可以分享的片段