Convert bytestream to RGB using libjpeg

I intercept the packet and retrieve the payload. This payload is compressed by 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 the libjpeg library to extract image information. My question is how can I pass my pointer ( *payload ) to the libjpeg function to get the RGB values ​​and image size?

Thanks.

+4
source share
4 answers

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

  • Create a structure (similar to the my_source_mgr structure in jdatasrc.c ) that has an instance of struct jpeg_source_mgr as its first member. Add any other members that you think are necessary. Note that we essentially do manual inheritance - in C ++ we define a class that comes from jpeg_source_mgr , and the various function pointers declared in jpeg_source_mgr will instead be virtual functions. But we use C, so we need to make inheritance and polymorphism the hard way.
  • Allocate space for an instance of your structure by specifying the desired number of bytes and fill in your details.
  • Perform 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 a buffer in memory, these functions are likely to be very trivial.

  • Finally, initialize the function pointers in your jpeg_source_mgr element to point to these functions.

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

+3
source

Re: Adam Rosenfield answers: you do not need to implement anything: 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 the answer to a really old question, but today I have to do just that, and this page was the first or second rating in response to my web search.

+3
source

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

0
source

In recent libjpeg builds, jpeg_mem_src already implemented for you.

It is very easy to compile libjpeg for windows.

Download the source from the project web page
Extract all in one folder
Then open the Visual Studio 10 command prompt in this directory

Enter

 nmake /f makefile.vc setup-v10 

This will create a .sln file that opens in VS2010. It should compile from there.

After that, you can also debug the source of libjpeg.

0
source

All Articles