| 1 |
TIFFCustomDirectory |
| 2 |
=================== |
| 3 |
|
| 4 |
Synopsis |
| 5 |
-------- |
| 6 |
|
| 7 |
.. highlight:: c |
| 8 |
|
| 9 |
:: |
| 10 |
|
| 11 |
#include <tiffio.h> |
| 12 |
|
| 13 |
.. c:function:: int TIFFCreateCustomDirectory(TIFF* tif, const TIFFFieldArray* infoarray) |
| 14 |
|
| 15 |
.. c:function:: int TIFFCreateEXIFDirectory(TIFF* tif) |
| 16 |
|
| 17 |
.. c:function:: int TIFFCreateGPSDirectory(TIFF* tif) |
| 18 |
|
| 19 |
.. c:function:: int TIFFWriteCustomDirectory(TIFF* tif, uint64 *pdiroff) |
| 20 |
|
| 21 |
.. c:function:: int TIFFReadCustomDirectory(TIFF* tif, toff_t diroff, const TIFFFieldArray* infoarray) |
| 22 |
|
| 23 |
.. c:function:: int TIFFReadEXIFDirectory(TIFF* tif, toff_t diroff) |
| 24 |
|
| 25 |
.. c:function:: int TIFFReadGPSDirectory(TIFF* tif, toff_t diroff) |
| 26 |
|
| 27 |
.. c:function:: const TIFFFieldArray* _TIFFGetExifFields(void) |
| 28 |
|
| 29 |
.. c:function:: const TIFFFieldArray* _TIFFGetGpsFields(void) |
| 30 |
|
| 31 |
Description |
| 32 |
----------- |
| 33 |
|
| 34 |
The following routines create a custom directory and retrieve information |
| 35 |
about directories in an open TIFF file. |
| 36 |
|
| 37 |
:c:func:`TIFFCreateCustomDirectory`, :c:func:`TIFFCreateEXIFDirectory`, |
| 38 |
:c:func:`TIFFCreateGPSDirectory` will setup a custom directory or one |
| 39 |
of the predefined EXIF or GPS directories and set the context of the |
| 40 |
TIFF-handle ``tif`` to that custom directory for functions |
| 41 |
like :c:func:`TIFFSetField`. |
| 42 |
|
| 43 |
:c:func:`TIFFWriteCustomDirectory` will write the contents of the |
| 44 |
current custom directory to the file and return the offset to that |
| 45 |
directory in :c:var:`pdiroff`. That offset has to be written to the main-IFD: |
| 46 |
|
| 47 |
.. highlight:: c |
| 48 |
|
| 49 |
:: |
| 50 |
|
| 51 |
/* Go back to the first directory, and add the EXIFIFD pointer. */ |
| 52 |
TIFFSetDirectory(tif, 0); |
| 53 |
TIFFSetField(tif, TIFFTAG_EXIFIFD, pdiroff); |
| 54 |
|
| 55 |
|
| 56 |
:c:func:`TIFFReadCustomDirectory` will read the custom directory from the |
| 57 |
arbitrary offset into the :c:var:`infoarray` and sets the context of the |
| 58 |
TIFF-handle :c:var:`tif` to that custom directory for functions like |
| 59 |
:c:func:`TIFFReadField`. The :c:type:`TIFFFieldArray` :c:var:`infoarray` |
| 60 |
has to be according the layout of the custom directory. For the predefined |
| 61 |
EXIF and GPS directories, the relevant :c:type:`TIFFFieldArray` definitions |
| 62 |
are hidden within the functions :c:func:`TIFFReadEXIFDirectory` and |
| 63 |
:c:func:`TIFFReadGPSDirectory` The code is very similar to :c:func:`TIFFReadDirectory`. |
| 64 |
The offset to the custom directory diroff has to be read from the |
| 65 |
relative TIFF tag first. |
| 66 |
|
| 67 |
:c:func:`_TIFFGetExifFields` and :c:func:`_TIFFGetGpsFields` will |
| 68 |
return a pointer to the ``libtiff`` internal definition list of the |
| 69 |
EXIF and GPS tags, respectively. |
| 70 |
|
| 71 |
Notes |
| 72 |
----- |
| 73 |
|
| 74 |
Be aware |
| 75 |
|
| 76 |
- that until a directory is not written to file AND read back, the query |
| 77 |
functions won't retrieve the correct information! |
| 78 |
- that the newly created directory will not exist on the file till |
| 79 |
:c:func:`TIFFWriteDirectory`, :c:func:`TIFFCheckpointDirectory`, |
| 80 |
:c:func:`TIFFFlush` or :c:func:`TIFFClose` has been called. |
| 81 |
- that :c:func:`TIFFCreateDirectory` and :c:func:`TIFFWriteDirectory` |
| 82 |
create a new directory, free the ``*tif`` structure and set up a new one. |
| 83 |
- that unlike :c:func:`TIFFWriteDirectory`, :c:func:`TIFFCheckpointDirectory` |
| 84 |
does not free up the directory data structures in memory. |
| 85 |
- that LibTiff does not support custom directory chains |
| 86 |
(NextIFD pointing to another IFD). |
| 87 |
NextIFD of custom directories is always set to zero |
| 88 |
and should be zero when reading. |
| 89 |
|
| 90 |
Unfortunately to create or read custom directories with predefined fields |
| 91 |
it is necessary to include the private tif_dir.h. However, for EXIF and |
| 92 |
GPS directories, which have a predefined schema within ``libtiff``, this |
| 93 |
is not necessary. There are some test programmes that briefly demonstrate |
| 94 |
the creation and reading of EXIF, GPS and custom directories. |
| 95 |
See test/custom_dir.c and test/custom_dir_EXIF_231.c |
| 96 |
|
| 97 |
After ``libtiff`` version 4.6.0 :c:func:`TIFFCreateCustomDirectory`, |
| 98 |
:c:func:`TIFFCreateEXIFDirectory` and :c:func:`TIFFCreateGPSDirectory` also |
| 99 |
releases the directory part of the `tif` structure by calling :c:func:`TIFFFreeDirectory`, |
| 100 |
which should have been called by the user application before creating a new directory. |
| 101 |
|
| 102 |
Hints and detailed instructions |
| 103 |
------------------------------- |
| 104 |
|
| 105 |
Writing TIFF files with more than one directory (IFD) is not easy because |
| 106 |
some side effects need to be known. |
| 107 |
|
| 108 |
The main point here is that there can only be one ``tif`` structure in |
| 109 |
the main memory for a file, which can only hold the tags of one directory |
| 110 |
at a time. It is useless to work with two different tiffOut1, tiffOut2 |
| 111 |
pointers, because there is only ONE TIFF object (TIFF directory) within |
| 112 |
the ``libtiff``. If you want to address a second directory in the file, |
| 113 |
the tags of the current directory must first be saved in the file, |
| 114 |
otherwise they will be lost (overwritten or mixed). Then the ``tif`` |
| 115 |
structure in the main memory must be tidied up, otherwise the old tags |
| 116 |
will beincluded in the new directory. |
| 117 |
This can be done either by creating a new, empty ``tif`` structure or by |
| 118 |
reading in an directory previously saved in the file. |
| 119 |
|
| 120 |
A sequence to handle a second (or third) TIFF directory - in this case |
| 121 |
the GPS IFD - is as follows: |
| 122 |
|
| 123 |
1) Create TIFF file |
| 124 |
2) Complete the "normal" metadata |
| 125 |
3) Set the tag for the custom directory with a “dummy” value in order to |
| 126 |
get the tag reserved. The final value will be inserted lateron. This |
| 127 |
prevents the main directory from being written to the file again at an |
| 128 |
additional area, leaving the first memory area unused: |
| 129 |
|
| 130 |
.. highlight:: c |
| 131 |
|
| 132 |
:: |
| 133 |
|
| 134 |
TIFFSetField(tiffOut, TIFFTAG_GPSIFD, dir_offset); |
| 135 |
|
| 136 |
4) Save current TIFF-directory to file. Otherwise, it will be lost. |
| 137 |
Remember also the number of the current directory: |
| 138 |
|
| 139 |
.. highlight:: c |
| 140 |
|
| 141 |
:: |
| 142 |
|
| 143 |
TIFFWriteDirectory(tiffOut); |
| 144 |
dirNum = TIFFCurrentDirectory(tiffOut); |
| 145 |
|
| 146 |
5) Create the second TIFF-directory (e.g. custom directory) and set its |
| 147 |
fields. The TIFFFieldArray infoarray has to be specified beforehand |
| 148 |
somewhere in your private include files. |
| 149 |
An example is given for the EXIF directory in tif_dirinfo.c |
| 150 |
|
| 151 |
.. highlight:: c |
| 152 |
|
| 153 |
:: |
| 154 |
|
| 155 |
TIFFCreateCustomDirectory(tiffOut, infoarray); /* for a real custom directory */ |
| 156 |
/* or alternatively, use GPS or EXIF with pre-defined TIFFFieldArray IFD field structure */ |
| 157 |
TIFFCreateGPSDirectory(tiffOut); |
| 158 |
TIFFSetField(tiffOut, GPSTAG_VERSIONID, gpsVersion); /* set fields of the custom directory */ |
| 159 |
|
| 160 |
Be aware that every :c:func:`TIFFCreateDirectory` or :c:func:`TIFFWriteDirectory` |
| 161 |
apparently frees the ``*tif`` structure and sets up a new one! |
| 162 |
|
| 163 |
6) Write custom directory to file. The offset to that directory in the file |
| 164 |
is returned in :c:var:`dir_offset`. |
| 165 |
|
| 166 |
.. highlight:: c |
| 167 |
|
| 168 |
:: |
| 169 |
|
| 170 |
TIFFWriteCustomDirectory(tiffOut, &dir_offset); |
| 171 |
|
| 172 |
7) Reload the first directory (i.e. the original TIFF directory). |
| 173 |
Apparently, this reads the data back from file. |
| 174 |
|
| 175 |
.. highlight:: c |
| 176 |
|
| 177 |
:: |
| 178 |
|
| 179 |
TIFFSetDirectory(tiffOut, dirNum); |
| 180 |
|
| 181 |
8) Set the correct offset value of the custom directory IFD tag and save |
| 182 |
that changes to file. |
| 183 |
|
| 184 |
.. highlight:: c |
| 185 |
|
| 186 |
:: |
| 187 |
|
| 188 |
TIFFSetField(tiffOut, TIFFTAG_GPSIFD, dir_offset); |
| 189 |
TIFFWriteDirectory(tiffOut); |
| 190 |
|
| 191 |
RETURN VALUES |
| 192 |
------------- |
| 193 |
|
| 194 |
1 is returned when the contents are successfully written to the file. |
| 195 |
Otherwise, 0 is returned if an error was encountered when writing the |
| 196 |
directory contents. |
| 197 |
|
| 198 |
Diagnostics |
| 199 |
----------- |
| 200 |
|
| 201 |
All error messages are directed to the :c:func:`TIFFErrorExtR` routine. |
| 202 |
Likewise, warning messages are directed to the :c:func:`TIFFWarningExtR` routine. |
| 203 |
|
| 204 |
See also |
| 205 |
-------- |
| 206 |
|
| 207 |
:doc:`libtiff` (3tiff), |
| 208 |
:doc:`TIFFCreateDirectory` (3tiff), |
| 209 |
:doc:`TIFFquery` (3tiff), |
| 210 |
:doc:`TIFFSetDirectory` (3tiff), |
| 211 |
:doc:`TIFFWriteDirectory` (3tiff) |
| 212 |
|