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_read.c < prev    next >
Text File  |  1996-11-18  |  16KB  |  564 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_read.c,v 1.55 93/08/26 14:26:43 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.  * Scanline-oriented Read Support
  32.  */
  33. #include "tiffiop.h"
  34. #include <stdio.h>
  35. #include <assert.h>
  36.  
  37. static    int TIFFFillStrip(TIFF*, tstrip_t);
  38. static    int TIFFFillTile(TIFF*, ttile_t);
  39. static    int TIFFStartStrip(TIFF*, tstrip_t);
  40. static    int TIFFStartTile(TIFF*, ttile_t);
  41. static    int TIFFCheckRead(TIFF*, int);
  42.  
  43. /*
  44.  * Seek to a random row+sample in a file.
  45.  */
  46. static int
  47. TIFFSeek(TIFF* tif, uint32 row, tsample_t sample)
  48. {
  49.     register TIFFDirectory *td = &tif->tif_dir;
  50.     tstrip_t strip;
  51.  
  52.     if (row >= td->td_imagelength) {    /* out of range */
  53.         TIFFError(tif->tif_name, "%lu: Row out of range, max %lu",
  54.             (u_long) row, (u_long) td->td_imagelength);
  55.         return (0);
  56.     }
  57.     if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
  58.         if (sample >= td->td_samplesperpixel) {
  59.             TIFFError(tif->tif_name,
  60.                 "%lu: Sample out of range, max %lu",
  61.                 (u_long) sample, (u_long) td->td_samplesperpixel);
  62.             return (0);
  63.         }
  64.         strip = sample*td->td_stripsperimage + row/td->td_rowsperstrip;
  65.     } else
  66.         strip = row / td->td_rowsperstrip;
  67.     if (strip != tif->tif_curstrip) {     /* different strip, refill */
  68.         if (!TIFFFillStrip(tif, strip))
  69.             return (0);
  70.     } else if (row < tif->tif_row) {
  71.         /*
  72.          * Moving backwards within the same strip: backup
  73.          * to the start and then decode forward (below).
  74.          *
  75.          * NB: If you're planning on lots of random access within a
  76.          * strip, it's better to just read and decode the entire
  77.          * strip, and then access the decoded data in a random fashion.
  78.          */
  79.         if (!TIFFStartStrip(tif, strip))
  80.             return (0);
  81.     }
  82.     if (row != tif->tif_row) {
  83.         if (tif->tif_seek) {
  84.             /*
  85.              * Seek forward to the desired row.
  86.              */
  87.             if (!(*tif->tif_seek)(tif, row - tif->tif_row))
  88.                 return (0);
  89.             tif->tif_row = row;
  90.         } else {
  91.             TIFFError(tif->tif_name,
  92.             "Compression algorithm does not support random access");
  93.             return (0);
  94.         }
  95.     }
  96.     return (1);
  97. }
  98.  
  99. int
  100. TIFFReadScanline(TIFF* tif, tdata_t buf, uint32 row, tsample_t sample)
  101. {
  102.     int e;
  103.  
  104.     if (!TIFFCheckRead(tif, 0))
  105.         return (-1);
  106.     if (e = TIFFSeek(tif, row, sample)) {
  107.         /*
  108.          * Decompress desired row into user buffer.
  109.          */
  110.         e = (*tif->tif_decoderow)
  111.             (tif, buf, tif->tif_scanlinesize, sample);
  112.         tif->tif_row++;
  113.         if (e)
  114.             (*tif->tif_postdecode)(tif, buf, tif->tif_scanlinesize);
  115.     }
  116.     return (e ? 1 : -1);
  117. }
  118.  
  119. /*
  120.  * Read a strip of data and decompress the specified
  121.  * amount into the user-supplied buffer.
  122.  */
  123. tsize_t
  124. TIFFReadEncodedStrip(TIFF* tif, tstrip_t strip, tdata_t buf, tsize_t size)
  125. {
  126.     TIFFDirectory *td = &tif->tif_dir;
  127.     uint32 nrows;
  128.     tsize_t stripsize;
  129.  
  130.     if (!TIFFCheckRead(tif, 0))
  131.         return (-1);
  132.     if (strip >= td->td_nstrips) {
  133.         TIFFError(tif->tif_name, "%ld: Strip out of range, max %ld",
  134.             (long) strip, (long) td->td_nstrips);
  135.         return (-1);
  136.     }
  137.     /*
  138.      * Calculate the strip size according to the number of
  139.      * rows in the strip (check for truncated last strip).
  140.      */
  141.     if (strip != td->td_nstrips-1 ||
  142.         (nrows = td->td_imagelength % td->td_rowsperstrip) == 0)
  143.         nrows = td->td_rowsperstrip;
  144.     stripsize = TIFFVStripSize(tif, nrows);
  145.     if (size == (tsize_t) -1)
  146.         size = stripsize;
  147.     else if (size > stripsize)
  148.         size = stripsize;
  149.     if (TIFFFillStrip(tif, strip) &&
  150.     (*tif->tif_decodestrip)(tif, buf, size, strip / td->td_stripsperimage)) {
  151.         (*tif->tif_postdecode)(tif, buf, size);
  152.         return (size);
  153.     } else
  154.         return ((tsize_t) -1);
  155. }
  156.  
  157. static tsize_t
  158. TIFFReadRawStrip1(TIFF* tif,
  159.     tstrip_t strip, tdata_t buf, tsize_t size, const char* module)
  160. {
  161.     TIFFDirectory *td = &tif->tif_dir;
  162.  
  163.     if (!isMapped(tif)) {
  164.         if (!SeekOK(tif, td->td_stripoffset[strip])) {
  165.             TIFFError(module,
  166.                 "%s: Seek error at scanline %lu, strip %lu",
  167.                 tif->tif_name,
  168.                 (u_long) tif->tif_row, (u_long) strip);
  169.             return (-1);
  170.         }
  171.         if (!ReadOK(tif, buf, size)) {
  172.             TIFFError(module, "%s: Read error at scanline %lu",
  173.                 tif->tif_name, (u_long) tif->tif_row);
  174.             return (-1);
  175.         }
  176.     } else {
  177.         if (td->td_stripoffset[strip] + size > tif->tif_size) {
  178.             TIFFError(module,
  179.                 "%s: Seek error at scanline %lu, strip %lu",
  180.                 tif->tif_name,
  181.                 (u_long) tif->tif_row, (u_long) strip);
  182.             return (-1);
  183.         }
  184.         memcpy(buf, tif->tif_base + td->td_stripoffset[strip], size);
  185.     }
  186.     return (size);
  187. }
  188.  
  189. /*
  190.  * Read a strip of data from the file.
  191.  */
  192. tsize_t
  193. TIFFReadRawStrip(TIFF* tif, tstrip_t strip, tdata_t buf, tsize_t size)
  194. {
  195.     static const char module[] = "TIFFReadRawStrip";
  196.     TIFFDirectory *td = &tif->tif_dir;
  197.     tsize_t bytecount;
  198.  
  199.     if (!TIFFCheckRead(tif, 0))
  200.         return (-1);
  201.     if (strip >= td->td_nstrips) {
  202.         TIFFError(tif->tif_name, "%lu: Strip out of range, max %lu",
  203.             (u_long) strip, (u_long) td->td_nstrips);
  204.         return (-1);
  205.     }
  206.     bytecount = td->td_stripbytecount[strip];
  207.     if (size != (tsize_t)-1 && size < bytecount)
  208.         bytecount = size;
  209.     return (TIFFReadRawStrip1(tif, strip, buf, bytecount, module));
  210. }
  211.  
  212. /*
  213.  * Read the specified strip and setup for decoding. 
  214.  * The data buffer is expanded, as necessary, to
  215.  * hold the strip's data.
  216.  */
  217. static int
  218. TIFFFillStrip(TIFF* tif, tstrip_t strip)
  219. {
  220.     static const char module[] = "TIFFFillStrip";
  221.     TIFFDirectory *td = &tif->tif_dir;
  222.     tsize_t bytecount;
  223.  
  224.     bytecount = td->td_stripbytecount[strip];
  225.     if (isMapped(tif) &&
  226.         (td->td_fillorder == tif->tif_fillorder || (tif->tif_flags & TIFF_NOBITREV))) {
  227.         /*
  228.          * The image is mapped into memory and we either don't
  229.          * need to flip bits or the compression routine is going
  230.          * to handle this operation itself.  In this case, avoid
  231.          * copying the raw data and instead just reference the
  232.          * data from the memory mapped file image.  This assumes
  233.          * that the decompression routines do not modify the
  234.          * contents of the raw data buffer (if they try to,
  235.          * the application will get a fault since the file is
  236.          * mapped read-only).
  237.          */
  238.         if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
  239.             _TIFFfree(tif->tif_rawdata);
  240.         tif->tif_flags &= ~TIFF_MYBUFFER;
  241.         if (td->td_stripoffset[strip] + bytecount > tif->tif_size) {
  242.             /*
  243.              * This error message might seem strange, but it's
  244.              * what would happen if a read were done instead.
  245.              */
  246.             TIFFError(module, "%s: Read error on strip %lu",
  247.                 tif->tif_name, (u_long) strip);
  248.             tif->tif_curstrip = -1;        /* unknown state */
  249.             return (0);
  250.         }
  251.         tif->tif_rawdatasize = bytecount;
  252.         tif->tif_rawdata = tif->tif_base + td->td_stripoffset[strip];
  253.     } else {
  254.         /*
  255.          * Expand raw data buffer, if needed, to
  256.          * hold data strip coming from file
  257.          * (perhaps should set upper bound on
  258.          *  the size of a buffer we'll use?).
  259.          */
  260.         if (bytecount > tif->tif_rawdatasize) {
  261.             tif->tif_curstrip = -1;        /* unknown state */
  262.             if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
  263.                 TIFFError(module,
  264.                 "%s: Data buffer too small to hold strip %lu",
  265.                     tif->tif_name, (u_long) strip);
  266.                 return (0);
  267.             }
  268.             if (!TIFFReadBufferSetup(tif, 0,
  269.                 roundup(bytecount, 1024)))
  270.                 return (0);
  271.         }
  272.         if (TIFFReadRawStrip1(tif, strip, (u_char *)tif->tif_rawdata,
  273.             bytecount, module) != bytecount)
  274.             return (0);
  275.         if (td->td_fillorder != tif->tif_fillorder &&
  276.             (tif->tif_flags & TIFF_NOBITREV) == 0)
  277.             TIFFReverseBits(tif->tif_rawdata, bytecount);
  278.     }
  279.     return (TIFFStartStrip(tif, strip));
  280. }
  281.  
  282. /*
  283.  * Tile-oriented Read Support
  284.  * Contributed by Nancy Cam (Silicon Graphics).
  285.  */
  286.  
  287. /*
  288.  * Read and decompress a tile of data.  The
  289.  * tile is selected by the (x,y,z,s) coordinates.
  290.  */
  291. tsize_t
  292. TIFFReadTile(TIFF* tif,
  293.     tdata_t buf, uint32 x, uint32 y, uint32 z, tsample_t s)
  294. {
  295.     if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
  296.         return (-1);
  297.     return (TIFFReadEncodedTile(tif,
  298.         TIFFComputeTile(tif, x, y, z, s), buf, (tsize_t) -1));
  299. }
  300.  
  301. /*
  302.  * Read a tile of data and decompress the specified
  303.  * amount into the user-supplied buffer.
  304.  */
  305. tsize_t
  306. TIFFReadEncodedTile(TIFF* tif, ttile_t tile, tdata_t buf, tsize_t size)
  307. {
  308.     TIFFDirectory *td = &tif->tif_dir;
  309.     tsize_t tilesize = tif->tif_tilesize;
  310.  
  311.     if (!TIFFCheckRead(tif, 1))
  312.         return (-1);
  313.     if (tile >= td->td_nstrips) {
  314.         TIFFError(tif->tif_name, "%ld: Tile out of range, max %ld",
  315.             (long) tile, (u_long) td->td_nstrips);
  316.         return (-1);
  317.     }
  318.     if (size == (tsize_t) -1)
  319.         size = tilesize;
  320.     else if (size > tilesize)
  321.         size = tilesize;
  322.     if (TIFFFillTile(tif, tile) && 
  323.         (*tif->tif_decodetile)(tif, buf, size, tile/td->td_stripsperimage)) {
  324.         (*tif->tif_postdecode)(tif, buf, size);
  325.         return (size);
  326.     } else
  327.         return (-1);
  328. }
  329.  
  330. static tsize_t
  331. TIFFReadRawTile1(TIFF* tif,
  332.     ttile_t tile, tdata_t buf, tsize_t size, const char* module)
  333. {
  334.     TIFFDirectory *td = &tif->tif_dir;
  335.  
  336.     if (!isMapped(tif)) {
  337.         if (!SeekOK(tif, td->td_stripoffset[tile])) {
  338.             TIFFError(module,
  339.                 "%s: Seek error at row %ld, col %ld, tile %ld",
  340.                 tif->tif_name,
  341.                 (long) tif->tif_row,
  342.                 (long) tif->tif_col,
  343.                 (long) tile);
  344.             return ((tsize_t) -1);
  345.         }
  346.         if (!ReadOK(tif, buf, size)) {
  347.             TIFFError(module, "%s: Read error at row %ld, col %ld",
  348.                 tif->tif_name,
  349.                 (long) tif->tif_row,
  350.                 (long) tif->tif_col);
  351.             return ((tsize_t) -1);
  352.         }
  353.     } else {
  354.         if (td->td_stripoffset[tile] + size > tif->tif_size) {
  355.             TIFFError(module,
  356.                 "%s: Seek error at row %ld, col %ld, tile %ld",
  357.                 tif->tif_name,
  358.                 (long) tif->tif_row,
  359.                 (long) tif->tif_col,
  360.                 (long) tile);
  361.             return ((tsize_t) -1);
  362.         }
  363.         memcpy(buf, tif->tif_base + td->td_stripoffset[tile], size);
  364.     }
  365.     return (size);
  366. }
  367.  
  368. /*
  369.  * Read a tile of data from the file.
  370.  */
  371. tsize_t
  372. TIFFReadRawTile(TIFF* tif, ttile_t tile, tdata_t buf, tsize_t size)
  373. {
  374.     static const char module[] = "TIFFReadRawTile";
  375.     TIFFDirectory *td = &tif->tif_dir;
  376.     tsize_t bytecount;
  377.  
  378.     if (!TIFFCheckRead(tif, 1))
  379.         return ((tsize_t) -1);
  380.     if (tile >= td->td_nstrips) {
  381.         TIFFError(tif->tif_name, "%lu: Tile out of range, max %lu",
  382.             (u_long) tile, (u_long) td->td_nstrips);
  383.         return ((tsize_t) -1);
  384.     }
  385.     bytecount = td->td_stripbytecount[tile];
  386.     if (size != (tsize_t) -1 && size < bytecount)
  387.         bytecount = size;
  388.     return (TIFFReadRawTile1(tif, tile, buf, bytecount, module));
  389. }
  390.  
  391. /*
  392.  * Read the specified tile and setup for decoding. 
  393.  * The data buffer is expanded, as necessary, to
  394.  * hold the tile's data.
  395.  */
  396. static int
  397. TIFFFillTile(TIFF* tif, ttile_t tile)
  398. {
  399.     static const char module[] = "TIFFFillTile";
  400.     TIFFDirectory *td = &tif->tif_dir;
  401.     tsize_t bytecount;
  402.  
  403.     bytecount = td->td_stripbytecount[tile];
  404.     if (isMapped(tif) &&
  405.         (td->td_fillorder == tif->tif_fillorder || (tif->tif_flags & TIFF_NOBITREV))) {
  406.         /*
  407.          * The image is mapped into memory and we either don't
  408.          * need to flip bits or the compression routine is going
  409.          * to handle this operation itself.  In this case, avoid
  410.          * copying the raw data and instead just reference the
  411.          * data from the memory mapped file image.  This assumes
  412.          * that the decompression routines do not modify the
  413.          * contents of the raw data buffer (if they try to,
  414.          * the application will get a fault since the file is
  415.          * mapped read-only).
  416.          */
  417.         if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
  418.             _TIFFfree(tif->tif_rawdata);
  419.         tif->tif_flags &= ~TIFF_MYBUFFER;
  420.         if (td->td_stripoffset[tile] + bytecount > tif->tif_size) {
  421.             tif->tif_curtile = -1;        /* unknown state */
  422.             return (0);
  423.         }
  424.         tif->tif_rawdatasize = bytecount;
  425.         tif->tif_rawdata = tif->tif_base + td->td_stripoffset[tile];
  426.     } else {
  427.         /*
  428.          * Expand raw data buffer, if needed, to
  429.          * hold data tile coming from file
  430.          * (perhaps should set upper bound on
  431.          *  the size of a buffer we'll use?).
  432.          */
  433.         if (bytecount > tif->tif_rawdatasize) {
  434.             tif->tif_curtile = -1;        /* unknown state */
  435.             if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
  436.                 TIFFError(module,
  437.                 "%s: Data buffer too small to hold tile %ld",
  438.                     tif->tif_name, (long) tile);
  439.                 return (0);
  440.             }
  441.             if (!TIFFReadBufferSetup(tif, 0,
  442.                 roundup(bytecount, 1024)))
  443.                 return (0);
  444.         }
  445.         if (TIFFReadRawTile1(tif, tile, (u_char *)tif->tif_rawdata,
  446.             bytecount, module) != bytecount)
  447.             return (0);
  448.         if (td->td_fillorder != tif->tif_fillorder &&
  449.             (tif->tif_flags & TIFF_NOBITREV) == 0)
  450.             TIFFReverseBits(tif->tif_rawdata, bytecount);
  451.     }
  452.     return (TIFFStartTile(tif, tile));
  453. }
  454.  
  455. /*
  456.  * Setup the raw data buffer in preparation for
  457.  * reading a strip of raw data.  If the buffer
  458.  * is specified as zero, then a buffer of appropriate
  459.  * size is allocated by the library.  Otherwise,
  460.  * the client must guarantee that the buffer is
  461.  * large enough to hold any individual strip of
  462.  * raw data.
  463.  */
  464. int
  465. TIFFReadBufferSetup(TIFF* tif, tdata_t bp, tsize_t size)
  466. {
  467.     static const char module[] = "TIFFReadBufferSetup";
  468.  
  469.     if (tif->tif_rawdata) {
  470.         if (tif->tif_flags & TIFF_MYBUFFER)
  471.             _TIFFfree(tif->tif_rawdata);
  472.         tif->tif_rawdata = NULL;
  473.     }
  474.     if (bp) {
  475.         tif->tif_rawdatasize = size;
  476.         tif->tif_rawdata = (tidata_t) bp;
  477.         tif->tif_flags &= ~TIFF_MYBUFFER;
  478.     } else {
  479.         tif->tif_rawdatasize = roundup(size, 1024);
  480.         tif->tif_rawdata = (tdata_t) _TIFFmalloc(tif->tif_rawdatasize);
  481.         tif->tif_flags |= TIFF_MYBUFFER;
  482.     }
  483.     if (tif->tif_rawdata == NULL) {
  484.         TIFFError(module,
  485.             "%s: No space for data buffer at scanline %ld",
  486.             tif->tif_name, (long) tif->tif_row);
  487.         tif->tif_rawdatasize = 0;
  488.         return (0);
  489.     }
  490.     return (1);
  491. }
  492.  
  493. /*
  494.  * Set state to appear as if a
  495.  * strip has just been read in.
  496.  */
  497. static int
  498. TIFFStartStrip(TIFF* tif, tstrip_t strip)
  499. {
  500.     TIFFDirectory *td = &tif->tif_dir;
  501.  
  502.     tif->tif_curstrip = strip;
  503.     tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
  504.     tif->tif_rawcp = tif->tif_rawdata;
  505.     tif->tif_rawcc = td->td_stripbytecount[strip];
  506.     return (tif->tif_predecode == NULL || (*tif->tif_predecode)(tif));
  507. }
  508.  
  509. /*
  510.  * Set state to appear as if a
  511.  * tile has just been read in.
  512.  */
  513. static int
  514. TIFFStartTile(TIFF* tif, ttile_t tile)
  515. {
  516.     TIFFDirectory *td = &tif->tif_dir;
  517.  
  518.     tif->tif_curtile = tile;
  519.     tif->tif_row =
  520.         (tile % howmany(td->td_imagewidth, td->td_tilewidth)) *
  521.         td->td_tilelength;
  522.     tif->tif_col =
  523.         (tile % howmany(td->td_imagelength, td->td_tilelength)) *
  524.         td->td_tilewidth;
  525.     tif->tif_rawcp = tif->tif_rawdata;
  526.     tif->tif_rawcc = td->td_stripbytecount[tile];
  527.     return (tif->tif_predecode == NULL || (*tif->tif_predecode)(tif));
  528. }
  529.  
  530. static int
  531. TIFFCheckRead(TIFF* tif, int tiles)
  532. {
  533.     if (tif->tif_mode == O_WRONLY) {
  534.         TIFFError(tif->tif_name, "File not open for reading");
  535.         return (0);
  536.     }
  537.     if (tiles ^ isTiled(tif)) {
  538.         TIFFError(tif->tif_name, tiles ?
  539.             "Can not read tiles from a stripped image" :
  540.             "Can not read scanlines from a tiled image");
  541.         return (0);
  542.     }
  543.     return (1);
  544. }
  545.  
  546. void
  547. TIFFNoPostDecode(TIFF* tif, tidata_t buf, tsize_t cc)
  548. {
  549. }
  550.  
  551. void
  552. TIFFSwab16BitData(TIFF* tif, tidata_t buf, tsize_t cc)
  553. {
  554.     assert((cc & 1) == 0);
  555.     TIFFSwabArrayOfShort((uint16*) buf, cc/2);
  556. }
  557.  
  558. void
  559. TIFFSwab32BitData(TIFF* tif, tidata_t buf, tsize_t cc)
  560. {
  561.     assert((cc & 3) == 0);
  562.     TIFFSwabArrayOfLong((uint32*) buf, cc/4);
  563. }
  564.