如何使用 libjpeg 从 std::istream 读取 JPEG?

发布于 2024-11-15 12:39:47 字数 163 浏览 3 评论 0原文

libjpeg 可以从 FILE* 或缓冲区读取 JPEG 数据。我的数据来自 std::istream。我可以将整个 std::istream 读入缓冲区以与 libjpeg 一起使用,但如果可能的话,我宁愿让 libjpeg 直接从 std::istream 读取。这怎么能做到呢?

libjpeg can read JPEG data from a FILE* or a buffer. My data is coming from a std::istream. I could read the entire std::istream into a buffer to use with libjpeg, but I'd rather have libjpeg read directly from the std::istream if possible. How can this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

血之狂魔 2024-11-22 12:39:47

您只需要提供 istream 周围的包装器。例如,定义一个结构体

struct JpegStream {
  jpeg_source_mgr pub;
  std::istream* stream;
  byte buffer [4096];
}

然后您需要四种方法来操作流:

void init_source (j_decompress_ptr cinfo)
{
    auto src = (JpegStream*)(cinfo->src);
    src->stream-> // seek to 0 here
}

boolean fill_buffer (j_decompress_ptr cinfo)
{
    // Read to buffer
    JpegStream* src = // as above
    src->pub.next_input_byte = src->buffer;
    src->pub.bytes_in_buffer = // How many yo could read

    return eof() ? FALSE : TRUE;
}

void skip (j_decompress_ptr cinfo, long count)
{
   // Seek by count bytes forward
   // Make sure you know how much you have cached and subtract that
   // set bytes_in_buffer and next_input_byte
}

void term (j_decompress_ptr cinfo)
{
    // Close the stream, can be nop
}

以及一种将它们绑定到 JPEG 解压缩信息结构的方法:

void make_stream (j_decompress_ptr cinfo, std::istream* in)
{
    JpegStream * src;

    /* The source object and input buffer are made permanent so that a series
    * of JPEG images can be read from the same file by calling jpeg_stdio_src
    * only before the first one.  (If we discarded the buffer at the end of
    * one image, we'd likely lose the start of the next one.)
    * This makes it unsafe to use this manager and a different source
    * manager serially with the same JPEG object.  Caveat programmer.
    */
    if (cinfo->src == NULL)
    {   
            /* first time for this JPEG object? */
            cinfo->src = (struct jpeg_source_mgr *)
        (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, POOL_PERMANENT,   sizeof(JpegStream));
            src = reinterpret_cast<JpegStream*> (cinfo->src);
    }

    src = reinterpret_cast<JpegStream*> (cinfo->src);
    src->pub.init_source = init_source;
    src->pub.fill_input_buffer = fill_buffer;
    src->pub.skip_input_data = skip;
    src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
    src->pub.term_source = term;
    src->stream = in;
    src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
    src->pub.next_input_byte = NULL; /* until buffer loaded */
}

在调用jpeg_create_decompress之后,调用您的make_stream 功能。

You just need to provide wrappers around your istream. Define a struct, for instance

struct JpegStream {
  jpeg_source_mgr pub;
  std::istream* stream;
  byte buffer [4096];
}

Then you need four methods to operate on the stream:

void init_source (j_decompress_ptr cinfo)
{
    auto src = (JpegStream*)(cinfo->src);
    src->stream-> // seek to 0 here
}

boolean fill_buffer (j_decompress_ptr cinfo)
{
    // Read to buffer
    JpegStream* src = // as above
    src->pub.next_input_byte = src->buffer;
    src->pub.bytes_in_buffer = // How many yo could read

    return eof() ? FALSE : TRUE;
}

void skip (j_decompress_ptr cinfo, long count)
{
   // Seek by count bytes forward
   // Make sure you know how much you have cached and subtract that
   // set bytes_in_buffer and next_input_byte
}

void term (j_decompress_ptr cinfo)
{
    // Close the stream, can be nop
}

