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_strip.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-04  |  5.5 KB  |  194 lines  |  [TEXT/CWIE]

  1. /*
  2.  * Copyright (c) 1991-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.  * Strip-organized Image Support Routines.
  29.  */
  30.  
  31. /* $Id: tif_strip.c,v 1.3 2001/03/21 10:41:18 rjs Exp $ */
  32.  
  33. #include "tiffiop.h"
  34.  
  35. /*
  36.  * Compute which strip a (row,sample) value is in.
  37.  */
  38. tstrip_t
  39. TIFFComputeStrip(TIFF* tif, uint32 row, tsample_t sample)
  40. {
  41.     TIFFDirectory *td = &tif->tif_dir;
  42.     tstrip_t strip;
  43.  
  44.     strip = row / td->td_rowsperstrip;
  45.     if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
  46.         if (sample >= td->td_samplesperpixel) {
  47.             TIFFError(tif->tif_name,
  48.                 "%u: Sample out of range, max %u",
  49.                 sample, td->td_samplesperpixel);
  50.             return ((tstrip_t) 0);
  51.         }
  52.         strip += sample*td->td_stripsperimage;
  53.     }
  54.     return (strip);
  55. }
  56.  
  57. /*
  58.  * Compute how many strips are in an image.
  59.  */
  60. tstrip_t
  61. TIFFNumberOfStrips(TIFF* tif)
  62. {
  63.     TIFFDirectory *td = &tif->tif_dir;
  64.     tstrip_t nstrips;
  65.  
  66.     nstrips = (td->td_rowsperstrip == (uint32) -1 ?
  67.          (td->td_imagelength != 0 ? 1 : 0) :
  68.          TIFFhowmany(td->td_imagelength, td->td_rowsperstrip));
  69.     if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
  70.         nstrips *= td->td_samplesperpixel;
  71.     return (nstrips);
  72. }
  73.  
  74. /*
  75.  * Compute the # bytes in a variable height, row-aligned strip.
  76.  */
  77. tsize_t
  78. TIFFVStripSize(TIFF* tif, uint32 nrows)
  79. {
  80.     TIFFDirectory *td = &tif->tif_dir;
  81.  
  82.     if (nrows == (uint32) -1)
  83.         nrows = td->td_imagelength;
  84. #ifdef YCBCR_SUPPORT
  85.     if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
  86.         td->td_photometric == PHOTOMETRIC_YCBCR &&
  87.         !isUpSampled(tif)) {
  88.         /*
  89.          * Packed YCbCr data contain one Cb+Cr for every
  90.          * HorizontalSampling*VerticalSampling Y values.
  91.          * Must also roundup width and height when calculating
  92.          * since images that are not a multiple of the
  93.          * horizontal/vertical subsampling area include
  94.          * YCbCr data for the extended image.
  95.          */
  96.         tsize_t w =
  97.             TIFFroundup(td->td_imagewidth, td->td_ycbcrsubsampling[0]);
  98.         tsize_t scanline = TIFFhowmany(w*td->td_bitspersample, 8);
  99.         tsize_t samplingarea =
  100.             td->td_ycbcrsubsampling[0]*td->td_ycbcrsubsampling[1];
  101.         nrows = TIFFroundup(nrows, td->td_ycbcrsubsampling[1]);
  102.         /* NB: don't need TIFFhowmany here 'cuz everything is rounded */
  103.         return ((tsize_t)
  104.             (nrows*scanline + 2*(nrows*scanline / samplingarea)));
  105.     } else
  106. #endif
  107.         return ((tsize_t)(nrows * TIFFScanlineSize(tif)));
  108. }
  109.  
  110. /*
  111.  * Compute the # bytes in a (row-aligned) strip.
  112.  *
  113.  * Note that if RowsPerStrip is larger than the
  114.  * recorded ImageLength, then the strip size is
  115.  * truncated to reflect the actual space required
  116.  * to hold the strip.
  117.  */
  118. tsize_t
  119. TIFFStripSize(TIFF* tif)
  120. {
  121.     TIFFDirectory* td = &tif->tif_dir;
  122.     uint32 rps = td->td_rowsperstrip;
  123.     if (rps > td->td_imagelength)
  124.         rps = td->td_imagelength;
  125.     return (TIFFVStripSize(tif, rps));
  126. }
  127.  
  128. /*
  129.  * Compute a default strip size based on the image
  130.  * characteristics and a requested value.  If the
  131.  * request is <1 then we choose a strip size according
  132.  * to certain heuristics.
  133.  */
  134. uint32
  135. TIFFDefaultStripSize(TIFF* tif, uint32 request)
  136. {
  137.     return (*tif->tif_defstripsize)(tif, request);
  138. }
  139.  
  140. uint32
  141. _TIFFDefaultStripSize(TIFF* tif, uint32 s)
  142. {
  143.     if ((int32) s < 1) {
  144.         /*
  145.          * If RowsPerStrip is unspecified, try to break the
  146.          * image up into strips that are approximately 8Kbytes.
  147.          */
  148.         tsize_t scanline = TIFFScanlineSize(tif);
  149.         s = (uint32)(8*1024) / (scanline == 0 ? 1 : scanline);
  150.         if (s == 0)        /* very wide images */
  151.             s = 1;
  152.     }
  153.     return (s);
  154. }
  155.  
  156. /*
  157.  * Return the number of bytes to read/write in a call to
  158.  * one of the scanline-oriented i/o routines.  Note that
  159.  * this number may be 1/samples-per-pixel if data is
  160.  * stored as separate planes.
  161.  */
  162. tsize_t
  163. TIFFScanlineSize(TIFF* tif)
  164. {
  165.     TIFFDirectory *td = &tif->tif_dir;
  166.     tsize_t scanline;
  167.     
  168.     scanline = td->td_bitspersample * td->td_imagewidth;
  169.     if (td->td_planarconfig == PLANARCONFIG_CONTIG)
  170.         scanline *= td->td_samplesperpixel;
  171.     return ((tsize_t) TIFFhowmany(scanline, 8));
  172. }
  173.  
  174. /*
  175.  * Return the number of bytes required to store a complete
  176.  * decoded and packed raster scanline (as opposed to the
  177.  * I/O size returned by TIFFScanlineSize which may be less
  178.  * if data is store as separate planes).
  179.  */
  180. tsize_t
  181. TIFFRasterScanlineSize(TIFF* tif)
  182. {
  183.     TIFFDirectory *td = &tif->tif_dir;
  184.     tsize_t scanline;
  185.     
  186.     scanline = td->td_bitspersample * td->td_imagewidth;
  187.     if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
  188.         scanline *= td->td_samplesperpixel;
  189.         return ((tsize_t) TIFFhowmany(scanline, 8));
  190.     } else
  191.         return ((tsize_t)
  192.             TIFFhowmany(scanline, 8)*td->td_samplesperpixel);
  193. }
  194.