LCOV - code coverage report
Current view: directory - frmts/gtiff/libtiff - tif_tile.c (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 129 78 60.5 %
Date: 2011-12-18 Functions: 11 8 72.7 %

       1                 : /* $Id: tif_tile.c,v 1.22 2010-07-01 15:33:28 dron Exp $ */
       2                 : 
       3                 : /*
       4                 :  * Copyright (c) 1991-1997 Sam Leffler
       5                 :  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
       6                 :  *
       7                 :  * Permission to use, copy, modify, distribute, and sell this software and 
       8                 :  * its documentation for any purpose is hereby granted without fee, provided
       9                 :  * that (i) the above copyright notices and this permission notice appear in
      10                 :  * all copies of the software and related documentation, and (ii) the names of
      11                 :  * Sam Leffler and Silicon Graphics may not be used in any advertising or
      12                 :  * publicity relating to the software without the specific, prior written
      13                 :  * permission of Sam Leffler and Silicon Graphics.
      14                 :  * 
      15                 :  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
      16                 :  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
      17                 :  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
      18                 :  * 
      19                 :  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
      20                 :  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
      21                 :  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
      22                 :  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
      23                 :  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
      24                 :  * OF THIS SOFTWARE.
      25                 :  */
      26                 : 
      27                 : /*
      28                 :  * TIFF Library.
      29                 :  *
      30                 :  * Tiled Image Support Routines.
      31                 :  */
      32                 : #include "tiffiop.h"
      33                 : 
      34                 : /*
      35                 :  * Compute which tile an (x,y,z,s) value is in.
      36                 :  */
      37                 : uint32
      38               1 : TIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)
      39                 : {
      40               1 :   TIFFDirectory *td = &tif->tif_dir;
      41               1 :   uint32 dx = td->td_tilewidth;
      42               1 :   uint32 dy = td->td_tilelength;
      43               1 :   uint32 dz = td->td_tiledepth;
      44               1 :   uint32 tile = 1;
      45                 : 
      46               1 :   if (td->td_imagedepth == 1)
      47               1 :     z = 0;
      48               1 :   if (dx == (uint32) -1)
      49               0 :     dx = td->td_imagewidth;
      50               1 :   if (dy == (uint32) -1)
      51               0 :     dy = td->td_imagelength;
      52               1 :   if (dz == (uint32) -1)
      53               0 :     dz = td->td_imagedepth;
      54               1 :   if (dx != 0 && dy != 0 && dz != 0) {
      55               1 :     uint32 xpt = TIFFhowmany_32(td->td_imagewidth, dx);
      56               1 :     uint32 ypt = TIFFhowmany_32(td->td_imagelength, dy);
      57               1 :     uint32 zpt = TIFFhowmany_32(td->td_imagedepth, dz);
      58                 : 
      59               1 :     if (td->td_planarconfig == PLANARCONFIG_SEPARATE) 
      60               0 :       tile = (xpt*ypt*zpt)*s +
      61               0 :            (xpt*ypt)*(z/dz) +
      62               0 :            xpt*(y/dy) +
      63               0 :            x/dx;
      64                 :     else
      65               1 :       tile = (xpt*ypt)*(z/dz) + xpt*(y/dy) + x/dx;
      66                 :   }
      67               1 :   return (tile);
      68                 : }
      69                 : 
      70                 : /*
      71                 :  * Check an (x,y,z,s) coordinate
      72                 :  * against the image bounds.
      73                 :  */
      74                 : int
      75               1 : TIFFCheckTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)
      76                 : {
      77               1 :   TIFFDirectory *td = &tif->tif_dir;
      78                 : 
      79               1 :   if (x >= td->td_imagewidth) {
      80               0 :     TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
      81                 :            "%lu: Col out of range, max %lu",
      82                 :            (unsigned long) x,
      83               0 :            (unsigned long) (td->td_imagewidth - 1));
      84               0 :     return (0);
      85                 :   }
      86               1 :   if (y >= td->td_imagelength) {
      87               0 :     TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
      88                 :            "%lu: Row out of range, max %lu",
      89                 :            (unsigned long) y,
      90               0 :            (unsigned long) (td->td_imagelength - 1));
      91               0 :     return (0);
      92                 :   }
      93               1 :   if (z >= td->td_imagedepth) {
      94               0 :     TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
      95                 :            "%lu: Depth out of range, max %lu",
      96                 :            (unsigned long) z,
      97               0 :            (unsigned long) (td->td_imagedepth - 1));
      98               0 :     return (0);
      99                 :   }
     100               1 :   if (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
     101               0 :       s >= td->td_samplesperpixel) {
     102               0 :     TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
     103                 :            "%lu: Sample out of range, max %lu",
     104                 :            (unsigned long) s,
     105               0 :            (unsigned long) (td->td_samplesperpixel - 1));
     106               0 :     return (0);
     107                 :   }
     108               1 :   return (1);
     109                 : }
     110                 : 
     111                 : /*
     112                 :  * Compute how many tiles are in an image.
     113                 :  */
     114                 : uint32
     115            2286 : TIFFNumberOfTiles(TIFF* tif)
     116                 : {
     117            2286 :   TIFFDirectory *td = &tif->tif_dir;
     118            2286 :   uint32 dx = td->td_tilewidth;
     119            2286 :   uint32 dy = td->td_tilelength;
     120            2286 :   uint32 dz = td->td_tiledepth;
     121                 :   uint32 ntiles;
     122                 : 
     123            2286 :   if (dx == (uint32) -1)
     124               0 :     dx = td->td_imagewidth;
     125            2286 :   if (dy == (uint32) -1)
     126               0 :     dy = td->td_imagelength;
     127            2286 :   if (dz == (uint32) -1)
     128               0 :     dz = td->td_imagedepth;
     129            4572 :   ntiles = (dx == 0 || dy == 0 || dz == 0) ? 0 :
     130            9144 :       _TIFFMultiply32(tif, _TIFFMultiply32(tif, TIFFhowmany_32(td->td_imagewidth, dx),
     131            4572 :       TIFFhowmany_32(td->td_imagelength, dy),
     132                 :       "TIFFNumberOfTiles"),
     133            4572 :       TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles");
     134            2286 :   if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
     135             392 :     ntiles = _TIFFMultiply32(tif, ntiles, td->td_samplesperpixel,
     136                 :         "TIFFNumberOfTiles");
     137            2286 :   return (ntiles);
     138                 : }
     139                 : 
     140                 : /*
     141                 :  * Compute the # bytes in each row of a tile.
     142                 :  */
     143                 : uint64
     144           30382 : TIFFTileRowSize64(TIFF* tif)
     145                 : {
     146           30382 :   TIFFDirectory *td = &tif->tif_dir;
     147                 :   uint64 rowsize;
     148                 : 
     149           30382 :   if (td->td_tilelength == 0 || td->td_tilewidth == 0)
     150               0 :     return (0);
     151           30382 :   rowsize = _TIFFMultiply64(tif, td->td_bitspersample, td->td_tilewidth,
     152                 :       "TIFFTileRowSize");
     153           30382 :   if (td->td_planarconfig == PLANARCONFIG_CONTIG)
     154           28059 :     rowsize = _TIFFMultiply64(tif, rowsize, td->td_samplesperpixel,
     155                 :         "TIFFTileRowSize");
     156           30382 :   return (TIFFhowmany8_64(rowsize));
     157                 : }
     158                 : tmsize_t
     159             649 : TIFFTileRowSize(TIFF* tif)
     160                 : {
     161                 :   static const char module[] = "TIFFTileRowSize";
     162                 :   uint64 m;
     163                 :   tmsize_t n;
     164             649 :   m=TIFFTileRowSize64(tif);
     165             649 :   n=(tmsize_t)m;
     166             649 :   if ((uint64)n!=m)
     167                 :   {
     168               0 :     TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
     169               0 :     n=0;
     170                 :   }
     171             649 :   return(n);
     172                 : }
     173                 : 
     174                 : /*
     175                 :  * Compute the # bytes in a variable length, row-aligned tile.
     176                 :  */
     177                 : uint64
     178           30202 : TIFFVTileSize64(TIFF* tif, uint32 nrows)
     179                 : {
     180                 :   static const char module[] = "TIFFVTileSize64";
     181           30202 :   TIFFDirectory *td = &tif->tif_dir;
     182           60404 :   if (td->td_tilelength == 0 || td->td_tilewidth == 0 ||
     183           30202 :       td->td_tiledepth == 0)
     184               0 :     return (0);
     185           63593 :   if ((td->td_planarconfig==PLANARCONFIG_CONTIG)&&
     186           27919 :       (td->td_photometric==PHOTOMETRIC_YCBCR)&&
     187            2736 :       (td->td_samplesperpixel==3)&&
     188            2736 :       (!isUpSampled(tif)))
     189                 :   {
     190                 :     /*
     191                 :      * Packed YCbCr data contain one Cb+Cr for every
     192                 :      * HorizontalSampling*VerticalSampling Y values.
     193                 :      * Must also roundup width and height when calculating
     194                 :      * since images that are not a multiple of the
     195                 :      * horizontal/vertical subsampling area include
     196                 :      * YCbCr data for the extended image.
     197                 :      */
     198                 :     uint16 ycbcrsubsampling[2];
     199                 :     uint16 samplingblock_samples;
     200                 :     uint32 samplingblocks_hor;
     201                 :     uint32 samplingblocks_ver;
     202                 :     uint64 samplingrow_samples;
     203                 :     uint64 samplingrow_size;
     204             469 :     TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,ycbcrsubsampling+0,
     205                 :         ycbcrsubsampling+1);
     206             469 :     assert((ycbcrsubsampling[0]==1)||(ycbcrsubsampling[0]==2)||(ycbcrsubsampling[0]==4));
     207             469 :     assert((ycbcrsubsampling[1]==1)||(ycbcrsubsampling[1]==2)||(ycbcrsubsampling[1]==4));
     208             469 :     if (ycbcrsubsampling[0]*ycbcrsubsampling[1]==0)
     209                 :     {
     210               0 :       TIFFErrorExt(tif->tif_clientdata,module,
     211                 :           "Invalid YCbCr subsampling");
     212               0 :       return 0;
     213                 :     }
     214             469 :     samplingblock_samples=ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
     215             469 :     samplingblocks_hor=TIFFhowmany_32(td->td_tilewidth,ycbcrsubsampling[0]);
     216             469 :     samplingblocks_ver=TIFFhowmany_32(nrows,ycbcrsubsampling[1]);
     217             469 :     samplingrow_samples=_TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
     218             469 :     samplingrow_size=TIFFhowmany8_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module));
     219             469 :     return(_TIFFMultiply64(tif,samplingrow_size,samplingblocks_ver,module));
     220                 :   }
     221                 :   else
     222           29733 :     return(_TIFFMultiply64(tif,nrows,TIFFTileRowSize64(tif),module));
     223                 : }
     224                 : tmsize_t
     225               0 : TIFFVTileSize(TIFF* tif, uint32 nrows)
     226                 : {
     227                 :   static const char module[] = "TIFFVTileSize";
     228                 :   uint64 m;
     229                 :   tmsize_t n;
     230               0 :   m=TIFFVTileSize64(tif,nrows);
     231               0 :   n=(tmsize_t)m;
     232               0 :   if ((uint64)n!=m)
     233                 :   {
     234               0 :     TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
     235               0 :     n=0;
     236                 :   }
     237               0 :   return(n);
     238                 : }
     239                 : 
     240                 : /*
     241                 :  * Compute the # bytes in a row-aligned tile.
     242                 :  */
     243                 : uint64
     244           27078 : TIFFTileSize64(TIFF* tif)
     245                 : {
     246           27078 :   return (TIFFVTileSize64(tif, tif->tif_dir.td_tilelength));
     247                 : }
     248                 : tmsize_t
     249           27078 : TIFFTileSize(TIFF* tif)
     250                 : {
     251                 :   static const char module[] = "TIFFTileSize";
     252                 :   uint64 m;
     253                 :   tmsize_t n;
     254           27078 :   m=TIFFTileSize64(tif);
     255           27078 :   n=(tmsize_t)m;
     256           27078 :   if ((uint64)n!=m)
     257                 :   {
     258               0 :     TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
     259               0 :     n=0;
     260                 :   }
     261           27078 :   return(n);
     262                 : }
     263                 : 
     264                 : /*
     265                 :  * Compute a default tile size based on the image
     266                 :  * characteristics and a requested value.  If a
     267                 :  * request is <1 then we choose a size according
     268                 :  * to certain heuristics.
     269                 :  */
     270                 : void
     271               0 : TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
     272                 : {
     273               0 :   (*tif->tif_deftilesize)(tif, tw, th);
     274               0 : }
     275                 : 
     276                 : void
     277               0 : _TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
     278                 : {
     279                 :   (void) tif;
     280               0 :   if (*(int32*) tw < 1)
     281               0 :     *tw = 256;
     282               0 :   if (*(int32*) th < 1)
     283               0 :     *th = 256;
     284                 :   /* roundup to a multiple of 16 per the spec */
     285               0 :   if (*tw & 0xf)
     286               0 :     *tw = TIFFroundup_32(*tw, 16);
     287               0 :   if (*th & 0xf)
     288               0 :     *th = TIFFroundup_32(*th, 16);
     289               0 : }
     290                 : 
     291                 : /* vim: set ts=8 sts=8 sw=8 noet: */
     292                 : /*
     293                 :  * Local Variables:
     294                 :  * mode: c
     295                 :  * c-basic-offset: 8
     296                 :  * fill-column: 78
     297                 :  * End:
     298                 :  */

Generated by: LCOV version 1.7