views:

1210

answers:

2

Hello all,

I have JPEG compressed byte stream stored in a variable called "Image" and I want to convert this byte stream to RGB.

Eg: unsigned char *Image;

My question is: Is there any way to pass "Image" to jpeg_stdio_src() to get the RGB color values?

Can anyone tell me how I could use jpeglib library to get RGB from byte stream "Image"?

Thank you

+2  A: 

Try something like this. This uses an object currImage to store the results (not declared here).

typedef struct {
  struct jpeg_source_mgr pub;   // public fields

  JOCTET * buffer;              // start of buffer
  boolean start_of_file;        // have we gotten any data yet?
} my_source_mgr;

typedef my_source_mgr * my_src_ptr;

static void jpg_memInitSource(j_decompress_ptr cinfo)
{
  my_src_ptr src = (my_src_ptr) cinfo->src;
  src->start_of_file = true;
}

static boolean jpg_memFillInputBuffer(j_decompress_ptr cinfo)
{
  my_src_ptr src = (my_src_ptr) cinfo->src;
  src->start_of_file = FALSE;
  return TRUE;
}

static void jpg_memSkipInputData(j_decompress_ptr cinfo, long num_bytes)
{
  my_src_ptr src = (my_src_ptr) cinfo->src;
  if (num_bytes > 0) {
    src->pub.next_input_byte += (size_t) num_bytes;
    src->pub.bytes_in_buffer -= (size_t) num_bytes;
  }
}

static void jpg_memTermSource(j_decompress_ptr cinfo)
{
  // no work necessary here
}

void decompressJpeg(uint8 const *compressed, size_t len)
{
  struct jpeg_decompress_struct cinfo;
  struct jpeg_error_mgr         jerr;
  my_src_ptr src;

  // ToDo: change error handling to work with our return codes
  cinfo.err = jpeg_std_error(&jerr);
  jpeg_create_decompress(&cinfo);

  cinfo.src = (struct jpeg_source_mgr *)
    (*cinfo.mem->alloc_small) ((j_common_ptr) &cinfo, JPOOL_PERMANENT,
    sizeof(my_source_mgr));
  src = (my_src_ptr) cinfo.src;
  src->buffer = (JOCTET *)compressed;

  src->pub.init_source = jpg_memInitSource;
  src->pub.fill_input_buffer = jpg_memFillInputBuffer;
  src->pub.skip_input_data = jpg_memSkipInputData;
  src->pub.resync_to_restart = jpeg_resync_to_restart;
  src->pub.term_source = jpg_memTermSource;
  src->pub.bytes_in_buffer = len;
  src->pub.next_input_byte = compressed;

  jpeg_read_header(&cinfo, true);
  jpeg_start_decompress(&cinfo);

  // check: cinfo.out_color_space == JCS_RGB

  currImage.setSize(
    (int)cinfo.output_width, (int)cinfo.output_height));

  size_t w = currImage.getWidth() * 3;
  uint8 *p = (uint8*)currImage.getPixels();
  while (cinfo.output_scanline < cinfo.output_height) {
    jpeg_read_scanlines(&cinfo, &p, 1);
    p += w;
  }

  jpeg_finish_decompress(&cinfo); // Do this even under error conditions
  jpeg_destroy_decompress(&cinfo); // Do this even under error conditions
}

EDIT: Added some of the missing support code. Thanks, Edu Filipe!

Mr Fooz
This does not work. Please, provide a working version of that code.
Edu Felipe
@Edu: could you be a little more specific? You'll need to #include the jpeglib and replace any of the currImage code with whatever image representation you prefer. Here, I use an object that stores images as packed RGB pixels with no stride adjustment for better memory alignment. You should use whatever representation makes sense for your application: there is no single standard in C or C++.
Mr Fooz
@mr-fooz Actually the tricky part, that's implementing the callback functions is absent from your code. Things like jpg_memFillInputBuffer, jpg_memSkipInputData and jpg_memTermSource.
Edu Felipe
@Edu: thanks...for some reason I was thinking those were built-in. I've added the implementation I use.
Mr Fooz
@mr-fooz SO is not letting me upvote your question, but it definitely deserves it!
Edu Felipe
@Edu: thanks for helping to point out what I was missing.
Mr Fooz
A: 

I am getting the errors below.

classifier.c:190: error: ‘my_source_mgr’ has no member named ‘buffer’

classifier.c:192: error: ‘jpg_memInitSource’ undeclared (first use in this function)

classifier.c:192: error: (Each undeclared identifier is reported only once

classifier.c:192: error: for each function it appears in.)

classifier.c:193: error: ‘jpg_memFillInputBuffer’ undeclared (first use in this function)

classifier.c:194: error: ‘jpg_memSkipInputData’ undeclared (first use in this function)

classifier.c:196: error: ‘jpg_memTermSource’ undeclared (first use in this function)

classifier.c:228: error: ‘row_pointer’ undeclared (first use in this function)

classifier.c:234: error: ‘location’ undeclared (first use in this function)

Do I have to include some other library to fix this issue other than jpeglib.h?