使用Jpeglib

/******************************************************************* 
*  Copyright(c) 2017 
*  All rights reserved. 
* 
*  文件名称: Jpeg.cpp
*  简要描述: 使用Jpeglib
* 
*  创建日期: 2017-08-14 
*  作者:  gongluck 
*  说明: 
* 
*  修改日期: 
*  作者: 
*  说明: 
******************************************************************/
#include "Jpeg.h"

int DecodeJpeg2Bmp(const char* filename,void* outbuf,int bufsize,int* nBits,int* width,int* height)
{
    if(filename == NULL)
        return -1;

    //声明并初始化解压缩对象,同时制定错误信息管理器
    jpeg_decompress_struct cinfo = {0};
    jpeg_error_mgr error = {0};
    cinfo.err = jpeg_std_error(&error);
    jpeg_create_decompress(&cinfo);

    //打开jpg图像文件,并指定为解压缩对象的源文件
    FILE* finput = fopen(filename,"rb");
    if(finput == NULL)
        return -2;

    jpeg_stdio_src(&cinfo,finput);

    //读取图像信息
    jpeg_read_header(&cinfo,TRUE);

    //根据图像信息申请一个图像缓冲区
    if(outbuf == NULL)
    {
        *nBits = cinfo.num_components * 8;
        *width = cinfo.image_width;
        *height = cinfo.image_height;
        jpeg_destroy_decompress(&cinfo);
        fclose(finput);
        return sizeof(BYTE)*cinfo.image_width*(cinfo.image_height+1)*cinfo.num_components;
    }
        
    JSAMPROW data = (JSAMPROW)outbuf;

    //开始解压缩
    jpeg_start_decompress(&cinfo);
    JSAMPROW row_pointer[1];
    while (cinfo.output_scanline < cinfo.output_height)
    {
        row_pointer[0] = &data[(cinfo.output_scanline)*cinfo.image_width*cinfo.num_components];
        jpeg_read_scanlines(&cinfo,row_pointer ,1);
    }
    jpeg_finish_decompress(&cinfo);

    //释放资源
    jpeg_destroy_decompress(&cinfo);
    fclose(finput);
    return 0;
}
github
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值