home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / graphics / libtiff_1 / c / tif_open < prev    next >
Text File  |  1995-10-12  |  10KB  |  400 lines

  1. /* $Header: /usr/people/sam/tiff/libtiff/RCS/tif_open.c,v 1.59 1995/07/20 16:38:53 sam Exp $ */
  2.  
  3. /*
  4.  * Copyright (c) 1988-1995 Sam Leffler
  5.  * Copyright (c) 1991-1995 Silicon Graphics, Inc.
  6.  *
  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.
  14.  * 
  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.  
  18.  * 
  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 
  24.  * OF THIS SOFTWARE.
  25.  */
  26.  
  27. /*
  28.  * TIFF Library.
  29.  */
  30. #include "tiffiop.h"
  31.  
  32. static const long typemask[13] = {
  33.     0L,        /* TIFF_NOTYPE */
  34.     0x000000ffL,    /* TIFF_BYTE */
  35.     0xffffffffL,    /* TIFF_ASCII */
  36.     0x0000ffffL,    /* TIFF_SHORT */
  37.     0xffffffffL,    /* TIFF_LONG */
  38.     0xffffffffL,    /* TIFF_RATIONAL */
  39.     0x000000ffL,    /* TIFF_SBYTE */
  40.     0x000000ffL,    /* TIFF_UNDEFINED */
  41.     0x0000ffffL,    /* TIFF_SSHORT */
  42.     0xffffffffL,    /* TIFF_SLONG */
  43.     0xffffffffL,    /* TIFF_SRATIONAL */
  44.     0xffffffffL,    /* TIFF_FLOAT */
  45.     0xffffffffL,    /* TIFF_DOUBLE */
  46. };
  47. static const int bigTypeshift[13] = {
  48.     0,        /* TIFF_NOTYPE */
  49.     24,        /* TIFF_BYTE */
  50.     0,        /* TIFF_ASCII */
  51.     16,        /* TIFF_SHORT */
  52.     0,        /* TIFF_LONG */
  53.     0,        /* TIFF_RATIONAL */
  54.     24,        /* TIFF_SBYTE */
  55.     24,        /* TIFF_UNDEFINED */
  56.     16,        /* TIFF_SSHORT */
  57.     0,        /* TIFF_SLONG */
  58.     0,        /* TIFF_SRATIONAL */
  59.     0,        /* TIFF_FLOAT */
  60.     0,        /* TIFF_DOUBLE */
  61. };
  62. static const int litTypeshift[13] = {
  63.     0,        /* TIFF_NOTYPE */
  64.     0,        /* TIFF_BYTE */
  65.     0,        /* TIFF_ASCII */
  66.     0,        /* TIFF_SHORT */
  67.     0,        /* TIFF_LONG */
  68.     0,        /* TIFF_RATIONAL */
  69.     0,        /* TIFF_SBYTE */
  70.     0,        /* TIFF_UNDEFINED */
  71.     0,        /* TIFF_SSHORT */
  72.     0,        /* TIFF_SLONG */
  73.     0,        /* TIFF_SRATIONAL */
  74.     0,        /* TIFF_FLOAT */
  75.     0,        /* TIFF_DOUBLE */
  76. };
  77.  
  78. /*
  79.  * Initialize the bit fill order, the
  80.  * shift & mask tables, and the byte
  81.  * swapping state according to the file
  82.  * contents and the machine architecture.
  83.  */
  84. static void
  85. TIFFInitOrder(register TIFF* tif, int magic, int bigendian)
  86. {
  87. #ifdef notdef
  88.     /*
  89.      * NB: too many applications assume that data is returned
  90.      *     by the library in MSB2LSB bit order to change the
  91.      *     default bit order to reflect the native cpu.  This
  92.      *     may change in the future in which case applications
  93.      *     will need to check the value of the FillOrder tag.
  94.      */
  95.     tif->tif_flags = (tif->tif_flags &~ TIFF_FILLORDER) | HOST_FILLORDER;
  96. #else
  97.     tif->tif_flags = (tif->tif_flags &~ TIFF_FILLORDER) | FILLORDER_MSB2LSB;
  98. #endif
  99.  
  100.     tif->tif_typemask = typemask;
  101.     if (magic == TIFF_BIGENDIAN) {
  102.         tif->tif_typeshift = bigTypeshift;
  103.         if (!bigendian)
  104.             tif->tif_flags |= TIFF_SWAB;
  105.     } else {
  106.         tif->tif_typeshift = litTypeshift;
  107.         if (bigendian)
  108.             tif->tif_flags |= TIFF_SWAB;
  109.     }
  110. }
  111.  
  112. int
  113. _TIFFgetMode(const char* mode, const char* module)
  114. {
  115.     int m = -1;
  116.  
  117.     switch (mode[0]) {
  118.     case 'r':
  119.         m = O_RDONLY;
  120.         if (mode[1] == '+')
  121.             m = O_RDWR;
  122.         break;
  123.     case 'w':
  124.     case 'a':
  125.         m = O_RDWR|O_CREAT;
  126.         if (mode[0] == 'w')
  127.             m |= O_TRUNC;
  128.         break;
  129.     default:
  130.         TIFFError(module, "\"%s\": Bad mode", mode);
  131.         break;
  132.     }
  133.     return (m);
  134. }
  135.  
  136. TIFF*
  137. TIFFClientOpen(
  138.     const char* name, const char* mode,
  139.     thandle_t clientdata,
  140.     TIFFReadWriteProc readproc,
  141.     TIFFReadWriteProc writeproc,
  142.     TIFFSeekProc seekproc,
  143.     TIFFCloseProc closeproc,
  144.     TIFFSizeProc sizeproc,
  145.     TIFFMapFileProc mapproc,
  146.     TIFFUnmapFileProc unmapproc
  147. )
  148. {
  149.     static const char module[] = "TIFFClientOpen";
  150.     TIFF *tif;
  151.     int m, bigendian;
  152.  
  153.     m = _TIFFgetMode(mode, module);
  154.     if (m == -1)
  155.         goto bad2;
  156.     tif = (TIFF *)_TIFFmalloc(sizeof (TIFF) + strlen(name) + 1);
  157.     if (tif == NULL) {
  158.         TIFFError(module, "%s: Out of memory (TIFF structure)", name);
  159.         goto bad2;
  160.     }
  161.     _TIFFmemset(tif, 0, sizeof (*tif));
  162.     tif->tif_name = (char *)tif + sizeof (TIFF);
  163.     strcpy(tif->tif_name, name);
  164.     tif->tif_mode = m &~ (O_CREAT|O_TRUNC);
  165.     tif->tif_curdir = (tdir_t) -1;        /* non-existent directory */
  166.     tif->tif_curoff = 0;
  167.     tif->tif_curstrip = (tstrip_t) -1;    /* invalid strip */
  168.     tif->tif_row = (uint32)-1;        /* read/write pre-increment */
  169.     tif->tif_clientdata = clientdata;
  170.     tif->tif_readproc = readproc;
  171.     tif->tif_writeproc = writeproc;
  172.     tif->tif_seekproc = seekproc;
  173.     tif->tif_closeproc = closeproc;
  174.     tif->tif_sizeproc = sizeproc;
  175.     tif->tif_mapproc = mapproc;
  176.     tif->tif_unmapproc = unmapproc;
  177.  
  178.     { union { int32 i; char c[4]; } u; u.i = 1; bigendian = u.c[0] == 0; }
  179. #ifdef ENDIANHACK_SUPPORT
  180.     /*
  181.      * Numerous vendors, typically on the PC, do not correctly
  182.      * support TIFF; they only support the Intel little-endian
  183.      * byte order.  If this hack is enabled, then applications
  184.      * can open a file with a specific byte-order by specifying
  185.      * either "wl" (for litt-endian byte order) or "wb" for
  186.      * (big-endian byte order).  This support is not configured
  187.      * by default because it supports the violation of the TIFF
  188.      * spec that says that readers *MUST* support both byte orders.
  189.      *
  190.      * It is strongly recommended that you not use this feature
  191.      * except to deal with busted apps that write invalid TIFF.
  192.      * And even in those cases you should bang on the vendors to
  193.      * fix their software.
  194.      */
  195.     if ((m&O_CREAT) &&
  196.         ((bigendian && mode[1] == 'l') || (!bigendian && mode[1] == 'b')))
  197.         tif->tif_flags |= TIFF_SWAB;
  198. #endif
  199.     /*
  200.      * Read in TIFF header.
  201.      */
  202.     if (!ReadOK(tif, &tif->tif_header, sizeof (TIFFHeader))) {
  203.         if (tif->tif_mode == O_RDONLY) {
  204.             TIFFError(name, "Cannot read TIFF header");
  205.             goto bad;
  206.         }
  207.         /*
  208.          * Setup header and write.
  209.          */
  210.         tif->tif_header.tiff_magic = tif->tif_flags & TIFF_SWAB
  211.             ? (bigendian ? TIFF_LITTLEENDIAN : TIFF_BIGENDIAN)
  212.             : (bigendian ? TIFF_BIGENDIAN : TIFF_LITTLEENDIAN);
  213.         tif->tif_header.tiff_version = TIFF_VERSION;
  214.         tif->tif_header.tiff_diroff = 0;    /* filled in later */
  215.         if (!WriteOK(tif, &tif->tif_header, sizeof (TIFFHeader))) {
  216.             TIFFError(name, "Error writing TIFF header");
  217.             goto bad;
  218.         }
  219.         /*
  220.          * Setup the byte order handling.
  221.          */
  222.         TIFFInitOrder(tif, tif->tif_header.tiff_magic, bigendian);
  223.         /*
  224.          * Setup default directory.
  225.          */
  226.         if (!TIFFDefaultDirectory(tif))
  227.             goto bad;
  228.         tif->tif_diroff = 0;
  229.         return (tif);
  230.     }
  231.     /*
  232.      * Setup the byte order handling.
  233.      */
  234.     if (tif->tif_header.tiff_magic != TIFF_BIGENDIAN &&
  235.         tif->tif_header.tiff_magic != TIFF_LITTLEENDIAN) {
  236.         TIFFError(name,  "Not a TIFF file, bad magic number %d (0x%x)",
  237.             tif->tif_header.tiff_magic,
  238.             tif->tif_header.tiff_magic);
  239.         goto bad;
  240.     }
  241.     TIFFInitOrder(tif, tif->tif_header.tiff_magic, bigendian);
  242.     /*
  243.      * Swap header if required.
  244.      */
  245.     if (tif->tif_flags & TIFF_SWAB) {
  246.         TIFFSwabShort(&tif->tif_header.tiff_version);
  247.         TIFFSwabLong(&tif->tif_header.tiff_diroff);
  248.     }
  249.     /*
  250.      * Now check version (if needed, it's been byte-swapped).
  251.      * Note that this isn't actually a version number, it's a
  252.      * magic number that doesn't change (stupid).
  253.      */
  254.     if (tif->tif_header.tiff_version != TIFF_VERSION) {
  255.         TIFFError(name,
  256.             "Not a TIFF file, bad version number %d (0x%x)",
  257.             tif->tif_header.tiff_version,
  258.             tif->tif_header.tiff_version); 
  259.         goto bad;
  260.     }
  261.     tif->tif_flags |= TIFF_MYBUFFER;
  262.     tif->tif_rawcp = tif->tif_rawdata = 0;
  263.     tif->tif_rawdatasize = 0;
  264.     /*
  265.      * Setup initial directory.
  266.      */
  267.     switch (mode[0]) {
  268.     case 'r':
  269.         tif->tif_nextdiroff = tif->tif_header.tiff_diroff;
  270.         if (TIFFMapFileContents(tif, (tdata_t*) &tif->tif_base, &tif->tif_size))
  271.             tif->tif_flags |= TIFF_MAPPED;
  272.         if (TIFFReadDirectory(tif)) {
  273.             tif->tif_rawcc = -1;
  274.             tif->tif_flags |= TIFF_BUFFERSETUP;
  275.             return (tif);
  276.         }
  277.         break;
  278.     case 'a':
  279.         /*
  280.          * New directories are automatically append
  281.          * to the end of the directory chain when they
  282.          * are written out (see TIFFWriteDirectory).
  283.          */
  284.         if (!TIFFDefaultDirectory(tif))
  285.             goto bad;
  286.         return (tif);
  287.     }
  288. bad:
  289.     tif->tif_mode = O_RDONLY;    /* XXX avoid flush */
  290.     TIFFClose(tif);
  291.     return ((TIFF*)0);
  292. bad2:
  293.     (void) (*closeproc)(clientdata);
  294.     return ((TIFF*)0);
  295. }
  296.  
  297. /*
  298.  * Query functions to access private data.
  299.  */
  300.  
  301. /*
  302.  * Return open file's name.
  303.  */
  304. const char *
  305. TIFFFileName(TIFF* tif)
  306. {
  307.     return (tif->tif_name);
  308. }
  309.  
  310. /*
  311.  * Return open file's I/O descriptor.
  312.  */
  313. int
  314. TIFFFileno(TIFF* tif)
  315. {
  316.     return (tif->tif_fd);
  317. }
  318.  
  319. /*
  320.  * Return read/write mode.
  321.  */
  322. int
  323. TIFFGetMode(TIFF* tif)
  324. {
  325.     return (tif->tif_mode);
  326. }
  327.  
  328. /*
  329.  * Return nonzero if file is organized in
  330.  * tiles; zero if organized as strips.
  331.  */
  332. int
  333. TIFFIsTiled(TIFF* tif)
  334. {
  335.     return (isTiled(tif));
  336. }
  337.  
  338. /*
  339.  * Return current row being read/written.
  340.  */
  341. uint32
  342. TIFFCurrentRow(TIFF* tif)
  343. {
  344.     return (tif->tif_row);
  345. }
  346.  
  347. /*
  348.  * Return index of the current directory.
  349.  */
  350. tdir_t
  351. TIFFCurrentDirectory(TIFF* tif)
  352. {
  353.     return (tif->tif_curdir);
  354. }
  355.  
  356. /*
  357.  * Return current strip.
  358.  */
  359. tstrip_t
  360. TIFFCurrentStrip(TIFF* tif)
  361. {
  362.     return (tif->tif_curstrip);
  363. }
  364.  
  365. /*
  366.  * Return current tile.
  367.  */
  368. ttile_t
  369. TIFFCurrentTile(TIFF* tif)
  370. {
  371.     return (tif->tif_curtile);
  372. }
  373.  
  374. /*
  375.  * Return nonzero if the file has byte-swapped data.
  376.  */
  377. int
  378. TIFFIsByteSwapped(TIFF* tif)
  379. {
  380.     return ((tif->tif_flags & TIFF_SWAB) != 0);
  381. }
  382.  
  383. /*
  384.  * Return nonzero if the data is returned up-sampled.
  385.  */
  386. int
  387. TIFFIsUpSampled(TIFF* tif)
  388. {
  389.     return (isUpSampled(tif));
  390. }
  391.  
  392. /*
  393.  * Return nonzero if the data is returned in MSB-to-LSB bit order.
  394.  */
  395. int
  396. TIFFIsMSB2LSB(TIFF* tif)
  397. {
  398.     return (isFillOrder(tif, FILLORDER_MSB2LSB));
  399. }
  400.