Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/doc/multi_page.rst

raw
1 Multi Page / Multi Image TIFF
2 =============================
3
4 There may be more than one :ref:`Image File Directory (IFD) <ImageFileDirectory>`
5 in a TIFF file. Each IFD defines a :ref:`subfile <SubFile>`.
6
7 One potential use of *subfiles* is to describe related images,
8 such as the pages of a facsimile transmission or reduced-resolution images
9 of the first full-resolution image.
10 Such files are also named "*multi-page* TIFF" or "*multi-image* TIFF".
11
12 There are two mechanisms for storing multiple images in a TIFF file:
13
14 1. A **main-IFD chain**, where the images are stored in linked IFDs (directories).
15 This mechanism is widely used.
16 2. A **SubIFD chain**, where additional images are stored within the SubIFD tag
17 of a main-IFD. Such child images provide extra information for the parent image
18 - such as a subsampled version of the parent image.
19 SubIFD chains are rarely supported.
20 For SubIFD refer also to
21 `Adobe PageMaker® 6.0 TIFF Technical Notes <https://www.awaresystems.be/imaging/tiff/specification/TIFFPM6.pdf>`_
22
23 .. _SubIFDAccess:
24
25 Sub IFD chains
26 --------------
27
28 In the case of several SubIFDs of a main image, there are two possibilities
29 that are not even mutually exclusive.
30
31 a. The ``SubIFD`` tag contains an array with all offsets of the SubIFDs.
32 This is called SubIFD tree and DNG recommends the use of SubIFD trees,
33 as described in the TIFF-EP specification. SubIFD chains are not supported.
34 b. The SubIFDs are concatenated with their ``NextIFD`` parameters
35 to a SubIFD chain.
36
37 LibTiff does support SubIFD chains partially. When the first
38 ``SubIFD`` tag is activated and read with :c:func:`TIFFSetSubDirectory()`,
39 the following can be parsed with :c:func:`TIFFReadDirectory()`.
40 The *tif_curdir* is just incremented from its current value
41 and thus gets arbitrary values when parsing through SubIFD chains.
42 When the SubIFDs are not chained, each offset
43 within the SubIFD array has to be activated and read individually.
44 :c:func:`TIFFSetDirectory()` only works with main-IFD chains because
45 always starts with the first main-IFD and thus is able to reset
46 the SubIFD reading chain to the main-IFD chain.
47
48 Writing Multi Page TIFF
49 -----------------------
50
51 The following example code shows how to write multi-page TIFF
52 as main-IFD chain and as SubIFD chain.
53 ``libtiff`` writes SubIFDs as an array of IFDs that are not chained
54 additionally, as Adobe PageMaker® 6.0 TIFF Technical Notes suggests.
55
56 .. highlight:: c
57
58 ::
59
60 #include <tiffio.h>
61
62 int main(int argc, const char **argv)
63 {
64 TIFF *tiff;
65
66 /* Define the number of pages/images (main-IFDs) you are going to write */
67 int number_of_images = 3;
68
69 /* Define the number of sub - IFDs you are going to write */
70 #define NUMBER_OF_SUBIFDs 2
71 int number_of_sub_IFDs = NUMBER_OF_SUBIFDs;
72 toff_t sub_IFDs_offsets[NUMBER_OF_SUBIFDs] = {
73 0UL}; /* array for SubIFD tag */
74 int blnWriteSubIFD = 0;
75
76 if (!(tiff = TIFFOpen("multiPageTiff2.tif", "w")))
77 return 1;
78
79 for (int i = 0; i < number_of_images; i++)
80 {
81 char pixel[1] = {128};
82
83 TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, 1);
84 TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, 1);
85 TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, 1);
86 TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, 8);
87 TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
88
89 /* For the last but one multi-page image, add a SubIFD e.g. for a
90 * thumbnail */
91 if (number_of_images - 2 == i)
92 blnWriteSubIFD = 1;
93
94 if (blnWriteSubIFD)
95 {
96 /* Now here is the trick: the next n directories written
97 * will be sub-IFDs of the main-IFD (where n is number_of_sub_IFDs
98 * specified when you set the TIFFTAG_SUBIFD field.
99 * The SubIFD offset array sub_IFDs_offsets is filled automatically
100 * with the proper offset values by the following number_of_sub_IFDs
101 * TIFFWriteDirectory() calls and updated in the related main-IFD
102 * with the last call.
103 */
104 if (!TIFFSetField(tiff, TIFFTAG_SUBIFD, number_of_sub_IFDs,
105 sub_IFDs_offsets))
106 return 1;
107 }
108
109 /* Write dummy pixel to image */
110 if (TIFFWriteScanline(tiff, pixel, 0, 0) < 0)
111 return 1;
112 /* Write image / directory to file */
113 if (!TIFFWriteDirectory(tiff))
114 return 1;
115
116 if (blnWriteSubIFD)
117 {
118 /* A SubIFD tag has been written for that main-IFD and this
119 * triggers that previous TIFFWriteDirectory() to switch to the
120 * SubIFD-chain for the next number_of_sub_IFDs writings.
121 * Thus, only the thumbnail images need to be
122 * set up and written to file using TIFFWriteDirectory().
123 * The last of this TIFFWriteDirectory() calls will setup
124 * the next fresh main-IFD.
125 */
126 for (int i = 0; i < number_of_sub_IFDs; i++)
127 {
128 TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, 1);
129 TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, 1);
130 TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, 1);
131 TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, 8);
132 TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
133 /* SUBFILETYPE tag is not mandatory for SubIFD writing, but a
134 * good idea to indicate thumbnails */
135 if (!TIFFSetField(tiff, TIFFTAG_SUBFILETYPE,
136 FILETYPE_REDUCEDIMAGE))
137 return 1;
138
139 /* Write dummy pixel to thumbnail image */
140 pixel[0] = 64;
141 if (TIFFWriteScanline(tiff, pixel, 0, 0) < 0)
142 return 1;
143 /* Writes now in the SubIFD chain */
144 if (!TIFFWriteDirectory(tiff))
145 return 1;
146
147 blnWriteSubIFD = 0;
148 }
149 }
150 }
151 TIFFClose(tiff);
152 return 0;
153 }
154
155 Reading Multi Page TIFF
156 -----------------------
157
158 For a reading example see code of `tools/tiffinfo.c` or below:
159
160 .. highlight:: c
161
162 ::
163
164 /* Reading of multi-page and SubIFD images (subfiles) */
165 if (!(tiff = TIFFOpen(filename, "r")))
166 return 1;
167
168 tdir_t currentDirNumber = TIFFCurrentDirectory(tiff);
169
170 /* The first directory is already read through TIFFOpen() */
171 int blnRead = 0;
172 do
173 {
174 /*Check if there are SubIFD subfiles */
175 void *ptr;
176 if (TIFFGetField(tiff, TIFFTAG_SUBIFD, &number_of_sub_IFDs, &ptr))
177 {
178 /* Copy SubIFD array from pointer */
179 memcpy(sub_IFDs_offsets, ptr,
180 number_of_sub_IFDs * sizeof(sub_IFDs_offsets[0]));
181
182 for (int i = 0; i < number_of_sub_IFDs; i++)
183 {
184 /* Read first SubIFD directory */
185 if (!TIFFSetSubDirectory(tiff, sub_IFDs_offsets[i]))
186 return 1;
187 /* Check if there is a SubIFD chain behind the first one from
188 * the array, as specified by Adobe */
189 while (TIFFReadDirectory(tiff))
190 /* analyse subfile */
191 ;
192 }
193 /* Go back to main-IFD chain and re-read that main-IFD directory */
194 if (!TIFFSetDirectory(tiff, currentDirNumber))
195 return 1;
196 }
197 /* Read next main-IFD directory (subfile) */
198 blnRead = TIFFReadDirectory(tiff);
199 currentDirNumber = TIFFCurrentDirectory(tiff);
200 } while (blnRead);
201 TIFFClose(tiff);
202
203
204
205
206 See also
207 --------
208
209 :doc:`terms`,
210 :doc:`/functions/TIFFSetDirectory` (3tiff),
211 :doc:`/functions/TIFFWriteDirectory` (3tiff),
212 `Adobe PageMaker® 6.0 TIFF Technical Notes <https://www.awaresystems.be/imaging/tiff/specification/TIFFPM6.pdf>`_,
213 `Example from StackOverflow <https://stackoverflow.com/questions/11959617/in-a-tiff-create-a-sub-ifd-with-thumbnail-libtiff>`_