c++ - 如何在有和没有 std::vector 的情况下正确使用 jpeg_mem_dest

标签 c++ c c++11 jpeg libjpeg

我在搜索如何写入缓冲区而不是文件时遇到了这个问题,但我找不到一个很好的例子。

这就是 jpeglib.h 所说的,但我不确定如何使用它。

/* Data source and destination managers: memory buffers. */
EXTERN(void) jpeg_mem_dest JPP((j_compress_ptr cinfo,
                   unsigned char ** outbuffer,
                   unsigned long * outsize));

example.c 中,我们在第 2 步 中设置了数据目的地,那么如何使用它在这里如果我们已经有一个 std::vector<unsigned char> buffer保存 RGB 值。

/* Step 2: specify data destination (eg, a file) */
/* Note: steps 2 and 3 can be done in either order. */
    jpeg_mem_dest(&cinfo, /*WHAT GOES HERE*/, /*WHAT GOES HERE*/);

完成此操作后,我们对第 5 步 进行哪些更改,以便我们以相同 编写> 缓冲我们解压缩的 RGB 值在哪里?

/* Step 5: while (scan lines remain to be written) */
/*           jpeg_write_scanlines(...); */

我错过了什么吗?非常感谢您的帮助,在此先感谢您。

最佳答案

假设您已经继续前进,但以下内容适用于其他正在寻找的人。

struct Result{
  unsigned char* buf;
  unsigned long size;
}
Result result;
// ..Here allocate the buffer and initialize the size
// before using in jpeg_mem_dest.  Or it will allocate for you
// and you have to clean up.
jpeg_mem_dest(&cinfo, &result->buf, &result->size);
// use buffer
free(result->buf); // <- of course depending on the way the buffer was allocated 

如果您使用的是 std::vector,您可以使用 &myVec[0] 或 myVec.data() 之类的东西来传递原始缓冲区,因为 c++11(但我想我可能遇到了麻烦).一种解决方法是使用原始缓冲区,如果对性能要求不高,则只复制/插入。

// Copy based.  Could also just buffer.insert if not preallocated using resize.
std::vector<unsigned char> buffer;
budder.resize(result->size())
std::copy(result->buf
        , result->buf + result->size
        , buffer.begin());

关于c++ - 如何在有和没有 std::vector 的情况下正确使用 jpeg_mem_dest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39180492/

相关文章:

c++ - 对 `kill' 的 undefined reference

c - 有什么办法可以让这个C switch case更简单吗?

c - 更改字符串字符时出现段错误(核心已转储)

c++ - 使用 unique_ptr 离开范围时堆损坏

c++ - 未检测到发射信号

c++ - 斧头错误 : You cannot access the cpp package while targeting cross (for cpp. 库)

c++ - C++ 中有类似 numpy.logspace 的东西吗?

c++ - 对 map 容器使用remove_if

C++/OpenGL VAO 问题

C++ std::forward<T> 与 static_cast<T>