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_tile.c < prev    next >
Text File  |  1996-11-18  |  6KB  |  193 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_tile.c,v 1.15 93/08/26 14:27:29 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 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.  *
  32.  * Tiled Image Support Routines.
  33.  */
  34. #include "tiffiop.h"
  35.  
  36. /*
  37.  * Compute which tile an (x,y,z,s) value is in.
  38.  */
  39. ttile_t
  40. TIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, tsample_t s)
  41. {
  42.     TIFFDirectory *td = &tif->tif_dir;
  43.     u_long dx = td->td_tilewidth;
  44.     u_long dy = td->td_tilelength;
  45.     u_long dz = td->td_tiledepth;
  46.     ttile_t tile = 1;
  47.  
  48.     if (td->td_imagedepth == 1)
  49.         z = 0;
  50.     if (dx == (u_long) -1)
  51.         dx = td->td_imagewidth;
  52.     if (dy == (u_long) -1)
  53.         dy = td->td_imagelength;
  54.     if (dz == (u_long) -1)
  55.         dz = td->td_imagedepth;
  56.     if (dx != 0 && dy != 0 && dz != 0) {
  57.         u_long xpt = howmany(td->td_imagewidth, dx); 
  58.         u_long ypt = howmany(td->td_imagelength, dy); 
  59.         u_long zpt = howmany(td->td_imagedepth, dz); 
  60.  
  61.         if (td->td_planarconfig == PLANARCONFIG_SEPARATE) 
  62.             tile = (xpt*ypt*zpt)*s +
  63.                  (xpt*ypt)*(z/dz) +
  64.                  xpt*(y/dy) +
  65.                  x/dx;
  66.         else
  67.             tile = (xpt*ypt)*(z/dz) + xpt*(y/dy) + x/dx + s;
  68.     }
  69.     return (tile);
  70. }
  71.  
  72. /*
  73.  * Check an (x,y,z,s) coordinate
  74.  * against the image bounds.
  75.  */
  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.     u_long dx = td->td_tilewidth;
  112.     u_long dy = td->td_tilelength;
  113.     u_long dz = td->td_tiledepth;
  114.     ttile_t ntiles;
  115.  
  116.     if (dx == (u_long) -1)
  117.         dx = td->td_imagewidth;
  118.     if (dy == (u_long) -1)
  119.         dy = td->td_imagelength;
  120.     if (dz == (u_long) -1)
  121.         dz = td->td_imagedepth;
  122.     ntiles = (dx != 0 && dy != 0 && dz != 0) ?
  123.         (howmany(td->td_imagewidth, dx) * howmany(td->td_imagelength, dy) *
  124.         howmany(td->td_imagedepth, dz)) :
  125.         0;
  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 (howmany(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.         /*
  164.          * Packed YCbCr data contain one Cb+Cr for every
  165.          * HorizontalSampling*VerticalSampling Y values.
  166.          * Must also roundup width and height when calculating
  167.          * since images that are not a multiple of the
  168.          * horizontal/vertical subsampling area include
  169.          * YCbCr data for the extended image.
  170.          */
  171.         tsize_t w =
  172.             roundup(td->td_tilewidth, td->td_ycbcrsubsampling[0]);
  173.         tsize_t rowsize = howmany(w*td->td_bitspersample, 8);
  174.         tsize_t samplingarea =
  175.             td->td_ycbcrsubsampling[0]*td->td_ycbcrsubsampling[1];
  176.         nrows = roundup(nrows, td->td_ycbcrsubsampling[1]);
  177.         /* NB: don't need howmany here 'cuz everything is rounded */
  178.         tilesize = nrows*rowsize + 2*(nrows*rowsize / samplingarea);
  179.     } else
  180. #endif
  181.         tilesize = nrows * TIFFTileRowSize(tif);
  182.     return (tilesize * td->td_tiledepth);
  183. }
  184.  
  185. /*
  186.  * Compute the # bytes in a row-aligned tile.
  187.  */
  188. tsize_t
  189. TIFFTileSize(TIFF* tif)
  190. {
  191.     return (TIFFVTileSize(tif, tif->tif_dir.td_tilelength));
  192. }
  193.