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