1 /* $Id: tif_open.c,v 1.46 2010-12-06 16:54:54 faxguy Exp $ */
4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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.
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.
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
34 * Dummy functions to fill the omitted client procedures.
37 _tiffDummyMapProc(thandle_t fd
, void** pbase
, toff_t
* psize
)
39 (void) fd
; (void) pbase
; (void) psize
;
44 _tiffDummyUnmapProc(thandle_t fd
, void* base
, toff_t size
)
46 (void) fd
; (void) base
; (void) size
;
50 _TIFFgetMode(const char* mode
, const char* module
)
67 TIFFErrorExt(0, module
, "\"%s\": Bad mode", mode
);
75 const char* name
, const char* mode
,
77 TIFFReadWriteProc readproc
,
78 TIFFReadWriteProc writeproc
,
79 TIFFSeekProc seekproc
,
80 TIFFCloseProc closeproc
,
81 TIFFSizeProc sizeproc
,
82 TIFFMapFileProc mapproc
,
83 TIFFUnmapFileProc unmapproc
86 static const char module
[] = "TIFFClientOpen";
91 /* The following are configuration checks. They should be redundant, but should not
92 * compile to any actual code in an optimised release build anyway. If any of them
93 * fail, (makefile-based or other) configuration is not correct */
94 assert(sizeof(uint8
)==1);
95 assert(sizeof(int8
)==1);
96 assert(sizeof(uint16
)==2);
97 assert(sizeof(int16
)==2);
98 assert(sizeof(uint32
)==4);
99 assert(sizeof(int32
)==4);
100 assert(sizeof(uint64
)==8);
101 assert(sizeof(int64
)==8);
102 assert(sizeof(tmsize_t
)==sizeof(void*));
110 #ifdef WORDS_BIGENDIAN
117 m
= _TIFFgetMode(mode
, module
);
120 tif
= (TIFF
*)_TIFFmalloc((tmsize_t
)(sizeof (TIFF
) + strlen(name
) + 1));
122 TIFFErrorExt(clientdata
, module
, "%s: Out of memory (TIFF structure)", name
);
125 _TIFFmemset(tif
, 0, sizeof (*tif
));
126 tif
->tif_name
= (char *)tif
+ sizeof (TIFF
);
127 strcpy(tif
->tif_name
, name
);
128 tif
->tif_mode
= m
&~ (O_CREAT
|O_TRUNC
);
129 tif
->tif_curdir
= (uint16
) -1; /* non-existent directory */
131 tif
->tif_curstrip
= (uint32
) -1; /* invalid strip */
132 tif
->tif_row
= (uint32
) -1; /* read/write pre-increment */
133 tif
->tif_clientdata
= clientdata
;
134 if (!readproc
|| !writeproc
|| !seekproc
|| !closeproc
|| !sizeproc
) {
135 TIFFErrorExt(clientdata
, module
,
136 "One of the client procedures is NULL pointer.");
139 tif
->tif_readproc
= readproc
;
140 tif
->tif_writeproc
= writeproc
;
141 tif
->tif_seekproc
= seekproc
;
142 tif
->tif_closeproc
= closeproc
;
143 tif
->tif_sizeproc
= sizeproc
;
145 tif
->tif_mapproc
= mapproc
;
147 tif
->tif_mapproc
= _tiffDummyMapProc
;
149 tif
->tif_unmapproc
= unmapproc
;
151 tif
->tif_unmapproc
= _tiffDummyUnmapProc
;
152 _TIFFSetDefaultCompressionState(tif
); /* setup default state */
154 * Default is to return data MSB2LSB and enable the
155 * use of memory-mapped files and strip chopping when
156 * a file is opened read-only.
158 tif
->tif_flags
= FILLORDER_MSB2LSB
;
160 tif
->tif_flags
|= TIFF_MAPPED
;
162 #ifdef STRIPCHOP_DEFAULT
163 if (m
== O_RDONLY
|| m
== O_RDWR
)
164 tif
->tif_flags
|= STRIPCHOP_DEFAULT
;
168 * Process library-specific flags in the open mode string.
169 * The following flags may be used to control intrinsic library
170 * behaviour that may or may not be desirable (usually for
171 * compatibility with some application that claims to support
172 * TIFF but only supports some braindead idea of what the
173 * vendor thinks TIFF is):
175 * 'l' use little-endian byte order for creating a file
176 * 'b' use big-endian byte order for creating a file
177 * 'L' read/write information using LSB2MSB bit order
178 * 'B' read/write information using MSB2LSB bit order
179 * 'H' read/write information using host bit order
180 * 'M' enable use of memory-mapped files when supported
181 * 'm' disable use of memory-mapped files
182 * 'C' enable strip chopping support when reading
183 * 'c' disable strip chopping support
184 * 'h' read TIFF header only, do not load the first IFD
185 * '4' ClassicTIFF for creating a file (default)
186 * '8' BigTIFF for creating a file
188 * The use of the 'l' and 'b' flags is strongly discouraged.
189 * These flags are provided solely because numerous vendors,
190 * typically on the PC, do not correctly support TIFF; they
191 * only support the Intel little-endian byte order. This
192 * support is not configured by default because it supports
193 * the violation of the TIFF spec that says that readers *MUST*
194 * support both byte orders. It is strongly recommended that
195 * you not use this feature except to deal with busted apps
196 * that write invalid TIFF. And even in those cases you should
197 * bang on the vendors to fix their software.
199 * The 'L', 'B', and 'H' flags are intended for applications
200 * that can optimize operations on data by using a particular
201 * bit order. By default the library returns data in MSB2LSB
202 * bit order for compatibiltiy with older versions of this
203 * library. Returning data in the bit order of the native cpu
204 * makes the most sense but also requires applications to check
205 * the value of the FillOrder tag; something they probably do
208 * The 'M' and 'm' flags are provided because some virtual memory
209 * systems exhibit poor behaviour when large images are mapped.
210 * These options permit clients to control the use of memory-mapped
211 * files on a per-file basis.
213 * The 'C' and 'c' flags are provided because the library support
214 * for chopping up large strips into multiple smaller strips is not
215 * application-transparent and as such can cause problems. The 'c'
216 * option permits applications that only want to look at the tags,
217 * for example, to get the unadulterated TIFF tag information.
219 for (cp
= mode
; *cp
; cp
++)
222 #ifndef WORDS_BIGENDIAN
224 tif
->tif_flags
|= TIFF_SWAB
;
228 #ifdef WORDS_BIGENDIAN
230 tif
->tif_flags
|= TIFF_SWAB
;
234 tif
->tif_flags
= (tif
->tif_flags
&~ TIFF_FILLORDER
) |
238 tif
->tif_flags
= (tif
->tif_flags
&~ TIFF_FILLORDER
) |
242 tif
->tif_flags
= (tif
->tif_flags
&~ TIFF_FILLORDER
) |
247 tif
->tif_flags
|= TIFF_MAPPED
;
251 tif
->tif_flags
&= ~TIFF_MAPPED
;
255 tif
->tif_flags
|= TIFF_STRIPCHOP
;
259 tif
->tif_flags
&= ~TIFF_STRIPCHOP
;
262 tif
->tif_flags
|= TIFF_HEADERONLY
;
266 tif
->tif_flags
|= TIFF_BIGTIFF
;
270 * Read in TIFF header.
273 !ReadOK(tif
, &tif
->tif_header
, sizeof (TIFFHeaderClassic
))) {
274 if (tif
->tif_mode
== O_RDONLY
) {
275 TIFFErrorExt(tif
->tif_clientdata
, name
,
276 "Cannot read TIFF header");
280 * Setup header and write.
282 #ifdef WORDS_BIGENDIAN
283 tif
->tif_header
.common
.tiff_magic
= tif
->tif_flags
& TIFF_SWAB
284 ? TIFF_LITTLEENDIAN
: TIFF_BIGENDIAN
;
286 tif
->tif_header
.common
.tiff_magic
= tif
->tif_flags
& TIFF_SWAB
287 ? TIFF_BIGENDIAN
: TIFF_LITTLEENDIAN
;
289 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
291 tif
->tif_header
.common
.tiff_version
= TIFF_VERSION_CLASSIC
;
292 tif
->tif_header
.classic
.tiff_diroff
= 0;
293 if (tif
->tif_flags
& TIFF_SWAB
)
294 TIFFSwabShort(&tif
->tif_header
.common
.tiff_version
);
295 tif
->tif_header_size
= sizeof(TIFFHeaderClassic
);
299 tif
->tif_header
.common
.tiff_version
= TIFF_VERSION_BIG
;
300 tif
->tif_header
.big
.tiff_offsetsize
= 8;
301 tif
->tif_header
.big
.tiff_unused
= 0;
302 tif
->tif_header
.big
.tiff_diroff
= 0;
303 if (tif
->tif_flags
& TIFF_SWAB
)
305 TIFFSwabShort(&tif
->tif_header
.common
.tiff_version
);
306 TIFFSwabShort(&tif
->tif_header
.big
.tiff_offsetsize
);
308 tif
->tif_header_size
= sizeof (TIFFHeaderBig
);
311 * The doc for "fopen" for some STD_C_LIBs says that if you
312 * open a file for modify ("+"), then you must fseek (or
313 * fflush?) between any freads and fwrites. This is not
314 * necessary on most systems, but has been shown to be needed
317 TIFFSeekFile( tif
, 0, SEEK_SET
);
318 if (!WriteOK(tif
, &tif
->tif_header
, (tmsize_t
)(tif
->tif_header_size
))) {
319 TIFFErrorExt(tif
->tif_clientdata
, name
,
320 "Error writing TIFF header");
324 * Setup the byte order handling.
326 if (tif
->tif_header
.common
.tiff_magic
== TIFF_BIGENDIAN
) {
327 #ifndef WORDS_BIGENDIAN
328 tif
->tif_flags
|= TIFF_SWAB
;
331 #ifdef WORDS_BIGENDIAN
332 tif
->tif_flags
|= TIFF_SWAB
;
336 * Setup default directory.
338 if (!TIFFDefaultDirectory(tif
))
341 tif
->tif_dirlist
= NULL
;
342 tif
->tif_dirlistsize
= 0;
343 tif
->tif_dirnumber
= 0;
347 * Setup the byte order handling.
349 if (tif
->tif_header
.common
.tiff_magic
!= TIFF_BIGENDIAN
&&
350 tif
->tif_header
.common
.tiff_magic
!= TIFF_LITTLEENDIAN
354 tif
->tif_header
.common
.tiff_magic
!= MDI_BIGENDIAN
356 tif
->tif_header
.common
.tiff_magic
!= MDI_LITTLEENDIAN
359 TIFFErrorExt(tif
->tif_clientdata
, name
,
360 "Not a TIFF or MDI file, bad magic number %d (0x%x)",
363 TIFFErrorExt(tif
->tif_clientdata
, name
,
364 "Not a TIFF file, bad magic number %d (0x%x)",
366 tif
->tif_header
.common
.tiff_magic
,
367 tif
->tif_header
.common
.tiff_magic
);
370 if (tif
->tif_header
.common
.tiff_magic
== TIFF_BIGENDIAN
) {
371 #ifndef WORDS_BIGENDIAN
372 tif
->tif_flags
|= TIFF_SWAB
;
375 #ifdef WORDS_BIGENDIAN
376 tif
->tif_flags
|= TIFF_SWAB
;
379 if (tif
->tif_flags
& TIFF_SWAB
)
380 TIFFSwabShort(&tif
->tif_header
.common
.tiff_version
);
381 if ((tif
->tif_header
.common
.tiff_version
!= TIFF_VERSION_CLASSIC
)&&
382 (tif
->tif_header
.common
.tiff_version
!= TIFF_VERSION_BIG
)) {
383 TIFFErrorExt(tif
->tif_clientdata
, name
,
384 "Not a TIFF file, bad version number %d (0x%x)",
385 tif
->tif_header
.common
.tiff_version
,
386 tif
->tif_header
.common
.tiff_version
);
389 if (tif
->tif_header
.common
.tiff_version
== TIFF_VERSION_CLASSIC
)
391 if (tif
->tif_flags
& TIFF_SWAB
)
392 TIFFSwabLong(&tif
->tif_header
.classic
.tiff_diroff
);
393 tif
->tif_header_size
= sizeof(TIFFHeaderClassic
);
397 if (!ReadOK(tif
, ((uint8
*)(&tif
->tif_header
) + sizeof(TIFFHeaderClassic
)), (sizeof(TIFFHeaderBig
)-sizeof(TIFFHeaderClassic
))))
399 TIFFErrorExt(tif
->tif_clientdata
, name
,
400 "Cannot read TIFF header");
403 if (tif
->tif_flags
& TIFF_SWAB
)
405 TIFFSwabShort(&tif
->tif_header
.big
.tiff_offsetsize
);
406 TIFFSwabLong8(&tif
->tif_header
.big
.tiff_diroff
);
408 if (tif
->tif_header
.big
.tiff_offsetsize
!= 8)
410 TIFFErrorExt(tif
->tif_clientdata
, name
,
411 "Not a TIFF file, bad BigTIFF offsetsize %d (0x%x)",
412 tif
->tif_header
.big
.tiff_offsetsize
,
413 tif
->tif_header
.big
.tiff_offsetsize
);
416 if (tif
->tif_header
.big
.tiff_unused
!= 0)
418 TIFFErrorExt(tif
->tif_clientdata
, name
,
419 "Not a TIFF file, bad BigTIFF unused %d (0x%x)",
420 tif
->tif_header
.big
.tiff_unused
,
421 tif
->tif_header
.big
.tiff_unused
);
424 tif
->tif_header_size
= sizeof(TIFFHeaderBig
);
425 tif
->tif_flags
|= TIFF_BIGTIFF
;
427 tif
->tif_flags
|= TIFF_MYBUFFER
;
428 tif
->tif_rawcp
= tif
->tif_rawdata
= 0;
429 tif
->tif_rawdatasize
= 0;
430 tif
->tif_rawdataoff
= 0;
431 tif
->tif_rawdataloaded
= 0;
435 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
436 tif
->tif_nextdiroff
= tif
->tif_header
.classic
.tiff_diroff
;
438 tif
->tif_nextdiroff
= tif
->tif_header
.big
.tiff_diroff
;
440 * Try to use a memory-mapped file if the client
441 * has not explicitly suppressed usage with the
442 * 'm' flag in the open mode (see above).
444 if (tif
->tif_flags
& TIFF_MAPPED
)
447 if (TIFFMapFileContents(tif
,(void**)(&tif
->tif_base
),&n
))
449 tif
->tif_size
=(tmsize_t
)n
;
450 assert((toff_t
)tif
->tif_size
==n
);
453 tif
->tif_flags
&= ~TIFF_MAPPED
;
456 * Sometimes we do not want to read the first directory (for example,
457 * it may be broken) and want to proceed to other directories. I this
458 * case we use the TIFF_HEADERONLY flag to open file and return
459 * immediately after reading TIFF header.
461 if (tif
->tif_flags
& TIFF_HEADERONLY
)
465 * Setup initial directory.
467 if (TIFFReadDirectory(tif
)) {
468 tif
->tif_rawcc
= (tmsize_t
)-1;
469 tif
->tif_flags
|= TIFF_BUFFERSETUP
;
475 * New directories are automatically append
476 * to the end of the directory chain when they
477 * are written out (see TIFFWriteDirectory).
479 if (!TIFFDefaultDirectory(tif
))
484 tif
->tif_mode
= O_RDONLY
; /* XXX avoid flush */
491 * Query functions to access private data.
495 * Return open file's name.
498 TIFFFileName(TIFF
* tif
)
500 return (tif
->tif_name
);
507 TIFFSetFileName(TIFF
* tif
, const char *name
)
509 const char* old_name
= tif
->tif_name
;
510 tif
->tif_name
= (char *)name
;
515 * Return open file's I/O descriptor.
518 TIFFFileno(TIFF
* tif
)
520 return (tif
->tif_fd
);
524 * Set open file's I/O descriptor, and return previous value.
527 TIFFSetFileno(TIFF
* tif
, int fd
)
529 int old_fd
= tif
->tif_fd
;
535 * Return open file's clientdata.
538 TIFFClientdata(TIFF
* tif
)
540 return (tif
->tif_clientdata
);
544 * Set open file's clientdata, and return previous value.
547 TIFFSetClientdata(TIFF
* tif
, thandle_t newvalue
)
549 thandle_t m
= tif
->tif_clientdata
;
550 tif
->tif_clientdata
= newvalue
;
555 * Return read/write mode.
558 TIFFGetMode(TIFF
* tif
)
560 return (tif
->tif_mode
);
564 * Return read/write mode.
567 TIFFSetMode(TIFF
* tif
, int mode
)
569 int old_mode
= tif
->tif_mode
;
570 tif
->tif_mode
= mode
;
575 * Return nonzero if file is organized in
576 * tiles; zero if organized as strips.
579 TIFFIsTiled(TIFF
* tif
)
581 return (isTiled(tif
));
585 * Return current row being read/written.
588 TIFFCurrentRow(TIFF
* tif
)
590 return (tif
->tif_row
);
594 * Return index of the current directory.
597 TIFFCurrentDirectory(TIFF
* tif
)
599 return (tif
->tif_curdir
);
603 * Return current strip.
606 TIFFCurrentStrip(TIFF
* tif
)
608 return (tif
->tif_curstrip
);
612 * Return current tile.
615 TIFFCurrentTile(TIFF
* tif
)
617 return (tif
->tif_curtile
);
621 * Return nonzero if the file has byte-swapped data.
624 TIFFIsByteSwapped(TIFF
* tif
)
626 return ((tif
->tif_flags
& TIFF_SWAB
) != 0);
630 * Return nonzero if the data is returned up-sampled.
633 TIFFIsUpSampled(TIFF
* tif
)
635 return (isUpSampled(tif
));
639 * Return nonzero if the data is returned in MSB-to-LSB bit order.
642 TIFFIsMSB2LSB(TIFF
* tif
)
644 return (isFillOrder(tif
, FILLORDER_MSB2LSB
));
648 * Return nonzero if given file was written in big-endian order.
651 TIFFIsBigEndian(TIFF
* tif
)
653 return (tif
->tif_header
.common
.tiff_magic
== TIFF_BIGENDIAN
);
657 * Return pointer to file read method.
660 TIFFGetReadProc(TIFF
* tif
)
662 return (tif
->tif_readproc
);
666 * Return pointer to file write method.
669 TIFFGetWriteProc(TIFF
* tif
)
671 return (tif
->tif_writeproc
);
675 * Return pointer to file seek method.
678 TIFFGetSeekProc(TIFF
* tif
)
680 return (tif
->tif_seekproc
);
684 * Return pointer to file close method.
687 TIFFGetCloseProc(TIFF
* tif
)
689 return (tif
->tif_closeproc
);
693 * Return pointer to file size requesting method.
696 TIFFGetSizeProc(TIFF
* tif
)
698 return (tif
->tif_sizeproc
);
702 * Return pointer to memory mapping method.
705 TIFFGetMapFileProc(TIFF
* tif
)
707 return (tif
->tif_mapproc
);
711 * Return pointer to memory unmapping method.
714 TIFFGetUnmapFileProc(TIFF
* tif
)
716 return (tif
->tif_unmapproc
);
719 /* vim: set ts=8 sts=8 sw=8 noet: */