Tuesday, July 10, 2012

Decompressing JPEGs in RAM

As part of my summer computer vision job, I need to write a piece of software that will decompress JPEGs into bitmaps, then process and analyze the images to find and track the hockey players in the scene. Decompressing JPEGs really isn't that difficult; I've been doing all my testing so far using the OpenCV image loading mechanism, and that has been working fine.

The problem is that once I deploy the final system, I'm not going to be able to use the typical JPEG decompression routines, because they expect you to pass them the file path of the JPEG - fine when you are trying to decompress a JPEG file, but I need to decompress JPEGs which are entirely stored in memory buffers, and which are hopefully never even written to disk (due to some fancy mmap trickery).

Luckily, the Independent JPEG Group has been maintaining the de facto JPEG library (jpeglib) since approximately the beginning of time, so this shouldn't be too large of an issue.  Unfortunately, it seems that they didn't add memory buffers as a canned JPEG source until quite recently, so most of the questions online on how to use it are answered simply with "roll your own source manager module."  Further, all the examples that come with jpeglib are for loading from filename.jpg, which simply won't do for what I need.

Long story short, I spent part of an incredibly frustrating day trying to cobble together enough understanding of jpeglib to finally be able to build a working proof-of-concept snippet.  For the good of the Internet, I present to you the ~40 lines of C and ~160 lines of comments which resulted from my frustration, and which finally get libjpeg-8d to decompress a JPEG from a memory buffer.

Source Code:

1 comment:

  1. This post just saved me hours of work. I was trying to decode an image in RAM using libjpeg, but even looking in the library's documentation, I didn't find how to do exactly this. To the extent I read, it never mentioned I could do this, it goes directly to the "source manager module" thing. I was almost giving it up, but then this post shows up on Google!