and one method to bind them to the JPEG decompression info structure:

void make_stream (j_decompress_ptr cinfo, std::istream* in)
{
    JpegStream * src;

    /* The source object and input buffer are made permanent so that a series
    * of JPEG images can be read from the same file by calling jpeg_stdio_src
    * only before the first one.  (If we discarded the buffer at the end of
    * one image, we'd likely lose the start of the next one.)
    * This makes it unsafe to use this manager and a different source
    * manager serially with the same JPEG object.  Caveat programmer.
    */
    if (cinfo->src == NULL)
    {   
            /* first time for this JPEG object? */
            cinfo->src = (struct jpeg_source_mgr *)
        (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, POOL_PERMANENT,   sizeof(JpegStream));
            src = reinterpret_cast<JpegStream*> (cinfo->src);
    }

    src = reinterpret_cast<JpegStream*> (cinfo->src);
    src->pub.init_source = init_source;
    src->pub.fill_input_buffer = fill_buffer;
    src->pub.skip_input_data = skip;
    src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
    src->pub.term_source = term;
    src->stream = in;
    src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
    src->pub.next_input_byte = NULL; /* until buffer loaded */
}

After calling jpeg_create_decompress, call your make_stream function.

送你一个梦 2024-11-22 12:39:47

扩展 Anteru 的出色答案(大约 13 年前!),我根据它生成了以下代码。它通过使用 JpegStream 结构中的静态成员函数将所有内容包装在一起,并完全实现所有功能(尽管我还没有看到 skip 被调用,所以我不能 100% 确定它可以正常工作)。

#define STREAM_BUFFER_SIZE 4096

struct JpegStream {
    jpeg_source_mgr mgr;
    std::istream* stream;
    unsigned char buf[STREAM_BUFFER_SIZE];
    size_t ofs;
    static void initSource(j_decompress_ptr cinfo)
    {
        JpegStream* src = reinterpret_cast<JpegStream*>(cinfo->src);
        src->stream->seekg(0);
        src->ofs = 0;
    }

    static boolean fillBuffer(j_decompress_ptr cinfo)
    {
        // Read to buffer
        JpegStream* src = reinterpret_cast<JpegStream*>(cinfo->src);
        std::istream& stream = *src->stream;
        stream.read((char*)src->buf, STREAM_BUFFER_SIZE);
        src->mgr.next_input_byte = src->buf;
        src->ofs = 0;
        src->mgr.bytes_in_buffer = stream.gcount();
        return stream ? TRUE : FALSE;
    }

    static void skip(j_decompress_ptr cinfo, long count)
    {
        // Seek by count bytes forward
        JpegStream* src = reinterpret_cast<JpegStream*>(cinfo->src);
        if (src->ofs + count < STREAM_BUFFER_SIZE)
        {
            src->ofs += count;
            // Make sure you know how much you have cached and subtract that
            // set bytes_in_buffer and next_input_byte
            src->mgr.next_input_byte = &src->buf[src->ofs];
            src->mgr.bytes_in_buffer = STREAM_BUFFER_SIZE - src->ofs;
        }
        else
        {
            std::istream& stream = *src->stream;
            stream.seekg(count - STREAM_BUFFER_SIZE);
            src->mgr.next_input_byte = NULL;
            src->mgr.bytes_in_buffer = 0;
        }
    }

    static void term(j_decompress_ptr /*cinfo*/)
    {
        // Close the stream, be we ignore.
    }
    
    static void attach(j_decompress_ptr cinfo, std::istream* stream)
    {
        /* The source object and input buffer are made permanent so that a series
         * of JPEG images can be read from the same file by calling jpeg_stdio_src
         * only before the first one.  (If we discarded the buffer at the end of
         * one image, we'd likely lose the start of the next one.)
         * This makes it unsafe to use this manager and a different source
         * manager serially with the same JPEG object.  Caveat programmer.
         */
        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(JpegStream));
        }
        JpegStream* src = reinterpret_cast<JpegStream*>(cinfo->src);
        src->mgr.init_source = initSource;
        src->mgr.fill_input_buffer = fillBuffer;
        src->mgr.skip_input_data = skip;
        src->mgr.resync_to_restart = jpeg_resync_to_restart; /* use default method */
        src->mgr.term_source = term;
        src->stream = stream;
        src->ofs = 0;
        src->mgr.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
        src->mgr.next_input_byte = NULL; /* until buffer loaded */
    }
};

