2017.02.15 13:54 "[Tiff] Incompatibility with unsigned rationals", by Alnahawi, Nouri

2017.02.15 13:54 "[Tiff] Incompatibility with unsigned rationals", by Alnahawi, Nouri

Dear Tiff Community,

after a long struggle with trying to write EXIF fields into .tif files using libtiff v.4.0.7, I've discovered the following problem in the source file tif_dirwrite.c - method TIFFWriteDirectoryTagCheckedRational:

Starting at line 2110:

    else if (value<1.0)

libtiff defines RATIONAL as float for its IO, when used in other applications, and converts them when reading from files, and when writing into files, from / into suitable unsigned rationals (32 bit nom. and 32 bit denom.). However, using the exiftool made by Phil Harvey http://www.sno.phy.queensu.ca/~phil/exiftool/ I found that the values are interpreted correctly. Using IfranView, it was not the case. A rational was interpreted as a signed rational, ignoring the decimal point numbers and making the value negative and signed. This seemingly bug is a result of the 0xFFFFFFFF used for precision while converting the floats.

My workaround was to define my own field array, changing all TIFF_RATIONAL fields to TIFF_SRATIONAL, and it worked for IfranView and exiftool, since it forced using TIFFWriteDirectoryTagCheckedSrationalArray, which converts using 0x7FFFFFFF because of the sign bit at the front, not maximizing precision as in 0xFFFFFFFF, which had lead to this incompatibility.

I didn't find a method such as TIFFWriteDirectoryTagCheckedSRational. Now I've spent almost 2 months trying to figure out how to write EXIF data to tif files, and I've found this method, by Paul Heckbert to be the best so far: http://www.asmail.be/msg0054967491.html

I just replaced TIFFCreateEXIFDirectory by TIFFCreateCustomDirectory, for which I had to define my own TIFFField exifFields[] and TIFFFieldArray exifFieldArray from tif_dirinfo.c. I didn't change them there, I just copied them into my application and changed the tags I needed.

I hope this helps someone, and I hope someone would take care of these issues in a future update of the library.