Libjpeg将图像写入内存数据
Libjpeg write image to memory data
我想使用libjpeg库将图像保存到内存(矢量)中。我发现了以下功能:
init_destination
empty_output_buffer
term_destination
我的问题是如何在并行程序中安全、正确地完成它?我的函数可以从不同的线程执行。我想在c++和Visual Studio 2010中实现它。
其他具有回调功能的库总是有额外的函数参数来存储一些额外的数据。我看不出有任何方法可以添加任何额外的参数,例如指向向量的本地实例的指针。
编辑:mmy问题的最佳解决方案如下:http://www.christian-etter.de/?cat=48
这里描述了不错的解决方案: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);
相关文章:
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 如何在 malloc 内存中初始化非 POD 数据
- 如何使用 MPI 的远程内存访问 (RMA) 功能并行化数据聚合?
- 为什么字符串的 move() 会改变内存中底层数据的位置?
- C++,您能否设计一种数据结构,将指针保存在连续内存中并且不会使它们失效?
- 存储在哪个内存段(代码/数据段)类(员工)中?
- 基于浅树的数据结构的内存分配器,用于频繁分配和解除分配
- 获取 R 数据帧的内存地址
- 将数据存储在内存中以供以后访问
- 整数数据如何以位为单位存储在内存中?不是右对齐吗?
- 数据在内存 c++ 中丢失
- 我可以使用哪种数据结构来释放连续内存中的内存?
- 使用 delete [] 运算符取消分配类中数据成员的内存
- 为什么 std::set 容器使用的内存比其数据大小多得多?
- 如果一个变量在它之前释放了另一个(相同的数据类型)变量,如何将其分配给内存?
- 内存映射C++中的流数据
- 如何返回定义良好的内存部分?例如来自图像数据的像素的颜色值
- C++来自课堂的内存数据?
- Libjpeg将图像写入内存数据
- 将内存数据PTR映射到qt输入字段