开源JPEG图像(解)压缩库libjpeg的编译与使用示例(以VS2010为例)

首先解释以下两个名词(仅供参考,可跳过)

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

转载于:https://www.cnblogs.com/fengyhack/p/10603584.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值