在C ++中编写PNG [英] Writing a PNG in C++

查看:482
本文介绍了在C ++中编写PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows 7 64位上使用Visual Studio Express 2013使用C ++将一些数据写入PNG文件。我理解,为了做到这一点,我需要使用外部库,但这里是我有一些困难。

I am trying to write some data to a PNG file using C++ with Visual Studio Express 2013 on Windows 7 64-bit. I understand that to do this, I need to use an external library, but here is where I'm having some difficulty.

我试过使用LodePNG - 它看起来很简单,重量轻,使用方便。问题是,它是TOO简单,似乎需要某种像素格式的数据,不匹配我有。我可以修改我的数据以使其与LodePNG兼容,但我更愿意使用像libpng这样的库有更多的灵活性。

I tried using LodePNG - it looked simple, lightweight, and easy to use. The problem is, it was TOO simple, and seems to require data in a certain pixel format that doesn't match what I have. I could modify my data to make it compatible with LodePNG, but I'd much rather use a library such as libpng with a bit more flexibility.

但是,理解关于构建或链接库的第一件事,libpng已经证明是一个绝对的噩梦。我尝试了这本指南,并设法产生libpng .lib和png.h,但是当我试图在我的项目中包括这两个文件(我把这两个文件放在我的项目目录,添加png.h到我的头文件,并将libpng.lib添加到链接器附加依赖字段),我有一吨的构建错误,特别是:

However, I don't understand the first thing about building or linking libraries, and libpng has proved to be an absolute nightmare in this. I tried following this guide, and managed to produce "libpng.lib" and "png.h", but when I try to include these in my project (I placed both files in my project directory, added "png.h" to my header files and added "libpng.lib" to the Linker's "Additional Dependencies" field), I got a ton of build errors, notably:

error C1083: Cannot open include file: 'pnglibconf.h': No such file or directory

任何人都可以指示如何安装libpng ,指导我到一个很好的指南(我很惊讶的缺乏指南在那里...),或者推荐一个不同的(轻量级,易于安装)PNG图书馆?我在这里疯了。

Can anyone please instruct me as to how to install libpng, direct me to a good guide on the subject (I'm amazed by the lack of guides out there...), or recommend a different (lightweight, easy to install) PNG library? I'm going crazy here.

推荐答案

LodePNG和你说的一样简单。我以前也使用过。为了防止你改变主意,并决定将数据编码为正确的格式(假设它是 BGRA )..以下将BGRA格式转换为RGBA为

LodePNG is as easy as you say. I've used it before as well. Just in case you change your mind and decide to encode the data you have into the right format (assuming it is BGRA).. The following will convert the BGRA format to RGBA as required by lodepng..

std::vector<std::uint8_t> PngBuffer(ImageData.size());

for(std::int32_t I = 0; I < Height; ++I)
{
    for(std::int32_t J = 0; J < Width; ++J)
    {
        std::size_t OldPos = (Height - I - 1) * (Width * 4) + 4 * J;
        std::size_t NewPos = I * (Width * 4) + 4 * J;
        PngBuffer[NewPos + 0] = ImageData[OldPos + 2]; //B is offset 2
        PngBuffer[NewPos + 1] = ImageData[OldPos + 1]; //G is offset 1
        PngBuffer[NewPos + 2] = ImageData[OldPos + 0]; //R is offset 0
        PngBuffer[NewPos + 3] = ImageData[OldPos + 3]; //A is offset 3
    }
}

std::vector<std::uint8_t> ImageBuffer;
lodepng::encode(ImageBuffer, PngBuffer, Width, Height);
lodepng::save_file(ImageBuffer, "SomeImage.png");

这篇关于在C ++中编写PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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