2001.08.18 04:10 "jpeg in tiff", by Joris Van Damme

2001.08.18 04:10 "jpeg in tiff", by Joris Van Damme

Hey folks,

I'm having trouble with my jpeg-in-tiff decoder. It's probably related to my somewhat unusual setup.

I'm interfacing with both libraries from withing borland Delphi. That's pascal. Not using DLL's, but compiling both libraries into obj files with Borland C++ builder, and linking these into my Delphi pascal project with the Delphi linker. Works just fine.

I did have some error handling problems. Can't use longjump stuff in this setup, so calls to libjpeg from within my pascal project get wrapped in exception handlers, and my libjpeg error handler, in pascal, raises an exception. Works fine. One other main difference between my setup and the default is memory managment. Since the Delphi memory manager is very fast and efficient, I've rewired both libraries to use this.

But the problems begin when libtiff needs to interface with libjpeg, of course, 'cause I got neither longjump nor exception handling in that case.

The solution I implemented was to have libtiff interface to a delphi pascal layer, and have this layer in turn forward the requests to libjpeg. That way, I get to use exception handling wrappers in that pascal layer and all works fine.

BUT, for some mysterious reason, this only works if I set the TIFFTAG_JPEGCOLORMODE to JPEGCOLORMODE_RGB. If I do, all works fine; if I don't, the code crashes. I investigated this further since I really would like to get the original YCbCr from jpeg-in-tiff files. The problem seems to relate to the alloc_sarray memory manager function in libjpeg. If I use JPEGCOLORMODE_RGB, this routine never gets called and all is fine. If I don't, libtiff seems to need alloc_sarray, and makes the call to my delphi pascal layer. This one makes an exception handling call to a routine within libjpeg that in turn calls *cinfo->mem->alloc_sarray(cinfo,pool_id,samplesperrow,numrows) and BANG! Acces violation!! It's not an access to a null address that's causing the problems, but a random seems-like-legit address.

I really can't figure out what's going wrong. Did the jpeg memory manager not get set up properly? Is it some problem inside alloc_sarray? What is alloc_sarray? Can I replace it?

I've tried wiring the call to *cinfo->mem->alloc_sarray directly to alloc_sarray, but that does not seem to work either. I use my own memory allocation (again within a pascal layer), so everything is getting so complicated by now and I really don't know what's going on. I have no tracing possibility at all in the C code. I can only trace pascal.

Does anyone have any thoughts, whatsoever?? Are there any known problems with alloc_sarray (guess not, knowing who wrote it)? Does it use some stuff that never gets used in any other functions and that would explain the crash? Can I replace it with something else? Why is it used? Can I eliminate the need for it without resorting to JPEGCOLORMODE_RGB?

Joris