前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Jpeglib

使用Jpeglib

作者头像
_gongluck
发布2018-03-08 15:56:10
1.1K0
发布2018-03-08 15:56:10
举报
/******************************************************************* 
*  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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年08月14日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档