PNG编解码实现---LodePNG

LodePNG是一个集合了PNG图像解码器和编码器的代码文件,不依赖于诸如zlib和libpng的外部链接/库,提供方便友好的PNG编解码器调用方法。LodePNG主要是采用C(ISO C90)编写的,并提供了C++的接口。LodePNG的使用非常简单,只要在项目文件中包含lodepng.cpp和lodepng.h或者lodepng.c和lodepng.h就可以。

LodePNG文件

lodepng.cpp: 这个LodePNG的主体部分,里面实现了PNG的编解码的核心功能,无需多言。

lodepng.c: 如果你要在c语言下使用,只要把上面的文件重命名为lodepng.c就可以了。

lodepng.h: 是lodepng.cpp和lodepng.c的头文件。

应用例程

几个常用函数

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. unsigned lodepng_decode_memory(unsigned char** out, unsigned* w, unsigned* h,  
  2.                                const unsigned char* in, size_t insize,  
  3.                                LodePNGColorType colortype, unsigned bitdepth);  

这个函数将一张PNG图像解码成原始的像素数据。

参数:

out:  Pointer to buffer that will contain the raw pixel data.After decoding, its size is w * h * (bytes per pixel) bytes larger than initially. Bytes per pixel depends on colortype and bitdepth.Must be freed after usage with free(*out).Note: for 16-bit per channel colors, uses big endian format like PNG does.

w:  Pointer to width of pixel data.

h: Pointer to height of pixel data.

in: Memory buffer with the PNG file.

insize: size of the in buffer.

colortype: the desired color type for the raw output image. See explanation on PNG color types.

bitdepth: the desired bit depth for the raw output image. See explanation on PNG color types.

Return value: LodePNG error code (0 means no error).


[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. unsigned lodepng_encode_file(const char* filename,  
  2.                              const unsigned char* image, unsigned w, unsigned h,  
  3.                              LodePNGColorType colortype, unsigned bitdepth);  
这个函数将原始的像素数据编码成一张PNG图像。

FAQ

Q: When using OpenGL and decoding textures with LodePNG, the image is upside down.
A: LodePNG uses a pixel order where it goes in rows from top to bottom. In OpenGL, how would you define upside down? There are many things which affect the orientation with which something is drawn in OpenGL: camera orientation, texture coordinates, the orientation of the object, ... If you don't want to solve the problem by changing OpenGL coordinates, e.g. because your game engine uses a bottom to top representation for all textures, you can still go through the raw pixel buffer of LodePNG with a for loop and swap the rows to become upside down. Remember that for 32-bit RGBA, each row is (width * 4 bytes) long, for 24-bit RGB it's (width * 3 bytes) long.

Q: The image is wrong when using LodePNG with BITMAPs in Visual Studio or win32, or with BMP images.
A: LodePNG uses raw buffers with RGBA or RGB pixels in the common order RGBARGBARGBA... or RGBRGBRGBRGB..., from top to bottom, without any special byte boundaries. BMP images and bitmaps in win32 use a different format. They do three things differently:
Instead of being from top to bottom, it goes from bottom to top (upside down).
Instead of using the order red, green, blue, it uses the order blue, green, red, or BGRBGRBGR..., (can result in wrong colors).
It has a limitation where rows always must be a multiple of 4 bytes, so if the width of the image is not a multiple of 4 some unused bytes are introduced at the end of each row (can result in a skewed image).
All of this are related to how BMP works, not how PNG or LodePNG work. When you're working with BMP, you need to take each of these three things into account and convert this to/from the buffer format that LodePNG uses. Also check out the png2bmp and bmp2png samples, which already do this.


Q: What's the difference between zlib, deflate and gzip? And between CRC32 and ADLER32?
A: That can be confusing. In short:
PNG uses zlib. Zlib uses deflate. Gzip uses deflate and is not used by PNG.
PNG uses CRC32, and indirectly ADLER32. Zlib uses ADLER32. Gzip uses CRC32.
IETF Standards: PNG is RFC 2083. Zlib is RFC 1950. Deflate is RFC 1951. Gzip is RFC 1952.

Q: Why did the interface of LodePNG change? Why not keep it as is, fixing compile errors is annoying!
A: Sorry about that. Sometimes when a feature is added the interface grows into something inconsistent, and then I try to redesign it to get a new, better, one. And sometimes when a new feature is added the cleanest way to do it requires an interface change. In the last change, the following improvements were made that changed it:
There used to be some fields in camelCase, and others with_underscores. Now, this is made more consistent: all fields and functions use underscore between words (except between two nouns that form one word, like bitdepth or palettesize), and all typenames start with a capital and use CamelCase.
The large amount of structs got reduced a bit.
The C++ class got removed: there was really no reason to have a class here. A encoder and decoder class with subclass implementations makes sense in a framework that can encode/decode many different image formats, but not in a single format like here. Instead, a convenient RAII wrapper lodepng::State is available around the new LodePNGState struct.
The C++ namespace is lodepng instead of LodePNG because namespaces look better in small letters imho.


官方原文链接:http://lodev.org/lodepng/

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值