使用它很简单:

    std::istream stream;
    struct jpeg_decompress_struct cinfo;
    // Set up cinfo and stream
    JpegStream::attach(&cinfo, &stream);

Expanding on Anteru's excellent answer (from almost 13 years ago!), I produced the following code based on it. It wraps everything up together by using static member functions in the JpegStream struct and fully implements all the functionality (although I have not yet seen skip get called, so I am not 100% sure that works properly).

#define STREAM_BUFFER_SIZE 4096

struct JpegStream {
    jpeg_source_mgr mgr;
    std::istream* stream;
    unsigned char buf[STREAM_BUFFER_SIZE];
    size_t ofs;
    static void initSource(j_decompress_ptr cinfo)
    {
        JpegStream* src = reinterpret_cast<JpegStream*>(cinfo->src);
        src->stream->seekg(0);
        src->ofs = 0;
    }

    static boolean fillBuffer(j_decompress_ptr cinfo)
    {
        // Read to buffer
        JpegStream* src = reinterpret_cast<JpegStream*>(cinfo->src);
        std::istream& stream = *src->stream;
        stream.read((char*)src->buf, STREAM_BUFFER_SIZE);
        src->mgr.next_input_byte = src->buf;
        src->ofs = 0;
        src->mgr.bytes_in_buffer = stream.gcount();
        return stream ? TRUE : FALSE;
    }

    static void skip(j_decompress_ptr cinfo, long count)
    {
        // Seek by count bytes forward
        JpegStream* src = reinterpret_cast<JpegStream*>(cinfo->src);
        if (src->ofs + count < STREAM_BUFFER_SIZE)
        {
            src->ofs += count;
            // Make sure you know how much you have cached and subtract that
            // set bytes_in_buffer and next_input_byte
            src->mgr.next_input_byte = &src->buf[src->ofs];
            src->mgr.bytes_in_buffer = STREAM_BUFFER_SIZE - src->ofs;
        }
        else
        {
            std::istream& stream = *src->stream;
            stream.seekg(count - STREAM_BUFFER_SIZE);
            src->mgr.next_input_byte = NULL;
            src->mgr.bytes_in_buffer = 0;
        }
    }

    static void term(j_decompress_ptr /*cinfo*/)
    {
        // Close the stream, be we ignore.
    }
    
    static void attach(j_decompress_ptr cinfo, std::istream* stream)
    {
        /* The source object and input buffer are made permanent so that a series
         * of JPEG images can be read from the same file by calling jpeg_stdio_src
         * only before the first one.  (If we discarded the buffer at the end of
         * one image, we'd likely lose the start of the next one.)
         * This makes it unsafe to use this manager and a different source
         * manager serially with the same JPEG object.  Caveat programmer.
         */
        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(JpegStream));
        }
        JpegStream* src = reinterpret_cast<JpegStream*>(cinfo->src);
        src->mgr.init_source = initSource;
        src->mgr.fill_input_buffer = fillBuffer;
        src->mgr.skip_input_data = skip;
        src->mgr.resync_to_restart = jpeg_resync_to_restart; /* use default method */
        src->mgr.term_source = term;
        src->stream = stream;
        src->ofs = 0;
        src->mgr.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
        src->mgr.next_input_byte = NULL; /* until buffer loaded */
    }
};

Using it is easy:

    std::istream stream;
    struct jpeg_decompress_struct cinfo;
    // Set up cinfo and stream
    JpegStream::attach(&cinfo, &stream);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文