Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用jpeg_mem_dest使用libjpeg turbo压缩到内存_C++_Libjpeg_Libjpeg Turbo - Fatal编程技术网

C++ 如何使用jpeg_mem_dest使用libjpeg turbo压缩到内存

C++ 如何使用jpeg_mem_dest使用libjpeg turbo压缩到内存,c++,libjpeg,libjpeg-turbo,C++,Libjpeg,Libjpeg Turbo,我试过跟随,但似乎做不好。我有大约8MB的RBGX位图,可以使用libjpeg-turbo在内存中转换为jpeg。如果我使用jpeg\u stdio\u dest我可以将整个内容写入一个文件,然后将文件读回,就可以了。然而,试图使用jpeg\u mem\u dest一直是一个难题。我的设置与jpeg\u stdio\u dest相同,但使用mem似乎只分配一次4kb空间,然后再也不会分配更多空间 我找不到关于如何使用jpeg\u mem\u dest的更多说明的文档,我真的需要一些指导 void

我试过跟随,但似乎做不好。我有大约8MB的RBGX位图,可以使用libjpeg-turbo在内存中转换为jpeg。如果我使用jpeg\u stdio\u dest我可以将整个内容写入一个文件,然后将文件读回,就可以了。然而,试图使用jpeg\u mem\u dest一直是一个难题。我的设置与
jpeg\u stdio\u dest
相同,但使用
mem
似乎只分配一次4kb空间,然后再也不会分配更多空间

我找不到关于如何使用jpeg\u mem\u dest的更多说明的文档,我真的需要一些指导

void compress(std::vector<unsigned char>& input) {
  jpeg_compress_struct cinfo{};
  jpeg_error_mgr err{};

  cinfo.err = jpeg_std_error(&err);
  jpeg_create_compress(&cinfo);
#if 0 // using this with an open FILE* out works
  jpeg_stdio_dest(&cinfo, out);
#endif


  cinfo.image_width = kWidth; // constants defined somewhere
  cinfo.image_height = kHeight;
  cinfo.input_components = 4;
  cinfo.in_color_space = JCS_EXT_RGBX;

  // what's wrong with this?
  unsigned char* buf{};
  unsigned long buf_sz{};
  jpeg_mem_dest(&cinfo, &buf, &buf_sz);

  jpeg_set_defaults(&cinfo);
  jpeg_set_quality(&cinfo, 70, true);

  jpeg_start_compress(&cinfo, true);   
  while (cinfo.next_scanline < cinfo.image_height) {
    auto row = static_cast<JSAMPROW>(&input[cinfo.next_scanline  * 4  * kWidth]);
    jpeg_write_scanlines(&cinfo, &row, 1);
    // Always prints 4096, and buf never changes 
    std::cout << "buf_sz: " << buf_sz 
              << " buf: " << static_cast<void*>(buf) << '\n';
  }
  jpeg_finish_compress(&cinfo);

  // ...
  // in reality, return the compressed data
}
void压缩(标准::向量和输入){
jpeg_compress_struct cinfo{};
jpeg_error_mgr err{};
cinfo.err=jpeg\u std\u error(&err);
jpeg\u创建\u压缩(&cinfo);
#如果0//将其与打开的文件一起使用*out有效
jpeg标准目的地(和cinfo,输出);
#恩迪夫
cinfo.image_width=kWidth;//在某处定义的常量
cinfo.image_height=kHeight;
cinfo.input_组件=4;
cinfo.in_color_space=JCS_EXT_RGBX;
//这个怎么了?
无符号字符*buf{};
未签名的long buf_sz{};
jpeg_mem_dest(&cinfo,&buf,&buf_sz);
jpeg\u set\u默认值(&cinfo);
jpeg设置质量(&cinfo,70,真);
jpeg\u start\u compress(&cinfo,true);
while(cinfo.next\u扫描线是的,这一点都不直观。提出jpeg\u mem\u dest()的程序员tweak没有太多的选择,扩展一个现有的api并不是那么容易,因为它最初并不支持某个功能。完全不明显的是,在调用jpeg\u finish\u compress()之后,变量才会更新。库中的相关代码是:

METHODDEF(void)
term_mem_destination (j_compress_ptr cinfo)
{
  my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest;

  *dest->outbuffer = dest->buffer;
  *dest->outsize = (unsigned long)(dest->bufsize - dest->pub.free_in_buffer);
}
请注意“term”一词。此函数通过函数指针间接调用:

GLOBAL(void)
jpeg_finish_compress (j_compress_ptr cinfo)
{
  //...
  /* Write EOI, do final cleanup */
  (*cinfo->marker->write_file_trailer) (cinfo);
  (*cinfo->dest->term_destination) (cinfo);
  //...
}
对此您无能为力。只需调整std::cout代码,在循环之后移动它,以适应库的工作方式

注意这个函数的另一个细节,也是不明显的。你必须释放()它创建的缓冲区。在提供的cjpeg.c示例程序中,main()的末尾可见