home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / NETWORK / netpbm_src.lzh / NETPBM / LIBTIFF / tif_open.c < prev    next >
Text File  |  1996-11-18  |  9KB  |  375 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_open.c,v 1.43 93/08/26 14:25:21 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * TIFF Library.
  31.  */
  32. #include "tiffiop.h"
  33.  
  34. static const long typemask[13] = {
  35.     0L,        /* TIFF_NOTYPE */
  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 */
  48. };
  49. static const int bigTypeshift[13] = {
  50.     0,        /* TIFF_NOTYPE */
  51.     24,        /* TIFF_BYTE */
  52.     0,        /* TIFF_ASCII */
  53.     16,        /* TIFF_SHORT */
  54.     0,        /* TIFF_LONG */
  55.     0,        /* TIFF_RATIONAL */
  56.     16,        /* TIFF_SBYTE */
  57.     16,        /* TIFF_UNDEFINED */
  58.     24,        /* TIFF_SSHORT */
  59.     0,        /* TIFF_SLONG */
  60.     0,        /* TIFF_SRATIONAL */
  61.     0,        /* TIFF_FLOAT */
  62.     0,        /* TIFF_DOUBLE */
  63. };
  64. static const int litTypeshift[13] = {
  65.     0,        /* TIFF_NOTYPE */
  66.     0,        /* TIFF_BYTE */
  67.     0,        /* TIFF_ASCII */
  68.     0,        /* TIFF_SHORT */
  69.     0,        /* TIFF_LONG */
  70.     0,        /* TIFF_RATIONAL */
  71.     0,        /* TIFF_SBYTE */
  72.     0,        /* TIFF_UNDEFINED */
  73.     0,        /* TIFF_SSHORT */
  74.     0,        /* TIFF_SLONG */
  75.     0,        /* TIFF_SRATIONAL */
  76.     0,        /* TIFF_FLOAT */
  77.     0,        /* TIFF_DOUBLE */
  78. };
  79.  
  80. /*
  81.  * Initialize the bit fill order, the
  82.  * shift & mask tables, and the byte
  83.  * swapping state according to the file
  84.  * contents and the machine architecture.
  85.  */
  86. static void
  87. TIFFInitOrder(register TIFF* tif, int magic, int bigendian)
  88. {
  89.     /* XXX how can we deduce this dynamically? */
  90.     tif->tif_fillorder = FILLORDER_MSB2LSB;
  91.  
  92.     tif->tif_typemask = typemask;
  93.     if (magic == TIFF_BIGENDIAN) {
  94.         tif->tif_typeshift = bigTypeshift;
  95.         if (!bigendian)
  96.             tif->tif_flags |= TIFF_SWAB;
  97.     } else {
  98.         tif->tif_typeshift = litTypeshift;
  99.         if (bigendian)
  100.             tif->tif_flags |= TIFF_SWAB;
  101.     }
  102. }
  103.  
  104. int
  105. _TIFFgetMode(const char* mode, const char* module)
  106. {
  107.     int m = -1;
  108.  
  109.     switch (mode[0]) {
  110.     case 'r':
  111.         m = O_RDONLY;
  112.         if (mode[1] == '+')
  113.             m = O_RDWR;
  114.         break;
  115.     case 'w':
  116.     case 'a':
  117.         m = O_RDWR|O_CREAT;
  118.         if (mode[0] == 'w')
  119.             m |= O_TRUNC;
  120.         break;
  121.     default:
  122.         TIFFError(module, "\"%s\": Bad mode", mode);
  123.         break;
  124.     }
  125.     return (m);
  126. }
  127.  
  128. TIFF*
  129. TIFFClientOpen(
  130.     const char* name, const char* mode,
  131.     thandle_t clientdata,
  132.     TIFFReadWriteProc readproc,
  133.     TIFFReadWriteProc writeproc,
  134.     TIFFSeekProc seekproc,
  135.     TIFFCloseProc closeproc,
  136.     TIFFSizeProc sizeproc,
  137.     TIFFMapFileProc mapproc,
  138.     TIFFUnmapFileProc unmapproc
  139. )
  140. {
  141.     static const char module[] = "TIFFClientOpen";
  142.     TIFF *tif;
  143.     int m, bigendian;
  144.  
  145.     m = _TIFFgetMode(mode, module);
  146.     if (m == -1)
  147.         goto bad2;
  148.     tif = (TIFF *)_TIFFmalloc(sizeof (TIFF) + strlen(name) + 1);
  149.     if (tif == NULL) {
  150.         TIFFError(module, "%s: Out of memory (TIFF structure)", name);
  151.         goto bad2;
  152.     }
  153.     memset(tif, 0, sizeof (*tif));
  154.     tif->tif_name = (char *)tif + sizeof (TIFF);
  155.     strcpy(tif->tif_name, name);
  156.     tif->tif_mode = m &~ (O_CREAT|O_TRUNC);
  157.     tif->tif_curdir = -1;        /* non-existent directory */
  158.     tif->tif_curoff = 0;
  159.     tif->tif_curstrip = -1;        /* invalid strip */
  160.     tif->tif_row = -1;        /* read/write pre-increment */
  161.     tif->tif_clientdata = clientdata;
  162.     tif->tif_readproc = readproc;
  163.     tif->tif_writeproc = writeproc;
  164.     tif->tif_seekproc = seekproc;
  165.     tif->tif_closeproc = closeproc;
  166.     tif->tif_sizeproc = sizeproc;
  167.     tif->tif_mapproc = mapproc;
  168.     tif->tif_unmapproc = unmapproc;
  169.  
  170.     { int one = 1; char* cp = (char*)&one; bigendian = (*cp == 0); }
  171.     /*
  172.      * Read in TIFF header.
  173.      */
  174.     if (!ReadOK(tif, &tif->tif_header, sizeof (TIFFHeader))) {
  175.         if (tif->tif_mode == O_RDONLY) {
  176.             TIFFError(name, "Cannot read TIFF header");
  177.             goto bad;
  178.         }
  179.         /*
  180.          * Setup header and write.
  181.          */
  182.         tif->tif_header.tiff_magic =  bigendian ?
  183.             TIFF_BIGENDIAN : TIFF_LITTLEENDIAN;
  184.         tif->tif_header.tiff_version = TIFF_VERSION;
  185.         tif->tif_header.tiff_diroff = 0;    /* filled in later */
  186.         if (!WriteOK(tif, &tif->tif_header, sizeof (TIFFHeader))) {
  187.             TIFFError(name, "Error writing TIFF header");
  188.             goto bad;
  189.         }
  190.         /*
  191.          * Setup the byte order handling.
  192.          */
  193.         TIFFInitOrder(tif, tif->tif_header.tiff_magic, bigendian);
  194.         /*
  195.          * Setup default directory.
  196.          */
  197.         if (!TIFFDefaultDirectory(tif))
  198.             goto bad;
  199.         tif->tif_diroff = 0;
  200.         return (tif);
  201.     }
  202.     /*
  203.      * Setup the byte order handling.
  204.      */
  205.     if (tif->tif_header.tiff_magic != TIFF_BIGENDIAN &&
  206.         tif->tif_header.tiff_magic != TIFF_LITTLEENDIAN) {
  207.         TIFFError(name,  "Not a TIFF file, bad magic number %d (0x%x)",
  208.             tif->tif_header.tiff_magic,
  209.             tif->tif_header.tiff_magic);
  210.         goto bad;
  211.     }
  212.     TIFFInitOrder(tif, tif->tif_header.tiff_magic, bigendian);
  213.     /*
  214.      * Swap header if required.
  215.      */
  216.     if (tif->tif_flags & TIFF_SWAB) {
  217.         TIFFSwabShort(&tif->tif_header.tiff_version);
  218.         TIFFSwabLong(&tif->tif_header.tiff_diroff);
  219.     }
  220.     /*
  221.      * Now check version (if needed, it's been byte-swapped).
  222.      * Note that this isn't actually a version number, it's a
  223.      * magic number that doesn't change (stupid).
  224.      */
  225.     if (tif->tif_header.tiff_version != TIFF_VERSION) {
  226.         TIFFError(name,
  227.             "Not a TIFF file, bad version number %d (0x%x)",
  228.             tif->tif_header.tiff_version,
  229.             tif->tif_header.tiff_version); 
  230.         goto bad;
  231.     }
  232.     tif->tif_flags |= TIFF_MYBUFFER;
  233.     tif->tif_rawcp = tif->tif_rawdata = 0;
  234.     tif->tif_rawdatasize = 0;
  235.     /*
  236.      * Setup initial directory.
  237.      */
  238.     switch (mode[0]) {
  239.     case 'r':
  240.         tif->tif_nextdiroff = tif->tif_header.tiff_diroff;
  241.         if (TIFFMapFileContents(tif, (tdata_t*) &tif->tif_base, &tif->tif_size))
  242.             tif->tif_flags |= TIFF_MAPPED;
  243.         if (TIFFReadDirectory(tif)) {
  244.             tif->tif_rawcc = -1;
  245.             tif->tif_flags |= TIFF_BUFFERSETUP;
  246.             return (tif);
  247.         }
  248.         break;
  249.     case 'a':
  250.         /*
  251.          * Don't append to file that has information
  252.          * byte swapped -- we will write data that is
  253.          * in the opposite order.
  254.          */
  255.         if (tif->tif_flags & TIFF_SWAB) {
  256.             TIFFError(name,
  257.         "Cannot append to file that has opposite byte ordering");
  258.             goto bad;
  259.         }
  260.         /*
  261.          * New directories are automatically append
  262.          * to the end of the directory chain when they
  263.          * are written out (see TIFFWriteDirectory).
  264.          */
  265.         if (!TIFFDefaultDirectory(tif))
  266.             goto bad;
  267.         return (tif);
  268.     }
  269. bad:
  270.     tif->tif_mode = O_RDONLY;    /* XXX avoid flush */
  271.     TIFFClose(tif);
  272.     return ((TIFF*)0);
  273. bad2:
  274.     (void) (*closeproc)(clientdata);
  275.     return ((TIFF*)0);
  276. }
  277.  
  278. tsize_t
  279. TIFFScanlineSize(TIFF* tif)
  280. {
  281.     TIFFDirectory *td = &tif->tif_dir;
  282.     tsize_t scanline;
  283.     
  284.     scanline = td->td_bitspersample * td->td_imagewidth;
  285.     if (td->td_planarconfig == PLANARCONFIG_CONTIG)
  286.         scanline *= td->td_samplesperpixel;
  287.     return (howmany(scanline, 8));
  288. }
  289.  
  290. /*
  291.  * Query functions to access private data.
  292.  */
  293.  
  294. /*
  295.  * Return open file's name.
  296.  */
  297. const char *
  298. TIFFFileName(TIFF* tif)
  299. {
  300.     return (tif->tif_name);
  301. }
  302.  
  303. /*
  304.  * Return open file's I/O descriptor.
  305.  */
  306. int
  307. TIFFFileno(TIFF* tif)
  308. {
  309.     return (tif->tif_fd);
  310. }
  311.  
  312. /*
  313.  * Return read/write mode.
  314.  */
  315. int
  316. TIFFGetMode(TIFF* tif)
  317. {
  318.     return (tif->tif_mode);
  319. }
  320.  
  321. /*
  322.  * Return nonzero if file is organized in
  323.  * tiles; zero if organized as strips.
  324.  */
  325. int
  326. TIFFIsTiled(TIFF* tif)
  327. {
  328.     return (isTiled(tif));
  329. }
  330.  
  331. /*
  332.  * Return current row being read/written.
  333.  */
  334. uint32
  335. TIFFCurrentRow(TIFF* tif)
  336. {
  337.     return (tif->tif_row);
  338. }
  339.  
  340. /*
  341.  * Return index of the current directory.
  342.  */
  343. tdir_t
  344. TIFFCurrentDirectory(TIFF* tif)
  345. {
  346.     return (tif->tif_curdir);
  347. }
  348.  
  349. /*
  350.  * Return current strip.
  351.  */
  352. tstrip_t
  353. TIFFCurrentStrip(TIFF* tif)
  354. {
  355.     return (tif->tif_curstrip);
  356. }
  357.  
  358. /*
  359.  * Return current tile.
  360.  */
  361. ttile_t
  362. TIFFCurrentTile(TIFF* tif)
  363. {
  364.     return (tif->tif_curtile);
  365. }
  366.  
  367. /*
  368.  * Return nonzero if the file has byte-swapped data.
  369.  */
  370. int
  371. TIFFIsByteSwapped(TIFF* tif)
  372. {
  373.     return ((tif->tif_flags & TIFF_SWAB) != 0);
  374. }
  375.