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_tile.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-04  |  6.0 KB  |  221 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.  * Tiled Image Support Routines.
  29.  */
  30.  
  31. /* $Id: tif_tile.c,v 1.3 2001/03/21 10:41:18 rjs Exp $ */
  32.  
  33. #include "tiffiop.h"
  34.  
  35. /*
  36.  * Compute which tile an (x,y,z,s) value is in.
  37.  */
  38. ttile_t
  39. TIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, tsample_t s)
  40. {
  41.     TIFFDirectory *td = &tif->tif_dir;
  42.     uint32 dx = td->td_tilewidth;
  43.     uint32 dy = td->td_tilelength;
  44.     uint32 dz = td->td_tiledepth;
  45.     ttile_t tile = 1;
  46.  
  47.     if (td->td_imagedepth == 1)
  48.         z = 0;
  49.     if (dx == (uint32) -1)
  50.         dx = td->td_imagewidth;
  51.     if (dy == (uint32) -1)
  52.         dy = td->td_imagelength;
  53.     if (dz == (uint32) -1)
  54.         dz = td->td_imagedepth;
  55.     if (dx != 0 && dy != 0 && dz != 0) {
  56.         uint32 xpt = TIFFhowmany(td->td_imagewidth, dx); 
  57.         uint32 ypt = TIFFhowmany(td->td_imagelength, dy); 
  58.         uint32 zpt = TIFFhowmany(td->td_imagedepth, dz); 
  59.  
  60.         if (td->td_planarconfig == PLANARCONFIG_SEPARATE) 
  61.             tile = (xpt*ypt*zpt)*s +
  62.                  (xpt*ypt)*(z/dz) +
  63.                  xpt*(y/dy) +
  64.                  x/dx;
  65.         else
  66.             tile = (xpt*ypt)*(z/dz) + xpt*(y/dy) + x/dx + s;
  67.     }
  68.     return (tile);
  69. }
  70.  
  71. /*
  72.  * Check an (x,y,z,s) coordinate
  73.  * against the image bounds.
  74.  */
  75. int
  76. TIFFCheckTile(TIFF* tif, uint32 x, uint32 y, uint32 z, tsample_t s)
  77. {
  78.     TIFFDirectory *td = &tif->tif_dir;
  79.  
  80.     if (x >= td->td_imagewidth) {
  81.         TIFFError(tif->tif_name, "Col %ld out of range, max %lu",
  82.             (long) x, (u_long) td->td_imagewidth);
  83.         return (0);
  84.     }
  85.     if (y >= td->td_imagelength) {
  86.         TIFFError(tif->tif_name, "Row %ld out of range, max %lu",
  87.             (long) y, (u_long) td->td_imagelength);
  88.         return (0);
  89.     }
  90.     if (z >= td->td_imagedepth) {
  91.         TIFFError(tif->tif_name, "Depth %ld out of range, max %lu",
  92.             (long) z, (u_long) td->td_imagedepth);
  93.         return (0);
  94.     }
  95.     if (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
  96.         s >= td->td_samplesperpixel) {
  97.         TIFFError(tif->tif_name, "Sample %d out of range, max %u",
  98.             (int) s, td->td_samplesperpixel);
  99.         return (0);
  100.     }
  101.     return (1);
  102. }
  103.  
  104. /*
  105.  * Compute how many tiles are in an image.
  106.  */
  107. ttile_t
  108. TIFFNumberOfTiles(TIFF* tif)
  109. {
  110.     TIFFDirectory *td = &tif->tif_dir;
  111.     uint32 dx = td->td_tilewidth;
  112.     uint32 dy = td->td_tilelength;
  113.     uint32 dz = td->td_tiledepth;
  114.     ttile_t ntiles;
  115.  
  116.     if (dx == (uint32) -1)
  117.         dx = td->td_imagewidth;
  118.     if (dy == (uint32) -1)
  119.         dy = td->td_imagelength;
  120.     if (dz == (uint32) -1)
  121.         dz = td->td_imagedepth;
  122.     ntiles = (dx == 0 || dy == 0 || dz == 0) ? 0 :
  123.         (TIFFhowmany(td->td_imagewidth, dx) *
  124.          TIFFhowmany(td->td_imagelength, dy) *
  125.          TIFFhowmany(td->td_imagedepth, dz));
  126.     if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
  127.         ntiles *= td->td_samplesperpixel;
  128.     return (ntiles);
  129. }
  130.  
  131. /*
  132.  * Compute the # bytes in each row of a tile.
  133.  */
  134. tsize_t
  135. TIFFTileRowSize(TIFF* tif)
  136. {
  137.     TIFFDirectory *td = &tif->tif_dir;
  138.     tsize_t rowsize;
  139.     
  140.     if (td->td_tilelength == 0 || td->td_tilewidth == 0)
  141.         return ((tsize_t) 0);
  142.     rowsize = td->td_bitspersample * td->td_tilewidth;
  143.     if (td->td_planarconfig == PLANARCONFIG_CONTIG)
  144.         rowsize *= td->td_samplesperpixel;
  145.     return ((tsize_t) TIFFhowmany(rowsize, 8));
  146. }
  147.  
  148. /*
  149.  * Compute the # bytes in a variable length, row-aligned tile.
  150.  */
  151. tsize_t
  152. TIFFVTileSize(TIFF* tif, uint32 nrows)
  153. {
  154.     TIFFDirectory *td = &tif->tif_dir;
  155.     tsize_t tilesize;
  156.  
  157.     if (td->td_tilelength == 0 || td->td_tilewidth == 0 ||
  158.         td->td_tiledepth == 0)
  159.         return ((tsize_t) 0);
  160. #ifdef YCBCR_SUPPORT
  161.     if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
  162.         td->td_photometric == PHOTOMETRIC_YCBCR &&
  163.         !isUpSampled(tif)) {
  164.         /*
  165.          * Packed YCbCr data contain one Cb+Cr for every
  166.          * HorizontalSampling*VerticalSampling Y values.
  167.          * Must also roundup width and height when calculating
  168.          * since images that are not a multiple of the
  169.          * horizontal/vertical subsampling area include
  170.          * YCbCr data for the extended image.
  171.          */
  172.         tsize_t w =
  173.             TIFFroundup(td->td_tilewidth, td->td_ycbcrsubsampling[0]);
  174.         tsize_t rowsize = TIFFhowmany(w*td->td_bitspersample, 8);
  175.         tsize_t samplingarea =
  176.             td->td_ycbcrsubsampling[0]*td->td_ycbcrsubsampling[1];
  177.         nrows = TIFFroundup(nrows, td->td_ycbcrsubsampling[1]);
  178.         /* NB: don't need TIFFhowmany here 'cuz everything is rounded */
  179.         tilesize = nrows*rowsize + 2*(nrows*rowsize / samplingarea);
  180.     } else
  181. #endif
  182.         tilesize = nrows * TIFFTileRowSize(tif);
  183.     return ((tsize_t)(tilesize * td->td_tiledepth));
  184. }
  185.  
  186. /*
  187.  * Compute the # bytes in a row-aligned tile.
  188.  */
  189. tsize_t
  190. TIFFTileSize(TIFF* tif)
  191. {
  192.     return (TIFFVTileSize(tif, tif->tif_dir.td_tilelength));
  193. }
  194.  
  195. /*
  196.  * Compute a default tile size based on the image
  197.  * characteristics and a requested value.  If a
  198.  * request is <1 then we choose a size according
  199.  * to certain heuristics.
  200.  */
  201. void
  202. TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
  203. {
  204.     (*tif->tif_deftilesize)(tif, tw, th);
  205. }
  206.  
  207. void
  208. _TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
  209. {
  210.     (void) tif;
  211.     if (*(int32*) tw < 1)
  212.         *tw = 256;
  213.     if (*(int32*) th < 1)
  214.         *th = 256;
  215.     /* roundup to a multiple of 16 per the spec */
  216.     if (*tw & 0xf)
  217.         *tw = TIFFroundup(*tw, 16);
  218.     if (*th & 0xf)
  219.         *th = TIFFroundup(*th, 16);
  220. }
  221.