【问题标题】:Libjpeg write image to memory dataLibjpeg 将图像写入内存数据
【发布时间】:2014-10-01 14:27:33
【问题描述】:

我想使用 libjpeg 库将图像保存到内存(矢量)中。 我发现那里有功能:

init_destination
empty_output_buffer 
term_destination

我的问题是如何在并行程序中安全正确地执行此操作?我的函数可能从不同的线程执行。 我想在 c++ 和 Visual Studio 2010 中完成。

其他具有回调功能的库总是有额外的函数参数来存储一些额外的数据。 我看不到任何添加任何附加参数的方法,例如指向我的本地向量实例的指针。

编辑: mmy 问题的最佳解决方案在这里:http://www.christian-etter.de/?cat=48

【问题讨论】:

标签: c++ libjpeg


【解决方案1】:

这里描述了很好的解决方案:http://www.christian-etter.de/?cat=48

typedef struct _jpeg_destination_mem_mgr
{
    jpeg_destination_mgr mgr;
    std::vector<unsigned char> data;
} jpeg_destination_mem_mgr;

初始化:

static void mem_init_destination( j_compress_ptr cinfo )
{
    jpeg_destination_mem_mgr* dst = (jpeg_destination_mem_mgr*)cinfo->dest;
    dst->data.resize( JPEG_MEM_DST_MGR_BUFFER_SIZE );
    cinfo->dest->next_output_byte = dst->data.data();
    cinfo->dest->free_in_buffer = dst->data.size();
}

完成后,我们需要将缓冲区大小调整为实际大小:

static void mem_term_destination( j_compress_ptr cinfo )
{
    jpeg_destination_mem_mgr* dst = (jpeg_destination_mem_mgr*)cinfo->dest;
    dst->data.resize( dst->data.size() - cinfo->dest->free_in_buffer );
}

当缓冲区太小时,我们需要增加它:

static boolean mem_empty_output_buffer( j_compress_ptr cinfo )
{
    jpeg_destination_mem_mgr* dst = (jpeg_destination_mem_mgr*)cinfo->dest;
    size_t oldsize = dst->data.size();
    dst->data.resize( oldsize + JPEG_MEM_DST_MGR_BUFFER_SIZE );
    cinfo->dest->next_output_byte = dst->data.data() + oldsize;
    cinfo->dest->free_in_buffer = JPEG_MEM_DST_MGR_BUFFER_SIZE;
    return true;
}

回调配置:

static void jpeg_mem_dest( j_compress_ptr cinfo, jpeg_destination_mem_mgr * dst )
{
    cinfo->dest = (jpeg_destination_mgr*)dst;
    cinfo->dest->init_destination = mem_init_destination;
    cinfo->dest->term_destination = mem_term_destination;
    cinfo->dest->empty_output_buffer = mem_empty_output_buffer;
}

及示例用法:

jpeg_destination_mem_mgr dst_mem;
jpeg_compress_struct_wrapper cinfo;
j_compress_ptr pcinfo = cinfo;
jpeg_mem_dest( cinfo, &dst_mem);

【讨论】:

    猜你喜欢
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    相关资源
    最近更新 更多