26

libjpeg を使用してファイルに書き込むこの関数を見つけました。

int write_jpeg_file( char *filename )
{
    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;

    /* this is a pointer to one row of image data */
    JSAMPROW row_pointer[1];
    FILE *outfile = fopen( filename, "wb" );

    if ( !outfile )
    {
        printf("Error opening output jpeg file %s\n!", filename );
        return -1;
    }
    cinfo.err = jpeg_std_error( &jerr );
    jpeg_create_compress(&cinfo);
    jpeg_stdio_dest(&cinfo, outfile);

    /* Setting the parameters of the output file here */
    cinfo.image_width = width;  
    cinfo.image_height = height;
    cinfo.input_components = bytes_per_pixel;
    cinfo.in_color_space = color_space;
    /* default compression parameters, we shouldn't be worried about these */
    jpeg_set_defaults( &cinfo );
    /* Now do the compression .. */
    jpeg_start_compress( &cinfo, TRUE );
    /* like reading a file, this time write one row at a time */
    while( cinfo.next_scanline < cinfo.image_height )
    {
        row_pointer[0] = &raw_image[ cinfo.next_scanline * cinfo.image_width *  cinfo.input_components];
        jpeg_write_scanlines( &cinfo, row_pointer, 1 );
    }
    /* similar to read file, clean up after we're done compressing */
    jpeg_finish_compress( &cinfo );
    jpeg_destroy_compress( &cinfo );
    fclose( outfile );
    /* success code is 1! */
    return 1;
}

時間を節約するために、ファイルに保存せずに、実際には jpeg 圧縮イメージをメモリ バッファに書き込む必要があります。誰かがそれを行う方法の例を教えてもらえますか?

私はしばらくウェブを検索してきましたが、ドキュメントがあったとしても非常にまれであり、例も入手するのが困難です.

4

5 に答える 5

18

独自の宛先マネージャーを非常に簡単に定義できます。には、バッファへのポインタ、バッファに残っているスペースのカウント、および関数への 3 つのポインタを含むへのjpeg_compress_structポインタが含まれています。jpeg_destination_mgr

init_destination (j_compress_ptr cinfo)
empty_output_buffer (j_compress_ptr cinfo)
term_destination (j_compress_ptr cinfo)

jpeg ライブラリへの最初の呼び出しを行う前に関数ポインターを入力し、それらの関数にバッファーを処理させる必要があります。予想される最大の出力よりも大きなバッファを作成すると、これは些細なことになります。init_destinationバッファ ポインタとカウントを埋めるだけで、何もempty_output_bufferterm_destinationません。

サンプルコードは次のとおりです。

std::vector<JOCTET> my_buffer;
#define BLOCK_SIZE 16384

void my_init_destination(j_compress_ptr cinfo)
{
    my_buffer.resize(BLOCK_SIZE);
    cinfo->dest->next_output_byte = &my_buffer[0];
    cinfo->dest->free_in_buffer = my_buffer.size();
}

boolean my_empty_output_buffer(j_compress_ptr cinfo)
{
    size_t oldsize = my_buffer.size();
    my_buffer.resize(oldsize + BLOCK_SIZE);
    cinfo->dest->next_output_byte = &my_buffer[oldsize];
    cinfo->dest->free_in_buffer = my_buffer.size() - oldsize;
    return true;
}

void my_term_destination(j_compress_ptr cinfo)
{
    my_buffer.resize(my_buffer.size() - cinfo->dest->free_in_buffer);
}

cinfo->dest->init_destination = &my_init_destination;
cinfo->dest->empty_output_buffer = &my_empty_output_buffer;
cinfo->dest->term_destination = &my_term_destination;
于 2010-12-30T04:10:04.690 に答える
0

FILEのようなオブジェクトを に渡すだけですjpeg_stdio_dest()

于 2010-12-30T03:02:08.287 に答える