关于libjpeg或libjpeg-turbo使用中的一个注意事项

概述:在使用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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值