2015.12.09 15:52 "[Tiff] Creating private IFD", by Mat Maher

2015.12.21 16:18 "Re: [Tiff] Creating private IFD", by

Writing TIFF files with more than one IFD isn’t easy, because the TIFF Library and its documentation isn’t clearly and some side effects have to be known. In the past, I have had also my issues with that.

Firstly, a basic example can be found in  test/custom_dir.c, where an EXIF IFD is filled and saved. Furthermore, read the article in the LibTiff archive: “2012.06.06 05:05 "Custom and EXIF directory read/write", by Frank Warmerdam”.

Nevertheless, it was very cumbersome for me to figure out, how to deal with additional custom directories (i.e. IFDs or Sub-IFDs). My sequence to handle a second IFD - in my case the EXIF-GPS IFD - is as follows:

  1. Create TIFF file

2) Complete the ‘normal’ metadata

3) Set the tag for the sub-IFD with a “dummy” value in order to get the tag reserved. The final value will be inserted lateron:

    TIFFSetField(tiffOut, TIFFTAG_GPSIFD, dir_offset );
 

4) Save current TIFF-directory to file. Otherwise, it will be lost. UnlikeTIFFWriteDirectory(), TIFFCheckpointDirectory() does not free up the directory data structures in memory, so they can be updated. Remember also the number of the current directory: TIFFCheckpointDirectory(tiffOut);

    dirNum = TIFFCurrentDirectory(tiffOut);
 

5) Create the second TIFF-directory (e.g. custom directory) and set its fields. The TIFFFieldArray infoarray has to be specified beforehand somewhere in your private include files. An example is given for the EXIF directory in tif_dirinfo.c TIFFCreateCustomDirectory(tiffOut, infoarray);

    TIFFSetField( tiffOut, GPSTAG_VERSIONID, gpsVersion);
 

Be aware that every CreateDirectory() or WriteDirectory()  apparently frees the *tif structure and sets up a new one!

 
6) Write custom directory to file.
    TIFFWriteCustomDirectory( tiffOut, &dir_offset );
 

7) Reload the first directory (i.e. the original TIFF directory). Apparently, this reads the data back from file.

    TIFFSetDirectory(tiffOut, dirNum);
 

8) Set the correct offset value of the Sub-IFD tag and save that changes to file.

    TIFFSetField(tiffOut, TIFFTAG_GPSIFD, dir_offset );
    TIFFCheckpointDirectory(tiffOut);
 

9) I found that to re-set the current directory may avoid the behaviour, that the current tiff-tags are written a second time at the end of the file because it resets the TIFF_DIRTYDIRECT flag.

    TIFFSetDirectory(tiffOut, dirNum);
 

It is useless to work with two different tiffOut1, tiffOut2 pointers, because there is only ONE TIFF object (TIFF directory) within the LibTiff. So, when you create a second one, or switch from one to another directory without having saved the modified tags to file, they are lost.

In principle, the same is true for two full IFDs (not Sub-IFDs like EXIF and GPS) for example when two images are stored within one TIFF file.

 
I hope that helps a little,
Su
 

_______________________________________________
Tiff mailing list: Tiff@lists.maptools.org

http://lists.maptools.org/mailman/listi