1. 程式人生 > >libjpeg學習3:turbojpeg試用

libjpeg學習3:turbojpeg試用

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