c++ - 使用libjpeg写入内存缓冲区而不是文件?

标签 c++ c libjpeg

我发现了这个使用 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 压缩图像写入内存缓冲区,而不将其保存到文件中,以节省时间。有人可以举个例子吗?

我已经在网上搜索了一段时间,但是如果有的话,文档非常稀少,而且示例也很难找到。

最佳答案

您可以很容易地定义自己的目的地管理器。 jpeg_compress_struct 包含指向 jpeg_destination_mgr 的指针,其中包含指向缓冲区的指针、缓冲区中剩余空间的计数和 3 个函数指针:

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;

关于c++ - 使用libjpeg写入内存缓冲区而不是文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4559648/

相关文章:

c - 在 yacc 中解析赋值语句时出现语法错误

c - C 中的文档分类工具 - 嵌套函数/范围中的编译错误(可能是)

.net - libjpeg 和 .Net jpeg 编解码器在单色数据上真的有很大不同吗?

c - epoll ET,我应该在监听套接字上订阅什么事件?

c++ - 错误 : unitialized member with const type c++

c++ - 派生函数中的 NULL 实现

c++ - MSVCP110.dll 丢失错误

ios - JPEG 保存到 ios 照片库后 DCT 系数发生变化

image-processing - 在变换域中将 JPEG 快速缩放到一半或四分之一

c++ - 如何在 gcc 中为未使用的 lambda 表达式启用警告?