基于libjpeg实现的jpeg解码demo

使用的libjpeg版本为jpegsr9b,在上一篇blog有libjpeg在VS2012下的编译步骤:
VS2012编译libjpeg

libjpeg解码jpeg图片

解码调用流程如下:

static bool _jpgToRGBColor(PICTUREINFO picInputInfo, PICTUREINFO *picOutputInfo)
{
    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;

    BYTE *inBuffer = picInputInfo.pBuffer;
    unsigned inBufferLen = picInputInfo.bufferLen;
    BYTE *outBuffer = NULL;

    if(picInputInfo.pBuffer == NULL || picInputInfo.bufferLen<=0 || picInputInfo.format != PIC_JPEG)
    {
        printf("%s %d error input parameters!\n", __FUNCTION__, __LINE__);
        return false;
    }

    if(!picOutputInfo)
    {
        printf("%s %d error input parameters!\n", __FUNCTION__, __LINE__);
        return false;
    }

    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_decompress(&cinfo);
    jpeg_mem_src(&cinfo, inBuffer, inBufferLen);

    jpeg_read_header(&cinfo, TRUE);
    if(!jpeg_start_decompress(&cinfo))
    {
        printf("%s %d decode failed!\n", __FUNCTION__, __LINE__);

        jpeg_finish_decompress(&cinfo);
        jpeg_destroy_decompress(&cinfo);
        return false;
    }

    unsigned picWidth = cinfo.output_width;
    unsigned picHeight = cinfo.output_height;
    unsigned picDepth = cinfo.output_components;

    printf("The picture width is:%u, height is:%u, depth is:%u\n", picWidth, picHeight, picDepth);

    outBuffer = (BYTE *)malloc(picWidth*picHeight*picDepth);
    if(!(outBuffer))
    {
        printf("%s %d allocate buffer failed!\n", __FUNCTION__, __LINE__);
        return false;
    }
    memset(outBuffer, 0x00, picWidth*picHeight*picDepth);

    JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo, JPOOL_IMAGE, picWidth*picHeight,1);
    BYTE *pTmpBuffer = (outBuffer)+(picHeight-cinfo.output_scanline-1)*(picWidth*picDepth);
    while(cinfo.output_scanline<picHeight)
    {
        jpeg_read_scanlines(&cinfo, buffer, 1);
        memcpy(pTmpBuffer, *buffer, picWidth*picDepth);
        pTmpBuffer -= picWidth*picDepth;
    }
    jpeg_finish_decompress(&cinfo);
    jp
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值