使用 libjpeg 将字节流转换为 RGB

发布于 2024-07-18 23:39:53 字数 225 浏览 7 评论 0原文

我拦截一个数据包并提取有效负载。 该有效负载是压缩的 jpeg 字节流数据(例如,该数据被分配给unsigned char *payload)。 我知道如果我有一个 FILE 指针,那么我可以使用 libjpeg 库来提取图像信息。 我的问题是有没有办法将我的指针(*payload)传递给 libjpeg 函数来获取 RGB 值和图像的尺寸?

谢谢。

I intercept a packet and extract the payload. This payload is compressed jpeg bytestream data (for example this data is assigned to unsigned char *payload). I know that if I have a FILE pointer, then I can use libjpeg library to extract image information. My question is there a way to pass my pointer(*payload) to the libjpeg functions to get the RGB values and the dimension of the image?

Thank you.

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

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

发布评论

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

评论(4

顾北清歌寒 2024-07-25 23:39:53

回复:Adam Rosenfield 的回答,您不必自己实现任何内容:jpeg_mem_src() 将执行您想要的操作:

jpeg_mem_src(&cinfo, payload, payload_len);

您必须将整个图像存储在内存中(并知道其长度)。

obStackoverflow:我知道,这是一个非常古老的问题的答案,但今天我必须这样做,并且该页面是我的网络搜索响应中排名第一或第二的页面。

Re: Adam Rosenfield's answer, you don't have to implement anything yourself: jpeg_mem_src() will do what you want:

jpeg_mem_src(&cinfo, payload, payload_len);

You must have the entire image stored in memory (and know its length).

obStackoverflow: I know, an answer to a really old question, but today I have to do just this and this page was the first or second ranked in response to my web search.

ζ澈沫 2024-07-25 23:39:53

要添加到 Tal 的答案,你应该做的是查看 jdatasrc.cjpeg_stdio_src() 函数的实现。 该函数的目的是初始化数据源对象,即cinfo->src。 因此,您应该执行以下操作:

  1. 创建一个结构(类似于jdatasrc.c中的my_source_mgr结构),其第一个成员为结构jpeg_source_mgr。 添加您认为必要的任何其他成员。 请注意,我们本质上是在进行手动继承 - 在 C++ 中,我们将定义一个派生自 jpeg_source_mgr 的类,并且在 jpeg_source_mgr 中声明的各种函数指针将改为虚拟的功能。 但是,我们使用的是 C,因此我们必须以困难的方式进行继承和多态性。

  2. 为结构实例分配空间,确保分配正确的字节数,并填写数据成员。

  3. 实现以下五个功能:

    • void init_source(j_decompress_ptr)
    • 布尔值 fill_input_buffer(j_decompress_ptr)
    • voidskip_input_data(j_decompress_ptr, long)
    • 布尔 resync_to_start(j_decompress_ptr, int)
    • void term_source(j_decompress_ptr)

    请注意,对于内存缓冲区,这些函数可能非常微不足道。

  4. 最后,初始化 jpeg_source_mgr 成员中的函数指针以指向这些函数。

完成此操作后,您就可以使用它而不是 jpeg_stdio_src() 来初始化数据源管理器,然后您就可以像从文件系统中解码文件一样进行操作了。

To add to Tal's answer, what you should do is take a look at the implementation of the jpeg_stdio_src() function in jdatasrc.c. The purpose of that function is to initialize the data source object, namely cinfo->src. So, what you should do is the following:

  1. Create a structure (similar to the my_source_mgr structure in jdatasrc.c) which has as its first member an instance of struct jpeg_source_mgr. Add any other members that you feel are necessary. Note that we're essentially doing manual inheritance -- in C++, we would define a class that derives from jpeg_source_mgr, and the various function pointers declared in jpeg_source_mgr would instead be virtual functions. But, we're using C, so we have to do inheritance and polymorphism the hard way.

  2. Allocate space for an instance of your structure, making sure to allocate the right number of bytes, and fill out your data members.

  3. Implement the following five functions:

    • void init_source(j_decompress_ptr)
    • boolean fill_input_buffer(j_decompress_ptr)
    • void skip_input_data(j_decompress_ptr, long)
    • boolean resync_to_start(j_decompress_ptr, int)
    • void term_source(j_decompress_ptr)

    Note that for an in-memory buffer, these functions will probably be very trivial.

  4. Finally, initialize the function pointers in your jpeg_source_mgr member to point to those functions.

With this in place, you can then use this instead of jpeg_stdio_src() to initialize the data source manager, and then you should be good to go as if you were decoding a file from the file system.

墨离汐 2024-07-25 23:39:53

libjpeg 具有输入/输出管理器,可以扩展为使用原始内存缓冲区或任何其他形式的 I/O,但据我所知,该库的版本实际上仅实现了 FILE*。

libjpeg has input/output managers that can be extended to use raw memory buffers or any other form of I/O you have, but AFAIK only FILE* is actually implemented in the library's release.

刘备忘录 2024-07-25 23:39:53

在 libjpeg 的最新版本中,jpeg_mem_src 已经为您实现了。

为 Windows 编译 libjpeg 非常容易。

从项目网页下载源代码
将所有内容提取到一个文件夹中
然后打开 Visual Studio 10 命令提示符到该目录,

键入

nmake /f makefile.vc setup-v10

这将创建.sln 文件将在 VS2010 中打开。 它应该从那里编译。

之后您还可以调试 libjpeg 源。

In recent builds of libjpeg jpeg_mem_src is already implemented for you.

It is very easy to compile libjpeg for windows.

Download the source from the project webpage
Extract everything in a single folder
Then open a Visual Studio 10 command prompt to that directory

Type

nmake /f makefile.vc setup-v10

This will create a .sln file which will open in VS2010. It should compile from there.

You may also debug libjpeg source afterwards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文