编译CxImage类库
作者已经提供了整个类库配置的工程文件CxImgLib.dsw (VC++6.0),只要打开它进行编译即可。需要大家注意的是:整个CxImage类库非常大。如果你只需要能处理其中的几种格式,编译该类库时,你可以在配置的头文件ximcfg.h 中找到一些编译开关选项来关闭一些图像库。JPG、PNG、TIFF中的每一个库,都会向最终程序增加约100KB的内容。而CxImage类库压缩后只有约60KB。所以,你需要谨慎挑选一些你真正需要的类库。

















其他的可以不打开
编译该类库有好几个选择的工程,如下图所示:
各工程的作用对应如下:
- CxImage : cximage.lib - static library
- CxImageCrtDll : cximagecrt.dll - DLL not using mfc
- CxImageMfcDll : cximage.dll - DLL using mfc
- Demo : demo.exe - program linked with cximage.lib and the C libraries
- DemoDll : demodll.exe - program linked with cximagecrt.dll
- j2k,jasper,jbig,jpeg,png,tiff,zlib : static C libraries
编译这些工程需要耗费几分钟的时间(中间文件可达60MB)。
在程序中应用CxImage类库进行图像类型转换
在你的VC工程中使用这个类库,要对工程进行如下设置(Project Settings):
|- C/C++
| |- Code Generation
| | |- Use run-time library : Multithreaded DLL (must be the same for
| | | all the linked libraries) //应该只要是多线程DLL即可,DEBUG的也行
| | |- Struct member alignment : must be the same for all the linked libraries
| |- Precompiled headers : not using precompiled headers
| |- Preprocessor
| |- Additional Include Directories: ../cximage(该处填CxImage里的.h和.cpp文件拷贝并导入工程后所在的文件夹,填写后在工程中include时编译器会查找该文件 夹,故include的文件无需路径)
|- Link
|- General
|- Object/library modules: png.lib
jpeg.lib
zlib.lib
tiff.lib
jasper.lib
cximage.lib (把需要的lib文件从CxImage中拷贝到工程中的lib文件所在的目录)并且从CxImage中将xfile.h、ximacfg.h、ximadef.h、ximage.cpp、ximage.h、 xiofile.h、xmemfile.cpp、xmemfile.h拷贝到工程文件夹下并将CxImage.h文件加入工程中即可。也可以设置vc6 的"tools"中的"include"路径.
下面介绍应用它进行图像类型转换的方式:
1.从一种图像文件类型转换为另一种文件类型(convert from a format to another)
CxImage image; // 定义一个CxImage对象
// 从bmp文件转换为jpg文件(bmp -> jpg)
image.Load( " image.bmp " , CXIMAGE_FORMAT_BMP); // 先装载bmp文件,需要指定文件类型
// 判断加载的bmp文件是否存在。
if (image.IsValid())... {
// Returns true if the image has 256 colors and a linear grey scale palette.
if ( ! image.IsGrayScale()) image.IncreaseBpp( 24 ); // param nbit: 4, 8, 24
image.SetJpegQuality( 99 ); // 设置图像的压缩质量参数(从0到100,数值越大,质量越高)
image.Save( " image.jpg " ,CXIMAGE_FORMAT_JPG); // 把压缩后的图像以jpg文件类型保存起来。
}
// 从png文件转换为tif文件(png -> tif)
image.Load( " image.png " , CXIMAGE_FORMAT_PNG);
if (image.IsValid()) {
image.Save( " image.tif " ,CXIMAGE_FORMAT_TIF);
}