LCOV - code coverage report
Current view: top level - frmts/gtiff/libtiff - tif_strip.c (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 93 140 66.4 %
Date: 2025-10-17 19:28:35 Functions: 10 14 71.4 %

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

Generated by: LCOV version 1.14