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的头文件。
应用例程:
几个常用函数:
unsigned lodepng_decode_memory(unsigned char** out, unsigned* w, unsigned* h,
const unsigned char* in, size_t insize,
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).
unsigned lodepng_encode_file(const char* filename,
const unsigned char* image, unsigned w, unsigned h,
LodePNGColorType colortype, unsigned bitdepth);
这个函数将原始的像素数据编码成一张PNG图像。
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/
更多相关资源请关注 博客:LinJM-机器视觉 微博:林建民-机器视觉