LCOV - code coverage report
Current view: top level - frmts/gtiff/libtiff - tif_tile.c (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 69 113 61.1 %
Date: 2026-06-17 14:20:39 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             : 
      61           6 :         if ((xpt_ypt == 0 && xpt != 0 && ypt != 0) ||
      62           0 :             (xpt_ypt_zpt == 0 && xpt_ypt != 0 && zpt != 0))
      63           0 :             return (0);
      64             : 
      65           6 :         if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
      66           4 :             tile =
      67           4 :                 xpt_ypt_zpt * s + xpt_ypt * (z / dz) + xpt * (y / dy) + x / dx;
      68             :         else
      69           2 :             tile = xpt_ypt * (z / dz) + xpt * (y / dy) + x / dx;
      70             :     }
      71           6 :     return (tile);
      72             : }
      73             : 
      74             : /*
      75             :  * Check an (x,y,z,s) coordinate
      76             :  * against the image bounds.
      77             :  */
      78           6 : int TIFFCheckTile(TIFF *tif, uint32_t x, uint32_t y, uint32_t z, uint16_t s)
      79             : {
      80           6 :     TIFFDirectory *td = &tif->tif_dir;
      81             : 
      82           6 :     if (x >= td->td_imagewidth)
      83             :     {
      84           0 :         TIFFErrorExtR(tif, tif->tif_name, "%lu: Col out of range, max %lu",
      85           0 :                       (unsigned long)x, (unsigned long)(td->td_imagewidth - 1));
      86           0 :         return (0);
      87             :     }
      88           6 :     if (y >= td->td_imagelength)
      89             :     {
      90           0 :         TIFFErrorExtR(tif, tif->tif_name, "%lu: Row out of range, max %lu",
      91             :                       (unsigned long)y,
      92           0 :                       (unsigned long)(td->td_imagelength - 1));
      93           0 :         return (0);
      94             :     }
      95           6 :     if (z >= td->td_imagedepth)
      96             :     {
      97           0 :         TIFFErrorExtR(tif, tif->tif_name, "%lu: Depth out of range, max %lu",
      98           0 :                       (unsigned long)z, (unsigned long)(td->td_imagedepth - 1));
      99           0 :         return (0);
     100             :     }
     101           6 :     if (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
     102           4 :         s >= td->td_samplesperpixel)
     103             :     {
     104           0 :         TIFFErrorExtR(tif, tif->tif_name, "%lu: Sample out of range, max %lu",
     105             :                       (unsigned long)s,
     106           0 :                       (unsigned long)(td->td_samplesperpixel - 1));
     107           0 :         return (0);
     108             :     }
     109           6 :     return (1);
     110             : }
     111             : 
     112             : /*
     113             :  * Compute how many tiles are in an image.
     114             :  */
     115       49030 : uint32_t TIFFNumberOfTiles(TIFF *tif)
     116             : {
     117       49030 :     TIFFDirectory *td = &tif->tif_dir;
     118       49030 :     uint32_t dx = td->td_tilewidth;
     119       49030 :     uint32_t dy = td->td_tilelength;
     120       49030 :     uint32_t dz = td->td_tiledepth;
     121             :     uint32_t ntiles;
     122             : 
     123       49030 :     if (dx == (uint32_t)-1)
     124           0 :         dx = td->td_imagewidth;
     125       49030 :     if (dy == (uint32_t)-1)
     126           0 :         dy = td->td_imagelength;
     127       49030 :     if (dz == (uint32_t)-1)
     128           0 :         dz = td->td_imagedepth;
     129       49029 :     ntiles =
     130       49029 :         (dx == 0 || dy == 0 || dz == 0)
     131             :             ? 0
     132       98059 :             : _TIFFMultiply32(
     133             :                   tif,
     134       49029 :                   _TIFFMultiply32(tif, TIFFhowmany_32(td->td_imagewidth, dx),
     135       49029 :                                   TIFFhowmany_32(td->td_imagelength, dy),
     136             :                                   "TIFFNumberOfTiles"),
     137       49029 :                   TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles");
     138       49029 :     if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
     139        6695 :         ntiles = _TIFFMultiply32(tif, ntiles, td->td_samplesperpixel,
     140             :                                  "TIFFNumberOfTiles");
     141       49030 :     return (ntiles);
     142             : }
     143             : 
     144             : /*
     145             :  * Compute the # bytes in each row of a tile.
     146             :  */
     147      181899 : uint64_t TIFFTileRowSize64(TIFF *tif)
     148             : {
     149             :     static const char module[] = "TIFFTileRowSize64";
     150      181899 :     TIFFDirectory *td = &tif->tif_dir;
     151             :     uint64_t rowsize;
     152             :     uint64_t tilerowsize;
     153             : 
     154      181899 :     if (td->td_tilelength == 0)
     155             :     {
     156           0 :         TIFFErrorExtR(tif, module, "Tile length is zero");
     157           0 :         return 0;
     158             :     }
     159      181899 :     if (td->td_tilewidth == 0)
     160             :     {
     161           0 :         TIFFErrorExtR(tif, module, "Tile width is zero");
     162           0 :         return (0);
     163             :     }
     164      181899 :     rowsize = _TIFFMultiply64(tif, td->td_bitspersample, td->td_tilewidth,
     165             :                               "TIFFTileRowSize");
     166      181993 :     if (td->td_planarconfig == PLANARCONFIG_CONTIG)
     167             :     {
     168      161594 :         if (td->td_samplesperpixel == 0)
     169             :         {
     170           0 :             TIFFErrorExtR(tif, module, "Samples per pixel is zero");
     171           0 :             return 0;
     172             :         }
     173      161594 :         rowsize = _TIFFMultiply64(tif, rowsize, td->td_samplesperpixel,
     174             :                                   "TIFFTileRowSize");
     175             :     }
     176      181873 :     tilerowsize = TIFFhowmany8_64(rowsize);
     177      181873 :     if (tilerowsize == 0)
     178             :     {
     179           0 :         TIFFErrorExtR(tif, module, "Computed tile row size is zero");
     180           0 :         return 0;
     181             :     }
     182      181873 :     return (tilerowsize);
     183             : }
     184        1164 : tmsize_t TIFFTileRowSize(TIFF *tif)
     185             : {
     186             :     static const char module[] = "TIFFTileRowSize";
     187             :     uint64_t m;
     188        1164 :     m = TIFFTileRowSize64(tif);
     189        1164 :     return _TIFFCastUInt64ToSSize(tif, m, module);
     190             : }
     191             : 
     192             : /*
     193             :  * Compute the # bytes in a variable length, row-aligned tile.
     194             :  */
     195      181373 : uint64_t TIFFVTileSize64(TIFF *tif, uint32_t nrows)
     196             : {
     197      181373 :     return _TIFFStrileSize64(tif, nrows, /* isStrip = */ FALSE);
     198             : }
     199             : 
     200           0 : tmsize_t TIFFVTileSize(TIFF *tif, uint32_t nrows)
     201             : {
     202             :     static const char module[] = "TIFFVTileSize";
     203             :     uint64_t m;
     204           0 :     m = TIFFVTileSize64(tif, nrows);
     205           0 :     return _TIFFCastUInt64ToSSize(tif, m, module);
     206             : }
     207             : 
     208             : /*
     209             :  * Compute the # bytes in a row-aligned tile.
     210             :  */
     211      159553 : uint64_t TIFFTileSize64(TIFF *tif)
     212             : {
     213      159553 :     return (TIFFVTileSize64(tif, tif->tif_dir.td_tilelength));
     214             : }
     215      150869 : tmsize_t TIFFTileSize(TIFF *tif)
     216             : {
     217             :     static const char module[] = "TIFFTileSize";
     218             :     uint64_t m;
     219      150869 :     m = TIFFTileSize64(tif);
     220      150862 :     return _TIFFCastUInt64ToSSize(tif, m, module);
     221             : }
     222             : 
     223             : /*
     224             :  * Compute a default tile size based on the image
     225             :  * characteristics and a requested value.  If a
     226             :  * request is <1 then we choose a size according
     227             :  * to certain heuristics.
     228             :  */
     229           0 : void TIFFDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th)
     230             : {
     231           0 :     (*tif->tif_deftilesize)(tif, tw, th);
     232           0 : }
     233             : 
     234           0 : void _TIFFDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th)
     235             : {
     236             :     (void)tif;
     237           0 :     if (*(int32_t *)tw < 1)
     238           0 :         *tw = 256;
     239           0 :     if (*(int32_t *)th < 1)
     240           0 :         *th = 256;
     241             :     /* roundup to a multiple of 16 per the spec */
     242           0 :     if (*tw & 0xf)
     243           0 :         *tw = TIFFroundup_32(*tw, 16);
     244           0 :     if (*th & 0xf)
     245           0 :         *th = TIFFroundup_32(*th, 16);
     246           0 : }

Generated by: LCOV version 1.14