tif_open.c
Upload User: kairuinn
Upload Date: 2009-02-07
Package Size: 2922k
Code Size: 13k
Category:

Graph program

Development Platform:

Visual C++

  1. /* $Header: /cvsroot/osrs/libtiff/libtiff/tif_open.c,v 1.6 2001/07/20 02:22:46 warmerda Exp $ */
  2. /*
  3.  * Copyright (c) 1988-1997 Sam Leffler
  4.  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and 
  7.  * its documentation for any purpose is hereby granted without fee, provided
  8.  * that (i) the above copyright notices and this permission notice appear in
  9.  * all copies of the software and related documentation, and (ii) the names of
  10.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11.  * publicity relating to the software without the specific, prior written
  12.  * permission of Sam Leffler and Silicon Graphics.
  13.  * 
  14.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  15.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  16.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  17.  * 
  18.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  22.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  23.  * OF THIS SOFTWARE.
  24.  */
  25. /*
  26.  * TIFF Library.
  27.  */
  28. #include "tiffiop.h"
  29. void _TIFFSetDefaultCompressionState(TIFF* tif);
  30. static const long typemask[13] = {
  31. 0L, /* TIFF_NOTYPE */
  32. 0x000000ffL, /* TIFF_BYTE */
  33. 0xffffffffL, /* TIFF_ASCII */
  34. 0x0000ffffL, /* TIFF_SHORT */
  35. 0xffffffffL, /* TIFF_LONG */
  36. 0xffffffffL, /* TIFF_RATIONAL */
  37. 0x000000ffL, /* TIFF_SBYTE */
  38. 0x000000ffL, /* TIFF_UNDEFINED */
  39. 0x0000ffffL, /* TIFF_SSHORT */
  40. 0xffffffffL, /* TIFF_SLONG */
  41. 0xffffffffL, /* TIFF_SRATIONAL */
  42. 0xffffffffL, /* TIFF_FLOAT */
  43. 0xffffffffL, /* TIFF_DOUBLE */
  44. };
  45. static const int bigTypeshift[13] = {
  46. 0, /* TIFF_NOTYPE */
  47. 24, /* TIFF_BYTE */
  48. 0, /* TIFF_ASCII */
  49. 16, /* TIFF_SHORT */
  50. 0, /* TIFF_LONG */
  51. 0, /* TIFF_RATIONAL */
  52. 24, /* TIFF_SBYTE */
  53. 24, /* TIFF_UNDEFINED */
  54. 16, /* TIFF_SSHORT */
  55. 0, /* TIFF_SLONG */
  56. 0, /* TIFF_SRATIONAL */
  57. 0, /* TIFF_FLOAT */
  58. 0, /* TIFF_DOUBLE */
  59. };
  60. static const int litTypeshift[13] = {
  61. 0, /* TIFF_NOTYPE */
  62. 0, /* TIFF_BYTE */
  63. 0, /* TIFF_ASCII */
  64. 0, /* TIFF_SHORT */
  65. 0, /* TIFF_LONG */
  66. 0, /* TIFF_RATIONAL */
  67. 0, /* TIFF_SBYTE */
  68. 0, /* TIFF_UNDEFINED */
  69. 0, /* TIFF_SSHORT */
  70. 0, /* TIFF_SLONG */
  71. 0, /* TIFF_SRATIONAL */
  72. 0, /* TIFF_FLOAT */
  73. 0, /* TIFF_DOUBLE */
  74. };
  75. /*
  76.  * Initialize the shift & mask tables, and the
  77.  * byte swapping state according to the file
  78.  * contents and the machine architecture.
  79.  */
  80. static void
  81. TIFFInitOrder(TIFF* tif, int magic, int bigendian)
  82. {
  83. tif->tif_typemask = typemask;
  84. if (magic == TIFF_BIGENDIAN) {
  85. tif->tif_typeshift = bigTypeshift;
  86. if (!bigendian)
  87. tif->tif_flags |= TIFF_SWAB;
  88. } else {
  89. tif->tif_typeshift = litTypeshift;
  90. if (bigendian)
  91. tif->tif_flags |= TIFF_SWAB;
  92. }
  93. }
  94. int
  95. _TIFFgetMode(const char* mode, const char* module)
  96. {
  97. int m = -1;
  98. switch (mode[0]) {
  99. case 'r':
  100. m = O_RDONLY;
  101. if (mode[1] == '+')
  102. m = O_RDWR;
  103. break;
  104. case 'w':
  105. case 'a':
  106. m = O_RDWR|O_CREAT;
  107. if (mode[0] == 'w')
  108. m |= O_TRUNC;
  109. break;
  110. default:
  111. TIFFError(module, ""%s": Bad mode", mode);
  112. break;
  113. }
  114. return (m);
  115. }
  116. TIFF*
  117. TIFFClientOpen(
  118. const char* name, const char* mode,
  119. thandle_t clientdata,
  120. TIFFReadWriteProc readproc,
  121. TIFFReadWriteProc writeproc,
  122. TIFFSeekProc seekproc,
  123. TIFFCloseProc closeproc,
  124. TIFFSizeProc sizeproc,
  125. TIFFMapFileProc mapproc,
  126. TIFFUnmapFileProc unmapproc
  127. )
  128. {
  129. static const char module[] = "TIFFClientOpen";
  130. TIFF *tif;
  131. int m, bigendian;
  132. const char* cp;
  133. m = _TIFFgetMode(mode, module);
  134. if (m == -1)
  135. goto bad2;
  136. tif = (TIFF *)_TIFFmalloc(sizeof (TIFF) + strlen(name) + 1);
  137. if (tif == NULL) {
  138. TIFFError(module, "%s: Out of memory (TIFF structure)", name);
  139. goto bad2;
  140. }
  141. _TIFFmemset(tif, 0, sizeof (*tif));
  142. tif->tif_name = (char *)tif + sizeof (TIFF);
  143. strcpy(tif->tif_name, name);
  144. tif->tif_mode = m &~ (O_CREAT|O_TRUNC);
  145. tif->tif_curdir = (tdir_t) -1; /* non-existent directory */
  146. tif->tif_curoff = 0;
  147. tif->tif_curstrip = (tstrip_t) -1; /* invalid strip */
  148. tif->tif_row = (uint32) -1; /* read/write pre-increment */
  149. tif->tif_clientdata = clientdata;
  150. tif->tif_readproc = readproc;
  151. tif->tif_writeproc = writeproc;
  152. tif->tif_seekproc = seekproc;
  153. tif->tif_closeproc = closeproc;
  154. tif->tif_sizeproc = sizeproc;
  155. tif->tif_mapproc = mapproc;
  156. tif->tif_unmapproc = unmapproc;
  157. _TIFFSetDefaultCompressionState(tif); /* setup default state */
  158. /*
  159.  * Default is to return data MSB2LSB and enable the
  160.  * use of memory-mapped files and strip chopping when
  161.  * a file is opened read-only.
  162.  */
  163. tif->tif_flags = FILLORDER_MSB2LSB;
  164. if (m == O_RDONLY )
  165.             tif->tif_flags |= TIFF_MAPPED;
  166. #ifdef STRIPCHOP_DEFAULT
  167. if (m == O_RDONLY || m == O_RDWR)
  168. tif->tif_flags |= STRIPCHOP_DEFAULT;
  169. #endif
  170. { union { int32 i; char c[4]; } u; u.i = 1; bigendian = u.c[0] == 0; }
  171. /*
  172.  * Process library-specific flags in the open mode string.
  173.  * The following flags may be used to control intrinsic library
  174.  * behaviour that may or may not be desirable (usually for
  175.  * compatibility with some application that claims to support
  176.  * TIFF but only supports some braindead idea of what the
  177.  * vendor thinks TIFF is):
  178.  *
  179.  * 'l' use little-endian byte order for creating a file
  180.  * 'b' use big-endian byte order for creating a file
  181.  * 'L' read/write information using LSB2MSB bit order
  182.  * 'B' read/write information using MSB2LSB bit order
  183.  * 'H' read/write information using host bit order
  184.  * 'M' enable use of memory-mapped files when supported
  185.  * 'm' disable use of memory-mapped files
  186.  * 'C' enable strip chopping support when reading
  187.  * 'c' disable strip chopping support
  188.  *
  189.  * The use of the 'l' and 'b' flags is strongly discouraged.
  190.  * These flags are provided solely because numerous vendors,
  191.  * typically on the PC, do not correctly support TIFF; they
  192.  * only support the Intel little-endian byte order.  This
  193.  * support is not configured by default because it supports
  194.  * the violation of the TIFF spec that says that readers *MUST*
  195.  * support both byte orders.  It is strongly recommended that
  196.  * you not use this feature except to deal with busted apps
  197.  * that write invalid TIFF.  And even in those cases you should
  198.  * bang on the vendors to fix their software.
  199.  *
  200.  * The 'L', 'B', and 'H' flags are intended for applications
  201.  * that can optimize operations on data by using a particular
  202.  * bit order.  By default the library returns data in MSB2LSB
  203.  * bit order for compatibiltiy with older versions of this
  204.  * library.  Returning data in the bit order of the native cpu
  205.  * makes the most sense but also requires applications to check
  206.  * the value of the FillOrder tag; something they probabyl do
  207.  * not do right now.
  208.  *
  209.  * The 'M' and 'm' flags are provided because some virtual memory
  210.  * systems exhibit poor behaviour when large images are mapped.
  211.  * These options permit clients to control the use of memory-mapped
  212.  * files on a per-file basis.
  213.  *
  214.  * The 'C' and 'c' flags are provided because the library support
  215.  * for chopping up large strips into multiple smaller strips is not
  216.  * application-transparent and as such can cause problems.  The 'c'
  217.  * option permits applications that only want to look at the tags,
  218.  * for example, to get the unadulterated TIFF tag information.
  219.  */
  220. for (cp = mode; *cp; cp++)
  221. switch (*cp) {
  222. case 'b':
  223. if ((m&O_CREAT) && !bigendian)
  224. tif->tif_flags |= TIFF_SWAB;
  225. break;
  226. case 'l':
  227. if ((m&O_CREAT) && bigendian)
  228. tif->tif_flags |= TIFF_SWAB;
  229. break;
  230. case 'B':
  231. tif->tif_flags = (tif->tif_flags &~ TIFF_FILLORDER) |
  232.     FILLORDER_MSB2LSB;
  233. break;
  234. case 'L':
  235. tif->tif_flags = (tif->tif_flags &~ TIFF_FILLORDER) |
  236.     FILLORDER_LSB2MSB;
  237. break;
  238. case 'H':
  239. tif->tif_flags = (tif->tif_flags &~ TIFF_FILLORDER) |
  240.     HOST_FILLORDER;
  241. break;
  242. case 'M':
  243. if (m == O_RDONLY)
  244. tif->tif_flags |= TIFF_MAPPED;
  245. break;
  246. case 'm':
  247. if (m == O_RDONLY)
  248. tif->tif_flags &= ~TIFF_MAPPED;
  249. break;
  250. case 'C':
  251. if (m == O_RDONLY)
  252. tif->tif_flags |= TIFF_STRIPCHOP;
  253. break;
  254. case 'c':
  255. if (m == O_RDONLY)
  256. tif->tif_flags &= ~TIFF_STRIPCHOP;
  257. break;
  258. }
  259. /*
  260.  * Read in TIFF header.
  261.  */
  262. if (!ReadOK(tif, &tif->tif_header, sizeof (TIFFHeader))) {
  263. if (tif->tif_mode == O_RDONLY) {
  264. TIFFError(name, "Cannot read TIFF header");
  265. goto bad;
  266. }
  267. /*
  268.  * Setup header and write.
  269.  */
  270. tif->tif_header.tiff_magic = tif->tif_flags & TIFF_SWAB
  271.     ? (bigendian ? TIFF_LITTLEENDIAN : TIFF_BIGENDIAN)
  272.     : (bigendian ? TIFF_BIGENDIAN : TIFF_LITTLEENDIAN);
  273. tif->tif_header.tiff_version = TIFF_VERSION;
  274. if (tif->tif_flags & TIFF_SWAB)
  275. TIFFSwabShort(&tif->tif_header.tiff_version);
  276. tif->tif_header.tiff_diroff = 0; /* filled in later */
  277.                 /*
  278.                  * This seek shouldn't be necessary, but I have had some
  279.                  * crazy problems with a failed fseek() on Solaris leaving
  280.                  * the current file pointer out of whack when an fwrite()
  281.                  * is done. 
  282.                  */
  283.                 TIFFSeekFile( tif, 0, SEEK_SET );
  284. if (!WriteOK(tif, &tif->tif_header, sizeof (TIFFHeader))) {
  285. TIFFError(name, "Error writing TIFF header");
  286. goto bad;
  287. }
  288. /*
  289.  * Setup the byte order handling.
  290.  */
  291. TIFFInitOrder(tif, tif->tif_header.tiff_magic, bigendian);
  292. /*
  293.  * Setup default directory.
  294.  */
  295. if (!TIFFDefaultDirectory(tif))
  296. goto bad;
  297. tif->tif_diroff = 0;
  298. return (tif);
  299. }
  300. /*
  301.  * Setup the byte order handling.
  302.  */
  303. if (tif->tif_header.tiff_magic != TIFF_BIGENDIAN &&
  304.     tif->tif_header.tiff_magic != TIFF_LITTLEENDIAN) {
  305. TIFFError(name,  "Not a TIFF file, bad magic number %d (0x%x)",
  306.     tif->tif_header.tiff_magic,
  307.     tif->tif_header.tiff_magic);
  308. goto bad;
  309. }
  310. TIFFInitOrder(tif, tif->tif_header.tiff_magic, bigendian);
  311. /*
  312.  * Swap header if required.
  313.  */
  314. if (tif->tif_flags & TIFF_SWAB) {
  315. TIFFSwabShort(&tif->tif_header.tiff_version);
  316. TIFFSwabLong(&tif->tif_header.tiff_diroff);
  317. }
  318. /*
  319.  * Now check version (if needed, it's been byte-swapped).
  320.  * Note that this isn't actually a version number, it's a
  321.  * magic number that doesn't change (stupid).
  322.  */
  323. if (tif->tif_header.tiff_version != TIFF_VERSION) {
  324. TIFFError(name,
  325.     "Not a TIFF file, bad version number %d (0x%x)",
  326.     tif->tif_header.tiff_version,
  327.     tif->tif_header.tiff_version); 
  328. goto bad;
  329. }
  330. tif->tif_flags |= TIFF_MYBUFFER;
  331. tif->tif_rawcp = tif->tif_rawdata = 0;
  332. tif->tif_rawdatasize = 0;
  333. /*
  334.  * Setup initial directory.
  335.  */
  336. switch (mode[0]) {
  337. case 'r':
  338. tif->tif_nextdiroff = tif->tif_header.tiff_diroff;
  339. /*
  340.  * Try to use a memory-mapped file if the client
  341.  * has not explicitly suppressed usage with the
  342.  * 'm' flag in the open mode (see above).
  343.  */
  344. if ((tif->tif_flags & TIFF_MAPPED) &&
  345. !TIFFMapFileContents(tif, (tdata_t*) &tif->tif_base, &tif->tif_size))
  346. tif->tif_flags &= ~TIFF_MAPPED;
  347. if (TIFFReadDirectory(tif)) {
  348.                         if( m != O_RDONLY 
  349.                           && tif->tif_dir.td_compression != COMPRESSION_NONE )
  350.                         {
  351.                             TIFFError( name, 
  352.                                        "Can't open a compressed TIFF file"
  353.                                        " with compression for update." );
  354.                             goto bad;
  355.                         }
  356. tif->tif_rawcc = -1;
  357. tif->tif_flags |= TIFF_BUFFERSETUP;
  358. return (tif);
  359. }
  360. break;
  361. case 'a':
  362. /*
  363.  * New directories are automatically append
  364.  * to the end of the directory chain when they
  365.  * are written out (see TIFFWriteDirectory).
  366.  */
  367. if (!TIFFDefaultDirectory(tif))
  368. goto bad;
  369. return (tif);
  370. }
  371. bad:
  372. tif->tif_mode = O_RDONLY; /* XXX avoid flush */
  373. TIFFClose(tif);
  374. return ((TIFF*)0);
  375. bad2:
  376. (void) (*closeproc)(clientdata);
  377. return ((TIFF*)0);
  378. }
  379. /*
  380.  * Query functions to access private data.
  381.  */
  382. /*
  383.  * Return open file's name.
  384.  */
  385. const char *
  386. TIFFFileName(TIFF* tif)
  387. {
  388. return (tif->tif_name);
  389. }
  390. /*
  391.  * Return open file's I/O descriptor.
  392.  */
  393. int
  394. TIFFFileno(TIFF* tif)
  395. {
  396. return (tif->tif_fd);
  397. }
  398. /*
  399.  * Return read/write mode.
  400.  */
  401. int
  402. TIFFGetMode(TIFF* tif)
  403. {
  404. return (tif->tif_mode);
  405. }
  406. /*
  407.  * Return nonzero if file is organized in
  408.  * tiles; zero if organized as strips.
  409.  */
  410. int
  411. TIFFIsTiled(TIFF* tif)
  412. {
  413. return (isTiled(tif));
  414. }
  415. /*
  416.  * Return current row being read/written.
  417.  */
  418. uint32
  419. TIFFCurrentRow(TIFF* tif)
  420. {
  421. return (tif->tif_row);
  422. }
  423. /*
  424.  * Return index of the current directory.
  425.  */
  426. tdir_t
  427. TIFFCurrentDirectory(TIFF* tif)
  428. {
  429. return (tif->tif_curdir);
  430. }
  431. /*
  432.  * Return current strip.
  433.  */
  434. tstrip_t
  435. TIFFCurrentStrip(TIFF* tif)
  436. {
  437. return (tif->tif_curstrip);
  438. }
  439. /*
  440.  * Return current tile.
  441.  */
  442. ttile_t
  443. TIFFCurrentTile(TIFF* tif)
  444. {
  445. return (tif->tif_curtile);
  446. }
  447. /*
  448.  * Return nonzero if the file has byte-swapped data.
  449.  */
  450. int
  451. TIFFIsByteSwapped(TIFF* tif)
  452. {
  453. return ((tif->tif_flags & TIFF_SWAB) != 0);
  454. }
  455. /*
  456.  * Return nonzero if the data is returned up-sampled.
  457.  */
  458. int
  459. TIFFIsUpSampled(TIFF* tif)
  460. {
  461. return (isUpSampled(tif));
  462. }
  463. /*
  464.  * Return nonzero if the data is returned in MSB-to-LSB bit order.
  465.  */
  466. int
  467. TIFFIsMSB2LSB(TIFF* tif)
  468. {
  469. return (isFillOrder(tif, FILLORDER_MSB2LSB));
  470. }