LCOV - code coverage report
Current view: top level - frmts/gtiff/libtiff - tif_tile.c (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 82 135 60.7 %
Date: 2026-07-16 12:28:33 Functions: 8 11 72.7 %

          Line data    Source code
       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             : #include "tiffiop.h"
      31             : 
      32             : /*
      33             :  * Compute which tile an (x,y,z,s) value is in.
      34             :  */
      35           6 : uint32_t TIFFComputeTile(TIFF *tif, uint32_t x, uint32_t y, uint32_t z,
      36             :                          uint16_t s)
      37             : {
      38           6 :     TIFFDirectory *td = &tif->tif_dir;
      39           6 :     uint32_t dx = td->td_tilewidth;
      40           6 :     uint32_t dy = td->td_tilelength;
      41           6 :     uint32_t dz = td->td_tiledepth;
      42           6 :     uint32_t tile = 1;
      43             : 
      44           6 :     if (td->td_imagedepth == 1)
      45           6 :         z = 0;
      46           6 :     if (dx == (uint32_t)-1)
      47           0 :         dx = td->td_imagewidth;
      48           6 :     if (dy == (uint32_t)-1)
      49           0 :         dy = td->td_imagelength;
      50           6 :     if (dz == (uint32_t)-1)
      51           0 :         dz = td->td_imagedepth;
      52           6 :     if (dx != 0 && dy != 0 && dz != 0)
      53             :     {
      54           6 :         uint32_t xpt = TIFFhowmany_32(td->td_imagewidth, dx);
      55           6 :         uint32_t ypt = TIFFhowmany_32(td->td_imagelength, dy);
      56           6 :         uint32_t zpt = TIFFhowmany_32(td->td_imagedepth, dz);
      57           6 :         uint32_t xpt_ypt = _TIFFMultiply32(tif, xpt, ypt, "TIFFComputeTile");
      58             :         uint32_t xpt_ypt_zpt =
      59           6 :             _TIFFMultiply32(tif, xpt_ypt, zpt, "TIFFComputeTile");
      60             :         uint64_t z_offset;
      61             :         uint64_t y_offset;
      62             :         uint64_t tile64;
      63             : 
      64           6 :         if ((xpt_ypt == 0 && xpt != 0 && ypt != 0) ||
      65           0 :             (xpt_ypt_zpt == 0 && xpt_ypt != 0 && zpt != 0))
      66           0 :             return (0);
      67             : 
      68           6 :         z_offset = _TIFFMultiply64(tif, xpt_ypt, z / dz, "TIFFComputeTile");
      69           6 :         y_offset = _TIFFMultiply64(tif, xpt, y / dy, "TIFFComputeTile");
      70           6 :         if ((z_offset == 0 && xpt_ypt != 0 && (z / dz) != 0) ||
      71           6 :             (y_offset == 0 && xpt != 0 && (y / dy) != 0))
      72           0 :             return (0);
      73           6 :         tile64 = _TIFFAdd64(tif, z_offset, y_offset, "TIFFComputeTile");
      74           6 :         if (tile64 == 0 && (z_offset != 0 || y_offset != 0))
      75           0 :             return (0);
      76           6 :         tile64 = _TIFFAdd64(tif, tile64, x / dx, "TIFFComputeTile");
      77           6 :         if (tile64 == 0 && (z_offset != 0 || y_offset != 0 || (x / dx) != 0))
      78           0 :             return (0);
      79           6 :         if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
      80             :         {
      81             :             uint64_t sample_offset;
      82           4 :             if (s >= td->td_samplesperpixel)
      83             :             {
      84           0 :                 TIFFErrorExtR(
      85             :                     tif, "TIFFComputeTile", "%lu: Sample out of range, max %lu",
      86           0 :                     (unsigned long)s, (unsigned long)td->td_samplesperpixel);
      87           0 :                 return (0);
      88             :             }
      89             :             sample_offset =
      90           4 :                 _TIFFMultiply64(tif, xpt_ypt_zpt, s, "TIFFComputeTile");
      91           4 :             if (sample_offset == 0 && xpt_ypt_zpt != 0 && s != 0)
      92           0 :                 return (0);
      93           4 :             tile64 = _TIFFAdd64(tif, sample_offset, tile64, "TIFFComputeTile");
      94           4 :             if (tile64 == 0 && (sample_offset != 0 || z_offset != 0 ||
      95           2 :                                 y_offset != 0 || (x / dx) != 0))
      96           0 :                 return (0);
      97             :         }
      98           6 :         tile = _TIFFCastUInt64ToUInt32(tif, tile64, "TIFFComputeTile");
      99           6 :         if (tile == 0 && tile64 != 0)
     100           0 :             return (0);
     101             :     }
     102           6 :     return (tile);
     103             : }
     104             : 
     105             : /*
     106             :  * Check an (x,y,z,s) coordinate
     107             :  * against the image bounds.
     108             :  */
     109           6 : int TIFFCheckTile(TIFF *tif, uint32_t x, uint32_t y, uint32_t z, uint16_t s)
     110             : {
     111           6 :     TIFFDirectory *td = &tif->tif_dir;
     112             : 
     113           6 :     if (x >= td->td_imagewidth)
     114             :     {
     115           0 :         TIFFErrorExtR(tif, tif->tif_name, "%lu: Col out of range, max %lu",
     116           0 :                       (unsigned long)x, (unsigned long)(td->td_imagewidth - 1));
     117           0 :         return (0);
     118             :     }
     119           6 :     if (y >= td->td_imagelength)
     120             :     {
     121           0 :         TIFFErrorExtR(tif, tif->tif_name, "%lu: Row out of range, max %lu",
     122             :                       (unsigned long)y,
     123           0 :                       (unsigned long)(td->td_imagelength - 1));
     124           0 :         return (0);
     125             :     }
     126           6 :     if (z >= td->td_imagedepth)
     127             :     {
     128           0 :         TIFFErrorExtR(tif, tif->tif_name, "%lu: Depth out of range, max %lu",
     129           0 :                       (unsigned long)z, (unsigned long)(td->td_imagedepth - 1));
     130           0 :         return (0);
     131             :     }
     132           6 :     if (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
     133           4 :         s >= td->td_samplesperpixel)
     134             :     {
     135           0 :         TIFFErrorExtR(tif, tif->tif_name, "%lu: Sample out of range, max %lu",
     136             :                       (unsigned long)s,
     137           0 :                       (unsigned long)(td->td_samplesperpixel - 1));
     138           0 :         return (0);
     139             :     }
     140           6 :     return (1);
     141             : }
     142             : 
     143             : /*
     144             :  * Compute how many tiles are in an image.
     145             :  */
     146       49033 : uint32_t TIFFNumberOfTiles(TIFF *tif)
     147             : {
     148       49033 :     TIFFDirectory *td = &tif->tif_dir;
     149       49033 :     uint32_t dx = td->td_tilewidth;
     150       49033 :     uint32_t dy = td->td_tilelength;
     151       49033 :     uint32_t dz = td->td_tiledepth;
     152             :     uint32_t ntiles;
     153             : 
     154       49033 :     if (dx == (uint32_t)-1)
     155           0 :         dx = td->td_imagewidth;
     156       49033 :     if (dy == (uint32_t)-1)
     157           0 :         dy = td->td_imagelength;
     158       49033 :     if (dz == (uint32_t)-1)
     159           0 :         dz = td->td_imagedepth;
     160       49033 :     ntiles =
     161       49033 :         (dx == 0 || dy == 0 || dz == 0)
     162             :             ? 0
     163       98066 :             : _TIFFMultiply32(
     164             :                   tif,
     165       49033 :                   _TIFFMultiply32(tif, TIFFhowmany_32(td->td_imagewidth, dx),
     166       49033 :                                   TIFFhowmany_32(td->td_imagelength, dy),
     167             :                                   "TIFFNumberOfTiles"),
     168       49033 :                   TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles");
     169       49033 :     if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
     170        6695 :         ntiles = _TIFFMultiply32(tif, ntiles, td->td_samplesperpixel,
     171             :                                  "TIFFNumberOfTiles");
     172       49033 :     return (ntiles);
     173             : }
     174             : 
     175             : /*
     176             :  * Compute the # bytes in each row of a tile.
     177             :  */
     178      182039 : uint64_t TIFFTileRowSize64(TIFF *tif)
     179             : {
     180             :     static const char module[] = "TIFFTileRowSize64";
     181      182039 :     TIFFDirectory *td = &tif->tif_dir;
     182             :     uint64_t rowsize;
     183             :     uint64_t tilerowsize;
     184             : 
     185      182039 :     if (td->td_tilelength == 0)
     186             :     {
     187           0 :         TIFFErrorExtR(tif, module, "Tile length is zero");
     188           0 :         return 0;
     189             :     }
     190      182039 :     if (td->td_tilewidth == 0)
     191             :     {
     192           0 :         TIFFErrorExtR(tif, module, "Tile width is zero");
     193           0 :         return (0);
     194             :     }
     195      182039 :     rowsize = _TIFFMultiply64(tif, td->td_bitspersample, td->td_tilewidth,
     196             :                               "TIFFTileRowSize");
     197      182080 :     if (td->td_planarconfig == PLANARCONFIG_CONTIG)
     198             :     {
     199      161732 :         if (td->td_samplesperpixel == 0)
     200             :         {
     201           0 :             TIFFErrorExtR(tif, module, "Samples per pixel is zero");
     202           0 :             return 0;
     203             :         }
     204      161732 :         rowsize = _TIFFMultiply64(tif, rowsize, td->td_samplesperpixel,
     205             :                                   "TIFFTileRowSize");
     206             :     }
     207      182037 :     tilerowsize = TIFFhowmany8_64(rowsize);
     208      182037 :     if (tilerowsize == 0)
     209             :     {
     210           0 :         TIFFErrorExtR(tif, module, "Computed tile row size is zero");
     211           0 :         return 0;
     212             :     }
     213      182037 :     return (tilerowsize);
     214             : }
     215        1164 : tmsize_t TIFFTileRowSize(TIFF *tif)
     216             : {
     217             :     static const char module[] = "TIFFTileRowSize";
     218             :     uint64_t m;
     219        1164 :     m = TIFFTileRowSize64(tif);
     220        1164 :     return _TIFFCastUInt64ToSSize(tif, m, module);
     221             : }
     222             : 
     223             : /*
     224             :  * Compute the # bytes in a variable length, row-aligned tile.
     225             :  */
     226      181462 : uint64_t TIFFVTileSize64(TIFF *tif, uint32_t nrows)
     227             : {
     228      181462 :     return _TIFFStrileSize64(tif, nrows, /* isStrip = */ FALSE);
     229             : }
     230             : 
     231           0 : tmsize_t TIFFVTileSize(TIFF *tif, uint32_t nrows)
     232             : {
     233             :     static const char module[] = "TIFFVTileSize";
     234             :     uint64_t m;
     235           0 :     m = TIFFVTileSize64(tif, nrows);
     236           0 :     return _TIFFCastUInt64ToSSize(tif, m, module);
     237             : }
     238             : 
     239             : /*
     240             :  * Compute the # bytes in a row-aligned tile.
     241             :  */
     242      159571 : uint64_t TIFFTileSize64(TIFF *tif)
     243             : {
     244      159571 :     return (TIFFVTileSize64(tif, tif->tif_dir.td_tilelength));
     245             : }
     246      150885 : tmsize_t TIFFTileSize(TIFF *tif)
     247             : {
     248             :     static const char module[] = "TIFFTileSize";
     249             :     uint64_t m;
     250      150885 :     m = TIFFTileSize64(tif);
     251      150883 :     return _TIFFCastUInt64ToSSize(tif, m, module);
     252             : }
     253             : 
     254             : /*
     255             :  * Compute a default tile size based on the image
     256             :  * characteristics and a requested value.  If a
     257             :  * request is <1 then we choose a size according
     258             :  * to certain heuristics.
     259             :  */
     260           0 : void TIFFDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th)
     261             : {
     262           0 :     (*tif->tif_deftilesize)(tif, tw, th);
     263           0 : }
     264             : 
     265           0 : void _TIFFDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th)
     266             : {
     267             :     (void)tif;
     268           0 :     if (*(int32_t *)tw < 1)
     269           0 :         *tw = 256;
     270           0 :     if (*(int32_t *)th < 1)
     271           0 :         *th = 256;
     272             :     /* roundup to a multiple of 16 per the spec */
     273           0 :     if (*tw & 0xf)
     274           0 :         *tw = TIFFroundup_32(*tw, 16);
     275           0 :     if (*th & 0xf)
     276           0 :         *th = TIFFroundup_32(*th, 16);
     277           0 : }

Generated by: LCOV version 1.14