dave
dave

Reputation:

converting bytestream to RGB using libjpeg

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.

Upvotes: 4

Views: 2684

Answers (4)

Eric
Eric

Reputation: 19873

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.

Upvotes: 0

dland
dland

Reputation: 4419

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.

Upvotes: 4

Adam Rosenfield
Adam Rosenfield

Reputation: 400462

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.

Upvotes: 3

Tal Pressman
Tal Pressman

Reputation: 7317

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.

Upvotes: 0

Related Questions