1 /* $Header: /usr/people/sam/tiff/libtiff/RCS/tiffiop.h,v 1.49 1994/09/28 00:54:41 sam Exp $ */
2 
3 /*
4  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994 Sam Leffler
5  * Copyright (c) 1991, 1992, 1993, 1994 Silicon Graphics, Inc.
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and
8  * its documentation for any purpose is hereby granted without fee, provided
9  * that (i) the above copyright notices and this permission notice appear in
10  * all copies of the software and related documentation, and (ii) the names of
11  * Sam Leffler and Silicon Graphics may not be used in any advertising or
12  * publicity relating to the software without the specific, prior written
13  * permission of Sam Leffler and Silicon Graphics.
14  *
15  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  */
26 
27 #ifndef _TIFFIOP_
28 #define	_TIFFIOP_
29 /*
30  * ``Library-private'' definitions.
31  */
32 #include "tiffconf.h"
33 #include "tiffcomp.h"
34 #include "tiffio.h"
35 
36 /*
37  * Internal format of a TIFF directory entry.
38  */
39 typedef	struct {
40 #define	FIELD_SETLONGS	2
41 	/* bit vector of fields that are set */
42 	u_long	td_fieldsset[FIELD_SETLONGS];
43 
44 	uint32	td_imagewidth, td_imagelength, td_imagedepth;
45 	uint32	td_tilewidth, td_tilelength, td_tiledepth;
46 	uint32	td_subfiletype;
47 	uint16	td_bitspersample;
48 	uint16	td_sampleformat;
49 	uint16	td_compression;
50 	uint16	td_photometric;
51 	uint16	td_threshholding;
52 	uint16	td_fillorder;
53 	uint16	td_orientation;
54 	uint16	td_samplesperpixel;
55 	uint16	td_predictor;
56 	uint32	td_rowsperstrip;
57 	uint16	td_minsamplevalue, td_maxsamplevalue;
58 	float	td_xresolution, td_yresolution;
59 	uint16	td_resolutionunit;
60 	uint16	td_planarconfig;
61 	float	td_xposition, td_yposition;
62 	uint32	td_group3options;
63 	uint32	td_group4options;
64 	uint16	td_pagenumber[2];
65 	uint16	td_cleanfaxdata;
66 	uint32	td_badfaxrun;
67 	uint32	td_badfaxlines;
68 	uint16	*td_colormap[3];
69 	uint16	td_halftonehints[2];
70 	uint16	td_extrasamples, *td_sampleinfo;
71 	char	*td_documentname;
72 	char	*td_artist;
73 	char	*td_datetime;
74 	char	*td_hostcomputer;
75 	char	*td_imagedescription;
76 	char	*td_make;
77 	char	*td_model;
78 	char	*td_software;
79 	char	*td_pagename;
80 	tstrip_t td_stripsperimage;
81 	tstrip_t td_nstrips;		/* size of offset & bytecount arrays */
82 	uint32	*td_stripoffset;
83 	uint32	*td_stripbytecount;
84 #if SUBIFD_SUPPORT
85 	uint16	td_nsubifd;
86 	uint32	*td_subifd;
87 #endif
88 #ifdef YCBCR_SUPPORT
89 	float	*td_ycbcrcoeffs;
90 	uint16	td_ycbcrsubsampling[2];
91 	uint16	td_ycbcrpositioning;
92 #endif
93 #ifdef JPEG_SUPPORT
94 	uint16	td_jpegproc;
95 	uint16	td_jpegrestartinterval;
96 	u_char	**td_qtab;
97 	u_char	**td_dctab;
98 	u_char	**td_actab;
99 #endif
100 #ifdef COLORIMETRY_SUPPORT
101 	float	*td_whitepoint;
102 	float	*td_primarychromas;
103 	float	*td_refblackwhite;
104 	uint16	*td_transferfunction[3];
105 #endif
106 #ifdef CMYK_SUPPORT
107 	uint16	td_inkset;
108 	uint16	td_dotrange[2];
109 	char	*td_inknames;
110 	char	*td_targetprinter;
111 #endif
112 } TIFFDirectory;
113 
114 /*
115  * Field flags used to indicate fields that have
116  * been set in a directory, and to reference fields
117  * when manipulating a directory.
118  */
119 /* multi-entry fields */
120 #define	FIELD_IMAGEDIMENSIONS		0
121 #define FIELD_TILEDIMENSIONS		1
122 #define	FIELD_CELLDIMENSIONS		2		/* XXX */
123 #define	FIELD_RESOLUTION		3
124 #define	FIELD_POSITION			4
125 /* single-entry fields */
126 #define	FIELD_SUBFILETYPE		5
127 #define	FIELD_BITSPERSAMPLE		6
128 #define	FIELD_COMPRESSION		7
129 #define	FIELD_PHOTOMETRIC		8
130 #define	FIELD_THRESHHOLDING		9
131 #define	FIELD_FILLORDER			10
132 #define	FIELD_DOCUMENTNAME		11
133 #define	FIELD_IMAGEDESCRIPTION		12
134 #define	FIELD_MAKE			13
135 #define	FIELD_MODEL			14
136 #define	FIELD_ORIENTATION		15
137 #define	FIELD_SAMPLESPERPIXEL		16
138 #define	FIELD_ROWSPERSTRIP		17
139 #define	FIELD_MINSAMPLEVALUE		18
140 #define	FIELD_MAXSAMPLEVALUE		19
141 #define	FIELD_PLANARCONFIG		20
142 #define	FIELD_PAGENAME			21
143 #define	FIELD_GROUP3OPTIONS		22
144 #define	FIELD_GROUP4OPTIONS		23
145 #define	FIELD_RESOLUTIONUNIT		24
146 #define	FIELD_PAGENUMBER		25
147 #define	FIELD_STRIPBYTECOUNTS		26
148 #define	FIELD_STRIPOFFSETS		27
149 #define	FIELD_COLORMAP			28
150 #define FIELD_PREDICTOR			29
151 #define FIELD_ARTIST			30
152 #define FIELD_DATETIME			31
153 #define FIELD_HOSTCOMPUTER		32
154 #define FIELD_SOFTWARE			33
155 #define	FIELD_EXTRASAMPLES		34
156 #define	FIELD_BADFAXLINES		35
157 #define	FIELD_CLEANFAXDATA		36
158 #define	FIELD_BADFAXRUN			37
159 #define FIELD_SAMPLEFORMAT		38
160 #define	FIELD_SMINSAMPLEVALUE		39
161 #define	FIELD_SMAXSAMPLEVALUE		40
162 #define FIELD_IMAGEDEPTH		41
163 #define FIELD_TILEDEPTH			42
164 #define	FIELD_HALFTONEHINTS		43
165 #ifdef YCBCR_SUPPORT
166 #define FIELD_YCBCRCOEFFICIENTS		44
167 #define FIELD_YCBCRSUBSAMPLING		45
168 #define FIELD_YCBCRPOSITIONING		46
169 #endif
170 #ifdef JPEG_SUPPORT
171 #define FIELD_JPEGPROC			47
172 #define FIELD_JPEGRESTARTINTERVAL	48
173 #define FIELD_JPEGQTABLES		49
174 #define FIELD_JPEGDCTABLES		50
175 #define FIELD_JPEGACTABLES		51
176 #endif
177 #ifdef COLORIMETRY_SUPPORT
178 #define	FIELD_REFBLACKWHITE		52
179 #define	FIELD_WHITEPOINT		53
180 #define	FIELD_PRIMARYCHROMAS		54
181 #define	FIELD_TRANSFERFUNCTION		55
182 #endif
183 #ifdef CMYK_SUPPORT
184 #define	FIELD_INKSET			56
185 #define	FIELD_INKNAMES			57
186 #define	FIELD_DOTRANGE			58
187 #define	FIELD_TARGETPRINTER		59
188 #endif
189 #define	FIELD_SUBIFD			60
190 #define	FIELD_LAST			60
191 
192 #define	TIFFExtractData(tif, type, v) \
193     ((uint32) ((tif)->tif_header.tiff_magic == TIFF_BIGENDIAN ? \
194         ((v) >> (tif)->tif_typeshift[type]) & (tif)->tif_typemask[type] : \
195 	(v) & (tif)->tif_typemask[type]))
196 #define	TIFFInsertData(tif, type, v) \
197     ((uint32) ((tif)->tif_header.tiff_magic == TIFF_BIGENDIAN ? \
198         ((v) & (tif)->tif_typemask[type]) << (tif)->tif_typeshift[type] : \
199 	(v) & (tif)->tif_typemask[type]))
200 
201 typedef	struct {
202 	ttag_t	field_tag;		/* field's tag */
203 	short	field_readcount;	/* read count (-1 for unknown) */
204 	short	field_writecount;	/* write count (-1 for unknown) */
205 	TIFFDataType field_type;	/* type of associated data */
206 	u_short	field_bit;		/* bit in fieldsset bit vector */
207 	u_char	field_oktochange;	/* if true, can change while writing */
208 	u_char	field_passcount;	/* if true, pass dir count on set */
209 	char	*field_name;		/* ASCII name */
210 } TIFFFieldInfo;
211 
212 #define	FIELD_IGNORE	((u_short)-1)	/* tags processed but ignored */
213 
214 #define	TIFF_ANY	TIFF_NOTYPE	/* for field descriptor searching */
215 #define	TIFF_VARIABLE	-1		/* marker for variable length tags */
216 #define	TIFF_SPP	-2		/* marker for SamplesPerPixel tags */
217 
218 extern	const TIFFFieldInfo tiffFieldInfo[];/* table of field descriptors */
219 extern	const int tiffDataWidth[];	/* table of tag datatype widths */
220 
221 #define BITn(n)				(((u_long)1L)<<((n)&0x1f))
222 #define BITFIELDn(tif, n)		((tif)->tif_dir.td_fieldsset[(n)/32])
223 #define TIFFFieldSet(tif, field)	(BITFIELDn(tif, field) & BITn(field))
224 #define TIFFSetFieldBit(tif, field)	(BITFIELDn(tif, field) |= BITn(field))
225 #define TIFFClrFieldBit(tif, field)	(BITFIELDn(tif, field) &= ~BITn(field))
226 
227 #define	FieldSet(fields, f)		(fields[(f)/32] & BITn(f))
228 #define	ResetFieldBit(fields, f)	(fields[(f)/32] &= ~BITn(f))
229 
230 /*
231  * Typedefs for ``method pointers'' used internally.
232  */
233 typedef	unsigned char tidataval_t;	/* internal image data value type */
234 typedef	tidataval_t* tidata_t;		/* reference to internal image data */
235 
236 typedef	void (*TIFFVoidMethod)(TIFF*);
237 typedef	int (*TIFFBoolMethod)(TIFF*);
238 typedef	int (*TIFFCodeMethod)(TIFF*, tidata_t, tsize_t, tsample_t);
239 typedef	int (*TIFFSeekMethod)(TIFF*, uint32);
240 typedef	void (*TIFFPostMethod)(TIFF*, tidata_t, tsize_t);
241 
242 struct tiff {
243 	char*		tif_name;	/* name of open file */
244 	short		tif_fd;		/* open file descriptor */
245 	short		tif_mode;	/* open mode (O_*) */
246 	char		tif_fillorder;	/* natural bit fill order for machine */
247 	char		tif_options;	/* compression-specific options */
248 	short		tif_flags;
249 #define	TIFF_DIRTYHEADER	0x1	/* header must be written on close */
250 #define	TIFF_DIRTYDIRECT	0x2	/* current directory must be written */
251 #define	TIFF_BUFFERSETUP	0x4	/* data buffers setup */
252 #define	TIFF_BEENWRITING	0x8	/* written 1+ scanlines to file */
253 #define	TIFF_SWAB		0x10	/* byte swap file information */
254 #define	TIFF_NOBITREV		0x20	/* inhibit bit reversal logic */
255 #define	TIFF_MYBUFFER		0x40	/* my raw data buffer; free on close */
256 #define	TIFF_ISTILED		0x80	/* file is tile, not strip- based */
257 #define	TIFF_MAPPED		0x100	/* file is mapped into memory */
258 #define	TIFF_POSTENCODE		0x200	/* need call to postencode routine */
259 	toff_t		tif_diroff;	/* file offset of current directory */
260 	toff_t		tif_nextdiroff;	/* file offset of following directory */
261 	TIFFDirectory	tif_dir;	/* internal rep of current directory */
262 	TIFFHeader	tif_header;	/* file's header block */
263 	const int*	tif_typeshift;	/* data type shift counts */
264 	const long*	tif_typemask;	/* data type masks */
265 	uint32		tif_row;	/* current scanline */
266 	tdir_t		tif_curdir;	/* current directory (index) */
267 	tstrip_t	tif_curstrip;	/* current strip for read/write */
268 	toff_t		tif_curoff;	/* current offset for read/write */
269 	toff_t		tif_dataoff;	/* current offset for writing dir */
270 #if SUBIFD_SUPPORT
271 #define	TIFF_INSUBIFD		0x400	/* currently writing a subifd */
272 	uint16		tif_nsubifd;	/* remaining subifds to write */
273 	toff_t		tif_subifdoff;	/* offset for patching SubIFD link */
274 #endif
275 /* tiling support */
276 	uint32 		tif_col;	/* current column (offset by row too) */
277 	ttile_t		tif_curtile;	/* current tile for read/write */
278 	tsize_t		tif_tilesize;	/* # of bytes in a tile */
279 /* compression scheme hooks */
280 	TIFFBoolMethod	tif_predecode;	/* pre row/strip/tile decoding */
281 	TIFFBoolMethod	tif_preencode;	/* pre row/strip/tile encoding */
282 	TIFFBoolMethod	tif_postencode;	/* post row/strip/tile encoding */
283 	TIFFCodeMethod	tif_decoderow;	/* scanline decoding routine */
284 	TIFFCodeMethod	tif_encoderow;	/* scanline encoding routine */
285 	TIFFCodeMethod	tif_decodestrip;/* strip decoding routine */
286 	TIFFCodeMethod	tif_encodestrip;/* strip encoding routine */
287 	TIFFCodeMethod	tif_decodetile;	/* tile decoding routine */
288 	TIFFCodeMethod	tif_encodetile;	/* tile encoding routine */
289 	TIFFVoidMethod	tif_close;	/* cleanup-on-close routine */
290 	TIFFSeekMethod	tif_seek;	/* position within a strip routine */
291 	TIFFVoidMethod	tif_cleanup;	/* routine called to cleanup state */
292 	tidata_t	tif_data;	/* compression scheme private data */
293 /* input/output buffering */
294 	tsize_t		tif_scanlinesize;/* # of bytes in a scanline */
295 	tsize_t		tif_scanlineskew;/* scanline skew for reading strips */
296 	tidata_t	tif_rawdata;	/* raw data buffer */
297 	tsize_t		tif_rawdatasize;/* # of bytes in raw data buffer */
298 	tidata_t	tif_rawcp;	/* current spot in raw buffer */
299 	tsize_t		tif_rawcc;	/* bytes unread from raw buffer */
300 /* memory-mapped file support */
301 	tidata_t	tif_base;	/* base of mapped file */
302 	toff_t		tif_size;	/* size of mapped file region (bytes) */
303 	TIFFMapFileProc	tif_mapproc;	/* map file method */
304 	TIFFUnmapFileProc tif_unmapproc;/* unmap file method */
305 /* input/output callback methods */
306 	thandle_t	tif_clientdata;	/* callback parameter */
307 	TIFFReadWriteProc tif_readproc;	/* read method */
308 	TIFFReadWriteProc tif_writeproc;/* write method */
309 	TIFFSeekProc	tif_seekproc;	/* lseek method */
310 	TIFFCloseProc	tif_closeproc;	/* close method */
311 	TIFFSizeProc	tif_sizeproc;	/* filesize method */
312 /* post-decoding support */
313 	TIFFPostMethod	tif_postdecode;/* post decoding routine */
314 };
315 
316 #define	isTiled(tif)	(((tif)->tif_flags & TIFF_ISTILED) != 0)
317 #define	isMapped(tif)	(((tif)->tif_flags & TIFF_MAPPED) != 0)
318 #define	TIFFReadFile(tif, buf, size) \
319 	((*(tif)->tif_readproc)((tif)->tif_clientdata,buf,size))
320 #define	TIFFWriteFile(tif, buf, size) \
321 	((*(tif)->tif_writeproc)((tif)->tif_clientdata,buf,size))
322 #define	TIFFSeekFile(tif, off, whence) \
323 	((*(tif)->tif_seekproc)((tif)->tif_clientdata,(toff_t)(off),whence))
324 #define	TIFFCloseFile(tif) \
325 	((*(tif)->tif_closeproc)((tif)->tif_clientdata))
326 #define	TIFFGetFileSize(tif) \
327 	((*(tif)->tif_sizeproc)((tif)->tif_clientdata))
328 #define	TIFFMapFileContents(tif, paddr, psize) \
329 	((*(tif)->tif_mapproc)((tif)->tif_clientdata,paddr,psize))
330 #define	TIFFUnmapFileContents(tif, addr, size) \
331 	((*(tif)->tif_unmapproc)((tif)->tif_clientdata,addr,size))
332 
333 /*
334  * Default Read/Seek/Write definitions.
335  */
336 #ifndef ReadOK
337 #define	ReadOK(tif, buf, size) \
338 	(TIFFReadFile(tif, (tdata_t) buf, (tsize_t) size) == (tsize_t) size)
339 #endif
340 #ifndef SeekOK
341 #define	SeekOK(tif, off) \
342 	(TIFFSeekFile(tif, (toff_t) off, L_SET) == (toff_t) off)
343 #endif
344 #ifndef WriteOK
345 #define	WriteOK(tif, buf, size) \
346 	(TIFFWriteFile(tif, (tdata_t) buf, (tsize_t) size) == (tsize_t) size)
347 #endif
348 
349 /* generic option bit names */
350 #define	TIFF_OPT0	0x1
351 #define	TIFF_OPT1	0x2
352 #define	TIFF_OPT2	0x4
353 #define	TIFF_OPT3	0x8
354 #define	TIFF_OPT4	0x10
355 #define	TIFF_OPT5	0x20
356 #define	TIFF_OPT6	0x40
357 #define	TIFF_OPT7	0x80
358 
359 /* NB: the uint32 casts are to silence certain ANSI-C compilers */
360 #ifdef howmany
361 #undef howmany
362 #endif
363 #define	howmany(x, y)	((((uint32)(x))+(((uint32)(y))-1))/((uint32)(y)))
364 #ifdef roundup
365 #undef roundup
366 #endif
367 #define	roundup(x, y)	(howmany(x,y)*((uint32)(y)))
368 
369 #if defined(__cplusplus)
370 extern "C" {
371 #endif
372 extern	void* _TIFFmalloc(size_t);
373 extern	void* _TIFFrealloc(void*, size_t);
374 extern	void _TIFFmemset(void*, int, size_t);
375 extern	void _TIFFmemcpy(void*, const void*, size_t);
376 extern	int _TIFFmemcmp(const void*, const void*, size_t);
377 extern	void _TIFFfree(void*);
378 extern	int _TIFFgetMode(const char* mode, const char* module);
379 extern	const TIFFFieldInfo *TIFFFindFieldInfo(ttag_t, TIFFDataType);
380 extern	const TIFFFieldInfo *TIFFFieldWithTag(ttag_t);
381 extern	void _TIFFgetfield(TIFFDirectory*, ttag_t, ...);
382 extern	int TIFFNoRowEncode(TIFF*, tidata_t, tsize_t, tsample_t);
383 extern	int TIFFNoStripEncode(TIFF*, tidata_t, tsize_t, tsample_t);
384 extern	int TIFFNoTileEncode(TIFF*, tidata_t, tsize_t, tsample_t);
385 extern	int TIFFNoRowDecode(TIFF*, tidata_t, tsize_t, tsample_t);
386 extern	int TIFFNoStripDecode(TIFF*, tidata_t, tsize_t, tsample_t);
387 extern	int TIFFNoTileDecode(TIFF*, tidata_t, tsize_t, tsample_t);
388 extern	void TIFFNoPostDecode(TIFF*, tidata_t, tsize_t);
389 extern	void TIFFSwab16BitData(TIFF*, tidata_t, tsize_t);
390 extern	void TIFFSwab32BitData(TIFF*, tidata_t, tsize_t);
391 extern	int TIFFFlushData1(TIFF*);
392 extern	void TIFFFreeDirectory(TIFF*);
393 extern	int TIFFDefaultDirectory(TIFF*);
394 extern	int TIFFSetCompressionScheme(TIFF *, int);
395 
396 extern	TIFFErrorHandler _TIFFwarningHandler;
397 extern	TIFFErrorHandler _TIFFerrorHandler;
398 
399 extern	int TIFFInitDumpMode(TIFF*);
400 #ifdef PACKBITS_SUPPORT
401 extern	int TIFFInitPackBits(TIFF*);
402 #endif
403 #ifdef CCITT_SUPPORT
404 extern	int TIFFInitCCITTRLE(TIFF*), TIFFInitCCITTRLEW(TIFF*);
405 extern	int TIFFInitCCITTFax3(TIFF*), TIFFInitCCITTFax4(TIFF*);
406 #endif
407 #ifdef THUNDER_SUPPORT
408 extern	int TIFFInitThunderScan(TIFF*);
409 #endif
410 #ifdef NEXT_SUPPORT
411 extern	int TIFFInitNeXT(TIFF*);
412 #endif
413 #ifdef LZW_SUPPORT
414 extern	int TIFFInitLZW(TIFF*);
415 #endif
416 #ifdef JPEG_SUPPORT
417 extern	int TIFFInitJPEG(TIFF*);
418 #endif
419 #if defined(__cplusplus)
420 }
421 #endif
422 #endif /* _TIFFIOP_ */
423