/src/3rdparty/libtiff/html/addingtags.html

https://bitbucket.org/ultra_iter/qt-vtl · HTML · 292 lines · 241 code · 51 blank · 0 comment · 0 complexity · c578567b0dc4e5a61fd6d80f849b00d0 MD5 · raw file

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