LCOV - code coverage report
Current view: top level - frmts/gtiff/libtiff - tif_strip.c (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 103 152 67.8 %
Date: 2026-06-11 02:21:55 Functions: 11 15 73.3 %

          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             :  * Strip-organized Image Support Routines.
      29             :  */
      30             : #include "tiffiop.h"
      31             : 
      32             : /*
      33             :  * Compute which strip a (row,sample) value is in.
      34             :  */
      35          55 : uint32_t TIFFComputeStrip(TIFF *tif, uint32_t row, uint16_t sample)
      36             : {
      37             :     static const char module[] = "TIFFComputeStrip";
      38          55 :     TIFFDirectory *td = &tif->tif_dir;
      39             :     uint32_t strip;
      40             : 
      41          55 :     if (td->td_rowsperstrip == 0)
      42             :     {
      43           0 :         TIFFErrorExtR(tif, module,
      44             :                       "Cannot compute strip: RowsPerStrip is zero");
      45           0 :         return 0;
      46             :     }
      47          55 :     strip = row / td->td_rowsperstrip;
      48          55 :     if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
      49             :     {
      50           4 :         if (sample >= td->td_samplesperpixel)
      51             :         {
      52           0 :             TIFFErrorExtR(tif, module, "%lu: Sample out of range, max %lu",
      53             :                           (unsigned long)sample,
      54           0 :                           (unsigned long)td->td_samplesperpixel);
      55           0 :             return (0);
      56             :         }
      57           4 :         uint32_t sample_offset = _TIFFMultiply32(
      58             :             tif, (uint32_t)sample, td->td_stripsperimage, "TIFFComputeStrip");
      59           4 :         if (sample_offset == 0 && sample != 0 && td->td_stripsperimage != 0)
      60           0 :             return (0);
      61           4 :         strip += sample_offset;
      62             :     }
      63          55 :     return (strip);
      64             : }
      65             : 
      66             : /*
      67             :  * Compute how many strips are in an image.
      68             :  */
      69      184549 : uint32_t TIFFNumberOfStrips(TIFF *tif)
      70             : {
      71      184549 :     TIFFDirectory *td = &tif->tif_dir;
      72             :     uint32_t nstrips;
      73             : 
      74      184549 :     if (td->td_rowsperstrip == 0)
      75             :     {
      76           0 :         TIFFWarningExtR(tif, "TIFFNumberOfStrips", "RowsPerStrip is zero");
      77           0 :         return 0;
      78             :     }
      79      369098 :     nstrips = (td->td_rowsperstrip == (uint32_t)-1
      80             :                    ? 1
      81      184549 :                    : TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));
      82      184549 :     if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
      83             :         nstrips =
      84       14745 :             _TIFFMultiply32(tif, nstrips, (uint32_t)td->td_samplesperpixel,
      85             :                             "TIFFNumberOfStrips");
      86      184538 :     return (nstrips);
      87             : }
      88             : 
      89             : /*
      90             :  * Compute the # bytes in a variable height, row-aligned strip if isStrip is
      91             :  * TRUE, or in a tile if isStrip is FALSE
      92             :  */
      93     4946400 : uint64_t _TIFFStrileSize64(TIFF *tif, uint32_t nrows, int isStrip)
      94             : {
      95             :     static const char module[] = "_TIFFStrileSize64";
      96     4946400 :     TIFFDirectory *td = &tif->tif_dir;
      97     4946400 :     if (isStrip)
      98             :     {
      99     4767230 :         if (nrows == (uint32_t)(-1))
     100           0 :             nrows = td->td_imagelength;
     101             :     }
     102             :     else
     103             :     {
     104      179172 :         if (td->td_tilelength == 0 || td->td_tilewidth == 0 ||
     105      179189 :             td->td_tiledepth == 0)
     106           0 :             return (0);
     107             :     }
     108     4946400 :     if ((td->td_planarconfig == PLANARCONFIG_CONTIG) &&
     109     4837860 :         (td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
     110             :     {
     111             :         /*
     112             :          * Packed YCbCr data contain one Cb+Cr for every
     113             :          * HorizontalSampling*VerticalSampling Y values.
     114             :          * Must also roundup width and height when calculating
     115             :          * since images that are not a multiple of the
     116             :          * horizontal/vertical subsampling area include
     117             :          * YCbCr data for the extended image.
     118             :          */
     119             :         uint16_t ycbcrsubsampling[2];
     120             :         uint16_t samplingblock_samples;
     121             :         uint32_t samplingblocks_hor;
     122             :         uint32_t samplingblocks_ver;
     123             :         uint64_t samplingrow_samples;
     124             :         uint64_t samplingrow_size;
     125        1534 :         if (td->td_samplesperpixel != 3)
     126             :         {
     127           0 :             TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value");
     128           0 :             return 0;
     129             :         }
     130        1534 :         TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
     131             :                               ycbcrsubsampling + 0, ycbcrsubsampling + 1);
     132        1534 :         if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 &&
     133          20 :              ycbcrsubsampling[0] != 4) ||
     134        1534 :             (ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 &&
     135          10 :              ycbcrsubsampling[1] != 4) ||
     136        1534 :             (ycbcrsubsampling[0] == 0 || ycbcrsubsampling[1] == 0))
     137             :         {
     138           0 :             TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling (%dx%d)",
     139           0 :                           ycbcrsubsampling[0], ycbcrsubsampling[1]);
     140           0 :             return 0;
     141             :         }
     142        1534 :         samplingblock_samples =
     143        1534 :             (uint16_t)(ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2);
     144        1534 :         const uint32_t width = isStrip ? td->td_imagewidth : td->td_tilewidth;
     145        1534 :         samplingblocks_hor = TIFFhowmany_32(width, ycbcrsubsampling[0]);
     146        1534 :         samplingblocks_ver = TIFFhowmany_32(nrows, ycbcrsubsampling[1]);
     147        1534 :         samplingrow_samples = _TIFFMultiply64(tif, samplingblocks_hor,
     148             :                                               samplingblock_samples, module);
     149        1534 :         samplingrow_size = TIFFhowmany8_64(_TIFFMultiply64(
     150             :             tif, samplingrow_samples, td->td_bitspersample, module));
     151             :         return (
     152        1534 :             _TIFFMultiply64(tif, samplingrow_size, samplingblocks_ver, module));
     153             :     }
     154             :     else
     155     9889760 :         return (_TIFFMultiply64(tif, nrows,
     156     4766250 :                                 isStrip ? TIFFScanlineSize64(tif)
     157      178621 :                                         : TIFFTileRowSize64(tif),
     158             :                                 module));
     159             : }
     160             : 
     161             : /*
     162             :  * Compute the # bytes in a variable height, row-aligned strip.
     163             :  */
     164     4767230 : uint64_t TIFFVStripSize64(TIFF *tif, uint32_t nrows)
     165             : {
     166     4767230 :     return _TIFFStrileSize64(tif, nrows, /* isStrip = */ TRUE);
     167             : }
     168             : 
     169     2102130 : tmsize_t TIFFVStripSize(TIFF *tif, uint32_t nrows)
     170             : {
     171             :     static const char module[] = "TIFFVStripSize";
     172             :     uint64_t m;
     173     2102130 :     m = TIFFVStripSize64(tif, nrows);
     174     2102130 :     return _TIFFCastUInt64ToSSize(tif, m, module);
     175             : }
     176             : 
     177             : /*
     178             :  * Compute the # bytes in a raw strip.
     179             :  */
     180           0 : uint64_t TIFFRawStripSize64(TIFF *tif, uint32_t strip)
     181             : {
     182             :     static const char module[] = "TIFFRawStripSize64";
     183           0 :     uint64_t bytecount = TIFFGetStrileByteCount(tif, strip);
     184             : 
     185           0 :     if (bytecount == 0)
     186             :     {
     187           0 :         TIFFErrorExtR(tif, module,
     188             :                       "%" PRIu64 ": Invalid strip byte count, strip %lu",
     189             :                       (uint64_t)bytecount, (unsigned long)strip);
     190           0 :         bytecount = (uint64_t)-1;
     191             :     }
     192             : 
     193           0 :     return bytecount;
     194             : }
     195           0 : tmsize_t TIFFRawStripSize(TIFF *tif, uint32_t strip)
     196             : {
     197             :     static const char module[] = "TIFFRawStripSize";
     198             :     uint64_t m;
     199             :     tmsize_t n;
     200           0 :     m = TIFFRawStripSize64(tif, strip);
     201           0 :     if (m == (uint64_t)(-1))
     202           0 :         n = (tmsize_t)(-1);
     203             :     else
     204             :     {
     205           0 :         n = (tmsize_t)m;
     206           0 :         if ((uint64_t)n != m)
     207             :         {
     208           0 :             TIFFErrorExtR(tif, module, "Integer overflow");
     209           0 :             n = 0;
     210             :         }
     211             :     }
     212           0 :     return (n);
     213             : }
     214             : 
     215             : /*
     216             :  * Compute the # bytes in a (row-aligned) strip.
     217             :  *
     218             :  * Note that if RowsPerStrip is larger than the
     219             :  * recorded ImageLength, then the strip size is
     220             :  * truncated to reflect the actual space required
     221             :  * to hold the strip.
     222             :  */
     223     2650640 : uint64_t TIFFStripSize64(TIFF *tif)
     224             : {
     225     2650640 :     TIFFDirectory *td = &tif->tif_dir;
     226     2650640 :     uint32_t rps = td->td_rowsperstrip;
     227     2650640 :     if (rps > td->td_imagelength)
     228         131 :         rps = td->td_imagelength;
     229     2650640 :     return (TIFFVStripSize64(tif, rps));
     230             : }
     231     2620290 : tmsize_t TIFFStripSize(TIFF *tif)
     232             : {
     233             :     static const char module[] = "TIFFStripSize";
     234             :     uint64_t m;
     235     2620290 :     m = TIFFStripSize64(tif);
     236     2620280 :     return _TIFFCastUInt64ToSSize(tif, m, module);
     237             : }
     238             : 
     239             : /*
     240             :  * Compute a default strip size based on the image
     241             :  * characteristics and a requested value.  If the
     242             :  * request is <1 then we choose a strip size according
     243             :  * to certain heuristics.
     244             :  */
     245        7012 : uint32_t TIFFDefaultStripSize(TIFF *tif, uint32_t request)
     246             : {
     247        7012 :     return (*tif->tif_defstripsize)(tif, request);
     248             : }
     249             : 
     250        7012 : uint32_t _TIFFDefaultStripSize(TIFF *tif, uint32_t s)
     251             : {
     252        7012 :     if ((int32_t)s < 1)
     253             :     {
     254             :         /*
     255             :          * If RowsPerStrip is unspecified, try to break the
     256             :          * image up into strips that are approximately
     257             :          * STRIP_SIZE_DEFAULT bytes long.
     258             :          */
     259             :         uint64_t scanlinesize;
     260             :         uint64_t rows;
     261        7012 :         scanlinesize = TIFFScanlineSize64(tif);
     262        7012 :         if (scanlinesize == 0)
     263           0 :             scanlinesize = 1;
     264        7012 :         rows = (uint64_t)STRIP_SIZE_DEFAULT / scanlinesize;
     265        7012 :         if (rows == 0)
     266          32 :             rows = 1;
     267        6980 :         else if (rows > 0xFFFFFFFF)
     268           0 :             rows = 0xFFFFFFFF;
     269        7012 :         s = (uint32_t)rows;
     270             :     }
     271        7012 :     return (s);
     272             : }
     273             : 
     274             : /*
     275             :  * Return the number of bytes to read/write in a call to
     276             :  * one of the scanline-oriented i/o routines.  Note that
     277             :  * this number may be 1/samples-per-pixel if data is
     278             :  * stored as separate planes.
     279             :  * The ScanlineSize in case of YCbCrSubsampling is defined as the
     280             :  * strip size divided by the strip height, i.e. the size of a pack of vertical
     281             :  * subsampling lines divided by vertical subsampling. It should thus make
     282             :  * sense when multiplied by a multiple of vertical subsampling.
     283             :  */
     284     4942640 : uint64_t TIFFScanlineSize64(TIFF *tif)
     285             : {
     286             :     static const char module[] = "TIFFScanlineSize64";
     287     4942640 :     TIFFDirectory *td = &tif->tif_dir;
     288             :     uint64_t scanline_size;
     289     4942640 :     if (td->td_planarconfig == PLANARCONFIG_CONTIG)
     290             :     {
     291     4808500 :         if ((td->td_photometric == PHOTOMETRIC_YCBCR) &&
     292       14990 :             (td->td_samplesperpixel == 3) && (!isUpSampled(tif)))
     293        2170 :         {
     294             :             uint16_t ycbcrsubsampling[2];
     295             :             uint16_t samplingblock_samples;
     296             :             uint32_t samplingblocks_hor;
     297             :             uint64_t samplingrow_samples;
     298             :             uint64_t samplingrow_size;
     299        2170 :             if (td->td_samplesperpixel != 3)
     300             :             {
     301           0 :                 TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value");
     302           0 :                 return 0;
     303             :             }
     304        2170 :             TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
     305             :                                   ycbcrsubsampling + 0, ycbcrsubsampling + 1);
     306        2170 :             if (((ycbcrsubsampling[0] != 1) && (ycbcrsubsampling[0] != 2) &&
     307          10 :                  (ycbcrsubsampling[0] != 4)) ||
     308        2170 :                 ((ycbcrsubsampling[1] != 1) && (ycbcrsubsampling[1] != 2) &&
     309           6 :                  (ycbcrsubsampling[1] != 4)) ||
     310        2170 :                 ((ycbcrsubsampling[0] == 0) || (ycbcrsubsampling[1] == 0)))
     311             :             {
     312           0 :                 TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling");
     313           0 :                 return 0;
     314             :             }
     315        2170 :             samplingblock_samples =
     316        2170 :                 (uint16_t)(ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2);
     317        2170 :             samplingblocks_hor =
     318        2170 :                 TIFFhowmany_32(td->td_imagewidth, ycbcrsubsampling[0]);
     319        2170 :             samplingrow_samples = _TIFFMultiply64(
     320             :                 tif, samplingblocks_hor, samplingblock_samples, module);
     321        2170 :             samplingrow_size =
     322        2170 :                 TIFFhowmany_64(_TIFFMultiply64(tif, samplingrow_samples,
     323             :                                                td->td_bitspersample, module),
     324             :                                8);
     325        2170 :             scanline_size = (samplingrow_size / ycbcrsubsampling[1]);
     326             :         }
     327             :         else
     328             :         {
     329             :             uint64_t scanline_samples;
     330     4806330 :             uint32_t scanline_width = td->td_imagewidth;
     331             : 
     332             : #if 0
     333             :             // Tries to fix https://gitlab.com/libtiff/libtiff/-/merge_requests/564
     334             :             // but causes regression when decoding legit files with tiffcp -c none
     335             :             // Cf https://gitlab.com/libtiff/libtiff/-/merge_requests/644
     336             :             if (td->td_photometric == PHOTOMETRIC_YCBCR)
     337             :             {
     338             :                 uint16_t subsampling_hor;
     339             :                 uint16_t ignored;
     340             :                 TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
     341             :                                       &subsampling_hor, &ignored);
     342             :                 if (subsampling_hor > 1) // roundup width for YCbCr
     343             :                     scanline_width =
     344             :                         TIFFroundup_32(scanline_width, subsampling_hor);
     345             :             }
     346             : #endif
     347             : 
     348     4806330 :             scanline_samples = _TIFFMultiply64(tif, scanline_width,
     349     4806330 :                                                td->td_samplesperpixel, module);
     350     4806340 :             scanline_size =
     351     4806320 :                 TIFFhowmany_64(_TIFFMultiply64(tif, scanline_samples,
     352             :                                                td->td_bitspersample, module),
     353             :                                8);
     354             :         }
     355             :     }
     356             :     else
     357             :     {
     358      134143 :         scanline_size =
     359      134142 :             TIFFhowmany_64(_TIFFMultiply64(tif, td->td_imagewidth,
     360             :                                            td->td_bitspersample, module),
     361             :                            8);
     362             :     }
     363     4942650 :     if (scanline_size == 0)
     364             :     {
     365           0 :         TIFFErrorExtR(tif, module, "Computed scanline size is zero");
     366           0 :         return 0;
     367             :     }
     368     4942650 :     return (scanline_size);
     369             : }
     370      148186 : tmsize_t TIFFScanlineSize(TIFF *tif)
     371             : {
     372             :     static const char module[] = "TIFFScanlineSize";
     373             :     uint64_t m;
     374      148186 :     m = TIFFScanlineSize64(tif);
     375      148180 :     return _TIFFCastUInt64ToSSize(tif, m, module);
     376             : }
     377             : 
     378             : /*
     379             :  * Return the number of bytes required to store a complete
     380             :  * decoded and packed raster scanline (as opposed to the
     381             :  * I/O size returned by TIFFScanlineSize which may be less
     382             :  * if data is store as separate planes).
     383             :  */
     384           0 : uint64_t TIFFRasterScanlineSize64(TIFF *tif)
     385             : {
     386             :     static const char module[] = "TIFFRasterScanlineSize64";
     387           0 :     TIFFDirectory *td = &tif->tif_dir;
     388             :     uint64_t scanline;
     389             : 
     390             :     scanline =
     391           0 :         _TIFFMultiply64(tif, td->td_bitspersample, td->td_imagewidth, module);
     392           0 :     if (td->td_planarconfig == PLANARCONFIG_CONTIG)
     393             :     {
     394             :         scanline =
     395           0 :             _TIFFMultiply64(tif, scanline, td->td_samplesperpixel, module);
     396           0 :         return (TIFFhowmany8_64(scanline));
     397             :     }
     398             :     else
     399           0 :         return (_TIFFMultiply64(tif, TIFFhowmany8_64(scanline),
     400           0 :                                 td->td_samplesperpixel, module));
     401             : }
     402           0 : tmsize_t TIFFRasterScanlineSize(TIFF *tif)
     403             : {
     404             :     static const char module[] = "TIFFRasterScanlineSize";
     405             :     uint64_t m;
     406           0 :     m = TIFFRasterScanlineSize64(tif);
     407           0 :     return _TIFFCastUInt64ToSSize(tif, m, module);
     408             : }

Generated by: LCOV version 1.14