34

libjpeg-turboの説明では、 TurboJPEG APIについて次のように説明しています。「このAPIは、libjpeg-turboをラップし、メモリ内のJPEG画像を圧縮および解凍するための使いやすいインターフェイスを提供します」。すばらしいですが、このAPIを使用した確かな例はありますか?メモリ内のかなりバニラのjpegを解凍しようとしています。

TurboJPEG APIを使用しているように見えるhttps://github.com/erlyvideo/jpeg/blob/master/c_src/jpeg.cなどのビットをいくつか見つけましたが、他に堅実で多様な例はありますか?

libjpeg-turboのソースは十分に文書化されているので、それは役に立ちます。

4

4 に答える 4

65

わかりました。あなたはすでに問題を解決していることは知っていますが、私のように、簡単な例を検索している人もいるので、私が作成したものを共有します。これは一例であり、RGB画像を圧縮および解凍します。そうでなければ、 TurboJPEGのAPIドキュメントは非常に理解しやすいと思います!

圧縮:

#include <turbojpeg.h>

const int JPEG_QUALITY = 75;
const int COLOR_COMPONENTS = 3;
int _width = 1920;
int _height = 1080;
long unsigned int _jpegSize = 0;
unsigned char* _compressedImage = NULL; //!< Memory is allocated by tjCompress2 if _jpegSize == 0
unsigned char buffer[_width*_height*COLOR_COMPONENTS]; //!< Contains the uncompressed image

tjhandle _jpegCompressor = tjInitCompress();

tjCompress2(_jpegCompressor, buffer, _width, 0, _height, TJPF_RGB,
          &_compressedImage, &_jpegSize, TJSAMP_444, JPEG_QUALITY,
          TJFLAG_FASTDCT);

tjDestroy(_jpegCompressor);

//to free the memory allocated by TurboJPEG (either by tjAlloc(), 
//or by the Compress/Decompress) after you are done working on it:
tjFree(&_compressedImage);

その後、_compressedImageに圧縮画像があります。解凍するには、次の手順を実行する必要があります。

減圧:

#include <turbojpeg.h>

long unsigned int _jpegSize; //!< _jpegSize from above
unsigned char* _compressedImage; //!< _compressedImage from above

int jpegSubsamp, width, height;
unsigned char buffer[width*height*COLOR_COMPONENTS]; //!< will contain the decompressed image

tjhandle _jpegDecompressor = tjInitDecompress();

tjDecompressHeader2(_jpegDecompressor, _compressedImage, _jpegSize, &width, &height, &jpegSubsamp);

tjDecompress2(_jpegDecompressor, _compressedImage, _jpegSize, buffer, width, 0/*pitch*/, height, TJPF_RGB, TJFLAG_FASTDCT);

tjDestroy(_jpegDecompressor);

いくつかのランダムな考え:

学士論文を書いているときにこれに戻ってきました。圧縮をループで実行する場合は、毎ターン新しいものを割り当てる必要がないように、最大​​サイズのJPEGバッファを保存することが望ましいことに気付きました。基本的に、行う代わりに:

long unsigned int _jpegSize = 0;

tjCompress2(_jpegCompressor, buffer, _width, 0, _height, TJPF_RGB,
          &_compressedImage, &_jpegSize, TJSAMP_444, JPEG_QUALITY,
          TJFLAG_FASTDCT);

割り当てられたメモリのサイズを保持するオブジェクト変数を追加し、long unsigned int _jpegBufferSize = 0;すべての圧縮ラウンドの前に、jpegSizeをその値に戻します。

long unsigned int jpegSize = _jpegBufferSize;

tjCompress2(_jpegCompressor, buffer, _width, 0, _height, TJPF_RGB,
          &_compressedImage, &jpegSize, TJSAMP_444, JPEG_QUALITY,
          TJFLAG_FASTDCT);

_jpegBufferSize = _jpegBufferSize >= jpegSize? _jpegBufferSize : jpegSize;

圧縮後、メモリサイズを実際のjpegSizeと比較し、前のメモリサイズよりも大きい場合はjpegSizeに設定します。

于 2013-07-16T07:55:13.220 に答える
7

JPEGエンコーディングとデコーディングの両方の実用的な例として以下のコードを使用することになりました。私が見つけることができる最良の例は、ダミー画像を初期化し、エンコードされた画像をローカルファイルに出力する自己完結型です。

以下のコードは私自身のものではありません。クレジットはhttps://sourceforge.net/p/libjpeg-turbo/discussion/1086868/thread/e402d36f/#8722にあります。libjpegターボを動作させるのが難しいと誰もが気付くのを助けるために、ここにもう一度投稿してください。

#include "turbojpeg.h"
#include <iostream>
#include <string.h>
#include <errno.h>

using namespace std;

