home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 February / PCWorld_2002-02_cd.bin / Software / Vyzkuste / pdflib / pdflib-4.0.1.sit / pdflib-4.0.1 / tiff / tif_open.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-04  |  11.8 KB  |  443 lines  |  [TEXT/CWIE]

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