C++ libjpeg版本。6b jpeg_stdio_src vs jpeg_mem_src

C++ libjpeg版本。6b jpeg_stdio_src vs jpeg_mem_src,c++,libjpeg,C++,Libjpeg,我使用的是Libjpeg版本6b。在版本8中,它们有一个很好的函数,可以从内存中读取数据,称为jpeg\u mem\u src(…),不幸的是,这是一个版本。6b没有此功能 我可以用什么直接从内存中读取压缩数据?我看到的是从硬盘读取的jpeg\u stdio\u src。编写自己的 /* Read JPEG image from a memory segment */ static void init_source (j_decompress_ptr cinfo) {} static boole

我使用的是Libjpeg版本6b。在版本8中,它们有一个很好的函数,可以从内存中读取数据,称为
jpeg\u mem\u src(…)
,不幸的是,这是一个版本。6b没有此功能

我可以用什么直接从内存中读取压缩数据?我看到的是从硬盘读取的jpeg\u stdio\u src。

编写自己的

/* Read JPEG image from a memory segment */
static void init_source (j_decompress_ptr cinfo) {}
static boolean fill_input_buffer (j_decompress_ptr cinfo)
{
    ERREXIT(cinfo, JERR_INPUT_EMPTY);
return TRUE;
}
static void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
{
    struct jpeg_source_mgr* src = (struct jpeg_source_mgr*) cinfo->src;

    if (num_bytes > 0) {
        src->next_input_byte += (size_t) num_bytes;
        src->bytes_in_buffer -= (size_t) num_bytes;
    }
}
static void term_source (j_decompress_ptr cinfo) {}
static void jpeg_mem_src (j_decompress_ptr cinfo, void* buffer, long nbytes)
{
    struct jpeg_source_mgr* src;

    if (cinfo->src == NULL) {   /* first time for this JPEG object? */
        cinfo->src = (struct jpeg_source_mgr *)
            (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
            SIZEOF(struct jpeg_source_mgr));
    }

    src = (struct jpeg_source_mgr*) cinfo->src;
    src->init_source = init_source;
    src->fill_input_buffer = fill_input_buffer;
    src->skip_input_data = skip_input_data;
    src->resync_to_restart = jpeg_resync_to_restart; /* use default method */
    src->term_source = term_source;
    src->bytes_in_buffer = nbytes;
    src->next_input_byte = (JOCTET*)buffer;
}
然后使用它:

...
    /* Step 2: specify data source (eg, a file) */
    jpeg_mem_src(&dinfo, buffer, nbytes);
...

其中buffer是指向包含压缩jpeg图像的内存块的指针,nbytes是该缓冲区的长度。

或者您也可以尝试使用GNU的fmemopen()函数,该函数应在stdio.h头文件中声明

FILE * source = fmemopen(inbuffer, inlength, "rb");
if (source == NULL)
{
    fprintf(stderr, "Calling fmemopen() has failed.\n");
    exit(1);
}

// ...

jpeg_stdio_src(&cinfo, source);

// ...

fclose(source);

回答贫穷的s093294,他已经等待答案一年多了。我不能发表评论,所以创造一个新的答案是唯一的方法


ERREXIT是libjpeg中的宏。包括jerror.h,您就都准备好了。

什么是退出?复制粘贴了此代码,但未为我定义此代码。