int main(void)
{
    unsigned char *srcBuf; //passed in as a param containing pixel data in RGB pixel interleaved format
    tjhandle handle = tjInitCompress();

    if(handle == NULL)
    {
        const char *err = (const char *) tjGetErrorStr();
        cerr << "TJ Error: " << err << " UNABLE TO INIT TJ Compressor Object\n";
        return -1;
    }
    int jpegQual =92;
    int width = 128;
    int height = 128;
    int nbands = 3;
    int flags = 0;
    unsigned char* jpegBuf = NULL;
    int pitch = width * nbands;
    int pixelFormat = TJPF_GRAY;
    int jpegSubsamp = TJSAMP_GRAY;
    if(nbands == 3)
    {
        pixelFormat = TJPF_RGB;
        jpegSubsamp = TJSAMP_411;
    }
    unsigned long jpegSize = 0;

    srcBuf = new unsigned char[width * height * nbands];
    for(int j = 0; j < height; j++)
    {
        for(int i = 0; i < width; i++)
        {
            srcBuf[(j * width + i) * nbands + 0] = (i) % 256;
            srcBuf[(j * width + i) * nbands + 1] = (j) % 256;
            srcBuf[(j * width + i) * nbands + 2] = (j + i) % 256;
        }
    }

    int tj_stat = tjCompress2( handle, srcBuf, width, pitch, height,
        pixelFormat, &(jpegBuf), &jpegSize, jpegSubsamp, jpegQual, flags);
    if(tj_stat != 0)
    {
        const char *err = (const char *) tjGetErrorStr();
        cerr << "TurboJPEG Error: " << err << " UNABLE TO COMPRESS JPEG IMAGE\n";
        tjDestroy(handle);
        handle = NULL;
        return -1;
    }

    FILE *file = fopen("out.jpg", "wb");
    if (!file) {
        cerr << "Could not open JPEG file: " << strerror(errno);
        return -1;
    }
    if (fwrite(jpegBuf, jpegSize, 1, file) < 1) {
        cerr << "Could not write JPEG file: " << strerror(errno);
        return -1;
    }
    fclose(file);

    //write out the compress date to the image file
    //cleanup
    int tjstat = tjDestroy(handle); //should deallocate data buffer
    handle = 0;
}
于 2018-03-07T00:40:38.193 に答える
4

結局、インターネットで見つかったランダムコード(例:https ://github.com/erlyvideo/jpeg/blob/master/c_src/jpeg.c )とlibjeg-turboの.cおよびヘッダーファイルの組み合わせを使用しました。十分に文書化されています。 この公式APIは、優れた情報源でもあります。

于 2012-02-18T03:09:57.973 に答える
2

これは、メモリからjpegをロードするために使用するコードの断片です。プロジェクト内のさまざまなファイルから抽出したため、多少の修正が必要になる場合があります。グレースケール画像とrgb画像の両方が読み込まれます(bppは1または3に設定されます)。

struct Image
{
    int bpp;
    int width;
    int height;
    unsigned char* data;
};

struct jerror_mgr
{
    jpeg_error_mgr base;
    jmp_buf        jmp;
};

METHODDEF(void) jerror_exit(j_common_ptr jinfo)
{
    jerror_mgr* err = (jerror_mgr*)jinfo->err;
    longjmp(err->jmp, 1);
}

METHODDEF(void) joutput_message(j_common_ptr)
{
}

bool Image_LoadJpeg(Image* image, unsigned char* img_data, unsigned int img_size)
{
    jpeg_decompress_struct jinfo;
    jerror_mgr jerr;

    jinfo.err = jpeg_std_error(&jerr.base);
    jerr.base.error_exit = jerror_exit;
    jerr.base.output_message = joutput_message;
    jpeg_create_decompress(&jinfo);

    image->data = NULL;

    if (setjmp(jerr.jmp)) goto bail;

    jpeg_mem_src(&jinfo, img_data, img_size);

    if (jpeg_read_header(&jinfo, TRUE) != JPEG_HEADER_OK) goto bail;

    jinfo.dct_method = JDCT_FLOAT; // change this to JDCT_ISLOW on Android/iOS

    if (!jpeg_start_decompress(&jinfo)) goto bail;

    if (jinfo.num_components != 1 && jinfo.num_components != 3) goto bail;

    image->data = new (std::nothrow) unsigned char [jinfo.output_width * jinfo.output_height * jinfo.output_components];
    if (!image->data) goto bail;

    {
        JSAMPROW ptr = image->data;
        while (jinfo.output_scanline < jinfo.output_height)
        {
            if (jpeg_read_scanlines(&jinfo, &ptr, 1) != 1) goto bail;

            ptr += jinfo.output_width * jinfo.output_components;
        }
    }

    if (!jpeg_finish_decompress(&jinfo)) goto bail;

    image->bpp = jinfo.output_components;
    image->width = jinfo.output_width;
    image->height = jinfo.output_height;

    jpeg_destroy_decompress(&jinfo);

    return true;

bail:
    jpeg_destroy_decompress(&jinfo);
    if (image->data) delete [] data;

    return false;
}
于 2012-02-01T19:48:09.870 に答える