1 /* $Id: tif_open.c,v 1.1 2005/06/17 13:54:52 vp153 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
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 * Dummy functions to fill the omitted client procedures.
84 _tiffDummyMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
90 _tiffDummyUnmapProc(thandle_t fd, tdata_t base, toff_t size)
95 * Initialize the shift & mask tables, and the
96 * byte swapping state according to the file
97 * contents and the machine architecture.
100 TIFFInitOrder(TIFF* tif, int magic, int bigendian)
102 tif->tif_typemask = typemask;
103 if (magic == TIFF_BIGENDIAN) {
104 tif->tif_typeshift = bigTypeshift;
106 tif->tif_flags |= TIFF_SWAB;
108 tif->tif_typeshift = litTypeshift;
110 tif->tif_flags |= TIFF_SWAB;
115 _TIFFgetMode(const char* mode, const char* module)
132 TIFFError(module, "\"%s\": Bad mode", mode);
140 const char* name, const char* mode,
141 thandle_t clientdata,
142 TIFFReadWriteProc readproc,
143 TIFFReadWriteProc writeproc,
144 TIFFSeekProc seekproc,
145 TIFFCloseProc closeproc,
146 TIFFSizeProc sizeproc,
147 TIFFMapFileProc mapproc,
148 TIFFUnmapFileProc unmapproc
151 static const char module[] = "TIFFClientOpen";
156 m = _TIFFgetMode(mode, module);
159 tif = (TIFF *)_TIFFmalloc(sizeof (TIFF) + strlen(name) + 1);
161 TIFFError(module, "%s: Out of memory (TIFF structure)", name);
164 _TIFFmemset(tif, 0, sizeof (*tif));
165 tif->tif_name = (char *)tif + sizeof (TIFF);
166 strcpy(tif->tif_name, name);
167 tif->tif_mode = m &~ (O_CREAT|O_TRUNC);
168 tif->tif_curdir = (tdir_t) -1; /* non-existent directory */
170 tif->tif_curstrip = (tstrip_t) -1; /* invalid strip */
171 tif->tif_row = (uint32) -1; /* read/write pre-increment */
172 tif->tif_clientdata = clientdata;
173 if (!readproc || !writeproc || !seekproc || !closeproc || !sizeproc) {
175 "One of the client procedures is NULL pointer.");
178 tif->tif_readproc = readproc;
179 tif->tif_writeproc = writeproc;
180 tif->tif_seekproc = seekproc;
181 tif->tif_closeproc = closeproc;
182 tif->tif_sizeproc = sizeproc;
184 tif->tif_mapproc = mapproc;
186 tif->tif_mapproc = _tiffDummyMapProc;
188 tif->tif_unmapproc = unmapproc;
190 tif->tif_unmapproc = _tiffDummyUnmapProc;
191 _TIFFSetDefaultCompressionState(tif); /* setup default state */
193 * Default is to return data MSB2LSB and enable the
194 * use of memory-mapped files and strip chopping when
195 * a file is opened read-only.
197 tif->tif_flags = FILLORDER_MSB2LSB;
199 tif->tif_flags |= TIFF_MAPPED;
201 #ifdef STRIPCHOP_DEFAULT
202 if (m == O_RDONLY || m == O_RDWR)
203 tif->tif_flags |= STRIPCHOP_DEFAULT;
206 { union { int32 i; char c[4]; } u; u.i = 1; bigendian = u.c[0] == 0; }
208 * Process library-specific flags in the open mode string.
209 * The following flags may be used to control intrinsic library
210 * behaviour that may or may not be desirable (usually for
211 * compatibility with some application that claims to support
212 * TIFF but only supports some braindead idea of what the
213 * vendor thinks TIFF is):
215 * 'l' use little-endian byte order for creating a file
216 * 'b' use big-endian byte order for creating a file
217 * 'L' read/write information using LSB2MSB bit order
218 * 'B' read/write information using MSB2LSB bit order
219 * 'H' read/write information using host bit order
220 * 'M' enable use of memory-mapped files when supported
221 * 'm' disable use of memory-mapped files
222 * 'C' enable strip chopping support when reading
223 * 'c' disable strip chopping support
225 * The use of the 'l' and 'b' flags is strongly discouraged.
226 * These flags are provided solely because numerous vendors,
227 * typically on the PC, do not correctly support TIFF; they
228 * only support the Intel little-endian byte order. This
229 * support is not configured by default because it supports
230 * the violation of the TIFF spec that says that readers *MUST*
231 * support both byte orders. It is strongly recommended that
232 * you not use this feature except to deal with busted apps
233 * that write invalid TIFF. And even in those cases you should
234 * bang on the vendors to fix their software.
236 * The 'L', 'B', and 'H' flags are intended for applications
237 * that can optimize operations on data by using a particular
238 * bit order. By default the library returns data in MSB2LSB
239 * bit order for compatibiltiy with older versions of this
240 * library. Returning data in the bit order of the native cpu
241 * makes the most sense but also requires applications to check
242 * the value of the FillOrder tag; something they probably do
245 * The 'M' and 'm' flags are provided because some virtual memory
246 * systems exhibit poor behaviour when large images are mapped.
247 * These options permit clients to control the use of memory-mapped
248 * files on a per-file basis.
250 * The 'C' and 'c' flags are provided because the library support
251 * for chopping up large strips into multiple smaller strips is not
252 * application-transparent and as such can cause problems. The 'c'
253 * option permits applications that only want to look at the tags,
254 * for example, to get the unadulterated TIFF tag information.
256 for (cp = mode; *cp; cp++)
259 if ((m&O_CREAT) && !bigendian)
260 tif->tif_flags |= TIFF_SWAB;
263 if ((m&O_CREAT) && bigendian)
264 tif->tif_flags |= TIFF_SWAB;
267 tif->tif_flags = (tif->tif_flags &~ TIFF_FILLORDER) |
271 tif->tif_flags = (tif->tif_flags &~ TIFF_FILLORDER) |
275 tif->tif_flags = (tif->tif_flags &~ TIFF_FILLORDER) |
280 tif->tif_flags |= TIFF_MAPPED;
284 tif->tif_flags &= ~TIFF_MAPPED;
288 tif->tif_flags |= TIFF_STRIPCHOP;
292 tif->tif_flags &= ~TIFF_STRIPCHOP;
296 * Read in TIFF header.
298 if (!ReadOK(tif, &tif->tif_header, sizeof (TIFFHeader))) {
299 if (tif->tif_mode == O_RDONLY) {
300 TIFFError(name, "Cannot read TIFF header");
304 * Setup header and write.
306 tif->tif_header.tiff_magic = tif->tif_flags & TIFF_SWAB
307 ? (bigendian ? TIFF_LITTLEENDIAN : TIFF_BIGENDIAN)
308 : (bigendian ? TIFF_BIGENDIAN : TIFF_LITTLEENDIAN);
309 tif->tif_header.tiff_version = TIFF_VERSION;
310 if (tif->tif_flags & TIFF_SWAB)
311 TIFFSwabShort(&tif->tif_header.tiff_version);
312 tif->tif_header.tiff_diroff = 0; /* filled in later */
314 if (!WriteOK(tif, &tif->tif_header, sizeof (TIFFHeader))) {
315 TIFFError(name, "Error writing TIFF header");
319 * Setup the byte order handling.
321 TIFFInitOrder(tif, tif->tif_header.tiff_magic, bigendian);
323 * Setup default directory.
325 if (!TIFFDefaultDirectory(tif))
328 tif->tif_dirlist = NULL;
329 tif->tif_dirnumber = 0;
333 * Setup the byte order handling.
335 if (tif->tif_header.tiff_magic != TIFF_BIGENDIAN &&
336 tif->tif_header.tiff_magic != TIFF_LITTLEENDIAN) {
337 TIFFError(name, "Not a TIFF file, bad magic number %d (0x%x)",
338 tif->tif_header.tiff_magic,
339 tif->tif_header.tiff_magic);
342 TIFFInitOrder(tif, tif->tif_header.tiff_magic, bigendian);
344 * Swap header if required.
346 if (tif->tif_flags & TIFF_SWAB) {
347 TIFFSwabShort(&tif->tif_header.tiff_version);
348 TIFFSwabLong(&tif->tif_header.tiff_diroff);
351 * Now check version (if needed, it's been byte-swapped).
352 * Note that this isn't actually a version number, it's a
353 * magic number that doesn't change (stupid).
355 if (tif->tif_header.tiff_version == TIFF_BIGTIFF_VERSION) {
357 "This is a BigTIFF file. This format not supported\n"
358 "by this version of libtiff." );
361 if (tif->tif_header.tiff_version != TIFF_VERSION) {
363 "Not a TIFF file, bad version number %d (0x%x)",
364 tif->tif_header.tiff_version,
365 tif->tif_header.tiff_version);
368 tif->tif_flags |= TIFF_MYBUFFER;
369 tif->tif_rawcp = tif->tif_rawdata = 0;
370 tif->tif_rawdatasize = 0;
372 * Setup initial directory.
376 tif->tif_nextdiroff = tif->tif_header.tiff_diroff;
378 * Try to use a memory-mapped file if the client
379 * has not explicitly suppressed usage with the
380 * 'm' flag in the open mode (see above).
382 if ((tif->tif_flags & TIFF_MAPPED) &&
383 !TIFFMapFileContents(tif, (tdata_t*) &tif->tif_base, &tif->tif_size))
384 tif->tif_flags &= ~TIFF_MAPPED;
385 if (TIFFReadDirectory(tif)) {
387 tif->tif_flags |= TIFF_BUFFERSETUP;
393 * New directories are automatically append
394 * to the end of the directory chain when they
395 * are written out (see TIFFWriteDirectory).
397 if (!TIFFDefaultDirectory(tif))
402 tif->tif_mode = O_RDONLY; /* XXX avoid flush */
409 * Query functions to access private data.
413 * Return open file's name.
416 TIFFFileName(TIFF* tif)
418 return (tif->tif_name);
425 TIFFSetFileName(TIFF* tif, const char *name)
427 const char* old_name = tif->tif_name;
428 tif->tif_name = (char *)name;
433 * Return open file's I/O descriptor.
436 TIFFFileno(TIFF* tif)
438 return (tif->tif_fd);
442 * Set open file's I/O descriptor, and return previous value.
445 TIFFSetFileno(TIFF* tif, int fd)
447 int old_fd = tif->tif_fd;
453 * Return open file's clientdata.
456 TIFFClientdata(TIFF* tif)
458 return (tif->tif_clientdata);
462 * Set open file's clientdata, and return previous value.
465 TIFFSetClientdata(TIFF* tif, thandle_t newvalue)
467 thandle_t m = tif->tif_clientdata;
468 tif->tif_clientdata = newvalue;
473 * Return read/write mode.
476 TIFFGetMode(TIFF* tif)
478 return (tif->tif_mode);
482 * Return read/write mode.
485 TIFFSetMode(TIFF* tif, int mode)
487 int old_mode = tif->tif_mode;
488 tif->tif_mode = mode;
493 * Return nonzero if file is organized in
494 * tiles; zero if organized as strips.
497 TIFFIsTiled(TIFF* tif)
499 return (isTiled(tif));
503 * Return current row being read/written.
506 TIFFCurrentRow(TIFF* tif)
508 return (tif->tif_row);
512 * Return index of the current directory.
515 TIFFCurrentDirectory(TIFF* tif)
517 return (tif->tif_curdir);
521 * Return current strip.
524 TIFFCurrentStrip(TIFF* tif)
526 return (tif->tif_curstrip);
530 * Return current tile.
533 TIFFCurrentTile(TIFF* tif)
535 return (tif->tif_curtile);
539 * Return nonzero if the file has byte-swapped data.
542 TIFFIsByteSwapped(TIFF* tif)
544 return ((tif->tif_flags & TIFF_SWAB) != 0);
548 * Return nonzero if the data is returned up-sampled.
551 TIFFIsUpSampled(TIFF* tif)
553 return (isUpSampled(tif));
557 * Return nonzero if the data is returned in MSB-to-LSB bit order.
560 TIFFIsMSB2LSB(TIFF* tif)
562 return (isFillOrder(tif, FILLORDER_MSB2LSB));
566 * Return nonzero if given file was written in big-endian order.
569 TIFFIsBigEndian(TIFF* tif)
571 return (tif->tif_header.tiff_magic == TIFF_BIGENDIAN);
575 * Return pointer to file read method.
578 TIFFGetReadProc(TIFF* tif)
580 return (tif->tif_readproc);
584 * Return pointer to file write method.
587 TIFFGetWriteProc(TIFF* tif)
589 return (tif->tif_writeproc);
593 * Return pointer to file seek method.
596 TIFFGetSeekProc(TIFF* tif)
598 return (tif->tif_seekproc);
602 * Return pointer to file close method.
605 TIFFGetCloseProc(TIFF* tif)
607 return (tif->tif_closeproc);
611 * Return pointer to file size requesting method.
614 TIFFGetSizeProc(TIFF* tif)
616 return (tif->tif_sizeproc);
620 * Return pointer to memory mapping method.
623 TIFFGetMapFileProc(TIFF* tif)
625 return (tif->tif_mapproc);
629 * Return pointer to memory unmapping method.
632 TIFFGetUnmapFileProc(TIFF* tif)
634 return (tif->tif_unmapproc);
637 /* vim: set ts=8 sts=8 sw=8 noet: */