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
33 * Dummy functions to fill the omitted client procedures.
36 _tiffDummyMapProc(thandle_t fd
, void** pbase
, toff_t
* psize
)
38 (void) fd
; (void) pbase
; (void) psize
;
43 _tiffDummyUnmapProc(thandle_t fd
, void* base
, toff_t size
)
45 (void) fd
; (void) base
; (void) size
;
49 _TIFFgetMode(const char* mode
, const char* module)
66 TIFFErrorExt(0, module, "\"%s\": Bad mode", mode
);
74 const char* name
, const char* mode
,
76 TIFFReadWriteProc readproc
,
77 TIFFReadWriteProc writeproc
,
78 TIFFSeekProc seekproc
,
79 TIFFCloseProc closeproc
,
80 TIFFSizeProc sizeproc
,
81 TIFFMapFileProc mapproc
,
82 TIFFUnmapFileProc unmapproc
85 static const char module[] = "TIFFClientOpen";
90 /* The following are configuration checks. They should be redundant, but should not
91 * compile to any actual code in an optimised release build anyway. If any of them
92 * fail, (makefile-based or other) configuration is not correct */
93 assert(sizeof(uint8
)==1);
94 assert(sizeof(int8
)==1);
95 assert(sizeof(uint16
)==2);
96 assert(sizeof(int16
)==2);
97 assert(sizeof(uint32
)==4);
98 assert(sizeof(int32
)==4);
99 assert(sizeof(uint64
)==8);
100 assert(sizeof(int64
)==8);
101 assert(sizeof(tmsize_t
)==sizeof(void*));
109 #ifdef WORDS_BIGENDIAN
116 m
= _TIFFgetMode(mode
, module);
119 tif
= (TIFF
*)_TIFFmalloc((tmsize_t
)(sizeof (TIFF
) + strlen(name
) + 1));
121 TIFFErrorExt(clientdata
, module, "%s: Out of memory (TIFF structure)", name
);
124 _TIFFmemset(tif
, 0, sizeof (*tif
));
125 tif
->tif_name
= (char *)tif
+ sizeof (TIFF
);
126 strcpy(tif
->tif_name
, name
);
127 tif
->tif_mode
= m
&~ (O_CREAT
|O_TRUNC
);
128 tif
->tif_curdir
= (uint16
) -1; /* non-existent directory */
130 tif
->tif_curstrip
= (uint32
) -1; /* invalid strip */
131 tif
->tif_row
= (uint32
) -1; /* read/write pre-increment */
132 tif
->tif_clientdata
= clientdata
;
133 if (!readproc
|| !writeproc
|| !seekproc
|| !closeproc
|| !sizeproc
) {
134 TIFFErrorExt(clientdata
, module,
135 "One of the client procedures is NULL pointer.");
138 tif
->tif_readproc
= readproc
;
139 tif
->tif_writeproc
= writeproc
;
140 tif
->tif_seekproc
= seekproc
;
141 tif
->tif_closeproc
= closeproc
;
142 tif
->tif_sizeproc
= sizeproc
;
144 tif
->tif_mapproc
= mapproc
;
146 tif
->tif_mapproc
= _tiffDummyMapProc
;
148 tif
->tif_unmapproc
= unmapproc
;
150 tif
->tif_unmapproc
= _tiffDummyUnmapProc
;
151 _TIFFSetDefaultCompressionState(tif
); /* setup default state */
153 * Default is to return data MSB2LSB and enable the
154 * use of memory-mapped files and strip chopping when
155 * a file is opened read-only.
157 tif
->tif_flags
= FILLORDER_MSB2LSB
;
159 tif
->tif_flags
|= TIFF_MAPPED
;
161 #ifdef STRIPCHOP_DEFAULT
162 if (m
== O_RDONLY
|| m
== O_RDWR
)
163 tif
->tif_flags
|= STRIPCHOP_DEFAULT
;
167 * Process library-specific flags in the open mode string.
168 * The following flags may be used to control intrinsic library
169 * behaviour that may or may not be desirable (usually for
170 * compatibility with some application that claims to support
171 * TIFF but only supports some braindead idea of what the
172 * vendor thinks TIFF is):
174 * 'l' use little-endian byte order for creating a file
175 * 'b' use big-endian byte order for creating a file
176 * 'L' read/write information using LSB2MSB bit order
177 * 'B' read/write information using MSB2LSB bit order
178 * 'H' read/write information using host bit order
179 * 'M' enable use of memory-mapped files when supported
180 * 'm' disable use of memory-mapped files
181 * 'C' enable strip chopping support when reading
182 * 'c' disable strip chopping support
183 * 'h' read TIFF header only, do not load the first IFD
184 * '4' ClassicTIFF for creating a file (default)
185 * '8' BigTIFF for creating a file
187 * The use of the 'l' and 'b' flags is strongly discouraged.
188 * These flags are provided solely because numerous vendors,
189 * typically on the PC, do not correctly support TIFF; they
190 * only support the Intel little-endian byte order. This
191 * support is not configured by default because it supports
192 * the violation of the TIFF spec that says that readers *MUST*
193 * support both byte orders. It is strongly recommended that
194 * you not use this feature except to deal with busted apps
195 * that write invalid TIFF. And even in those cases you should
196 * bang on the vendors to fix their software.
198 * The 'L', 'B', and 'H' flags are intended for applications
199 * that can optimize operations on data by using a particular
200 * bit order. By default the library returns data in MSB2LSB
201 * bit order for compatibiltiy with older versions of this
202 * library. Returning data in the bit order of the native cpu
203 * makes the most sense but also requires applications to check
204 * the value of the FillOrder tag; something they probably do
207 * The 'M' and 'm' flags are provided because some virtual memory
208 * systems exhibit poor behaviour when large images are mapped.
209 * These options permit clients to control the use of memory-mapped
210 * files on a per-file basis.
212 * The 'C' and 'c' flags are provided because the library support
213 * for chopping up large strips into multiple smaller strips is not
214 * application-transparent and as such can cause problems. The 'c'
215 * option permits applications that only want to look at the tags,
216 * for example, to get the unadulterated TIFF tag information.
218 for (cp
= mode
; *cp
; cp
++)
221 #ifndef WORDS_BIGENDIAN
223 tif
->tif_flags
|= TIFF_SWAB
;
227 #ifdef WORDS_BIGENDIAN
229 tif
->tif_flags
|= TIFF_SWAB
;
233 tif
->tif_flags
= (tif
->tif_flags
&~ TIFF_FILLORDER
) |
237 tif
->tif_flags
= (tif
->tif_flags
&~ TIFF_FILLORDER
) |
241 tif
->tif_flags
= (tif
->tif_flags
&~ TIFF_FILLORDER
) |
246 tif
->tif_flags
|= TIFF_MAPPED
;
250 tif
->tif_flags
&= ~TIFF_MAPPED
;
254 tif
->tif_flags
|= TIFF_STRIPCHOP
;
258 tif
->tif_flags
&= ~TIFF_STRIPCHOP
;
261 tif
->tif_flags
|= TIFF_HEADERONLY
;
265 tif
->tif_flags
|= TIFF_BIGTIFF
;
269 * Read in TIFF header.
272 !ReadOK(tif
, &tif
->tif_header
, sizeof (TIFFHeaderClassic
))) {
273 if (tif
->tif_mode
== O_RDONLY
) {
274 TIFFErrorExt(tif
->tif_clientdata
, name
,
275 "Cannot read TIFF header");
279 * Setup header and write.
281 #ifdef WORDS_BIGENDIAN
282 tif
->tif_header
.common
.tiff_magic
= tif
->tif_flags
& TIFF_SWAB
283 ? TIFF_LITTLEENDIAN
: TIFF_BIGENDIAN
;
285 tif
->tif_header
.common
.tiff_magic
= tif
->tif_flags
& TIFF_SWAB
286 ? TIFF_BIGENDIAN
: TIFF_LITTLEENDIAN
;
288 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
290 tif
->tif_header
.common
.tiff_version
= TIFF_VERSION_CLASSIC
;
291 tif
->tif_header
.classic
.tiff_diroff
= 0;
292 if (tif
->tif_flags
& TIFF_SWAB
)
293 TIFFSwabShort(&tif
->tif_header
.common
.tiff_version
);
294 tif
->tif_header_size
= sizeof(TIFFHeaderClassic
);
298 tif
->tif_header
.common
.tiff_version
= TIFF_VERSION_BIG
;
299 tif
->tif_header
.big
.tiff_offsetsize
= 8;
300 tif
->tif_header
.big
.tiff_unused
= 0;
301 tif
->tif_header
.big
.tiff_diroff
= 0;
302 if (tif
->tif_flags
& TIFF_SWAB
)
304 TIFFSwabShort(&tif
->tif_header
.common
.tiff_version
);
305 TIFFSwabShort(&tif
->tif_header
.big
.tiff_offsetsize
);
307 tif
->tif_header_size
= sizeof (TIFFHeaderBig
);
310 * The doc for "fopen" for some STD_C_LIBs says that if you
311 * open a file for modify ("+"), then you must fseek (or
312 * fflush?) between any freads and fwrites. This is not
313 * necessary on most systems, but has been shown to be needed
316 TIFFSeekFile( tif
, 0, SEEK_SET
);
317 if (!WriteOK(tif
, &tif
->tif_header
, (tmsize_t
)(tif
->tif_header_size
))) {
318 TIFFErrorExt(tif
->tif_clientdata
, name
,
319 "Error writing TIFF header");
323 * Setup the byte order handling.
325 if (tif
->tif_header
.common
.tiff_magic
== TIFF_BIGENDIAN
) {
326 #ifndef WORDS_BIGENDIAN
327 tif
->tif_flags
|= TIFF_SWAB
;
330 #ifdef WORDS_BIGENDIAN
331 tif
->tif_flags
|= TIFF_SWAB
;
335 * Setup default directory.
337 if (!TIFFDefaultDirectory(tif
))
340 tif
->tif_dirlist
= NULL
;
341 tif
->tif_dirlistsize
= 0;
342 tif
->tif_dirnumber
= 0;
346 * Setup the byte order handling.
348 if (tif
->tif_header
.common
.tiff_magic
!= TIFF_BIGENDIAN
&&
349 tif
->tif_header
.common
.tiff_magic
!= TIFF_LITTLEENDIAN
353 tif
->tif_header
.common
.tiff_magic
!= MDI_BIGENDIAN
355 tif
->tif_header
.common
.tiff_magic
!= MDI_LITTLEENDIAN
358 TIFFErrorExt(tif
->tif_clientdata
, name
,
359 "Not a TIFF or MDI file, bad magic number %d (0x%x)",
362 TIFFErrorExt(tif
->tif_clientdata
, name
,
363 "Not a TIFF file, bad magic number %d (0x%x)",
365 tif
->tif_header
.common
.tiff_magic
,
366 tif
->tif_header
.common
.tiff_magic
);
369 if (tif
->tif_header
.common
.tiff_magic
== TIFF_BIGENDIAN
) {
370 #ifndef WORDS_BIGENDIAN
371 tif
->tif_flags
|= TIFF_SWAB
;
374 #ifdef WORDS_BIGENDIAN
375 tif
->tif_flags
|= TIFF_SWAB
;
378 if (tif
->tif_flags
& TIFF_SWAB
)
379 TIFFSwabShort(&tif
->tif_header
.common
.tiff_version
);
380 if ((tif
->tif_header
.common
.tiff_version
!= TIFF_VERSION_CLASSIC
)&&
381 (tif
->tif_header
.common
.tiff_version
!= TIFF_VERSION_BIG
)) {
382 TIFFErrorExt(tif
->tif_clientdata
, name
,
383 "Not a TIFF file, bad version number %d (0x%x)",
384 tif
->tif_header
.common
.tiff_version
,
385 tif
->tif_header
.common
.tiff_version
);
388 if (tif
->tif_header
.common
.tiff_version
== TIFF_VERSION_CLASSIC
)
390 if (tif
->tif_flags
& TIFF_SWAB
)
391 TIFFSwabLong(&tif
->tif_header
.classic
.tiff_diroff
);
392 tif
->tif_header_size
= sizeof(TIFFHeaderClassic
);
396 if (!ReadOK(tif
, ((uint8
*)(&tif
->tif_header
) + sizeof(TIFFHeaderClassic
)), (sizeof(TIFFHeaderBig
)-sizeof(TIFFHeaderClassic
))))
398 TIFFErrorExt(tif
->tif_clientdata
, name
,
399 "Cannot read TIFF header");
402 if (tif
->tif_flags
& TIFF_SWAB
)
404 TIFFSwabShort(&tif
->tif_header
.big
.tiff_offsetsize
);
405 TIFFSwabLong8(&tif
->tif_header
.big
.tiff_diroff
);
407 if (tif
->tif_header
.big
.tiff_offsetsize
!= 8)
409 TIFFErrorExt(tif
->tif_clientdata
, name
,
410 "Not a TIFF file, bad BigTIFF offsetsize %d (0x%x)",
411 tif
->tif_header
.big
.tiff_offsetsize
,
412 tif
->tif_header
.big
.tiff_offsetsize
);
415 if (tif
->tif_header
.big
.tiff_unused
!= 0)
417 TIFFErrorExt(tif
->tif_clientdata
, name
,
418 "Not a TIFF file, bad BigTIFF unused %d (0x%x)",
419 tif
->tif_header
.big
.tiff_unused
,
420 tif
->tif_header
.big
.tiff_unused
);
423 tif
->tif_header_size
= sizeof(TIFFHeaderBig
);
424 tif
->tif_flags
|= TIFF_BIGTIFF
;
426 tif
->tif_flags
|= TIFF_MYBUFFER
;
427 tif
->tif_rawcp
= tif
->tif_rawdata
= 0;
428 tif
->tif_rawdatasize
= 0;
429 tif
->tif_rawdataoff
= 0;
430 tif
->tif_rawdataloaded
= 0;
434 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
435 tif
->tif_nextdiroff
= tif
->tif_header
.classic
.tiff_diroff
;
437 tif
->tif_nextdiroff
= tif
->tif_header
.big
.tiff_diroff
;
439 * Try to use a memory-mapped file if the client
440 * has not explicitly suppressed usage with the
441 * 'm' flag in the open mode (see above).
443 if (tif
->tif_flags
& TIFF_MAPPED
)
446 if (TIFFMapFileContents(tif
,(void**)(&tif
->tif_base
),&n
))
448 tif
->tif_size
=(tmsize_t
)n
;
449 assert((toff_t
)tif
->tif_size
==n
);
452 tif
->tif_flags
&= ~TIFF_MAPPED
;
455 * Sometimes we do not want to read the first directory (for example,
456 * it may be broken) and want to proceed to other directories. I this
457 * case we use the TIFF_HEADERONLY flag to open file and return
458 * immediately after reading TIFF header.
460 if (tif
->tif_flags
& TIFF_HEADERONLY
)
464 * Setup initial directory.
466 if (TIFFReadDirectory(tif
)) {
467 tif
->tif_rawcc
= (tmsize_t
)-1;
468 tif
->tif_flags
|= TIFF_BUFFERSETUP
;
474 * New directories are automatically append
475 * to the end of the directory chain when they
476 * are written out (see TIFFWriteDirectory).
478 if (!TIFFDefaultDirectory(tif
))
483 tif
->tif_mode
= O_RDONLY
; /* XXX avoid flush */
490 * Query functions to access private data.
494 * Return open file's name.
497 TIFFFileName(TIFF
* tif
)
499 return (tif
->tif_name
);
506 TIFFSetFileName(TIFF
* tif
, const char *name
)
508 const char* old_name
= tif
->tif_name
;
509 tif
->tif_name
= (char *)name
;
514 * Return open file's I/O descriptor.
517 TIFFFileno(TIFF
* tif
)
519 return (tif
->tif_fd
);
523 * Set open file's I/O descriptor, and return previous value.
526 TIFFSetFileno(TIFF
* tif
, int fd
)
528 int old_fd
= tif
->tif_fd
;
534 * Return open file's clientdata.
537 TIFFClientdata(TIFF
* tif
)
539 return (tif
->tif_clientdata
);
543 * Set open file's clientdata, and return previous value.
546 TIFFSetClientdata(TIFF
* tif
, thandle_t newvalue
)
548 thandle_t m
= tif
->tif_clientdata
;
549 tif
->tif_clientdata
= newvalue
;
554 * Return read/write mode.
557 TIFFGetMode(TIFF
* tif
)
559 return (tif
->tif_mode
);
563 * Return read/write mode.
566 TIFFSetMode(TIFF
* tif
, int mode
)
568 int old_mode
= tif
->tif_mode
;
569 tif
->tif_mode
= mode
;
574 * Return nonzero if file is organized in
575 * tiles; zero if organized as strips.
578 TIFFIsTiled(TIFF
* tif
)
580 return (isTiled(tif
));
584 * Return current row being read/written.
587 TIFFCurrentRow(TIFF
* tif
)
589 return (tif
->tif_row
);
593 * Return index of the current directory.
596 TIFFCurrentDirectory(TIFF
* tif
)
598 return (tif
->tif_curdir
);
602 * Return current strip.
605 TIFFCurrentStrip(TIFF
* tif
)
607 return (tif
->tif_curstrip
);
611 * Return current tile.
614 TIFFCurrentTile(TIFF
* tif
)
616 return (tif
->tif_curtile
);
620 * Return nonzero if the file has byte-swapped data.
623 TIFFIsByteSwapped(TIFF
* tif
)
625 return ((tif
->tif_flags
& TIFF_SWAB
) != 0);
629 * Return nonzero if the data is returned up-sampled.
632 TIFFIsUpSampled(TIFF
* tif
)
634 return (isUpSampled(tif
));
638 * Return nonzero if the data is returned in MSB-to-LSB bit order.
641 TIFFIsMSB2LSB(TIFF
* tif
)
643 return (isFillOrder(tif
, FILLORDER_MSB2LSB
));
647 * Return nonzero if given file was written in big-endian order.
650 TIFFIsBigEndian(TIFF
* tif
)
652 return (tif
->tif_header
.common
.tiff_magic
== TIFF_BIGENDIAN
);
656 * Return pointer to file read method.
659 TIFFGetReadProc(TIFF
* tif
)
661 return (tif
->tif_readproc
);
665 * Return pointer to file write method.
668 TIFFGetWriteProc(TIFF
* tif
)
670 return (tif
->tif_writeproc
);
674 * Return pointer to file seek method.
677 TIFFGetSeekProc(TIFF
* tif
)
679 return (tif
->tif_seekproc
);
683 * Return pointer to file close method.
686 TIFFGetCloseProc(TIFF
* tif
)
688 return (tif
->tif_closeproc
);
692 * Return pointer to file size requesting method.
695 TIFFGetSizeProc(TIFF
* tif
)
697 return (tif
->tif_sizeproc
);
701 * Return pointer to memory mapping method.
704 TIFFGetMapFileProc(TIFF
* tif
)
706 return (tif
->tif_mapproc
);
710 * Return pointer to memory unmapping method.
713 TIFFGetUnmapFileProc(TIFF
* tif
)
715 return (tif
->tif_unmapproc
);
718 /* vim: set ts=8 sts=8 sw=8 noet: */