首先解释以下两个名词(仅供参考,可跳过)
JPEG=Joint Photographic Experts Group(联合图像专家小组),是第一个国际图像压缩标准。JPEG图像压缩算法能够在提供良好的压缩性能的同时,具有比较好的重建质量,被广泛应用于图像、视频处理领域。人们日常碰到的.jpeg,.jpg等指代的是图像数据经压缩编码后在媒体上的封存形式,不能与JPEG压缩标准混为一谈。(百度百科)
IJG is an informal group that writes and distributes a widely used free library for JPEG image compression. The first version was released on 7-Oct-1991. (http://www.ijg.org/)
本文将关注IJG提供的JPEG图像压缩/解压缩库的编译与初步使用。
首先在IJG网站http://www.ijg.org/下载源代码,此文编辑时已发布的版本为version 9a
编译
我们将要在Visual Studio 2010环境下编译,编译目标为libjpeg.lib
1、下载jpegsrc9a.zip并解压
2、复制jconfig.vc文件内容到新建文件jconfig.h
3、编辑Makefile.vc文件内容
其中.mak文件路径可能会因机器配置等原因而不一样
以上截图中是测试机器(Windows 7 专业版 64位)上的文件路径
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include\Win32.Mak
4、编译
开始菜单-->VS2010工具-->VS命令提示
切换到源码所在目录
键入命令 nmake /f makefile.vc nodebug=1
其中nodebug开关(=1打开)表示以release模式编译
数秒之后,编译完成
生成目标为libjpeg.lib
5、使用
拷贝以下4个文件
其中jconfig.h是第2步骤中修改的,jmorecfg.h是一些较为详细的配置,jpeglib.h是库引用声明
在使用时,将以上头文件和库文件添加到工程即可。
示例
以下是一个简单的JPEG文件读取(解压)示例
#include <stdio.h>
#include "jpeglib.h"
#pragma comment(lib,"libjpeg.lib")
#pragma warning(disable:4996)
typedef unsigned char BYTE;
int main(void)
{
printf("Input jpeg file path:");
char szFileName[256] = { 0 };
scanf("%s", szFileName);
jpeg_decompress_struct cinfo;
jpeg_error_mgr jerr;
// STEP 1: StdError
cinfo.err = jpeg_std_error(&jerr);
// STEP 2: Create
jpeg_create_decompress(&cinfo);
FILE* pf = fopen(szFileName, "rb");
if (pf != NULL)
{
// STEP 3: IO
jpeg_stdio_src(&cinfo, pf);
// STEP 4: Header
jpeg_read_header(&cinfo, TRUE);
long bytes = cinfo.image_width*cinfo.image_height*cinfo.num_components;
printf("Allocate %d bytes memory:",bytes);
BYTE* data = new BYTE[bytes];
if (data != NULL)
{
printf("OK.\nPrepare to decompress the image...\n");
// STEP 5: Start
jpeg_start_decompress(&cinfo);
JSAMPROW row_pointer[1];
// STEP 6: ReadScan
while (cinfo.output_scanline < cinfo.output_height)
{
row_pointer[0] = &data[(cinfo.output_height - cinfo.output_scanline - 1)*cinfo.image_width*cinfo.num_components];
jpeg_read_scanlines(&cinfo, row_pointer, 1);
}
// STEP 7: Finish
jpeg_finish_decompress(&cinfo);
// Do something with
// BYTE data[] here
// and then release it
delete[] data;
}
else
{
printf("FAILED.\n");
}
// STEP 8: Destroy
jpeg_destroy_decompress(&cinfo);
fclose(pf);
printf("JPEG decompression finished.\n");
}
else
{
printf("Failed to open \'%s\'\n", szFileName);
}
printf("Press ENTER to continue...");
getchar();
getchar();
return 0;
}
某次运行的结果截图(截取了一张1920*1080的JPG格式图片做测试)
本文原创,博文地址
http://blog.csdn.net/fengyhack/article/details/42239807