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] = {
36 0x000000ffL
, /* TIFF_BYTE */
37 0xffffffffL
, /* TIFF_ASCII */
38 0x0000ffffL
, /* TIFF_SHORT */
39 0xffffffffL
, /* TIFF_LONG */
40 0xffffffffL
, /* TIFF_RATIONAL */
41 0x000000ffL
, /* TIFF_SBYTE */
42 0x000000ffL
, /* TIFF_UNDEFINED */
43 0x0000ffffL
, /* TIFF_SSHORT */
44 0xffffffffL
, /* TIFF_SLONG */
45 0xffffffffL
, /* TIFF_SRATIONAL */
46 0xffffffffL
, /* TIFF_FLOAT */
47 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 tif
->tif_readproc
= readproc
;
160 tif
->tif_writeproc
= writeproc
;
161 tif
->tif_seekproc
= seekproc
;
162 tif
->tif_closeproc
= closeproc
;
163 tif
->tif_sizeproc
= sizeproc
;
164 tif
->tif_mapproc
= mapproc
;
165 tif
->tif_unmapproc
= unmapproc
;
166 _TIFFSetDefaultCompressionState(tif
); /* setup default state */
168 * Default is to return data MSB2LSB and enable the
169 * use of memory-mapped files and strip chopping when
170 * a file is opened read-only.
172 tif
->tif_flags
= FILLORDER_MSB2LSB
;
174 #ifdef STRIPCHOP_DEFAULT
175 tif
->tif_flags
|= TIFF_MAPPED
|STRIPCHOP_DEFAULT
;
177 tif
->tif_flags
|= TIFF_MAPPED
;
180 { union { int32 i
; char c
[4]; } u
; u
.i
= 1; bigendian
= u
.c
[0] == 0; }
182 * Process library-specific flags in the open mode string.
183 * The following flags may be used to control intrinsic library
184 * behaviour that may or may not be desirable (usually for
185 * compatibility with some application that claims to support
186 * TIFF but only supports some braindead idea of what the
187 * vendor thinks TIFF is):
189 * 'l' use little-endian byte order for creating a file
190 * 'b' use big-endian byte order for creating a file
191 * 'L' read/write information using LSB2MSB bit order
192 * 'B' read/write information using MSB2LSB bit order
193 * 'H' read/write information using host bit order
194 * 'M' enable use of memory-mapped files when supported
195 * 'm' disable use of memory-mapped files
196 * 'C' enable strip chopping support when reading
197 * 'c' disable strip chopping support
199 * The use of the 'l' and 'b' flags is strongly discouraged.
200 * These flags are provided solely because numerous vendors,
201 * typically on the PC, do not correctly support TIFF; they
202 * only support the Intel little-endian byte order. This
203 * support is not configured by default because it supports
204 * the violation of the TIFF spec that says that readers *MUST*
205 * support both byte orders. It is strongly recommended that
206 * you not use this feature except to deal with busted apps
207 * that write invalid TIFF. And even in those cases you should
208 * bang on the vendors to fix their software.
210 * The 'L', 'B', and 'H' flags are intended for applications
211 * that can optimize operations on data by using a particular
212 * bit order. By default the library returns data in MSB2LSB
213 * bit order for compatibiltiy with older versions of this
214 * library. Returning data in the bit order of the native cpu
215 * makes the most sense but also requires applications to check
216 * the value of the FillOrder tag; something they probabyl do
219 * The 'M' and 'm' flags are provided because some virtual memory
220 * systems exhibit poor behaviour when large images are mapped.
221 * These options permit clients to control the use of memory-mapped
222 * files on a per-file basis.
224 * The 'C' and 'c' flags are provided because the library support
225 * for chopping up large strips into multiple smaller strips is not
226 * application-transparent and as such can cause problems. The 'c'
227 * option permits applications that only want to look at the tags,
228 * for example, to get the unadulterated TIFF tag information.
230 for (cp
= mode
; *cp
; cp
++)
233 if ((m
&O_CREAT
) && !bigendian
)
234 tif
->tif_flags
|= TIFF_SWAB
;
237 if ((m
&O_CREAT
) && bigendian
)
238 tif
->tif_flags
|= TIFF_SWAB
;
241 tif
->tif_flags
= (tif
->tif_flags
&~ TIFF_FILLORDER
) |
245 tif
->tif_flags
= (tif
->tif_flags
&~ TIFF_FILLORDER
) |
249 tif
->tif_flags
= (tif
->tif_flags
&~ TIFF_FILLORDER
) |
254 tif
->tif_flags
|= TIFF_MAPPED
;
258 tif
->tif_flags
&= ~TIFF_MAPPED
;
262 tif
->tif_flags
|= TIFF_STRIPCHOP
;
266 tif
->tif_flags
&= ~TIFF_STRIPCHOP
;
270 * Read in TIFF header.
272 if (!ReadOK(tif
, &tif
->tif_header
, sizeof (TIFFHeader
))) {
273 if (tif
->tif_mode
== O_RDONLY
) {
274 TIFFError(name
, "Cannot read TIFF header");
278 * Setup header and write.
280 tif
->tif_header
.tiff_magic
= tif
->tif_flags
& TIFF_SWAB
281 ? (bigendian
? TIFF_LITTLEENDIAN
: TIFF_BIGENDIAN
)
282 : (bigendian
? TIFF_BIGENDIAN
: TIFF_LITTLEENDIAN
);
283 tif
->tif_header
.tiff_version
= TIFF_VERSION
;
284 if (tif
->tif_flags
& TIFF_SWAB
)
285 TIFFSwabShort(&tif
->tif_header
.tiff_version
);
286 tif
->tif_header
.tiff_diroff
= 0; /* filled in later */
287 if (!WriteOK(tif
, &tif
->tif_header
, sizeof (TIFFHeader
))) {
288 TIFFError(name
, "Error writing TIFF header");
292 * Setup the byte order handling.
294 TIFFInitOrder(tif
, tif
->tif_header
.tiff_magic
, bigendian
);
296 * Setup default directory.
298 if (!TIFFDefaultDirectory(tif
))
304 * Setup the byte order handling.
306 if (tif
->tif_header
.tiff_magic
!= TIFF_BIGENDIAN
&&
307 tif
->tif_header
.tiff_magic
!= TIFF_LITTLEENDIAN
) {
308 TIFFError(name
, "Not a TIFF file, bad magic number %d (0x%x)",
309 tif
->tif_header
.tiff_magic
,
310 tif
->tif_header
.tiff_magic
);
313 TIFFInitOrder(tif
, tif
->tif_header
.tiff_magic
, bigendian
);
315 * Swap header if required.
317 if (tif
->tif_flags
& TIFF_SWAB
) {
318 TIFFSwabShort(&tif
->tif_header
.tiff_version
);
319 TIFFSwabLong(&tif
->tif_header
.tiff_diroff
);
322 * Now check version (if needed, it's been byte-swapped).
323 * Note that this isn't actually a version number, it's a
324 * magic number that doesn't change (stupid).
326 if (tif
->tif_header
.tiff_version
!= TIFF_VERSION
) {
328 "Not a TIFF file, bad version number %d (0x%x)",
329 tif
->tif_header
.tiff_version
,
330 tif
->tif_header
.tiff_version
);
333 tif
->tif_flags
|= TIFF_MYBUFFER
;
334 tif
->tif_rawcp
= tif
->tif_rawdata
= 0;
335 tif
->tif_rawdatasize
= 0;
337 * Setup initial directory.
341 tif
->tif_nextdiroff
= tif
->tif_header
.tiff_diroff
;
343 * Try to use a memory-mapped file if the client
344 * has not explicitly suppressed usage with the
345 * 'm' flag in the open mode (see above).
347 if ((tif
->tif_flags
& TIFF_MAPPED
) &&
348 !TIFFMapFileContents(tif
, (tdata_t
*) &tif
->tif_base
, &tif
->tif_size
))
349 tif
->tif_flags
&= ~TIFF_MAPPED
;
350 if (TIFFReadDirectory(tif
)) {
352 tif
->tif_flags
|= TIFF_BUFFERSETUP
;
358 * New directories are automatically append
359 * to the end of the directory chain when they
360 * are written out (see TIFFWriteDirectory).
362 if (!TIFFDefaultDirectory(tif
))
367 tif
->tif_mode
= O_RDONLY
; /* XXX avoid flush */
371 (void) (*closeproc
)(clientdata
);
376 * Query functions to access private data.
380 * Return open file's name.
383 TIFFFileName(TIFF
* tif
)
385 return (tif
->tif_name
);
389 * Return open file's I/O descriptor.
392 TIFFFileno(TIFF
* tif
)
394 return (tif
->tif_fd
);
398 * Return read/write mode.
401 TIFFGetMode(TIFF
* tif
)
403 return (tif
->tif_mode
);
407 * Return nonzero if file is organized in
408 * tiles; zero if organized as strips.
411 TIFFIsTiled(TIFF
* tif
)
413 return (isTiled(tif
));
417 * Return current row being read/written.
420 TIFFCurrentRow(TIFF
* tif
)
422 return (tif
->tif_row
);
426 * Return index of the current directory.
429 TIFFCurrentDirectory(TIFF
* tif
)
431 return (tif
->tif_curdir
);
435 * Return current strip.
438 TIFFCurrentStrip(TIFF
* tif
)
440 return (tif
->tif_curstrip
);
444 * Return current tile.
447 TIFFCurrentTile(TIFF
* tif
)
449 return (tif
->tif_curtile
);
453 * Return nonzero if the file has byte-swapped data.
456 TIFFIsByteSwapped(TIFF
* tif
)
458 return ((tif
->tif_flags
& TIFF_SWAB
) != 0);
462 * Return nonzero if the data is returned up-sampled.
465 TIFFIsUpSampled(TIFF
* tif
)
467 return (isUpSampled(tif
));
471 * Return nonzero if the data is returned in MSB-to-LSB bit order.
474 TIFFIsMSB2LSB(TIFF
* tif
)
476 return (isFillOrder(tif
, FILLORDER_MSB2LSB
));