tiffiop.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* $Id: tiffiop.h,v 1.84 2012-05-30 01:50:17 fwarmerdam Exp $ */
  2. /*
  3. * Copyright (c) 1988-1997 Sam Leffler
  4. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and
  7. * its documentation for any purpose is hereby granted without fee, provided
  8. * that (i) the above copyright notices and this permission notice appear in
  9. * all copies of the software and related documentation, and (ii) the names of
  10. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11. * publicity relating to the software without the specific, prior written
  12. * permission of Sam Leffler and Silicon Graphics.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  22. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. */
  25. #ifndef _TIFFIOP_
  26. #define _TIFFIOP_
  27. /*
  28. * ``Library-private'' definitions.
  29. */
  30. #include "tif_config.h"
  31. #ifdef HAVE_FCNTL_H
  32. # include <fcntl.h>
  33. #endif
  34. #ifdef HAVE_SYS_TYPES_H
  35. # include <sys/types.h>
  36. #endif
  37. #ifdef HAVE_STRING_H
  38. # include <string.h>
  39. #endif
  40. #ifdef HAVE_ASSERT_H
  41. # include <assert.h>
  42. #else
  43. # define assert(x)
  44. #endif
  45. #ifdef HAVE_SEARCH_H
  46. # include <search.h>
  47. #else
  48. extern void *lfind(const void *, const void *, size_t *, size_t,
  49. int (*)(const void *, const void *));
  50. #endif
  51. #include "tiffio.h"
  52. #include "tif_dir.h"
  53. #ifndef STRIP_SIZE_DEFAULT
  54. # define STRIP_SIZE_DEFAULT 8192
  55. #endif
  56. #define streq(a,b) (strcmp(a,b) == 0)
  57. #ifndef TRUE
  58. #define TRUE 1
  59. #define FALSE 0
  60. #endif
  61. typedef struct client_info {
  62. struct client_info *next;
  63. void *data;
  64. char *name;
  65. } TIFFClientInfoLink;
  66. /*
  67. * Typedefs for ``method pointers'' used internally.
  68. * these are depriciated and provided only for backwards compatibility
  69. */
  70. typedef unsigned char tidataval_t; /* internal image data value type */
  71. typedef tidataval_t* tidata_t; /* reference to internal image data */
  72. typedef void (*TIFFVoidMethod)(TIFF*);
  73. typedef int (*TIFFBoolMethod)(TIFF*);
  74. typedef int (*TIFFPreMethod)(TIFF*, uint16);
  75. typedef int (*TIFFCodeMethod)(TIFF* tif, uint8* buf, tmsize_t size, uint16 sample);
  76. typedef int (*TIFFSeekMethod)(TIFF*, uint32);
  77. typedef void (*TIFFPostMethod)(TIFF* tif, uint8* buf, tmsize_t size);
  78. typedef uint32 (*TIFFStripMethod)(TIFF*, uint32);
  79. typedef void (*TIFFTileMethod)(TIFF*, uint32*, uint32*);
  80. struct tiff {
  81. char* tif_name; /* name of open file */
  82. int tif_fd; /* open file descriptor */
  83. int tif_mode; /* open mode (O_*) */
  84. uint32 tif_flags;
  85. #define TIFF_FILLORDER 0x00003 /* natural bit fill order for machine */
  86. #define TIFF_DIRTYHEADER 0x00004 /* header must be written on close */
  87. #define TIFF_DIRTYDIRECT 0x00008 /* current directory must be written */
  88. #define TIFF_BUFFERSETUP 0x00010 /* data buffers setup */
  89. #define TIFF_CODERSETUP 0x00020 /* encoder/decoder setup done */
  90. #define TIFF_BEENWRITING 0x00040 /* written 1+ scanlines to file */
  91. #define TIFF_SWAB 0x00080 /* byte swap file information */
  92. #define TIFF_NOBITREV 0x00100 /* inhibit bit reversal logic */
  93. #define TIFF_MYBUFFER 0x00200 /* my raw data buffer; free on close */
  94. #define TIFF_ISTILED 0x00400 /* file is tile, not strip- based */
  95. #define TIFF_MAPPED 0x00800 /* file is mapped into memory */
  96. #define TIFF_POSTENCODE 0x01000 /* need call to postencode routine */
  97. #define TIFF_INSUBIFD 0x02000 /* currently writing a subifd */
  98. #define TIFF_UPSAMPLED 0x04000 /* library is doing data up-sampling */
  99. #define TIFF_STRIPCHOP 0x08000 /* enable strip chopping support */
  100. #define TIFF_HEADERONLY 0x10000 /* read header only, do not process the first directory */
  101. #define TIFF_NOREADRAW 0x20000 /* skip reading of raw uncompressed image data */
  102. #define TIFF_INCUSTOMIFD 0x40000 /* currently writing a custom IFD */
  103. #define TIFF_BIGTIFF 0x80000 /* read/write bigtiff */
  104. #define TIFF_BUF4WRITE 0x100000 /* rawcc bytes are for writing */
  105. #define TIFF_DIRTYSTRIP 0x200000 /* stripoffsets/stripbytecount dirty*/
  106. #define TIFF_PERSAMPLE 0x400000 /* get/set per sample tags as arrays */
  107. #define TIFF_BUFFERMMAP 0x800000 /* read buffer (tif_rawdata) points into mmap() memory */
  108. uint64 tif_diroff; /* file offset of current directory */
  109. uint64 tif_nextdiroff; /* file offset of following directory */
  110. uint64* tif_dirlist; /* list of offsets to already seen directories to prevent IFD looping */
  111. uint16 tif_dirlistsize; /* number of entires in offset list */
  112. uint16 tif_dirnumber; /* number of already seen directories */
  113. TIFFDirectory tif_dir; /* internal rep of current directory */
  114. TIFFDirectory tif_customdir; /* custom IFDs are separated from the main ones */
  115. union {
  116. TIFFHeaderCommon common;
  117. TIFFHeaderClassic classic;
  118. TIFFHeaderBig big;
  119. } tif_header;
  120. uint16 tif_header_size; /* file's header block and its length */
  121. uint32 tif_row; /* current scanline */
  122. uint16 tif_curdir; /* current directory (index) */
  123. uint32 tif_curstrip; /* current strip for read/write */
  124. uint64 tif_curoff; /* current offset for read/write */
  125. uint64 tif_dataoff; /* current offset for writing dir */
  126. /* SubIFD support */
  127. uint16 tif_nsubifd; /* remaining subifds to write */
  128. uint64 tif_subifdoff; /* offset for patching SubIFD link */
  129. /* tiling support */
  130. uint32 tif_col; /* current column (offset by row too) */
  131. uint32 tif_curtile; /* current tile for read/write */
  132. tmsize_t tif_tilesize; /* # of bytes in a tile */
  133. /* compression scheme hooks */
  134. int tif_decodestatus;
  135. TIFFBoolMethod tif_fixuptags; /* called in TIFFReadDirectory */
  136. TIFFBoolMethod tif_setupdecode; /* called once before predecode */
  137. TIFFPreMethod tif_predecode; /* pre- row/strip/tile decoding */
  138. TIFFBoolMethod tif_setupencode; /* called once before preencode */
  139. int tif_encodestatus;
  140. TIFFPreMethod tif_preencode; /* pre- row/strip/tile encoding */
  141. TIFFBoolMethod tif_postencode; /* post- row/strip/tile encoding */
  142. TIFFCodeMethod tif_decoderow; /* scanline decoding routine */
  143. TIFFCodeMethod tif_encoderow; /* scanline encoding routine */
  144. TIFFCodeMethod tif_decodestrip; /* strip decoding routine */
  145. TIFFCodeMethod tif_encodestrip; /* strip encoding routine */
  146. TIFFCodeMethod tif_decodetile; /* tile decoding routine */
  147. TIFFCodeMethod tif_encodetile; /* tile encoding routine */
  148. TIFFVoidMethod tif_close; /* cleanup-on-close routine */
  149. TIFFSeekMethod tif_seek; /* position within a strip routine */
  150. TIFFVoidMethod tif_cleanup; /* cleanup state routine */
  151. TIFFStripMethod tif_defstripsize; /* calculate/constrain strip size */
  152. TIFFTileMethod tif_deftilesize; /* calculate/constrain tile size */
  153. uint8* tif_data; /* compression scheme private data */
  154. /* input/output buffering */
  155. tmsize_t tif_scanlinesize; /* # of bytes in a scanline */
  156. tmsize_t tif_scanlineskew; /* scanline skew for reading strips */
  157. uint8* tif_rawdata; /* raw data buffer */
  158. tmsize_t tif_rawdatasize; /* # of bytes in raw data buffer */
  159. tmsize_t tif_rawdataoff; /* rawdata offset within strip */
  160. tmsize_t tif_rawdataloaded;/* amount of data in rawdata */
  161. uint8* tif_rawcp; /* current spot in raw buffer */
  162. tmsize_t tif_rawcc; /* bytes unread from raw buffer */
  163. /* memory-mapped file support */
  164. uint8* tif_base; /* base of mapped file */
  165. tmsize_t tif_size; /* size of mapped file region (bytes, thus tmsize_t) */
  166. TIFFMapFileProc tif_mapproc; /* map file method */
  167. TIFFUnmapFileProc tif_unmapproc; /* unmap file method */
  168. /* input/output callback methods */
  169. thandle_t tif_clientdata; /* callback parameter */
  170. TIFFReadWriteProc tif_readproc; /* read method */
  171. TIFFReadWriteProc tif_writeproc; /* write method */
  172. TIFFSeekProc tif_seekproc; /* lseek method */
  173. TIFFCloseProc tif_closeproc; /* close method */
  174. TIFFSizeProc tif_sizeproc; /* filesize method */
  175. /* post-decoding support */
  176. TIFFPostMethod tif_postdecode; /* post decoding routine */
  177. /* tag support */
  178. TIFFField** tif_fields; /* sorted table of registered tags */
  179. size_t tif_nfields; /* # entries in registered tag table */
  180. const TIFFField* tif_foundfield; /* cached pointer to already found tag */
  181. TIFFTagMethods tif_tagmethods; /* tag get/set/print routines */
  182. TIFFClientInfoLink* tif_clientinfo; /* extra client information. */
  183. /* Backward compatibility stuff. We need these two fields for
  184. * setting up an old tag extension scheme. */
  185. TIFFFieldArray* tif_fieldscompat;
  186. size_t tif_nfieldscompat;
  187. };
  188. #define isPseudoTag(t) (t > 0xffff) /* is tag value normal or pseudo */
  189. #define isTiled(tif) (((tif)->tif_flags & TIFF_ISTILED) != 0)
  190. #define isMapped(tif) (((tif)->tif_flags & TIFF_MAPPED) != 0)
  191. #define isFillOrder(tif, o) (((tif)->tif_flags & (o)) != 0)
  192. #define isUpSampled(tif) (((tif)->tif_flags & TIFF_UPSAMPLED) != 0)
  193. #define TIFFReadFile(tif, buf, size) \
  194. ((*(tif)->tif_readproc)((tif)->tif_clientdata,(buf),(size)))
  195. #define TIFFWriteFile(tif, buf, size) \
  196. ((*(tif)->tif_writeproc)((tif)->tif_clientdata,(buf),(size)))
  197. #define TIFFSeekFile(tif, off, whence) \
  198. ((*(tif)->tif_seekproc)((tif)->tif_clientdata,(off),(whence)))
  199. #define TIFFCloseFile(tif) \
  200. ((*(tif)->tif_closeproc)((tif)->tif_clientdata))
  201. #define TIFFGetFileSize(tif) \
  202. ((*(tif)->tif_sizeproc)((tif)->tif_clientdata))
  203. #define TIFFMapFileContents(tif, paddr, psize) \
  204. ((*(tif)->tif_mapproc)((tif)->tif_clientdata,(paddr),(psize)))
  205. #define TIFFUnmapFileContents(tif, addr, size) \
  206. ((*(tif)->tif_unmapproc)((tif)->tif_clientdata,(addr),(size)))
  207. /*
  208. * Default Read/Seek/Write definitions.
  209. */
  210. #ifndef ReadOK
  211. #define ReadOK(tif, buf, size) \
  212. (TIFFReadFile((tif),(buf),(size))==(size))
  213. #endif
  214. #ifndef SeekOK
  215. #define SeekOK(tif, off) \
  216. (TIFFSeekFile((tif),(off),SEEK_SET)==(off))
  217. #endif
  218. #ifndef WriteOK
  219. #define WriteOK(tif, buf, size) \
  220. (TIFFWriteFile((tif),(buf),(size))==(size))
  221. #endif
  222. /* NB: the uint32 casts are to silence certain ANSI-C compilers */
  223. #define TIFFhowmany_32(x, y) (((uint32)x < (0xffffffff - (uint32)(y-1))) ? \
  224. ((((uint32)(x))+(((uint32)(y))-1))/((uint32)(y))) : \
  225. 0U)
  226. #define TIFFhowmany8_32(x) (((x)&0x07)?((uint32)(x)>>3)+1:(uint32)(x)>>3)
  227. #define TIFFroundup_32(x, y) (TIFFhowmany_32(x,y)*(y))
  228. #define TIFFhowmany_64(x, y) ((((uint64)(x))+(((uint64)(y))-1))/((uint64)(y)))
  229. #define TIFFhowmany8_64(x) (((x)&0x07)?((uint64)(x)>>3)+1:(uint64)(x)>>3)
  230. #define TIFFroundup_64(x, y) (TIFFhowmany_64(x,y)*(y))
  231. /* Safe multiply which returns zero if there is an integer overflow */
  232. #define TIFFSafeMultiply(t,v,m) ((((t)(m) != (t)0) && (((t)(((v)*(m))/(m))) == (t)(v))) ? (t)((v)*(m)) : (t)0)
  233. #define TIFFmax(A,B) ((A)>(B)?(A):(B))
  234. #define TIFFmin(A,B) ((A)<(B)?(A):(B))
  235. #define TIFFArrayCount(a) (sizeof (a) / sizeof ((a)[0]))
  236. #if defined(__cplusplus)
  237. extern "C" {
  238. #endif
  239. extern int _TIFFgetMode(const char* mode, const char* module);
  240. extern int _TIFFNoRowEncode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s);
  241. extern int _TIFFNoStripEncode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s);
  242. extern int _TIFFNoTileEncode(TIFF*, uint8* pp, tmsize_t cc, uint16 s);
  243. extern int _TIFFNoRowDecode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s);
  244. extern int _TIFFNoStripDecode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s);
  245. extern int _TIFFNoTileDecode(TIFF*, uint8* pp, tmsize_t cc, uint16 s);
  246. extern void _TIFFNoPostDecode(TIFF* tif, uint8* buf, tmsize_t cc);
  247. extern int _TIFFNoPreCode(TIFF* tif, uint16 s);
  248. extern int _TIFFNoSeek(TIFF* tif, uint32 off);
  249. extern void _TIFFSwab16BitData(TIFF* tif, uint8* buf, tmsize_t cc);
  250. extern void _TIFFSwab24BitData(TIFF* tif, uint8* buf, tmsize_t cc);
  251. extern void _TIFFSwab32BitData(TIFF* tif, uint8* buf, tmsize_t cc);
  252. extern void _TIFFSwab64BitData(TIFF* tif, uint8* buf, tmsize_t cc);
  253. extern int TIFFFlushData1(TIFF* tif);
  254. extern int TIFFDefaultDirectory(TIFF* tif);
  255. extern void _TIFFSetDefaultCompressionState(TIFF* tif);
  256. extern int _TIFFRewriteField(TIFF *, uint16, TIFFDataType, tmsize_t, void *);
  257. extern int TIFFSetCompressionScheme(TIFF* tif, int scheme);
  258. extern int TIFFSetDefaultCompressionState(TIFF* tif);
  259. extern uint32 _TIFFDefaultStripSize(TIFF* tif, uint32 s);
  260. extern void _TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th);
  261. extern int _TIFFDataSize(TIFFDataType type);
  262. extern void _TIFFsetByteArray(void**, void*, uint32);
  263. extern void _TIFFsetString(char**, char*);
  264. extern void _TIFFsetShortArray(uint16**, uint16*, uint32);
  265. extern void _TIFFsetLongArray(uint32**, uint32*, uint32);
  266. extern void _TIFFsetFloatArray(float**, float*, uint32);
  267. extern void _TIFFsetDoubleArray(double**, double*, uint32);
  268. extern void _TIFFprintAscii(FILE*, const char*);
  269. extern void _TIFFprintAsciiTag(FILE*, const char*, const char*);
  270. extern TIFFErrorHandler _TIFFwarningHandler;
  271. extern TIFFErrorHandler _TIFFerrorHandler;
  272. extern TIFFErrorHandlerExt _TIFFwarningHandlerExt;
  273. extern TIFFErrorHandlerExt _TIFFerrorHandlerExt;
  274. extern uint32 _TIFFMultiply32(TIFF*, uint32, uint32, const char*);
  275. extern uint64 _TIFFMultiply64(TIFF*, uint64, uint64, const char*);
  276. extern void* _TIFFCheckMalloc(TIFF*, tmsize_t, tmsize_t, const char*);
  277. extern void* _TIFFCheckRealloc(TIFF*, void*, tmsize_t, tmsize_t, const char*);
  278. extern double _TIFFUInt64ToDouble(uint64);
  279. extern float _TIFFUInt64ToFloat(uint64);
  280. extern int TIFFInitDumpMode(TIFF*, int);
  281. #ifdef PACKBITS_SUPPORT
  282. extern int TIFFInitPackBits(TIFF*, int);
  283. #endif
  284. #ifdef CCITT_SUPPORT
  285. extern int TIFFInitCCITTRLE(TIFF*, int), TIFFInitCCITTRLEW(TIFF*, int);
  286. extern int TIFFInitCCITTFax3(TIFF*, int), TIFFInitCCITTFax4(TIFF*, int);
  287. #endif
  288. #ifdef THUNDER_SUPPORT
  289. extern int TIFFInitThunderScan(TIFF*, int);
  290. #endif
  291. #ifdef NEXT_SUPPORT
  292. extern int TIFFInitNeXT(TIFF*, int);
  293. #endif
  294. #ifdef LZW_SUPPORT
  295. extern int TIFFInitLZW(TIFF*, int);
  296. #endif
  297. #ifdef OJPEG_SUPPORT
  298. extern int TIFFInitOJPEG(TIFF*, int);
  299. #endif
  300. #ifdef JPEG_SUPPORT
  301. extern int TIFFInitJPEG(TIFF*, int);
  302. #endif
  303. #ifdef JBIG_SUPPORT
  304. extern int TIFFInitJBIG(TIFF*, int);
  305. #endif
  306. #ifdef ZIP_SUPPORT
  307. extern int TIFFInitZIP(TIFF*, int);
  308. #endif
  309. #ifdef PIXARLOG_SUPPORT
  310. extern int TIFFInitPixarLog(TIFF*, int);
  311. #endif
  312. #ifdef LOGLUV_SUPPORT
  313. extern int TIFFInitSGILog(TIFF*, int);
  314. #endif
  315. #ifdef LZMA_SUPPORT
  316. extern int TIFFInitLZMA(TIFF*, int);
  317. #endif
  318. #ifdef VMS
  319. extern const TIFFCodec _TIFFBuiltinCODECS[];
  320. #else
  321. extern TIFFCodec _TIFFBuiltinCODECS[];
  322. #endif
  323. #if defined(__cplusplus)
  324. }
  325. #endif
  326. #endif /* _TIFFIOP_ */
  327. /* vim: set ts=8 sts=8 sw=8 noet: */
  328. /*
  329. * Local Variables:
  330. * mode: c
  331. * c-basic-offset: 8
  332. * fill-column: 78
  333. * End:
  334. */