前几天转载了 libjpeg实现内存内位图的压缩及解压缩 文章,通过实践和思考,我用类似的方法实现内存内位图的压缩及解压缩,但不需要修改libjpeg的原代码库。
其实使用libjpeg库时,通过抄袭改写库内jdatasrc.c和jdatadst.c两个文件,加入到自己的项目代码中就可以实现输入数据和输出数据的重定向。
基本使用上和调用libjpeg原库有如下区别:
1.解码:
......
//jpeg_create_decompress(&cinfo);//建立解码器
//配置解码器从内存源读入数据:
//用jpeg_mem_src()代码如下,代替原来的jpeg_stdio_src(&cinfo, fp/*文件指针*/)
//jpeg_mem_src(){
jpeg_source_mgr my_smgr;
my_smgr.next_input_byte=lpSrcBuf;
my_smgr.bytes_in_buffer=dwSrcBufSize;
my_smgr.init_source=init_mem_src;
my_smgr.fill_input_buffer=fill_mem_input_buffer;
my_smgr.skip_input_data=skip_mem_input_data;
my_smgr.resync_to_restart=jpeg_resync_to_restart;
my_smgr.term_source=term_mem_src;
cinfo.src=&my_smgr;
//}
//(void)jpeg_read_header(&cinfo, TRUE);//读入头部数据
......
2.编码:
jpeg_create_compress(&cinfo);//建立编码器
//配置编码器的数据输出内存:
//用jpeg_memio_dest()代替jpeg_stdio_dest((&cinfo, fp/*文件指针*/);
jpeg_memio_dest(&cinfo, lpDestBuf, &dwDestBufSize);
//设置编码器输入数据信息:
//cinfo.in_color_space=JCS_RGB;//colorspace of input image.
//cinfo.image_width=iWidth;//image width and height, in pixels.
//cinfo.image_height=iHeight;
//cinfo.input_components=3;//color components per pixel.
//jpeg_set_defaults(&cinfo);
//设置编码质量参数:
//jpeg_set_quality(&cinfo, iQuality, TRUE/*limit to baseline-JPEG values*/);
//启动编码器:
jpeg_start_compress(&cinfo, TRUE);
具体方法请参见我封装好的CJPG类吧,可以直接在VC工程中调用,欢迎大家修改和交流。
鼠标右键“目标另存为...”下载后后缀改为.rar解压即可。
转自http://blog.youkuaiyun.com/dj0379/archive/2009/07/08/4330765.aspx