3 * Copyright (c) 1988-1997 Sam Leffler
4 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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.
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.
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
32 * Dummy functions to fill the omitted client procedures.
35 _tiffDummyMapProc(thandle_t fd
, void** pbase
, toff_t
* psize
)
37 (void) fd
; (void) pbase
; (void) psize
;
42 _tiffDummyUnmapProc(thandle_t fd
, void* base
, toff_t size
)
44 (void) fd
; (void) base
; (void) size
;
48 _TIFFgetMode(const char* mode
, const char* module)
65 TIFFErrorExt(0, module, "\"%s\": Bad mode", mode
);
73 const char* name
, const char* mode
,
75 TIFFReadWriteProc readproc
,
76 TIFFReadWriteProc writeproc
,
77 TIFFSeekProc seekproc
,
78 TIFFCloseProc closeproc
,
79 TIFFSizeProc sizeproc
,
80 TIFFMapFileProc mapproc
,
81 TIFFUnmapFileProc unmapproc
84 static const char module[] = "TIFFClientOpen";
89 /* The following are configuration checks. They should be redundant, but should not
90 * compile to any actual code in an optimised release build anyway. If any of them
91 * fail, (makefile-based or other) configuration is not correct */
92 assert(sizeof(uint8
)==1);
93 assert(sizeof(int8
)==1);
94 assert(sizeof(uint16
)==2);
95 assert(sizeof(int16
)==2);
96 assert(sizeof(uint32
)==4);
97 assert(sizeof(int32
)==4);
98 assert(sizeof(uint64
)==8);
99 assert(sizeof(int64
)==8);
100 assert(sizeof(tmsize_t
)==sizeof(void*));
108 #ifdef WORDS_BIGENDIAN
115 m
= _TIFFgetMode(mode
, module);
118 tif
= (TIFF
*)_TIFFmalloc((tmsize_t
)(sizeof (TIFF
) + strlen(name
) + 1));
120 TIFFErrorExt(clientdata
, module, "%s: Out of memory (TIFF structure)", name
);
123 _TIFFmemset(tif
, 0, sizeof (*tif
));
124 tif
->tif_name
= (char *)tif
+ sizeof (TIFF
);
125 strcpy(tif
->tif_name
, name
);
126 tif
->tif_mode
= m
&~ (O_CREAT
|O_TRUNC
);
127 tif
->tif_curdir
= (uint16
) -1; /* non-existent directory */
129 tif
->tif_curstrip
= (uint32
) -1; /* invalid strip */
130 tif
->tif_row
= (uint32
) -1; /* read/write pre-increment */
131 tif
->tif_clientdata
= clientdata
;
132 if (!readproc
|| !writeproc
|| !seekproc
|| !closeproc
|| !sizeproc
) {
133 TIFFErrorExt(clientdata
, module,
134 "One of the client procedures is NULL pointer.");
137 tif
->tif_readproc
= readproc
;
138 tif
->tif_writeproc
= writeproc
;
139 tif
->tif_seekproc
= seekproc
;
140 tif
->tif_closeproc
= closeproc
;
141 tif
->tif_sizeproc
= sizeproc
;
143 tif
->tif_mapproc
= mapproc
;
145 tif
->tif_mapproc
= _tiffDummyMapProc
;
147 tif
->tif_unmapproc
= unmapproc
;
149 tif
->tif_unmapproc
= _tiffDummyUnmapProc
;
150 _TIFFSetDefaultCompressionState(tif
); /* setup default state */
152 * Default is to return data MSB2LSB and enable the
153 * use of memory-mapped files and strip chopping when
154 * a file is opened read-only.
156 tif
->tif_flags
= FILLORDER_MSB2LSB
;
158 tif
->tif_flags
|= TIFF_MAPPED
;
160 #ifdef STRIPCHOP_DEFAULT
161 if (m
== O_RDONLY
|| m
== O_RDWR
)
162 tif
->tif_flags
|= STRIPCHOP_DEFAULT
;
166 * Process library-specific flags in the open mode string.
167 * The following flags may be used to control intrinsic library
168 * behaviour that may or may not be desirable (usually for
169 * compatibility with some application that claims to support
170 * TIFF but only supports some braindead idea of what the
171 * vendor thinks TIFF is):
173 * 'l' use little-endian byte order for creating a file
174 * 'b' use big-endian byte order for creating a file
175 * 'L' read/write information using LSB2MSB bit order
176 * 'B' read/write information using MSB2LSB bit order
177 * 'H' read/write information using host bit order
178 * 'M' enable use of memory-mapped files when supported
179 * 'm' disable use of memory-mapped files
180 * 'C' enable strip chopping support when reading
181 * 'c' disable strip chopping support
182 * 'h' read TIFF header only, do not load the first IFD
183 * '4' ClassicTIFF for creating a file (default)
184 * '8' BigTIFF for creating a file
186 * The use of the 'l' and 'b' flags is strongly discouraged.
187 * These flags are provided solely because numerous vendors,
188 * typically on the PC, do not correctly support TIFF; they
189 * only support the Intel little-endian byte order. This
190 * support is not configured by default because it supports
191 * the violation of the TIFF spec that says that readers *MUST*
192 * support both byte orders. It is strongly recommended that
193 * you not use this feature except to deal with busted apps
194 * that write invalid TIFF. And even in those cases you should
195 * bang on the vendors to fix their software.
197 * The 'L', 'B', and 'H' flags are intended for applications
198 * that can optimize operations on data by using a particular
199 * bit order. By default the library returns data in MSB2LSB
200 * bit order for compatibiltiy with older versions of this
201 * library. Returning data in the bit order of the native cpu
202 * makes the most sense but also requires applications to check
203 * the value of the FillOrder tag; something they probably do
206 * The 'M' and 'm' flags are provided because some virtual memory
207 * systems exhibit poor behaviour when large images are mapped.
208 * These options permit clients to control the use of memory-mapped
209 * files on a per-file basis.
211 * The 'C' and 'c' flags are provided because the library support
212 * for chopping up large strips into multiple smaller strips is not
213 * application-transparent and as such can cause problems. The 'c'
214 * option permits applications that only want to look at the tags,
215 * for example, to get the unadulterated TIFF tag information.
217 for (cp
= mode
; *cp
; cp
++)
220 #ifndef WORDS_BIGENDIAN
222 tif
->tif_flags
|= TIFF_SWAB
;
226 #ifdef WORDS_BIGENDIAN
228 tif
->tif_flags
|= TIFF_SWAB
;
232 tif
->tif_flags
= (tif
->tif_flags
&~ TIFF_FILLORDER
) |
236 tif
->tif_flags
= (tif
->tif_flags
&~ TIFF_FILLORDER
) |
240 tif
->tif_flags
= (tif
->tif_flags
&~ TIFF_FILLORDER
) |
245 tif
->tif_flags
|= TIFF_MAPPED
;
249 tif
->tif_flags
&= ~TIFF_MAPPED
;
253 tif
->tif_flags
|= TIFF_STRIPCHOP
;
257 tif
->tif_flags
&= ~TIFF_STRIPCHOP
;
260 tif
->tif_flags
|= TIFF_HEADERONLY
;
264 tif
->tif_flags
|= TIFF_BIGTIFF
;
268 * Read in TIFF header.
271 !ReadOK(tif
, &tif
->tif_header
, sizeof (TIFFHeaderClassic
))) {
272 if (tif
->tif_mode
== O_RDONLY
) {
273 TIFFErrorExt(tif
->tif_clientdata
, name
,
274 "Cannot read TIFF header");
278 * Setup header and write.
280 #ifdef WORDS_BIGENDIAN
281 tif
->tif_header
.common
.tiff_magic
= tif
->tif_flags
& TIFF_SWAB
282 ? TIFF_LITTLEENDIAN
: TIFF_BIGENDIAN
;
284 tif
->tif_header
.common
.tiff_magic
= tif
->tif_flags
& TIFF_SWAB
285 ? TIFF_BIGENDIAN
: TIFF_LITTLEENDIAN
;
287 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
289 tif
->tif_header
.common
.tiff_version
= TIFF_VERSION_CLASSIC
;
290 tif
->tif_header
.classic
.tiff_diroff
= 0;
291 if (tif
->tif_flags
& TIFF_SWAB
)
292 TIFFSwabShort(&tif
->tif_header
.common
.tiff_version
);
293 tif
->tif_header_size
= sizeof(TIFFHeaderClassic
);
297 tif
->tif_header
.common
.tiff_version
= TIFF_VERSION_BIG
;
298 tif
->tif_header
.big
.tiff_offsetsize
= 8;
299 tif
->tif_header
.big
.tiff_unused
= 0;
300 tif
->tif_header
.big
.tiff_diroff
= 0;
301 if (tif
->tif_flags
& TIFF_SWAB
)
303 TIFFSwabShort(&tif
->tif_header
.common
.tiff_version
);
304 TIFFSwabShort(&tif
->tif_header
.big
.tiff_offsetsize
);
306 tif
->tif_header_size
= sizeof (TIFFHeaderBig
);
309 * The doc for "fopen" for some STD_C_LIBs says that if you
310 * open a file for modify ("+"), then you must fseek (or
311 * fflush?) between any freads and fwrites. This is not
312 * necessary on most systems, but has been shown to be needed
315 TIFFSeekFile( tif
, 0, SEEK_SET
);
316 if (!WriteOK(tif
, &tif
->tif_header
, (tmsize_t
)(tif
->tif_header_size
))) {
317 TIFFErrorExt(tif
->tif_clientdata
, name
,
318 "Error writing TIFF header");
322 * Setup the byte order handling.
324 if (tif
->tif_header
.common
.tiff_magic
== TIFF_BIGENDIAN
) {
325 #ifndef WORDS_BIGENDIAN
326 tif
->tif_flags
|= TIFF_SWAB
;
329 #ifdef WORDS_BIGENDIAN
330 tif
->tif_flags
|= TIFF_SWAB
;
334 * Setup default directory.
336 if (!TIFFDefaultDirectory(tif
))
339 tif
->tif_dirlist
= NULL
;
340 tif
->tif_dirlistsize
= 0;
341 tif
->tif_dirnumber
= 0;
345 * Setup the byte order handling.
347 if (tif
->tif_header
.common
.tiff_magic
!= TIFF_BIGENDIAN
&&
348 tif
->tif_header
.common
.tiff_magic
!= TIFF_LITTLEENDIAN
352 tif
->tif_header
.common
.tiff_magic
!= MDI_BIGENDIAN
354 tif
->tif_header
.common
.tiff_magic
!= MDI_LITTLEENDIAN
357 TIFFErrorExt(tif
->tif_clientdata
, name
,
358 "Not a TIFF or MDI file, bad magic number %d (0x%x)",
361 TIFFErrorExt(tif
->tif_clientdata
, name
,
362 "Not a TIFF file, bad magic number %d (0x%x)",
364 tif
->tif_header
.common
.tiff_magic
,
365 tif
->tif_header
.common
.tiff_magic
);
368 if (tif
->tif_header
.common
.tiff_magic
== TIFF_BIGENDIAN
) {
369 #ifndef WORDS_BIGENDIAN
370 tif
->tif_flags
|= TIFF_SWAB
;
373 #ifdef WORDS_BIGENDIAN
374 tif
->tif_flags
|= TIFF_SWAB
;
377 if (tif
->tif_flags
& TIFF_SWAB
)
378 TIFFSwabShort(&tif
->tif_header
.common
.tiff_version
);
379 if ((tif
->tif_header
.common
.tiff_version
!= TIFF_VERSION_CLASSIC
)&&
380 (tif
->tif_header
.common
.tiff_version
!= TIFF_VERSION_BIG
)) {
381 TIFFErrorExt(tif
->tif_clientdata
, name
,
382 "Not a TIFF file, bad version number %d (0x%x)",
383 tif
->tif_header
.common
.tiff_version
,
384 tif
->tif_header
.common
.tiff_version
);
387 if (tif
->tif_header
.common
.tiff_version
== TIFF_VERSION_CLASSIC
)
389 if (tif
->tif_flags
& TIFF_SWAB
)
390 TIFFSwabLong(&tif
->tif_header
.classic
.tiff_diroff
);
391 tif
->tif_header_size
= sizeof(TIFFHeaderClassic
);
395 if (!ReadOK(tif
, ((uint8
*)(&tif
->tif_header
) + sizeof(TIFFHeaderClassic
)), (sizeof(TIFFHeaderBig
)-sizeof(TIFFHeaderClassic
))))
397 TIFFErrorExt(tif
->tif_clientdata
, name
,
398 "Cannot read TIFF header");
401 if (tif
->tif_flags
& TIFF_SWAB
)
403 TIFFSwabShort(&tif
->tif_header
.big
.tiff_offsetsize
);
404 TIFFSwabLong8(&tif
->tif_header
.big
.tiff_diroff
);
406 if (tif
->tif_header
.big
.tiff_offsetsize
!= 8)
408 TIFFErrorExt(tif
->tif_clientdata
, name
,
409 "Not a TIFF file, bad BigTIFF offsetsize %d (0x%x)",
410 tif
->tif_header
.big
.tiff_offsetsize
,
411 tif
->tif_header
.big
.tiff_offsetsize
);
414 if (tif
->tif_header
.big
.tiff_unused
!= 0)
416 TIFFErrorExt(tif
->tif_clientdata
, name
,
417 "Not a TIFF file, bad BigTIFF unused %d (0x%x)",
418 tif
->tif_header
.big
.tiff_unused
,
419 tif
->tif_header
.big
.tiff_unused
);
422 tif
->tif_header_size
= sizeof(TIFFHeaderBig
);
423 tif
->tif_flags
|= TIFF_BIGTIFF
;
425 tif
->tif_flags
|= TIFF_MYBUFFER
;
426 tif
->tif_rawcp
= tif
->tif_rawdata
= 0;
427 tif
->tif_rawdatasize
= 0;
428 tif
->tif_rawdataoff
= 0;
429 tif
->tif_rawdataloaded
= 0;
433 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
434 tif
->tif_nextdiroff
= tif
->tif_header
.classic
.tiff_diroff
;
436 tif
->tif_nextdiroff
= tif
->tif_header
.big
.tiff_diroff
;
438 * Try to use a memory-mapped file if the client
439 * has not explicitly suppressed usage with the
440 * 'm' flag in the open mode (see above).
442 if (tif
->tif_flags
& TIFF_MAPPED
)
445 if (TIFFMapFileContents(tif
,(void**)(&tif
->tif_base
),&n
))
447 tif
->tif_size
=(tmsize_t
)n
;
448 assert((toff_t
)tif
->tif_size
==n
);
451 tif
->tif_flags
&= ~TIFF_MAPPED
;
454 * Sometimes we do not want to read the first directory (for example,
455 * it may be broken) and want to proceed to other directories. I this
456 * case we use the TIFF_HEADERONLY flag to open file and return
457 * immediately after reading TIFF header.
459 if (tif
->tif_flags
& TIFF_HEADERONLY
)
463 * Setup initial directory.
465 if (TIFFReadDirectory(tif
)) {
466 tif
->tif_rawcc
= (tmsize_t
)-1;
467 tif
->tif_flags
|= TIFF_BUFFERSETUP
;
473 * New directories are automatically append
474 * to the end of the directory chain when they
475 * are written out (see TIFFWriteDirectory).
477 if (!TIFFDefaultDirectory(tif
))
482 tif
->tif_mode
= O_RDONLY
; /* XXX avoid flush */
489 * Query functions to access private data.
493 * Return open file's name.
496 TIFFFileName(TIFF
* tif
)
498 return (tif
->tif_name
);
505 TIFFSetFileName(TIFF
* tif
, const char *name
)
507 const char* old_name
= tif
->tif_name
;
508 tif
->tif_name
= (char *)name
;
513 * Return open file's I/O descriptor.
516 TIFFFileno(TIFF
* tif
)
518 return (tif
->tif_fd
);
522 * Set open file's I/O descriptor, and return previous value.
525 TIFFSetFileno(TIFF
* tif
, int fd
)
527 int old_fd
= tif
->tif_fd
;
533 * Return open file's clientdata.
536 TIFFClientdata(TIFF
* tif
)
538 return (tif
->tif_clientdata
);
542 * Set open file's clientdata, and return previous value.
545 TIFFSetClientdata(TIFF
* tif
, thandle_t newvalue
)
547 thandle_t m
= tif
->tif_clientdata
;
548 tif
->tif_clientdata
= newvalue
;
553 * Return read/write mode.
556 TIFFGetMode(TIFF
* tif
)
558 return (tif
->tif_mode
);
562 * Return read/write mode.
565 TIFFSetMode(TIFF
* tif
, int mode
)
567 int old_mode
= tif
->tif_mode
;
568 tif
->tif_mode
= mode
;
573 * Return nonzero if file is organized in
574 * tiles; zero if organized as strips.
577 TIFFIsTiled(TIFF
* tif
)
579 return (isTiled(tif
));
583 * Return current row being read/written.
586 TIFFCurrentRow(TIFF
* tif
)
588 return (tif
->tif_row
);
592 * Return index of the current directory.
595 TIFFCurrentDirectory(TIFF
* tif
)
597 return (tif
->tif_curdir
);
601 * Return current strip.
604 TIFFCurrentStrip(TIFF
* tif
)
606 return (tif
->tif_curstrip
);
610 * Return current tile.
613 TIFFCurrentTile(TIFF
* tif
)
615 return (tif
->tif_curtile
);
619 * Return nonzero if the file has byte-swapped data.
622 TIFFIsByteSwapped(TIFF
* tif
)
624 return ((tif
->tif_flags
& TIFF_SWAB
) != 0);
628 * Return nonzero if the data is returned up-sampled.
631 TIFFIsUpSampled(TIFF
* tif
)
633 return (isUpSampled(tif
));
637 * Return nonzero if the data is returned in MSB-to-LSB bit order.
640 TIFFIsMSB2LSB(TIFF
* tif
)
642 return (isFillOrder(tif
, FILLORDER_MSB2LSB
));
646 * Return nonzero if given file was written in big-endian order.
649 TIFFIsBigEndian(TIFF
* tif
)
651 return (tif
->tif_header
.common
.tiff_magic
== TIFF_BIGENDIAN
);
655 * Return pointer to file read method.
658 TIFFGetReadProc(TIFF
* tif
)
660 return (tif
->tif_readproc
);
664 * Return pointer to file write method.
667 TIFFGetWriteProc(TIFF
* tif
)
669 return (tif
->tif_writeproc
);
673 * Return pointer to file seek method.
676 TIFFGetSeekProc(TIFF
* tif
)
678 return (tif
->tif_seekproc
);
682 * Return pointer to file close method.
685 TIFFGetCloseProc(TIFF
* tif
)
687 return (tif
->tif_closeproc
);
691 * Return pointer to file size requesting method.
694 TIFFGetSizeProc(TIFF
* tif
)
696 return (tif
->tif_sizeproc
);
700 * Return pointer to memory mapping method.
703 TIFFGetMapFileProc(TIFF
* tif
)
705 return (tif
->tif_mapproc
);
709 * Return pointer to memory unmapping method.
712 TIFFGetUnmapFileProc(TIFF
* tif
)
714 return (tif
->tif_unmapproc
);
717 /* vim: set ts=8 sts=8 sw=8 noet: */