I have implemented a working custom file handling for pnglib using:
png_set_read_fn( png_ptr, file, myReadFn );
png_set_write_fn( png_ptr, file, mywriteFn);
rather than png_init_io( png_ptr, fp );
which uses standard fread, fwrite calls.
I'd now like to implement the same thing with libjpeg.
It doesn't look like there is a simple function passing solution like the pnglib library. So I have edited ligjpeg's jinclude.h so macros JFREAD and JFWRITE now call myReadFn and myWriteFn respectively. This also requires an include adding containing the declarations for myReadFn and myWriteFn.
Having added the include it causes compile errors from many of the libjpeg files, from what I have read it seems is attempting to compile as C++ when libjpeg is built for 'C'.
2> jutils.c
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xtgmath.h(214): warning C4602: #pragma pop_macro : 'new' no previous #pragma push_macro for this identifier
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xtgmath.h(215): warning C4193: #pragma warning(pop) : no matching '#pragma warning(push)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xtgmath.h(216): warning C4161: #pragma pack(pop...) : more pops than pushes
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\cmath(23): error C2061: syntax error : identifier 'abs' ...... // LOADS MORE
You can create your own jpeg_source_mgr
(for example, see http://www.cs.stanford.edu/~acoates/decompressJpegFromMemory.txt)
Maybe these functions can also be useful
/* Data source and destination managers: memory buffers. */
EXTERN(void) jpeg_mem_dest JPP((j_compress_ptr cinfo,
unsigned char ** outbuffer,
unsigned long * outsize));
EXTERN(void) jpeg_mem_src JPP((j_decompress_ptr cinfo,
unsigned char * inbuffer,
unsigned long insize));