tags:

views:

556

answers:

2

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.

A: 

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.

Tal Pressman
+1  A: 

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.

Adam Rosenfield