概述:在使用libjpeg在我的项目中,在测试过程中压缩图像出错,然后程序直接退出了,这对于我们程序来说那肯定是不行的啊。所以就开始找问题啦,在网上找到原因,原来是我们在压缩过程中使用了libjpeg默认的出错处理函数error_exit(),所以导致出错以后程序就整个退出了。今天我主要就是把这个分析的过程记录下来。
我们先看一个例子,我们使用默认的错误处理函数进行图片解压缩。代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <jpeglib.h>
/*读取jpg文件并解压,使用官方自带的error处理函数*/
unsigned char *read_jpeg_file(char *filename)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE *infile;
JSAMPARRAY buffer;
int row_stride;
if (NULL == (infile = fopen(filename, "rb"))) {
printf("can't open %s\n",filename);
return NULL;
}
cinfo.err = jpeg_std_error(&jerr);
/*初始化*/
jpeg_create_decompress(&cinfo);
/*指定输入文件*/
jpeg_stdio_src(&cinfo, infile);
/*读取文件信息*/
jpeg_read_header(&cinfo, TRUE);
/*解压*/
jpeg_start_decompress(&cinfo);
row_stride = cinfo.output_height * cinfo.output_width * cinfo.output_components;
/*输出图像信息*/
printf("output_width = %d\n", cinfo.output_width);
printf("output_height = %d\n", cinfo.output_height);
printf("output_components = %d\n", cinfo.output_components);
/*申请内存用于存储解压数据*/
unsigned char *data = (unsigned char *)malloc(sizeof(unsigned char) * row_stride);
/*逐行扫描获取解压信息*/
JSAMPROW row_pointer[1];
while (cinfo.output_scanline < cinfo.output_height) {
row_pointer[0] = &data[(cinfo.output_height - cinfo.output_scanline-1)*cinfo.output_width*cinfo.output_components];
jpeg_read_scanlines(&cinfo,row_pointer ,1);
}
/*完成解压*/
jpeg_fi