LCOV - code coverage report
Current view: top level - frmts/gtiff - gtiffdataset_write.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 3891 4249 91.6 %
Date: 2026-02-23 15:56:29 Functions: 110 139 79.1 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GeoTIFF Driver
       4             :  * Purpose:  Write/set operations on GTiffDataset
       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 "gtiffdataset.h"
      15             : #include "gtiffrasterband.h"
      16             : #include "gtiffoddbitsband.h"
      17             : #include "gtiffjpegoverviewds.h"
      18             : 
      19             : #include <cassert>
      20             : #include <cerrno>
      21             : 
      22             : #include <algorithm>
      23             : #include <cmath>
      24             : #include <limits>
      25             : #include <memory>
      26             : #include <mutex>
      27             : #include <set>
      28             : #include <string>
      29             : #include <tuple>
      30             : #include <utility>
      31             : 
      32             : #include "cpl_error.h"
      33             : #include "cpl_error_internal.h"  // CPLErrorHandlerAccumulatorStruct
      34             : #include "cpl_float.h"
      35             : #include "cpl_md5.h"
      36             : #include "cpl_vsi.h"
      37             : #include "cpl_vsi_virtual.h"
      38             : #include "cpl_worker_thread_pool.h"
      39             : #include "fetchbufferdirectio.h"
      40             : #include "gdal_mdreader.h"  // GDALWriteRPCTXTFile()
      41             : #include "gdal_priv.h"
      42             : #include "gdal_priv_templates.hpp"  // GDALIsValueInRange<>
      43             : #include "gdal_thread_pool.h"       // GDALGetGlobalThreadPool()
      44             : #include "geovalues.h"              // RasterPixelIsPoint
      45             : #include "gt_jpeg_copy.h"
      46             : #include "gt_overview.h"  // GTIFFBuildOverviewMetadata()
      47             : #include "quant_table_md5sum.h"
      48             : #include "quant_table_md5sum_jpeg9e.h"
      49             : #include "tif_jxl.h"
      50             : #include "tifvsi.h"
      51             : #include "xtiffio.h"
      52             : 
      53             : #if LIFFLIB_VERSION > 20230908 || defined(INTERNAL_LIBTIFF)
      54             : /* libtiff < 4.6.1 doesn't generate a LERC mask for multi-band contig configuration */
      55             : #define LIBTIFF_MULTIBAND_LERC_NAN_OK
      56             : #endif
      57             : 
      58             : static const int knGTIFFJpegTablesModeDefault = JPEGTABLESMODE_QUANT;
      59             : 
      60             : static constexpr const char szPROFILE_BASELINE[] = "BASELINE";
      61             : static constexpr const char szPROFILE_GeoTIFF[] = "GeoTIFF";
      62             : static constexpr const char szPROFILE_GDALGeoTIFF[] = "GDALGeoTIFF";
      63             : 
      64             : // Due to libgeotiff/xtiff.c declaring TIFFTAG_GEOTIEPOINTS with field_readcount
      65             : // and field_writecount == -1 == TIFF_VARIABLE, we are limited to writing
      66             : // 65535 values in that tag. That could potentially be overcome by changing the tag
      67             : // declaration to using TIFF_VARIABLE2 where the count is a uint32_t.
      68             : constexpr int knMAX_GCP_COUNT =
      69             :     static_cast<int>(std::numeric_limits<uint16_t>::max() / 6);
      70             : 
      71             : enum
      72             : {
      73             :     ENDIANNESS_NATIVE,
      74             :     ENDIANNESS_LITTLE,
      75             :     ENDIANNESS_BIG
      76             : };
      77             : 
      78       17698 : static signed char GTiffGetWebPLevel(CSLConstList papszOptions)
      79             : {
      80       17698 :     int nWebPLevel = DEFAULT_WEBP_LEVEL;
      81       17698 :     const char *pszValue = CSLFetchNameValue(papszOptions, "WEBP_LEVEL");
      82       17698 :     if (pszValue != nullptr)
      83             :     {
      84          51 :         nWebPLevel = atoi(pszValue);
      85          51 :         if (!(nWebPLevel >= 1 && nWebPLevel <= 100))
      86             :         {
      87           0 :             CPLError(CE_Warning, CPLE_IllegalArg,
      88             :                      "WEBP_LEVEL=%s value not recognised, ignoring.", pszValue);
      89           0 :             nWebPLevel = DEFAULT_WEBP_LEVEL;
      90             :         }
      91             :     }
      92       17698 :     return static_cast<signed char>(nWebPLevel);
      93             : }
      94             : 
      95       17704 : static bool GTiffGetWebPLossless(CSLConstList papszOptions)
      96             : {
      97       17704 :     return CPLFetchBool(papszOptions, "WEBP_LOSSLESS", false);
      98             : }
      99             : 
     100       17770 : static double GTiffGetLERCMaxZError(CSLConstList papszOptions)
     101             : {
     102       17770 :     return CPLAtof(CSLFetchNameValueDef(papszOptions, "MAX_Z_ERROR", "0.0"));
     103             : }
     104             : 
     105        7922 : static double GTiffGetLERCMaxZErrorOverview(CSLConstList papszOptions)
     106             : {
     107        7922 :     return CPLAtof(CSLFetchNameValueDef(
     108             :         papszOptions, "MAX_Z_ERROR_OVERVIEW",
     109        7922 :         CSLFetchNameValueDef(papszOptions, "MAX_Z_ERROR", "0.0")));
     110             : }
     111             : 
     112             : #if HAVE_JXL
     113       17774 : static bool GTiffGetJXLLossless(CSLConstList papszOptions,
     114             :                                 bool *pbIsSpecified = nullptr)
     115             : {
     116       17774 :     const char *pszVal = CSLFetchNameValue(papszOptions, "JXL_LOSSLESS");
     117       17774 :     if (pbIsSpecified)
     118        9848 :         *pbIsSpecified = pszVal != nullptr;
     119       17774 :     return pszVal == nullptr || CPLTestBool(pszVal);
     120             : }
     121             : 
     122       17774 : static uint32_t GTiffGetJXLEffort(CSLConstList papszOptions)
     123             : {
     124       17774 :     return atoi(CSLFetchNameValueDef(papszOptions, "JXL_EFFORT", "5"));
     125             : }
     126             : 
     127       17692 : static float GTiffGetJXLDistance(CSLConstList papszOptions,
     128             :                                  bool *pbIsSpecified = nullptr)
     129             : {
     130       17692 :     const char *pszVal = CSLFetchNameValue(papszOptions, "JXL_DISTANCE");
     131       17692 :     if (pbIsSpecified)
     132        9848 :         *pbIsSpecified = pszVal != nullptr;
     133       17692 :     return pszVal == nullptr ? 1.0f : static_cast<float>(CPLAtof(pszVal));
     134             : }
     135             : 
     136       17774 : static float GTiffGetJXLAlphaDistance(CSLConstList papszOptions,
     137             :                                       bool *pbIsSpecified = nullptr)
     138             : {
     139       17774 :     const char *pszVal = CSLFetchNameValue(papszOptions, "JXL_ALPHA_DISTANCE");
     140       17774 :     if (pbIsSpecified)
     141        9848 :         *pbIsSpecified = pszVal != nullptr;
     142       17774 :     return pszVal == nullptr ? -1.0f : static_cast<float>(CPLAtof(pszVal));
     143             : }
     144             : 
     145             : #endif
     146             : 
     147             : /************************************************************************/
     148             : /*                           FillEmptyTiles()                           */
     149             : /************************************************************************/
     150             : 
     151        8104 : CPLErr GTiffDataset::FillEmptyTiles()
     152             : 
     153             : {
     154             :     /* -------------------------------------------------------------------- */
     155             :     /*      How many blocks are there in this file?                         */
     156             :     /* -------------------------------------------------------------------- */
     157       16208 :     const int nBlockCount = m_nPlanarConfig == PLANARCONFIG_SEPARATE
     158        8104 :                                 ? m_nBlocksPerBand * nBands
     159             :                                 : m_nBlocksPerBand;
     160             : 
     161             :     /* -------------------------------------------------------------------- */
     162             :     /*      Fetch block maps.                                               */
     163             :     /* -------------------------------------------------------------------- */
     164        8104 :     toff_t *panByteCounts = nullptr;
     165             : 
     166        8104 :     if (TIFFIsTiled(m_hTIFF))
     167        1125 :         TIFFGetField(m_hTIFF, TIFFTAG_TILEBYTECOUNTS, &panByteCounts);
     168             :     else
     169        6979 :         TIFFGetField(m_hTIFF, TIFFTAG_STRIPBYTECOUNTS, &panByteCounts);
     170             : 
     171        8104 :     if (panByteCounts == nullptr)
     172             :     {
     173             :         // Got here with libtiff 3.9.3 and tiff_write_8 test.
     174           0 :         ReportError(CE_Failure, CPLE_AppDefined,
     175             :                     "FillEmptyTiles() failed because panByteCounts == NULL");
     176           0 :         return CE_Failure;
     177             :     }
     178             : 
     179             :     /* -------------------------------------------------------------------- */
     180             :     /*      Prepare a blank data buffer to write for uninitialized blocks.  */
     181             :     /* -------------------------------------------------------------------- */
     182             :     const GPtrDiff_t nBlockBytes =
     183        8104 :         TIFFIsTiled(m_hTIFF) ? static_cast<GPtrDiff_t>(TIFFTileSize(m_hTIFF))
     184        6979 :                              : static_cast<GPtrDiff_t>(TIFFStripSize(m_hTIFF));
     185             : 
     186        8104 :     GByte *pabyData = static_cast<GByte *>(VSI_CALLOC_VERBOSE(nBlockBytes, 1));
     187        8104 :     if (pabyData == nullptr)
     188             :     {
     189           0 :         return CE_Failure;
     190             :     }
     191             : 
     192             :     // Force tiles completely filled with the nodata value to be written.
     193        8104 :     m_bWriteEmptyTiles = true;
     194             : 
     195             :     /* -------------------------------------------------------------------- */
     196             :     /*      If set, fill data buffer with no data value.                    */
     197             :     /* -------------------------------------------------------------------- */
     198        8104 :     if ((m_bNoDataSet && m_dfNoDataValue != 0.0) ||
     199        7836 :         (m_bNoDataSetAsInt64 && m_nNoDataValueInt64 != 0) ||
     200        7831 :         (m_bNoDataSetAsUInt64 && m_nNoDataValueUInt64 != 0))
     201             :     {
     202         278 :         const GDALDataType eDataType = GetRasterBand(1)->GetRasterDataType();
     203         278 :         const int nDataTypeSize = GDALGetDataTypeSizeBytes(eDataType);
     204         278 :         if (nDataTypeSize &&
     205         278 :             nDataTypeSize * 8 == static_cast<int>(m_nBitsPerSample))
     206             :         {
     207         267 :             if (m_bNoDataSetAsInt64)
     208             :             {
     209           6 :                 GDALCopyWords64(&m_nNoDataValueInt64, GDT_Int64, 0, pabyData,
     210             :                                 eDataType, nDataTypeSize,
     211           6 :                                 nBlockBytes / nDataTypeSize);
     212             :             }
     213         261 :             else if (m_bNoDataSetAsUInt64)
     214             :             {
     215           5 :                 GDALCopyWords64(&m_nNoDataValueUInt64, GDT_UInt64, 0, pabyData,
     216             :                                 eDataType, nDataTypeSize,
     217           5 :                                 nBlockBytes / nDataTypeSize);
     218             :             }
     219             :             else
     220             :             {
     221         256 :                 double dfNoData = m_dfNoDataValue;
     222         256 :                 GDALCopyWords64(&dfNoData, GDT_Float64, 0, pabyData, eDataType,
     223         256 :                                 nDataTypeSize, nBlockBytes / nDataTypeSize);
     224         267 :             }
     225             :         }
     226          11 :         else if (nDataTypeSize)
     227             :         {
     228             :             // Handle non power-of-two depths.
     229             :             // Ideally make a packed buffer, but that is a bit tedious,
     230             :             // so use the normal I/O interfaces.
     231             : 
     232          11 :             CPLFree(pabyData);
     233             : 
     234          11 :             pabyData = static_cast<GByte *>(VSI_MALLOC3_VERBOSE(
     235             :                 m_nBlockXSize, m_nBlockYSize, nDataTypeSize));
     236          11 :             if (pabyData == nullptr)
     237           0 :                 return CE_Failure;
     238          11 :             if (m_bNoDataSetAsInt64)
     239             :             {
     240           0 :                 GDALCopyWords64(&m_nNoDataValueInt64, GDT_Int64, 0, pabyData,
     241             :                                 eDataType, nDataTypeSize,
     242           0 :                                 static_cast<GPtrDiff_t>(m_nBlockXSize) *
     243           0 :                                     m_nBlockYSize);
     244             :             }
     245          11 :             else if (m_bNoDataSetAsUInt64)
     246             :             {
     247           0 :                 GDALCopyWords64(&m_nNoDataValueUInt64, GDT_UInt64, 0, pabyData,
     248             :                                 eDataType, nDataTypeSize,
     249           0 :                                 static_cast<GPtrDiff_t>(m_nBlockXSize) *
     250           0 :                                     m_nBlockYSize);
     251             :             }
     252             :             else
     253             :             {
     254          11 :                 GDALCopyWords64(&m_dfNoDataValue, GDT_Float64, 0, pabyData,
     255             :                                 eDataType, nDataTypeSize,
     256          11 :                                 static_cast<GPtrDiff_t>(m_nBlockXSize) *
     257          11 :                                     m_nBlockYSize);
     258             :             }
     259          11 :             CPLErr eErr = CE_None;
     260          46 :             for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
     261             :             {
     262          35 :                 if (panByteCounts[iBlock] == 0)
     263             :                 {
     264          18 :                     if (m_nPlanarConfig == PLANARCONFIG_SEPARATE || nBands == 1)
     265             :                     {
     266          24 :                         if (GetRasterBand(1 + iBlock / m_nBlocksPerBand)
     267          12 :                                 ->WriteBlock((iBlock % m_nBlocksPerBand) %
     268          12 :                                                  m_nBlocksPerRow,
     269          12 :                                              (iBlock % m_nBlocksPerBand) /
     270          12 :                                                  m_nBlocksPerRow,
     271          12 :                                              pabyData) != CE_None)
     272             :                         {
     273           0 :                             eErr = CE_Failure;
     274             :                         }
     275             :                     }
     276             :                     else
     277             :                     {
     278             :                         // In contig case, don't directly call WriteBlock(), as
     279             :                         // it could cause useless decompression-recompression.
     280           6 :                         const int nXOff =
     281           6 :                             (iBlock % m_nBlocksPerRow) * m_nBlockXSize;
     282           6 :                         const int nYOff =
     283           6 :                             (iBlock / m_nBlocksPerRow) * m_nBlockYSize;
     284           6 :                         const int nXSize =
     285           6 :                             (nXOff + m_nBlockXSize <= nRasterXSize)
     286           6 :                                 ? m_nBlockXSize
     287           2 :                                 : nRasterXSize - nXOff;
     288           6 :                         const int nYSize =
     289           6 :                             (nYOff + m_nBlockYSize <= nRasterYSize)
     290           6 :                                 ? m_nBlockYSize
     291           3 :                                 : nRasterYSize - nYOff;
     292          18 :                         for (int iBand = 1; iBand <= nBands; ++iBand)
     293             :                         {
     294          12 :                             if (GetRasterBand(iBand)->RasterIO(
     295             :                                     GF_Write, nXOff, nYOff, nXSize, nYSize,
     296             :                                     pabyData, nXSize, nYSize, eDataType, 0, 0,
     297          12 :                                     nullptr) != CE_None)
     298             :                             {
     299           0 :                                 eErr = CE_Failure;
     300             :                             }
     301             :                         }
     302             :                     }
     303             :                 }
     304             :             }
     305          11 :             CPLFree(pabyData);
     306          11 :             return eErr;
     307         267 :         }
     308             :     }
     309             : 
     310             :     /* -------------------------------------------------------------------- */
     311             :     /*      When we must fill with zeroes, try to create non-sparse file    */
     312             :     /*      w.r.t TIFF spec ... as a sparse file w.r.t filesystem, ie by    */
     313             :     /*      seeking to end of file instead of writing zero blocks.          */
     314             :     /* -------------------------------------------------------------------- */
     315        7826 :     else if (m_nCompression == COMPRESSION_NONE && (m_nBitsPerSample % 8) == 0)
     316             :     {
     317        6282 :         CPLErr eErr = CE_None;
     318             :         // Only use libtiff to write the first sparse block to ensure that it
     319             :         // will serialize offset and count arrays back to disk.
     320        6282 :         int nCountBlocksToZero = 0;
     321     2321160 :         for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
     322             :         {
     323     2314880 :             if (panByteCounts[iBlock] == 0)
     324             :             {
     325     2219650 :                 if (nCountBlocksToZero == 0)
     326             :                 {
     327        1111 :                     const bool bWriteEmptyTilesBak = m_bWriteEmptyTiles;
     328        1111 :                     m_bWriteEmptyTiles = true;
     329        1111 :                     const bool bOK = WriteEncodedTileOrStrip(iBlock, pabyData,
     330        1111 :                                                              FALSE) == CE_None;
     331        1111 :                     m_bWriteEmptyTiles = bWriteEmptyTilesBak;
     332        1111 :                     if (!bOK)
     333             :                     {
     334           2 :                         eErr = CE_Failure;
     335           2 :                         break;
     336             :                     }
     337             :                 }
     338     2219640 :                 nCountBlocksToZero++;
     339             :             }
     340             :         }
     341        6282 :         CPLFree(pabyData);
     342             : 
     343        6282 :         --nCountBlocksToZero;
     344             : 
     345             :         // And then seek to end of file for other ones.
     346        6282 :         if (nCountBlocksToZero > 0)
     347             :         {
     348         336 :             toff_t *panByteOffsets = nullptr;
     349             : 
     350         336 :             if (TIFFIsTiled(m_hTIFF))
     351          92 :                 TIFFGetField(m_hTIFF, TIFFTAG_TILEOFFSETS, &panByteOffsets);
     352             :             else
     353         244 :                 TIFFGetField(m_hTIFF, TIFFTAG_STRIPOFFSETS, &panByteOffsets);
     354             : 
     355         336 :             if (panByteOffsets == nullptr)
     356             :             {
     357           0 :                 ReportError(
     358             :                     CE_Failure, CPLE_AppDefined,
     359             :                     "FillEmptyTiles() failed because panByteOffsets == NULL");
     360           0 :                 return CE_Failure;
     361             :             }
     362             : 
     363         336 :             VSILFILE *fpTIF = VSI_TIFFGetVSILFile(TIFFClientdata(m_hTIFF));
     364         336 :             VSIFSeekL(fpTIF, 0, SEEK_END);
     365         336 :             const vsi_l_offset nOffset = VSIFTellL(fpTIF);
     366             : 
     367         336 :             vsi_l_offset iBlockToZero = 0;
     368     2227750 :             for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
     369             :             {
     370     2227410 :                 if (panByteCounts[iBlock] == 0)
     371             :                 {
     372     2218540 :                     panByteOffsets[iBlock] = static_cast<toff_t>(
     373     2218540 :                         nOffset + iBlockToZero * nBlockBytes);
     374     2218540 :                     panByteCounts[iBlock] = nBlockBytes;
     375     2218540 :                     iBlockToZero++;
     376             :                 }
     377             :             }
     378         336 :             CPLAssert(iBlockToZero ==
     379             :                       static_cast<vsi_l_offset>(nCountBlocksToZero));
     380             : 
     381         336 :             if (VSIFTruncateL(fpTIF, nOffset + iBlockToZero * nBlockBytes) != 0)
     382             :             {
     383           0 :                 eErr = CE_Failure;
     384           0 :                 ReportError(CE_Failure, CPLE_FileIO,
     385             :                             "Cannot initialize empty blocks");
     386             :             }
     387             :         }
     388             : 
     389        6282 :         return eErr;
     390             :     }
     391             : 
     392             :     /* -------------------------------------------------------------------- */
     393             :     /*      Check all blocks, writing out data for uninitialized blocks.    */
     394             :     /* -------------------------------------------------------------------- */
     395             : 
     396        1811 :     GByte *pabyRaw = nullptr;
     397        1811 :     vsi_l_offset nRawSize = 0;
     398        1811 :     CPLErr eErr = CE_None;
     399       56287 :     for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
     400             :     {
     401       54483 :         if (panByteCounts[iBlock] == 0)
     402             :         {
     403       17464 :             if (pabyRaw == nullptr)
     404             :             {
     405       10159 :                 if (WriteEncodedTileOrStrip(iBlock, pabyData, FALSE) != CE_None)
     406             :                 {
     407           7 :                     eErr = CE_Failure;
     408           7 :                     break;
     409             :                 }
     410             : 
     411       10152 :                 vsi_l_offset nOffset = 0;
     412       10152 :                 if (!IsBlockAvailable(iBlock, &nOffset, &nRawSize, nullptr))
     413           0 :                     break;
     414             : 
     415             :                 // When using compression, get back the compressed block
     416             :                 // so we can use the raw API to write it faster.
     417       10152 :                 if (m_nCompression != COMPRESSION_NONE)
     418             :                 {
     419             :                     pabyRaw = static_cast<GByte *>(
     420         478 :                         VSI_MALLOC_VERBOSE(static_cast<size_t>(nRawSize)));
     421         478 :                     if (pabyRaw)
     422             :                     {
     423             :                         VSILFILE *fp =
     424         478 :                             VSI_TIFFGetVSILFile(TIFFClientdata(m_hTIFF));
     425         478 :                         const vsi_l_offset nCurOffset = VSIFTellL(fp);
     426         478 :                         VSIFSeekL(fp, nOffset, SEEK_SET);
     427         478 :                         VSIFReadL(pabyRaw, 1, static_cast<size_t>(nRawSize),
     428             :                                   fp);
     429         478 :                         VSIFSeekL(fp, nCurOffset, SEEK_SET);
     430             :                     }
     431             :                 }
     432             :             }
     433             :             else
     434             :             {
     435        7305 :                 WriteRawStripOrTile(iBlock, pabyRaw,
     436             :                                     static_cast<GPtrDiff_t>(nRawSize));
     437             :             }
     438             :         }
     439             :     }
     440             : 
     441        1811 :     CPLFree(pabyData);
     442        1811 :     VSIFree(pabyRaw);
     443        1811 :     return eErr;
     444             : }
     445             : 
     446             : /************************************************************************/
     447             : /*                           HasOnlyNoData()                            */
     448             : /************************************************************************/
     449             : 
     450       42785 : bool GTiffDataset::HasOnlyNoData(const void *pBuffer, int nWidth, int nHeight,
     451             :                                  int nLineStride, int nComponents)
     452             : {
     453       42785 :     if (m_nSampleFormat == SAMPLEFORMAT_COMPLEXINT ||
     454       42785 :         m_nSampleFormat == SAMPLEFORMAT_COMPLEXIEEEFP)
     455           0 :         return false;
     456       42785 :     if (m_bNoDataSetAsInt64 || m_bNoDataSetAsUInt64)
     457           6 :         return false;  // FIXME: over pessimistic
     458       85558 :     return GDALBufferHasOnlyNoData(
     459       42779 :         pBuffer, m_bNoDataSet ? m_dfNoDataValue : 0.0, nWidth, nHeight,
     460       42779 :         nLineStride, nComponents, m_nBitsPerSample,
     461       42779 :         m_nSampleFormat == SAMPLEFORMAT_UINT  ? GSF_UNSIGNED_INT
     462        4824 :         : m_nSampleFormat == SAMPLEFORMAT_INT ? GSF_SIGNED_INT
     463       42779 :                                               : GSF_FLOATING_POINT);
     464             : }
     465             : 
     466             : /************************************************************************/
     467             : /*                     IsFirstPixelEqualToNoData()                      */
     468             : /************************************************************************/
     469             : 
     470      169040 : inline bool GTiffDataset::IsFirstPixelEqualToNoData(const void *pBuffer)
     471             : {
     472      169040 :     const GDALDataType eDT = GetRasterBand(1)->GetRasterDataType();
     473      169040 :     const double dfEffectiveNoData = (m_bNoDataSet) ? m_dfNoDataValue : 0.0;
     474      169040 :     if (m_bNoDataSetAsInt64 || m_bNoDataSetAsUInt64)
     475          10 :         return true;  // FIXME: over pessimistic
     476      169030 :     if (m_nBitsPerSample == 8 ||
     477       58706 :         (m_nBitsPerSample < 8 && dfEffectiveNoData == 0))
     478             :     {
     479      113770 :         if (eDT == GDT_Int8)
     480             :         {
     481         278 :             return GDALIsValueInRange<signed char>(dfEffectiveNoData) &&
     482         139 :                    *(static_cast<const signed char *>(pBuffer)) ==
     483         278 :                        static_cast<signed char>(dfEffectiveNoData);
     484             :         }
     485      227231 :         return GDALIsValueInRange<GByte>(dfEffectiveNoData) &&
     486      113600 :                *(static_cast<const GByte *>(pBuffer)) ==
     487      227231 :                    static_cast<GByte>(dfEffectiveNoData);
     488             :     }
     489       55260 :     if (m_nBitsPerSample == 16 && eDT == GDT_UInt16)
     490             :     {
     491        4178 :         return GDALIsValueInRange<GUInt16>(dfEffectiveNoData) &&
     492        2089 :                *(static_cast<const GUInt16 *>(pBuffer)) ==
     493        4178 :                    static_cast<GUInt16>(dfEffectiveNoData);
     494             :     }
     495       53171 :     if (m_nBitsPerSample == 16 && eDT == GDT_Int16)
     496             :     {
     497        8476 :         return GDALIsValueInRange<GInt16>(dfEffectiveNoData) &&
     498        4238 :                *(static_cast<const GInt16 *>(pBuffer)) ==
     499        8476 :                    static_cast<GInt16>(dfEffectiveNoData);
     500             :     }
     501       48933 :     if (m_nBitsPerSample == 32 && eDT == GDT_UInt32)
     502             :     {
     503         378 :         return GDALIsValueInRange<GUInt32>(dfEffectiveNoData) &&
     504         189 :                *(static_cast<const GUInt32 *>(pBuffer)) ==
     505         378 :                    static_cast<GUInt32>(dfEffectiveNoData);
     506             :     }
     507       48744 :     if (m_nBitsPerSample == 32 && eDT == GDT_Int32)
     508             :     {
     509         506 :         return GDALIsValueInRange<GInt32>(dfEffectiveNoData) &&
     510         253 :                *(static_cast<const GInt32 *>(pBuffer)) ==
     511         506 :                    static_cast<GInt32>(dfEffectiveNoData);
     512             :     }
     513       48491 :     if (m_nBitsPerSample == 64 && eDT == GDT_UInt64)
     514             :     {
     515         234 :         return GDALIsValueInRange<std::uint64_t>(dfEffectiveNoData) &&
     516         117 :                *(static_cast<const std::uint64_t *>(pBuffer)) ==
     517         234 :                    static_cast<std::uint64_t>(dfEffectiveNoData);
     518             :     }
     519       48374 :     if (m_nBitsPerSample == 64 && eDT == GDT_Int64)
     520             :     {
     521         236 :         return GDALIsValueInRange<std::int64_t>(dfEffectiveNoData) &&
     522         118 :                *(static_cast<const std::int64_t *>(pBuffer)) ==
     523         236 :                    static_cast<std::int64_t>(dfEffectiveNoData);
     524             :     }
     525       48256 :     if (m_nBitsPerSample == 32 && eDT == GDT_Float32)
     526             :     {
     527       41179 :         if (std::isnan(m_dfNoDataValue))
     528           3 :             return CPL_TO_BOOL(
     529           6 :                 std::isnan(*(static_cast<const float *>(pBuffer))));
     530       82352 :         return GDALIsValueInRange<float>(dfEffectiveNoData) &&
     531       41176 :                *(static_cast<const float *>(pBuffer)) ==
     532       82352 :                    static_cast<float>(dfEffectiveNoData);
     533             :     }
     534        7077 :     if (m_nBitsPerSample == 64 && eDT == GDT_Float64)
     535             :     {
     536        4415 :         if (std::isnan(dfEffectiveNoData))
     537           3 :             return CPL_TO_BOOL(
     538           6 :                 std::isnan(*(static_cast<const double *>(pBuffer))));
     539        4412 :         return *(static_cast<const double *>(pBuffer)) == dfEffectiveNoData;
     540             :     }
     541        2662 :     return false;
     542             : }
     543             : 
     544             : /************************************************************************/
     545             : /*                      WriteDealWithLercAndNan()                       */
     546             : /************************************************************************/
     547             : 
     548             : template <typename T>
     549           0 : void GTiffDataset::WriteDealWithLercAndNan(T *pBuffer, int nActualBlockWidth,
     550             :                                            int nActualBlockHeight,
     551             :                                            int nStrileHeight)
     552             : {
     553             :     // This method does 2 things:
     554             :     // - warn the user if he tries to write NaN values with libtiff < 4.6.1
     555             :     //   and multi-band PlanarConfig=Contig configuration
     556             :     // - and in right-most and bottom-most tiles, replace non accessible
     557             :     //   pixel values by a safe one.
     558             : 
     559           0 :     const auto fPaddingValue =
     560             : #if !defined(LIBTIFF_MULTIBAND_LERC_NAN_OK)
     561             :         m_nPlanarConfig == PLANARCONFIG_CONTIG && nBands > 1
     562             :             ? 0
     563             :             :
     564             : #endif
     565             :             std::numeric_limits<T>::quiet_NaN();
     566             : 
     567           0 :     const int nBandsPerStrile =
     568           0 :         m_nPlanarConfig == PLANARCONFIG_CONTIG ? nBands : 1;
     569           0 :     for (int j = 0; j < nActualBlockHeight; ++j)
     570             :     {
     571             : #if !defined(LIBTIFF_MULTIBAND_LERC_NAN_OK)
     572             :         static bool bHasWarned = false;
     573             :         if (m_nPlanarConfig == PLANARCONFIG_CONTIG && nBands > 1 && !bHasWarned)
     574             :         {
     575             :             for (int i = 0; i < nActualBlockWidth * nBandsPerStrile; ++i)
     576             :             {
     577             :                 if (std::isnan(
     578             :                         pBuffer[j * m_nBlockXSize * nBandsPerStrile + i]))
     579             :                 {
     580             :                     bHasWarned = true;
     581             :                     CPLError(CE_Warning, CPLE_AppDefined,
     582             :                              "libtiff < 4.6.1 does not handle properly NaN "
     583             :                              "values for multi-band PlanarConfig=Contig "
     584             :                              "configuration. As a workaround, you can set the "
     585             :                              "INTERLEAVE=BAND creation option.");
     586             :                     break;
     587             :                 }
     588             :             }
     589             :         }
     590             : #endif
     591           0 :         for (int i = nActualBlockWidth * nBandsPerStrile;
     592           0 :              i < m_nBlockXSize * nBandsPerStrile; ++i)
     593             :         {
     594           0 :             pBuffer[j * m_nBlockXSize * nBandsPerStrile + i] = fPaddingValue;
     595             :         }
     596             :     }
     597           0 :     for (int j = nActualBlockHeight; j < nStrileHeight; ++j)
     598             :     {
     599           0 :         for (int i = 0; i < m_nBlockXSize * nBandsPerStrile; ++i)
     600             :         {
     601           0 :             pBuffer[j * m_nBlockXSize * nBandsPerStrile + i] = fPaddingValue;
     602             :         }
     603             :     }
     604           0 : }
     605             : 
     606             : /************************************************************************/
     607             : /*                          WriteEncodedTile()                          */
     608             : /************************************************************************/
     609             : 
     610       50351 : bool GTiffDataset::WriteEncodedTile(uint32_t tile, GByte *pabyData,
     611             :                                     int bPreserveDataBuffer)
     612             : {
     613       50351 :     const int iColumn = (tile % m_nBlocksPerBand) % m_nBlocksPerRow;
     614       50351 :     const int iRow = (tile % m_nBlocksPerBand) / m_nBlocksPerRow;
     615             : 
     616      100702 :     const int nActualBlockWidth = (iColumn == m_nBlocksPerRow - 1)
     617       50351 :                                       ? nRasterXSize - iColumn * m_nBlockXSize
     618             :                                       : m_nBlockXSize;
     619      100702 :     const int nActualBlockHeight = (iRow == m_nBlocksPerColumn - 1)
     620       50351 :                                        ? nRasterYSize - iRow * m_nBlockYSize
     621             :                                        : m_nBlockYSize;
     622             : 
     623             :     /* -------------------------------------------------------------------- */
     624             :     /*      Don't write empty blocks in some cases.                         */
     625             :     /* -------------------------------------------------------------------- */
     626       50351 :     if (!m_bWriteEmptyTiles && IsFirstPixelEqualToNoData(pabyData))
     627             :     {
     628        1946 :         if (!IsBlockAvailable(tile, nullptr, nullptr, nullptr))
     629             :         {
     630        1946 :             const int nComponents =
     631        1946 :                 m_nPlanarConfig == PLANARCONFIG_CONTIG ? nBands : 1;
     632             : 
     633        1946 :             if (HasOnlyNoData(pabyData, nActualBlockWidth, nActualBlockHeight,
     634             :                               m_nBlockXSize, nComponents))
     635             :             {
     636        1171 :                 return true;
     637             :             }
     638             :         }
     639             :     }
     640             : 
     641             :     // Is this a partial right edge or bottom edge tile?
     642       95376 :     const bool bPartialTile = (nActualBlockWidth < m_nBlockXSize) ||
     643       46196 :                               (nActualBlockHeight < m_nBlockYSize);
     644             : 
     645             :     const bool bIsLercFloatingPoint =
     646       49246 :         m_nCompression == COMPRESSION_LERC &&
     647          66 :         (GetRasterBand(1)->GetRasterDataType() == GDT_Float32 ||
     648          64 :          GetRasterBand(1)->GetRasterDataType() == GDT_Float64);
     649             : 
     650             :     // Do we need to spread edge values right or down for a partial
     651             :     // JPEG encoded tile?  We do this to avoid edge artifacts.
     652             :     // We also need to be careful with LERC and NaN values
     653       49180 :     const bool bNeedTempBuffer =
     654       53842 :         bPartialTile &&
     655        4662 :         (m_nCompression == COMPRESSION_JPEG || bIsLercFloatingPoint);
     656             : 
     657             :     // If we need to fill out the tile, or if we want to prevent
     658             :     // TIFFWriteEncodedTile from altering the buffer as part of
     659             :     // byte swapping the data on write then we will need a temporary
     660             :     // working buffer.  If not, we can just do a direct write.
     661       49180 :     const GPtrDiff_t cc = static_cast<GPtrDiff_t>(TIFFTileSize(m_hTIFF));
     662             : 
     663       63489 :     if (bPreserveDataBuffer &&
     664       14309 :         (TIFFIsByteSwapped(m_hTIFF) || bNeedTempBuffer || m_panMaskOffsetLsb))
     665             :     {
     666         158 :         if (m_pabyTempWriteBuffer == nullptr)
     667             :         {
     668          35 :             m_pabyTempWriteBuffer = CPLMalloc(cc);
     669             :         }
     670         158 :         memcpy(m_pabyTempWriteBuffer, pabyData, cc);
     671             : 
     672         158 :         pabyData = static_cast<GByte *>(m_pabyTempWriteBuffer);
     673             :     }
     674             : 
     675             :     // Perform tile fill if needed.
     676             :     // TODO: we should also handle the case of nBitsPerSample == 12
     677             :     // but this is more involved.
     678       49180 :     if (bPartialTile && m_nCompression == COMPRESSION_JPEG &&
     679         134 :         m_nBitsPerSample == 8)
     680             :     {
     681         132 :         const int nComponents =
     682         132 :             m_nPlanarConfig == PLANARCONFIG_CONTIG ? nBands : 1;
     683             : 
     684         132 :         CPLDebug("GTiff", "Filling out jpeg edge tile on write.");
     685             : 
     686         132 :         const int nRightPixelsToFill =
     687         132 :             iColumn == m_nBlocksPerRow - 1
     688         132 :                 ? m_nBlockXSize * (iColumn + 1) - nRasterXSize
     689             :                 : 0;
     690         132 :         const int nBottomPixelsToFill =
     691         132 :             iRow == m_nBlocksPerColumn - 1
     692         132 :                 ? m_nBlockYSize * (iRow + 1) - nRasterYSize
     693             :                 : 0;
     694             : 
     695             :         // Fill out to the right.
     696         132 :         const int iSrcX = m_nBlockXSize - nRightPixelsToFill - 1;
     697             : 
     698       12461 :         for (int iX = iSrcX + 1; iX < m_nBlockXSize; ++iX)
     699             :         {
     700     3955880 :             for (int iY = 0; iY < m_nBlockYSize; ++iY)
     701             :             {
     702     3943550 :                 memcpy(pabyData +
     703     3943550 :                            (static_cast<GPtrDiff_t>(m_nBlockXSize) * iY + iX) *
     704     3943550 :                                nComponents,
     705     3943550 :                        pabyData + (static_cast<GPtrDiff_t>(m_nBlockXSize) * iY +
     706     3943550 :                                    iSrcX) *
     707     3943550 :                                       nComponents,
     708             :                        nComponents);
     709             :             }
     710             :         }
     711             : 
     712             :         // Now fill out the bottom.
     713         132 :         const int iSrcY = m_nBlockYSize - nBottomPixelsToFill - 1;
     714       17682 :         for (int iY = iSrcY + 1; iY < m_nBlockYSize; ++iY)
     715             :         {
     716       17550 :             memcpy(pabyData + static_cast<GPtrDiff_t>(m_nBlockXSize) *
     717       17550 :                                   nComponents * iY,
     718       17550 :                    pabyData + static_cast<GPtrDiff_t>(m_nBlockXSize) *
     719       17550 :                                   nComponents * iSrcY,
     720       17550 :                    static_cast<GPtrDiff_t>(m_nBlockXSize) * nComponents);
     721             :         }
     722             :     }
     723             : 
     724       49180 :     if (bIsLercFloatingPoint &&
     725             :         (bPartialTile
     726             : #if !defined(LIBTIFF_MULTIBAND_LERC_NAN_OK)
     727             :          /* libtiff < 4.6.1 doesn't generate a LERC mask for multi-band contig configuration */
     728             :          || (m_nPlanarConfig == PLANARCONFIG_CONTIG && nBands > 1)
     729             : #endif
     730             :              ))
     731             :     {
     732           0 :         if (GetRasterBand(1)->GetRasterDataType() == GDT_Float32)
     733           0 :             WriteDealWithLercAndNan(reinterpret_cast<float *>(pabyData),
     734             :                                     nActualBlockWidth, nActualBlockHeight,
     735             :                                     m_nBlockYSize);
     736             :         else
     737           0 :             WriteDealWithLercAndNan(reinterpret_cast<double *>(pabyData),
     738             :                                     nActualBlockWidth, nActualBlockHeight,
     739             :                                     m_nBlockYSize);
     740             :     }
     741             : 
     742       49180 :     if (m_panMaskOffsetLsb)
     743             :     {
     744           0 :         const int iBand = m_nPlanarConfig == PLANARCONFIG_SEPARATE
     745           0 :                               ? static_cast<int>(tile) / m_nBlocksPerBand
     746             :                               : -1;
     747           0 :         DiscardLsb(pabyData, cc, iBand);
     748             :     }
     749             : 
     750       49180 :     if (m_bStreamingOut)
     751             :     {
     752          17 :         if (tile != static_cast<uint32_t>(m_nLastWrittenBlockId + 1))
     753             :         {
     754           1 :             ReportError(CE_Failure, CPLE_NotSupported,
     755             :                         "Attempt to write block %d whereas %d was expected",
     756           1 :                         tile, m_nLastWrittenBlockId + 1);
     757           1 :             return false;
     758             :         }
     759          16 :         if (static_cast<GPtrDiff_t>(VSIFWriteL(pabyData, 1, cc, m_fpToWrite)) !=
     760             :             cc)
     761             :         {
     762           0 :             ReportError(CE_Failure, CPLE_FileIO,
     763             :                         "Could not write " CPL_FRMT_GUIB " bytes",
     764             :                         static_cast<GUIntBig>(cc));
     765           0 :             return false;
     766             :         }
     767          16 :         m_nLastWrittenBlockId = tile;
     768          16 :         return true;
     769             :     }
     770             : 
     771             :     /* -------------------------------------------------------------------- */
     772             :     /*      Should we do compression in a worker thread ?                   */
     773             :     /* -------------------------------------------------------------------- */
     774       49163 :     if (SubmitCompressionJob(tile, pabyData, cc, m_nBlockYSize))
     775       19832 :         return true;
     776             : 
     777       29331 :     return TIFFWriteEncodedTile(m_hTIFF, tile, pabyData, cc) == cc;
     778             : }
     779             : 
     780             : /************************************************************************/
     781             : /*                         WriteEncodedStrip()                          */
     782             : /************************************************************************/
     783             : 
     784      178063 : bool GTiffDataset::WriteEncodedStrip(uint32_t strip, GByte *pabyData,
     785             :                                      int bPreserveDataBuffer)
     786             : {
     787      178063 :     GPtrDiff_t cc = static_cast<GPtrDiff_t>(TIFFStripSize(m_hTIFF));
     788      178063 :     const auto ccFull = cc;
     789             : 
     790             :     /* -------------------------------------------------------------------- */
     791             :     /*      If this is the last strip in the image, and is partial, then    */
     792             :     /*      we need to trim the number of scanlines written to the          */
     793             :     /*      amount of valid data we have. (#2748)                           */
     794             :     /* -------------------------------------------------------------------- */
     795      178063 :     const int nStripWithinBand = strip % m_nBlocksPerBand;
     796      178063 :     int nStripHeight = m_nRowsPerStrip;
     797             : 
     798      178063 :     if (nStripWithinBand * nStripHeight > GetRasterYSize() - nStripHeight)
     799             :     {
     800         384 :         nStripHeight = GetRasterYSize() - nStripWithinBand * m_nRowsPerStrip;
     801         384 :         cc = (cc / m_nRowsPerStrip) * nStripHeight;
     802         768 :         CPLDebug("GTiff",
     803             :                  "Adjusted bytes to write from " CPL_FRMT_GUIB
     804             :                  " to " CPL_FRMT_GUIB ".",
     805         384 :                  static_cast<GUIntBig>(TIFFStripSize(m_hTIFF)),
     806             :                  static_cast<GUIntBig>(cc));
     807             :     }
     808             : 
     809             :     /* -------------------------------------------------------------------- */
     810             :     /*      Don't write empty blocks in some cases.                         */
     811             :     /* -------------------------------------------------------------------- */
     812      178063 :     if (!m_bWriteEmptyTiles && IsFirstPixelEqualToNoData(pabyData))
     813             :     {
     814       41013 :         if (!IsBlockAvailable(strip, nullptr, nullptr, nullptr))
     815             :         {
     816       40839 :             const int nComponents =
     817       40839 :                 m_nPlanarConfig == PLANARCONFIG_CONTIG ? nBands : 1;
     818             : 
     819       40839 :             if (HasOnlyNoData(pabyData, m_nBlockXSize, nStripHeight,
     820             :                               m_nBlockXSize, nComponents))
     821             :             {
     822       28308 :                 return true;
     823             :             }
     824             :         }
     825             :     }
     826             : 
     827             :     /* -------------------------------------------------------------------- */
     828             :     /*      TIFFWriteEncodedStrip can alter the passed buffer if            */
     829             :     /*      byte-swapping is necessary so we use a temporary buffer         */
     830             :     /*      before calling it.                                              */
     831             :     /* -------------------------------------------------------------------- */
     832      239227 :     if (bPreserveDataBuffer &&
     833       89472 :         (TIFFIsByteSwapped(m_hTIFF) || m_panMaskOffsetLsb))
     834             :     {
     835         294 :         if (m_pabyTempWriteBuffer == nullptr)
     836             :         {
     837         126 :             m_pabyTempWriteBuffer = CPLMalloc(ccFull);
     838             :         }
     839         294 :         memcpy(m_pabyTempWriteBuffer, pabyData, cc);
     840         294 :         pabyData = static_cast<GByte *>(m_pabyTempWriteBuffer);
     841             :     }
     842             : 
     843             : #if !defined(LIBTIFF_MULTIBAND_LERC_NAN_OK)
     844             :     const bool bIsLercFloatingPoint =
     845             :         m_nCompression == COMPRESSION_LERC &&
     846             :         (GetRasterBand(1)->GetRasterDataType() == GDT_Float32 ||
     847             :          GetRasterBand(1)->GetRasterDataType() == GDT_Float64);
     848             :     if (bIsLercFloatingPoint &&
     849             :         /* libtiff < 4.6.1 doesn't generate a LERC mask for multi-band contig configuration */
     850             :         m_nPlanarConfig == PLANARCONFIG_CONTIG && nBands > 1)
     851             :     {
     852             :         if (GetRasterBand(1)->GetRasterDataType() == GDT_Float32)
     853             :             WriteDealWithLercAndNan(reinterpret_cast<float *>(pabyData),
     854             :                                     m_nBlockXSize, nStripHeight, nStripHeight);
     855             :         else
     856             :             WriteDealWithLercAndNan(reinterpret_cast<double *>(pabyData),
     857             :                                     m_nBlockXSize, nStripHeight, nStripHeight);
     858             :     }
     859             : #endif
     860             : 
     861      149755 :     if (m_panMaskOffsetLsb)
     862             :     {
     863         366 :         int iBand = m_nPlanarConfig == PLANARCONFIG_SEPARATE
     864         183 :                         ? static_cast<int>(strip) / m_nBlocksPerBand
     865             :                         : -1;
     866         183 :         DiscardLsb(pabyData, cc, iBand);
     867             :     }
     868             : 
     869      149755 :     if (m_bStreamingOut)
     870             :     {
     871        1408 :         if (strip != static_cast<uint32_t>(m_nLastWrittenBlockId + 1))
     872             :         {
     873           1 :             ReportError(CE_Failure, CPLE_NotSupported,
     874             :                         "Attempt to write block %d whereas %d was expected",
     875           1 :                         strip, m_nLastWrittenBlockId + 1);
     876           1 :             return false;
     877             :         }
     878        1407 :         if (static_cast<GPtrDiff_t>(VSIFWriteL(pabyData, 1, cc, m_fpToWrite)) !=
     879             :             cc)
     880             :         {
     881           0 :             ReportError(CE_Failure, CPLE_FileIO,
     882             :                         "Could not write " CPL_FRMT_GUIB " bytes",
     883             :                         static_cast<GUIntBig>(cc));
     884           0 :             return false;
     885             :         }
     886        1407 :         m_nLastWrittenBlockId = strip;
     887        1407 :         return true;
     888             :     }
     889             : 
     890             :     /* -------------------------------------------------------------------- */
     891             :     /*      Should we do compression in a worker thread ?                   */
     892             :     /* -------------------------------------------------------------------- */
     893      148347 :     if (SubmitCompressionJob(strip, pabyData, cc, nStripHeight))
     894        6727 :         return true;
     895             : 
     896      141620 :     return TIFFWriteEncodedStrip(m_hTIFF, strip, pabyData, cc) == cc;
     897             : }
     898             : 
     899             : /************************************************************************/
     900             : /*                       InitCompressionThreads()                       */
     901             : /************************************************************************/
     902             : 
     903       31837 : void GTiffDataset::InitCompressionThreads(bool bUpdateMode,
     904             :                                           CSLConstList papszOptions)
     905             : {
     906             :     // Raster == tile, then no need for threads
     907       31837 :     if (m_nBlockXSize == nRasterXSize && m_nBlockYSize == nRasterYSize)
     908       23254 :         return;
     909             : 
     910        8583 :     const char *pszNumThreads = "";
     911        8583 :     bool bOK = false;
     912        8583 :     const int nThreads = GDALGetNumThreads(
     913             :         papszOptions, "NUM_THREADS", GDAL_DEFAULT_MAX_THREAD_COUNT,
     914             :         /* bDefaultToAllCPUs=*/false, &pszNumThreads, &bOK);
     915        8583 :     if (nThreads > 1)
     916             :     {
     917         106 :         if ((bUpdateMode && m_nCompression != COMPRESSION_NONE) ||
     918          24 :             (nBands >= 1 && IsMultiThreadedReadCompatible()))
     919             :         {
     920          77 :             CPLDebug("GTiff",
     921             :                      "Using up to %d threads for compression/decompression",
     922             :                      nThreads);
     923             : 
     924          77 :             m_poThreadPool = GDALGetGlobalThreadPool(nThreads);
     925          77 :             if (bUpdateMode && m_poThreadPool)
     926          58 :                 m_poCompressQueue = m_poThreadPool->CreateJobQueue();
     927             : 
     928          77 :             if (m_poCompressQueue != nullptr)
     929             :             {
     930             :                 // Add a margin of an extra job w.r.t thread number
     931             :                 // so as to optimize compression time (enables the main
     932             :                 // thread to do boring I/O while all CPUs are working).
     933          58 :                 m_asCompressionJobs.resize(nThreads + 1);
     934          58 :                 memset(&m_asCompressionJobs[0], 0,
     935          58 :                        m_asCompressionJobs.size() *
     936             :                            sizeof(GTiffCompressionJob));
     937          58 :                 for (int i = 0;
     938         280 :                      i < static_cast<int>(m_asCompressionJobs.size()); ++i)
     939             :                 {
     940         444 :                     m_asCompressionJobs[i].pszTmpFilename =
     941         222 :                         CPLStrdup(VSIMemGenerateHiddenFilename(
     942             :                             CPLSPrintf("thread_job_%d.tif", i)));
     943         222 :                     m_asCompressionJobs[i].nStripOrTile = -1;
     944             :                 }
     945             : 
     946             :                 // This is kind of a hack, but basically using
     947             :                 // TIFFWriteRawStrip/Tile and then TIFFReadEncodedStrip/Tile
     948             :                 // does not work on a newly created file, because
     949             :                 // TIFF_MYBUFFER is not set in tif_flags
     950             :                 // (if using TIFFWriteEncodedStrip/Tile first,
     951             :                 // TIFFWriteBufferSetup() is automatically called).
     952             :                 // This should likely rather fixed in libtiff itself.
     953          58 :                 CPL_IGNORE_RET_VAL(TIFFWriteBufferSetup(m_hTIFF, nullptr, -1));
     954             :             }
     955             :         }
     956             :     }
     957        8501 :     else if (!bOK)
     958             :     {
     959           3 :         ReportError(CE_Warning, CPLE_AppDefined,
     960             :                     "Invalid value for NUM_THREADS: %s", pszNumThreads);
     961             :     }
     962             : }
     963             : 
     964             : /************************************************************************/
     965             : /*                       ThreadCompressionFunc()                        */
     966             : /************************************************************************/
     967             : 
     968       26571 : void GTiffDataset::ThreadCompressionFunc(void *pData)
     969             : {
     970       26571 :     GTiffCompressionJob *psJob = static_cast<GTiffCompressionJob *>(pData);
     971       26571 :     GTiffDataset *poDS = psJob->poDS;
     972             : 
     973       26571 :     VSILFILE *fpTmp = VSIFOpenL(psJob->pszTmpFilename, "wb+");
     974       26571 :     TIFF *hTIFFTmp = VSI_TIFFOpen(
     975       53142 :         psJob->pszTmpFilename, psJob->bTIFFIsBigEndian ? "wb+" : "wl+", fpTmp);
     976       26571 :     CPLAssert(hTIFFTmp != nullptr);
     977       26571 :     TIFFSetField(hTIFFTmp, TIFFTAG_IMAGEWIDTH, poDS->m_nBlockXSize);
     978       26571 :     TIFFSetField(hTIFFTmp, TIFFTAG_IMAGELENGTH, psJob->nHeight);
     979       26571 :     TIFFSetField(hTIFFTmp, TIFFTAG_BITSPERSAMPLE, poDS->m_nBitsPerSample);
     980       26571 :     TIFFSetField(hTIFFTmp, TIFFTAG_COMPRESSION, poDS->m_nCompression);
     981       26571 :     TIFFSetField(hTIFFTmp, TIFFTAG_PHOTOMETRIC, poDS->m_nPhotometric);
     982       26571 :     TIFFSetField(hTIFFTmp, TIFFTAG_SAMPLEFORMAT, poDS->m_nSampleFormat);
     983       26571 :     TIFFSetField(hTIFFTmp, TIFFTAG_SAMPLESPERPIXEL, poDS->m_nSamplesPerPixel);
     984       26571 :     TIFFSetField(hTIFFTmp, TIFFTAG_ROWSPERSTRIP, poDS->m_nBlockYSize);
     985       26571 :     TIFFSetField(hTIFFTmp, TIFFTAG_PLANARCONFIG, poDS->m_nPlanarConfig);
     986       26571 :     if (psJob->nPredictor != PREDICTOR_NONE)
     987         263 :         TIFFSetField(hTIFFTmp, TIFFTAG_PREDICTOR, psJob->nPredictor);
     988       26571 :     if (poDS->m_nCompression == COMPRESSION_LERC)
     989             :     {
     990          24 :         TIFFSetField(hTIFFTmp, TIFFTAG_LERC_PARAMETERS, 2,
     991          24 :                      poDS->m_anLercAddCompressionAndVersion);
     992             :     }
     993       26571 :     if (psJob->nExtraSampleCount)
     994             :     {
     995         352 :         TIFFSetField(hTIFFTmp, TIFFTAG_EXTRASAMPLES, psJob->nExtraSampleCount,
     996             :                      psJob->pExtraSamples);
     997             :     }
     998             : 
     999       26571 :     poDS->RestoreVolatileParameters(hTIFFTmp);
    1000             : 
    1001       53142 :     bool bOK = TIFFWriteEncodedStrip(hTIFFTmp, 0, psJob->pabyBuffer,
    1002       26571 :                                      psJob->nBufferSize) == psJob->nBufferSize;
    1003             : 
    1004       26571 :     toff_t nOffset = 0;
    1005       26571 :     if (bOK)
    1006             :     {
    1007       26571 :         toff_t *panOffsets = nullptr;
    1008       26571 :         toff_t *panByteCounts = nullptr;
    1009       26571 :         TIFFGetField(hTIFFTmp, TIFFTAG_STRIPOFFSETS, &panOffsets);
    1010       26571 :         TIFFGetField(hTIFFTmp, TIFFTAG_STRIPBYTECOUNTS, &panByteCounts);
    1011             : 
    1012       26571 :         nOffset = panOffsets[0];
    1013       26571 :         psJob->nCompressedBufferSize =
    1014       26571 :             static_cast<GPtrDiff_t>(panByteCounts[0]);
    1015             :     }
    1016             :     else
    1017             :     {
    1018           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    1019             :                  "Error when compressing strip/tile %d", psJob->nStripOrTile);
    1020             :     }
    1021             : 
    1022       26571 :     XTIFFClose(hTIFFTmp);
    1023       26571 :     if (VSIFCloseL(fpTmp) != 0)
    1024             :     {
    1025           0 :         if (bOK)
    1026             :         {
    1027           0 :             bOK = false;
    1028           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    1029             :                      "Error when compressing strip/tile %d",
    1030             :                      psJob->nStripOrTile);
    1031             :         }
    1032             :     }
    1033             : 
    1034       26571 :     if (bOK)
    1035             :     {
    1036       26571 :         vsi_l_offset nFileSize = 0;
    1037             :         GByte *pabyCompressedBuffer =
    1038       26571 :             VSIGetMemFileBuffer(psJob->pszTmpFilename, &nFileSize, FALSE);
    1039       26571 :         CPLAssert(static_cast<vsi_l_offset>(
    1040             :                       nOffset + psJob->nCompressedBufferSize) <= nFileSize);
    1041       26571 :         psJob->pabyCompressedBuffer = pabyCompressedBuffer + nOffset;
    1042             :     }
    1043             :     else
    1044             :     {
    1045           0 :         psJob->pabyCompressedBuffer = nullptr;
    1046           0 :         psJob->nCompressedBufferSize = 0;
    1047             :     }
    1048             : 
    1049       26571 :     auto poMainDS = poDS->m_poBaseDS ? poDS->m_poBaseDS : poDS;
    1050       26571 :     if (poMainDS->m_poCompressQueue)
    1051             :     {
    1052        1576 :         std::lock_guard oLock(poMainDS->m_oCompressThreadPoolMutex);
    1053        1576 :         psJob->bReady = true;
    1054             :     }
    1055       26571 : }
    1056             : 
    1057             : /************************************************************************/
    1058             : /*                        WriteRawStripOrTile()                         */
    1059             : /************************************************************************/
    1060             : 
    1061       33876 : void GTiffDataset::WriteRawStripOrTile(int nStripOrTile,
    1062             :                                        GByte *pabyCompressedBuffer,
    1063             :                                        GPtrDiff_t nCompressedBufferSize)
    1064             : {
    1065             : #ifdef DEBUG_VERBOSE
    1066             :     CPLDebug("GTIFF", "Writing raw strip/tile %d, size " CPL_FRMT_GUIB,
    1067             :              nStripOrTile, static_cast<GUIntBig>(nCompressedBufferSize));
    1068             : #endif
    1069       33876 :     toff_t *panOffsets = nullptr;
    1070       33876 :     toff_t *panByteCounts = nullptr;
    1071       33876 :     bool bWriteAtEnd = true;
    1072       33876 :     bool bWriteLeader = m_bLeaderSizeAsUInt4;
    1073       33876 :     bool bWriteTrailer = m_bTrailerRepeatedLast4BytesRepeated;
    1074       33876 :     if (TIFFGetField(m_hTIFF,
    1075       33876 :                      TIFFIsTiled(m_hTIFF) ? TIFFTAG_TILEOFFSETS
    1076             :                                           : TIFFTAG_STRIPOFFSETS,
    1077       33876 :                      &panOffsets) &&
    1078       33876 :         panOffsets != nullptr && panOffsets[nStripOrTile] != 0)
    1079             :     {
    1080             :         // Forces TIFFAppendStrip() to consider if the location of the
    1081             :         // tile/strip can be reused or if the strile should be written at end of
    1082             :         // file.
    1083         360 :         TIFFSetWriteOffset(m_hTIFF, 0);
    1084             : 
    1085         360 :         if (m_bBlockOrderRowMajor)
    1086             :         {
    1087         264 :             if (TIFFGetField(m_hTIFF,
    1088         264 :                              TIFFIsTiled(m_hTIFF) ? TIFFTAG_TILEBYTECOUNTS
    1089             :                                                   : TIFFTAG_STRIPBYTECOUNTS,
    1090         528 :                              &panByteCounts) &&
    1091         264 :                 panByteCounts != nullptr)
    1092             :             {
    1093         264 :                 if (static_cast<GUIntBig>(nCompressedBufferSize) >
    1094         264 :                     panByteCounts[nStripOrTile])
    1095             :                 {
    1096           8 :                     GTiffDataset *poRootDS = m_poBaseDS ? m_poBaseDS : this;
    1097           8 :                     if (!poRootDS->m_bKnownIncompatibleEdition &&
    1098           8 :                         !poRootDS->m_bWriteKnownIncompatibleEdition)
    1099             :                     {
    1100           8 :                         ReportError(
    1101             :                             CE_Warning, CPLE_AppDefined,
    1102             :                             "A strile cannot be rewritten in place, which "
    1103             :                             "invalidates the BLOCK_ORDER optimization.");
    1104           8 :                         poRootDS->m_bKnownIncompatibleEdition = true;
    1105           8 :                         poRootDS->m_bWriteKnownIncompatibleEdition = true;
    1106             :                     }
    1107             :                 }
    1108             :                 // For mask interleaving, if the size is not exactly the same,
    1109             :                 // completely give up (we could potentially move the mask in
    1110             :                 // case the imagery is smaller)
    1111         256 :                 else if (m_poMaskDS && m_bMaskInterleavedWithImagery &&
    1112           0 :                          static_cast<GUIntBig>(nCompressedBufferSize) !=
    1113           0 :                              panByteCounts[nStripOrTile])
    1114             :                 {
    1115           0 :                     GTiffDataset *poRootDS = m_poBaseDS ? m_poBaseDS : this;
    1116           0 :                     if (!poRootDS->m_bKnownIncompatibleEdition &&
    1117           0 :                         !poRootDS->m_bWriteKnownIncompatibleEdition)
    1118             :                     {
    1119           0 :                         ReportError(
    1120             :                             CE_Warning, CPLE_AppDefined,
    1121             :                             "A strile cannot be rewritten in place, which "
    1122             :                             "invalidates the MASK_INTERLEAVED_WITH_IMAGERY "
    1123             :                             "optimization.");
    1124           0 :                         poRootDS->m_bKnownIncompatibleEdition = true;
    1125           0 :                         poRootDS->m_bWriteKnownIncompatibleEdition = true;
    1126             :                     }
    1127           0 :                     bWriteLeader = false;
    1128           0 :                     bWriteTrailer = false;
    1129           0 :                     if (m_bLeaderSizeAsUInt4)
    1130             :                     {
    1131             :                         // If there was a valid leader, invalidat it
    1132           0 :                         VSI_TIFFSeek(m_hTIFF, panOffsets[nStripOrTile] - 4,
    1133             :                                      SEEK_SET);
    1134             :                         uint32_t nOldSize;
    1135           0 :                         VSIFReadL(&nOldSize, 1, 4,
    1136             :                                   VSI_TIFFGetVSILFile(TIFFClientdata(m_hTIFF)));
    1137           0 :                         CPL_LSBPTR32(&nOldSize);
    1138           0 :                         if (nOldSize == panByteCounts[nStripOrTile])
    1139             :                         {
    1140           0 :                             uint32_t nInvalidatedSize = 0;
    1141           0 :                             VSI_TIFFSeek(m_hTIFF, panOffsets[nStripOrTile] - 4,
    1142             :                                          SEEK_SET);
    1143           0 :                             VSI_TIFFWrite(m_hTIFF, &nInvalidatedSize,
    1144             :                                           sizeof(nInvalidatedSize));
    1145             :                         }
    1146             :                     }
    1147             :                 }
    1148             :                 else
    1149             :                 {
    1150         256 :                     bWriteAtEnd = false;
    1151             :                 }
    1152             :             }
    1153             :         }
    1154             :     }
    1155       33876 :     if (bWriteLeader &&
    1156       25000 :         static_cast<GUIntBig>(nCompressedBufferSize) <= 0xFFFFFFFFU)
    1157             :     {
    1158             :         // cppcheck-suppress knownConditionTrueFalse
    1159       25000 :         if (bWriteAtEnd)
    1160             :         {
    1161       24744 :             VSI_TIFFSeek(m_hTIFF, 0, SEEK_END);
    1162             :         }
    1163             :         else
    1164             :         {
    1165             :             // If we rewrite an existing strile in place with an existing
    1166             :             // leader, check that the leader is valid, before rewriting it. And
    1167             :             // if it is not valid, then do not write the trailer, as we could
    1168             :             // corrupt other data.
    1169         256 :             VSI_TIFFSeek(m_hTIFF, panOffsets[nStripOrTile] - 4, SEEK_SET);
    1170             :             uint32_t nOldSize;
    1171         256 :             VSIFReadL(&nOldSize, 1, 4,
    1172             :                       VSI_TIFFGetVSILFile(TIFFClientdata(m_hTIFF)));
    1173         256 :             CPL_LSBPTR32(&nOldSize);
    1174         256 :             bWriteLeader =
    1175         256 :                 panByteCounts && nOldSize == panByteCounts[nStripOrTile];
    1176         256 :             bWriteTrailer = bWriteLeader;
    1177         256 :             VSI_TIFFSeek(m_hTIFF, panOffsets[nStripOrTile] - 4, SEEK_SET);
    1178             :         }
    1179             :         // cppcheck-suppress knownConditionTrueFalse
    1180       25000 :         if (bWriteLeader)
    1181             :         {
    1182       25000 :             uint32_t nSize = static_cast<uint32_t>(nCompressedBufferSize);
    1183       25000 :             CPL_LSBPTR32(&nSize);
    1184       25000 :             if (!VSI_TIFFWrite(m_hTIFF, &nSize, sizeof(nSize)))
    1185           0 :                 m_bWriteError = true;
    1186             :         }
    1187             :     }
    1188             :     tmsize_t written;
    1189       33876 :     if (TIFFIsTiled(m_hTIFF))
    1190       26221 :         written = TIFFWriteRawTile(m_hTIFF, nStripOrTile, pabyCompressedBuffer,
    1191             :                                    nCompressedBufferSize);
    1192             :     else
    1193        7655 :         written = TIFFWriteRawStrip(m_hTIFF, nStripOrTile, pabyCompressedBuffer,
    1194             :                                     nCompressedBufferSize);
    1195       33876 :     if (written != nCompressedBufferSize)
    1196          12 :         m_bWriteError = true;
    1197       33876 :     if (bWriteTrailer &&
    1198       25000 :         static_cast<GUIntBig>(nCompressedBufferSize) <= 0xFFFFFFFFU)
    1199             :     {
    1200       25000 :         GByte abyLastBytes[4] = {};
    1201       25000 :         if (nCompressedBufferSize >= 4)
    1202       25000 :             memcpy(abyLastBytes,
    1203       25000 :                    pabyCompressedBuffer + nCompressedBufferSize - 4, 4);
    1204             :         else
    1205           0 :             memcpy(abyLastBytes, pabyCompressedBuffer, nCompressedBufferSize);
    1206       25000 :         if (!VSI_TIFFWrite(m_hTIFF, abyLastBytes, 4))
    1207           0 :             m_bWriteError = true;
    1208             :     }
    1209       33876 : }
    1210             : 
    1211             : /************************************************************************/
    1212             : /*                      WaitCompletionForJobIdx()                       */
    1213             : /************************************************************************/
    1214             : 
    1215        1576 : void GTiffDataset::WaitCompletionForJobIdx(int i)
    1216             : {
    1217        1576 :     auto poMainDS = m_poBaseDS ? m_poBaseDS : this;
    1218        1576 :     auto poQueue = poMainDS->m_poCompressQueue.get();
    1219        1576 :     auto &oQueue = poMainDS->m_asQueueJobIdx;
    1220        1576 :     auto &asJobs = poMainDS->m_asCompressionJobs;
    1221        1576 :     auto &mutex = poMainDS->m_oCompressThreadPoolMutex;
    1222             : 
    1223        1576 :     CPLAssert(i >= 0 && static_cast<size_t>(i) < asJobs.size());
    1224        1576 :     CPLAssert(asJobs[i].nStripOrTile >= 0);
    1225        1576 :     CPLAssert(!oQueue.empty());
    1226             : 
    1227        1576 :     bool bHasWarned = false;
    1228             :     while (true)
    1229             :     {
    1230             :         bool bReady;
    1231             :         {
    1232        2269 :             std::lock_guard oLock(mutex);
    1233        2269 :             bReady = asJobs[i].bReady;
    1234             :         }
    1235        2269 :         if (!bReady)
    1236             :         {
    1237         693 :             if (!bHasWarned)
    1238             :             {
    1239         421 :                 CPLDebug("GTIFF",
    1240             :                          "Waiting for worker job to finish handling block %d",
    1241         421 :                          asJobs[i].nStripOrTile);
    1242         421 :                 bHasWarned = true;
    1243             :             }
    1244         693 :             poQueue->GetPool()->WaitEvent();
    1245             :         }
    1246             :         else
    1247             :         {
    1248        1576 :             break;
    1249             :         }
    1250         693 :     }
    1251             : 
    1252        1576 :     if (asJobs[i].nCompressedBufferSize)
    1253             :     {
    1254        3152 :         asJobs[i].poDS->WriteRawStripOrTile(asJobs[i].nStripOrTile,
    1255        1576 :                                             asJobs[i].pabyCompressedBuffer,
    1256        1576 :                                             asJobs[i].nCompressedBufferSize);
    1257             :     }
    1258        1576 :     asJobs[i].pabyCompressedBuffer = nullptr;
    1259        1576 :     asJobs[i].nBufferSize = 0;
    1260             :     {
    1261             :         // Likely useless, but makes Coverity happy
    1262        1576 :         std::lock_guard oLock(mutex);
    1263        1576 :         asJobs[i].bReady = false;
    1264             :     }
    1265        1576 :     asJobs[i].nStripOrTile = -1;
    1266        1576 :     oQueue.pop();
    1267        1576 : }
    1268             : 
    1269             : /************************************************************************/
    1270             : /*                       WaitCompletionForBlock()                       */
    1271             : /************************************************************************/
    1272             : 
    1273     2319110 : void GTiffDataset::WaitCompletionForBlock(int nBlockId)
    1274             : {
    1275     2319110 :     auto poQueue = m_poBaseDS ? m_poBaseDS->m_poCompressQueue.get()
    1276     2299890 :                               : m_poCompressQueue.get();
    1277             :     // cppcheck-suppress constVariableReference
    1278     2319110 :     auto &oQueue = m_poBaseDS ? m_poBaseDS->m_asQueueJobIdx : m_asQueueJobIdx;
    1279             :     // cppcheck-suppress constVariableReference
    1280     2299890 :     auto &asJobs =
    1281     2319110 :         m_poBaseDS ? m_poBaseDS->m_asCompressionJobs : m_asCompressionJobs;
    1282             : 
    1283     2319110 :     if (poQueue != nullptr && !oQueue.empty())
    1284             :     {
    1285        1066 :         for (int i = 0; i < static_cast<int>(asJobs.size()); ++i)
    1286             :         {
    1287         888 :             if (asJobs[i].poDS == this && asJobs[i].nStripOrTile == nBlockId)
    1288             :             {
    1289         128 :                 while (!oQueue.empty() &&
    1290          64 :                        !(asJobs[oQueue.front()].poDS == this &&
    1291          64 :                          asJobs[oQueue.front()].nStripOrTile == nBlockId))
    1292             :                 {
    1293           0 :                     WaitCompletionForJobIdx(oQueue.front());
    1294             :                 }
    1295          64 :                 CPLAssert(!oQueue.empty() &&
    1296             :                           asJobs[oQueue.front()].poDS == this &&
    1297             :                           asJobs[oQueue.front()].nStripOrTile == nBlockId);
    1298          64 :                 WaitCompletionForJobIdx(oQueue.front());
    1299             :             }
    1300             :         }
    1301             :     }
    1302     2319110 : }
    1303             : 
    1304             : /************************************************************************/
    1305             : /*                        SubmitCompressionJob()                        */
    1306             : /************************************************************************/
    1307             : 
    1308      197510 : bool GTiffDataset::SubmitCompressionJob(int nStripOrTile, GByte *pabyData,
    1309             :                                         GPtrDiff_t cc, int nHeight)
    1310             : {
    1311             :     /* -------------------------------------------------------------------- */
    1312             :     /*      Should we do compression in a worker thread ?                   */
    1313             :     /* -------------------------------------------------------------------- */
    1314      197510 :     auto poQueue = m_poBaseDS ? m_poBaseDS->m_poCompressQueue.get()
    1315      183487 :                               : m_poCompressQueue.get();
    1316             : 
    1317      197510 :     if (poQueue && m_nCompression == COMPRESSION_NONE)
    1318             :     {
    1319             :         // We don't do multi-threaded compression for uncompressed...
    1320             :         // but we must wait for other related compression tasks (e.g mask)
    1321             :         // to be completed
    1322           0 :         poQueue->WaitCompletion();
    1323             : 
    1324             :         // Flush remaining data
    1325             :         // cppcheck-suppress constVariableReference
    1326           0 :         auto &oQueue =
    1327           0 :             m_poBaseDS ? m_poBaseDS->m_asQueueJobIdx : m_asQueueJobIdx;
    1328           0 :         while (!oQueue.empty())
    1329             :         {
    1330           0 :             WaitCompletionForJobIdx(oQueue.front());
    1331             :         }
    1332             :     }
    1333             : 
    1334             :     const auto SetupJob =
    1335      123008 :         [this, pabyData, cc, nHeight, nStripOrTile](GTiffCompressionJob &sJob)
    1336             :     {
    1337       26571 :         sJob.poDS = this;
    1338       26571 :         sJob.bTIFFIsBigEndian = CPL_TO_BOOL(TIFFIsBigEndian(m_hTIFF));
    1339             :         GByte *pabyBuffer =
    1340       26571 :             static_cast<GByte *>(VSI_REALLOC_VERBOSE(sJob.pabyBuffer, cc));
    1341       26571 :         if (!pabyBuffer)
    1342           0 :             return false;
    1343       26571 :         sJob.pabyBuffer = pabyBuffer;
    1344       26571 :         memcpy(sJob.pabyBuffer, pabyData, cc);
    1345       26571 :         sJob.nBufferSize = cc;
    1346       26571 :         sJob.nHeight = nHeight;
    1347       26571 :         sJob.nStripOrTile = nStripOrTile;
    1348       26571 :         sJob.nPredictor = PREDICTOR_NONE;
    1349       26571 :         if (GTIFFSupportsPredictor(m_nCompression))
    1350             :         {
    1351       16724 :             TIFFGetField(m_hTIFF, TIFFTAG_PREDICTOR, &sJob.nPredictor);
    1352             :         }
    1353             : 
    1354       26571 :         sJob.pExtraSamples = nullptr;
    1355       26571 :         sJob.nExtraSampleCount = 0;
    1356       26571 :         TIFFGetField(m_hTIFF, TIFFTAG_EXTRASAMPLES, &sJob.nExtraSampleCount,
    1357             :                      &sJob.pExtraSamples);
    1358       26571 :         return true;
    1359      197510 :     };
    1360             : 
    1361      197510 :     if (poQueue == nullptr || !(m_nCompression == COMPRESSION_ADOBE_DEFLATE ||
    1362         806 :                                 m_nCompression == COMPRESSION_LZW ||
    1363          78 :                                 m_nCompression == COMPRESSION_PACKBITS ||
    1364          72 :                                 m_nCompression == COMPRESSION_LZMA ||
    1365          62 :                                 m_nCompression == COMPRESSION_ZSTD ||
    1366          52 :                                 m_nCompression == COMPRESSION_LERC ||
    1367          46 :                                 m_nCompression == COMPRESSION_JXL ||
    1368          46 :                                 m_nCompression == COMPRESSION_JXL_DNG_1_7 ||
    1369          28 :                                 m_nCompression == COMPRESSION_WEBP ||
    1370          18 :                                 m_nCompression == COMPRESSION_JPEG))
    1371             :     {
    1372      195934 :         if (m_bBlockOrderRowMajor || m_bLeaderSizeAsUInt4 ||
    1373      170939 :             m_bTrailerRepeatedLast4BytesRepeated)
    1374             :         {
    1375             :             GTiffCompressionJob sJob;
    1376       24995 :             memset(&sJob, 0, sizeof(sJob));
    1377       24995 :             if (SetupJob(sJob))
    1378             :             {
    1379       24995 :                 sJob.pszTmpFilename =
    1380       24995 :                     CPLStrdup(VSIMemGenerateHiddenFilename("temp.tif"));
    1381             : 
    1382       24995 :                 ThreadCompressionFunc(&sJob);
    1383             : 
    1384       24995 :                 if (sJob.nCompressedBufferSize)
    1385             :                 {
    1386       24995 :                     sJob.poDS->WriteRawStripOrTile(sJob.nStripOrTile,
    1387             :                                                    sJob.pabyCompressedBuffer,
    1388             :                                                    sJob.nCompressedBufferSize);
    1389             :                 }
    1390             : 
    1391       24995 :                 CPLFree(sJob.pabyBuffer);
    1392       24995 :                 VSIUnlink(sJob.pszTmpFilename);
    1393       24995 :                 CPLFree(sJob.pszTmpFilename);
    1394       24995 :                 return sJob.nCompressedBufferSize > 0 && !m_bWriteError;
    1395             :             }
    1396             :         }
    1397             : 
    1398      170939 :         return false;
    1399             :     }
    1400             : 
    1401        1576 :     auto poMainDS = m_poBaseDS ? m_poBaseDS : this;
    1402        1576 :     auto &oQueue = poMainDS->m_asQueueJobIdx;
    1403        1576 :     auto &asJobs = poMainDS->m_asCompressionJobs;
    1404             : 
    1405        1576 :     int nNextCompressionJobAvail = -1;
    1406             : 
    1407        1576 :     if (oQueue.size() == asJobs.size())
    1408             :     {
    1409        1443 :         CPLAssert(!oQueue.empty());
    1410        1443 :         nNextCompressionJobAvail = oQueue.front();
    1411        1443 :         WaitCompletionForJobIdx(nNextCompressionJobAvail);
    1412             :     }
    1413             :     else
    1414             :     {
    1415         133 :         const int nJobs = static_cast<int>(asJobs.size());
    1416         324 :         for (int i = 0; i < nJobs; ++i)
    1417             :         {
    1418         324 :             if (asJobs[i].nBufferSize == 0)
    1419             :             {
    1420         133 :                 nNextCompressionJobAvail = i;
    1421         133 :                 break;
    1422             :             }
    1423             :         }
    1424             :     }
    1425        1576 :     CPLAssert(nNextCompressionJobAvail >= 0);
    1426             : 
    1427        1576 :     GTiffCompressionJob *psJob = &asJobs[nNextCompressionJobAvail];
    1428        1576 :     bool bOK = SetupJob(*psJob);
    1429        1576 :     if (bOK)
    1430             :     {
    1431        1576 :         poQueue->SubmitJob(ThreadCompressionFunc, psJob);
    1432        1576 :         oQueue.push(nNextCompressionJobAvail);
    1433             :     }
    1434             : 
    1435        1576 :     return bOK;
    1436             : }
    1437             : 
    1438             : /************************************************************************/
    1439             : /*                             DiscardLsb()                             */
    1440             : /************************************************************************/
    1441             : 
    1442         272 : template <class T> bool MustNotDiscardLsb(T value, bool bHasNoData, T nodata)
    1443             : {
    1444         272 :     return bHasNoData && value == nodata;
    1445             : }
    1446             : 
    1447             : template <>
    1448          44 : bool MustNotDiscardLsb<float>(float value, bool bHasNoData, float nodata)
    1449             : {
    1450          44 :     return (bHasNoData && value == nodata) || !std::isfinite(value);
    1451             : }
    1452             : 
    1453             : template <>
    1454          44 : bool MustNotDiscardLsb<double>(double value, bool bHasNoData, double nodata)
    1455             : {
    1456          44 :     return (bHasNoData && value == nodata) || !std::isfinite(value);
    1457             : }
    1458             : 
    1459             : template <class T> T AdjustValue(T value, uint64_t nRoundUpBitTest);
    1460             : 
    1461          10 : template <class T> T AdjustValueInt(T value, uint64_t nRoundUpBitTest)
    1462             : {
    1463          10 :     if (value >=
    1464          10 :         static_cast<T>(std::numeric_limits<T>::max() - (nRoundUpBitTest << 1)))
    1465           0 :         return static_cast<T>(value - (nRoundUpBitTest << 1));
    1466          10 :     return static_cast<T>(value + (nRoundUpBitTest << 1));
    1467             : }
    1468             : 
    1469           0 : template <> int8_t AdjustValue<int8_t>(int8_t value, uint64_t nRoundUpBitTest)
    1470             : {
    1471           0 :     return AdjustValueInt(value, nRoundUpBitTest);
    1472             : }
    1473             : 
    1474             : template <>
    1475           2 : uint8_t AdjustValue<uint8_t>(uint8_t value, uint64_t nRoundUpBitTest)
    1476             : {
    1477           2 :     return AdjustValueInt(value, nRoundUpBitTest);
    1478             : }
    1479             : 
    1480             : template <>
    1481           2 : int16_t AdjustValue<int16_t>(int16_t value, uint64_t nRoundUpBitTest)
    1482             : {
    1483           2 :     return AdjustValueInt(value, nRoundUpBitTest);
    1484             : }
    1485             : 
    1486             : template <>
    1487           2 : uint16_t AdjustValue<uint16_t>(uint16_t value, uint64_t nRoundUpBitTest)
    1488             : {
    1489           2 :     return AdjustValueInt(value, nRoundUpBitTest);
    1490             : }
    1491             : 
    1492             : template <>
    1493           2 : int32_t AdjustValue<int32_t>(int32_t value, uint64_t nRoundUpBitTest)
    1494             : {
    1495           2 :     return AdjustValueInt(value, nRoundUpBitTest);
    1496             : }
    1497             : 
    1498             : template <>
    1499           2 : uint32_t AdjustValue<uint32_t>(uint32_t value, uint64_t nRoundUpBitTest)
    1500             : {
    1501           2 :     return AdjustValueInt(value, nRoundUpBitTest);
    1502             : }
    1503             : 
    1504             : template <>
    1505           0 : int64_t AdjustValue<int64_t>(int64_t value, uint64_t nRoundUpBitTest)
    1506             : {
    1507           0 :     return AdjustValueInt(value, nRoundUpBitTest);
    1508             : }
    1509             : 
    1510             : template <>
    1511           0 : uint64_t AdjustValue<uint64_t>(uint64_t value, uint64_t nRoundUpBitTest)
    1512             : {
    1513           0 :     return AdjustValueInt(value, nRoundUpBitTest);
    1514             : }
    1515             : 
    1516           0 : template <> GFloat16 AdjustValue<GFloat16>(GFloat16 value, uint64_t)
    1517             : {
    1518             :     using std::nextafter;
    1519           0 :     return nextafter(value, cpl::NumericLimits<GFloat16>::max());
    1520             : }
    1521             : 
    1522           0 : template <> float AdjustValue<float>(float value, uint64_t)
    1523             : {
    1524           0 :     return std::nextafter(value, std::numeric_limits<float>::max());
    1525             : }
    1526             : 
    1527           0 : template <> double AdjustValue<double>(double value, uint64_t)
    1528             : {
    1529           0 :     return std::nextafter(value, std::numeric_limits<double>::max());
    1530             : }
    1531             : 
    1532             : template <class Teffective, class T>
    1533             : T RoundValueDiscardLsb(const void *ptr, uint64_t nMask,
    1534             :                        uint64_t nRoundUpBitTest);
    1535             : 
    1536             : template <class T>
    1537          16 : T RoundValueDiscardLsbUnsigned(const void *ptr, uint64_t nMask,
    1538             :                                uint64_t nRoundUpBitTest)
    1539             : {
    1540          32 :     if ((*reinterpret_cast<const T *>(ptr) & nMask) >
    1541          16 :         static_cast<uint64_t>(std::numeric_limits<T>::max()) -
    1542          16 :             (nRoundUpBitTest << 1U))
    1543             :     {
    1544           4 :         return static_cast<T>(std::numeric_limits<T>::max() & nMask);
    1545             :     }
    1546          12 :     const uint64_t newval =
    1547          12 :         (*reinterpret_cast<const T *>(ptr) & nMask) + (nRoundUpBitTest << 1U);
    1548          12 :     return static_cast<T>(newval);
    1549             : }
    1550             : 
    1551             : template <class T>
    1552          18 : T RoundValueDiscardLsbSigned(const void *ptr, uint64_t nMask,
    1553             :                              uint64_t nRoundUpBitTest)
    1554             : {
    1555          18 :     T oldval = *reinterpret_cast<const T *>(ptr);
    1556          18 :     if (oldval < 0)
    1557             :     {
    1558           4 :         return static_cast<T>(oldval & nMask);
    1559             :     }
    1560          14 :     const uint64_t newval =
    1561          14 :         (*reinterpret_cast<const T *>(ptr) & nMask) + (nRoundUpBitTest << 1U);
    1562          14 :     if (newval > static_cast<uint64_t>(std::numeric_limits<T>::max()))
    1563           4 :         return static_cast<T>(std::numeric_limits<T>::max() & nMask);
    1564          10 :     return static_cast<T>(newval);
    1565             : }
    1566             : 
    1567             : template <>
    1568          11 : uint16_t RoundValueDiscardLsb<uint16_t, uint16_t>(const void *ptr,
    1569             :                                                   uint64_t nMask,
    1570             :                                                   uint64_t nRoundUpBitTest)
    1571             : {
    1572          11 :     return RoundValueDiscardLsbUnsigned<uint16_t>(ptr, nMask, nRoundUpBitTest);
    1573             : }
    1574             : 
    1575             : template <>
    1576           5 : uint32_t RoundValueDiscardLsb<uint32_t, uint32_t>(const void *ptr,
    1577             :                                                   uint64_t nMask,
    1578             :                                                   uint64_t nRoundUpBitTest)
    1579             : {
    1580           5 :     return RoundValueDiscardLsbUnsigned<uint32_t>(ptr, nMask, nRoundUpBitTest);
    1581             : }
    1582             : 
    1583             : template <>
    1584           0 : uint64_t RoundValueDiscardLsb<uint64_t, uint64_t>(const void *ptr,
    1585             :                                                   uint64_t nMask,
    1586             :                                                   uint64_t nRoundUpBitTest)
    1587             : {
    1588           0 :     return RoundValueDiscardLsbUnsigned<uint64_t>(ptr, nMask, nRoundUpBitTest);
    1589             : }
    1590             : 
    1591             : template <>
    1592           0 : int8_t RoundValueDiscardLsb<int8_t, int8_t>(const void *ptr, uint64_t nMask,
    1593             :                                             uint64_t nRoundUpBitTest)
    1594             : {
    1595           0 :     return RoundValueDiscardLsbSigned<int8_t>(ptr, nMask, nRoundUpBitTest);
    1596             : }
    1597             : 
    1598             : template <>
    1599          13 : int16_t RoundValueDiscardLsb<int16_t, int16_t>(const void *ptr, uint64_t nMask,
    1600             :                                                uint64_t nRoundUpBitTest)
    1601             : {
    1602          13 :     return RoundValueDiscardLsbSigned<int16_t>(ptr, nMask, nRoundUpBitTest);
    1603             : }
    1604             : 
    1605             : template <>
    1606           5 : int32_t RoundValueDiscardLsb<int32_t, int32_t>(const void *ptr, uint64_t nMask,
    1607             :                                                uint64_t nRoundUpBitTest)
    1608             : {
    1609           5 :     return RoundValueDiscardLsbSigned<int32_t>(ptr, nMask, nRoundUpBitTest);
    1610             : }
    1611             : 
    1612             : template <>
    1613           0 : int64_t RoundValueDiscardLsb<int64_t, int64_t>(const void *ptr, uint64_t nMask,
    1614             :                                                uint64_t nRoundUpBitTest)
    1615             : {
    1616           0 :     return RoundValueDiscardLsbSigned<int64_t>(ptr, nMask, nRoundUpBitTest);
    1617             : }
    1618             : 
    1619             : template <>
    1620           0 : uint16_t RoundValueDiscardLsb<GFloat16, uint16_t>(const void *ptr,
    1621             :                                                   uint64_t nMask,
    1622             :                                                   uint64_t nRoundUpBitTest)
    1623             : {
    1624           0 :     return RoundValueDiscardLsbUnsigned<uint16_t>(ptr, nMask, nRoundUpBitTest);
    1625             : }
    1626             : 
    1627             : template <>
    1628           0 : uint32_t RoundValueDiscardLsb<float, uint32_t>(const void *ptr, uint64_t nMask,
    1629             :                                                uint64_t nRoundUpBitTest)
    1630             : {
    1631           0 :     return RoundValueDiscardLsbUnsigned<uint32_t>(ptr, nMask, nRoundUpBitTest);
    1632             : }
    1633             : 
    1634             : template <>
    1635           0 : uint64_t RoundValueDiscardLsb<double, uint64_t>(const void *ptr, uint64_t nMask,
    1636             :                                                 uint64_t nRoundUpBitTest)
    1637             : {
    1638           0 :     return RoundValueDiscardLsbUnsigned<uint64_t>(ptr, nMask, nRoundUpBitTest);
    1639             : }
    1640             : 
    1641             : template <class Teffective, class T>
    1642         145 : static void DiscardLsbT(GByte *pabyBuffer, size_t nBytes, int iBand, int nBands,
    1643             :                         uint16_t nPlanarConfig,
    1644             :                         const GTiffDataset::MaskOffset *panMaskOffsetLsb,
    1645             :                         bool bHasNoData, Teffective nNoDataValue)
    1646             : {
    1647             :     static_assert(sizeof(Teffective) == sizeof(T),
    1648             :                   "sizeof(Teffective) == sizeof(T)");
    1649         145 :     if (nPlanarConfig == PLANARCONFIG_SEPARATE)
    1650             :     {
    1651          98 :         const auto nMask = panMaskOffsetLsb[iBand].nMask;
    1652          98 :         const auto nRoundUpBitTest = panMaskOffsetLsb[iBand].nRoundUpBitTest;
    1653         196 :         for (size_t i = 0; i < nBytes / sizeof(T); ++i)
    1654             :         {
    1655          98 :             if (MustNotDiscardLsb(reinterpret_cast<Teffective *>(pabyBuffer)[i],
    1656             :                                   bHasNoData, nNoDataValue))
    1657             :             {
    1658          22 :                 continue;
    1659             :             }
    1660             : 
    1661          76 :             if (reinterpret_cast<T *>(pabyBuffer)[i] & nRoundUpBitTest)
    1662             :             {
    1663          30 :                 reinterpret_cast<T *>(pabyBuffer)[i] =
    1664          15 :                     RoundValueDiscardLsb<Teffective, T>(
    1665          15 :                         &(reinterpret_cast<T *>(pabyBuffer)[i]), nMask,
    1666             :                         nRoundUpBitTest);
    1667             :             }
    1668             :             else
    1669             :             {
    1670          61 :                 reinterpret_cast<T *>(pabyBuffer)[i] = static_cast<T>(
    1671          61 :                     reinterpret_cast<T *>(pabyBuffer)[i] & nMask);
    1672             :             }
    1673             : 
    1674             :             // Make sure that by discarding LSB we don't end up to a value
    1675             :             // that is no the nodata value
    1676          76 :             if (MustNotDiscardLsb(reinterpret_cast<Teffective *>(pabyBuffer)[i],
    1677             :                                   bHasNoData, nNoDataValue))
    1678             :             {
    1679           8 :                 reinterpret_cast<Teffective *>(pabyBuffer)[i] =
    1680           4 :                     AdjustValue(nNoDataValue, nRoundUpBitTest);
    1681             :             }
    1682             :         }
    1683             :     }
    1684             :     else
    1685             :     {
    1686          94 :         for (size_t i = 0; i < nBytes / sizeof(T); i += nBands)
    1687             :         {
    1688         147 :             for (int j = 0; j < nBands; ++j)
    1689             :             {
    1690         100 :                 if (MustNotDiscardLsb(
    1691         100 :                         reinterpret_cast<Teffective *>(pabyBuffer)[i + j],
    1692             :                         bHasNoData, nNoDataValue))
    1693             :                 {
    1694          14 :                     continue;
    1695             :                 }
    1696             : 
    1697          86 :                 if (reinterpret_cast<T *>(pabyBuffer)[i + j] &
    1698          86 :                     panMaskOffsetLsb[j].nRoundUpBitTest)
    1699             :                 {
    1700          38 :                     reinterpret_cast<T *>(pabyBuffer)[i + j] =
    1701          19 :                         RoundValueDiscardLsb<Teffective, T>(
    1702          19 :                             &(reinterpret_cast<T *>(pabyBuffer)[i + j]),
    1703          19 :                             panMaskOffsetLsb[j].nMask,
    1704          19 :                             panMaskOffsetLsb[j].nRoundUpBitTest);
    1705             :                 }
    1706             :                 else
    1707             :                 {
    1708          67 :                     reinterpret_cast<T *>(pabyBuffer)[i + j] = static_cast<T>(
    1709          67 :                         (reinterpret_cast<T *>(pabyBuffer)[i + j] &
    1710          67 :                          panMaskOffsetLsb[j].nMask));
    1711             :                 }
    1712             : 
    1713             :                 // Make sure that by discarding LSB we don't end up to a value
    1714             :                 // that is no the nodata value
    1715          86 :                 if (MustNotDiscardLsb(
    1716          86 :                         reinterpret_cast<Teffective *>(pabyBuffer)[i + j],
    1717             :                         bHasNoData, nNoDataValue))
    1718             :                 {
    1719           8 :                     reinterpret_cast<Teffective *>(pabyBuffer)[i + j] =
    1720           4 :                         AdjustValue(nNoDataValue,
    1721           4 :                                     panMaskOffsetLsb[j].nRoundUpBitTest);
    1722             :                 }
    1723             :             }
    1724             :         }
    1725             :     }
    1726         145 : }
    1727             : 
    1728         183 : static void DiscardLsb(GByte *pabyBuffer, GPtrDiff_t nBytes, int iBand,
    1729             :                        int nBands, uint16_t nSampleFormat,
    1730             :                        uint16_t nBitsPerSample, uint16_t nPlanarConfig,
    1731             :                        const GTiffDataset::MaskOffset *panMaskOffsetLsb,
    1732             :                        bool bHasNoData, double dfNoDataValue)
    1733             : {
    1734         183 :     if (nBitsPerSample == 8 && nSampleFormat == SAMPLEFORMAT_UINT)
    1735             :     {
    1736          38 :         uint8_t nNoDataValue = 0;
    1737          38 :         if (bHasNoData && GDALIsValueExactAs<uint8_t>(dfNoDataValue))
    1738             :         {
    1739           6 :             nNoDataValue = static_cast<uint8_t>(dfNoDataValue);
    1740             :         }
    1741             :         else
    1742             :         {
    1743          32 :             bHasNoData = false;
    1744             :         }
    1745          38 :         if (nPlanarConfig == PLANARCONFIG_SEPARATE)
    1746             :         {
    1747          25 :             const auto nMask =
    1748          25 :                 static_cast<unsigned>(panMaskOffsetLsb[iBand].nMask);
    1749          25 :             const auto nRoundUpBitTest =
    1750          25 :                 static_cast<unsigned>(panMaskOffsetLsb[iBand].nRoundUpBitTest);
    1751          50 :             for (decltype(nBytes) i = 0; i < nBytes; ++i)
    1752             :             {
    1753          25 :                 if (bHasNoData && pabyBuffer[i] == nNoDataValue)
    1754           3 :                     continue;
    1755             : 
    1756             :                 // Keep 255 in case it is alpha.
    1757          22 :                 if (pabyBuffer[i] != 255)
    1758             :                 {
    1759          21 :                     if (pabyBuffer[i] & nRoundUpBitTest)
    1760           5 :                         pabyBuffer[i] = static_cast<GByte>(
    1761           5 :                             std::min(255U, (pabyBuffer[i] & nMask) +
    1762           5 :                                                (nRoundUpBitTest << 1U)));
    1763             :                     else
    1764          16 :                         pabyBuffer[i] =
    1765          16 :                             static_cast<GByte>(pabyBuffer[i] & nMask);
    1766             : 
    1767             :                     // Make sure that by discarding LSB we don't end up to a
    1768             :                     // value that is no the nodata value
    1769          21 :                     if (bHasNoData && pabyBuffer[i] == nNoDataValue)
    1770           2 :                         pabyBuffer[i] =
    1771           1 :                             AdjustValue(nNoDataValue, nRoundUpBitTest);
    1772             :                 }
    1773             :             }
    1774             :         }
    1775             :         else
    1776             :         {
    1777          26 :             for (decltype(nBytes) i = 0; i < nBytes; i += nBands)
    1778             :             {
    1779          42 :                 for (int j = 0; j < nBands; ++j)
    1780             :                 {
    1781          29 :                     if (bHasNoData && pabyBuffer[i + j] == nNoDataValue)
    1782           2 :                         continue;
    1783             : 
    1784             :                     // Keep 255 in case it is alpha.
    1785          27 :                     if (pabyBuffer[i + j] != 255)
    1786             :                     {
    1787          25 :                         if (pabyBuffer[i + j] &
    1788          25 :                             panMaskOffsetLsb[j].nRoundUpBitTest)
    1789             :                         {
    1790           6 :                             pabyBuffer[i + j] = static_cast<GByte>(std::min(
    1791          12 :                                 255U,
    1792           6 :                                 (pabyBuffer[i + j] &
    1793             :                                  static_cast<unsigned>(
    1794           6 :                                      panMaskOffsetLsb[j].nMask)) +
    1795             :                                     (static_cast<unsigned>(
    1796           6 :                                          panMaskOffsetLsb[j].nRoundUpBitTest)
    1797           6 :                                      << 1U)));
    1798             :                         }
    1799             :                         else
    1800             :                         {
    1801          19 :                             pabyBuffer[i + j] = static_cast<GByte>(
    1802          19 :                                 pabyBuffer[i + j] & panMaskOffsetLsb[j].nMask);
    1803             :                         }
    1804             : 
    1805             :                         // Make sure that by discarding LSB we don't end up to a
    1806             :                         // value that is no the nodata value
    1807          25 :                         if (bHasNoData && pabyBuffer[i + j] == nNoDataValue)
    1808           1 :                             pabyBuffer[i + j] = AdjustValue(
    1809             :                                 nNoDataValue,
    1810           1 :                                 panMaskOffsetLsb[j].nRoundUpBitTest);
    1811             :                     }
    1812             :                 }
    1813             :             }
    1814          38 :         }
    1815             :     }
    1816         145 :     else if (nBitsPerSample == 8 && nSampleFormat == SAMPLEFORMAT_INT)
    1817             :     {
    1818           0 :         int8_t nNoDataValue = 0;
    1819           0 :         if (bHasNoData && GDALIsValueExactAs<int8_t>(dfNoDataValue))
    1820             :         {
    1821           0 :             nNoDataValue = static_cast<int8_t>(dfNoDataValue);
    1822             :         }
    1823             :         else
    1824             :         {
    1825           0 :             bHasNoData = false;
    1826             :         }
    1827           0 :         DiscardLsbT<int8_t, int8_t>(pabyBuffer, nBytes, iBand, nBands,
    1828             :                                     nPlanarConfig, panMaskOffsetLsb, bHasNoData,
    1829           0 :                                     nNoDataValue);
    1830             :     }
    1831         145 :     else if (nBitsPerSample == 16 && nSampleFormat == SAMPLEFORMAT_INT)
    1832             :     {
    1833          48 :         int16_t nNoDataValue = 0;
    1834          48 :         if (bHasNoData && GDALIsValueExactAs<int16_t>(dfNoDataValue))
    1835             :         {
    1836           6 :             nNoDataValue = static_cast<int16_t>(dfNoDataValue);
    1837             :         }
    1838             :         else
    1839             :         {
    1840          42 :             bHasNoData = false;
    1841             :         }
    1842          48 :         DiscardLsbT<int16_t, int16_t>(pabyBuffer, nBytes, iBand, nBands,
    1843             :                                       nPlanarConfig, panMaskOffsetLsb,
    1844          48 :                                       bHasNoData, nNoDataValue);
    1845             :     }
    1846          97 :     else if (nBitsPerSample == 16 && nSampleFormat == SAMPLEFORMAT_UINT)
    1847             :     {
    1848          33 :         uint16_t nNoDataValue = 0;
    1849          33 :         if (bHasNoData && GDALIsValueExactAs<uint16_t>(dfNoDataValue))
    1850             :         {
    1851           6 :             nNoDataValue = static_cast<uint16_t>(dfNoDataValue);
    1852             :         }
    1853             :         else
    1854             :         {
    1855          27 :             bHasNoData = false;
    1856             :         }
    1857          33 :         DiscardLsbT<uint16_t, uint16_t>(pabyBuffer, nBytes, iBand, nBands,
    1858             :                                         nPlanarConfig, panMaskOffsetLsb,
    1859          33 :                                         bHasNoData, nNoDataValue);
    1860             :     }
    1861          64 :     else if (nBitsPerSample == 32 && nSampleFormat == SAMPLEFORMAT_INT)
    1862             :     {
    1863          13 :         int32_t nNoDataValue = 0;
    1864          13 :         if (bHasNoData && GDALIsValueExactAs<int32_t>(dfNoDataValue))
    1865             :         {
    1866           6 :             nNoDataValue = static_cast<int32_t>(dfNoDataValue);
    1867             :         }
    1868             :         else
    1869             :         {
    1870           7 :             bHasNoData = false;
    1871             :         }
    1872          13 :         DiscardLsbT<int32_t, int32_t>(pabyBuffer, nBytes, iBand, nBands,
    1873             :                                       nPlanarConfig, panMaskOffsetLsb,
    1874          13 :                                       bHasNoData, nNoDataValue);
    1875             :     }
    1876          51 :     else if (nBitsPerSample == 32 && nSampleFormat == SAMPLEFORMAT_UINT)
    1877             :     {
    1878          13 :         uint32_t nNoDataValue = 0;
    1879          13 :         if (bHasNoData && GDALIsValueExactAs<uint32_t>(dfNoDataValue))
    1880             :         {
    1881           6 :             nNoDataValue = static_cast<uint32_t>(dfNoDataValue);
    1882             :         }
    1883             :         else
    1884             :         {
    1885           7 :             bHasNoData = false;
    1886             :         }
    1887          13 :         DiscardLsbT<uint32_t, uint32_t>(pabyBuffer, nBytes, iBand, nBands,
    1888             :                                         nPlanarConfig, panMaskOffsetLsb,
    1889          13 :                                         bHasNoData, nNoDataValue);
    1890             :     }
    1891          38 :     else if (nBitsPerSample == 64 && nSampleFormat == SAMPLEFORMAT_INT)
    1892             :     {
    1893             :         // FIXME: we should not rely on dfNoDataValue when we support native
    1894             :         // data type for nodata
    1895           0 :         int64_t nNoDataValue = 0;
    1896           0 :         if (bHasNoData && GDALIsValueExactAs<int64_t>(dfNoDataValue))
    1897             :         {
    1898           0 :             nNoDataValue = static_cast<int64_t>(dfNoDataValue);
    1899             :         }
    1900             :         else
    1901             :         {
    1902           0 :             bHasNoData = false;
    1903             :         }
    1904           0 :         DiscardLsbT<int64_t, int64_t>(pabyBuffer, nBytes, iBand, nBands,
    1905             :                                       nPlanarConfig, panMaskOffsetLsb,
    1906           0 :                                       bHasNoData, nNoDataValue);
    1907             :     }
    1908          38 :     else if (nBitsPerSample == 64 && nSampleFormat == SAMPLEFORMAT_UINT)
    1909             :     {
    1910             :         // FIXME: we should not rely on dfNoDataValue when we support native
    1911             :         // data type for nodata
    1912           0 :         uint64_t nNoDataValue = 0;
    1913           0 :         if (bHasNoData && GDALIsValueExactAs<uint64_t>(dfNoDataValue))
    1914             :         {
    1915           0 :             nNoDataValue = static_cast<uint64_t>(dfNoDataValue);
    1916             :         }
    1917             :         else
    1918             :         {
    1919           0 :             bHasNoData = false;
    1920             :         }
    1921           0 :         DiscardLsbT<uint64_t, uint64_t>(pabyBuffer, nBytes, iBand, nBands,
    1922             :                                         nPlanarConfig, panMaskOffsetLsb,
    1923           0 :                                         bHasNoData, nNoDataValue);
    1924             :     }
    1925          38 :     else if (nBitsPerSample == 16 && nSampleFormat == SAMPLEFORMAT_IEEEFP)
    1926             :     {
    1927           0 :         const GFloat16 fNoDataValue = static_cast<GFloat16>(dfNoDataValue);
    1928           0 :         DiscardLsbT<GFloat16, uint16_t>(pabyBuffer, nBytes, iBand, nBands,
    1929             :                                         nPlanarConfig, panMaskOffsetLsb,
    1930           0 :                                         bHasNoData, fNoDataValue);
    1931             :     }
    1932          38 :     else if (nBitsPerSample == 32 && nSampleFormat == SAMPLEFORMAT_IEEEFP)
    1933             :     {
    1934          19 :         const float fNoDataValue = static_cast<float>(dfNoDataValue);
    1935          19 :         DiscardLsbT<float, uint32_t>(pabyBuffer, nBytes, iBand, nBands,
    1936             :                                      nPlanarConfig, panMaskOffsetLsb,
    1937          19 :                                      bHasNoData, fNoDataValue);
    1938             :     }
    1939          19 :     else if (nBitsPerSample == 64 && nSampleFormat == SAMPLEFORMAT_IEEEFP)
    1940             :     {
    1941          19 :         DiscardLsbT<double, uint64_t>(pabyBuffer, nBytes, iBand, nBands,
    1942             :                                       nPlanarConfig, panMaskOffsetLsb,
    1943             :                                       bHasNoData, dfNoDataValue);
    1944             :     }
    1945         183 : }
    1946             : 
    1947         183 : void GTiffDataset::DiscardLsb(GByte *pabyBuffer, GPtrDiff_t nBytes,
    1948             :                               int iBand) const
    1949             : {
    1950         183 :     ::DiscardLsb(pabyBuffer, nBytes, iBand, nBands, m_nSampleFormat,
    1951         183 :                  m_nBitsPerSample, m_nPlanarConfig, m_panMaskOffsetLsb,
    1952         183 :                  m_bNoDataSet, m_dfNoDataValue);
    1953         183 : }
    1954             : 
    1955             : /************************************************************************/
    1956             : /*                      WriteEncodedTileOrStrip()                       */
    1957             : /************************************************************************/
    1958             : 
    1959      228414 : CPLErr GTiffDataset::WriteEncodedTileOrStrip(uint32_t tile_or_strip, void *data,
    1960             :                                              int bPreserveDataBuffer)
    1961             : {
    1962      228414 :     CPLErr eErr = CE_None;
    1963             : 
    1964      228414 :     if (TIFFIsTiled(m_hTIFF))
    1965             :     {
    1966       50351 :         if (!(WriteEncodedTile(tile_or_strip, static_cast<GByte *>(data),
    1967             :                                bPreserveDataBuffer)))
    1968             :         {
    1969          14 :             eErr = CE_Failure;
    1970             :         }
    1971             :     }
    1972             :     else
    1973             :     {
    1974      178063 :         if (!(WriteEncodedStrip(tile_or_strip, static_cast<GByte *>(data),
    1975             :                                 bPreserveDataBuffer)))
    1976             :         {
    1977           8 :             eErr = CE_Failure;
    1978             :         }
    1979             :     }
    1980             : 
    1981      228414 :     return eErr;
    1982             : }
    1983             : 
    1984             : /************************************************************************/
    1985             : /*                           FlushBlockBuf()                            */
    1986             : /************************************************************************/
    1987             : 
    1988        9602 : CPLErr GTiffDataset::FlushBlockBuf()
    1989             : 
    1990             : {
    1991        9602 :     if (m_nLoadedBlock < 0 || !m_bLoadedBlockDirty)
    1992           0 :         return CE_None;
    1993             : 
    1994        9602 :     m_bLoadedBlockDirty = false;
    1995             : 
    1996             :     const CPLErr eErr =
    1997        9602 :         WriteEncodedTileOrStrip(m_nLoadedBlock, m_pabyBlockBuf, true);
    1998        9602 :     if (eErr != CE_None)
    1999             :     {
    2000           0 :         ReportError(CE_Failure, CPLE_AppDefined,
    2001             :                     "WriteEncodedTile/Strip() failed.");
    2002           0 :         m_bWriteError = true;
    2003             :     }
    2004             : 
    2005        9602 :     return eErr;
    2006             : }
    2007             : 
    2008             : /************************************************************************/
    2009             : /*                 GTiffFillStreamableOffsetAndCount()                  */
    2010             : /************************************************************************/
    2011             : 
    2012           8 : static void GTiffFillStreamableOffsetAndCount(TIFF *hTIFF, int nSize)
    2013             : {
    2014           8 :     uint32_t nXSize = 0;
    2015           8 :     uint32_t nYSize = 0;
    2016           8 :     TIFFGetField(hTIFF, TIFFTAG_IMAGEWIDTH, &nXSize);
    2017           8 :     TIFFGetField(hTIFF, TIFFTAG_IMAGELENGTH, &nYSize);
    2018           8 :     const bool bIsTiled = CPL_TO_BOOL(TIFFIsTiled(hTIFF));
    2019             :     const int nBlockCount =
    2020           8 :         bIsTiled ? TIFFNumberOfTiles(hTIFF) : TIFFNumberOfStrips(hTIFF);
    2021             : 
    2022           8 :     toff_t *panOffset = nullptr;
    2023           8 :     TIFFGetField(hTIFF, bIsTiled ? TIFFTAG_TILEOFFSETS : TIFFTAG_STRIPOFFSETS,
    2024             :                  &panOffset);
    2025           8 :     toff_t *panSize = nullptr;
    2026           8 :     TIFFGetField(hTIFF,
    2027             :                  bIsTiled ? TIFFTAG_TILEBYTECOUNTS : TIFFTAG_STRIPBYTECOUNTS,
    2028             :                  &panSize);
    2029           8 :     toff_t nOffset = nSize;
    2030             :     // Trick to avoid clang static analyzer raising false positive about
    2031             :     // divide by zero later.
    2032           8 :     int nBlocksPerBand = 1;
    2033           8 :     uint32_t nRowsPerStrip = 0;
    2034           8 :     if (!bIsTiled)
    2035             :     {
    2036           6 :         TIFFGetField(hTIFF, TIFFTAG_ROWSPERSTRIP, &nRowsPerStrip);
    2037           6 :         if (nRowsPerStrip > static_cast<uint32_t>(nYSize))
    2038           0 :             nRowsPerStrip = nYSize;
    2039           6 :         nBlocksPerBand = DIV_ROUND_UP(nYSize, nRowsPerStrip);
    2040             :     }
    2041        2947 :     for (int i = 0; i < nBlockCount; ++i)
    2042             :     {
    2043             :         GPtrDiff_t cc = bIsTiled
    2044        2939 :                             ? static_cast<GPtrDiff_t>(TIFFTileSize(hTIFF))
    2045        2907 :                             : static_cast<GPtrDiff_t>(TIFFStripSize(hTIFF));
    2046        2939 :         if (!bIsTiled)
    2047             :         {
    2048             :             /* --------------------------------------------------------------------
    2049             :              */
    2050             :             /*      If this is the last strip in the image, and is partial, then
    2051             :              */
    2052             :             /*      we need to trim the number of scanlines written to the */
    2053             :             /*      amount of valid data we have. (#2748) */
    2054             :             /* --------------------------------------------------------------------
    2055             :              */
    2056        2907 :             int nStripWithinBand = i % nBlocksPerBand;
    2057        2907 :             if (nStripWithinBand * nRowsPerStrip > nYSize - nRowsPerStrip)
    2058             :             {
    2059           1 :                 cc = (cc / nRowsPerStrip) *
    2060           1 :                      (nYSize - nStripWithinBand * nRowsPerStrip);
    2061             :             }
    2062             :         }
    2063        2939 :         panOffset[i] = nOffset;
    2064        2939 :         panSize[i] = cc;
    2065        2939 :         nOffset += cc;
    2066             :     }
    2067           8 : }
    2068             : 
    2069             : /************************************************************************/
    2070             : /*                             Crystalize()                             */
    2071             : /*                                                                      */
    2072             : /*      Make sure that the directory information is written out for     */
    2073             : /*      a new file, require before writing any imagery data.            */
    2074             : /************************************************************************/
    2075             : 
    2076     2653510 : void GTiffDataset::Crystalize()
    2077             : 
    2078             : {
    2079     2653510 :     if (m_bCrystalized)
    2080     2647790 :         return;
    2081             : 
    2082             :     // TODO: libtiff writes extended tags in the order they are specified
    2083             :     // and not in increasing order.
    2084        5716 :     WriteMetadata(this, m_hTIFF, true, m_eProfile, m_osFilename.c_str(),
    2085        5716 :                   m_papszCreationOptions);
    2086        5716 :     WriteGeoTIFFInfo();
    2087        5716 :     if (m_bNoDataSet)
    2088         339 :         WriteNoDataValue(m_hTIFF, m_dfNoDataValue);
    2089        5377 :     else if (m_bNoDataSetAsInt64)
    2090           4 :         WriteNoDataValue(m_hTIFF, m_nNoDataValueInt64);
    2091        5373 :     else if (m_bNoDataSetAsUInt64)
    2092           4 :         WriteNoDataValue(m_hTIFF, m_nNoDataValueUInt64);
    2093             : 
    2094        5716 :     m_bMetadataChanged = false;
    2095        5716 :     m_bGeoTIFFInfoChanged = false;
    2096        5716 :     m_bNoDataChanged = false;
    2097        5716 :     m_bNeedsRewrite = false;
    2098             : 
    2099        5716 :     m_bCrystalized = true;
    2100             : 
    2101        5716 :     TIFFWriteCheck(m_hTIFF, TIFFIsTiled(m_hTIFF), "GTiffDataset::Crystalize");
    2102             : 
    2103        5716 :     TIFFWriteDirectory(m_hTIFF);
    2104        5716 :     if (m_bStreamingOut)
    2105             :     {
    2106             :         // We need to write twice the directory to be sure that custom
    2107             :         // TIFF tags are correctly sorted and that padding bytes have been
    2108             :         // added.
    2109           3 :         TIFFSetDirectory(m_hTIFF, 0);
    2110           3 :         TIFFWriteDirectory(m_hTIFF);
    2111             : 
    2112           3 :         if (VSIFSeekL(m_fpL, 0, SEEK_END) != 0)
    2113             :         {
    2114           0 :             ReportError(CE_Failure, CPLE_FileIO, "Could not seek");
    2115             :         }
    2116           3 :         const int nSize = static_cast<int>(VSIFTellL(m_fpL));
    2117             : 
    2118           3 :         TIFFSetDirectory(m_hTIFF, 0);
    2119           3 :         GTiffFillStreamableOffsetAndCount(m_hTIFF, nSize);
    2120           3 :         TIFFWriteDirectory(m_hTIFF);
    2121             : 
    2122           3 :         vsi_l_offset nDataLength = 0;
    2123             :         void *pabyBuffer =
    2124           3 :             VSIGetMemFileBuffer(m_pszTmpFilename, &nDataLength, FALSE);
    2125           3 :         if (static_cast<int>(VSIFWriteL(
    2126           3 :                 pabyBuffer, 1, static_cast<int>(nDataLength), m_fpToWrite)) !=
    2127             :             static_cast<int>(nDataLength))
    2128             :         {
    2129           0 :             ReportError(CE_Failure, CPLE_FileIO, "Could not write %d bytes",
    2130             :                         static_cast<int>(nDataLength));
    2131             :         }
    2132             :         // In case of single strip file, there's a libtiff check that would
    2133             :         // issue a warning since the file hasn't the required size.
    2134           3 :         CPLPushErrorHandler(CPLQuietErrorHandler);
    2135           3 :         TIFFSetDirectory(m_hTIFF, 0);
    2136           3 :         CPLPopErrorHandler();
    2137             :     }
    2138             :     else
    2139             :     {
    2140        5713 :         const tdir_t nNumberOfDirs = TIFFNumberOfDirectories(m_hTIFF);
    2141        5713 :         if (nNumberOfDirs > 0)
    2142             :         {
    2143        5713 :             TIFFSetDirectory(m_hTIFF, static_cast<tdir_t>(nNumberOfDirs - 1));
    2144             :         }
    2145             :     }
    2146             : 
    2147        5716 :     RestoreVolatileParameters(m_hTIFF);
    2148             : 
    2149        5716 :     m_nDirOffset = TIFFCurrentDirOffset(m_hTIFF);
    2150             : }
    2151             : 
    2152             : /************************************************************************/
    2153             : /*                             FlushCache()                             */
    2154             : /*                                                                      */
    2155             : /*      We override this so we can also flush out local tiff strip      */
    2156             : /*      cache if need be.                                               */
    2157             : /************************************************************************/
    2158             : 
    2159        4532 : CPLErr GTiffDataset::FlushCache(bool bAtClosing)
    2160             : 
    2161             : {
    2162        4532 :     return FlushCacheInternal(bAtClosing, true);
    2163             : }
    2164             : 
    2165       46287 : CPLErr GTiffDataset::FlushCacheInternal(bool bAtClosing, bool bFlushDirectory)
    2166             : {
    2167       46287 :     if (m_bIsFinalized)
    2168           1 :         return CE_None;
    2169             : 
    2170       46286 :     CPLErr eErr = GDALPamDataset::FlushCache(bAtClosing);
    2171             : 
    2172       46286 :     if (m_bLoadedBlockDirty && m_nLoadedBlock != -1)
    2173             :     {
    2174         262 :         if (FlushBlockBuf() != CE_None)
    2175           0 :             eErr = CE_Failure;
    2176             :     }
    2177             : 
    2178       46286 :     CPLFree(m_pabyBlockBuf);
    2179       46286 :     m_pabyBlockBuf = nullptr;
    2180       46286 :     m_nLoadedBlock = -1;
    2181       46286 :     m_bLoadedBlockDirty = false;
    2182             : 
    2183             :     // Finish compression
    2184       46286 :     auto poQueue = m_poBaseDS ? m_poBaseDS->m_poCompressQueue.get()
    2185       43969 :                               : m_poCompressQueue.get();
    2186       46286 :     if (poQueue)
    2187             :     {
    2188         161 :         poQueue->WaitCompletion();
    2189             : 
    2190             :         // Flush remaining data
    2191             :         // cppcheck-suppress constVariableReference
    2192             : 
    2193         161 :         auto &oQueue =
    2194         161 :             m_poBaseDS ? m_poBaseDS->m_asQueueJobIdx : m_asQueueJobIdx;
    2195         230 :         while (!oQueue.empty())
    2196             :         {
    2197          69 :             WaitCompletionForJobIdx(oQueue.front());
    2198             :         }
    2199             :     }
    2200             : 
    2201       46286 :     if (bFlushDirectory && GetAccess() == GA_Update)
    2202             :     {
    2203       13831 :         if (FlushDirectory() != CE_None)
    2204          12 :             eErr = CE_Failure;
    2205             :     }
    2206       46286 :     return eErr;
    2207             : }
    2208             : 
    2209             : /************************************************************************/
    2210             : /*                           FlushDirectory()                           */
    2211             : /************************************************************************/
    2212             : 
    2213       21730 : CPLErr GTiffDataset::FlushDirectory()
    2214             : 
    2215             : {
    2216       21730 :     CPLErr eErr = CE_None;
    2217             : 
    2218         682 :     const auto ReloadAllOtherDirectories = [this]()
    2219             :     {
    2220         336 :         const auto poBaseDS = m_poBaseDS ? m_poBaseDS : this;
    2221         339 :         for (auto &poOvrDS : poBaseDS->m_apoOverviewDS)
    2222             :         {
    2223           3 :             if (poOvrDS->m_bCrystalized && poOvrDS.get() != this)
    2224             :             {
    2225           3 :                 poOvrDS->ReloadDirectory(true);
    2226             :             }
    2227             : 
    2228           3 :             if (poOvrDS->m_poMaskDS && poOvrDS->m_poMaskDS.get() != this &&
    2229           0 :                 poOvrDS->m_poMaskDS->m_bCrystalized)
    2230             :             {
    2231           0 :                 poOvrDS->m_poMaskDS->ReloadDirectory(true);
    2232             :             }
    2233             :         }
    2234         336 :         if (poBaseDS->m_poMaskDS && poBaseDS->m_poMaskDS.get() != this &&
    2235           0 :             poBaseDS->m_poMaskDS->m_bCrystalized)
    2236             :         {
    2237           0 :             poBaseDS->m_poMaskDS->ReloadDirectory(true);
    2238             :         }
    2239         336 :         if (poBaseDS->m_bCrystalized && poBaseDS != this)
    2240             :         {
    2241           7 :             poBaseDS->ReloadDirectory(true);
    2242             :         }
    2243         336 :     };
    2244             : 
    2245       21730 :     if (eAccess == GA_Update)
    2246             :     {
    2247       15499 :         if (m_bMetadataChanged)
    2248             :         {
    2249         199 :             m_bNeedsRewrite =
    2250         199 :                 WriteMetadata(this, m_hTIFF, true, m_eProfile,
    2251         199 :                               m_osFilename.c_str(), m_papszCreationOptions);
    2252         199 :             m_bMetadataChanged = false;
    2253             : 
    2254         199 :             if (m_bForceUnsetRPC)
    2255             :             {
    2256           5 :                 double *padfRPCTag = nullptr;
    2257             :                 uint16_t nCount;
    2258           5 :                 if (TIFFGetField(m_hTIFF, TIFFTAG_RPCCOEFFICIENT, &nCount,
    2259           5 :                                  &padfRPCTag))
    2260             :                 {
    2261           3 :                     std::vector<double> zeroes(92);
    2262           3 :                     TIFFSetField(m_hTIFF, TIFFTAG_RPCCOEFFICIENT, 92,
    2263             :                                  zeroes.data());
    2264           3 :                     TIFFUnsetField(m_hTIFF, TIFFTAG_RPCCOEFFICIENT);
    2265           3 :                     m_bNeedsRewrite = true;
    2266             :                 }
    2267             : 
    2268           5 :                 if (m_poBaseDS == nullptr)
    2269             :                 {
    2270           5 :                     GDALWriteRPCTXTFile(m_osFilename.c_str(), nullptr);
    2271           5 :                     GDALWriteRPBFile(m_osFilename.c_str(), nullptr);
    2272             :                 }
    2273             :             }
    2274             :         }
    2275             : 
    2276       15499 :         if (m_bGeoTIFFInfoChanged)
    2277             :         {
    2278         145 :             WriteGeoTIFFInfo();
    2279         145 :             m_bGeoTIFFInfoChanged = false;
    2280             :         }
    2281             : 
    2282       15499 :         if (m_bNoDataChanged)
    2283             :         {
    2284          53 :             if (m_bNoDataSet)
    2285             :             {
    2286          37 :                 WriteNoDataValue(m_hTIFF, m_dfNoDataValue);
    2287             :             }
    2288          16 :             else if (m_bNoDataSetAsInt64)
    2289             :             {
    2290           0 :                 WriteNoDataValue(m_hTIFF, m_nNoDataValueInt64);
    2291             :             }
    2292          16 :             else if (m_bNoDataSetAsUInt64)
    2293             :             {
    2294           0 :                 WriteNoDataValue(m_hTIFF, m_nNoDataValueUInt64);
    2295             :             }
    2296             :             else
    2297             :             {
    2298          16 :                 UnsetNoDataValue(m_hTIFF);
    2299             :             }
    2300          53 :             m_bNeedsRewrite = true;
    2301          53 :             m_bNoDataChanged = false;
    2302             :         }
    2303             : 
    2304       15499 :         if (m_bNeedsRewrite)
    2305             :         {
    2306         361 :             if (!m_bCrystalized)
    2307             :             {
    2308          28 :                 Crystalize();
    2309             :             }
    2310             :             else
    2311             :             {
    2312         333 :                 const TIFFSizeProc pfnSizeProc = TIFFGetSizeProc(m_hTIFF);
    2313             : 
    2314         333 :                 m_nDirOffset = pfnSizeProc(TIFFClientdata(m_hTIFF));
    2315         333 :                 if ((m_nDirOffset % 2) == 1)
    2316          69 :                     ++m_nDirOffset;
    2317             : 
    2318         333 :                 if (TIFFRewriteDirectory(m_hTIFF) == 0)
    2319           0 :                     eErr = CE_Failure;
    2320             : 
    2321         333 :                 TIFFSetSubDirectory(m_hTIFF, m_nDirOffset);
    2322             : 
    2323         333 :                 ReloadAllOtherDirectories();
    2324             : 
    2325         333 :                 if (m_bLayoutIFDSBeforeData && m_bBlockOrderRowMajor &&
    2326           1 :                     m_bLeaderSizeAsUInt4 &&
    2327           1 :                     m_bTrailerRepeatedLast4BytesRepeated &&
    2328           1 :                     !m_bKnownIncompatibleEdition &&
    2329           1 :                     !m_bWriteKnownIncompatibleEdition)
    2330             :                 {
    2331           1 :                     ReportError(CE_Warning, CPLE_AppDefined,
    2332             :                                 "The IFD has been rewritten at the end of "
    2333             :                                 "the file, which breaks COG layout.");
    2334           1 :                     m_bKnownIncompatibleEdition = true;
    2335           1 :                     m_bWriteKnownIncompatibleEdition = true;
    2336             :                 }
    2337             :             }
    2338             : 
    2339         361 :             m_bNeedsRewrite = false;
    2340             :         }
    2341             :     }
    2342             : 
    2343             :     // There are some circumstances in which we can reach this point
    2344             :     // without having made this our directory (SetDirectory()) in which
    2345             :     // case we should not risk a flush.
    2346       37229 :     if (GetAccess() == GA_Update &&
    2347       15499 :         TIFFCurrentDirOffset(m_hTIFF) == m_nDirOffset)
    2348             :     {
    2349       15499 :         const TIFFSizeProc pfnSizeProc = TIFFGetSizeProc(m_hTIFF);
    2350             : 
    2351       15499 :         toff_t nNewDirOffset = pfnSizeProc(TIFFClientdata(m_hTIFF));
    2352       15499 :         if ((nNewDirOffset % 2) == 1)
    2353        3389 :             ++nNewDirOffset;
    2354             : 
    2355       15499 :         if (TIFFFlush(m_hTIFF) == 0)
    2356          12 :             eErr = CE_Failure;
    2357             : 
    2358       15499 :         if (m_nDirOffset != TIFFCurrentDirOffset(m_hTIFF))
    2359             :         {
    2360           3 :             m_nDirOffset = nNewDirOffset;
    2361           3 :             ReloadAllOtherDirectories();
    2362           3 :             CPLDebug("GTiff",
    2363             :                      "directory moved during flush in FlushDirectory()");
    2364             :         }
    2365             :     }
    2366             : 
    2367       21730 :     SetDirectory();
    2368       21730 :     return eErr;
    2369             : }
    2370             : 
    2371             : /************************************************************************/
    2372             : /*                           CleanOverviews()                           */
    2373             : /************************************************************************/
    2374             : 
    2375           5 : CPLErr GTiffDataset::CleanOverviews()
    2376             : 
    2377             : {
    2378           5 :     CPLAssert(!m_poBaseDS);
    2379             : 
    2380           5 :     ScanDirectories();
    2381             : 
    2382           5 :     FlushDirectory();
    2383             : 
    2384             :     /* -------------------------------------------------------------------- */
    2385             :     /*      Cleanup overviews objects, and get offsets to all overview      */
    2386             :     /*      directories.                                                    */
    2387             :     /* -------------------------------------------------------------------- */
    2388          10 :     std::vector<toff_t> anOvDirOffsets;
    2389             : 
    2390          10 :     for (auto &poOvrDS : m_apoOverviewDS)
    2391             :     {
    2392           5 :         anOvDirOffsets.push_back(poOvrDS->m_nDirOffset);
    2393           5 :         if (poOvrDS->m_poMaskDS)
    2394           1 :             anOvDirOffsets.push_back(poOvrDS->m_poMaskDS->m_nDirOffset);
    2395             :     }
    2396           5 :     m_apoOverviewDS.clear();
    2397             : 
    2398             :     /* -------------------------------------------------------------------- */
    2399             :     /*      Loop through all the directories, translating the offsets       */
    2400             :     /*      into indexes we can use with TIFFUnlinkDirectory().             */
    2401             :     /* -------------------------------------------------------------------- */
    2402          10 :     std::vector<uint16_t> anOvDirIndexes;
    2403           5 :     int iThisOffset = 1;
    2404             : 
    2405           5 :     TIFFSetDirectory(m_hTIFF, 0);
    2406             : 
    2407             :     while (true)
    2408             :     {
    2409          28 :         for (toff_t nOffset : anOvDirOffsets)
    2410             :         {
    2411          16 :             if (nOffset == TIFFCurrentDirOffset(m_hTIFF))
    2412             :             {
    2413           6 :                 anOvDirIndexes.push_back(static_cast<uint16_t>(iThisOffset));
    2414             :             }
    2415             :         }
    2416             : 
    2417          12 :         if (TIFFLastDirectory(m_hTIFF))
    2418           5 :             break;
    2419             : 
    2420           7 :         TIFFReadDirectory(m_hTIFF);
    2421           7 :         ++iThisOffset;
    2422           7 :     }
    2423             : 
    2424             :     /* -------------------------------------------------------------------- */
    2425             :     /*      Actually unlink the target directories.  Note that we do        */
    2426             :     /*      this from last to first so as to avoid renumbering any of       */
    2427             :     /*      the earlier directories we need to remove.                      */
    2428             :     /* -------------------------------------------------------------------- */
    2429          11 :     while (!anOvDirIndexes.empty())
    2430             :     {
    2431           6 :         TIFFUnlinkDirectory(m_hTIFF, anOvDirIndexes.back());
    2432           6 :         anOvDirIndexes.pop_back();
    2433             :     }
    2434             : 
    2435           5 :     if (m_poMaskDS)
    2436             :     {
    2437           1 :         m_poMaskDS->m_apoOverviewDS.clear();
    2438             :     }
    2439             : 
    2440           5 :     if (!SetDirectory())
    2441           0 :         return CE_Failure;
    2442             : 
    2443           5 :     return CE_None;
    2444             : }
    2445             : 
    2446             : /************************************************************************/
    2447             : /*                     RegisterNewOverviewDataset()                     */
    2448             : /************************************************************************/
    2449             : 
    2450         509 : CPLErr GTiffDataset::RegisterNewOverviewDataset(toff_t nOverviewOffset,
    2451             :                                                 int l_nJpegQuality,
    2452             :                                                 CSLConstList papszOptions)
    2453             : {
    2454             :     const auto GetOptionValue =
    2455        5599 :         [papszOptions](const char *pszOptionKey, const char *pszConfigOptionKey,
    2456       11197 :                        const char **ppszKeyUsed = nullptr)
    2457             :     {
    2458        5599 :         const char *pszVal = CSLFetchNameValue(papszOptions, pszOptionKey);
    2459        5599 :         if (pszVal)
    2460             :         {
    2461           1 :             if (ppszKeyUsed)
    2462           1 :                 *ppszKeyUsed = pszOptionKey;
    2463           1 :             return pszVal;
    2464             :         }
    2465        5598 :         pszVal = CSLFetchNameValue(papszOptions, pszConfigOptionKey);
    2466        5598 :         if (pszVal)
    2467             :         {
    2468           0 :             if (ppszKeyUsed)
    2469           0 :                 *ppszKeyUsed = pszConfigOptionKey;
    2470           0 :             return pszVal;
    2471             :         }
    2472        5598 :         if (pszConfigOptionKey)
    2473             :         {
    2474        5598 :             pszVal = CPLGetConfigOption(pszConfigOptionKey, nullptr);
    2475        5598 :             if (pszVal && ppszKeyUsed)
    2476          13 :                 *ppszKeyUsed = pszConfigOptionKey;
    2477             :         }
    2478        5598 :         return pszVal;
    2479         509 :     };
    2480             : 
    2481         509 :     int nZLevel = m_nZLevel;
    2482         509 :     if (const char *opt = GetOptionValue("ZLEVEL", "ZLEVEL_OVERVIEW"))
    2483             :     {
    2484           4 :         nZLevel = atoi(opt);
    2485             :     }
    2486             : 
    2487         509 :     int nZSTDLevel = m_nZSTDLevel;
    2488         509 :     if (const char *opt = GetOptionValue("ZSTD_LEVEL", "ZSTD_LEVEL_OVERVIEW"))
    2489             :     {
    2490           4 :         nZSTDLevel = atoi(opt);
    2491             :     }
    2492             : 
    2493         509 :     bool bWebpLossless = m_bWebPLossless;
    2494             :     const char *pszWebPLosslessOverview =
    2495         509 :         GetOptionValue("WEBP_LOSSLESS", "WEBP_LOSSLESS_OVERVIEW");
    2496         509 :     if (pszWebPLosslessOverview)
    2497             :     {
    2498           2 :         bWebpLossless = CPLTestBool(pszWebPLosslessOverview);
    2499             :     }
    2500             : 
    2501         509 :     int nWebpLevel = m_nWebPLevel;
    2502         509 :     const char *pszKeyWebpLevel = "";
    2503         509 :     if (const char *opt = GetOptionValue("WEBP_LEVEL", "WEBP_LEVEL_OVERVIEW",
    2504             :                                          &pszKeyWebpLevel))
    2505             :     {
    2506          14 :         if (pszWebPLosslessOverview == nullptr && m_bWebPLossless)
    2507             :         {
    2508           1 :             CPLDebug("GTiff",
    2509             :                      "%s specified, but not WEBP_LOSSLESS_OVERVIEW. "
    2510             :                      "Assuming WEBP_LOSSLESS_OVERVIEW=NO",
    2511             :                      pszKeyWebpLevel);
    2512           1 :             bWebpLossless = false;
    2513             :         }
    2514          13 :         else if (bWebpLossless)
    2515             :         {
    2516           0 :             CPLError(CE_Warning, CPLE_AppDefined,
    2517             :                      "%s is specified, but WEBP_LOSSLESS_OVERVIEW=YES. "
    2518             :                      "%s will be ignored.",
    2519             :                      pszKeyWebpLevel, pszKeyWebpLevel);
    2520             :         }
    2521          14 :         nWebpLevel = atoi(opt);
    2522             :     }
    2523             : 
    2524         509 :     double dfMaxZError = m_dfMaxZErrorOverview;
    2525         509 :     if (const char *opt = GetOptionValue("MAX_Z_ERROR", "MAX_Z_ERROR_OVERVIEW"))
    2526             :     {
    2527          20 :         dfMaxZError = CPLAtof(opt);
    2528             :     }
    2529             : 
    2530         509 :     signed char nJpegTablesMode = m_nJpegTablesMode;
    2531         509 :     if (const char *opt =
    2532         509 :             GetOptionValue("JPEG_TABLESMODE", "JPEG_TABLESMODE_OVERVIEW"))
    2533             :     {
    2534           0 :         nJpegTablesMode = static_cast<signed char>(atoi(opt));
    2535             :     }
    2536             : 
    2537             : #ifdef HAVE_JXL
    2538         509 :     bool bJXLLossless = m_bJXLLossless;
    2539         509 :     if (const char *opt =
    2540         509 :             GetOptionValue("JXL_LOSSLESS", "JXL_LOSSLESS_OVERVIEW"))
    2541             :     {
    2542           0 :         bJXLLossless = CPLTestBool(opt);
    2543             :     }
    2544             : 
    2545         509 :     float fJXLDistance = m_fJXLDistance;
    2546         509 :     if (const char *opt =
    2547         509 :             GetOptionValue("JXL_DISTANCE", "JXL_DISTANCE_OVERVIEW"))
    2548             :     {
    2549           0 :         fJXLDistance = static_cast<float>(CPLAtof(opt));
    2550             :     }
    2551             : 
    2552         509 :     float fJXLAlphaDistance = m_fJXLAlphaDistance;
    2553         509 :     if (const char *opt =
    2554         509 :             GetOptionValue("JXL_ALPHA_DISTANCE", "JXL_ALPHA_DISTANCE_OVERVIEW"))
    2555             :     {
    2556           0 :         fJXLAlphaDistance = static_cast<float>(CPLAtof(opt));
    2557             :     }
    2558             : 
    2559         509 :     int nJXLEffort = m_nJXLEffort;
    2560         509 :     if (const char *opt = GetOptionValue("JXL_EFFORT", "JXL_EFFORT_OVERVIEW"))
    2561             :     {
    2562           0 :         nJXLEffort = atoi(opt);
    2563             :     }
    2564             : #endif
    2565             : 
    2566        1018 :     auto poODS = std::make_shared<GTiffDataset>();
    2567         509 :     poODS->ShareLockWithParentDataset(this);
    2568         509 :     poODS->eAccess = GA_Update;
    2569         509 :     poODS->m_osFilename = m_osFilename;
    2570         509 :     const char *pszSparseOK = GetOptionValue("SPARSE_OK", "SPARSE_OK_OVERVIEW");
    2571         509 :     if (pszSparseOK && CPLTestBool(pszSparseOK))
    2572             :     {
    2573           1 :         poODS->m_bWriteEmptyTiles = false;
    2574           1 :         poODS->m_bFillEmptyTilesAtClosing = false;
    2575             :     }
    2576             :     else
    2577             :     {
    2578         508 :         poODS->m_bWriteEmptyTiles = m_bWriteEmptyTiles;
    2579         508 :         poODS->m_bFillEmptyTilesAtClosing = m_bFillEmptyTilesAtClosing;
    2580             :     }
    2581         509 :     poODS->m_nJpegQuality = static_cast<signed char>(l_nJpegQuality);
    2582         509 :     poODS->m_nWebPLevel = static_cast<signed char>(nWebpLevel);
    2583         509 :     poODS->m_nZLevel = static_cast<signed char>(nZLevel);
    2584         509 :     poODS->m_nLZMAPreset = m_nLZMAPreset;
    2585         509 :     poODS->m_nZSTDLevel = static_cast<signed char>(nZSTDLevel);
    2586         509 :     poODS->m_bWebPLossless = bWebpLossless;
    2587         509 :     poODS->m_nJpegTablesMode = nJpegTablesMode;
    2588         509 :     poODS->m_dfMaxZError = dfMaxZError;
    2589         509 :     poODS->m_dfMaxZErrorOverview = dfMaxZError;
    2590        1018 :     memcpy(poODS->m_anLercAddCompressionAndVersion,
    2591         509 :            m_anLercAddCompressionAndVersion,
    2592             :            sizeof(m_anLercAddCompressionAndVersion));
    2593             : #ifdef HAVE_JXL
    2594         509 :     poODS->m_bJXLLossless = bJXLLossless;
    2595         509 :     poODS->m_fJXLDistance = fJXLDistance;
    2596         509 :     poODS->m_fJXLAlphaDistance = fJXLAlphaDistance;
    2597         509 :     poODS->m_nJXLEffort = nJXLEffort;
    2598             : #endif
    2599             : 
    2600         509 :     if (poODS->OpenOffset(VSI_TIFFOpenChild(m_hTIFF), nOverviewOffset,
    2601         509 :                           GA_Update) != CE_None)
    2602             :     {
    2603           0 :         return CE_Failure;
    2604             :     }
    2605             : 
    2606             :     // Assign color interpretation from main dataset
    2607         509 :     const int l_nBands = GetRasterCount();
    2608        1522 :     for (int i = 1; i <= l_nBands; i++)
    2609             :     {
    2610        1013 :         auto poBand = dynamic_cast<GTiffRasterBand *>(poODS->GetRasterBand(i));
    2611        1013 :         if (poBand)
    2612        1013 :             poBand->m_eBandInterp = GetRasterBand(i)->GetColorInterpretation();
    2613             :     }
    2614             : 
    2615             :     // Do that now that m_nCompression is set
    2616         509 :     poODS->RestoreVolatileParameters(poODS->m_hTIFF);
    2617             : 
    2618         509 :     poODS->m_poBaseDS = this;
    2619         509 :     poODS->m_bIsOverview = true;
    2620             : 
    2621         509 :     m_apoOverviewDS.push_back(std::move(poODS));
    2622         509 :     return CE_None;
    2623             : }
    2624             : 
    2625             : /************************************************************************/
    2626             : /*                        CreateTIFFColorTable()                        */
    2627             : /************************************************************************/
    2628             : 
    2629          12 : static void CreateTIFFColorTable(
    2630             :     GDALColorTable *poColorTable, int nBits, int nColorTableMultiplier,
    2631             :     std::vector<unsigned short> &anTRed, std::vector<unsigned short> &anTGreen,
    2632             :     std::vector<unsigned short> &anTBlue, unsigned short *&panRed,
    2633             :     unsigned short *&panGreen, unsigned short *&panBlue)
    2634             : {
    2635             :     int nColors;
    2636             : 
    2637          12 :     if (nBits == 8)
    2638          12 :         nColors = 256;
    2639           0 :     else if (nBits < 8)
    2640           0 :         nColors = 1 << nBits;
    2641             :     else
    2642           0 :         nColors = 65536;
    2643             : 
    2644          12 :     anTRed.resize(nColors, 0);
    2645          12 :     anTGreen.resize(nColors, 0);
    2646          12 :     anTBlue.resize(nColors, 0);
    2647             : 
    2648        3084 :     for (int iColor = 0; iColor < nColors; ++iColor)
    2649             :     {
    2650        3072 :         if (iColor < poColorTable->GetColorEntryCount())
    2651             :         {
    2652             :             GDALColorEntry sRGB;
    2653             : 
    2654        3072 :             poColorTable->GetColorEntryAsRGB(iColor, &sRGB);
    2655             : 
    2656        3072 :             anTRed[iColor] = GTiffDataset::ClampCTEntry(iColor, 1, sRGB.c1,
    2657             :                                                         nColorTableMultiplier);
    2658        3072 :             anTGreen[iColor] = GTiffDataset::ClampCTEntry(
    2659        3072 :                 iColor, 2, sRGB.c2, nColorTableMultiplier);
    2660        3072 :             anTBlue[iColor] = GTiffDataset::ClampCTEntry(iColor, 3, sRGB.c3,
    2661             :                                                          nColorTableMultiplier);
    2662             :         }
    2663             :         else
    2664             :         {
    2665           0 :             anTRed[iColor] = 0;
    2666           0 :             anTGreen[iColor] = 0;
    2667           0 :             anTBlue[iColor] = 0;
    2668             :         }
    2669             :     }
    2670             : 
    2671          12 :     panRed = &(anTRed[0]);
    2672          12 :     panGreen = &(anTGreen[0]);
    2673          12 :     panBlue = &(anTBlue[0]);
    2674          12 : }
    2675             : 
    2676             : /************************************************************************/
    2677             : /*                       GetOverviewParameters()                        */
    2678             : /************************************************************************/
    2679             : 
    2680         326 : bool GTiffDataset::GetOverviewParameters(
    2681             :     int &nCompression, uint16_t &nPlanarConfig, uint16_t &nPredictor,
    2682             :     uint16_t &nPhotometric, int &nOvrJpegQuality, std::string &osNoData,
    2683             :     uint16_t *&panExtraSampleValues, uint16_t &nExtraSamples,
    2684             :     CSLConstList papszOptions) const
    2685             : {
    2686             :     const auto GetOptionValue =
    2687        1080 :         [papszOptions](const char *pszOptionKey, const char *pszConfigOptionKey,
    2688        2152 :                        const char **ppszKeyUsed = nullptr)
    2689             :     {
    2690        1080 :         const char *pszVal = CSLFetchNameValue(papszOptions, pszOptionKey);
    2691        1080 :         if (pszVal)
    2692             :         {
    2693           8 :             if (ppszKeyUsed)
    2694           8 :                 *ppszKeyUsed = pszOptionKey;
    2695           8 :             return pszVal;
    2696             :         }
    2697        1072 :         pszVal = CSLFetchNameValue(papszOptions, pszConfigOptionKey);
    2698        1072 :         if (pszVal)
    2699             :         {
    2700           0 :             if (ppszKeyUsed)
    2701           0 :                 *ppszKeyUsed = pszConfigOptionKey;
    2702           0 :             return pszVal;
    2703             :         }
    2704        1072 :         pszVal = CPLGetConfigOption(pszConfigOptionKey, nullptr);
    2705        1072 :         if (pszVal && ppszKeyUsed)
    2706          58 :             *ppszKeyUsed = pszConfigOptionKey;
    2707        1072 :         return pszVal;
    2708         326 :     };
    2709             : 
    2710             :     /* -------------------------------------------------------------------- */
    2711             :     /*      Determine compression method.                                   */
    2712             :     /* -------------------------------------------------------------------- */
    2713         326 :     nCompression = m_nCompression;
    2714         326 :     const char *pszOptionKey = "";
    2715             :     const char *pszCompressValue =
    2716         326 :         GetOptionValue("COMPRESS", "COMPRESS_OVERVIEW", &pszOptionKey);
    2717         326 :     if (pszCompressValue != nullptr)
    2718             :     {
    2719          56 :         nCompression =
    2720          56 :             GTIFFGetCompressionMethod(pszCompressValue, pszOptionKey);
    2721          56 :         if (nCompression < 0)
    2722             :         {
    2723           0 :             nCompression = m_nCompression;
    2724             :         }
    2725             :     }
    2726             : 
    2727             :     /* -------------------------------------------------------------------- */
    2728             :     /*      Determine planar configuration.                                 */
    2729             :     /* -------------------------------------------------------------------- */
    2730         326 :     nPlanarConfig = m_nPlanarConfig;
    2731         326 :     if (nCompression == COMPRESSION_WEBP)
    2732             :     {
    2733          11 :         nPlanarConfig = PLANARCONFIG_CONTIG;
    2734             :     }
    2735             :     const char *pszInterleave =
    2736         326 :         GetOptionValue("INTERLEAVE", "INTERLEAVE_OVERVIEW", &pszOptionKey);
    2737         326 :     if (pszInterleave != nullptr && pszInterleave[0] != '\0')
    2738             :     {
    2739           2 :         if (EQUAL(pszInterleave, "PIXEL"))
    2740           1 :             nPlanarConfig = PLANARCONFIG_CONTIG;
    2741           1 :         else if (EQUAL(pszInterleave, "BAND"))
    2742           1 :             nPlanarConfig = PLANARCONFIG_SEPARATE;
    2743             :         else
    2744             :         {
    2745           0 :             CPLError(CE_Warning, CPLE_AppDefined,
    2746             :                      "%s=%s unsupported, "
    2747             :                      "value must be PIXEL or BAND. ignoring",
    2748             :                      pszOptionKey, pszInterleave);
    2749             :         }
    2750             :     }
    2751             : 
    2752             :     /* -------------------------------------------------------------------- */
    2753             :     /*      Determine predictor tag                                         */
    2754             :     /* -------------------------------------------------------------------- */
    2755         326 :     nPredictor = PREDICTOR_NONE;
    2756         326 :     if (GTIFFSupportsPredictor(nCompression))
    2757             :     {
    2758             :         const char *pszPredictor =
    2759          75 :             GetOptionValue("PREDICTOR", "PREDICTOR_OVERVIEW");
    2760          75 :         if (pszPredictor != nullptr)
    2761             :         {
    2762           1 :             nPredictor = static_cast<uint16_t>(atoi(pszPredictor));
    2763             :         }
    2764          74 :         else if (GTIFFSupportsPredictor(m_nCompression))
    2765          73 :             TIFFGetField(m_hTIFF, TIFFTAG_PREDICTOR, &nPredictor);
    2766             :     }
    2767             : 
    2768             :     /* -------------------------------------------------------------------- */
    2769             :     /*      Determine photometric tag                                       */
    2770             :     /* -------------------------------------------------------------------- */
    2771         326 :     if (m_nPhotometric == PHOTOMETRIC_YCBCR && nCompression != COMPRESSION_JPEG)
    2772           1 :         nPhotometric = PHOTOMETRIC_RGB;
    2773             :     else
    2774         325 :         nPhotometric = m_nPhotometric;
    2775             :     const char *pszPhotometric =
    2776         326 :         GetOptionValue("PHOTOMETRIC", "PHOTOMETRIC_OVERVIEW", &pszOptionKey);
    2777         326 :     if (!GTIFFUpdatePhotometric(pszPhotometric, pszOptionKey, nCompression,
    2778         326 :                                 pszInterleave, nBands, nPhotometric,
    2779             :                                 nPlanarConfig))
    2780             :     {
    2781           0 :         return false;
    2782             :     }
    2783             : 
    2784             :     /* -------------------------------------------------------------------- */
    2785             :     /*      Determine JPEG quality                                          */
    2786             :     /* -------------------------------------------------------------------- */
    2787         326 :     nOvrJpegQuality = m_nJpegQuality;
    2788         326 :     if (nCompression == COMPRESSION_JPEG)
    2789             :     {
    2790             :         const char *pszJPEGQuality =
    2791          27 :             GetOptionValue("JPEG_QUALITY", "JPEG_QUALITY_OVERVIEW");
    2792          27 :         if (pszJPEGQuality != nullptr)
    2793             :         {
    2794           9 :             nOvrJpegQuality = atoi(pszJPEGQuality);
    2795             :         }
    2796             :     }
    2797             : 
    2798             :     /* -------------------------------------------------------------------- */
    2799             :     /*      Set nodata.                                                     */
    2800             :     /* -------------------------------------------------------------------- */
    2801         326 :     if (m_bNoDataSet)
    2802             :     {
    2803          17 :         osNoData = GTiffFormatGDALNoDataTagValue(m_dfNoDataValue);
    2804             :     }
    2805             : 
    2806             :     /* -------------------------------------------------------------------- */
    2807             :     /*      Fetch extra sample tag                                          */
    2808             :     /* -------------------------------------------------------------------- */
    2809         326 :     panExtraSampleValues = nullptr;
    2810         326 :     nExtraSamples = 0;
    2811         326 :     if (TIFFGetField(m_hTIFF, TIFFTAG_EXTRASAMPLES, &nExtraSamples,
    2812         326 :                      &panExtraSampleValues))
    2813             :     {
    2814             :         uint16_t *panExtraSampleValuesNew = static_cast<uint16_t *>(
    2815          40 :             CPLMalloc(nExtraSamples * sizeof(uint16_t)));
    2816          40 :         memcpy(panExtraSampleValuesNew, panExtraSampleValues,
    2817          40 :                nExtraSamples * sizeof(uint16_t));
    2818          40 :         panExtraSampleValues = panExtraSampleValuesNew;
    2819             :     }
    2820             :     else
    2821             :     {
    2822         286 :         panExtraSampleValues = nullptr;
    2823         286 :         nExtraSamples = 0;
    2824             :     }
    2825             : 
    2826         326 :     return true;
    2827             : }
    2828             : 
    2829             : /************************************************************************/
    2830             : /*                  CreateOverviewsFromSrcOverviews()                   */
    2831             : /************************************************************************/
    2832             : 
    2833             : // If poOvrDS is not null, it is used and poSrcDS is ignored.
    2834             : 
    2835          69 : CPLErr GTiffDataset::CreateOverviewsFromSrcOverviews(GDALDataset *poSrcDS,
    2836             :                                                      GDALDataset *poOvrDS,
    2837             :                                                      int nOverviews)
    2838             : {
    2839          69 :     CPLAssert(poSrcDS->GetRasterCount() != 0);
    2840          69 :     CPLAssert(m_apoOverviewDS.empty());
    2841             : 
    2842          69 :     ScanDirectories();
    2843             : 
    2844          69 :     FlushDirectory();
    2845             : 
    2846          69 :     int nOvBitsPerSample = m_nBitsPerSample;
    2847             : 
    2848             :     /* -------------------------------------------------------------------- */
    2849             :     /*      Do we need some metadata for the overviews?                     */
    2850             :     /* -------------------------------------------------------------------- */
    2851         138 :     CPLString osMetadata;
    2852             : 
    2853          69 :     GTIFFBuildOverviewMetadata("NONE", this, false, osMetadata);
    2854             : 
    2855             :     int nCompression;
    2856             :     uint16_t nPlanarConfig;
    2857             :     uint16_t nPredictor;
    2858             :     uint16_t nPhotometric;
    2859             :     int nOvrJpegQuality;
    2860         138 :     std::string osNoData;
    2861          69 :     uint16_t *panExtraSampleValues = nullptr;
    2862          69 :     uint16_t nExtraSamples = 0;
    2863          69 :     if (!GetOverviewParameters(nCompression, nPlanarConfig, nPredictor,
    2864             :                                nPhotometric, nOvrJpegQuality, osNoData,
    2865             :                                panExtraSampleValues, nExtraSamples,
    2866             :                                /*papszOptions=*/nullptr))
    2867             :     {
    2868           0 :         return CE_Failure;
    2869             :     }
    2870             : 
    2871             :     /* -------------------------------------------------------------------- */
    2872             :     /*      Do we have a palette?  If so, create a TIFF compatible version. */
    2873             :     /* -------------------------------------------------------------------- */
    2874         138 :     std::vector<unsigned short> anTRed;
    2875         138 :     std::vector<unsigned short> anTGreen;
    2876          69 :     std::vector<unsigned short> anTBlue;
    2877          69 :     unsigned short *panRed = nullptr;
    2878          69 :     unsigned short *panGreen = nullptr;
    2879          69 :     unsigned short *panBlue = nullptr;
    2880             : 
    2881          69 :     if (nPhotometric == PHOTOMETRIC_PALETTE && m_poColorTable != nullptr)
    2882             :     {
    2883           0 :         if (m_nColorTableMultiplier == 0)
    2884           0 :             m_nColorTableMultiplier = DEFAULT_COLOR_TABLE_MULTIPLIER_257;
    2885             : 
    2886           0 :         CreateTIFFColorTable(m_poColorTable.get(), nOvBitsPerSample,
    2887             :                              m_nColorTableMultiplier, anTRed, anTGreen, anTBlue,
    2888             :                              panRed, panGreen, panBlue);
    2889             :     }
    2890             : 
    2891          69 :     int nOvrBlockXSize = 0;
    2892          69 :     int nOvrBlockYSize = 0;
    2893          69 :     GTIFFGetOverviewBlockSize(GDALRasterBand::ToHandle(GetRasterBand(1)),
    2894             :                               &nOvrBlockXSize, &nOvrBlockYSize, nullptr,
    2895             :                               nullptr);
    2896             : 
    2897          69 :     CPLErr eErr = CE_None;
    2898             : 
    2899         195 :     for (int i = 0; i < nOverviews && eErr == CE_None; ++i)
    2900             :     {
    2901             :         GDALRasterBand *poOvrBand =
    2902         163 :             poOvrDS ? ((i == 0) ? poOvrDS->GetRasterBand(1)
    2903          37 :                                 : poOvrDS->GetRasterBand(1)->GetOverview(i - 1))
    2904          55 :                     : poSrcDS->GetRasterBand(1)->GetOverview(i);
    2905             : 
    2906         126 :         int nOXSize = poOvrBand->GetXSize();
    2907         126 :         int nOYSize = poOvrBand->GetYSize();
    2908             : 
    2909         252 :         toff_t nOverviewOffset = GTIFFWriteDirectory(
    2910             :             m_hTIFF, FILETYPE_REDUCEDIMAGE, nOXSize, nOYSize, nOvBitsPerSample,
    2911         126 :             nPlanarConfig, m_nSamplesPerPixel, nOvrBlockXSize, nOvrBlockYSize,
    2912         126 :             TRUE, nCompression, nPhotometric, m_nSampleFormat, nPredictor,
    2913             :             panRed, panGreen, panBlue, nExtraSamples, panExtraSampleValues,
    2914             :             osMetadata,
    2915         126 :             nOvrJpegQuality >= 0 ? CPLSPrintf("%d", nOvrJpegQuality) : nullptr,
    2916         126 :             CPLSPrintf("%d", m_nJpegTablesMode),
    2917           2 :             osNoData.empty() ? nullptr : osNoData.c_str(),
    2918         126 :             m_anLercAddCompressionAndVersion, m_bWriteCOGLayout);
    2919             : 
    2920         126 :         if (nOverviewOffset == 0)
    2921           0 :             eErr = CE_Failure;
    2922             :         else
    2923         126 :             eErr = RegisterNewOverviewDataset(nOverviewOffset, nOvrJpegQuality,
    2924             :                                               nullptr);
    2925             :     }
    2926             : 
    2927             :     // For directory reloading, so that the chaining to the next directory is
    2928             :     // reloaded, as well as compression parameters.
    2929          69 :     ReloadDirectory();
    2930             : 
    2931          69 :     CPLFree(panExtraSampleValues);
    2932          69 :     panExtraSampleValues = nullptr;
    2933             : 
    2934          69 :     return eErr;
    2935             : }
    2936             : 
    2937             : /************************************************************************/
    2938             : /*                    CreateInternalMaskOverviews()                     */
    2939             : /************************************************************************/
    2940             : 
    2941         272 : CPLErr GTiffDataset::CreateInternalMaskOverviews(int nOvrBlockXSize,
    2942             :                                                  int nOvrBlockYSize)
    2943             : {
    2944         272 :     ScanDirectories();
    2945             : 
    2946             :     /* -------------------------------------------------------------------- */
    2947             :     /*      Create overviews for the mask.                                  */
    2948             :     /* -------------------------------------------------------------------- */
    2949         272 :     CPLErr eErr = CE_None;
    2950             : 
    2951         272 :     if (m_poMaskDS != nullptr && m_poMaskDS->GetRasterCount() == 1)
    2952             :     {
    2953             :         int nMaskOvrCompression;
    2954          43 :         if (strstr(GDALGetMetadataItem(GDALGetDriverByName("GTiff"),
    2955             :                                        GDAL_DMD_CREATIONOPTIONLIST, nullptr),
    2956          43 :                    "<Value>DEFLATE</Value>") != nullptr)
    2957          43 :             nMaskOvrCompression = COMPRESSION_ADOBE_DEFLATE;
    2958             :         else
    2959           0 :             nMaskOvrCompression = COMPRESSION_PACKBITS;
    2960             : 
    2961         115 :         for (auto &poOvrDS : m_apoOverviewDS)
    2962             :         {
    2963          72 :             if (poOvrDS->m_poMaskDS == nullptr)
    2964             :             {
    2965          60 :                 const toff_t nOverviewOffset = GTIFFWriteDirectory(
    2966             :                     m_hTIFF, FILETYPE_REDUCEDIMAGE | FILETYPE_MASK,
    2967          60 :                     poOvrDS->nRasterXSize, poOvrDS->nRasterYSize, 1,
    2968             :                     PLANARCONFIG_CONTIG, 1, nOvrBlockXSize, nOvrBlockYSize,
    2969             :                     TRUE, nMaskOvrCompression, PHOTOMETRIC_MASK,
    2970             :                     SAMPLEFORMAT_UINT, PREDICTOR_NONE, nullptr, nullptr,
    2971             :                     nullptr, 0, nullptr, "", nullptr, nullptr, nullptr, nullptr,
    2972          60 :                     m_bWriteCOGLayout);
    2973             : 
    2974          60 :                 if (nOverviewOffset == 0)
    2975             :                 {
    2976           0 :                     eErr = CE_Failure;
    2977           0 :                     continue;
    2978             :                 }
    2979             : 
    2980         120 :                 auto poMaskODS = std::make_shared<GTiffDataset>();
    2981          60 :                 poMaskODS->eAccess = GA_Update;
    2982          60 :                 poMaskODS->ShareLockWithParentDataset(this);
    2983          60 :                 poMaskODS->m_osFilename = m_osFilename;
    2984          60 :                 if (poMaskODS->OpenOffset(VSI_TIFFOpenChild(m_hTIFF),
    2985             :                                           nOverviewOffset,
    2986          60 :                                           GA_Update) != CE_None)
    2987             :                 {
    2988           0 :                     eErr = CE_Failure;
    2989             :                 }
    2990             :                 else
    2991             :                 {
    2992         120 :                     poMaskODS->m_bPromoteTo8Bits =
    2993          60 :                         CPLTestBool(CPLGetConfigOption(
    2994             :                             "GDAL_TIFF_INTERNAL_MASK_TO_8BIT", "YES"));
    2995          60 :                     poMaskODS->m_poBaseDS = this;
    2996          60 :                     poMaskODS->m_poImageryDS = poOvrDS.get();
    2997          60 :                     poOvrDS->m_poMaskDS = poMaskODS;
    2998          60 :                     m_poMaskDS->m_apoOverviewDS.push_back(std::move(poMaskODS));
    2999             :                 }
    3000             :             }
    3001             :         }
    3002             :     }
    3003             : 
    3004         272 :     ReloadDirectory();
    3005             : 
    3006         272 :     return eErr;
    3007             : }
    3008             : 
    3009             : /************************************************************************/
    3010             : /*                            AddOverviews()                            */
    3011             : /************************************************************************/
    3012             : 
    3013             : CPLErr
    3014          13 : GTiffDataset::AddOverviews(const std::vector<GDALDataset *> &apoSrcOvrDSIn,
    3015             :                            GDALProgressFunc pfnProgress, void *pProgressData,
    3016             :                            CSLConstList papszOptions)
    3017             : {
    3018             :     /* -------------------------------------------------------------------- */
    3019             :     /*      If we don't have read access, then create the overviews         */
    3020             :     /*      externally.                                                     */
    3021             :     /* -------------------------------------------------------------------- */
    3022          13 :     if (GetAccess() != GA_Update)
    3023             :     {
    3024           4 :         CPLDebug("GTiff", "File open for read-only accessing, "
    3025             :                           "creating overviews externally.");
    3026             : 
    3027           4 :         CPLErr eErr = GDALDataset::AddOverviews(apoSrcOvrDSIn, pfnProgress,
    3028             :                                                 pProgressData, papszOptions);
    3029           4 :         if (eErr == CE_None && m_poMaskDS)
    3030             :         {
    3031           0 :             ReportError(
    3032             :                 CE_Warning, CPLE_NotSupported,
    3033             :                 "Building external overviews whereas there is an internal "
    3034             :                 "mask is not fully supported. "
    3035             :                 "The overviews of the non-mask bands will be created, "
    3036             :                 "but not the overviews of the mask band.");
    3037             :         }
    3038           4 :         return eErr;
    3039             :     }
    3040             : 
    3041          18 :     std::vector<GDALDataset *> apoSrcOvrDS = apoSrcOvrDSIn;
    3042             :     // Sort overviews by descending size
    3043           9 :     std::sort(apoSrcOvrDS.begin(), apoSrcOvrDS.end(),
    3044           0 :               [](const GDALDataset *poDS1, const GDALDataset *poDS2)
    3045           0 :               { return poDS1->GetRasterXSize() > poDS2->GetRasterXSize(); });
    3046             : 
    3047           9 :     if (!GDALDefaultOverviews::CheckSrcOverviewsConsistencyWithBase(
    3048             :             this, apoSrcOvrDS))
    3049           5 :         return CE_Failure;
    3050             : 
    3051           4 :     ScanDirectories();
    3052             : 
    3053             :     // Make implicit JPEG overviews invisible, but do not destroy
    3054             :     // them in case they are already used (not sure that the client
    3055             :     // has the right to do that). Behavior maybe undefined in GDAL API.
    3056           4 :     std::swap(m_apoJPEGOverviewDSOld, m_apoJPEGOverviewDS);
    3057           4 :     m_apoJPEGOverviewDS.clear();
    3058             : 
    3059           4 :     FlushDirectory();
    3060             : 
    3061             :     /* -------------------------------------------------------------------- */
    3062             :     /*      If we are averaging bit data to grayscale we need to create     */
    3063             :     /*      8bit overviews.                                                 */
    3064             :     /* -------------------------------------------------------------------- */
    3065           4 :     int nOvBitsPerSample = m_nBitsPerSample;
    3066             : 
    3067             :     /* -------------------------------------------------------------------- */
    3068             :     /*      Do we need some metadata for the overviews?                     */
    3069             :     /* -------------------------------------------------------------------- */
    3070           8 :     CPLString osMetadata;
    3071             : 
    3072           4 :     const bool bIsForMaskBand = nBands == 1 && GetRasterBand(1)->IsMaskBand();
    3073           4 :     GTIFFBuildOverviewMetadata(/* resampling = */ "", this, bIsForMaskBand,
    3074             :                                osMetadata);
    3075             : 
    3076             :     int nCompression;
    3077             :     uint16_t nPlanarConfig;
    3078             :     uint16_t nPredictor;
    3079             :     uint16_t nPhotometric;
    3080             :     int nOvrJpegQuality;
    3081           8 :     std::string osNoData;
    3082           4 :     uint16_t *panExtraSampleValues = nullptr;
    3083           4 :     uint16_t nExtraSamples = 0;
    3084           4 :     if (!GetOverviewParameters(nCompression, nPlanarConfig, nPredictor,
    3085             :                                nPhotometric, nOvrJpegQuality, osNoData,
    3086             :                                panExtraSampleValues, nExtraSamples,
    3087             :                                papszOptions))
    3088             :     {
    3089           0 :         return CE_Failure;
    3090             :     }
    3091             : 
    3092             :     /* -------------------------------------------------------------------- */
    3093             :     /*      Do we have a palette?  If so, create a TIFF compatible version. */
    3094             :     /* -------------------------------------------------------------------- */
    3095           8 :     std::vector<unsigned short> anTRed;
    3096           8 :     std::vector<unsigned short> anTGreen;
    3097           4 :     std::vector<unsigned short> anTBlue;
    3098           4 :     unsigned short *panRed = nullptr;
    3099           4 :     unsigned short *panGreen = nullptr;
    3100           4 :     unsigned short *panBlue = nullptr;
    3101             : 
    3102           4 :     if (nPhotometric == PHOTOMETRIC_PALETTE && m_poColorTable != nullptr)
    3103             :     {
    3104           0 :         if (m_nColorTableMultiplier == 0)
    3105           0 :             m_nColorTableMultiplier = DEFAULT_COLOR_TABLE_MULTIPLIER_257;
    3106             : 
    3107           0 :         CreateTIFFColorTable(m_poColorTable.get(), nOvBitsPerSample,
    3108             :                              m_nColorTableMultiplier, anTRed, anTGreen, anTBlue,
    3109             :                              panRed, panGreen, panBlue);
    3110             :     }
    3111             : 
    3112             :     /* -------------------------------------------------------------------- */
    3113             :     /*      Establish which of the overview levels we already have, and     */
    3114             :     /*      which are new.  We assume that band 1 of the file is            */
    3115             :     /*      representative.                                                 */
    3116             :     /* -------------------------------------------------------------------- */
    3117           4 :     int nOvrBlockXSize = 0;
    3118           4 :     int nOvrBlockYSize = 0;
    3119           4 :     GTIFFGetOverviewBlockSize(GDALRasterBand::ToHandle(GetRasterBand(1)),
    3120             :                               &nOvrBlockXSize, &nOvrBlockYSize, papszOptions,
    3121             :                               "BLOCKSIZE");
    3122             : 
    3123           4 :     CPLErr eErr = CE_None;
    3124           8 :     for (const auto *poSrcOvrDS : apoSrcOvrDS)
    3125             :     {
    3126           4 :         bool bFound = false;
    3127           4 :         for (auto &poOvrDS : m_apoOverviewDS)
    3128             :         {
    3129           4 :             if (poOvrDS->GetRasterXSize() == poSrcOvrDS->GetRasterXSize() &&
    3130           2 :                 poOvrDS->GetRasterYSize() == poSrcOvrDS->GetRasterYSize())
    3131             :             {
    3132           2 :                 bFound = true;
    3133           2 :                 break;
    3134             :             }
    3135             :         }
    3136           4 :         if (!bFound && eErr == CE_None)
    3137             :         {
    3138           2 :             if (m_bLayoutIFDSBeforeData && !m_bKnownIncompatibleEdition &&
    3139           0 :                 !m_bWriteKnownIncompatibleEdition)
    3140             :             {
    3141           0 :                 ReportError(CE_Warning, CPLE_AppDefined,
    3142             :                             "Adding new overviews invalidates the "
    3143             :                             "LAYOUT=IFDS_BEFORE_DATA property");
    3144           0 :                 m_bKnownIncompatibleEdition = true;
    3145           0 :                 m_bWriteKnownIncompatibleEdition = true;
    3146             :             }
    3147             : 
    3148           6 :             const toff_t nOverviewOffset = GTIFFWriteDirectory(
    3149             :                 m_hTIFF, FILETYPE_REDUCEDIMAGE, poSrcOvrDS->GetRasterXSize(),
    3150             :                 poSrcOvrDS->GetRasterYSize(), nOvBitsPerSample, nPlanarConfig,
    3151           2 :                 m_nSamplesPerPixel, nOvrBlockXSize, nOvrBlockYSize, TRUE,
    3152           2 :                 nCompression, nPhotometric, m_nSampleFormat, nPredictor, panRed,
    3153             :                 panGreen, panBlue, nExtraSamples, panExtraSampleValues,
    3154             :                 osMetadata,
    3155           2 :                 nOvrJpegQuality >= 0 ? CPLSPrintf("%d", nOvrJpegQuality)
    3156             :                                      : nullptr,
    3157           2 :                 CPLSPrintf("%d", m_nJpegTablesMode),
    3158           0 :                 osNoData.empty() ? nullptr : osNoData.c_str(),
    3159           2 :                 m_anLercAddCompressionAndVersion, false);
    3160             : 
    3161           2 :             if (nOverviewOffset == 0)
    3162           0 :                 eErr = CE_Failure;
    3163             :             else
    3164           2 :                 eErr = RegisterNewOverviewDataset(
    3165             :                     nOverviewOffset, nOvrJpegQuality, papszOptions);
    3166             :         }
    3167             :     }
    3168             : 
    3169           4 :     CPLFree(panExtraSampleValues);
    3170           4 :     panExtraSampleValues = nullptr;
    3171             : 
    3172           4 :     ReloadDirectory();
    3173             : 
    3174           4 :     if (!pfnProgress)
    3175           2 :         pfnProgress = GDALDummyProgress;
    3176             : 
    3177             :     // almost 0, but not 0 to please Coverity Scan
    3178           4 :     double dfTotalPixels = std::numeric_limits<double>::min();
    3179           8 :     for (const auto *poSrcOvrDS : apoSrcOvrDS)
    3180             :     {
    3181           4 :         dfTotalPixels += static_cast<double>(poSrcOvrDS->GetRasterXSize()) *
    3182           4 :                          poSrcOvrDS->GetRasterYSize();
    3183             :     }
    3184             : 
    3185             :     // Copy source datasets into target overview datasets
    3186           4 :     double dfCurPixels = 0;
    3187           8 :     for (auto *poSrcOvrDS : apoSrcOvrDS)
    3188             :     {
    3189           4 :         GDALDataset *poDstOvrDS = nullptr;
    3190           4 :         for (auto &poOvrDS : m_apoOverviewDS)
    3191             :         {
    3192           8 :             if (poOvrDS->GetRasterXSize() == poSrcOvrDS->GetRasterXSize() &&
    3193           4 :                 poOvrDS->GetRasterYSize() == poSrcOvrDS->GetRasterYSize())
    3194             :             {
    3195           4 :                 poDstOvrDS = poOvrDS.get();
    3196           4 :                 break;
    3197             :             }
    3198             :         }
    3199           4 :         if (eErr == CE_None && poDstOvrDS)
    3200             :         {
    3201             :             const double dfThisPixels =
    3202           4 :                 static_cast<double>(poSrcOvrDS->GetRasterXSize()) *
    3203           4 :                 poSrcOvrDS->GetRasterYSize();
    3204           8 :             void *pScaledProgressData = GDALCreateScaledProgress(
    3205             :                 dfCurPixels / dfTotalPixels,
    3206           4 :                 (dfCurPixels + dfThisPixels) / dfTotalPixels, pfnProgress,
    3207             :                 pProgressData);
    3208           4 :             dfCurPixels += dfThisPixels;
    3209           4 :             eErr = GDALDatasetCopyWholeRaster(GDALDataset::ToHandle(poSrcOvrDS),
    3210             :                                               GDALDataset::ToHandle(poDstOvrDS),
    3211             :                                               nullptr, GDALScaledProgress,
    3212             :                                               pScaledProgressData);
    3213           4 :             GDALDestroyScaledProgress(pScaledProgressData);
    3214             :         }
    3215             :     }
    3216             : 
    3217           4 :     return eErr;
    3218             : }
    3219             : 
    3220             : /************************************************************************/
    3221             : /*                          IBuildOverviews()                           */
    3222             : /************************************************************************/
    3223             : 
    3224         407 : CPLErr GTiffDataset::IBuildOverviews(const char *pszResampling, int nOverviews,
    3225             :                                      const int *panOverviewList, int nBandsIn,
    3226             :                                      const int *panBandList,
    3227             :                                      GDALProgressFunc pfnProgress,
    3228             :                                      void *pProgressData,
    3229             :                                      CSLConstList papszOptions)
    3230             : 
    3231             : {
    3232         407 :     ScanDirectories();
    3233             : 
    3234             :     // Make implicit JPEG overviews invisible, but do not destroy
    3235             :     // them in case they are already used (not sure that the client
    3236             :     // has the right to do that.  Behavior maybe undefined in GDAL API.
    3237         407 :     std::swap(m_apoJPEGOverviewDSOld, m_apoJPEGOverviewDS);
    3238         407 :     m_apoJPEGOverviewDS.clear();
    3239             : 
    3240             :     /* -------------------------------------------------------------------- */
    3241             :     /*      If RRD or external OVR overviews requested, then invoke         */
    3242             :     /*      generic handling.                                               */
    3243             :     /* -------------------------------------------------------------------- */
    3244         407 :     bool bUseGenericHandling = false;
    3245         407 :     bool bUseRRD = false;
    3246         814 :     CPLStringList aosOptions(papszOptions);
    3247             : 
    3248         407 :     const char *pszLocation = CSLFetchNameValue(papszOptions, "LOCATION");
    3249         407 :     if (pszLocation && EQUAL(pszLocation, "EXTERNAL"))
    3250             :     {
    3251           1 :         bUseGenericHandling = true;
    3252             :     }
    3253         406 :     else if (pszLocation && EQUAL(pszLocation, "INTERNAL"))
    3254             :     {
    3255           0 :         if (GetAccess() != GA_Update)
    3256             :         {
    3257           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    3258             :                      "Cannot create internal overviews on file opened in "
    3259             :                      "read-only mode");
    3260           0 :             return CE_Failure;
    3261             :         }
    3262             :     }
    3263         406 :     else if (pszLocation && EQUAL(pszLocation, "RRD"))
    3264             :     {
    3265           3 :         bUseGenericHandling = true;
    3266           3 :         bUseRRD = true;
    3267           3 :         aosOptions.SetNameValue("USE_RRD", "YES");
    3268             :     }
    3269             :     // Legacy
    3270         403 :     else if ((bUseRRD = CPLTestBool(
    3271             :                   CSLFetchNameValueDef(papszOptions, "USE_RRD",
    3272         806 :                                        CPLGetConfigOption("USE_RRD", "NO")))) ||
    3273         403 :              CPLTestBool(CSLFetchNameValueDef(
    3274             :                  papszOptions, "TIFF_USE_OVR",
    3275             :                  CPLGetConfigOption("TIFF_USE_OVR", "NO"))))
    3276             :     {
    3277           0 :         bUseGenericHandling = true;
    3278             :     }
    3279             : 
    3280             :     /* -------------------------------------------------------------------- */
    3281             :     /*      If we don't have read access, then create the overviews         */
    3282             :     /*      externally.                                                     */
    3283             :     /* -------------------------------------------------------------------- */
    3284         407 :     if (GetAccess() != GA_Update)
    3285             :     {
    3286         143 :         CPLDebug("GTiff", "File open for read-only accessing, "
    3287             :                           "creating overviews externally.");
    3288             : 
    3289         143 :         bUseGenericHandling = true;
    3290             :     }
    3291             : 
    3292         407 :     if (bUseGenericHandling)
    3293             :     {
    3294         146 :         if (!m_apoOverviewDS.empty())
    3295             :         {
    3296           0 :             ReportError(CE_Failure, CPLE_NotSupported,
    3297             :                         "Cannot add external overviews when there are already "
    3298             :                         "internal overviews");
    3299           0 :             return CE_Failure;
    3300             :         }
    3301             : 
    3302         146 :         if (!m_bWriteEmptyTiles && !bUseRRD)
    3303             :         {
    3304           1 :             aosOptions.SetNameValue("SPARSE_OK", "YES");
    3305             :         }
    3306             : 
    3307         146 :         CPLErr eErr = GDALDataset::IBuildOverviews(
    3308             :             pszResampling, nOverviews, panOverviewList, nBandsIn, panBandList,
    3309         146 :             pfnProgress, pProgressData, aosOptions);
    3310         146 :         if (eErr == CE_None && m_poMaskDS)
    3311             :         {
    3312           1 :             ReportError(
    3313             :                 CE_Warning, CPLE_NotSupported,
    3314             :                 "Building external overviews whereas there is an internal "
    3315             :                 "mask is not fully supported. "
    3316             :                 "The overviews of the non-mask bands will be created, "
    3317             :                 "but not the overviews of the mask band.");
    3318             :         }
    3319         146 :         return eErr;
    3320             :     }
    3321             : 
    3322             :     /* -------------------------------------------------------------------- */
    3323             :     /*      Our TIFF overview support currently only works safely if all    */
    3324             :     /*      bands are handled at the same time.                             */
    3325             :     /* -------------------------------------------------------------------- */
    3326         261 :     if (nBandsIn != GetRasterCount())
    3327             :     {
    3328           0 :         ReportError(CE_Failure, CPLE_NotSupported,
    3329             :                     "Generation of overviews in TIFF currently only "
    3330             :                     "supported when operating on all bands.  "
    3331             :                     "Operation failed.");
    3332           0 :         return CE_Failure;
    3333             :     }
    3334             : 
    3335             :     /* -------------------------------------------------------------------- */
    3336             :     /*      If zero overviews were requested, we need to clear all          */
    3337             :     /*      existing overviews.                                             */
    3338             :     /* -------------------------------------------------------------------- */
    3339         261 :     if (nOverviews == 0)
    3340             :     {
    3341           8 :         if (m_apoOverviewDS.empty())
    3342           3 :             return GDALDataset::IBuildOverviews(
    3343             :                 pszResampling, nOverviews, panOverviewList, nBandsIn,
    3344           3 :                 panBandList, pfnProgress, pProgressData, papszOptions);
    3345             : 
    3346           5 :         return CleanOverviews();
    3347             :     }
    3348             : 
    3349         253 :     CPLErr eErr = CE_None;
    3350             : 
    3351             :     /* -------------------------------------------------------------------- */
    3352             :     /*      Initialize progress counter.                                    */
    3353             :     /* -------------------------------------------------------------------- */
    3354         253 :     if (!pfnProgress(0.0, nullptr, pProgressData))
    3355             :     {
    3356           0 :         ReportError(CE_Failure, CPLE_UserInterrupt, "User terminated");
    3357           0 :         return CE_Failure;
    3358             :     }
    3359             : 
    3360         253 :     FlushDirectory();
    3361             : 
    3362             :     /* -------------------------------------------------------------------- */
    3363             :     /*      If we are averaging bit data to grayscale we need to create     */
    3364             :     /*      8bit overviews.                                                 */
    3365             :     /* -------------------------------------------------------------------- */
    3366         253 :     int nOvBitsPerSample = m_nBitsPerSample;
    3367             : 
    3368         253 :     if (STARTS_WITH_CI(pszResampling, "AVERAGE_BIT2"))
    3369           2 :         nOvBitsPerSample = 8;
    3370             : 
    3371             :     /* -------------------------------------------------------------------- */
    3372             :     /*      Do we need some metadata for the overviews?                     */
    3373             :     /* -------------------------------------------------------------------- */
    3374         506 :     CPLString osMetadata;
    3375             : 
    3376         253 :     const bool bIsForMaskBand = nBands == 1 && GetRasterBand(1)->IsMaskBand();
    3377         253 :     GTIFFBuildOverviewMetadata(pszResampling, this, bIsForMaskBand, osMetadata);
    3378             : 
    3379             :     int nCompression;
    3380             :     uint16_t nPlanarConfig;
    3381             :     uint16_t nPredictor;
    3382             :     uint16_t nPhotometric;
    3383             :     int nOvrJpegQuality;
    3384         506 :     std::string osNoData;
    3385         253 :     uint16_t *panExtraSampleValues = nullptr;
    3386         253 :     uint16_t nExtraSamples = 0;
    3387         253 :     if (!GetOverviewParameters(nCompression, nPlanarConfig, nPredictor,
    3388             :                                nPhotometric, nOvrJpegQuality, osNoData,
    3389             :                                panExtraSampleValues, nExtraSamples,
    3390             :                                papszOptions))
    3391             :     {
    3392           0 :         return CE_Failure;
    3393             :     }
    3394             : 
    3395             :     /* -------------------------------------------------------------------- */
    3396             :     /*      Do we have a palette?  If so, create a TIFF compatible version. */
    3397             :     /* -------------------------------------------------------------------- */
    3398         506 :     std::vector<unsigned short> anTRed;
    3399         506 :     std::vector<unsigned short> anTGreen;
    3400         506 :     std::vector<unsigned short> anTBlue;
    3401         253 :     unsigned short *panRed = nullptr;
    3402         253 :     unsigned short *panGreen = nullptr;
    3403         253 :     unsigned short *panBlue = nullptr;
    3404             : 
    3405         253 :     if (nPhotometric == PHOTOMETRIC_PALETTE && m_poColorTable != nullptr)
    3406             :     {
    3407          12 :         if (m_nColorTableMultiplier == 0)
    3408           0 :             m_nColorTableMultiplier = DEFAULT_COLOR_TABLE_MULTIPLIER_257;
    3409             : 
    3410          12 :         CreateTIFFColorTable(m_poColorTable.get(), nOvBitsPerSample,
    3411             :                              m_nColorTableMultiplier, anTRed, anTGreen, anTBlue,
    3412             :                              panRed, panGreen, panBlue);
    3413             :     }
    3414             : 
    3415             :     /* -------------------------------------------------------------------- */
    3416             :     /*      Establish which of the overview levels we already have, and     */
    3417             :     /*      which are new.  We assume that band 1 of the file is            */
    3418             :     /*      representative.                                                 */
    3419             :     /* -------------------------------------------------------------------- */
    3420         253 :     int nOvrBlockXSize = 0;
    3421         253 :     int nOvrBlockYSize = 0;
    3422         253 :     GTIFFGetOverviewBlockSize(GDALRasterBand::ToHandle(GetRasterBand(1)),
    3423             :                               &nOvrBlockXSize, &nOvrBlockYSize, papszOptions,
    3424             :                               "BLOCKSIZE");
    3425         506 :     std::vector<bool> abRequireNewOverview(nOverviews, true);
    3426         690 :     for (int i = 0; i < nOverviews && eErr == CE_None; ++i)
    3427             :     {
    3428         770 :         for (auto &poODS : m_apoOverviewDS)
    3429             :         {
    3430             :             const int nOvFactor =
    3431         778 :                 GDALComputeOvFactor(poODS->GetRasterXSize(), GetRasterXSize(),
    3432         389 :                                     poODS->GetRasterYSize(), GetRasterYSize());
    3433             : 
    3434             :             // If we already have a 1x1 overview and this new one would result
    3435             :             // in it too, then don't create it.
    3436         449 :             if (poODS->GetRasterXSize() == 1 && poODS->GetRasterYSize() == 1 &&
    3437         449 :                 DIV_ROUND_UP(GetRasterXSize(), panOverviewList[i]) == 1 &&
    3438          21 :                 DIV_ROUND_UP(GetRasterYSize(), panOverviewList[i]) == 1)
    3439             :             {
    3440          21 :                 abRequireNewOverview[i] = false;
    3441          21 :                 break;
    3442             :             }
    3443             : 
    3444         701 :             if (nOvFactor == panOverviewList[i] ||
    3445         333 :                 nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
    3446             :                                                 GetRasterXSize(),
    3447             :                                                 GetRasterYSize()))
    3448             :             {
    3449          35 :                 abRequireNewOverview[i] = false;
    3450          35 :                 break;
    3451             :             }
    3452             :         }
    3453             : 
    3454         437 :         if (abRequireNewOverview[i])
    3455             :         {
    3456         381 :             if (m_bLayoutIFDSBeforeData && !m_bKnownIncompatibleEdition &&
    3457           2 :                 !m_bWriteKnownIncompatibleEdition)
    3458             :             {
    3459           2 :                 ReportError(CE_Warning, CPLE_AppDefined,
    3460             :                             "Adding new overviews invalidates the "
    3461             :                             "LAYOUT=IFDS_BEFORE_DATA property");
    3462           2 :                 m_bKnownIncompatibleEdition = true;
    3463           2 :                 m_bWriteKnownIncompatibleEdition = true;
    3464             :             }
    3465             : 
    3466             :             const int nOXSize =
    3467         381 :                 DIV_ROUND_UP(GetRasterXSize(), panOverviewList[i]);
    3468             :             const int nOYSize =
    3469         381 :                 DIV_ROUND_UP(GetRasterYSize(), panOverviewList[i]);
    3470             : 
    3471         762 :             const toff_t nOverviewOffset = GTIFFWriteDirectory(
    3472             :                 m_hTIFF, FILETYPE_REDUCEDIMAGE, nOXSize, nOYSize,
    3473         381 :                 nOvBitsPerSample, nPlanarConfig, m_nSamplesPerPixel,
    3474             :                 nOvrBlockXSize, nOvrBlockYSize, TRUE, nCompression,
    3475         381 :                 nPhotometric, m_nSampleFormat, nPredictor, panRed, panGreen,
    3476             :                 panBlue, nExtraSamples, panExtraSampleValues, osMetadata,
    3477         381 :                 nOvrJpegQuality >= 0 ? CPLSPrintf("%d", nOvrJpegQuality)
    3478             :                                      : nullptr,
    3479         381 :                 CPLSPrintf("%d", m_nJpegTablesMode),
    3480          25 :                 osNoData.empty() ? nullptr : osNoData.c_str(),
    3481         381 :                 m_anLercAddCompressionAndVersion, false);
    3482             : 
    3483         381 :             if (nOverviewOffset == 0)
    3484           0 :                 eErr = CE_Failure;
    3485             :             else
    3486         381 :                 eErr = RegisterNewOverviewDataset(
    3487             :                     nOverviewOffset, nOvrJpegQuality, papszOptions);
    3488             :         }
    3489             :     }
    3490             : 
    3491         253 :     CPLFree(panExtraSampleValues);
    3492         253 :     panExtraSampleValues = nullptr;
    3493             : 
    3494         253 :     ReloadDirectory();
    3495             : 
    3496             :     /* -------------------------------------------------------------------- */
    3497             :     /*      Create overviews for the mask.                                  */
    3498             :     /* -------------------------------------------------------------------- */
    3499         253 :     if (eErr != CE_None)
    3500           0 :         return eErr;
    3501             : 
    3502         253 :     eErr = CreateInternalMaskOverviews(nOvrBlockXSize, nOvrBlockYSize);
    3503             : 
    3504             :     /* -------------------------------------------------------------------- */
    3505             :     /*      Refresh overviews for the mask                                  */
    3506             :     /* -------------------------------------------------------------------- */
    3507             :     const bool bHasInternalMask =
    3508         253 :         m_poMaskDS != nullptr && m_poMaskDS->GetRasterCount() == 1;
    3509             :     const bool bHasExternalMask =
    3510         253 :         !bHasInternalMask && oOvManager.HaveMaskFile();
    3511         253 :     const bool bHasMask = bHasInternalMask || bHasExternalMask;
    3512             : 
    3513         253 :     if (bHasInternalMask)
    3514             :     {
    3515          48 :         std::vector<GDALRasterBandH> ahOverviewBands;
    3516          64 :         for (auto &poOvrDS : m_apoOverviewDS)
    3517             :         {
    3518          40 :             if (poOvrDS->m_poMaskDS != nullptr)
    3519             :             {
    3520          40 :                 ahOverviewBands.push_back(GDALRasterBand::ToHandle(
    3521          40 :                     poOvrDS->m_poMaskDS->GetRasterBand(1)));
    3522             :             }
    3523             :         }
    3524             : 
    3525          48 :         void *pScaledProgressData = GDALCreateScaledProgress(
    3526          24 :             0, 1.0 / (nBands + 1), pfnProgress, pProgressData);
    3527          24 :         eErr = GDALRegenerateOverviewsEx(
    3528          24 :             m_poMaskDS->GetRasterBand(1),
    3529          24 :             static_cast<int>(ahOverviewBands.size()), ahOverviewBands.data(),
    3530             :             pszResampling, GDALScaledProgress, pScaledProgressData,
    3531             :             papszOptions);
    3532          24 :         GDALDestroyScaledProgress(pScaledProgressData);
    3533             :     }
    3534         229 :     else if (bHasExternalMask)
    3535             :     {
    3536           4 :         void *pScaledProgressData = GDALCreateScaledProgress(
    3537           2 :             0, 1.0 / (nBands + 1), pfnProgress, pProgressData);
    3538           2 :         eErr = oOvManager.BuildOverviewsMask(
    3539             :             pszResampling, nOverviews, panOverviewList, GDALScaledProgress,
    3540             :             pScaledProgressData, papszOptions);
    3541           2 :         GDALDestroyScaledProgress(pScaledProgressData);
    3542             :     }
    3543             : 
    3544             :     // If we have an alpha band, we want it to be generated before downsampling
    3545             :     // other bands
    3546         253 :     bool bHasAlphaBand = false;
    3547       66253 :     for (int iBand = 0; iBand < nBands; iBand++)
    3548             :     {
    3549       66000 :         if (papoBands[iBand]->GetColorInterpretation() == GCI_AlphaBand)
    3550          18 :             bHasAlphaBand = true;
    3551             :     }
    3552             : 
    3553             :     /* -------------------------------------------------------------------- */
    3554             :     /*      Refresh old overviews that were listed.                         */
    3555             :     /* -------------------------------------------------------------------- */
    3556         253 :     const auto poColorTable = GetRasterBand(panBandList[0])->GetColorTable();
    3557          21 :     if ((m_nPlanarConfig == PLANARCONFIG_CONTIG || bHasAlphaBand) &&
    3558         234 :         GDALDataTypeIsComplex(
    3559         234 :             GetRasterBand(panBandList[0])->GetRasterDataType()) == FALSE &&
    3560          12 :         (poColorTable == nullptr || STARTS_WITH_CI(pszResampling, "NEAR") ||
    3561         507 :          poColorTable->IsIdentity()) &&
    3562         226 :         (STARTS_WITH_CI(pszResampling, "NEAR") ||
    3563         117 :          EQUAL(pszResampling, "AVERAGE") || EQUAL(pszResampling, "RMS") ||
    3564          47 :          EQUAL(pszResampling, "GAUSS") || EQUAL(pszResampling, "CUBIC") ||
    3565          29 :          EQUAL(pszResampling, "CUBICSPLINE") ||
    3566          28 :          EQUAL(pszResampling, "LANCZOS") || EQUAL(pszResampling, "BILINEAR") ||
    3567          24 :          EQUAL(pszResampling, "MODE")))
    3568             :     {
    3569             :         // In the case of pixel interleaved compressed overviews, we want to
    3570             :         // generate the overviews for all the bands block by block, and not
    3571             :         // band after band, in order to write the block once and not loose
    3572             :         // space in the TIFF file.  We also use that logic for uncompressed
    3573             :         // overviews, since GDALRegenerateOverviewsMultiBand() will be able to
    3574             :         // trigger cascading overview regeneration even in the presence
    3575             :         // of an alpha band.
    3576             : 
    3577         205 :         int nNewOverviews = 0;
    3578             : 
    3579             :         GDALRasterBand ***papapoOverviewBands = static_cast<GDALRasterBand ***>(
    3580         205 :             CPLCalloc(sizeof(void *), nBandsIn));
    3581             :         GDALRasterBand **papoBandList =
    3582         205 :             static_cast<GDALRasterBand **>(CPLCalloc(sizeof(void *), nBandsIn));
    3583       66109 :         for (int iBand = 0; iBand < nBandsIn; ++iBand)
    3584             :         {
    3585       65904 :             GDALRasterBand *poBand = GetRasterBand(panBandList[iBand]);
    3586             : 
    3587       65904 :             papoBandList[iBand] = poBand;
    3588      131808 :             papapoOverviewBands[iBand] = static_cast<GDALRasterBand **>(
    3589       65904 :                 CPLCalloc(sizeof(void *), poBand->GetOverviewCount()));
    3590             : 
    3591       65904 :             int iCurOverview = 0;
    3592             :             std::vector<bool> abAlreadyUsedOverviewBand(
    3593       65904 :                 poBand->GetOverviewCount(), false);
    3594             : 
    3595      132094 :             for (int i = 0; i < nOverviews; ++i)
    3596             :             {
    3597       66650 :                 for (int j = 0; j < poBand->GetOverviewCount(); ++j)
    3598             :                 {
    3599       66635 :                     if (abAlreadyUsedOverviewBand[j])
    3600         459 :                         continue;
    3601             : 
    3602             :                     int nOvFactor;
    3603       66176 :                     GDALRasterBand *poOverview = poBand->GetOverview(j);
    3604             : 
    3605       66176 :                     nOvFactor = GDALComputeOvFactor(
    3606             :                         poOverview->GetXSize(), poBand->GetXSize(),
    3607             :                         poOverview->GetYSize(), poBand->GetYSize());
    3608             : 
    3609       66176 :                     GDALCopyNoDataValue(poOverview, poBand);
    3610             : 
    3611       66177 :                     if (nOvFactor == panOverviewList[i] ||
    3612           1 :                         nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
    3613             :                                                         poBand->GetXSize(),
    3614             :                                                         poBand->GetYSize()))
    3615             :                     {
    3616       66175 :                         if (iBand == 0)
    3617             :                         {
    3618             :                             const auto osNewResampling =
    3619         664 :                                 GDALGetNormalizedOvrResampling(pszResampling);
    3620             :                             const char *pszExistingResampling =
    3621         332 :                                 poOverview->GetMetadataItem("RESAMPLING");
    3622         664 :                             if (pszExistingResampling &&
    3623         332 :                                 pszExistingResampling != osNewResampling)
    3624             :                             {
    3625           2 :                                 poOverview->SetMetadataItem(
    3626           2 :                                     "RESAMPLING", osNewResampling.c_str());
    3627             :                             }
    3628             :                         }
    3629             : 
    3630       66175 :                         abAlreadyUsedOverviewBand[j] = true;
    3631       66175 :                         CPLAssert(iCurOverview < poBand->GetOverviewCount());
    3632       66175 :                         papapoOverviewBands[iBand][iCurOverview] = poOverview;
    3633       66175 :                         ++iCurOverview;
    3634       66175 :                         break;
    3635             :                     }
    3636             :                 }
    3637             :             }
    3638             : 
    3639       65904 :             if (nNewOverviews == 0)
    3640             :             {
    3641         205 :                 nNewOverviews = iCurOverview;
    3642             :             }
    3643       65699 :             else if (nNewOverviews != iCurOverview)
    3644             :             {
    3645           0 :                 CPLAssert(false);
    3646             :                 return CE_Failure;
    3647             :             }
    3648             :         }
    3649             : 
    3650             :         void *pScaledProgressData =
    3651         205 :             bHasMask ? GDALCreateScaledProgress(1.0 / (nBands + 1), 1.0,
    3652             :                                                 pfnProgress, pProgressData)
    3653         179 :                      : GDALCreateScaledProgress(0.0, 1.0, pfnProgress,
    3654         205 :                                                 pProgressData);
    3655         205 :         GDALRegenerateOverviewsMultiBand(nBandsIn, papoBandList, nNewOverviews,
    3656             :                                          papapoOverviewBands, pszResampling,
    3657             :                                          GDALScaledProgress,
    3658             :                                          pScaledProgressData, papszOptions);
    3659         205 :         GDALDestroyScaledProgress(pScaledProgressData);
    3660             : 
    3661       66109 :         for (int iBand = 0; iBand < nBandsIn; ++iBand)
    3662             :         {
    3663       65904 :             CPLFree(papapoOverviewBands[iBand]);
    3664             :         }
    3665         205 :         CPLFree(papapoOverviewBands);
    3666         205 :         CPLFree(papoBandList);
    3667             :     }
    3668             :     else
    3669             :     {
    3670             :         GDALRasterBand **papoOverviewBands = static_cast<GDALRasterBand **>(
    3671          48 :             CPLCalloc(sizeof(void *), nOverviews));
    3672             : 
    3673          48 :         const int iBandOffset = bHasMask ? 1 : 0;
    3674             : 
    3675         144 :         for (int iBand = 0; iBand < nBandsIn && eErr == CE_None; ++iBand)
    3676             :         {
    3677          96 :             GDALRasterBand *poBand = GetRasterBand(panBandList[iBand]);
    3678          96 :             if (poBand == nullptr)
    3679             :             {
    3680           0 :                 eErr = CE_Failure;
    3681           0 :                 break;
    3682             :             }
    3683             : 
    3684             :             std::vector<bool> abAlreadyUsedOverviewBand(
    3685         192 :                 poBand->GetOverviewCount(), false);
    3686             : 
    3687          96 :             int nNewOverviews = 0;
    3688         288 :             for (int i = 0; i < nOverviews; ++i)
    3689             :             {
    3690         450 :                 for (int j = 0; j < poBand->GetOverviewCount(); ++j)
    3691             :                 {
    3692         432 :                     if (abAlreadyUsedOverviewBand[j])
    3693         257 :                         continue;
    3694             : 
    3695         175 :                     GDALRasterBand *poOverview = poBand->GetOverview(j);
    3696             : 
    3697         175 :                     GDALCopyNoDataValue(poOverview, poBand);
    3698             : 
    3699         175 :                     const int nOvFactor = GDALComputeOvFactor(
    3700             :                         poOverview->GetXSize(), poBand->GetXSize(),
    3701             :                         poOverview->GetYSize(), poBand->GetYSize());
    3702             : 
    3703         176 :                     if (nOvFactor == panOverviewList[i] ||
    3704           1 :                         nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
    3705             :                                                         poBand->GetXSize(),
    3706             :                                                         poBand->GetYSize()))
    3707             :                     {
    3708         174 :                         if (iBand == 0)
    3709             :                         {
    3710             :                             const auto osNewResampling =
    3711         168 :                                 GDALGetNormalizedOvrResampling(pszResampling);
    3712             :                             const char *pszExistingResampling =
    3713          84 :                                 poOverview->GetMetadataItem("RESAMPLING");
    3714         136 :                             if (pszExistingResampling &&
    3715          52 :                                 pszExistingResampling != osNewResampling)
    3716             :                             {
    3717           1 :                                 poOverview->SetMetadataItem(
    3718           1 :                                     "RESAMPLING", osNewResampling.c_str());
    3719             :                             }
    3720             :                         }
    3721             : 
    3722         174 :                         abAlreadyUsedOverviewBand[j] = true;
    3723         174 :                         CPLAssert(nNewOverviews < poBand->GetOverviewCount());
    3724         174 :                         papoOverviewBands[nNewOverviews++] = poOverview;
    3725         174 :                         break;
    3726             :                     }
    3727             :                 }
    3728             :             }
    3729             : 
    3730         192 :             void *pScaledProgressData = GDALCreateScaledProgress(
    3731          96 :                 (iBand + iBandOffset) /
    3732          96 :                     static_cast<double>(nBandsIn + iBandOffset),
    3733          96 :                 (iBand + iBandOffset + 1) /
    3734          96 :                     static_cast<double>(nBandsIn + iBandOffset),
    3735             :                 pfnProgress, pProgressData);
    3736             : 
    3737          96 :             eErr = GDALRegenerateOverviewsEx(
    3738             :                 poBand, nNewOverviews,
    3739             :                 reinterpret_cast<GDALRasterBandH *>(papoOverviewBands),
    3740             :                 pszResampling, GDALScaledProgress, pScaledProgressData,
    3741             :                 papszOptions);
    3742             : 
    3743          96 :             GDALDestroyScaledProgress(pScaledProgressData);
    3744             :         }
    3745             : 
    3746             :         /* --------------------------------------------------------------------
    3747             :          */
    3748             :         /*      Cleanup */
    3749             :         /* --------------------------------------------------------------------
    3750             :          */
    3751          48 :         CPLFree(papoOverviewBands);
    3752             :     }
    3753             : 
    3754         253 :     pfnProgress(1.0, nullptr, pProgressData);
    3755             : 
    3756         253 :     return eErr;
    3757             : }
    3758             : 
    3759             : /************************************************************************/
    3760             : /*                   GTiffWriteDummyGeokeyDirectory()                   */
    3761             : /************************************************************************/
    3762             : 
    3763        1502 : static void GTiffWriteDummyGeokeyDirectory(TIFF *hTIFF)
    3764             : {
    3765             :     // If we have existing geokeys, try to wipe them
    3766             :     // by writing a dummy geokey directory. (#2546)
    3767        1502 :     uint16_t *panVI = nullptr;
    3768        1502 :     uint16_t nKeyCount = 0;
    3769             : 
    3770        1502 :     if (TIFFGetField(hTIFF, TIFFTAG_GEOKEYDIRECTORY, &nKeyCount, &panVI))
    3771             :     {
    3772          24 :         GUInt16 anGKVersionInfo[4] = {1, 1, 0, 0};
    3773          24 :         double adfDummyDoubleParams[1] = {0.0};
    3774          24 :         TIFFSetField(hTIFF, TIFFTAG_GEOKEYDIRECTORY, 4, anGKVersionInfo);
    3775          24 :         TIFFSetField(hTIFF, TIFFTAG_GEODOUBLEPARAMS, 1, adfDummyDoubleParams);
    3776          24 :         TIFFSetField(hTIFF, TIFFTAG_GEOASCIIPARAMS, "");
    3777             :     }
    3778        1502 : }
    3779             : 
    3780             : /************************************************************************/
    3781             : /*                      IsSRSCompatibleOfGeoTIFF()                      */
    3782             : /************************************************************************/
    3783             : 
    3784        3155 : static bool IsSRSCompatibleOfGeoTIFF(const OGRSpatialReference *poSRS,
    3785             :                                      GTIFFKeysFlavorEnum eGeoTIFFKeysFlavor)
    3786             : {
    3787        3155 :     char *pszWKT = nullptr;
    3788        3155 :     if ((poSRS->IsGeographic() || poSRS->IsProjected()) && !poSRS->IsCompound())
    3789             :     {
    3790        3137 :         const char *pszAuthName = poSRS->GetAuthorityName(nullptr);
    3791        3137 :         const char *pszAuthCode = poSRS->GetAuthorityCode(nullptr);
    3792        3137 :         if (pszAuthName && pszAuthCode && EQUAL(pszAuthName, "EPSG"))
    3793        2567 :             return true;
    3794             :     }
    3795             :     OGRErr eErr;
    3796             :     {
    3797        1176 :         CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
    3798        1176 :         if (poSRS->IsDerivedGeographic() ||
    3799         588 :             (poSRS->IsProjected() && !poSRS->IsCompound() &&
    3800          70 :              poSRS->GetAxesCount() == 3))
    3801             :         {
    3802           0 :             eErr = OGRERR_FAILURE;
    3803             :         }
    3804             :         else
    3805             :         {
    3806             :             // Geographic3D CRS can't be exported to WKT1, but are
    3807             :             // valid GeoTIFF 1.1
    3808         588 :             const char *const apszOptions[] = {
    3809         588 :                 poSRS->IsGeographic() ? nullptr : "FORMAT=WKT1", nullptr};
    3810         588 :             eErr = poSRS->exportToWkt(&pszWKT, apszOptions);
    3811         588 :             if (eErr == OGRERR_FAILURE && poSRS->IsProjected() &&
    3812             :                 eGeoTIFFKeysFlavor == GEOTIFF_KEYS_ESRI_PE)
    3813             :             {
    3814           0 :                 CPLFree(pszWKT);
    3815           0 :                 const char *const apszOptionsESRIWKT[] = {"FORMAT=WKT1_ESRI",
    3816             :                                                           nullptr};
    3817           0 :                 eErr = poSRS->exportToWkt(&pszWKT, apszOptionsESRIWKT);
    3818             :             }
    3819             :         }
    3820             :     }
    3821         588 :     const bool bCompatibleOfGeoTIFF =
    3822        1175 :         (eErr == OGRERR_NONE && pszWKT != nullptr &&
    3823         587 :          strstr(pszWKT, "custom_proj4") == nullptr);
    3824         588 :     CPLFree(pszWKT);
    3825         588 :     return bCompatibleOfGeoTIFF;
    3826             : }
    3827             : 
    3828             : /************************************************************************/
    3829             : /*                          WriteGeoTIFFInfo()                          */
    3830             : /************************************************************************/
    3831             : 
    3832        5861 : void GTiffDataset::WriteGeoTIFFInfo()
    3833             : 
    3834             : {
    3835        5861 :     bool bPixelIsPoint = false;
    3836        5861 :     bool bPointGeoIgnore = false;
    3837             : 
    3838             :     const char *pszAreaOrPoint =
    3839        5861 :         GTiffDataset::GetMetadataItem(GDALMD_AREA_OR_POINT);
    3840        5861 :     if (pszAreaOrPoint && EQUAL(pszAreaOrPoint, GDALMD_AOP_POINT))
    3841             :     {
    3842          19 :         bPixelIsPoint = true;
    3843             :         bPointGeoIgnore =
    3844          19 :             CPLTestBool(CPLGetConfigOption("GTIFF_POINT_GEO_IGNORE", "FALSE"));
    3845             :     }
    3846             : 
    3847        5861 :     if (m_bForceUnsetGTOrGCPs)
    3848             :     {
    3849          11 :         m_bNeedsRewrite = true;
    3850          11 :         m_bForceUnsetGTOrGCPs = false;
    3851             : 
    3852          11 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOPIXELSCALE);
    3853          11 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOTIEPOINTS);
    3854          11 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOTRANSMATRIX);
    3855             :     }
    3856             : 
    3857        5861 :     if (m_bForceUnsetProjection)
    3858             :     {
    3859           8 :         m_bNeedsRewrite = true;
    3860           8 :         m_bForceUnsetProjection = false;
    3861             : 
    3862           8 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOKEYDIRECTORY);
    3863           8 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEODOUBLEPARAMS);
    3864           8 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOASCIIPARAMS);
    3865             :     }
    3866             : 
    3867             :     /* -------------------------------------------------------------------- */
    3868             :     /*      Write geotransform if valid.                                    */
    3869             :     /* -------------------------------------------------------------------- */
    3870        5861 :     if (m_bGeoTransformValid)
    3871             :     {
    3872        1809 :         m_bNeedsRewrite = true;
    3873             : 
    3874             :         /* --------------------------------------------------------------------
    3875             :          */
    3876             :         /*      Clear old tags to ensure we don't end up with conflicting */
    3877             :         /*      information. (#2625) */
    3878             :         /* --------------------------------------------------------------------
    3879             :          */
    3880        1809 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOPIXELSCALE);
    3881        1809 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOTIEPOINTS);
    3882        1809 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOTRANSMATRIX);
    3883             : 
    3884             :         /* --------------------------------------------------------------------
    3885             :          */
    3886             :         /*      Write the transform.  If we have a normal north-up image we */
    3887             :         /*      use the tiepoint plus pixelscale otherwise we use a matrix. */
    3888             :         /* --------------------------------------------------------------------
    3889             :          */
    3890        1809 :         if (m_gt.xrot == 0.0 && m_gt.yrot == 0.0 && m_gt.yscale < 0.0)
    3891             :         {
    3892        1717 :             double dfOffset = 0.0;
    3893        1717 :             if (m_eProfile != GTiffProfile::BASELINE)
    3894             :             {
    3895             :                 // In the case the SRS has a vertical component and we have
    3896             :                 // a single band, encode its scale/offset in the GeoTIFF tags
    3897        1711 :                 int bHasScale = FALSE;
    3898        1711 :                 double dfScale = GetRasterBand(1)->GetScale(&bHasScale);
    3899        1711 :                 int bHasOffset = FALSE;
    3900        1711 :                 dfOffset = GetRasterBand(1)->GetOffset(&bHasOffset);
    3901             :                 const bool bApplyScaleOffset =
    3902        1711 :                     m_oSRS.IsVertical() && GetRasterCount() == 1;
    3903        1711 :                 if (bApplyScaleOffset && !bHasScale)
    3904           0 :                     dfScale = 1.0;
    3905        1711 :                 if (!bApplyScaleOffset || !bHasOffset)
    3906        1708 :                     dfOffset = 0.0;
    3907        1711 :                 const double adfPixelScale[3] = {m_gt.xscale, fabs(m_gt.yscale),
    3908        1711 :                                                  bApplyScaleOffset ? dfScale
    3909        1711 :                                                                    : 0.0};
    3910        1711 :                 TIFFSetField(m_hTIFF, TIFFTAG_GEOPIXELSCALE, 3, adfPixelScale);
    3911             :             }
    3912             : 
    3913        1717 :             double adfTiePoints[6] = {0.0,        0.0,        0.0,
    3914        1717 :                                       m_gt.xorig, m_gt.yorig, dfOffset};
    3915             : 
    3916        1717 :             if (bPixelIsPoint && !bPointGeoIgnore)
    3917             :             {
    3918          15 :                 adfTiePoints[3] += m_gt.xscale * 0.5 + m_gt.xrot * 0.5;
    3919          15 :                 adfTiePoints[4] += m_gt.yrot * 0.5 + m_gt.yscale * 0.5;
    3920             :             }
    3921             : 
    3922        1717 :             if (m_eProfile != GTiffProfile::BASELINE)
    3923        1717 :                 TIFFSetField(m_hTIFF, TIFFTAG_GEOTIEPOINTS, 6, adfTiePoints);
    3924             :         }
    3925             :         else
    3926             :         {
    3927          92 :             double adfMatrix[16] = {};
    3928             : 
    3929          92 :             adfMatrix[0] = m_gt.xscale;
    3930          92 :             adfMatrix[1] = m_gt.xrot;
    3931          92 :             adfMatrix[3] = m_gt.xorig;
    3932          92 :             adfMatrix[4] = m_gt.yrot;
    3933          92 :             adfMatrix[5] = m_gt.yscale;
    3934          92 :             adfMatrix[7] = m_gt.yorig;
    3935          92 :             adfMatrix[15] = 1.0;
    3936             : 
    3937          92 :             if (bPixelIsPoint && !bPointGeoIgnore)
    3938             :             {
    3939           0 :                 adfMatrix[3] += m_gt.xscale * 0.5 + m_gt.xrot * 0.5;
    3940           0 :                 adfMatrix[7] += m_gt.yrot * 0.5 + m_gt.yscale * 0.5;
    3941             :             }
    3942             : 
    3943          92 :             if (m_eProfile != GTiffProfile::BASELINE)
    3944          92 :                 TIFFSetField(m_hTIFF, TIFFTAG_GEOTRANSMATRIX, 16, adfMatrix);
    3945             :         }
    3946             : 
    3947        1809 :         if (m_poBaseDS == nullptr)
    3948             :         {
    3949             :             // Do we need a world file?
    3950        1809 :             if (CPLFetchBool(m_papszCreationOptions, "TFW", false))
    3951           7 :                 GDALWriteWorldFile(m_osFilename.c_str(), "tfw", m_gt.data());
    3952        1802 :             else if (CPLFetchBool(m_papszCreationOptions, "WORLDFILE", false))
    3953           2 :                 GDALWriteWorldFile(m_osFilename.c_str(), "wld", m_gt.data());
    3954             :         }
    3955             :     }
    3956        4066 :     else if (GetGCPCount() > 0 && GetGCPCount() <= knMAX_GCP_COUNT &&
    3957          14 :              m_eProfile != GTiffProfile::BASELINE)
    3958             :     {
    3959          14 :         m_bNeedsRewrite = true;
    3960             : 
    3961             :         double *padfTiePoints = static_cast<double *>(
    3962          14 :             CPLMalloc(6 * sizeof(double) * GetGCPCount()));
    3963             : 
    3964          74 :         for (size_t iGCP = 0; iGCP < m_aoGCPs.size(); ++iGCP)
    3965             :         {
    3966             : 
    3967          60 :             padfTiePoints[iGCP * 6 + 0] = m_aoGCPs[iGCP].Pixel();
    3968          60 :             padfTiePoints[iGCP * 6 + 1] = m_aoGCPs[iGCP].Line();
    3969          60 :             padfTiePoints[iGCP * 6 + 2] = 0;
    3970          60 :             padfTiePoints[iGCP * 6 + 3] = m_aoGCPs[iGCP].X();
    3971          60 :             padfTiePoints[iGCP * 6 + 4] = m_aoGCPs[iGCP].Y();
    3972          60 :             padfTiePoints[iGCP * 6 + 5] = m_aoGCPs[iGCP].Z();
    3973             : 
    3974          60 :             if (bPixelIsPoint && !bPointGeoIgnore)
    3975             :             {
    3976           0 :                 padfTiePoints[iGCP * 6 + 0] += 0.5;
    3977           0 :                 padfTiePoints[iGCP * 6 + 1] += 0.5;
    3978             :             }
    3979             :         }
    3980             : 
    3981          14 :         TIFFSetField(m_hTIFF, TIFFTAG_GEOTIEPOINTS, 6 * GetGCPCount(),
    3982             :                      padfTiePoints);
    3983          14 :         CPLFree(padfTiePoints);
    3984             :     }
    3985             : 
    3986             :     /* -------------------------------------------------------------------- */
    3987             :     /*      Write out projection definition.                                */
    3988             :     /* -------------------------------------------------------------------- */
    3989        5861 :     const bool bHasProjection = !m_oSRS.IsEmpty();
    3990        5861 :     if ((bHasProjection || bPixelIsPoint) &&
    3991        1506 :         m_eProfile != GTiffProfile::BASELINE)
    3992             :     {
    3993        1502 :         m_bNeedsRewrite = true;
    3994             : 
    3995             :         // If we have existing geokeys, try to wipe them
    3996             :         // by writing a dummy geokey directory. (#2546)
    3997        1502 :         GTiffWriteDummyGeokeyDirectory(m_hTIFF);
    3998             : 
    3999        1502 :         GTIF *psGTIF = GTiffDataset::GTIFNew(m_hTIFF);
    4000             : 
    4001             :         // Set according to coordinate system.
    4002        1502 :         if (bHasProjection)
    4003             :         {
    4004        1501 :             if (IsSRSCompatibleOfGeoTIFF(&m_oSRS, m_eGeoTIFFKeysFlavor))
    4005             :             {
    4006        1499 :                 GTIFSetFromOGISDefnEx(psGTIF,
    4007             :                                       OGRSpatialReference::ToHandle(&m_oSRS),
    4008             :                                       m_eGeoTIFFKeysFlavor, m_eGeoTIFFVersion);
    4009             :             }
    4010             :             else
    4011             :             {
    4012           2 :                 GDALPamDataset::SetSpatialRef(&m_oSRS);
    4013             :             }
    4014             :         }
    4015             : 
    4016        1502 :         if (bPixelIsPoint)
    4017             :         {
    4018          19 :             GTIFKeySet(psGTIF, GTRasterTypeGeoKey, TYPE_SHORT, 1,
    4019             :                        RasterPixelIsPoint);
    4020             :         }
    4021             : 
    4022        1502 :         GTIFWriteKeys(psGTIF);
    4023        1502 :         GTIFFree(psGTIF);
    4024             :     }
    4025        5861 : }
    4026             : 
    4027             : /************************************************************************/
    4028             : /*                         AppendMetadataItem()                         */
    4029             : /************************************************************************/
    4030             : 
    4031        3888 : static void AppendMetadataItem(CPLXMLNode **ppsRoot, CPLXMLNode **ppsTail,
    4032             :                                const char *pszKey, const char *pszValue,
    4033             :                                CPLXMLNode *psValueNode, int nBand,
    4034             :                                const char *pszRole, const char *pszDomain)
    4035             : 
    4036             : {
    4037        3888 :     CPLAssert(pszValue || psValueNode);
    4038        3888 :     CPLAssert(!(pszValue && psValueNode));
    4039             : 
    4040             :     /* -------------------------------------------------------------------- */
    4041             :     /*      Create the Item element, and subcomponents.                     */
    4042             :     /* -------------------------------------------------------------------- */
    4043        3888 :     CPLXMLNode *psItem = CPLCreateXMLNode(nullptr, CXT_Element, "Item");
    4044        3888 :     CPLAddXMLAttributeAndValue(psItem, "name", pszKey);
    4045             : 
    4046        3888 :     if (nBand > 0)
    4047             :     {
    4048        1164 :         char szBandId[32] = {};
    4049        1164 :         snprintf(szBandId, sizeof(szBandId), "%d", nBand - 1);
    4050        1164 :         CPLAddXMLAttributeAndValue(psItem, "sample", szBandId);
    4051             :     }
    4052             : 
    4053        3888 :     if (pszRole != nullptr)
    4054         382 :         CPLAddXMLAttributeAndValue(psItem, "role", pszRole);
    4055             : 
    4056        3888 :     if (pszDomain != nullptr && strlen(pszDomain) > 0)
    4057        1010 :         CPLAddXMLAttributeAndValue(psItem, "domain", pszDomain);
    4058             : 
    4059        3888 :     if (pszValue)
    4060             :     {
    4061             :         // Note: this escaping should not normally be done, as the serialization
    4062             :         // of the tree to XML also does it, so we end up width double XML escaping,
    4063             :         // but keep it for backward compatibility.
    4064        3868 :         char *pszEscapedItemValue = CPLEscapeString(pszValue, -1, CPLES_XML);
    4065        3868 :         CPLCreateXMLNode(psItem, CXT_Text, pszEscapedItemValue);
    4066        3868 :         CPLFree(pszEscapedItemValue);
    4067             :     }
    4068             :     else
    4069             :     {
    4070          20 :         CPLAddXMLChild(psItem, psValueNode);
    4071             :     }
    4072             : 
    4073             :     /* -------------------------------------------------------------------- */
    4074             :     /*      Create root, if missing.                                        */
    4075             :     /* -------------------------------------------------------------------- */
    4076        3888 :     if (*ppsRoot == nullptr)
    4077         760 :         *ppsRoot = CPLCreateXMLNode(nullptr, CXT_Element, "GDALMetadata");
    4078             : 
    4079             :     /* -------------------------------------------------------------------- */
    4080             :     /*      Append item to tail.  We keep track of the tail to avoid        */
    4081             :     /*      O(nsquared) time as the list gets longer.                       */
    4082             :     /* -------------------------------------------------------------------- */
    4083        3888 :     if (*ppsTail == nullptr)
    4084         760 :         CPLAddXMLChild(*ppsRoot, psItem);
    4085             :     else
    4086        3128 :         CPLAddXMLSibling(*ppsTail, psItem);
    4087             : 
    4088        3888 :     *ppsTail = psItem;
    4089        3888 : }
    4090             : 
    4091             : /************************************************************************/
    4092             : /*                         AppendMetadataItem()                         */
    4093             : /************************************************************************/
    4094             : 
    4095        3868 : static void AppendMetadataItem(CPLXMLNode **ppsRoot, CPLXMLNode **ppsTail,
    4096             :                                const char *pszKey, const char *pszValue,
    4097             :                                int nBand, const char *pszRole,
    4098             :                                const char *pszDomain)
    4099             : 
    4100             : {
    4101        3868 :     AppendMetadataItem(ppsRoot, ppsTail, pszKey, pszValue, nullptr, nBand,
    4102             :                        pszRole, pszDomain);
    4103        3868 : }
    4104             : 
    4105             : /************************************************************************/
    4106             : /*                          WriteMDMetadata()                           */
    4107             : /************************************************************************/
    4108             : 
    4109      311019 : static void WriteMDMetadata(GDALMultiDomainMetadata *poMDMD, TIFF *hTIFF,
    4110             :                             CPLXMLNode **ppsRoot, CPLXMLNode **ppsTail,
    4111             :                             int nBand, GTiffProfile eProfile)
    4112             : 
    4113             : {
    4114             : 
    4115             :     /* ==================================================================== */
    4116             :     /*      Process each domain.                                            */
    4117             :     /* ==================================================================== */
    4118      311019 :     CSLConstList papszDomainList = poMDMD->GetDomainList();
    4119      319480 :     for (int iDomain = 0; papszDomainList && papszDomainList[iDomain];
    4120             :          ++iDomain)
    4121             :     {
    4122        8461 :         CSLConstList papszMD = poMDMD->GetMetadata(papszDomainList[iDomain]);
    4123        8461 :         bool bIsXMLOrJSON = false;
    4124             : 
    4125        8461 :         if (EQUAL(papszDomainList[iDomain], "IMAGE_STRUCTURE") ||
    4126        2466 :             EQUAL(papszDomainList[iDomain], "DERIVED_SUBDATASETS"))
    4127        5998 :             continue;  // Ignored.
    4128        2463 :         if (EQUAL(papszDomainList[iDomain], "COLOR_PROFILE"))
    4129           3 :             continue;  // Handled elsewhere.
    4130        2460 :         if (EQUAL(papszDomainList[iDomain], MD_DOMAIN_RPC))
    4131           7 :             continue;  // Handled elsewhere.
    4132        2454 :         if (EQUAL(papszDomainList[iDomain], "xml:ESRI") &&
    4133           1 :             CPLTestBool(CPLGetConfigOption("ESRI_XML_PAM", "NO")))
    4134           1 :             continue;  // Handled elsewhere.
    4135        2452 :         if (EQUAL(papszDomainList[iDomain], "xml:XMP"))
    4136           2 :             continue;  // Handled in SetMetadata.
    4137             : 
    4138        2450 :         if (STARTS_WITH_CI(papszDomainList[iDomain], "xml:") ||
    4139        2448 :             STARTS_WITH_CI(papszDomainList[iDomain], "json:"))
    4140             :         {
    4141          12 :             bIsXMLOrJSON = true;
    4142             :         }
    4143             : 
    4144             :         /* --------------------------------------------------------------------
    4145             :          */
    4146             :         /*      Process each item in this domain. */
    4147             :         /* --------------------------------------------------------------------
    4148             :          */
    4149        7527 :         for (int iItem = 0; papszMD && papszMD[iItem]; ++iItem)
    4150             :         {
    4151        5077 :             const char *pszItemValue = nullptr;
    4152        5077 :             char *pszItemName = nullptr;
    4153             : 
    4154        5077 :             if (bIsXMLOrJSON)
    4155             :             {
    4156          11 :                 pszItemName = CPLStrdup("doc");
    4157          11 :                 pszItemValue = papszMD[iItem];
    4158             :             }
    4159             :             else
    4160             :             {
    4161        5066 :                 pszItemValue = CPLParseNameValue(papszMD[iItem], &pszItemName);
    4162        5066 :                 if (pszItemName == nullptr)
    4163             :                 {
    4164          49 :                     CPLDebug("GTiff", "Invalid metadata item : %s",
    4165          49 :                              papszMD[iItem]);
    4166          49 :                     continue;
    4167             :                 }
    4168             :             }
    4169             : 
    4170             :             /* --------------------------------------------------------------------
    4171             :              */
    4172             :             /*      Convert into XML item or handle as a special TIFF tag. */
    4173             :             /* --------------------------------------------------------------------
    4174             :              */
    4175        5028 :             if (strlen(papszDomainList[iDomain]) == 0 && nBand == 0 &&
    4176        3673 :                 (STARTS_WITH_CI(pszItemName, "TIFFTAG_") ||
    4177        3612 :                  (EQUAL(pszItemName, "GEO_METADATA") &&
    4178        3611 :                   eProfile == GTiffProfile::GDALGEOTIFF) ||
    4179        3611 :                  (EQUAL(pszItemName, "TIFF_RSID") &&
    4180             :                   eProfile == GTiffProfile::GDALGEOTIFF)))
    4181             :             {
    4182          63 :                 if (EQUAL(pszItemName, "TIFFTAG_RESOLUTIONUNIT"))
    4183             :                 {
    4184             :                     // ResolutionUnit can't be 0, which is the default if
    4185             :                     // atoi() fails.  Set to 1=Unknown.
    4186           9 :                     int v = atoi(pszItemValue);
    4187           9 :                     if (!v)
    4188           1 :                         v = RESUNIT_NONE;
    4189           9 :                     TIFFSetField(hTIFF, TIFFTAG_RESOLUTIONUNIT, v);
    4190             :                 }
    4191             :                 else
    4192             :                 {
    4193          54 :                     bool bFoundTag = false;
    4194          54 :                     size_t iTag = 0;  // Used after for.
    4195          54 :                     const auto *pasTIFFTags = GTiffDataset::GetTIFFTags();
    4196         286 :                     for (; pasTIFFTags[iTag].pszTagName; ++iTag)
    4197             :                     {
    4198         286 :                         if (EQUAL(pszItemName, pasTIFFTags[iTag].pszTagName))
    4199             :                         {
    4200          54 :                             bFoundTag = true;
    4201          54 :                             break;
    4202             :                         }
    4203             :                     }
    4204             : 
    4205          54 :                     if (bFoundTag &&
    4206          54 :                         pasTIFFTags[iTag].eType == GTIFFTAGTYPE_STRING)
    4207          33 :                         TIFFSetField(hTIFF, pasTIFFTags[iTag].nTagVal,
    4208             :                                      pszItemValue);
    4209          21 :                     else if (bFoundTag &&
    4210          21 :                              pasTIFFTags[iTag].eType == GTIFFTAGTYPE_FLOAT)
    4211          16 :                         TIFFSetField(hTIFF, pasTIFFTags[iTag].nTagVal,
    4212             :                                      CPLAtof(pszItemValue));
    4213           5 :                     else if (bFoundTag &&
    4214           5 :                              pasTIFFTags[iTag].eType == GTIFFTAGTYPE_SHORT)
    4215           4 :                         TIFFSetField(hTIFF, pasTIFFTags[iTag].nTagVal,
    4216             :                                      atoi(pszItemValue));
    4217           1 :                     else if (bFoundTag && pasTIFFTags[iTag].eType ==
    4218             :                                               GTIFFTAGTYPE_BYTE_STRING)
    4219             :                     {
    4220           1 :                         uint32_t nLen =
    4221           1 :                             static_cast<uint32_t>(strlen(pszItemValue));
    4222           1 :                         if (nLen)
    4223             :                         {
    4224           1 :                             TIFFSetField(hTIFF, pasTIFFTags[iTag].nTagVal, nLen,
    4225             :                                          pszItemValue);
    4226           1 :                         }
    4227             :                     }
    4228             :                     else
    4229           0 :                         CPLError(CE_Warning, CPLE_NotSupported,
    4230             :                                  "%s metadata item is unhandled and "
    4231             :                                  "will not be written",
    4232             :                                  pszItemName);
    4233          63 :                 }
    4234             :             }
    4235        4965 :             else if (nBand == 0 && EQUAL(pszItemName, GDALMD_AREA_OR_POINT))
    4236             :             {
    4237             :                 /* Do nothing, handled elsewhere. */;
    4238             :             }
    4239             :             else
    4240             :             {
    4241        3075 :                 AppendMetadataItem(ppsRoot, ppsTail, pszItemName, pszItemValue,
    4242        3075 :                                    nBand, nullptr, papszDomainList[iDomain]);
    4243             :             }
    4244             : 
    4245        5028 :             CPLFree(pszItemName);
    4246             :         }
    4247             : 
    4248             :         /* --------------------------------------------------------------------
    4249             :          */
    4250             :         /*      Remove TIFFTAG_xxxxxx that are already set but no longer in */
    4251             :         /*      the metadata list (#5619) */
    4252             :         /* --------------------------------------------------------------------
    4253             :          */
    4254        2450 :         if (strlen(papszDomainList[iDomain]) == 0 && nBand == 0)
    4255             :         {
    4256        2164 :             const auto *pasTIFFTags = GTiffDataset::GetTIFFTags();
    4257       32460 :             for (size_t iTag = 0; pasTIFFTags[iTag].pszTagName; ++iTag)
    4258             :             {
    4259       30296 :                 uint32_t nCount = 0;
    4260       30296 :                 char *pszText = nullptr;
    4261       30296 :                 int16_t nVal = 0;
    4262       30296 :                 float fVal = 0.0f;
    4263             :                 const char *pszVal =
    4264       30296 :                     CSLFetchNameValue(papszMD, pasTIFFTags[iTag].pszTagName);
    4265       60529 :                 if (pszVal == nullptr &&
    4266       30233 :                     ((pasTIFFTags[iTag].eType == GTIFFTAGTYPE_STRING &&
    4267       17279 :                       TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal,
    4268       30225 :                                    &pszText)) ||
    4269       30225 :                      (pasTIFFTags[iTag].eType == GTIFFTAGTYPE_SHORT &&
    4270        6479 :                       TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal, &nVal)) ||
    4271       30222 :                      (pasTIFFTags[iTag].eType == GTIFFTAGTYPE_FLOAT &&
    4272        4312 :                       TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal, &fVal)) ||
    4273       30221 :                      (pasTIFFTags[iTag].eType == GTIFFTAGTYPE_BYTE_STRING &&
    4274        2163 :                       TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal, &nCount,
    4275             :                                    &pszText))))
    4276             :                 {
    4277          13 :                     TIFFUnsetField(hTIFF, pasTIFFTags[iTag].nTagVal);
    4278             :                 }
    4279             :             }
    4280             :         }
    4281             :     }
    4282      311019 : }
    4283             : 
    4284             : /************************************************************************/
    4285             : /*                              WriteRPC()                              */
    4286             : /************************************************************************/
    4287             : 
    4288       10136 : void GTiffDataset::WriteRPC(GDALDataset *poSrcDS, TIFF *l_hTIFF,
    4289             :                             int bSrcIsGeoTIFF, GTiffProfile eProfile,
    4290             :                             const char *pszTIFFFilename,
    4291             :                             CSLConstList papszCreationOptions,
    4292             :                             bool bWriteOnlyInPAMIfNeeded)
    4293             : {
    4294             :     /* -------------------------------------------------------------------- */
    4295             :     /*      Handle RPC data written to TIFF RPCCoefficient tag, RPB file,   */
    4296             :     /*      RPCTEXT file or PAM.                                            */
    4297             :     /* -------------------------------------------------------------------- */
    4298       10136 :     CSLConstList papszRPCMD = poSrcDS->GetMetadata(MD_DOMAIN_RPC);
    4299       10136 :     if (papszRPCMD != nullptr)
    4300             :     {
    4301          32 :         bool bRPCSerializedOtherWay = false;
    4302             : 
    4303          32 :         if (eProfile == GTiffProfile::GDALGEOTIFF)
    4304             :         {
    4305          20 :             if (!bWriteOnlyInPAMIfNeeded)
    4306          11 :                 GTiffDatasetWriteRPCTag(l_hTIFF, papszRPCMD);
    4307          20 :             bRPCSerializedOtherWay = true;
    4308             :         }
    4309             : 
    4310             :         // Write RPB file if explicitly asked, or if a non GDAL specific
    4311             :         // profile is selected and RPCTXT is not asked.
    4312             :         bool bRPBExplicitlyAsked =
    4313          32 :             CPLFetchBool(papszCreationOptions, "RPB", false);
    4314             :         bool bRPBExplicitlyDenied =
    4315          32 :             !CPLFetchBool(papszCreationOptions, "RPB", true);
    4316          44 :         if ((eProfile != GTiffProfile::GDALGEOTIFF &&
    4317          12 :              !CPLFetchBool(papszCreationOptions, "RPCTXT", false) &&
    4318          44 :              !bRPBExplicitlyDenied) ||
    4319             :             bRPBExplicitlyAsked)
    4320             :         {
    4321           8 :             if (!bWriteOnlyInPAMIfNeeded)
    4322           4 :                 GDALWriteRPBFile(pszTIFFFilename, papszRPCMD);
    4323           8 :             bRPCSerializedOtherWay = true;
    4324             :         }
    4325             : 
    4326          32 :         if (CPLFetchBool(papszCreationOptions, "RPCTXT", false))
    4327             :         {
    4328           2 :             if (!bWriteOnlyInPAMIfNeeded)
    4329           1 :                 GDALWriteRPCTXTFile(pszTIFFFilename, papszRPCMD);
    4330           2 :             bRPCSerializedOtherWay = true;
    4331             :         }
    4332             : 
    4333          32 :         if (!bRPCSerializedOtherWay && bWriteOnlyInPAMIfNeeded && bSrcIsGeoTIFF)
    4334           1 :             cpl::down_cast<GTiffDataset *>(poSrcDS)
    4335           1 :                 ->GDALPamDataset::SetMetadata(papszRPCMD, MD_DOMAIN_RPC);
    4336             :     }
    4337       10136 : }
    4338             : 
    4339             : /************************************************************************/
    4340             : /*                           WriteMetadata()                            */
    4341             : /************************************************************************/
    4342             : 
    4343        8042 : bool GTiffDataset::WriteMetadata(GDALDataset *poSrcDS, TIFF *l_hTIFF,
    4344             :                                  bool bSrcIsGeoTIFF, GTiffProfile eProfile,
    4345             :                                  const char *pszTIFFFilename,
    4346             :                                  CSLConstList papszCreationOptions,
    4347             :                                  bool bExcludeRPBandIMGFileWriting)
    4348             : 
    4349             : {
    4350             :     /* -------------------------------------------------------------------- */
    4351             :     /*      Convert all the remaining metadata into a simple XML            */
    4352             :     /*      format.                                                         */
    4353             :     /* -------------------------------------------------------------------- */
    4354        8042 :     CPLXMLNode *psRoot = nullptr;
    4355        8042 :     CPLXMLNode *psTail = nullptr;
    4356             : 
    4357             :     const char *pszCopySrcMDD =
    4358        8042 :         CSLFetchNameValueDef(papszCreationOptions, "COPY_SRC_MDD", "AUTO");
    4359             :     char **papszSrcMDD =
    4360        8042 :         CSLFetchNameValueMultiple(papszCreationOptions, "SRC_MDD");
    4361             : 
    4362             :     GTiffDataset *poSrcDSGTiff =
    4363        8042 :         bSrcIsGeoTIFF ? cpl::down_cast<GTiffDataset *>(poSrcDS) : nullptr;
    4364             : 
    4365        8042 :     if (poSrcDSGTiff)
    4366             :     {
    4367        5921 :         WriteMDMetadata(&poSrcDSGTiff->m_oGTiffMDMD, l_hTIFF, &psRoot, &psTail,
    4368             :                         0, eProfile);
    4369             :     }
    4370             :     else
    4371             :     {
    4372        2121 :         if (EQUAL(pszCopySrcMDD, "AUTO") || CPLTestBool(pszCopySrcMDD) ||
    4373             :             papszSrcMDD)
    4374             :         {
    4375        4236 :             GDALMultiDomainMetadata l_oMDMD;
    4376             :             {
    4377        2118 :                 CSLConstList papszMD = poSrcDS->GetMetadata();
    4378        2122 :                 if (CSLCount(papszMD) > 0 &&
    4379           4 :                     (!papszSrcMDD || CSLFindString(papszSrcMDD, "") >= 0 ||
    4380           2 :                      CSLFindString(papszSrcMDD, "_DEFAULT_") >= 0))
    4381             :                 {
    4382        1601 :                     l_oMDMD.SetMetadata(papszMD);
    4383             :                 }
    4384             :             }
    4385             : 
    4386        2118 :             if (EQUAL(pszCopySrcMDD, "AUTO") && !papszSrcMDD)
    4387             :             {
    4388             :                 // Propagate ISIS3 or VICAR metadata
    4389        6327 :                 for (const char *pszMDD : {"json:ISIS3", "json:VICAR"})
    4390             :                 {
    4391        4218 :                     CSLConstList papszMD = poSrcDS->GetMetadata(pszMDD);
    4392        4218 :                     if (papszMD)
    4393             :                     {
    4394           5 :                         l_oMDMD.SetMetadata(papszMD, pszMDD);
    4395             :                     }
    4396             :                 }
    4397             :             }
    4398             : 
    4399        2118 :             if ((!EQUAL(pszCopySrcMDD, "AUTO") && CPLTestBool(pszCopySrcMDD)) ||
    4400             :                 papszSrcMDD)
    4401             :             {
    4402           9 :                 char **papszDomainList = poSrcDS->GetMetadataDomainList();
    4403          39 :                 for (CSLConstList papszIter = papszDomainList;
    4404          39 :                      papszIter && *papszIter; ++papszIter)
    4405             :                 {
    4406          30 :                     const char *pszDomain = *papszIter;
    4407          46 :                     if (pszDomain[0] != 0 &&
    4408          16 :                         (!papszSrcMDD ||
    4409          16 :                          CSLFindString(papszSrcMDD, pszDomain) >= 0))
    4410             :                     {
    4411          12 :                         l_oMDMD.SetMetadata(poSrcDS->GetMetadata(pszDomain),
    4412             :                                             pszDomain);
    4413             :                     }
    4414             :                 }
    4415           9 :                 CSLDestroy(papszDomainList);
    4416             :             }
    4417             : 
    4418        2118 :             WriteMDMetadata(&l_oMDMD, l_hTIFF, &psRoot, &psTail, 0, eProfile);
    4419             :         }
    4420             :     }
    4421             : 
    4422        8042 :     if (!bExcludeRPBandIMGFileWriting &&
    4423        5915 :         (!poSrcDSGTiff || poSrcDSGTiff->m_poBaseDS == nullptr))
    4424             :     {
    4425        8031 :         WriteRPC(poSrcDS, l_hTIFF, bSrcIsGeoTIFF, eProfile, pszTIFFFilename,
    4426             :                  papszCreationOptions);
    4427             : 
    4428             :         /* ------------------------------------------------------------------ */
    4429             :         /*      Handle metadata data written to an IMD file. */
    4430             :         /* ------------------------------------------------------------------ */
    4431        8031 :         CSLConstList papszIMDMD = poSrcDS->GetMetadata(MD_DOMAIN_IMD);
    4432        8031 :         if (papszIMDMD != nullptr)
    4433             :         {
    4434          20 :             GDALWriteIMDFile(pszTIFFFilename, papszIMDMD);
    4435             :         }
    4436             :     }
    4437             : 
    4438        8042 :     uint16_t nPhotometric = 0;
    4439        8042 :     if (!TIFFGetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, &(nPhotometric)))
    4440           1 :         nPhotometric = PHOTOMETRIC_MINISBLACK;
    4441             : 
    4442        8042 :     const bool bStandardColorInterp = GTIFFIsStandardColorInterpretation(
    4443             :         GDALDataset::ToHandle(poSrcDS), nPhotometric, papszCreationOptions);
    4444             : 
    4445             :     /* -------------------------------------------------------------------- */
    4446             :     /*      We also need to address band specific metadata, and special     */
    4447             :     /*      "role" metadata.                                                */
    4448             :     /* -------------------------------------------------------------------- */
    4449      315969 :     for (int nBand = 1; nBand <= poSrcDS->GetRasterCount(); ++nBand)
    4450             :     {
    4451      307927 :         GDALRasterBand *poBand = poSrcDS->GetRasterBand(nBand);
    4452             : 
    4453      307927 :         if (bSrcIsGeoTIFF)
    4454             :         {
    4455             :             GTiffRasterBand *poSrcBandGTiff =
    4456      302889 :                 cpl::down_cast<GTiffRasterBand *>(poBand);
    4457      302889 :             assert(poSrcBandGTiff);
    4458      302889 :             WriteMDMetadata(&poSrcBandGTiff->m_oGTiffMDMD, l_hTIFF, &psRoot,
    4459             :                             &psTail, nBand, eProfile);
    4460             :         }
    4461             :         else
    4462             :         {
    4463       10076 :             GDALMultiDomainMetadata l_oMDMD;
    4464        5038 :             bool bOMDMDSet = false;
    4465             : 
    4466        5038 :             if (EQUAL(pszCopySrcMDD, "AUTO") && !papszSrcMDD)
    4467             :             {
    4468       15078 :                 for (const char *pszDomain : {"", "IMAGERY"})
    4469             :                 {
    4470       10052 :                     if (CSLConstList papszMD = poBand->GetMetadata(pszDomain))
    4471             :                     {
    4472          89 :                         if (papszMD[0])
    4473             :                         {
    4474          89 :                             bOMDMDSet = true;
    4475          89 :                             l_oMDMD.SetMetadata(papszMD, pszDomain);
    4476             :                         }
    4477             :                     }
    4478        5026 :                 }
    4479             :             }
    4480          12 :             else if (CPLTestBool(pszCopySrcMDD) || papszSrcMDD)
    4481             :             {
    4482           9 :                 char **papszDomainList = poBand->GetMetadataDomainList();
    4483           3 :                 for (const char *pszDomain :
    4484          15 :                      cpl::Iterate(CSLConstList(papszDomainList)))
    4485             :                 {
    4486           9 :                     if (pszDomain[0] != 0 &&
    4487           5 :                         !EQUAL(pszDomain, "IMAGE_STRUCTURE") &&
    4488           2 :                         (!papszSrcMDD ||
    4489           2 :                          CSLFindString(papszSrcMDD, pszDomain) >= 0))
    4490             :                     {
    4491           2 :                         bOMDMDSet = true;
    4492           2 :                         l_oMDMD.SetMetadata(poBand->GetMetadata(pszDomain),
    4493             :                                             pszDomain);
    4494             :                     }
    4495             :                 }
    4496           9 :                 CSLDestroy(papszDomainList);
    4497             :             }
    4498             : 
    4499        5038 :             if (bOMDMDSet)
    4500             :             {
    4501          91 :                 WriteMDMetadata(&l_oMDMD, l_hTIFF, &psRoot, &psTail, nBand,
    4502             :                                 eProfile);
    4503             :             }
    4504             :         }
    4505             : 
    4506      307927 :         const double dfOffset = poBand->GetOffset();
    4507      307927 :         const double dfScale = poBand->GetScale();
    4508      307927 :         bool bGeoTIFFScaleOffsetInZ = false;
    4509      307927 :         GDALGeoTransform gt;
    4510             :         // Check if we have already encoded scale/offset in the GeoTIFF tags
    4511      314007 :         if (poSrcDS->GetGeoTransform(gt) == CE_None && gt.xrot == 0.0 &&
    4512        6064 :             gt.yrot == 0.0 && gt.yscale < 0.0 && poSrcDS->GetSpatialRef() &&
    4513      314014 :             poSrcDS->GetSpatialRef()->IsVertical() &&
    4514           7 :             poSrcDS->GetRasterCount() == 1)
    4515             :         {
    4516           7 :             bGeoTIFFScaleOffsetInZ = true;
    4517             :         }
    4518             : 
    4519      307927 :         if ((dfOffset != 0.0 || dfScale != 1.0) && !bGeoTIFFScaleOffsetInZ)
    4520             :         {
    4521          25 :             char szValue[128] = {};
    4522             : 
    4523          25 :             CPLsnprintf(szValue, sizeof(szValue), "%.17g", dfOffset);
    4524          25 :             AppendMetadataItem(&psRoot, &psTail, "OFFSET", szValue, nBand,
    4525             :                                "offset", "");
    4526          25 :             CPLsnprintf(szValue, sizeof(szValue), "%.17g", dfScale);
    4527          25 :             AppendMetadataItem(&psRoot, &psTail, "SCALE", szValue, nBand,
    4528             :                                "scale", "");
    4529             :         }
    4530             : 
    4531      307927 :         const char *pszUnitType = poBand->GetUnitType();
    4532      307927 :         if (pszUnitType != nullptr && pszUnitType[0] != '\0')
    4533             :         {
    4534          40 :             bool bWriteUnit = true;
    4535          40 :             auto poSRS = poSrcDS->GetSpatialRef();
    4536          40 :             if (poSRS && poSRS->IsCompound())
    4537             :             {
    4538           2 :                 const char *pszVertUnit = nullptr;
    4539           2 :                 poSRS->GetTargetLinearUnits("COMPD_CS|VERT_CS", &pszVertUnit);
    4540           2 :                 if (pszVertUnit && EQUAL(pszVertUnit, pszUnitType))
    4541             :                 {
    4542           2 :                     bWriteUnit = false;
    4543             :                 }
    4544             :             }
    4545          40 :             if (bWriteUnit)
    4546             :             {
    4547          38 :                 AppendMetadataItem(&psRoot, &psTail, "UNITTYPE", pszUnitType,
    4548             :                                    nBand, "unittype", "");
    4549             :             }
    4550             :         }
    4551             : 
    4552      307927 :         if (strlen(poBand->GetDescription()) > 0)
    4553             :         {
    4554          24 :             AppendMetadataItem(&psRoot, &psTail, "DESCRIPTION",
    4555          24 :                                poBand->GetDescription(), nBand, "description",
    4556             :                                "");
    4557             :         }
    4558             : 
    4559      308144 :         if (!bStandardColorInterp &&
    4560         217 :             !(nBand <= 3 && EQUAL(CSLFetchNameValueDef(papszCreationOptions,
    4561             :                                                        "PHOTOMETRIC", ""),
    4562             :                                   "RGB")))
    4563             :         {
    4564         250 :             AppendMetadataItem(&psRoot, &psTail, "COLORINTERP",
    4565             :                                GDALGetColorInterpretationName(
    4566         250 :                                    poBand->GetColorInterpretation()),
    4567             :                                nBand, "colorinterp", "");
    4568             :         }
    4569             :     }
    4570             : 
    4571        8042 :     CSLDestroy(papszSrcMDD);
    4572             : 
    4573             :     const char *pszTilingSchemeName =
    4574        8042 :         CSLFetchNameValue(papszCreationOptions, "@TILING_SCHEME_NAME");
    4575        8042 :     if (pszTilingSchemeName)
    4576             :     {
    4577          23 :         AppendMetadataItem(&psRoot, &psTail, "NAME", pszTilingSchemeName, 0,
    4578             :                            nullptr, "TILING_SCHEME");
    4579             : 
    4580          23 :         const char *pszZoomLevel = CSLFetchNameValue(
    4581             :             papszCreationOptions, "@TILING_SCHEME_ZOOM_LEVEL");
    4582          23 :         if (pszZoomLevel)
    4583             :         {
    4584          23 :             AppendMetadataItem(&psRoot, &psTail, "ZOOM_LEVEL", pszZoomLevel, 0,
    4585             :                                nullptr, "TILING_SCHEME");
    4586             :         }
    4587             : 
    4588          23 :         const char *pszAlignedLevels = CSLFetchNameValue(
    4589             :             papszCreationOptions, "@TILING_SCHEME_ALIGNED_LEVELS");
    4590          23 :         if (pszAlignedLevels)
    4591             :         {
    4592           4 :             AppendMetadataItem(&psRoot, &psTail, "ALIGNED_LEVELS",
    4593             :                                pszAlignedLevels, 0, nullptr, "TILING_SCHEME");
    4594             :         }
    4595             :     }
    4596             : 
    4597        8042 :     if (const char *pszOverviewResampling =
    4598        8042 :             CSLFetchNameValue(papszCreationOptions, "@OVERVIEW_RESAMPLING"))
    4599             :     {
    4600          39 :         AppendMetadataItem(&psRoot, &psTail, "OVERVIEW_RESAMPLING",
    4601             :                            pszOverviewResampling, 0, nullptr,
    4602             :                            "IMAGE_STRUCTURE");
    4603             :     }
    4604             : 
    4605             :     /* -------------------------------------------------------------------- */
    4606             :     /*      Write information about some codecs.                            */
    4607             :     /* -------------------------------------------------------------------- */
    4608        8042 :     if (CPLTestBool(
    4609             :             CPLGetConfigOption("GTIFF_WRITE_IMAGE_STRUCTURE_METADATA", "YES")))
    4610             :     {
    4611             :         const char *pszTileInterleave =
    4612        8037 :             CSLFetchNameValue(papszCreationOptions, "@TILE_INTERLEAVE");
    4613        8037 :         if (pszTileInterleave && CPLTestBool(pszTileInterleave))
    4614             :         {
    4615           7 :             AppendMetadataItem(&psRoot, &psTail, "INTERLEAVE", "TILE", 0,
    4616             :                                nullptr, "IMAGE_STRUCTURE");
    4617             :         }
    4618             : 
    4619             :         const char *pszCompress =
    4620        8037 :             CSLFetchNameValue(papszCreationOptions, "COMPRESS");
    4621        8037 :         if (pszCompress && EQUAL(pszCompress, "WEBP"))
    4622             :         {
    4623          31 :             if (GTiffGetWebPLossless(papszCreationOptions))
    4624             :             {
    4625           6 :                 AppendMetadataItem(&psRoot, &psTail,
    4626             :                                    "COMPRESSION_REVERSIBILITY", "LOSSLESS", 0,
    4627             :                                    nullptr, "IMAGE_STRUCTURE");
    4628             :             }
    4629             :             else
    4630             :             {
    4631          25 :                 AppendMetadataItem(
    4632             :                     &psRoot, &psTail, "WEBP_LEVEL",
    4633          25 :                     CPLSPrintf("%d", GTiffGetWebPLevel(papszCreationOptions)),
    4634             :                     0, nullptr, "IMAGE_STRUCTURE");
    4635             :             }
    4636             :         }
    4637        8006 :         else if (pszCompress && STARTS_WITH_CI(pszCompress, "LERC"))
    4638             :         {
    4639             :             const double dfMaxZError =
    4640          97 :                 GTiffGetLERCMaxZError(papszCreationOptions);
    4641             :             const double dfMaxZErrorOverview =
    4642          97 :                 GTiffGetLERCMaxZErrorOverview(papszCreationOptions);
    4643          97 :             if (dfMaxZError == 0.0 && dfMaxZErrorOverview == 0.0)
    4644             :             {
    4645          83 :                 AppendMetadataItem(&psRoot, &psTail,
    4646             :                                    "COMPRESSION_REVERSIBILITY", "LOSSLESS", 0,
    4647             :                                    nullptr, "IMAGE_STRUCTURE");
    4648             :             }
    4649             :             else
    4650             :             {
    4651          14 :                 AppendMetadataItem(&psRoot, &psTail, "MAX_Z_ERROR",
    4652             :                                    CSLFetchNameValueDef(papszCreationOptions,
    4653             :                                                         "MAX_Z_ERROR", ""),
    4654             :                                    0, nullptr, "IMAGE_STRUCTURE");
    4655          14 :                 if (dfMaxZError != dfMaxZErrorOverview)
    4656             :                 {
    4657           3 :                     AppendMetadataItem(
    4658             :                         &psRoot, &psTail, "MAX_Z_ERROR_OVERVIEW",
    4659             :                         CSLFetchNameValueDef(papszCreationOptions,
    4660             :                                              "MAX_Z_ERROR_OVERVIEW", ""),
    4661             :                         0, nullptr, "IMAGE_STRUCTURE");
    4662             :                 }
    4663          97 :             }
    4664             :         }
    4665             : #if HAVE_JXL
    4666        7909 :         else if (pszCompress && EQUAL(pszCompress, "JXL"))
    4667             :         {
    4668         101 :             float fDistance = 0.0f;
    4669         101 :             if (GTiffGetJXLLossless(papszCreationOptions))
    4670             :             {
    4671          82 :                 AppendMetadataItem(&psRoot, &psTail,
    4672             :                                    "COMPRESSION_REVERSIBILITY", "LOSSLESS", 0,
    4673             :                                    nullptr, "IMAGE_STRUCTURE");
    4674             :             }
    4675             :             else
    4676             :             {
    4677          19 :                 fDistance = GTiffGetJXLDistance(papszCreationOptions);
    4678          19 :                 AppendMetadataItem(
    4679             :                     &psRoot, &psTail, "JXL_DISTANCE",
    4680             :                     CPLSPrintf("%f", static_cast<double>(fDistance)), 0,
    4681             :                     nullptr, "IMAGE_STRUCTURE");
    4682             :             }
    4683             :             const float fAlphaDistance =
    4684         101 :                 GTiffGetJXLAlphaDistance(papszCreationOptions);
    4685         101 :             if (fAlphaDistance >= 0.0f && fAlphaDistance != fDistance)
    4686             :             {
    4687           2 :                 AppendMetadataItem(
    4688             :                     &psRoot, &psTail, "JXL_ALPHA_DISTANCE",
    4689             :                     CPLSPrintf("%f", static_cast<double>(fAlphaDistance)), 0,
    4690             :                     nullptr, "IMAGE_STRUCTURE");
    4691             :             }
    4692         101 :             AppendMetadataItem(
    4693             :                 &psRoot, &psTail, "JXL_EFFORT",
    4694             :                 CPLSPrintf("%d", GTiffGetJXLEffort(papszCreationOptions)), 0,
    4695             :                 nullptr, "IMAGE_STRUCTURE");
    4696             :         }
    4697             : #endif
    4698             :     }
    4699             : 
    4700        8042 :     if (!CPLTestBool(CPLGetConfigOption("GTIFF_WRITE_RAT_TO_PAM", "NO")))
    4701             :     {
    4702      315965 :         for (int nBand = 1; nBand <= poSrcDS->GetRasterCount(); ++nBand)
    4703             :         {
    4704      307925 :             GDALRasterBand *poBand = poSrcDS->GetRasterBand(nBand);
    4705      307925 :             const auto poRAT = poBand->GetDefaultRAT();
    4706      307925 :             if (poRAT)
    4707             :             {
    4708          21 :                 auto psSerializedRAT = poRAT->Serialize();
    4709          21 :                 if (psSerializedRAT)
    4710             :                 {
    4711          20 :                     AppendMetadataItem(
    4712             :                         &psRoot, &psTail, DEFAULT_RASTER_ATTRIBUTE_TABLE,
    4713             :                         nullptr, psSerializedRAT, nBand, RAT_ROLE, nullptr);
    4714             :                 }
    4715             :             }
    4716             :         }
    4717             :     }
    4718             : 
    4719             :     /* -------------------------------------------------------------------- */
    4720             :     /*      Write out the generic XML metadata if there is any.             */
    4721             :     /* -------------------------------------------------------------------- */
    4722        8042 :     if (psRoot != nullptr)
    4723             :     {
    4724         760 :         bool bRet = true;
    4725             : 
    4726         760 :         if (eProfile == GTiffProfile::GDALGEOTIFF)
    4727             :         {
    4728         743 :             char *pszXML_MD = CPLSerializeXMLTree(psRoot);
    4729         743 :             TIFFSetField(l_hTIFF, TIFFTAG_GDAL_METADATA, pszXML_MD);
    4730         743 :             CPLFree(pszXML_MD);
    4731             :         }
    4732             :         else
    4733             :         {
    4734          17 :             if (bSrcIsGeoTIFF)
    4735          11 :                 cpl::down_cast<GTiffDataset *>(poSrcDS)->PushMetadataToPam();
    4736             :             else
    4737           6 :                 bRet = false;
    4738             :         }
    4739             : 
    4740         760 :         CPLDestroyXMLNode(psRoot);
    4741             : 
    4742         760 :         return bRet;
    4743             :     }
    4744             : 
    4745             :     // If we have no more metadata but it existed before,
    4746             :     // remove the GDAL_METADATA tag.
    4747        7282 :     if (eProfile == GTiffProfile::GDALGEOTIFF)
    4748             :     {
    4749        7258 :         char *pszText = nullptr;
    4750        7258 :         if (TIFFGetField(l_hTIFF, TIFFTAG_GDAL_METADATA, &pszText))
    4751             :         {
    4752           7 :             TIFFUnsetField(l_hTIFF, TIFFTAG_GDAL_METADATA);
    4753             :         }
    4754             :     }
    4755             : 
    4756        7282 :     return true;
    4757             : }
    4758             : 
    4759             : /************************************************************************/
    4760             : /*                         PushMetadataToPam()                          */
    4761             : /*                                                                      */
    4762             : /*      When producing a strict profile TIFF or if our aggregate        */
    4763             : /*      metadata is too big for a single tiff tag we may end up         */
    4764             : /*      needing to write it via the PAM mechanisms.  This method        */
    4765             : /*      copies all the appropriate metadata into the PAM level          */
    4766             : /*      metadata object but with special care to avoid copying          */
    4767             : /*      metadata handled in other ways in TIFF format.                  */
    4768             : /************************************************************************/
    4769             : 
    4770          17 : void GTiffDataset::PushMetadataToPam()
    4771             : 
    4772             : {
    4773          17 :     if (GetPamFlags() & GPF_DISABLED)
    4774           0 :         return;
    4775             : 
    4776          17 :     const bool bStandardColorInterp = GTIFFIsStandardColorInterpretation(
    4777          17 :         GDALDataset::ToHandle(this), m_nPhotometric, m_papszCreationOptions);
    4778             : 
    4779          55 :     for (int nBand = 0; nBand <= GetRasterCount(); ++nBand)
    4780             :     {
    4781          38 :         GDALMultiDomainMetadata *poSrcMDMD = nullptr;
    4782          38 :         GTiffRasterBand *poBand = nullptr;
    4783             : 
    4784          38 :         if (nBand == 0)
    4785             :         {
    4786          17 :             poSrcMDMD = &(this->m_oGTiffMDMD);
    4787             :         }
    4788             :         else
    4789             :         {
    4790          21 :             poBand = cpl::down_cast<GTiffRasterBand *>(GetRasterBand(nBand));
    4791          21 :             poSrcMDMD = &(poBand->m_oGTiffMDMD);
    4792             :         }
    4793             : 
    4794             :         /* --------------------------------------------------------------------
    4795             :          */
    4796             :         /*      Loop over the available domains. */
    4797             :         /* --------------------------------------------------------------------
    4798             :          */
    4799          38 :         CSLConstList papszDomainList = poSrcMDMD->GetDomainList();
    4800          74 :         for (int iDomain = 0; papszDomainList && papszDomainList[iDomain];
    4801             :              ++iDomain)
    4802             :         {
    4803          36 :             char **papszMD = poSrcMDMD->GetMetadata(papszDomainList[iDomain]);
    4804             : 
    4805          36 :             if (EQUAL(papszDomainList[iDomain], MD_DOMAIN_RPC) ||
    4806          36 :                 EQUAL(papszDomainList[iDomain], MD_DOMAIN_IMD) ||
    4807          36 :                 EQUAL(papszDomainList[iDomain], "_temporary_") ||
    4808          36 :                 EQUAL(papszDomainList[iDomain], "IMAGE_STRUCTURE") ||
    4809          19 :                 EQUAL(papszDomainList[iDomain], "COLOR_PROFILE"))
    4810          17 :                 continue;
    4811             : 
    4812          19 :             papszMD = CSLDuplicate(papszMD);
    4813             : 
    4814          69 :             for (int i = CSLCount(papszMD) - 1; i >= 0; --i)
    4815             :             {
    4816          50 :                 if (STARTS_WITH_CI(papszMD[i], "TIFFTAG_") ||
    4817          50 :                     EQUALN(papszMD[i], GDALMD_AREA_OR_POINT,
    4818             :                            strlen(GDALMD_AREA_OR_POINT)))
    4819           4 :                     papszMD = CSLRemoveStrings(papszMD, i, 1, nullptr);
    4820             :             }
    4821             : 
    4822          19 :             if (nBand == 0)
    4823          10 :                 GDALPamDataset::SetMetadata(papszMD, papszDomainList[iDomain]);
    4824             :             else
    4825           9 :                 poBand->GDALPamRasterBand::SetMetadata(
    4826           9 :                     papszMD, papszDomainList[iDomain]);
    4827             : 
    4828          19 :             CSLDestroy(papszMD);
    4829             :         }
    4830             : 
    4831             :         /* --------------------------------------------------------------------
    4832             :          */
    4833             :         /*      Handle some "special domain" stuff. */
    4834             :         /* --------------------------------------------------------------------
    4835             :          */
    4836          38 :         if (poBand != nullptr)
    4837             :         {
    4838          21 :             poBand->GDALPamRasterBand::SetOffset(poBand->GetOffset());
    4839          21 :             poBand->GDALPamRasterBand::SetScale(poBand->GetScale());
    4840          21 :             poBand->GDALPamRasterBand::SetUnitType(poBand->GetUnitType());
    4841          21 :             poBand->GDALPamRasterBand::SetDescription(poBand->GetDescription());
    4842          21 :             if (!bStandardColorInterp)
    4843             :             {
    4844           3 :                 poBand->GDALPamRasterBand::SetColorInterpretation(
    4845           3 :                     poBand->GetColorInterpretation());
    4846             :             }
    4847             :         }
    4848             :     }
    4849          17 :     MarkPamDirty();
    4850             : }
    4851             : 
    4852             : /************************************************************************/
    4853             : /*                          WriteNoDataValue()                          */
    4854             : /************************************************************************/
    4855             : 
    4856         521 : void GTiffDataset::WriteNoDataValue(TIFF *hTIFF, double dfNoData)
    4857             : 
    4858             : {
    4859        1042 :     CPLString osVal(GTiffFormatGDALNoDataTagValue(dfNoData));
    4860         521 :     TIFFSetField(hTIFF, TIFFTAG_GDAL_NODATA, osVal.c_str());
    4861         521 : }
    4862             : 
    4863           5 : void GTiffDataset::WriteNoDataValue(TIFF *hTIFF, int64_t nNoData)
    4864             : 
    4865             : {
    4866           5 :     TIFFSetField(hTIFF, TIFFTAG_GDAL_NODATA,
    4867             :                  CPLSPrintf(CPL_FRMT_GIB, static_cast<GIntBig>(nNoData)));
    4868           5 : }
    4869             : 
    4870           5 : void GTiffDataset::WriteNoDataValue(TIFF *hTIFF, uint64_t nNoData)
    4871             : 
    4872             : {
    4873           5 :     TIFFSetField(hTIFF, TIFFTAG_GDAL_NODATA,
    4874             :                  CPLSPrintf(CPL_FRMT_GUIB, static_cast<GUIntBig>(nNoData)));
    4875           5 : }
    4876             : 
    4877             : /************************************************************************/
    4878             : /*                          UnsetNoDataValue()                          */
    4879             : /************************************************************************/
    4880             : 
    4881          16 : void GTiffDataset::UnsetNoDataValue(TIFF *l_hTIFF)
    4882             : 
    4883             : {
    4884          16 :     TIFFUnsetField(l_hTIFF, TIFFTAG_GDAL_NODATA);
    4885          16 : }
    4886             : 
    4887             : /************************************************************************/
    4888             : /*                             SaveICCProfile()                         */
    4889             : /*                                                                      */
    4890             : /*      Save ICC Profile or colorimetric data into file                 */
    4891             : /* pDS:                                                                 */
    4892             : /*      Dataset that contains the metadata with the ICC or colorimetric */
    4893             : /*      data. If this argument is specified, all other arguments are    */
    4894             : /*      ignored. Set them to NULL or 0.                                 */
    4895             : /* hTIFF:                                                               */
    4896             : /*      Pointer to TIFF handle. Only needed if pDS is NULL or           */
    4897             : /*      pDS->m_hTIFF is NULL.                                             */
    4898             : /* papszParamList:                                                       */
    4899             : /*      Options containing the ICC profile or colorimetric metadata.    */
    4900             : /*      Ignored if pDS is not NULL.                                     */
    4901             : /* nBitsPerSample:                                                      */
    4902             : /*      Bits per sample. Ignored if pDS is not NULL.                    */
    4903             : /************************************************************************/
    4904             : 
    4905        9789 : void GTiffDataset::SaveICCProfile(GTiffDataset *pDS, TIFF *l_hTIFF,
    4906             :                                   CSLConstList papszParamList,
    4907             :                                   uint32_t l_nBitsPerSample)
    4908             : {
    4909        9789 :     if ((pDS != nullptr) && (pDS->eAccess != GA_Update))
    4910           0 :         return;
    4911             : 
    4912        9789 :     if (l_hTIFF == nullptr)
    4913             :     {
    4914           2 :         if (pDS == nullptr)
    4915           0 :             return;
    4916             : 
    4917           2 :         l_hTIFF = pDS->m_hTIFF;
    4918           2 :         if (l_hTIFF == nullptr)
    4919           0 :             return;
    4920             :     }
    4921             : 
    4922        9789 :     if ((papszParamList == nullptr) && (pDS == nullptr))
    4923        4854 :         return;
    4924             : 
    4925             :     const char *pszICCProfile =
    4926             :         (pDS != nullptr)
    4927        4935 :             ? pDS->GetMetadataItem("SOURCE_ICC_PROFILE", "COLOR_PROFILE")
    4928        4933 :             : CSLFetchNameValue(papszParamList, "SOURCE_ICC_PROFILE");
    4929        4935 :     if (pszICCProfile != nullptr)
    4930             :     {
    4931           8 :         char *pEmbedBuffer = CPLStrdup(pszICCProfile);
    4932             :         int32_t nEmbedLen =
    4933           8 :             CPLBase64DecodeInPlace(reinterpret_cast<GByte *>(pEmbedBuffer));
    4934             : 
    4935           8 :         TIFFSetField(l_hTIFF, TIFFTAG_ICCPROFILE, nEmbedLen, pEmbedBuffer);
    4936             : 
    4937           8 :         CPLFree(pEmbedBuffer);
    4938             :     }
    4939             :     else
    4940             :     {
    4941             :         // Output colorimetric data.
    4942        4927 :         float pCHR[6] = {};     // Primaries.
    4943        4927 :         uint16_t pTXR[6] = {};  // Transfer range.
    4944        4927 :         const char *pszCHRNames[] = {"SOURCE_PRIMARIES_RED",
    4945             :                                      "SOURCE_PRIMARIES_GREEN",
    4946             :                                      "SOURCE_PRIMARIES_BLUE"};
    4947        4927 :         const char *pszTXRNames[] = {"TIFFTAG_TRANSFERRANGE_BLACK",
    4948             :                                      "TIFFTAG_TRANSFERRANGE_WHITE"};
    4949             : 
    4950             :         // Output chromacities.
    4951        4927 :         bool bOutputCHR = true;
    4952        4942 :         for (int i = 0; i < 3 && bOutputCHR; ++i)
    4953             :         {
    4954             :             const char *pszColorProfile =
    4955             :                 (pDS != nullptr)
    4956        4937 :                     ? pDS->GetMetadataItem(pszCHRNames[i], "COLOR_PROFILE")
    4957        4934 :                     : CSLFetchNameValue(papszParamList, pszCHRNames[i]);
    4958        4937 :             if (pszColorProfile == nullptr)
    4959             :             {
    4960        4922 :                 bOutputCHR = false;
    4961        4922 :                 break;
    4962             :             }
    4963             : 
    4964             :             const CPLStringList aosTokens(CSLTokenizeString2(
    4965             :                 pszColorProfile, ",",
    4966             :                 CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
    4967          15 :                     CSLT_STRIPENDSPACES));
    4968             : 
    4969          15 :             if (aosTokens.size() != 3)
    4970             :             {
    4971           0 :                 bOutputCHR = false;
    4972           0 :                 break;
    4973             :             }
    4974             : 
    4975          60 :             for (int j = 0; j < 3; ++j)
    4976             :             {
    4977          45 :                 float v = static_cast<float>(CPLAtof(aosTokens[j]));
    4978             : 
    4979          45 :                 if (j == 2)
    4980             :                 {
    4981             :                     // Last term of xyY color must be 1.0.
    4982          15 :                     if (v != 1.0f)
    4983             :                     {
    4984           0 :                         bOutputCHR = false;
    4985           0 :                         break;
    4986             :                     }
    4987             :                 }
    4988             :                 else
    4989             :                 {
    4990          30 :                     pCHR[i * 2 + j] = v;
    4991             :                 }
    4992             :             }
    4993             :         }
    4994             : 
    4995        4927 :         if (bOutputCHR)
    4996             :         {
    4997           5 :             TIFFSetField(l_hTIFF, TIFFTAG_PRIMARYCHROMATICITIES, pCHR);
    4998             :         }
    4999             : 
    5000             :         // Output whitepoint.
    5001             :         const char *pszSourceWhitePoint =
    5002             :             (pDS != nullptr)
    5003        4927 :                 ? pDS->GetMetadataItem("SOURCE_WHITEPOINT", "COLOR_PROFILE")
    5004        4926 :                 : CSLFetchNameValue(papszParamList, "SOURCE_WHITEPOINT");
    5005        4927 :         if (pszSourceWhitePoint != nullptr)
    5006             :         {
    5007             :             const CPLStringList aosTokens(CSLTokenizeString2(
    5008             :                 pszSourceWhitePoint, ",",
    5009             :                 CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
    5010          10 :                     CSLT_STRIPENDSPACES));
    5011             : 
    5012           5 :             bool bOutputWhitepoint = true;
    5013           5 :             float pWP[2] = {0.0f, 0.0f};  // Whitepoint
    5014           5 :             if (aosTokens.size() != 3)
    5015             :             {
    5016           0 :                 bOutputWhitepoint = false;
    5017             :             }
    5018             :             else
    5019             :             {
    5020          20 :                 for (int j = 0; j < 3; ++j)
    5021             :                 {
    5022          15 :                     const float v = static_cast<float>(CPLAtof(aosTokens[j]));
    5023             : 
    5024          15 :                     if (j == 2)
    5025             :                     {
    5026             :                         // Last term of xyY color must be 1.0.
    5027           5 :                         if (v != 1.0f)
    5028             :                         {
    5029           0 :                             bOutputWhitepoint = false;
    5030           0 :                             break;
    5031             :                         }
    5032             :                     }
    5033             :                     else
    5034             :                     {
    5035          10 :                         pWP[j] = v;
    5036             :                     }
    5037             :                 }
    5038             :             }
    5039             : 
    5040           5 :             if (bOutputWhitepoint)
    5041             :             {
    5042           5 :                 TIFFSetField(l_hTIFF, TIFFTAG_WHITEPOINT, pWP);
    5043             :             }
    5044             :         }
    5045             : 
    5046             :         // Set transfer function metadata.
    5047             :         char const *pszTFRed =
    5048             :             (pDS != nullptr)
    5049        4927 :                 ? pDS->GetMetadataItem("TIFFTAG_TRANSFERFUNCTION_RED",
    5050             :                                        "COLOR_PROFILE")
    5051        4926 :                 : CSLFetchNameValue(papszParamList,
    5052        4927 :                                     "TIFFTAG_TRANSFERFUNCTION_RED");
    5053             : 
    5054             :         char const *pszTFGreen =
    5055             :             (pDS != nullptr)
    5056        4927 :                 ? pDS->GetMetadataItem("TIFFTAG_TRANSFERFUNCTION_GREEN",
    5057             :                                        "COLOR_PROFILE")
    5058        4926 :                 : CSLFetchNameValue(papszParamList,
    5059        4927 :                                     "TIFFTAG_TRANSFERFUNCTION_GREEN");
    5060             : 
    5061             :         char const *pszTFBlue =
    5062             :             (pDS != nullptr)
    5063        4927 :                 ? pDS->GetMetadataItem("TIFFTAG_TRANSFERFUNCTION_BLUE",
    5064             :                                        "COLOR_PROFILE")
    5065        4926 :                 : CSLFetchNameValue(papszParamList,
    5066        4927 :                                     "TIFFTAG_TRANSFERFUNCTION_BLUE");
    5067             : 
    5068        4927 :         if ((pszTFRed != nullptr) && (pszTFGreen != nullptr) &&
    5069             :             (pszTFBlue != nullptr))
    5070             :         {
    5071             :             // Get length of table.
    5072           4 :             const int nTransferFunctionLength =
    5073           4 :                 1 << ((pDS != nullptr) ? pDS->m_nBitsPerSample
    5074             :                                        : l_nBitsPerSample);
    5075             : 
    5076             :             const CPLStringList aosTokensRed(CSLTokenizeString2(
    5077             :                 pszTFRed, ",",
    5078             :                 CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
    5079           8 :                     CSLT_STRIPENDSPACES));
    5080             :             const CPLStringList aosTokensGreen(CSLTokenizeString2(
    5081             :                 pszTFGreen, ",",
    5082             :                 CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
    5083           8 :                     CSLT_STRIPENDSPACES));
    5084             :             const CPLStringList aosTokensBlue(CSLTokenizeString2(
    5085             :                 pszTFBlue, ",",
    5086             :                 CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
    5087           8 :                     CSLT_STRIPENDSPACES));
    5088             : 
    5089           4 :             if ((aosTokensRed.size() == nTransferFunctionLength) &&
    5090           8 :                 (aosTokensGreen.size() == nTransferFunctionLength) &&
    5091           4 :                 (aosTokensBlue.size() == nTransferFunctionLength))
    5092             :             {
    5093             :                 std::vector<uint16_t> anTransferFuncRed(
    5094           8 :                     nTransferFunctionLength);
    5095             :                 std::vector<uint16_t> anTransferFuncGreen(
    5096           8 :                     nTransferFunctionLength);
    5097             :                 std::vector<uint16_t> anTransferFuncBlue(
    5098           8 :                     nTransferFunctionLength);
    5099             : 
    5100             :                 // Convert our table in string format into int16_t format.
    5101        1028 :                 for (int i = 0; i < nTransferFunctionLength; ++i)
    5102             :                 {
    5103        2048 :                     anTransferFuncRed[i] =
    5104        1024 :                         static_cast<uint16_t>(atoi(aosTokensRed[i]));
    5105        2048 :                     anTransferFuncGreen[i] =
    5106        1024 :                         static_cast<uint16_t>(atoi(aosTokensGreen[i]));
    5107        1024 :                     anTransferFuncBlue[i] =
    5108        1024 :                         static_cast<uint16_t>(atoi(aosTokensBlue[i]));
    5109             :                 }
    5110             : 
    5111           4 :                 TIFFSetField(
    5112             :                     l_hTIFF, TIFFTAG_TRANSFERFUNCTION, anTransferFuncRed.data(),
    5113             :                     anTransferFuncGreen.data(), anTransferFuncBlue.data());
    5114             :             }
    5115             :         }
    5116             : 
    5117             :         // Output transfer range.
    5118        4927 :         bool bOutputTransferRange = true;
    5119        4927 :         for (int i = 0; (i < 2) && bOutputTransferRange; ++i)
    5120             :         {
    5121             :             const char *pszTXRVal =
    5122             :                 (pDS != nullptr)
    5123        4927 :                     ? pDS->GetMetadataItem(pszTXRNames[i], "COLOR_PROFILE")
    5124        4926 :                     : CSLFetchNameValue(papszParamList, pszTXRNames[i]);
    5125        4927 :             if (pszTXRVal == nullptr)
    5126             :             {
    5127        4927 :                 bOutputTransferRange = false;
    5128        4927 :                 break;
    5129             :             }
    5130             : 
    5131             :             const CPLStringList aosTokens(CSLTokenizeString2(
    5132             :                 pszTXRVal, ",",
    5133             :                 CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
    5134           0 :                     CSLT_STRIPENDSPACES));
    5135             : 
    5136           0 :             if (aosTokens.size() != 3)
    5137             :             {
    5138           0 :                 bOutputTransferRange = false;
    5139           0 :                 break;
    5140             :             }
    5141             : 
    5142           0 :             for (int j = 0; j < 3; ++j)
    5143             :             {
    5144           0 :                 pTXR[i + j * 2] = static_cast<uint16_t>(atoi(aosTokens[j]));
    5145             :             }
    5146             :         }
    5147             : 
    5148        4927 :         if (bOutputTransferRange)
    5149             :         {
    5150           0 :             const int TIFFTAG_TRANSFERRANGE = 0x0156;
    5151           0 :             TIFFSetField(l_hTIFF, TIFFTAG_TRANSFERRANGE, pTXR);
    5152             :         }
    5153             :     }
    5154             : }
    5155             : 
    5156       17673 : static signed char GTiffGetLZMAPreset(CSLConstList papszOptions)
    5157             : {
    5158       17673 :     int nLZMAPreset = -1;
    5159       17673 :     const char *pszValue = CSLFetchNameValue(papszOptions, "LZMA_PRESET");
    5160       17673 :     if (pszValue != nullptr)
    5161             :     {
    5162          20 :         nLZMAPreset = atoi(pszValue);
    5163          20 :         if (!(nLZMAPreset >= 0 && nLZMAPreset <= 9))
    5164             :         {
    5165           0 :             CPLError(CE_Warning, CPLE_IllegalArg,
    5166             :                      "LZMA_PRESET=%s value not recognised, ignoring.",
    5167             :                      pszValue);
    5168           0 :             nLZMAPreset = -1;
    5169             :         }
    5170             :     }
    5171       17673 :     return static_cast<signed char>(nLZMAPreset);
    5172             : }
    5173             : 
    5174       17673 : static signed char GTiffGetZSTDPreset(CSLConstList papszOptions)
    5175             : {
    5176       17673 :     int nZSTDLevel = -1;
    5177       17673 :     const char *pszValue = CSLFetchNameValue(papszOptions, "ZSTD_LEVEL");
    5178       17673 :     if (pszValue != nullptr)
    5179             :     {
    5180          24 :         nZSTDLevel = atoi(pszValue);
    5181          24 :         if (!(nZSTDLevel >= 1 && nZSTDLevel <= 22))
    5182             :         {
    5183           0 :             CPLError(CE_Warning, CPLE_IllegalArg,
    5184             :                      "ZSTD_LEVEL=%s value not recognised, ignoring.", pszValue);
    5185           0 :             nZSTDLevel = -1;
    5186             :         }
    5187             :     }
    5188       17673 :     return static_cast<signed char>(nZSTDLevel);
    5189             : }
    5190             : 
    5191       17673 : static signed char GTiffGetZLevel(CSLConstList papszOptions)
    5192             : {
    5193       17673 :     int nZLevel = -1;
    5194       17673 :     const char *pszValue = CSLFetchNameValue(papszOptions, "ZLEVEL");
    5195       17673 :     if (pszValue != nullptr)
    5196             :     {
    5197          44 :         nZLevel = atoi(pszValue);
    5198             : #ifdef TIFFTAG_DEFLATE_SUBCODEC
    5199          44 :         constexpr int nMaxLevel = 12;
    5200             : #ifndef LIBDEFLATE_SUPPORT
    5201             :         if (nZLevel > 9 && nZLevel <= nMaxLevel)
    5202             :         {
    5203             :             CPLDebug("GTiff",
    5204             :                      "ZLEVEL=%d not supported in a non-libdeflate enabled "
    5205             :                      "libtiff build. Capping to 9",
    5206             :                      nZLevel);
    5207             :             nZLevel = 9;
    5208             :         }
    5209             : #endif
    5210             : #else
    5211             :         constexpr int nMaxLevel = 9;
    5212             : #endif
    5213          44 :         if (nZLevel < 1 || nZLevel > nMaxLevel)
    5214             :         {
    5215           0 :             CPLError(CE_Warning, CPLE_IllegalArg,
    5216             :                      "ZLEVEL=%s value not recognised, ignoring.", pszValue);
    5217           0 :             nZLevel = -1;
    5218             :         }
    5219             :     }
    5220       17673 :     return static_cast<signed char>(nZLevel);
    5221             : }
    5222             : 
    5223       17673 : static signed char GTiffGetJpegQuality(CSLConstList papszOptions)
    5224             : {
    5225       17673 :     int nJpegQuality = -1;
    5226       17673 :     const char *pszValue = CSLFetchNameValue(papszOptions, "JPEG_QUALITY");
    5227       17673 :     if (pszValue != nullptr)
    5228             :     {
    5229        1939 :         nJpegQuality = atoi(pszValue);
    5230        1939 :         if (nJpegQuality < 1 || nJpegQuality > 100)
    5231             :         {
    5232           0 :             CPLError(CE_Warning, CPLE_IllegalArg,
    5233             :                      "JPEG_QUALITY=%s value not recognised, ignoring.",
    5234             :                      pszValue);
    5235           0 :             nJpegQuality = -1;
    5236             :         }
    5237             :     }
    5238       17673 :     return static_cast<signed char>(nJpegQuality);
    5239             : }
    5240             : 
    5241       17673 : static signed char GTiffGetJpegTablesMode(CSLConstList papszOptions)
    5242             : {
    5243       17673 :     return static_cast<signed char>(atoi(
    5244             :         CSLFetchNameValueDef(papszOptions, "JPEGTABLESMODE",
    5245       17673 :                              CPLSPrintf("%d", knGTIFFJpegTablesModeDefault))));
    5246             : }
    5247             : 
    5248             : /************************************************************************/
    5249             : /*                        GetDiscardLsbOption()                         */
    5250             : /************************************************************************/
    5251             : 
    5252        7825 : static GTiffDataset::MaskOffset *GetDiscardLsbOption(TIFF *hTIFF,
    5253             :                                                      CSLConstList papszOptions)
    5254             : {
    5255        7825 :     const char *pszBits = CSLFetchNameValue(papszOptions, "DISCARD_LSB");
    5256        7825 :     if (pszBits == nullptr)
    5257        7703 :         return nullptr;
    5258             : 
    5259         122 :     uint16_t nPhotometric = 0;
    5260         122 :     TIFFGetFieldDefaulted(hTIFF, TIFFTAG_PHOTOMETRIC, &nPhotometric);
    5261             : 
    5262         122 :     uint16_t nBitsPerSample = 0;
    5263         122 :     if (!TIFFGetField(hTIFF, TIFFTAG_BITSPERSAMPLE, &nBitsPerSample))
    5264           0 :         nBitsPerSample = 1;
    5265             : 
    5266         122 :     uint16_t nSamplesPerPixel = 0;
    5267         122 :     if (!TIFFGetField(hTIFF, TIFFTAG_SAMPLESPERPIXEL, &nSamplesPerPixel))
    5268           0 :         nSamplesPerPixel = 1;
    5269             : 
    5270         122 :     uint16_t nSampleFormat = 0;
    5271         122 :     if (!TIFFGetField(hTIFF, TIFFTAG_SAMPLEFORMAT, &nSampleFormat))
    5272           0 :         nSampleFormat = SAMPLEFORMAT_UINT;
    5273             : 
    5274         122 :     if (nPhotometric == PHOTOMETRIC_PALETTE)
    5275             :     {
    5276           1 :         CPLError(CE_Warning, CPLE_AppDefined,
    5277             :                  "DISCARD_LSB ignored on a paletted image");
    5278           1 :         return nullptr;
    5279             :     }
    5280         121 :     if (!(nBitsPerSample == 8 || nBitsPerSample == 16 || nBitsPerSample == 32 ||
    5281          13 :           nBitsPerSample == 64))
    5282             :     {
    5283           1 :         CPLError(CE_Warning, CPLE_AppDefined,
    5284             :                  "DISCARD_LSB ignored on non 8, 16, 32 or 64 bits images");
    5285           1 :         return nullptr;
    5286             :     }
    5287             : 
    5288         240 :     const CPLStringList aosTokens(CSLTokenizeString2(pszBits, ",", 0));
    5289         120 :     const int nTokens = aosTokens.size();
    5290         120 :     GTiffDataset::MaskOffset *panMaskOffsetLsb = nullptr;
    5291         120 :     if (nTokens == 1 || nTokens == nSamplesPerPixel)
    5292             :     {
    5293             :         panMaskOffsetLsb = static_cast<GTiffDataset::MaskOffset *>(
    5294         119 :             CPLCalloc(nSamplesPerPixel, sizeof(GTiffDataset::MaskOffset)));
    5295         374 :         for (int i = 0; i < nSamplesPerPixel; ++i)
    5296             :         {
    5297         255 :             const int nBits = atoi(aosTokens[nTokens == 1 ? 0 : i]);
    5298         510 :             const int nMaxBits = (nSampleFormat == SAMPLEFORMAT_IEEEFP)
    5299         510 :                                      ? ((nBitsPerSample == 16)   ? 11 - 1
    5300          78 :                                         : (nBitsPerSample == 32) ? 23 - 1
    5301          26 :                                         : (nBitsPerSample == 64) ? 53 - 1
    5302             :                                                                  : 0)
    5303         203 :                                  : nSampleFormat == SAMPLEFORMAT_INT
    5304         203 :                                      ? nBitsPerSample - 2
    5305         119 :                                      : nBitsPerSample - 1;
    5306             : 
    5307         255 :             if (nBits < 0 || nBits > nMaxBits)
    5308             :             {
    5309           0 :                 CPLError(
    5310             :                     CE_Warning, CPLE_AppDefined,
    5311             :                     "DISCARD_LSB ignored: values should be in [0,%d] range",
    5312             :                     nMaxBits);
    5313           0 :                 VSIFree(panMaskOffsetLsb);
    5314           0 :                 return nullptr;
    5315             :             }
    5316         255 :             panMaskOffsetLsb[i].nMask =
    5317         255 :                 ~((static_cast<uint64_t>(1) << nBits) - 1);
    5318         255 :             if (nBits > 1)
    5319             :             {
    5320         249 :                 panMaskOffsetLsb[i].nRoundUpBitTest = static_cast<uint64_t>(1)
    5321         249 :                                                       << (nBits - 1);
    5322             :             }
    5323         119 :         }
    5324             :     }
    5325             :     else
    5326             :     {
    5327           1 :         CPLError(CE_Warning, CPLE_AppDefined,
    5328             :                  "DISCARD_LSB ignored: wrong number of components");
    5329             :     }
    5330         120 :     return panMaskOffsetLsb;
    5331             : }
    5332             : 
    5333        7825 : void GTiffDataset::GetDiscardLsbOption(CSLConstList papszOptions)
    5334             : {
    5335        7825 :     m_panMaskOffsetLsb = ::GetDiscardLsbOption(m_hTIFF, papszOptions);
    5336        7825 : }
    5337             : 
    5338             : /************************************************************************/
    5339             : /*                             GetProfile()                             */
    5340             : /************************************************************************/
    5341             : 
    5342       17724 : static GTiffProfile GetProfile(const char *pszProfile)
    5343             : {
    5344       17724 :     GTiffProfile eProfile = GTiffProfile::GDALGEOTIFF;
    5345       17724 :     if (pszProfile != nullptr)
    5346             :     {
    5347          70 :         if (EQUAL(pszProfile, szPROFILE_BASELINE))
    5348          50 :             eProfile = GTiffProfile::BASELINE;
    5349          20 :         else if (EQUAL(pszProfile, szPROFILE_GeoTIFF))
    5350          18 :             eProfile = GTiffProfile::GEOTIFF;
    5351           2 :         else if (!EQUAL(pszProfile, szPROFILE_GDALGeoTIFF))
    5352             :         {
    5353           0 :             CPLError(CE_Warning, CPLE_NotSupported,
    5354             :                      "Unsupported value for PROFILE: %s", pszProfile);
    5355             :         }
    5356             :     }
    5357       17724 :     return eProfile;
    5358             : }
    5359             : 
    5360             : /************************************************************************/
    5361             : /*                            GTiffCreate()                             */
    5362             : /*                                                                      */
    5363             : /*      Shared functionality between GTiffDataset::Create() and         */
    5364             : /*      GTiffCreateCopy() for creating TIFF file based on a set of      */
    5365             : /*      options and a configuration.                                    */
    5366             : /************************************************************************/
    5367             : 
    5368        9868 : TIFF *GTiffDataset::CreateLL(const char *pszFilename, int nXSize, int nYSize,
    5369             :                              int l_nBands, GDALDataType eType,
    5370             :                              double dfExtraSpaceForOverviews,
    5371             :                              int nColorTableMultiplier,
    5372             :                              CSLConstList papszParamList, VSILFILE **pfpL,
    5373             :                              CPLString &l_osTmpFilename, bool bCreateCopy,
    5374             :                              bool &bTileInterleavingOut)
    5375             : 
    5376             : {
    5377        9868 :     bTileInterleavingOut = false;
    5378             : 
    5379        9868 :     GTiffOneTimeInit();
    5380             : 
    5381             :     /* -------------------------------------------------------------------- */
    5382             :     /*      Blow on a few errors.                                           */
    5383             :     /* -------------------------------------------------------------------- */
    5384        9868 :     if (nXSize < 1 || nYSize < 1 || l_nBands < 1)
    5385             :     {
    5386           2 :         ReportError(
    5387             :             pszFilename, CE_Failure, CPLE_AppDefined,
    5388             :             "Attempt to create %dx%dx%d TIFF file, but width, height and bands"
    5389             :             "must be positive.",
    5390             :             nXSize, nYSize, l_nBands);
    5391             : 
    5392           2 :         return nullptr;
    5393             :     }
    5394             : 
    5395        9866 :     if (l_nBands > 65535)
    5396             :     {
    5397           1 :         ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    5398             :                     "Attempt to create %dx%dx%d TIFF file, but bands "
    5399             :                     "must be lesser or equal to 65535.",
    5400             :                     nXSize, nYSize, l_nBands);
    5401             : 
    5402           1 :         return nullptr;
    5403             :     }
    5404             : 
    5405             :     /* -------------------------------------------------------------------- */
    5406             :     /*      Setup values based on options.                                  */
    5407             :     /* -------------------------------------------------------------------- */
    5408             :     const GTiffProfile eProfile =
    5409        9865 :         GetProfile(CSLFetchNameValue(papszParamList, "PROFILE"));
    5410             : 
    5411        9865 :     const bool bTiled = CPLFetchBool(papszParamList, "TILED", false);
    5412             : 
    5413        9865 :     int l_nBlockXSize = 0;
    5414        9865 :     if (const char *pszValue = CSLFetchNameValue(papszParamList, "BLOCKXSIZE"))
    5415             :     {
    5416         460 :         l_nBlockXSize = atoi(pszValue);
    5417         460 :         if (l_nBlockXSize < 0)
    5418             :         {
    5419           0 :             ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
    5420             :                         "Invalid value for BLOCKXSIZE");
    5421           0 :             return nullptr;
    5422             :         }
    5423         460 :         if (!bTiled)
    5424             :         {
    5425          10 :             ReportError(pszFilename, CE_Warning, CPLE_IllegalArg,
    5426             :                         "BLOCKXSIZE can only be used with TILED=YES");
    5427             :         }
    5428         450 :         else if (l_nBlockXSize % 16 != 0)
    5429             :         {
    5430           1 :             ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
    5431             :                         "BLOCKXSIZE must be a multiple of 16");
    5432           1 :             return nullptr;
    5433             :         }
    5434             :     }
    5435             : 
    5436        9864 :     int l_nBlockYSize = 0;
    5437        9864 :     if (const char *pszValue = CSLFetchNameValue(papszParamList, "BLOCKYSIZE"))
    5438             :     {
    5439        2571 :         l_nBlockYSize = atoi(pszValue);
    5440        2571 :         if (l_nBlockYSize < 0)
    5441             :         {
    5442           0 :             ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
    5443             :                         "Invalid value for BLOCKYSIZE");
    5444           0 :             return nullptr;
    5445             :         }
    5446        2571 :         if (bTiled && (l_nBlockYSize % 16 != 0))
    5447             :         {
    5448           2 :             ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
    5449             :                         "BLOCKYSIZE must be a multiple of 16");
    5450           2 :             return nullptr;
    5451             :         }
    5452             :     }
    5453             : 
    5454        9862 :     if (bTiled)
    5455             :     {
    5456         799 :         if (l_nBlockXSize == 0)
    5457         351 :             l_nBlockXSize = 256;
    5458             : 
    5459         799 :         if (l_nBlockYSize == 0)
    5460         351 :             l_nBlockYSize = 256;
    5461             :     }
    5462             : 
    5463        9862 :     int nPlanar = 0;
    5464             : 
    5465             :     // Hidden @TILE_INTERLEAVE=YES parameter used by the COG driver
    5466        9862 :     if (bCreateCopy && CPLTestBool(CSLFetchNameValueDef(
    5467             :                            papszParamList, "@TILE_INTERLEAVE", "NO")))
    5468             :     {
    5469           7 :         bTileInterleavingOut = true;
    5470           7 :         nPlanar = PLANARCONFIG_SEPARATE;
    5471             :     }
    5472             :     else
    5473             :     {
    5474        9855 :         if (const char *pszValue =
    5475        9855 :                 CSLFetchNameValue(papszParamList, "INTERLEAVE"))
    5476             :         {
    5477        1573 :             if (EQUAL(pszValue, "PIXEL"))
    5478         404 :                 nPlanar = PLANARCONFIG_CONTIG;
    5479        1169 :             else if (EQUAL(pszValue, "BAND"))
    5480             :             {
    5481        1168 :                 nPlanar = PLANARCONFIG_SEPARATE;
    5482             :             }
    5483           1 :             else if (EQUAL(pszValue, "BAND"))
    5484             :             {
    5485           0 :                 nPlanar = PLANARCONFIG_SEPARATE;
    5486             :             }
    5487             :             else
    5488             :             {
    5489           1 :                 ReportError(
    5490             :                     pszFilename, CE_Failure, CPLE_IllegalArg,
    5491             :                     "INTERLEAVE=%s unsupported, value must be PIXEL or BAND.",
    5492             :                     pszValue);
    5493           1 :                 return nullptr;
    5494             :             }
    5495             :         }
    5496             :         else
    5497             :         {
    5498        8282 :             nPlanar = PLANARCONFIG_CONTIG;
    5499             :         }
    5500             :     }
    5501             : 
    5502        9861 :     int l_nCompression = COMPRESSION_NONE;
    5503        9861 :     if (const char *pszValue = CSLFetchNameValue(papszParamList, "COMPRESS"))
    5504             :     {
    5505        3341 :         l_nCompression = GTIFFGetCompressionMethod(pszValue, "COMPRESS");
    5506        3341 :         if (l_nCompression < 0)
    5507           0 :             return nullptr;
    5508             :     }
    5509             : 
    5510        9861 :     constexpr int JPEG_MAX_DIMENSION = 65500;  // Defined in jpeglib.h
    5511        9861 :     constexpr int WEBP_MAX_DIMENSION = 16383;
    5512             : 
    5513             :     const struct
    5514             :     {
    5515             :         int nCodecID;
    5516             :         const char *pszCodecName;
    5517             :         int nMaxDim;
    5518        9861 :     } asLimitations[] = {
    5519             :         {COMPRESSION_JPEG, "JPEG", JPEG_MAX_DIMENSION},
    5520             :         {COMPRESSION_WEBP, "WEBP", WEBP_MAX_DIMENSION},
    5521             :     };
    5522             : 
    5523       29571 :     for (const auto &sLimitation : asLimitations)
    5524             :     {
    5525       19718 :         if (l_nCompression == sLimitation.nCodecID && !bTiled &&
    5526        2074 :             nXSize > sLimitation.nMaxDim)
    5527             :         {
    5528           2 :             ReportError(
    5529             :                 pszFilename, CE_Failure, CPLE_IllegalArg,
    5530             :                 "COMPRESS=%s is only compatible of un-tiled images whose "
    5531             :                 "width is lesser or equal to %d pixels. "
    5532             :                 "To overcome this limitation, set the TILED=YES creation "
    5533             :                 "option.",
    5534           2 :                 sLimitation.pszCodecName, sLimitation.nMaxDim);
    5535           2 :             return nullptr;
    5536             :         }
    5537       19716 :         else if (l_nCompression == sLimitation.nCodecID && bTiled &&
    5538          52 :                  l_nBlockXSize > sLimitation.nMaxDim)
    5539             :         {
    5540           2 :             ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
    5541             :                         "COMPRESS=%s is only compatible of tiled images whose "
    5542             :                         "BLOCKXSIZE is lesser or equal to %d pixels.",
    5543           2 :                         sLimitation.pszCodecName, sLimitation.nMaxDim);
    5544           2 :             return nullptr;
    5545             :         }
    5546       19714 :         else if (l_nCompression == sLimitation.nCodecID &&
    5547        2122 :                  l_nBlockYSize > sLimitation.nMaxDim)
    5548             :         {
    5549           4 :             ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
    5550             :                         "COMPRESS=%s is only compatible of images whose "
    5551             :                         "BLOCKYSIZE is lesser or equal to %d pixels. "
    5552             :                         "To overcome this limitation, set the TILED=YES "
    5553             :                         "creation option",
    5554           4 :                         sLimitation.pszCodecName, sLimitation.nMaxDim);
    5555           4 :             return nullptr;
    5556             :         }
    5557             :     }
    5558             : 
    5559             :     /* -------------------------------------------------------------------- */
    5560             :     /*      How many bits per sample?  We have a special case if NBITS      */
    5561             :     /*      specified for GDT_UInt8, GDT_UInt16, GDT_UInt32.                 */
    5562             :     /* -------------------------------------------------------------------- */
    5563        9853 :     int l_nBitsPerSample = GDALGetDataTypeSizeBits(eType);
    5564        9853 :     if (CSLFetchNameValue(papszParamList, "NBITS") != nullptr)
    5565             :     {
    5566        1747 :         int nMinBits = 0;
    5567        1747 :         int nMaxBits = 0;
    5568        1747 :         l_nBitsPerSample = atoi(CSLFetchNameValue(papszParamList, "NBITS"));
    5569        1747 :         if (eType == GDT_UInt8)
    5570             :         {
    5571         527 :             nMinBits = 1;
    5572         527 :             nMaxBits = 8;
    5573             :         }
    5574        1220 :         else if (eType == GDT_UInt16)
    5575             :         {
    5576        1202 :             nMinBits = 9;
    5577        1202 :             nMaxBits = 16;
    5578             :         }
    5579          18 :         else if (eType == GDT_UInt32)
    5580             :         {
    5581          14 :             nMinBits = 17;
    5582          14 :             nMaxBits = 32;
    5583             :         }
    5584           4 :         else if (eType == GDT_Float32)
    5585             :         {
    5586           4 :             if (l_nBitsPerSample != 16 && l_nBitsPerSample != 32)
    5587             :             {
    5588           1 :                 ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
    5589             :                             "Only NBITS=16 is supported for data type Float32");
    5590           1 :                 l_nBitsPerSample = GDALGetDataTypeSizeBits(eType);
    5591             :             }
    5592             :         }
    5593             :         else
    5594             :         {
    5595           0 :             ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
    5596             :                         "NBITS is not supported for data type %s",
    5597             :                         GDALGetDataTypeName(eType));
    5598           0 :             l_nBitsPerSample = GDALGetDataTypeSizeBits(eType);
    5599             :         }
    5600             : 
    5601        1747 :         if (nMinBits != 0)
    5602             :         {
    5603        1743 :             if (l_nBitsPerSample < nMinBits)
    5604             :             {
    5605           2 :                 ReportError(
    5606             :                     pszFilename, CE_Warning, CPLE_AppDefined,
    5607             :                     "NBITS=%d is invalid for data type %s. Using NBITS=%d",
    5608             :                     l_nBitsPerSample, GDALGetDataTypeName(eType), nMinBits);
    5609           2 :                 l_nBitsPerSample = nMinBits;
    5610             :             }
    5611        1741 :             else if (l_nBitsPerSample > nMaxBits)
    5612             :             {
    5613           3 :                 ReportError(
    5614             :                     pszFilename, CE_Warning, CPLE_AppDefined,
    5615             :                     "NBITS=%d is invalid for data type %s. Using NBITS=%d",
    5616             :                     l_nBitsPerSample, GDALGetDataTypeName(eType), nMaxBits);
    5617           3 :                 l_nBitsPerSample = nMaxBits;
    5618             :             }
    5619             :         }
    5620             :     }
    5621             : 
    5622             : #ifdef HAVE_JXL
    5623        9853 :     if ((l_nCompression == COMPRESSION_JXL ||
    5624         106 :          l_nCompression == COMPRESSION_JXL_DNG_1_7) &&
    5625         105 :         eType != GDT_Float16 && eType != GDT_Float32)
    5626             :     {
    5627             :         // Reflects tif_jxl's GetJXLDataType()
    5628          85 :         if (eType != GDT_UInt8 && eType != GDT_UInt16)
    5629             :         {
    5630           1 :             ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5631             :                         "Data type %s not supported for JXL compression. Only "
    5632             :                         "Byte, UInt16, Float16, Float32 are supported",
    5633             :                         GDALGetDataTypeName(eType));
    5634           2 :             return nullptr;
    5635             :         }
    5636             : 
    5637             :         const struct
    5638             :         {
    5639             :             GDALDataType eDT;
    5640             :             int nBitsPerSample;
    5641          84 :         } asSupportedDTBitsPerSample[] = {
    5642             :             {GDT_UInt8, 8},
    5643             :             {GDT_UInt16, 16},
    5644             :         };
    5645             : 
    5646         250 :         for (const auto &sSupportedDTBitsPerSample : asSupportedDTBitsPerSample)
    5647             :         {
    5648         167 :             if (eType == sSupportedDTBitsPerSample.eDT &&
    5649          84 :                 l_nBitsPerSample != sSupportedDTBitsPerSample.nBitsPerSample)
    5650             :             {
    5651           1 :                 ReportError(
    5652             :                     pszFilename, CE_Failure, CPLE_NotSupported,
    5653             :                     "Bits per sample=%d not supported for JXL compression. "
    5654             :                     "Only %d is supported for %s data type.",
    5655           1 :                     l_nBitsPerSample, sSupportedDTBitsPerSample.nBitsPerSample,
    5656             :                     GDALGetDataTypeName(eType));
    5657           1 :                 return nullptr;
    5658             :             }
    5659             :         }
    5660             :     }
    5661             : #endif
    5662             : 
    5663        9851 :     int nPredictor = PREDICTOR_NONE;
    5664        9851 :     const char *pszPredictor = CSLFetchNameValue(papszParamList, "PREDICTOR");
    5665        9851 :     if (pszPredictor)
    5666             :     {
    5667          31 :         nPredictor = atoi(pszPredictor);
    5668             :     }
    5669             : 
    5670        9851 :     if (nPredictor != PREDICTOR_NONE &&
    5671          18 :         l_nCompression != COMPRESSION_ADOBE_DEFLATE &&
    5672           2 :         l_nCompression != COMPRESSION_LZW &&
    5673           2 :         l_nCompression != COMPRESSION_LZMA &&
    5674             :         l_nCompression != COMPRESSION_ZSTD)
    5675             :     {
    5676           1 :         ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
    5677             :                     "PREDICTOR option is ignored for COMPRESS=%s. "
    5678             :                     "Only valid for DEFLATE, LZW, LZMA or ZSTD",
    5679             :                     CSLFetchNameValueDef(papszParamList, "COMPRESS", "NONE"));
    5680             :     }
    5681             : 
    5682             :     // Do early checks as libtiff will only error out when starting to write.
    5683        9880 :     else if (nPredictor != PREDICTOR_NONE &&
    5684          30 :              CPLTestBool(
    5685             :                  CPLGetConfigOption("GDAL_GTIFF_PREDICTOR_CHECKS", "YES")))
    5686             :     {
    5687             : #if (TIFFLIB_VERSION > 20210416) || defined(INTERNAL_LIBTIFF)
    5688             : #define HAVE_PREDICTOR_2_FOR_64BIT
    5689             : #endif
    5690          30 :         if (nPredictor == 2)
    5691             :         {
    5692          24 :             if (l_nBitsPerSample != 8 && l_nBitsPerSample != 16 &&
    5693             :                 l_nBitsPerSample != 32
    5694             : #ifdef HAVE_PREDICTOR_2_FOR_64BIT
    5695           2 :                 && l_nBitsPerSample != 64
    5696             : #endif
    5697             :             )
    5698             :             {
    5699             : #if !defined(HAVE_PREDICTOR_2_FOR_64BIT)
    5700             :                 if (l_nBitsPerSample == 64)
    5701             :                 {
    5702             :                     ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    5703             :                                 "PREDICTOR=2 is supported on 64 bit samples "
    5704             :                                 "starting with libtiff > 4.3.0.");
    5705             :                 }
    5706             :                 else
    5707             : #endif
    5708             :                 {
    5709           2 :                     const int nBITSHint = (l_nBitsPerSample < 8)    ? 8
    5710           1 :                                           : (l_nBitsPerSample < 16) ? 16
    5711           0 :                                           : (l_nBitsPerSample < 32) ? 32
    5712             :                                                                     : 64;
    5713           1 :                     ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    5714             : #ifdef HAVE_PREDICTOR_2_FOR_64BIT
    5715             :                                 "PREDICTOR=2 is only supported with 8/16/32/64 "
    5716             :                                 "bit samples. You can specify the NBITS=%d "
    5717             :                                 "creation option to promote to the closest "
    5718             :                                 "supported bits per sample value.",
    5719             : #else
    5720             :                                 "PREDICTOR=2 is only supported with 8/16/32 "
    5721             :                                 "bit samples. You can specify the NBITS=%d "
    5722             :                                 "creation option to promote to the closest "
    5723             :                                 "supported bits per sample value.",
    5724             : #endif
    5725             :                                 nBITSHint);
    5726             :                 }
    5727           1 :                 return nullptr;
    5728             :             }
    5729             :         }
    5730           6 :         else if (nPredictor == 3)
    5731             :         {
    5732           5 :             if (eType != GDT_Float16 && eType != GDT_Float32 &&
    5733             :                 eType != GDT_Float64)
    5734             :             {
    5735           1 :                 ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    5736             :                             "PREDICTOR=3 is only supported with Float16, "
    5737             :                             "Float32 or Float64.");
    5738           1 :                 return nullptr;
    5739             :             }
    5740             :         }
    5741             :         else
    5742             :         {
    5743           1 :             ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    5744             :                         "PREDICTOR=%s is not supported.", pszPredictor);
    5745           1 :             return nullptr;
    5746             :         }
    5747             :     }
    5748             : 
    5749        9848 :     const int l_nZLevel = GTiffGetZLevel(papszParamList);
    5750        9848 :     const int l_nLZMAPreset = GTiffGetLZMAPreset(papszParamList);
    5751        9848 :     const int l_nZSTDLevel = GTiffGetZSTDPreset(papszParamList);
    5752        9848 :     const int l_nWebPLevel = GTiffGetWebPLevel(papszParamList);
    5753        9848 :     const bool l_bWebPLossless = GTiffGetWebPLossless(papszParamList);
    5754        9848 :     const int l_nJpegQuality = GTiffGetJpegQuality(papszParamList);
    5755        9848 :     const int l_nJpegTablesMode = GTiffGetJpegTablesMode(papszParamList);
    5756        9848 :     const double l_dfMaxZError = GTiffGetLERCMaxZError(papszParamList);
    5757             : #if HAVE_JXL
    5758        9848 :     bool bJXLLosslessSpecified = false;
    5759             :     const bool l_bJXLLossless =
    5760        9848 :         GTiffGetJXLLossless(papszParamList, &bJXLLosslessSpecified);
    5761        9848 :     const uint32_t l_nJXLEffort = GTiffGetJXLEffort(papszParamList);
    5762        9848 :     bool bJXLDistanceSpecified = false;
    5763             :     const float l_fJXLDistance =
    5764        9848 :         GTiffGetJXLDistance(papszParamList, &bJXLDistanceSpecified);
    5765        9848 :     if (bJXLDistanceSpecified && l_bJXLLossless)
    5766             :     {
    5767           1 :         ReportError(pszFilename, CE_Warning, CPLE_AppDefined,
    5768             :                     "JXL_DISTANCE creation option is ignored, given %s "
    5769             :                     "JXL_LOSSLESS=YES",
    5770             :                     bJXLLosslessSpecified ? "(explicit)" : "(implicit)");
    5771             :     }
    5772        9848 :     bool bJXLAlphaDistanceSpecified = false;
    5773             :     const float l_fJXLAlphaDistance =
    5774        9848 :         GTiffGetJXLAlphaDistance(papszParamList, &bJXLAlphaDistanceSpecified);
    5775        9848 :     if (bJXLAlphaDistanceSpecified && l_bJXLLossless)
    5776             :     {
    5777           1 :         ReportError(pszFilename, CE_Warning, CPLE_AppDefined,
    5778             :                     "JXL_ALPHA_DISTANCE creation option is ignored, given %s "
    5779             :                     "JXL_LOSSLESS=YES",
    5780             :                     bJXLLosslessSpecified ? "(explicit)" : "(implicit)");
    5781             :     }
    5782             : #endif
    5783             :     /* -------------------------------------------------------------------- */
    5784             :     /*      Streaming related code                                          */
    5785             :     /* -------------------------------------------------------------------- */
    5786       19696 :     const CPLString osOriFilename(pszFilename);
    5787       19696 :     bool bStreaming = strcmp(pszFilename, "/vsistdout/") == 0 ||
    5788        9848 :                       CPLFetchBool(papszParamList, "STREAMABLE_OUTPUT", false);
    5789             : #ifdef S_ISFIFO
    5790        9848 :     if (!bStreaming)
    5791             :     {
    5792             :         VSIStatBufL sStat;
    5793        9836 :         if (VSIStatExL(pszFilename, &sStat,
    5794       10716 :                        VSI_STAT_EXISTS_FLAG | VSI_STAT_NATURE_FLAG) == 0 &&
    5795         880 :             S_ISFIFO(sStat.st_mode))
    5796             :         {
    5797           0 :             bStreaming = true;
    5798             :         }
    5799             :     }
    5800             : #endif
    5801        9848 :     if (bStreaming && !EQUAL("NONE", CSLFetchNameValueDef(papszParamList,
    5802             :                                                           "COMPRESS", "NONE")))
    5803             :     {
    5804           1 :         ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5805             :                     "Streaming only supported to uncompressed TIFF");
    5806           1 :         return nullptr;
    5807             :     }
    5808        9847 :     if (bStreaming && CPLFetchBool(papszParamList, "SPARSE_OK", false))
    5809             :     {
    5810           1 :         ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5811             :                     "Streaming not supported with SPARSE_OK");
    5812           1 :         return nullptr;
    5813             :     }
    5814             :     const bool bCopySrcOverviews =
    5815        9846 :         CPLFetchBool(papszParamList, "COPY_SRC_OVERVIEWS", false);
    5816        9846 :     if (bStreaming && bCopySrcOverviews)
    5817             :     {
    5818           1 :         ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5819             :                     "Streaming not supported with COPY_SRC_OVERVIEWS");
    5820           1 :         return nullptr;
    5821             :     }
    5822        9845 :     if (bStreaming)
    5823             :     {
    5824           9 :         l_osTmpFilename = VSIMemGenerateHiddenFilename("vsistdout.tif");
    5825           9 :         pszFilename = l_osTmpFilename.c_str();
    5826             :     }
    5827             : 
    5828             :     /* -------------------------------------------------------------------- */
    5829             :     /*      Compute the uncompressed size.                                  */
    5830             :     /* -------------------------------------------------------------------- */
    5831        9845 :     const unsigned nTileXCount =
    5832        9845 :         bTiled ? DIV_ROUND_UP(nXSize, l_nBlockXSize) : 0;
    5833        9845 :     const unsigned nTileYCount =
    5834        9845 :         bTiled ? DIV_ROUND_UP(nYSize, l_nBlockYSize) : 0;
    5835             :     const double dfUncompressedImageSize =
    5836        9845 :         (bTiled ? (static_cast<double>(nTileXCount) * nTileYCount *
    5837         795 :                    l_nBlockXSize * l_nBlockYSize)
    5838        9050 :                 : (nXSize * static_cast<double>(nYSize))) *
    5839        9845 :             l_nBands * GDALGetDataTypeSizeBytes(eType) +
    5840        9845 :         dfExtraSpaceForOverviews;
    5841             : 
    5842             :     /* -------------------------------------------------------------------- */
    5843             :     /*      Should the file be created as a bigtiff file?                   */
    5844             :     /* -------------------------------------------------------------------- */
    5845        9845 :     const char *pszBIGTIFF = CSLFetchNameValue(papszParamList, "BIGTIFF");
    5846             : 
    5847        9845 :     if (pszBIGTIFF == nullptr)
    5848        9404 :         pszBIGTIFF = "IF_NEEDED";
    5849             : 
    5850        9845 :     bool bCreateBigTIFF = false;
    5851        9845 :     if (EQUAL(pszBIGTIFF, "IF_NEEDED"))
    5852             :     {
    5853        9405 :         if (l_nCompression == COMPRESSION_NONE &&
    5854             :             dfUncompressedImageSize > 4200000000.0)
    5855          17 :             bCreateBigTIFF = true;
    5856             :     }
    5857         440 :     else if (EQUAL(pszBIGTIFF, "IF_SAFER"))
    5858             :     {
    5859         419 :         if (dfUncompressedImageSize > 2000000000.0)
    5860           1 :             bCreateBigTIFF = true;
    5861             :     }
    5862             :     else
    5863             :     {
    5864          21 :         bCreateBigTIFF = CPLTestBool(pszBIGTIFF);
    5865          21 :         if (!bCreateBigTIFF && l_nCompression == COMPRESSION_NONE &&
    5866             :             dfUncompressedImageSize > 4200000000.0)
    5867             :         {
    5868           2 :             ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5869             :                         "The TIFF file will be larger than 4GB, so BigTIFF is "
    5870             :                         "necessary.  Creation failed.");
    5871           2 :             return nullptr;
    5872             :         }
    5873             :     }
    5874             : 
    5875        9843 :     if (bCreateBigTIFF)
    5876          35 :         CPLDebug("GTiff", "File being created as a BigTIFF.");
    5877             : 
    5878             :     /* -------------------------------------------------------------------- */
    5879             :     /*      Sanity check.                                                   */
    5880             :     /* -------------------------------------------------------------------- */
    5881        9843 :     if (bTiled)
    5882             :     {
    5883             :         // libtiff implementation limitation
    5884         795 :         if (nTileXCount > 0x80000000U / (bCreateBigTIFF ? 8 : 4) / nTileYCount)
    5885             :         {
    5886           3 :             ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5887             :                         "File too large regarding tile size. This would result "
    5888             :                         "in a file with tile arrays larger than 2GB");
    5889           3 :             return nullptr;
    5890             :         }
    5891             :     }
    5892             : 
    5893             :     /* -------------------------------------------------------------------- */
    5894             :     /*      Check free space (only for big, non sparse)                     */
    5895             :     /* -------------------------------------------------------------------- */
    5896        9840 :     const double dfLikelyFloorOfFinalSize =
    5897             :         l_nCompression == COMPRESSION_NONE
    5898        9840 :             ? dfUncompressedImageSize
    5899             :             :
    5900             :             /* For compressed, we target 1% as the most optimistic reduction factor! */
    5901             :             0.01 * dfUncompressedImageSize;
    5902        9862 :     if (dfLikelyFloorOfFinalSize >= 1e9 &&
    5903          22 :         !CPLFetchBool(papszParamList, "SPARSE_OK", false) &&
    5904           5 :         osOriFilename != "/vsistdout/" &&
    5905        9867 :         osOriFilename != "/vsistdout_redirect/" &&
    5906           5 :         CPLTestBool(CPLGetConfigOption("CHECK_DISK_FREE_SPACE", "TRUE")))
    5907             :     {
    5908             :         const GIntBig nFreeDiskSpace =
    5909           4 :             VSIGetDiskFreeSpace(CPLGetDirnameSafe(pszFilename).c_str());
    5910           4 :         if (nFreeDiskSpace >= 0 && nFreeDiskSpace < dfLikelyFloorOfFinalSize)
    5911             :         {
    5912           6 :             ReportError(
    5913             :                 pszFilename, CE_Failure, CPLE_FileIO,
    5914             :                 "Free disk space available is %s, "
    5915             :                 "whereas %s are %s necessary. "
    5916             :                 "You can disable this check by defining the "
    5917             :                 "CHECK_DISK_FREE_SPACE configuration option to FALSE.",
    5918           4 :                 CPLFormatReadableFileSize(static_cast<uint64_t>(nFreeDiskSpace))
    5919             :                     .c_str(),
    5920           4 :                 CPLFormatReadableFileSize(dfLikelyFloorOfFinalSize).c_str(),
    5921             :                 l_nCompression == COMPRESSION_NONE
    5922             :                     ? "at least"
    5923             :                     : "likely at least (probably more)");
    5924           2 :             return nullptr;
    5925             :         }
    5926             :     }
    5927             : 
    5928             :     /* -------------------------------------------------------------------- */
    5929             :     /*      Check if the user wishes a particular endianness                */
    5930             :     /* -------------------------------------------------------------------- */
    5931             : 
    5932        9838 :     int eEndianness = ENDIANNESS_NATIVE;
    5933        9838 :     const char *pszEndianness = CSLFetchNameValue(papszParamList, "ENDIANNESS");
    5934        9838 :     if (pszEndianness == nullptr)
    5935        9775 :         pszEndianness = CPLGetConfigOption("GDAL_TIFF_ENDIANNESS", nullptr);
    5936        9838 :     if (pszEndianness != nullptr)
    5937             :     {
    5938         123 :         if (EQUAL(pszEndianness, "LITTLE"))
    5939             :         {
    5940          36 :             eEndianness = ENDIANNESS_LITTLE;
    5941             :         }
    5942          87 :         else if (EQUAL(pszEndianness, "BIG"))
    5943             :         {
    5944           1 :             eEndianness = ENDIANNESS_BIG;
    5945             :         }
    5946          86 :         else if (EQUAL(pszEndianness, "INVERTED"))
    5947             :         {
    5948             : #ifdef CPL_LSB
    5949          82 :             eEndianness = ENDIANNESS_BIG;
    5950             : #else
    5951             :             eEndianness = ENDIANNESS_LITTLE;
    5952             : #endif
    5953             :         }
    5954           4 :         else if (!EQUAL(pszEndianness, "NATIVE"))
    5955             :         {
    5956           1 :             ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
    5957             :                         "ENDIANNESS=%s not supported. Defaulting to NATIVE",
    5958             :                         pszEndianness);
    5959             :         }
    5960             :     }
    5961             : 
    5962             :     /* -------------------------------------------------------------------- */
    5963             :     /*      Try opening the dataset.                                        */
    5964             :     /* -------------------------------------------------------------------- */
    5965             : 
    5966             :     const bool bAppend =
    5967        9838 :         CPLFetchBool(papszParamList, "APPEND_SUBDATASET", false);
    5968             : 
    5969        9838 :     char szOpeningFlag[5] = {};
    5970        9838 :     strcpy(szOpeningFlag, bAppend ? "r+" : "w+");
    5971        9838 :     if (bCreateBigTIFF)
    5972          32 :         strcat(szOpeningFlag, "8");
    5973        9838 :     if (eEndianness == ENDIANNESS_BIG)
    5974          83 :         strcat(szOpeningFlag, "b");
    5975        9755 :     else if (eEndianness == ENDIANNESS_LITTLE)
    5976          36 :         strcat(szOpeningFlag, "l");
    5977             : 
    5978        9838 :     VSIErrorReset();
    5979        9838 :     const bool bOnlyVisibleAtCloseTime = CPLTestBool(CSLFetchNameValueDef(
    5980             :         papszParamList, "@CREATE_ONLY_VISIBLE_AT_CLOSE_TIME", "NO"));
    5981        9838 :     const bool bSuppressASAP = CPLTestBool(
    5982             :         CSLFetchNameValueDef(papszParamList, "@SUPPRESS_ASAP", "NO"));
    5983             :     auto l_fpL =
    5984        9838 :         (bOnlyVisibleAtCloseTime || bSuppressASAP) && !bAppend
    5985        9906 :             ? VSIFileManager::GetHandler(pszFilename)
    5986         136 :                   ->CreateOnlyVisibleAtCloseTime(pszFilename, true, nullptr)
    5987          68 :                   .release()
    5988       19608 :             : VSIFilesystemHandler::OpenStatic(pszFilename,
    5989             :                                                bAppend ? "r+b" : "w+b", true)
    5990        9838 :                   .release();
    5991        9838 :     if (l_fpL == nullptr)
    5992             :     {
    5993          21 :         VSIToCPLErrorWithMsg(CE_Failure, CPLE_OpenFailed,
    5994          42 :                              std::string("Attempt to create new tiff file `")
    5995          21 :                                  .append(pszFilename)
    5996          21 :                                  .append("' failed")
    5997             :                                  .c_str());
    5998          21 :         return nullptr;
    5999             :     }
    6000             : 
    6001        9817 :     if (bSuppressASAP)
    6002             :     {
    6003          38 :         l_fpL->CancelCreation();
    6004             :     }
    6005             : 
    6006        9817 :     TIFF *l_hTIFF = VSI_TIFFOpen(pszFilename, szOpeningFlag, l_fpL);
    6007        9817 :     if (l_hTIFF == nullptr)
    6008             :     {
    6009           2 :         if (CPLGetLastErrorNo() == 0)
    6010           0 :             CPLError(CE_Failure, CPLE_OpenFailed,
    6011             :                      "Attempt to create new tiff file `%s' "
    6012             :                      "failed in XTIFFOpen().",
    6013             :                      pszFilename);
    6014           2 :         l_fpL->CancelCreation();
    6015           2 :         CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    6016           2 :         return nullptr;
    6017             :     }
    6018             : 
    6019        9815 :     if (bAppend)
    6020             :     {
    6021             : #if !(defined(INTERNAL_LIBTIFF) || TIFFLIB_VERSION > 20240911)
    6022             :         // This is a bit of a hack to cause (*tif->tif_cleanup)(tif); to be
    6023             :         // called. See https://trac.osgeo.org/gdal/ticket/2055
    6024             :         // Fixed in libtiff > 4.7.0
    6025             :         TIFFSetField(l_hTIFF, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
    6026             :         TIFFFreeDirectory(l_hTIFF);
    6027             : #endif
    6028           6 :         TIFFCreateDirectory(l_hTIFF);
    6029             :     }
    6030             : 
    6031             :     /* -------------------------------------------------------------------- */
    6032             :     /*      Do we have a custom pixel type (just used for signed byte now). */
    6033             :     /* -------------------------------------------------------------------- */
    6034        9815 :     const char *pszPixelType = CSLFetchNameValue(papszParamList, "PIXELTYPE");
    6035        9815 :     if (pszPixelType == nullptr)
    6036        9807 :         pszPixelType = "";
    6037        9815 :     if (eType == GDT_UInt8 && EQUAL(pszPixelType, "SIGNEDBYTE"))
    6038             :     {
    6039           8 :         CPLError(CE_Warning, CPLE_AppDefined,
    6040             :                  "Using PIXELTYPE=SIGNEDBYTE with Byte data type is deprecated "
    6041             :                  "(but still works). "
    6042             :                  "Using Int8 data type instead is now recommended.");
    6043             :     }
    6044             : 
    6045             :     /* -------------------------------------------------------------------- */
    6046             :     /*      Setup some standard flags.                                      */
    6047             :     /* -------------------------------------------------------------------- */
    6048        9815 :     TIFFSetField(l_hTIFF, TIFFTAG_IMAGEWIDTH, nXSize);
    6049        9815 :     TIFFSetField(l_hTIFF, TIFFTAG_IMAGELENGTH, nYSize);
    6050        9815 :     TIFFSetField(l_hTIFF, TIFFTAG_BITSPERSAMPLE, l_nBitsPerSample);
    6051             : 
    6052        9815 :     uint16_t l_nSampleFormat = 0;
    6053        9815 :     if ((eType == GDT_UInt8 && EQUAL(pszPixelType, "SIGNEDBYTE")) ||
    6054        9666 :         eType == GDT_Int8 || eType == GDT_Int16 || eType == GDT_Int32 ||
    6055             :         eType == GDT_Int64)
    6056         808 :         l_nSampleFormat = SAMPLEFORMAT_INT;
    6057        9007 :     else if (eType == GDT_CInt16 || eType == GDT_CInt32)
    6058         363 :         l_nSampleFormat = SAMPLEFORMAT_COMPLEXINT;
    6059        8644 :     else if (eType == GDT_Float16 || eType == GDT_Float32 ||
    6060             :              eType == GDT_Float64)
    6061        1153 :         l_nSampleFormat = SAMPLEFORMAT_IEEEFP;
    6062        7491 :     else if (eType == GDT_CFloat16 || eType == GDT_CFloat32 ||
    6063             :              eType == GDT_CFloat64)
    6064         471 :         l_nSampleFormat = SAMPLEFORMAT_COMPLEXIEEEFP;
    6065             :     else
    6066        7020 :         l_nSampleFormat = SAMPLEFORMAT_UINT;
    6067             : 
    6068        9815 :     TIFFSetField(l_hTIFF, TIFFTAG_SAMPLEFORMAT, l_nSampleFormat);
    6069        9815 :     TIFFSetField(l_hTIFF, TIFFTAG_SAMPLESPERPIXEL, l_nBands);
    6070        9815 :     TIFFSetField(l_hTIFF, TIFFTAG_PLANARCONFIG, nPlanar);
    6071             : 
    6072             :     /* -------------------------------------------------------------------- */
    6073             :     /*      Setup Photometric Interpretation. Take this value from the user */
    6074             :     /*      passed option or guess correct value otherwise.                 */
    6075             :     /* -------------------------------------------------------------------- */
    6076        9815 :     int nSamplesAccountedFor = 1;
    6077        9815 :     bool bForceColorTable = false;
    6078             : 
    6079        9815 :     if (const char *pszValue = CSLFetchNameValue(papszParamList, "PHOTOMETRIC"))
    6080             :     {
    6081        1906 :         if (EQUAL(pszValue, "MINISBLACK"))
    6082          14 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    6083        1892 :         else if (EQUAL(pszValue, "MINISWHITE"))
    6084             :         {
    6085           2 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
    6086             :         }
    6087        1890 :         else if (EQUAL(pszValue, "PALETTE"))
    6088             :         {
    6089           5 :             if (eType == GDT_UInt8 || eType == GDT_UInt16)
    6090             :             {
    6091           4 :                 TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
    6092           4 :                 nSamplesAccountedFor = 1;
    6093           4 :                 bForceColorTable = true;
    6094             :             }
    6095             :             else
    6096             :             {
    6097           1 :                 ReportError(
    6098             :                     pszFilename, CE_Warning, CPLE_AppDefined,
    6099             :                     "PHOTOMETRIC=PALETTE only compatible with Byte or UInt16");
    6100             :             }
    6101             :         }
    6102        1885 :         else if (EQUAL(pszValue, "RGB"))
    6103             :         {
    6104        1145 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
    6105        1145 :             nSamplesAccountedFor = 3;
    6106             :         }
    6107         740 :         else if (EQUAL(pszValue, "CMYK"))
    6108             :         {
    6109          10 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_SEPARATED);
    6110          10 :             nSamplesAccountedFor = 4;
    6111             :         }
    6112         730 :         else if (EQUAL(pszValue, "YCBCR"))
    6113             :         {
    6114             :             // Because of subsampling, setting YCBCR without JPEG compression
    6115             :             // leads to a crash currently. Would need to make
    6116             :             // GTiffRasterBand::IWriteBlock() aware of subsampling so that it
    6117             :             // doesn't overrun buffer size returned by libtiff.
    6118         729 :             if (l_nCompression != COMPRESSION_JPEG)
    6119             :             {
    6120           1 :                 ReportError(
    6121             :                     pszFilename, CE_Failure, CPLE_NotSupported,
    6122             :                     "Currently, PHOTOMETRIC=YCBCR requires COMPRESS=JPEG");
    6123           1 :                 XTIFFClose(l_hTIFF);
    6124           1 :                 l_fpL->CancelCreation();
    6125           1 :                 CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    6126           1 :                 return nullptr;
    6127             :             }
    6128             : 
    6129         728 :             if (nPlanar == PLANARCONFIG_SEPARATE)
    6130             :             {
    6131           1 :                 ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    6132             :                             "PHOTOMETRIC=YCBCR requires INTERLEAVE=PIXEL");
    6133           1 :                 XTIFFClose(l_hTIFF);
    6134           1 :                 l_fpL->CancelCreation();
    6135           1 :                 CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    6136           1 :                 return nullptr;
    6137             :             }
    6138             : 
    6139             :             // YCBCR strictly requires 3 bands. Not less, not more Issue an
    6140             :             // explicit error message as libtiff one is a bit cryptic:
    6141             :             // TIFFVStripSize64:Invalid td_samplesperpixel value.
    6142         727 :             if (l_nBands != 3)
    6143             :             {
    6144           1 :                 ReportError(
    6145             :                     pszFilename, CE_Failure, CPLE_NotSupported,
    6146             :                     "PHOTOMETRIC=YCBCR not supported on a %d-band raster: "
    6147             :                     "only compatible of a 3-band (RGB) raster",
    6148             :                     l_nBands);
    6149           1 :                 XTIFFClose(l_hTIFF);
    6150           1 :                 l_fpL->CancelCreation();
    6151           1 :                 CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    6152           1 :                 return nullptr;
    6153             :             }
    6154             : 
    6155         726 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);
    6156         726 :             nSamplesAccountedFor = 3;
    6157             : 
    6158             :             // Explicitly register the subsampling so that JPEGFixupTags
    6159             :             // is a no-op (helps for cloud optimized geotiffs)
    6160         726 :             TIFFSetField(l_hTIFF, TIFFTAG_YCBCRSUBSAMPLING, 2, 2);
    6161             :         }
    6162           1 :         else if (EQUAL(pszValue, "CIELAB"))
    6163             :         {
    6164           0 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_CIELAB);
    6165           0 :             nSamplesAccountedFor = 3;
    6166             :         }
    6167           1 :         else if (EQUAL(pszValue, "ICCLAB"))
    6168             :         {
    6169           0 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_ICCLAB);
    6170           0 :             nSamplesAccountedFor = 3;
    6171             :         }
    6172           1 :         else if (EQUAL(pszValue, "ITULAB"))
    6173             :         {
    6174           0 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_ITULAB);
    6175           0 :             nSamplesAccountedFor = 3;
    6176             :         }
    6177             :         else
    6178             :         {
    6179           1 :             ReportError(pszFilename, CE_Warning, CPLE_IllegalArg,
    6180             :                         "PHOTOMETRIC=%s value not recognised, ignoring.  "
    6181             :                         "Set the Photometric Interpretation as MINISBLACK.",
    6182             :                         pszValue);
    6183           1 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    6184             :         }
    6185             : 
    6186        1903 :         if (l_nBands < nSamplesAccountedFor)
    6187             :         {
    6188           1 :             ReportError(pszFilename, CE_Warning, CPLE_IllegalArg,
    6189             :                         "PHOTOMETRIC=%s value does not correspond to number "
    6190             :                         "of bands (%d), ignoring.  "
    6191             :                         "Set the Photometric Interpretation as MINISBLACK.",
    6192             :                         pszValue, l_nBands);
    6193           1 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    6194             :         }
    6195             :     }
    6196             :     else
    6197             :     {
    6198             :         // If image contains 3 or 4 bands and datatype is Byte then we will
    6199             :         // assume it is RGB. In all other cases assume it is MINISBLACK.
    6200        7909 :         if (l_nBands == 3 && eType == GDT_UInt8)
    6201             :         {
    6202         318 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
    6203         318 :             nSamplesAccountedFor = 3;
    6204             :         }
    6205        7591 :         else if (l_nBands == 4 && eType == GDT_UInt8)
    6206             :         {
    6207             :             uint16_t v[1] = {
    6208         718 :                 GTiffGetAlphaValue(CSLFetchNameValue(papszParamList, "ALPHA"),
    6209         718 :                                    DEFAULT_ALPHA_TYPE)};
    6210             : 
    6211         718 :             TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, 1, v);
    6212         718 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
    6213         718 :             nSamplesAccountedFor = 4;
    6214             :         }
    6215             :         else
    6216             :         {
    6217        6873 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    6218        6873 :             nSamplesAccountedFor = 1;
    6219             :         }
    6220             :     }
    6221             : 
    6222             :     /* -------------------------------------------------------------------- */
    6223             :     /*      If there are extra samples, we need to mark them with an        */
    6224             :     /*      appropriate extrasamples definition here.                       */
    6225             :     /* -------------------------------------------------------------------- */
    6226        9812 :     if (l_nBands > nSamplesAccountedFor)
    6227             :     {
    6228        1382 :         const int nExtraSamples = l_nBands - nSamplesAccountedFor;
    6229             : 
    6230             :         uint16_t *v = static_cast<uint16_t *>(
    6231        1382 :             CPLMalloc(sizeof(uint16_t) * nExtraSamples));
    6232             : 
    6233        1382 :         v[0] = GTiffGetAlphaValue(CSLFetchNameValue(papszParamList, "ALPHA"),
    6234             :                                   EXTRASAMPLE_UNSPECIFIED);
    6235             : 
    6236      297693 :         for (int i = 1; i < nExtraSamples; ++i)
    6237      296311 :             v[i] = EXTRASAMPLE_UNSPECIFIED;
    6238             : 
    6239        1382 :         TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, nExtraSamples, v);
    6240             : 
    6241        1382 :         CPLFree(v);
    6242             :     }
    6243             : 
    6244             :     // Set the ICC color profile.
    6245        9812 :     if (eProfile != GTiffProfile::BASELINE)
    6246             :     {
    6247        9787 :         SaveICCProfile(nullptr, l_hTIFF, papszParamList, l_nBitsPerSample);
    6248             :     }
    6249             : 
    6250             :     // Set the compression method before asking the default strip size
    6251             :     // This is useful when translating to a JPEG-In-TIFF file where
    6252             :     // the default strip size is 8 or 16 depending on the photometric value.
    6253        9812 :     TIFFSetField(l_hTIFF, TIFFTAG_COMPRESSION, l_nCompression);
    6254             : 
    6255        9812 :     if (l_nCompression == COMPRESSION_LERC)
    6256             :     {
    6257             :         const char *pszCompress =
    6258          97 :             CSLFetchNameValueDef(papszParamList, "COMPRESS", "");
    6259          97 :         if (EQUAL(pszCompress, "LERC_DEFLATE"))
    6260             :         {
    6261          16 :             TIFFSetField(l_hTIFF, TIFFTAG_LERC_ADD_COMPRESSION,
    6262             :                          LERC_ADD_COMPRESSION_DEFLATE);
    6263             :         }
    6264          81 :         else if (EQUAL(pszCompress, "LERC_ZSTD"))
    6265             :         {
    6266          14 :             if (TIFFSetField(l_hTIFF, TIFFTAG_LERC_ADD_COMPRESSION,
    6267          14 :                              LERC_ADD_COMPRESSION_ZSTD) != 1)
    6268             :             {
    6269           0 :                 XTIFFClose(l_hTIFF);
    6270           0 :                 l_fpL->CancelCreation();
    6271           0 :                 CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    6272           0 :                 return nullptr;
    6273             :             }
    6274             :         }
    6275             :     }
    6276             :     // TODO later: take into account LERC version
    6277             : 
    6278             :     /* -------------------------------------------------------------------- */
    6279             :     /*      Setup tiling/stripping flags.                                   */
    6280             :     /* -------------------------------------------------------------------- */
    6281        9812 :     if (bTiled)
    6282             :     {
    6283        1570 :         if (!TIFFSetField(l_hTIFF, TIFFTAG_TILEWIDTH, l_nBlockXSize) ||
    6284         785 :             !TIFFSetField(l_hTIFF, TIFFTAG_TILELENGTH, l_nBlockYSize))
    6285             :         {
    6286           0 :             XTIFFClose(l_hTIFF);
    6287           0 :             l_fpL->CancelCreation();
    6288           0 :             CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    6289           0 :             return nullptr;
    6290             :         }
    6291             :     }
    6292             :     else
    6293             :     {
    6294        9027 :         const uint32_t l_nRowsPerStrip = std::min(
    6295             :             nYSize, l_nBlockYSize == 0
    6296        9027 :                         ? static_cast<int>(TIFFDefaultStripSize(l_hTIFF, 0))
    6297        9027 :                         : l_nBlockYSize);
    6298             : 
    6299        9027 :         TIFFSetField(l_hTIFF, TIFFTAG_ROWSPERSTRIP, l_nRowsPerStrip);
    6300             :     }
    6301             : 
    6302             :     /* -------------------------------------------------------------------- */
    6303             :     /*      Set compression related tags.                                   */
    6304             :     /* -------------------------------------------------------------------- */
    6305        9812 :     if (GTIFFSupportsPredictor(l_nCompression))
    6306         960 :         TIFFSetField(l_hTIFF, TIFFTAG_PREDICTOR, nPredictor);
    6307        9812 :     if (l_nCompression == COMPRESSION_ADOBE_DEFLATE ||
    6308             :         l_nCompression == COMPRESSION_LERC)
    6309             :     {
    6310         280 :         GTiffSetDeflateSubCodec(l_hTIFF);
    6311             : 
    6312         280 :         if (l_nZLevel != -1)
    6313          22 :             TIFFSetField(l_hTIFF, TIFFTAG_ZIPQUALITY, l_nZLevel);
    6314             :     }
    6315        9812 :     if (l_nCompression == COMPRESSION_JPEG && l_nJpegQuality != -1)
    6316        1905 :         TIFFSetField(l_hTIFF, TIFFTAG_JPEGQUALITY, l_nJpegQuality);
    6317        9812 :     if (l_nCompression == COMPRESSION_LZMA && l_nLZMAPreset != -1)
    6318          10 :         TIFFSetField(l_hTIFF, TIFFTAG_LZMAPRESET, l_nLZMAPreset);
    6319        9812 :     if ((l_nCompression == COMPRESSION_ZSTD ||
    6320         190 :          l_nCompression == COMPRESSION_LERC) &&
    6321             :         l_nZSTDLevel != -1)
    6322          12 :         TIFFSetField(l_hTIFF, TIFFTAG_ZSTD_LEVEL, l_nZSTDLevel);
    6323        9812 :     if (l_nCompression == COMPRESSION_LERC)
    6324             :     {
    6325          97 :         TIFFSetField(l_hTIFF, TIFFTAG_LERC_MAXZERROR, l_dfMaxZError);
    6326             :     }
    6327             : #if HAVE_JXL
    6328        9812 :     if (l_nCompression == COMPRESSION_JXL ||
    6329             :         l_nCompression == COMPRESSION_JXL_DNG_1_7)
    6330             :     {
    6331         104 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_LOSSYNESS,
    6332             :                      l_bJXLLossless ? JXL_LOSSLESS : JXL_LOSSY);
    6333         104 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_EFFORT, l_nJXLEffort);
    6334         104 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_DISTANCE,
    6335             :                      static_cast<double>(l_fJXLDistance));
    6336         104 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_ALPHA_DISTANCE,
    6337             :                      static_cast<double>(l_fJXLAlphaDistance));
    6338             :     }
    6339             : #endif
    6340        9812 :     if (l_nCompression == COMPRESSION_WEBP)
    6341          33 :         TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LEVEL, l_nWebPLevel);
    6342        9812 :     if (l_nCompression == COMPRESSION_WEBP && l_bWebPLossless)
    6343           7 :         TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LOSSLESS, 1);
    6344             : 
    6345        9812 :     if (l_nCompression == COMPRESSION_JPEG)
    6346        2083 :         TIFFSetField(l_hTIFF, TIFFTAG_JPEGTABLESMODE, l_nJpegTablesMode);
    6347             : 
    6348             :     /* -------------------------------------------------------------------- */
    6349             :     /*      If we forced production of a file with photometric=palette,     */
    6350             :     /*      we need to push out a default color table.                      */
    6351             :     /* -------------------------------------------------------------------- */
    6352        9812 :     if (bForceColorTable)
    6353             :     {
    6354           4 :         const int nColors = eType == GDT_UInt8 ? 256 : 65536;
    6355             : 
    6356             :         unsigned short *panTRed = static_cast<unsigned short *>(
    6357           4 :             CPLMalloc(sizeof(unsigned short) * nColors));
    6358             :         unsigned short *panTGreen = static_cast<unsigned short *>(
    6359           4 :             CPLMalloc(sizeof(unsigned short) * nColors));
    6360             :         unsigned short *panTBlue = static_cast<unsigned short *>(
    6361           4 :             CPLMalloc(sizeof(unsigned short) * nColors));
    6362             : 
    6363        1028 :         for (int iColor = 0; iColor < nColors; ++iColor)
    6364             :         {
    6365        1024 :             if (eType == GDT_UInt8)
    6366             :             {
    6367        1024 :                 panTRed[iColor] = GTiffDataset::ClampCTEntry(
    6368             :                     iColor, 1, iColor, nColorTableMultiplier);
    6369        1024 :                 panTGreen[iColor] = GTiffDataset::ClampCTEntry(
    6370             :                     iColor, 2, iColor, nColorTableMultiplier);
    6371        1024 :                 panTBlue[iColor] = GTiffDataset::ClampCTEntry(
    6372             :                     iColor, 3, iColor, nColorTableMultiplier);
    6373             :             }
    6374             :             else
    6375             :             {
    6376           0 :                 panTRed[iColor] = static_cast<unsigned short>(iColor);
    6377           0 :                 panTGreen[iColor] = static_cast<unsigned short>(iColor);
    6378           0 :                 panTBlue[iColor] = static_cast<unsigned short>(iColor);
    6379             :             }
    6380             :         }
    6381             : 
    6382           4 :         TIFFSetField(l_hTIFF, TIFFTAG_COLORMAP, panTRed, panTGreen, panTBlue);
    6383             : 
    6384           4 :         CPLFree(panTRed);
    6385           4 :         CPLFree(panTGreen);
    6386           4 :         CPLFree(panTBlue);
    6387             :     }
    6388             : 
    6389             :     // This trick
    6390             :     // creates a temporary in-memory file and fetches its JPEG tables so that
    6391             :     // we can directly set them, before tif_jpeg.c compute them at the first
    6392             :     // strip/tile writing, which is too late, since we have already crystalized
    6393             :     // the directory. This way we avoid a directory rewriting.
    6394       11895 :     if (l_nCompression == COMPRESSION_JPEG &&
    6395        2083 :         CPLTestBool(
    6396             :             CSLFetchNameValueDef(papszParamList, "WRITE_JPEGTABLE_TAG", "YES")))
    6397             :     {
    6398        1014 :         GTiffWriteJPEGTables(
    6399             :             l_hTIFF, CSLFetchNameValue(papszParamList, "PHOTOMETRIC"),
    6400             :             CSLFetchNameValue(papszParamList, "JPEG_QUALITY"),
    6401             :             CSLFetchNameValue(papszParamList, "JPEGTABLESMODE"));
    6402             :     }
    6403             : 
    6404        9812 :     *pfpL = l_fpL;
    6405             : 
    6406        9812 :     return l_hTIFF;
    6407             : }
    6408             : 
    6409             : /************************************************************************/
    6410             : /*                            GuessJPEGQuality()                        */
    6411             : /*                                                                      */
    6412             : /*      Guess JPEG quality from JPEGTABLES tag.                         */
    6413             : /************************************************************************/
    6414             : 
    6415        3850 : static const GByte *GTIFFFindNextTable(const GByte *paby, GByte byMarker,
    6416             :                                        int nLen, int *pnLenTable)
    6417             : {
    6418        7967 :     for (int i = 0; i + 1 < nLen;)
    6419             :     {
    6420        7967 :         if (paby[i] != 0xFF)
    6421           0 :             return nullptr;
    6422        7967 :         ++i;
    6423        7967 :         if (paby[i] == 0xD8)
    6424             :         {
    6425        3117 :             ++i;
    6426        3117 :             continue;
    6427             :         }
    6428        4850 :         if (i + 2 >= nLen)
    6429         833 :             return nullptr;
    6430        4017 :         int nMarkerLen = paby[i + 1] * 256 + paby[i + 2];
    6431        4017 :         if (i + 1 + nMarkerLen >= nLen)
    6432           0 :             return nullptr;
    6433        4017 :         if (paby[i] == byMarker)
    6434             :         {
    6435        3017 :             if (pnLenTable)
    6436        2473 :                 *pnLenTable = nMarkerLen;
    6437        3017 :             return paby + i + 1;
    6438             :         }
    6439        1000 :         i += 1 + nMarkerLen;
    6440             :     }
    6441           0 :     return nullptr;
    6442             : }
    6443             : 
    6444             : constexpr GByte MARKER_HUFFMAN_TABLE = 0xC4;
    6445             : constexpr GByte MARKER_QUANT_TABLE = 0xDB;
    6446             : 
    6447             : // We assume that if there are several quantization tables, they are
    6448             : // in the same order. Which is a reasonable assumption for updating
    6449             : // a file generated by ourselves.
    6450         904 : static bool GTIFFQuantizationTablesEqual(const GByte *paby1, int nLen1,
    6451             :                                          const GByte *paby2, int nLen2)
    6452             : {
    6453         904 :     bool bFound = false;
    6454             :     while (true)
    6455             :     {
    6456         945 :         int nLenTable1 = 0;
    6457         945 :         int nLenTable2 = 0;
    6458             :         const GByte *paby1New =
    6459         945 :             GTIFFFindNextTable(paby1, MARKER_QUANT_TABLE, nLen1, &nLenTable1);
    6460             :         const GByte *paby2New =
    6461         945 :             GTIFFFindNextTable(paby2, MARKER_QUANT_TABLE, nLen2, &nLenTable2);
    6462         945 :         if (paby1New == nullptr && paby2New == nullptr)
    6463         904 :             return bFound;
    6464         911 :         if (paby1New == nullptr || paby2New == nullptr)
    6465           0 :             return false;
    6466         911 :         if (nLenTable1 != nLenTable2)
    6467         207 :             return false;
    6468         704 :         if (memcmp(paby1New, paby2New, nLenTable1) != 0)
    6469         663 :             return false;
    6470          41 :         paby1New += nLenTable1;
    6471          41 :         paby2New += nLenTable2;
    6472          41 :         nLen1 -= static_cast<int>(paby1New - paby1);
    6473          41 :         nLen2 -= static_cast<int>(paby2New - paby2);
    6474          41 :         paby1 = paby1New;
    6475          41 :         paby2 = paby2New;
    6476          41 :         bFound = true;
    6477          41 :     }
    6478             : }
    6479             : 
    6480             : // Guess the JPEG quality by comparing against the MD5Sum of precomputed
    6481             : // quantization tables
    6482         409 : static int GuessJPEGQualityFromMD5(const uint8_t md5JPEGQuantTable[][16],
    6483             :                                    const GByte *const pabyJPEGTable,
    6484             :                                    int nJPEGTableSize)
    6485             : {
    6486         409 :     int nRemainingLen = nJPEGTableSize;
    6487         409 :     const GByte *pabyCur = pabyJPEGTable;
    6488             : 
    6489             :     struct CPLMD5Context context;
    6490         409 :     CPLMD5Init(&context);
    6491             : 
    6492             :     while (true)
    6493             :     {
    6494        1060 :         int nLenTable = 0;
    6495        1060 :         const GByte *pabyNew = GTIFFFindNextTable(pabyCur, MARKER_QUANT_TABLE,
    6496             :                                                   nRemainingLen, &nLenTable);
    6497        1060 :         if (pabyNew == nullptr)
    6498         409 :             break;
    6499         651 :         CPLMD5Update(&context, pabyNew, nLenTable);
    6500         651 :         pabyNew += nLenTable;
    6501         651 :         nRemainingLen -= static_cast<int>(pabyNew - pabyCur);
    6502         651 :         pabyCur = pabyNew;
    6503         651 :     }
    6504             : 
    6505             :     GByte digest[16];
    6506         409 :     CPLMD5Final(digest, &context);
    6507             : 
    6508       28846 :     for (int i = 0; i < 100; i++)
    6509             :     {
    6510       28843 :         if (memcmp(md5JPEGQuantTable[i], digest, 16) == 0)
    6511             :         {
    6512         406 :             return i + 1;
    6513             :         }
    6514             :     }
    6515           3 :     return -1;
    6516             : }
    6517             : 
    6518         464 : int GTiffDataset::GuessJPEGQuality(bool &bOutHasQuantizationTable,
    6519             :                                    bool &bOutHasHuffmanTable)
    6520             : {
    6521         464 :     CPLAssert(m_nCompression == COMPRESSION_JPEG);
    6522         464 :     uint32_t nJPEGTableSize = 0;
    6523         464 :     void *pJPEGTable = nullptr;
    6524         464 :     if (!TIFFGetField(m_hTIFF, TIFFTAG_JPEGTABLES, &nJPEGTableSize,
    6525             :                       &pJPEGTable))
    6526             :     {
    6527          14 :         bOutHasQuantizationTable = false;
    6528          14 :         bOutHasHuffmanTable = false;
    6529          14 :         return -1;
    6530             :     }
    6531             : 
    6532         450 :     bOutHasQuantizationTable =
    6533         450 :         GTIFFFindNextTable(static_cast<const GByte *>(pJPEGTable),
    6534             :                            MARKER_QUANT_TABLE, nJPEGTableSize,
    6535         450 :                            nullptr) != nullptr;
    6536         450 :     bOutHasHuffmanTable =
    6537         450 :         GTIFFFindNextTable(static_cast<const GByte *>(pJPEGTable),
    6538             :                            MARKER_HUFFMAN_TABLE, nJPEGTableSize,
    6539         450 :                            nullptr) != nullptr;
    6540         450 :     if (!bOutHasQuantizationTable)
    6541           7 :         return -1;
    6542             : 
    6543         443 :     if ((nBands == 1 && m_nBitsPerSample == 8) ||
    6544         382 :         (nBands == 3 && m_nBitsPerSample == 8 &&
    6545         336 :          m_nPhotometric == PHOTOMETRIC_RGB) ||
    6546         288 :         (nBands == 4 && m_nBitsPerSample == 8 &&
    6547          27 :          m_nPhotometric == PHOTOMETRIC_SEPARATED))
    6548             :     {
    6549         167 :         return GuessJPEGQualityFromMD5(md5JPEGQuantTable_generic_8bit,
    6550             :                                        static_cast<const GByte *>(pJPEGTable),
    6551         167 :                                        static_cast<int>(nJPEGTableSize));
    6552             :     }
    6553             : 
    6554         276 :     if (nBands == 3 && m_nBitsPerSample == 8 &&
    6555         242 :         m_nPhotometric == PHOTOMETRIC_YCBCR)
    6556             :     {
    6557             :         int nRet =
    6558         242 :             GuessJPEGQualityFromMD5(md5JPEGQuantTable_3_YCBCR_8bit,
    6559             :                                     static_cast<const GByte *>(pJPEGTable),
    6560             :                                     static_cast<int>(nJPEGTableSize));
    6561         242 :         if (nRet < 0)
    6562             :         {
    6563             :             // libjpeg 9e has modified the YCbCr quantization tables.
    6564             :             nRet =
    6565           0 :                 GuessJPEGQualityFromMD5(md5JPEGQuantTable_3_YCBCR_8bit_jpeg9e,
    6566             :                                         static_cast<const GByte *>(pJPEGTable),
    6567             :                                         static_cast<int>(nJPEGTableSize));
    6568             :         }
    6569         242 :         return nRet;
    6570             :     }
    6571             : 
    6572          34 :     char **papszLocalParameters = nullptr;
    6573             :     papszLocalParameters =
    6574          34 :         CSLSetNameValue(papszLocalParameters, "COMPRESS", "JPEG");
    6575          34 :     if (m_nPhotometric == PHOTOMETRIC_YCBCR)
    6576             :         papszLocalParameters =
    6577           7 :             CSLSetNameValue(papszLocalParameters, "PHOTOMETRIC", "YCBCR");
    6578          27 :     else if (m_nPhotometric == PHOTOMETRIC_SEPARATED)
    6579             :         papszLocalParameters =
    6580           0 :             CSLSetNameValue(papszLocalParameters, "PHOTOMETRIC", "CMYK");
    6581             :     papszLocalParameters =
    6582          34 :         CSLSetNameValue(papszLocalParameters, "BLOCKYSIZE", "16");
    6583          34 :     if (m_nBitsPerSample == 12)
    6584             :         papszLocalParameters =
    6585          16 :             CSLSetNameValue(papszLocalParameters, "NBITS", "12");
    6586             : 
    6587             :     const CPLString osTmpFilenameIn(
    6588          34 :         VSIMemGenerateHiddenFilename("gtiffdataset_guess_jpeg_quality_tmp"));
    6589             : 
    6590          34 :     int nRet = -1;
    6591         938 :     for (int nQuality = 0; nQuality <= 100 && nRet < 0; ++nQuality)
    6592             :     {
    6593         904 :         VSILFILE *fpTmp = nullptr;
    6594         904 :         if (nQuality == 0)
    6595             :             papszLocalParameters =
    6596          34 :                 CSLSetNameValue(papszLocalParameters, "JPEG_QUALITY", "75");
    6597             :         else
    6598             :             papszLocalParameters =
    6599         870 :                 CSLSetNameValue(papszLocalParameters, "JPEG_QUALITY",
    6600             :                                 CPLSPrintf("%d", nQuality));
    6601             : 
    6602         904 :         CPLPushErrorHandler(CPLQuietErrorHandler);
    6603         904 :         CPLString osTmp;
    6604             :         bool bTileInterleaving;
    6605        1808 :         TIFF *hTIFFTmp = CreateLL(
    6606         904 :             osTmpFilenameIn, 16, 16, (nBands <= 4) ? nBands : 1,
    6607             :             GetRasterBand(1)->GetRasterDataType(), 0.0, 0, papszLocalParameters,
    6608             :             &fpTmp, osTmp, /* bCreateCopy=*/false, bTileInterleaving);
    6609         904 :         CPLPopErrorHandler();
    6610         904 :         if (!hTIFFTmp)
    6611             :         {
    6612           0 :             break;
    6613             :         }
    6614             : 
    6615         904 :         TIFFWriteCheck(hTIFFTmp, FALSE, "CreateLL");
    6616         904 :         TIFFWriteDirectory(hTIFFTmp);
    6617         904 :         TIFFSetDirectory(hTIFFTmp, 0);
    6618             :         // Now reset jpegcolormode.
    6619        1196 :         if (m_nPhotometric == PHOTOMETRIC_YCBCR &&
    6620         292 :             CPLTestBool(CPLGetConfigOption("CONVERT_YCBCR_TO_RGB", "YES")))
    6621             :         {
    6622         292 :             TIFFSetField(hTIFFTmp, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
    6623             :         }
    6624             : 
    6625         904 :         GByte abyZeroData[(16 * 16 * 4 * 3) / 2] = {};
    6626         904 :         const int nBlockSize =
    6627         904 :             (16 * 16 * ((nBands <= 4) ? nBands : 1) * m_nBitsPerSample) / 8;
    6628         904 :         TIFFWriteEncodedStrip(hTIFFTmp, 0, abyZeroData, nBlockSize);
    6629             : 
    6630         904 :         uint32_t nJPEGTableSizeTry = 0;
    6631         904 :         void *pJPEGTableTry = nullptr;
    6632         904 :         if (TIFFGetField(hTIFFTmp, TIFFTAG_JPEGTABLES, &nJPEGTableSizeTry,
    6633         904 :                          &pJPEGTableTry))
    6634             :         {
    6635         904 :             if (GTIFFQuantizationTablesEqual(
    6636             :                     static_cast<GByte *>(pJPEGTable), nJPEGTableSize,
    6637             :                     static_cast<GByte *>(pJPEGTableTry), nJPEGTableSizeTry))
    6638             :             {
    6639          34 :                 nRet = (nQuality == 0) ? 75 : nQuality;
    6640             :             }
    6641             :         }
    6642             : 
    6643         904 :         XTIFFClose(hTIFFTmp);
    6644         904 :         CPL_IGNORE_RET_VAL(VSIFCloseL(fpTmp));
    6645             :     }
    6646             : 
    6647          34 :     CSLDestroy(papszLocalParameters);
    6648          34 :     VSIUnlink(osTmpFilenameIn);
    6649             : 
    6650          34 :     return nRet;
    6651             : }
    6652             : 
    6653             : /************************************************************************/
    6654             : /*                SetJPEGQualityAndTablesModeFromFile()                 */
    6655             : /************************************************************************/
    6656             : 
    6657         161 : void GTiffDataset::SetJPEGQualityAndTablesModeFromFile(
    6658             :     int nQuality, bool bHasQuantizationTable, bool bHasHuffmanTable)
    6659             : {
    6660         161 :     if (nQuality > 0)
    6661             :     {
    6662         154 :         CPLDebug("GTiff", "Guessed JPEG quality to be %d", nQuality);
    6663         154 :         m_nJpegQuality = static_cast<signed char>(nQuality);
    6664         154 :         TIFFSetField(m_hTIFF, TIFFTAG_JPEGQUALITY, nQuality);
    6665             : 
    6666             :         // This means we will use the quantization tables from the
    6667             :         // JpegTables tag.
    6668         154 :         m_nJpegTablesMode = JPEGTABLESMODE_QUANT;
    6669             :     }
    6670             :     else
    6671             :     {
    6672           7 :         uint32_t nJPEGTableSize = 0;
    6673           7 :         void *pJPEGTable = nullptr;
    6674           7 :         if (!TIFFGetField(m_hTIFF, TIFFTAG_JPEGTABLES, &nJPEGTableSize,
    6675             :                           &pJPEGTable))
    6676             :         {
    6677           4 :             toff_t *panByteCounts = nullptr;
    6678           8 :             const int nBlockCount = m_nPlanarConfig == PLANARCONFIG_SEPARATE
    6679           4 :                                         ? m_nBlocksPerBand * nBands
    6680             :                                         : m_nBlocksPerBand;
    6681           4 :             if (TIFFIsTiled(m_hTIFF))
    6682           1 :                 TIFFGetField(m_hTIFF, TIFFTAG_TILEBYTECOUNTS, &panByteCounts);
    6683             :             else
    6684           3 :                 TIFFGetField(m_hTIFF, TIFFTAG_STRIPBYTECOUNTS, &panByteCounts);
    6685             : 
    6686           4 :             bool bFoundNonEmptyBlock = false;
    6687           4 :             if (panByteCounts != nullptr)
    6688             :             {
    6689          56 :                 for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
    6690             :                 {
    6691          53 :                     if (panByteCounts[iBlock] != 0)
    6692             :                     {
    6693           1 :                         bFoundNonEmptyBlock = true;
    6694           1 :                         break;
    6695             :                     }
    6696             :                 }
    6697             :             }
    6698           4 :             if (bFoundNonEmptyBlock)
    6699             :             {
    6700           1 :                 CPLDebug("GTiff", "Could not guess JPEG quality. "
    6701             :                                   "JPEG tables are missing, so going in "
    6702             :                                   "TIFFTAG_JPEGTABLESMODE = 0/2 mode");
    6703             :                 // Write quantization tables in each strile.
    6704           1 :                 m_nJpegTablesMode = 0;
    6705             :             }
    6706             :         }
    6707             :         else
    6708             :         {
    6709           3 :             if (bHasQuantizationTable)
    6710             :             {
    6711             :                 // FIXME in libtiff: this is likely going to cause issues
    6712             :                 // since libtiff will reuse in each strile the number of
    6713             :                 // the global quantization table, which is invalid.
    6714           1 :                 CPLDebug("GTiff",
    6715             :                          "Could not guess JPEG quality although JPEG "
    6716             :                          "quantization tables are present, so going in "
    6717             :                          "TIFFTAG_JPEGTABLESMODE = 0/2 mode");
    6718             :             }
    6719             :             else
    6720             :             {
    6721           2 :                 CPLDebug("GTiff",
    6722             :                          "Could not guess JPEG quality since JPEG "
    6723             :                          "quantization tables are not present, so going in "
    6724             :                          "TIFFTAG_JPEGTABLESMODE = 0/2 mode");
    6725             :             }
    6726             : 
    6727             :             // Write quantization tables in each strile.
    6728           3 :             m_nJpegTablesMode = 0;
    6729             :         }
    6730             :     }
    6731         161 :     if (bHasHuffmanTable)
    6732             :     {
    6733             :         // If there are Huffman tables in header use them, otherwise
    6734             :         // if we use optimized tables, libtiff will currently reuse
    6735             :         // the number of the Huffman tables of the header for the
    6736             :         // optimized version of each strile, which is illegal.
    6737          23 :         m_nJpegTablesMode |= JPEGTABLESMODE_HUFF;
    6738             :     }
    6739         161 :     if (m_nJpegTablesMode >= 0)
    6740         159 :         TIFFSetField(m_hTIFF, TIFFTAG_JPEGTABLESMODE, m_nJpegTablesMode);
    6741         161 : }
    6742             : 
    6743             : /************************************************************************/
    6744             : /*                               Create()                               */
    6745             : /*                                                                      */
    6746             : /*      Create a new GeoTIFF or TIFF file.                              */
    6747             : /************************************************************************/
    6748             : 
    6749        5755 : GDALDataset *GTiffDataset::Create(const char *pszFilename, int nXSize,
    6750             :                                   int nYSize, int l_nBands, GDALDataType eType,
    6751             :                                   CSLConstList papszParamList)
    6752             : 
    6753             : {
    6754        5755 :     VSILFILE *l_fpL = nullptr;
    6755       11510 :     CPLString l_osTmpFilename;
    6756             : 
    6757             :     const int nColorTableMultiplier = std::max(
    6758       11510 :         1,
    6759       11510 :         std::min(257,
    6760        5755 :                  atoi(CSLFetchNameValueDef(
    6761             :                      papszParamList, "COLOR_TABLE_MULTIPLIER",
    6762        5755 :                      CPLSPrintf("%d", DEFAULT_COLOR_TABLE_MULTIPLIER_257)))));
    6763             : 
    6764             :     /* -------------------------------------------------------------------- */
    6765             :     /*      Create the underlying TIFF file.                                */
    6766             :     /* -------------------------------------------------------------------- */
    6767             :     bool bTileInterleaving;
    6768             :     TIFF *l_hTIFF =
    6769        5755 :         CreateLL(pszFilename, nXSize, nYSize, l_nBands, eType, 0,
    6770             :                  nColorTableMultiplier, papszParamList, &l_fpL, l_osTmpFilename,
    6771             :                  /* bCreateCopy=*/false, bTileInterleaving);
    6772        5755 :     const bool bStreaming = !l_osTmpFilename.empty();
    6773             : 
    6774        5755 :     if (l_hTIFF == nullptr)
    6775          38 :         return nullptr;
    6776             : 
    6777             :     /* -------------------------------------------------------------------- */
    6778             :     /*      Create the new GTiffDataset object.                             */
    6779             :     /* -------------------------------------------------------------------- */
    6780       11434 :     auto poDS = std::make_unique<GTiffDataset>();
    6781        5717 :     poDS->m_hTIFF = l_hTIFF;
    6782        5717 :     poDS->m_fpL = l_fpL;
    6783        5717 :     const bool bSuppressASAP = CPLTestBool(
    6784             :         CSLFetchNameValueDef(papszParamList, "@SUPPRESS_ASAP", "NO"));
    6785        5717 :     if (bSuppressASAP)
    6786          34 :         poDS->MarkSuppressOnClose();
    6787        5717 :     if (bStreaming)
    6788             :     {
    6789           4 :         poDS->m_bStreamingOut = true;
    6790           4 :         poDS->m_pszTmpFilename = CPLStrdup(l_osTmpFilename);
    6791           4 :         poDS->m_fpToWrite = VSIFOpenL(pszFilename, "wb");
    6792           4 :         if (poDS->m_fpToWrite == nullptr)
    6793             :         {
    6794           1 :             VSIUnlink(l_osTmpFilename);
    6795           1 :             return nullptr;
    6796             :         }
    6797             :     }
    6798        5716 :     poDS->nRasterXSize = nXSize;
    6799        5716 :     poDS->nRasterYSize = nYSize;
    6800        5716 :     poDS->eAccess = GA_Update;
    6801             : 
    6802        5716 :     poDS->m_nColorTableMultiplier = nColorTableMultiplier;
    6803             : 
    6804        5716 :     poDS->m_bCrystalized = false;
    6805        5716 :     poDS->m_nSamplesPerPixel = static_cast<uint16_t>(l_nBands);
    6806        5716 :     poDS->m_osFilename = pszFilename;
    6807             : 
    6808             :     // Don't try to load external metadata files (#6597).
    6809        5716 :     poDS->m_bIMDRPCMetadataLoaded = true;
    6810             : 
    6811             :     // Avoid premature crystalization that will cause directory re-writing if
    6812             :     // GetProjectionRef() or GetGeoTransform() are called on the newly created
    6813             :     // GeoTIFF.
    6814        5716 :     poDS->m_bLookedForProjection = true;
    6815             : 
    6816        5716 :     TIFFGetField(l_hTIFF, TIFFTAG_SAMPLEFORMAT, &(poDS->m_nSampleFormat));
    6817        5716 :     TIFFGetField(l_hTIFF, TIFFTAG_PLANARCONFIG, &(poDS->m_nPlanarConfig));
    6818             :     // Weird that we need this, but otherwise we get a Valgrind warning on
    6819             :     // tiff_write_124.
    6820        5716 :     if (!TIFFGetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, &(poDS->m_nPhotometric)))
    6821           1 :         poDS->m_nPhotometric = PHOTOMETRIC_MINISBLACK;
    6822        5716 :     TIFFGetField(l_hTIFF, TIFFTAG_BITSPERSAMPLE, &(poDS->m_nBitsPerSample));
    6823        5716 :     TIFFGetField(l_hTIFF, TIFFTAG_COMPRESSION, &(poDS->m_nCompression));
    6824             : 
    6825        5716 :     if (TIFFIsTiled(l_hTIFF))
    6826             :     {
    6827         395 :         TIFFGetField(l_hTIFF, TIFFTAG_TILEWIDTH, &(poDS->m_nBlockXSize));
    6828         395 :         TIFFGetField(l_hTIFF, TIFFTAG_TILELENGTH, &(poDS->m_nBlockYSize));
    6829             :     }
    6830             :     else
    6831             :     {
    6832        5321 :         if (!TIFFGetField(l_hTIFF, TIFFTAG_ROWSPERSTRIP,
    6833        5321 :                           &(poDS->m_nRowsPerStrip)))
    6834           0 :             poDS->m_nRowsPerStrip = 1;  // Dummy value.
    6835             : 
    6836        5321 :         poDS->m_nBlockXSize = nXSize;
    6837       10642 :         poDS->m_nBlockYSize =
    6838        5321 :             std::min(static_cast<int>(poDS->m_nRowsPerStrip), nYSize);
    6839             :     }
    6840             : 
    6841        5716 :     if (!poDS->ComputeBlocksPerColRowAndBand(l_nBands))
    6842             :     {
    6843           0 :         poDS->m_fpL->CancelCreation();
    6844           0 :         return nullptr;
    6845             :     }
    6846             : 
    6847        5716 :     poDS->m_eProfile = GetProfile(CSLFetchNameValue(papszParamList, "PROFILE"));
    6848             : 
    6849             :     /* -------------------------------------------------------------------- */
    6850             :     /*      YCbCr JPEG compressed images should be translated on the fly    */
    6851             :     /*      to RGB by libtiff/libjpeg unless specifically requested         */
    6852             :     /*      otherwise.                                                      */
    6853             :     /* -------------------------------------------------------------------- */
    6854        5716 :     if (poDS->m_nCompression == COMPRESSION_JPEG &&
    6855        5737 :         poDS->m_nPhotometric == PHOTOMETRIC_YCBCR &&
    6856          21 :         CPLTestBool(CPLGetConfigOption("CONVERT_YCBCR_TO_RGB", "YES")))
    6857             :     {
    6858          21 :         int nColorMode = 0;
    6859             : 
    6860          21 :         poDS->SetMetadataItem("SOURCE_COLOR_SPACE", "YCbCr", "IMAGE_STRUCTURE");
    6861          42 :         if (!TIFFGetField(l_hTIFF, TIFFTAG_JPEGCOLORMODE, &nColorMode) ||
    6862          21 :             nColorMode != JPEGCOLORMODE_RGB)
    6863          21 :             TIFFSetField(l_hTIFF, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
    6864             :     }
    6865             : 
    6866        5716 :     if (poDS->m_nCompression == COMPRESSION_LERC)
    6867             :     {
    6868          26 :         uint32_t nLercParamCount = 0;
    6869          26 :         uint32_t *panLercParams = nullptr;
    6870          26 :         if (TIFFGetField(l_hTIFF, TIFFTAG_LERC_PARAMETERS, &nLercParamCount,
    6871          52 :                          &panLercParams) &&
    6872          26 :             nLercParamCount == 2)
    6873             :         {
    6874          26 :             memcpy(poDS->m_anLercAddCompressionAndVersion, panLercParams,
    6875             :                    sizeof(poDS->m_anLercAddCompressionAndVersion));
    6876             :         }
    6877             :     }
    6878             : 
    6879             :     /* -------------------------------------------------------------------- */
    6880             :     /*      Read palette back as a color table if it has one.               */
    6881             :     /* -------------------------------------------------------------------- */
    6882        5716 :     unsigned short *panRed = nullptr;
    6883        5716 :     unsigned short *panGreen = nullptr;
    6884        5716 :     unsigned short *panBlue = nullptr;
    6885             : 
    6886        5720 :     if (poDS->m_nPhotometric == PHOTOMETRIC_PALETTE &&
    6887           4 :         TIFFGetField(l_hTIFF, TIFFTAG_COLORMAP, &panRed, &panGreen, &panBlue))
    6888             :     {
    6889             : 
    6890           4 :         poDS->m_poColorTable = std::make_unique<GDALColorTable>();
    6891             : 
    6892           4 :         const int nColorCount = 1 << poDS->m_nBitsPerSample;
    6893             : 
    6894        1028 :         for (int iColor = nColorCount - 1; iColor >= 0; iColor--)
    6895             :         {
    6896        1024 :             const GDALColorEntry oEntry = {
    6897        1024 :                 static_cast<short>(panRed[iColor] / nColorTableMultiplier),
    6898        1024 :                 static_cast<short>(panGreen[iColor] / nColorTableMultiplier),
    6899        1024 :                 static_cast<short>(panBlue[iColor] / nColorTableMultiplier),
    6900        1024 :                 static_cast<short>(255)};
    6901             : 
    6902        1024 :             poDS->m_poColorTable->SetColorEntry(iColor, &oEntry);
    6903             :         }
    6904             :     }
    6905             : 
    6906             :     /* -------------------------------------------------------------------- */
    6907             :     /*      Do we want to ensure all blocks get written out on close to     */
    6908             :     /*      avoid sparse files?                                             */
    6909             :     /* -------------------------------------------------------------------- */
    6910        5716 :     if (!CPLFetchBool(papszParamList, "SPARSE_OK", false))
    6911        5606 :         poDS->m_bFillEmptyTilesAtClosing = true;
    6912             : 
    6913        5716 :     poDS->m_bWriteEmptyTiles =
    6914        6534 :         bStreaming || (poDS->m_nCompression != COMPRESSION_NONE &&
    6915         818 :                        poDS->m_bFillEmptyTilesAtClosing);
    6916             :     // Only required for people writing non-compressed striped files in the
    6917             :     // right order and wanting all tstrips to be written in the same order
    6918             :     // so that the end result can be memory mapped without knowledge of each
    6919             :     // strip offset.
    6920        5716 :     if (CPLTestBool(CSLFetchNameValueDef(
    6921       11432 :             papszParamList, "WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")) ||
    6922        5716 :         CPLTestBool(CSLFetchNameValueDef(
    6923             :             papszParamList, "@WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")))
    6924             :     {
    6925          26 :         poDS->m_bWriteEmptyTiles = true;
    6926             :     }
    6927             : 
    6928             :     /* -------------------------------------------------------------------- */
    6929             :     /*      Preserve creation options for consulting later (for instance    */
    6930             :     /*      to decide if a TFW file should be written).                     */
    6931             :     /* -------------------------------------------------------------------- */
    6932        5716 :     poDS->m_papszCreationOptions = CSLDuplicate(papszParamList);
    6933             : 
    6934        5716 :     poDS->m_nZLevel = GTiffGetZLevel(papszParamList);
    6935        5716 :     poDS->m_nLZMAPreset = GTiffGetLZMAPreset(papszParamList);
    6936        5716 :     poDS->m_nZSTDLevel = GTiffGetZSTDPreset(papszParamList);
    6937        5716 :     poDS->m_nWebPLevel = GTiffGetWebPLevel(papszParamList);
    6938        5716 :     poDS->m_bWebPLossless = GTiffGetWebPLossless(papszParamList);
    6939        5718 :     if (poDS->m_nWebPLevel != 100 && poDS->m_bWebPLossless &&
    6940           2 :         CSLFetchNameValue(papszParamList, "WEBP_LEVEL"))
    6941             :     {
    6942           0 :         CPLError(CE_Warning, CPLE_AppDefined,
    6943             :                  "WEBP_LEVEL is specified, but WEBP_LOSSLESS=YES. "
    6944             :                  "WEBP_LEVEL will be ignored.");
    6945             :     }
    6946        5716 :     poDS->m_nJpegQuality = GTiffGetJpegQuality(papszParamList);
    6947        5716 :     poDS->m_nJpegTablesMode = GTiffGetJpegTablesMode(papszParamList);
    6948        5716 :     poDS->m_dfMaxZError = GTiffGetLERCMaxZError(papszParamList);
    6949        5716 :     poDS->m_dfMaxZErrorOverview = GTiffGetLERCMaxZErrorOverview(papszParamList);
    6950             : #if HAVE_JXL
    6951        5716 :     poDS->m_bJXLLossless = GTiffGetJXLLossless(papszParamList);
    6952        5716 :     poDS->m_nJXLEffort = GTiffGetJXLEffort(papszParamList);
    6953        5716 :     poDS->m_fJXLDistance = GTiffGetJXLDistance(papszParamList);
    6954        5716 :     poDS->m_fJXLAlphaDistance = GTiffGetJXLAlphaDistance(papszParamList);
    6955             : #endif
    6956        5716 :     poDS->InitCreationOrOpenOptions(true, papszParamList);
    6957             : 
    6958             :     /* -------------------------------------------------------------------- */
    6959             :     /*      Create band information objects.                                */
    6960             :     /* -------------------------------------------------------------------- */
    6961      308271 :     for (int iBand = 0; iBand < l_nBands; ++iBand)
    6962             :     {
    6963      371760 :         if (poDS->m_nBitsPerSample == 8 || poDS->m_nBitsPerSample == 16 ||
    6964      372001 :             poDS->m_nBitsPerSample == 32 || poDS->m_nBitsPerSample == 64 ||
    6965         241 :             poDS->m_nBitsPerSample == 128)
    6966             :         {
    6967      604980 :             poDS->SetBand(iBand + 1, std::make_unique<GTiffRasterBand>(
    6968      604980 :                                          poDS.get(), iBand + 1));
    6969             :         }
    6970             :         else
    6971             :         {
    6972         130 :             poDS->SetBand(iBand + 1, std::make_unique<GTiffOddBitsBand>(
    6973          65 :                                          poDS.get(), iBand + 1));
    6974         130 :             poDS->GetRasterBand(iBand + 1)->SetMetadataItem(
    6975         130 :                 "NBITS", CPLString().Printf("%d", poDS->m_nBitsPerSample),
    6976          65 :                 "IMAGE_STRUCTURE");
    6977             :         }
    6978             :     }
    6979             : 
    6980        5716 :     poDS->GetDiscardLsbOption(papszParamList);
    6981             : 
    6982        5716 :     if (poDS->m_nPlanarConfig == PLANARCONFIG_CONTIG && l_nBands != 1)
    6983         835 :         poDS->SetMetadataItem("INTERLEAVE", "PIXEL", "IMAGE_STRUCTURE");
    6984             :     else
    6985        4881 :         poDS->SetMetadataItem("INTERLEAVE", "BAND", "IMAGE_STRUCTURE");
    6986             : 
    6987        5716 :     poDS->oOvManager.Initialize(poDS.get(), pszFilename);
    6988             : 
    6989        5716 :     return poDS.release();
    6990             : }
    6991             : 
    6992             : /************************************************************************/
    6993             : /*                         CopyImageryAndMask()                         */
    6994             : /************************************************************************/
    6995             : 
    6996         344 : CPLErr GTiffDataset::CopyImageryAndMask(GTiffDataset *poDstDS,
    6997             :                                         GDALDataset *poSrcDS,
    6998             :                                         GDALRasterBand *poSrcMaskBand,
    6999             :                                         GDALProgressFunc pfnProgress,
    7000             :                                         void *pProgressData)
    7001             : {
    7002         344 :     CPLErr eErr = CE_None;
    7003             : 
    7004         344 :     const auto eType = poDstDS->GetRasterBand(1)->GetRasterDataType();
    7005         344 :     const int nDataTypeSize = GDALGetDataTypeSizeBytes(eType);
    7006         344 :     const int l_nBands = poDstDS->GetRasterCount();
    7007             :     GByte *pBlockBuffer = static_cast<GByte *>(
    7008         344 :         VSI_MALLOC3_VERBOSE(poDstDS->m_nBlockXSize, poDstDS->m_nBlockYSize,
    7009             :                             cpl::fits_on<int>(l_nBands * nDataTypeSize)));
    7010         344 :     if (pBlockBuffer == nullptr)
    7011             :     {
    7012           0 :         eErr = CE_Failure;
    7013             :     }
    7014         344 :     const int nYSize = poDstDS->nRasterYSize;
    7015         344 :     const int nXSize = poDstDS->nRasterXSize;
    7016             :     const bool bIsOddBand =
    7017         344 :         dynamic_cast<GTiffOddBitsBand *>(poDstDS->GetRasterBand(1)) != nullptr;
    7018             : 
    7019         344 :     if (poDstDS->m_poMaskDS)
    7020             :     {
    7021          59 :         CPLAssert(poDstDS->m_poMaskDS->m_nBlockXSize == poDstDS->m_nBlockXSize);
    7022          59 :         CPLAssert(poDstDS->m_poMaskDS->m_nBlockYSize == poDstDS->m_nBlockYSize);
    7023             :     }
    7024             : 
    7025         344 :     if (poDstDS->m_nPlanarConfig == PLANARCONFIG_SEPARATE &&
    7026          58 :         !poDstDS->m_bTileInterleave)
    7027             :     {
    7028          45 :         int iBlock = 0;
    7029          45 :         const int nBlocks = poDstDS->m_nBlocksPerBand *
    7030          45 :                             (l_nBands + (poDstDS->m_poMaskDS ? 1 : 0));
    7031         195 :         for (int i = 0; eErr == CE_None && i < l_nBands; i++)
    7032             :         {
    7033         345 :             for (int iY = 0, nYBlock = 0; iY < nYSize && eErr == CE_None;
    7034         195 :                  iY = ((nYSize - iY < poDstDS->m_nBlockYSize)
    7035         195 :                            ? nYSize
    7036          59 :                            : iY + poDstDS->m_nBlockYSize),
    7037             :                      nYBlock++)
    7038             :             {
    7039             :                 const int nReqYSize =
    7040         195 :                     std::min(nYSize - iY, poDstDS->m_nBlockYSize);
    7041         495 :                 for (int iX = 0, nXBlock = 0; iX < nXSize && eErr == CE_None;
    7042         300 :                      iX = ((nXSize - iX < poDstDS->m_nBlockXSize)
    7043         300 :                                ? nXSize
    7044         155 :                                : iX + poDstDS->m_nBlockXSize),
    7045             :                          nXBlock++)
    7046             :                 {
    7047             :                     const int nReqXSize =
    7048         300 :                         std::min(nXSize - iX, poDstDS->m_nBlockXSize);
    7049         300 :                     if (nReqXSize < poDstDS->m_nBlockXSize ||
    7050         155 :                         nReqYSize < poDstDS->m_nBlockYSize)
    7051             :                     {
    7052         190 :                         memset(pBlockBuffer, 0,
    7053         190 :                                static_cast<size_t>(poDstDS->m_nBlockXSize) *
    7054         190 :                                    poDstDS->m_nBlockYSize * nDataTypeSize);
    7055             :                     }
    7056         300 :                     eErr = poSrcDS->GetRasterBand(i + 1)->RasterIO(
    7057             :                         GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
    7058             :                         nReqXSize, nReqYSize, eType, nDataTypeSize,
    7059         300 :                         static_cast<GSpacing>(nDataTypeSize) *
    7060         300 :                             poDstDS->m_nBlockXSize,
    7061             :                         nullptr);
    7062         300 :                     if (eErr == CE_None)
    7063             :                     {
    7064         300 :                         eErr = poDstDS->WriteEncodedTileOrStrip(
    7065             :                             iBlock, pBlockBuffer, false);
    7066             :                     }
    7067             : 
    7068         300 :                     iBlock++;
    7069         600 :                     if (pfnProgress &&
    7070         300 :                         !pfnProgress(static_cast<double>(iBlock) / nBlocks,
    7071             :                                      nullptr, pProgressData))
    7072             :                     {
    7073           0 :                         eErr = CE_Failure;
    7074             :                     }
    7075             : 
    7076         300 :                     if (poDstDS->m_bWriteError)
    7077           0 :                         eErr = CE_Failure;
    7078             :                 }
    7079             :             }
    7080             :         }
    7081          45 :         if (poDstDS->m_poMaskDS && eErr == CE_None)
    7082             :         {
    7083           6 :             int iBlockMask = 0;
    7084          17 :             for (int iY = 0, nYBlock = 0; iY < nYSize && eErr == CE_None;
    7085          11 :                  iY = ((nYSize - iY < poDstDS->m_nBlockYSize)
    7086          11 :                            ? nYSize
    7087           5 :                            : iY + poDstDS->m_nBlockYSize),
    7088             :                      nYBlock++)
    7089             :             {
    7090             :                 const int nReqYSize =
    7091          11 :                     std::min(nYSize - iY, poDstDS->m_nBlockYSize);
    7092          49 :                 for (int iX = 0, nXBlock = 0; iX < nXSize && eErr == CE_None;
    7093          38 :                      iX = ((nXSize - iX < poDstDS->m_nBlockXSize)
    7094          38 :                                ? nXSize
    7095          30 :                                : iX + poDstDS->m_nBlockXSize),
    7096             :                          nXBlock++)
    7097             :                 {
    7098             :                     const int nReqXSize =
    7099          38 :                         std::min(nXSize - iX, poDstDS->m_nBlockXSize);
    7100          38 :                     if (nReqXSize < poDstDS->m_nBlockXSize ||
    7101          30 :                         nReqYSize < poDstDS->m_nBlockYSize)
    7102             :                     {
    7103          16 :                         memset(pBlockBuffer, 0,
    7104          16 :                                static_cast<size_t>(poDstDS->m_nBlockXSize) *
    7105          16 :                                    poDstDS->m_nBlockYSize);
    7106             :                     }
    7107          76 :                     eErr = poSrcMaskBand->RasterIO(
    7108             :                         GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
    7109             :                         nReqXSize, nReqYSize, GDT_UInt8, 1,
    7110          38 :                         poDstDS->m_nBlockXSize, nullptr);
    7111          38 :                     if (eErr == CE_None)
    7112             :                     {
    7113             :                         // Avoid any attempt to load from disk
    7114          38 :                         poDstDS->m_poMaskDS->m_nLoadedBlock = iBlockMask;
    7115             :                         eErr =
    7116          38 :                             poDstDS->m_poMaskDS->GetRasterBand(1)->WriteBlock(
    7117             :                                 nXBlock, nYBlock, pBlockBuffer);
    7118          38 :                         if (eErr == CE_None)
    7119          38 :                             eErr = poDstDS->m_poMaskDS->FlushBlockBuf();
    7120             :                     }
    7121             : 
    7122          38 :                     iBlockMask++;
    7123          76 :                     if (pfnProgress &&
    7124          38 :                         !pfnProgress(static_cast<double>(iBlock + iBlockMask) /
    7125             :                                          nBlocks,
    7126             :                                      nullptr, pProgressData))
    7127             :                     {
    7128           0 :                         eErr = CE_Failure;
    7129             :                     }
    7130             : 
    7131          38 :                     if (poDstDS->m_poMaskDS->m_bWriteError)
    7132           0 :                         eErr = CE_Failure;
    7133             :                 }
    7134             :             }
    7135          45 :         }
    7136             :     }
    7137             :     else
    7138             :     {
    7139         299 :         int iBlock = 0;
    7140         299 :         const int nBlocks = poDstDS->m_nBlocksPerBand;
    7141        7092 :         for (int iY = 0, nYBlock = 0; iY < nYSize && eErr == CE_None;
    7142        6793 :              iY = ((nYSize - iY < poDstDS->m_nBlockYSize)
    7143        6793 :                        ? nYSize
    7144        6569 :                        : iY + poDstDS->m_nBlockYSize),
    7145             :                  nYBlock++)
    7146             :         {
    7147        6793 :             const int nReqYSize = std::min(nYSize - iY, poDstDS->m_nBlockYSize);
    7148       26509 :             for (int iX = 0, nXBlock = 0; iX < nXSize && eErr == CE_None;
    7149       19716 :                  iX = ((nXSize - iX < poDstDS->m_nBlockXSize)
    7150       19716 :                            ? nXSize
    7151       19420 :                            : iX + poDstDS->m_nBlockXSize),
    7152             :                      nXBlock++)
    7153             :             {
    7154             :                 const int nReqXSize =
    7155       19716 :                     std::min(nXSize - iX, poDstDS->m_nBlockXSize);
    7156       19716 :                 if (nReqXSize < poDstDS->m_nBlockXSize ||
    7157       19420 :                     nReqYSize < poDstDS->m_nBlockYSize)
    7158             :                 {
    7159         465 :                     memset(pBlockBuffer, 0,
    7160         465 :                            static_cast<size_t>(poDstDS->m_nBlockXSize) *
    7161         465 :                                poDstDS->m_nBlockYSize * l_nBands *
    7162         465 :                                nDataTypeSize);
    7163             :                 }
    7164             : 
    7165       19716 :                 if (poDstDS->m_bTileInterleave)
    7166             :                 {
    7167         114 :                     eErr = poSrcDS->RasterIO(
    7168             :                         GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
    7169             :                         nReqXSize, nReqYSize, eType, l_nBands, nullptr,
    7170             :                         nDataTypeSize,
    7171          57 :                         static_cast<GSpacing>(nDataTypeSize) *
    7172          57 :                             poDstDS->m_nBlockXSize,
    7173          57 :                         static_cast<GSpacing>(nDataTypeSize) *
    7174          57 :                             poDstDS->m_nBlockXSize * poDstDS->m_nBlockYSize,
    7175             :                         nullptr);
    7176          57 :                     if (eErr == CE_None)
    7177             :                     {
    7178         228 :                         for (int i = 0; eErr == CE_None && i < l_nBands; i++)
    7179             :                         {
    7180         171 :                             eErr = poDstDS->WriteEncodedTileOrStrip(
    7181         171 :                                 iBlock + i * poDstDS->m_nBlocksPerBand,
    7182         171 :                                 pBlockBuffer + static_cast<size_t>(i) *
    7183         171 :                                                    poDstDS->m_nBlockXSize *
    7184         171 :                                                    poDstDS->m_nBlockYSize *
    7185         171 :                                                    nDataTypeSize,
    7186             :                                 false);
    7187             :                         }
    7188             :                     }
    7189             :                 }
    7190       19659 :                 else if (!bIsOddBand)
    7191             :                 {
    7192       39196 :                     eErr = poSrcDS->RasterIO(
    7193             :                         GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
    7194             :                         nReqXSize, nReqYSize, eType, l_nBands, nullptr,
    7195       19598 :                         static_cast<GSpacing>(nDataTypeSize) * l_nBands,
    7196       19598 :                         static_cast<GSpacing>(nDataTypeSize) * l_nBands *
    7197       19598 :                             poDstDS->m_nBlockXSize,
    7198             :                         nDataTypeSize, nullptr);
    7199       19598 :                     if (eErr == CE_None)
    7200             :                     {
    7201       19597 :                         eErr = poDstDS->WriteEncodedTileOrStrip(
    7202             :                             iBlock, pBlockBuffer, false);
    7203             :                     }
    7204             :                 }
    7205             :                 else
    7206             :                 {
    7207             :                     // In the odd bit case, this is a bit messy to ensure
    7208             :                     // the strile gets written synchronously.
    7209             :                     // We load the content of the n-1 bands in the cache,
    7210             :                     // and for the last band we invoke WriteBlock() directly
    7211             :                     // We also force FlushBlockBuf()
    7212         122 :                     std::vector<GDALRasterBlock *> apoLockedBlocks;
    7213          91 :                     for (int i = 0; eErr == CE_None && i < l_nBands - 1; i++)
    7214             :                     {
    7215             :                         auto poBlock =
    7216          30 :                             poDstDS->GetRasterBand(i + 1)->GetLockedBlockRef(
    7217          30 :                                 nXBlock, nYBlock, TRUE);
    7218          30 :                         if (poBlock)
    7219             :                         {
    7220          60 :                             eErr = poSrcDS->GetRasterBand(i + 1)->RasterIO(
    7221             :                                 GF_Read, iX, iY, nReqXSize, nReqYSize,
    7222             :                                 poBlock->GetDataRef(), nReqXSize, nReqYSize,
    7223             :                                 eType, nDataTypeSize,
    7224          30 :                                 static_cast<GSpacing>(nDataTypeSize) *
    7225          30 :                                     poDstDS->m_nBlockXSize,
    7226             :                                 nullptr);
    7227          30 :                             poBlock->MarkDirty();
    7228          30 :                             apoLockedBlocks.emplace_back(poBlock);
    7229             :                         }
    7230             :                         else
    7231             :                         {
    7232           0 :                             eErr = CE_Failure;
    7233             :                         }
    7234             :                     }
    7235          61 :                     if (eErr == CE_None)
    7236             :                     {
    7237         122 :                         eErr = poSrcDS->GetRasterBand(l_nBands)->RasterIO(
    7238             :                             GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
    7239             :                             nReqXSize, nReqYSize, eType, nDataTypeSize,
    7240          61 :                             static_cast<GSpacing>(nDataTypeSize) *
    7241          61 :                                 poDstDS->m_nBlockXSize,
    7242             :                             nullptr);
    7243             :                     }
    7244          61 :                     if (eErr == CE_None)
    7245             :                     {
    7246             :                         // Avoid any attempt to load from disk
    7247          61 :                         poDstDS->m_nLoadedBlock = iBlock;
    7248          61 :                         eErr = poDstDS->GetRasterBand(l_nBands)->WriteBlock(
    7249             :                             nXBlock, nYBlock, pBlockBuffer);
    7250          61 :                         if (eErr == CE_None)
    7251          61 :                             eErr = poDstDS->FlushBlockBuf();
    7252             :                     }
    7253          91 :                     for (auto poBlock : apoLockedBlocks)
    7254             :                     {
    7255          30 :                         poBlock->MarkClean();
    7256          30 :                         poBlock->DropLock();
    7257             :                     }
    7258             :                 }
    7259             : 
    7260       19716 :                 if (eErr == CE_None && poDstDS->m_poMaskDS)
    7261             :                 {
    7262        4664 :                     if (nReqXSize < poDstDS->m_nBlockXSize ||
    7263        4621 :                         nReqYSize < poDstDS->m_nBlockYSize)
    7264             :                     {
    7265          81 :                         memset(pBlockBuffer, 0,
    7266          81 :                                static_cast<size_t>(poDstDS->m_nBlockXSize) *
    7267          81 :                                    poDstDS->m_nBlockYSize);
    7268             :                     }
    7269        9328 :                     eErr = poSrcMaskBand->RasterIO(
    7270             :                         GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
    7271             :                         nReqXSize, nReqYSize, GDT_UInt8, 1,
    7272        4664 :                         poDstDS->m_nBlockXSize, nullptr);
    7273        4664 :                     if (eErr == CE_None)
    7274             :                     {
    7275             :                         // Avoid any attempt to load from disk
    7276        4664 :                         poDstDS->m_poMaskDS->m_nLoadedBlock = iBlock;
    7277             :                         eErr =
    7278        4664 :                             poDstDS->m_poMaskDS->GetRasterBand(1)->WriteBlock(
    7279             :                                 nXBlock, nYBlock, pBlockBuffer);
    7280        4664 :                         if (eErr == CE_None)
    7281        4664 :                             eErr = poDstDS->m_poMaskDS->FlushBlockBuf();
    7282             :                     }
    7283             :                 }
    7284       19716 :                 if (poDstDS->m_bWriteError)
    7285           6 :                     eErr = CE_Failure;
    7286             : 
    7287       19716 :                 iBlock++;
    7288       39432 :                 if (pfnProgress &&
    7289       19716 :                     !pfnProgress(static_cast<double>(iBlock) / nBlocks, nullptr,
    7290             :                                  pProgressData))
    7291             :                 {
    7292           0 :                     eErr = CE_Failure;
    7293             :                 }
    7294             :             }
    7295             :         }
    7296             :     }
    7297             : 
    7298         344 :     poDstDS->FlushCache(false);  // mostly to wait for thread completion
    7299         344 :     VSIFree(pBlockBuffer);
    7300             : 
    7301         344 :     return eErr;
    7302             : }
    7303             : 
    7304             : /************************************************************************/
    7305             : /*                             CreateCopy()                             */
    7306             : /************************************************************************/
    7307             : 
    7308        2145 : GDALDataset *GTiffDataset::CreateCopy(const char *pszFilename,
    7309             :                                       GDALDataset *poSrcDS, int bStrict,
    7310             :                                       CSLConstList papszOptions,
    7311             :                                       GDALProgressFunc pfnProgress,
    7312             :                                       void *pProgressData)
    7313             : 
    7314             : {
    7315        2145 :     if (poSrcDS->GetRasterCount() == 0)
    7316             :     {
    7317           2 :         ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    7318             :                     "Unable to export GeoTIFF files with zero bands.");
    7319           2 :         return nullptr;
    7320             :     }
    7321             : 
    7322        2143 :     GDALRasterBand *const poPBand = poSrcDS->GetRasterBand(1);
    7323        2143 :     GDALDataType eType = poPBand->GetRasterDataType();
    7324             : 
    7325             :     /* -------------------------------------------------------------------- */
    7326             :     /*      Check, whether all bands in input dataset has the same type.    */
    7327             :     /* -------------------------------------------------------------------- */
    7328        2143 :     const int l_nBands = poSrcDS->GetRasterCount();
    7329        5064 :     for (int iBand = 2; iBand <= l_nBands; ++iBand)
    7330             :     {
    7331        2921 :         if (eType != poSrcDS->GetRasterBand(iBand)->GetRasterDataType())
    7332             :         {
    7333           0 :             if (bStrict)
    7334             :             {
    7335           0 :                 ReportError(
    7336             :                     pszFilename, CE_Failure, CPLE_AppDefined,
    7337             :                     "Unable to export GeoTIFF file with different datatypes "
    7338             :                     "per different bands. All bands should have the same "
    7339             :                     "types in TIFF.");
    7340           0 :                 return nullptr;
    7341             :             }
    7342             :             else
    7343             :             {
    7344           0 :                 ReportError(
    7345             :                     pszFilename, CE_Warning, CPLE_AppDefined,
    7346             :                     "Unable to export GeoTIFF file with different datatypes "
    7347             :                     "per different bands. All bands should have the same "
    7348             :                     "types in TIFF.");
    7349             :             }
    7350             :         }
    7351             :     }
    7352             : 
    7353             :     /* -------------------------------------------------------------------- */
    7354             :     /*      Capture the profile.                                            */
    7355             :     /* -------------------------------------------------------------------- */
    7356             :     const GTiffProfile eProfile =
    7357        2143 :         GetProfile(CSLFetchNameValue(papszOptions, "PROFILE"));
    7358             : 
    7359        2143 :     const bool bGeoTIFF = eProfile != GTiffProfile::BASELINE;
    7360             : 
    7361             :     /* -------------------------------------------------------------------- */
    7362             :     /*      Special handling for NBITS.  Copy from band metadata if found.  */
    7363             :     /* -------------------------------------------------------------------- */
    7364        2143 :     char **papszCreateOptions = CSLDuplicate(papszOptions);
    7365             : 
    7366        2143 :     if (poPBand->GetMetadataItem("NBITS", "IMAGE_STRUCTURE") != nullptr &&
    7367        2160 :         atoi(poPBand->GetMetadataItem("NBITS", "IMAGE_STRUCTURE")) > 0 &&
    7368          17 :         CSLFetchNameValue(papszCreateOptions, "NBITS") == nullptr)
    7369             :     {
    7370           3 :         papszCreateOptions = CSLSetNameValue(
    7371             :             papszCreateOptions, "NBITS",
    7372           3 :             poPBand->GetMetadataItem("NBITS", "IMAGE_STRUCTURE"));
    7373             :     }
    7374             : 
    7375        2143 :     if (CSLFetchNameValue(papszOptions, "PIXELTYPE") == nullptr &&
    7376             :         eType == GDT_UInt8)
    7377             :     {
    7378        1777 :         poPBand->EnablePixelTypeSignedByteWarning(false);
    7379             :         const char *pszPixelType =
    7380        1777 :             poPBand->GetMetadataItem("PIXELTYPE", "IMAGE_STRUCTURE");
    7381        1777 :         poPBand->EnablePixelTypeSignedByteWarning(true);
    7382        1777 :         if (pszPixelType)
    7383             :         {
    7384           1 :             papszCreateOptions =
    7385           1 :                 CSLSetNameValue(papszCreateOptions, "PIXELTYPE", pszPixelType);
    7386             :         }
    7387             :     }
    7388             : 
    7389             :     /* -------------------------------------------------------------------- */
    7390             :     /*      Color profile.  Copy from band metadata if found.              */
    7391             :     /* -------------------------------------------------------------------- */
    7392        2143 :     if (bGeoTIFF)
    7393             :     {
    7394        2126 :         const char *pszOptionsMD[] = {"SOURCE_ICC_PROFILE",
    7395             :                                       "SOURCE_PRIMARIES_RED",
    7396             :                                       "SOURCE_PRIMARIES_GREEN",
    7397             :                                       "SOURCE_PRIMARIES_BLUE",
    7398             :                                       "SOURCE_WHITEPOINT",
    7399             :                                       "TIFFTAG_TRANSFERFUNCTION_RED",
    7400             :                                       "TIFFTAG_TRANSFERFUNCTION_GREEN",
    7401             :                                       "TIFFTAG_TRANSFERFUNCTION_BLUE",
    7402             :                                       "TIFFTAG_TRANSFERRANGE_BLACK",
    7403             :                                       "TIFFTAG_TRANSFERRANGE_WHITE",
    7404             :                                       nullptr};
    7405             : 
    7406             :         // Copy all the tags.  Options will override tags in the source.
    7407        2126 :         int i = 0;
    7408       23366 :         while (pszOptionsMD[i] != nullptr)
    7409             :         {
    7410             :             char const *pszMD =
    7411       21242 :                 CSLFetchNameValue(papszOptions, pszOptionsMD[i]);
    7412       21242 :             if (pszMD == nullptr)
    7413             :                 pszMD =
    7414       21234 :                     poSrcDS->GetMetadataItem(pszOptionsMD[i], "COLOR_PROFILE");
    7415             : 
    7416       21242 :             if ((pszMD != nullptr) && !EQUAL(pszMD, ""))
    7417             :             {
    7418          16 :                 papszCreateOptions =
    7419          16 :                     CSLSetNameValue(papszCreateOptions, pszOptionsMD[i], pszMD);
    7420             : 
    7421             :                 // If an ICC profile exists, other tags are not needed.
    7422          16 :                 if (EQUAL(pszOptionsMD[i], "SOURCE_ICC_PROFILE"))
    7423           2 :                     break;
    7424             :             }
    7425             : 
    7426       21240 :             ++i;
    7427             :         }
    7428             :     }
    7429             : 
    7430        2143 :     double dfExtraSpaceForOverviews = 0;
    7431             :     const bool bCopySrcOverviews =
    7432        2143 :         CPLFetchBool(papszCreateOptions, "COPY_SRC_OVERVIEWS", false);
    7433        2143 :     std::unique_ptr<GDALDataset> poOvrDS;
    7434        2143 :     int nSrcOverviews = 0;
    7435        2143 :     if (bCopySrcOverviews)
    7436             :     {
    7437             :         const char *pszOvrDS =
    7438         228 :             CSLFetchNameValue(papszCreateOptions, "@OVERVIEW_DATASET");
    7439         228 :         if (pszOvrDS)
    7440             :         {
    7441             :             // Empty string is used by COG driver to indicate that we want
    7442             :             // to ignore source overviews.
    7443          37 :             if (!EQUAL(pszOvrDS, ""))
    7444             :             {
    7445          35 :                 poOvrDS.reset(GDALDataset::Open(pszOvrDS));
    7446          35 :                 if (!poOvrDS)
    7447             :                 {
    7448           0 :                     CSLDestroy(papszCreateOptions);
    7449           0 :                     return nullptr;
    7450             :                 }
    7451          35 :                 if (poOvrDS->GetRasterCount() != l_nBands)
    7452             :                 {
    7453           0 :                     CSLDestroy(papszCreateOptions);
    7454           0 :                     return nullptr;
    7455             :                 }
    7456          35 :                 nSrcOverviews =
    7457          35 :                     poOvrDS->GetRasterBand(1)->GetOverviewCount() + 1;
    7458             :             }
    7459             :         }
    7460             :         else
    7461             :         {
    7462         191 :             nSrcOverviews = poSrcDS->GetRasterBand(1)->GetOverviewCount();
    7463             :         }
    7464             : 
    7465             :         // Limit number of overviews if specified
    7466             :         const char *pszOverviewCount =
    7467         228 :             CSLFetchNameValue(papszCreateOptions, "@OVERVIEW_COUNT");
    7468         228 :         if (pszOverviewCount)
    7469           8 :             nSrcOverviews =
    7470           8 :                 std::max(0, std::min(nSrcOverviews, atoi(pszOverviewCount)));
    7471             : 
    7472         228 :         if (nSrcOverviews)
    7473             :         {
    7474         204 :             for (int j = 1; j <= l_nBands; ++j)
    7475             :             {
    7476             :                 const int nOtherBandOverviewCount =
    7477         134 :                     poOvrDS ? poOvrDS->GetRasterBand(j)->GetOverviewCount() + 1
    7478         198 :                             : poSrcDS->GetRasterBand(j)->GetOverviewCount();
    7479         134 :                 if (nOtherBandOverviewCount < nSrcOverviews)
    7480             :                 {
    7481           1 :                     ReportError(
    7482             :                         pszFilename, CE_Failure, CPLE_NotSupported,
    7483             :                         "COPY_SRC_OVERVIEWS cannot be used when the bands have "
    7484             :                         "not the same number of overview levels.");
    7485           1 :                     CSLDestroy(papszCreateOptions);
    7486           1 :                     return nullptr;
    7487             :                 }
    7488         388 :                 for (int i = 0; i < nSrcOverviews; ++i)
    7489             :                 {
    7490             :                     GDALRasterBand *poOvrBand =
    7491             :                         poOvrDS
    7492         353 :                             ? (i == 0 ? poOvrDS->GetRasterBand(j)
    7493         192 :                                       : poOvrDS->GetRasterBand(j)->GetOverview(
    7494          96 :                                             i - 1))
    7495         348 :                             : poSrcDS->GetRasterBand(j)->GetOverview(i);
    7496         257 :                     if (poOvrBand == nullptr)
    7497             :                     {
    7498           1 :                         ReportError(
    7499             :                             pszFilename, CE_Failure, CPLE_NotSupported,
    7500             :                             "COPY_SRC_OVERVIEWS cannot be used when one "
    7501             :                             "overview band is NULL.");
    7502           1 :                         CSLDestroy(papszCreateOptions);
    7503           1 :                         return nullptr;
    7504             :                     }
    7505             :                     GDALRasterBand *poOvrFirstBand =
    7506             :                         poOvrDS
    7507         352 :                             ? (i == 0 ? poOvrDS->GetRasterBand(1)
    7508         192 :                                       : poOvrDS->GetRasterBand(1)->GetOverview(
    7509          96 :                                             i - 1))
    7510         346 :                             : poSrcDS->GetRasterBand(1)->GetOverview(i);
    7511         511 :                     if (poOvrBand->GetXSize() != poOvrFirstBand->GetXSize() ||
    7512         255 :                         poOvrBand->GetYSize() != poOvrFirstBand->GetYSize())
    7513             :                     {
    7514           1 :                         ReportError(
    7515             :                             pszFilename, CE_Failure, CPLE_NotSupported,
    7516             :                             "COPY_SRC_OVERVIEWS cannot be used when the "
    7517             :                             "overview bands have not the same dimensions "
    7518             :                             "among bands.");
    7519           1 :                         CSLDestroy(papszCreateOptions);
    7520           1 :                         return nullptr;
    7521             :                     }
    7522             :                 }
    7523             :             }
    7524             : 
    7525         198 :             for (int i = 0; i < nSrcOverviews; ++i)
    7526             :             {
    7527             :                 GDALRasterBand *poOvrFirstBand =
    7528             :                     poOvrDS
    7529         201 :                         ? (i == 0
    7530          73 :                                ? poOvrDS->GetRasterBand(1)
    7531          38 :                                : poOvrDS->GetRasterBand(1)->GetOverview(i - 1))
    7532         183 :                         : poSrcDS->GetRasterBand(1)->GetOverview(i);
    7533         128 :                 dfExtraSpaceForOverviews +=
    7534         128 :                     static_cast<double>(poOvrFirstBand->GetXSize()) *
    7535         128 :                     poOvrFirstBand->GetYSize();
    7536             :             }
    7537          70 :             dfExtraSpaceForOverviews *=
    7538          70 :                 l_nBands * GDALGetDataTypeSizeBytes(eType);
    7539             :         }
    7540             :         else
    7541             :         {
    7542         155 :             CPLDebug("GTiff", "No source overviews to copy");
    7543             :         }
    7544             :     }
    7545             : 
    7546             : /* -------------------------------------------------------------------- */
    7547             : /*      Should we use optimized way of copying from an input JPEG       */
    7548             : /*      dataset?                                                        */
    7549             : /* -------------------------------------------------------------------- */
    7550             : 
    7551             : // TODO(schwehr): Refactor bDirectCopyFromJPEG to be a const.
    7552             : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
    7553        2140 :     bool bDirectCopyFromJPEG = false;
    7554             : #endif
    7555             : 
    7556             :     // Note: JPEG_DIRECT_COPY is not defined by default, because it is mainly
    7557             :     // useful for debugging purposes.
    7558             : #ifdef JPEG_DIRECT_COPY
    7559             :     if (CPLFetchBool(papszCreateOptions, "JPEG_DIRECT_COPY", false) &&
    7560             :         GTIFF_CanDirectCopyFromJPEG(poSrcDS, papszCreateOptions))
    7561             :     {
    7562             :         CPLDebug("GTiff", "Using special direct copy mode from a JPEG dataset");
    7563             : 
    7564             :         bDirectCopyFromJPEG = true;
    7565             :     }
    7566             : #endif
    7567             : 
    7568             : #ifdef HAVE_LIBJPEG
    7569        2140 :     bool bCopyFromJPEG = false;
    7570             : 
    7571             :     // When CreateCopy'ing() from a JPEG dataset, and asking for COMPRESS=JPEG,
    7572             :     // use DCT coefficients (unless other options are incompatible, like
    7573             :     // strip/tile dimensions, specifying JPEG_QUALITY option, incompatible
    7574             :     // PHOTOMETRIC with the source colorspace, etc.) to avoid the lossy steps
    7575             :     // involved by decompression/recompression.
    7576        4280 :     if (!bDirectCopyFromJPEG &&
    7577        2140 :         GTIFF_CanCopyFromJPEG(poSrcDS, papszCreateOptions))
    7578             :     {
    7579          12 :         CPLDebug("GTiff", "Using special copy mode from a JPEG dataset");
    7580             : 
    7581          12 :         bCopyFromJPEG = true;
    7582             :     }
    7583             : #endif
    7584             : 
    7585             :     /* -------------------------------------------------------------------- */
    7586             :     /*      If the source is RGB, then set the PHOTOMETRIC=RGB value        */
    7587             :     /* -------------------------------------------------------------------- */
    7588             : 
    7589             :     const bool bForcePhotometric =
    7590        2140 :         CSLFetchNameValue(papszOptions, "PHOTOMETRIC") != nullptr;
    7591             : 
    7592        1225 :     if (l_nBands >= 3 && !bForcePhotometric &&
    7593             : #ifdef HAVE_LIBJPEG
    7594        1187 :         !bCopyFromJPEG &&
    7595             : #endif
    7596        1181 :         poSrcDS->GetRasterBand(1)->GetColorInterpretation() == GCI_RedBand &&
    7597        4434 :         poSrcDS->GetRasterBand(2)->GetColorInterpretation() == GCI_GreenBand &&
    7598        1069 :         poSrcDS->GetRasterBand(3)->GetColorInterpretation() == GCI_BlueBand)
    7599             :     {
    7600        1063 :         papszCreateOptions =
    7601        1063 :             CSLSetNameValue(papszCreateOptions, "PHOTOMETRIC", "RGB");
    7602             :     }
    7603             : 
    7604             :     /* -------------------------------------------------------------------- */
    7605             :     /*      Create the file.                                                */
    7606             :     /* -------------------------------------------------------------------- */
    7607        2140 :     VSILFILE *l_fpL = nullptr;
    7608        4280 :     CPLString l_osTmpFilename;
    7609             : 
    7610        2140 :     const int nXSize = poSrcDS->GetRasterXSize();
    7611        2140 :     const int nYSize = poSrcDS->GetRasterYSize();
    7612             : 
    7613             :     const int nColorTableMultiplier = std::max(
    7614        4280 :         1,
    7615        4280 :         std::min(257,
    7616        2140 :                  atoi(CSLFetchNameValueDef(
    7617             :                      papszOptions, "COLOR_TABLE_MULTIPLIER",
    7618        2140 :                      CPLSPrintf("%d", DEFAULT_COLOR_TABLE_MULTIPLIER_257)))));
    7619             : 
    7620        2140 :     bool bTileInterleaving = false;
    7621        2140 :     TIFF *l_hTIFF = CreateLL(pszFilename, nXSize, nYSize, l_nBands, eType,
    7622             :                              dfExtraSpaceForOverviews, nColorTableMultiplier,
    7623             :                              papszCreateOptions, &l_fpL, l_osTmpFilename,
    7624             :                              /* bCreateCopy = */ true, bTileInterleaving);
    7625        2140 :     const bool bStreaming = !l_osTmpFilename.empty();
    7626             : 
    7627        2140 :     CSLDestroy(papszCreateOptions);
    7628        2140 :     papszCreateOptions = nullptr;
    7629             : 
    7630        2140 :     if (l_hTIFF == nullptr)
    7631             :     {
    7632          18 :         if (bStreaming)
    7633           0 :             VSIUnlink(l_osTmpFilename);
    7634          18 :         return nullptr;
    7635             :     }
    7636             : 
    7637        2122 :     uint16_t l_nPlanarConfig = 0;
    7638        2122 :     TIFFGetField(l_hTIFF, TIFFTAG_PLANARCONFIG, &l_nPlanarConfig);
    7639             : 
    7640        2122 :     uint16_t l_nCompression = 0;
    7641             : 
    7642        2122 :     if (!TIFFGetField(l_hTIFF, TIFFTAG_COMPRESSION, &(l_nCompression)))
    7643           0 :         l_nCompression = COMPRESSION_NONE;
    7644             : 
    7645             :     /* -------------------------------------------------------------------- */
    7646             :     /*      Set the alpha channel if we find one.                           */
    7647             :     /* -------------------------------------------------------------------- */
    7648        2122 :     uint16_t *extraSamples = nullptr;
    7649        2122 :     uint16_t nExtraSamples = 0;
    7650        2122 :     if (TIFFGetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, &nExtraSamples,
    7651        2387 :                      &extraSamples) &&
    7652         265 :         nExtraSamples > 0)
    7653             :     {
    7654             :         // We need to allocate a new array as (current) libtiff
    7655             :         // versions will not like that we reuse the array we got from
    7656             :         // TIFFGetField().
    7657             :         uint16_t *pasNewExtraSamples = static_cast<uint16_t *>(
    7658         265 :             CPLMalloc(nExtraSamples * sizeof(uint16_t)));
    7659         265 :         memcpy(pasNewExtraSamples, extraSamples,
    7660         265 :                nExtraSamples * sizeof(uint16_t));
    7661         265 :         const char *pszAlpha = CPLGetConfigOption(
    7662             :             "GTIFF_ALPHA", CSLFetchNameValue(papszOptions, "ALPHA"));
    7663             :         const uint16_t nAlpha =
    7664         265 :             GTiffGetAlphaValue(pszAlpha, DEFAULT_ALPHA_TYPE);
    7665         265 :         const int nBaseSamples = l_nBands - nExtraSamples;
    7666         895 :         for (int iExtraBand = nBaseSamples + 1; iExtraBand <= l_nBands;
    7667             :              iExtraBand++)
    7668             :         {
    7669         630 :             if (poSrcDS->GetRasterBand(iExtraBand)->GetColorInterpretation() ==
    7670             :                 GCI_AlphaBand)
    7671             :             {
    7672         145 :                 pasNewExtraSamples[iExtraBand - nBaseSamples - 1] = nAlpha;
    7673         145 :                 if (!pszAlpha)
    7674             :                 {
    7675             :                     // Use the ALPHA metadata item from the source band, when
    7676             :                     // present, if no explicit ALPHA creation option
    7677         286 :                     pasNewExtraSamples[iExtraBand - nBaseSamples - 1] =
    7678         143 :                         GTiffGetAlphaValue(
    7679         143 :                             poSrcDS->GetRasterBand(iExtraBand)
    7680         143 :                                 ->GetMetadataItem("ALPHA", "IMAGE_STRUCTURE"),
    7681             :                             nAlpha);
    7682             :                 }
    7683             :             }
    7684             :         }
    7685         265 :         TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, nExtraSamples,
    7686             :                      pasNewExtraSamples);
    7687             : 
    7688         265 :         CPLFree(pasNewExtraSamples);
    7689             :     }
    7690             : 
    7691             :     /* -------------------------------------------------------------------- */
    7692             :     /*      If the output is jpeg compressed, and the input is RGB make     */
    7693             :     /*      sure we note that.                                              */
    7694             :     /* -------------------------------------------------------------------- */
    7695             : 
    7696        2122 :     if (l_nCompression == COMPRESSION_JPEG)
    7697             :     {
    7698         134 :         if (l_nBands >= 3 &&
    7699          58 :             (poSrcDS->GetRasterBand(1)->GetColorInterpretation() ==
    7700           0 :              GCI_YCbCr_YBand) &&
    7701           0 :             (poSrcDS->GetRasterBand(2)->GetColorInterpretation() ==
    7702         134 :              GCI_YCbCr_CbBand) &&
    7703           0 :             (poSrcDS->GetRasterBand(3)->GetColorInterpretation() ==
    7704             :              GCI_YCbCr_CrBand))
    7705             :         {
    7706             :             // Do nothing.
    7707             :         }
    7708             :         else
    7709             :         {
    7710             :             // Assume RGB if it is not explicitly YCbCr.
    7711          76 :             CPLDebug("GTiff", "Setting JPEGCOLORMODE_RGB");
    7712          76 :             TIFFSetField(l_hTIFF, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
    7713             :         }
    7714             :     }
    7715             : 
    7716             :     /* -------------------------------------------------------------------- */
    7717             :     /*      Does the source image consist of one band, with a palette?      */
    7718             :     /*      If so, copy over.                                               */
    7719             :     /* -------------------------------------------------------------------- */
    7720        1311 :     if ((l_nBands == 1 || l_nBands == 2) &&
    7721        3433 :         poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr &&
    7722             :         eType == GDT_UInt8)
    7723             :     {
    7724          21 :         unsigned short anTRed[256] = {0};
    7725          21 :         unsigned short anTGreen[256] = {0};
    7726          21 :         unsigned short anTBlue[256] = {0};
    7727          21 :         GDALColorTable *poCT = poSrcDS->GetRasterBand(1)->GetColorTable();
    7728             : 
    7729        5397 :         for (int iColor = 0; iColor < 256; ++iColor)
    7730             :         {
    7731        5376 :             if (iColor < poCT->GetColorEntryCount())
    7732             :             {
    7733        4241 :                 GDALColorEntry sRGB = {0, 0, 0, 0};
    7734             : 
    7735        4241 :                 poCT->GetColorEntryAsRGB(iColor, &sRGB);
    7736             : 
    7737        8482 :                 anTRed[iColor] = GTiffDataset::ClampCTEntry(
    7738        4241 :                     iColor, 1, sRGB.c1, nColorTableMultiplier);
    7739        8482 :                 anTGreen[iColor] = GTiffDataset::ClampCTEntry(
    7740        4241 :                     iColor, 2, sRGB.c2, nColorTableMultiplier);
    7741        4241 :                 anTBlue[iColor] = GTiffDataset::ClampCTEntry(
    7742        4241 :                     iColor, 3, sRGB.c3, nColorTableMultiplier);
    7743             :             }
    7744             :             else
    7745             :             {
    7746        1135 :                 anTRed[iColor] = 0;
    7747        1135 :                 anTGreen[iColor] = 0;
    7748        1135 :                 anTBlue[iColor] = 0;
    7749             :             }
    7750             :         }
    7751             : 
    7752          21 :         if (!bForcePhotometric)
    7753          21 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
    7754          21 :         TIFFSetField(l_hTIFF, TIFFTAG_COLORMAP, anTRed, anTGreen, anTBlue);
    7755             :     }
    7756        1310 :     else if ((l_nBands == 1 || l_nBands == 2) &&
    7757        3411 :              poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr &&
    7758             :              eType == GDT_UInt16)
    7759             :     {
    7760             :         unsigned short *panTRed = static_cast<unsigned short *>(
    7761           1 :             CPLMalloc(65536 * sizeof(unsigned short)));
    7762             :         unsigned short *panTGreen = static_cast<unsigned short *>(
    7763           1 :             CPLMalloc(65536 * sizeof(unsigned short)));
    7764             :         unsigned short *panTBlue = static_cast<unsigned short *>(
    7765           1 :             CPLMalloc(65536 * sizeof(unsigned short)));
    7766             : 
    7767           1 :         GDALColorTable *poCT = poSrcDS->GetRasterBand(1)->GetColorTable();
    7768             : 
    7769       65537 :         for (int iColor = 0; iColor < 65536; ++iColor)
    7770             :         {
    7771       65536 :             if (iColor < poCT->GetColorEntryCount())
    7772             :             {
    7773       65536 :                 GDALColorEntry sRGB = {0, 0, 0, 0};
    7774             : 
    7775       65536 :                 poCT->GetColorEntryAsRGB(iColor, &sRGB);
    7776             : 
    7777      131072 :                 panTRed[iColor] = GTiffDataset::ClampCTEntry(
    7778       65536 :                     iColor, 1, sRGB.c1, nColorTableMultiplier);
    7779      131072 :                 panTGreen[iColor] = GTiffDataset::ClampCTEntry(
    7780       65536 :                     iColor, 2, sRGB.c2, nColorTableMultiplier);
    7781       65536 :                 panTBlue[iColor] = GTiffDataset::ClampCTEntry(
    7782       65536 :                     iColor, 3, sRGB.c3, nColorTableMultiplier);
    7783             :             }
    7784             :             else
    7785             :             {
    7786           0 :                 panTRed[iColor] = 0;
    7787           0 :                 panTGreen[iColor] = 0;
    7788           0 :                 panTBlue[iColor] = 0;
    7789             :             }
    7790             :         }
    7791             : 
    7792           1 :         if (!bForcePhotometric)
    7793           1 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
    7794           1 :         TIFFSetField(l_hTIFF, TIFFTAG_COLORMAP, panTRed, panTGreen, panTBlue);
    7795             : 
    7796           1 :         CPLFree(panTRed);
    7797           1 :         CPLFree(panTGreen);
    7798           1 :         CPLFree(panTBlue);
    7799             :     }
    7800        2100 :     else if (poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr)
    7801           1 :         ReportError(
    7802             :             pszFilename, CE_Failure, CPLE_AppDefined,
    7803             :             "Unable to export color table to GeoTIFF file.  Color tables "
    7804             :             "can only be written to 1 band or 2 bands Byte or "
    7805             :             "UInt16 GeoTIFF files.");
    7806             : 
    7807        2122 :     if (l_nCompression == COMPRESSION_JPEG)
    7808             :     {
    7809          76 :         uint16_t l_nPhotometric = 0;
    7810          76 :         TIFFGetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, &l_nPhotometric);
    7811             :         // Check done in tif_jpeg.c later, but not with a very clear error
    7812             :         // message
    7813          76 :         if (l_nPhotometric == PHOTOMETRIC_PALETTE)
    7814             :         {
    7815           1 :             ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    7816             :                         "JPEG compression not supported with paletted image");
    7817           1 :             XTIFFClose(l_hTIFF);
    7818           1 :             VSIUnlink(l_osTmpFilename);
    7819           1 :             CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    7820           1 :             return nullptr;
    7821             :         }
    7822             :     }
    7823             : 
    7824        2208 :     if (l_nBands == 2 &&
    7825        2121 :         poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr &&
    7826           0 :         (eType == GDT_UInt8 || eType == GDT_UInt16))
    7827             :     {
    7828           1 :         uint16_t v[1] = {EXTRASAMPLE_UNASSALPHA};
    7829             : 
    7830           1 :         TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, 1, v);
    7831             :     }
    7832             : 
    7833        2121 :     const int nMaskFlags = poSrcDS->GetRasterBand(1)->GetMaskFlags();
    7834        2121 :     bool bCreateMask = false;
    7835        4242 :     CPLString osHiddenStructuralMD;
    7836             :     const char *pszInterleave =
    7837        2121 :         CSLFetchNameValueDef(papszOptions, "INTERLEAVE", "PIXEL");
    7838        2343 :     if (bCopySrcOverviews &&
    7839         222 :         CPLTestBool(CSLFetchNameValueDef(papszOptions, "TILED", "NO")))
    7840             :     {
    7841         210 :         osHiddenStructuralMD += "LAYOUT=IFDS_BEFORE_DATA\n";
    7842         210 :         osHiddenStructuralMD += "BLOCK_ORDER=ROW_MAJOR\n";
    7843         210 :         osHiddenStructuralMD += "BLOCK_LEADER=SIZE_AS_UINT4\n";
    7844         210 :         osHiddenStructuralMD += "BLOCK_TRAILER=LAST_4_BYTES_REPEATED\n";
    7845         210 :         if (l_nBands > 1 && !EQUAL(pszInterleave, "PIXEL"))
    7846             :         {
    7847          21 :             osHiddenStructuralMD += "INTERLEAVE=";
    7848          21 :             osHiddenStructuralMD += CPLString(pszInterleave).toupper();
    7849          21 :             osHiddenStructuralMD += "\n";
    7850             :         }
    7851             :         osHiddenStructuralMD +=
    7852         210 :             "KNOWN_INCOMPATIBLE_EDITION=NO\n ";  // Final space intended, so
    7853             :                                                  // this can be replaced by YES
    7854             :     }
    7855        2121 :     if (!(nMaskFlags & (GMF_ALL_VALID | GMF_ALPHA | GMF_NODATA)) &&
    7856          42 :         (nMaskFlags & GMF_PER_DATASET) && !bStreaming)
    7857             :     {
    7858          38 :         bCreateMask = true;
    7859          38 :         if (GTiffDataset::MustCreateInternalMask() &&
    7860          38 :             !osHiddenStructuralMD.empty() && EQUAL(pszInterleave, "PIXEL"))
    7861             :         {
    7862          21 :             osHiddenStructuralMD += "MASK_INTERLEAVED_WITH_IMAGERY=YES\n";
    7863             :         }
    7864             :     }
    7865        2331 :     if (!osHiddenStructuralMD.empty() &&
    7866         210 :         CPLTestBool(CPLGetConfigOption("GTIFF_WRITE_COG_GHOST_AREA", "YES")))
    7867             :     {
    7868         209 :         const int nHiddenMDSize = static_cast<int>(osHiddenStructuralMD.size());
    7869             :         osHiddenStructuralMD =
    7870         209 :             CPLOPrintf("GDAL_STRUCTURAL_METADATA_SIZE=%06d bytes\n",
    7871         418 :                        nHiddenMDSize) +
    7872         209 :             osHiddenStructuralMD;
    7873         209 :         VSI_TIFFWrite(l_hTIFF, osHiddenStructuralMD.c_str(),
    7874             :                       osHiddenStructuralMD.size());
    7875             :     }
    7876             : 
    7877             :     // FIXME? libtiff writes extended tags in the order they are specified
    7878             :     // and not in increasing order.
    7879             : 
    7880             :     /* -------------------------------------------------------------------- */
    7881             :     /*      Transfer some TIFF specific metadata, if available.             */
    7882             :     /*      The return value will tell us if we need to try again later with*/
    7883             :     /*      PAM because the profile doesn't allow to write some metadata    */
    7884             :     /*      as TIFF tag                                                     */
    7885             :     /* -------------------------------------------------------------------- */
    7886        2121 :     const bool bHasWrittenMDInGeotiffTAG = GTiffDataset::WriteMetadata(
    7887             :         poSrcDS, l_hTIFF, false, eProfile, pszFilename, papszOptions);
    7888             : 
    7889             :     /* -------------------------------------------------------------------- */
    7890             :     /*      Write NoData value, if exist.                                   */
    7891             :     /* -------------------------------------------------------------------- */
    7892        2121 :     if (eProfile == GTiffProfile::GDALGEOTIFF)
    7893             :     {
    7894        2100 :         int bSuccess = FALSE;
    7895        2100 :         GDALRasterBand *poFirstBand = poSrcDS->GetRasterBand(1);
    7896        2100 :         if (poFirstBand->GetRasterDataType() == GDT_Int64)
    7897             :         {
    7898           4 :             const auto nNoData = poFirstBand->GetNoDataValueAsInt64(&bSuccess);
    7899           4 :             if (bSuccess)
    7900           1 :                 GTiffDataset::WriteNoDataValue(l_hTIFF, nNoData);
    7901             :         }
    7902        2096 :         else if (poFirstBand->GetRasterDataType() == GDT_UInt64)
    7903             :         {
    7904           4 :             const auto nNoData = poFirstBand->GetNoDataValueAsUInt64(&bSuccess);
    7905           4 :             if (bSuccess)
    7906           1 :                 GTiffDataset::WriteNoDataValue(l_hTIFF, nNoData);
    7907             :         }
    7908             :         else
    7909             :         {
    7910        2092 :             const auto dfNoData = poFirstBand->GetNoDataValue(&bSuccess);
    7911        2092 :             if (bSuccess)
    7912         145 :                 GTiffDataset::WriteNoDataValue(l_hTIFF, dfNoData);
    7913             :         }
    7914             :     }
    7915             : 
    7916             :     /* -------------------------------------------------------------------- */
    7917             :     /*      Are we addressing PixelIsPoint mode?                            */
    7918             :     /* -------------------------------------------------------------------- */
    7919        2121 :     bool bPixelIsPoint = false;
    7920        2121 :     bool bPointGeoIgnore = false;
    7921             : 
    7922        3555 :     if (poSrcDS->GetMetadataItem(GDALMD_AREA_OR_POINT) &&
    7923        1434 :         EQUAL(poSrcDS->GetMetadataItem(GDALMD_AREA_OR_POINT), GDALMD_AOP_POINT))
    7924             :     {
    7925          10 :         bPixelIsPoint = true;
    7926             :         bPointGeoIgnore =
    7927          10 :             CPLTestBool(CPLGetConfigOption("GTIFF_POINT_GEO_IGNORE", "FALSE"));
    7928             :     }
    7929             : 
    7930             :     /* -------------------------------------------------------------------- */
    7931             :     /*      Write affine transform if it is meaningful.                     */
    7932             :     /* -------------------------------------------------------------------- */
    7933        2121 :     const OGRSpatialReference *l_poSRS = nullptr;
    7934        2121 :     GDALGeoTransform l_gt;
    7935        2121 :     if (poSrcDS->GetGeoTransform(l_gt) == CE_None)
    7936             :     {
    7937        1681 :         if (bGeoTIFF)
    7938             :         {
    7939        1676 :             l_poSRS = poSrcDS->GetSpatialRef();
    7940             : 
    7941        1676 :             if (l_gt.xrot == 0.0 && l_gt.yrot == 0.0 && l_gt.yscale < 0.0)
    7942             :             {
    7943        1668 :                 double dfOffset = 0.0;
    7944             :                 {
    7945             :                     // In the case the SRS has a vertical component and we have
    7946             :                     // a single band, encode its scale/offset in the GeoTIFF
    7947             :                     // tags
    7948        1668 :                     int bHasScale = FALSE;
    7949             :                     double dfScale =
    7950        1668 :                         poSrcDS->GetRasterBand(1)->GetScale(&bHasScale);
    7951        1668 :                     int bHasOffset = FALSE;
    7952             :                     dfOffset =
    7953        1668 :                         poSrcDS->GetRasterBand(1)->GetOffset(&bHasOffset);
    7954             :                     const bool bApplyScaleOffset =
    7955        1672 :                         l_poSRS && l_poSRS->IsVertical() &&
    7956           4 :                         poSrcDS->GetRasterCount() == 1;
    7957        1668 :                     if (bApplyScaleOffset && !bHasScale)
    7958           0 :                         dfScale = 1.0;
    7959        1668 :                     if (!bApplyScaleOffset || !bHasOffset)
    7960        1664 :                         dfOffset = 0.0;
    7961             :                     const double adfPixelScale[3] = {
    7962        1668 :                         l_gt.xscale, fabs(l_gt.yscale),
    7963        1668 :                         bApplyScaleOffset ? dfScale : 0.0};
    7964             : 
    7965        1668 :                     TIFFSetField(l_hTIFF, TIFFTAG_GEOPIXELSCALE, 3,
    7966             :                                  adfPixelScale);
    7967             :                 }
    7968             : 
    7969        1668 :                 double adfTiePoints[6] = {0.0,        0.0,        0.0,
    7970        1668 :                                           l_gt.xorig, l_gt.yorig, dfOffset};
    7971             : 
    7972        1668 :                 if (bPixelIsPoint && !bPointGeoIgnore)
    7973             :                 {
    7974           6 :                     adfTiePoints[3] += l_gt.xscale * 0.5 + l_gt.xrot * 0.5;
    7975           6 :                     adfTiePoints[4] += l_gt.yrot * 0.5 + l_gt.yscale * 0.5;
    7976             :                 }
    7977             : 
    7978        1668 :                 TIFFSetField(l_hTIFF, TIFFTAG_GEOTIEPOINTS, 6, adfTiePoints);
    7979             :             }
    7980             :             else
    7981             :             {
    7982           8 :                 double adfMatrix[16] = {0.0};
    7983             : 
    7984           8 :                 adfMatrix[0] = l_gt.xscale;
    7985           8 :                 adfMatrix[1] = l_gt.xrot;
    7986           8 :                 adfMatrix[3] = l_gt.xorig;
    7987           8 :                 adfMatrix[4] = l_gt.yrot;
    7988           8 :                 adfMatrix[5] = l_gt.yscale;
    7989           8 :                 adfMatrix[7] = l_gt.yorig;
    7990           8 :                 adfMatrix[15] = 1.0;
    7991             : 
    7992           8 :                 if (bPixelIsPoint && !bPointGeoIgnore)
    7993             :                 {
    7994           0 :                     adfMatrix[3] += l_gt.xscale * 0.5 + l_gt.xrot * 0.5;
    7995           0 :                     adfMatrix[7] += l_gt.yrot * 0.5 + l_gt.yscale * 0.5;
    7996             :                 }
    7997             : 
    7998           8 :                 TIFFSetField(l_hTIFF, TIFFTAG_GEOTRANSMATRIX, 16, adfMatrix);
    7999             :             }
    8000             :         }
    8001             : 
    8002             :         /* --------------------------------------------------------------------
    8003             :          */
    8004             :         /*      Do we need a TFW file? */
    8005             :         /* --------------------------------------------------------------------
    8006             :          */
    8007        1681 :         if (CPLFetchBool(papszOptions, "TFW", false))
    8008           2 :             GDALWriteWorldFile(pszFilename, "tfw", l_gt.data());
    8009        1679 :         else if (CPLFetchBool(papszOptions, "WORLDFILE", false))
    8010           1 :             GDALWriteWorldFile(pszFilename, "wld", l_gt.data());
    8011             :     }
    8012             : 
    8013             :     /* -------------------------------------------------------------------- */
    8014             :     /*      Otherwise write tiepoints if they are available.                */
    8015             :     /* -------------------------------------------------------------------- */
    8016         440 :     else if (poSrcDS->GetGCPCount() > 0 && bGeoTIFF)
    8017             :     {
    8018          12 :         const GDAL_GCP *pasGCPs = poSrcDS->GetGCPs();
    8019             :         double *padfTiePoints = static_cast<double *>(
    8020          12 :             CPLMalloc(6 * sizeof(double) * poSrcDS->GetGCPCount()));
    8021             : 
    8022          60 :         for (int iGCP = 0; iGCP < poSrcDS->GetGCPCount(); ++iGCP)
    8023             :         {
    8024             : 
    8025          48 :             padfTiePoints[iGCP * 6 + 0] = pasGCPs[iGCP].dfGCPPixel;
    8026          48 :             padfTiePoints[iGCP * 6 + 1] = pasGCPs[iGCP].dfGCPLine;
    8027          48 :             padfTiePoints[iGCP * 6 + 2] = 0;
    8028          48 :             padfTiePoints[iGCP * 6 + 3] = pasGCPs[iGCP].dfGCPX;
    8029          48 :             padfTiePoints[iGCP * 6 + 4] = pasGCPs[iGCP].dfGCPY;
    8030          48 :             padfTiePoints[iGCP * 6 + 5] = pasGCPs[iGCP].dfGCPZ;
    8031             : 
    8032          48 :             if (bPixelIsPoint && !bPointGeoIgnore)
    8033             :             {
    8034           4 :                 padfTiePoints[iGCP * 6 + 0] -= 0.5;
    8035           4 :                 padfTiePoints[iGCP * 6 + 1] -= 0.5;
    8036             :             }
    8037             :         }
    8038             : 
    8039          12 :         TIFFSetField(l_hTIFF, TIFFTAG_GEOTIEPOINTS, 6 * poSrcDS->GetGCPCount(),
    8040             :                      padfTiePoints);
    8041          12 :         CPLFree(padfTiePoints);
    8042             : 
    8043          12 :         l_poSRS = poSrcDS->GetGCPSpatialRef();
    8044             : 
    8045          24 :         if (CPLFetchBool(papszOptions, "TFW", false) ||
    8046          12 :             CPLFetchBool(papszOptions, "WORLDFILE", false))
    8047             :         {
    8048           0 :             ReportError(
    8049             :                 pszFilename, CE_Warning, CPLE_AppDefined,
    8050             :                 "TFW=ON or WORLDFILE=ON creation options are ignored when "
    8051             :                 "GCPs are available");
    8052             :         }
    8053             :     }
    8054             :     else
    8055             :     {
    8056         428 :         l_poSRS = poSrcDS->GetSpatialRef();
    8057             :     }
    8058             : 
    8059             :     /* -------------------------------------------------------------------- */
    8060             :     /*      Copy xml:XMP data                                               */
    8061             :     /* -------------------------------------------------------------------- */
    8062        2121 :     CSLConstList papszXMP = poSrcDS->GetMetadata("xml:XMP");
    8063        2121 :     if (papszXMP != nullptr && *papszXMP != nullptr)
    8064             :     {
    8065           9 :         int nTagSize = static_cast<int>(strlen(*papszXMP));
    8066           9 :         TIFFSetField(l_hTIFF, TIFFTAG_XMLPACKET, nTagSize, *papszXMP);
    8067             :     }
    8068             : 
    8069             :     /* -------------------------------------------------------------------- */
    8070             :     /*      Write the projection information, if possible.                  */
    8071             :     /* -------------------------------------------------------------------- */
    8072        2121 :     const bool bHasProjection = l_poSRS != nullptr;
    8073        2121 :     bool bExportSRSToPAM = false;
    8074        2121 :     if ((bHasProjection || bPixelIsPoint) && bGeoTIFF)
    8075             :     {
    8076        1654 :         GTIF *psGTIF = GTiffDataset::GTIFNew(l_hTIFF);
    8077             : 
    8078        1654 :         if (bHasProjection)
    8079             :         {
    8080        1654 :             const auto eGeoTIFFKeysFlavor = GetGTIFFKeysFlavor(papszOptions);
    8081        1654 :             if (IsSRSCompatibleOfGeoTIFF(l_poSRS, eGeoTIFFKeysFlavor))
    8082             :             {
    8083        1654 :                 GTIFSetFromOGISDefnEx(
    8084             :                     psGTIF,
    8085             :                     OGRSpatialReference::ToHandle(
    8086             :                         const_cast<OGRSpatialReference *>(l_poSRS)),
    8087             :                     eGeoTIFFKeysFlavor, GetGeoTIFFVersion(papszOptions));
    8088             :             }
    8089             :             else
    8090             :             {
    8091           0 :                 bExportSRSToPAM = true;
    8092             :             }
    8093             :         }
    8094             : 
    8095        1654 :         if (bPixelIsPoint)
    8096             :         {
    8097          10 :             GTIFKeySet(psGTIF, GTRasterTypeGeoKey, TYPE_SHORT, 1,
    8098             :                        RasterPixelIsPoint);
    8099             :         }
    8100             : 
    8101        1654 :         GTIFWriteKeys(psGTIF);
    8102        1654 :         GTIFFree(psGTIF);
    8103             :     }
    8104             : 
    8105        2121 :     bool l_bDontReloadFirstBlock = false;
    8106             : 
    8107             : #ifdef HAVE_LIBJPEG
    8108        2121 :     if (bCopyFromJPEG)
    8109             :     {
    8110          12 :         GTIFF_CopyFromJPEG_WriteAdditionalTags(l_hTIFF, poSrcDS);
    8111             :     }
    8112             : #endif
    8113             : 
    8114             :     /* -------------------------------------------------------------------- */
    8115             :     /*      Cleanup                                                         */
    8116             :     /* -------------------------------------------------------------------- */
    8117        2121 :     if (bCopySrcOverviews)
    8118             :     {
    8119         222 :         TIFFDeferStrileArrayWriting(l_hTIFF);
    8120             :     }
    8121        2121 :     TIFFWriteCheck(l_hTIFF, TIFFIsTiled(l_hTIFF), "GTiffCreateCopy()");
    8122        2121 :     TIFFWriteDirectory(l_hTIFF);
    8123        2121 :     if (bStreaming)
    8124             :     {
    8125             :         // We need to write twice the directory to be sure that custom
    8126             :         // TIFF tags are correctly sorted and that padding bytes have been
    8127             :         // added.
    8128           5 :         TIFFSetDirectory(l_hTIFF, 0);
    8129           5 :         TIFFWriteDirectory(l_hTIFF);
    8130             : 
    8131           5 :         if (VSIFSeekL(l_fpL, 0, SEEK_END) != 0)
    8132           0 :             ReportError(pszFilename, CE_Failure, CPLE_FileIO, "Cannot seek");
    8133           5 :         const int nSize = static_cast<int>(VSIFTellL(l_fpL));
    8134             : 
    8135           5 :         vsi_l_offset nDataLength = 0;
    8136           5 :         VSIGetMemFileBuffer(l_osTmpFilename, &nDataLength, FALSE);
    8137           5 :         TIFFSetDirectory(l_hTIFF, 0);
    8138           5 :         GTiffFillStreamableOffsetAndCount(l_hTIFF, nSize);
    8139           5 :         TIFFWriteDirectory(l_hTIFF);
    8140             :     }
    8141        2121 :     const auto nDirCount = TIFFNumberOfDirectories(l_hTIFF);
    8142        2121 :     if (nDirCount >= 1)
    8143             :     {
    8144        2114 :         TIFFSetDirectory(l_hTIFF, static_cast<tdir_t>(nDirCount - 1));
    8145             :     }
    8146        2121 :     const toff_t l_nDirOffset = TIFFCurrentDirOffset(l_hTIFF);
    8147        2121 :     TIFFFlush(l_hTIFF);
    8148        2121 :     XTIFFClose(l_hTIFF);
    8149             : 
    8150        2121 :     VSIFSeekL(l_fpL, 0, SEEK_SET);
    8151             : 
    8152             :     // fpStreaming will assigned to the instance and not closed here.
    8153        2121 :     VSILFILE *fpStreaming = nullptr;
    8154        2121 :     if (bStreaming)
    8155             :     {
    8156           5 :         vsi_l_offset nDataLength = 0;
    8157             :         void *pabyBuffer =
    8158           5 :             VSIGetMemFileBuffer(l_osTmpFilename, &nDataLength, FALSE);
    8159           5 :         fpStreaming = VSIFOpenL(pszFilename, "wb");
    8160           5 :         if (fpStreaming == nullptr)
    8161             :         {
    8162           1 :             VSIUnlink(l_osTmpFilename);
    8163           1 :             CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    8164           1 :             return nullptr;
    8165             :         }
    8166           4 :         if (static_cast<vsi_l_offset>(VSIFWriteL(pabyBuffer, 1,
    8167             :                                                  static_cast<int>(nDataLength),
    8168           4 :                                                  fpStreaming)) != nDataLength)
    8169             :         {
    8170           0 :             ReportError(pszFilename, CE_Failure, CPLE_FileIO,
    8171             :                         "Could not write %d bytes",
    8172             :                         static_cast<int>(nDataLength));
    8173           0 :             CPL_IGNORE_RET_VAL(VSIFCloseL(fpStreaming));
    8174           0 :             VSIUnlink(l_osTmpFilename);
    8175           0 :             CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    8176           0 :             return nullptr;
    8177             :         }
    8178             :     }
    8179             : 
    8180             :     /* -------------------------------------------------------------------- */
    8181             :     /*      Re-open as a dataset and copy over missing metadata using       */
    8182             :     /*      PAM facilities.                                                 */
    8183             :     /* -------------------------------------------------------------------- */
    8184        2120 :     l_hTIFF = VSI_TIFFOpen(bStreaming ? l_osTmpFilename.c_str() : pszFilename,
    8185             :                            "r+", l_fpL);
    8186        2120 :     if (l_hTIFF == nullptr)
    8187             :     {
    8188          11 :         if (bStreaming)
    8189           0 :             VSIUnlink(l_osTmpFilename);
    8190          11 :         l_fpL->CancelCreation();
    8191          11 :         CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    8192          11 :         return nullptr;
    8193             :     }
    8194             : 
    8195             :     /* -------------------------------------------------------------------- */
    8196             :     /*      Create a corresponding GDALDataset.                             */
    8197             :     /* -------------------------------------------------------------------- */
    8198        4218 :     auto poDS = std::make_unique<GTiffDataset>();
    8199             :     const bool bSuppressASAP =
    8200        2109 :         CPLTestBool(CSLFetchNameValueDef(papszOptions, "@SUPPRESS_ASAP", "NO"));
    8201        2109 :     if (bSuppressASAP)
    8202           4 :         poDS->MarkSuppressOnClose();
    8203        2109 :     poDS->SetDescription(pszFilename);
    8204        2109 :     poDS->eAccess = GA_Update;
    8205        2109 :     poDS->m_osFilename = pszFilename;
    8206        2109 :     poDS->m_fpL = l_fpL;
    8207        2109 :     poDS->m_bIMDRPCMetadataLoaded = true;
    8208        2109 :     poDS->m_nColorTableMultiplier = nColorTableMultiplier;
    8209        2109 :     poDS->m_bTileInterleave = bTileInterleaving;
    8210             : 
    8211        2109 :     if (bTileInterleaving)
    8212             :     {
    8213           7 :         poDS->m_oGTiffMDMD.SetMetadataItem("INTERLEAVE", "TILE",
    8214             :                                            "IMAGE_STRUCTURE");
    8215             :     }
    8216             : 
    8217        2109 :     const bool bAppend = CPLFetchBool(papszOptions, "APPEND_SUBDATASET", false);
    8218        4217 :     if (poDS->OpenOffset(l_hTIFF,
    8219        2108 :                          bAppend ? l_nDirOffset : TIFFCurrentDirOffset(l_hTIFF),
    8220             :                          GA_Update,
    8221             :                          false,  // bAllowRGBAInterface
    8222             :                          true    // bReadGeoTransform
    8223        2109 :                          ) != CE_None)
    8224             :     {
    8225           0 :         l_fpL->CancelCreation();
    8226           0 :         poDS.reset();
    8227           0 :         if (bStreaming)
    8228           0 :             VSIUnlink(l_osTmpFilename);
    8229           0 :         return nullptr;
    8230             :     }
    8231             : 
    8232             :     // Legacy... Patch back GDT_Int8 type to GDT_UInt8 if the user used
    8233             :     // PIXELTYPE=SIGNEDBYTE
    8234        2109 :     const char *pszPixelType = CSLFetchNameValue(papszOptions, "PIXELTYPE");
    8235        2109 :     if (pszPixelType == nullptr)
    8236        2104 :         pszPixelType = "";
    8237        2109 :     if (eType == GDT_UInt8 && EQUAL(pszPixelType, "SIGNEDBYTE"))
    8238             :     {
    8239          10 :         for (int i = 0; i < poDS->nBands; ++i)
    8240             :         {
    8241           5 :             auto poBand = static_cast<GTiffRasterBand *>(poDS->papoBands[i]);
    8242           5 :             poBand->eDataType = GDT_UInt8;
    8243           5 :             poBand->EnablePixelTypeSignedByteWarning(false);
    8244           5 :             poBand->SetMetadataItem("PIXELTYPE", "SIGNEDBYTE",
    8245             :                                     "IMAGE_STRUCTURE");
    8246           5 :             poBand->EnablePixelTypeSignedByteWarning(true);
    8247             :         }
    8248             :     }
    8249             : 
    8250        2109 :     poDS->oOvManager.Initialize(poDS.get(), pszFilename);
    8251             : 
    8252        2109 :     if (bStreaming)
    8253             :     {
    8254           4 :         VSIUnlink(l_osTmpFilename);
    8255           4 :         poDS->m_fpToWrite = fpStreaming;
    8256             :     }
    8257        2109 :     poDS->m_eProfile = eProfile;
    8258             : 
    8259        2109 :     int nCloneInfoFlags = GCIF_PAM_DEFAULT & ~GCIF_MASK;
    8260             : 
    8261             :     // If we explicitly asked not to tag the alpha band as such, do not
    8262             :     // reintroduce this alpha color interpretation in PAM.
    8263        2109 :     if (poSrcDS->GetRasterBand(l_nBands)->GetColorInterpretation() ==
    8264        2237 :             GCI_AlphaBand &&
    8265         128 :         GTiffGetAlphaValue(
    8266             :             CPLGetConfigOption("GTIFF_ALPHA",
    8267             :                                CSLFetchNameValue(papszOptions, "ALPHA")),
    8268             :             DEFAULT_ALPHA_TYPE) == EXTRASAMPLE_UNSPECIFIED)
    8269             :     {
    8270           1 :         nCloneInfoFlags &= ~GCIF_COLORINTERP;
    8271             :     }
    8272             :     // Ignore source band color interpretation if requesting PHOTOMETRIC=RGB
    8273        3330 :     else if (l_nBands >= 3 &&
    8274        1222 :              EQUAL(CSLFetchNameValueDef(papszOptions, "PHOTOMETRIC", ""),
    8275             :                    "RGB"))
    8276             :     {
    8277          28 :         for (int i = 1; i <= 3; i++)
    8278             :         {
    8279          21 :             poDS->GetRasterBand(i)->SetColorInterpretation(
    8280          21 :                 static_cast<GDALColorInterp>(GCI_RedBand + (i - 1)));
    8281             :         }
    8282           7 :         nCloneInfoFlags &= ~GCIF_COLORINTERP;
    8283           9 :         if (!(l_nBands == 4 &&
    8284           2 :               CSLFetchNameValue(papszOptions, "ALPHA") != nullptr))
    8285             :         {
    8286          15 :             for (int i = 4; i <= l_nBands; i++)
    8287             :             {
    8288          18 :                 poDS->GetRasterBand(i)->SetColorInterpretation(
    8289           9 :                     poSrcDS->GetRasterBand(i)->GetColorInterpretation());
    8290             :             }
    8291             :         }
    8292             :     }
    8293             : 
    8294             :     CPLString osOldGTIFF_REPORT_COMPD_CSVal(
    8295        4218 :         CPLGetConfigOption("GTIFF_REPORT_COMPD_CS", ""));
    8296        2109 :     CPLSetThreadLocalConfigOption("GTIFF_REPORT_COMPD_CS", "YES");
    8297        2109 :     poDS->CloneInfo(poSrcDS, nCloneInfoFlags);
    8298        2109 :     CPLSetThreadLocalConfigOption("GTIFF_REPORT_COMPD_CS",
    8299        2109 :                                   osOldGTIFF_REPORT_COMPD_CSVal.empty()
    8300             :                                       ? nullptr
    8301           0 :                                       : osOldGTIFF_REPORT_COMPD_CSVal.c_str());
    8302             : 
    8303        2126 :     if ((!bGeoTIFF || bExportSRSToPAM) &&
    8304          17 :         (poDS->GetPamFlags() & GPF_DISABLED) == 0)
    8305             :     {
    8306             :         // Copy georeferencing info to PAM if the profile is not GeoTIFF
    8307          16 :         poDS->GDALPamDataset::SetSpatialRef(poDS->GetSpatialRef());
    8308          16 :         GDALGeoTransform gt;
    8309          16 :         if (poDS->GetGeoTransform(gt) == CE_None)
    8310             :         {
    8311           5 :             poDS->GDALPamDataset::SetGeoTransform(gt);
    8312             :         }
    8313          16 :         poDS->GDALPamDataset::SetGCPs(poDS->GetGCPCount(), poDS->GetGCPs(),
    8314             :                                       poDS->GetGCPSpatialRef());
    8315             :     }
    8316             : 
    8317        2109 :     poDS->m_papszCreationOptions = CSLDuplicate(papszOptions);
    8318        2109 :     poDS->m_bDontReloadFirstBlock = l_bDontReloadFirstBlock;
    8319             : 
    8320             :     /* -------------------------------------------------------------------- */
    8321             :     /*      CloneInfo() does not merge metadata, it just replaces it        */
    8322             :     /*      totally.  So we have to merge it.                               */
    8323             :     /* -------------------------------------------------------------------- */
    8324             : 
    8325        2109 :     CSLConstList papszSRC_MD = poSrcDS->GetMetadata();
    8326        2109 :     char **papszDST_MD = CSLDuplicate(poDS->GetMetadata());
    8327             : 
    8328        2109 :     papszDST_MD = CSLMerge(papszDST_MD, papszSRC_MD);
    8329             : 
    8330        2109 :     poDS->SetMetadata(papszDST_MD);
    8331        2109 :     CSLDestroy(papszDST_MD);
    8332             : 
    8333             :     // Depending on the PHOTOMETRIC tag, the TIFF file may not have the same
    8334             :     // band count as the source. Will fail later in GDALDatasetCopyWholeRaster
    8335             :     // anyway.
    8336        7133 :     for (int nBand = 1;
    8337        7133 :          nBand <= std::min(poDS->GetRasterCount(), poSrcDS->GetRasterCount());
    8338             :          ++nBand)
    8339             :     {
    8340        5024 :         GDALRasterBand *poSrcBand = poSrcDS->GetRasterBand(nBand);
    8341        5024 :         GDALRasterBand *poDstBand = poDS->GetRasterBand(nBand);
    8342        5024 :         papszSRC_MD = poSrcBand->GetMetadata();
    8343        5024 :         papszDST_MD = CSLDuplicate(poDstBand->GetMetadata());
    8344             : 
    8345        5024 :         papszDST_MD = CSLMerge(papszDST_MD, papszSRC_MD);
    8346             : 
    8347        5024 :         poDstBand->SetMetadata(papszDST_MD);
    8348        5024 :         CSLDestroy(papszDST_MD);
    8349             : 
    8350        5024 :         char **papszCatNames = poSrcBand->GetCategoryNames();
    8351        5024 :         if (nullptr != papszCatNames)
    8352           0 :             poDstBand->SetCategoryNames(papszCatNames);
    8353             :     }
    8354             : 
    8355        2109 :     l_hTIFF = static_cast<TIFF *>(poDS->GetInternalHandle("TIFF_HANDLE"));
    8356             : 
    8357             :     /* -------------------------------------------------------------------- */
    8358             :     /*      Handle forcing xml:ESRI data to be written to PAM.              */
    8359             :     /* -------------------------------------------------------------------- */
    8360        2109 :     if (CPLTestBool(CPLGetConfigOption("ESRI_XML_PAM", "NO")))
    8361             :     {
    8362           1 :         CSLConstList papszESRIMD = poSrcDS->GetMetadata("xml:ESRI");
    8363           1 :         if (papszESRIMD)
    8364             :         {
    8365           1 :             poDS->SetMetadata(papszESRIMD, "xml:ESRI");
    8366             :         }
    8367             :     }
    8368             : 
    8369             :     /* -------------------------------------------------------------------- */
    8370             :     /*      Second chance: now that we have a PAM dataset, it is possible   */
    8371             :     /*      to write metadata that we could not write as a TIFF tag.        */
    8372             :     /* -------------------------------------------------------------------- */
    8373        2109 :     if (!bHasWrittenMDInGeotiffTAG && !bStreaming)
    8374             :     {
    8375           6 :         GTiffDataset::WriteMetadata(
    8376           6 :             poDS.get(), l_hTIFF, true, eProfile, pszFilename, papszOptions,
    8377             :             true /* don't write RPC and IMD file again */);
    8378             :     }
    8379             : 
    8380        2109 :     if (!bStreaming)
    8381        2105 :         GTiffDataset::WriteRPC(poDS.get(), l_hTIFF, true, eProfile, pszFilename,
    8382             :                                papszOptions,
    8383             :                                true /* write only in PAM AND if needed */);
    8384             : 
    8385        2109 :     poDS->m_bWriteCOGLayout = bCopySrcOverviews;
    8386             : 
    8387             :     // To avoid unnecessary directory rewriting.
    8388        2109 :     poDS->m_bMetadataChanged = false;
    8389        2109 :     poDS->m_bGeoTIFFInfoChanged = false;
    8390        2109 :     poDS->m_bNoDataChanged = false;
    8391        2109 :     poDS->m_bForceUnsetGTOrGCPs = false;
    8392        2109 :     poDS->m_bForceUnsetProjection = false;
    8393        2109 :     poDS->m_bStreamingOut = bStreaming;
    8394             : 
    8395             :     // Don't try to load external metadata files (#6597).
    8396        2109 :     poDS->m_bIMDRPCMetadataLoaded = true;
    8397             : 
    8398             :     // We must re-set the compression level at this point, since it has been
    8399             :     // lost a few lines above when closing the newly create TIFF file The
    8400             :     // TIFFTAG_ZIPQUALITY & TIFFTAG_JPEGQUALITY are not store in the TIFF file.
    8401             :     // They are just TIFF session parameters.
    8402             : 
    8403        2109 :     poDS->m_nZLevel = GTiffGetZLevel(papszOptions);
    8404        2109 :     poDS->m_nLZMAPreset = GTiffGetLZMAPreset(papszOptions);
    8405        2109 :     poDS->m_nZSTDLevel = GTiffGetZSTDPreset(papszOptions);
    8406        2109 :     poDS->m_nWebPLevel = GTiffGetWebPLevel(papszOptions);
    8407        2109 :     poDS->m_bWebPLossless = GTiffGetWebPLossless(papszOptions);
    8408        2112 :     if (poDS->m_nWebPLevel != 100 && poDS->m_bWebPLossless &&
    8409           3 :         CSLFetchNameValue(papszOptions, "WEBP_LEVEL"))
    8410             :     {
    8411           0 :         CPLError(CE_Warning, CPLE_AppDefined,
    8412             :                  "WEBP_LEVEL is specified, but WEBP_LOSSLESS=YES. "
    8413             :                  "WEBP_LEVEL will be ignored.");
    8414             :     }
    8415        2109 :     poDS->m_nJpegQuality = GTiffGetJpegQuality(papszOptions);
    8416        2109 :     poDS->m_nJpegTablesMode = GTiffGetJpegTablesMode(papszOptions);
    8417        2109 :     poDS->GetDiscardLsbOption(papszOptions);
    8418        2109 :     poDS->m_dfMaxZError = GTiffGetLERCMaxZError(papszOptions);
    8419        2109 :     poDS->m_dfMaxZErrorOverview = GTiffGetLERCMaxZErrorOverview(papszOptions);
    8420             : #if HAVE_JXL
    8421        2109 :     poDS->m_bJXLLossless = GTiffGetJXLLossless(papszOptions);
    8422        2109 :     poDS->m_nJXLEffort = GTiffGetJXLEffort(papszOptions);
    8423        2109 :     poDS->m_fJXLDistance = GTiffGetJXLDistance(papszOptions);
    8424        2109 :     poDS->m_fJXLAlphaDistance = GTiffGetJXLAlphaDistance(papszOptions);
    8425             : #endif
    8426        2109 :     poDS->InitCreationOrOpenOptions(true, papszOptions);
    8427             : 
    8428        2109 :     if (l_nCompression == COMPRESSION_ADOBE_DEFLATE ||
    8429        2081 :         l_nCompression == COMPRESSION_LERC)
    8430             :     {
    8431          99 :         GTiffSetDeflateSubCodec(l_hTIFF);
    8432             : 
    8433          99 :         if (poDS->m_nZLevel != -1)
    8434             :         {
    8435          12 :             TIFFSetField(l_hTIFF, TIFFTAG_ZIPQUALITY, poDS->m_nZLevel);
    8436             :         }
    8437             :     }
    8438        2109 :     if (l_nCompression == COMPRESSION_JPEG)
    8439             :     {
    8440          75 :         if (poDS->m_nJpegQuality != -1)
    8441             :         {
    8442           9 :             TIFFSetField(l_hTIFF, TIFFTAG_JPEGQUALITY, poDS->m_nJpegQuality);
    8443             :         }
    8444          75 :         TIFFSetField(l_hTIFF, TIFFTAG_JPEGTABLESMODE, poDS->m_nJpegTablesMode);
    8445             :     }
    8446        2109 :     if (l_nCompression == COMPRESSION_LZMA)
    8447             :     {
    8448           7 :         if (poDS->m_nLZMAPreset != -1)
    8449             :         {
    8450           6 :             TIFFSetField(l_hTIFF, TIFFTAG_LZMAPRESET, poDS->m_nLZMAPreset);
    8451             :         }
    8452             :     }
    8453        2109 :     if (l_nCompression == COMPRESSION_ZSTD ||
    8454        2098 :         l_nCompression == COMPRESSION_LERC)
    8455             :     {
    8456          82 :         if (poDS->m_nZSTDLevel != -1)
    8457             :         {
    8458           8 :             TIFFSetField(l_hTIFF, TIFFTAG_ZSTD_LEVEL, poDS->m_nZSTDLevel);
    8459             :         }
    8460             :     }
    8461        2109 :     if (l_nCompression == COMPRESSION_LERC)
    8462             :     {
    8463          71 :         TIFFSetField(l_hTIFF, TIFFTAG_LERC_MAXZERROR, poDS->m_dfMaxZError);
    8464             :     }
    8465             : #if HAVE_JXL
    8466        2109 :     if (l_nCompression == COMPRESSION_JXL ||
    8467        2109 :         l_nCompression == COMPRESSION_JXL_DNG_1_7)
    8468             :     {
    8469          91 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_LOSSYNESS,
    8470          91 :                      poDS->m_bJXLLossless ? JXL_LOSSLESS : JXL_LOSSY);
    8471          91 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_EFFORT, poDS->m_nJXLEffort);
    8472          91 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_DISTANCE,
    8473          91 :                      static_cast<double>(poDS->m_fJXLDistance));
    8474          91 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_ALPHA_DISTANCE,
    8475          91 :                      static_cast<double>(poDS->m_fJXLAlphaDistance));
    8476             :     }
    8477             : #endif
    8478        2109 :     if (l_nCompression == COMPRESSION_WEBP)
    8479             :     {
    8480          14 :         if (poDS->m_nWebPLevel != -1)
    8481             :         {
    8482          14 :             TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LEVEL, poDS->m_nWebPLevel);
    8483             :         }
    8484             : 
    8485          14 :         if (poDS->m_bWebPLossless)
    8486             :         {
    8487           5 :             TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LOSSLESS, poDS->m_bWebPLossless);
    8488             :         }
    8489             :     }
    8490             : 
    8491             :     /* -------------------------------------------------------------------- */
    8492             :     /*      Do we want to ensure all blocks get written out on close to     */
    8493             :     /*      avoid sparse files?                                             */
    8494             :     /* -------------------------------------------------------------------- */
    8495        2109 :     if (!CPLFetchBool(papszOptions, "SPARSE_OK", false))
    8496        2081 :         poDS->m_bFillEmptyTilesAtClosing = true;
    8497             : 
    8498        2109 :     poDS->m_bWriteEmptyTiles =
    8499        4000 :         (bCopySrcOverviews && poDS->m_bFillEmptyTilesAtClosing) || bStreaming ||
    8500        1891 :         (poDS->m_nCompression != COMPRESSION_NONE &&
    8501         310 :          poDS->m_bFillEmptyTilesAtClosing);
    8502             :     // Only required for people writing non-compressed striped files in the
    8503             :     // rightorder and wanting all tstrips to be written in the same order
    8504             :     // so that the end result can be memory mapped without knowledge of each
    8505             :     // strip offset
    8506        2109 :     if (CPLTestBool(CSLFetchNameValueDef(
    8507        4218 :             papszOptions, "WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")) ||
    8508        2109 :         CPLTestBool(CSLFetchNameValueDef(
    8509             :             papszOptions, "@WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")))
    8510             :     {
    8511           0 :         poDS->m_bWriteEmptyTiles = true;
    8512             :     }
    8513             : 
    8514             :     // Precreate (internal) mask, so that the IBuildOverviews() below
    8515             :     // has a chance to create also the overviews of the mask.
    8516        2109 :     CPLErr eErr = CE_None;
    8517             : 
    8518        2109 :     if (bCreateMask)
    8519             :     {
    8520          38 :         eErr = poDS->CreateMaskBand(nMaskFlags);
    8521          38 :         if (poDS->m_poMaskDS)
    8522             :         {
    8523          37 :             poDS->m_poMaskDS->m_bFillEmptyTilesAtClosing =
    8524          37 :                 poDS->m_bFillEmptyTilesAtClosing;
    8525          37 :             poDS->m_poMaskDS->m_bWriteEmptyTiles = poDS->m_bWriteEmptyTiles;
    8526             :         }
    8527             :     }
    8528             : 
    8529             :     /* -------------------------------------------------------------------- */
    8530             :     /*      Create and then copy existing overviews if requested            */
    8531             :     /*  We do it such that all the IFDs are at the beginning of the file,   */
    8532             :     /*  and that the imagery data for the smallest overview is written      */
    8533             :     /*  first, that way the file is more usable when embedded in a          */
    8534             :     /*  compressed stream.                                                  */
    8535             :     /* -------------------------------------------------------------------- */
    8536             : 
    8537             :     // For scaled progress due to overview copying.
    8538        2109 :     const int nBandsWidthMask = l_nBands + (bCreateMask ? 1 : 0);
    8539        2109 :     double dfTotalPixels =
    8540        2109 :         static_cast<double>(nXSize) * nYSize * nBandsWidthMask;
    8541        2109 :     double dfCurPixels = 0;
    8542             : 
    8543        2109 :     if (eErr == CE_None && bCopySrcOverviews)
    8544             :     {
    8545           0 :         std::unique_ptr<GDALDataset> poMaskOvrDS;
    8546             :         const char *pszMaskOvrDS =
    8547         219 :             CSLFetchNameValue(papszOptions, "@MASK_OVERVIEW_DATASET");
    8548         219 :         if (pszMaskOvrDS)
    8549             :         {
    8550           6 :             poMaskOvrDS.reset(GDALDataset::Open(pszMaskOvrDS));
    8551           6 :             if (!poMaskOvrDS)
    8552             :             {
    8553           0 :                 l_fpL->CancelCreation();
    8554           0 :                 return nullptr;
    8555             :             }
    8556           6 :             if (poMaskOvrDS->GetRasterCount() != 1)
    8557             :             {
    8558           0 :                 l_fpL->CancelCreation();
    8559           0 :                 return nullptr;
    8560             :             }
    8561             :         }
    8562         219 :         if (nSrcOverviews)
    8563             :         {
    8564          69 :             eErr = poDS->CreateOverviewsFromSrcOverviews(poSrcDS, poOvrDS.get(),
    8565             :                                                          nSrcOverviews);
    8566             : 
    8567         201 :             if (eErr == CE_None &&
    8568          69 :                 (poMaskOvrDS != nullptr ||
    8569          63 :                  (poSrcDS->GetRasterBand(1)->GetOverview(0) &&
    8570          35 :                   poSrcDS->GetRasterBand(1)->GetOverview(0)->GetMaskFlags() ==
    8571             :                       GMF_PER_DATASET)))
    8572             :             {
    8573          19 :                 int nOvrBlockXSize = 0;
    8574          19 :                 int nOvrBlockYSize = 0;
    8575          19 :                 GTIFFGetOverviewBlockSize(
    8576          19 :                     GDALRasterBand::ToHandle(poDS->GetRasterBand(1)),
    8577             :                     &nOvrBlockXSize, &nOvrBlockYSize, nullptr, nullptr);
    8578          19 :                 eErr = poDS->CreateInternalMaskOverviews(nOvrBlockXSize,
    8579             :                                                          nOvrBlockYSize);
    8580             :             }
    8581             :         }
    8582             : 
    8583         219 :         TIFFForceStrileArrayWriting(poDS->m_hTIFF);
    8584             : 
    8585         219 :         if (poDS->m_poMaskDS)
    8586             :         {
    8587          27 :             TIFFForceStrileArrayWriting(poDS->m_poMaskDS->m_hTIFF);
    8588             :         }
    8589             : 
    8590         345 :         for (auto &poIterOvrDS : poDS->m_apoOverviewDS)
    8591             :         {
    8592         126 :             TIFFForceStrileArrayWriting(poIterOvrDS->m_hTIFF);
    8593             : 
    8594         126 :             if (poIterOvrDS->m_poMaskDS)
    8595             :             {
    8596          32 :                 TIFFForceStrileArrayWriting(poIterOvrDS->m_poMaskDS->m_hTIFF);
    8597             :             }
    8598             :         }
    8599             : 
    8600         219 :         if (eErr == CE_None && nSrcOverviews)
    8601             :         {
    8602          69 :             if (poDS->m_apoOverviewDS.size() !=
    8603          69 :                 static_cast<size_t>(nSrcOverviews))
    8604             :             {
    8605           0 :                 ReportError(
    8606             :                     pszFilename, CE_Failure, CPLE_AppDefined,
    8607             :                     "Did only manage to instantiate %d overview levels, "
    8608             :                     "whereas source contains %d",
    8609           0 :                     static_cast<int>(poDS->m_apoOverviewDS.size()),
    8610             :                     nSrcOverviews);
    8611           0 :                 eErr = CE_Failure;
    8612             :             }
    8613             : 
    8614         195 :             for (int i = 0; eErr == CE_None && i < nSrcOverviews; ++i)
    8615             :             {
    8616             :                 GDALRasterBand *poOvrBand =
    8617             :                     poOvrDS
    8618         197 :                         ? (i == 0
    8619          71 :                                ? poOvrDS->GetRasterBand(1)
    8620          37 :                                : poOvrDS->GetRasterBand(1)->GetOverview(i - 1))
    8621         181 :                         : poSrcDS->GetRasterBand(1)->GetOverview(i);
    8622             :                 const double dfOvrPixels =
    8623         126 :                     static_cast<double>(poOvrBand->GetXSize()) *
    8624         126 :                     poOvrBand->GetYSize();
    8625         126 :                 dfTotalPixels += dfOvrPixels * l_nBands;
    8626         234 :                 if (poOvrBand->GetMaskFlags() == GMF_PER_DATASET ||
    8627         108 :                     poMaskOvrDS != nullptr)
    8628             :                 {
    8629          32 :                     dfTotalPixels += dfOvrPixels;
    8630             :                 }
    8631          94 :                 else if (i == 0 && poDS->GetRasterBand(1)->GetMaskFlags() ==
    8632             :                                        GMF_PER_DATASET)
    8633             :                 {
    8634           1 :                     ReportError(pszFilename, CE_Warning, CPLE_AppDefined,
    8635             :                                 "Source dataset has a mask band on full "
    8636             :                                 "resolution, overviews on the regular bands, "
    8637             :                                 "but lacks overviews on the mask band.");
    8638             :                 }
    8639             :             }
    8640             : 
    8641             :             // Now copy the imagery.
    8642             :             // Begin with the smallest overview.
    8643          69 :             for (int iOvrLevel = nSrcOverviews - 1;
    8644         194 :                  eErr == CE_None && iOvrLevel >= 0; --iOvrLevel)
    8645             :             {
    8646         125 :                 auto poDstDS = poDS->m_apoOverviewDS[iOvrLevel].get();
    8647             : 
    8648             :                 // Create a fake dataset with the source overview level so that
    8649             :                 // GDALDatasetCopyWholeRaster can cope with it.
    8650             :                 GDALDataset *poSrcOvrDS =
    8651             :                     poOvrDS
    8652         162 :                         ? (iOvrLevel == 0 ? poOvrDS.get()
    8653          37 :                                           : GDALCreateOverviewDataset(
    8654             :                                                 poOvrDS.get(), iOvrLevel - 1,
    8655             :                                                 /* bThisLevelOnly = */ true))
    8656          54 :                         : GDALCreateOverviewDataset(
    8657             :                               poSrcDS, iOvrLevel,
    8658         125 :                               /* bThisLevelOnly = */ true);
    8659             :                 GDALRasterBand *poSrcOvrBand =
    8660         196 :                     poOvrDS ? (iOvrLevel == 0
    8661          71 :                                    ? poOvrDS->GetRasterBand(1)
    8662          74 :                                    : poOvrDS->GetRasterBand(1)->GetOverview(
    8663          37 :                                          iOvrLevel - 1))
    8664         179 :                             : poSrcDS->GetRasterBand(1)->GetOverview(iOvrLevel);
    8665             :                 double dfNextCurPixels =
    8666             :                     dfCurPixels +
    8667         125 :                     static_cast<double>(poSrcOvrBand->GetXSize()) *
    8668         125 :                         poSrcOvrBand->GetYSize() * l_nBands;
    8669             : 
    8670         125 :                 poDstDS->m_bBlockOrderRowMajor = true;
    8671         125 :                 poDstDS->m_bLeaderSizeAsUInt4 = true;
    8672         125 :                 poDstDS->m_bTrailerRepeatedLast4BytesRepeated = true;
    8673         125 :                 poDstDS->m_bFillEmptyTilesAtClosing =
    8674         125 :                     poDS->m_bFillEmptyTilesAtClosing;
    8675         125 :                 poDstDS->m_bWriteEmptyTiles = poDS->m_bWriteEmptyTiles;
    8676         125 :                 poDstDS->m_bTileInterleave = poDS->m_bTileInterleave;
    8677         125 :                 GDALRasterBand *poSrcMaskBand = nullptr;
    8678         125 :                 if (poDstDS->m_poMaskDS)
    8679             :                 {
    8680          32 :                     poDstDS->m_poMaskDS->m_bBlockOrderRowMajor = true;
    8681          32 :                     poDstDS->m_poMaskDS->m_bLeaderSizeAsUInt4 = true;
    8682          32 :                     poDstDS->m_poMaskDS->m_bTrailerRepeatedLast4BytesRepeated =
    8683             :                         true;
    8684          64 :                     poDstDS->m_poMaskDS->m_bFillEmptyTilesAtClosing =
    8685          32 :                         poDS->m_bFillEmptyTilesAtClosing;
    8686          64 :                     poDstDS->m_poMaskDS->m_bWriteEmptyTiles =
    8687          32 :                         poDS->m_bWriteEmptyTiles;
    8688             : 
    8689          32 :                     poSrcMaskBand =
    8690             :                         poMaskOvrDS
    8691          46 :                             ? (iOvrLevel == 0
    8692          14 :                                    ? poMaskOvrDS->GetRasterBand(1)
    8693          16 :                                    : poMaskOvrDS->GetRasterBand(1)->GetOverview(
    8694           8 :                                          iOvrLevel - 1))
    8695          50 :                             : poSrcOvrBand->GetMaskBand();
    8696             :                 }
    8697             : 
    8698         125 :                 if (poDstDS->m_poMaskDS)
    8699             :                 {
    8700          32 :                     dfNextCurPixels +=
    8701          32 :                         static_cast<double>(poSrcOvrBand->GetXSize()) *
    8702          32 :                         poSrcOvrBand->GetYSize();
    8703             :                 }
    8704             :                 void *pScaledData =
    8705         125 :                     GDALCreateScaledProgress(dfCurPixels / dfTotalPixels,
    8706             :                                              dfNextCurPixels / dfTotalPixels,
    8707             :                                              pfnProgress, pProgressData);
    8708             : 
    8709         125 :                 eErr = CopyImageryAndMask(poDstDS, poSrcOvrDS, poSrcMaskBand,
    8710             :                                           GDALScaledProgress, pScaledData);
    8711             : 
    8712         125 :                 dfCurPixels = dfNextCurPixels;
    8713         125 :                 GDALDestroyScaledProgress(pScaledData);
    8714             : 
    8715         125 :                 if (poSrcOvrDS != poOvrDS.get())
    8716          91 :                     delete poSrcOvrDS;
    8717         125 :                 poSrcOvrDS = nullptr;
    8718             :             }
    8719             :         }
    8720             :     }
    8721             : 
    8722             :     /* -------------------------------------------------------------------- */
    8723             :     /*      Copy actual imagery.                                            */
    8724             :     /* -------------------------------------------------------------------- */
    8725        2109 :     double dfNextCurPixels =
    8726        2109 :         dfCurPixels + static_cast<double>(nXSize) * nYSize * l_nBands;
    8727        2109 :     void *pScaledData = GDALCreateScaledProgress(
    8728             :         dfCurPixels / dfTotalPixels, dfNextCurPixels / dfTotalPixels,
    8729             :         pfnProgress, pProgressData);
    8730             : 
    8731             : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
    8732        2109 :     bool bTryCopy = true;
    8733             : #endif
    8734             : 
    8735             : #ifdef HAVE_LIBJPEG
    8736        2109 :     if (bCopyFromJPEG)
    8737             :     {
    8738          12 :         eErr = GTIFF_CopyFromJPEG(poDS.get(), poSrcDS, pfnProgress,
    8739             :                                   pProgressData, bTryCopy);
    8740             : 
    8741             :         // In case of failure in the decompression step, try normal copy.
    8742          12 :         if (bTryCopy)
    8743           0 :             eErr = CE_None;
    8744             :     }
    8745             : #endif
    8746             : 
    8747             : #ifdef JPEG_DIRECT_COPY
    8748             :     if (bDirectCopyFromJPEG)
    8749             :     {
    8750             :         eErr = GTIFF_DirectCopyFromJPEG(poDS.get(), poSrcDS, pfnProgress,
    8751             :                                         pProgressData, bTryCopy);
    8752             : 
    8753             :         // In case of failure in the reading step, try normal copy.
    8754             :         if (bTryCopy)
    8755             :             eErr = CE_None;
    8756             :     }
    8757             : #endif
    8758             : 
    8759        2109 :     bool bWriteMask = true;
    8760        2109 :     if (
    8761             : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
    8762        4206 :         bTryCopy &&
    8763             : #endif
    8764        2097 :         (poDS->m_bTreatAsSplit || poDS->m_bTreatAsSplitBitmap))
    8765             :     {
    8766             :         // For split bands, we use TIFFWriteScanline() interface.
    8767           9 :         CPLAssert(poDS->m_nBitsPerSample == 8 || poDS->m_nBitsPerSample == 1);
    8768             : 
    8769           9 :         if (poDS->m_nPlanarConfig == PLANARCONFIG_CONTIG && poDS->nBands > 1)
    8770             :         {
    8771             :             GByte *pabyScanline = static_cast<GByte *>(
    8772           3 :                 VSI_MALLOC_VERBOSE(TIFFScanlineSize(l_hTIFF)));
    8773           3 :             if (pabyScanline == nullptr)
    8774           0 :                 eErr = CE_Failure;
    8775        9052 :             for (int j = 0; j < nYSize && eErr == CE_None; ++j)
    8776             :             {
    8777       18098 :                 eErr = poSrcDS->RasterIO(GF_Read, 0, j, nXSize, 1, pabyScanline,
    8778             :                                          nXSize, 1, GDT_UInt8, l_nBands,
    8779        9049 :                                          nullptr, poDS->nBands, 0, 1, nullptr);
    8780       18098 :                 if (eErr == CE_None &&
    8781        9049 :                     TIFFWriteScanline(l_hTIFF, pabyScanline, j, 0) == -1)
    8782             :                 {
    8783           0 :                     ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    8784             :                                 "TIFFWriteScanline() failed.");
    8785           0 :                     eErr = CE_Failure;
    8786             :                 }
    8787        9049 :                 if (!GDALScaledProgress((j + 1) * 1.0 / nYSize, nullptr,
    8788             :                                         pScaledData))
    8789           0 :                     eErr = CE_Failure;
    8790             :             }
    8791           3 :             CPLFree(pabyScanline);
    8792             :         }
    8793             :         else
    8794             :         {
    8795             :             GByte *pabyScanline =
    8796           6 :                 static_cast<GByte *>(VSI_MALLOC_VERBOSE(nXSize));
    8797           6 :             if (pabyScanline == nullptr)
    8798           0 :                 eErr = CE_Failure;
    8799             :             else
    8800           6 :                 eErr = CE_None;
    8801          14 :             for (int iBand = 1; iBand <= l_nBands && eErr == CE_None; ++iBand)
    8802             :             {
    8803       48211 :                 for (int j = 0; j < nYSize && eErr == CE_None; ++j)
    8804             :                 {
    8805       48203 :                     eErr = poSrcDS->GetRasterBand(iBand)->RasterIO(
    8806             :                         GF_Read, 0, j, nXSize, 1, pabyScanline, nXSize, 1,
    8807             :                         GDT_UInt8, 0, 0, nullptr);
    8808       48203 :                     if (poDS->m_bTreatAsSplitBitmap)
    8809             :                     {
    8810     7225210 :                         for (int i = 0; i < nXSize; ++i)
    8811             :                         {
    8812     7216010 :                             const GByte byVal = pabyScanline[i];
    8813     7216010 :                             if ((i & 0x7) == 0)
    8814      902001 :                                 pabyScanline[i >> 3] = 0;
    8815     7216010 :                             if (byVal)
    8816     7097220 :                                 pabyScanline[i >> 3] |= 0x80 >> (i & 0x7);
    8817             :                         }
    8818             :                     }
    8819       96406 :                     if (eErr == CE_None &&
    8820       48203 :                         TIFFWriteScanline(l_hTIFF, pabyScanline, j,
    8821       48203 :                                           static_cast<uint16_t>(iBand - 1)) ==
    8822             :                             -1)
    8823             :                     {
    8824           0 :                         ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    8825             :                                     "TIFFWriteScanline() failed.");
    8826           0 :                         eErr = CE_Failure;
    8827             :                     }
    8828       48203 :                     if (!GDALScaledProgress((j + 1 + (iBand - 1) * nYSize) *
    8829       48203 :                                                 1.0 / (l_nBands * nYSize),
    8830             :                                             nullptr, pScaledData))
    8831           0 :                         eErr = CE_Failure;
    8832             :                 }
    8833             :             }
    8834           6 :             CPLFree(pabyScanline);
    8835             :         }
    8836             : 
    8837             :         // Necessary to be able to read the file without re-opening.
    8838           9 :         TIFFSizeProc pfnSizeProc = TIFFGetSizeProc(l_hTIFF);
    8839             : 
    8840           9 :         TIFFFlushData(l_hTIFF);
    8841             : 
    8842           9 :         toff_t nNewDirOffset = pfnSizeProc(TIFFClientdata(l_hTIFF));
    8843           9 :         if ((nNewDirOffset % 2) == 1)
    8844           5 :             ++nNewDirOffset;
    8845             : 
    8846           9 :         TIFFFlush(l_hTIFF);
    8847             : 
    8848           9 :         if (poDS->m_nDirOffset != TIFFCurrentDirOffset(l_hTIFF))
    8849             :         {
    8850           0 :             poDS->m_nDirOffset = nNewDirOffset;
    8851           0 :             CPLDebug("GTiff", "directory moved during flush.");
    8852             :         }
    8853             :     }
    8854        2100 :     else if (
    8855             : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
    8856        2088 :         bTryCopy &&
    8857             : #endif
    8858             :         eErr == CE_None)
    8859             :     {
    8860        2087 :         const char *papszCopyWholeRasterOptions[3] = {nullptr, nullptr,
    8861             :                                                       nullptr};
    8862        2087 :         int iNextOption = 0;
    8863        2087 :         papszCopyWholeRasterOptions[iNextOption++] = "SKIP_HOLES=YES";
    8864        2087 :         if (l_nCompression != COMPRESSION_NONE)
    8865             :         {
    8866         494 :             papszCopyWholeRasterOptions[iNextOption++] = "COMPRESSED=YES";
    8867             :         }
    8868             : 
    8869             :         // For streaming with separate, we really want that bands are written
    8870             :         // after each other, even if the source is pixel interleaved.
    8871        1593 :         else if (bStreaming && poDS->m_nPlanarConfig == PLANARCONFIG_SEPARATE)
    8872             :         {
    8873           1 :             papszCopyWholeRasterOptions[iNextOption++] = "INTERLEAVE=BAND";
    8874             :         }
    8875             : 
    8876        2087 :         if (bCopySrcOverviews || bTileInterleaving)
    8877             :         {
    8878         219 :             poDS->m_bBlockOrderRowMajor = true;
    8879         219 :             poDS->m_bLeaderSizeAsUInt4 = bCopySrcOverviews;
    8880         219 :             poDS->m_bTrailerRepeatedLast4BytesRepeated = bCopySrcOverviews;
    8881         219 :             if (poDS->m_poMaskDS)
    8882             :             {
    8883          27 :                 poDS->m_poMaskDS->m_bBlockOrderRowMajor = true;
    8884          27 :                 poDS->m_poMaskDS->m_bLeaderSizeAsUInt4 = bCopySrcOverviews;
    8885          27 :                 poDS->m_poMaskDS->m_bTrailerRepeatedLast4BytesRepeated =
    8886             :                     bCopySrcOverviews;
    8887          27 :                 GDALDestroyScaledProgress(pScaledData);
    8888             :                 pScaledData =
    8889          27 :                     GDALCreateScaledProgress(dfCurPixels / dfTotalPixels, 1.0,
    8890             :                                              pfnProgress, pProgressData);
    8891             :             }
    8892             : 
    8893         219 :             eErr = CopyImageryAndMask(poDS.get(), poSrcDS,
    8894         219 :                                       poSrcDS->GetRasterBand(1)->GetMaskBand(),
    8895             :                                       GDALScaledProgress, pScaledData);
    8896         219 :             if (poDS->m_poMaskDS)
    8897             :             {
    8898          27 :                 bWriteMask = false;
    8899             :             }
    8900             :         }
    8901             :         else
    8902             :         {
    8903        1868 :             eErr = GDALDatasetCopyWholeRaster(GDALDataset::ToHandle(poSrcDS),
    8904        1868 :                                               GDALDataset::ToHandle(poDS.get()),
    8905             :                                               papszCopyWholeRasterOptions,
    8906             :                                               GDALScaledProgress, pScaledData);
    8907             :         }
    8908             :     }
    8909             : 
    8910        2109 :     GDALDestroyScaledProgress(pScaledData);
    8911             : 
    8912        2109 :     if (eErr == CE_None && !bStreaming && bWriteMask)
    8913             :     {
    8914        2060 :         pScaledData = GDALCreateScaledProgress(dfNextCurPixels / dfTotalPixels,
    8915             :                                                1.0, pfnProgress, pProgressData);
    8916        2060 :         if (poDS->m_poMaskDS)
    8917             :         {
    8918          10 :             const char *l_papszOptions[2] = {"COMPRESSED=YES", nullptr};
    8919          10 :             eErr = GDALRasterBandCopyWholeRaster(
    8920          10 :                 poSrcDS->GetRasterBand(1)->GetMaskBand(),
    8921          10 :                 poDS->GetRasterBand(1)->GetMaskBand(),
    8922             :                 const_cast<char **>(l_papszOptions), GDALScaledProgress,
    8923             :                 pScaledData);
    8924             :         }
    8925             :         else
    8926             :         {
    8927        2050 :             eErr = GDALDriver::DefaultCopyMasks(poSrcDS, poDS.get(), bStrict,
    8928             :                                                 nullptr, GDALScaledProgress,
    8929             :                                                 pScaledData);
    8930             :         }
    8931        2060 :         GDALDestroyScaledProgress(pScaledData);
    8932             :     }
    8933             : 
    8934        2109 :     poDS->m_bWriteCOGLayout = false;
    8935             : 
    8936        4200 :     if (eErr == CE_None &&
    8937        2091 :         CPLTestBool(CSLFetchNameValueDef(poDS->m_papszCreationOptions,
    8938             :                                          "@FLUSHCACHE", "NO")))
    8939             :     {
    8940         172 :         if (poDS->FlushCache(false) != CE_None)
    8941             :         {
    8942           0 :             eErr = CE_Failure;
    8943             :         }
    8944             :     }
    8945             : 
    8946        2109 :     if (eErr == CE_Failure)
    8947             :     {
    8948          18 :         if (CPLTestBool(CPLGetConfigOption("GTIFF_DELETE_ON_ERROR", "YES")))
    8949             :         {
    8950          17 :             l_fpL->CancelCreation();
    8951          17 :             poDS.reset();
    8952             : 
    8953          17 :             if (!bStreaming)
    8954             :             {
    8955             :                 // Should really delete more carefully.
    8956          17 :                 VSIUnlink(pszFilename);
    8957             :             }
    8958             :         }
    8959             :         else
    8960             :         {
    8961           1 :             poDS.reset();
    8962             :         }
    8963             :     }
    8964             : 
    8965        2109 :     return poDS.release();
    8966             : }
    8967             : 
    8968             : /************************************************************************/
    8969             : /*                           SetSpatialRef()                            */
    8970             : /************************************************************************/
    8971             : 
    8972        1505 : CPLErr GTiffDataset::SetSpatialRef(const OGRSpatialReference *poSRS)
    8973             : 
    8974             : {
    8975        1505 :     if (m_bStreamingOut && m_bCrystalized)
    8976             :     {
    8977           1 :         ReportError(CE_Failure, CPLE_NotSupported,
    8978             :                     "Cannot modify projection at that point in "
    8979             :                     "a streamed output file");
    8980           1 :         return CE_Failure;
    8981             :     }
    8982             : 
    8983        1504 :     LoadGeoreferencingAndPamIfNeeded();
    8984        1504 :     LookForProjection();
    8985             : 
    8986        1504 :     CPLErr eErr = CE_None;
    8987        1504 :     if (eAccess == GA_Update)
    8988             :     {
    8989        1506 :         if ((m_eProfile == GTiffProfile::BASELINE) &&
    8990           7 :             (GetPamFlags() & GPF_DISABLED) == 0)
    8991             :         {
    8992           7 :             eErr = GDALPamDataset::SetSpatialRef(poSRS);
    8993             :         }
    8994             :         else
    8995             :         {
    8996        1492 :             if (GDALPamDataset::GetSpatialRef() != nullptr)
    8997             :             {
    8998             :                 // Cancel any existing SRS from PAM file.
    8999           1 :                 GDALPamDataset::SetSpatialRef(nullptr);
    9000             :             }
    9001        1492 :             m_bGeoTIFFInfoChanged = true;
    9002             :         }
    9003             :     }
    9004             :     else
    9005             :     {
    9006           5 :         CPLDebug("GTIFF", "SetSpatialRef() goes to PAM instead of TIFF tags");
    9007           5 :         eErr = GDALPamDataset::SetSpatialRef(poSRS);
    9008             :     }
    9009             : 
    9010        1504 :     if (eErr == CE_None)
    9011             :     {
    9012        1504 :         if (poSRS == nullptr || poSRS->IsEmpty())
    9013             :         {
    9014          14 :             if (!m_oSRS.IsEmpty())
    9015             :             {
    9016           4 :                 m_bForceUnsetProjection = true;
    9017             :             }
    9018          14 :             m_oSRS.Clear();
    9019             :         }
    9020             :         else
    9021             :         {
    9022        1490 :             m_oSRS = *poSRS;
    9023        1490 :             m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    9024             :         }
    9025             :     }
    9026             : 
    9027        1504 :     return eErr;
    9028             : }
    9029             : 
    9030             : /************************************************************************/
    9031             : /*                          SetGeoTransform()                           */
    9032             : /************************************************************************/
    9033             : 
    9034        1814 : CPLErr GTiffDataset::SetGeoTransform(const GDALGeoTransform &gt)
    9035             : 
    9036             : {
    9037        1814 :     if (m_bStreamingOut && m_bCrystalized)
    9038             :     {
    9039           1 :         ReportError(CE_Failure, CPLE_NotSupported,
    9040             :                     "Cannot modify geotransform at that point in a "
    9041             :                     "streamed output file");
    9042           1 :         return CE_Failure;
    9043             :     }
    9044             : 
    9045        1813 :     LoadGeoreferencingAndPamIfNeeded();
    9046             : 
    9047        1813 :     CPLErr eErr = CE_None;
    9048        1813 :     if (eAccess == GA_Update)
    9049             :     {
    9050        1807 :         if (!m_aoGCPs.empty())
    9051             :         {
    9052           1 :             ReportError(CE_Warning, CPLE_AppDefined,
    9053             :                         "GCPs previously set are going to be cleared "
    9054             :                         "due to the setting of a geotransform.");
    9055           1 :             m_bForceUnsetGTOrGCPs = true;
    9056           1 :             m_aoGCPs.clear();
    9057             :         }
    9058        1806 :         else if (gt.xorig == 0.0 && gt.xscale == 0.0 && gt.xrot == 0.0 &&
    9059           2 :                  gt.yorig == 0.0 && gt.yrot == 0.0 && gt.yscale == 0.0)
    9060             :         {
    9061           2 :             if (m_bGeoTransformValid)
    9062             :             {
    9063           2 :                 m_bForceUnsetGTOrGCPs = true;
    9064           2 :                 m_bGeoTIFFInfoChanged = true;
    9065             :             }
    9066           2 :             m_bGeoTransformValid = false;
    9067           2 :             m_gt = gt;
    9068           2 :             return CE_None;
    9069             :         }
    9070             : 
    9071        3619 :         if ((m_eProfile == GTiffProfile::BASELINE) &&
    9072           9 :             !CPLFetchBool(m_papszCreationOptions, "TFW", false) &&
    9073        1819 :             !CPLFetchBool(m_papszCreationOptions, "WORLDFILE", false) &&
    9074           5 :             (GetPamFlags() & GPF_DISABLED) == 0)
    9075             :         {
    9076           5 :             eErr = GDALPamDataset::SetGeoTransform(gt);
    9077             :         }
    9078             :         else
    9079             :         {
    9080             :             // Cancel any existing geotransform from PAM file.
    9081        1800 :             GDALPamDataset::DeleteGeoTransform();
    9082        1800 :             m_bGeoTIFFInfoChanged = true;
    9083             :         }
    9084             :     }
    9085             :     else
    9086             :     {
    9087           6 :         CPLDebug("GTIFF", "SetGeoTransform() goes to PAM instead of TIFF tags");
    9088           6 :         eErr = GDALPamDataset::SetGeoTransform(gt);
    9089             :     }
    9090             : 
    9091        1811 :     if (eErr == CE_None)
    9092             :     {
    9093        1811 :         m_gt = gt;
    9094        1811 :         m_bGeoTransformValid = true;
    9095             :     }
    9096             : 
    9097        1811 :     return eErr;
    9098             : }
    9099             : 
    9100             : /************************************************************************/
    9101             : /*                              SetGCPs()                               */
    9102             : /************************************************************************/
    9103             : 
    9104          23 : CPLErr GTiffDataset::SetGCPs(int nGCPCountIn, const GDAL_GCP *pasGCPListIn,
    9105             :                              const OGRSpatialReference *poGCPSRS)
    9106             : {
    9107          23 :     CPLErr eErr = CE_None;
    9108          23 :     LoadGeoreferencingAndPamIfNeeded();
    9109          23 :     LookForProjection();
    9110             : 
    9111          23 :     if (eAccess == GA_Update)
    9112             :     {
    9113          21 :         if (!m_aoGCPs.empty() && nGCPCountIn == 0)
    9114             :         {
    9115           3 :             m_bForceUnsetGTOrGCPs = true;
    9116             :         }
    9117          18 :         else if (nGCPCountIn > 0 && m_bGeoTransformValid)
    9118             :         {
    9119           5 :             ReportError(CE_Warning, CPLE_AppDefined,
    9120             :                         "A geotransform previously set is going to be cleared "
    9121             :                         "due to the setting of GCPs.");
    9122           5 :             m_gt = GDALGeoTransform();
    9123           5 :             m_bGeoTransformValid = false;
    9124           5 :             m_bForceUnsetGTOrGCPs = true;
    9125             :         }
    9126          21 :         if ((m_eProfile == GTiffProfile::BASELINE) &&
    9127           0 :             (GetPamFlags() & GPF_DISABLED) == 0)
    9128             :         {
    9129           0 :             eErr = GDALPamDataset::SetGCPs(nGCPCountIn, pasGCPListIn, poGCPSRS);
    9130             :         }
    9131             :         else
    9132             :         {
    9133          21 :             if (nGCPCountIn > knMAX_GCP_COUNT)
    9134             :             {
    9135           2 :                 if (GDALPamDataset::GetGCPCount() == 0 && !m_aoGCPs.empty())
    9136             :                 {
    9137           1 :                     m_bForceUnsetGTOrGCPs = true;
    9138             :                 }
    9139           2 :                 ReportError(CE_Warning, CPLE_AppDefined,
    9140             :                             "Trying to write %d GCPs, whereas the maximum "
    9141             :                             "supported in GeoTIFF tag is %d. "
    9142             :                             "Falling back to writing them to PAM",
    9143             :                             nGCPCountIn, knMAX_GCP_COUNT);
    9144           2 :                 eErr = GDALPamDataset::SetGCPs(nGCPCountIn, pasGCPListIn,
    9145             :                                                poGCPSRS);
    9146             :             }
    9147          19 :             else if (GDALPamDataset::GetGCPCount() > 0)
    9148             :             {
    9149             :                 // Cancel any existing GCPs from PAM file.
    9150           1 :                 GDALPamDataset::SetGCPs(
    9151             :                     0, nullptr,
    9152             :                     static_cast<const OGRSpatialReference *>(nullptr));
    9153             :             }
    9154          21 :             m_bGeoTIFFInfoChanged = true;
    9155             :         }
    9156             :     }
    9157             :     else
    9158             :     {
    9159           2 :         CPLDebug("GTIFF", "SetGCPs() goes to PAM instead of TIFF tags");
    9160           2 :         eErr = GDALPamDataset::SetGCPs(nGCPCountIn, pasGCPListIn, poGCPSRS);
    9161             :     }
    9162             : 
    9163          23 :     if (eErr == CE_None)
    9164             :     {
    9165          23 :         if (poGCPSRS == nullptr || poGCPSRS->IsEmpty())
    9166             :         {
    9167          12 :             if (!m_oSRS.IsEmpty())
    9168             :             {
    9169           5 :                 m_bForceUnsetProjection = true;
    9170             :             }
    9171          12 :             m_oSRS.Clear();
    9172             :         }
    9173             :         else
    9174             :         {
    9175          11 :             m_oSRS = *poGCPSRS;
    9176          11 :             m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    9177             :         }
    9178             : 
    9179          23 :         m_aoGCPs = gdal::GCP::fromC(pasGCPListIn, nGCPCountIn);
    9180             :     }
    9181             : 
    9182          23 :     return eErr;
    9183             : }
    9184             : 
    9185             : /************************************************************************/
    9186             : /*                            SetMetadata()                             */
    9187             : /************************************************************************/
    9188        2698 : CPLErr GTiffDataset::SetMetadata(CSLConstList papszMD, const char *pszDomain)
    9189             : 
    9190             : {
    9191        2698 :     LoadGeoreferencingAndPamIfNeeded();
    9192             : 
    9193        2698 :     if (m_bStreamingOut && m_bCrystalized)
    9194             :     {
    9195           1 :         ReportError(
    9196             :             CE_Failure, CPLE_NotSupported,
    9197             :             "Cannot modify metadata at that point in a streamed output file");
    9198           1 :         return CE_Failure;
    9199             :     }
    9200             : 
    9201        2697 :     if (pszDomain && EQUAL(pszDomain, "json:ISIS3"))
    9202             :     {
    9203           5 :         m_oISIS3Metadata.Deinit();
    9204           5 :         m_oMapISIS3MetadataItems.clear();
    9205             :     }
    9206             : 
    9207        2697 :     CPLErr eErr = CE_None;
    9208        2697 :     if (eAccess == GA_Update)
    9209             :     {
    9210        2694 :         if (pszDomain != nullptr && EQUAL(pszDomain, MD_DOMAIN_RPC))
    9211             :         {
    9212             :             // So that a subsequent GetMetadata() wouldn't override our new
    9213             :             // values
    9214          22 :             LoadMetadata();
    9215          22 :             m_bForceUnsetRPC = (CSLCount(papszMD) == 0);
    9216             :         }
    9217             : 
    9218        2694 :         if ((papszMD != nullptr) && (pszDomain != nullptr) &&
    9219        1858 :             EQUAL(pszDomain, "COLOR_PROFILE"))
    9220             :         {
    9221           0 :             m_bColorProfileMetadataChanged = true;
    9222             :         }
    9223        2694 :         else if (pszDomain == nullptr || !EQUAL(pszDomain, "_temporary_"))
    9224             :         {
    9225        2694 :             m_bMetadataChanged = true;
    9226             :             // Cancel any existing metadata from PAM file.
    9227        2694 :             if (GDALPamDataset::GetMetadata(pszDomain) != nullptr)
    9228           1 :                 GDALPamDataset::SetMetadata(nullptr, pszDomain);
    9229             :         }
    9230             : 
    9231        5352 :         if ((pszDomain == nullptr || EQUAL(pszDomain, "")) &&
    9232        2658 :             CSLFetchNameValue(papszMD, GDALMD_AREA_OR_POINT) != nullptr)
    9233             :         {
    9234        2022 :             const char *pszPrevValue = GetMetadataItem(GDALMD_AREA_OR_POINT);
    9235             :             const char *pszNewValue =
    9236        2022 :                 CSLFetchNameValue(papszMD, GDALMD_AREA_OR_POINT);
    9237        2022 :             if (pszPrevValue == nullptr || pszNewValue == nullptr ||
    9238        1600 :                 !EQUAL(pszPrevValue, pszNewValue))
    9239             :             {
    9240         426 :                 LookForProjection();
    9241         426 :                 m_bGeoTIFFInfoChanged = true;
    9242             :             }
    9243             :         }
    9244             : 
    9245        2694 :         if (pszDomain != nullptr && EQUAL(pszDomain, "xml:XMP"))
    9246             :         {
    9247           2 :             if (papszMD != nullptr && *papszMD != nullptr)
    9248             :             {
    9249           1 :                 int nTagSize = static_cast<int>(strlen(*papszMD));
    9250           1 :                 TIFFSetField(m_hTIFF, TIFFTAG_XMLPACKET, nTagSize, *papszMD);
    9251             :             }
    9252             :             else
    9253             :             {
    9254           1 :                 TIFFUnsetField(m_hTIFF, TIFFTAG_XMLPACKET);
    9255             :             }
    9256             :         }
    9257             :     }
    9258             :     else
    9259             :     {
    9260           3 :         CPLDebug(
    9261             :             "GTIFF",
    9262             :             "GTiffDataset::SetMetadata() goes to PAM instead of TIFF tags");
    9263           3 :         eErr = GDALPamDataset::SetMetadata(papszMD, pszDomain);
    9264             :     }
    9265             : 
    9266        2697 :     if (eErr == CE_None)
    9267             :     {
    9268        2697 :         eErr = m_oGTiffMDMD.SetMetadata(papszMD, pszDomain);
    9269             :     }
    9270        2697 :     return eErr;
    9271             : }
    9272             : 
    9273             : /************************************************************************/
    9274             : /*                          SetMetadataItem()                           */
    9275             : /************************************************************************/
    9276             : 
    9277        5844 : CPLErr GTiffDataset::SetMetadataItem(const char *pszName, const char *pszValue,
    9278             :                                      const char *pszDomain)
    9279             : 
    9280             : {
    9281        5844 :     LoadGeoreferencingAndPamIfNeeded();
    9282             : 
    9283        5844 :     if (m_bStreamingOut && m_bCrystalized)
    9284             :     {
    9285           1 :         ReportError(
    9286             :             CE_Failure, CPLE_NotSupported,
    9287             :             "Cannot modify metadata at that point in a streamed output file");
    9288           1 :         return CE_Failure;
    9289             :     }
    9290             : 
    9291        5843 :     if (pszDomain && EQUAL(pszDomain, "json:ISIS3"))
    9292             :     {
    9293           1 :         ReportError(CE_Failure, CPLE_NotSupported,
    9294             :                     "Updating part of json:ISIS3 is not supported. "
    9295             :                     "Use SetMetadata() instead");
    9296           1 :         return CE_Failure;
    9297             :     }
    9298             : 
    9299        5842 :     CPLErr eErr = CE_None;
    9300        5842 :     if (eAccess == GA_Update)
    9301             :     {
    9302        5835 :         if ((pszDomain != nullptr) && EQUAL(pszDomain, "COLOR_PROFILE"))
    9303             :         {
    9304           8 :             m_bColorProfileMetadataChanged = true;
    9305             :         }
    9306        5827 :         else if (pszDomain == nullptr || !EQUAL(pszDomain, "_temporary_"))
    9307             :         {
    9308        5827 :             m_bMetadataChanged = true;
    9309             :             // Cancel any existing metadata from PAM file.
    9310        5827 :             if (GDALPamDataset::GetMetadataItem(pszName, pszDomain) != nullptr)
    9311           1 :                 GDALPamDataset::SetMetadataItem(pszName, nullptr, pszDomain);
    9312             :         }
    9313             : 
    9314        5835 :         if ((pszDomain == nullptr || EQUAL(pszDomain, "")) &&
    9315          82 :             pszName != nullptr && EQUAL(pszName, GDALMD_AREA_OR_POINT))
    9316             :         {
    9317           7 :             LookForProjection();
    9318           7 :             m_bGeoTIFFInfoChanged = true;
    9319             :         }
    9320             :     }
    9321             :     else
    9322             :     {
    9323           7 :         CPLDebug(
    9324             :             "GTIFF",
    9325             :             "GTiffDataset::SetMetadataItem() goes to PAM instead of TIFF tags");
    9326           7 :         eErr = GDALPamDataset::SetMetadataItem(pszName, pszValue, pszDomain);
    9327             :     }
    9328             : 
    9329        5842 :     if (eErr == CE_None)
    9330             :     {
    9331        5842 :         eErr = m_oGTiffMDMD.SetMetadataItem(pszName, pszValue, pszDomain);
    9332             :     }
    9333             : 
    9334        5842 :     return eErr;
    9335             : }
    9336             : 
    9337             : /************************************************************************/
    9338             : /*                           CreateMaskBand()                           */
    9339             : /************************************************************************/
    9340             : 
    9341          98 : CPLErr GTiffDataset::CreateMaskBand(int nFlagsIn)
    9342             : {
    9343          98 :     ScanDirectories();
    9344             : 
    9345          98 :     if (m_poMaskDS != nullptr)
    9346             :     {
    9347           1 :         ReportError(CE_Failure, CPLE_AppDefined,
    9348             :                     "This TIFF dataset has already an internal mask band");
    9349           1 :         return CE_Failure;
    9350             :     }
    9351          97 :     else if (MustCreateInternalMask())
    9352             :     {
    9353          84 :         if (nFlagsIn != GMF_PER_DATASET)
    9354             :         {
    9355           1 :             ReportError(CE_Failure, CPLE_AppDefined,
    9356             :                         "The only flag value supported for internal mask is "
    9357             :                         "GMF_PER_DATASET");
    9358           1 :             return CE_Failure;
    9359             :         }
    9360             : 
    9361          83 :         int l_nCompression = COMPRESSION_PACKBITS;
    9362          83 :         if (strstr(GDALGetMetadataItem(GDALGetDriverByName("GTiff"),
    9363             :                                        GDAL_DMD_CREATIONOPTIONLIST, nullptr),
    9364          83 :                    "<Value>DEFLATE</Value>") != nullptr)
    9365          83 :             l_nCompression = COMPRESSION_ADOBE_DEFLATE;
    9366             : 
    9367             :         /* --------------------------------------------------------------------
    9368             :          */
    9369             :         /*      If we don't have read access, then create the mask externally.
    9370             :          */
    9371             :         /* --------------------------------------------------------------------
    9372             :          */
    9373          83 :         if (GetAccess() != GA_Update)
    9374             :         {
    9375           1 :             ReportError(CE_Warning, CPLE_AppDefined,
    9376             :                         "File open for read-only accessing, "
    9377             :                         "creating mask externally.");
    9378             : 
    9379           1 :             return GDALPamDataset::CreateMaskBand(nFlagsIn);
    9380             :         }
    9381             : 
    9382          82 :         if (m_bLayoutIFDSBeforeData && !m_bKnownIncompatibleEdition &&
    9383           0 :             !m_bWriteKnownIncompatibleEdition)
    9384             :         {
    9385           0 :             ReportError(CE_Warning, CPLE_AppDefined,
    9386             :                         "Adding a mask invalidates the "
    9387             :                         "LAYOUT=IFDS_BEFORE_DATA property");
    9388           0 :             m_bKnownIncompatibleEdition = true;
    9389           0 :             m_bWriteKnownIncompatibleEdition = true;
    9390             :         }
    9391             : 
    9392          82 :         bool bIsOverview = false;
    9393          82 :         uint32_t nSubType = 0;
    9394          82 :         if (TIFFGetField(m_hTIFF, TIFFTAG_SUBFILETYPE, &nSubType))
    9395             :         {
    9396           8 :             bIsOverview = (nSubType & FILETYPE_REDUCEDIMAGE) != 0;
    9397             : 
    9398           8 :             if ((nSubType & FILETYPE_MASK) != 0)
    9399             :             {
    9400           0 :                 ReportError(CE_Failure, CPLE_AppDefined,
    9401             :                             "Cannot create a mask on a TIFF mask IFD !");
    9402           0 :                 return CE_Failure;
    9403             :             }
    9404             :         }
    9405             : 
    9406          82 :         const int bIsTiled = TIFFIsTiled(m_hTIFF);
    9407             : 
    9408          82 :         FlushDirectory();
    9409             : 
    9410          82 :         const toff_t nOffset = GTIFFWriteDirectory(
    9411             :             m_hTIFF,
    9412             :             bIsOverview ? FILETYPE_REDUCEDIMAGE | FILETYPE_MASK : FILETYPE_MASK,
    9413             :             nRasterXSize, nRasterYSize, 1, PLANARCONFIG_CONTIG, 1,
    9414             :             m_nBlockXSize, m_nBlockYSize, bIsTiled, l_nCompression,
    9415             :             PHOTOMETRIC_MASK, PREDICTOR_NONE, SAMPLEFORMAT_UINT, nullptr,
    9416             :             nullptr, nullptr, 0, nullptr, "", nullptr, nullptr, nullptr,
    9417          82 :             nullptr, m_bWriteCOGLayout);
    9418             : 
    9419          82 :         ReloadDirectory();
    9420             : 
    9421          82 :         if (nOffset == 0)
    9422           0 :             return CE_Failure;
    9423             : 
    9424          82 :         m_poMaskDS = std::make_shared<GTiffDataset>();
    9425          82 :         m_poMaskDS->eAccess = GA_Update;
    9426          82 :         m_poMaskDS->m_poBaseDS = this;
    9427          82 :         m_poMaskDS->m_poImageryDS = this;
    9428          82 :         m_poMaskDS->ShareLockWithParentDataset(this);
    9429          82 :         m_poMaskDS->m_osFilename = m_osFilename;
    9430          82 :         m_poMaskDS->m_bPromoteTo8Bits = CPLTestBool(
    9431             :             CPLGetConfigOption("GDAL_TIFF_INTERNAL_MASK_TO_8BIT", "YES"));
    9432          82 :         return m_poMaskDS->OpenOffset(VSI_TIFFOpenChild(m_hTIFF), nOffset,
    9433          82 :                                       GA_Update);
    9434             :     }
    9435             : 
    9436          13 :     return GDALPamDataset::CreateMaskBand(nFlagsIn);
    9437             : }
    9438             : 
    9439             : /************************************************************************/
    9440             : /*                       MustCreateInternalMask()                       */
    9441             : /************************************************************************/
    9442             : 
    9443         135 : bool GTiffDataset::MustCreateInternalMask()
    9444             : {
    9445         135 :     return CPLTestBool(CPLGetConfigOption("GDAL_TIFF_INTERNAL_MASK", "YES"));
    9446             : }
    9447             : 
    9448             : /************************************************************************/
    9449             : /*                           CreateMaskBand()                           */
    9450             : /************************************************************************/
    9451             : 
    9452          29 : CPLErr GTiffRasterBand::CreateMaskBand(int nFlagsIn)
    9453             : {
    9454          29 :     m_poGDS->ScanDirectories();
    9455             : 
    9456          29 :     if (m_poGDS->m_poMaskDS != nullptr)
    9457             :     {
    9458           5 :         ReportError(CE_Failure, CPLE_AppDefined,
    9459             :                     "This TIFF dataset has already an internal mask band");
    9460           5 :         return CE_Failure;
    9461             :     }
    9462             : 
    9463             :     const char *pszGDAL_TIFF_INTERNAL_MASK =
    9464          24 :         CPLGetConfigOption("GDAL_TIFF_INTERNAL_MASK", nullptr);
    9465          27 :     if ((pszGDAL_TIFF_INTERNAL_MASK &&
    9466          24 :          CPLTestBool(pszGDAL_TIFF_INTERNAL_MASK)) ||
    9467             :         nFlagsIn == GMF_PER_DATASET)
    9468             :     {
    9469          16 :         return m_poGDS->CreateMaskBand(nFlagsIn);
    9470             :     }
    9471             : 
    9472           8 :     return GDALPamRasterBand::CreateMaskBand(nFlagsIn);
    9473             : }
    9474             : 
    9475             : /************************************************************************/
    9476             : /*                            ClampCTEntry()                            */
    9477             : /************************************************************************/
    9478             : 
    9479      236415 : /* static */ unsigned short GTiffDataset::ClampCTEntry(int iColor, int iComp,
    9480             :                                                        int nCTEntryVal,
    9481             :                                                        int nMultFactor)
    9482             : {
    9483      236415 :     const int nVal = nCTEntryVal * nMultFactor;
    9484      236415 :     if (nVal < 0)
    9485             :     {
    9486           0 :         CPLError(CE_Warning, CPLE_AppDefined,
    9487             :                  "Color table entry [%d][%d] = %d, clamped to 0", iColor, iComp,
    9488             :                  nCTEntryVal);
    9489           0 :         return 0;
    9490             :     }
    9491      236415 :     if (nVal > 65535)
    9492             :     {
    9493           2 :         CPLError(CE_Warning, CPLE_AppDefined,
    9494             :                  "Color table entry [%d][%d] = %d, clamped to 65535", iColor,
    9495             :                  iComp, nCTEntryVal);
    9496           2 :         return 65535;
    9497             :     }
    9498      236413 :     return static_cast<unsigned short>(nVal);
    9499             : }

Generated by: LCOV version 1.14