`
ihuashao
  • 浏览: 4563638 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

zlib 与 libpng 的配置与使用 part 2 zlib的安装

阅读更多

zlib 的安装<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

libpng 是一套免费的、公开源代码的程序库,支持对 PNG 图形文件的创建、读写等操作。libpng 使用 zlib 程序库作为压缩引擎,zlib 也是著名的 gzip (GNU zip) 所采用的压缩引擎。

我们首先安装zlib,从其官方网站下载最新的源程序,不妨假设文件名是zlib-1.1.4.tar.gz。网址:http://www.gzip.org/zlib/

D:\ 建立 libpng 目录,将 zlib-1.1.4.tar.gz 释放到这个目录。尽管没有合适的makefile,我们仍然可以直接编译链接 zlib.lib

D:\libpng\zlib-1.1.4>bcc32 -c -O2 -6 -w-8004 -w-8057 -w-8012 *.c

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland

adler32.c:

compress.c:

crc32.c:

deflate.c:

example.c:

gzio.c:

infblock.c:

infcodes.c:

inffast.c:

inflate.c:

inftrees.c:

infutil.c:

maketree.c:

minigzip.c:

trees.c:

uncompr.c:

zutil.c:

D:\libpng\zlib-1.1.4>tlib zlib.lib +adler32.obj +compress.obj +crc32.obj

+deflate.obj +gzio.obj +infblock.obj +infcodes.obj +inffast.obj

+inflate.obj +inftrees.obj +infutil.obj +maketree.obj +trees.obj

+uncompr.obj +zutil.obj

TLIB 4.5 Copyright (c) 1987, 1999 Inprise Corporation

注意,在 tlib 的命令行中,没有 example.obj minigzip.obj。接下来,测试 zlib.lib 是否编译成功,执行:

D:\libpng\zlib-1.1.4>bcc32 minigzip.obj zlib.lib

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland

Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

D:\libpng\zlib-1.1.4>bcc32 example.obj zlib.lib

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland

Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

D:\libpng\zlib-1.1.4>example

uncompress(): hello, hello!

gzread(): hello, hello!

gzgets() after gzseek: hello!

inflate(): hello, hello!

large_inflate(): OK

after inflateSync(): hello, hello!

inflate with dictionary: hello, hello!

执行 example.exe,看见“hello, hello!”,表明生成的 zlib.lib 是好的。

zlib 是通用的压缩库,提供了一套 in-memory 压缩和解压函数,并能检测解压出来的数据的完整性(integrity)zlib 也支持读写 gzip (.gz) 格式的文件。下面介绍两个最有用的函数——compress uncompress

int compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);

compress函数将 source 缓冲区中的内容压缩到 dest 缓冲区。 sourceLen 表示source 缓冲区的大小(以字节计)。注意函数的第二个参数 destLen 是传址调用。当调用函数时,destLen表示 dest 缓冲区的大小,destLen > (sourceLen + 12)*100.1%。当函数退出后,destLen 表示压缩后缓冲区的实际大小。此时 destLen / sourceLen 正好是压缩率。

compress 若成功,则返回 Z_OK;若没有足够内存,则返回 Z_MEM_ERROR;若输出缓冲区不够大,则返回 Z_BUF_ERROR

int uncompress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);

uncompress 函数将 source 缓冲区的内容解压缩到 dest 缓冲区。sourceLen source 缓冲区的大小(以字节计)。注意函数的第二个参数 destLen 是传址调用。当调用函数时,destLen 表示 dest 缓冲区的大小, dest 缓冲区要足以容下解压后的数据。在进行解压缩时,需要提前知道被压缩的数据解压出来会有多大。这就要求在进行压缩之前,保存原始数据的大小(也就是解压后的数据的大小)。这不是 zlib 函数库的功能,需要我们做额外的工作。当函数退出后, destLen 是解压出来的数据的实际大小。

uncompress 若成功,则返回 Z_OK ;若没有足够内存,则返回 Z_MEM_ERROR;若输出缓冲区不够大,则返回 Z_BUF_ERROR。若输入数据有误,则返回 Z_DATA_ERROR

zlib 带的 example.c 是个很好的学习范例,值得一观。我们写个程序,验证 zlib 的压缩功能。所写的测试程序保存为 testzlib.cpp ,放在 zlib-1.1.4 目录下。程序源代码:

// testzlib.cpp 简单测试 zlib 的压缩功能

#include <cstring>

#include <cstdlib>

#include <iostream>

#include "zlib.h"

using namespace std;

int main()

{

int err;

Byte compr[200], uncompr[200]; // big enough

uLong comprLen, uncomprLen;

const char* hello = "12345678901234567890123456789012345678901234567890";

uLong len = strlen(hello) + 1;

comprLen = sizeof(compr) / sizeof(compr[0]);

err = compress(compr, &comprLen, (const Bytef*)hello, len);

if (err != Z_OK) {

cerr << "compess error: " << err << '\n';

exit(1);

}

cout << "orignal size: " << len

<< " , compressed size : " << comprLen << '\n';

strcpy((char*)uncompr, "garbage");

err = uncompress(uncompr, &uncomprLen, compr, comprLen);

if (err != Z_OK) {

cerr << "uncompess error: " << err << '\n';

exit(1);

}

cout << "orignal size: " << len

<< " , uncompressed size : " << uncomprLen << '\n';

if (strcmp((char*)uncompr, hello)) {

cerr << "BAD uncompress!!!\n";

exit(1);

} else {

cout << "uncompress() succeed: \n" << (char *)uncompr;

}

}

编译执行这个程序,输出应该是

D:\libpng\zlib-1.1.4>bcc32 testzlib.cpp zlib.lib

D:\libpng\zlib-1.1.4>testzlib

orignal size: 51 , compressed size : 22

orignal size: 51 , uncompressed size : 51

uncompress() succeed:

12345678901234567890123456789012345678901234567890

至此, zlib的安装任务算是完成了。为了以后使用方便,我将zlib.libzlib.hzconf.h拷贝到D:\mylibs\
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics