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