xref: /libtiff-4.0.7/html/addingtags.html (revision d4dd6ccc)
1<HTML>
2<HEAD>
3<TITLE>
4Modifying The TIFF Library
5</TITLE>
6</HEAD>
7<BODY BGCOLOR=white>
8<FONT FACE="Arial, Helvetica, Sans">
9
10<H1>
11Defining New TIFF Tags
12</H1>
13
14Libtiff has built-in knowledge of all the standard TIFF tags, as
15well as extentions.  The following describes how to add knowledge of
16new tags as builtins to libtiff, or how to application specific tags can
17be used by applications without modifying libtiff.
18<p>
19
20<h2>TIFFFieldInfo</h2>
21
22How libtiff manages specific tags is primarily controlled by the
23definition for that tag value stored internally as a TIFFFieldInfo structure.
24This structure looks like this:
25<p>
26
27<pre>
28typedef	struct {
29  ttag_t    field_tag;          /* field's tag */
30  short	    field_readcount;    /* read count/TIFF_VARIABLE/TIFF_SPP */
31  short	    field_writecount;   /* write count/TIFF_VARIABLE */
32  TIFFDataType field_type;      /* type of associated data */
33  unsigned short field_bit;     /* bit in fieldsset bit vector */
34  unsigned char field_oktochange;/* if true, can change while writing */
35  unsigned char field_passcount;/* if true, pass dir count on set */
36  char	*field_name;		/* ASCII name */
37} TIFFFieldInfo;
38</pre>
39
40<ul>
41<li> <b>field_tag</b>: the tag number.  For instance 277 for the
42SamplesPerPixel tag.  Builtin tags will generally have a #define in
43tiff.h for each known tag. <p>
44
45<li> <b>field_readcount</b>: The number of values which should be read.
46The special value TIFF_VARIABLE (-1) indicates that a variable number of
47values may be read.  The special value TIFFTAG_SPP (-2) indicates that there
48should be one value for each sample as defined by TIFFTAG_SAMPLESPERPIXEL.
49The special value TIFF_VARIABLE2 (-3) is presumably similar to TIFF_VARIABLE
50though I am not sure what the distinction in behaviour is.  This field
51is TIFF_VARIABLE for variable length ascii fields.<p>
52
53<li> <b>field_writecount</b>: The number of values which should be written.
54Generally the same as field_readcount.  A few built-in exceptions exist, but
55I haven't analysed why they differ. <p>
56
57<li> <b>field_type</b>: Type of the field.  One of TIFF_BYTE, TIFF_ASCII,
58TIFF_SHORT, TIFF_LONG, TIFF_RATIONAL, TIFF_SBYTE, TIFF_UNDEFINED,
59TIFF_SSHORT, TIFF_SLONG, TIFF_SRATIONAL, TIFF_FLOAT, TIFF_DOUBLE or
60TIFF_IFD.  Note that some fields can support more than one type (for
61instance short and long).  These fields should have multiple TIFFFieldInfos.
62<p>
63
64<li> <b>field_bit</b>: Built-in tags stored in special fields in the
65TIFF structure have assigned field numbers to distinguish them (ie.
66FIELD_SAMPLESPERPIXEL).  New tags should generally just use
67FIELD_CUSTOM indicating they are stored in the generic tag list.<p>
68
69<li> <b>field_oktochange</b>: TRUE if it is OK to change this tag value
70while an image is being written.  FALSE for stuff that must be set once
71and then left unchanged (like ImageWidth, or PhotometricInterpretation for
72instance).<p>
73
74<li> <b>field_passcount</b>: If TRUE, then the count value must be passed
75in TIFFSetField(), and TIFFGetField(), otherwise the count is not required.
76This should generally be TRUE for non-ascii variable count tags unless
77the count is implicit (such as with the colormap).<p>
78
79<li> <b>field_name</b>: A name for the tag.  Normally mixed case (studly caps)
80like "StripByteCounts" and relatively short. <p>
81
82</ul>
83
84A TIFFFieldInfo definition exists for each built-in tag in the tif_dirinfo.c
85file.  Some tags which support multiple data types have more than one
86definition, one per data type supported. <p>
87
88Various functions exist for getting the internal TIFFFieldInfo definitions,
89including _TIFFFindFieldInfo(), and _TIFFFindFieldInfoByName().  See
90tif_dirinfo.c for details.  There must be some mechanism to get the whole
91list, though I don't see it off hand.<p>
92
93<h2>Default Tag Auto-registration</h2>
94
95In libtiff 3.6.0 a new mechanism was introduced allowing libtiff to
96read unrecognised tags automatically.  When an unknown tags is encountered,
97it is automatically internally defined with a default name and a type
98derived from the tag value in the file.  Applications only need to predefine
99application specific tags if they need to be able to set them in a file, or
100if particular calling conventions are desired for TIFFSetField() and
101TIFFGetField().<p>
102
103When tags are autodefined like this the <b>field_readcount</b> and
104<b>field_writecount</b> values are always TIFF_VARIABLE.  The
105<b>field_passcount</b> is always TRUE, and the <b>field_bit</b> is
106FIELD_CUSTOM.  The field name will be "Tag %d" where the %d is the tag
107number.<p>
108
109<h2>Defining Application Tags</h2>
110
111For various reasons, it is common for applications to want to define
112their own tags to store information outside the core TIFF specification.
113This is done by calling TIFFMergeFieldInfo() with one or more TIFFFieldInfos.
114<p>
115
116The libgeotiff library provides geospatial information extentions within
117a TIFF file.  First, a set of TIFFFieldInfo's is prepared with information
118on the new tags:<p>
119
120<pre>
121static const TIFFFieldInfo xtiffFieldInfo[] = {
122
123  /* XXX Insert Your tags here */
124    { TIFFTAG_GEOPIXELSCALE,	-1,-1, TIFF_DOUBLE,	FIELD_CUSTOM,
125      TRUE,	TRUE,	"GeoPixelScale" },
126    { TIFFTAG_GEOTRANSMATRIX,	-1,-1, TIFF_DOUBLE,	FIELD_CUSTOM,
127      TRUE,	TRUE,	"GeoTransformationMatrix" },
128    { TIFFTAG_GEOTIEPOINTS,	-1,-1, TIFF_DOUBLE,	FIELD_CUSTOM,
129      TRUE,	TRUE,	"GeoTiePoints" },
130    { TIFFTAG_GEOKEYDIRECTORY, -1,-1, TIFF_SHORT,	FIELD_CUSTOM,
131      TRUE,	TRUE,	"GeoKeyDirectory" },
132    { TIFFTAG_GEODOUBLEPARAMS,	-1,-1, TIFF_DOUBLE,	FIELD_CUSTOM,
133      TRUE,	TRUE,	"GeoDoubleParams" },
134    { TIFFTAG_GEOASCIIPARAMS,	-1,-1, TIFF_ASCII,	FIELD_CUSTOM,
135      TRUE,	FALSE,	"GeoASCIIParams" }
136};
137</pre>
138
139In order to define the tags, we call TIFFMergeFieldInfo() on the
140desired TIFF handle with the list of TIFFFieldInfos.<p>
141
142<pre>
143#define	N(a)	(sizeof (a) / sizeof (a[0]))
144
145    /* Install the extended Tag field info */
146    TIFFMergeFieldInfo(tif, xtiffFieldInfo, N(xtiffFieldInfo));
147</pre>
148
149The tags need to be defined for each TIFF file opened - and when reading
150they should be defined before the tags of the file are read, yet a valid
151TIFF * is needed to merge the tags against.   In order to get them
152registered at the appropriate part of the setup process, it is necessary
153to register our merge function as an extender callback with libtiff.
154This is done with TIFFSetTagExtender().  We also keep track of the
155previous tag extender (if any) so that we can call it from our extender
156allowing a chain of customizations to take effect. <P>
157
158<pre>
159static TIFFExtendProc _ParentExtender = NULL;
160
161static
162void _XTIFFInitialize(void)
163{
164    static int first_time=1;
165
166    if (! first_time) return; /* Been there. Done that. */
167    first_time = 0;
168
169    /* Grab the inherited method and install */
170    _ParentExtender = TIFFSetTagExtender(_XTIFFDefaultDirectory);
171}
172</pre>
173
174The extender callback is looks like this.  It merges in our new fields
175and then calls the next extender if there is one in effect.<p>
176
177<pre>
178static void
179_XTIFFDefaultDirectory(TIFF *tif)
180{
181    /* Install the extended Tag field info */
182    TIFFMergeFieldInfo(tif, xtiffFieldInfo, N(xtiffFieldInfo));
183
184    /* Since an XTIFF client module may have overridden
185     * the default directory method, we call it now to
186     * allow it to set up the rest of its own methods.
187     */
188
189    if (_ParentExtender)
190        (*_ParentExtender)(tif);
191}
192</pre>
193
194The above approach ensures that our new definitions are used when reading
195or writing any TIFF file.  However, since on reading we already have
196default definitions for tags, it is usually not critical to pre-define them.
197If tag definitions are only required for writing custom tags, you can just
198call TIFFMergeFieldInfo() before setting new tags.  The whole extender
199architecture can then be avoided.<p>
200
201<A NAME=AddingTags><P><H2>Adding New Builtin Tags</H2></A>
202
203A similar approach is taken to the above.  However, the TIFFFieldInfo
204should be added to the tiffFieldInfo[] list in tif_dirinfo.c.  Ensure that
205new tags are added in sorted order by the tag number.<p>
206
207Normally new built-in tags should be defined with FIELD_CUSTOM; however, if
208it is desirable for the tag value to have it's own field in the TIFFDirectory
209structure, then you will need to #define a new FIELD_ value for it, and
210add appropriate handling as follows:
211
212
213<OL>
214<LI>Define the tag in <B>tiff.h</B>.
215<LI>Add a field to the directory structure in <B>tif_dir.h</B>
216   and define a <TT>FIELD_*</TT> bit (also update the definition of
217   <TT>FIELD_CODEC</TT> to reflect your addition).
218<LI>Add an entry in the <TT>TIFFFieldInfo</TT> array defined at the top of
219   <B>tif_dirinfo.c</B>.
220   Note that you must keep this array sorted by tag
221   number and that the widest variant entry for a tag should come
222   first (e.g. <TT>LONG</TT> before <TT>SHORT</TT>).
223<LI>Add entries in <TT>_TIFFVSetField()</TT> and <TT>_TIFFVGetField()</TT>
224   for the new tag.
225<LI>(<I>optional</I>) If the value associated with the tag is not a scalar value
226   (e.g. the array for <TT>TransferFunction</TT>) and requires
227   special processing,
228   then add the appropriate code to <TT>TIFFReadDirectory()</TT> and
229   <TT>TIFFWriteDirectory()</TT>.  You're best off finding a similar tag and
230   cribbing code.
231<LI>Add support to <TT>TIFFPrintDirectory()</TT> in <B>tif_print.c</B>
232    to print the tag's value.
233</OL>
234
235<P>
236If you want to maintain portability, beware of making assumptions
237about data types.  Use the typedefs (<TT>uint16</TT>, etc. when dealing with
238data on disk and <TT>t*_t</TT> when stuff is in memory) and be careful about
239passing items through printf or similar vararg interfaces.
240
241<A NAME=AddingCODECTags><P><H2>Adding New Codec-private Tags</H2></A>
242
243To add tags that are meaningful <EM>only when a particular compression
244algorithm is used</EM> follow these steps:
245
246<OL>
247<LI>Define the tag in <B>tiff.h</B>.
248<LI>Allocate storage for the tag values in the private state block of
249   the codec.
250<LI>Insure the state block is created when the codec is initialized.
251<LI>At <TT>TIFFInitfoo</TT> time override the method pointers in the
252    TIFF structure
253   for getting, setting and printing tag values.  For example,
254<PRE>
255    sp->vgetparent = tif->tif_vgetfield;
256    tif->tif_vgetfield = fooVGetField;	/* hook for codec tags */
257    sp->vsetparent = tif->tif_vsetfield;
258    tif->tif_vsetfield = fooVSetField;	/* hook for codec tags */
259    tif->tif_printdir = fooPrintDir;	/* hook for codec tags */
260</PRE>
261   (Actually you may decide not to override the
262   <TT>tif_printdir</TT> method, but rather just specify it).
263<LI>Create a private <TT>TIFFFieldInfo</TT> array for your tags and
264    merge them into the core tags at initialization time using
265    <TT>_TIFFMergeFieldInfo</TT>; e.g.
266<PRE>
267    _TIFFMergeFieldInfo(tif, fooFieldInfo, N(fooFieldInfo));
268</PRE>
269   (where <TT>N</TT> is a macro used liberaly throughout the distributed code).
270<LI>Fill in the get and set routines.  Be sure to call the parent method
271   for tags that you are not handled directly.  Also be sure to set the
272   <TT>FIELD_*</TT> bits for tags that are to be written to the file.  Note that
273   you can create ``pseudo-tags'' by defining tags that are processed
274   exclusively in the get/set routines and never written to file (see
275   the handling of <TT>TIFFTAG_FAXMODE</TT> in <B>tif_fax3.c</B>
276   for an example of this).
277<LI>Fill in the print routine, if appropriate.
278</OL>
279
280Note that space has been allocated in the <TT>FIELD_*</TT> bit space for
281codec-private tags.  Define your bits as <TT>FIELD_CODEC+&lt;offset&gt;</TT> to
282keep them away from the core tags.  If you need more tags than there
283is room for, just increase <TT>FIELD_SETLONGS</TT> at the top of
284<B>tiffiop.h</B>.
285
286<HR>
287
288Last updated: $Date: 2016-09-25 20:05:44 $
289
290</BODY>
291
292</HTML>
293