tif_strip.c
Upload User: wgos123
Upload Date: 2013-09-16
Package Size: 16709k
Code Size: 6k
Category:

GDI-Bitmap

Development Platform:

Visual C++

  1. /* $Header: /cvsroot/osrs/libtiff/libtiff/tif_strip.c,v 1.1.1.1 1999/07/27 21:50:27 mike Exp $ */
  2. /*
  3.  * Copyright (c) 1991-1997 Sam Leffler
  4.  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and 
  7.  * its documentation for any purpose is hereby granted without fee, provided
  8.  * that (i) the above copyright notices and this permission notice appear in
  9.  * all copies of the software and related documentation, and (ii) the names of
  10.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11.  * publicity relating to the software without the specific, prior written
  12.  * permission of Sam Leffler and Silicon Graphics.
  13.  * 
  14.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  15.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  16.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  17.  * 
  18.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  22.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  23.  * OF THIS SOFTWARE.
  24.  */
  25. /*
  26.  * TIFF Library.
  27.  *
  28.  * Strip-organized Image Support Routines.
  29.  */
  30. #include "tiffiop.h"
  31. /*
  32.  * Compute which strip a (row,sample) value is in.
  33.  */
  34. tstrip_t
  35. TIFFComputeStrip(TIFF* tif, uint32 row, tsample_t sample)
  36. {
  37. TIFFDirectory *td = &tif->tif_dir;
  38. tstrip_t strip;
  39. strip = row / td->td_rowsperstrip;
  40. if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
  41. if (sample >= td->td_samplesperpixel) {
  42. TIFFError(tif->tif_name,
  43.     "%u: Sample out of range, max %u",
  44.     sample, td->td_samplesperpixel);
  45. return ((tstrip_t) 0);
  46. }
  47. strip += sample*td->td_stripsperimage;
  48. }
  49. return (strip);
  50. }
  51. /*
  52.  * Compute how many strips are in an image.
  53.  */
  54. tstrip_t
  55. TIFFNumberOfStrips(TIFF* tif)
  56. {
  57. TIFFDirectory *td = &tif->tif_dir;
  58. tstrip_t nstrips;
  59. nstrips = (td->td_rowsperstrip == (uint32) -1 ?
  60.      (td->td_imagelength != 0 ? 1 : 0) :
  61.      TIFFhowmany(td->td_imagelength, td->td_rowsperstrip));
  62. if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
  63. nstrips *= td->td_samplesperpixel;
  64. return (nstrips);
  65. }
  66. /*
  67.  * Compute the # bytes in a variable height, row-aligned strip.
  68.  */
  69. tsize_t
  70. TIFFVStripSize(TIFF* tif, uint32 nrows)
  71. {
  72. TIFFDirectory *td = &tif->tif_dir;
  73. if (nrows == (uint32) -1)
  74. nrows = td->td_imagelength;
  75. #ifdef YCBCR_SUPPORT
  76. if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
  77.     td->td_photometric == PHOTOMETRIC_YCBCR &&
  78.     !isUpSampled(tif)) {
  79. /*
  80.  * Packed YCbCr data contain one Cb+Cr for every
  81.  * HorizontalSampling*VerticalSampling Y values.
  82.  * Must also roundup width and height when calculating
  83.  * since images that are not a multiple of the
  84.  * horizontal/vertical subsampling area include
  85.  * YCbCr data for the extended image.
  86.  */
  87. tsize_t w =
  88.     TIFFroundup(td->td_imagewidth, td->td_ycbcrsubsampling[0]);
  89. tsize_t scanline = TIFFhowmany(w*td->td_bitspersample, 8);
  90. tsize_t samplingarea =
  91.     td->td_ycbcrsubsampling[0]*td->td_ycbcrsubsampling[1];
  92. nrows = TIFFroundup(nrows, td->td_ycbcrsubsampling[1]);
  93. /* NB: don't need TIFFhowmany here 'cuz everything is rounded */
  94. return ((tsize_t)
  95.     (nrows*scanline + 2*(nrows*scanline / samplingarea)));
  96. } else
  97. #endif
  98. return ((tsize_t)(nrows * TIFFScanlineSize(tif)));
  99. }
  100. /*
  101.  * Compute the # bytes in a (row-aligned) strip.
  102.  *
  103.  * Note that if RowsPerStrip is larger than the
  104.  * recorded ImageLength, then the strip size is
  105.  * truncated to reflect the actual space required
  106.  * to hold the strip.
  107.  */
  108. tsize_t
  109. TIFFStripSize(TIFF* tif)
  110. {
  111. TIFFDirectory* td = &tif->tif_dir;
  112. uint32 rps = td->td_rowsperstrip;
  113. if (rps > td->td_imagelength)
  114. rps = td->td_imagelength;
  115. return (TIFFVStripSize(tif, rps));
  116. }
  117. /*
  118.  * Compute a default strip size based on the image
  119.  * characteristics and a requested value.  If the
  120.  * request is <1 then we choose a strip size according
  121.  * to certain heuristics.
  122.  */
  123. uint32
  124. TIFFDefaultStripSize(TIFF* tif, uint32 request)
  125. {
  126. return (*tif->tif_defstripsize)(tif, request);
  127. }
  128. uint32
  129. _TIFFDefaultStripSize(TIFF* tif, uint32 s)
  130. {
  131. if ((int32) s < 1) {
  132. /*
  133.  * If RowsPerStrip is unspecified, try to break the
  134.  * image up into strips that are approximately 8Kbytes.
  135.  */
  136. tsize_t scanline = TIFFScanlineSize(tif);
  137. s = (uint32)(8*1024) / (scanline == 0 ? 1 : scanline);
  138. if (s == 0) /* very wide images */
  139. s = 1;
  140. }
  141. return (s);
  142. }
  143. /*
  144.  * Return the number of bytes to read/write in a call to
  145.  * one of the scanline-oriented i/o routines.  Note that
  146.  * this number may be 1/samples-per-pixel if data is
  147.  * stored as separate planes.
  148.  */
  149. tsize_t
  150. TIFFScanlineSize(TIFF* tif)
  151. {
  152. TIFFDirectory *td = &tif->tif_dir;
  153. tsize_t scanline;
  154. scanline = td->td_bitspersample * td->td_imagewidth;
  155. if (td->td_planarconfig == PLANARCONFIG_CONTIG)
  156. scanline *= td->td_samplesperpixel;
  157. return ((tsize_t) TIFFhowmany(scanline, 8));
  158. }
  159. /*
  160.  * Return the number of bytes required to store a complete
  161.  * decoded and packed raster scanline (as opposed to the
  162.  * I/O size returned by TIFFScanlineSize which may be less
  163.  * if data is store as separate planes).
  164.  */
  165. tsize_t
  166. TIFFRasterScanlineSize(TIFF* tif)
  167. {
  168. TIFFDirectory *td = &tif->tif_dir;
  169. tsize_t scanline;
  170. scanline = td->td_bitspersample * td->td_imagewidth;
  171. if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
  172. scanline *= td->td_samplesperpixel;
  173. return ((tsize_t) TIFFhowmany(scanline, 8));
  174. } else
  175. return ((tsize_t)
  176.     TIFFhowmany(scanline, 8)*td->td_samplesperpixel);
  177. }