tif_dir.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright (c) 1988-1997 Sam Leffler
  3. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  4. *
  5. * Permission to use, copy, modify, distribute, and sell this software and
  6. * its documentation for any purpose is hereby granted without fee, provided
  7. * that (i) the above copyright notices and this permission notice appear in
  8. * all copies of the software and related documentation, and (ii) the names of
  9. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  10. * publicity relating to the software without the specific, prior written
  11. * permission of Sam Leffler and Silicon Graphics.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  14. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  18. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22. * OF THIS SOFTWARE.
  23. */
  24. #ifndef _TIFFDIR_
  25. #define _TIFFDIR_
  26. #include "tiff.h"
  27. #include "tiffio.h"
  28. /*
  29. * ``Library-private'' Directory-related Definitions.
  30. */
  31. typedef struct
  32. {
  33. const TIFFField *info;
  34. int count;
  35. void *value;
  36. } TIFFTagValue;
  37. /*
  38. * TIFF Image File Directories are comprised of a table of field
  39. * descriptors of the form shown below. The table is sorted in
  40. * ascending order by tag. The values associated with each entry are
  41. * disjoint and may appear anywhere in the file (so long as they are
  42. * placed on a word boundary).
  43. *
  44. * If the value is 4 bytes or less, in ClassicTIFF, or 8 bytes or less in
  45. * BigTIFF, then it is placed in the offset field to save space. If so,
  46. * it is left-justified in the offset field.
  47. */
  48. typedef struct
  49. {
  50. uint16_t tdir_tag; /* see below */
  51. uint16_t tdir_type; /* data type; see below */
  52. uint64_t tdir_count; /* number of items; length in spec */
  53. union
  54. {
  55. uint16_t toff_short;
  56. uint32_t toff_long;
  57. uint64_t toff_long8;
  58. } tdir_offset; /* either offset or the data itself if fits */
  59. uint8_t tdir_ignore; /* flag status to ignore tag when parsing tags in
  60. tif_dirread.c */
  61. } TIFFDirEntry;
  62. typedef struct
  63. {
  64. uint64_t offset;
  65. uint64_t length;
  66. } TIFFEntryOffsetAndLength; /* auxiliary for evaluating size of IFD data */
  67. /*
  68. * Internal format of a TIFF directory entry.
  69. */
  70. typedef struct
  71. {
  72. #define FIELDSET_ITEMS 4
  73. /* bit vector of fields that are set */
  74. uint32_t td_fieldsset[FIELDSET_ITEMS];
  75. uint32_t td_imagewidth, td_imagelength, td_imagedepth;
  76. uint32_t td_tilewidth, td_tilelength, td_tiledepth;
  77. uint32_t td_subfiletype;
  78. uint16_t td_bitspersample;
  79. uint16_t td_sampleformat;
  80. uint16_t td_compression;
  81. uint16_t td_photometric;
  82. uint16_t td_threshholding;
  83. uint16_t td_fillorder;
  84. uint16_t td_orientation;
  85. uint16_t td_samplesperpixel;
  86. uint32_t td_rowsperstrip;
  87. uint16_t td_minsamplevalue, td_maxsamplevalue;
  88. double *td_sminsamplevalue;
  89. double *td_smaxsamplevalue;
  90. float td_xresolution, td_yresolution;
  91. uint16_t td_resolutionunit;
  92. uint16_t td_planarconfig;
  93. float td_xposition, td_yposition;
  94. uint16_t td_pagenumber[2];
  95. uint16_t *td_colormap[3];
  96. uint16_t td_halftonehints[2];
  97. uint16_t td_extrasamples;
  98. uint16_t *td_sampleinfo;
  99. /* even though the name is misleading, td_stripsperimage is the number
  100. * of striles (=strips or tiles) per plane, and td_nstrips the total
  101. * number of striles */
  102. uint32_t td_stripsperimage;
  103. uint32_t td_nstrips; /* size of offset & bytecount arrays */
  104. uint64_t
  105. *td_stripoffset_p; /* should be accessed with TIFFGetStrileOffset */
  106. uint64_t *td_stripbytecount_p; /* should be accessed with
  107. TIFFGetStrileByteCount */
  108. uint32_t
  109. td_stripoffsetbyteallocsize; /* number of elements currently allocated
  110. for td_stripoffset/td_stripbytecount.
  111. Only used if TIFF_LAZYSTRILELOAD is set
  112. */
  113. #ifdef STRIPBYTECOUNTSORTED_UNUSED
  114. int td_stripbytecountsorted; /* is the bytecount array sorted ascending? */
  115. #endif
  116. /* Be aware that the parameters of td_stripoffset_entry and
  117. * td_stripbytecount_entry are swapped but tdir_offset is not
  118. * and has to be swapped when used. */
  119. TIFFDirEntry td_stripoffset_entry; /* for deferred loading */
  120. TIFFDirEntry td_stripbytecount_entry; /* for deferred loading */
  121. uint16_t td_nsubifd;
  122. uint64_t *td_subifd;
  123. /* YCbCr parameters */
  124. uint16_t td_ycbcrsubsampling[2];
  125. uint16_t td_ycbcrpositioning;
  126. /* Colorimetry parameters */
  127. uint16_t *td_transferfunction[3];
  128. float *td_refblackwhite;
  129. /* CMYK parameters */
  130. int td_inknameslen;
  131. char *td_inknames;
  132. uint16_t td_numberofinks; /* number of inks in InkNames string */
  133. int td_customValueCount;
  134. TIFFTagValue *td_customValues;
  135. unsigned char
  136. td_deferstrilearraywriting; /* see TIFFDeferStrileArrayWriting() */
  137. unsigned char
  138. td_iswrittentofile; /* indicates if current IFD is present on file */
  139. /* LibTIFF writes all data that does not fit into the IFD entries directly
  140. * after the IFD tag entry part. When reading, only the IFD data directly
  141. * and continuously behind the IFD tags is taken into account for the IFD
  142. * data size.*/
  143. uint64_t td_dirdatasize_write; /* auxiliary for evaluating size of IFD data
  144. to be written */
  145. uint64_t td_dirdatasize_read; /* auxiliary for evaluating size of IFD data
  146. read from file */
  147. uint32_t td_dirdatasize_Noffsets; /* auxiliary counter for
  148. tif_dir.td_dirdatasize_offsets array */
  149. TIFFEntryOffsetAndLength
  150. *td_dirdatasize_offsets; /* auxiliary array for all offsets of IFD tag
  151. entries with data outside the IFD tag
  152. entries. */
  153. } TIFFDirectory;
  154. /*
  155. * Field flags used to indicate fields that have been set in a directory, and
  156. * to reference fields when manipulating a directory.
  157. */
  158. /*
  159. * FIELD_IGNORE is used to signify tags that are to be processed but otherwise
  160. * ignored. This permits antiquated tags to be quietly read and discarded.
  161. * Note that a bit *is* allocated for ignored tags; this is understood by the
  162. * directory reading logic which uses this fact to avoid special-case handling
  163. */
  164. #define FIELD_IGNORE 0
  165. /* multi-item fields */
  166. #define FIELD_IMAGEDIMENSIONS 1
  167. #define FIELD_TILEDIMENSIONS 2
  168. #define FIELD_RESOLUTION 3
  169. #define FIELD_POSITION 4
  170. /* single-item fields */
  171. #define FIELD_SUBFILETYPE 5
  172. #define FIELD_BITSPERSAMPLE 6
  173. #define FIELD_COMPRESSION 7
  174. #define FIELD_PHOTOMETRIC 8
  175. #define FIELD_THRESHHOLDING 9
  176. #define FIELD_FILLORDER 10
  177. #define FIELD_ORIENTATION 15
  178. #define FIELD_SAMPLESPERPIXEL 16
  179. #define FIELD_ROWSPERSTRIP 17
  180. #define FIELD_MINSAMPLEVALUE 18
  181. #define FIELD_MAXSAMPLEVALUE 19
  182. #define FIELD_PLANARCONFIG 20
  183. #define FIELD_RESOLUTIONUNIT 22
  184. #define FIELD_PAGENUMBER 23
  185. #define FIELD_STRIPBYTECOUNTS 24
  186. #define FIELD_STRIPOFFSETS 25
  187. #define FIELD_COLORMAP 26
  188. #define FIELD_EXTRASAMPLES 31
  189. #define FIELD_SAMPLEFORMAT 32
  190. #define FIELD_SMINSAMPLEVALUE 33
  191. #define FIELD_SMAXSAMPLEVALUE 34
  192. #define FIELD_IMAGEDEPTH 35
  193. #define FIELD_TILEDEPTH 36
  194. #define FIELD_HALFTONEHINTS 37
  195. #define FIELD_YCBCRSUBSAMPLING 39
  196. #define FIELD_YCBCRPOSITIONING 40
  197. #define FIELD_REFBLACKWHITE 41
  198. #define FIELD_TRANSFERFUNCTION 44
  199. #define FIELD_INKNAMES 46
  200. #define FIELD_SUBIFD 49
  201. #define FIELD_NUMBEROFINKS 50
  202. /* FIELD_CUSTOM (see tiffio.h) 65 */
  203. /* end of support for well-known tags; codec-private tags follow */
  204. #define FIELD_CODEC 66 /* base of codec-private tags */
  205. /*
  206. * Pseudo-tags don't normally need field bits since they are not written to an
  207. * output file (by definition). The library also has express logic to always
  208. * query a codec for a pseudo-tag so allocating a field bit for one is a
  209. * waste. If codec wants to promote the notion of a pseudo-tag being ``set''
  210. * or ``unset'' then it can do using internal state flags without polluting
  211. * the field bit space defined for real tags.
  212. */
  213. #define FIELD_PSEUDO 0
  214. #define FIELD_LAST (32 * FIELDSET_ITEMS - 1)
  215. #define BITn(n) (((uint32_t)1L) << ((n)&0x1f))
  216. #define BITFIELDn(tif, n) ((tif)->tif_dir.td_fieldsset[(n) / 32])
  217. #define TIFFFieldSet(tif, field) (BITFIELDn(tif, field) & BITn(field))
  218. #define TIFFSetFieldBit(tif, field) (BITFIELDn(tif, field) |= BITn(field))
  219. #define TIFFClrFieldBit(tif, field) (BITFIELDn(tif, field) &= ~BITn(field))
  220. #define FieldSet(fields, f) (fields[(f) / 32] & BITn(f))
  221. #define ResetFieldBit(fields, f) (fields[(f) / 32] &= ~BITn(f))
  222. typedef enum
  223. {
  224. TIFF_SETGET_UNDEFINED = 0,
  225. TIFF_SETGET_ASCII = 1,
  226. TIFF_SETGET_UINT8 = 2,
  227. TIFF_SETGET_SINT8 = 3,
  228. TIFF_SETGET_UINT16 = 4,
  229. TIFF_SETGET_SINT16 = 5,
  230. TIFF_SETGET_UINT32 = 6,
  231. TIFF_SETGET_SINT32 = 7,
  232. TIFF_SETGET_UINT64 = 8,
  233. TIFF_SETGET_SINT64 = 9,
  234. TIFF_SETGET_FLOAT = 10,
  235. TIFF_SETGET_DOUBLE = 11,
  236. TIFF_SETGET_IFD8 = 12,
  237. TIFF_SETGET_INT = 13,
  238. TIFF_SETGET_UINT16_PAIR = 14,
  239. TIFF_SETGET_C0_ASCII = 15,
  240. TIFF_SETGET_C0_UINT8 = 16,
  241. TIFF_SETGET_C0_SINT8 = 17,
  242. TIFF_SETGET_C0_UINT16 = 18,
  243. TIFF_SETGET_C0_SINT16 = 19,
  244. TIFF_SETGET_C0_UINT32 = 20,
  245. TIFF_SETGET_C0_SINT32 = 21,
  246. TIFF_SETGET_C0_UINT64 = 22,
  247. TIFF_SETGET_C0_SINT64 = 23,
  248. TIFF_SETGET_C0_FLOAT = 24,
  249. TIFF_SETGET_C0_DOUBLE = 25,
  250. TIFF_SETGET_C0_IFD8 = 26,
  251. TIFF_SETGET_C16_ASCII = 27,
  252. TIFF_SETGET_C16_UINT8 = 28,
  253. TIFF_SETGET_C16_SINT8 = 29,
  254. TIFF_SETGET_C16_UINT16 = 30,
  255. TIFF_SETGET_C16_SINT16 = 31,
  256. TIFF_SETGET_C16_UINT32 = 32,
  257. TIFF_SETGET_C16_SINT32 = 33,
  258. TIFF_SETGET_C16_UINT64 = 34,
  259. TIFF_SETGET_C16_SINT64 = 35,
  260. TIFF_SETGET_C16_FLOAT = 36,
  261. TIFF_SETGET_C16_DOUBLE = 37,
  262. TIFF_SETGET_C16_IFD8 = 38,
  263. TIFF_SETGET_C32_ASCII = 39,
  264. TIFF_SETGET_C32_UINT8 = 40,
  265. TIFF_SETGET_C32_SINT8 = 41,
  266. TIFF_SETGET_C32_UINT16 = 42,
  267. TIFF_SETGET_C32_SINT16 = 43,
  268. TIFF_SETGET_C32_UINT32 = 44,
  269. TIFF_SETGET_C32_SINT32 = 45,
  270. TIFF_SETGET_C32_UINT64 = 46,
  271. TIFF_SETGET_C32_SINT64 = 47,
  272. TIFF_SETGET_C32_FLOAT = 48,
  273. TIFF_SETGET_C32_DOUBLE = 49,
  274. TIFF_SETGET_C32_IFD8 = 50,
  275. TIFF_SETGET_OTHER = 51
  276. } TIFFSetGetFieldType;
  277. #if defined(__cplusplus)
  278. extern "C"
  279. {
  280. #endif
  281. extern const TIFFFieldArray *_TIFFGetFields(void);
  282. extern const TIFFFieldArray *_TIFFGetExifFields(void);
  283. extern const TIFFFieldArray *_TIFFGetGpsFields(void);
  284. extern void _TIFFSetupFields(TIFF *tif, const TIFFFieldArray *infoarray);
  285. extern void _TIFFPrintFieldInfo(TIFF *, FILE *);
  286. extern int _TIFFFillStriles(TIFF *);
  287. typedef enum
  288. {
  289. tfiatImage,
  290. tfiatExif,
  291. tfiatGps, /* EXIF-GPS fields array type */
  292. tfiatOther
  293. } TIFFFieldArrayType;
  294. struct _TIFFFieldArray
  295. {
  296. TIFFFieldArrayType type; /* array type, will be used to determine if IFD
  297. is image and such */
  298. uint32_t allocated_size; /* 0 if array is constant, other if modified by
  299. future definition extension support */
  300. uint32_t count; /* number of elements in fields array */
  301. TIFFField *fields; /* actual field info */
  302. };
  303. struct _TIFFField
  304. {
  305. uint32_t field_tag; /* field's tag */
  306. short field_readcount; /* read count/TIFF_VARIABLE/TIFF_SPP */
  307. short field_writecount; /* write count/TIFF_VARIABLE */
  308. TIFFDataType field_type; /* type of associated data */
  309. uint32_t
  310. field_anonymous; /* if true, this is a unknown / anonymous tag */
  311. TIFFSetGetFieldType set_field_type; /* type to be passed to TIFFSetField
  312. and TIFFGetField*/
  313. TIFFSetGetFieldType get_field_type; /* not used */
  314. unsigned short field_bit; /* bit in fieldsset bit vector */
  315. unsigned char field_oktochange; /* if true, can change while writing */
  316. unsigned char field_passcount; /* if true, pass dir count on set */
  317. char *field_name; /* ASCII name */
  318. TIFFFieldArray *field_subfields; /* if field points to child ifds, child
  319. ifd field definition array */
  320. };
  321. extern int _TIFFMergeFields(TIFF *, const TIFFField[], uint32_t);
  322. extern const TIFFField *_TIFFFindOrRegisterField(TIFF *, uint32_t,
  323. TIFFDataType);
  324. extern TIFFField *_TIFFCreateAnonField(TIFF *, uint32_t, TIFFDataType);
  325. extern int _TIFFCheckFieldIsValidForCodec(TIFF *tif, ttag_t tag);
  326. extern int _TIFFCheckDirNumberAndOffset(TIFF *tif, tdir_t dirn,
  327. uint64_t diroff);
  328. extern int _TIFFGetDirNumberFromOffset(TIFF *tif, uint64_t diroff,
  329. tdir_t *dirn);
  330. extern int _TIFFGetOffsetFromDirNumber(TIFF *tif, tdir_t dirn,
  331. uint64_t *diroff);
  332. extern int _TIFFRemoveEntryFromDirectoryListByOffset(TIFF *tif,
  333. uint64_t diroff);
  334. #if defined(__cplusplus)
  335. }
  336. #endif
  337. #endif /* _TIFFDIR_ */