2

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

1 Answers1

5

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!

Osyotr
  • 784
  • 1
  • 7
  • 20
Mr Fooz
  • 109,094
  • 6
  • 73
  • 101
  • This does not work. Please, provide a working version of that code. – Edu Felipe Feb 12 '10 at 13:16
  • 1
    @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 Feb 13 '10 at 06:03
  • 1
    @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 Feb 19 '10 at 17:48
  • 1
    @Edu: thanks...for some reason I was thinking those were built-in. I've added the implementation I use. – Mr Fooz Feb 23 '10 at 01:51
  • @mr-fooz SO is not letting me upvote your question, but it definitely deserves it! – Edu Felipe Mar 01 '10 at 17:49