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
32 void _TIFFSetDefaultCompressionState(TIFF
* tif
);
34 static const long typemask
[13] = {
35 (long)0L, /* TIFF_NOTYPE */
36 (long)0x000000ffL
, /* TIFF_BYTE */
37 (long)0xffffffffL
, /* TIFF_ASCII */
38 (long)0x0000ffffL
, /* TIFF_SHORT */
39 (long)0xffffffffL
, /* TIFF_LONG */
40 (long)0xffffffffL
, /* TIFF_RATIONAL */
41 (long)0x000000ffL
, /* TIFF_SBYTE */
42 (long)0x000000ffL
, /* TIFF_UNDEFINED */
43 (long)0x0000ffffL
, /* TIFF_SSHORT */
44 (long)0xffffffffL
, /* TIFF_SLONG */
45 (long)0xffffffffL
, /* TIFF_SRATIONAL */
46 (long)0xffffffffL
, /* TIFF_FLOAT */
47 (long)0xffffffffL
, /* TIFF_DOUBLE */
49 static const int bigTypeshift
[13] = {
55 0, /* TIFF_RATIONAL */
57 24, /* TIFF_UNDEFINED */
60 0, /* TIFF_SRATIONAL */
64 static const int litTypeshift
[13] = {
70 0, /* TIFF_RATIONAL */
72 0, /* TIFF_UNDEFINED */
75 0, /* TIFF_SRATIONAL */
81 * Initialize the shift & mask tables, and the
82 * byte swapping state according to the file
83 * contents and the machine architecture.
86 TIFFInitOrder(TIFF
* tif
, int magic
, int bigendian
)
88 tif
->tif_typemask
= typemask
;
89 if (magic
== TIFF_BIGENDIAN
) {
90 tif
->tif_typeshift
= bigTypeshift
;
92 tif
->tif_flags
|= TIFF_SWAB
;
94 tif
->tif_typeshift
= litTypeshift
;
96 tif
->tif_flags
|= TIFF_SWAB
;
101 _TIFFgetMode(const char* mode
, const char* module)
118 TIFFError(module, "\"%s\": Bad mode", mode
);
126 const char* name
, const char* mode
,
127 thandle_t clientdata
,
128 TIFFReadWriteProc readproc
,
129 TIFFReadWriteProc writeproc
,
130 TIFFSeekProc seekproc
,
131 TIFFCloseProc closeproc
,
132 TIFFSizeProc sizeproc
,
133 TIFFMapFileProc mapproc
,
134 TIFFUnmapFileProc unmapproc
137 static const char module[] = "TIFFClientOpen";
142 m
= _TIFFgetMode(mode
, module);
145 tif
= (TIFF
*)_TIFFmalloc(sizeof (TIFF
) + strlen(name
) + 1);
147 TIFFError(module, "%s: Out of memory (TIFF structure)", name
);
150 _TIFFmemset(tif
, 0, sizeof (*tif
));
151 tif
->tif_name
= (char *)tif
+ sizeof (TIFF
);
152 strcpy(tif
->tif_name
, name
);
153 tif
->tif_mode
= m
&~ (O_CREAT
|O_TRUNC
);
154 tif
->tif_curdir
= (tdir_t
) -1; /* non-existent directory */
156 tif
->tif_curstrip
= (tstrip_t
) -1; /* invalid strip */
157 tif
->tif_row
= (uint32
) -1; /* read/write pre-increment */
158 tif
->tif_clientdata
= clientdata
;
159 if (!readproc
|| !writeproc
|| !seekproc
|| !closeproc
160 || !sizeproc
|| !mapproc
|| !unmapproc
) {
161 TIFFError(module, "One of the client procedures are NULL pointer");
164 tif
->tif_readproc
= readproc
;
165 tif
->tif_writeproc
= writeproc
;
166 tif
->tif_seekproc
= seekproc
;
167 tif
->tif_closeproc
= closeproc
;
168 tif
->tif_sizeproc
= sizeproc
;
169 tif
->tif_mapproc
= mapproc
;
170 tif
->tif_unmapproc
= unmapproc
;
171 _TIFFSetDefaultCompressionState(tif
); /* setup default state */
173 * Default is to return data MSB2LSB and enable the
174 * use of memory-mapped files and strip chopping when
175 * a file is opened read-only.
177 tif
->tif_flags
= FILLORDER_MSB2LSB
;
179 tif
->tif_flags
|= TIFF_MAPPED
;
181 #ifdef STRIPCHOP_DEFAULT
182 if (m
== O_RDONLY
|| m
== O_RDWR
)
183 tif
->tif_flags
|= STRIPCHOP_DEFAULT
;
186 { union { int32 i
; char c
[4]; } u
; u
.i
= 1; bigendian
= u
.c
[0] == 0; }
188 * Process library-specific flags in the open mode string.
189 * The following flags may be used to control intrinsic library
190 * behaviour that may or may not be desirable (usually for
191 * compatibility with some application that claims to support
192 * TIFF but only supports some braindead idea of what the
193 * vendor thinks TIFF is):
195 * 'l' use little-endian byte order for creating a file
196 * 'b' use big-endian byte order for creating a file
197 * 'L' read/write information using LSB2MSB bit order
198 * 'B' read/write information using MSB2LSB bit order
199 * 'H' read/write information using host bit order
200 * 'M' enable use of memory-mapped files when supported
201 * 'm' disable use of memory-mapped files
202 * 'C' enable strip chopping support when reading
203 * 'c' disable strip chopping support
205 * The use of the 'l' and 'b' flags is strongly discouraged.
206 * These flags are provided solely because numerous vendors,
207 * typically on the PC, do not correctly support TIFF; they
208 * only support the Intel little-endian byte order. This
209 * support is not configured by default because it supports
210 * the violation of the TIFF spec that says that readers *MUST*
211 * support both byte orders. It is strongly recommended that
212 * you not use this feature except to deal with busted apps
213 * that write invalid TIFF. And even in those cases you should
214 * bang on the vendors to fix their software.
216 * The 'L', 'B', and 'H' flags are intended for applications
217 * that can optimize operations on data by using a particular
218 * bit order. By default the library returns data in MSB2LSB
219 * bit order for compatibility with older versions of this
220 * library. Returning data in the bit order of the native cpu
221 * makes the most sense but also requires applications to check
222 * the value of the FillOrder tag; something they probabyl do
225 * The 'M' and 'm' flags are provided because some virtual memory
226 * systems exhibit poor behaviour when large images are mapped.
227 * These options permit clients to control the use of memory-mapped
228 * files on a per-file basis.
230 * The 'C' and 'c' flags are provided because the library support
231 * for chopping up large strips into multiple smaller strips is not
232 * application-transparent and as such can cause problems. The 'c'
233 * option permits applications that only want to look at the tags,
234 * for example, to get the unadulterated TIFF tag information.
236 for (cp
= mode
; *cp
; cp
++)
239 if ((m
&O_CREAT
) && !bigendian
)
240 tif
->tif_flags
|= TIFF_SWAB
;
243 if ((m
&O_CREAT
) && bigendian
)
244 tif
->tif_flags
|= TIFF_SWAB
;
247 tif
->tif_flags
= (tif
->tif_flags
&~ TIFF_FILLORDER
) |
251 tif
->tif_flags
= (tif
->tif_flags
&~ TIFF_FILLORDER
) |
255 tif
->tif_flags
= (tif
->tif_flags
&~ TIFF_FILLORDER
) |
260 tif
->tif_flags
|= TIFF_MAPPED
;
264 tif
->tif_flags
&= ~TIFF_MAPPED
;
268 tif
->tif_flags
|= TIFF_STRIPCHOP
;
272 tif
->tif_flags
&= ~TIFF_STRIPCHOP
;
276 * Read in TIFF header.
278 if (!ReadOK(tif
, &tif
->tif_header
, sizeof (TIFFHeader
))) {
279 if (tif
->tif_mode
== O_RDONLY
) {
280 TIFFError(name
, "Cannot read TIFF header");
284 * Setup header and write.
286 tif
->tif_header
.tiff_magic
= tif
->tif_flags
& TIFF_SWAB
287 ? (bigendian
? TIFF_LITTLEENDIAN
: TIFF_BIGENDIAN
)
288 : (bigendian
? TIFF_BIGENDIAN
: TIFF_LITTLEENDIAN
);
289 tif
->tif_header
.tiff_version
= TIFF_VERSION
;
290 if (tif
->tif_flags
& TIFF_SWAB
)
291 TIFFSwabShort(&tif
->tif_header
.tiff_version
);
292 tif
->tif_header
.tiff_diroff
= 0; /* filled in later */
295 * This seek shouldn't be necessary, but I have had some
296 * crazy problems with a failed fseek() on Solaris leaving
297 * the current file pointer out of whack when an fwrite()
300 TIFFSeekFile( tif
, 0, SEEK_SET
);
302 if (!WriteOK(tif
, &tif
->tif_header
, sizeof (TIFFHeader
))) {
303 TIFFError(name
, "Error writing TIFF header");
307 * Setup the byte order handling.
309 TIFFInitOrder(tif
, tif
->tif_header
.tiff_magic
, bigendian
);
311 * Setup default directory.
313 if (!TIFFDefaultDirectory(tif
))
316 tif
->tif_dirlist
= NULL
;
317 tif
->tif_dirnumber
= 0;
321 * Setup the byte order handling.
323 if (tif
->tif_header
.tiff_magic
!= TIFF_BIGENDIAN
&&
324 tif
->tif_header
.tiff_magic
!= TIFF_LITTLEENDIAN
) {
325 TIFFError(name
, "Not a TIFF file, bad magic number %d (0x%x)",
326 tif
->tif_header
.tiff_magic
,
327 tif
->tif_header
.tiff_magic
);
330 TIFFInitOrder(tif
, tif
->tif_header
.tiff_magic
, bigendian
);
332 * Swap header if required.
334 if (tif
->tif_flags
& TIFF_SWAB
) {
335 TIFFSwabShort(&tif
->tif_header
.tiff_version
);
336 TIFFSwabLong(&tif
->tif_header
.tiff_diroff
);
339 * Now check version (if needed, it's been byte-swapped).
340 * Note that this isn't actually a version number, it's a
341 * magic number that doesn't change (stupid).
343 if (tif
->tif_header
.tiff_version
!= TIFF_VERSION
) {
345 "Not a TIFF file, bad version number %d (0x%x)",
346 tif
->tif_header
.tiff_version
,
347 tif
->tif_header
.tiff_version
);
350 tif
->tif_flags
|= TIFF_MYBUFFER
;
351 tif
->tif_rawcp
= tif
->tif_rawdata
= 0;
352 tif
->tif_rawdatasize
= 0;
354 * Setup initial directory.
358 tif
->tif_nextdiroff
= tif
->tif_header
.tiff_diroff
;
360 * Try to use a memory-mapped file if the client
361 * has not explicitly suppressed usage with the
362 * 'm' flag in the open mode (see above).
364 if ((tif
->tif_flags
& TIFF_MAPPED
) &&
365 !TIFFMapFileContents(tif
, (tdata_t
*) &tif
->tif_base
, &tif
->tif_size
))
366 tif
->tif_flags
&= ~TIFF_MAPPED
;
367 if (TIFFReadDirectory(tif
)) {
369 tif
->tif_flags
|= TIFF_BUFFERSETUP
;
375 * New directories are automatically append
376 * to the end of the directory chain when they
377 * are written out (see TIFFWriteDirectory).
379 if (!TIFFDefaultDirectory(tif
))
384 tif
->tif_mode
= O_RDONLY
; /* XXX avoid flush */
388 (void) (*closeproc
)(clientdata
);
394 * Query functions to access private data.
398 * Return open file's name.
401 TIFFFileName(TIFF
* tif
)
403 return (tif
->tif_name
);
407 * Return open file's I/O descriptor.
410 TIFFFileno(TIFF
* tif
)
412 return (tif
->tif_fd
);
416 * Return read/write mode.
419 TIFFGetMode(TIFF
* tif
)
421 return (tif
->tif_mode
);
425 * Return nonzero if file is organized in
426 * tiles; zero if organized as strips.
429 TIFFIsTiled(TIFF
* tif
)
431 return (isTiled(tif
));
435 * Return current row being read/written.
438 TIFFCurrentRow(TIFF
* tif
)
440 return (tif
->tif_row
);
444 * Return index of the current directory.
447 TIFFCurrentDirectory(TIFF
* tif
)
449 return (tif
->tif_curdir
);
453 * Return current strip.
456 TIFFCurrentStrip(TIFF
* tif
)
458 return (tif
->tif_curstrip
);
462 * Return current tile.
465 TIFFCurrentTile(TIFF
* tif
)
467 return (tif
->tif_curtile
);
471 * Return nonzero if the file has byte-swapped data.
474 TIFFIsByteSwapped(TIFF
* tif
)
476 return ((tif
->tif_flags
& TIFF_SWAB
) != 0);
480 * Return nonzero if the data is returned up-sampled.
483 TIFFIsUpSampled(TIFF
* tif
)
485 return (isUpSampled(tif
));
489 * Return nonzero if the data is returned in MSB-to-LSB bit order.
492 TIFFIsMSB2LSB(TIFF
* tif
)
494 return (isFillOrder(tif
, FILLORDER_MSB2LSB
));