libjpeg学习3:turbojpeg试用

本文详细解析了TurboJPEG库针对ARM和X86的优化,对比传统libjpeg性能提升显著。通过提供的代码示例,展示了如何利用TurboJPEG实现从JPEG编码到RGB转换以及从RGB到JPEG编码的基本流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

turbojpeg针对ARM和X86对了优化,宣称其速度是libjpeg的2到4倍。下载其源码,值得称赞的地方是其例子,单元测试很到位。另外是它的注释,或者说是html说明文件,对于宏、函数都有详细的说明。本文就是参考源码的例子和html文档写的简单示例。由于只是试用,并无深入研究,只是在我的虚拟机里运行。对于性能测试,并未进行。

代码示例:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <setjmp.h>

#include "jpeg-utils.h"

// jpeg库头文件必须放到stdio.h后面
#include "libjpeg/include/turbojpeg.h"


int tjpeg_header(unsigned char* jpeg_buffer, int jpeg_size, int* width, int* height, int* subsample, int* colorspace)
{
    tjhandle handle = NULL;

    handle = tjInitDecompress();
    tjDecompressHeader3(handle, jpeg_buffer, jpeg_size, width, height, subsample, colorspace);

    tjDestroy(handle);

    return 0;
}

int tjpeg2rgb(unsigned char* jpeg_buffer, int jpeg_size, unsigned char* rgb_buffer, int* size)
{
    tjhandle handle = NULL;
    int width, height, subsample, colorspace;
    int flags = 0;
    int pixelfmt = TJPF_RGB;

    handle = tjInitDecompress();
    tjDecompressHeader3(handle, jpeg_buffer, jpeg_size, &width, &height, &subsample, &colorspace);

    flags |= 0;
    tjDecompress2(handle, jpeg_buffer, jpeg_size, rgb_buffer, width, 0,
            height, pixelfmt, flags);

    tjDestroy(handle);

    return 0;
}

int trgb2jpeg(unsigned char* rgb_buffer, int width, int height, int quality, unsigned char** jpeg_buffer, unsigned long* jpeg_size)
{
    tjhandle handle = NULL;
    //unsigned long size=0;
    int flags = 0;
    int subsamp = TJSAMP_422;
    int pixelfmt = TJPF_RGB;

    handle=tjInitCompress();
    //size=tjBufSize(width, height, subsamp);
    tjCompress2(handle, rgb_buffer, width, 0, height, pixelfmt, jpeg_buffer, jpeg_size, subsamp,
            quality, flags);

    tjDestroy(handle);

    return 0;
}


李迟 2015.7.7


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值