LCOV - code coverage report
Current view: top level - frmts/gtiff - gtiffrasterband.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 250 261 95.8 %
Date: 2026-11-18 01:45:17 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GeoTIFF Driver
       4             :  * Purpose:  General methods of GTiffRasterBand
       5             :  * Author:   Frank Warmerdam, warmerdam@pobox.com
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 1998, 2002, Frank Warmerdam <warmerdam@pobox.com>
       9             :  * Copyright (c) 2007-2015, Even Rouault <even dot rouault at spatialys dot com>
      10             :  *
      11             :  * SPDX-License-Identifier: MIT
      12             :  ****************************************************************************/
      13             : 
      14             : #include "gtiffrasterband.h"
      15             : #include "gtiffdataset.h"
      16             : 
      17             : #include <algorithm>
      18             : #include <set>
      19             : 
      20             : #include "gdal_priv.h"
      21             : #include "cpl_vsi_virtual.h"
      22             : #include "tifvsi.h"
      23             : 
      24             : /************************************************************************/
      25             : /*                          GTiffRasterBand()                           */
      26             : /************************************************************************/
      27             : 
      28      902567 : GTiffRasterBand::GTiffRasterBand(GTiffDataset *poDSIn, int nBandIn)
      29      902567 :     : m_poGDS(poDSIn)
      30             : {
      31      902567 :     poDS = poDSIn;
      32      902567 :     nBand = nBandIn;
      33             : 
      34             :     /* -------------------------------------------------------------------- */
      35             :     /*      Get the GDAL data type.                                         */
      36             :     /* -------------------------------------------------------------------- */
      37      902567 :     const uint16_t nBitsPerSample = m_poGDS->m_nBitsPerSample;
      38      902567 :     const uint16_t nSampleFormat = m_poGDS->m_nSampleFormat;
      39             : 
      40      902567 :     eDataType = GDT_Unknown;
      41             : 
      42      902567 :     if (nBitsPerSample <= 8)
      43             :     {
      44      759834 :         if (nSampleFormat == SAMPLEFORMAT_INT)
      45         326 :             eDataType = GDT_Int8;
      46             :         else
      47      759508 :             eDataType = GDT_UInt8;
      48             :     }
      49      142733 :     else if (nBitsPerSample <= 16)
      50             :     {
      51      134562 :         if (nBitsPerSample == 16 && nSampleFormat == SAMPLEFORMAT_IEEEFP)
      52         256 :             eDataType = GDT_Float16;
      53      134306 :         else if (nSampleFormat == SAMPLEFORMAT_INT)
      54       67170 :             eDataType = GDT_Int16;
      55             :         else
      56       67136 :             eDataType = GDT_UInt16;
      57             :     }
      58        8171 :     else if (nBitsPerSample == 32)
      59             :     {
      60        4414 :         if (nSampleFormat == SAMPLEFORMAT_COMPLEXINT)
      61         695 :             eDataType = GDT_CInt16;
      62        3719 :         else if (nSampleFormat == SAMPLEFORMAT_COMPLEXIEEEFP)
      63         224 :             eDataType = GDT_CFloat16;
      64        3495 :         else if (nSampleFormat == SAMPLEFORMAT_IEEEFP)
      65        2262 :             eDataType = GDT_Float32;
      66        1233 :         else if (nSampleFormat == SAMPLEFORMAT_INT)
      67         651 :             eDataType = GDT_Int32;
      68             :         else
      69         582 :             eDataType = GDT_UInt32;
      70             :     }
      71        3757 :     else if (nBitsPerSample == 64)
      72             :     {
      73        3207 :         if (nSampleFormat == SAMPLEFORMAT_IEEEFP)
      74        1719 :             eDataType = GDT_Float64;
      75        1488 :         else if (nSampleFormat == SAMPLEFORMAT_COMPLEXIEEEFP)
      76         476 :             eDataType = GDT_CFloat32;
      77        1012 :         else if (nSampleFormat == SAMPLEFORMAT_COMPLEXINT)
      78         448 :             eDataType = GDT_CInt32;
      79         564 :         else if (nSampleFormat == SAMPLEFORMAT_INT)
      80         289 :             eDataType = GDT_Int64;
      81             :         else
      82         275 :             eDataType = GDT_UInt64;
      83             :     }
      84         550 :     else if (nBitsPerSample == 128)
      85             :     {
      86         480 :         if (nSampleFormat == SAMPLEFORMAT_COMPLEXIEEEFP)
      87         480 :             eDataType = GDT_CFloat64;
      88             :     }
      89             : 
      90             :     /* -------------------------------------------------------------------- */
      91             :     /*      Try to work out band color interpretation.                      */
      92             :     /* -------------------------------------------------------------------- */
      93      902567 :     bool bLookForExtraSamples = false;
      94             : 
      95      902567 :     if (m_poGDS->m_poColorTable != nullptr && nBand == 1)
      96             :     {
      97         173 :         m_eBandInterp = GCI_PaletteIndex;
      98             :     }
      99     1786780 :     else if (m_poGDS->m_nPhotometric == PHOTOMETRIC_RGB ||
     100      884391 :              (m_poGDS->m_nPhotometric == PHOTOMETRIC_YCBCR &&
     101         828 :               m_poGDS->m_nCompression == COMPRESSION_JPEG &&
     102         789 :               CPLTestBool(CPLGetConfigOption("CONVERT_YCBCR_TO_RGB", "YES"))))
     103             :     {
     104       18792 :         if (nBand == 1)
     105        5918 :             m_eBandInterp = GCI_RedBand;
     106       12874 :         else if (nBand == 2)
     107        5918 :             m_eBandInterp = GCI_GreenBand;
     108        6956 :         else if (nBand == 3)
     109        5918 :             m_eBandInterp = GCI_BlueBand;
     110             :         else
     111        1038 :             bLookForExtraSamples = true;
     112             :     }
     113      883602 :     else if (m_poGDS->m_nPhotometric == PHOTOMETRIC_YCBCR)
     114             :     {
     115          39 :         if (nBand == 1)
     116          13 :             m_eBandInterp = GCI_YCbCr_YBand;
     117          26 :         else if (nBand == 2)
     118          13 :             m_eBandInterp = GCI_YCbCr_CbBand;
     119          13 :         else if (nBand == 3)
     120          13 :             m_eBandInterp = GCI_YCbCr_CrBand;
     121             :         else
     122           0 :             bLookForExtraSamples = true;
     123             :     }
     124      883563 :     else if (m_poGDS->m_nPhotometric == PHOTOMETRIC_SEPARATED)
     125             :     {
     126          88 :         if (nBand == 1)
     127          22 :             m_eBandInterp = GCI_CyanBand;
     128          66 :         else if (nBand == 2)
     129          22 :             m_eBandInterp = GCI_MagentaBand;
     130          44 :         else if (nBand == 3)
     131          22 :             m_eBandInterp = GCI_YellowBand;
     132          22 :         else if (nBand == 4)
     133          22 :             m_eBandInterp = GCI_BlackBand;
     134             :         else
     135           0 :             bLookForExtraSamples = true;
     136             :     }
     137      883475 :     else if (m_poGDS->m_nPhotometric == PHOTOMETRIC_MINISBLACK && nBand == 1)
     138             :     {
     139       27514 :         m_eBandInterp = GCI_GrayIndex;
     140             :     }
     141             :     else
     142             :     {
     143      855961 :         bLookForExtraSamples = true;
     144             :     }
     145             : 
     146      902567 :     if (bLookForExtraSamples)
     147             :     {
     148      856999 :         uint16_t *v = nullptr;
     149      856999 :         uint16_t count = 0;
     150             : 
     151      856999 :         if (TIFFGetField(m_poGDS->m_hTIFF, TIFFTAG_EXTRASAMPLES, &count, &v))
     152             :         {
     153      791044 :             const int nBaseSamples = m_poGDS->m_nSamplesPerPixel - count;
     154      791044 :             const int nExpectedBaseSamples =
     155      792085 :                 (m_poGDS->m_nPhotometric == PHOTOMETRIC_MINISBLACK)   ? 1
     156        2082 :                 : (m_poGDS->m_nPhotometric == PHOTOMETRIC_MINISWHITE) ? 1
     157        1047 :                 : (m_poGDS->m_nPhotometric == PHOTOMETRIC_RGB)        ? 3
     158          12 :                 : (m_poGDS->m_nPhotometric == PHOTOMETRIC_YCBCR)      ? 3
     159           6 :                 : (m_poGDS->m_nPhotometric == PHOTOMETRIC_SEPARATED)  ? 4
     160             :                                                                       : 0;
     161             : 
     162      791044 :             if (nExpectedBaseSamples > 0 && nBand == nExpectedBaseSamples + 1 &&
     163             :                 nBaseSamples != nExpectedBaseSamples)
     164             :             {
     165           1 :                 ReportError(
     166             :                     CE_Warning, CPLE_AppDefined,
     167             :                     "Wrong number of ExtraSamples : %d. %d were expected",
     168           1 :                     count, m_poGDS->m_nSamplesPerPixel - nExpectedBaseSamples);
     169             :             }
     170             : 
     171      791044 :             if (nBand > nBaseSamples && nBand - nBaseSamples - 1 < count &&
     172      791039 :                 (v[nBand - nBaseSamples - 1] == EXTRASAMPLE_ASSOCALPHA ||
     173      791023 :                  v[nBand - nBaseSamples - 1] == EXTRASAMPLE_UNASSALPHA))
     174             :             {
     175        1134 :                 if (v[nBand - nBaseSamples - 1] == EXTRASAMPLE_ASSOCALPHA)
     176          16 :                     m_oGTiffMDMD.SetMetadataItem("ALPHA", "PREMULTIPLIED",
     177             :                                                  GDAL_MDD_IMAGE_STRUCTURE);
     178        1134 :                 m_eBandInterp = GCI_AlphaBand;
     179             :             }
     180             :             else
     181      789910 :                 m_eBandInterp = GCI_Undefined;
     182             :         }
     183             :         else
     184             :         {
     185       65955 :             m_eBandInterp = GCI_Undefined;
     186             :         }
     187             :     }
     188             : 
     189             :     /* -------------------------------------------------------------------- */
     190             :     /*      Establish block size for strip or tiles.                        */
     191             :     /* -------------------------------------------------------------------- */
     192      902567 :     nBlockXSize = m_poGDS->m_nBlockXSize;
     193      902567 :     nBlockYSize = m_poGDS->m_nBlockYSize;
     194      902567 :     nRasterXSize = m_poGDS->nRasterXSize;
     195      902567 :     nRasterYSize = m_poGDS->nRasterYSize;
     196      902567 :     nBlocksPerRow = DIV_ROUND_UP(nRasterXSize, nBlockXSize);
     197      902567 :     nBlocksPerColumn = DIV_ROUND_UP(nRasterYSize, nBlockYSize);
     198      902567 : }
     199             : 
     200             : /************************************************************************/
     201             : /*                          ~GTiffRasterBand()                          */
     202             : /************************************************************************/
     203             : 
     204     1673022 : GTiffRasterBand::~GTiffRasterBand()
     205             : {
     206             :     // So that any future DropReferenceVirtualMem() will not try to access the
     207             :     // raster band object, but this would not conform to the advertised
     208             :     // contract.
     209      902556 :     if (!m_aSetPSelf.empty())
     210             :     {
     211           0 :         ReportError(CE_Warning, CPLE_AppDefined,
     212             :                     "Virtual memory objects still exist at GTiffRasterBand "
     213             :                     "destruction");
     214           0 :         std::set<GTiffRasterBand **>::iterator oIter = m_aSetPSelf.begin();
     215           0 :         for (; oIter != m_aSetPSelf.end(); ++oIter)
     216           0 :             *(*oIter) = nullptr;
     217             :     }
     218     1673022 : }
     219             : 
     220             : /************************************************************************/
     221             : /*                MayMultiBlockReadingBeMultiThreaded()                 */
     222             : /************************************************************************/
     223             : 
     224          12 : bool GTiffRasterBand::MayMultiBlockReadingBeMultiThreaded() const
     225             : {
     226          24 :     return m_poGDS->m_nDisableMultiThreadedRead == 0 &&
     227          16 :            m_poGDS->m_poThreadPool != nullptr &&
     228          16 :            m_poGDS->IsMultiThreadedReadCompatible();
     229             : }
     230             : 
     231             : /************************************************************************/
     232             : /*                             IRasterIO()                              */
     233             : /************************************************************************/
     234             : 
     235     5836220 : CPLErr GTiffRasterBand::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,
     236             :                                   int nXSize, int nYSize, void *pData,
     237             :                                   int nBufXSize, int nBufYSize,
     238             :                                   GDALDataType eBufType, GSpacing nPixelSpace,
     239             :                                   GSpacing nLineSpace,
     240             :                                   GDALRasterIOExtraArg *psExtraArg)
     241             : {
     242             : #if DEBUG_VERBOSE
     243             :     CPLDebug("GTiff", "RasterIO(%d, %d, %d, %d, %d, %d)", nXOff, nYOff, nXSize,
     244             :              nYSize, nBufXSize, nBufYSize);
     245             : #endif
     246             : 
     247             :     // Try to pass the request to the most appropriate overview dataset.
     248     5836220 :     if (nBufXSize < nXSize && nBufYSize < nYSize)
     249             :     {
     250      362419 :         int bTried = FALSE;
     251           0 :         std::unique_ptr<GTiffDataset::JPEGOverviewVisibilitySetter> setter;
     252      362419 :         if (psExtraArg->eResampleAlg == GRIORA_NearestNeighbour)
     253             :         {
     254      362051 :             setter = m_poGDS->MakeJPEGOverviewVisible();
     255      362051 :             CPL_IGNORE_RET_VAL(setter);
     256             :         }
     257      362419 :         const CPLErr eErr = TryOverviewRasterIO(
     258             :             eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, nBufYSize,
     259             :             eBufType, nPixelSpace, nLineSpace, psExtraArg, &bTried);
     260      362419 :         if (bTried)
     261          46 :             return eErr;
     262             :     }
     263             : 
     264     5836170 :     if (m_poGDS->m_eVirtualMemIOUsage != GTiffDataset::VirtualMemIOEnum::NO)
     265             :     {
     266         904 :         const int nErr = m_poGDS->VirtualMemIO(
     267             :             eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, nBufYSize,
     268         452 :             eBufType, 1, &nBand, nPixelSpace, nLineSpace, 0, psExtraArg);
     269         452 :         if (nErr >= 0)
     270         363 :             return static_cast<CPLErr>(nErr);
     271             :     }
     272     5835810 :     if (m_poGDS->m_bDirectIO)
     273             :     {
     274             :         int nErr =
     275        2523 :             DirectIO(eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize,
     276             :                      nBufYSize, eBufType, nPixelSpace, nLineSpace, psExtraArg);
     277        2523 :         if (nErr >= 0)
     278        1410 :             return static_cast<CPLErr>(nErr);
     279             :     }
     280             : 
     281     5834400 :     bool bCanUseMultiThreadedRead = false;
     282     5834120 :     if (m_poGDS->m_nDisableMultiThreadedRead == 0 && eRWFlag == GF_Read &&
     283     3372910 :         m_poGDS->m_poThreadPool != nullptr && nXSize == nBufXSize &&
     284    11668500 :         nYSize == nBufYSize && m_poGDS->IsMultiThreadedReadCompatible())
     285             :     {
     286         127 :         const int nBlockX1 = nXOff / nBlockXSize;
     287         127 :         const int nBlockY1 = nYOff / nBlockYSize;
     288         127 :         const int nBlockX2 = (nXOff + nXSize - 1) / nBlockXSize;
     289         127 :         const int nBlockY2 = (nYOff + nYSize - 1) / nBlockYSize;
     290         127 :         const int nXBlocks = nBlockX2 - nBlockX1 + 1;
     291         127 :         const int nYBlocks = nBlockY2 - nBlockY1 + 1;
     292         127 :         if (nXBlocks > 1 || nYBlocks > 1)
     293             :         {
     294         112 :             if (psExtraArg->bFloatingPointWindowValidity)
     295             :             {
     296             :                 const char *pszNumThreads =
     297           3 :                     CPLGetConfigOption("GDAL_NUM_THREADS", nullptr);
     298           3 :                 if (pszNumThreads != nullptr)
     299             :                 {
     300           3 :                     if (!m_bWarnedAboutMultiThreadedReadDisabled)
     301             :                     {
     302           3 :                         m_bWarnedAboutMultiThreadedReadDisabled = true;
     303           3 :                         ReportError(
     304             :                             CE_Warning, CPLE_AppDefined,
     305             :                             "Multi-threaded reading is disabled because a "
     306             :                             "floating point window is requested.");
     307             :                     }
     308             :                 }
     309             :             }
     310             :             else
     311             :             {
     312         109 :                 bCanUseMultiThreadedRead = true;
     313             :             }
     314             :         }
     315             :     }
     316             : 
     317             :     // Cleanup data cached by below CacheMultiRange() call.
     318             :     struct BufferedDataFreer
     319             :     {
     320             :         void *m_pBufferedData = nullptr;
     321             :         TIFF *m_hTIFF = nullptr;
     322             : 
     323         255 :         void Init(void *pBufferedData, TIFF *hTIFF)
     324             :         {
     325         255 :             m_pBufferedData = pBufferedData;
     326         255 :             m_hTIFF = hTIFF;
     327         255 :         }
     328             : 
     329     5834400 :         ~BufferedDataFreer()
     330     5834400 :         {
     331     5834400 :             if (m_pBufferedData)
     332             :             {
     333          35 :                 VSIFree(m_pBufferedData);
     334          35 :                 VSI_TIFFSetCachedRanges(TIFFClientdata(m_hTIFF), 0, nullptr,
     335             :                                         nullptr, nullptr);
     336             :             }
     337     5834400 :         }
     338             :     };
     339             : 
     340             :     // bufferedDataFreer must be left in this scope !
     341     5834400 :     BufferedDataFreer bufferedDataFreer;
     342             : 
     343     8704940 :     if (m_poGDS->eAccess == GA_ReadOnly && eRWFlag == GF_Read &&
     344     2870540 :         m_poGDS->HasOptimizedReadMultiRange())
     345             :     {
     346         263 :         if (bCanUseMultiThreadedRead &&
     347           4 :             VSI_TIFFGetVSILFile(TIFFClientdata(m_poGDS->m_hTIFF))->HasPRead())
     348             :         {
     349             :             // use the multi-threaded implementation rather than the multi-range
     350             :             // one
     351             :         }
     352             :         else
     353             :         {
     354         255 :             bCanUseMultiThreadedRead = false;
     355         255 :             GTiffDataset *poDSForCache = m_poGDS;
     356         255 :             int nBandForCache = nBand;
     357         255 :             if (!m_poGDS->m_bStreamingIn && m_poGDS->m_bBlockOrderRowMajor &&
     358         110 :                 m_poGDS->m_bLeaderSizeAsUInt4 &&
     359         110 :                 m_poGDS->m_bMaskInterleavedWithImagery &&
     360         100 :                 m_poGDS->m_poImageryDS)
     361             :             {
     362          62 :                 poDSForCache = m_poGDS->m_poImageryDS;
     363          62 :                 nBandForCache = 1;
     364             :             }
     365         255 :             bufferedDataFreer.Init(
     366             :                 poDSForCache->CacheMultiRange(nXOff, nYOff, nXSize, nYSize,
     367             :                                               nBufXSize, nBufYSize,
     368             :                                               &nBandForCache, 1, psExtraArg),
     369             :                 poDSForCache->m_hTIFF);
     370             :         }
     371             :     }
     372             : 
     373     5834400 :     if (eRWFlag == GF_Read && nXSize == nBufXSize && nYSize == nBufYSize)
     374             :     {
     375     2975660 :         const int nBlockX1 = nXOff / nBlockXSize;
     376     2975660 :         const int nBlockY1 = nYOff / nBlockYSize;
     377     2975660 :         const int nBlockX2 = (nXOff + nXSize - 1) / nBlockXSize;
     378     2975660 :         const int nBlockY2 = (nYOff + nYSize - 1) / nBlockYSize;
     379     2975660 :         const int nXBlocks = nBlockX2 - nBlockX1 + 1;
     380     2975660 :         const int nYBlocks = nBlockY2 - nBlockY1 + 1;
     381             : 
     382     2975660 :         if (bCanUseMultiThreadedRead)
     383             :         {
     384         218 :             return m_poGDS->MultiThreadedRead(nXOff, nYOff, nXSize, nYSize,
     385         109 :                                               pData, eBufType, 1, &nBand,
     386         109 :                                               nPixelSpace, nLineSpace, 0);
     387             :         }
     388     2975550 :         else if (m_poGDS->nBands != 1 &&
     389      748686 :                  m_poGDS->m_nPlanarConfig == PLANARCONFIG_CONTIG)
     390             :         {
     391             :             const GIntBig nRequiredMem =
     392      683641 :                 static_cast<GIntBig>(m_poGDS->nBands) * nXBlocks * nYBlocks *
     393      683641 :                 nBlockXSize * nBlockYSize * GDALGetDataTypeSizeBytes(eDataType);
     394      683641 :             if (nRequiredMem > GDALGetCacheMax64())
     395             :             {
     396        9061 :                 if (!m_poGDS->m_bHasWarnedDisableAggressiveBandCaching)
     397             :                 {
     398          15 :                     CPLDebug("GTiff",
     399             :                              "Disable aggressive band caching. "
     400             :                              "Cache not big enough. "
     401             :                              "At least " CPL_FRMT_GIB " bytes necessary",
     402             :                              nRequiredMem);
     403          15 :                     m_poGDS->m_bHasWarnedDisableAggressiveBandCaching = true;
     404             :                 }
     405        9061 :                 m_poGDS->m_bLoadingOtherBands = true;
     406             :             }
     407     2975550 :         }
     408             :     }
     409             : 
     410             :     // Write optimization when writing whole blocks, by-passing the block cache.
     411             :     // We require the block cache to be non instantiated to simplify things
     412             :     // (otherwise we might need to evict corresponding existing blocks from the
     413             :     // block cache).
     414     5319950 :     else if (eRWFlag == GF_Write &&
     415             :              // Could be extended to "odd bit" case, but more work
     416     2461220 :              m_poGDS->m_nBitsPerSample == GDALGetDataTypeSizeBits(eDataType) &&
     417     2309030 :              nXSize == nBufXSize && nYSize == nBufYSize && !HasBlockCache() &&
     418       49961 :              !m_poGDS->m_bLoadedBlockDirty &&
     419       49949 :              (m_poGDS->nBands == 1 ||
     420        4544 :               m_poGDS->m_nPlanarConfig == PLANARCONFIG_SEPARATE) &&
     421       48835 :              !m_poGDS->m_bLeaderSizeAsUInt4 && (nXOff % nBlockXSize) == 0 &&
     422       48788 :              (nYOff % nBlockYSize) == 0 &&
     423     5368560 :              (nXOff + nXSize == nRasterXSize || (nXSize % nBlockXSize) == 0) &&
     424       48606 :              (nYOff + nYSize == nRasterYSize || (nYSize % nBlockYSize) == 0))
     425             :     {
     426       48248 :         m_poGDS->Crystalize();
     427             : 
     428       48248 :         if (m_poGDS->m_bDebugDontWriteBlocks)
     429           0 :             return CE_None;
     430             : 
     431       48248 :         const int nDTSize = GDALGetDataTypeSizeBytes(eDataType);
     432       48248 :         if (nXSize == nBlockXSize && nYSize == nBlockYSize &&
     433       44134 :             eBufType == eDataType && nPixelSpace == nDTSize &&
     434       42393 :             nLineSpace == nPixelSpace * nBlockXSize)
     435             :         {
     436             :             // If writing one single block with the right data type and layout,
     437             :             // we don't need a temporary buffer
     438             :             const int nBlockId =
     439       42393 :                 ComputeBlockId(nXOff / nBlockXSize, nYOff / nBlockYSize);
     440       42393 :             return m_poGDS->WriteEncodedTileOrStrip(
     441       42393 :                 nBlockId, pData, /* bPreserveDataBuffer= */ true);
     442             :         }
     443             : 
     444             :         // Make sure m_poGDS->m_pabyBlockBuf is allocated.
     445             :         // We could actually use any temporary buffer
     446        5855 :         if (m_poGDS->LoadBlockBuf(/* nBlockId = */ -1,
     447        5855 :                                   /* bReadFromDisk = */ false) != CE_None)
     448             :         {
     449           0 :             return CE_Failure;
     450             :         }
     451             : 
     452             :         // Iterate over all blocks defined by
     453             :         // [nXOff, nXOff+nXSize[ * [nYOff, nYOff+nYSize[
     454             :         // and write their content as a nBlockXSize x nBlockYSize strile
     455             :         // in a temporary buffer, before calling WriteEncodedTileOrStrip()
     456             :         // on it
     457        5855 :         const int nYBlockStart = nYOff / nBlockYSize;
     458        5855 :         const int nYBlockEnd = 1 + (nYOff + nYSize - 1) / nBlockYSize;
     459        5855 :         const int nXBlockStart = nXOff / nBlockXSize;
     460        5855 :         const int nXBlockEnd = 1 + (nXOff + nXSize - 1) / nBlockXSize;
     461       54903 :         for (int nYBlock = nYBlockStart; nYBlock < nYBlockEnd; ++nYBlock)
     462             :         {
     463             :             const int nValidY =
     464       49048 :                 std::min(nBlockYSize, nRasterYSize - nYBlock * nBlockYSize);
     465      114190 :             for (int nXBlock = nXBlockStart; nXBlock < nXBlockEnd; ++nXBlock)
     466             :             {
     467             :                 const int nValidX =
     468       65142 :                     std::min(nBlockXSize, nRasterXSize - nXBlock * nBlockXSize);
     469       65142 :                 if (nValidY < nBlockYSize || nValidX < nBlockXSize)
     470             :                 {
     471             :                     // Make sure padding bytes at the right/bottom of the
     472             :                     // tile are initialized to zero.
     473        3071 :                     memset(m_poGDS->m_pabyBlockBuf, 0,
     474        3071 :                            static_cast<size_t>(nBlockXSize) * nBlockYSize *
     475        3071 :                                nDTSize);
     476             :                 }
     477       65142 :                 const GByte *pabySrcData =
     478             :                     static_cast<const GByte *>(pData) +
     479       65142 :                     static_cast<size_t>(nYBlock - nYBlockStart) * nBlockYSize *
     480       65142 :                         nLineSpace +
     481       65142 :                     static_cast<size_t>(nXBlock - nXBlockStart) * nBlockXSize *
     482       65142 :                         nPixelSpace;
     483     3862850 :                 for (int iY = 0; iY < nValidY; ++iY)
     484             :                 {
     485     3797710 :                     GDALCopyWords64(
     486     3797710 :                         pabySrcData + static_cast<size_t>(iY) * nLineSpace,
     487             :                         eBufType, static_cast<int>(nPixelSpace),
     488     3797710 :                         m_poGDS->m_pabyBlockBuf +
     489     3797710 :                             static_cast<size_t>(iY) * nBlockXSize * nDTSize,
     490             :                         eDataType, nDTSize, nValidX);
     491             :                 }
     492       65142 :                 const int nBlockId = ComputeBlockId(nXBlock, nYBlock);
     493      130284 :                 if (m_poGDS->WriteEncodedTileOrStrip(
     494       65142 :                         nBlockId, m_poGDS->m_pabyBlockBuf,
     495       65142 :                         /* bPreserveDataBuffer= */ false) != CE_None)
     496             :                 {
     497           0 :                     return CE_Failure;
     498             :                 }
     499             :             }
     500             :         }
     501        5855 :         return CE_None;
     502             :     }
     503             : 
     504           0 :     std::unique_ptr<GTiffDataset::JPEGOverviewVisibilitySetter> setter;
     505     5786040 :     if (psExtraArg->eResampleAlg == GRIORA_NearestNeighbour)
     506             :     {
     507     5783540 :         setter = m_poGDS->MakeJPEGOverviewVisible();
     508     5783540 :         CPL_IGNORE_RET_VAL(setter);
     509             :     }
     510     5786040 :     const CPLErr eErr = GDALPamRasterBand::IRasterIO(
     511             :         eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, nBufYSize,
     512             :         eBufType, nPixelSpace, nLineSpace, psExtraArg);
     513             : 
     514     5786040 :     m_poGDS->m_bLoadingOtherBands = false;
     515             : 
     516     5786040 :     return eErr;
     517             : }
     518             : 
     519             : /************************************************************************/
     520             : /*                           ComputeBlockId()                           */
     521             : /************************************************************************/
     522             : 
     523             : /** Computes the TIFF block identifier from the tile coordinate, band
     524             :  * number and planar configuration.
     525             :  */
     526     3315870 : int GTiffRasterBand::ComputeBlockId(int nBlockXOff, int nBlockYOff) const
     527             : {
     528     3315870 :     const int nBlockId = nBlockXOff + nBlockYOff * nBlocksPerRow;
     529     3315870 :     if (m_poGDS->m_nPlanarConfig == PLANARCONFIG_SEPARATE)
     530             :     {
     531      666228 :         return nBlockId + (nBand - 1) * m_poGDS->m_nBlocksPerBand;
     532             :     }
     533     2649640 :     return nBlockId;
     534             : }

Generated by: LCOV version 1.14