Using Magick++ and libtiff intermittently

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
paavo256

Using Magick++ and libtiff intermittently

Post by paavo256 »

I have got a large application which calls both ImageMagick libraries (via Magick++) as well as libtiff directly. It seems these libraries get somehow entangled behind the scenes, when calling libtiff I get an assert failure from ImageMagick (if there occurred a warning in earlier ImageMagick call).

Maybe I have somehow to clear the "warning exception" before continuing, but I'm not quite sure how to do this.

Scenario:
Read a TIFF image with ImageMagick. There is an extra tag which causes an ImageMagick warning: "unknown field with tag 33560 (0x8318) encountered.".

Read another TIFF file in the same process using libtiff directly:
magick/exception.c:948: ThrowMagickExceptionList: Assertion `exception->signature == 0xabacadabUL' failed.
Aborted (core dumped)

If there is no warning in the first call, the sequence appears to work.

How to get it working? Thanks in advance!
Paavo

Stack backtrace from gdb:
#0 0x00002aaac51a0535 in raise () from /lib64/libc.so.6
#1 0x00002aaac51a1990 in abort () from /lib64/libc.so.6
#2 0x00002aaac5199c16 in __assert_fail () from /lib64/libc.so.6
#3 0x00002aaaab280e5a in ThrowMagickExceptionList () from /usr/lib64/libMagick.so.10
#4 0x00002aaaab280efa in ThrowMagickException () from /usr/lib64/libMagick.so.10
#5 0x00002aaaad7b29e0 in RegisterTIFFImage () from /usr/lib64/ImageMagick-6.3.0/modules-Q16/coders/tiff.so
#6 0x00002aaaabac06e2 in TIFFErrorExt () from /usr/lib64/libtiff.so.3
#7 0x00002aaaabad522d in TIFFInitLZW () from /usr/lib64/libtiff.so.3
#8 0x00002aaaabadd9e5 in TIFFReadEncodedStrip () from /usr/lib64/libtiff.so.3
#9 -- Our application code --
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Using Magick++ and libtiff intermittently

Post by magick »

ImageMagick sets the TIFF error and warning handlers. We will add a patch that reset these handlers just before the TIFF read / write methods return.
paavo256

Re: Using Magick++ and libtiff intermittently

Post by paavo256 »

Thanks for clarifying the issue. I added code to save and restore the TIFF error and warning handlers in my code calling libtiff, now it appears to work. I had to call both TIFFSetErrorHandler() and TIFFSetErrorHandlerExt() functions (the latter seems to be not documented anywhere).

However, the idea of such global handlers does not work out very well in multithreaded environment anyway. But this is actually a libtiff problem, not related to imagemagick directly.

Paavo
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Using Magick++ and libtiff intermittently

Post by magick »

We considered that a problem as well. However, libTIFF is not thread safe so it should not be used in a threaded environment. Within ImageMagick we invoke a lock to ensure only one instance of TIFF reading or writing is invoked at any given time. By the way, we have a patch in the latest ImageMagick release to restore the TIFF error and warning handlers.
Post Reply