LCOV - code coverage report
Current view: top level - frmts/gtiff - gtiffdataset_write.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 3894 4252 91.6 %
Date: 2026-05-04 15:01:57 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       17808 : static signed char GTiffGetWebPLevel(CSLConstList papszOptions)
      79             : {
      80       17808 :     int nWebPLevel = DEFAULT_WEBP_LEVEL;
      81       17808 :     const char *pszValue = CSLFetchNameValue(papszOptions, "WEBP_LEVEL");
      82       17808 :     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       17808 :     return static_cast<signed char>(nWebPLevel);
      93             : }
      94             : 
      95       17814 : static bool GTiffGetWebPLossless(CSLConstList papszOptions)
      96             : {
      97       17814 :     return CPLFetchBool(papszOptions, "WEBP_LOSSLESS", false);
      98             : }
      99             : 
     100       17880 : static double GTiffGetLERCMaxZError(CSLConstList papszOptions)
     101             : {
     102       17880 :     return CPLAtof(CSLFetchNameValueDef(papszOptions, "MAX_Z_ERROR", "0.0"));
     103             : }
     104             : 
     105        7977 : static double GTiffGetLERCMaxZErrorOverview(CSLConstList papszOptions)
     106             : {
     107        7977 :     return CPLAtof(CSLFetchNameValueDef(
     108             :         papszOptions, "MAX_Z_ERROR_OVERVIEW",
     109        7977 :         CSLFetchNameValueDef(papszOptions, "MAX_Z_ERROR", "0.0")));
     110             : }
     111             : 
     112             : #if HAVE_JXL
     113       17884 : static bool GTiffGetJXLLossless(CSLConstList papszOptions,
     114             :                                 bool *pbIsSpecified = nullptr)
     115             : {
     116       17884 :     const char *pszVal = CSLFetchNameValue(papszOptions, "JXL_LOSSLESS");
     117       17884 :     if (pbIsSpecified)
     118        9903 :         *pbIsSpecified = pszVal != nullptr;
     119       17884 :     return pszVal == nullptr || CPLTestBool(pszVal);
     120             : }
     121             : 
     122       17884 : static uint32_t GTiffGetJXLEffort(CSLConstList papszOptions)
     123             : {
     124       17884 :     return atoi(CSLFetchNameValueDef(papszOptions, "JXL_EFFORT", "5"));
     125             : }
     126             : 
     127       17802 : static float GTiffGetJXLDistance(CSLConstList papszOptions,
     128             :                                  bool *pbIsSpecified = nullptr)
     129             : {
     130       17802 :     const char *pszVal = CSLFetchNameValue(papszOptions, "JXL_DISTANCE");
     131       17802 :     if (pbIsSpecified)
     132        9903 :         *pbIsSpecified = pszVal != nullptr;
     133       17802 :     return pszVal == nullptr ? 1.0f : static_cast<float>(CPLAtof(pszVal));
     134             : }
     135             : 
     136       17884 : static float GTiffGetJXLAlphaDistance(CSLConstList papszOptions,
     137             :                                       bool *pbIsSpecified = nullptr)
     138             : {
     139       17884 :     const char *pszVal = CSLFetchNameValue(papszOptions, "JXL_ALPHA_DISTANCE");
     140       17884 :     if (pbIsSpecified)
     141        9903 :         *pbIsSpecified = pszVal != nullptr;
     142       17884 :     return pszVal == nullptr ? -1.0f : static_cast<float>(CPLAtof(pszVal));
     143             : }
     144             : 
     145             : #endif
     146             : 
     147             : /************************************************************************/
     148             : /*                           FillEmptyTiles()                           */
     149             : /************************************************************************/
     150             : 
     151        8167 : CPLErr GTiffDataset::FillEmptyTiles()
     152             : 
     153             : {
     154             :     /* -------------------------------------------------------------------- */
     155             :     /*      How many blocks are there in this file?                         */
     156             :     /* -------------------------------------------------------------------- */
     157       16334 :     const int nBlockCount = m_nPlanarConfig == PLANARCONFIG_SEPARATE
     158        8167 :                                 ? m_nBlocksPerBand * nBands
     159             :                                 : m_nBlocksPerBand;
     160             : 
     161             :     /* -------------------------------------------------------------------- */
     162             :     /*      Fetch block maps.                                               */
     163             :     /* -------------------------------------------------------------------- */
     164        8167 :     toff_t *panByteCounts = nullptr;
     165             : 
     166        8167 :     if (TIFFIsTiled(m_hTIFF))
     167        1147 :         TIFFGetField(m_hTIFF, TIFFTAG_TILEBYTECOUNTS, &panByteCounts);
     168             :     else
     169        7020 :         TIFFGetField(m_hTIFF, TIFFTAG_STRIPBYTECOUNTS, &panByteCounts);
     170             : 
     171        8167 :     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        8167 :         TIFFIsTiled(m_hTIFF) ? static_cast<GPtrDiff_t>(TIFFTileSize(m_hTIFF))
     184        7020 :                              : static_cast<GPtrDiff_t>(TIFFStripSize(m_hTIFF));
     185             : 
     186        8167 :     GByte *pabyData = static_cast<GByte *>(VSI_CALLOC_VERBOSE(nBlockBytes, 1));
     187        8167 :     if (pabyData == nullptr)
     188             :     {
     189           0 :         return CE_Failure;
     190             :     }
     191             : 
     192             :     // Force tiles completely filled with the nodata value to be written.
     193        8167 :     m_bWriteEmptyTiles = true;
     194             : 
     195             :     /* -------------------------------------------------------------------- */
     196             :     /*      If set, fill data buffer with no data value.                    */
     197             :     /* -------------------------------------------------------------------- */
     198        8167 :     if ((m_bNoDataSet && m_dfNoDataValue != 0.0) ||
     199        7899 :         (m_bNoDataSetAsInt64 && m_nNoDataValueInt64 != 0) ||
     200        7894 :         (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        7889 :     else if (m_nCompression == COMPRESSION_NONE && (m_nBitsPerSample % 8) == 0)
     316             :     {
     317        6335 :         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        6335 :         int nCountBlocksToZero = 0;
     321     2329970 :         for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
     322             :         {
     323     2323630 :             if (panByteCounts[iBlock] == 0)
     324             :             {
     325     2228140 :                 if (nCountBlocksToZero == 0)
     326             :                 {
     327        1138 :                     const bool bWriteEmptyTilesBak = m_bWriteEmptyTiles;
     328        1138 :                     m_bWriteEmptyTiles = true;
     329        1138 :                     const bool bOK = WriteEncodedTileOrStrip(iBlock, pabyData,
     330        1138 :                                                              FALSE) == CE_None;
     331        1138 :                     m_bWriteEmptyTiles = bWriteEmptyTilesBak;
     332        1138 :                     if (!bOK)
     333             :                     {
     334           2 :                         eErr = CE_Failure;
     335           2 :                         break;
     336             :                     }
     337             :                 }
     338     2228140 :                 nCountBlocksToZero++;
     339             :             }
     340             :         }
     341        6335 :         CPLFree(pabyData);
     342             : 
     343        6335 :         --nCountBlocksToZero;
     344             : 
     345             :         // And then seek to end of file for other ones.
     346        6335 :         if (nCountBlocksToZero > 0)
     347             :         {
     348         349 :             toff_t *panByteOffsets = nullptr;
     349             : 
     350         349 :             if (TIFFIsTiled(m_hTIFF))
     351         101 :                 TIFFGetField(m_hTIFF, TIFFTAG_TILEOFFSETS, &panByteOffsets);
     352             :             else
     353         248 :                 TIFFGetField(m_hTIFF, TIFFTAG_STRIPOFFSETS, &panByteOffsets);
     354             : 
     355         349 :             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         349 :             VSILFILE *fpTIF = VSI_TIFFGetVSILFile(TIFFClientdata(m_hTIFF));
     364         349 :             VSIFSeekL(fpTIF, 0, SEEK_END);
     365         349 :             const vsi_l_offset nOffset = VSIFTellL(fpTIF);
     366             : 
     367         349 :             vsi_l_offset iBlockToZero = 0;
     368     2236250 :             for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
     369             :             {
     370     2235900 :                 if (panByteCounts[iBlock] == 0)
     371             :                 {
     372     2227010 :                     panByteOffsets[iBlock] = static_cast<toff_t>(
     373     2227010 :                         nOffset + iBlockToZero * nBlockBytes);
     374     2227010 :                     panByteCounts[iBlock] = nBlockBytes;
     375     2227010 :                     iBlockToZero++;
     376             :                 }
     377             :             }
     378         349 :             CPLAssert(iBlockToZero ==
     379             :                       static_cast<vsi_l_offset>(nCountBlocksToZero));
     380             : 
     381         349 :             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        6335 :         return eErr;
     390             :     }
     391             : 
     392             :     /* -------------------------------------------------------------------- */
     393             :     /*      Check all blocks, writing out data for uninitialized blocks.    */
     394             :     /* -------------------------------------------------------------------- */
     395             : 
     396        1821 :     GByte *pabyRaw = nullptr;
     397        1821 :     vsi_l_offset nRawSize = 0;
     398        1821 :     CPLErr eErr = CE_None;
     399       56484 :     for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
     400             :     {
     401       54670 :         if (panByteCounts[iBlock] == 0)
     402             :         {
     403       17544 :             if (pabyRaw == nullptr)
     404             :             {
     405       10161 :                 if (WriteEncodedTileOrStrip(iBlock, pabyData, FALSE) != CE_None)
     406             :                 {
     407           7 :                     eErr = CE_Failure;
     408           7 :                     break;
     409             :                 }
     410             : 
     411       10154 :                 vsi_l_offset nOffset = 0;
     412       10154 :                 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       10154 :                 if (m_nCompression != COMPRESSION_NONE)
     418             :                 {
     419             :                     pabyRaw = static_cast<GByte *>(
     420         480 :                         VSI_MALLOC_VERBOSE(static_cast<size_t>(nRawSize)));
     421         480 :                     if (pabyRaw)
     422             :                     {
     423             :                         VSILFILE *fp =
     424         480 :                             VSI_TIFFGetVSILFile(TIFFClientdata(m_hTIFF));
     425         480 :                         const vsi_l_offset nCurOffset = VSIFTellL(fp);
     426         480 :                         VSIFSeekL(fp, nOffset, SEEK_SET);
     427         480 :                         VSIFReadL(pabyRaw, 1, static_cast<size_t>(nRawSize),
     428             :                                   fp);
     429         480 :                         VSIFSeekL(fp, nCurOffset, SEEK_SET);
     430             :                     }
     431             :                 }
     432             :             }
     433             :             else
     434             :             {
     435        7383 :                 WriteRawStripOrTile(iBlock, pabyRaw,
     436             :                                     static_cast<GPtrDiff_t>(nRawSize));
     437             :             }
     438             :         }
     439             :     }
     440             : 
     441        1821 :     CPLFree(pabyData);
     442        1821 :     VSIFree(pabyRaw);
     443        1821 :     return eErr;
     444             : }
     445             : 
     446             : /************************************************************************/
     447             : /*                           HasOnlyNoData()                            */
     448             : /************************************************************************/
     449             : 
     450       42815 : bool GTiffDataset::HasOnlyNoData(const void *pBuffer, int nWidth, int nHeight,
     451             :                                  int nLineStride, int nComponents)
     452             : {
     453       42815 :     if (m_nSampleFormat == SAMPLEFORMAT_COMPLEXINT ||
     454       42815 :         m_nSampleFormat == SAMPLEFORMAT_COMPLEXIEEEFP)
     455           0 :         return false;
     456       42815 :     if (m_bNoDataSetAsInt64 || m_bNoDataSetAsUInt64)
     457           6 :         return false;  // FIXME: over pessimistic
     458       85618 :     return GDALBufferHasOnlyNoData(
     459       42809 :         pBuffer, m_bNoDataSet ? m_dfNoDataValue : 0.0, nWidth, nHeight,
     460       42809 :         nLineStride, nComponents, m_nBitsPerSample,
     461       42809 :         m_nSampleFormat == SAMPLEFORMAT_UINT  ? GSF_UNSIGNED_INT
     462        4824 :         : m_nSampleFormat == SAMPLEFORMAT_INT ? GSF_SIGNED_INT
     463       42809 :                                               : GSF_FLOATING_POINT);
     464             : }
     465             : 
     466             : /************************************************************************/
     467             : /*                     IsFirstPixelEqualToNoData()                      */
     468             : /************************************************************************/
     469             : 
     470      169323 : inline bool GTiffDataset::IsFirstPixelEqualToNoData(const void *pBuffer)
     471             : {
     472      169323 :     const GDALDataType eDT = GetRasterBand(1)->GetRasterDataType();
     473      169323 :     const double dfEffectiveNoData = (m_bNoDataSet) ? m_dfNoDataValue : 0.0;
     474      169323 :     if (m_bNoDataSetAsInt64 || m_bNoDataSetAsUInt64)
     475          10 :         return true;  // FIXME: over pessimistic
     476      169313 :     if (m_nBitsPerSample == 8 ||
     477       58898 :         (m_nBitsPerSample < 8 && dfEffectiveNoData == 0))
     478             :     {
     479      113861 :         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      227413 :         return GDALIsValueInRange<GByte>(dfEffectiveNoData) &&
     486      113691 :                *(static_cast<const GByte *>(pBuffer)) ==
     487      227413 :                    static_cast<GByte>(dfEffectiveNoData);
     488             :     }
     489       55452 :     if (m_nBitsPerSample == 16 && eDT == GDT_UInt16)
     490             :     {
     491        4686 :         return GDALIsValueInRange<GUInt16>(dfEffectiveNoData) &&
     492        2343 :                *(static_cast<const GUInt16 *>(pBuffer)) ==
     493        4686 :                    static_cast<GUInt16>(dfEffectiveNoData);
     494             :     }
     495       53109 :     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       48871 :     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       48682 :     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       48429 :     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       48312 :     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       48194 :     if (m_nBitsPerSample == 32 && eDT == GDT_Float32)
     526             :     {
     527       41181 :         if (std::isnan(m_dfNoDataValue))
     528           3 :             return std::isnan(*(static_cast<const float *>(pBuffer)));
     529       82356 :         return GDALIsValueInRange<float>(dfEffectiveNoData) &&
     530       41178 :                *(static_cast<const float *>(pBuffer)) ==
     531       82356 :                    static_cast<float>(dfEffectiveNoData);
     532             :     }
     533        7013 :     if (m_nBitsPerSample == 64 && eDT == GDT_Float64)
     534             :     {
     535        4351 :         if (std::isnan(dfEffectiveNoData))
     536           3 :             return std::isnan(*(static_cast<const double *>(pBuffer)));
     537        4348 :         return *(static_cast<const double *>(pBuffer)) == dfEffectiveNoData;
     538             :     }
     539        2662 :     return false;
     540             : }
     541             : 
     542             : /************************************************************************/
     543             : /*                      WriteDealWithLercAndNan()                       */
     544             : /************************************************************************/
     545             : 
     546             : template <typename T>
     547           0 : void GTiffDataset::WriteDealWithLercAndNan(T *pBuffer, int nActualBlockWidth,
     548             :                                            int nActualBlockHeight,
     549             :                                            int nStrileHeight)
     550             : {
     551             :     // This method does 2 things:
     552             :     // - warn the user if he tries to write NaN values with libtiff < 4.6.1
     553             :     //   and multi-band PlanarConfig=Contig configuration
     554             :     // - and in right-most and bottom-most tiles, replace non accessible
     555             :     //   pixel values by a safe one.
     556             : 
     557           0 :     const auto fPaddingValue =
     558             : #if !defined(LIBTIFF_MULTIBAND_LERC_NAN_OK)
     559             :         m_nPlanarConfig == PLANARCONFIG_CONTIG && nBands > 1
     560             :             ? 0
     561             :             :
     562             : #endif
     563             :             std::numeric_limits<T>::quiet_NaN();
     564             : 
     565           0 :     const int nBandsPerStrile =
     566           0 :         m_nPlanarConfig == PLANARCONFIG_CONTIG ? nBands : 1;
     567           0 :     for (int j = 0; j < nActualBlockHeight; ++j)
     568             :     {
     569             : #if !defined(LIBTIFF_MULTIBAND_LERC_NAN_OK)
     570             :         static bool bHasWarned = false;
     571             :         if (m_nPlanarConfig == PLANARCONFIG_CONTIG && nBands > 1 && !bHasWarned)
     572             :         {
     573             :             for (int i = 0; i < nActualBlockWidth * nBandsPerStrile; ++i)
     574             :             {
     575             :                 if (std::isnan(
     576             :                         pBuffer[j * m_nBlockXSize * nBandsPerStrile + i]))
     577             :                 {
     578             :                     bHasWarned = true;
     579             :                     CPLError(CE_Warning, CPLE_AppDefined,
     580             :                              "libtiff < 4.6.1 does not handle properly NaN "
     581             :                              "values for multi-band PlanarConfig=Contig "
     582             :                              "configuration. As a workaround, you can set the "
     583             :                              "INTERLEAVE=BAND creation option.");
     584             :                     break;
     585             :                 }
     586             :             }
     587             :         }
     588             : #endif
     589           0 :         for (int i = nActualBlockWidth * nBandsPerStrile;
     590           0 :              i < m_nBlockXSize * nBandsPerStrile; ++i)
     591             :         {
     592           0 :             pBuffer[j * m_nBlockXSize * nBandsPerStrile + i] = fPaddingValue;
     593             :         }
     594             :     }
     595           0 :     for (int j = nActualBlockHeight; j < nStrileHeight; ++j)
     596             :     {
     597           0 :         for (int i = 0; i < m_nBlockXSize * nBandsPerStrile; ++i)
     598             :         {
     599           0 :             pBuffer[j * m_nBlockXSize * nBandsPerStrile + i] = fPaddingValue;
     600             :         }
     601             :     }
     602           0 : }
     603             : 
     604             : /************************************************************************/
     605             : /*                          WriteEncodedTile()                          */
     606             : /************************************************************************/
     607             : 
     608       50503 : bool GTiffDataset::WriteEncodedTile(uint32_t tile, GByte *pabyData,
     609             :                                     int bPreserveDataBuffer)
     610             : {
     611       50503 :     const int iColumn = (tile % m_nBlocksPerBand) % m_nBlocksPerRow;
     612       50503 :     const int iRow = (tile % m_nBlocksPerBand) / m_nBlocksPerRow;
     613             : 
     614      101006 :     const int nActualBlockWidth = (iColumn == m_nBlocksPerRow - 1)
     615       50503 :                                       ? nRasterXSize - iColumn * m_nBlockXSize
     616             :                                       : m_nBlockXSize;
     617      101006 :     const int nActualBlockHeight = (iRow == m_nBlocksPerColumn - 1)
     618       50503 :                                        ? nRasterYSize - iRow * m_nBlockYSize
     619             :                                        : m_nBlockYSize;
     620             : 
     621             :     /* -------------------------------------------------------------------- */
     622             :     /*      Don't write empty blocks in some cases.                         */
     623             :     /* -------------------------------------------------------------------- */
     624       50503 :     if (!m_bWriteEmptyTiles && IsFirstPixelEqualToNoData(pabyData))
     625             :     {
     626        1976 :         if (!IsBlockAvailable(tile, nullptr, nullptr, nullptr))
     627             :         {
     628        1976 :             const int nComponents =
     629        1976 :                 m_nPlanarConfig == PLANARCONFIG_CONTIG ? nBands : 1;
     630             : 
     631        1976 :             if (HasOnlyNoData(pabyData, nActualBlockWidth, nActualBlockHeight,
     632             :                               m_nBlockXSize, nComponents))
     633             :             {
     634        1200 :                 return true;
     635             :             }
     636             :         }
     637             :     }
     638             : 
     639             :     // Is this a partial right edge or bottom edge tile?
     640       95595 :     const bool bPartialTile = (nActualBlockWidth < m_nBlockXSize) ||
     641       46292 :                               (nActualBlockHeight < m_nBlockYSize);
     642             : 
     643             :     const bool bIsLercFloatingPoint =
     644       49369 :         m_nCompression == COMPRESSION_LERC &&
     645          66 :         (GetRasterBand(1)->GetRasterDataType() == GDT_Float32 ||
     646          64 :          GetRasterBand(1)->GetRasterDataType() == GDT_Float64);
     647             : 
     648             :     // Do we need to spread edge values right or down for a partial
     649             :     // JPEG encoded tile?  We do this to avoid edge artifacts.
     650             :     // We also need to be careful with LERC and NaN values
     651       49303 :     const bool bNeedTempBuffer =
     652       54007 :         bPartialTile &&
     653        4704 :         (m_nCompression == COMPRESSION_JPEG || bIsLercFloatingPoint);
     654             : 
     655             :     // If we need to fill out the tile, or if we want to prevent
     656             :     // TIFFWriteEncodedTile from altering the buffer as part of
     657             :     // byte swapping the data on write then we will need a temporary
     658             :     // working buffer.  If not, we can just do a direct write.
     659       49303 :     const GPtrDiff_t cc = static_cast<GPtrDiff_t>(TIFFTileSize(m_hTIFF));
     660             : 
     661       63612 :     if (bPreserveDataBuffer &&
     662       14309 :         (TIFFIsByteSwapped(m_hTIFF) || bNeedTempBuffer || m_panMaskOffsetLsb))
     663             :     {
     664         158 :         if (m_pabyTempWriteBuffer == nullptr)
     665             :         {
     666          35 :             m_pabyTempWriteBuffer = CPLMalloc(cc);
     667             :         }
     668         158 :         memcpy(m_pabyTempWriteBuffer, pabyData, cc);
     669             : 
     670         158 :         pabyData = static_cast<GByte *>(m_pabyTempWriteBuffer);
     671             :     }
     672             : 
     673             :     // Perform tile fill if needed.
     674             :     // TODO: we should also handle the case of nBitsPerSample == 12
     675             :     // but this is more involved.
     676       49303 :     if (bPartialTile && m_nCompression == COMPRESSION_JPEG &&
     677         134 :         m_nBitsPerSample == 8)
     678             :     {
     679         132 :         const int nComponents =
     680         132 :             m_nPlanarConfig == PLANARCONFIG_CONTIG ? nBands : 1;
     681             : 
     682         132 :         CPLDebug("GTiff", "Filling out jpeg edge tile on write.");
     683             : 
     684         132 :         const int nRightPixelsToFill =
     685         132 :             iColumn == m_nBlocksPerRow - 1
     686         132 :                 ? m_nBlockXSize * (iColumn + 1) - nRasterXSize
     687             :                 : 0;
     688         132 :         const int nBottomPixelsToFill =
     689         132 :             iRow == m_nBlocksPerColumn - 1
     690         132 :                 ? m_nBlockYSize * (iRow + 1) - nRasterYSize
     691             :                 : 0;
     692             : 
     693             :         // Fill out to the right.
     694         132 :         const int iSrcX = m_nBlockXSize - nRightPixelsToFill - 1;
     695             : 
     696       12461 :         for (int iX = iSrcX + 1; iX < m_nBlockXSize; ++iX)
     697             :         {
     698     3955880 :             for (int iY = 0; iY < m_nBlockYSize; ++iY)
     699             :             {
     700     3943550 :                 memcpy(pabyData +
     701     3943550 :                            (static_cast<GPtrDiff_t>(m_nBlockXSize) * iY + iX) *
     702     3943550 :                                nComponents,
     703     3943550 :                        pabyData + (static_cast<GPtrDiff_t>(m_nBlockXSize) * iY +
     704     3943550 :                                    iSrcX) *
     705     3943550 :                                       nComponents,
     706             :                        nComponents);
     707             :             }
     708             :         }
     709             : 
     710             :         // Now fill out the bottom.
     711         132 :         const int iSrcY = m_nBlockYSize - nBottomPixelsToFill - 1;
     712       17682 :         for (int iY = iSrcY + 1; iY < m_nBlockYSize; ++iY)
     713             :         {
     714       17550 :             memcpy(pabyData + static_cast<GPtrDiff_t>(m_nBlockXSize) *
     715       17550 :                                   nComponents * iY,
     716       17550 :                    pabyData + static_cast<GPtrDiff_t>(m_nBlockXSize) *
     717       17550 :                                   nComponents * iSrcY,
     718       17550 :                    static_cast<GPtrDiff_t>(m_nBlockXSize) * nComponents);
     719             :         }
     720             :     }
     721             : 
     722       49303 :     if (bIsLercFloatingPoint &&
     723             :         (bPartialTile
     724             : #if !defined(LIBTIFF_MULTIBAND_LERC_NAN_OK)
     725             :          /* libtiff < 4.6.1 doesn't generate a LERC mask for multi-band contig configuration */
     726             :          || (m_nPlanarConfig == PLANARCONFIG_CONTIG && nBands > 1)
     727             : #endif
     728             :              ))
     729             :     {
     730           0 :         if (GetRasterBand(1)->GetRasterDataType() == GDT_Float32)
     731           0 :             WriteDealWithLercAndNan(reinterpret_cast<float *>(pabyData),
     732             :                                     nActualBlockWidth, nActualBlockHeight,
     733             :                                     m_nBlockYSize);
     734             :         else
     735           0 :             WriteDealWithLercAndNan(reinterpret_cast<double *>(pabyData),
     736             :                                     nActualBlockWidth, nActualBlockHeight,
     737             :                                     m_nBlockYSize);
     738             :     }
     739             : 
     740       49303 :     if (m_panMaskOffsetLsb)
     741             :     {
     742           0 :         const int iBand = m_nPlanarConfig == PLANARCONFIG_SEPARATE
     743           0 :                               ? static_cast<int>(tile) / m_nBlocksPerBand
     744             :                               : -1;
     745           0 :         DiscardLsb(pabyData, cc, iBand);
     746             :     }
     747             : 
     748       49303 :     if (m_bStreamingOut)
     749             :     {
     750          17 :         if (tile != static_cast<uint32_t>(m_nLastWrittenBlockId + 1))
     751             :         {
     752           1 :             ReportError(CE_Failure, CPLE_NotSupported,
     753             :                         "Attempt to write block %d whereas %d was expected",
     754           1 :                         tile, m_nLastWrittenBlockId + 1);
     755           1 :             return false;
     756             :         }
     757          16 :         if (static_cast<GPtrDiff_t>(VSIFWriteL(pabyData, 1, cc, m_fpToWrite)) !=
     758             :             cc)
     759             :         {
     760           0 :             ReportError(CE_Failure, CPLE_FileIO,
     761             :                         "Could not write " CPL_FRMT_GUIB " bytes",
     762             :                         static_cast<GUIntBig>(cc));
     763           0 :             return false;
     764             :         }
     765          16 :         m_nLastWrittenBlockId = tile;
     766          16 :         return true;
     767             :     }
     768             : 
     769             :     /* -------------------------------------------------------------------- */
     770             :     /*      Should we do compression in a worker thread ?                   */
     771             :     /* -------------------------------------------------------------------- */
     772       49286 :     if (SubmitCompressionJob(tile, pabyData, cc, m_nBlockYSize))
     773       19939 :         return true;
     774             : 
     775       29347 :     return TIFFWriteEncodedTile(m_hTIFF, tile, pabyData, cc) == cc;
     776             : }
     777             : 
     778             : /************************************************************************/
     779             : /*                         WriteEncodedStrip()                          */
     780             : /************************************************************************/
     781             : 
     782      178334 : bool GTiffDataset::WriteEncodedStrip(uint32_t strip, GByte *pabyData,
     783             :                                      int bPreserveDataBuffer)
     784             : {
     785      178334 :     GPtrDiff_t cc = static_cast<GPtrDiff_t>(TIFFStripSize(m_hTIFF));
     786      178334 :     const auto ccFull = cc;
     787             : 
     788             :     /* -------------------------------------------------------------------- */
     789             :     /*      If this is the last strip in the image, and is partial, then    */
     790             :     /*      we need to trim the number of scanlines written to the          */
     791             :     /*      amount of valid data we have. (#2748)                           */
     792             :     /* -------------------------------------------------------------------- */
     793      178334 :     const int nStripWithinBand = strip % m_nBlocksPerBand;
     794      178334 :     int nStripHeight = m_nRowsPerStrip;
     795             : 
     796      178334 :     if (nStripWithinBand * nStripHeight > GetRasterYSize() - nStripHeight)
     797             :     {
     798         388 :         nStripHeight = GetRasterYSize() - nStripWithinBand * m_nRowsPerStrip;
     799         388 :         cc = (cc / m_nRowsPerStrip) * nStripHeight;
     800         776 :         CPLDebug("GTiff",
     801             :                  "Adjusted bytes to write from " CPL_FRMT_GUIB
     802             :                  " to " CPL_FRMT_GUIB ".",
     803         388 :                  static_cast<GUIntBig>(TIFFStripSize(m_hTIFF)),
     804             :                  static_cast<GUIntBig>(cc));
     805             :     }
     806             : 
     807             :     /* -------------------------------------------------------------------- */
     808             :     /*      Don't write empty blocks in some cases.                         */
     809             :     /* -------------------------------------------------------------------- */
     810      178334 :     if (!m_bWriteEmptyTiles && IsFirstPixelEqualToNoData(pabyData))
     811             :     {
     812       41013 :         if (!IsBlockAvailable(strip, nullptr, nullptr, nullptr))
     813             :         {
     814       40839 :             const int nComponents =
     815       40839 :                 m_nPlanarConfig == PLANARCONFIG_CONTIG ? nBands : 1;
     816             : 
     817       40839 :             if (HasOnlyNoData(pabyData, m_nBlockXSize, nStripHeight,
     818             :                               m_nBlockXSize, nComponents))
     819             :             {
     820       28308 :                 return true;
     821             :             }
     822             :         }
     823             :     }
     824             : 
     825             :     /* -------------------------------------------------------------------- */
     826             :     /*      TIFFWriteEncodedStrip can alter the passed buffer if            */
     827             :     /*      byte-swapping is necessary so we use a temporary buffer         */
     828             :     /*      before calling it.                                              */
     829             :     /* -------------------------------------------------------------------- */
     830      239521 :     if (bPreserveDataBuffer &&
     831       89495 :         (TIFFIsByteSwapped(m_hTIFF) || m_panMaskOffsetLsb))
     832             :     {
     833         294 :         if (m_pabyTempWriteBuffer == nullptr)
     834             :         {
     835         126 :             m_pabyTempWriteBuffer = CPLMalloc(ccFull);
     836             :         }
     837         294 :         memcpy(m_pabyTempWriteBuffer, pabyData, cc);
     838         294 :         pabyData = static_cast<GByte *>(m_pabyTempWriteBuffer);
     839             :     }
     840             : 
     841             : #if !defined(LIBTIFF_MULTIBAND_LERC_NAN_OK)
     842             :     const bool bIsLercFloatingPoint =
     843             :         m_nCompression == COMPRESSION_LERC &&
     844             :         (GetRasterBand(1)->GetRasterDataType() == GDT_Float32 ||
     845             :          GetRasterBand(1)->GetRasterDataType() == GDT_Float64);
     846             :     if (bIsLercFloatingPoint &&
     847             :         /* libtiff < 4.6.1 doesn't generate a LERC mask for multi-band contig configuration */
     848             :         m_nPlanarConfig == PLANARCONFIG_CONTIG && nBands > 1)
     849             :     {
     850             :         if (GetRasterBand(1)->GetRasterDataType() == GDT_Float32)
     851             :             WriteDealWithLercAndNan(reinterpret_cast<float *>(pabyData),
     852             :                                     m_nBlockXSize, nStripHeight, nStripHeight);
     853             :         else
     854             :             WriteDealWithLercAndNan(reinterpret_cast<double *>(pabyData),
     855             :                                     m_nBlockXSize, nStripHeight, nStripHeight);
     856             :     }
     857             : #endif
     858             : 
     859      150026 :     if (m_panMaskOffsetLsb)
     860             :     {
     861         366 :         int iBand = m_nPlanarConfig == PLANARCONFIG_SEPARATE
     862         183 :                         ? static_cast<int>(strip) / m_nBlocksPerBand
     863             :                         : -1;
     864         183 :         DiscardLsb(pabyData, cc, iBand);
     865             :     }
     866             : 
     867      150026 :     if (m_bStreamingOut)
     868             :     {
     869        1408 :         if (strip != static_cast<uint32_t>(m_nLastWrittenBlockId + 1))
     870             :         {
     871           1 :             ReportError(CE_Failure, CPLE_NotSupported,
     872             :                         "Attempt to write block %d whereas %d was expected",
     873           1 :                         strip, m_nLastWrittenBlockId + 1);
     874           1 :             return false;
     875             :         }
     876        1407 :         if (static_cast<GPtrDiff_t>(VSIFWriteL(pabyData, 1, cc, m_fpToWrite)) !=
     877             :             cc)
     878             :         {
     879           0 :             ReportError(CE_Failure, CPLE_FileIO,
     880             :                         "Could not write " CPL_FRMT_GUIB " bytes",
     881             :                         static_cast<GUIntBig>(cc));
     882           0 :             return false;
     883             :         }
     884        1407 :         m_nLastWrittenBlockId = strip;
     885        1407 :         return true;
     886             :     }
     887             : 
     888             :     /* -------------------------------------------------------------------- */
     889             :     /*      Should we do compression in a worker thread ?                   */
     890             :     /* -------------------------------------------------------------------- */
     891      148618 :     if (SubmitCompressionJob(strip, pabyData, cc, nStripHeight))
     892        6727 :         return true;
     893             : 
     894      141891 :     return TIFFWriteEncodedStrip(m_hTIFF, strip, pabyData, cc) == cc;
     895             : }
     896             : 
     897             : /************************************************************************/
     898             : /*                       InitCompressionThreads()                       */
     899             : /************************************************************************/
     900             : 
     901       32026 : void GTiffDataset::InitCompressionThreads(bool bUpdateMode,
     902             :                                           CSLConstList papszOptions)
     903             : {
     904             :     // Raster == tile, then no need for threads
     905       32026 :     if (m_nBlockXSize == nRasterXSize && m_nBlockYSize == nRasterYSize)
     906       23406 :         return;
     907             : 
     908        8620 :     const char *pszNumThreads = "";
     909        8620 :     bool bOK = false;
     910        8620 :     const int nThreads = GDALGetNumThreads(
     911             :         papszOptions, "NUM_THREADS", GDAL_DEFAULT_MAX_THREAD_COUNT,
     912             :         /* bDefaultToAllCPUs=*/false, &pszNumThreads, &bOK);
     913        8620 :     if (nThreads > 1)
     914             :     {
     915         106 :         if ((bUpdateMode && m_nCompression != COMPRESSION_NONE) ||
     916          24 :             (nBands >= 1 && IsMultiThreadedReadCompatible()))
     917             :         {
     918          77 :             CPLDebug("GTiff",
     919             :                      "Using up to %d threads for compression/decompression",
     920             :                      nThreads);
     921             : 
     922          77 :             m_poThreadPool = GDALGetGlobalThreadPool(nThreads);
     923          77 :             if (bUpdateMode && m_poThreadPool)
     924          58 :                 m_poCompressQueue = m_poThreadPool->CreateJobQueue();
     925             : 
     926          77 :             if (m_poCompressQueue != nullptr)
     927             :             {
     928             :                 // Add a margin of an extra job w.r.t thread number
     929             :                 // so as to optimize compression time (enables the main
     930             :                 // thread to do boring I/O while all CPUs are working).
     931          58 :                 m_asCompressionJobs.resize(nThreads + 1);
     932          58 :                 memset(&m_asCompressionJobs[0], 0,
     933          58 :                        m_asCompressionJobs.size() *
     934             :                            sizeof(GTiffCompressionJob));
     935          58 :                 for (int i = 0;
     936         280 :                      i < static_cast<int>(m_asCompressionJobs.size()); ++i)
     937             :                 {
     938         444 :                     m_asCompressionJobs[i].pszTmpFilename =
     939         222 :                         CPLStrdup(VSIMemGenerateHiddenFilename(
     940             :                             CPLSPrintf("thread_job_%d.tif", i)));
     941         222 :                     m_asCompressionJobs[i].nStripOrTile = -1;
     942             :                 }
     943             : 
     944             :                 // This is kind of a hack, but basically using
     945             :                 // TIFFWriteRawStrip/Tile and then TIFFReadEncodedStrip/Tile
     946             :                 // does not work on a newly created file, because
     947             :                 // TIFF_MYBUFFER is not set in tif_flags
     948             :                 // (if using TIFFWriteEncodedStrip/Tile first,
     949             :                 // TIFFWriteBufferSetup() is automatically called).
     950             :                 // This should likely rather fixed in libtiff itself.
     951          58 :                 CPL_IGNORE_RET_VAL(TIFFWriteBufferSetup(m_hTIFF, nullptr, -1));
     952             :             }
     953             :         }
     954             :     }
     955        8538 :     else if (!bOK)
     956             :     {
     957           3 :         ReportError(CE_Warning, CPLE_AppDefined,
     958             :                     "Invalid value for NUM_THREADS: %s", pszNumThreads);
     959             :     }
     960             : }
     961             : 
     962             : /************************************************************************/
     963             : /*                       ThreadCompressionFunc()                        */
     964             : /************************************************************************/
     965             : 
     966       26678 : void GTiffDataset::ThreadCompressionFunc(void *pData)
     967             : {
     968       26678 :     GTiffCompressionJob *psJob = static_cast<GTiffCompressionJob *>(pData);
     969       26678 :     GTiffDataset *poDS = psJob->poDS;
     970             : 
     971       26678 :     VSILFILE *fpTmp = VSIFOpenL(psJob->pszTmpFilename, "wb+");
     972       26678 :     TIFF *hTIFFTmp = VSI_TIFFOpen(
     973       53356 :         psJob->pszTmpFilename, psJob->bTIFFIsBigEndian ? "wb+" : "wl+", fpTmp);
     974       26678 :     CPLAssert(hTIFFTmp != nullptr);
     975       26678 :     TIFFSetField(hTIFFTmp, TIFFTAG_IMAGEWIDTH, poDS->m_nBlockXSize);
     976       26678 :     TIFFSetField(hTIFFTmp, TIFFTAG_IMAGELENGTH, psJob->nHeight);
     977       26678 :     TIFFSetField(hTIFFTmp, TIFFTAG_BITSPERSAMPLE, poDS->m_nBitsPerSample);
     978       26678 :     TIFFSetField(hTIFFTmp, TIFFTAG_COMPRESSION, poDS->m_nCompression);
     979       26678 :     TIFFSetField(hTIFFTmp, TIFFTAG_PHOTOMETRIC, poDS->m_nPhotometric);
     980       26678 :     TIFFSetField(hTIFFTmp, TIFFTAG_SAMPLEFORMAT, poDS->m_nSampleFormat);
     981       26678 :     TIFFSetField(hTIFFTmp, TIFFTAG_SAMPLESPERPIXEL, poDS->m_nSamplesPerPixel);
     982       26678 :     TIFFSetField(hTIFFTmp, TIFFTAG_ROWSPERSTRIP, poDS->m_nBlockYSize);
     983       26678 :     TIFFSetField(hTIFFTmp, TIFFTAG_PLANARCONFIG, poDS->m_nPlanarConfig);
     984       26678 :     if (psJob->nPredictor != PREDICTOR_NONE)
     985         263 :         TIFFSetField(hTIFFTmp, TIFFTAG_PREDICTOR, psJob->nPredictor);
     986       26678 :     if (poDS->m_nCompression == COMPRESSION_LERC)
     987             :     {
     988          24 :         TIFFSetField(hTIFFTmp, TIFFTAG_LERC_PARAMETERS, 2,
     989          24 :                      poDS->m_anLercAddCompressionAndVersion);
     990             :     }
     991       26678 :     if (psJob->nExtraSampleCount)
     992             :     {
     993         352 :         TIFFSetField(hTIFFTmp, TIFFTAG_EXTRASAMPLES, psJob->nExtraSampleCount,
     994             :                      psJob->pExtraSamples);
     995             :     }
     996             : 
     997       26678 :     poDS->RestoreVolatileParameters(hTIFFTmp);
     998             : 
     999       53356 :     bool bOK = TIFFWriteEncodedStrip(hTIFFTmp, 0, psJob->pabyBuffer,
    1000       26678 :                                      psJob->nBufferSize) == psJob->nBufferSize;
    1001             : 
    1002       26678 :     toff_t nOffset = 0;
    1003       26678 :     if (bOK)
    1004             :     {
    1005       26678 :         toff_t *panOffsets = nullptr;
    1006       26678 :         toff_t *panByteCounts = nullptr;
    1007       26678 :         TIFFGetField(hTIFFTmp, TIFFTAG_STRIPOFFSETS, &panOffsets);
    1008       26678 :         TIFFGetField(hTIFFTmp, TIFFTAG_STRIPBYTECOUNTS, &panByteCounts);
    1009             : 
    1010       26678 :         nOffset = panOffsets[0];
    1011       26678 :         psJob->nCompressedBufferSize =
    1012       26678 :             static_cast<GPtrDiff_t>(panByteCounts[0]);
    1013             :     }
    1014             :     else
    1015             :     {
    1016           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    1017             :                  "Error when compressing strip/tile %d", psJob->nStripOrTile);
    1018             :     }
    1019             : 
    1020       26678 :     XTIFFClose(hTIFFTmp);
    1021       26678 :     if (VSIFCloseL(fpTmp) != 0)
    1022             :     {
    1023           0 :         if (bOK)
    1024             :         {
    1025           0 :             bOK = false;
    1026           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    1027             :                      "Error when compressing strip/tile %d",
    1028             :                      psJob->nStripOrTile);
    1029             :         }
    1030             :     }
    1031             : 
    1032       26678 :     if (bOK)
    1033             :     {
    1034       26678 :         vsi_l_offset nFileSize = 0;
    1035             :         GByte *pabyCompressedBuffer =
    1036       26678 :             VSIGetMemFileBuffer(psJob->pszTmpFilename, &nFileSize, FALSE);
    1037       26678 :         CPLAssert(static_cast<vsi_l_offset>(
    1038             :                       nOffset + psJob->nCompressedBufferSize) <= nFileSize);
    1039       26678 :         psJob->pabyCompressedBuffer = pabyCompressedBuffer + nOffset;
    1040             :     }
    1041             :     else
    1042             :     {
    1043           0 :         psJob->pabyCompressedBuffer = nullptr;
    1044           0 :         psJob->nCompressedBufferSize = 0;
    1045             :     }
    1046             : 
    1047       26678 :     auto poMainDS = poDS->m_poBaseDS ? poDS->m_poBaseDS : poDS;
    1048       26678 :     if (poMainDS->m_poCompressQueue)
    1049             :     {
    1050        1576 :         std::lock_guard oLock(poMainDS->m_oCompressThreadPoolMutex);
    1051        1576 :         psJob->bReady = true;
    1052             :     }
    1053       26678 : }
    1054             : 
    1055             : /************************************************************************/
    1056             : /*                        WriteRawStripOrTile()                         */
    1057             : /************************************************************************/
    1058             : 
    1059       34061 : void GTiffDataset::WriteRawStripOrTile(int nStripOrTile,
    1060             :                                        GByte *pabyCompressedBuffer,
    1061             :                                        GPtrDiff_t nCompressedBufferSize)
    1062             : {
    1063             : #ifdef DEBUG_VERBOSE
    1064             :     CPLDebug("GTIFF", "Writing raw strip/tile %d, size " CPL_FRMT_GUIB,
    1065             :              nStripOrTile, static_cast<GUIntBig>(nCompressedBufferSize));
    1066             : #endif
    1067       34061 :     toff_t *panOffsets = nullptr;
    1068       34061 :     toff_t *panByteCounts = nullptr;
    1069       34061 :     bool bWriteAtEnd = true;
    1070       34061 :     bool bWriteLeader = m_bLeaderSizeAsUInt4;
    1071       34061 :     bool bWriteTrailer = m_bTrailerRepeatedLast4BytesRepeated;
    1072       34061 :     if (TIFFGetField(m_hTIFF,
    1073       34061 :                      TIFFIsTiled(m_hTIFF) ? TIFFTAG_TILEOFFSETS
    1074             :                                           : TIFFTAG_STRIPOFFSETS,
    1075       34061 :                      &panOffsets) &&
    1076       34061 :         panOffsets != nullptr && panOffsets[nStripOrTile] != 0)
    1077             :     {
    1078             :         // Forces TIFFAppendStrip() to consider if the location of the
    1079             :         // tile/strip can be reused or if the strile should be written at end of
    1080             :         // file.
    1081         360 :         TIFFSetWriteOffset(m_hTIFF, 0);
    1082             : 
    1083         360 :         if (m_bBlockOrderRowMajor)
    1084             :         {
    1085         264 :             if (TIFFGetField(m_hTIFF,
    1086         264 :                              TIFFIsTiled(m_hTIFF) ? TIFFTAG_TILEBYTECOUNTS
    1087             :                                                   : TIFFTAG_STRIPBYTECOUNTS,
    1088         528 :                              &panByteCounts) &&
    1089         264 :                 panByteCounts != nullptr)
    1090             :             {
    1091         264 :                 if (static_cast<GUIntBig>(nCompressedBufferSize) >
    1092         264 :                     panByteCounts[nStripOrTile])
    1093             :                 {
    1094           8 :                     GTiffDataset *poRootDS = m_poBaseDS ? m_poBaseDS : this;
    1095           8 :                     if (!poRootDS->m_bKnownIncompatibleEdition &&
    1096           8 :                         !poRootDS->m_bWriteKnownIncompatibleEdition)
    1097             :                     {
    1098           8 :                         ReportError(
    1099             :                             CE_Warning, CPLE_AppDefined,
    1100             :                             "A strile cannot be rewritten in place, which "
    1101             :                             "invalidates the BLOCK_ORDER optimization.");
    1102           8 :                         poRootDS->m_bKnownIncompatibleEdition = true;
    1103           8 :                         poRootDS->m_bWriteKnownIncompatibleEdition = true;
    1104             :                     }
    1105             :                 }
    1106             :                 // For mask interleaving, if the size is not exactly the same,
    1107             :                 // completely give up (we could potentially move the mask in
    1108             :                 // case the imagery is smaller)
    1109         256 :                 else if (m_poMaskDS && m_bMaskInterleavedWithImagery &&
    1110           0 :                          static_cast<GUIntBig>(nCompressedBufferSize) !=
    1111           0 :                              panByteCounts[nStripOrTile])
    1112             :                 {
    1113           0 :                     GTiffDataset *poRootDS = m_poBaseDS ? m_poBaseDS : this;
    1114           0 :                     if (!poRootDS->m_bKnownIncompatibleEdition &&
    1115           0 :                         !poRootDS->m_bWriteKnownIncompatibleEdition)
    1116             :                     {
    1117           0 :                         ReportError(
    1118             :                             CE_Warning, CPLE_AppDefined,
    1119             :                             "A strile cannot be rewritten in place, which "
    1120             :                             "invalidates the MASK_INTERLEAVED_WITH_IMAGERY "
    1121             :                             "optimization.");
    1122           0 :                         poRootDS->m_bKnownIncompatibleEdition = true;
    1123           0 :                         poRootDS->m_bWriteKnownIncompatibleEdition = true;
    1124             :                     }
    1125           0 :                     bWriteLeader = false;
    1126           0 :                     bWriteTrailer = false;
    1127           0 :                     if (m_bLeaderSizeAsUInt4)
    1128             :                     {
    1129             :                         // If there was a valid leader, invalidat it
    1130           0 :                         VSI_TIFFSeek(m_hTIFF, panOffsets[nStripOrTile] - 4,
    1131             :                                      SEEK_SET);
    1132             :                         uint32_t nOldSize;
    1133           0 :                         VSIFReadL(&nOldSize, 1, 4,
    1134             :                                   VSI_TIFFGetVSILFile(TIFFClientdata(m_hTIFF)));
    1135           0 :                         CPL_LSBPTR32(&nOldSize);
    1136           0 :                         if (nOldSize == panByteCounts[nStripOrTile])
    1137             :                         {
    1138           0 :                             uint32_t nInvalidatedSize = 0;
    1139           0 :                             VSI_TIFFSeek(m_hTIFF, panOffsets[nStripOrTile] - 4,
    1140             :                                          SEEK_SET);
    1141           0 :                             VSI_TIFFWrite(m_hTIFF, &nInvalidatedSize,
    1142             :                                           sizeof(nInvalidatedSize));
    1143             :                         }
    1144             :                     }
    1145             :                 }
    1146             :                 else
    1147             :                 {
    1148         256 :                     bWriteAtEnd = false;
    1149             :                 }
    1150             :             }
    1151             :         }
    1152             :     }
    1153       34061 :     if (bWriteLeader &&
    1154       25107 :         static_cast<GUIntBig>(nCompressedBufferSize) <= 0xFFFFFFFFU)
    1155             :     {
    1156             :         // cppcheck-suppress knownConditionTrueFalse
    1157       25107 :         if (bWriteAtEnd)
    1158             :         {
    1159       24851 :             VSI_TIFFSeek(m_hTIFF, 0, SEEK_END);
    1160             :         }
    1161             :         else
    1162             :         {
    1163             :             // If we rewrite an existing strile in place with an existing
    1164             :             // leader, check that the leader is valid, before rewriting it. And
    1165             :             // if it is not valid, then do not write the trailer, as we could
    1166             :             // corrupt other data.
    1167         256 :             VSI_TIFFSeek(m_hTIFF, panOffsets[nStripOrTile] - 4, SEEK_SET);
    1168             :             uint32_t nOldSize;
    1169         256 :             VSIFReadL(&nOldSize, 1, 4,
    1170             :                       VSI_TIFFGetVSILFile(TIFFClientdata(m_hTIFF)));
    1171         256 :             CPL_LSBPTR32(&nOldSize);
    1172         256 :             bWriteLeader =
    1173         256 :                 panByteCounts && nOldSize == panByteCounts[nStripOrTile];
    1174         256 :             bWriteTrailer = bWriteLeader;
    1175         256 :             VSI_TIFFSeek(m_hTIFF, panOffsets[nStripOrTile] - 4, SEEK_SET);
    1176             :         }
    1177             :         // cppcheck-suppress knownConditionTrueFalse
    1178       25107 :         if (bWriteLeader)
    1179             :         {
    1180       25107 :             uint32_t nSize = static_cast<uint32_t>(nCompressedBufferSize);
    1181       25107 :             CPL_LSBPTR32(&nSize);
    1182       25107 :             if (!VSI_TIFFWrite(m_hTIFF, &nSize, sizeof(nSize)))
    1183           0 :                 m_bWriteError = true;
    1184             :         }
    1185             :     }
    1186             :     tmsize_t written;
    1187       34061 :     if (TIFFIsTiled(m_hTIFF))
    1188       26406 :         written = TIFFWriteRawTile(m_hTIFF, nStripOrTile, pabyCompressedBuffer,
    1189             :                                    nCompressedBufferSize);
    1190             :     else
    1191        7655 :         written = TIFFWriteRawStrip(m_hTIFF, nStripOrTile, pabyCompressedBuffer,
    1192             :                                     nCompressedBufferSize);
    1193       34061 :     if (written != nCompressedBufferSize)
    1194          12 :         m_bWriteError = true;
    1195       34061 :     if (bWriteTrailer &&
    1196       25107 :         static_cast<GUIntBig>(nCompressedBufferSize) <= 0xFFFFFFFFU)
    1197             :     {
    1198       25107 :         GByte abyLastBytes[4] = {};
    1199       25107 :         if (nCompressedBufferSize >= 4)
    1200       25107 :             memcpy(abyLastBytes,
    1201       25107 :                    pabyCompressedBuffer + nCompressedBufferSize - 4, 4);
    1202             :         else
    1203           0 :             memcpy(abyLastBytes, pabyCompressedBuffer, nCompressedBufferSize);
    1204       25107 :         if (!VSI_TIFFWrite(m_hTIFF, abyLastBytes, 4))
    1205           0 :             m_bWriteError = true;
    1206             :     }
    1207       34061 : }
    1208             : 
    1209             : /************************************************************************/
    1210             : /*                      WaitCompletionForJobIdx()                       */
    1211             : /************************************************************************/
    1212             : 
    1213        1576 : void GTiffDataset::WaitCompletionForJobIdx(int i)
    1214             : {
    1215        1576 :     auto poMainDS = m_poBaseDS ? m_poBaseDS : this;
    1216        1576 :     auto poQueue = poMainDS->m_poCompressQueue.get();
    1217        1576 :     auto &oQueue = poMainDS->m_asQueueJobIdx;
    1218        1576 :     auto &asJobs = poMainDS->m_asCompressionJobs;
    1219        1576 :     auto &mutex = poMainDS->m_oCompressThreadPoolMutex;
    1220             : 
    1221        1576 :     CPLAssert(i >= 0 && static_cast<size_t>(i) < asJobs.size());
    1222        1576 :     CPLAssert(asJobs[i].nStripOrTile >= 0);
    1223        1576 :     CPLAssert(!oQueue.empty());
    1224             : 
    1225        1576 :     bool bHasWarned = false;
    1226             :     while (true)
    1227             :     {
    1228             :         bool bReady;
    1229             :         {
    1230        2311 :             std::lock_guard oLock(mutex);
    1231        2311 :             bReady = asJobs[i].bReady;
    1232             :         }
    1233        2311 :         if (!bReady)
    1234             :         {
    1235         735 :             if (!bHasWarned)
    1236             :             {
    1237         500 :                 CPLDebug("GTIFF",
    1238             :                          "Waiting for worker job to finish handling block %d",
    1239         500 :                          asJobs[i].nStripOrTile);
    1240         500 :                 bHasWarned = true;
    1241             :             }
    1242         735 :             poQueue->GetPool()->WaitEvent();
    1243             :         }
    1244             :         else
    1245             :         {
    1246        1576 :             break;
    1247             :         }
    1248         735 :     }
    1249             : 
    1250        1576 :     if (asJobs[i].nCompressedBufferSize)
    1251             :     {
    1252        3152 :         asJobs[i].poDS->WriteRawStripOrTile(asJobs[i].nStripOrTile,
    1253        1576 :                                             asJobs[i].pabyCompressedBuffer,
    1254        1576 :                                             asJobs[i].nCompressedBufferSize);
    1255             :     }
    1256        1576 :     asJobs[i].pabyCompressedBuffer = nullptr;
    1257        1576 :     asJobs[i].nBufferSize = 0;
    1258             :     {
    1259             :         // Likely useless, but makes Coverity happy
    1260        1576 :         std::lock_guard oLock(mutex);
    1261        1576 :         asJobs[i].bReady = false;
    1262             :     }
    1263        1576 :     asJobs[i].nStripOrTile = -1;
    1264        1576 :     oQueue.pop();
    1265        1576 : }
    1266             : 
    1267             : /************************************************************************/
    1268             : /*                       WaitCompletionForBlock()                       */
    1269             : /************************************************************************/
    1270             : 
    1271     2320570 : void GTiffDataset::WaitCompletionForBlock(int nBlockId)
    1272             : {
    1273     2320570 :     auto poQueue = m_poBaseDS ? m_poBaseDS->m_poCompressQueue.get()
    1274     2301320 :                               : m_poCompressQueue.get();
    1275             :     // cppcheck-suppress constVariableReference
    1276     2320570 :     auto &oQueue = m_poBaseDS ? m_poBaseDS->m_asQueueJobIdx : m_asQueueJobIdx;
    1277             :     // cppcheck-suppress constVariableReference
    1278     2301320 :     auto &asJobs =
    1279     2320570 :         m_poBaseDS ? m_poBaseDS->m_asCompressionJobs : m_asCompressionJobs;
    1280             : 
    1281     2320570 :     if (poQueue != nullptr && !oQueue.empty())
    1282             :     {
    1283        1066 :         for (int i = 0; i < static_cast<int>(asJobs.size()); ++i)
    1284             :         {
    1285         888 :             if (asJobs[i].poDS == this && asJobs[i].nStripOrTile == nBlockId)
    1286             :             {
    1287         128 :                 while (!oQueue.empty() &&
    1288          64 :                        !(asJobs[oQueue.front()].poDS == this &&
    1289          64 :                          asJobs[oQueue.front()].nStripOrTile == nBlockId))
    1290             :                 {
    1291           0 :                     WaitCompletionForJobIdx(oQueue.front());
    1292             :                 }
    1293          64 :                 CPLAssert(!oQueue.empty() &&
    1294             :                           asJobs[oQueue.front()].poDS == this &&
    1295             :                           asJobs[oQueue.front()].nStripOrTile == nBlockId);
    1296          64 :                 WaitCompletionForJobIdx(oQueue.front());
    1297             :             }
    1298             :         }
    1299             :     }
    1300     2320570 : }
    1301             : 
    1302             : /************************************************************************/
    1303             : /*                        SubmitCompressionJob()                        */
    1304             : /************************************************************************/
    1305             : 
    1306      197904 : bool GTiffDataset::SubmitCompressionJob(int nStripOrTile, GByte *pabyData,
    1307             :                                         GPtrDiff_t cc, int nHeight)
    1308             : {
    1309             :     /* -------------------------------------------------------------------- */
    1310             :     /*      Should we do compression in a worker thread ?                   */
    1311             :     /* -------------------------------------------------------------------- */
    1312      197904 :     auto poQueue = m_poBaseDS ? m_poBaseDS->m_poCompressQueue.get()
    1313      183849 :                               : m_poCompressQueue.get();
    1314             : 
    1315      197904 :     if (poQueue && m_nCompression == COMPRESSION_NONE)
    1316             :     {
    1317             :         // We don't do multi-threaded compression for uncompressed...
    1318             :         // but we must wait for other related compression tasks (e.g mask)
    1319             :         // to be completed
    1320           0 :         poQueue->WaitCompletion();
    1321             : 
    1322             :         // Flush remaining data
    1323             :         // cppcheck-suppress constVariableReference
    1324           0 :         auto &oQueue =
    1325           0 :             m_poBaseDS ? m_poBaseDS->m_asQueueJobIdx : m_asQueueJobIdx;
    1326           0 :         while (!oQueue.empty())
    1327             :         {
    1328           0 :             WaitCompletionForJobIdx(oQueue.front());
    1329             :         }
    1330             :     }
    1331             : 
    1332             :     const auto SetupJob =
    1333      123543 :         [this, pabyData, cc, nHeight, nStripOrTile](GTiffCompressionJob &sJob)
    1334             :     {
    1335       26678 :         sJob.poDS = this;
    1336       26678 :         sJob.bTIFFIsBigEndian = CPL_TO_BOOL(TIFFIsBigEndian(m_hTIFF));
    1337             :         GByte *pabyBuffer =
    1338       26678 :             static_cast<GByte *>(VSI_REALLOC_VERBOSE(sJob.pabyBuffer, cc));
    1339       26678 :         if (!pabyBuffer)
    1340           0 :             return false;
    1341       26678 :         sJob.pabyBuffer = pabyBuffer;
    1342       26678 :         memcpy(sJob.pabyBuffer, pabyData, cc);
    1343       26678 :         sJob.nBufferSize = cc;
    1344       26678 :         sJob.nHeight = nHeight;
    1345       26678 :         sJob.nStripOrTile = nStripOrTile;
    1346       26678 :         sJob.nPredictor = PREDICTOR_NONE;
    1347       26678 :         if (GTIFFSupportsPredictor(m_nCompression))
    1348             :         {
    1349       16831 :             TIFFGetField(m_hTIFF, TIFFTAG_PREDICTOR, &sJob.nPredictor);
    1350             :         }
    1351             : 
    1352       26678 :         sJob.pExtraSamples = nullptr;
    1353       26678 :         sJob.nExtraSampleCount = 0;
    1354       26678 :         TIFFGetField(m_hTIFF, TIFFTAG_EXTRASAMPLES, &sJob.nExtraSampleCount,
    1355             :                      &sJob.pExtraSamples);
    1356       26678 :         return true;
    1357      197904 :     };
    1358             : 
    1359      197904 :     if (poQueue == nullptr || !(m_nCompression == COMPRESSION_ADOBE_DEFLATE ||
    1360         806 :                                 m_nCompression == COMPRESSION_LZW ||
    1361          78 :                                 m_nCompression == COMPRESSION_PACKBITS ||
    1362          72 :                                 m_nCompression == COMPRESSION_LZMA ||
    1363          62 :                                 m_nCompression == COMPRESSION_ZSTD ||
    1364          52 :                                 m_nCompression == COMPRESSION_LERC ||
    1365          46 :                                 m_nCompression == COMPRESSION_JXL ||
    1366          46 :                                 m_nCompression == COMPRESSION_JXL_DNG_1_7 ||
    1367          28 :                                 m_nCompression == COMPRESSION_WEBP ||
    1368          18 :                                 m_nCompression == COMPRESSION_JPEG))
    1369             :     {
    1370      196328 :         if (m_bBlockOrderRowMajor || m_bLeaderSizeAsUInt4 ||
    1371      171226 :             m_bTrailerRepeatedLast4BytesRepeated)
    1372             :         {
    1373             :             GTiffCompressionJob sJob;
    1374       25102 :             memset(&sJob, 0, sizeof(sJob));
    1375       25102 :             if (SetupJob(sJob))
    1376             :             {
    1377       25102 :                 sJob.pszTmpFilename =
    1378       25102 :                     CPLStrdup(VSIMemGenerateHiddenFilename("temp.tif"));
    1379             : 
    1380       25102 :                 ThreadCompressionFunc(&sJob);
    1381             : 
    1382       25102 :                 if (sJob.nCompressedBufferSize)
    1383             :                 {
    1384       25102 :                     sJob.poDS->WriteRawStripOrTile(sJob.nStripOrTile,
    1385             :                                                    sJob.pabyCompressedBuffer,
    1386             :                                                    sJob.nCompressedBufferSize);
    1387             :                 }
    1388             : 
    1389       25102 :                 CPLFree(sJob.pabyBuffer);
    1390       25102 :                 VSIUnlink(sJob.pszTmpFilename);
    1391       25102 :                 CPLFree(sJob.pszTmpFilename);
    1392       25102 :                 return sJob.nCompressedBufferSize > 0 && !m_bWriteError;
    1393             :             }
    1394             :         }
    1395             : 
    1396      171226 :         return false;
    1397             :     }
    1398             : 
    1399        1576 :     auto poMainDS = m_poBaseDS ? m_poBaseDS : this;
    1400        1576 :     auto &oQueue = poMainDS->m_asQueueJobIdx;
    1401        1576 :     auto &asJobs = poMainDS->m_asCompressionJobs;
    1402             : 
    1403        1576 :     int nNextCompressionJobAvail = -1;
    1404             : 
    1405        1576 :     if (oQueue.size() == asJobs.size())
    1406             :     {
    1407        1443 :         CPLAssert(!oQueue.empty());
    1408        1443 :         nNextCompressionJobAvail = oQueue.front();
    1409        1443 :         WaitCompletionForJobIdx(nNextCompressionJobAvail);
    1410             :     }
    1411             :     else
    1412             :     {
    1413         133 :         const int nJobs = static_cast<int>(asJobs.size());
    1414         324 :         for (int i = 0; i < nJobs; ++i)
    1415             :         {
    1416         324 :             if (asJobs[i].nBufferSize == 0)
    1417             :             {
    1418         133 :                 nNextCompressionJobAvail = i;
    1419         133 :                 break;
    1420             :             }
    1421             :         }
    1422             :     }
    1423        1576 :     CPLAssert(nNextCompressionJobAvail >= 0);
    1424             : 
    1425        1576 :     GTiffCompressionJob *psJob = &asJobs[nNextCompressionJobAvail];
    1426        1576 :     bool bOK = SetupJob(*psJob);
    1427        1576 :     if (bOK)
    1428             :     {
    1429        1576 :         poQueue->SubmitJob(ThreadCompressionFunc, psJob);
    1430        1576 :         oQueue.push(nNextCompressionJobAvail);
    1431             :     }
    1432             : 
    1433        1576 :     return bOK;
    1434             : }
    1435             : 
    1436             : /************************************************************************/
    1437             : /*                             DiscardLsb()                             */
    1438             : /************************************************************************/
    1439             : 
    1440         272 : template <class T> bool MustNotDiscardLsb(T value, bool bHasNoData, T nodata)
    1441             : {
    1442         272 :     return bHasNoData && value == nodata;
    1443             : }
    1444             : 
    1445             : template <>
    1446          44 : bool MustNotDiscardLsb<float>(float value, bool bHasNoData, float nodata)
    1447             : {
    1448          44 :     return (bHasNoData && value == nodata) || !std::isfinite(value);
    1449             : }
    1450             : 
    1451             : template <>
    1452          44 : bool MustNotDiscardLsb<double>(double value, bool bHasNoData, double nodata)
    1453             : {
    1454          44 :     return (bHasNoData && value == nodata) || !std::isfinite(value);
    1455             : }
    1456             : 
    1457             : template <class T> T AdjustValue(T value, uint64_t nRoundUpBitTest);
    1458             : 
    1459          10 : template <class T> T AdjustValueInt(T value, uint64_t nRoundUpBitTest)
    1460             : {
    1461          10 :     if (value >=
    1462          10 :         static_cast<T>(std::numeric_limits<T>::max() - (nRoundUpBitTest << 1)))
    1463           0 :         return static_cast<T>(value - (nRoundUpBitTest << 1));
    1464          10 :     return static_cast<T>(value + (nRoundUpBitTest << 1));
    1465             : }
    1466             : 
    1467           0 : template <> int8_t AdjustValue<int8_t>(int8_t value, uint64_t nRoundUpBitTest)
    1468             : {
    1469           0 :     return AdjustValueInt(value, nRoundUpBitTest);
    1470             : }
    1471             : 
    1472             : template <>
    1473           2 : uint8_t AdjustValue<uint8_t>(uint8_t value, uint64_t nRoundUpBitTest)
    1474             : {
    1475           2 :     return AdjustValueInt(value, nRoundUpBitTest);
    1476             : }
    1477             : 
    1478             : template <>
    1479           2 : int16_t AdjustValue<int16_t>(int16_t value, uint64_t nRoundUpBitTest)
    1480             : {
    1481           2 :     return AdjustValueInt(value, nRoundUpBitTest);
    1482             : }
    1483             : 
    1484             : template <>
    1485           2 : uint16_t AdjustValue<uint16_t>(uint16_t value, uint64_t nRoundUpBitTest)
    1486             : {
    1487           2 :     return AdjustValueInt(value, nRoundUpBitTest);
    1488             : }
    1489             : 
    1490             : template <>
    1491           2 : int32_t AdjustValue<int32_t>(int32_t value, uint64_t nRoundUpBitTest)
    1492             : {
    1493           2 :     return AdjustValueInt(value, nRoundUpBitTest);
    1494             : }
    1495             : 
    1496             : template <>
    1497           2 : uint32_t AdjustValue<uint32_t>(uint32_t value, uint64_t nRoundUpBitTest)
    1498             : {
    1499           2 :     return AdjustValueInt(value, nRoundUpBitTest);
    1500             : }
    1501             : 
    1502             : template <>
    1503           0 : int64_t AdjustValue<int64_t>(int64_t value, uint64_t nRoundUpBitTest)
    1504             : {
    1505           0 :     return AdjustValueInt(value, nRoundUpBitTest);
    1506             : }
    1507             : 
    1508             : template <>
    1509           0 : uint64_t AdjustValue<uint64_t>(uint64_t value, uint64_t nRoundUpBitTest)
    1510             : {
    1511           0 :     return AdjustValueInt(value, nRoundUpBitTest);
    1512             : }
    1513             : 
    1514           0 : template <> GFloat16 AdjustValue<GFloat16>(GFloat16 value, uint64_t)
    1515             : {
    1516             :     using std::nextafter;
    1517           0 :     return nextafter(value, cpl::NumericLimits<GFloat16>::max());
    1518             : }
    1519             : 
    1520           0 : template <> float AdjustValue<float>(float value, uint64_t)
    1521             : {
    1522           0 :     return std::nextafter(value, std::numeric_limits<float>::max());
    1523             : }
    1524             : 
    1525           0 : template <> double AdjustValue<double>(double value, uint64_t)
    1526             : {
    1527           0 :     return std::nextafter(value, std::numeric_limits<double>::max());
    1528             : }
    1529             : 
    1530             : template <class Teffective, class T>
    1531             : T RoundValueDiscardLsb(const void *ptr, uint64_t nMask,
    1532             :                        uint64_t nRoundUpBitTest);
    1533             : 
    1534             : template <class T>
    1535          16 : T RoundValueDiscardLsbUnsigned(const void *ptr, uint64_t nMask,
    1536             :                                uint64_t nRoundUpBitTest)
    1537             : {
    1538          32 :     if ((*reinterpret_cast<const T *>(ptr) & nMask) >
    1539          16 :         static_cast<uint64_t>(std::numeric_limits<T>::max()) -
    1540          16 :             (nRoundUpBitTest << 1U))
    1541             :     {
    1542           4 :         return static_cast<T>(std::numeric_limits<T>::max() & nMask);
    1543             :     }
    1544          12 :     const uint64_t newval =
    1545          12 :         (*reinterpret_cast<const T *>(ptr) & nMask) + (nRoundUpBitTest << 1U);
    1546          12 :     return static_cast<T>(newval);
    1547             : }
    1548             : 
    1549             : template <class T>
    1550          18 : T RoundValueDiscardLsbSigned(const void *ptr, uint64_t nMask,
    1551             :                              uint64_t nRoundUpBitTest)
    1552             : {
    1553          18 :     T oldval = *reinterpret_cast<const T *>(ptr);
    1554          18 :     if (oldval < 0)
    1555             :     {
    1556           4 :         return static_cast<T>(oldval & nMask);
    1557             :     }
    1558          14 :     const uint64_t newval =
    1559          14 :         (*reinterpret_cast<const T *>(ptr) & nMask) + (nRoundUpBitTest << 1U);
    1560          14 :     if (newval > static_cast<uint64_t>(std::numeric_limits<T>::max()))
    1561           4 :         return static_cast<T>(std::numeric_limits<T>::max() & nMask);
    1562          10 :     return static_cast<T>(newval);
    1563             : }
    1564             : 
    1565             : template <>
    1566          11 : uint16_t RoundValueDiscardLsb<uint16_t, uint16_t>(const void *ptr,
    1567             :                                                   uint64_t nMask,
    1568             :                                                   uint64_t nRoundUpBitTest)
    1569             : {
    1570          11 :     return RoundValueDiscardLsbUnsigned<uint16_t>(ptr, nMask, nRoundUpBitTest);
    1571             : }
    1572             : 
    1573             : template <>
    1574           5 : uint32_t RoundValueDiscardLsb<uint32_t, uint32_t>(const void *ptr,
    1575             :                                                   uint64_t nMask,
    1576             :                                                   uint64_t nRoundUpBitTest)
    1577             : {
    1578           5 :     return RoundValueDiscardLsbUnsigned<uint32_t>(ptr, nMask, nRoundUpBitTest);
    1579             : }
    1580             : 
    1581             : template <>
    1582           0 : uint64_t RoundValueDiscardLsb<uint64_t, uint64_t>(const void *ptr,
    1583             :                                                   uint64_t nMask,
    1584             :                                                   uint64_t nRoundUpBitTest)
    1585             : {
    1586           0 :     return RoundValueDiscardLsbUnsigned<uint64_t>(ptr, nMask, nRoundUpBitTest);
    1587             : }
    1588             : 
    1589             : template <>
    1590           0 : int8_t RoundValueDiscardLsb<int8_t, int8_t>(const void *ptr, uint64_t nMask,
    1591             :                                             uint64_t nRoundUpBitTest)
    1592             : {
    1593           0 :     return RoundValueDiscardLsbSigned<int8_t>(ptr, nMask, nRoundUpBitTest);
    1594             : }
    1595             : 
    1596             : template <>
    1597          13 : int16_t RoundValueDiscardLsb<int16_t, int16_t>(const void *ptr, uint64_t nMask,
    1598             :                                                uint64_t nRoundUpBitTest)
    1599             : {
    1600          13 :     return RoundValueDiscardLsbSigned<int16_t>(ptr, nMask, nRoundUpBitTest);
    1601             : }
    1602             : 
    1603             : template <>
    1604           5 : int32_t RoundValueDiscardLsb<int32_t, int32_t>(const void *ptr, uint64_t nMask,
    1605             :                                                uint64_t nRoundUpBitTest)
    1606             : {
    1607           5 :     return RoundValueDiscardLsbSigned<int32_t>(ptr, nMask, nRoundUpBitTest);
    1608             : }
    1609             : 
    1610             : template <>
    1611           0 : int64_t RoundValueDiscardLsb<int64_t, int64_t>(const void *ptr, uint64_t nMask,
    1612             :                                                uint64_t nRoundUpBitTest)
    1613             : {
    1614           0 :     return RoundValueDiscardLsbSigned<int64_t>(ptr, nMask, nRoundUpBitTest);
    1615             : }
    1616             : 
    1617             : template <>
    1618           0 : uint16_t RoundValueDiscardLsb<GFloat16, uint16_t>(const void *ptr,
    1619             :                                                   uint64_t nMask,
    1620             :                                                   uint64_t nRoundUpBitTest)
    1621             : {
    1622           0 :     return RoundValueDiscardLsbUnsigned<uint16_t>(ptr, nMask, nRoundUpBitTest);
    1623             : }
    1624             : 
    1625             : template <>
    1626           0 : uint32_t RoundValueDiscardLsb<float, uint32_t>(const void *ptr, uint64_t nMask,
    1627             :                                                uint64_t nRoundUpBitTest)
    1628             : {
    1629           0 :     return RoundValueDiscardLsbUnsigned<uint32_t>(ptr, nMask, nRoundUpBitTest);
    1630             : }
    1631             : 
    1632             : template <>
    1633           0 : uint64_t RoundValueDiscardLsb<double, uint64_t>(const void *ptr, uint64_t nMask,
    1634             :                                                 uint64_t nRoundUpBitTest)
    1635             : {
    1636           0 :     return RoundValueDiscardLsbUnsigned<uint64_t>(ptr, nMask, nRoundUpBitTest);
    1637             : }
    1638             : 
    1639             : template <class Teffective, class T>
    1640         145 : static void DiscardLsbT(GByte *pabyBuffer, size_t nBytes, int iBand, int nBands,
    1641             :                         uint16_t nPlanarConfig,
    1642             :                         const GTiffDataset::MaskOffset *panMaskOffsetLsb,
    1643             :                         bool bHasNoData, Teffective nNoDataValue)
    1644             : {
    1645             :     static_assert(sizeof(Teffective) == sizeof(T),
    1646             :                   "sizeof(Teffective) == sizeof(T)");
    1647         145 :     if (nPlanarConfig == PLANARCONFIG_SEPARATE)
    1648             :     {
    1649          98 :         const auto nMask = panMaskOffsetLsb[iBand].nMask;
    1650          98 :         const auto nRoundUpBitTest = panMaskOffsetLsb[iBand].nRoundUpBitTest;
    1651         196 :         for (size_t i = 0; i < nBytes / sizeof(T); ++i)
    1652             :         {
    1653          98 :             if (MustNotDiscardLsb(reinterpret_cast<Teffective *>(pabyBuffer)[i],
    1654             :                                   bHasNoData, nNoDataValue))
    1655             :             {
    1656          22 :                 continue;
    1657             :             }
    1658             : 
    1659          76 :             if (reinterpret_cast<T *>(pabyBuffer)[i] & nRoundUpBitTest)
    1660             :             {
    1661          30 :                 reinterpret_cast<T *>(pabyBuffer)[i] =
    1662          15 :                     RoundValueDiscardLsb<Teffective, T>(
    1663          15 :                         &(reinterpret_cast<T *>(pabyBuffer)[i]), nMask,
    1664             :                         nRoundUpBitTest);
    1665             :             }
    1666             :             else
    1667             :             {
    1668          61 :                 reinterpret_cast<T *>(pabyBuffer)[i] = static_cast<T>(
    1669          61 :                     reinterpret_cast<T *>(pabyBuffer)[i] & nMask);
    1670             :             }
    1671             : 
    1672             :             // Make sure that by discarding LSB we don't end up to a value
    1673             :             // that is no the nodata value
    1674          76 :             if (MustNotDiscardLsb(reinterpret_cast<Teffective *>(pabyBuffer)[i],
    1675             :                                   bHasNoData, nNoDataValue))
    1676             :             {
    1677           8 :                 reinterpret_cast<Teffective *>(pabyBuffer)[i] =
    1678           4 :                     AdjustValue(nNoDataValue, nRoundUpBitTest);
    1679             :             }
    1680             :         }
    1681             :     }
    1682             :     else
    1683             :     {
    1684          94 :         for (size_t i = 0; i < nBytes / sizeof(T); i += nBands)
    1685             :         {
    1686         147 :             for (int j = 0; j < nBands; ++j)
    1687             :             {
    1688         100 :                 if (MustNotDiscardLsb(
    1689         100 :                         reinterpret_cast<Teffective *>(pabyBuffer)[i + j],
    1690             :                         bHasNoData, nNoDataValue))
    1691             :                 {
    1692          14 :                     continue;
    1693             :                 }
    1694             : 
    1695          86 :                 if (reinterpret_cast<T *>(pabyBuffer)[i + j] &
    1696          86 :                     panMaskOffsetLsb[j].nRoundUpBitTest)
    1697             :                 {
    1698          38 :                     reinterpret_cast<T *>(pabyBuffer)[i + j] =
    1699          19 :                         RoundValueDiscardLsb<Teffective, T>(
    1700          19 :                             &(reinterpret_cast<T *>(pabyBuffer)[i + j]),
    1701          19 :                             panMaskOffsetLsb[j].nMask,
    1702          19 :                             panMaskOffsetLsb[j].nRoundUpBitTest);
    1703             :                 }
    1704             :                 else
    1705             :                 {
    1706          67 :                     reinterpret_cast<T *>(pabyBuffer)[i + j] = static_cast<T>(
    1707          67 :                         (reinterpret_cast<T *>(pabyBuffer)[i + j] &
    1708          67 :                          panMaskOffsetLsb[j].nMask));
    1709             :                 }
    1710             : 
    1711             :                 // Make sure that by discarding LSB we don't end up to a value
    1712             :                 // that is no the nodata value
    1713          86 :                 if (MustNotDiscardLsb(
    1714          86 :                         reinterpret_cast<Teffective *>(pabyBuffer)[i + j],
    1715             :                         bHasNoData, nNoDataValue))
    1716             :                 {
    1717           8 :                     reinterpret_cast<Teffective *>(pabyBuffer)[i + j] =
    1718           4 :                         AdjustValue(nNoDataValue,
    1719           4 :                                     panMaskOffsetLsb[j].nRoundUpBitTest);
    1720             :                 }
    1721             :             }
    1722             :         }
    1723             :     }
    1724         145 : }
    1725             : 
    1726         183 : static void DiscardLsb(GByte *pabyBuffer, GPtrDiff_t nBytes, int iBand,
    1727             :                        int nBands, uint16_t nSampleFormat,
    1728             :                        uint16_t nBitsPerSample, uint16_t nPlanarConfig,
    1729             :                        const GTiffDataset::MaskOffset *panMaskOffsetLsb,
    1730             :                        bool bHasNoData, double dfNoDataValue)
    1731             : {
    1732         183 :     if (nBitsPerSample == 8 && nSampleFormat == SAMPLEFORMAT_UINT)
    1733             :     {
    1734          38 :         uint8_t nNoDataValue = 0;
    1735          38 :         if (bHasNoData && GDALIsValueExactAs<uint8_t>(dfNoDataValue))
    1736             :         {
    1737           6 :             nNoDataValue = static_cast<uint8_t>(dfNoDataValue);
    1738             :         }
    1739             :         else
    1740             :         {
    1741          32 :             bHasNoData = false;
    1742             :         }
    1743          38 :         if (nPlanarConfig == PLANARCONFIG_SEPARATE)
    1744             :         {
    1745          25 :             const auto nMask =
    1746          25 :                 static_cast<unsigned>(panMaskOffsetLsb[iBand].nMask);
    1747          25 :             const auto nRoundUpBitTest =
    1748          25 :                 static_cast<unsigned>(panMaskOffsetLsb[iBand].nRoundUpBitTest);
    1749          50 :             for (decltype(nBytes) i = 0; i < nBytes; ++i)
    1750             :             {
    1751          25 :                 if (bHasNoData && pabyBuffer[i] == nNoDataValue)
    1752           3 :                     continue;
    1753             : 
    1754             :                 // Keep 255 in case it is alpha.
    1755          22 :                 if (pabyBuffer[i] != 255)
    1756             :                 {
    1757          21 :                     if (pabyBuffer[i] & nRoundUpBitTest)
    1758           5 :                         pabyBuffer[i] = static_cast<GByte>(
    1759           5 :                             std::min(255U, (pabyBuffer[i] & nMask) +
    1760           5 :                                                (nRoundUpBitTest << 1U)));
    1761             :                     else
    1762          16 :                         pabyBuffer[i] =
    1763          16 :                             static_cast<GByte>(pabyBuffer[i] & nMask);
    1764             : 
    1765             :                     // Make sure that by discarding LSB we don't end up to a
    1766             :                     // value that is no the nodata value
    1767          21 :                     if (bHasNoData && pabyBuffer[i] == nNoDataValue)
    1768           2 :                         pabyBuffer[i] =
    1769           1 :                             AdjustValue(nNoDataValue, nRoundUpBitTest);
    1770             :                 }
    1771             :             }
    1772             :         }
    1773             :         else
    1774             :         {
    1775          26 :             for (decltype(nBytes) i = 0; i < nBytes; i += nBands)
    1776             :             {
    1777          42 :                 for (int j = 0; j < nBands; ++j)
    1778             :                 {
    1779          29 :                     if (bHasNoData && pabyBuffer[i + j] == nNoDataValue)
    1780           2 :                         continue;
    1781             : 
    1782             :                     // Keep 255 in case it is alpha.
    1783          27 :                     if (pabyBuffer[i + j] != 255)
    1784             :                     {
    1785          25 :                         if (pabyBuffer[i + j] &
    1786          25 :                             panMaskOffsetLsb[j].nRoundUpBitTest)
    1787             :                         {
    1788           6 :                             pabyBuffer[i + j] = static_cast<GByte>(std::min(
    1789          12 :                                 255U,
    1790           6 :                                 (pabyBuffer[i + j] &
    1791             :                                  static_cast<unsigned>(
    1792           6 :                                      panMaskOffsetLsb[j].nMask)) +
    1793             :                                     (static_cast<unsigned>(
    1794           6 :                                          panMaskOffsetLsb[j].nRoundUpBitTest)
    1795           6 :                                      << 1U)));
    1796             :                         }
    1797             :                         else
    1798             :                         {
    1799          19 :                             pabyBuffer[i + j] = static_cast<GByte>(
    1800          19 :                                 pabyBuffer[i + j] & panMaskOffsetLsb[j].nMask);
    1801             :                         }
    1802             : 
    1803             :                         // Make sure that by discarding LSB we don't end up to a
    1804             :                         // value that is no the nodata value
    1805          25 :                         if (bHasNoData && pabyBuffer[i + j] == nNoDataValue)
    1806           1 :                             pabyBuffer[i + j] = AdjustValue(
    1807             :                                 nNoDataValue,
    1808           1 :                                 panMaskOffsetLsb[j].nRoundUpBitTest);
    1809             :                     }
    1810             :                 }
    1811             :             }
    1812          38 :         }
    1813             :     }
    1814         145 :     else if (nBitsPerSample == 8 && nSampleFormat == SAMPLEFORMAT_INT)
    1815             :     {
    1816           0 :         int8_t nNoDataValue = 0;
    1817           0 :         if (bHasNoData && GDALIsValueExactAs<int8_t>(dfNoDataValue))
    1818             :         {
    1819           0 :             nNoDataValue = static_cast<int8_t>(dfNoDataValue);
    1820             :         }
    1821             :         else
    1822             :         {
    1823           0 :             bHasNoData = false;
    1824             :         }
    1825           0 :         DiscardLsbT<int8_t, int8_t>(pabyBuffer, nBytes, iBand, nBands,
    1826             :                                     nPlanarConfig, panMaskOffsetLsb, bHasNoData,
    1827           0 :                                     nNoDataValue);
    1828             :     }
    1829         145 :     else if (nBitsPerSample == 16 && nSampleFormat == SAMPLEFORMAT_INT)
    1830             :     {
    1831          48 :         int16_t nNoDataValue = 0;
    1832          48 :         if (bHasNoData && GDALIsValueExactAs<int16_t>(dfNoDataValue))
    1833             :         {
    1834           6 :             nNoDataValue = static_cast<int16_t>(dfNoDataValue);
    1835             :         }
    1836             :         else
    1837             :         {
    1838          42 :             bHasNoData = false;
    1839             :         }
    1840          48 :         DiscardLsbT<int16_t, int16_t>(pabyBuffer, nBytes, iBand, nBands,
    1841             :                                       nPlanarConfig, panMaskOffsetLsb,
    1842          48 :                                       bHasNoData, nNoDataValue);
    1843             :     }
    1844          97 :     else if (nBitsPerSample == 16 && nSampleFormat == SAMPLEFORMAT_UINT)
    1845             :     {
    1846          33 :         uint16_t nNoDataValue = 0;
    1847          33 :         if (bHasNoData && GDALIsValueExactAs<uint16_t>(dfNoDataValue))
    1848             :         {
    1849           6 :             nNoDataValue = static_cast<uint16_t>(dfNoDataValue);
    1850             :         }
    1851             :         else
    1852             :         {
    1853          27 :             bHasNoData = false;
    1854             :         }
    1855          33 :         DiscardLsbT<uint16_t, uint16_t>(pabyBuffer, nBytes, iBand, nBands,
    1856             :                                         nPlanarConfig, panMaskOffsetLsb,
    1857          33 :                                         bHasNoData, nNoDataValue);
    1858             :     }
    1859          64 :     else if (nBitsPerSample == 32 && nSampleFormat == SAMPLEFORMAT_INT)
    1860             :     {
    1861          13 :         int32_t nNoDataValue = 0;
    1862          13 :         if (bHasNoData && GDALIsValueExactAs<int32_t>(dfNoDataValue))
    1863             :         {
    1864           6 :             nNoDataValue = static_cast<int32_t>(dfNoDataValue);
    1865             :         }
    1866             :         else
    1867             :         {
    1868           7 :             bHasNoData = false;
    1869             :         }
    1870          13 :         DiscardLsbT<int32_t, int32_t>(pabyBuffer, nBytes, iBand, nBands,
    1871             :                                       nPlanarConfig, panMaskOffsetLsb,
    1872          13 :                                       bHasNoData, nNoDataValue);
    1873             :     }
    1874          51 :     else if (nBitsPerSample == 32 && nSampleFormat == SAMPLEFORMAT_UINT)
    1875             :     {
    1876          13 :         uint32_t nNoDataValue = 0;
    1877          13 :         if (bHasNoData && GDALIsValueExactAs<uint32_t>(dfNoDataValue))
    1878             :         {
    1879           6 :             nNoDataValue = static_cast<uint32_t>(dfNoDataValue);
    1880             :         }
    1881             :         else
    1882             :         {
    1883           7 :             bHasNoData = false;
    1884             :         }
    1885          13 :         DiscardLsbT<uint32_t, uint32_t>(pabyBuffer, nBytes, iBand, nBands,
    1886             :                                         nPlanarConfig, panMaskOffsetLsb,
    1887          13 :                                         bHasNoData, nNoDataValue);
    1888             :     }
    1889          38 :     else if (nBitsPerSample == 64 && nSampleFormat == SAMPLEFORMAT_INT)
    1890             :     {
    1891             :         // FIXME: we should not rely on dfNoDataValue when we support native
    1892             :         // data type for nodata
    1893           0 :         int64_t nNoDataValue = 0;
    1894           0 :         if (bHasNoData && GDALIsValueExactAs<int64_t>(dfNoDataValue))
    1895             :         {
    1896           0 :             nNoDataValue = static_cast<int64_t>(dfNoDataValue);
    1897             :         }
    1898             :         else
    1899             :         {
    1900           0 :             bHasNoData = false;
    1901             :         }
    1902           0 :         DiscardLsbT<int64_t, int64_t>(pabyBuffer, nBytes, iBand, nBands,
    1903             :                                       nPlanarConfig, panMaskOffsetLsb,
    1904           0 :                                       bHasNoData, nNoDataValue);
    1905             :     }
    1906          38 :     else if (nBitsPerSample == 64 && nSampleFormat == SAMPLEFORMAT_UINT)
    1907             :     {
    1908             :         // FIXME: we should not rely on dfNoDataValue when we support native
    1909             :         // data type for nodata
    1910           0 :         uint64_t nNoDataValue = 0;
    1911           0 :         if (bHasNoData && GDALIsValueExactAs<uint64_t>(dfNoDataValue))
    1912             :         {
    1913           0 :             nNoDataValue = static_cast<uint64_t>(dfNoDataValue);
    1914             :         }
    1915             :         else
    1916             :         {
    1917           0 :             bHasNoData = false;
    1918             :         }
    1919           0 :         DiscardLsbT<uint64_t, uint64_t>(pabyBuffer, nBytes, iBand, nBands,
    1920             :                                         nPlanarConfig, panMaskOffsetLsb,
    1921           0 :                                         bHasNoData, nNoDataValue);
    1922             :     }
    1923          38 :     else if (nBitsPerSample == 16 && nSampleFormat == SAMPLEFORMAT_IEEEFP)
    1924             :     {
    1925           0 :         const GFloat16 fNoDataValue = static_cast<GFloat16>(dfNoDataValue);
    1926           0 :         DiscardLsbT<GFloat16, uint16_t>(pabyBuffer, nBytes, iBand, nBands,
    1927             :                                         nPlanarConfig, panMaskOffsetLsb,
    1928           0 :                                         bHasNoData, fNoDataValue);
    1929             :     }
    1930          38 :     else if (nBitsPerSample == 32 && nSampleFormat == SAMPLEFORMAT_IEEEFP)
    1931             :     {
    1932          19 :         const float fNoDataValue = static_cast<float>(dfNoDataValue);
    1933          19 :         DiscardLsbT<float, uint32_t>(pabyBuffer, nBytes, iBand, nBands,
    1934             :                                      nPlanarConfig, panMaskOffsetLsb,
    1935          19 :                                      bHasNoData, fNoDataValue);
    1936             :     }
    1937          19 :     else if (nBitsPerSample == 64 && nSampleFormat == SAMPLEFORMAT_IEEEFP)
    1938             :     {
    1939          19 :         DiscardLsbT<double, uint64_t>(pabyBuffer, nBytes, iBand, nBands,
    1940             :                                       nPlanarConfig, panMaskOffsetLsb,
    1941             :                                       bHasNoData, dfNoDataValue);
    1942             :     }
    1943         183 : }
    1944             : 
    1945         183 : void GTiffDataset::DiscardLsb(GByte *pabyBuffer, GPtrDiff_t nBytes,
    1946             :                               int iBand) const
    1947             : {
    1948         183 :     ::DiscardLsb(pabyBuffer, nBytes, iBand, nBands, m_nSampleFormat,
    1949         183 :                  m_nBitsPerSample, m_nPlanarConfig, m_panMaskOffsetLsb,
    1950         183 :                  m_bNoDataSet, m_dfNoDataValue);
    1951         183 : }
    1952             : 
    1953             : /************************************************************************/
    1954             : /*                      WriteEncodedTileOrStrip()                       */
    1955             : /************************************************************************/
    1956             : 
    1957      228837 : CPLErr GTiffDataset::WriteEncodedTileOrStrip(uint32_t tile_or_strip, void *data,
    1958             :                                              int bPreserveDataBuffer)
    1959             : {
    1960      228837 :     CPLErr eErr = CE_None;
    1961             : 
    1962      228837 :     if (TIFFIsTiled(m_hTIFF))
    1963             :     {
    1964       50503 :         if (!(WriteEncodedTile(tile_or_strip, static_cast<GByte *>(data),
    1965             :                                bPreserveDataBuffer)))
    1966             :         {
    1967          14 :             eErr = CE_Failure;
    1968             :         }
    1969             :     }
    1970             :     else
    1971             :     {
    1972      178334 :         if (!(WriteEncodedStrip(tile_or_strip, static_cast<GByte *>(data),
    1973             :                                 bPreserveDataBuffer)))
    1974             :         {
    1975           8 :             eErr = CE_Failure;
    1976             :         }
    1977             :     }
    1978             : 
    1979      228837 :     return eErr;
    1980             : }
    1981             : 
    1982             : /************************************************************************/
    1983             : /*                           FlushBlockBuf()                            */
    1984             : /************************************************************************/
    1985             : 
    1986        9605 : CPLErr GTiffDataset::FlushBlockBuf()
    1987             : 
    1988             : {
    1989        9605 :     if (m_nLoadedBlock < 0 || !m_bLoadedBlockDirty)
    1990           0 :         return CE_None;
    1991             : 
    1992        9605 :     m_bLoadedBlockDirty = false;
    1993             : 
    1994             :     const CPLErr eErr =
    1995        9605 :         WriteEncodedTileOrStrip(m_nLoadedBlock, m_pabyBlockBuf, true);
    1996        9605 :     if (eErr != CE_None)
    1997             :     {
    1998           0 :         ReportError(CE_Failure, CPLE_AppDefined,
    1999             :                     "WriteEncodedTile/Strip() failed.");
    2000           0 :         m_bWriteError = true;
    2001             :     }
    2002             : 
    2003        9605 :     return eErr;
    2004             : }
    2005             : 
    2006             : /************************************************************************/
    2007             : /*                 GTiffFillStreamableOffsetAndCount()                  */
    2008             : /************************************************************************/
    2009             : 
    2010           8 : static void GTiffFillStreamableOffsetAndCount(TIFF *hTIFF, int nSize)
    2011             : {
    2012           8 :     uint32_t nXSize = 0;
    2013           8 :     uint32_t nYSize = 0;
    2014           8 :     TIFFGetField(hTIFF, TIFFTAG_IMAGEWIDTH, &nXSize);
    2015           8 :     TIFFGetField(hTIFF, TIFFTAG_IMAGELENGTH, &nYSize);
    2016           8 :     const bool bIsTiled = CPL_TO_BOOL(TIFFIsTiled(hTIFF));
    2017             :     const int nBlockCount =
    2018           8 :         bIsTiled ? TIFFNumberOfTiles(hTIFF) : TIFFNumberOfStrips(hTIFF);
    2019             : 
    2020           8 :     toff_t *panOffset = nullptr;
    2021           8 :     TIFFGetField(hTIFF, bIsTiled ? TIFFTAG_TILEOFFSETS : TIFFTAG_STRIPOFFSETS,
    2022             :                  &panOffset);
    2023           8 :     toff_t *panSize = nullptr;
    2024           8 :     TIFFGetField(hTIFF,
    2025             :                  bIsTiled ? TIFFTAG_TILEBYTECOUNTS : TIFFTAG_STRIPBYTECOUNTS,
    2026             :                  &panSize);
    2027           8 :     toff_t nOffset = nSize;
    2028             :     // Trick to avoid clang static analyzer raising false positive about
    2029             :     // divide by zero later.
    2030           8 :     int nBlocksPerBand = 1;
    2031           8 :     uint32_t nRowsPerStrip = 0;
    2032           8 :     if (!bIsTiled)
    2033             :     {
    2034           6 :         TIFFGetField(hTIFF, TIFFTAG_ROWSPERSTRIP, &nRowsPerStrip);
    2035           6 :         if (nRowsPerStrip > static_cast<uint32_t>(nYSize))
    2036           0 :             nRowsPerStrip = nYSize;
    2037           6 :         nBlocksPerBand = DIV_ROUND_UP(nYSize, nRowsPerStrip);
    2038             :     }
    2039        2947 :     for (int i = 0; i < nBlockCount; ++i)
    2040             :     {
    2041             :         GPtrDiff_t cc = bIsTiled
    2042        2939 :                             ? static_cast<GPtrDiff_t>(TIFFTileSize(hTIFF))
    2043        2907 :                             : static_cast<GPtrDiff_t>(TIFFStripSize(hTIFF));
    2044        2939 :         if (!bIsTiled)
    2045             :         {
    2046             :             /* --------------------------------------------------------------------
    2047             :              */
    2048             :             /*      If this is the last strip in the image, and is partial, then
    2049             :              */
    2050             :             /*      we need to trim the number of scanlines written to the */
    2051             :             /*      amount of valid data we have. (#2748) */
    2052             :             /* --------------------------------------------------------------------
    2053             :              */
    2054        2907 :             int nStripWithinBand = i % nBlocksPerBand;
    2055        2907 :             if (nStripWithinBand * nRowsPerStrip > nYSize - nRowsPerStrip)
    2056             :             {
    2057           1 :                 cc = (cc / nRowsPerStrip) *
    2058           1 :                      (nYSize - nStripWithinBand * nRowsPerStrip);
    2059             :             }
    2060             :         }
    2061        2939 :         panOffset[i] = nOffset;
    2062        2939 :         panSize[i] = cc;
    2063        2939 :         nOffset += cc;
    2064             :     }
    2065           8 : }
    2066             : 
    2067             : /************************************************************************/
    2068             : /*                             Crystalize()                             */
    2069             : /*                                                                      */
    2070             : /*      Make sure that the directory information is written out for     */
    2071             : /*      a new file, require before writing any imagery data.            */
    2072             : /************************************************************************/
    2073             : 
    2074     2655520 : void GTiffDataset::Crystalize()
    2075             : 
    2076             : {
    2077     2655520 :     if (m_bCrystalized)
    2078     2649760 :         return;
    2079             : 
    2080             :     // TODO: libtiff writes extended tags in the order they are specified
    2081             :     // and not in increasing order.
    2082        5750 :     WriteMetadata(this, m_hTIFF, true, m_eProfile, m_osFilename.c_str(),
    2083        5750 :                   m_papszCreationOptions);
    2084        5750 :     WriteGeoTIFFInfo();
    2085        5750 :     if (m_bNoDataSet)
    2086         339 :         WriteNoDataValue(m_hTIFF, m_dfNoDataValue);
    2087        5411 :     else if (m_bNoDataSetAsInt64)
    2088           4 :         WriteNoDataValue(m_hTIFF, m_nNoDataValueInt64);
    2089        5407 :     else if (m_bNoDataSetAsUInt64)
    2090           4 :         WriteNoDataValue(m_hTIFF, m_nNoDataValueUInt64);
    2091             : 
    2092        5750 :     m_bMetadataChanged = false;
    2093        5750 :     m_bGeoTIFFInfoChanged = false;
    2094        5750 :     m_bNoDataChanged = false;
    2095        5750 :     m_bNeedsRewrite = false;
    2096             : 
    2097        5750 :     m_bCrystalized = true;
    2098             : 
    2099        5750 :     TIFFWriteCheck(m_hTIFF, TIFFIsTiled(m_hTIFF), "GTiffDataset::Crystalize");
    2100             : 
    2101        5750 :     TIFFWriteDirectory(m_hTIFF);
    2102        5750 :     if (m_bStreamingOut)
    2103             :     {
    2104             :         // We need to write twice the directory to be sure that custom
    2105             :         // TIFF tags are correctly sorted and that padding bytes have been
    2106             :         // added.
    2107           3 :         TIFFSetDirectory(m_hTIFF, 0);
    2108           3 :         TIFFWriteDirectory(m_hTIFF);
    2109             : 
    2110           3 :         if (VSIFSeekL(m_fpL, 0, SEEK_END) != 0)
    2111             :         {
    2112           0 :             ReportError(CE_Failure, CPLE_FileIO, "Could not seek");
    2113             :         }
    2114           3 :         const int nSize = static_cast<int>(VSIFTellL(m_fpL));
    2115             : 
    2116           3 :         TIFFSetDirectory(m_hTIFF, 0);
    2117           3 :         GTiffFillStreamableOffsetAndCount(m_hTIFF, nSize);
    2118           3 :         TIFFWriteDirectory(m_hTIFF);
    2119             : 
    2120           3 :         vsi_l_offset nDataLength = 0;
    2121             :         void *pabyBuffer =
    2122           3 :             VSIGetMemFileBuffer(m_pszTmpFilename, &nDataLength, FALSE);
    2123           3 :         if (static_cast<int>(VSIFWriteL(
    2124           3 :                 pabyBuffer, 1, static_cast<int>(nDataLength), m_fpToWrite)) !=
    2125             :             static_cast<int>(nDataLength))
    2126             :         {
    2127           0 :             ReportError(CE_Failure, CPLE_FileIO, "Could not write %d bytes",
    2128             :                         static_cast<int>(nDataLength));
    2129             :         }
    2130             :         // In case of single strip file, there's a libtiff check that would
    2131             :         // issue a warning since the file hasn't the required size.
    2132           3 :         CPLPushErrorHandler(CPLQuietErrorHandler);
    2133           3 :         TIFFSetDirectory(m_hTIFF, 0);
    2134           3 :         CPLPopErrorHandler();
    2135             :     }
    2136             :     else
    2137             :     {
    2138        5747 :         const tdir_t nNumberOfDirs = TIFFNumberOfDirectories(m_hTIFF);
    2139        5747 :         if (nNumberOfDirs > 0)
    2140             :         {
    2141        5747 :             TIFFSetDirectory(m_hTIFF, static_cast<tdir_t>(nNumberOfDirs - 1));
    2142             :         }
    2143             :     }
    2144             : 
    2145        5750 :     RestoreVolatileParameters(m_hTIFF);
    2146             : 
    2147        5750 :     m_nDirOffset = TIFFCurrentDirOffset(m_hTIFF);
    2148             : }
    2149             : 
    2150             : /************************************************************************/
    2151             : /*                             FlushCache()                             */
    2152             : /*                                                                      */
    2153             : /*      We override this so we can also flush out local tiff strip      */
    2154             : /*      cache if need be.                                               */
    2155             : /************************************************************************/
    2156             : 
    2157        4568 : CPLErr GTiffDataset::FlushCache(bool bAtClosing)
    2158             : 
    2159             : {
    2160        4568 :     return FlushCacheInternal(bAtClosing, true);
    2161             : }
    2162             : 
    2163       46602 : CPLErr GTiffDataset::FlushCacheInternal(bool bAtClosing, bool bFlushDirectory)
    2164             : {
    2165       46602 :     if (m_bIsFinalized)
    2166           1 :         return CE_None;
    2167             : 
    2168       46601 :     CPLErr eErr = GDALPamDataset::FlushCache(bAtClosing);
    2169             : 
    2170       46601 :     if (m_bLoadedBlockDirty && m_nLoadedBlock != -1)
    2171             :     {
    2172         264 :         if (FlushBlockBuf() != CE_None)
    2173           0 :             eErr = CE_Failure;
    2174             :     }
    2175             : 
    2176       46601 :     CPLFree(m_pabyBlockBuf);
    2177       46601 :     m_pabyBlockBuf = nullptr;
    2178       46601 :     m_nLoadedBlock = -1;
    2179       46601 :     m_bLoadedBlockDirty = false;
    2180             : 
    2181             :     // Finish compression
    2182       46601 :     auto poQueue = m_poBaseDS ? m_poBaseDS->m_poCompressQueue.get()
    2183       44245 :                               : m_poCompressQueue.get();
    2184       46601 :     if (poQueue)
    2185             :     {
    2186         161 :         poQueue->WaitCompletion();
    2187             : 
    2188             :         // Flush remaining data
    2189             :         // cppcheck-suppress constVariableReference
    2190             : 
    2191         161 :         auto &oQueue =
    2192         161 :             m_poBaseDS ? m_poBaseDS->m_asQueueJobIdx : m_asQueueJobIdx;
    2193         230 :         while (!oQueue.empty())
    2194             :         {
    2195          69 :             WaitCompletionForJobIdx(oQueue.front());
    2196             :         }
    2197             :     }
    2198             : 
    2199       46601 :     if (bFlushDirectory && GetAccess() == GA_Update)
    2200             :     {
    2201       13944 :         if (FlushDirectory() != CE_None)
    2202          12 :             eErr = CE_Failure;
    2203             :     }
    2204       46601 :     return eErr;
    2205             : }
    2206             : 
    2207             : /************************************************************************/
    2208             : /*                           FlushDirectory()                           */
    2209             : /************************************************************************/
    2210             : 
    2211       21918 : CPLErr GTiffDataset::FlushDirectory()
    2212             : 
    2213             : {
    2214       21918 :     CPLErr eErr = CE_None;
    2215             : 
    2216         686 :     const auto ReloadAllOtherDirectories = [this]()
    2217             :     {
    2218         338 :         const auto poBaseDS = m_poBaseDS ? m_poBaseDS : this;
    2219         341 :         for (auto &poOvrDS : poBaseDS->m_apoOverviewDS)
    2220             :         {
    2221           3 :             if (poOvrDS->m_bCrystalized && poOvrDS.get() != this)
    2222             :             {
    2223           3 :                 poOvrDS->ReloadDirectory(true);
    2224             :             }
    2225             : 
    2226           3 :             if (poOvrDS->m_poMaskDS && poOvrDS->m_poMaskDS.get() != this &&
    2227           0 :                 poOvrDS->m_poMaskDS->m_bCrystalized)
    2228             :             {
    2229           0 :                 poOvrDS->m_poMaskDS->ReloadDirectory(true);
    2230             :             }
    2231             :         }
    2232         338 :         if (poBaseDS->m_poMaskDS && poBaseDS->m_poMaskDS.get() != this &&
    2233           0 :             poBaseDS->m_poMaskDS->m_bCrystalized)
    2234             :         {
    2235           0 :             poBaseDS->m_poMaskDS->ReloadDirectory(true);
    2236             :         }
    2237         338 :         if (poBaseDS->m_bCrystalized && poBaseDS != this)
    2238             :         {
    2239           7 :             poBaseDS->ReloadDirectory(true);
    2240             :         }
    2241         338 :     };
    2242             : 
    2243       21918 :     if (eAccess == GA_Update)
    2244             :     {
    2245       15638 :         if (m_bMetadataChanged)
    2246             :         {
    2247         201 :             m_bNeedsRewrite =
    2248         201 :                 WriteMetadata(this, m_hTIFF, true, m_eProfile,
    2249         201 :                               m_osFilename.c_str(), m_papszCreationOptions);
    2250         201 :             m_bMetadataChanged = false;
    2251             : 
    2252         201 :             if (m_bForceUnsetRPC)
    2253             :             {
    2254           5 :                 double *padfRPCTag = nullptr;
    2255             :                 uint16_t nCount;
    2256           5 :                 if (TIFFGetField(m_hTIFF, TIFFTAG_RPCCOEFFICIENT, &nCount,
    2257           5 :                                  &padfRPCTag))
    2258             :                 {
    2259           3 :                     std::vector<double> zeroes(92);
    2260           3 :                     TIFFSetField(m_hTIFF, TIFFTAG_RPCCOEFFICIENT, 92,
    2261             :                                  zeroes.data());
    2262           3 :                     TIFFUnsetField(m_hTIFF, TIFFTAG_RPCCOEFFICIENT);
    2263           3 :                     m_bNeedsRewrite = true;
    2264             :                 }
    2265             : 
    2266           5 :                 if (m_poBaseDS == nullptr)
    2267             :                 {
    2268           5 :                     GDALWriteRPCTXTFile(m_osFilename.c_str(), nullptr);
    2269           5 :                     GDALWriteRPBFile(m_osFilename.c_str(), nullptr);
    2270             :                 }
    2271             :             }
    2272             :         }
    2273             : 
    2274       15638 :         if (m_bGeoTIFFInfoChanged)
    2275             :         {
    2276         145 :             WriteGeoTIFFInfo();
    2277         145 :             m_bGeoTIFFInfoChanged = false;
    2278             :         }
    2279             : 
    2280       15638 :         if (m_bNoDataChanged)
    2281             :         {
    2282          53 :             if (m_bNoDataSet)
    2283             :             {
    2284          37 :                 WriteNoDataValue(m_hTIFF, m_dfNoDataValue);
    2285             :             }
    2286          16 :             else if (m_bNoDataSetAsInt64)
    2287             :             {
    2288           0 :                 WriteNoDataValue(m_hTIFF, m_nNoDataValueInt64);
    2289             :             }
    2290          16 :             else if (m_bNoDataSetAsUInt64)
    2291             :             {
    2292           0 :                 WriteNoDataValue(m_hTIFF, m_nNoDataValueUInt64);
    2293             :             }
    2294             :             else
    2295             :             {
    2296          16 :                 UnsetNoDataValue(m_hTIFF);
    2297             :             }
    2298          53 :             m_bNeedsRewrite = true;
    2299          53 :             m_bNoDataChanged = false;
    2300             :         }
    2301             : 
    2302       15638 :         if (m_bNeedsRewrite)
    2303             :         {
    2304         363 :             if (!m_bCrystalized)
    2305             :             {
    2306          28 :                 Crystalize();
    2307             :             }
    2308             :             else
    2309             :             {
    2310         335 :                 const TIFFSizeProc pfnSizeProc = TIFFGetSizeProc(m_hTIFF);
    2311             : 
    2312         335 :                 m_nDirOffset = pfnSizeProc(TIFFClientdata(m_hTIFF));
    2313         335 :                 if ((m_nDirOffset % 2) == 1)
    2314          71 :                     ++m_nDirOffset;
    2315             : 
    2316         335 :                 if (TIFFRewriteDirectory(m_hTIFF) == 0)
    2317           0 :                     eErr = CE_Failure;
    2318             : 
    2319         335 :                 TIFFSetSubDirectory(m_hTIFF, m_nDirOffset);
    2320             : 
    2321         335 :                 ReloadAllOtherDirectories();
    2322             : 
    2323         335 :                 if (m_bLayoutIFDSBeforeData && m_bBlockOrderRowMajor &&
    2324           2 :                     m_bLeaderSizeAsUInt4 &&
    2325           2 :                     m_bTrailerRepeatedLast4BytesRepeated &&
    2326           2 :                     !m_bKnownIncompatibleEdition &&
    2327           2 :                     !m_bWriteKnownIncompatibleEdition)
    2328             :                 {
    2329           2 :                     ReportError(CE_Warning, CPLE_AppDefined,
    2330             :                                 "The IFD has been rewritten at the end of "
    2331             :                                 "the file, which breaks COG layout.");
    2332           2 :                     m_bKnownIncompatibleEdition = true;
    2333           2 :                     m_bWriteKnownIncompatibleEdition = true;
    2334             :                 }
    2335             :             }
    2336             : 
    2337         363 :             m_bNeedsRewrite = false;
    2338             :         }
    2339             :     }
    2340             : 
    2341             :     // There are some circumstances in which we can reach this point
    2342             :     // without having made this our directory (SetDirectory()) in which
    2343             :     // case we should not risk a flush.
    2344       37556 :     if (GetAccess() == GA_Update &&
    2345       15638 :         TIFFCurrentDirOffset(m_hTIFF) == m_nDirOffset)
    2346             :     {
    2347       15638 :         const TIFFSizeProc pfnSizeProc = TIFFGetSizeProc(m_hTIFF);
    2348             : 
    2349       15638 :         toff_t nNewDirOffset = pfnSizeProc(TIFFClientdata(m_hTIFF));
    2350       15638 :         if ((nNewDirOffset % 2) == 1)
    2351        3444 :             ++nNewDirOffset;
    2352             : 
    2353       15638 :         if (TIFFFlush(m_hTIFF) == 0)
    2354          12 :             eErr = CE_Failure;
    2355             : 
    2356       15638 :         if (m_nDirOffset != TIFFCurrentDirOffset(m_hTIFF))
    2357             :         {
    2358           3 :             m_nDirOffset = nNewDirOffset;
    2359           3 :             ReloadAllOtherDirectories();
    2360           3 :             CPLDebug("GTiff",
    2361             :                      "directory moved during flush in FlushDirectory()");
    2362             :         }
    2363             :     }
    2364             : 
    2365       21918 :     SetDirectory();
    2366       21918 :     return eErr;
    2367             : }
    2368             : 
    2369             : /************************************************************************/
    2370             : /*                           CleanOverviews()                           */
    2371             : /************************************************************************/
    2372             : 
    2373           5 : CPLErr GTiffDataset::CleanOverviews()
    2374             : 
    2375             : {
    2376           5 :     CPLAssert(!m_poBaseDS);
    2377             : 
    2378           5 :     ScanDirectories();
    2379             : 
    2380           5 :     FlushDirectory();
    2381             : 
    2382             :     /* -------------------------------------------------------------------- */
    2383             :     /*      Cleanup overviews objects, and get offsets to all overview      */
    2384             :     /*      directories.                                                    */
    2385             :     /* -------------------------------------------------------------------- */
    2386          10 :     std::vector<toff_t> anOvDirOffsets;
    2387             : 
    2388          10 :     for (auto &poOvrDS : m_apoOverviewDS)
    2389             :     {
    2390           5 :         anOvDirOffsets.push_back(poOvrDS->m_nDirOffset);
    2391           5 :         if (poOvrDS->m_poMaskDS)
    2392           1 :             anOvDirOffsets.push_back(poOvrDS->m_poMaskDS->m_nDirOffset);
    2393             :     }
    2394           5 :     m_apoOverviewDS.clear();
    2395             : 
    2396             :     /* -------------------------------------------------------------------- */
    2397             :     /*      Loop through all the directories, translating the offsets       */
    2398             :     /*      into indexes we can use with TIFFUnlinkDirectory().             */
    2399             :     /* -------------------------------------------------------------------- */
    2400          10 :     std::vector<uint16_t> anOvDirIndexes;
    2401           5 :     int iThisOffset = 1;
    2402             : 
    2403           5 :     TIFFSetDirectory(m_hTIFF, 0);
    2404             : 
    2405             :     while (true)
    2406             :     {
    2407          28 :         for (toff_t nOffset : anOvDirOffsets)
    2408             :         {
    2409          16 :             if (nOffset == TIFFCurrentDirOffset(m_hTIFF))
    2410             :             {
    2411           6 :                 anOvDirIndexes.push_back(static_cast<uint16_t>(iThisOffset));
    2412             :             }
    2413             :         }
    2414             : 
    2415          12 :         if (TIFFLastDirectory(m_hTIFF))
    2416           5 :             break;
    2417             : 
    2418           7 :         TIFFReadDirectory(m_hTIFF);
    2419           7 :         ++iThisOffset;
    2420           7 :     }
    2421             : 
    2422             :     /* -------------------------------------------------------------------- */
    2423             :     /*      Actually unlink the target directories.  Note that we do        */
    2424             :     /*      this from last to first so as to avoid renumbering any of       */
    2425             :     /*      the earlier directories we need to remove.                      */
    2426             :     /* -------------------------------------------------------------------- */
    2427          11 :     while (!anOvDirIndexes.empty())
    2428             :     {
    2429           6 :         TIFFUnlinkDirectory(m_hTIFF, anOvDirIndexes.back());
    2430           6 :         anOvDirIndexes.pop_back();
    2431             :     }
    2432             : 
    2433           5 :     if (m_poMaskDS)
    2434             :     {
    2435           1 :         m_poMaskDS->m_apoOverviewDS.clear();
    2436             :     }
    2437             : 
    2438           5 :     if (!SetDirectory())
    2439           0 :         return CE_Failure;
    2440             : 
    2441           5 :     return CE_None;
    2442             : }
    2443             : 
    2444             : /************************************************************************/
    2445             : /*                     RegisterNewOverviewDataset()                     */
    2446             : /************************************************************************/
    2447             : 
    2448         517 : CPLErr GTiffDataset::RegisterNewOverviewDataset(toff_t nOverviewOffset,
    2449             :                                                 int l_nJpegQuality,
    2450             :                                                 CSLConstList papszOptions)
    2451             : {
    2452             :     const auto GetOptionValue =
    2453        5687 :         [papszOptions](const char *pszOptionKey, const char *pszConfigOptionKey,
    2454       11373 :                        const char **ppszKeyUsed = nullptr)
    2455             :     {
    2456        5687 :         const char *pszVal = CSLFetchNameValue(papszOptions, pszOptionKey);
    2457        5687 :         if (pszVal)
    2458             :         {
    2459           1 :             if (ppszKeyUsed)
    2460           1 :                 *ppszKeyUsed = pszOptionKey;
    2461           1 :             return pszVal;
    2462             :         }
    2463        5686 :         pszVal = CSLFetchNameValue(papszOptions, pszConfigOptionKey);
    2464        5686 :         if (pszVal)
    2465             :         {
    2466           0 :             if (ppszKeyUsed)
    2467           0 :                 *ppszKeyUsed = pszConfigOptionKey;
    2468           0 :             return pszVal;
    2469             :         }
    2470        5686 :         if (pszConfigOptionKey)
    2471             :         {
    2472        5686 :             pszVal = CPLGetConfigOption(pszConfigOptionKey, nullptr);
    2473        5686 :             if (pszVal && ppszKeyUsed)
    2474          13 :                 *ppszKeyUsed = pszConfigOptionKey;
    2475             :         }
    2476        5686 :         return pszVal;
    2477         517 :     };
    2478             : 
    2479         517 :     int nZLevel = m_nZLevel;
    2480         517 :     if (const char *opt = GetOptionValue("ZLEVEL", "ZLEVEL_OVERVIEW"))
    2481             :     {
    2482           4 :         nZLevel = atoi(opt);
    2483             :     }
    2484             : 
    2485         517 :     int nZSTDLevel = m_nZSTDLevel;
    2486         517 :     if (const char *opt = GetOptionValue("ZSTD_LEVEL", "ZSTD_LEVEL_OVERVIEW"))
    2487             :     {
    2488           4 :         nZSTDLevel = atoi(opt);
    2489             :     }
    2490             : 
    2491         517 :     bool bWebpLossless = m_bWebPLossless;
    2492             :     const char *pszWebPLosslessOverview =
    2493         517 :         GetOptionValue("WEBP_LOSSLESS", "WEBP_LOSSLESS_OVERVIEW");
    2494         517 :     if (pszWebPLosslessOverview)
    2495             :     {
    2496           2 :         bWebpLossless = CPLTestBool(pszWebPLosslessOverview);
    2497             :     }
    2498             : 
    2499         517 :     int nWebpLevel = m_nWebPLevel;
    2500         517 :     const char *pszKeyWebpLevel = "";
    2501         517 :     if (const char *opt = GetOptionValue("WEBP_LEVEL", "WEBP_LEVEL_OVERVIEW",
    2502             :                                          &pszKeyWebpLevel))
    2503             :     {
    2504          14 :         if (pszWebPLosslessOverview == nullptr && m_bWebPLossless)
    2505             :         {
    2506           1 :             CPLDebug("GTiff",
    2507             :                      "%s specified, but not WEBP_LOSSLESS_OVERVIEW. "
    2508             :                      "Assuming WEBP_LOSSLESS_OVERVIEW=NO",
    2509             :                      pszKeyWebpLevel);
    2510           1 :             bWebpLossless = false;
    2511             :         }
    2512          13 :         else if (bWebpLossless)
    2513             :         {
    2514           0 :             CPLError(CE_Warning, CPLE_AppDefined,
    2515             :                      "%s is specified, but WEBP_LOSSLESS_OVERVIEW=YES. "
    2516             :                      "%s will be ignored.",
    2517             :                      pszKeyWebpLevel, pszKeyWebpLevel);
    2518             :         }
    2519          14 :         nWebpLevel = atoi(opt);
    2520             :     }
    2521             : 
    2522         517 :     double dfMaxZError = m_dfMaxZErrorOverview;
    2523         517 :     if (const char *opt = GetOptionValue("MAX_Z_ERROR", "MAX_Z_ERROR_OVERVIEW"))
    2524             :     {
    2525          20 :         dfMaxZError = CPLAtof(opt);
    2526             :     }
    2527             : 
    2528         517 :     signed char nJpegTablesMode = m_nJpegTablesMode;
    2529         517 :     if (const char *opt =
    2530         517 :             GetOptionValue("JPEG_TABLESMODE", "JPEG_TABLESMODE_OVERVIEW"))
    2531             :     {
    2532           0 :         nJpegTablesMode = static_cast<signed char>(atoi(opt));
    2533             :     }
    2534             : 
    2535             : #ifdef HAVE_JXL
    2536         517 :     bool bJXLLossless = m_bJXLLossless;
    2537         517 :     if (const char *opt =
    2538         517 :             GetOptionValue("JXL_LOSSLESS", "JXL_LOSSLESS_OVERVIEW"))
    2539             :     {
    2540           0 :         bJXLLossless = CPLTestBool(opt);
    2541             :     }
    2542             : 
    2543         517 :     float fJXLDistance = m_fJXLDistance;
    2544         517 :     if (const char *opt =
    2545         517 :             GetOptionValue("JXL_DISTANCE", "JXL_DISTANCE_OVERVIEW"))
    2546             :     {
    2547           0 :         fJXLDistance = static_cast<float>(CPLAtof(opt));
    2548             :     }
    2549             : 
    2550         517 :     float fJXLAlphaDistance = m_fJXLAlphaDistance;
    2551         517 :     if (const char *opt =
    2552         517 :             GetOptionValue("JXL_ALPHA_DISTANCE", "JXL_ALPHA_DISTANCE_OVERVIEW"))
    2553             :     {
    2554           0 :         fJXLAlphaDistance = static_cast<float>(CPLAtof(opt));
    2555             :     }
    2556             : 
    2557         517 :     int nJXLEffort = m_nJXLEffort;
    2558         517 :     if (const char *opt = GetOptionValue("JXL_EFFORT", "JXL_EFFORT_OVERVIEW"))
    2559             :     {
    2560           0 :         nJXLEffort = atoi(opt);
    2561             :     }
    2562             : #endif
    2563             : 
    2564        1034 :     auto poODS = std::make_shared<GTiffDataset>();
    2565         517 :     poODS->ShareLockWithParentDataset(this);
    2566         517 :     poODS->eAccess = GA_Update;
    2567         517 :     poODS->m_osFilename = m_osFilename;
    2568         517 :     const char *pszSparseOK = GetOptionValue("SPARSE_OK", "SPARSE_OK_OVERVIEW");
    2569         517 :     if (pszSparseOK && CPLTestBool(pszSparseOK))
    2570             :     {
    2571           1 :         poODS->m_bWriteEmptyTiles = false;
    2572           1 :         poODS->m_bFillEmptyTilesAtClosing = false;
    2573             :     }
    2574             :     else
    2575             :     {
    2576         516 :         poODS->m_bWriteEmptyTiles = m_bWriteEmptyTiles;
    2577         516 :         poODS->m_bFillEmptyTilesAtClosing = m_bFillEmptyTilesAtClosing;
    2578             :     }
    2579         517 :     poODS->m_nJpegQuality = static_cast<signed char>(l_nJpegQuality);
    2580         517 :     poODS->m_nWebPLevel = static_cast<signed char>(nWebpLevel);
    2581         517 :     poODS->m_nZLevel = static_cast<signed char>(nZLevel);
    2582         517 :     poODS->m_nLZMAPreset = m_nLZMAPreset;
    2583         517 :     poODS->m_nZSTDLevel = static_cast<signed char>(nZSTDLevel);
    2584         517 :     poODS->m_bWebPLossless = bWebpLossless;
    2585         517 :     poODS->m_nJpegTablesMode = nJpegTablesMode;
    2586         517 :     poODS->m_dfMaxZError = dfMaxZError;
    2587         517 :     poODS->m_dfMaxZErrorOverview = dfMaxZError;
    2588        1034 :     memcpy(poODS->m_anLercAddCompressionAndVersion,
    2589         517 :            m_anLercAddCompressionAndVersion,
    2590             :            sizeof(m_anLercAddCompressionAndVersion));
    2591             : #ifdef HAVE_JXL
    2592         517 :     poODS->m_bJXLLossless = bJXLLossless;
    2593         517 :     poODS->m_fJXLDistance = fJXLDistance;
    2594         517 :     poODS->m_fJXLAlphaDistance = fJXLAlphaDistance;
    2595         517 :     poODS->m_nJXLEffort = nJXLEffort;
    2596             : #endif
    2597             : 
    2598         517 :     if (poODS->OpenOffset(VSI_TIFFOpenChild(m_hTIFF), nOverviewOffset,
    2599         517 :                           GA_Update) != CE_None)
    2600             :     {
    2601           0 :         return CE_Failure;
    2602             :     }
    2603             : 
    2604             :     // Assign color interpretation from main dataset
    2605         517 :     const int l_nBands = GetRasterCount();
    2606        1538 :     for (int i = 1; i <= l_nBands; i++)
    2607             :     {
    2608        1021 :         auto poBand = dynamic_cast<GTiffRasterBand *>(poODS->GetRasterBand(i));
    2609        1021 :         if (poBand)
    2610        1021 :             poBand->m_eBandInterp = GetRasterBand(i)->GetColorInterpretation();
    2611             :     }
    2612             : 
    2613             :     // Do that now that m_nCompression is set
    2614         517 :     poODS->RestoreVolatileParameters(poODS->m_hTIFF);
    2615             : 
    2616         517 :     poODS->m_poBaseDS = this;
    2617         517 :     poODS->m_bIsOverview = true;
    2618             : 
    2619         517 :     m_apoOverviewDS.push_back(std::move(poODS));
    2620         517 :     return CE_None;
    2621             : }
    2622             : 
    2623             : /************************************************************************/
    2624             : /*                        CreateTIFFColorTable()                        */
    2625             : /************************************************************************/
    2626             : 
    2627          12 : static void CreateTIFFColorTable(
    2628             :     GDALColorTable *poColorTable, int nBits, int nColorTableMultiplier,
    2629             :     std::vector<unsigned short> &anTRed, std::vector<unsigned short> &anTGreen,
    2630             :     std::vector<unsigned short> &anTBlue, unsigned short *&panRed,
    2631             :     unsigned short *&panGreen, unsigned short *&panBlue)
    2632             : {
    2633             :     int nColors;
    2634             : 
    2635          12 :     if (nBits == 8)
    2636          12 :         nColors = 256;
    2637           0 :     else if (nBits < 8)
    2638           0 :         nColors = 1 << nBits;
    2639             :     else
    2640           0 :         nColors = 65536;
    2641             : 
    2642          12 :     anTRed.resize(nColors, 0);
    2643          12 :     anTGreen.resize(nColors, 0);
    2644          12 :     anTBlue.resize(nColors, 0);
    2645             : 
    2646        3084 :     for (int iColor = 0; iColor < nColors; ++iColor)
    2647             :     {
    2648        3072 :         if (iColor < poColorTable->GetColorEntryCount())
    2649             :         {
    2650             :             GDALColorEntry sRGB;
    2651             : 
    2652        3072 :             poColorTable->GetColorEntryAsRGB(iColor, &sRGB);
    2653             : 
    2654        3072 :             anTRed[iColor] = GTiffDataset::ClampCTEntry(iColor, 1, sRGB.c1,
    2655             :                                                         nColorTableMultiplier);
    2656        3072 :             anTGreen[iColor] = GTiffDataset::ClampCTEntry(
    2657        3072 :                 iColor, 2, sRGB.c2, nColorTableMultiplier);
    2658        3072 :             anTBlue[iColor] = GTiffDataset::ClampCTEntry(iColor, 3, sRGB.c3,
    2659             :                                                          nColorTableMultiplier);
    2660             :         }
    2661             :         else
    2662             :         {
    2663           0 :             anTRed[iColor] = 0;
    2664           0 :             anTGreen[iColor] = 0;
    2665           0 :             anTBlue[iColor] = 0;
    2666             :         }
    2667             :     }
    2668             : 
    2669          12 :     panRed = &(anTRed[0]);
    2670          12 :     panGreen = &(anTGreen[0]);
    2671          12 :     panBlue = &(anTBlue[0]);
    2672          12 : }
    2673             : 
    2674             : /************************************************************************/
    2675             : /*                       GetOverviewParameters()                        */
    2676             : /************************************************************************/
    2677             : 
    2678         331 : bool GTiffDataset::GetOverviewParameters(
    2679             :     int &nCompression, uint16_t &nPlanarConfig, uint16_t &nPredictor,
    2680             :     uint16_t &nPhotometric, int &nOvrJpegQuality, std::string &osNoData,
    2681             :     uint16_t *&panExtraSampleValues, uint16_t &nExtraSamples,
    2682             :     CSLConstList papszOptions) const
    2683             : {
    2684             :     const auto GetOptionValue =
    2685        1097 :         [papszOptions](const char *pszOptionKey, const char *pszConfigOptionKey,
    2686        2186 :                        const char **ppszKeyUsed = nullptr)
    2687             :     {
    2688        1097 :         const char *pszVal = CSLFetchNameValue(papszOptions, pszOptionKey);
    2689        1097 :         if (pszVal)
    2690             :         {
    2691           8 :             if (ppszKeyUsed)
    2692           8 :                 *ppszKeyUsed = pszOptionKey;
    2693           8 :             return pszVal;
    2694             :         }
    2695        1089 :         pszVal = CSLFetchNameValue(papszOptions, pszConfigOptionKey);
    2696        1089 :         if (pszVal)
    2697             :         {
    2698           0 :             if (ppszKeyUsed)
    2699           0 :                 *ppszKeyUsed = pszConfigOptionKey;
    2700           0 :             return pszVal;
    2701             :         }
    2702        1089 :         pszVal = CPLGetConfigOption(pszConfigOptionKey, nullptr);
    2703        1089 :         if (pszVal && ppszKeyUsed)
    2704          60 :             *ppszKeyUsed = pszConfigOptionKey;
    2705        1089 :         return pszVal;
    2706         331 :     };
    2707             : 
    2708             :     /* -------------------------------------------------------------------- */
    2709             :     /*      Determine compression method.                                   */
    2710             :     /* -------------------------------------------------------------------- */
    2711         331 :     nCompression = m_nCompression;
    2712         331 :     const char *pszOptionKey = "";
    2713             :     const char *pszCompressValue =
    2714         331 :         GetOptionValue("COMPRESS", "COMPRESS_OVERVIEW", &pszOptionKey);
    2715         331 :     if (pszCompressValue != nullptr)
    2716             :     {
    2717          58 :         nCompression =
    2718          58 :             GTIFFGetCompressionMethod(pszCompressValue, pszOptionKey);
    2719          58 :         if (nCompression < 0)
    2720             :         {
    2721           0 :             nCompression = m_nCompression;
    2722             :         }
    2723             :     }
    2724             : 
    2725             :     /* -------------------------------------------------------------------- */
    2726             :     /*      Determine planar configuration.                                 */
    2727             :     /* -------------------------------------------------------------------- */
    2728         331 :     nPlanarConfig = m_nPlanarConfig;
    2729         331 :     if (nCompression == COMPRESSION_WEBP)
    2730             :     {
    2731          11 :         nPlanarConfig = PLANARCONFIG_CONTIG;
    2732             :     }
    2733             :     const char *pszInterleave =
    2734         331 :         GetOptionValue("INTERLEAVE", "INTERLEAVE_OVERVIEW", &pszOptionKey);
    2735         331 :     if (pszInterleave != nullptr && pszInterleave[0] != '\0')
    2736             :     {
    2737           2 :         if (EQUAL(pszInterleave, "PIXEL"))
    2738           1 :             nPlanarConfig = PLANARCONFIG_CONTIG;
    2739           1 :         else if (EQUAL(pszInterleave, "BAND"))
    2740           1 :             nPlanarConfig = PLANARCONFIG_SEPARATE;
    2741             :         else
    2742             :         {
    2743           0 :             CPLError(CE_Warning, CPLE_AppDefined,
    2744             :                      "%s=%s unsupported, "
    2745             :                      "value must be PIXEL or BAND. ignoring",
    2746             :                      pszOptionKey, pszInterleave);
    2747             :         }
    2748             :     }
    2749             : 
    2750             :     /* -------------------------------------------------------------------- */
    2751             :     /*      Determine predictor tag                                         */
    2752             :     /* -------------------------------------------------------------------- */
    2753         331 :     nPredictor = PREDICTOR_NONE;
    2754         331 :     if (GTIFFSupportsPredictor(nCompression))
    2755             :     {
    2756             :         const char *pszPredictor =
    2757          77 :             GetOptionValue("PREDICTOR", "PREDICTOR_OVERVIEW");
    2758          77 :         if (pszPredictor != nullptr)
    2759             :         {
    2760           1 :             nPredictor = static_cast<uint16_t>(atoi(pszPredictor));
    2761             :         }
    2762          76 :         else if (GTIFFSupportsPredictor(m_nCompression))
    2763          75 :             TIFFGetField(m_hTIFF, TIFFTAG_PREDICTOR, &nPredictor);
    2764             :     }
    2765             : 
    2766             :     /* -------------------------------------------------------------------- */
    2767             :     /*      Determine photometric tag                                       */
    2768             :     /* -------------------------------------------------------------------- */
    2769         331 :     if (m_nPhotometric == PHOTOMETRIC_YCBCR && nCompression != COMPRESSION_JPEG)
    2770           1 :         nPhotometric = PHOTOMETRIC_RGB;
    2771             :     else
    2772         330 :         nPhotometric = m_nPhotometric;
    2773             :     const char *pszPhotometric =
    2774         331 :         GetOptionValue("PHOTOMETRIC", "PHOTOMETRIC_OVERVIEW", &pszOptionKey);
    2775         331 :     if (!GTIFFUpdatePhotometric(pszPhotometric, pszOptionKey, nCompression,
    2776         331 :                                 pszInterleave, nBands, nPhotometric,
    2777             :                                 nPlanarConfig))
    2778             :     {
    2779           0 :         return false;
    2780             :     }
    2781             : 
    2782             :     /* -------------------------------------------------------------------- */
    2783             :     /*      Determine JPEG quality                                          */
    2784             :     /* -------------------------------------------------------------------- */
    2785         331 :     nOvrJpegQuality = m_nJpegQuality;
    2786         331 :     if (nCompression == COMPRESSION_JPEG)
    2787             :     {
    2788             :         const char *pszJPEGQuality =
    2789          27 :             GetOptionValue("JPEG_QUALITY", "JPEG_QUALITY_OVERVIEW");
    2790          27 :         if (pszJPEGQuality != nullptr)
    2791             :         {
    2792           9 :             nOvrJpegQuality = atoi(pszJPEGQuality);
    2793             :         }
    2794             :     }
    2795             : 
    2796             :     /* -------------------------------------------------------------------- */
    2797             :     /*      Set nodata.                                                     */
    2798             :     /* -------------------------------------------------------------------- */
    2799         331 :     if (m_bNoDataSet)
    2800             :     {
    2801          17 :         osNoData = GTiffFormatGDALNoDataTagValue(m_dfNoDataValue);
    2802             :     }
    2803             : 
    2804             :     /* -------------------------------------------------------------------- */
    2805             :     /*      Fetch extra sample tag                                          */
    2806             :     /* -------------------------------------------------------------------- */
    2807         331 :     panExtraSampleValues = nullptr;
    2808         331 :     nExtraSamples = 0;
    2809         331 :     if (TIFFGetField(m_hTIFF, TIFFTAG_EXTRASAMPLES, &nExtraSamples,
    2810         331 :                      &panExtraSampleValues))
    2811             :     {
    2812             :         uint16_t *panExtraSampleValuesNew = static_cast<uint16_t *>(
    2813          40 :             CPLMalloc(nExtraSamples * sizeof(uint16_t)));
    2814          40 :         memcpy(panExtraSampleValuesNew, panExtraSampleValues,
    2815          40 :                nExtraSamples * sizeof(uint16_t));
    2816          40 :         panExtraSampleValues = panExtraSampleValuesNew;
    2817             :     }
    2818             :     else
    2819             :     {
    2820         291 :         panExtraSampleValues = nullptr;
    2821         291 :         nExtraSamples = 0;
    2822             :     }
    2823             : 
    2824         331 :     return true;
    2825             : }
    2826             : 
    2827             : /************************************************************************/
    2828             : /*                  CreateOverviewsFromSrcOverviews()                   */
    2829             : /************************************************************************/
    2830             : 
    2831             : // If poOvrDS is not null, it is used and poSrcDS is ignored.
    2832             : 
    2833          71 : CPLErr GTiffDataset::CreateOverviewsFromSrcOverviews(GDALDataset *poSrcDS,
    2834             :                                                      GDALDataset *poOvrDS,
    2835             :                                                      int nOverviews)
    2836             : {
    2837          71 :     CPLAssert(poSrcDS->GetRasterCount() != 0);
    2838          71 :     CPLAssert(m_apoOverviewDS.empty());
    2839             : 
    2840          71 :     ScanDirectories();
    2841             : 
    2842          71 :     FlushDirectory();
    2843             : 
    2844          71 :     int nOvBitsPerSample = m_nBitsPerSample;
    2845             : 
    2846             :     /* -------------------------------------------------------------------- */
    2847             :     /*      Do we need some metadata for the overviews?                     */
    2848             :     /* -------------------------------------------------------------------- */
    2849         142 :     CPLString osMetadata;
    2850             : 
    2851          71 :     GTIFFBuildOverviewMetadata("NONE", this, false, osMetadata);
    2852             : 
    2853             :     int nCompression;
    2854             :     uint16_t nPlanarConfig;
    2855             :     uint16_t nPredictor;
    2856             :     uint16_t nPhotometric;
    2857             :     int nOvrJpegQuality;
    2858         142 :     std::string osNoData;
    2859          71 :     uint16_t *panExtraSampleValues = nullptr;
    2860          71 :     uint16_t nExtraSamples = 0;
    2861          71 :     if (!GetOverviewParameters(nCompression, nPlanarConfig, nPredictor,
    2862             :                                nPhotometric, nOvrJpegQuality, osNoData,
    2863             :                                panExtraSampleValues, nExtraSamples,
    2864             :                                /*papszOptions=*/nullptr))
    2865             :     {
    2866           0 :         return CE_Failure;
    2867             :     }
    2868             : 
    2869             :     /* -------------------------------------------------------------------- */
    2870             :     /*      Do we have a palette?  If so, create a TIFF compatible version. */
    2871             :     /* -------------------------------------------------------------------- */
    2872         142 :     std::vector<unsigned short> anTRed;
    2873         142 :     std::vector<unsigned short> anTGreen;
    2874          71 :     std::vector<unsigned short> anTBlue;
    2875          71 :     unsigned short *panRed = nullptr;
    2876          71 :     unsigned short *panGreen = nullptr;
    2877          71 :     unsigned short *panBlue = nullptr;
    2878             : 
    2879          71 :     if (nPhotometric == PHOTOMETRIC_PALETTE && m_poColorTable != nullptr)
    2880             :     {
    2881           0 :         if (m_nColorTableMultiplier == 0)
    2882           0 :             m_nColorTableMultiplier = DEFAULT_COLOR_TABLE_MULTIPLIER_257;
    2883             : 
    2884           0 :         CreateTIFFColorTable(m_poColorTable.get(), nOvBitsPerSample,
    2885             :                              m_nColorTableMultiplier, anTRed, anTGreen, anTBlue,
    2886             :                              panRed, panGreen, panBlue);
    2887             :     }
    2888             : 
    2889          71 :     int nOvrBlockXSize = 0;
    2890          71 :     int nOvrBlockYSize = 0;
    2891          71 :     GTIFFGetOverviewBlockSize(GDALRasterBand::ToHandle(GetRasterBand(1)),
    2892             :                               &nOvrBlockXSize, &nOvrBlockYSize, nullptr,
    2893             :                               nullptr);
    2894             : 
    2895          71 :     CPLErr eErr = CE_None;
    2896             : 
    2897         202 :     for (int i = 0; i < nOverviews && eErr == CE_None; ++i)
    2898             :     {
    2899             :         GDALRasterBand *poOvrBand =
    2900         171 :             poOvrDS ? ((i == 0) ? poOvrDS->GetRasterBand(1)
    2901          40 :                                 : poOvrDS->GetRasterBand(1)->GetOverview(i - 1))
    2902          55 :                     : poSrcDS->GetRasterBand(1)->GetOverview(i);
    2903             : 
    2904         131 :         int nOXSize = poOvrBand->GetXSize();
    2905         131 :         int nOYSize = poOvrBand->GetYSize();
    2906             : 
    2907         262 :         toff_t nOverviewOffset = GTIFFWriteDirectory(
    2908             :             m_hTIFF, FILETYPE_REDUCEDIMAGE, nOXSize, nOYSize, nOvBitsPerSample,
    2909         131 :             nPlanarConfig, m_nSamplesPerPixel, nOvrBlockXSize, nOvrBlockYSize,
    2910         131 :             TRUE, nCompression, nPhotometric, m_nSampleFormat, nPredictor,
    2911             :             panRed, panGreen, panBlue, nExtraSamples, panExtraSampleValues,
    2912             :             osMetadata,
    2913         131 :             nOvrJpegQuality >= 0 ? CPLSPrintf("%d", nOvrJpegQuality) : nullptr,
    2914         131 :             CPLSPrintf("%d", m_nJpegTablesMode),
    2915           2 :             osNoData.empty() ? nullptr : osNoData.c_str(),
    2916         131 :             m_anLercAddCompressionAndVersion, m_bWriteCOGLayout);
    2917             : 
    2918         131 :         if (nOverviewOffset == 0)
    2919           0 :             eErr = CE_Failure;
    2920             :         else
    2921         131 :             eErr = RegisterNewOverviewDataset(nOverviewOffset, nOvrJpegQuality,
    2922             :                                               nullptr);
    2923             :     }
    2924             : 
    2925             :     // For directory reloading, so that the chaining to the next directory is
    2926             :     // reloaded, as well as compression parameters.
    2927          71 :     ReloadDirectory();
    2928             : 
    2929          71 :     CPLFree(panExtraSampleValues);
    2930          71 :     panExtraSampleValues = nullptr;
    2931             : 
    2932          71 :     return eErr;
    2933             : }
    2934             : 
    2935             : /************************************************************************/
    2936             : /*                    CreateInternalMaskOverviews()                     */
    2937             : /************************************************************************/
    2938             : 
    2939         275 : CPLErr GTiffDataset::CreateInternalMaskOverviews(int nOvrBlockXSize,
    2940             :                                                  int nOvrBlockYSize)
    2941             : {
    2942         275 :     ScanDirectories();
    2943             : 
    2944             :     /* -------------------------------------------------------------------- */
    2945             :     /*      Create overviews for the mask.                                  */
    2946             :     /* -------------------------------------------------------------------- */
    2947         275 :     CPLErr eErr = CE_None;
    2948             : 
    2949         275 :     if (m_poMaskDS != nullptr && m_poMaskDS->GetRasterCount() == 1)
    2950             :     {
    2951             :         int nMaskOvrCompression;
    2952          43 :         if (strstr(GDALGetMetadataItem(GDALGetDriverByName("GTiff"),
    2953             :                                        GDAL_DMD_CREATIONOPTIONLIST, nullptr),
    2954          43 :                    "<Value>DEFLATE</Value>") != nullptr)
    2955          43 :             nMaskOvrCompression = COMPRESSION_ADOBE_DEFLATE;
    2956             :         else
    2957           0 :             nMaskOvrCompression = COMPRESSION_PACKBITS;
    2958             : 
    2959         115 :         for (auto &poOvrDS : m_apoOverviewDS)
    2960             :         {
    2961          72 :             if (poOvrDS->m_poMaskDS == nullptr)
    2962             :             {
    2963          60 :                 const toff_t nOverviewOffset = GTIFFWriteDirectory(
    2964             :                     m_hTIFF, FILETYPE_REDUCEDIMAGE | FILETYPE_MASK,
    2965          60 :                     poOvrDS->nRasterXSize, poOvrDS->nRasterYSize, 1,
    2966             :                     PLANARCONFIG_CONTIG, 1, nOvrBlockXSize, nOvrBlockYSize,
    2967             :                     TRUE, nMaskOvrCompression, PHOTOMETRIC_MASK,
    2968             :                     SAMPLEFORMAT_UINT, PREDICTOR_NONE, nullptr, nullptr,
    2969             :                     nullptr, 0, nullptr, "", nullptr, nullptr, nullptr, nullptr,
    2970          60 :                     m_bWriteCOGLayout);
    2971             : 
    2972          60 :                 if (nOverviewOffset == 0)
    2973             :                 {
    2974           0 :                     eErr = CE_Failure;
    2975           0 :                     continue;
    2976             :                 }
    2977             : 
    2978         120 :                 auto poMaskODS = std::make_shared<GTiffDataset>();
    2979          60 :                 poMaskODS->eAccess = GA_Update;
    2980          60 :                 poMaskODS->ShareLockWithParentDataset(this);
    2981          60 :                 poMaskODS->m_osFilename = m_osFilename;
    2982          60 :                 if (poMaskODS->OpenOffset(VSI_TIFFOpenChild(m_hTIFF),
    2983             :                                           nOverviewOffset,
    2984          60 :                                           GA_Update) != CE_None)
    2985             :                 {
    2986           0 :                     eErr = CE_Failure;
    2987             :                 }
    2988             :                 else
    2989             :                 {
    2990         120 :                     poMaskODS->m_bPromoteTo8Bits =
    2991          60 :                         CPLTestBool(CPLGetConfigOption(
    2992             :                             "GDAL_TIFF_INTERNAL_MASK_TO_8BIT", "YES"));
    2993          60 :                     poMaskODS->m_poBaseDS = this;
    2994          60 :                     poMaskODS->m_poImageryDS = poOvrDS.get();
    2995          60 :                     poOvrDS->m_poMaskDS = poMaskODS;
    2996          60 :                     m_poMaskDS->m_apoOverviewDS.push_back(std::move(poMaskODS));
    2997             :                 }
    2998             :             }
    2999             :         }
    3000             :     }
    3001             : 
    3002         275 :     ReloadDirectory();
    3003             : 
    3004         275 :     return eErr;
    3005             : }
    3006             : 
    3007             : /************************************************************************/
    3008             : /*                            AddOverviews()                            */
    3009             : /************************************************************************/
    3010             : 
    3011             : CPLErr
    3012          13 : GTiffDataset::AddOverviews(const std::vector<GDALDataset *> &apoSrcOvrDSIn,
    3013             :                            GDALProgressFunc pfnProgress, void *pProgressData,
    3014             :                            CSLConstList papszOptions)
    3015             : {
    3016             :     /* -------------------------------------------------------------------- */
    3017             :     /*      If we don't have read access, then create the overviews         */
    3018             :     /*      externally.                                                     */
    3019             :     /* -------------------------------------------------------------------- */
    3020          13 :     if (GetAccess() != GA_Update)
    3021             :     {
    3022           4 :         CPLDebug("GTiff", "File open for read-only accessing, "
    3023             :                           "creating overviews externally.");
    3024             : 
    3025           4 :         CPLErr eErr = GDALDataset::AddOverviews(apoSrcOvrDSIn, pfnProgress,
    3026             :                                                 pProgressData, papszOptions);
    3027           4 :         if (eErr == CE_None && m_poMaskDS)
    3028             :         {
    3029           0 :             ReportError(
    3030             :                 CE_Warning, CPLE_NotSupported,
    3031             :                 "Building external overviews whereas there is an internal "
    3032             :                 "mask is not fully supported. "
    3033             :                 "The overviews of the non-mask bands will be created, "
    3034             :                 "but not the overviews of the mask band.");
    3035             :         }
    3036           4 :         return eErr;
    3037             :     }
    3038             : 
    3039          18 :     std::vector<GDALDataset *> apoSrcOvrDS = apoSrcOvrDSIn;
    3040             :     // Sort overviews by descending size
    3041           9 :     std::sort(apoSrcOvrDS.begin(), apoSrcOvrDS.end(),
    3042           0 :               [](const GDALDataset *poDS1, const GDALDataset *poDS2)
    3043           0 :               { return poDS1->GetRasterXSize() > poDS2->GetRasterXSize(); });
    3044             : 
    3045           9 :     if (!GDALDefaultOverviews::CheckSrcOverviewsConsistencyWithBase(
    3046             :             this, apoSrcOvrDS))
    3047           5 :         return CE_Failure;
    3048             : 
    3049           4 :     ScanDirectories();
    3050             : 
    3051             :     // Make implicit JPEG overviews invisible, but do not destroy
    3052             :     // them in case they are already used (not sure that the client
    3053             :     // has the right to do that). Behavior maybe undefined in GDAL API.
    3054           4 :     std::swap(m_apoJPEGOverviewDSOld, m_apoJPEGOverviewDS);
    3055           4 :     m_apoJPEGOverviewDS.clear();
    3056             : 
    3057           4 :     FlushDirectory();
    3058             : 
    3059             :     /* -------------------------------------------------------------------- */
    3060             :     /*      If we are averaging bit data to grayscale we need to create     */
    3061             :     /*      8bit overviews.                                                 */
    3062             :     /* -------------------------------------------------------------------- */
    3063           4 :     int nOvBitsPerSample = m_nBitsPerSample;
    3064             : 
    3065             :     /* -------------------------------------------------------------------- */
    3066             :     /*      Do we need some metadata for the overviews?                     */
    3067             :     /* -------------------------------------------------------------------- */
    3068           8 :     CPLString osMetadata;
    3069             : 
    3070           4 :     const bool bIsForMaskBand = nBands == 1 && GetRasterBand(1)->IsMaskBand();
    3071           4 :     GTIFFBuildOverviewMetadata(/* resampling = */ "", this, bIsForMaskBand,
    3072             :                                osMetadata);
    3073             : 
    3074             :     int nCompression;
    3075             :     uint16_t nPlanarConfig;
    3076             :     uint16_t nPredictor;
    3077             :     uint16_t nPhotometric;
    3078             :     int nOvrJpegQuality;
    3079           8 :     std::string osNoData;
    3080           4 :     uint16_t *panExtraSampleValues = nullptr;
    3081           4 :     uint16_t nExtraSamples = 0;
    3082           4 :     if (!GetOverviewParameters(nCompression, nPlanarConfig, nPredictor,
    3083             :                                nPhotometric, nOvrJpegQuality, osNoData,
    3084             :                                panExtraSampleValues, nExtraSamples,
    3085             :                                papszOptions))
    3086             :     {
    3087           0 :         return CE_Failure;
    3088             :     }
    3089             : 
    3090             :     /* -------------------------------------------------------------------- */
    3091             :     /*      Do we have a palette?  If so, create a TIFF compatible version. */
    3092             :     /* -------------------------------------------------------------------- */
    3093           8 :     std::vector<unsigned short> anTRed;
    3094           8 :     std::vector<unsigned short> anTGreen;
    3095           4 :     std::vector<unsigned short> anTBlue;
    3096           4 :     unsigned short *panRed = nullptr;
    3097           4 :     unsigned short *panGreen = nullptr;
    3098           4 :     unsigned short *panBlue = nullptr;
    3099             : 
    3100           4 :     if (nPhotometric == PHOTOMETRIC_PALETTE && m_poColorTable != nullptr)
    3101             :     {
    3102           0 :         if (m_nColorTableMultiplier == 0)
    3103           0 :             m_nColorTableMultiplier = DEFAULT_COLOR_TABLE_MULTIPLIER_257;
    3104             : 
    3105           0 :         CreateTIFFColorTable(m_poColorTable.get(), nOvBitsPerSample,
    3106             :                              m_nColorTableMultiplier, anTRed, anTGreen, anTBlue,
    3107             :                              panRed, panGreen, panBlue);
    3108             :     }
    3109             : 
    3110             :     /* -------------------------------------------------------------------- */
    3111             :     /*      Establish which of the overview levels we already have, and     */
    3112             :     /*      which are new.  We assume that band 1 of the file is            */
    3113             :     /*      representative.                                                 */
    3114             :     /* -------------------------------------------------------------------- */
    3115           4 :     int nOvrBlockXSize = 0;
    3116           4 :     int nOvrBlockYSize = 0;
    3117           4 :     GTIFFGetOverviewBlockSize(GDALRasterBand::ToHandle(GetRasterBand(1)),
    3118             :                               &nOvrBlockXSize, &nOvrBlockYSize, papszOptions,
    3119             :                               "BLOCKSIZE");
    3120             : 
    3121           4 :     CPLErr eErr = CE_None;
    3122           8 :     for (const auto *poSrcOvrDS : apoSrcOvrDS)
    3123             :     {
    3124           4 :         bool bFound = false;
    3125           4 :         for (auto &poOvrDS : m_apoOverviewDS)
    3126             :         {
    3127           4 :             if (poOvrDS->GetRasterXSize() == poSrcOvrDS->GetRasterXSize() &&
    3128           2 :                 poOvrDS->GetRasterYSize() == poSrcOvrDS->GetRasterYSize())
    3129             :             {
    3130           2 :                 bFound = true;
    3131           2 :                 break;
    3132             :             }
    3133             :         }
    3134           4 :         if (!bFound && eErr == CE_None)
    3135             :         {
    3136           2 :             if (m_bLayoutIFDSBeforeData && !m_bKnownIncompatibleEdition &&
    3137           0 :                 !m_bWriteKnownIncompatibleEdition)
    3138             :             {
    3139           0 :                 ReportError(CE_Warning, CPLE_AppDefined,
    3140             :                             "Adding new overviews invalidates the "
    3141             :                             "LAYOUT=IFDS_BEFORE_DATA property");
    3142           0 :                 m_bKnownIncompatibleEdition = true;
    3143           0 :                 m_bWriteKnownIncompatibleEdition = true;
    3144             :             }
    3145             : 
    3146           6 :             const toff_t nOverviewOffset = GTIFFWriteDirectory(
    3147             :                 m_hTIFF, FILETYPE_REDUCEDIMAGE, poSrcOvrDS->GetRasterXSize(),
    3148             :                 poSrcOvrDS->GetRasterYSize(), nOvBitsPerSample, nPlanarConfig,
    3149           2 :                 m_nSamplesPerPixel, nOvrBlockXSize, nOvrBlockYSize, TRUE,
    3150           2 :                 nCompression, nPhotometric, m_nSampleFormat, nPredictor, panRed,
    3151             :                 panGreen, panBlue, nExtraSamples, panExtraSampleValues,
    3152             :                 osMetadata,
    3153           2 :                 nOvrJpegQuality >= 0 ? CPLSPrintf("%d", nOvrJpegQuality)
    3154             :                                      : nullptr,
    3155           2 :                 CPLSPrintf("%d", m_nJpegTablesMode),
    3156           0 :                 osNoData.empty() ? nullptr : osNoData.c_str(),
    3157           2 :                 m_anLercAddCompressionAndVersion, false);
    3158             : 
    3159           2 :             if (nOverviewOffset == 0)
    3160           0 :                 eErr = CE_Failure;
    3161             :             else
    3162           2 :                 eErr = RegisterNewOverviewDataset(
    3163             :                     nOverviewOffset, nOvrJpegQuality, papszOptions);
    3164             :         }
    3165             :     }
    3166             : 
    3167           4 :     CPLFree(panExtraSampleValues);
    3168           4 :     panExtraSampleValues = nullptr;
    3169             : 
    3170           4 :     ReloadDirectory();
    3171             : 
    3172           4 :     if (!pfnProgress)
    3173           2 :         pfnProgress = GDALDummyProgress;
    3174             : 
    3175             :     // almost 0, but not 0 to please Coverity Scan
    3176           4 :     double dfTotalPixels = std::numeric_limits<double>::min();
    3177           8 :     for (const auto *poSrcOvrDS : apoSrcOvrDS)
    3178             :     {
    3179           4 :         dfTotalPixels += static_cast<double>(poSrcOvrDS->GetRasterXSize()) *
    3180           4 :                          poSrcOvrDS->GetRasterYSize();
    3181             :     }
    3182             : 
    3183             :     // Copy source datasets into target overview datasets
    3184           4 :     double dfCurPixels = 0;
    3185           8 :     for (auto *poSrcOvrDS : apoSrcOvrDS)
    3186             :     {
    3187           4 :         GDALDataset *poDstOvrDS = nullptr;
    3188           4 :         for (auto &poOvrDS : m_apoOverviewDS)
    3189             :         {
    3190           8 :             if (poOvrDS->GetRasterXSize() == poSrcOvrDS->GetRasterXSize() &&
    3191           4 :                 poOvrDS->GetRasterYSize() == poSrcOvrDS->GetRasterYSize())
    3192             :             {
    3193           4 :                 poDstOvrDS = poOvrDS.get();
    3194           4 :                 break;
    3195             :             }
    3196             :         }
    3197           4 :         if (eErr == CE_None && poDstOvrDS)
    3198             :         {
    3199             :             const double dfThisPixels =
    3200           4 :                 static_cast<double>(poSrcOvrDS->GetRasterXSize()) *
    3201           4 :                 poSrcOvrDS->GetRasterYSize();
    3202           8 :             void *pScaledProgressData = GDALCreateScaledProgress(
    3203             :                 dfCurPixels / dfTotalPixels,
    3204           4 :                 (dfCurPixels + dfThisPixels) / dfTotalPixels, pfnProgress,
    3205             :                 pProgressData);
    3206           4 :             dfCurPixels += dfThisPixels;
    3207           4 :             eErr = GDALDatasetCopyWholeRaster(GDALDataset::ToHandle(poSrcOvrDS),
    3208             :                                               GDALDataset::ToHandle(poDstOvrDS),
    3209             :                                               nullptr, GDALScaledProgress,
    3210             :                                               pScaledProgressData);
    3211           4 :             GDALDestroyScaledProgress(pScaledProgressData);
    3212             :         }
    3213             :     }
    3214             : 
    3215           4 :     return eErr;
    3216             : }
    3217             : 
    3218             : /************************************************************************/
    3219             : /*                          IBuildOverviews()                           */
    3220             : /************************************************************************/
    3221             : 
    3222         410 : CPLErr GTiffDataset::IBuildOverviews(const char *pszResampling, int nOverviews,
    3223             :                                      const int *panOverviewList, int nBandsIn,
    3224             :                                      const int *panBandList,
    3225             :                                      GDALProgressFunc pfnProgress,
    3226             :                                      void *pProgressData,
    3227             :                                      CSLConstList papszOptions)
    3228             : 
    3229             : {
    3230         410 :     ScanDirectories();
    3231             : 
    3232             :     // Make implicit JPEG overviews invisible, but do not destroy
    3233             :     // them in case they are already used (not sure that the client
    3234             :     // has the right to do that.  Behavior maybe undefined in GDAL API.
    3235         410 :     std::swap(m_apoJPEGOverviewDSOld, m_apoJPEGOverviewDS);
    3236         410 :     m_apoJPEGOverviewDS.clear();
    3237             : 
    3238             :     /* -------------------------------------------------------------------- */
    3239             :     /*      If RRD or external OVR overviews requested, then invoke         */
    3240             :     /*      generic handling.                                               */
    3241             :     /* -------------------------------------------------------------------- */
    3242         410 :     bool bUseGenericHandling = false;
    3243         410 :     bool bUseRRD = false;
    3244         820 :     CPLStringList aosOptions(papszOptions);
    3245             : 
    3246         410 :     const char *pszLocation = CSLFetchNameValue(papszOptions, "LOCATION");
    3247         410 :     if (pszLocation && EQUAL(pszLocation, "EXTERNAL"))
    3248             :     {
    3249           1 :         bUseGenericHandling = true;
    3250             :     }
    3251         409 :     else if (pszLocation && EQUAL(pszLocation, "INTERNAL"))
    3252             :     {
    3253           0 :         if (GetAccess() != GA_Update)
    3254             :         {
    3255           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    3256             :                      "Cannot create internal overviews on file opened in "
    3257             :                      "read-only mode");
    3258           0 :             return CE_Failure;
    3259             :         }
    3260             :     }
    3261         409 :     else if (pszLocation && EQUAL(pszLocation, "RRD"))
    3262             :     {
    3263           3 :         bUseGenericHandling = true;
    3264           3 :         bUseRRD = true;
    3265           3 :         aosOptions.SetNameValue("USE_RRD", "YES");
    3266             :     }
    3267             :     // Legacy
    3268         406 :     else if ((bUseRRD = CPLTestBool(
    3269             :                   CSLFetchNameValueDef(papszOptions, "USE_RRD",
    3270         812 :                                        CPLGetConfigOption("USE_RRD", "NO")))) ||
    3271         406 :              CPLTestBool(CSLFetchNameValueDef(
    3272             :                  papszOptions, "TIFF_USE_OVR",
    3273             :                  CPLGetConfigOption("TIFF_USE_OVR", "NO"))))
    3274             :     {
    3275           0 :         bUseGenericHandling = true;
    3276             :     }
    3277             : 
    3278             :     /* -------------------------------------------------------------------- */
    3279             :     /*      If we don't have read access, then create the overviews         */
    3280             :     /*      externally.                                                     */
    3281             :     /* -------------------------------------------------------------------- */
    3282         410 :     if (GetAccess() != GA_Update)
    3283             :     {
    3284         143 :         CPLDebug("GTiff", "File open for read-only accessing, "
    3285             :                           "creating overviews externally.");
    3286             : 
    3287         143 :         bUseGenericHandling = true;
    3288             :     }
    3289             : 
    3290         410 :     if (bUseGenericHandling)
    3291             :     {
    3292         146 :         if (!m_apoOverviewDS.empty())
    3293             :         {
    3294           0 :             ReportError(CE_Failure, CPLE_NotSupported,
    3295             :                         "Cannot add external overviews when there are already "
    3296             :                         "internal overviews");
    3297           0 :             return CE_Failure;
    3298             :         }
    3299             : 
    3300         146 :         if (!m_bWriteEmptyTiles && !bUseRRD)
    3301             :         {
    3302           1 :             aosOptions.SetNameValue("SPARSE_OK", "YES");
    3303             :         }
    3304             : 
    3305         146 :         CPLErr eErr = GDALDataset::IBuildOverviews(
    3306             :             pszResampling, nOverviews, panOverviewList, nBandsIn, panBandList,
    3307         146 :             pfnProgress, pProgressData, aosOptions);
    3308         146 :         if (eErr == CE_None && m_poMaskDS)
    3309             :         {
    3310           1 :             ReportError(
    3311             :                 CE_Warning, CPLE_NotSupported,
    3312             :                 "Building external overviews whereas there is an internal "
    3313             :                 "mask is not fully supported. "
    3314             :                 "The overviews of the non-mask bands will be created, "
    3315             :                 "but not the overviews of the mask band.");
    3316             :         }
    3317         146 :         return eErr;
    3318             :     }
    3319             : 
    3320             :     /* -------------------------------------------------------------------- */
    3321             :     /*      Our TIFF overview support currently only works safely if all    */
    3322             :     /*      bands are handled at the same time.                             */
    3323             :     /* -------------------------------------------------------------------- */
    3324         264 :     if (nBandsIn != GetRasterCount())
    3325             :     {
    3326           0 :         ReportError(CE_Failure, CPLE_NotSupported,
    3327             :                     "Generation of overviews in TIFF currently only "
    3328             :                     "supported when operating on all bands.  "
    3329             :                     "Operation failed.");
    3330           0 :         return CE_Failure;
    3331             :     }
    3332             : 
    3333             :     /* -------------------------------------------------------------------- */
    3334             :     /*      If zero overviews were requested, we need to clear all          */
    3335             :     /*      existing overviews.                                             */
    3336             :     /* -------------------------------------------------------------------- */
    3337         264 :     if (nOverviews == 0)
    3338             :     {
    3339           8 :         if (m_apoOverviewDS.empty())
    3340           3 :             return GDALDataset::IBuildOverviews(
    3341             :                 pszResampling, nOverviews, panOverviewList, nBandsIn,
    3342           3 :                 panBandList, pfnProgress, pProgressData, papszOptions);
    3343             : 
    3344           5 :         return CleanOverviews();
    3345             :     }
    3346             : 
    3347         256 :     CPLErr eErr = CE_None;
    3348             : 
    3349             :     /* -------------------------------------------------------------------- */
    3350             :     /*      Initialize progress counter.                                    */
    3351             :     /* -------------------------------------------------------------------- */
    3352         256 :     if (!pfnProgress(0.0, nullptr, pProgressData))
    3353             :     {
    3354           0 :         ReportError(CE_Failure, CPLE_UserInterrupt, "User terminated");
    3355           0 :         return CE_Failure;
    3356             :     }
    3357             : 
    3358         256 :     FlushDirectory();
    3359             : 
    3360             :     /* -------------------------------------------------------------------- */
    3361             :     /*      If we are averaging bit data to grayscale we need to create     */
    3362             :     /*      8bit overviews.                                                 */
    3363             :     /* -------------------------------------------------------------------- */
    3364         256 :     int nOvBitsPerSample = m_nBitsPerSample;
    3365             : 
    3366         256 :     if (STARTS_WITH_CI(pszResampling, "AVERAGE_BIT2"))
    3367           2 :         nOvBitsPerSample = 8;
    3368             : 
    3369             :     /* -------------------------------------------------------------------- */
    3370             :     /*      Do we need some metadata for the overviews?                     */
    3371             :     /* -------------------------------------------------------------------- */
    3372         512 :     CPLString osMetadata;
    3373             : 
    3374         256 :     const bool bIsForMaskBand = nBands == 1 && GetRasterBand(1)->IsMaskBand();
    3375         256 :     GTIFFBuildOverviewMetadata(pszResampling, this, bIsForMaskBand, osMetadata);
    3376             : 
    3377             :     int nCompression;
    3378             :     uint16_t nPlanarConfig;
    3379             :     uint16_t nPredictor;
    3380             :     uint16_t nPhotometric;
    3381             :     int nOvrJpegQuality;
    3382         512 :     std::string osNoData;
    3383         256 :     uint16_t *panExtraSampleValues = nullptr;
    3384         256 :     uint16_t nExtraSamples = 0;
    3385         256 :     if (!GetOverviewParameters(nCompression, nPlanarConfig, nPredictor,
    3386             :                                nPhotometric, nOvrJpegQuality, osNoData,
    3387             :                                panExtraSampleValues, nExtraSamples,
    3388             :                                papszOptions))
    3389             :     {
    3390           0 :         return CE_Failure;
    3391             :     }
    3392             : 
    3393             :     /* -------------------------------------------------------------------- */
    3394             :     /*      Do we have a palette?  If so, create a TIFF compatible version. */
    3395             :     /* -------------------------------------------------------------------- */
    3396         512 :     std::vector<unsigned short> anTRed;
    3397         512 :     std::vector<unsigned short> anTGreen;
    3398         512 :     std::vector<unsigned short> anTBlue;
    3399         256 :     unsigned short *panRed = nullptr;
    3400         256 :     unsigned short *panGreen = nullptr;
    3401         256 :     unsigned short *panBlue = nullptr;
    3402             : 
    3403         256 :     if (nPhotometric == PHOTOMETRIC_PALETTE && m_poColorTable != nullptr)
    3404             :     {
    3405          12 :         if (m_nColorTableMultiplier == 0)
    3406           0 :             m_nColorTableMultiplier = DEFAULT_COLOR_TABLE_MULTIPLIER_257;
    3407             : 
    3408          12 :         CreateTIFFColorTable(m_poColorTable.get(), nOvBitsPerSample,
    3409             :                              m_nColorTableMultiplier, anTRed, anTGreen, anTBlue,
    3410             :                              panRed, panGreen, panBlue);
    3411             :     }
    3412             : 
    3413             :     /* -------------------------------------------------------------------- */
    3414             :     /*      Establish which of the overview levels we already have, and     */
    3415             :     /*      which are new.  We assume that band 1 of the file is            */
    3416             :     /*      representative.                                                 */
    3417             :     /* -------------------------------------------------------------------- */
    3418         256 :     int nOvrBlockXSize = 0;
    3419         256 :     int nOvrBlockYSize = 0;
    3420         256 :     GTIFFGetOverviewBlockSize(GDALRasterBand::ToHandle(GetRasterBand(1)),
    3421             :                               &nOvrBlockXSize, &nOvrBlockYSize, papszOptions,
    3422             :                               "BLOCKSIZE");
    3423         512 :     std::vector<bool> abRequireNewOverview(nOverviews, true);
    3424         696 :     for (int i = 0; i < nOverviews && eErr == CE_None; ++i)
    3425             :     {
    3426         773 :         for (auto &poODS : m_apoOverviewDS)
    3427             :         {
    3428             :             const int nOvFactor =
    3429         778 :                 GDALComputeOvFactor(poODS->GetRasterXSize(), GetRasterXSize(),
    3430         389 :                                     poODS->GetRasterYSize(), GetRasterYSize());
    3431             : 
    3432             :             // If we already have a 1x1 overview and this new one would result
    3433             :             // in it too, then don't create it.
    3434         449 :             if (poODS->GetRasterXSize() == 1 && poODS->GetRasterYSize() == 1 &&
    3435         449 :                 DIV_ROUND_UP(GetRasterXSize(), panOverviewList[i]) == 1 &&
    3436          21 :                 DIV_ROUND_UP(GetRasterYSize(), panOverviewList[i]) == 1)
    3437             :             {
    3438          21 :                 abRequireNewOverview[i] = false;
    3439          21 :                 break;
    3440             :             }
    3441             : 
    3442         701 :             if (nOvFactor == panOverviewList[i] ||
    3443         333 :                 nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
    3444             :                                                 GetRasterXSize(),
    3445             :                                                 GetRasterYSize()))
    3446             :             {
    3447          35 :                 abRequireNewOverview[i] = false;
    3448          35 :                 break;
    3449             :             }
    3450             :         }
    3451             : 
    3452         440 :         if (abRequireNewOverview[i])
    3453             :         {
    3454         384 :             if (m_bLayoutIFDSBeforeData && !m_bKnownIncompatibleEdition &&
    3455           2 :                 !m_bWriteKnownIncompatibleEdition)
    3456             :             {
    3457           2 :                 ReportError(CE_Warning, CPLE_AppDefined,
    3458             :                             "Adding new overviews invalidates the "
    3459             :                             "LAYOUT=IFDS_BEFORE_DATA property");
    3460           2 :                 m_bKnownIncompatibleEdition = true;
    3461           2 :                 m_bWriteKnownIncompatibleEdition = true;
    3462             :             }
    3463             : 
    3464             :             const int nOXSize =
    3465         384 :                 DIV_ROUND_UP(GetRasterXSize(), panOverviewList[i]);
    3466             :             const int nOYSize =
    3467         384 :                 DIV_ROUND_UP(GetRasterYSize(), panOverviewList[i]);
    3468             : 
    3469         768 :             const toff_t nOverviewOffset = GTIFFWriteDirectory(
    3470             :                 m_hTIFF, FILETYPE_REDUCEDIMAGE, nOXSize, nOYSize,
    3471         384 :                 nOvBitsPerSample, nPlanarConfig, m_nSamplesPerPixel,
    3472             :                 nOvrBlockXSize, nOvrBlockYSize, TRUE, nCompression,
    3473         384 :                 nPhotometric, m_nSampleFormat, nPredictor, panRed, panGreen,
    3474             :                 panBlue, nExtraSamples, panExtraSampleValues, osMetadata,
    3475         384 :                 nOvrJpegQuality >= 0 ? CPLSPrintf("%d", nOvrJpegQuality)
    3476             :                                      : nullptr,
    3477         384 :                 CPLSPrintf("%d", m_nJpegTablesMode),
    3478          25 :                 osNoData.empty() ? nullptr : osNoData.c_str(),
    3479         384 :                 m_anLercAddCompressionAndVersion, false);
    3480             : 
    3481         384 :             if (nOverviewOffset == 0)
    3482           0 :                 eErr = CE_Failure;
    3483             :             else
    3484         384 :                 eErr = RegisterNewOverviewDataset(
    3485             :                     nOverviewOffset, nOvrJpegQuality, papszOptions);
    3486             :         }
    3487             :     }
    3488             : 
    3489         256 :     CPLFree(panExtraSampleValues);
    3490         256 :     panExtraSampleValues = nullptr;
    3491             : 
    3492         256 :     ReloadDirectory();
    3493             : 
    3494             :     /* -------------------------------------------------------------------- */
    3495             :     /*      Create overviews for the mask.                                  */
    3496             :     /* -------------------------------------------------------------------- */
    3497         256 :     if (eErr != CE_None)
    3498           0 :         return eErr;
    3499             : 
    3500         256 :     eErr = CreateInternalMaskOverviews(nOvrBlockXSize, nOvrBlockYSize);
    3501             : 
    3502             :     /* -------------------------------------------------------------------- */
    3503             :     /*      Refresh overviews for the mask                                  */
    3504             :     /* -------------------------------------------------------------------- */
    3505             :     const bool bHasInternalMask =
    3506         256 :         m_poMaskDS != nullptr && m_poMaskDS->GetRasterCount() == 1;
    3507             :     const bool bHasExternalMask =
    3508         256 :         !bHasInternalMask && oOvManager.HaveMaskFile();
    3509         256 :     const bool bHasMask = bHasInternalMask || bHasExternalMask;
    3510             : 
    3511         256 :     if (bHasInternalMask)
    3512             :     {
    3513          48 :         std::vector<GDALRasterBandH> ahOverviewBands;
    3514          64 :         for (auto &poOvrDS : m_apoOverviewDS)
    3515             :         {
    3516          40 :             if (poOvrDS->m_poMaskDS != nullptr)
    3517             :             {
    3518          40 :                 ahOverviewBands.push_back(GDALRasterBand::ToHandle(
    3519          40 :                     poOvrDS->m_poMaskDS->GetRasterBand(1)));
    3520             :             }
    3521             :         }
    3522             : 
    3523          48 :         void *pScaledProgressData = GDALCreateScaledProgress(
    3524          24 :             0, 1.0 / (nBands + 1), pfnProgress, pProgressData);
    3525          24 :         eErr = GDALRegenerateOverviewsEx(
    3526          24 :             m_poMaskDS->GetRasterBand(1),
    3527          24 :             static_cast<int>(ahOverviewBands.size()), ahOverviewBands.data(),
    3528             :             pszResampling, GDALScaledProgress, pScaledProgressData,
    3529             :             papszOptions);
    3530          24 :         GDALDestroyScaledProgress(pScaledProgressData);
    3531             :     }
    3532         232 :     else if (bHasExternalMask)
    3533             :     {
    3534           4 :         void *pScaledProgressData = GDALCreateScaledProgress(
    3535           2 :             0, 1.0 / (nBands + 1), pfnProgress, pProgressData);
    3536           2 :         eErr = oOvManager.BuildOverviewsMask(
    3537             :             pszResampling, nOverviews, panOverviewList, GDALScaledProgress,
    3538             :             pScaledProgressData, papszOptions);
    3539           2 :         GDALDestroyScaledProgress(pScaledProgressData);
    3540             :     }
    3541             : 
    3542             :     // If we have an alpha band, we want it to be generated before downsampling
    3543             :     // other bands
    3544         256 :     bool bHasAlphaBand = false;
    3545       66259 :     for (int iBand = 0; iBand < nBands; iBand++)
    3546             :     {
    3547       66003 :         if (papoBands[iBand]->GetColorInterpretation() == GCI_AlphaBand)
    3548          18 :             bHasAlphaBand = true;
    3549             :     }
    3550             : 
    3551             :     /* -------------------------------------------------------------------- */
    3552             :     /*      Refresh old overviews that were listed.                         */
    3553             :     /* -------------------------------------------------------------------- */
    3554         256 :     const auto poColorTable = GetRasterBand(panBandList[0])->GetColorTable();
    3555          21 :     if ((m_nPlanarConfig == PLANARCONFIG_CONTIG || bHasAlphaBand) &&
    3556         237 :         GDALDataTypeIsComplex(
    3557         237 :             GetRasterBand(panBandList[0])->GetRasterDataType()) == FALSE &&
    3558          12 :         (poColorTable == nullptr || STARTS_WITH_CI(pszResampling, "NEAR") ||
    3559         513 :          poColorTable->IsIdentity()) &&
    3560         229 :         (STARTS_WITH_CI(pszResampling, "NEAR") ||
    3561         118 :          EQUAL(pszResampling, "AVERAGE") || EQUAL(pszResampling, "RMS") ||
    3562          48 :          EQUAL(pszResampling, "GAUSS") || EQUAL(pszResampling, "CUBIC") ||
    3563          30 :          EQUAL(pszResampling, "CUBICSPLINE") ||
    3564          29 :          EQUAL(pszResampling, "LANCZOS") || EQUAL(pszResampling, "BILINEAR") ||
    3565          25 :          EQUAL(pszResampling, "MODE")))
    3566             :     {
    3567             :         // In the case of pixel interleaved compressed overviews, we want to
    3568             :         // generate the overviews for all the bands block by block, and not
    3569             :         // band after band, in order to write the block once and not loose
    3570             :         // space in the TIFF file.  We also use that logic for uncompressed
    3571             :         // overviews, since GDALRegenerateOverviewsMultiBand() will be able to
    3572             :         // trigger cascading overview regeneration even in the presence
    3573             :         // of an alpha band.
    3574             : 
    3575         207 :         int nNewOverviews = 0;
    3576             : 
    3577             :         GDALRasterBand ***papapoOverviewBands = static_cast<GDALRasterBand ***>(
    3578         207 :             CPLCalloc(sizeof(void *), nBandsIn));
    3579             :         GDALRasterBand **papoBandList =
    3580         207 :             static_cast<GDALRasterBand **>(CPLCalloc(sizeof(void *), nBandsIn));
    3581       66113 :         for (int iBand = 0; iBand < nBandsIn; ++iBand)
    3582             :         {
    3583       65906 :             GDALRasterBand *poBand = GetRasterBand(panBandList[iBand]);
    3584             : 
    3585       65906 :             papoBandList[iBand] = poBand;
    3586      131812 :             papapoOverviewBands[iBand] = static_cast<GDALRasterBand **>(
    3587       65906 :                 CPLCalloc(sizeof(void *), poBand->GetOverviewCount()));
    3588             : 
    3589       65906 :             int iCurOverview = 0;
    3590             :             std::vector<bool> abAlreadyUsedOverviewBand(
    3591       65906 :                 poBand->GetOverviewCount(), false);
    3592             : 
    3593      132098 :             for (int i = 0; i < nOverviews; ++i)
    3594             :             {
    3595       66652 :                 for (int j = 0; j < poBand->GetOverviewCount(); ++j)
    3596             :                 {
    3597       66637 :                     if (abAlreadyUsedOverviewBand[j])
    3598         459 :                         continue;
    3599             : 
    3600             :                     int nOvFactor;
    3601       66178 :                     GDALRasterBand *poOverview = poBand->GetOverview(j);
    3602             : 
    3603       66178 :                     nOvFactor = GDALComputeOvFactor(
    3604             :                         poOverview->GetXSize(), poBand->GetXSize(),
    3605             :                         poOverview->GetYSize(), poBand->GetYSize());
    3606             : 
    3607       66178 :                     GDALCopyNoDataValue(poOverview, poBand);
    3608             : 
    3609       66179 :                     if (nOvFactor == panOverviewList[i] ||
    3610           1 :                         nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
    3611             :                                                         poBand->GetXSize(),
    3612             :                                                         poBand->GetYSize()))
    3613             :                     {
    3614       66177 :                         if (iBand == 0)
    3615             :                         {
    3616             :                             const auto osNewResampling =
    3617         668 :                                 GDALGetNormalizedOvrResampling(pszResampling);
    3618             :                             const char *pszExistingResampling =
    3619         334 :                                 poOverview->GetMetadataItem("RESAMPLING");
    3620         668 :                             if (pszExistingResampling &&
    3621         334 :                                 pszExistingResampling != osNewResampling)
    3622             :                             {
    3623           2 :                                 poOverview->SetMetadataItem(
    3624           2 :                                     "RESAMPLING", osNewResampling.c_str());
    3625             :                             }
    3626             :                         }
    3627             : 
    3628       66177 :                         abAlreadyUsedOverviewBand[j] = true;
    3629       66177 :                         CPLAssert(iCurOverview < poBand->GetOverviewCount());
    3630       66177 :                         papapoOverviewBands[iBand][iCurOverview] = poOverview;
    3631       66177 :                         ++iCurOverview;
    3632       66177 :                         break;
    3633             :                     }
    3634             :                 }
    3635             :             }
    3636             : 
    3637       65906 :             if (nNewOverviews == 0)
    3638             :             {
    3639         207 :                 nNewOverviews = iCurOverview;
    3640             :             }
    3641       65699 :             else if (nNewOverviews != iCurOverview)
    3642             :             {
    3643           0 :                 CPLAssert(false);
    3644             :                 return CE_Failure;
    3645             :             }
    3646             :         }
    3647             : 
    3648             :         void *pScaledProgressData =
    3649         207 :             bHasMask ? GDALCreateScaledProgress(1.0 / (nBands + 1), 1.0,
    3650             :                                                 pfnProgress, pProgressData)
    3651         181 :                      : GDALCreateScaledProgress(0.0, 1.0, pfnProgress,
    3652         207 :                                                 pProgressData);
    3653         207 :         GDALRegenerateOverviewsMultiBand(nBandsIn, papoBandList, nNewOverviews,
    3654             :                                          papapoOverviewBands, pszResampling,
    3655             :                                          GDALScaledProgress,
    3656             :                                          pScaledProgressData, papszOptions);
    3657         207 :         GDALDestroyScaledProgress(pScaledProgressData);
    3658             : 
    3659       66113 :         for (int iBand = 0; iBand < nBandsIn; ++iBand)
    3660             :         {
    3661       65906 :             CPLFree(papapoOverviewBands[iBand]);
    3662             :         }
    3663         207 :         CPLFree(papapoOverviewBands);
    3664         207 :         CPLFree(papoBandList);
    3665             :     }
    3666             :     else
    3667             :     {
    3668             :         GDALRasterBand **papoOverviewBands = static_cast<GDALRasterBand **>(
    3669          49 :             CPLCalloc(sizeof(void *), nOverviews));
    3670             : 
    3671          49 :         const int iBandOffset = bHasMask ? 1 : 0;
    3672             : 
    3673         146 :         for (int iBand = 0; iBand < nBandsIn && eErr == CE_None; ++iBand)
    3674             :         {
    3675          97 :             GDALRasterBand *poBand = GetRasterBand(panBandList[iBand]);
    3676          97 :             if (poBand == nullptr)
    3677             :             {
    3678           0 :                 eErr = CE_Failure;
    3679           0 :                 break;
    3680             :             }
    3681             : 
    3682             :             std::vector<bool> abAlreadyUsedOverviewBand(
    3683         194 :                 poBand->GetOverviewCount(), false);
    3684             : 
    3685          97 :             int nNewOverviews = 0;
    3686         290 :             for (int i = 0; i < nOverviews; ++i)
    3687             :             {
    3688         451 :                 for (int j = 0; j < poBand->GetOverviewCount(); ++j)
    3689             :                 {
    3690         433 :                     if (abAlreadyUsedOverviewBand[j])
    3691         257 :                         continue;
    3692             : 
    3693         176 :                     GDALRasterBand *poOverview = poBand->GetOverview(j);
    3694             : 
    3695         176 :                     GDALCopyNoDataValue(poOverview, poBand);
    3696             : 
    3697         176 :                     const int nOvFactor = GDALComputeOvFactor(
    3698             :                         poOverview->GetXSize(), poBand->GetXSize(),
    3699             :                         poOverview->GetYSize(), poBand->GetYSize());
    3700             : 
    3701         177 :                     if (nOvFactor == panOverviewList[i] ||
    3702           1 :                         nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
    3703             :                                                         poBand->GetXSize(),
    3704             :                                                         poBand->GetYSize()))
    3705             :                     {
    3706         175 :                         if (iBand == 0)
    3707             :                         {
    3708             :                             const auto osNewResampling =
    3709         170 :                                 GDALGetNormalizedOvrResampling(pszResampling);
    3710             :                             const char *pszExistingResampling =
    3711          85 :                                 poOverview->GetMetadataItem("RESAMPLING");
    3712         137 :                             if (pszExistingResampling &&
    3713          52 :                                 pszExistingResampling != osNewResampling)
    3714             :                             {
    3715           1 :                                 poOverview->SetMetadataItem(
    3716           1 :                                     "RESAMPLING", osNewResampling.c_str());
    3717             :                             }
    3718             :                         }
    3719             : 
    3720         175 :                         abAlreadyUsedOverviewBand[j] = true;
    3721         175 :                         CPLAssert(nNewOverviews < poBand->GetOverviewCount());
    3722         175 :                         papoOverviewBands[nNewOverviews++] = poOverview;
    3723         175 :                         break;
    3724             :                     }
    3725             :                 }
    3726             :             }
    3727             : 
    3728         194 :             void *pScaledProgressData = GDALCreateScaledProgress(
    3729          97 :                 (iBand + iBandOffset) /
    3730          97 :                     static_cast<double>(nBandsIn + iBandOffset),
    3731          97 :                 (iBand + iBandOffset + 1) /
    3732          97 :                     static_cast<double>(nBandsIn + iBandOffset),
    3733             :                 pfnProgress, pProgressData);
    3734             : 
    3735          97 :             eErr = GDALRegenerateOverviewsEx(
    3736             :                 poBand, nNewOverviews,
    3737             :                 reinterpret_cast<GDALRasterBandH *>(papoOverviewBands),
    3738             :                 pszResampling, GDALScaledProgress, pScaledProgressData,
    3739             :                 papszOptions);
    3740             : 
    3741          97 :             GDALDestroyScaledProgress(pScaledProgressData);
    3742             :         }
    3743             : 
    3744             :         /* --------------------------------------------------------------------
    3745             :          */
    3746             :         /*      Cleanup */
    3747             :         /* --------------------------------------------------------------------
    3748             :          */
    3749          49 :         CPLFree(papoOverviewBands);
    3750             :     }
    3751             : 
    3752         256 :     pfnProgress(1.0, nullptr, pProgressData);
    3753             : 
    3754         256 :     return eErr;
    3755             : }
    3756             : 
    3757             : /************************************************************************/
    3758             : /*                   GTiffWriteDummyGeokeyDirectory()                   */
    3759             : /************************************************************************/
    3760             : 
    3761        1521 : static void GTiffWriteDummyGeokeyDirectory(TIFF *hTIFF)
    3762             : {
    3763             :     // If we have existing geokeys, try to wipe them
    3764             :     // by writing a dummy geokey directory. (#2546)
    3765        1521 :     uint16_t *panVI = nullptr;
    3766        1521 :     uint16_t nKeyCount = 0;
    3767             : 
    3768        1521 :     if (TIFFGetField(hTIFF, TIFFTAG_GEOKEYDIRECTORY, &nKeyCount, &panVI))
    3769             :     {
    3770          25 :         GUInt16 anGKVersionInfo[4] = {1, 1, 0, 0};
    3771          25 :         double adfDummyDoubleParams[1] = {0.0};
    3772          25 :         TIFFSetField(hTIFF, TIFFTAG_GEOKEYDIRECTORY, 4, anGKVersionInfo);
    3773          25 :         TIFFSetField(hTIFF, TIFFTAG_GEODOUBLEPARAMS, 1, adfDummyDoubleParams);
    3774          25 :         TIFFSetField(hTIFF, TIFFTAG_GEOASCIIPARAMS, "");
    3775             :     }
    3776        1521 : }
    3777             : 
    3778             : /************************************************************************/
    3779             : /*                      IsSRSCompatibleOfGeoTIFF()                      */
    3780             : /************************************************************************/
    3781             : 
    3782        3195 : static bool IsSRSCompatibleOfGeoTIFF(const OGRSpatialReference *poSRS,
    3783             :                                      GTIFFKeysFlavorEnum eGeoTIFFKeysFlavor)
    3784             : {
    3785        3195 :     char *pszWKT = nullptr;
    3786        3195 :     if ((poSRS->IsGeographic() || poSRS->IsProjected()) && !poSRS->IsCompound())
    3787             :     {
    3788        3177 :         const char *pszAuthName = poSRS->GetAuthorityName();
    3789        3177 :         const char *pszAuthCode = poSRS->GetAuthorityCode();
    3790        3177 :         if (pszAuthName && pszAuthCode && EQUAL(pszAuthName, "EPSG"))
    3791        2607 :             return true;
    3792             :     }
    3793             :     OGRErr eErr;
    3794             :     {
    3795        1176 :         CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
    3796        1176 :         if (poSRS->IsDerivedGeographic() ||
    3797         588 :             (poSRS->IsProjected() && !poSRS->IsCompound() &&
    3798          70 :              poSRS->GetAxesCount() == 3))
    3799             :         {
    3800           0 :             eErr = OGRERR_FAILURE;
    3801             :         }
    3802             :         else
    3803             :         {
    3804             :             // Geographic3D CRS can't be exported to WKT1, but are
    3805             :             // valid GeoTIFF 1.1
    3806         588 :             const char *const apszOptions[] = {
    3807         588 :                 poSRS->IsGeographic() ? nullptr : "FORMAT=WKT1", nullptr};
    3808         588 :             eErr = poSRS->exportToWkt(&pszWKT, apszOptions);
    3809         588 :             if (eErr == OGRERR_FAILURE && poSRS->IsProjected() &&
    3810             :                 eGeoTIFFKeysFlavor == GEOTIFF_KEYS_ESRI_PE)
    3811             :             {
    3812           0 :                 CPLFree(pszWKT);
    3813           0 :                 const char *const apszOptionsESRIWKT[] = {"FORMAT=WKT1_ESRI",
    3814             :                                                           nullptr};
    3815           0 :                 eErr = poSRS->exportToWkt(&pszWKT, apszOptionsESRIWKT);
    3816             :             }
    3817             :         }
    3818             :     }
    3819         588 :     const bool bCompatibleOfGeoTIFF =
    3820        1175 :         (eErr == OGRERR_NONE && pszWKT != nullptr &&
    3821         587 :          strstr(pszWKT, "custom_proj4") == nullptr);
    3822         588 :     CPLFree(pszWKT);
    3823         588 :     return bCompatibleOfGeoTIFF;
    3824             : }
    3825             : 
    3826             : /************************************************************************/
    3827             : /*                          WriteGeoTIFFInfo()                          */
    3828             : /************************************************************************/
    3829             : 
    3830        5895 : void GTiffDataset::WriteGeoTIFFInfo()
    3831             : 
    3832             : {
    3833        5895 :     bool bPixelIsPoint = false;
    3834        5895 :     bool bPointGeoIgnore = false;
    3835             : 
    3836             :     const char *pszAreaOrPoint =
    3837        5895 :         GTiffDataset::GetMetadataItem(GDALMD_AREA_OR_POINT);
    3838        5895 :     if (pszAreaOrPoint && EQUAL(pszAreaOrPoint, GDALMD_AOP_POINT))
    3839             :     {
    3840          19 :         bPixelIsPoint = true;
    3841             :         bPointGeoIgnore =
    3842          19 :             CPLTestBool(CPLGetConfigOption("GTIFF_POINT_GEO_IGNORE", "FALSE"));
    3843             :     }
    3844             : 
    3845        5895 :     if (m_bForceUnsetGTOrGCPs)
    3846             :     {
    3847          11 :         m_bNeedsRewrite = true;
    3848          11 :         m_bForceUnsetGTOrGCPs = false;
    3849             : 
    3850          11 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOPIXELSCALE);
    3851          11 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOTIEPOINTS);
    3852          11 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOTRANSMATRIX);
    3853             :     }
    3854             : 
    3855        5895 :     if (m_bForceUnsetProjection)
    3856             :     {
    3857           8 :         m_bNeedsRewrite = true;
    3858           8 :         m_bForceUnsetProjection = false;
    3859             : 
    3860           8 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOKEYDIRECTORY);
    3861           8 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEODOUBLEPARAMS);
    3862           8 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOASCIIPARAMS);
    3863             :     }
    3864             : 
    3865             :     /* -------------------------------------------------------------------- */
    3866             :     /*      Write geotransform if valid.                                    */
    3867             :     /* -------------------------------------------------------------------- */
    3868        5895 :     if (m_bGeoTransformValid)
    3869             :     {
    3870        1833 :         m_bNeedsRewrite = true;
    3871             : 
    3872             :         /* --------------------------------------------------------------------
    3873             :          */
    3874             :         /*      Clear old tags to ensure we don't end up with conflicting */
    3875             :         /*      information. (#2625) */
    3876             :         /* --------------------------------------------------------------------
    3877             :          */
    3878        1833 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOPIXELSCALE);
    3879        1833 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOTIEPOINTS);
    3880        1833 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOTRANSMATRIX);
    3881             : 
    3882             :         /* --------------------------------------------------------------------
    3883             :          */
    3884             :         /*      Write the transform.  If we have a normal north-up image we */
    3885             :         /*      use the tiepoint plus pixelscale otherwise we use a matrix. */
    3886             :         /* --------------------------------------------------------------------
    3887             :          */
    3888        1833 :         if (m_gt.xrot == 0.0 && m_gt.yrot == 0.0 && m_gt.yscale < 0.0)
    3889             :         {
    3890        1740 :             double dfOffset = 0.0;
    3891        1740 :             if (m_eProfile != GTiffProfile::BASELINE)
    3892             :             {
    3893             :                 // In the case the SRS has a vertical component and we have
    3894             :                 // a single band, encode its scale/offset in the GeoTIFF tags
    3895        1734 :                 int bHasScale = FALSE;
    3896        1734 :                 double dfScale = GetRasterBand(1)->GetScale(&bHasScale);
    3897        1734 :                 int bHasOffset = FALSE;
    3898        1734 :                 dfOffset = GetRasterBand(1)->GetOffset(&bHasOffset);
    3899             :                 const bool bApplyScaleOffset =
    3900        1734 :                     m_oSRS.IsVertical() && GetRasterCount() == 1;
    3901        1734 :                 if (bApplyScaleOffset && !bHasScale)
    3902           0 :                     dfScale = 1.0;
    3903        1734 :                 if (!bApplyScaleOffset || !bHasOffset)
    3904        1731 :                     dfOffset = 0.0;
    3905        1734 :                 const double adfPixelScale[3] = {m_gt.xscale, fabs(m_gt.yscale),
    3906        1734 :                                                  bApplyScaleOffset ? dfScale
    3907        1734 :                                                                    : 0.0};
    3908        1734 :                 TIFFSetField(m_hTIFF, TIFFTAG_GEOPIXELSCALE, 3, adfPixelScale);
    3909             :             }
    3910             : 
    3911        1740 :             double adfTiePoints[6] = {0.0,        0.0,        0.0,
    3912        1740 :                                       m_gt.xorig, m_gt.yorig, dfOffset};
    3913             : 
    3914        1740 :             if (bPixelIsPoint && !bPointGeoIgnore)
    3915             :             {
    3916          15 :                 adfTiePoints[3] += m_gt.xscale * 0.5 + m_gt.xrot * 0.5;
    3917          15 :                 adfTiePoints[4] += m_gt.yrot * 0.5 + m_gt.yscale * 0.5;
    3918             :             }
    3919             : 
    3920        1740 :             if (m_eProfile != GTiffProfile::BASELINE)
    3921        1740 :                 TIFFSetField(m_hTIFF, TIFFTAG_GEOTIEPOINTS, 6, adfTiePoints);
    3922             :         }
    3923             :         else
    3924             :         {
    3925          93 :             double adfMatrix[16] = {};
    3926             : 
    3927          93 :             adfMatrix[0] = m_gt.xscale;
    3928          93 :             adfMatrix[1] = m_gt.xrot;
    3929          93 :             adfMatrix[3] = m_gt.xorig;
    3930          93 :             adfMatrix[4] = m_gt.yrot;
    3931          93 :             adfMatrix[5] = m_gt.yscale;
    3932          93 :             adfMatrix[7] = m_gt.yorig;
    3933          93 :             adfMatrix[15] = 1.0;
    3934             : 
    3935          93 :             if (bPixelIsPoint && !bPointGeoIgnore)
    3936             :             {
    3937           0 :                 adfMatrix[3] += m_gt.xscale * 0.5 + m_gt.xrot * 0.5;
    3938           0 :                 adfMatrix[7] += m_gt.yrot * 0.5 + m_gt.yscale * 0.5;
    3939             :             }
    3940             : 
    3941          93 :             if (m_eProfile != GTiffProfile::BASELINE)
    3942          93 :                 TIFFSetField(m_hTIFF, TIFFTAG_GEOTRANSMATRIX, 16, adfMatrix);
    3943             :         }
    3944             : 
    3945        1833 :         if (m_poBaseDS == nullptr)
    3946             :         {
    3947             :             // Do we need a world file?
    3948        1833 :             if (CPLFetchBool(m_papszCreationOptions, "TFW", false))
    3949           7 :                 GDALWriteWorldFile(m_osFilename.c_str(), "tfw", m_gt.data());
    3950        1826 :             else if (CPLFetchBool(m_papszCreationOptions, "WORLDFILE", false))
    3951           2 :                 GDALWriteWorldFile(m_osFilename.c_str(), "wld", m_gt.data());
    3952             :         }
    3953             :     }
    3954        4076 :     else if (GetGCPCount() > 0 && GetGCPCount() <= knMAX_GCP_COUNT &&
    3955          14 :              m_eProfile != GTiffProfile::BASELINE)
    3956             :     {
    3957          14 :         m_bNeedsRewrite = true;
    3958             : 
    3959             :         double *padfTiePoints = static_cast<double *>(
    3960          14 :             CPLMalloc(6 * sizeof(double) * GetGCPCount()));
    3961             : 
    3962          74 :         for (size_t iGCP = 0; iGCP < m_aoGCPs.size(); ++iGCP)
    3963             :         {
    3964             : 
    3965          60 :             padfTiePoints[iGCP * 6 + 0] = m_aoGCPs[iGCP].Pixel();
    3966          60 :             padfTiePoints[iGCP * 6 + 1] = m_aoGCPs[iGCP].Line();
    3967          60 :             padfTiePoints[iGCP * 6 + 2] = 0;
    3968          60 :             padfTiePoints[iGCP * 6 + 3] = m_aoGCPs[iGCP].X();
    3969          60 :             padfTiePoints[iGCP * 6 + 4] = m_aoGCPs[iGCP].Y();
    3970          60 :             padfTiePoints[iGCP * 6 + 5] = m_aoGCPs[iGCP].Z();
    3971             : 
    3972          60 :             if (bPixelIsPoint && !bPointGeoIgnore)
    3973             :             {
    3974           0 :                 padfTiePoints[iGCP * 6 + 0] += 0.5;
    3975           0 :                 padfTiePoints[iGCP * 6 + 1] += 0.5;
    3976             :             }
    3977             :         }
    3978             : 
    3979          14 :         TIFFSetField(m_hTIFF, TIFFTAG_GEOTIEPOINTS, 6 * GetGCPCount(),
    3980             :                      padfTiePoints);
    3981          14 :         CPLFree(padfTiePoints);
    3982             :     }
    3983             : 
    3984             :     /* -------------------------------------------------------------------- */
    3985             :     /*      Write out projection definition.                                */
    3986             :     /* -------------------------------------------------------------------- */
    3987        5895 :     const bool bHasProjection = !m_oSRS.IsEmpty();
    3988        5895 :     if ((bHasProjection || bPixelIsPoint) &&
    3989        1525 :         m_eProfile != GTiffProfile::BASELINE)
    3990             :     {
    3991        1521 :         m_bNeedsRewrite = true;
    3992             : 
    3993             :         // If we have existing geokeys, try to wipe them
    3994             :         // by writing a dummy geokey directory. (#2546)
    3995        1521 :         GTiffWriteDummyGeokeyDirectory(m_hTIFF);
    3996             : 
    3997        1521 :         GTIF *psGTIF = GTiffDataset::GTIFNew(m_hTIFF);
    3998             : 
    3999             :         // Set according to coordinate system.
    4000        1521 :         if (bHasProjection)
    4001             :         {
    4002        1520 :             if (IsSRSCompatibleOfGeoTIFF(&m_oSRS, m_eGeoTIFFKeysFlavor))
    4003             :             {
    4004        1518 :                 GTIFSetFromOGISDefnEx(psGTIF,
    4005             :                                       OGRSpatialReference::ToHandle(&m_oSRS),
    4006             :                                       m_eGeoTIFFKeysFlavor, m_eGeoTIFFVersion);
    4007             :             }
    4008             :             else
    4009             :             {
    4010           2 :                 GDALPamDataset::SetSpatialRef(&m_oSRS);
    4011             :             }
    4012             :         }
    4013             : 
    4014        1521 :         if (bPixelIsPoint)
    4015             :         {
    4016          19 :             GTIFKeySet(psGTIF, GTRasterTypeGeoKey, TYPE_SHORT, 1,
    4017             :                        RasterPixelIsPoint);
    4018             :         }
    4019             : 
    4020        1521 :         GTIFWriteKeys(psGTIF);
    4021        1521 :         GTIFFree(psGTIF);
    4022             :     }
    4023        5895 : }
    4024             : 
    4025             : /************************************************************************/
    4026             : /*                         AppendMetadataItem()                         */
    4027             : /************************************************************************/
    4028             : 
    4029        3893 : static void AppendMetadataItem(CPLXMLNode **ppsRoot, CPLXMLNode **ppsTail,
    4030             :                                const char *pszKey, const char *pszValue,
    4031             :                                CPLXMLNode *psValueNode, int nBand,
    4032             :                                const char *pszRole, const char *pszDomain)
    4033             : 
    4034             : {
    4035        3893 :     CPLAssert(pszValue || psValueNode);
    4036        3893 :     CPLAssert(!(pszValue && psValueNode));
    4037             : 
    4038             :     /* -------------------------------------------------------------------- */
    4039             :     /*      Create the Item element, and subcomponents.                     */
    4040             :     /* -------------------------------------------------------------------- */
    4041        3893 :     CPLXMLNode *psItem = CPLCreateXMLNode(nullptr, CXT_Element, "Item");
    4042        3893 :     CPLAddXMLAttributeAndValue(psItem, "name", pszKey);
    4043             : 
    4044        3893 :     if (nBand > 0)
    4045             :     {
    4046        1165 :         char szBandId[32] = {};
    4047        1165 :         snprintf(szBandId, sizeof(szBandId), "%d", nBand - 1);
    4048        1165 :         CPLAddXMLAttributeAndValue(psItem, "sample", szBandId);
    4049             :     }
    4050             : 
    4051        3893 :     if (pszRole != nullptr)
    4052         383 :         CPLAddXMLAttributeAndValue(psItem, "role", pszRole);
    4053             : 
    4054        3893 :     if (pszDomain != nullptr && strlen(pszDomain) > 0)
    4055        1012 :         CPLAddXMLAttributeAndValue(psItem, "domain", pszDomain);
    4056             : 
    4057        3893 :     if (pszValue)
    4058             :     {
    4059             :         // Note: this escaping should not normally be done, as the serialization
    4060             :         // of the tree to XML also does it, so we end up width double XML escaping,
    4061             :         // but keep it for backward compatibility.
    4062        3872 :         char *pszEscapedItemValue = CPLEscapeString(pszValue, -1, CPLES_XML);
    4063        3872 :         CPLCreateXMLNode(psItem, CXT_Text, pszEscapedItemValue);
    4064        3872 :         CPLFree(pszEscapedItemValue);
    4065             :     }
    4066             :     else
    4067             :     {
    4068          21 :         CPLAddXMLChild(psItem, psValueNode);
    4069             :     }
    4070             : 
    4071             :     /* -------------------------------------------------------------------- */
    4072             :     /*      Create root, if missing.                                        */
    4073             :     /* -------------------------------------------------------------------- */
    4074        3893 :     if (*ppsRoot == nullptr)
    4075         764 :         *ppsRoot = CPLCreateXMLNode(nullptr, CXT_Element, "GDALMetadata");
    4076             : 
    4077             :     /* -------------------------------------------------------------------- */
    4078             :     /*      Append item to tail.  We keep track of the tail to avoid        */
    4079             :     /*      O(nsquared) time as the list gets longer.                       */
    4080             :     /* -------------------------------------------------------------------- */
    4081        3893 :     if (*ppsTail == nullptr)
    4082         764 :         CPLAddXMLChild(*ppsRoot, psItem);
    4083             :     else
    4084        3129 :         CPLAddXMLSibling(*ppsTail, psItem);
    4085             : 
    4086        3893 :     *ppsTail = psItem;
    4087        3893 : }
    4088             : 
    4089             : /************************************************************************/
    4090             : /*                         AppendMetadataItem()                         */
    4091             : /************************************************************************/
    4092             : 
    4093        3872 : static void AppendMetadataItem(CPLXMLNode **ppsRoot, CPLXMLNode **ppsTail,
    4094             :                                const char *pszKey, const char *pszValue,
    4095             :                                int nBand, const char *pszRole,
    4096             :                                const char *pszDomain)
    4097             : 
    4098             : {
    4099        3872 :     AppendMetadataItem(ppsRoot, ppsTail, pszKey, pszValue, nullptr, nBand,
    4100             :                        pszRole, pszDomain);
    4101        3872 : }
    4102             : 
    4103             : /************************************************************************/
    4104             : /*                          WriteMDMetadata()                           */
    4105             : /************************************************************************/
    4106             : 
    4107      311128 : static void WriteMDMetadata(GDALMultiDomainMetadata *poMDMD, TIFF *hTIFF,
    4108             :                             CPLXMLNode **ppsRoot, CPLXMLNode **ppsTail,
    4109             :                             int nBand, GTiffProfile eProfile)
    4110             : 
    4111             : {
    4112             : 
    4113             :     /* ==================================================================== */
    4114             :     /*      Process each domain.                                            */
    4115             :     /* ==================================================================== */
    4116      311128 :     CSLConstList papszDomainList = poMDMD->GetDomainList();
    4117      319647 :     for (int iDomain = 0; papszDomainList && papszDomainList[iDomain];
    4118             :          ++iDomain)
    4119             :     {
    4120        8519 :         CSLConstList papszMD = poMDMD->GetMetadata(papszDomainList[iDomain]);
    4121        8519 :         bool bIsXMLOrJSON = false;
    4122             : 
    4123        8519 :         if (EQUAL(papszDomainList[iDomain], "IMAGE_STRUCTURE") ||
    4124        2488 :             EQUAL(papszDomainList[iDomain], "DERIVED_SUBDATASETS"))
    4125        6034 :             continue;  // Ignored.
    4126        2485 :         if (EQUAL(papszDomainList[iDomain], "COLOR_PROFILE"))
    4127           3 :             continue;  // Handled elsewhere.
    4128        2482 :         if (EQUAL(papszDomainList[iDomain], MD_DOMAIN_RPC))
    4129           7 :             continue;  // Handled elsewhere.
    4130        2476 :         if (EQUAL(papszDomainList[iDomain], "xml:ESRI") &&
    4131           1 :             CPLTestBool(CPLGetConfigOption("ESRI_XML_PAM", "NO")))
    4132           1 :             continue;  // Handled elsewhere.
    4133        2474 :         if (EQUAL(papszDomainList[iDomain], "xml:XMP"))
    4134           2 :             continue;  // Handled in SetMetadata.
    4135             : 
    4136        2472 :         if (STARTS_WITH_CI(papszDomainList[iDomain], "xml:") ||
    4137        2470 :             STARTS_WITH_CI(papszDomainList[iDomain], "json:"))
    4138             :         {
    4139          12 :             bIsXMLOrJSON = true;
    4140             :         }
    4141             : 
    4142             :         /* --------------------------------------------------------------------
    4143             :          */
    4144             :         /*      Process each item in this domain. */
    4145             :         /* --------------------------------------------------------------------
    4146             :          */
    4147        7571 :         for (int iItem = 0; papszMD && papszMD[iItem]; ++iItem)
    4148             :         {
    4149        5099 :             const char *pszItemValue = nullptr;
    4150        5099 :             char *pszItemName = nullptr;
    4151             : 
    4152        5099 :             if (bIsXMLOrJSON)
    4153             :             {
    4154          11 :                 pszItemName = CPLStrdup("doc");
    4155          11 :                 pszItemValue = papszMD[iItem];
    4156             :             }
    4157             :             else
    4158             :             {
    4159        5088 :                 pszItemValue = CPLParseNameValue(papszMD[iItem], &pszItemName);
    4160        5088 :                 if (pszItemName == nullptr)
    4161             :                 {
    4162          49 :                     CPLDebug("GTiff", "Invalid metadata item : %s",
    4163          49 :                              papszMD[iItem]);
    4164          49 :                     continue;
    4165             :                 }
    4166             :             }
    4167             : 
    4168             :             /* --------------------------------------------------------------------
    4169             :              */
    4170             :             /*      Convert into XML item or handle as a special TIFF tag. */
    4171             :             /* --------------------------------------------------------------------
    4172             :              */
    4173        5050 :             if (strlen(papszDomainList[iDomain]) == 0 && nBand == 0 &&
    4174        3695 :                 (STARTS_WITH_CI(pszItemName, "TIFFTAG_") ||
    4175        3634 :                  (EQUAL(pszItemName, "GEO_METADATA") &&
    4176        3633 :                   eProfile == GTiffProfile::GDALGEOTIFF) ||
    4177        3633 :                  (EQUAL(pszItemName, "TIFF_RSID") &&
    4178             :                   eProfile == GTiffProfile::GDALGEOTIFF)))
    4179             :             {
    4180          63 :                 if (EQUAL(pszItemName, "TIFFTAG_RESOLUTIONUNIT"))
    4181             :                 {
    4182             :                     // ResolutionUnit can't be 0, which is the default if
    4183             :                     // atoi() fails.  Set to 1=Unknown.
    4184           9 :                     int v = atoi(pszItemValue);
    4185           9 :                     if (!v)
    4186           1 :                         v = RESUNIT_NONE;
    4187           9 :                     TIFFSetField(hTIFF, TIFFTAG_RESOLUTIONUNIT, v);
    4188             :                 }
    4189             :                 else
    4190             :                 {
    4191          54 :                     bool bFoundTag = false;
    4192          54 :                     size_t iTag = 0;  // Used after for.
    4193          54 :                     const auto *pasTIFFTags = GTiffDataset::GetTIFFTags();
    4194         286 :                     for (; pasTIFFTags[iTag].pszTagName; ++iTag)
    4195             :                     {
    4196         286 :                         if (EQUAL(pszItemName, pasTIFFTags[iTag].pszTagName))
    4197             :                         {
    4198          54 :                             bFoundTag = true;
    4199          54 :                             break;
    4200             :                         }
    4201             :                     }
    4202             : 
    4203          54 :                     if (bFoundTag &&
    4204          54 :                         pasTIFFTags[iTag].eType == GTIFFTAGTYPE_STRING)
    4205          33 :                         TIFFSetField(hTIFF, pasTIFFTags[iTag].nTagVal,
    4206             :                                      pszItemValue);
    4207          21 :                     else if (bFoundTag &&
    4208          21 :                              pasTIFFTags[iTag].eType == GTIFFTAGTYPE_FLOAT)
    4209          16 :                         TIFFSetField(hTIFF, pasTIFFTags[iTag].nTagVal,
    4210             :                                      CPLAtof(pszItemValue));
    4211           5 :                     else if (bFoundTag &&
    4212           5 :                              pasTIFFTags[iTag].eType == GTIFFTAGTYPE_SHORT)
    4213           4 :                         TIFFSetField(hTIFF, pasTIFFTags[iTag].nTagVal,
    4214             :                                      atoi(pszItemValue));
    4215           1 :                     else if (bFoundTag && pasTIFFTags[iTag].eType ==
    4216             :                                               GTIFFTAGTYPE_BYTE_STRING)
    4217             :                     {
    4218           1 :                         uint32_t nLen =
    4219           1 :                             static_cast<uint32_t>(strlen(pszItemValue));
    4220           1 :                         if (nLen)
    4221             :                         {
    4222           1 :                             TIFFSetField(hTIFF, pasTIFFTags[iTag].nTagVal, nLen,
    4223             :                                          pszItemValue);
    4224           1 :                         }
    4225             :                     }
    4226             :                     else
    4227           0 :                         CPLError(CE_Warning, CPLE_NotSupported,
    4228             :                                  "%s metadata item is unhandled and "
    4229             :                                  "will not be written",
    4230             :                                  pszItemName);
    4231          63 :                 }
    4232             :             }
    4233        4987 :             else if (nBand == 0 && EQUAL(pszItemName, GDALMD_AREA_OR_POINT))
    4234             :             {
    4235             :                 /* Do nothing, handled elsewhere. */;
    4236             :             }
    4237             :             else
    4238             :             {
    4239        3077 :                 AppendMetadataItem(ppsRoot, ppsTail, pszItemName, pszItemValue,
    4240        3077 :                                    nBand, nullptr, papszDomainList[iDomain]);
    4241             :             }
    4242             : 
    4243        5050 :             CPLFree(pszItemName);
    4244             :         }
    4245             : 
    4246             :         /* --------------------------------------------------------------------
    4247             :          */
    4248             :         /*      Remove TIFFTAG_xxxxxx that are already set but no longer in */
    4249             :         /*      the metadata list (#5619) */
    4250             :         /* --------------------------------------------------------------------
    4251             :          */
    4252        2472 :         if (strlen(papszDomainList[iDomain]) == 0 && nBand == 0)
    4253             :         {
    4254        2186 :             const auto *pasTIFFTags = GTiffDataset::GetTIFFTags();
    4255       32790 :             for (size_t iTag = 0; pasTIFFTags[iTag].pszTagName; ++iTag)
    4256             :             {
    4257       30604 :                 uint32_t nCount = 0;
    4258       30604 :                 char *pszText = nullptr;
    4259       30604 :                 int16_t nVal = 0;
    4260       30604 :                 float fVal = 0.0f;
    4261             :                 const char *pszVal =
    4262       30604 :                     CSLFetchNameValue(papszMD, pasTIFFTags[iTag].pszTagName);
    4263       61145 :                 if (pszVal == nullptr &&
    4264       30541 :                     ((pasTIFFTags[iTag].eType == GTIFFTAGTYPE_STRING &&
    4265       17455 :                       TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal,
    4266       30533 :                                    &pszText)) ||
    4267       30533 :                      (pasTIFFTags[iTag].eType == GTIFFTAGTYPE_SHORT &&
    4268        6545 :                       TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal, &nVal)) ||
    4269       30530 :                      (pasTIFFTags[iTag].eType == GTIFFTAGTYPE_FLOAT &&
    4270        4356 :                       TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal, &fVal)) ||
    4271       30529 :                      (pasTIFFTags[iTag].eType == GTIFFTAGTYPE_BYTE_STRING &&
    4272        2185 :                       TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal, &nCount,
    4273             :                                    &pszText))))
    4274             :                 {
    4275          13 :                     TIFFUnsetField(hTIFF, pasTIFFTags[iTag].nTagVal);
    4276             :                 }
    4277             :             }
    4278             :         }
    4279             :     }
    4280      311128 : }
    4281             : 
    4282             : /************************************************************************/
    4283             : /*                              WriteRPC()                              */
    4284             : /************************************************************************/
    4285             : 
    4286       10214 : void GTiffDataset::WriteRPC(GDALDataset *poSrcDS, TIFF *l_hTIFF,
    4287             :                             int bSrcIsGeoTIFF, GTiffProfile eProfile,
    4288             :                             const char *pszTIFFFilename,
    4289             :                             CSLConstList papszCreationOptions,
    4290             :                             bool bWriteOnlyInPAMIfNeeded)
    4291             : {
    4292             :     /* -------------------------------------------------------------------- */
    4293             :     /*      Handle RPC data written to TIFF RPCCoefficient tag, RPB file,   */
    4294             :     /*      RPCTEXT file or PAM.                                            */
    4295             :     /* -------------------------------------------------------------------- */
    4296       10214 :     CSLConstList papszRPCMD = poSrcDS->GetMetadata(MD_DOMAIN_RPC);
    4297       10214 :     if (papszRPCMD != nullptr)
    4298             :     {
    4299          32 :         bool bRPCSerializedOtherWay = false;
    4300             : 
    4301          32 :         if (eProfile == GTiffProfile::GDALGEOTIFF)
    4302             :         {
    4303          20 :             if (!bWriteOnlyInPAMIfNeeded)
    4304          11 :                 GTiffDatasetWriteRPCTag(l_hTIFF, papszRPCMD);
    4305          20 :             bRPCSerializedOtherWay = true;
    4306             :         }
    4307             : 
    4308             :         // Write RPB file if explicitly asked, or if a non GDAL specific
    4309             :         // profile is selected and RPCTXT is not asked.
    4310             :         bool bRPBExplicitlyAsked =
    4311          32 :             CPLFetchBool(papszCreationOptions, "RPB", false);
    4312             :         bool bRPBExplicitlyDenied =
    4313          32 :             !CPLFetchBool(papszCreationOptions, "RPB", true);
    4314          44 :         if ((eProfile != GTiffProfile::GDALGEOTIFF &&
    4315          12 :              !CPLFetchBool(papszCreationOptions, "RPCTXT", false) &&
    4316          44 :              !bRPBExplicitlyDenied) ||
    4317             :             bRPBExplicitlyAsked)
    4318             :         {
    4319           8 :             if (!bWriteOnlyInPAMIfNeeded)
    4320           4 :                 GDALWriteRPBFile(pszTIFFFilename, papszRPCMD);
    4321           8 :             bRPCSerializedOtherWay = true;
    4322             :         }
    4323             : 
    4324          32 :         if (CPLFetchBool(papszCreationOptions, "RPCTXT", false))
    4325             :         {
    4326           2 :             if (!bWriteOnlyInPAMIfNeeded)
    4327           1 :                 GDALWriteRPCTXTFile(pszTIFFFilename, papszRPCMD);
    4328           2 :             bRPCSerializedOtherWay = true;
    4329             :         }
    4330             : 
    4331          32 :         if (!bRPCSerializedOtherWay && bWriteOnlyInPAMIfNeeded && bSrcIsGeoTIFF)
    4332           1 :             cpl::down_cast<GTiffDataset *>(poSrcDS)
    4333           1 :                 ->GDALPamDataset::SetMetadata(papszRPCMD, MD_DOMAIN_RPC);
    4334             :     }
    4335       10214 : }
    4336             : 
    4337             : /************************************************************************/
    4338             : /*                           WriteMetadata()                            */
    4339             : /************************************************************************/
    4340             : 
    4341        8099 : bool GTiffDataset::WriteMetadata(GDALDataset *poSrcDS, TIFF *l_hTIFF,
    4342             :                                  bool bSrcIsGeoTIFF, GTiffProfile eProfile,
    4343             :                                  const char *pszTIFFFilename,
    4344             :                                  CSLConstList papszCreationOptions,
    4345             :                                  bool bExcludeRPBandIMGFileWriting)
    4346             : 
    4347             : {
    4348             :     /* -------------------------------------------------------------------- */
    4349             :     /*      Convert all the remaining metadata into a simple XML            */
    4350             :     /*      format.                                                         */
    4351             :     /* -------------------------------------------------------------------- */
    4352        8099 :     CPLXMLNode *psRoot = nullptr;
    4353        8099 :     CPLXMLNode *psTail = nullptr;
    4354             : 
    4355             :     const char *pszCopySrcMDD =
    4356        8099 :         CSLFetchNameValueDef(papszCreationOptions, "COPY_SRC_MDD", "AUTO");
    4357             :     char **papszSrcMDD =
    4358        8099 :         CSLFetchNameValueMultiple(papszCreationOptions, "SRC_MDD");
    4359             : 
    4360             :     GTiffDataset *poSrcDSGTiff =
    4361        8099 :         bSrcIsGeoTIFF ? cpl::down_cast<GTiffDataset *>(poSrcDS) : nullptr;
    4362             : 
    4363        8099 :     if (poSrcDSGTiff)
    4364             :     {
    4365        5957 :         WriteMDMetadata(&poSrcDSGTiff->m_oGTiffMDMD, l_hTIFF, &psRoot, &psTail,
    4366             :                         0, eProfile);
    4367             :     }
    4368             :     else
    4369             :     {
    4370        2142 :         if (EQUAL(pszCopySrcMDD, "AUTO") || CPLTestBool(pszCopySrcMDD) ||
    4371             :             papszSrcMDD)
    4372             :         {
    4373        4278 :             GDALMultiDomainMetadata l_oMDMD;
    4374             :             {
    4375        2139 :                 CSLConstList papszMD = poSrcDS->GetMetadata();
    4376        2143 :                 if (CSLCount(papszMD) > 0 &&
    4377           4 :                     (!papszSrcMDD || CSLFindString(papszSrcMDD, "") >= 0 ||
    4378           2 :                      CSLFindString(papszSrcMDD, "_DEFAULT_") >= 0))
    4379             :                 {
    4380        1620 :                     l_oMDMD.SetMetadata(papszMD);
    4381             :                 }
    4382             :             }
    4383             : 
    4384        2139 :             if (EQUAL(pszCopySrcMDD, "AUTO") && !papszSrcMDD)
    4385             :             {
    4386             :                 // Propagate ISIS3 or VICAR metadata
    4387        6390 :                 for (const char *pszMDD : {"json:ISIS3", "json:VICAR"})
    4388             :                 {
    4389        4260 :                     CSLConstList papszMD = poSrcDS->GetMetadata(pszMDD);
    4390        4260 :                     if (papszMD)
    4391             :                     {
    4392           5 :                         l_oMDMD.SetMetadata(papszMD, pszMDD);
    4393             :                     }
    4394             :                 }
    4395             :             }
    4396             : 
    4397        2139 :             if ((!EQUAL(pszCopySrcMDD, "AUTO") && CPLTestBool(pszCopySrcMDD)) ||
    4398             :                 papszSrcMDD)
    4399             :             {
    4400           9 :                 char **papszDomainList = poSrcDS->GetMetadataDomainList();
    4401          39 :                 for (CSLConstList papszIter = papszDomainList;
    4402          39 :                      papszIter && *papszIter; ++papszIter)
    4403             :                 {
    4404          30 :                     const char *pszDomain = *papszIter;
    4405          46 :                     if (pszDomain[0] != 0 &&
    4406          16 :                         (!papszSrcMDD ||
    4407          16 :                          CSLFindString(papszSrcMDD, pszDomain) >= 0))
    4408             :                     {
    4409          12 :                         l_oMDMD.SetMetadata(poSrcDS->GetMetadata(pszDomain),
    4410             :                                             pszDomain);
    4411             :                     }
    4412             :                 }
    4413           9 :                 CSLDestroy(papszDomainList);
    4414             :             }
    4415             : 
    4416        2139 :             WriteMDMetadata(&l_oMDMD, l_hTIFF, &psRoot, &psTail, 0, eProfile);
    4417             :         }
    4418             :     }
    4419             : 
    4420        8099 :     if (!bExcludeRPBandIMGFileWriting &&
    4421        5951 :         (!poSrcDSGTiff || poSrcDSGTiff->m_poBaseDS == nullptr))
    4422             :     {
    4423        8088 :         WriteRPC(poSrcDS, l_hTIFF, bSrcIsGeoTIFF, eProfile, pszTIFFFilename,
    4424             :                  papszCreationOptions);
    4425             : 
    4426             :         /* ------------------------------------------------------------------ */
    4427             :         /*      Handle metadata data written to an IMD file. */
    4428             :         /* ------------------------------------------------------------------ */
    4429        8088 :         CSLConstList papszIMDMD = poSrcDS->GetMetadata(MD_DOMAIN_IMD);
    4430        8088 :         if (papszIMDMD != nullptr)
    4431             :         {
    4432          20 :             GDALWriteIMDFile(pszTIFFFilename, papszIMDMD);
    4433             :         }
    4434             :     }
    4435             : 
    4436        8099 :     uint16_t nPhotometric = 0;
    4437        8099 :     if (!TIFFGetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, &(nPhotometric)))
    4438           1 :         nPhotometric = PHOTOMETRIC_MINISBLACK;
    4439             : 
    4440        8099 :     const bool bStandardColorInterp = GTIFFIsStandardColorInterpretation(
    4441             :         GDALDataset::ToHandle(poSrcDS), nPhotometric, papszCreationOptions);
    4442             : 
    4443             :     /* -------------------------------------------------------------------- */
    4444             :     /*      We also need to address band specific metadata, and special     */
    4445             :     /*      "role" metadata.                                                */
    4446             :     /* -------------------------------------------------------------------- */
    4447      316107 :     for (int nBand = 1; nBand <= poSrcDS->GetRasterCount(); ++nBand)
    4448             :     {
    4449      308008 :         GDALRasterBand *poBand = poSrcDS->GetRasterBand(nBand);
    4450             : 
    4451      308008 :         if (bSrcIsGeoTIFF)
    4452             :         {
    4453             :             GTiffRasterBand *poSrcBandGTiff =
    4454      302941 :                 cpl::down_cast<GTiffRasterBand *>(poBand);
    4455      302941 :             assert(poSrcBandGTiff);
    4456      302941 :             WriteMDMetadata(&poSrcBandGTiff->m_oGTiffMDMD, l_hTIFF, &psRoot,
    4457             :                             &psTail, nBand, eProfile);
    4458             :         }
    4459             :         else
    4460             :         {
    4461       10134 :             GDALMultiDomainMetadata l_oMDMD;
    4462        5067 :             bool bOMDMDSet = false;
    4463             : 
    4464        5067 :             if (EQUAL(pszCopySrcMDD, "AUTO") && !papszSrcMDD)
    4465             :             {
    4466       15165 :                 for (const char *pszDomain : {"", "IMAGERY"})
    4467             :                 {
    4468       10110 :                     if (CSLConstList papszMD = poBand->GetMetadata(pszDomain))
    4469             :                     {
    4470          89 :                         if (papszMD[0])
    4471             :                         {
    4472          89 :                             bOMDMDSet = true;
    4473          89 :                             l_oMDMD.SetMetadata(papszMD, pszDomain);
    4474             :                         }
    4475             :                     }
    4476        5055 :                 }
    4477             :             }
    4478          12 :             else if (CPLTestBool(pszCopySrcMDD) || papszSrcMDD)
    4479             :             {
    4480           9 :                 char **papszDomainList = poBand->GetMetadataDomainList();
    4481           3 :                 for (const char *pszDomain :
    4482          15 :                      cpl::Iterate(CSLConstList(papszDomainList)))
    4483             :                 {
    4484           9 :                     if (pszDomain[0] != 0 &&
    4485           5 :                         !EQUAL(pszDomain, "IMAGE_STRUCTURE") &&
    4486           2 :                         (!papszSrcMDD ||
    4487           2 :                          CSLFindString(papszSrcMDD, pszDomain) >= 0))
    4488             :                     {
    4489           2 :                         bOMDMDSet = true;
    4490           2 :                         l_oMDMD.SetMetadata(poBand->GetMetadata(pszDomain),
    4491             :                                             pszDomain);
    4492             :                     }
    4493             :                 }
    4494           9 :                 CSLDestroy(papszDomainList);
    4495             :             }
    4496             : 
    4497        5067 :             if (bOMDMDSet)
    4498             :             {
    4499          91 :                 WriteMDMetadata(&l_oMDMD, l_hTIFF, &psRoot, &psTail, nBand,
    4500             :                                 eProfile);
    4501             :             }
    4502             :         }
    4503             : 
    4504      308008 :         const double dfOffset = poBand->GetOffset();
    4505      308008 :         const double dfScale = poBand->GetScale();
    4506      308008 :         bool bGeoTIFFScaleOffsetInZ = false;
    4507      308008 :         GDALGeoTransform gt;
    4508             :         // Check if we have already encoded scale/offset in the GeoTIFF tags
    4509      314150 :         if (poSrcDS->GetGeoTransform(gt) == CE_None && gt.xrot == 0.0 &&
    4510        6126 :             gt.yrot == 0.0 && gt.yscale < 0.0 && poSrcDS->GetSpatialRef() &&
    4511      314157 :             poSrcDS->GetSpatialRef()->IsVertical() &&
    4512           7 :             poSrcDS->GetRasterCount() == 1)
    4513             :         {
    4514           7 :             bGeoTIFFScaleOffsetInZ = true;
    4515             :         }
    4516             : 
    4517      308008 :         if ((dfOffset != 0.0 || dfScale != 1.0) && !bGeoTIFFScaleOffsetInZ)
    4518             :         {
    4519          25 :             char szValue[128] = {};
    4520             : 
    4521          25 :             CPLsnprintf(szValue, sizeof(szValue), "%.17g", dfOffset);
    4522          25 :             AppendMetadataItem(&psRoot, &psTail, "OFFSET", szValue, nBand,
    4523             :                                "offset", "");
    4524          25 :             CPLsnprintf(szValue, sizeof(szValue), "%.17g", dfScale);
    4525          25 :             AppendMetadataItem(&psRoot, &psTail, "SCALE", szValue, nBand,
    4526             :                                "scale", "");
    4527             :         }
    4528             : 
    4529      308008 :         const char *pszUnitType = poBand->GetUnitType();
    4530      308008 :         if (pszUnitType != nullptr && pszUnitType[0] != '\0')
    4531             :         {
    4532          40 :             bool bWriteUnit = true;
    4533          40 :             auto poSRS = poSrcDS->GetSpatialRef();
    4534          40 :             if (poSRS && poSRS->IsCompound())
    4535             :             {
    4536           2 :                 const char *pszVertUnit = nullptr;
    4537           2 :                 poSRS->GetTargetLinearUnits("COMPD_CS|VERT_CS", &pszVertUnit);
    4538           2 :                 if (pszVertUnit && EQUAL(pszVertUnit, pszUnitType))
    4539             :                 {
    4540           2 :                     bWriteUnit = false;
    4541             :                 }
    4542             :             }
    4543          40 :             if (bWriteUnit)
    4544             :             {
    4545          38 :                 AppendMetadataItem(&psRoot, &psTail, "UNITTYPE", pszUnitType,
    4546             :                                    nBand, "unittype", "");
    4547             :             }
    4548             :         }
    4549             : 
    4550      308008 :         if (strlen(poBand->GetDescription()) > 0)
    4551             :         {
    4552          24 :             AppendMetadataItem(&psRoot, &psTail, "DESCRIPTION",
    4553          24 :                                poBand->GetDescription(), nBand, "description",
    4554             :                                "");
    4555             :         }
    4556             : 
    4557      308225 :         if (!bStandardColorInterp &&
    4558         217 :             !(nBand <= 3 && EQUAL(CSLFetchNameValueDef(papszCreationOptions,
    4559             :                                                        "PHOTOMETRIC", ""),
    4560             :                                   "RGB")))
    4561             :         {
    4562         250 :             AppendMetadataItem(&psRoot, &psTail, "COLORINTERP",
    4563             :                                GDALGetColorInterpretationName(
    4564         250 :                                    poBand->GetColorInterpretation()),
    4565             :                                nBand, "colorinterp", "");
    4566             :         }
    4567             :     }
    4568             : 
    4569        8099 :     CSLDestroy(papszSrcMDD);
    4570             : 
    4571             :     const char *pszTilingSchemeName =
    4572        8099 :         CSLFetchNameValue(papszCreationOptions, "@TILING_SCHEME_NAME");
    4573        8099 :     if (pszTilingSchemeName)
    4574             :     {
    4575          23 :         AppendMetadataItem(&psRoot, &psTail, "NAME", pszTilingSchemeName, 0,
    4576             :                            nullptr, "TILING_SCHEME");
    4577             : 
    4578          23 :         const char *pszZoomLevel = CSLFetchNameValue(
    4579             :             papszCreationOptions, "@TILING_SCHEME_ZOOM_LEVEL");
    4580          23 :         if (pszZoomLevel)
    4581             :         {
    4582          23 :             AppendMetadataItem(&psRoot, &psTail, "ZOOM_LEVEL", pszZoomLevel, 0,
    4583             :                                nullptr, "TILING_SCHEME");
    4584             :         }
    4585             : 
    4586          23 :         const char *pszAlignedLevels = CSLFetchNameValue(
    4587             :             papszCreationOptions, "@TILING_SCHEME_ALIGNED_LEVELS");
    4588          23 :         if (pszAlignedLevels)
    4589             :         {
    4590           4 :             AppendMetadataItem(&psRoot, &psTail, "ALIGNED_LEVELS",
    4591             :                                pszAlignedLevels, 0, nullptr, "TILING_SCHEME");
    4592             :         }
    4593             :     }
    4594             : 
    4595        8099 :     if (const char *pszOverviewResampling =
    4596        8099 :             CSLFetchNameValue(papszCreationOptions, "@OVERVIEW_RESAMPLING"))
    4597             :     {
    4598          41 :         AppendMetadataItem(&psRoot, &psTail, "OVERVIEW_RESAMPLING",
    4599             :                            pszOverviewResampling, 0, nullptr,
    4600             :                            "IMAGE_STRUCTURE");
    4601             :     }
    4602             : 
    4603             :     /* -------------------------------------------------------------------- */
    4604             :     /*      Write information about some codecs.                            */
    4605             :     /* -------------------------------------------------------------------- */
    4606        8099 :     if (CPLTestBool(
    4607             :             CPLGetConfigOption("GTIFF_WRITE_IMAGE_STRUCTURE_METADATA", "YES")))
    4608             :     {
    4609             :         const char *pszTileInterleave =
    4610        8094 :             CSLFetchNameValue(papszCreationOptions, "@TILE_INTERLEAVE");
    4611        8094 :         if (pszTileInterleave && CPLTestBool(pszTileInterleave))
    4612             :         {
    4613           7 :             AppendMetadataItem(&psRoot, &psTail, "INTERLEAVE", "TILE", 0,
    4614             :                                nullptr, "IMAGE_STRUCTURE");
    4615             :         }
    4616             : 
    4617             :         const char *pszCompress =
    4618        8094 :             CSLFetchNameValue(papszCreationOptions, "COMPRESS");
    4619        8094 :         if (pszCompress && EQUAL(pszCompress, "WEBP"))
    4620             :         {
    4621          31 :             if (GTiffGetWebPLossless(papszCreationOptions))
    4622             :             {
    4623           6 :                 AppendMetadataItem(&psRoot, &psTail,
    4624             :                                    "COMPRESSION_REVERSIBILITY", "LOSSLESS", 0,
    4625             :                                    nullptr, "IMAGE_STRUCTURE");
    4626             :             }
    4627             :             else
    4628             :             {
    4629          25 :                 AppendMetadataItem(
    4630             :                     &psRoot, &psTail, "WEBP_LEVEL",
    4631          25 :                     CPLSPrintf("%d", GTiffGetWebPLevel(papszCreationOptions)),
    4632             :                     0, nullptr, "IMAGE_STRUCTURE");
    4633             :             }
    4634             :         }
    4635        8063 :         else if (pszCompress && STARTS_WITH_CI(pszCompress, "LERC"))
    4636             :         {
    4637             :             const double dfMaxZError =
    4638          97 :                 GTiffGetLERCMaxZError(papszCreationOptions);
    4639             :             const double dfMaxZErrorOverview =
    4640          97 :                 GTiffGetLERCMaxZErrorOverview(papszCreationOptions);
    4641          97 :             if (dfMaxZError == 0.0 && dfMaxZErrorOverview == 0.0)
    4642             :             {
    4643          83 :                 AppendMetadataItem(&psRoot, &psTail,
    4644             :                                    "COMPRESSION_REVERSIBILITY", "LOSSLESS", 0,
    4645             :                                    nullptr, "IMAGE_STRUCTURE");
    4646             :             }
    4647             :             else
    4648             :             {
    4649          14 :                 AppendMetadataItem(&psRoot, &psTail, "MAX_Z_ERROR",
    4650             :                                    CSLFetchNameValueDef(papszCreationOptions,
    4651             :                                                         "MAX_Z_ERROR", ""),
    4652             :                                    0, nullptr, "IMAGE_STRUCTURE");
    4653          14 :                 if (dfMaxZError != dfMaxZErrorOverview)
    4654             :                 {
    4655           3 :                     AppendMetadataItem(
    4656             :                         &psRoot, &psTail, "MAX_Z_ERROR_OVERVIEW",
    4657             :                         CSLFetchNameValueDef(papszCreationOptions,
    4658             :                                              "MAX_Z_ERROR_OVERVIEW", ""),
    4659             :                         0, nullptr, "IMAGE_STRUCTURE");
    4660             :                 }
    4661          97 :             }
    4662             :         }
    4663             : #if HAVE_JXL
    4664        7966 :         else if (pszCompress && EQUAL(pszCompress, "JXL"))
    4665             :         {
    4666         101 :             float fDistance = 0.0f;
    4667         101 :             if (GTiffGetJXLLossless(papszCreationOptions))
    4668             :             {
    4669          82 :                 AppendMetadataItem(&psRoot, &psTail,
    4670             :                                    "COMPRESSION_REVERSIBILITY", "LOSSLESS", 0,
    4671             :                                    nullptr, "IMAGE_STRUCTURE");
    4672             :             }
    4673             :             else
    4674             :             {
    4675          19 :                 fDistance = GTiffGetJXLDistance(papszCreationOptions);
    4676          19 :                 AppendMetadataItem(
    4677             :                     &psRoot, &psTail, "JXL_DISTANCE",
    4678             :                     CPLSPrintf("%f", static_cast<double>(fDistance)), 0,
    4679             :                     nullptr, "IMAGE_STRUCTURE");
    4680             :             }
    4681             :             const float fAlphaDistance =
    4682         101 :                 GTiffGetJXLAlphaDistance(papszCreationOptions);
    4683         101 :             if (fAlphaDistance >= 0.0f && fAlphaDistance != fDistance)
    4684             :             {
    4685           2 :                 AppendMetadataItem(
    4686             :                     &psRoot, &psTail, "JXL_ALPHA_DISTANCE",
    4687             :                     CPLSPrintf("%f", static_cast<double>(fAlphaDistance)), 0,
    4688             :                     nullptr, "IMAGE_STRUCTURE");
    4689             :             }
    4690         101 :             AppendMetadataItem(
    4691             :                 &psRoot, &psTail, "JXL_EFFORT",
    4692             :                 CPLSPrintf("%d", GTiffGetJXLEffort(papszCreationOptions)), 0,
    4693             :                 nullptr, "IMAGE_STRUCTURE");
    4694             :         }
    4695             : #endif
    4696             :     }
    4697             : 
    4698        8099 :     if (!CPLTestBool(CPLGetConfigOption("GTIFF_WRITE_RAT_TO_PAM", "NO")))
    4699             :     {
    4700      316101 :         for (int nBand = 1; nBand <= poSrcDS->GetRasterCount(); ++nBand)
    4701             :         {
    4702      308005 :             GDALRasterAttributeTable *poRAT = nullptr;
    4703      308005 :             if (poSrcDSGTiff)
    4704             :             {
    4705      302939 :                 auto poBand = cpl::down_cast<GTiffRasterBand *>(
    4706             :                     poSrcDSGTiff->GetRasterBand(nBand));
    4707             :                 // Scenario of https://github.com/OSGeo/gdal/issues/13930
    4708             :                 // Do not try to fetch the RAT from auxiliary files if creating
    4709             :                 // a new GeoTIFF file
    4710      302939 :                 if (poBand->m_bRATSet)
    4711         105 :                     poRAT = poBand->GetDefaultRAT();
    4712             :             }
    4713             :             else
    4714             :             {
    4715        5066 :                 poRAT = poSrcDS->GetRasterBand(nBand)->GetDefaultRAT();
    4716             :             }
    4717      308005 :             if (poRAT)
    4718             :             {
    4719          22 :                 auto psSerializedRAT = poRAT->Serialize();
    4720          22 :                 if (psSerializedRAT)
    4721             :                 {
    4722          21 :                     AppendMetadataItem(
    4723             :                         &psRoot, &psTail, DEFAULT_RASTER_ATTRIBUTE_TABLE,
    4724             :                         nullptr, psSerializedRAT, nBand, RAT_ROLE, nullptr);
    4725             :                 }
    4726             :             }
    4727             :         }
    4728             :     }
    4729             : 
    4730             :     /* -------------------------------------------------------------------- */
    4731             :     /*      Write out the generic XML metadata if there is any.             */
    4732             :     /* -------------------------------------------------------------------- */
    4733        8099 :     if (psRoot != nullptr)
    4734             :     {
    4735         764 :         bool bRet = true;
    4736             : 
    4737         764 :         if (eProfile == GTiffProfile::GDALGEOTIFF)
    4738             :         {
    4739         747 :             char *pszXML_MD = CPLSerializeXMLTree(psRoot);
    4740         747 :             TIFFSetField(l_hTIFF, TIFFTAG_GDAL_METADATA, pszXML_MD);
    4741         747 :             CPLFree(pszXML_MD);
    4742             :         }
    4743             :         else
    4744             :         {
    4745          17 :             if (bSrcIsGeoTIFF)
    4746          11 :                 cpl::down_cast<GTiffDataset *>(poSrcDS)->PushMetadataToPam();
    4747             :             else
    4748           6 :                 bRet = false;
    4749             :         }
    4750             : 
    4751         764 :         CPLDestroyXMLNode(psRoot);
    4752             : 
    4753         764 :         return bRet;
    4754             :     }
    4755             : 
    4756             :     // If we have no more metadata but it existed before,
    4757             :     // remove the GDAL_METADATA tag.
    4758        7335 :     if (eProfile == GTiffProfile::GDALGEOTIFF)
    4759             :     {
    4760        7311 :         char *pszText = nullptr;
    4761        7311 :         if (TIFFGetField(l_hTIFF, TIFFTAG_GDAL_METADATA, &pszText))
    4762             :         {
    4763           7 :             TIFFUnsetField(l_hTIFF, TIFFTAG_GDAL_METADATA);
    4764             :         }
    4765             :     }
    4766             : 
    4767        7335 :     return true;
    4768             : }
    4769             : 
    4770             : /************************************************************************/
    4771             : /*                         PushMetadataToPam()                          */
    4772             : /*                                                                      */
    4773             : /*      When producing a strict profile TIFF or if our aggregate        */
    4774             : /*      metadata is too big for a single tiff tag we may end up         */
    4775             : /*      needing to write it via the PAM mechanisms.  This method        */
    4776             : /*      copies all the appropriate metadata into the PAM level          */
    4777             : /*      metadata object but with special care to avoid copying          */
    4778             : /*      metadata handled in other ways in TIFF format.                  */
    4779             : /************************************************************************/
    4780             : 
    4781          17 : void GTiffDataset::PushMetadataToPam()
    4782             : 
    4783             : {
    4784          17 :     if (GetPamFlags() & GPF_DISABLED)
    4785           0 :         return;
    4786             : 
    4787          17 :     const bool bStandardColorInterp = GTIFFIsStandardColorInterpretation(
    4788          17 :         GDALDataset::ToHandle(this), m_nPhotometric, m_papszCreationOptions);
    4789             : 
    4790          55 :     for (int nBand = 0; nBand <= GetRasterCount(); ++nBand)
    4791             :     {
    4792          38 :         GDALMultiDomainMetadata *poSrcMDMD = nullptr;
    4793          38 :         GTiffRasterBand *poBand = nullptr;
    4794             : 
    4795          38 :         if (nBand == 0)
    4796             :         {
    4797          17 :             poSrcMDMD = &(this->m_oGTiffMDMD);
    4798             :         }
    4799             :         else
    4800             :         {
    4801          21 :             poBand = cpl::down_cast<GTiffRasterBand *>(GetRasterBand(nBand));
    4802          21 :             poSrcMDMD = &(poBand->m_oGTiffMDMD);
    4803             :         }
    4804             : 
    4805             :         /* --------------------------------------------------------------------
    4806             :          */
    4807             :         /*      Loop over the available domains. */
    4808             :         /* --------------------------------------------------------------------
    4809             :          */
    4810          38 :         CSLConstList papszDomainList = poSrcMDMD->GetDomainList();
    4811          74 :         for (int iDomain = 0; papszDomainList && papszDomainList[iDomain];
    4812             :              ++iDomain)
    4813             :         {
    4814          36 :             char **papszMD = poSrcMDMD->GetMetadata(papszDomainList[iDomain]);
    4815             : 
    4816          36 :             if (EQUAL(papszDomainList[iDomain], MD_DOMAIN_RPC) ||
    4817          36 :                 EQUAL(papszDomainList[iDomain], MD_DOMAIN_IMD) ||
    4818          36 :                 EQUAL(papszDomainList[iDomain], "_temporary_") ||
    4819          36 :                 EQUAL(papszDomainList[iDomain], "IMAGE_STRUCTURE") ||
    4820          19 :                 EQUAL(papszDomainList[iDomain], "COLOR_PROFILE"))
    4821          17 :                 continue;
    4822             : 
    4823          19 :             papszMD = CSLDuplicate(papszMD);
    4824             : 
    4825          69 :             for (int i = CSLCount(papszMD) - 1; i >= 0; --i)
    4826             :             {
    4827          50 :                 if (STARTS_WITH_CI(papszMD[i], "TIFFTAG_") ||
    4828          50 :                     EQUALN(papszMD[i], GDALMD_AREA_OR_POINT,
    4829             :                            strlen(GDALMD_AREA_OR_POINT)))
    4830           4 :                     papszMD = CSLRemoveStrings(papszMD, i, 1, nullptr);
    4831             :             }
    4832             : 
    4833          19 :             if (nBand == 0)
    4834          10 :                 GDALPamDataset::SetMetadata(papszMD, papszDomainList[iDomain]);
    4835             :             else
    4836           9 :                 poBand->GDALPamRasterBand::SetMetadata(
    4837           9 :                     papszMD, papszDomainList[iDomain]);
    4838             : 
    4839          19 :             CSLDestroy(papszMD);
    4840             :         }
    4841             : 
    4842             :         /* --------------------------------------------------------------------
    4843             :          */
    4844             :         /*      Handle some "special domain" stuff. */
    4845             :         /* --------------------------------------------------------------------
    4846             :          */
    4847          38 :         if (poBand != nullptr)
    4848             :         {
    4849          21 :             poBand->GDALPamRasterBand::SetOffset(poBand->GetOffset());
    4850          21 :             poBand->GDALPamRasterBand::SetScale(poBand->GetScale());
    4851          21 :             poBand->GDALPamRasterBand::SetUnitType(poBand->GetUnitType());
    4852          21 :             poBand->GDALPamRasterBand::SetDescription(poBand->GetDescription());
    4853          21 :             if (!bStandardColorInterp)
    4854             :             {
    4855           3 :                 poBand->GDALPamRasterBand::SetColorInterpretation(
    4856           3 :                     poBand->GetColorInterpretation());
    4857             :             }
    4858             :         }
    4859             :     }
    4860          17 :     MarkPamDirty();
    4861             : }
    4862             : 
    4863             : /************************************************************************/
    4864             : /*                          WriteNoDataValue()                          */
    4865             : /************************************************************************/
    4866             : 
    4867         521 : void GTiffDataset::WriteNoDataValue(TIFF *hTIFF, double dfNoData)
    4868             : 
    4869             : {
    4870        1042 :     CPLString osVal(GTiffFormatGDALNoDataTagValue(dfNoData));
    4871         521 :     TIFFSetField(hTIFF, TIFFTAG_GDAL_NODATA, osVal.c_str());
    4872         521 : }
    4873             : 
    4874           5 : void GTiffDataset::WriteNoDataValue(TIFF *hTIFF, int64_t nNoData)
    4875             : 
    4876             : {
    4877           5 :     TIFFSetField(hTIFF, TIFFTAG_GDAL_NODATA,
    4878             :                  CPLSPrintf(CPL_FRMT_GIB, static_cast<GIntBig>(nNoData)));
    4879           5 : }
    4880             : 
    4881           5 : void GTiffDataset::WriteNoDataValue(TIFF *hTIFF, uint64_t nNoData)
    4882             : 
    4883             : {
    4884           5 :     TIFFSetField(hTIFF, TIFFTAG_GDAL_NODATA,
    4885             :                  CPLSPrintf(CPL_FRMT_GUIB, static_cast<GUIntBig>(nNoData)));
    4886           5 : }
    4887             : 
    4888             : /************************************************************************/
    4889             : /*                          UnsetNoDataValue()                          */
    4890             : /************************************************************************/
    4891             : 
    4892          16 : void GTiffDataset::UnsetNoDataValue(TIFF *l_hTIFF)
    4893             : 
    4894             : {
    4895          16 :     TIFFUnsetField(l_hTIFF, TIFFTAG_GDAL_NODATA);
    4896          16 : }
    4897             : 
    4898             : /************************************************************************/
    4899             : /*                             SaveICCProfile()                         */
    4900             : /*                                                                      */
    4901             : /*      Save ICC Profile or colorimetric data into file                 */
    4902             : /* pDS:                                                                 */
    4903             : /*      Dataset that contains the metadata with the ICC or colorimetric */
    4904             : /*      data. If this argument is specified, all other arguments are    */
    4905             : /*      ignored. Set them to NULL or 0.                                 */
    4906             : /* hTIFF:                                                               */
    4907             : /*      Pointer to TIFF handle. Only needed if pDS is NULL or           */
    4908             : /*      pDS->m_hTIFF is NULL.                                             */
    4909             : /* papszParamList:                                                       */
    4910             : /*      Options containing the ICC profile or colorimetric metadata.    */
    4911             : /*      Ignored if pDS is not NULL.                                     */
    4912             : /* nBitsPerSample:                                                      */
    4913             : /*      Bits per sample. Ignored if pDS is not NULL.                    */
    4914             : /************************************************************************/
    4915             : 
    4916        9844 : void GTiffDataset::SaveICCProfile(GTiffDataset *pDS, TIFF *l_hTIFF,
    4917             :                                   CSLConstList papszParamList,
    4918             :                                   uint32_t l_nBitsPerSample)
    4919             : {
    4920        9844 :     if ((pDS != nullptr) && (pDS->eAccess != GA_Update))
    4921           0 :         return;
    4922             : 
    4923        9844 :     if (l_hTIFF == nullptr)
    4924             :     {
    4925           2 :         if (pDS == nullptr)
    4926           0 :             return;
    4927             : 
    4928           2 :         l_hTIFF = pDS->m_hTIFF;
    4929           2 :         if (l_hTIFF == nullptr)
    4930           0 :             return;
    4931             :     }
    4932             : 
    4933        9844 :     if ((papszParamList == nullptr) && (pDS == nullptr))
    4934        4889 :         return;
    4935             : 
    4936             :     const char *pszICCProfile =
    4937             :         (pDS != nullptr)
    4938        4955 :             ? pDS->GetMetadataItem("SOURCE_ICC_PROFILE", "COLOR_PROFILE")
    4939        4953 :             : CSLFetchNameValue(papszParamList, "SOURCE_ICC_PROFILE");
    4940        4955 :     if (pszICCProfile != nullptr)
    4941             :     {
    4942           8 :         char *pEmbedBuffer = CPLStrdup(pszICCProfile);
    4943             :         int32_t nEmbedLen =
    4944           8 :             CPLBase64DecodeInPlace(reinterpret_cast<GByte *>(pEmbedBuffer));
    4945             : 
    4946           8 :         TIFFSetField(l_hTIFF, TIFFTAG_ICCPROFILE, nEmbedLen, pEmbedBuffer);
    4947             : 
    4948           8 :         CPLFree(pEmbedBuffer);
    4949             :     }
    4950             :     else
    4951             :     {
    4952             :         // Output colorimetric data.
    4953        4947 :         float pCHR[6] = {};     // Primaries.
    4954        4947 :         uint16_t pTXR[6] = {};  // Transfer range.
    4955        4947 :         const char *pszCHRNames[] = {"SOURCE_PRIMARIES_RED",
    4956             :                                      "SOURCE_PRIMARIES_GREEN",
    4957             :                                      "SOURCE_PRIMARIES_BLUE"};
    4958        4947 :         const char *pszTXRNames[] = {"TIFFTAG_TRANSFERRANGE_BLACK",
    4959             :                                      "TIFFTAG_TRANSFERRANGE_WHITE"};
    4960             : 
    4961             :         // Output chromacities.
    4962        4947 :         bool bOutputCHR = true;
    4963        4962 :         for (int i = 0; i < 3 && bOutputCHR; ++i)
    4964             :         {
    4965             :             const char *pszColorProfile =
    4966             :                 (pDS != nullptr)
    4967        4957 :                     ? pDS->GetMetadataItem(pszCHRNames[i], "COLOR_PROFILE")
    4968        4954 :                     : CSLFetchNameValue(papszParamList, pszCHRNames[i]);
    4969        4957 :             if (pszColorProfile == nullptr)
    4970             :             {
    4971        4942 :                 bOutputCHR = false;
    4972        4942 :                 break;
    4973             :             }
    4974             : 
    4975             :             const CPLStringList aosTokens(CSLTokenizeString2(
    4976             :                 pszColorProfile, ",",
    4977             :                 CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
    4978          15 :                     CSLT_STRIPENDSPACES));
    4979             : 
    4980          15 :             if (aosTokens.size() != 3)
    4981             :             {
    4982           0 :                 bOutputCHR = false;
    4983           0 :                 break;
    4984             :             }
    4985             : 
    4986          60 :             for (int j = 0; j < 3; ++j)
    4987             :             {
    4988          45 :                 float v = static_cast<float>(CPLAtof(aosTokens[j]));
    4989             : 
    4990          45 :                 if (j == 2)
    4991             :                 {
    4992             :                     // Last term of xyY color must be 1.0.
    4993          15 :                     if (v != 1.0f)
    4994             :                     {
    4995           0 :                         bOutputCHR = false;
    4996           0 :                         break;
    4997             :                     }
    4998             :                 }
    4999             :                 else
    5000             :                 {
    5001          30 :                     pCHR[i * 2 + j] = v;
    5002             :                 }
    5003             :             }
    5004             :         }
    5005             : 
    5006        4947 :         if (bOutputCHR)
    5007             :         {
    5008           5 :             TIFFSetField(l_hTIFF, TIFFTAG_PRIMARYCHROMATICITIES, pCHR);
    5009             :         }
    5010             : 
    5011             :         // Output whitepoint.
    5012             :         const char *pszSourceWhitePoint =
    5013             :             (pDS != nullptr)
    5014        4947 :                 ? pDS->GetMetadataItem("SOURCE_WHITEPOINT", "COLOR_PROFILE")
    5015        4946 :                 : CSLFetchNameValue(papszParamList, "SOURCE_WHITEPOINT");
    5016        4947 :         if (pszSourceWhitePoint != nullptr)
    5017             :         {
    5018             :             const CPLStringList aosTokens(CSLTokenizeString2(
    5019             :                 pszSourceWhitePoint, ",",
    5020             :                 CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
    5021          10 :                     CSLT_STRIPENDSPACES));
    5022             : 
    5023           5 :             bool bOutputWhitepoint = true;
    5024           5 :             float pWP[2] = {0.0f, 0.0f};  // Whitepoint
    5025           5 :             if (aosTokens.size() != 3)
    5026             :             {
    5027           0 :                 bOutputWhitepoint = false;
    5028             :             }
    5029             :             else
    5030             :             {
    5031          20 :                 for (int j = 0; j < 3; ++j)
    5032             :                 {
    5033          15 :                     const float v = static_cast<float>(CPLAtof(aosTokens[j]));
    5034             : 
    5035          15 :                     if (j == 2)
    5036             :                     {
    5037             :                         // Last term of xyY color must be 1.0.
    5038           5 :                         if (v != 1.0f)
    5039             :                         {
    5040           0 :                             bOutputWhitepoint = false;
    5041           0 :                             break;
    5042             :                         }
    5043             :                     }
    5044             :                     else
    5045             :                     {
    5046          10 :                         pWP[j] = v;
    5047             :                     }
    5048             :                 }
    5049             :             }
    5050             : 
    5051           5 :             if (bOutputWhitepoint)
    5052             :             {
    5053           5 :                 TIFFSetField(l_hTIFF, TIFFTAG_WHITEPOINT, pWP);
    5054             :             }
    5055             :         }
    5056             : 
    5057             :         // Set transfer function metadata.
    5058             :         char const *pszTFRed =
    5059             :             (pDS != nullptr)
    5060        4947 :                 ? pDS->GetMetadataItem("TIFFTAG_TRANSFERFUNCTION_RED",
    5061             :                                        "COLOR_PROFILE")
    5062        4946 :                 : CSLFetchNameValue(papszParamList,
    5063        4947 :                                     "TIFFTAG_TRANSFERFUNCTION_RED");
    5064             : 
    5065             :         char const *pszTFGreen =
    5066             :             (pDS != nullptr)
    5067        4947 :                 ? pDS->GetMetadataItem("TIFFTAG_TRANSFERFUNCTION_GREEN",
    5068             :                                        "COLOR_PROFILE")
    5069        4946 :                 : CSLFetchNameValue(papszParamList,
    5070        4947 :                                     "TIFFTAG_TRANSFERFUNCTION_GREEN");
    5071             : 
    5072             :         char const *pszTFBlue =
    5073             :             (pDS != nullptr)
    5074        4947 :                 ? pDS->GetMetadataItem("TIFFTAG_TRANSFERFUNCTION_BLUE",
    5075             :                                        "COLOR_PROFILE")
    5076        4946 :                 : CSLFetchNameValue(papszParamList,
    5077        4947 :                                     "TIFFTAG_TRANSFERFUNCTION_BLUE");
    5078             : 
    5079        4947 :         if ((pszTFRed != nullptr) && (pszTFGreen != nullptr) &&
    5080             :             (pszTFBlue != nullptr))
    5081             :         {
    5082             :             // Get length of table.
    5083           4 :             const int nTransferFunctionLength =
    5084           4 :                 1 << ((pDS != nullptr) ? pDS->m_nBitsPerSample
    5085             :                                        : l_nBitsPerSample);
    5086             : 
    5087             :             const CPLStringList aosTokensRed(CSLTokenizeString2(
    5088             :                 pszTFRed, ",",
    5089             :                 CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
    5090           8 :                     CSLT_STRIPENDSPACES));
    5091             :             const CPLStringList aosTokensGreen(CSLTokenizeString2(
    5092             :                 pszTFGreen, ",",
    5093             :                 CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
    5094           8 :                     CSLT_STRIPENDSPACES));
    5095             :             const CPLStringList aosTokensBlue(CSLTokenizeString2(
    5096             :                 pszTFBlue, ",",
    5097             :                 CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
    5098           8 :                     CSLT_STRIPENDSPACES));
    5099             : 
    5100           4 :             if ((aosTokensRed.size() == nTransferFunctionLength) &&
    5101           8 :                 (aosTokensGreen.size() == nTransferFunctionLength) &&
    5102           4 :                 (aosTokensBlue.size() == nTransferFunctionLength))
    5103             :             {
    5104             :                 std::vector<uint16_t> anTransferFuncRed(
    5105           8 :                     nTransferFunctionLength);
    5106             :                 std::vector<uint16_t> anTransferFuncGreen(
    5107           8 :                     nTransferFunctionLength);
    5108             :                 std::vector<uint16_t> anTransferFuncBlue(
    5109           8 :                     nTransferFunctionLength);
    5110             : 
    5111             :                 // Convert our table in string format into int16_t format.
    5112        1028 :                 for (int i = 0; i < nTransferFunctionLength; ++i)
    5113             :                 {
    5114        2048 :                     anTransferFuncRed[i] =
    5115        1024 :                         static_cast<uint16_t>(atoi(aosTokensRed[i]));
    5116        2048 :                     anTransferFuncGreen[i] =
    5117        1024 :                         static_cast<uint16_t>(atoi(aosTokensGreen[i]));
    5118        1024 :                     anTransferFuncBlue[i] =
    5119        1024 :                         static_cast<uint16_t>(atoi(aosTokensBlue[i]));
    5120             :                 }
    5121             : 
    5122           4 :                 TIFFSetField(
    5123             :                     l_hTIFF, TIFFTAG_TRANSFERFUNCTION, anTransferFuncRed.data(),
    5124             :                     anTransferFuncGreen.data(), anTransferFuncBlue.data());
    5125             :             }
    5126             :         }
    5127             : 
    5128             :         // Output transfer range.
    5129        4947 :         bool bOutputTransferRange = true;
    5130        4947 :         for (int i = 0; (i < 2) && bOutputTransferRange; ++i)
    5131             :         {
    5132             :             const char *pszTXRVal =
    5133             :                 (pDS != nullptr)
    5134        4947 :                     ? pDS->GetMetadataItem(pszTXRNames[i], "COLOR_PROFILE")
    5135        4946 :                     : CSLFetchNameValue(papszParamList, pszTXRNames[i]);
    5136        4947 :             if (pszTXRVal == nullptr)
    5137             :             {
    5138        4947 :                 bOutputTransferRange = false;
    5139        4947 :                 break;
    5140             :             }
    5141             : 
    5142             :             const CPLStringList aosTokens(CSLTokenizeString2(
    5143             :                 pszTXRVal, ",",
    5144             :                 CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
    5145           0 :                     CSLT_STRIPENDSPACES));
    5146             : 
    5147           0 :             if (aosTokens.size() != 3)
    5148             :             {
    5149           0 :                 bOutputTransferRange = false;
    5150           0 :                 break;
    5151             :             }
    5152             : 
    5153           0 :             for (int j = 0; j < 3; ++j)
    5154             :             {
    5155           0 :                 pTXR[i + j * 2] = static_cast<uint16_t>(atoi(aosTokens[j]));
    5156             :             }
    5157             :         }
    5158             : 
    5159        4947 :         if (bOutputTransferRange)
    5160             :         {
    5161           0 :             const int TIFFTAG_TRANSFERRANGE = 0x0156;
    5162           0 :             TIFFSetField(l_hTIFF, TIFFTAG_TRANSFERRANGE, pTXR);
    5163             :         }
    5164             :     }
    5165             : }
    5166             : 
    5167       17783 : static signed char GTiffGetLZMAPreset(CSLConstList papszOptions)
    5168             : {
    5169       17783 :     int nLZMAPreset = -1;
    5170       17783 :     const char *pszValue = CSLFetchNameValue(papszOptions, "LZMA_PRESET");
    5171       17783 :     if (pszValue != nullptr)
    5172             :     {
    5173          20 :         nLZMAPreset = atoi(pszValue);
    5174          20 :         if (!(nLZMAPreset >= 0 && nLZMAPreset <= 9))
    5175             :         {
    5176           0 :             CPLError(CE_Warning, CPLE_IllegalArg,
    5177             :                      "LZMA_PRESET=%s value not recognised, ignoring.",
    5178             :                      pszValue);
    5179           0 :             nLZMAPreset = -1;
    5180             :         }
    5181             :     }
    5182       17783 :     return static_cast<signed char>(nLZMAPreset);
    5183             : }
    5184             : 
    5185       17783 : static signed char GTiffGetZSTDPreset(CSLConstList papszOptions)
    5186             : {
    5187       17783 :     int nZSTDLevel = -1;
    5188       17783 :     const char *pszValue = CSLFetchNameValue(papszOptions, "ZSTD_LEVEL");
    5189       17783 :     if (pszValue != nullptr)
    5190             :     {
    5191          24 :         nZSTDLevel = atoi(pszValue);
    5192          24 :         if (!(nZSTDLevel >= 1 && nZSTDLevel <= 22))
    5193             :         {
    5194           0 :             CPLError(CE_Warning, CPLE_IllegalArg,
    5195             :                      "ZSTD_LEVEL=%s value not recognised, ignoring.", pszValue);
    5196           0 :             nZSTDLevel = -1;
    5197             :         }
    5198             :     }
    5199       17783 :     return static_cast<signed char>(nZSTDLevel);
    5200             : }
    5201             : 
    5202       17783 : static signed char GTiffGetZLevel(CSLConstList papszOptions)
    5203             : {
    5204       17783 :     int nZLevel = -1;
    5205       17783 :     const char *pszValue = CSLFetchNameValue(papszOptions, "ZLEVEL");
    5206       17783 :     if (pszValue != nullptr)
    5207             :     {
    5208          44 :         nZLevel = atoi(pszValue);
    5209             : #ifdef TIFFTAG_DEFLATE_SUBCODEC
    5210          44 :         constexpr int nMaxLevel = 12;
    5211             : #ifndef LIBDEFLATE_SUPPORT
    5212             :         if (nZLevel > 9 && nZLevel <= nMaxLevel)
    5213             :         {
    5214             :             CPLDebug("GTiff",
    5215             :                      "ZLEVEL=%d not supported in a non-libdeflate enabled "
    5216             :                      "libtiff build. Capping to 9",
    5217             :                      nZLevel);
    5218             :             nZLevel = 9;
    5219             :         }
    5220             : #endif
    5221             : #else
    5222             :         constexpr int nMaxLevel = 9;
    5223             : #endif
    5224          44 :         if (nZLevel < 1 || nZLevel > nMaxLevel)
    5225             :         {
    5226           0 :             CPLError(CE_Warning, CPLE_IllegalArg,
    5227             :                      "ZLEVEL=%s value not recognised, ignoring.", pszValue);
    5228           0 :             nZLevel = -1;
    5229             :         }
    5230             :     }
    5231       17783 :     return static_cast<signed char>(nZLevel);
    5232             : }
    5233             : 
    5234       17783 : static signed char GTiffGetJpegQuality(CSLConstList papszOptions)
    5235             : {
    5236       17783 :     int nJpegQuality = -1;
    5237       17783 :     const char *pszValue = CSLFetchNameValue(papszOptions, "JPEG_QUALITY");
    5238       17783 :     if (pszValue != nullptr)
    5239             :     {
    5240        1939 :         nJpegQuality = atoi(pszValue);
    5241        1939 :         if (nJpegQuality < 1 || nJpegQuality > 100)
    5242             :         {
    5243           0 :             CPLError(CE_Warning, CPLE_IllegalArg,
    5244             :                      "JPEG_QUALITY=%s value not recognised, ignoring.",
    5245             :                      pszValue);
    5246           0 :             nJpegQuality = -1;
    5247             :         }
    5248             :     }
    5249       17783 :     return static_cast<signed char>(nJpegQuality);
    5250             : }
    5251             : 
    5252       17783 : static signed char GTiffGetJpegTablesMode(CSLConstList papszOptions)
    5253             : {
    5254       17783 :     return static_cast<signed char>(atoi(
    5255             :         CSLFetchNameValueDef(papszOptions, "JPEGTABLESMODE",
    5256       17783 :                              CPLSPrintf("%d", knGTIFFJpegTablesModeDefault))));
    5257             : }
    5258             : 
    5259             : /************************************************************************/
    5260             : /*                        GetDiscardLsbOption()                         */
    5261             : /************************************************************************/
    5262             : 
    5263        7880 : static GTiffDataset::MaskOffset *GetDiscardLsbOption(TIFF *hTIFF,
    5264             :                                                      CSLConstList papszOptions)
    5265             : {
    5266        7880 :     const char *pszBits = CSLFetchNameValue(papszOptions, "DISCARD_LSB");
    5267        7880 :     if (pszBits == nullptr)
    5268        7758 :         return nullptr;
    5269             : 
    5270         122 :     uint16_t nPhotometric = 0;
    5271         122 :     TIFFGetFieldDefaulted(hTIFF, TIFFTAG_PHOTOMETRIC, &nPhotometric);
    5272             : 
    5273         122 :     uint16_t nBitsPerSample = 0;
    5274         122 :     if (!TIFFGetField(hTIFF, TIFFTAG_BITSPERSAMPLE, &nBitsPerSample))
    5275           0 :         nBitsPerSample = 1;
    5276             : 
    5277         122 :     uint16_t nSamplesPerPixel = 0;
    5278         122 :     if (!TIFFGetField(hTIFF, TIFFTAG_SAMPLESPERPIXEL, &nSamplesPerPixel))
    5279           0 :         nSamplesPerPixel = 1;
    5280             : 
    5281         122 :     uint16_t nSampleFormat = 0;
    5282         122 :     if (!TIFFGetField(hTIFF, TIFFTAG_SAMPLEFORMAT, &nSampleFormat))
    5283           0 :         nSampleFormat = SAMPLEFORMAT_UINT;
    5284             : 
    5285         122 :     if (nPhotometric == PHOTOMETRIC_PALETTE)
    5286             :     {
    5287           1 :         CPLError(CE_Warning, CPLE_AppDefined,
    5288             :                  "DISCARD_LSB ignored on a paletted image");
    5289           1 :         return nullptr;
    5290             :     }
    5291         121 :     if (!(nBitsPerSample == 8 || nBitsPerSample == 16 || nBitsPerSample == 32 ||
    5292          13 :           nBitsPerSample == 64))
    5293             :     {
    5294           1 :         CPLError(CE_Warning, CPLE_AppDefined,
    5295             :                  "DISCARD_LSB ignored on non 8, 16, 32 or 64 bits images");
    5296           1 :         return nullptr;
    5297             :     }
    5298             : 
    5299         240 :     const CPLStringList aosTokens(CSLTokenizeString2(pszBits, ",", 0));
    5300         120 :     const int nTokens = aosTokens.size();
    5301         120 :     GTiffDataset::MaskOffset *panMaskOffsetLsb = nullptr;
    5302         120 :     if (nTokens == 1 || nTokens == nSamplesPerPixel)
    5303             :     {
    5304             :         panMaskOffsetLsb = static_cast<GTiffDataset::MaskOffset *>(
    5305         119 :             CPLCalloc(nSamplesPerPixel, sizeof(GTiffDataset::MaskOffset)));
    5306         374 :         for (int i = 0; i < nSamplesPerPixel; ++i)
    5307             :         {
    5308         255 :             const int nBits = atoi(aosTokens[nTokens == 1 ? 0 : i]);
    5309         510 :             const int nMaxBits = (nSampleFormat == SAMPLEFORMAT_IEEEFP)
    5310         510 :                                      ? ((nBitsPerSample == 16)   ? 11 - 1
    5311          78 :                                         : (nBitsPerSample == 32) ? 23 - 1
    5312          26 :                                         : (nBitsPerSample == 64) ? 53 - 1
    5313             :                                                                  : 0)
    5314         203 :                                  : nSampleFormat == SAMPLEFORMAT_INT
    5315         203 :                                      ? nBitsPerSample - 2
    5316         119 :                                      : nBitsPerSample - 1;
    5317             : 
    5318         255 :             if (nBits < 0 || nBits > nMaxBits)
    5319             :             {
    5320           0 :                 CPLError(
    5321             :                     CE_Warning, CPLE_AppDefined,
    5322             :                     "DISCARD_LSB ignored: values should be in [0,%d] range",
    5323             :                     nMaxBits);
    5324           0 :                 VSIFree(panMaskOffsetLsb);
    5325           0 :                 return nullptr;
    5326             :             }
    5327         255 :             panMaskOffsetLsb[i].nMask =
    5328         255 :                 ~((static_cast<uint64_t>(1) << nBits) - 1);
    5329         255 :             if (nBits > 1)
    5330             :             {
    5331         249 :                 panMaskOffsetLsb[i].nRoundUpBitTest = static_cast<uint64_t>(1)
    5332         249 :                                                       << (nBits - 1);
    5333             :             }
    5334         119 :         }
    5335             :     }
    5336             :     else
    5337             :     {
    5338           1 :         CPLError(CE_Warning, CPLE_AppDefined,
    5339             :                  "DISCARD_LSB ignored: wrong number of components");
    5340             :     }
    5341         120 :     return panMaskOffsetLsb;
    5342             : }
    5343             : 
    5344        7880 : void GTiffDataset::GetDiscardLsbOption(CSLConstList papszOptions)
    5345             : {
    5346        7880 :     m_panMaskOffsetLsb = ::GetDiscardLsbOption(m_hTIFF, papszOptions);
    5347        7880 : }
    5348             : 
    5349             : /************************************************************************/
    5350             : /*                             GetProfile()                             */
    5351             : /************************************************************************/
    5352             : 
    5353       17834 : static GTiffProfile GetProfile(const char *pszProfile)
    5354             : {
    5355       17834 :     GTiffProfile eProfile = GTiffProfile::GDALGEOTIFF;
    5356       17834 :     if (pszProfile != nullptr)
    5357             :     {
    5358          70 :         if (EQUAL(pszProfile, szPROFILE_BASELINE))
    5359          50 :             eProfile = GTiffProfile::BASELINE;
    5360          20 :         else if (EQUAL(pszProfile, szPROFILE_GeoTIFF))
    5361          18 :             eProfile = GTiffProfile::GEOTIFF;
    5362           2 :         else if (!EQUAL(pszProfile, szPROFILE_GDALGeoTIFF))
    5363             :         {
    5364           0 :             CPLError(CE_Warning, CPLE_NotSupported,
    5365             :                      "Unsupported value for PROFILE: %s", pszProfile);
    5366             :         }
    5367             :     }
    5368       17834 :     return eProfile;
    5369             : }
    5370             : 
    5371             : /************************************************************************/
    5372             : /*                            GTiffCreate()                             */
    5373             : /*                                                                      */
    5374             : /*      Shared functionality between GTiffDataset::Create() and         */
    5375             : /*      GTiffCreateCopy() for creating TIFF file based on a set of      */
    5376             : /*      options and a configuration.                                    */
    5377             : /************************************************************************/
    5378             : 
    5379        9923 : TIFF *GTiffDataset::CreateLL(const char *pszFilename, int nXSize, int nYSize,
    5380             :                              int l_nBands, GDALDataType eType,
    5381             :                              double dfExtraSpaceForOverviews,
    5382             :                              int nColorTableMultiplier,
    5383             :                              CSLConstList papszParamList, VSILFILE **pfpL,
    5384             :                              CPLString &l_osTmpFilename, bool bCreateCopy,
    5385             :                              bool &bTileInterleavingOut)
    5386             : 
    5387             : {
    5388        9923 :     bTileInterleavingOut = false;
    5389             : 
    5390        9923 :     GTiffOneTimeInit();
    5391             : 
    5392             :     /* -------------------------------------------------------------------- */
    5393             :     /*      Blow on a few errors.                                           */
    5394             :     /* -------------------------------------------------------------------- */
    5395        9923 :     if (nXSize < 1 || nYSize < 1 || l_nBands < 1)
    5396             :     {
    5397           2 :         ReportError(
    5398             :             pszFilename, CE_Failure, CPLE_AppDefined,
    5399             :             "Attempt to create %dx%dx%d TIFF file, but width, height and bands "
    5400             :             "must be positive.",
    5401             :             nXSize, nYSize, l_nBands);
    5402             : 
    5403           2 :         return nullptr;
    5404             :     }
    5405             : 
    5406        9921 :     if (l_nBands > 65535)
    5407             :     {
    5408           1 :         ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    5409             :                     "Attempt to create %dx%dx%d TIFF file, but bands "
    5410             :                     "must be lesser or equal to 65535.",
    5411             :                     nXSize, nYSize, l_nBands);
    5412             : 
    5413           1 :         return nullptr;
    5414             :     }
    5415             : 
    5416             :     /* -------------------------------------------------------------------- */
    5417             :     /*      Setup values based on options.                                  */
    5418             :     /* -------------------------------------------------------------------- */
    5419             :     const GTiffProfile eProfile =
    5420        9920 :         GetProfile(CSLFetchNameValue(papszParamList, "PROFILE"));
    5421             : 
    5422        9920 :     const bool bTiled = CPLFetchBool(papszParamList, "TILED", false);
    5423             : 
    5424        9920 :     int l_nBlockXSize = 0;
    5425        9920 :     if (const char *pszValue = CSLFetchNameValue(papszParamList, "BLOCKXSIZE"))
    5426             :     {
    5427         474 :         l_nBlockXSize = atoi(pszValue);
    5428         474 :         if (l_nBlockXSize < 0)
    5429             :         {
    5430           0 :             ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
    5431             :                         "Invalid value for BLOCKXSIZE");
    5432           0 :             return nullptr;
    5433             :         }
    5434         474 :         if (!bTiled)
    5435             :         {
    5436          10 :             ReportError(pszFilename, CE_Warning, CPLE_IllegalArg,
    5437             :                         "BLOCKXSIZE can only be used with TILED=YES");
    5438             :         }
    5439         464 :         else if (l_nBlockXSize % 16 != 0)
    5440             :         {
    5441           1 :             ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
    5442             :                         "BLOCKXSIZE must be a multiple of 16");
    5443           1 :             return nullptr;
    5444             :         }
    5445             :     }
    5446             : 
    5447        9919 :     int l_nBlockYSize = 0;
    5448        9919 :     if (const char *pszValue = CSLFetchNameValue(papszParamList, "BLOCKYSIZE"))
    5449             :     {
    5450        2585 :         l_nBlockYSize = atoi(pszValue);
    5451        2585 :         if (l_nBlockYSize < 0)
    5452             :         {
    5453           0 :             ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
    5454             :                         "Invalid value for BLOCKYSIZE");
    5455           0 :             return nullptr;
    5456             :         }
    5457        2585 :         if (bTiled && (l_nBlockYSize % 16 != 0))
    5458             :         {
    5459           2 :             ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
    5460             :                         "BLOCKYSIZE must be a multiple of 16");
    5461           2 :             return nullptr;
    5462             :         }
    5463             :     }
    5464             : 
    5465        9917 :     if (bTiled)
    5466             :     {
    5467         813 :         if (l_nBlockXSize == 0)
    5468         351 :             l_nBlockXSize = 256;
    5469             : 
    5470         813 :         if (l_nBlockYSize == 0)
    5471         351 :             l_nBlockYSize = 256;
    5472             :     }
    5473             : 
    5474        9917 :     int nPlanar = 0;
    5475             : 
    5476             :     // Hidden @TILE_INTERLEAVE=YES parameter used by the COG driver
    5477        9917 :     if (bCreateCopy && CPLTestBool(CSLFetchNameValueDef(
    5478             :                            papszParamList, "@TILE_INTERLEAVE", "NO")))
    5479             :     {
    5480           7 :         bTileInterleavingOut = true;
    5481           7 :         nPlanar = PLANARCONFIG_SEPARATE;
    5482             :     }
    5483             :     else
    5484             :     {
    5485        9910 :         if (const char *pszValue =
    5486        9910 :                 CSLFetchNameValue(papszParamList, "INTERLEAVE"))
    5487             :         {
    5488        1581 :             if (EQUAL(pszValue, "PIXEL"))
    5489         409 :                 nPlanar = PLANARCONFIG_CONTIG;
    5490        1172 :             else if (EQUAL(pszValue, "BAND"))
    5491             :             {
    5492        1171 :                 nPlanar = PLANARCONFIG_SEPARATE;
    5493             :             }
    5494           1 :             else if (EQUAL(pszValue, "BAND"))
    5495             :             {
    5496           0 :                 nPlanar = PLANARCONFIG_SEPARATE;
    5497             :             }
    5498             :             else
    5499             :             {
    5500           1 :                 ReportError(
    5501             :                     pszFilename, CE_Failure, CPLE_IllegalArg,
    5502             :                     "INTERLEAVE=%s unsupported, value must be PIXEL or BAND.",
    5503             :                     pszValue);
    5504           1 :                 return nullptr;
    5505             :             }
    5506             :         }
    5507             :         else
    5508             :         {
    5509        8329 :             nPlanar = PLANARCONFIG_CONTIG;
    5510             :         }
    5511             :     }
    5512             : 
    5513        9916 :     int l_nCompression = COMPRESSION_NONE;
    5514        9916 :     if (const char *pszValue = CSLFetchNameValue(papszParamList, "COMPRESS"))
    5515             :     {
    5516        3346 :         l_nCompression = GTIFFGetCompressionMethod(pszValue, "COMPRESS");
    5517        3346 :         if (l_nCompression < 0)
    5518           0 :             return nullptr;
    5519             :     }
    5520             : 
    5521        9916 :     constexpr int JPEG_MAX_DIMENSION = 65500;  // Defined in jpeglib.h
    5522        9916 :     constexpr int WEBP_MAX_DIMENSION = 16383;
    5523             : 
    5524             :     const struct
    5525             :     {
    5526             :         int nCodecID;
    5527             :         const char *pszCodecName;
    5528             :         int nMaxDim;
    5529        9916 :     } asLimitations[] = {
    5530             :         {COMPRESSION_JPEG, "JPEG", JPEG_MAX_DIMENSION},
    5531             :         {COMPRESSION_WEBP, "WEBP", WEBP_MAX_DIMENSION},
    5532             :     };
    5533             : 
    5534       29736 :     for (const auto &sLimitation : asLimitations)
    5535             :     {
    5536       19828 :         if (l_nCompression == sLimitation.nCodecID && !bTiled &&
    5537        2074 :             nXSize > sLimitation.nMaxDim)
    5538             :         {
    5539           2 :             ReportError(
    5540             :                 pszFilename, CE_Failure, CPLE_IllegalArg,
    5541             :                 "COMPRESS=%s is only compatible of un-tiled images whose "
    5542             :                 "width is lesser or equal to %d pixels. "
    5543             :                 "To overcome this limitation, set the TILED=YES creation "
    5544             :                 "option.",
    5545           2 :                 sLimitation.pszCodecName, sLimitation.nMaxDim);
    5546           2 :             return nullptr;
    5547             :         }
    5548       19826 :         else if (l_nCompression == sLimitation.nCodecID && bTiled &&
    5549          52 :                  l_nBlockXSize > sLimitation.nMaxDim)
    5550             :         {
    5551           2 :             ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
    5552             :                         "COMPRESS=%s is only compatible of tiled images whose "
    5553             :                         "BLOCKXSIZE is lesser or equal to %d pixels.",
    5554           2 :                         sLimitation.pszCodecName, sLimitation.nMaxDim);
    5555           2 :             return nullptr;
    5556             :         }
    5557       19824 :         else if (l_nCompression == sLimitation.nCodecID &&
    5558        2122 :                  l_nBlockYSize > sLimitation.nMaxDim)
    5559             :         {
    5560           4 :             ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
    5561             :                         "COMPRESS=%s is only compatible of images whose "
    5562             :                         "BLOCKYSIZE is lesser or equal to %d pixels. "
    5563             :                         "To overcome this limitation, set the TILED=YES "
    5564             :                         "creation option",
    5565           4 :                         sLimitation.pszCodecName, sLimitation.nMaxDim);
    5566           4 :             return nullptr;
    5567             :         }
    5568             :     }
    5569             : 
    5570             :     /* -------------------------------------------------------------------- */
    5571             :     /*      How many bits per sample?  We have a special case if NBITS      */
    5572             :     /*      specified for GDT_UInt8, GDT_UInt16, GDT_UInt32.                 */
    5573             :     /* -------------------------------------------------------------------- */
    5574        9908 :     int l_nBitsPerSample = GDALGetDataTypeSizeBits(eType);
    5575        9908 :     if (CSLFetchNameValue(papszParamList, "NBITS") != nullptr)
    5576             :     {
    5577        1747 :         int nMinBits = 0;
    5578        1747 :         int nMaxBits = 0;
    5579        1747 :         l_nBitsPerSample = atoi(CSLFetchNameValue(papszParamList, "NBITS"));
    5580        1747 :         if (eType == GDT_UInt8)
    5581             :         {
    5582         527 :             nMinBits = 1;
    5583         527 :             nMaxBits = 8;
    5584             :         }
    5585        1220 :         else if (eType == GDT_UInt16)
    5586             :         {
    5587        1202 :             nMinBits = 9;
    5588        1202 :             nMaxBits = 16;
    5589             :         }
    5590          18 :         else if (eType == GDT_UInt32)
    5591             :         {
    5592          14 :             nMinBits = 17;
    5593          14 :             nMaxBits = 32;
    5594             :         }
    5595           4 :         else if (eType == GDT_Float32)
    5596             :         {
    5597           4 :             if (l_nBitsPerSample != 16 && l_nBitsPerSample != 32)
    5598             :             {
    5599           1 :                 ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
    5600             :                             "Only NBITS=16 is supported for data type Float32");
    5601           1 :                 l_nBitsPerSample = GDALGetDataTypeSizeBits(eType);
    5602             :             }
    5603             :         }
    5604             :         else
    5605             :         {
    5606           0 :             ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
    5607             :                         "NBITS is not supported for data type %s",
    5608             :                         GDALGetDataTypeName(eType));
    5609           0 :             l_nBitsPerSample = GDALGetDataTypeSizeBits(eType);
    5610             :         }
    5611             : 
    5612        1747 :         if (nMinBits != 0)
    5613             :         {
    5614        1743 :             if (l_nBitsPerSample < nMinBits)
    5615             :             {
    5616           2 :                 ReportError(
    5617             :                     pszFilename, CE_Warning, CPLE_AppDefined,
    5618             :                     "NBITS=%d is invalid for data type %s. Using NBITS=%d",
    5619             :                     l_nBitsPerSample, GDALGetDataTypeName(eType), nMinBits);
    5620           2 :                 l_nBitsPerSample = nMinBits;
    5621             :             }
    5622        1741 :             else if (l_nBitsPerSample > nMaxBits)
    5623             :             {
    5624           3 :                 ReportError(
    5625             :                     pszFilename, CE_Warning, CPLE_AppDefined,
    5626             :                     "NBITS=%d is invalid for data type %s. Using NBITS=%d",
    5627             :                     l_nBitsPerSample, GDALGetDataTypeName(eType), nMaxBits);
    5628           3 :                 l_nBitsPerSample = nMaxBits;
    5629             :             }
    5630             :         }
    5631             :     }
    5632             : 
    5633             : #ifdef HAVE_JXL
    5634        9908 :     if ((l_nCompression == COMPRESSION_JXL ||
    5635         106 :          l_nCompression == COMPRESSION_JXL_DNG_1_7) &&
    5636         105 :         eType != GDT_Float16 && eType != GDT_Float32)
    5637             :     {
    5638             :         // Reflects tif_jxl's GetJXLDataType()
    5639          85 :         if (eType != GDT_UInt8 && eType != GDT_UInt16)
    5640             :         {
    5641           1 :             ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5642             :                         "Data type %s not supported for JXL compression. Only "
    5643             :                         "Byte, UInt16, Float16, Float32 are supported",
    5644             :                         GDALGetDataTypeName(eType));
    5645           2 :             return nullptr;
    5646             :         }
    5647             : 
    5648             :         const struct
    5649             :         {
    5650             :             GDALDataType eDT;
    5651             :             int nBitsPerSample;
    5652          84 :         } asSupportedDTBitsPerSample[] = {
    5653             :             {GDT_UInt8, 8},
    5654             :             {GDT_UInt16, 16},
    5655             :         };
    5656             : 
    5657         250 :         for (const auto &sSupportedDTBitsPerSample : asSupportedDTBitsPerSample)
    5658             :         {
    5659         167 :             if (eType == sSupportedDTBitsPerSample.eDT &&
    5660          84 :                 l_nBitsPerSample != sSupportedDTBitsPerSample.nBitsPerSample)
    5661             :             {
    5662           1 :                 ReportError(
    5663             :                     pszFilename, CE_Failure, CPLE_NotSupported,
    5664             :                     "Bits per sample=%d not supported for JXL compression. "
    5665             :                     "Only %d is supported for %s data type.",
    5666           1 :                     l_nBitsPerSample, sSupportedDTBitsPerSample.nBitsPerSample,
    5667             :                     GDALGetDataTypeName(eType));
    5668           1 :                 return nullptr;
    5669             :             }
    5670             :         }
    5671             :     }
    5672             : #endif
    5673             : 
    5674        9906 :     int nPredictor = PREDICTOR_NONE;
    5675        9906 :     const char *pszPredictor = CSLFetchNameValue(papszParamList, "PREDICTOR");
    5676        9906 :     if (pszPredictor)
    5677             :     {
    5678          31 :         nPredictor = atoi(pszPredictor);
    5679             :     }
    5680             : 
    5681        9906 :     if (nPredictor != PREDICTOR_NONE &&
    5682          18 :         l_nCompression != COMPRESSION_ADOBE_DEFLATE &&
    5683           2 :         l_nCompression != COMPRESSION_LZW &&
    5684           2 :         l_nCompression != COMPRESSION_LZMA &&
    5685             :         l_nCompression != COMPRESSION_ZSTD)
    5686             :     {
    5687           1 :         ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
    5688             :                     "PREDICTOR option is ignored for COMPRESS=%s. "
    5689             :                     "Only valid for DEFLATE, LZW, LZMA or ZSTD",
    5690             :                     CSLFetchNameValueDef(papszParamList, "COMPRESS", "NONE"));
    5691             :     }
    5692             : 
    5693             :     // Do early checks as libtiff will only error out when starting to write.
    5694        9935 :     else if (nPredictor != PREDICTOR_NONE &&
    5695          30 :              CPLTestBool(
    5696             :                  CPLGetConfigOption("GDAL_GTIFF_PREDICTOR_CHECKS", "YES")))
    5697             :     {
    5698             : #if (TIFFLIB_VERSION > 20210416) || defined(INTERNAL_LIBTIFF)
    5699             : #define HAVE_PREDICTOR_2_FOR_64BIT
    5700             : #endif
    5701          30 :         if (nPredictor == 2)
    5702             :         {
    5703          24 :             if (l_nBitsPerSample != 8 && l_nBitsPerSample != 16 &&
    5704             :                 l_nBitsPerSample != 32
    5705             : #ifdef HAVE_PREDICTOR_2_FOR_64BIT
    5706           2 :                 && l_nBitsPerSample != 64
    5707             : #endif
    5708             :             )
    5709             :             {
    5710             : #if !defined(HAVE_PREDICTOR_2_FOR_64BIT)
    5711             :                 if (l_nBitsPerSample == 64)
    5712             :                 {
    5713             :                     ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    5714             :                                 "PREDICTOR=2 is supported on 64 bit samples "
    5715             :                                 "starting with libtiff > 4.3.0.");
    5716             :                 }
    5717             :                 else
    5718             : #endif
    5719             :                 {
    5720           2 :                     const int nBITSHint = (l_nBitsPerSample < 8)    ? 8
    5721           1 :                                           : (l_nBitsPerSample < 16) ? 16
    5722           0 :                                           : (l_nBitsPerSample < 32) ? 32
    5723             :                                                                     : 64;
    5724           1 :                     ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    5725             : #ifdef HAVE_PREDICTOR_2_FOR_64BIT
    5726             :                                 "PREDICTOR=2 is only supported with 8/16/32/64 "
    5727             :                                 "bit samples. You can specify the NBITS=%d "
    5728             :                                 "creation option to promote to the closest "
    5729             :                                 "supported bits per sample value.",
    5730             : #else
    5731             :                                 "PREDICTOR=2 is only supported with 8/16/32 "
    5732             :                                 "bit samples. You can specify the NBITS=%d "
    5733             :                                 "creation option to promote to the closest "
    5734             :                                 "supported bits per sample value.",
    5735             : #endif
    5736             :                                 nBITSHint);
    5737             :                 }
    5738           1 :                 return nullptr;
    5739             :             }
    5740             :         }
    5741           6 :         else if (nPredictor == 3)
    5742             :         {
    5743           5 :             if (eType != GDT_Float16 && eType != GDT_Float32 &&
    5744             :                 eType != GDT_Float64)
    5745             :             {
    5746           1 :                 ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    5747             :                             "PREDICTOR=3 is only supported with Float16, "
    5748             :                             "Float32 or Float64.");
    5749           1 :                 return nullptr;
    5750             :             }
    5751             :         }
    5752             :         else
    5753             :         {
    5754           1 :             ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    5755             :                         "PREDICTOR=%s is not supported.", pszPredictor);
    5756           1 :             return nullptr;
    5757             :         }
    5758             :     }
    5759             : 
    5760        9903 :     const int l_nZLevel = GTiffGetZLevel(papszParamList);
    5761        9903 :     const int l_nLZMAPreset = GTiffGetLZMAPreset(papszParamList);
    5762        9903 :     const int l_nZSTDLevel = GTiffGetZSTDPreset(papszParamList);
    5763        9903 :     const int l_nWebPLevel = GTiffGetWebPLevel(papszParamList);
    5764        9903 :     const bool l_bWebPLossless = GTiffGetWebPLossless(papszParamList);
    5765        9903 :     const int l_nJpegQuality = GTiffGetJpegQuality(papszParamList);
    5766        9903 :     const int l_nJpegTablesMode = GTiffGetJpegTablesMode(papszParamList);
    5767        9903 :     const double l_dfMaxZError = GTiffGetLERCMaxZError(papszParamList);
    5768             : #if HAVE_JXL
    5769        9903 :     bool bJXLLosslessSpecified = false;
    5770             :     const bool l_bJXLLossless =
    5771        9903 :         GTiffGetJXLLossless(papszParamList, &bJXLLosslessSpecified);
    5772        9903 :     const uint32_t l_nJXLEffort = GTiffGetJXLEffort(papszParamList);
    5773        9903 :     bool bJXLDistanceSpecified = false;
    5774             :     const float l_fJXLDistance =
    5775        9903 :         GTiffGetJXLDistance(papszParamList, &bJXLDistanceSpecified);
    5776        9903 :     if (bJXLDistanceSpecified && l_bJXLLossless)
    5777             :     {
    5778           1 :         ReportError(pszFilename, CE_Warning, CPLE_AppDefined,
    5779             :                     "JXL_DISTANCE creation option is ignored, given %s "
    5780             :                     "JXL_LOSSLESS=YES",
    5781             :                     bJXLLosslessSpecified ? "(explicit)" : "(implicit)");
    5782             :     }
    5783        9903 :     bool bJXLAlphaDistanceSpecified = false;
    5784             :     const float l_fJXLAlphaDistance =
    5785        9903 :         GTiffGetJXLAlphaDistance(papszParamList, &bJXLAlphaDistanceSpecified);
    5786        9903 :     if (bJXLAlphaDistanceSpecified && l_bJXLLossless)
    5787             :     {
    5788           1 :         ReportError(pszFilename, CE_Warning, CPLE_AppDefined,
    5789             :                     "JXL_ALPHA_DISTANCE creation option is ignored, given %s "
    5790             :                     "JXL_LOSSLESS=YES",
    5791             :                     bJXLLosslessSpecified ? "(explicit)" : "(implicit)");
    5792             :     }
    5793             : #endif
    5794             :     /* -------------------------------------------------------------------- */
    5795             :     /*      Streaming related code                                          */
    5796             :     /* -------------------------------------------------------------------- */
    5797       19806 :     const CPLString osOriFilename(pszFilename);
    5798       19806 :     bool bStreaming = strcmp(pszFilename, "/vsistdout/") == 0 ||
    5799        9903 :                       CPLFetchBool(papszParamList, "STREAMABLE_OUTPUT", false);
    5800             : #ifdef S_ISFIFO
    5801        9903 :     if (!bStreaming)
    5802             :     {
    5803             :         VSIStatBufL sStat;
    5804        9891 :         if (VSIStatExL(pszFilename, &sStat,
    5805       10770 :                        VSI_STAT_EXISTS_FLAG | VSI_STAT_NATURE_FLAG) == 0 &&
    5806         879 :             S_ISFIFO(sStat.st_mode))
    5807             :         {
    5808           0 :             bStreaming = true;
    5809             :         }
    5810             :     }
    5811             : #endif
    5812        9903 :     if (bStreaming && !EQUAL("NONE", CSLFetchNameValueDef(papszParamList,
    5813             :                                                           "COMPRESS", "NONE")))
    5814             :     {
    5815           1 :         ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5816             :                     "Streaming only supported to uncompressed TIFF");
    5817           1 :         return nullptr;
    5818             :     }
    5819        9902 :     if (bStreaming && CPLFetchBool(papszParamList, "SPARSE_OK", false))
    5820             :     {
    5821           1 :         ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5822             :                     "Streaming not supported with SPARSE_OK");
    5823           1 :         return nullptr;
    5824             :     }
    5825             :     const bool bCopySrcOverviews =
    5826        9901 :         CPLFetchBool(papszParamList, "COPY_SRC_OVERVIEWS", false);
    5827        9901 :     if (bStreaming && bCopySrcOverviews)
    5828             :     {
    5829           1 :         ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5830             :                     "Streaming not supported with COPY_SRC_OVERVIEWS");
    5831           1 :         return nullptr;
    5832             :     }
    5833        9900 :     if (bStreaming)
    5834             :     {
    5835           9 :         l_osTmpFilename = VSIMemGenerateHiddenFilename("vsistdout.tif");
    5836           9 :         pszFilename = l_osTmpFilename.c_str();
    5837             :     }
    5838             : 
    5839             :     /* -------------------------------------------------------------------- */
    5840             :     /*      Compute the uncompressed size.                                  */
    5841             :     /* -------------------------------------------------------------------- */
    5842        9900 :     const unsigned nTileXCount =
    5843        9900 :         bTiled ? DIV_ROUND_UP(nXSize, l_nBlockXSize) : 0;
    5844        9900 :     const unsigned nTileYCount =
    5845        9900 :         bTiled ? DIV_ROUND_UP(nYSize, l_nBlockYSize) : 0;
    5846             :     const double dfUncompressedImageSize =
    5847        9900 :         (bTiled ? (static_cast<double>(nTileXCount) * nTileYCount *
    5848         809 :                    l_nBlockXSize * l_nBlockYSize)
    5849        9091 :                 : (nXSize * static_cast<double>(nYSize))) *
    5850        9900 :             l_nBands * GDALGetDataTypeSizeBytes(eType) +
    5851        9900 :         dfExtraSpaceForOverviews;
    5852             : 
    5853             :     /* -------------------------------------------------------------------- */
    5854             :     /*      Should the file be created as a bigtiff file?                   */
    5855             :     /* -------------------------------------------------------------------- */
    5856        9900 :     const char *pszBIGTIFF = CSLFetchNameValue(papszParamList, "BIGTIFF");
    5857             : 
    5858        9900 :     if (pszBIGTIFF == nullptr)
    5859        9459 :         pszBIGTIFF = "IF_NEEDED";
    5860             : 
    5861        9900 :     bool bCreateBigTIFF = false;
    5862        9900 :     if (EQUAL(pszBIGTIFF, "IF_NEEDED"))
    5863             :     {
    5864        9460 :         if (l_nCompression == COMPRESSION_NONE &&
    5865             :             dfUncompressedImageSize > 4200000000.0)
    5866          17 :             bCreateBigTIFF = true;
    5867             :     }
    5868         440 :     else if (EQUAL(pszBIGTIFF, "IF_SAFER"))
    5869             :     {
    5870         419 :         if (dfUncompressedImageSize > 2000000000.0)
    5871           1 :             bCreateBigTIFF = true;
    5872             :     }
    5873             :     else
    5874             :     {
    5875          21 :         bCreateBigTIFF = CPLTestBool(pszBIGTIFF);
    5876          21 :         if (!bCreateBigTIFF && l_nCompression == COMPRESSION_NONE &&
    5877             :             dfUncompressedImageSize > 4200000000.0)
    5878             :         {
    5879           2 :             ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5880             :                         "The TIFF file will be larger than 4GB, so BigTIFF is "
    5881             :                         "necessary.  Creation failed.");
    5882           2 :             return nullptr;
    5883             :         }
    5884             :     }
    5885             : 
    5886        9898 :     if (bCreateBigTIFF)
    5887          35 :         CPLDebug("GTiff", "File being created as a BigTIFF.");
    5888             : 
    5889             :     /* -------------------------------------------------------------------- */
    5890             :     /*      Sanity check.                                                   */
    5891             :     /* -------------------------------------------------------------------- */
    5892        9898 :     if (bTiled)
    5893             :     {
    5894             :         // libtiff implementation limitation
    5895         809 :         if (nTileXCount > 0x80000000U / (bCreateBigTIFF ? 8 : 4) / nTileYCount)
    5896             :         {
    5897           3 :             ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5898             :                         "File too large regarding tile size. This would result "
    5899             :                         "in a file with tile arrays larger than 2GB");
    5900           3 :             return nullptr;
    5901             :         }
    5902             :     }
    5903             : 
    5904             :     /* -------------------------------------------------------------------- */
    5905             :     /*      Check free space (only for big, non sparse)                     */
    5906             :     /* -------------------------------------------------------------------- */
    5907        9895 :     const double dfLikelyFloorOfFinalSize =
    5908             :         l_nCompression == COMPRESSION_NONE
    5909        9895 :             ? dfUncompressedImageSize
    5910             :             :
    5911             :             /* For compressed, we target 1% as the most optimistic reduction factor! */
    5912             :             0.01 * dfUncompressedImageSize;
    5913        9917 :     if (dfLikelyFloorOfFinalSize >= 1e9 &&
    5914          22 :         !CPLFetchBool(papszParamList, "SPARSE_OK", false) &&
    5915           5 :         osOriFilename != "/vsistdout/" &&
    5916        9922 :         osOriFilename != "/vsistdout_redirect/" &&
    5917           5 :         CPLTestBool(CPLGetConfigOption("CHECK_DISK_FREE_SPACE", "TRUE")))
    5918             :     {
    5919             :         const GIntBig nFreeDiskSpace =
    5920           4 :             VSIGetDiskFreeSpace(CPLGetDirnameSafe(pszFilename).c_str());
    5921           4 :         if (nFreeDiskSpace >= 0 && nFreeDiskSpace < dfLikelyFloorOfFinalSize)
    5922             :         {
    5923           6 :             ReportError(
    5924             :                 pszFilename, CE_Failure, CPLE_FileIO,
    5925             :                 "Free disk space available is %s, "
    5926             :                 "whereas %s are %s necessary. "
    5927             :                 "You can disable this check by defining the "
    5928             :                 "CHECK_DISK_FREE_SPACE configuration option to FALSE.",
    5929           4 :                 CPLFormatReadableFileSize(static_cast<uint64_t>(nFreeDiskSpace))
    5930             :                     .c_str(),
    5931           4 :                 CPLFormatReadableFileSize(dfLikelyFloorOfFinalSize).c_str(),
    5932             :                 l_nCompression == COMPRESSION_NONE
    5933             :                     ? "at least"
    5934             :                     : "likely at least (probably more)");
    5935           2 :             return nullptr;
    5936             :         }
    5937             :     }
    5938             : 
    5939             :     /* -------------------------------------------------------------------- */
    5940             :     /*      Check if the user wishes a particular endianness                */
    5941             :     /* -------------------------------------------------------------------- */
    5942             : 
    5943        9893 :     int eEndianness = ENDIANNESS_NATIVE;
    5944        9893 :     const char *pszEndianness = CSLFetchNameValue(papszParamList, "ENDIANNESS");
    5945        9893 :     if (pszEndianness == nullptr)
    5946        9830 :         pszEndianness = CPLGetConfigOption("GDAL_TIFF_ENDIANNESS", nullptr);
    5947        9893 :     if (pszEndianness != nullptr)
    5948             :     {
    5949         123 :         if (EQUAL(pszEndianness, "LITTLE"))
    5950             :         {
    5951          36 :             eEndianness = ENDIANNESS_LITTLE;
    5952             :         }
    5953          87 :         else if (EQUAL(pszEndianness, "BIG"))
    5954             :         {
    5955           1 :             eEndianness = ENDIANNESS_BIG;
    5956             :         }
    5957          86 :         else if (EQUAL(pszEndianness, "INVERTED"))
    5958             :         {
    5959             : #ifdef CPL_LSB
    5960          82 :             eEndianness = ENDIANNESS_BIG;
    5961             : #else
    5962             :             eEndianness = ENDIANNESS_LITTLE;
    5963             : #endif
    5964             :         }
    5965           4 :         else if (!EQUAL(pszEndianness, "NATIVE"))
    5966             :         {
    5967           1 :             ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
    5968             :                         "ENDIANNESS=%s not supported. Defaulting to NATIVE",
    5969             :                         pszEndianness);
    5970             :         }
    5971             :     }
    5972             : 
    5973             :     /* -------------------------------------------------------------------- */
    5974             :     /*      Try opening the dataset.                                        */
    5975             :     /* -------------------------------------------------------------------- */
    5976             : 
    5977             :     const bool bAppend =
    5978        9893 :         CPLFetchBool(papszParamList, "APPEND_SUBDATASET", false);
    5979             : 
    5980        9893 :     char szOpeningFlag[5] = {};
    5981        9893 :     strcpy(szOpeningFlag, bAppend ? "r+" : "w+");
    5982        9893 :     if (bCreateBigTIFF)
    5983          32 :         strcat(szOpeningFlag, "8");
    5984        9893 :     if (eEndianness == ENDIANNESS_BIG)
    5985          83 :         strcat(szOpeningFlag, "b");
    5986        9810 :     else if (eEndianness == ENDIANNESS_LITTLE)
    5987          36 :         strcat(szOpeningFlag, "l");
    5988             : 
    5989        9893 :     VSIErrorReset();
    5990        9893 :     const bool bOnlyVisibleAtCloseTime = CPLTestBool(CSLFetchNameValueDef(
    5991             :         papszParamList, "@CREATE_ONLY_VISIBLE_AT_CLOSE_TIME", "NO"));
    5992        9893 :     const bool bSuppressASAP = CPLTestBool(
    5993             :         CSLFetchNameValueDef(papszParamList, "@SUPPRESS_ASAP", "NO"));
    5994             :     auto l_fpL =
    5995        9893 :         (bOnlyVisibleAtCloseTime || bSuppressASAP) && !bAppend
    5996        9963 :             ? VSIFileManager::GetHandler(pszFilename)
    5997         140 :                   ->CreateOnlyVisibleAtCloseTime(pszFilename, true, nullptr)
    5998          70 :                   .release()
    5999       19716 :             : VSIFilesystemHandler::OpenStatic(pszFilename,
    6000             :                                                bAppend ? "r+b" : "w+b", true)
    6001        9893 :                   .release();
    6002        9893 :     if (l_fpL == nullptr)
    6003             :     {
    6004          21 :         VSIToCPLErrorWithMsg(CE_Failure, CPLE_OpenFailed,
    6005          42 :                              std::string("Attempt to create new tiff file `")
    6006          21 :                                  .append(pszFilename)
    6007          21 :                                  .append("' failed")
    6008             :                                  .c_str());
    6009          21 :         return nullptr;
    6010             :     }
    6011             : 
    6012        9872 :     if (bSuppressASAP)
    6013             :     {
    6014          40 :         l_fpL->CancelCreation();
    6015             :     }
    6016             : 
    6017        9872 :     TIFF *l_hTIFF = VSI_TIFFOpen(pszFilename, szOpeningFlag, l_fpL);
    6018        9872 :     if (l_hTIFF == nullptr)
    6019             :     {
    6020           2 :         if (CPLGetLastErrorNo() == 0)
    6021           0 :             CPLError(CE_Failure, CPLE_OpenFailed,
    6022             :                      "Attempt to create new tiff file `%s' "
    6023             :                      "failed in XTIFFOpen().",
    6024             :                      pszFilename);
    6025           2 :         l_fpL->CancelCreation();
    6026           2 :         CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    6027           2 :         return nullptr;
    6028             :     }
    6029             : 
    6030        9870 :     if (bAppend)
    6031             :     {
    6032             : #if !(defined(INTERNAL_LIBTIFF) || TIFFLIB_VERSION > 20240911)
    6033             :         // This is a bit of a hack to cause (*tif->tif_cleanup)(tif); to be
    6034             :         // called. See https://trac.osgeo.org/gdal/ticket/2055
    6035             :         // Fixed in libtiff > 4.7.0
    6036             :         TIFFSetField(l_hTIFF, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
    6037             :         TIFFFreeDirectory(l_hTIFF);
    6038             : #endif
    6039           6 :         TIFFCreateDirectory(l_hTIFF);
    6040             :     }
    6041             : 
    6042             :     /* -------------------------------------------------------------------- */
    6043             :     /*      Do we have a custom pixel type (just used for signed byte now). */
    6044             :     /* -------------------------------------------------------------------- */
    6045        9870 :     const char *pszPixelType = CSLFetchNameValue(papszParamList, "PIXELTYPE");
    6046        9870 :     if (pszPixelType == nullptr)
    6047        9862 :         pszPixelType = "";
    6048        9870 :     if (eType == GDT_UInt8 && EQUAL(pszPixelType, "SIGNEDBYTE"))
    6049             :     {
    6050           8 :         CPLError(CE_Warning, CPLE_AppDefined,
    6051             :                  "Using PIXELTYPE=SIGNEDBYTE with Byte data type is deprecated "
    6052             :                  "(but still works). "
    6053             :                  "Using Int8 data type instead is now recommended.");
    6054             :     }
    6055             : 
    6056             :     /* -------------------------------------------------------------------- */
    6057             :     /*      Setup some standard flags.                                      */
    6058             :     /* -------------------------------------------------------------------- */
    6059        9870 :     TIFFSetField(l_hTIFF, TIFFTAG_IMAGEWIDTH, nXSize);
    6060        9870 :     TIFFSetField(l_hTIFF, TIFFTAG_IMAGELENGTH, nYSize);
    6061        9870 :     TIFFSetField(l_hTIFF, TIFFTAG_BITSPERSAMPLE, l_nBitsPerSample);
    6062             : 
    6063        9870 :     uint16_t l_nSampleFormat = 0;
    6064        9870 :     if ((eType == GDT_UInt8 && EQUAL(pszPixelType, "SIGNEDBYTE")) ||
    6065        9721 :         eType == GDT_Int8 || eType == GDT_Int16 || eType == GDT_Int32 ||
    6066             :         eType == GDT_Int64)
    6067         808 :         l_nSampleFormat = SAMPLEFORMAT_INT;
    6068        9062 :     else if (eType == GDT_CInt16 || eType == GDT_CInt32)
    6069         363 :         l_nSampleFormat = SAMPLEFORMAT_COMPLEXINT;
    6070        8699 :     else if (eType == GDT_Float16 || eType == GDT_Float32 ||
    6071             :              eType == GDT_Float64)
    6072        1155 :         l_nSampleFormat = SAMPLEFORMAT_IEEEFP;
    6073        7544 :     else if (eType == GDT_CFloat16 || eType == GDT_CFloat32 ||
    6074             :              eType == GDT_CFloat64)
    6075         471 :         l_nSampleFormat = SAMPLEFORMAT_COMPLEXIEEEFP;
    6076             :     else
    6077        7073 :         l_nSampleFormat = SAMPLEFORMAT_UINT;
    6078             : 
    6079        9870 :     TIFFSetField(l_hTIFF, TIFFTAG_SAMPLEFORMAT, l_nSampleFormat);
    6080        9870 :     TIFFSetField(l_hTIFF, TIFFTAG_SAMPLESPERPIXEL, l_nBands);
    6081        9870 :     TIFFSetField(l_hTIFF, TIFFTAG_PLANARCONFIG, nPlanar);
    6082             : 
    6083             :     /* -------------------------------------------------------------------- */
    6084             :     /*      Setup Photometric Interpretation. Take this value from the user */
    6085             :     /*      passed option or guess correct value otherwise.                 */
    6086             :     /* -------------------------------------------------------------------- */
    6087        9870 :     int nSamplesAccountedFor = 1;
    6088        9870 :     bool bForceColorTable = false;
    6089             : 
    6090        9870 :     if (const char *pszValue = CSLFetchNameValue(papszParamList, "PHOTOMETRIC"))
    6091             :     {
    6092        1911 :         if (EQUAL(pszValue, "MINISBLACK"))
    6093          14 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    6094        1897 :         else if (EQUAL(pszValue, "MINISWHITE"))
    6095             :         {
    6096           2 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
    6097             :         }
    6098        1895 :         else if (EQUAL(pszValue, "PALETTE"))
    6099             :         {
    6100           5 :             if (eType == GDT_UInt8 || eType == GDT_UInt16)
    6101             :             {
    6102           4 :                 TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
    6103           4 :                 nSamplesAccountedFor = 1;
    6104           4 :                 bForceColorTable = true;
    6105             :             }
    6106             :             else
    6107             :             {
    6108           1 :                 ReportError(
    6109             :                     pszFilename, CE_Warning, CPLE_AppDefined,
    6110             :                     "PHOTOMETRIC=PALETTE only compatible with Byte or UInt16");
    6111             :             }
    6112             :         }
    6113        1890 :         else if (EQUAL(pszValue, "RGB"))
    6114             :         {
    6115        1150 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
    6116        1150 :             nSamplesAccountedFor = 3;
    6117             :         }
    6118         740 :         else if (EQUAL(pszValue, "CMYK"))
    6119             :         {
    6120          10 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_SEPARATED);
    6121          10 :             nSamplesAccountedFor = 4;
    6122             :         }
    6123         730 :         else if (EQUAL(pszValue, "YCBCR"))
    6124             :         {
    6125             :             // Because of subsampling, setting YCBCR without JPEG compression
    6126             :             // leads to a crash currently. Would need to make
    6127             :             // GTiffRasterBand::IWriteBlock() aware of subsampling so that it
    6128             :             // doesn't overrun buffer size returned by libtiff.
    6129         729 :             if (l_nCompression != COMPRESSION_JPEG)
    6130             :             {
    6131           1 :                 ReportError(
    6132             :                     pszFilename, CE_Failure, CPLE_NotSupported,
    6133             :                     "Currently, PHOTOMETRIC=YCBCR requires COMPRESS=JPEG");
    6134           1 :                 XTIFFClose(l_hTIFF);
    6135           1 :                 l_fpL->CancelCreation();
    6136           1 :                 CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    6137           1 :                 return nullptr;
    6138             :             }
    6139             : 
    6140         728 :             if (nPlanar == PLANARCONFIG_SEPARATE)
    6141             :             {
    6142           1 :                 ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    6143             :                             "PHOTOMETRIC=YCBCR requires INTERLEAVE=PIXEL");
    6144           1 :                 XTIFFClose(l_hTIFF);
    6145           1 :                 l_fpL->CancelCreation();
    6146           1 :                 CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    6147           1 :                 return nullptr;
    6148             :             }
    6149             : 
    6150             :             // YCBCR strictly requires 3 bands. Not less, not more Issue an
    6151             :             // explicit error message as libtiff one is a bit cryptic:
    6152             :             // TIFFVStripSize64:Invalid td_samplesperpixel value.
    6153         727 :             if (l_nBands != 3)
    6154             :             {
    6155           1 :                 ReportError(
    6156             :                     pszFilename, CE_Failure, CPLE_NotSupported,
    6157             :                     "PHOTOMETRIC=YCBCR not supported on a %d-band raster: "
    6158             :                     "only compatible of a 3-band (RGB) raster",
    6159             :                     l_nBands);
    6160           1 :                 XTIFFClose(l_hTIFF);
    6161           1 :                 l_fpL->CancelCreation();
    6162           1 :                 CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    6163           1 :                 return nullptr;
    6164             :             }
    6165             : 
    6166         726 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);
    6167         726 :             nSamplesAccountedFor = 3;
    6168             : 
    6169             :             // Explicitly register the subsampling so that JPEGFixupTags
    6170             :             // is a no-op (helps for cloud optimized geotiffs)
    6171         726 :             TIFFSetField(l_hTIFF, TIFFTAG_YCBCRSUBSAMPLING, 2, 2);
    6172             :         }
    6173           1 :         else if (EQUAL(pszValue, "CIELAB"))
    6174             :         {
    6175           0 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_CIELAB);
    6176           0 :             nSamplesAccountedFor = 3;
    6177             :         }
    6178           1 :         else if (EQUAL(pszValue, "ICCLAB"))
    6179             :         {
    6180           0 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_ICCLAB);
    6181           0 :             nSamplesAccountedFor = 3;
    6182             :         }
    6183           1 :         else if (EQUAL(pszValue, "ITULAB"))
    6184             :         {
    6185           0 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_ITULAB);
    6186           0 :             nSamplesAccountedFor = 3;
    6187             :         }
    6188             :         else
    6189             :         {
    6190           1 :             ReportError(pszFilename, CE_Warning, CPLE_IllegalArg,
    6191             :                         "PHOTOMETRIC=%s value not recognised, ignoring.  "
    6192             :                         "Set the Photometric Interpretation as MINISBLACK.",
    6193             :                         pszValue);
    6194           1 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    6195             :         }
    6196             : 
    6197        1908 :         if (l_nBands < nSamplesAccountedFor)
    6198             :         {
    6199           1 :             ReportError(pszFilename, CE_Warning, CPLE_IllegalArg,
    6200             :                         "PHOTOMETRIC=%s value does not correspond to number "
    6201             :                         "of bands (%d), ignoring.  "
    6202             :                         "Set the Photometric Interpretation as MINISBLACK.",
    6203             :                         pszValue, l_nBands);
    6204           1 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    6205             :         }
    6206             :     }
    6207             :     else
    6208             :     {
    6209             :         // If image contains 3 or 4 bands and datatype is Byte then we will
    6210             :         // assume it is RGB. In all other cases assume it is MINISBLACK.
    6211        7959 :         if (l_nBands == 3 && eType == GDT_UInt8)
    6212             :         {
    6213         322 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
    6214         322 :             nSamplesAccountedFor = 3;
    6215             :         }
    6216        7637 :         else if (l_nBands == 4 && eType == GDT_UInt8)
    6217             :         {
    6218             :             uint16_t v[1] = {
    6219         719 :                 GTiffGetAlphaValue(CSLFetchNameValue(papszParamList, "ALPHA"),
    6220         719 :                                    DEFAULT_ALPHA_TYPE)};
    6221             : 
    6222         719 :             TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, 1, v);
    6223         719 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
    6224         719 :             nSamplesAccountedFor = 4;
    6225             :         }
    6226             :         else
    6227             :         {
    6228        6918 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    6229        6918 :             nSamplesAccountedFor = 1;
    6230             :         }
    6231             :     }
    6232             : 
    6233             :     /* -------------------------------------------------------------------- */
    6234             :     /*      If there are extra samples, we need to mark them with an        */
    6235             :     /*      appropriate extrasamples definition here.                       */
    6236             :     /* -------------------------------------------------------------------- */
    6237        9867 :     if (l_nBands > nSamplesAccountedFor)
    6238             :     {
    6239        1385 :         const int nExtraSamples = l_nBands - nSamplesAccountedFor;
    6240             : 
    6241             :         uint16_t *v = static_cast<uint16_t *>(
    6242        1385 :             CPLMalloc(sizeof(uint16_t) * nExtraSamples));
    6243             : 
    6244        1385 :         v[0] = GTiffGetAlphaValue(CSLFetchNameValue(papszParamList, "ALPHA"),
    6245             :                                   EXTRASAMPLE_UNSPECIFIED);
    6246             : 
    6247      297696 :         for (int i = 1; i < nExtraSamples; ++i)
    6248      296311 :             v[i] = EXTRASAMPLE_UNSPECIFIED;
    6249             : 
    6250        1385 :         TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, nExtraSamples, v);
    6251             : 
    6252        1385 :         CPLFree(v);
    6253             :     }
    6254             : 
    6255             :     // Set the ICC color profile.
    6256        9867 :     if (eProfile != GTiffProfile::BASELINE)
    6257             :     {
    6258        9842 :         SaveICCProfile(nullptr, l_hTIFF, papszParamList, l_nBitsPerSample);
    6259             :     }
    6260             : 
    6261             :     // Set the compression method before asking the default strip size
    6262             :     // This is useful when translating to a JPEG-In-TIFF file where
    6263             :     // the default strip size is 8 or 16 depending on the photometric value.
    6264        9867 :     TIFFSetField(l_hTIFF, TIFFTAG_COMPRESSION, l_nCompression);
    6265             : 
    6266        9867 :     if (l_nCompression == COMPRESSION_LERC)
    6267             :     {
    6268             :         const char *pszCompress =
    6269          97 :             CSLFetchNameValueDef(papszParamList, "COMPRESS", "");
    6270          97 :         if (EQUAL(pszCompress, "LERC_DEFLATE"))
    6271             :         {
    6272          16 :             TIFFSetField(l_hTIFF, TIFFTAG_LERC_ADD_COMPRESSION,
    6273             :                          LERC_ADD_COMPRESSION_DEFLATE);
    6274             :         }
    6275          81 :         else if (EQUAL(pszCompress, "LERC_ZSTD"))
    6276             :         {
    6277          14 :             if (TIFFSetField(l_hTIFF, TIFFTAG_LERC_ADD_COMPRESSION,
    6278          14 :                              LERC_ADD_COMPRESSION_ZSTD) != 1)
    6279             :             {
    6280           0 :                 XTIFFClose(l_hTIFF);
    6281           0 :                 l_fpL->CancelCreation();
    6282           0 :                 CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    6283           0 :                 return nullptr;
    6284             :             }
    6285             :         }
    6286             :     }
    6287             :     // TODO later: take into account LERC version
    6288             : 
    6289             :     /* -------------------------------------------------------------------- */
    6290             :     /*      Setup tiling/stripping flags.                                   */
    6291             :     /* -------------------------------------------------------------------- */
    6292        9867 :     if (bTiled)
    6293             :     {
    6294        1598 :         if (!TIFFSetField(l_hTIFF, TIFFTAG_TILEWIDTH, l_nBlockXSize) ||
    6295         799 :             !TIFFSetField(l_hTIFF, TIFFTAG_TILELENGTH, l_nBlockYSize))
    6296             :         {
    6297           0 :             XTIFFClose(l_hTIFF);
    6298           0 :             l_fpL->CancelCreation();
    6299           0 :             CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    6300           0 :             return nullptr;
    6301             :         }
    6302             :     }
    6303             :     else
    6304             :     {
    6305        9068 :         const uint32_t l_nRowsPerStrip = std::min(
    6306             :             nYSize, l_nBlockYSize == 0
    6307        9068 :                         ? static_cast<int>(TIFFDefaultStripSize(l_hTIFF, 0))
    6308        9068 :                         : l_nBlockYSize);
    6309             : 
    6310        9068 :         TIFFSetField(l_hTIFF, TIFFTAG_ROWSPERSTRIP, l_nRowsPerStrip);
    6311             :     }
    6312             : 
    6313             :     /* -------------------------------------------------------------------- */
    6314             :     /*      Set compression related tags.                                   */
    6315             :     /* -------------------------------------------------------------------- */
    6316        9867 :     if (GTIFFSupportsPredictor(l_nCompression))
    6317         965 :         TIFFSetField(l_hTIFF, TIFFTAG_PREDICTOR, nPredictor);
    6318        9867 :     if (l_nCompression == COMPRESSION_ADOBE_DEFLATE ||
    6319             :         l_nCompression == COMPRESSION_LERC)
    6320             :     {
    6321         280 :         GTiffSetDeflateSubCodec(l_hTIFF);
    6322             : 
    6323         280 :         if (l_nZLevel != -1)
    6324          22 :             TIFFSetField(l_hTIFF, TIFFTAG_ZIPQUALITY, l_nZLevel);
    6325             :     }
    6326        9867 :     if (l_nCompression == COMPRESSION_JPEG && l_nJpegQuality != -1)
    6327        1905 :         TIFFSetField(l_hTIFF, TIFFTAG_JPEGQUALITY, l_nJpegQuality);
    6328        9867 :     if (l_nCompression == COMPRESSION_LZMA && l_nLZMAPreset != -1)
    6329          10 :         TIFFSetField(l_hTIFF, TIFFTAG_LZMAPRESET, l_nLZMAPreset);
    6330        9867 :     if ((l_nCompression == COMPRESSION_ZSTD ||
    6331         192 :          l_nCompression == COMPRESSION_LERC) &&
    6332             :         l_nZSTDLevel != -1)
    6333          12 :         TIFFSetField(l_hTIFF, TIFFTAG_ZSTD_LEVEL, l_nZSTDLevel);
    6334        9867 :     if (l_nCompression == COMPRESSION_LERC)
    6335             :     {
    6336          97 :         TIFFSetField(l_hTIFF, TIFFTAG_LERC_MAXZERROR, l_dfMaxZError);
    6337             :     }
    6338             : #if HAVE_JXL
    6339        9867 :     if (l_nCompression == COMPRESSION_JXL ||
    6340             :         l_nCompression == COMPRESSION_JXL_DNG_1_7)
    6341             :     {
    6342         104 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_LOSSYNESS,
    6343             :                      l_bJXLLossless ? JXL_LOSSLESS : JXL_LOSSY);
    6344         104 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_EFFORT, l_nJXLEffort);
    6345         104 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_DISTANCE,
    6346             :                      static_cast<double>(l_fJXLDistance));
    6347         104 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_ALPHA_DISTANCE,
    6348             :                      static_cast<double>(l_fJXLAlphaDistance));
    6349             :     }
    6350             : #endif
    6351        9867 :     if (l_nCompression == COMPRESSION_WEBP)
    6352          33 :         TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LEVEL, l_nWebPLevel);
    6353        9867 :     if (l_nCompression == COMPRESSION_WEBP && l_bWebPLossless)
    6354           7 :         TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LOSSLESS, 1);
    6355             : 
    6356        9867 :     if (l_nCompression == COMPRESSION_JPEG)
    6357        2083 :         TIFFSetField(l_hTIFF, TIFFTAG_JPEGTABLESMODE, l_nJpegTablesMode);
    6358             : 
    6359             :     /* -------------------------------------------------------------------- */
    6360             :     /*      If we forced production of a file with photometric=palette,     */
    6361             :     /*      we need to push out a default color table.                      */
    6362             :     /* -------------------------------------------------------------------- */
    6363        9867 :     if (bForceColorTable)
    6364             :     {
    6365           4 :         const int nColors = eType == GDT_UInt8 ? 256 : 65536;
    6366             : 
    6367             :         unsigned short *panTRed = static_cast<unsigned short *>(
    6368           4 :             CPLMalloc(sizeof(unsigned short) * nColors));
    6369             :         unsigned short *panTGreen = static_cast<unsigned short *>(
    6370           4 :             CPLMalloc(sizeof(unsigned short) * nColors));
    6371             :         unsigned short *panTBlue = static_cast<unsigned short *>(
    6372           4 :             CPLMalloc(sizeof(unsigned short) * nColors));
    6373             : 
    6374        1028 :         for (int iColor = 0; iColor < nColors; ++iColor)
    6375             :         {
    6376        1024 :             if (eType == GDT_UInt8)
    6377             :             {
    6378        1024 :                 panTRed[iColor] = GTiffDataset::ClampCTEntry(
    6379             :                     iColor, 1, iColor, nColorTableMultiplier);
    6380        1024 :                 panTGreen[iColor] = GTiffDataset::ClampCTEntry(
    6381             :                     iColor, 2, iColor, nColorTableMultiplier);
    6382        1024 :                 panTBlue[iColor] = GTiffDataset::ClampCTEntry(
    6383             :                     iColor, 3, iColor, nColorTableMultiplier);
    6384             :             }
    6385             :             else
    6386             :             {
    6387           0 :                 panTRed[iColor] = static_cast<unsigned short>(iColor);
    6388           0 :                 panTGreen[iColor] = static_cast<unsigned short>(iColor);
    6389           0 :                 panTBlue[iColor] = static_cast<unsigned short>(iColor);
    6390             :             }
    6391             :         }
    6392             : 
    6393           4 :         TIFFSetField(l_hTIFF, TIFFTAG_COLORMAP, panTRed, panTGreen, panTBlue);
    6394             : 
    6395           4 :         CPLFree(panTRed);
    6396           4 :         CPLFree(panTGreen);
    6397           4 :         CPLFree(panTBlue);
    6398             :     }
    6399             : 
    6400             :     // This trick
    6401             :     // creates a temporary in-memory file and fetches its JPEG tables so that
    6402             :     // we can directly set them, before tif_jpeg.c compute them at the first
    6403             :     // strip/tile writing, which is too late, since we have already crystalized
    6404             :     // the directory. This way we avoid a directory rewriting.
    6405       11950 :     if (l_nCompression == COMPRESSION_JPEG &&
    6406        2083 :         CPLTestBool(
    6407             :             CSLFetchNameValueDef(papszParamList, "WRITE_JPEGTABLE_TAG", "YES")))
    6408             :     {
    6409        1014 :         GTiffWriteJPEGTables(
    6410             :             l_hTIFF, CSLFetchNameValue(papszParamList, "PHOTOMETRIC"),
    6411             :             CSLFetchNameValue(papszParamList, "JPEG_QUALITY"),
    6412             :             CSLFetchNameValue(papszParamList, "JPEGTABLESMODE"));
    6413             :     }
    6414             : 
    6415        9867 :     *pfpL = l_fpL;
    6416             : 
    6417        9867 :     return l_hTIFF;
    6418             : }
    6419             : 
    6420             : /************************************************************************/
    6421             : /*                            GuessJPEGQuality()                        */
    6422             : /*                                                                      */
    6423             : /*      Guess JPEG quality from JPEGTABLES tag.                         */
    6424             : /************************************************************************/
    6425             : 
    6426        3850 : static const GByte *GTIFFFindNextTable(const GByte *paby, GByte byMarker,
    6427             :                                        int nLen, int *pnLenTable)
    6428             : {
    6429        7967 :     for (int i = 0; i + 1 < nLen;)
    6430             :     {
    6431        7967 :         if (paby[i] != 0xFF)
    6432           0 :             return nullptr;
    6433        7967 :         ++i;
    6434        7967 :         if (paby[i] == 0xD8)
    6435             :         {
    6436        3117 :             ++i;
    6437        3117 :             continue;
    6438             :         }
    6439        4850 :         if (i + 2 >= nLen)
    6440         833 :             return nullptr;
    6441        4017 :         int nMarkerLen = paby[i + 1] * 256 + paby[i + 2];
    6442        4017 :         if (i + 1 + nMarkerLen >= nLen)
    6443           0 :             return nullptr;
    6444        4017 :         if (paby[i] == byMarker)
    6445             :         {
    6446        3017 :             if (pnLenTable)
    6447        2473 :                 *pnLenTable = nMarkerLen;
    6448        3017 :             return paby + i + 1;
    6449             :         }
    6450        1000 :         i += 1 + nMarkerLen;
    6451             :     }
    6452           0 :     return nullptr;
    6453             : }
    6454             : 
    6455             : constexpr GByte MARKER_HUFFMAN_TABLE = 0xC4;
    6456             : constexpr GByte MARKER_QUANT_TABLE = 0xDB;
    6457             : 
    6458             : // We assume that if there are several quantization tables, they are
    6459             : // in the same order. Which is a reasonable assumption for updating
    6460             : // a file generated by ourselves.
    6461         904 : static bool GTIFFQuantizationTablesEqual(const GByte *paby1, int nLen1,
    6462             :                                          const GByte *paby2, int nLen2)
    6463             : {
    6464         904 :     bool bFound = false;
    6465             :     while (true)
    6466             :     {
    6467         945 :         int nLenTable1 = 0;
    6468         945 :         int nLenTable2 = 0;
    6469             :         const GByte *paby1New =
    6470         945 :             GTIFFFindNextTable(paby1, MARKER_QUANT_TABLE, nLen1, &nLenTable1);
    6471             :         const GByte *paby2New =
    6472         945 :             GTIFFFindNextTable(paby2, MARKER_QUANT_TABLE, nLen2, &nLenTable2);
    6473         945 :         if (paby1New == nullptr && paby2New == nullptr)
    6474         904 :             return bFound;
    6475         911 :         if (paby1New == nullptr || paby2New == nullptr)
    6476           0 :             return false;
    6477         911 :         if (nLenTable1 != nLenTable2)
    6478         207 :             return false;
    6479         704 :         if (memcmp(paby1New, paby2New, nLenTable1) != 0)
    6480         663 :             return false;
    6481          41 :         paby1New += nLenTable1;
    6482          41 :         paby2New += nLenTable2;
    6483          41 :         nLen1 -= static_cast<int>(paby1New - paby1);
    6484          41 :         nLen2 -= static_cast<int>(paby2New - paby2);
    6485          41 :         paby1 = paby1New;
    6486          41 :         paby2 = paby2New;
    6487          41 :         bFound = true;
    6488          41 :     }
    6489             : }
    6490             : 
    6491             : // Guess the JPEG quality by comparing against the MD5Sum of precomputed
    6492             : // quantization tables
    6493         409 : static int GuessJPEGQualityFromMD5(const uint8_t md5JPEGQuantTable[][16],
    6494             :                                    const GByte *const pabyJPEGTable,
    6495             :                                    int nJPEGTableSize)
    6496             : {
    6497         409 :     int nRemainingLen = nJPEGTableSize;
    6498         409 :     const GByte *pabyCur = pabyJPEGTable;
    6499             : 
    6500             :     struct CPLMD5Context context;
    6501         409 :     CPLMD5Init(&context);
    6502             : 
    6503             :     while (true)
    6504             :     {
    6505        1060 :         int nLenTable = 0;
    6506        1060 :         const GByte *pabyNew = GTIFFFindNextTable(pabyCur, MARKER_QUANT_TABLE,
    6507             :                                                   nRemainingLen, &nLenTable);
    6508        1060 :         if (pabyNew == nullptr)
    6509         409 :             break;
    6510         651 :         CPLMD5Update(&context, pabyNew, nLenTable);
    6511         651 :         pabyNew += nLenTable;
    6512         651 :         nRemainingLen -= static_cast<int>(pabyNew - pabyCur);
    6513         651 :         pabyCur = pabyNew;
    6514         651 :     }
    6515             : 
    6516             :     GByte digest[16];
    6517         409 :     CPLMD5Final(digest, &context);
    6518             : 
    6519       28846 :     for (int i = 0; i < 100; i++)
    6520             :     {
    6521       28843 :         if (memcmp(md5JPEGQuantTable[i], digest, 16) == 0)
    6522             :         {
    6523         406 :             return i + 1;
    6524             :         }
    6525             :     }
    6526           3 :     return -1;
    6527             : }
    6528             : 
    6529         464 : int GTiffDataset::GuessJPEGQuality(bool &bOutHasQuantizationTable,
    6530             :                                    bool &bOutHasHuffmanTable)
    6531             : {
    6532         464 :     CPLAssert(m_nCompression == COMPRESSION_JPEG);
    6533         464 :     uint32_t nJPEGTableSize = 0;
    6534         464 :     void *pJPEGTable = nullptr;
    6535         464 :     if (!TIFFGetField(m_hTIFF, TIFFTAG_JPEGTABLES, &nJPEGTableSize,
    6536             :                       &pJPEGTable))
    6537             :     {
    6538          14 :         bOutHasQuantizationTable = false;
    6539          14 :         bOutHasHuffmanTable = false;
    6540          14 :         return -1;
    6541             :     }
    6542             : 
    6543         450 :     bOutHasQuantizationTable =
    6544         450 :         GTIFFFindNextTable(static_cast<const GByte *>(pJPEGTable),
    6545             :                            MARKER_QUANT_TABLE, nJPEGTableSize,
    6546         450 :                            nullptr) != nullptr;
    6547         450 :     bOutHasHuffmanTable =
    6548         450 :         GTIFFFindNextTable(static_cast<const GByte *>(pJPEGTable),
    6549             :                            MARKER_HUFFMAN_TABLE, nJPEGTableSize,
    6550         450 :                            nullptr) != nullptr;
    6551         450 :     if (!bOutHasQuantizationTable)
    6552           7 :         return -1;
    6553             : 
    6554         443 :     if ((nBands == 1 && m_nBitsPerSample == 8) ||
    6555         382 :         (nBands == 3 && m_nBitsPerSample == 8 &&
    6556         336 :          m_nPhotometric == PHOTOMETRIC_RGB) ||
    6557         288 :         (nBands == 4 && m_nBitsPerSample == 8 &&
    6558          27 :          m_nPhotometric == PHOTOMETRIC_SEPARATED))
    6559             :     {
    6560         167 :         return GuessJPEGQualityFromMD5(md5JPEGQuantTable_generic_8bit,
    6561             :                                        static_cast<const GByte *>(pJPEGTable),
    6562         167 :                                        static_cast<int>(nJPEGTableSize));
    6563             :     }
    6564             : 
    6565         276 :     if (nBands == 3 && m_nBitsPerSample == 8 &&
    6566         242 :         m_nPhotometric == PHOTOMETRIC_YCBCR)
    6567             :     {
    6568             :         int nRet =
    6569         242 :             GuessJPEGQualityFromMD5(md5JPEGQuantTable_3_YCBCR_8bit,
    6570             :                                     static_cast<const GByte *>(pJPEGTable),
    6571             :                                     static_cast<int>(nJPEGTableSize));
    6572         242 :         if (nRet < 0)
    6573             :         {
    6574             :             // libjpeg 9e has modified the YCbCr quantization tables.
    6575             :             nRet =
    6576           0 :                 GuessJPEGQualityFromMD5(md5JPEGQuantTable_3_YCBCR_8bit_jpeg9e,
    6577             :                                         static_cast<const GByte *>(pJPEGTable),
    6578             :                                         static_cast<int>(nJPEGTableSize));
    6579             :         }
    6580         242 :         return nRet;
    6581             :     }
    6582             : 
    6583          34 :     char **papszLocalParameters = nullptr;
    6584             :     papszLocalParameters =
    6585          34 :         CSLSetNameValue(papszLocalParameters, "COMPRESS", "JPEG");
    6586          34 :     if (m_nPhotometric == PHOTOMETRIC_YCBCR)
    6587             :         papszLocalParameters =
    6588           7 :             CSLSetNameValue(papszLocalParameters, "PHOTOMETRIC", "YCBCR");
    6589          27 :     else if (m_nPhotometric == PHOTOMETRIC_SEPARATED)
    6590             :         papszLocalParameters =
    6591           0 :             CSLSetNameValue(papszLocalParameters, "PHOTOMETRIC", "CMYK");
    6592             :     papszLocalParameters =
    6593          34 :         CSLSetNameValue(papszLocalParameters, "BLOCKYSIZE", "16");
    6594          34 :     if (m_nBitsPerSample == 12)
    6595             :         papszLocalParameters =
    6596          16 :             CSLSetNameValue(papszLocalParameters, "NBITS", "12");
    6597             : 
    6598             :     const CPLString osTmpFilenameIn(
    6599          34 :         VSIMemGenerateHiddenFilename("gtiffdataset_guess_jpeg_quality_tmp"));
    6600             : 
    6601          34 :     int nRet = -1;
    6602         938 :     for (int nQuality = 0; nQuality <= 100 && nRet < 0; ++nQuality)
    6603             :     {
    6604         904 :         VSILFILE *fpTmp = nullptr;
    6605         904 :         if (nQuality == 0)
    6606             :             papszLocalParameters =
    6607          34 :                 CSLSetNameValue(papszLocalParameters, "JPEG_QUALITY", "75");
    6608             :         else
    6609             :             papszLocalParameters =
    6610         870 :                 CSLSetNameValue(papszLocalParameters, "JPEG_QUALITY",
    6611             :                                 CPLSPrintf("%d", nQuality));
    6612             : 
    6613         904 :         CPLPushErrorHandler(CPLQuietErrorHandler);
    6614         904 :         CPLString osTmp;
    6615             :         bool bTileInterleaving;
    6616        1808 :         TIFF *hTIFFTmp = CreateLL(
    6617         904 :             osTmpFilenameIn, 16, 16, (nBands <= 4) ? nBands : 1,
    6618             :             GetRasterBand(1)->GetRasterDataType(), 0.0, 0, papszLocalParameters,
    6619             :             &fpTmp, osTmp, /* bCreateCopy=*/false, bTileInterleaving);
    6620         904 :         CPLPopErrorHandler();
    6621         904 :         if (!hTIFFTmp)
    6622             :         {
    6623           0 :             break;
    6624             :         }
    6625             : 
    6626         904 :         TIFFWriteCheck(hTIFFTmp, FALSE, "CreateLL");
    6627         904 :         TIFFWriteDirectory(hTIFFTmp);
    6628         904 :         TIFFSetDirectory(hTIFFTmp, 0);
    6629             :         // Now reset jpegcolormode.
    6630        1196 :         if (m_nPhotometric == PHOTOMETRIC_YCBCR &&
    6631         292 :             CPLTestBool(CPLGetConfigOption("CONVERT_YCBCR_TO_RGB", "YES")))
    6632             :         {
    6633         292 :             TIFFSetField(hTIFFTmp, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
    6634             :         }
    6635             : 
    6636         904 :         GByte abyZeroData[(16 * 16 * 4 * 3) / 2] = {};
    6637         904 :         const int nBlockSize =
    6638         904 :             (16 * 16 * ((nBands <= 4) ? nBands : 1) * m_nBitsPerSample) / 8;
    6639         904 :         TIFFWriteEncodedStrip(hTIFFTmp, 0, abyZeroData, nBlockSize);
    6640             : 
    6641         904 :         uint32_t nJPEGTableSizeTry = 0;
    6642         904 :         void *pJPEGTableTry = nullptr;
    6643         904 :         if (TIFFGetField(hTIFFTmp, TIFFTAG_JPEGTABLES, &nJPEGTableSizeTry,
    6644         904 :                          &pJPEGTableTry))
    6645             :         {
    6646         904 :             if (GTIFFQuantizationTablesEqual(
    6647             :                     static_cast<GByte *>(pJPEGTable), nJPEGTableSize,
    6648             :                     static_cast<GByte *>(pJPEGTableTry), nJPEGTableSizeTry))
    6649             :             {
    6650          34 :                 nRet = (nQuality == 0) ? 75 : nQuality;
    6651             :             }
    6652             :         }
    6653             : 
    6654         904 :         XTIFFClose(hTIFFTmp);
    6655         904 :         CPL_IGNORE_RET_VAL(VSIFCloseL(fpTmp));
    6656             :     }
    6657             : 
    6658          34 :     CSLDestroy(papszLocalParameters);
    6659          34 :     VSIUnlink(osTmpFilenameIn);
    6660             : 
    6661          34 :     return nRet;
    6662             : }
    6663             : 
    6664             : /************************************************************************/
    6665             : /*                SetJPEGQualityAndTablesModeFromFile()                 */
    6666             : /************************************************************************/
    6667             : 
    6668         161 : void GTiffDataset::SetJPEGQualityAndTablesModeFromFile(
    6669             :     int nQuality, bool bHasQuantizationTable, bool bHasHuffmanTable)
    6670             : {
    6671         161 :     if (nQuality > 0)
    6672             :     {
    6673         154 :         CPLDebug("GTiff", "Guessed JPEG quality to be %d", nQuality);
    6674         154 :         m_nJpegQuality = static_cast<signed char>(nQuality);
    6675         154 :         TIFFSetField(m_hTIFF, TIFFTAG_JPEGQUALITY, nQuality);
    6676             : 
    6677             :         // This means we will use the quantization tables from the
    6678             :         // JpegTables tag.
    6679         154 :         m_nJpegTablesMode = JPEGTABLESMODE_QUANT;
    6680             :     }
    6681             :     else
    6682             :     {
    6683           7 :         uint32_t nJPEGTableSize = 0;
    6684           7 :         void *pJPEGTable = nullptr;
    6685           7 :         if (!TIFFGetField(m_hTIFF, TIFFTAG_JPEGTABLES, &nJPEGTableSize,
    6686             :                           &pJPEGTable))
    6687             :         {
    6688           4 :             toff_t *panByteCounts = nullptr;
    6689           8 :             const int nBlockCount = m_nPlanarConfig == PLANARCONFIG_SEPARATE
    6690           4 :                                         ? m_nBlocksPerBand * nBands
    6691             :                                         : m_nBlocksPerBand;
    6692           4 :             if (TIFFIsTiled(m_hTIFF))
    6693           1 :                 TIFFGetField(m_hTIFF, TIFFTAG_TILEBYTECOUNTS, &panByteCounts);
    6694             :             else
    6695           3 :                 TIFFGetField(m_hTIFF, TIFFTAG_STRIPBYTECOUNTS, &panByteCounts);
    6696             : 
    6697           4 :             bool bFoundNonEmptyBlock = false;
    6698           4 :             if (panByteCounts != nullptr)
    6699             :             {
    6700          56 :                 for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
    6701             :                 {
    6702          53 :                     if (panByteCounts[iBlock] != 0)
    6703             :                     {
    6704           1 :                         bFoundNonEmptyBlock = true;
    6705           1 :                         break;
    6706             :                     }
    6707             :                 }
    6708             :             }
    6709           4 :             if (bFoundNonEmptyBlock)
    6710             :             {
    6711           1 :                 CPLDebug("GTiff", "Could not guess JPEG quality. "
    6712             :                                   "JPEG tables are missing, so going in "
    6713             :                                   "TIFFTAG_JPEGTABLESMODE = 0/2 mode");
    6714             :                 // Write quantization tables in each strile.
    6715           1 :                 m_nJpegTablesMode = 0;
    6716             :             }
    6717             :         }
    6718             :         else
    6719             :         {
    6720           3 :             if (bHasQuantizationTable)
    6721             :             {
    6722             :                 // FIXME in libtiff: this is likely going to cause issues
    6723             :                 // since libtiff will reuse in each strile the number of
    6724             :                 // the global quantization table, which is invalid.
    6725           1 :                 CPLDebug("GTiff",
    6726             :                          "Could not guess JPEG quality although JPEG "
    6727             :                          "quantization tables are present, so going in "
    6728             :                          "TIFFTAG_JPEGTABLESMODE = 0/2 mode");
    6729             :             }
    6730             :             else
    6731             :             {
    6732           2 :                 CPLDebug("GTiff",
    6733             :                          "Could not guess JPEG quality since JPEG "
    6734             :                          "quantization tables are not present, so going in "
    6735             :                          "TIFFTAG_JPEGTABLESMODE = 0/2 mode");
    6736             :             }
    6737             : 
    6738             :             // Write quantization tables in each strile.
    6739           3 :             m_nJpegTablesMode = 0;
    6740             :         }
    6741             :     }
    6742         161 :     if (bHasHuffmanTable)
    6743             :     {
    6744             :         // If there are Huffman tables in header use them, otherwise
    6745             :         // if we use optimized tables, libtiff will currently reuse
    6746             :         // the number of the Huffman tables of the header for the
    6747             :         // optimized version of each strile, which is illegal.
    6748          23 :         m_nJpegTablesMode |= JPEGTABLESMODE_HUFF;
    6749             :     }
    6750         161 :     if (m_nJpegTablesMode >= 0)
    6751         159 :         TIFFSetField(m_hTIFF, TIFFTAG_JPEGTABLESMODE, m_nJpegTablesMode);
    6752         161 : }
    6753             : 
    6754             : /************************************************************************/
    6755             : /*                               Create()                               */
    6756             : /*                                                                      */
    6757             : /*      Create a new GeoTIFF or TIFF file.                              */
    6758             : /************************************************************************/
    6759             : 
    6760        5789 : GDALDataset *GTiffDataset::Create(const char *pszFilename, int nXSize,
    6761             :                                   int nYSize, int l_nBands, GDALDataType eType,
    6762             :                                   CSLConstList papszParamList)
    6763             : 
    6764             : {
    6765        5789 :     VSILFILE *l_fpL = nullptr;
    6766       11578 :     CPLString l_osTmpFilename;
    6767             : 
    6768             :     const int nColorTableMultiplier = std::max(
    6769       11578 :         1,
    6770       11578 :         std::min(257,
    6771        5789 :                  atoi(CSLFetchNameValueDef(
    6772             :                      papszParamList, "COLOR_TABLE_MULTIPLIER",
    6773        5789 :                      CPLSPrintf("%d", DEFAULT_COLOR_TABLE_MULTIPLIER_257)))));
    6774             : 
    6775             :     /* -------------------------------------------------------------------- */
    6776             :     /*      Create the underlying TIFF file.                                */
    6777             :     /* -------------------------------------------------------------------- */
    6778             :     bool bTileInterleaving;
    6779             :     TIFF *l_hTIFF =
    6780        5789 :         CreateLL(pszFilename, nXSize, nYSize, l_nBands, eType, 0,
    6781             :                  nColorTableMultiplier, papszParamList, &l_fpL, l_osTmpFilename,
    6782             :                  /* bCreateCopy=*/false, bTileInterleaving);
    6783        5789 :     const bool bStreaming = !l_osTmpFilename.empty();
    6784             : 
    6785        5789 :     if (l_hTIFF == nullptr)
    6786          38 :         return nullptr;
    6787             : 
    6788             :     /* -------------------------------------------------------------------- */
    6789             :     /*      Create the new GTiffDataset object.                             */
    6790             :     /* -------------------------------------------------------------------- */
    6791       11502 :     auto poDS = std::make_unique<GTiffDataset>();
    6792        5751 :     poDS->m_hTIFF = l_hTIFF;
    6793        5751 :     poDS->m_fpL = l_fpL;
    6794        5751 :     const bool bSuppressASAP = CPLTestBool(
    6795             :         CSLFetchNameValueDef(papszParamList, "@SUPPRESS_ASAP", "NO"));
    6796        5751 :     if (bSuppressASAP)
    6797          36 :         poDS->MarkSuppressOnClose();
    6798        5751 :     if (bStreaming)
    6799             :     {
    6800           4 :         poDS->m_bStreamingOut = true;
    6801           4 :         poDS->m_pszTmpFilename = CPLStrdup(l_osTmpFilename);
    6802           4 :         poDS->m_fpToWrite = VSIFOpenL(pszFilename, "wb");
    6803           4 :         if (poDS->m_fpToWrite == nullptr)
    6804             :         {
    6805           1 :             VSIUnlink(l_osTmpFilename);
    6806           1 :             return nullptr;
    6807             :         }
    6808             :     }
    6809        5750 :     poDS->nRasterXSize = nXSize;
    6810        5750 :     poDS->nRasterYSize = nYSize;
    6811        5750 :     poDS->eAccess = GA_Update;
    6812             : 
    6813             :     // This will avoid GTiffDataset::GetSiblingFiles() to trigger a directory
    6814             :     // listing, which is potentially costly and only makes sense when opening
    6815             :     // new files, not creating new ones. Helps for scenario like
    6816             :     // https://github.com/OSGeo/gdal/issues/13930
    6817        5750 :     poDS->m_bHasGotSiblingFiles = true;
    6818             : 
    6819        5750 :     poDS->m_nColorTableMultiplier = nColorTableMultiplier;
    6820             : 
    6821        5750 :     poDS->m_bCrystalized = false;
    6822        5750 :     poDS->m_nSamplesPerPixel = static_cast<uint16_t>(l_nBands);
    6823        5750 :     poDS->m_osFilename = pszFilename;
    6824             : 
    6825             :     // Don't try to load external metadata files (#6597).
    6826        5750 :     poDS->m_bIMDRPCMetadataLoaded = true;
    6827             : 
    6828             :     // Avoid premature crystalization that will cause directory re-writing if
    6829             :     // GetProjectionRef() or GetGeoTransform() are called on the newly created
    6830             :     // GeoTIFF.
    6831        5750 :     poDS->m_bLookedForProjection = true;
    6832             : 
    6833        5750 :     TIFFGetField(l_hTIFF, TIFFTAG_SAMPLEFORMAT, &(poDS->m_nSampleFormat));
    6834        5750 :     TIFFGetField(l_hTIFF, TIFFTAG_PLANARCONFIG, &(poDS->m_nPlanarConfig));
    6835             :     // Weird that we need this, but otherwise we get a Valgrind warning on
    6836             :     // tiff_write_124.
    6837        5750 :     if (!TIFFGetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, &(poDS->m_nPhotometric)))
    6838           1 :         poDS->m_nPhotometric = PHOTOMETRIC_MINISBLACK;
    6839        5750 :     TIFFGetField(l_hTIFF, TIFFTAG_BITSPERSAMPLE, &(poDS->m_nBitsPerSample));
    6840        5750 :     TIFFGetField(l_hTIFF, TIFFTAG_COMPRESSION, &(poDS->m_nCompression));
    6841             : 
    6842        5750 :     if (TIFFIsTiled(l_hTIFF))
    6843             :     {
    6844         406 :         TIFFGetField(l_hTIFF, TIFFTAG_TILEWIDTH, &(poDS->m_nBlockXSize));
    6845         406 :         TIFFGetField(l_hTIFF, TIFFTAG_TILELENGTH, &(poDS->m_nBlockYSize));
    6846             :     }
    6847             :     else
    6848             :     {
    6849        5344 :         if (!TIFFGetField(l_hTIFF, TIFFTAG_ROWSPERSTRIP,
    6850        5344 :                           &(poDS->m_nRowsPerStrip)))
    6851           0 :             poDS->m_nRowsPerStrip = 1;  // Dummy value.
    6852             : 
    6853        5344 :         poDS->m_nBlockXSize = nXSize;
    6854       10688 :         poDS->m_nBlockYSize =
    6855        5344 :             std::min(static_cast<int>(poDS->m_nRowsPerStrip), nYSize);
    6856             :     }
    6857             : 
    6858        5750 :     if (!poDS->ComputeBlocksPerColRowAndBand(l_nBands))
    6859             :     {
    6860           0 :         poDS->m_fpL->CancelCreation();
    6861           0 :         return nullptr;
    6862             :     }
    6863             : 
    6864        5750 :     poDS->m_eProfile = GetProfile(CSLFetchNameValue(papszParamList, "PROFILE"));
    6865             : 
    6866             :     /* -------------------------------------------------------------------- */
    6867             :     /*      YCbCr JPEG compressed images should be translated on the fly    */
    6868             :     /*      to RGB by libtiff/libjpeg unless specifically requested         */
    6869             :     /*      otherwise.                                                      */
    6870             :     /* -------------------------------------------------------------------- */
    6871        5750 :     if (poDS->m_nCompression == COMPRESSION_JPEG &&
    6872        5771 :         poDS->m_nPhotometric == PHOTOMETRIC_YCBCR &&
    6873          21 :         CPLTestBool(CPLGetConfigOption("CONVERT_YCBCR_TO_RGB", "YES")))
    6874             :     {
    6875          21 :         int nColorMode = 0;
    6876             : 
    6877          21 :         poDS->SetMetadataItem("SOURCE_COLOR_SPACE", "YCbCr", "IMAGE_STRUCTURE");
    6878          42 :         if (!TIFFGetField(l_hTIFF, TIFFTAG_JPEGCOLORMODE, &nColorMode) ||
    6879          21 :             nColorMode != JPEGCOLORMODE_RGB)
    6880          21 :             TIFFSetField(l_hTIFF, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
    6881             :     }
    6882             : 
    6883        5750 :     if (poDS->m_nCompression == COMPRESSION_LERC)
    6884             :     {
    6885          26 :         uint32_t nLercParamCount = 0;
    6886          26 :         uint32_t *panLercParams = nullptr;
    6887          26 :         if (TIFFGetField(l_hTIFF, TIFFTAG_LERC_PARAMETERS, &nLercParamCount,
    6888          52 :                          &panLercParams) &&
    6889          26 :             nLercParamCount == 2)
    6890             :         {
    6891          26 :             memcpy(poDS->m_anLercAddCompressionAndVersion, panLercParams,
    6892             :                    sizeof(poDS->m_anLercAddCompressionAndVersion));
    6893             :         }
    6894             :     }
    6895             : 
    6896             :     /* -------------------------------------------------------------------- */
    6897             :     /*      Read palette back as a color table if it has one.               */
    6898             :     /* -------------------------------------------------------------------- */
    6899        5750 :     unsigned short *panRed = nullptr;
    6900        5750 :     unsigned short *panGreen = nullptr;
    6901        5750 :     unsigned short *panBlue = nullptr;
    6902             : 
    6903        5754 :     if (poDS->m_nPhotometric == PHOTOMETRIC_PALETTE &&
    6904           4 :         TIFFGetField(l_hTIFF, TIFFTAG_COLORMAP, &panRed, &panGreen, &panBlue))
    6905             :     {
    6906             : 
    6907           4 :         poDS->m_poColorTable = std::make_unique<GDALColorTable>();
    6908             : 
    6909           4 :         const int nColorCount = 1 << poDS->m_nBitsPerSample;
    6910             : 
    6911        1028 :         for (int iColor = nColorCount - 1; iColor >= 0; iColor--)
    6912             :         {
    6913        1024 :             const GDALColorEntry oEntry = {
    6914        1024 :                 static_cast<short>(panRed[iColor] / nColorTableMultiplier),
    6915        1024 :                 static_cast<short>(panGreen[iColor] / nColorTableMultiplier),
    6916        1024 :                 static_cast<short>(panBlue[iColor] / nColorTableMultiplier),
    6917        1024 :                 static_cast<short>(255)};
    6918             : 
    6919        1024 :             poDS->m_poColorTable->SetColorEntry(iColor, &oEntry);
    6920             :         }
    6921             :     }
    6922             : 
    6923             :     /* -------------------------------------------------------------------- */
    6924             :     /*      Do we want to ensure all blocks get written out on close to     */
    6925             :     /*      avoid sparse files?                                             */
    6926             :     /* -------------------------------------------------------------------- */
    6927        5750 :     if (!CPLFetchBool(papszParamList, "SPARSE_OK", false))
    6928        5640 :         poDS->m_bFillEmptyTilesAtClosing = true;
    6929             : 
    6930        5750 :     poDS->m_bWriteEmptyTiles =
    6931        6570 :         bStreaming || (poDS->m_nCompression != COMPRESSION_NONE &&
    6932         820 :                        poDS->m_bFillEmptyTilesAtClosing);
    6933             :     // Only required for people writing non-compressed striped files in the
    6934             :     // right order and wanting all tstrips to be written in the same order
    6935             :     // so that the end result can be memory mapped without knowledge of each
    6936             :     // strip offset.
    6937        5750 :     if (CPLTestBool(CSLFetchNameValueDef(
    6938       11500 :             papszParamList, "WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")) ||
    6939        5750 :         CPLTestBool(CSLFetchNameValueDef(
    6940             :             papszParamList, "@WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")))
    6941             :     {
    6942          26 :         poDS->m_bWriteEmptyTiles = true;
    6943             :     }
    6944             : 
    6945             :     /* -------------------------------------------------------------------- */
    6946             :     /*      Preserve creation options for consulting later (for instance    */
    6947             :     /*      to decide if a TFW file should be written).                     */
    6948             :     /* -------------------------------------------------------------------- */
    6949        5750 :     poDS->m_papszCreationOptions = CSLDuplicate(papszParamList);
    6950             : 
    6951        5750 :     poDS->m_nZLevel = GTiffGetZLevel(papszParamList);
    6952        5750 :     poDS->m_nLZMAPreset = GTiffGetLZMAPreset(papszParamList);
    6953        5750 :     poDS->m_nZSTDLevel = GTiffGetZSTDPreset(papszParamList);
    6954        5750 :     poDS->m_nWebPLevel = GTiffGetWebPLevel(papszParamList);
    6955        5750 :     poDS->m_bWebPLossless = GTiffGetWebPLossless(papszParamList);
    6956        5752 :     if (poDS->m_nWebPLevel != 100 && poDS->m_bWebPLossless &&
    6957           2 :         CSLFetchNameValue(papszParamList, "WEBP_LEVEL"))
    6958             :     {
    6959           0 :         CPLError(CE_Warning, CPLE_AppDefined,
    6960             :                  "WEBP_LEVEL is specified, but WEBP_LOSSLESS=YES. "
    6961             :                  "WEBP_LEVEL will be ignored.");
    6962             :     }
    6963        5750 :     poDS->m_nJpegQuality = GTiffGetJpegQuality(papszParamList);
    6964        5750 :     poDS->m_nJpegTablesMode = GTiffGetJpegTablesMode(papszParamList);
    6965        5750 :     poDS->m_dfMaxZError = GTiffGetLERCMaxZError(papszParamList);
    6966        5750 :     poDS->m_dfMaxZErrorOverview = GTiffGetLERCMaxZErrorOverview(papszParamList);
    6967             : #if HAVE_JXL
    6968        5750 :     poDS->m_bJXLLossless = GTiffGetJXLLossless(papszParamList);
    6969        5750 :     poDS->m_nJXLEffort = GTiffGetJXLEffort(papszParamList);
    6970        5750 :     poDS->m_fJXLDistance = GTiffGetJXLDistance(papszParamList);
    6971        5750 :     poDS->m_fJXLAlphaDistance = GTiffGetJXLAlphaDistance(papszParamList);
    6972             : #endif
    6973        5750 :     poDS->InitCreationOrOpenOptions(true, papszParamList);
    6974             : 
    6975             :     /* -------------------------------------------------------------------- */
    6976             :     /*      Create band information objects.                                */
    6977             :     /* -------------------------------------------------------------------- */
    6978      308355 :     for (int iBand = 0; iBand < l_nBands; ++iBand)
    6979             :     {
    6980      371812 :         if (poDS->m_nBitsPerSample == 8 || poDS->m_nBitsPerSample == 16 ||
    6981      372053 :             poDS->m_nBitsPerSample == 32 || poDS->m_nBitsPerSample == 64 ||
    6982         241 :             poDS->m_nBitsPerSample == 128)
    6983             :         {
    6984      605080 :             poDS->SetBand(iBand + 1, std::make_unique<GTiffRasterBand>(
    6985      605080 :                                          poDS.get(), iBand + 1));
    6986             :         }
    6987             :         else
    6988             :         {
    6989         130 :             poDS->SetBand(iBand + 1, std::make_unique<GTiffOddBitsBand>(
    6990          65 :                                          poDS.get(), iBand + 1));
    6991         130 :             poDS->GetRasterBand(iBand + 1)->SetMetadataItem(
    6992         130 :                 "NBITS", CPLString().Printf("%d", poDS->m_nBitsPerSample),
    6993          65 :                 "IMAGE_STRUCTURE");
    6994             :         }
    6995             :     }
    6996             : 
    6997        5750 :     poDS->GetDiscardLsbOption(papszParamList);
    6998             : 
    6999        5750 :     if (poDS->m_nPlanarConfig == PLANARCONFIG_CONTIG && l_nBands != 1)
    7000         842 :         poDS->SetMetadataItem("INTERLEAVE", "PIXEL", "IMAGE_STRUCTURE");
    7001             :     else
    7002        4908 :         poDS->SetMetadataItem("INTERLEAVE", "BAND", "IMAGE_STRUCTURE");
    7003             : 
    7004        5750 :     poDS->oOvManager.Initialize(poDS.get(), pszFilename);
    7005             : 
    7006        5750 :     return poDS.release();
    7007             : }
    7008             : 
    7009             : /************************************************************************/
    7010             : /*                         CopyImageryAndMask()                         */
    7011             : /************************************************************************/
    7012             : 
    7013         352 : CPLErr GTiffDataset::CopyImageryAndMask(GTiffDataset *poDstDS,
    7014             :                                         GDALDataset *poSrcDS,
    7015             :                                         GDALRasterBand *poSrcMaskBand,
    7016             :                                         GDALProgressFunc pfnProgress,
    7017             :                                         void *pProgressData)
    7018             : {
    7019         352 :     CPLErr eErr = CE_None;
    7020             : 
    7021         352 :     const auto eType = poDstDS->GetRasterBand(1)->GetRasterDataType();
    7022         352 :     const int nDataTypeSize = GDALGetDataTypeSizeBytes(eType);
    7023         352 :     const int l_nBands = poDstDS->GetRasterCount();
    7024             :     GByte *pBlockBuffer = static_cast<GByte *>(
    7025         352 :         VSI_MALLOC3_VERBOSE(poDstDS->m_nBlockXSize, poDstDS->m_nBlockYSize,
    7026             :                             cpl::fits_on<int>(l_nBands * nDataTypeSize)));
    7027         352 :     if (pBlockBuffer == nullptr)
    7028             :     {
    7029           0 :         eErr = CE_Failure;
    7030             :     }
    7031         352 :     const int nYSize = poDstDS->nRasterYSize;
    7032         352 :     const int nXSize = poDstDS->nRasterXSize;
    7033             :     const bool bIsOddBand =
    7034         352 :         dynamic_cast<GTiffOddBitsBand *>(poDstDS->GetRasterBand(1)) != nullptr;
    7035             : 
    7036         352 :     if (poDstDS->m_poMaskDS)
    7037             :     {
    7038          59 :         CPLAssert(poDstDS->m_poMaskDS->m_nBlockXSize == poDstDS->m_nBlockXSize);
    7039          59 :         CPLAssert(poDstDS->m_poMaskDS->m_nBlockYSize == poDstDS->m_nBlockYSize);
    7040             :     }
    7041             : 
    7042         352 :     if (poDstDS->m_nPlanarConfig == PLANARCONFIG_SEPARATE &&
    7043          58 :         !poDstDS->m_bTileInterleave)
    7044             :     {
    7045          45 :         int iBlock = 0;
    7046          45 :         const int nBlocks = poDstDS->m_nBlocksPerBand *
    7047          45 :                             (l_nBands + (poDstDS->m_poMaskDS ? 1 : 0));
    7048         195 :         for (int i = 0; eErr == CE_None && i < l_nBands; i++)
    7049             :         {
    7050         345 :             for (int iY = 0; iY < nYSize && eErr == CE_None;
    7051         195 :                  iY = ((nYSize - iY < poDstDS->m_nBlockYSize)
    7052         195 :                            ? nYSize
    7053          59 :                            : iY + poDstDS->m_nBlockYSize))
    7054             :             {
    7055             :                 const int nReqYSize =
    7056         195 :                     std::min(nYSize - iY, poDstDS->m_nBlockYSize);
    7057         495 :                 for (int iX = 0; iX < nXSize && eErr == CE_None;
    7058         300 :                      iX = ((nXSize - iX < poDstDS->m_nBlockXSize)
    7059         300 :                                ? nXSize
    7060         155 :                                : iX + poDstDS->m_nBlockXSize))
    7061             :                 {
    7062             :                     const int nReqXSize =
    7063         300 :                         std::min(nXSize - iX, poDstDS->m_nBlockXSize);
    7064         300 :                     if (nReqXSize < poDstDS->m_nBlockXSize ||
    7065         155 :                         nReqYSize < poDstDS->m_nBlockYSize)
    7066             :                     {
    7067         190 :                         memset(pBlockBuffer, 0,
    7068         190 :                                static_cast<size_t>(poDstDS->m_nBlockXSize) *
    7069         190 :                                    poDstDS->m_nBlockYSize * nDataTypeSize);
    7070             :                     }
    7071         300 :                     eErr = poSrcDS->GetRasterBand(i + 1)->RasterIO(
    7072             :                         GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
    7073             :                         nReqXSize, nReqYSize, eType, nDataTypeSize,
    7074         300 :                         static_cast<GSpacing>(nDataTypeSize) *
    7075         300 :                             poDstDS->m_nBlockXSize,
    7076             :                         nullptr);
    7077         300 :                     if (eErr == CE_None)
    7078             :                     {
    7079         300 :                         eErr = poDstDS->WriteEncodedTileOrStrip(
    7080             :                             iBlock, pBlockBuffer, false);
    7081             :                     }
    7082             : 
    7083         300 :                     iBlock++;
    7084         600 :                     if (pfnProgress &&
    7085         300 :                         !pfnProgress(static_cast<double>(iBlock) / nBlocks,
    7086             :                                      nullptr, pProgressData))
    7087             :                     {
    7088           0 :                         eErr = CE_Failure;
    7089             :                     }
    7090             : 
    7091         300 :                     if (poDstDS->m_bWriteError)
    7092           0 :                         eErr = CE_Failure;
    7093             :                 }
    7094             :             }
    7095             :         }
    7096          45 :         if (poDstDS->m_poMaskDS && eErr == CE_None)
    7097             :         {
    7098           6 :             int iBlockMask = 0;
    7099          17 :             for (int iY = 0, nYBlock = 0; iY < nYSize && eErr == CE_None;
    7100          11 :                  iY = ((nYSize - iY < poDstDS->m_nBlockYSize)
    7101          11 :                            ? nYSize
    7102           5 :                            : iY + poDstDS->m_nBlockYSize),
    7103             :                      nYBlock++)
    7104             :             {
    7105             :                 const int nReqYSize =
    7106          11 :                     std::min(nYSize - iY, poDstDS->m_nBlockYSize);
    7107          49 :                 for (int iX = 0, nXBlock = 0; iX < nXSize && eErr == CE_None;
    7108          38 :                      iX = ((nXSize - iX < poDstDS->m_nBlockXSize)
    7109          38 :                                ? nXSize
    7110          30 :                                : iX + poDstDS->m_nBlockXSize),
    7111             :                          nXBlock++)
    7112             :                 {
    7113             :                     const int nReqXSize =
    7114          38 :                         std::min(nXSize - iX, poDstDS->m_nBlockXSize);
    7115          38 :                     if (nReqXSize < poDstDS->m_nBlockXSize ||
    7116          30 :                         nReqYSize < poDstDS->m_nBlockYSize)
    7117             :                     {
    7118          16 :                         memset(pBlockBuffer, 0,
    7119          16 :                                static_cast<size_t>(poDstDS->m_nBlockXSize) *
    7120          16 :                                    poDstDS->m_nBlockYSize);
    7121             :                     }
    7122          76 :                     eErr = poSrcMaskBand->RasterIO(
    7123             :                         GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
    7124             :                         nReqXSize, nReqYSize, GDT_UInt8, 1,
    7125          38 :                         poDstDS->m_nBlockXSize, nullptr);
    7126          38 :                     if (eErr == CE_None)
    7127             :                     {
    7128             :                         // Avoid any attempt to load from disk
    7129          38 :                         poDstDS->m_poMaskDS->m_nLoadedBlock = iBlockMask;
    7130             :                         eErr =
    7131          38 :                             poDstDS->m_poMaskDS->GetRasterBand(1)->WriteBlock(
    7132             :                                 nXBlock, nYBlock, pBlockBuffer);
    7133          38 :                         if (eErr == CE_None)
    7134          38 :                             eErr = poDstDS->m_poMaskDS->FlushBlockBuf();
    7135             :                     }
    7136             : 
    7137          38 :                     iBlockMask++;
    7138          76 :                     if (pfnProgress &&
    7139          38 :                         !pfnProgress(static_cast<double>(iBlock + iBlockMask) /
    7140             :                                          nBlocks,
    7141             :                                      nullptr, pProgressData))
    7142             :                     {
    7143           0 :                         eErr = CE_Failure;
    7144             :                     }
    7145             : 
    7146          38 :                     if (poDstDS->m_poMaskDS->m_bWriteError)
    7147           0 :                         eErr = CE_Failure;
    7148             :                 }
    7149             :             }
    7150          45 :         }
    7151             :     }
    7152             :     else
    7153             :     {
    7154         307 :         int iBlock = 0;
    7155         307 :         const int nBlocks = poDstDS->m_nBlocksPerBand;
    7156        7123 :         for (int iY = 0, nYBlock = 0; iY < nYSize && eErr == CE_None;
    7157        6816 :              iY = ((nYSize - iY < poDstDS->m_nBlockYSize)
    7158        6816 :                        ? nYSize
    7159        6584 :                        : iY + poDstDS->m_nBlockYSize),
    7160             :                  nYBlock++)
    7161             :         {
    7162        6816 :             const int nReqYSize = std::min(nYSize - iY, poDstDS->m_nBlockYSize);
    7163       26639 :             for (int iX = 0, nXBlock = 0; iX < nXSize && eErr == CE_None;
    7164       19823 :                  iX = ((nXSize - iX < poDstDS->m_nBlockXSize)
    7165       19823 :                            ? nXSize
    7166       19504 :                            : iX + poDstDS->m_nBlockXSize),
    7167             :                      nXBlock++)
    7168             :             {
    7169             :                 const int nReqXSize =
    7170       19823 :                     std::min(nXSize - iX, poDstDS->m_nBlockXSize);
    7171       19823 :                 if (nReqXSize < poDstDS->m_nBlockXSize ||
    7172       19504 :                     nReqYSize < poDstDS->m_nBlockYSize)
    7173             :                 {
    7174         503 :                     memset(pBlockBuffer, 0,
    7175         503 :                            static_cast<size_t>(poDstDS->m_nBlockXSize) *
    7176         503 :                                poDstDS->m_nBlockYSize * l_nBands *
    7177         503 :                                nDataTypeSize);
    7178             :                 }
    7179             : 
    7180       19823 :                 if (poDstDS->m_bTileInterleave)
    7181             :                 {
    7182         114 :                     eErr = poSrcDS->RasterIO(
    7183             :                         GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
    7184             :                         nReqXSize, nReqYSize, eType, l_nBands, nullptr,
    7185             :                         nDataTypeSize,
    7186          57 :                         static_cast<GSpacing>(nDataTypeSize) *
    7187          57 :                             poDstDS->m_nBlockXSize,
    7188          57 :                         static_cast<GSpacing>(nDataTypeSize) *
    7189          57 :                             poDstDS->m_nBlockXSize * poDstDS->m_nBlockYSize,
    7190             :                         nullptr);
    7191          57 :                     if (eErr == CE_None)
    7192             :                     {
    7193         228 :                         for (int i = 0; eErr == CE_None && i < l_nBands; i++)
    7194             :                         {
    7195         171 :                             eErr = poDstDS->WriteEncodedTileOrStrip(
    7196         171 :                                 iBlock + i * poDstDS->m_nBlocksPerBand,
    7197         171 :                                 pBlockBuffer + static_cast<size_t>(i) *
    7198         171 :                                                    poDstDS->m_nBlockXSize *
    7199         171 :                                                    poDstDS->m_nBlockYSize *
    7200         171 :                                                    nDataTypeSize,
    7201             :                                 false);
    7202             :                         }
    7203             :                     }
    7204             :                 }
    7205       19766 :                 else if (!bIsOddBand)
    7206             :                 {
    7207       39410 :                     eErr = poSrcDS->RasterIO(
    7208             :                         GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
    7209             :                         nReqXSize, nReqYSize, eType, l_nBands, nullptr,
    7210       19705 :                         static_cast<GSpacing>(nDataTypeSize) * l_nBands,
    7211       19705 :                         static_cast<GSpacing>(nDataTypeSize) * l_nBands *
    7212       19705 :                             poDstDS->m_nBlockXSize,
    7213             :                         nDataTypeSize, nullptr);
    7214       19705 :                     if (eErr == CE_None)
    7215             :                     {
    7216       19704 :                         eErr = poDstDS->WriteEncodedTileOrStrip(
    7217             :                             iBlock, pBlockBuffer, false);
    7218             :                     }
    7219             :                 }
    7220             :                 else
    7221             :                 {
    7222             :                     // In the odd bit case, this is a bit messy to ensure
    7223             :                     // the strile gets written synchronously.
    7224             :                     // We load the content of the n-1 bands in the cache,
    7225             :                     // and for the last band we invoke WriteBlock() directly
    7226             :                     // We also force FlushBlockBuf()
    7227         122 :                     std::vector<GDALRasterBlock *> apoLockedBlocks;
    7228          91 :                     for (int i = 0; eErr == CE_None && i < l_nBands - 1; i++)
    7229             :                     {
    7230             :                         auto poBlock =
    7231          30 :                             poDstDS->GetRasterBand(i + 1)->GetLockedBlockRef(
    7232          30 :                                 nXBlock, nYBlock, TRUE);
    7233          30 :                         if (poBlock)
    7234             :                         {
    7235          60 :                             eErr = poSrcDS->GetRasterBand(i + 1)->RasterIO(
    7236             :                                 GF_Read, iX, iY, nReqXSize, nReqYSize,
    7237             :                                 poBlock->GetDataRef(), nReqXSize, nReqYSize,
    7238             :                                 eType, nDataTypeSize,
    7239          30 :                                 static_cast<GSpacing>(nDataTypeSize) *
    7240          30 :                                     poDstDS->m_nBlockXSize,
    7241             :                                 nullptr);
    7242          30 :                             poBlock->MarkDirty();
    7243          30 :                             apoLockedBlocks.emplace_back(poBlock);
    7244             :                         }
    7245             :                         else
    7246             :                         {
    7247           0 :                             eErr = CE_Failure;
    7248             :                         }
    7249             :                     }
    7250          61 :                     if (eErr == CE_None)
    7251             :                     {
    7252         122 :                         eErr = poSrcDS->GetRasterBand(l_nBands)->RasterIO(
    7253             :                             GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
    7254             :                             nReqXSize, nReqYSize, eType, nDataTypeSize,
    7255          61 :                             static_cast<GSpacing>(nDataTypeSize) *
    7256          61 :                                 poDstDS->m_nBlockXSize,
    7257             :                             nullptr);
    7258             :                     }
    7259          61 :                     if (eErr == CE_None)
    7260             :                     {
    7261             :                         // Avoid any attempt to load from disk
    7262          61 :                         poDstDS->m_nLoadedBlock = iBlock;
    7263          61 :                         eErr = poDstDS->GetRasterBand(l_nBands)->WriteBlock(
    7264             :                             nXBlock, nYBlock, pBlockBuffer);
    7265          61 :                         if (eErr == CE_None)
    7266          61 :                             eErr = poDstDS->FlushBlockBuf();
    7267             :                     }
    7268          91 :                     for (auto poBlock : apoLockedBlocks)
    7269             :                     {
    7270          30 :                         poBlock->MarkClean();
    7271          30 :                         poBlock->DropLock();
    7272             :                     }
    7273             :                 }
    7274             : 
    7275       19823 :                 if (eErr == CE_None && poDstDS->m_poMaskDS)
    7276             :                 {
    7277        4664 :                     if (nReqXSize < poDstDS->m_nBlockXSize ||
    7278        4621 :                         nReqYSize < poDstDS->m_nBlockYSize)
    7279             :                     {
    7280          81 :                         memset(pBlockBuffer, 0,
    7281          81 :                                static_cast<size_t>(poDstDS->m_nBlockXSize) *
    7282          81 :                                    poDstDS->m_nBlockYSize);
    7283             :                     }
    7284        9328 :                     eErr = poSrcMaskBand->RasterIO(
    7285             :                         GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
    7286             :                         nReqXSize, nReqYSize, GDT_UInt8, 1,
    7287        4664 :                         poDstDS->m_nBlockXSize, nullptr);
    7288        4664 :                     if (eErr == CE_None)
    7289             :                     {
    7290             :                         // Avoid any attempt to load from disk
    7291        4664 :                         poDstDS->m_poMaskDS->m_nLoadedBlock = iBlock;
    7292             :                         eErr =
    7293        4664 :                             poDstDS->m_poMaskDS->GetRasterBand(1)->WriteBlock(
    7294             :                                 nXBlock, nYBlock, pBlockBuffer);
    7295        4664 :                         if (eErr == CE_None)
    7296        4664 :                             eErr = poDstDS->m_poMaskDS->FlushBlockBuf();
    7297             :                     }
    7298             :                 }
    7299       19823 :                 if (poDstDS->m_bWriteError)
    7300           6 :                     eErr = CE_Failure;
    7301             : 
    7302       19823 :                 iBlock++;
    7303       39646 :                 if (pfnProgress &&
    7304       19823 :                     !pfnProgress(static_cast<double>(iBlock) / nBlocks, nullptr,
    7305             :                                  pProgressData))
    7306             :                 {
    7307           0 :                     eErr = CE_Failure;
    7308             :                 }
    7309             :             }
    7310             :         }
    7311             :     }
    7312             : 
    7313         352 :     poDstDS->FlushCache(false);  // mostly to wait for thread completion
    7314         352 :     VSIFree(pBlockBuffer);
    7315             : 
    7316         352 :     return eErr;
    7317             : }
    7318             : 
    7319             : /************************************************************************/
    7320             : /*                             CreateCopy()                             */
    7321             : /************************************************************************/
    7322             : 
    7323        2166 : GDALDataset *GTiffDataset::CreateCopy(const char *pszFilename,
    7324             :                                       GDALDataset *poSrcDS, int bStrict,
    7325             :                                       CSLConstList papszOptions,
    7326             :                                       GDALProgressFunc pfnProgress,
    7327             :                                       void *pProgressData)
    7328             : 
    7329             : {
    7330        2166 :     if (poSrcDS->GetRasterCount() == 0)
    7331             :     {
    7332           2 :         ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    7333             :                     "Unable to export GeoTIFF files with zero bands.");
    7334           2 :         return nullptr;
    7335             :     }
    7336             : 
    7337        2164 :     GDALRasterBand *const poPBand = poSrcDS->GetRasterBand(1);
    7338        2164 :     GDALDataType eType = poPBand->GetRasterDataType();
    7339             : 
    7340             :     /* -------------------------------------------------------------------- */
    7341             :     /*      Check, whether all bands in input dataset has the same type.    */
    7342             :     /* -------------------------------------------------------------------- */
    7343        2164 :     const int l_nBands = poSrcDS->GetRasterCount();
    7344        5093 :     for (int iBand = 2; iBand <= l_nBands; ++iBand)
    7345             :     {
    7346        2929 :         if (eType != poSrcDS->GetRasterBand(iBand)->GetRasterDataType())
    7347             :         {
    7348           0 :             if (bStrict)
    7349             :             {
    7350           0 :                 ReportError(
    7351             :                     pszFilename, CE_Failure, CPLE_AppDefined,
    7352             :                     "Unable to export GeoTIFF file with different datatypes "
    7353             :                     "per different bands. All bands should have the same "
    7354             :                     "types in TIFF.");
    7355           0 :                 return nullptr;
    7356             :             }
    7357             :             else
    7358             :             {
    7359           0 :                 ReportError(
    7360             :                     pszFilename, CE_Warning, CPLE_AppDefined,
    7361             :                     "Unable to export GeoTIFF file with different datatypes "
    7362             :                     "per different bands. All bands should have the same "
    7363             :                     "types in TIFF.");
    7364             :             }
    7365             :         }
    7366             :     }
    7367             : 
    7368             :     /* -------------------------------------------------------------------- */
    7369             :     /*      Capture the profile.                                            */
    7370             :     /* -------------------------------------------------------------------- */
    7371             :     const GTiffProfile eProfile =
    7372        2164 :         GetProfile(CSLFetchNameValue(papszOptions, "PROFILE"));
    7373             : 
    7374        2164 :     const bool bGeoTIFF = eProfile != GTiffProfile::BASELINE;
    7375             : 
    7376             :     /* -------------------------------------------------------------------- */
    7377             :     /*      Special handling for NBITS.  Copy from band metadata if found.  */
    7378             :     /* -------------------------------------------------------------------- */
    7379        2164 :     char **papszCreateOptions = CSLDuplicate(papszOptions);
    7380             : 
    7381        2164 :     if (poPBand->GetMetadataItem("NBITS", "IMAGE_STRUCTURE") != nullptr &&
    7382        2181 :         atoi(poPBand->GetMetadataItem("NBITS", "IMAGE_STRUCTURE")) > 0 &&
    7383          17 :         CSLFetchNameValue(papszCreateOptions, "NBITS") == nullptr)
    7384             :     {
    7385           3 :         papszCreateOptions = CSLSetNameValue(
    7386             :             papszCreateOptions, "NBITS",
    7387           3 :             poPBand->GetMetadataItem("NBITS", "IMAGE_STRUCTURE"));
    7388             :     }
    7389             : 
    7390        2164 :     if (CSLFetchNameValue(papszOptions, "PIXELTYPE") == nullptr &&
    7391             :         eType == GDT_UInt8)
    7392             :     {
    7393        1794 :         poPBand->EnablePixelTypeSignedByteWarning(false);
    7394             :         const char *pszPixelType =
    7395        1794 :             poPBand->GetMetadataItem("PIXELTYPE", "IMAGE_STRUCTURE");
    7396        1794 :         poPBand->EnablePixelTypeSignedByteWarning(true);
    7397        1794 :         if (pszPixelType)
    7398             :         {
    7399           1 :             papszCreateOptions =
    7400           1 :                 CSLSetNameValue(papszCreateOptions, "PIXELTYPE", pszPixelType);
    7401             :         }
    7402             :     }
    7403             : 
    7404             :     /* -------------------------------------------------------------------- */
    7405             :     /*      Color profile.  Copy from band metadata if found.              */
    7406             :     /* -------------------------------------------------------------------- */
    7407        2164 :     if (bGeoTIFF)
    7408             :     {
    7409        2147 :         const char *pszOptionsMD[] = {"SOURCE_ICC_PROFILE",
    7410             :                                       "SOURCE_PRIMARIES_RED",
    7411             :                                       "SOURCE_PRIMARIES_GREEN",
    7412             :                                       "SOURCE_PRIMARIES_BLUE",
    7413             :                                       "SOURCE_WHITEPOINT",
    7414             :                                       "TIFFTAG_TRANSFERFUNCTION_RED",
    7415             :                                       "TIFFTAG_TRANSFERFUNCTION_GREEN",
    7416             :                                       "TIFFTAG_TRANSFERFUNCTION_BLUE",
    7417             :                                       "TIFFTAG_TRANSFERRANGE_BLACK",
    7418             :                                       "TIFFTAG_TRANSFERRANGE_WHITE",
    7419             :                                       nullptr};
    7420             : 
    7421             :         // Copy all the tags.  Options will override tags in the source.
    7422        2147 :         int i = 0;
    7423       23597 :         while (pszOptionsMD[i] != nullptr)
    7424             :         {
    7425             :             char const *pszMD =
    7426       21452 :                 CSLFetchNameValue(papszOptions, pszOptionsMD[i]);
    7427       21452 :             if (pszMD == nullptr)
    7428             :                 pszMD =
    7429       21444 :                     poSrcDS->GetMetadataItem(pszOptionsMD[i], "COLOR_PROFILE");
    7430             : 
    7431       21452 :             if ((pszMD != nullptr) && !EQUAL(pszMD, ""))
    7432             :             {
    7433          16 :                 papszCreateOptions =
    7434          16 :                     CSLSetNameValue(papszCreateOptions, pszOptionsMD[i], pszMD);
    7435             : 
    7436             :                 // If an ICC profile exists, other tags are not needed.
    7437          16 :                 if (EQUAL(pszOptionsMD[i], "SOURCE_ICC_PROFILE"))
    7438           2 :                     break;
    7439             :             }
    7440             : 
    7441       21450 :             ++i;
    7442             :         }
    7443             :     }
    7444             : 
    7445        2164 :     double dfExtraSpaceForOverviews = 0;
    7446             :     const bool bCopySrcOverviews =
    7447        2164 :         CPLFetchBool(papszCreateOptions, "COPY_SRC_OVERVIEWS", false);
    7448        2164 :     std::unique_ptr<GDALDataset> poOvrDS;
    7449        2164 :     int nSrcOverviews = 0;
    7450        2164 :     if (bCopySrcOverviews)
    7451             :     {
    7452             :         const char *pszOvrDS =
    7453         231 :             CSLFetchNameValue(papszCreateOptions, "@OVERVIEW_DATASET");
    7454         231 :         if (pszOvrDS)
    7455             :         {
    7456             :             // Empty string is used by COG driver to indicate that we want
    7457             :             // to ignore source overviews.
    7458          39 :             if (!EQUAL(pszOvrDS, ""))
    7459             :             {
    7460          37 :                 poOvrDS.reset(GDALDataset::Open(pszOvrDS));
    7461          37 :                 if (!poOvrDS)
    7462             :                 {
    7463           0 :                     CSLDestroy(papszCreateOptions);
    7464           0 :                     return nullptr;
    7465             :                 }
    7466          37 :                 if (poOvrDS->GetRasterCount() != l_nBands)
    7467             :                 {
    7468           0 :                     CSLDestroy(papszCreateOptions);
    7469           0 :                     return nullptr;
    7470             :                 }
    7471          37 :                 nSrcOverviews =
    7472          37 :                     poOvrDS->GetRasterBand(1)->GetOverviewCount() + 1;
    7473             :             }
    7474             :         }
    7475             :         else
    7476             :         {
    7477         192 :             nSrcOverviews = poSrcDS->GetRasterBand(1)->GetOverviewCount();
    7478             :         }
    7479             : 
    7480             :         // Limit number of overviews if specified
    7481             :         const char *pszOverviewCount =
    7482         231 :             CSLFetchNameValue(papszCreateOptions, "@OVERVIEW_COUNT");
    7483         231 :         if (pszOverviewCount)
    7484           8 :             nSrcOverviews =
    7485           8 :                 std::max(0, std::min(nSrcOverviews, atoi(pszOverviewCount)));
    7486             : 
    7487         231 :         if (nSrcOverviews)
    7488             :         {
    7489         208 :             for (int j = 1; j <= l_nBands; ++j)
    7490             :             {
    7491             :                 const int nOtherBandOverviewCount =
    7492         136 :                     poOvrDS ? poOvrDS->GetRasterBand(j)->GetOverviewCount() + 1
    7493         200 :                             : poSrcDS->GetRasterBand(j)->GetOverviewCount();
    7494         136 :                 if (nOtherBandOverviewCount < nSrcOverviews)
    7495             :                 {
    7496           1 :                     ReportError(
    7497             :                         pszFilename, CE_Failure, CPLE_NotSupported,
    7498             :                         "COPY_SRC_OVERVIEWS cannot be used when the bands have "
    7499             :                         "not the same number of overview levels.");
    7500           1 :                     CSLDestroy(papszCreateOptions);
    7501           1 :                     return nullptr;
    7502             :                 }
    7503         395 :                 for (int i = 0; i < nSrcOverviews; ++i)
    7504             :                 {
    7505             :                     GDALRasterBand *poOvrBand =
    7506             :                         poOvrDS
    7507         361 :                             ? (i == 0 ? poOvrDS->GetRasterBand(j)
    7508         198 :                                       : poOvrDS->GetRasterBand(j)->GetOverview(
    7509          99 :                                             i - 1))
    7510         353 :                             : poSrcDS->GetRasterBand(j)->GetOverview(i);
    7511         262 :                     if (poOvrBand == nullptr)
    7512             :                     {
    7513           1 :                         ReportError(
    7514             :                             pszFilename, CE_Failure, CPLE_NotSupported,
    7515             :                             "COPY_SRC_OVERVIEWS cannot be used when one "
    7516             :                             "overview band is NULL.");
    7517           1 :                         CSLDestroy(papszCreateOptions);
    7518           1 :                         return nullptr;
    7519             :                     }
    7520             :                     GDALRasterBand *poOvrFirstBand =
    7521             :                         poOvrDS
    7522         360 :                             ? (i == 0 ? poOvrDS->GetRasterBand(1)
    7523         198 :                                       : poOvrDS->GetRasterBand(1)->GetOverview(
    7524          99 :                                             i - 1))
    7525         351 :                             : poSrcDS->GetRasterBand(1)->GetOverview(i);
    7526         521 :                     if (poOvrBand->GetXSize() != poOvrFirstBand->GetXSize() ||
    7527         260 :                         poOvrBand->GetYSize() != poOvrFirstBand->GetYSize())
    7528             :                     {
    7529           1 :                         ReportError(
    7530             :                             pszFilename, CE_Failure, CPLE_NotSupported,
    7531             :                             "COPY_SRC_OVERVIEWS cannot be used when the "
    7532             :                             "overview bands have not the same dimensions "
    7533             :                             "among bands.");
    7534           1 :                         CSLDestroy(papszCreateOptions);
    7535           1 :                         return nullptr;
    7536             :                     }
    7537             :                 }
    7538             :             }
    7539             : 
    7540         205 :             for (int i = 0; i < nSrcOverviews; ++i)
    7541             :             {
    7542             :                 GDALRasterBand *poOvrFirstBand =
    7543             :                     poOvrDS
    7544         211 :                         ? (i == 0
    7545          78 :                                ? poOvrDS->GetRasterBand(1)
    7546          41 :                                : poOvrDS->GetRasterBand(1)->GetOverview(i - 1))
    7547         188 :                         : poSrcDS->GetRasterBand(1)->GetOverview(i);
    7548         133 :                 dfExtraSpaceForOverviews +=
    7549         133 :                     static_cast<double>(poOvrFirstBand->GetXSize()) *
    7550         133 :                     poOvrFirstBand->GetYSize();
    7551             :             }
    7552          72 :             dfExtraSpaceForOverviews *=
    7553          72 :                 l_nBands * GDALGetDataTypeSizeBytes(eType);
    7554             :         }
    7555             :         else
    7556             :         {
    7557         156 :             CPLDebug("GTiff", "No source overviews to copy");
    7558             :         }
    7559             :     }
    7560             : 
    7561             : /* -------------------------------------------------------------------- */
    7562             : /*      Should we use optimized way of copying from an input JPEG       */
    7563             : /*      dataset?                                                        */
    7564             : /* -------------------------------------------------------------------- */
    7565             : 
    7566             : // TODO(schwehr): Refactor bDirectCopyFromJPEG to be a const.
    7567             : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
    7568        2161 :     bool bDirectCopyFromJPEG = false;
    7569             : #endif
    7570             : 
    7571             :     // Note: JPEG_DIRECT_COPY is not defined by default, because it is mainly
    7572             :     // useful for debugging purposes.
    7573             : #ifdef JPEG_DIRECT_COPY
    7574             :     if (CPLFetchBool(papszCreateOptions, "JPEG_DIRECT_COPY", false) &&
    7575             :         GTIFF_CanDirectCopyFromJPEG(poSrcDS, papszCreateOptions))
    7576             :     {
    7577             :         CPLDebug("GTiff", "Using special direct copy mode from a JPEG dataset");
    7578             : 
    7579             :         bDirectCopyFromJPEG = true;
    7580             :     }
    7581             : #endif
    7582             : 
    7583             : #ifdef HAVE_LIBJPEG
    7584        2161 :     bool bCopyFromJPEG = false;
    7585             : 
    7586             :     // When CreateCopy'ing() from a JPEG dataset, and asking for COMPRESS=JPEG,
    7587             :     // use DCT coefficients (unless other options are incompatible, like
    7588             :     // strip/tile dimensions, specifying JPEG_QUALITY option, incompatible
    7589             :     // PHOTOMETRIC with the source colorspace, etc.) to avoid the lossy steps
    7590             :     // involved by decompression/recompression.
    7591        4322 :     if (!bDirectCopyFromJPEG &&
    7592        2161 :         GTIFF_CanCopyFromJPEG(poSrcDS, papszCreateOptions))
    7593             :     {
    7594          12 :         CPLDebug("GTiff", "Using special copy mode from a JPEG dataset");
    7595             : 
    7596          12 :         bCopyFromJPEG = true;
    7597             :     }
    7598             : #endif
    7599             : 
    7600             :     /* -------------------------------------------------------------------- */
    7601             :     /*      If the source is RGB, then set the PHOTOMETRIC=RGB value        */
    7602             :     /* -------------------------------------------------------------------- */
    7603             : 
    7604             :     const bool bForcePhotometric =
    7605        2161 :         CSLFetchNameValue(papszOptions, "PHOTOMETRIC") != nullptr;
    7606             : 
    7607        1229 :     if (l_nBands >= 3 && !bForcePhotometric &&
    7608             : #ifdef HAVE_LIBJPEG
    7609        1191 :         !bCopyFromJPEG &&
    7610             : #endif
    7611        1185 :         poSrcDS->GetRasterBand(1)->GetColorInterpretation() == GCI_RedBand &&
    7612        4463 :         poSrcDS->GetRasterBand(2)->GetColorInterpretation() == GCI_GreenBand &&
    7613        1073 :         poSrcDS->GetRasterBand(3)->GetColorInterpretation() == GCI_BlueBand)
    7614             :     {
    7615        1067 :         papszCreateOptions =
    7616        1067 :             CSLSetNameValue(papszCreateOptions, "PHOTOMETRIC", "RGB");
    7617             :     }
    7618             : 
    7619             :     /* -------------------------------------------------------------------- */
    7620             :     /*      Create the file.                                                */
    7621             :     /* -------------------------------------------------------------------- */
    7622        2161 :     VSILFILE *l_fpL = nullptr;
    7623        4322 :     CPLString l_osTmpFilename;
    7624             : 
    7625        2161 :     const int nXSize = poSrcDS->GetRasterXSize();
    7626        2161 :     const int nYSize = poSrcDS->GetRasterYSize();
    7627             : 
    7628             :     const int nColorTableMultiplier = std::max(
    7629        4322 :         1,
    7630        4322 :         std::min(257,
    7631        2161 :                  atoi(CSLFetchNameValueDef(
    7632             :                      papszOptions, "COLOR_TABLE_MULTIPLIER",
    7633        2161 :                      CPLSPrintf("%d", DEFAULT_COLOR_TABLE_MULTIPLIER_257)))));
    7634             : 
    7635        2161 :     bool bTileInterleaving = false;
    7636        2161 :     TIFF *l_hTIFF = CreateLL(pszFilename, nXSize, nYSize, l_nBands, eType,
    7637             :                              dfExtraSpaceForOverviews, nColorTableMultiplier,
    7638             :                              papszCreateOptions, &l_fpL, l_osTmpFilename,
    7639             :                              /* bCreateCopy = */ true, bTileInterleaving);
    7640        2161 :     const bool bStreaming = !l_osTmpFilename.empty();
    7641             : 
    7642        2161 :     CSLDestroy(papszCreateOptions);
    7643        2161 :     papszCreateOptions = nullptr;
    7644             : 
    7645        2161 :     if (l_hTIFF == nullptr)
    7646             :     {
    7647          18 :         if (bStreaming)
    7648           0 :             VSIUnlink(l_osTmpFilename);
    7649          18 :         return nullptr;
    7650             :     }
    7651             : 
    7652        2143 :     uint16_t l_nPlanarConfig = 0;
    7653        2143 :     TIFFGetField(l_hTIFF, TIFFTAG_PLANARCONFIG, &l_nPlanarConfig);
    7654             : 
    7655        2143 :     uint16_t l_nCompression = 0;
    7656             : 
    7657        2143 :     if (!TIFFGetField(l_hTIFF, TIFFTAG_COMPRESSION, &(l_nCompression)))
    7658           0 :         l_nCompression = COMPRESSION_NONE;
    7659             : 
    7660             :     /* -------------------------------------------------------------------- */
    7661             :     /*      Set the alpha channel if we find one.                           */
    7662             :     /* -------------------------------------------------------------------- */
    7663        2143 :     uint16_t *extraSamples = nullptr;
    7664        2143 :     uint16_t nExtraSamples = 0;
    7665        2143 :     if (TIFFGetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, &nExtraSamples,
    7666        2408 :                      &extraSamples) &&
    7667         265 :         nExtraSamples > 0)
    7668             :     {
    7669             :         // We need to allocate a new array as (current) libtiff
    7670             :         // versions will not like that we reuse the array we got from
    7671             :         // TIFFGetField().
    7672             :         uint16_t *pasNewExtraSamples = static_cast<uint16_t *>(
    7673         265 :             CPLMalloc(nExtraSamples * sizeof(uint16_t)));
    7674         265 :         memcpy(pasNewExtraSamples, extraSamples,
    7675         265 :                nExtraSamples * sizeof(uint16_t));
    7676         265 :         const char *pszAlpha = CPLGetConfigOption(
    7677             :             "GTIFF_ALPHA", CSLFetchNameValue(papszOptions, "ALPHA"));
    7678             :         const uint16_t nAlpha =
    7679         265 :             GTiffGetAlphaValue(pszAlpha, DEFAULT_ALPHA_TYPE);
    7680         265 :         const int nBaseSamples = l_nBands - nExtraSamples;
    7681         895 :         for (int iExtraBand = nBaseSamples + 1; iExtraBand <= l_nBands;
    7682             :              iExtraBand++)
    7683             :         {
    7684         630 :             if (poSrcDS->GetRasterBand(iExtraBand)->GetColorInterpretation() ==
    7685             :                 GCI_AlphaBand)
    7686             :             {
    7687         145 :                 pasNewExtraSamples[iExtraBand - nBaseSamples - 1] = nAlpha;
    7688         145 :                 if (!pszAlpha)
    7689             :                 {
    7690             :                     // Use the ALPHA metadata item from the source band, when
    7691             :                     // present, if no explicit ALPHA creation option
    7692         286 :                     pasNewExtraSamples[iExtraBand - nBaseSamples - 1] =
    7693         143 :                         GTiffGetAlphaValue(
    7694         143 :                             poSrcDS->GetRasterBand(iExtraBand)
    7695         143 :                                 ->GetMetadataItem("ALPHA", "IMAGE_STRUCTURE"),
    7696             :                             nAlpha);
    7697             :                 }
    7698             :             }
    7699             :         }
    7700         265 :         TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, nExtraSamples,
    7701             :                      pasNewExtraSamples);
    7702             : 
    7703         265 :         CPLFree(pasNewExtraSamples);
    7704             :     }
    7705             : 
    7706             :     /* -------------------------------------------------------------------- */
    7707             :     /*      If the output is jpeg compressed, and the input is RGB make     */
    7708             :     /*      sure we note that.                                              */
    7709             :     /* -------------------------------------------------------------------- */
    7710             : 
    7711        2143 :     if (l_nCompression == COMPRESSION_JPEG)
    7712             :     {
    7713         134 :         if (l_nBands >= 3 &&
    7714          58 :             (poSrcDS->GetRasterBand(1)->GetColorInterpretation() ==
    7715           0 :              GCI_YCbCr_YBand) &&
    7716           0 :             (poSrcDS->GetRasterBand(2)->GetColorInterpretation() ==
    7717         134 :              GCI_YCbCr_CbBand) &&
    7718           0 :             (poSrcDS->GetRasterBand(3)->GetColorInterpretation() ==
    7719             :              GCI_YCbCr_CrBand))
    7720             :         {
    7721             :             // Do nothing.
    7722             :         }
    7723             :         else
    7724             :         {
    7725             :             // Assume RGB if it is not explicitly YCbCr.
    7726          76 :             CPLDebug("GTiff", "Setting JPEGCOLORMODE_RGB");
    7727          76 :             TIFFSetField(l_hTIFF, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
    7728             :         }
    7729             :     }
    7730             : 
    7731             :     /* -------------------------------------------------------------------- */
    7732             :     /*      Does the source image consist of one band, with a palette?      */
    7733             :     /*      If so, copy over.                                               */
    7734             :     /* -------------------------------------------------------------------- */
    7735        1315 :     if ((l_nBands == 1 || l_nBands == 2) &&
    7736        3458 :         poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr &&
    7737             :         eType == GDT_UInt8)
    7738             :     {
    7739          21 :         unsigned short anTRed[256] = {0};
    7740          21 :         unsigned short anTGreen[256] = {0};
    7741          21 :         unsigned short anTBlue[256] = {0};
    7742          21 :         GDALColorTable *poCT = poSrcDS->GetRasterBand(1)->GetColorTable();
    7743             : 
    7744        5397 :         for (int iColor = 0; iColor < 256; ++iColor)
    7745             :         {
    7746        5376 :             if (iColor < poCT->GetColorEntryCount())
    7747             :             {
    7748        4241 :                 GDALColorEntry sRGB = {0, 0, 0, 0};
    7749             : 
    7750        4241 :                 poCT->GetColorEntryAsRGB(iColor, &sRGB);
    7751             : 
    7752        8482 :                 anTRed[iColor] = GTiffDataset::ClampCTEntry(
    7753        4241 :                     iColor, 1, sRGB.c1, nColorTableMultiplier);
    7754        8482 :                 anTGreen[iColor] = GTiffDataset::ClampCTEntry(
    7755        4241 :                     iColor, 2, sRGB.c2, nColorTableMultiplier);
    7756        4241 :                 anTBlue[iColor] = GTiffDataset::ClampCTEntry(
    7757        4241 :                     iColor, 3, sRGB.c3, nColorTableMultiplier);
    7758             :             }
    7759             :             else
    7760             :             {
    7761        1135 :                 anTRed[iColor] = 0;
    7762        1135 :                 anTGreen[iColor] = 0;
    7763        1135 :                 anTBlue[iColor] = 0;
    7764             :             }
    7765             :         }
    7766             : 
    7767          21 :         if (!bForcePhotometric)
    7768          21 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
    7769          21 :         TIFFSetField(l_hTIFF, TIFFTAG_COLORMAP, anTRed, anTGreen, anTBlue);
    7770             :     }
    7771        1314 :     else if ((l_nBands == 1 || l_nBands == 2) &&
    7772        3436 :              poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr &&
    7773             :              eType == GDT_UInt16)
    7774             :     {
    7775             :         unsigned short *panTRed = static_cast<unsigned short *>(
    7776           1 :             CPLMalloc(65536 * sizeof(unsigned short)));
    7777             :         unsigned short *panTGreen = static_cast<unsigned short *>(
    7778           1 :             CPLMalloc(65536 * sizeof(unsigned short)));
    7779             :         unsigned short *panTBlue = static_cast<unsigned short *>(
    7780           1 :             CPLMalloc(65536 * sizeof(unsigned short)));
    7781             : 
    7782           1 :         GDALColorTable *poCT = poSrcDS->GetRasterBand(1)->GetColorTable();
    7783             : 
    7784       65537 :         for (int iColor = 0; iColor < 65536; ++iColor)
    7785             :         {
    7786       65536 :             if (iColor < poCT->GetColorEntryCount())
    7787             :             {
    7788       65536 :                 GDALColorEntry sRGB = {0, 0, 0, 0};
    7789             : 
    7790       65536 :                 poCT->GetColorEntryAsRGB(iColor, &sRGB);
    7791             : 
    7792      131072 :                 panTRed[iColor] = GTiffDataset::ClampCTEntry(
    7793       65536 :                     iColor, 1, sRGB.c1, nColorTableMultiplier);
    7794      131072 :                 panTGreen[iColor] = GTiffDataset::ClampCTEntry(
    7795       65536 :                     iColor, 2, sRGB.c2, nColorTableMultiplier);
    7796       65536 :                 panTBlue[iColor] = GTiffDataset::ClampCTEntry(
    7797       65536 :                     iColor, 3, sRGB.c3, nColorTableMultiplier);
    7798             :             }
    7799             :             else
    7800             :             {
    7801           0 :                 panTRed[iColor] = 0;
    7802           0 :                 panTGreen[iColor] = 0;
    7803           0 :                 panTBlue[iColor] = 0;
    7804             :             }
    7805             :         }
    7806             : 
    7807           1 :         if (!bForcePhotometric)
    7808           1 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
    7809           1 :         TIFFSetField(l_hTIFF, TIFFTAG_COLORMAP, panTRed, panTGreen, panTBlue);
    7810             : 
    7811           1 :         CPLFree(panTRed);
    7812           1 :         CPLFree(panTGreen);
    7813           1 :         CPLFree(panTBlue);
    7814             :     }
    7815        2121 :     else if (poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr)
    7816           1 :         ReportError(
    7817             :             pszFilename, CE_Failure, CPLE_AppDefined,
    7818             :             "Unable to export color table to GeoTIFF file.  Color tables "
    7819             :             "can only be written to 1 band or 2 bands Byte or "
    7820             :             "UInt16 GeoTIFF files.");
    7821             : 
    7822        2143 :     if (l_nCompression == COMPRESSION_JPEG)
    7823             :     {
    7824          76 :         uint16_t l_nPhotometric = 0;
    7825          76 :         TIFFGetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, &l_nPhotometric);
    7826             :         // Check done in tif_jpeg.c later, but not with a very clear error
    7827             :         // message
    7828          76 :         if (l_nPhotometric == PHOTOMETRIC_PALETTE)
    7829             :         {
    7830           1 :             ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    7831             :                         "JPEG compression not supported with paletted image");
    7832           1 :             XTIFFClose(l_hTIFF);
    7833           1 :             VSIUnlink(l_osTmpFilename);
    7834           1 :             CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    7835           1 :             return nullptr;
    7836             :         }
    7837             :     }
    7838             : 
    7839        2229 :     if (l_nBands == 2 &&
    7840        2142 :         poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr &&
    7841           0 :         (eType == GDT_UInt8 || eType == GDT_UInt16))
    7842             :     {
    7843           1 :         uint16_t v[1] = {EXTRASAMPLE_UNASSALPHA};
    7844             : 
    7845           1 :         TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, 1, v);
    7846             :     }
    7847             : 
    7848        2142 :     const int nMaskFlags = poSrcDS->GetRasterBand(1)->GetMaskFlags();
    7849        2142 :     bool bCreateMask = false;
    7850        4284 :     CPLString osHiddenStructuralMD;
    7851             :     const char *pszInterleave =
    7852        2142 :         CSLFetchNameValueDef(papszOptions, "INTERLEAVE", "PIXEL");
    7853        2367 :     if (bCopySrcOverviews &&
    7854         225 :         CPLTestBool(CSLFetchNameValueDef(papszOptions, "TILED", "NO")))
    7855             :     {
    7856         213 :         osHiddenStructuralMD += "LAYOUT=IFDS_BEFORE_DATA\n";
    7857         213 :         osHiddenStructuralMD += "BLOCK_ORDER=ROW_MAJOR\n";
    7858         213 :         osHiddenStructuralMD += "BLOCK_LEADER=SIZE_AS_UINT4\n";
    7859         213 :         osHiddenStructuralMD += "BLOCK_TRAILER=LAST_4_BYTES_REPEATED\n";
    7860         213 :         if (l_nBands > 1 && !EQUAL(pszInterleave, "PIXEL"))
    7861             :         {
    7862          21 :             osHiddenStructuralMD += "INTERLEAVE=";
    7863          21 :             osHiddenStructuralMD += CPLString(pszInterleave).toupper();
    7864          21 :             osHiddenStructuralMD += "\n";
    7865             :         }
    7866             :         osHiddenStructuralMD +=
    7867         213 :             "KNOWN_INCOMPATIBLE_EDITION=NO\n ";  // Final space intended, so
    7868             :                                                  // this can be replaced by YES
    7869             :     }
    7870        2142 :     if (!(nMaskFlags & (GMF_ALL_VALID | GMF_ALPHA | GMF_NODATA)) &&
    7871          42 :         (nMaskFlags & GMF_PER_DATASET) && !bStreaming)
    7872             :     {
    7873          38 :         bCreateMask = true;
    7874          38 :         if (GTiffDataset::MustCreateInternalMask() &&
    7875          38 :             !osHiddenStructuralMD.empty() && EQUAL(pszInterleave, "PIXEL"))
    7876             :         {
    7877          21 :             osHiddenStructuralMD += "MASK_INTERLEAVED_WITH_IMAGERY=YES\n";
    7878             :         }
    7879             :     }
    7880        2355 :     if (!osHiddenStructuralMD.empty() &&
    7881         213 :         CPLTestBool(CPLGetConfigOption("GTIFF_WRITE_COG_GHOST_AREA", "YES")))
    7882             :     {
    7883         212 :         const int nHiddenMDSize = static_cast<int>(osHiddenStructuralMD.size());
    7884             :         osHiddenStructuralMD =
    7885         212 :             CPLOPrintf("GDAL_STRUCTURAL_METADATA_SIZE=%06d bytes\n",
    7886         424 :                        nHiddenMDSize) +
    7887         212 :             osHiddenStructuralMD;
    7888         212 :         VSI_TIFFWrite(l_hTIFF, osHiddenStructuralMD.c_str(),
    7889             :                       osHiddenStructuralMD.size());
    7890             :     }
    7891             : 
    7892             :     // FIXME? libtiff writes extended tags in the order they are specified
    7893             :     // and not in increasing order.
    7894             : 
    7895             :     /* -------------------------------------------------------------------- */
    7896             :     /*      Transfer some TIFF specific metadata, if available.             */
    7897             :     /*      The return value will tell us if we need to try again later with*/
    7898             :     /*      PAM because the profile doesn't allow to write some metadata    */
    7899             :     /*      as TIFF tag                                                     */
    7900             :     /* -------------------------------------------------------------------- */
    7901        2142 :     const bool bHasWrittenMDInGeotiffTAG = GTiffDataset::WriteMetadata(
    7902             :         poSrcDS, l_hTIFF, false, eProfile, pszFilename, papszOptions);
    7903             : 
    7904             :     /* -------------------------------------------------------------------- */
    7905             :     /*      Write NoData value, if exist.                                   */
    7906             :     /* -------------------------------------------------------------------- */
    7907        2142 :     if (eProfile == GTiffProfile::GDALGEOTIFF)
    7908             :     {
    7909        2121 :         int bSuccess = FALSE;
    7910        2121 :         GDALRasterBand *poFirstBand = poSrcDS->GetRasterBand(1);
    7911        2121 :         if (poFirstBand->GetRasterDataType() == GDT_Int64)
    7912             :         {
    7913           4 :             const auto nNoData = poFirstBand->GetNoDataValueAsInt64(&bSuccess);
    7914           4 :             if (bSuccess)
    7915           1 :                 GTiffDataset::WriteNoDataValue(l_hTIFF, nNoData);
    7916             :         }
    7917        2117 :         else if (poFirstBand->GetRasterDataType() == GDT_UInt64)
    7918             :         {
    7919           4 :             const auto nNoData = poFirstBand->GetNoDataValueAsUInt64(&bSuccess);
    7920           4 :             if (bSuccess)
    7921           1 :                 GTiffDataset::WriteNoDataValue(l_hTIFF, nNoData);
    7922             :         }
    7923             :         else
    7924             :         {
    7925        2113 :             const auto dfNoData = poFirstBand->GetNoDataValue(&bSuccess);
    7926        2113 :             if (bSuccess)
    7927         145 :                 GTiffDataset::WriteNoDataValue(l_hTIFF, dfNoData);
    7928             :         }
    7929             :     }
    7930             : 
    7931             :     /* -------------------------------------------------------------------- */
    7932             :     /*      Are we addressing PixelIsPoint mode?                            */
    7933             :     /* -------------------------------------------------------------------- */
    7934        2142 :     bool bPixelIsPoint = false;
    7935        2142 :     bool bPointGeoIgnore = false;
    7936             : 
    7937        3595 :     if (poSrcDS->GetMetadataItem(GDALMD_AREA_OR_POINT) &&
    7938        1453 :         EQUAL(poSrcDS->GetMetadataItem(GDALMD_AREA_OR_POINT), GDALMD_AOP_POINT))
    7939             :     {
    7940          10 :         bPixelIsPoint = true;
    7941             :         bPointGeoIgnore =
    7942          10 :             CPLTestBool(CPLGetConfigOption("GTIFF_POINT_GEO_IGNORE", "FALSE"));
    7943             :     }
    7944             : 
    7945             :     /* -------------------------------------------------------------------- */
    7946             :     /*      Write affine transform if it is meaningful.                     */
    7947             :     /* -------------------------------------------------------------------- */
    7948        2142 :     const OGRSpatialReference *l_poSRS = nullptr;
    7949        2142 :     GDALGeoTransform l_gt;
    7950        2142 :     if (poSrcDS->GetGeoTransform(l_gt) == CE_None)
    7951             :     {
    7952        1702 :         if (bGeoTIFF)
    7953             :         {
    7954        1697 :             l_poSRS = poSrcDS->GetSpatialRef();
    7955             : 
    7956        1697 :             if (l_gt.xrot == 0.0 && l_gt.yrot == 0.0 && l_gt.yscale < 0.0)
    7957             :             {
    7958        1689 :                 double dfOffset = 0.0;
    7959             :                 {
    7960             :                     // In the case the SRS has a vertical component and we have
    7961             :                     // a single band, encode its scale/offset in the GeoTIFF
    7962             :                     // tags
    7963        1689 :                     int bHasScale = FALSE;
    7964             :                     double dfScale =
    7965        1689 :                         poSrcDS->GetRasterBand(1)->GetScale(&bHasScale);
    7966        1689 :                     int bHasOffset = FALSE;
    7967             :                     dfOffset =
    7968        1689 :                         poSrcDS->GetRasterBand(1)->GetOffset(&bHasOffset);
    7969             :                     const bool bApplyScaleOffset =
    7970        1693 :                         l_poSRS && l_poSRS->IsVertical() &&
    7971           4 :                         poSrcDS->GetRasterCount() == 1;
    7972        1689 :                     if (bApplyScaleOffset && !bHasScale)
    7973           0 :                         dfScale = 1.0;
    7974        1689 :                     if (!bApplyScaleOffset || !bHasOffset)
    7975        1685 :                         dfOffset = 0.0;
    7976             :                     const double adfPixelScale[3] = {
    7977        1689 :                         l_gt.xscale, fabs(l_gt.yscale),
    7978        1689 :                         bApplyScaleOffset ? dfScale : 0.0};
    7979             : 
    7980        1689 :                     TIFFSetField(l_hTIFF, TIFFTAG_GEOPIXELSCALE, 3,
    7981             :                                  adfPixelScale);
    7982             :                 }
    7983             : 
    7984        1689 :                 double adfTiePoints[6] = {0.0,        0.0,        0.0,
    7985        1689 :                                           l_gt.xorig, l_gt.yorig, dfOffset};
    7986             : 
    7987        1689 :                 if (bPixelIsPoint && !bPointGeoIgnore)
    7988             :                 {
    7989           6 :                     adfTiePoints[3] += l_gt.xscale * 0.5 + l_gt.xrot * 0.5;
    7990           6 :                     adfTiePoints[4] += l_gt.yrot * 0.5 + l_gt.yscale * 0.5;
    7991             :                 }
    7992             : 
    7993        1689 :                 TIFFSetField(l_hTIFF, TIFFTAG_GEOTIEPOINTS, 6, adfTiePoints);
    7994             :             }
    7995             :             else
    7996             :             {
    7997           8 :                 double adfMatrix[16] = {0.0};
    7998             : 
    7999           8 :                 adfMatrix[0] = l_gt.xscale;
    8000           8 :                 adfMatrix[1] = l_gt.xrot;
    8001           8 :                 adfMatrix[3] = l_gt.xorig;
    8002           8 :                 adfMatrix[4] = l_gt.yrot;
    8003           8 :                 adfMatrix[5] = l_gt.yscale;
    8004           8 :                 adfMatrix[7] = l_gt.yorig;
    8005           8 :                 adfMatrix[15] = 1.0;
    8006             : 
    8007           8 :                 if (bPixelIsPoint && !bPointGeoIgnore)
    8008             :                 {
    8009           0 :                     adfMatrix[3] += l_gt.xscale * 0.5 + l_gt.xrot * 0.5;
    8010           0 :                     adfMatrix[7] += l_gt.yrot * 0.5 + l_gt.yscale * 0.5;
    8011             :                 }
    8012             : 
    8013           8 :                 TIFFSetField(l_hTIFF, TIFFTAG_GEOTRANSMATRIX, 16, adfMatrix);
    8014             :             }
    8015             :         }
    8016             : 
    8017             :         /* --------------------------------------------------------------------
    8018             :          */
    8019             :         /*      Do we need a TFW file? */
    8020             :         /* --------------------------------------------------------------------
    8021             :          */
    8022        1702 :         if (CPLFetchBool(papszOptions, "TFW", false))
    8023           2 :             GDALWriteWorldFile(pszFilename, "tfw", l_gt.data());
    8024        1700 :         else if (CPLFetchBool(papszOptions, "WORLDFILE", false))
    8025           1 :             GDALWriteWorldFile(pszFilename, "wld", l_gt.data());
    8026             :     }
    8027             : 
    8028             :     /* -------------------------------------------------------------------- */
    8029             :     /*      Otherwise write tiepoints if they are available.                */
    8030             :     /* -------------------------------------------------------------------- */
    8031         440 :     else if (poSrcDS->GetGCPCount() > 0 && bGeoTIFF)
    8032             :     {
    8033          12 :         const GDAL_GCP *pasGCPs = poSrcDS->GetGCPs();
    8034             :         double *padfTiePoints = static_cast<double *>(
    8035          12 :             CPLMalloc(6 * sizeof(double) * poSrcDS->GetGCPCount()));
    8036             : 
    8037          60 :         for (int iGCP = 0; iGCP < poSrcDS->GetGCPCount(); ++iGCP)
    8038             :         {
    8039             : 
    8040          48 :             padfTiePoints[iGCP * 6 + 0] = pasGCPs[iGCP].dfGCPPixel;
    8041          48 :             padfTiePoints[iGCP * 6 + 1] = pasGCPs[iGCP].dfGCPLine;
    8042          48 :             padfTiePoints[iGCP * 6 + 2] = 0;
    8043          48 :             padfTiePoints[iGCP * 6 + 3] = pasGCPs[iGCP].dfGCPX;
    8044          48 :             padfTiePoints[iGCP * 6 + 4] = pasGCPs[iGCP].dfGCPY;
    8045          48 :             padfTiePoints[iGCP * 6 + 5] = pasGCPs[iGCP].dfGCPZ;
    8046             : 
    8047          48 :             if (bPixelIsPoint && !bPointGeoIgnore)
    8048             :             {
    8049           4 :                 padfTiePoints[iGCP * 6 + 0] -= 0.5;
    8050           4 :                 padfTiePoints[iGCP * 6 + 1] -= 0.5;
    8051             :             }
    8052             :         }
    8053             : 
    8054          12 :         TIFFSetField(l_hTIFF, TIFFTAG_GEOTIEPOINTS, 6 * poSrcDS->GetGCPCount(),
    8055             :                      padfTiePoints);
    8056          12 :         CPLFree(padfTiePoints);
    8057             : 
    8058          12 :         l_poSRS = poSrcDS->GetGCPSpatialRef();
    8059             : 
    8060          24 :         if (CPLFetchBool(papszOptions, "TFW", false) ||
    8061          12 :             CPLFetchBool(papszOptions, "WORLDFILE", false))
    8062             :         {
    8063           0 :             ReportError(
    8064             :                 pszFilename, CE_Warning, CPLE_AppDefined,
    8065             :                 "TFW=ON or WORLDFILE=ON creation options are ignored when "
    8066             :                 "GCPs are available");
    8067             :         }
    8068             :     }
    8069             :     else
    8070             :     {
    8071         428 :         l_poSRS = poSrcDS->GetSpatialRef();
    8072             :     }
    8073             : 
    8074             :     /* -------------------------------------------------------------------- */
    8075             :     /*      Copy xml:XMP data                                               */
    8076             :     /* -------------------------------------------------------------------- */
    8077        2142 :     CSLConstList papszXMP = poSrcDS->GetMetadata("xml:XMP");
    8078        2142 :     if (papszXMP != nullptr && *papszXMP != nullptr)
    8079             :     {
    8080           9 :         int nTagSize = static_cast<int>(strlen(*papszXMP));
    8081           9 :         TIFFSetField(l_hTIFF, TIFFTAG_XMLPACKET, nTagSize, *papszXMP);
    8082             :     }
    8083             : 
    8084             :     /* -------------------------------------------------------------------- */
    8085             :     /*      Write the projection information, if possible.                  */
    8086             :     /* -------------------------------------------------------------------- */
    8087        2142 :     const bool bHasProjection = l_poSRS != nullptr;
    8088        2142 :     bool bExportSRSToPAM = false;
    8089        2142 :     if ((bHasProjection || bPixelIsPoint) && bGeoTIFF)
    8090             :     {
    8091        1675 :         GTIF *psGTIF = GTiffDataset::GTIFNew(l_hTIFF);
    8092             : 
    8093        1675 :         if (bHasProjection)
    8094             :         {
    8095        1675 :             const auto eGeoTIFFKeysFlavor = GetGTIFFKeysFlavor(papszOptions);
    8096        1675 :             if (IsSRSCompatibleOfGeoTIFF(l_poSRS, eGeoTIFFKeysFlavor))
    8097             :             {
    8098        1675 :                 GTIFSetFromOGISDefnEx(
    8099             :                     psGTIF,
    8100             :                     OGRSpatialReference::ToHandle(
    8101             :                         const_cast<OGRSpatialReference *>(l_poSRS)),
    8102             :                     eGeoTIFFKeysFlavor, GetGeoTIFFVersion(papszOptions));
    8103             :             }
    8104             :             else
    8105             :             {
    8106           0 :                 bExportSRSToPAM = true;
    8107             :             }
    8108             :         }
    8109             : 
    8110        1675 :         if (bPixelIsPoint)
    8111             :         {
    8112          10 :             GTIFKeySet(psGTIF, GTRasterTypeGeoKey, TYPE_SHORT, 1,
    8113             :                        RasterPixelIsPoint);
    8114             :         }
    8115             : 
    8116        1675 :         GTIFWriteKeys(psGTIF);
    8117        1675 :         GTIFFree(psGTIF);
    8118             :     }
    8119             : 
    8120        2142 :     bool l_bDontReloadFirstBlock = false;
    8121             : 
    8122             : #ifdef HAVE_LIBJPEG
    8123        2142 :     if (bCopyFromJPEG)
    8124             :     {
    8125          12 :         GTIFF_CopyFromJPEG_WriteAdditionalTags(l_hTIFF, poSrcDS);
    8126             :     }
    8127             : #endif
    8128             : 
    8129             :     /* -------------------------------------------------------------------- */
    8130             :     /*      Cleanup                                                         */
    8131             :     /* -------------------------------------------------------------------- */
    8132        2142 :     if (bCopySrcOverviews)
    8133             :     {
    8134         225 :         TIFFDeferStrileArrayWriting(l_hTIFF);
    8135             :     }
    8136        2142 :     TIFFWriteCheck(l_hTIFF, TIFFIsTiled(l_hTIFF), "GTiffCreateCopy()");
    8137        2142 :     TIFFWriteDirectory(l_hTIFF);
    8138        2142 :     if (bStreaming)
    8139             :     {
    8140             :         // We need to write twice the directory to be sure that custom
    8141             :         // TIFF tags are correctly sorted and that padding bytes have been
    8142             :         // added.
    8143           5 :         TIFFSetDirectory(l_hTIFF, 0);
    8144           5 :         TIFFWriteDirectory(l_hTIFF);
    8145             : 
    8146           5 :         if (VSIFSeekL(l_fpL, 0, SEEK_END) != 0)
    8147           0 :             ReportError(pszFilename, CE_Failure, CPLE_FileIO, "Cannot seek");
    8148           5 :         const int nSize = static_cast<int>(VSIFTellL(l_fpL));
    8149             : 
    8150           5 :         vsi_l_offset nDataLength = 0;
    8151           5 :         VSIGetMemFileBuffer(l_osTmpFilename, &nDataLength, FALSE);
    8152           5 :         TIFFSetDirectory(l_hTIFF, 0);
    8153           5 :         GTiffFillStreamableOffsetAndCount(l_hTIFF, nSize);
    8154           5 :         TIFFWriteDirectory(l_hTIFF);
    8155             :     }
    8156        2142 :     const auto nDirCount = TIFFNumberOfDirectories(l_hTIFF);
    8157        2142 :     if (nDirCount >= 1)
    8158             :     {
    8159        2135 :         TIFFSetDirectory(l_hTIFF, static_cast<tdir_t>(nDirCount - 1));
    8160             :     }
    8161        2142 :     const toff_t l_nDirOffset = TIFFCurrentDirOffset(l_hTIFF);
    8162        2142 :     TIFFFlush(l_hTIFF);
    8163        2142 :     XTIFFClose(l_hTIFF);
    8164             : 
    8165        2142 :     VSIFSeekL(l_fpL, 0, SEEK_SET);
    8166             : 
    8167             :     // fpStreaming will assigned to the instance and not closed here.
    8168        2142 :     VSILFILE *fpStreaming = nullptr;
    8169        2142 :     if (bStreaming)
    8170             :     {
    8171           5 :         vsi_l_offset nDataLength = 0;
    8172             :         void *pabyBuffer =
    8173           5 :             VSIGetMemFileBuffer(l_osTmpFilename, &nDataLength, FALSE);
    8174           5 :         fpStreaming = VSIFOpenL(pszFilename, "wb");
    8175           5 :         if (fpStreaming == nullptr)
    8176             :         {
    8177           1 :             VSIUnlink(l_osTmpFilename);
    8178           1 :             CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    8179           1 :             return nullptr;
    8180             :         }
    8181           4 :         if (static_cast<vsi_l_offset>(VSIFWriteL(pabyBuffer, 1,
    8182             :                                                  static_cast<int>(nDataLength),
    8183           4 :                                                  fpStreaming)) != nDataLength)
    8184             :         {
    8185           0 :             ReportError(pszFilename, CE_Failure, CPLE_FileIO,
    8186             :                         "Could not write %d bytes",
    8187             :                         static_cast<int>(nDataLength));
    8188           0 :             CPL_IGNORE_RET_VAL(VSIFCloseL(fpStreaming));
    8189           0 :             VSIUnlink(l_osTmpFilename);
    8190           0 :             CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    8191           0 :             return nullptr;
    8192             :         }
    8193             :     }
    8194             : 
    8195             :     /* -------------------------------------------------------------------- */
    8196             :     /*      Re-open as a dataset and copy over missing metadata using       */
    8197             :     /*      PAM facilities.                                                 */
    8198             :     /* -------------------------------------------------------------------- */
    8199        2141 :     l_hTIFF = VSI_TIFFOpen(bStreaming ? l_osTmpFilename.c_str() : pszFilename,
    8200             :                            "r+", l_fpL);
    8201        2141 :     if (l_hTIFF == nullptr)
    8202             :     {
    8203          11 :         if (bStreaming)
    8204           0 :             VSIUnlink(l_osTmpFilename);
    8205          11 :         l_fpL->CancelCreation();
    8206          11 :         CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    8207          11 :         return nullptr;
    8208             :     }
    8209             : 
    8210             :     /* -------------------------------------------------------------------- */
    8211             :     /*      Create a corresponding GDALDataset.                             */
    8212             :     /* -------------------------------------------------------------------- */
    8213        4260 :     auto poDS = std::make_unique<GTiffDataset>();
    8214             :     const bool bSuppressASAP =
    8215        2130 :         CPLTestBool(CSLFetchNameValueDef(papszOptions, "@SUPPRESS_ASAP", "NO"));
    8216        2130 :     if (bSuppressASAP)
    8217           4 :         poDS->MarkSuppressOnClose();
    8218        2130 :     poDS->SetDescription(pszFilename);
    8219        2130 :     poDS->eAccess = GA_Update;
    8220        2130 :     poDS->m_osFilename = pszFilename;
    8221        2130 :     poDS->m_fpL = l_fpL;
    8222        2130 :     poDS->m_bIMDRPCMetadataLoaded = true;
    8223        2130 :     poDS->m_nColorTableMultiplier = nColorTableMultiplier;
    8224        2130 :     poDS->m_bTileInterleave = bTileInterleaving;
    8225             : 
    8226        2130 :     if (bTileInterleaving)
    8227             :     {
    8228           7 :         poDS->m_oGTiffMDMD.SetMetadataItem("INTERLEAVE", "TILE",
    8229             :                                            "IMAGE_STRUCTURE");
    8230             :     }
    8231             : 
    8232        2130 :     const bool bAppend = CPLFetchBool(papszOptions, "APPEND_SUBDATASET", false);
    8233        4259 :     if (poDS->OpenOffset(l_hTIFF,
    8234        2129 :                          bAppend ? l_nDirOffset : TIFFCurrentDirOffset(l_hTIFF),
    8235             :                          GA_Update,
    8236             :                          false,  // bAllowRGBAInterface
    8237             :                          true    // bReadGeoTransform
    8238        2130 :                          ) != CE_None)
    8239             :     {
    8240           0 :         l_fpL->CancelCreation();
    8241           0 :         poDS.reset();
    8242           0 :         if (bStreaming)
    8243           0 :             VSIUnlink(l_osTmpFilename);
    8244           0 :         return nullptr;
    8245             :     }
    8246             : 
    8247             :     // Legacy... Patch back GDT_Int8 type to GDT_UInt8 if the user used
    8248             :     // PIXELTYPE=SIGNEDBYTE
    8249        2130 :     const char *pszPixelType = CSLFetchNameValue(papszOptions, "PIXELTYPE");
    8250        2130 :     if (pszPixelType == nullptr)
    8251        2125 :         pszPixelType = "";
    8252        2130 :     if (eType == GDT_UInt8 && EQUAL(pszPixelType, "SIGNEDBYTE"))
    8253             :     {
    8254          10 :         for (int i = 0; i < poDS->nBands; ++i)
    8255             :         {
    8256           5 :             auto poBand = static_cast<GTiffRasterBand *>(poDS->papoBands[i]);
    8257           5 :             poBand->eDataType = GDT_UInt8;
    8258           5 :             poBand->EnablePixelTypeSignedByteWarning(false);
    8259           5 :             poBand->SetMetadataItem("PIXELTYPE", "SIGNEDBYTE",
    8260             :                                     "IMAGE_STRUCTURE");
    8261           5 :             poBand->EnablePixelTypeSignedByteWarning(true);
    8262             :         }
    8263             :     }
    8264             : 
    8265        2130 :     poDS->oOvManager.Initialize(poDS.get(), pszFilename);
    8266             : 
    8267        2130 :     if (bStreaming)
    8268             :     {
    8269           4 :         VSIUnlink(l_osTmpFilename);
    8270           4 :         poDS->m_fpToWrite = fpStreaming;
    8271             :     }
    8272        2130 :     poDS->m_eProfile = eProfile;
    8273             : 
    8274        2130 :     int nCloneInfoFlags = GCIF_PAM_DEFAULT & ~GCIF_MASK;
    8275             : 
    8276             :     // If we explicitly asked not to tag the alpha band as such, do not
    8277             :     // reintroduce this alpha color interpretation in PAM.
    8278        2130 :     if (poSrcDS->GetRasterBand(l_nBands)->GetColorInterpretation() ==
    8279        2258 :             GCI_AlphaBand &&
    8280         128 :         GTiffGetAlphaValue(
    8281             :             CPLGetConfigOption("GTIFF_ALPHA",
    8282             :                                CSLFetchNameValue(papszOptions, "ALPHA")),
    8283             :             DEFAULT_ALPHA_TYPE) == EXTRASAMPLE_UNSPECIFIED)
    8284             :     {
    8285           1 :         nCloneInfoFlags &= ~GCIF_COLORINTERP;
    8286             :     }
    8287             :     // Ignore source band color interpretation if requesting PHOTOMETRIC=RGB
    8288        3355 :     else if (l_nBands >= 3 &&
    8289        1226 :              EQUAL(CSLFetchNameValueDef(papszOptions, "PHOTOMETRIC", ""),
    8290             :                    "RGB"))
    8291             :     {
    8292          28 :         for (int i = 1; i <= 3; i++)
    8293             :         {
    8294          21 :             poDS->GetRasterBand(i)->SetColorInterpretation(
    8295          21 :                 static_cast<GDALColorInterp>(GCI_RedBand + (i - 1)));
    8296             :         }
    8297           7 :         nCloneInfoFlags &= ~GCIF_COLORINTERP;
    8298           9 :         if (!(l_nBands == 4 &&
    8299           2 :               CSLFetchNameValue(papszOptions, "ALPHA") != nullptr))
    8300             :         {
    8301          15 :             for (int i = 4; i <= l_nBands; i++)
    8302             :             {
    8303          18 :                 poDS->GetRasterBand(i)->SetColorInterpretation(
    8304           9 :                     poSrcDS->GetRasterBand(i)->GetColorInterpretation());
    8305             :             }
    8306             :         }
    8307             :     }
    8308             : 
    8309             :     CPLString osOldGTIFF_REPORT_COMPD_CSVal(
    8310        4260 :         CPLGetConfigOption("GTIFF_REPORT_COMPD_CS", ""));
    8311        2130 :     CPLSetThreadLocalConfigOption("GTIFF_REPORT_COMPD_CS", "YES");
    8312        2130 :     poDS->CloneInfo(poSrcDS, nCloneInfoFlags);
    8313        2130 :     CPLSetThreadLocalConfigOption("GTIFF_REPORT_COMPD_CS",
    8314        2130 :                                   osOldGTIFF_REPORT_COMPD_CSVal.empty()
    8315             :                                       ? nullptr
    8316           0 :                                       : osOldGTIFF_REPORT_COMPD_CSVal.c_str());
    8317             : 
    8318        2147 :     if ((!bGeoTIFF || bExportSRSToPAM) &&
    8319          17 :         (poDS->GetPamFlags() & GPF_DISABLED) == 0)
    8320             :     {
    8321             :         // Copy georeferencing info to PAM if the profile is not GeoTIFF
    8322          16 :         poDS->GDALPamDataset::SetSpatialRef(poDS->GetSpatialRef());
    8323          16 :         GDALGeoTransform gt;
    8324          16 :         if (poDS->GetGeoTransform(gt) == CE_None)
    8325             :         {
    8326           5 :             poDS->GDALPamDataset::SetGeoTransform(gt);
    8327             :         }
    8328          16 :         poDS->GDALPamDataset::SetGCPs(poDS->GetGCPCount(), poDS->GetGCPs(),
    8329             :                                       poDS->GetGCPSpatialRef());
    8330             :     }
    8331             : 
    8332        2130 :     poDS->m_papszCreationOptions = CSLDuplicate(papszOptions);
    8333        2130 :     poDS->m_bDontReloadFirstBlock = l_bDontReloadFirstBlock;
    8334             : 
    8335             :     /* -------------------------------------------------------------------- */
    8336             :     /*      CloneInfo() does not merge metadata, it just replaces it        */
    8337             :     /*      totally.  So we have to merge it.                               */
    8338             :     /* -------------------------------------------------------------------- */
    8339             : 
    8340        2130 :     CSLConstList papszSRC_MD = poSrcDS->GetMetadata();
    8341        2130 :     char **papszDST_MD = CSLDuplicate(poDS->GetMetadata());
    8342             : 
    8343        2130 :     papszDST_MD = CSLMerge(papszDST_MD, papszSRC_MD);
    8344             : 
    8345        2130 :     poDS->SetMetadata(papszDST_MD);
    8346        2130 :     CSLDestroy(papszDST_MD);
    8347             : 
    8348             :     // Depending on the PHOTOMETRIC tag, the TIFF file may not have the same
    8349             :     // band count as the source. Will fail later in GDALDatasetCopyWholeRaster
    8350             :     // anyway.
    8351        7183 :     for (int nBand = 1;
    8352        7183 :          nBand <= std::min(poDS->GetRasterCount(), poSrcDS->GetRasterCount());
    8353             :          ++nBand)
    8354             :     {
    8355        5053 :         GDALRasterBand *poSrcBand = poSrcDS->GetRasterBand(nBand);
    8356        5053 :         GDALRasterBand *poDstBand = poDS->GetRasterBand(nBand);
    8357        5053 :         papszSRC_MD = poSrcBand->GetMetadata();
    8358        5053 :         papszDST_MD = CSLDuplicate(poDstBand->GetMetadata());
    8359             : 
    8360        5053 :         papszDST_MD = CSLMerge(papszDST_MD, papszSRC_MD);
    8361             : 
    8362        5053 :         poDstBand->SetMetadata(papszDST_MD);
    8363        5053 :         CSLDestroy(papszDST_MD);
    8364             : 
    8365        5053 :         char **papszCatNames = poSrcBand->GetCategoryNames();
    8366        5053 :         if (nullptr != papszCatNames)
    8367           0 :             poDstBand->SetCategoryNames(papszCatNames);
    8368             :     }
    8369             : 
    8370        2130 :     l_hTIFF = static_cast<TIFF *>(poDS->GetInternalHandle("TIFF_HANDLE"));
    8371             : 
    8372             :     /* -------------------------------------------------------------------- */
    8373             :     /*      Handle forcing xml:ESRI data to be written to PAM.              */
    8374             :     /* -------------------------------------------------------------------- */
    8375        2130 :     if (CPLTestBool(CPLGetConfigOption("ESRI_XML_PAM", "NO")))
    8376             :     {
    8377           1 :         CSLConstList papszESRIMD = poSrcDS->GetMetadata("xml:ESRI");
    8378           1 :         if (papszESRIMD)
    8379             :         {
    8380           1 :             poDS->SetMetadata(papszESRIMD, "xml:ESRI");
    8381             :         }
    8382             :     }
    8383             : 
    8384             :     /* -------------------------------------------------------------------- */
    8385             :     /*      Second chance: now that we have a PAM dataset, it is possible   */
    8386             :     /*      to write metadata that we could not write as a TIFF tag.        */
    8387             :     /* -------------------------------------------------------------------- */
    8388        2130 :     if (!bHasWrittenMDInGeotiffTAG && !bStreaming)
    8389             :     {
    8390           6 :         GTiffDataset::WriteMetadata(
    8391           6 :             poDS.get(), l_hTIFF, true, eProfile, pszFilename, papszOptions,
    8392             :             true /* don't write RPC and IMD file again */);
    8393             :     }
    8394             : 
    8395        2130 :     if (!bStreaming)
    8396        2126 :         GTiffDataset::WriteRPC(poDS.get(), l_hTIFF, true, eProfile, pszFilename,
    8397             :                                papszOptions,
    8398             :                                true /* write only in PAM AND if needed */);
    8399             : 
    8400        2130 :     poDS->m_bWriteCOGLayout = bCopySrcOverviews;
    8401             : 
    8402             :     // To avoid unnecessary directory rewriting.
    8403        2130 :     poDS->m_bMetadataChanged = false;
    8404        2130 :     poDS->m_bGeoTIFFInfoChanged = false;
    8405        2130 :     poDS->m_bNoDataChanged = false;
    8406        2130 :     poDS->m_bForceUnsetGTOrGCPs = false;
    8407        2130 :     poDS->m_bForceUnsetProjection = false;
    8408        2130 :     poDS->m_bStreamingOut = bStreaming;
    8409             : 
    8410             :     // Don't try to load external metadata files (#6597).
    8411        2130 :     poDS->m_bIMDRPCMetadataLoaded = true;
    8412             : 
    8413             :     // We must re-set the compression level at this point, since it has been
    8414             :     // lost a few lines above when closing the newly create TIFF file The
    8415             :     // TIFFTAG_ZIPQUALITY & TIFFTAG_JPEGQUALITY are not store in the TIFF file.
    8416             :     // They are just TIFF session parameters.
    8417             : 
    8418        2130 :     poDS->m_nZLevel = GTiffGetZLevel(papszOptions);
    8419        2130 :     poDS->m_nLZMAPreset = GTiffGetLZMAPreset(papszOptions);
    8420        2130 :     poDS->m_nZSTDLevel = GTiffGetZSTDPreset(papszOptions);
    8421        2130 :     poDS->m_nWebPLevel = GTiffGetWebPLevel(papszOptions);
    8422        2130 :     poDS->m_bWebPLossless = GTiffGetWebPLossless(papszOptions);
    8423        2133 :     if (poDS->m_nWebPLevel != 100 && poDS->m_bWebPLossless &&
    8424           3 :         CSLFetchNameValue(papszOptions, "WEBP_LEVEL"))
    8425             :     {
    8426           0 :         CPLError(CE_Warning, CPLE_AppDefined,
    8427             :                  "WEBP_LEVEL is specified, but WEBP_LOSSLESS=YES. "
    8428             :                  "WEBP_LEVEL will be ignored.");
    8429             :     }
    8430        2130 :     poDS->m_nJpegQuality = GTiffGetJpegQuality(papszOptions);
    8431        2130 :     poDS->m_nJpegTablesMode = GTiffGetJpegTablesMode(papszOptions);
    8432        2130 :     poDS->GetDiscardLsbOption(papszOptions);
    8433        2130 :     poDS->m_dfMaxZError = GTiffGetLERCMaxZError(papszOptions);
    8434        2130 :     poDS->m_dfMaxZErrorOverview = GTiffGetLERCMaxZErrorOverview(papszOptions);
    8435             : #if HAVE_JXL
    8436        2130 :     poDS->m_bJXLLossless = GTiffGetJXLLossless(papszOptions);
    8437        2130 :     poDS->m_nJXLEffort = GTiffGetJXLEffort(papszOptions);
    8438        2130 :     poDS->m_fJXLDistance = GTiffGetJXLDistance(papszOptions);
    8439        2130 :     poDS->m_fJXLAlphaDistance = GTiffGetJXLAlphaDistance(papszOptions);
    8440             : #endif
    8441        2130 :     poDS->InitCreationOrOpenOptions(true, papszOptions);
    8442             : 
    8443        2130 :     if (l_nCompression == COMPRESSION_ADOBE_DEFLATE ||
    8444        2102 :         l_nCompression == COMPRESSION_LERC)
    8445             :     {
    8446          99 :         GTiffSetDeflateSubCodec(l_hTIFF);
    8447             : 
    8448          99 :         if (poDS->m_nZLevel != -1)
    8449             :         {
    8450          12 :             TIFFSetField(l_hTIFF, TIFFTAG_ZIPQUALITY, poDS->m_nZLevel);
    8451             :         }
    8452             :     }
    8453        2130 :     if (l_nCompression == COMPRESSION_JPEG)
    8454             :     {
    8455          75 :         if (poDS->m_nJpegQuality != -1)
    8456             :         {
    8457           9 :             TIFFSetField(l_hTIFF, TIFFTAG_JPEGQUALITY, poDS->m_nJpegQuality);
    8458             :         }
    8459          75 :         TIFFSetField(l_hTIFF, TIFFTAG_JPEGTABLESMODE, poDS->m_nJpegTablesMode);
    8460             :     }
    8461        2130 :     if (l_nCompression == COMPRESSION_LZMA)
    8462             :     {
    8463           7 :         if (poDS->m_nLZMAPreset != -1)
    8464             :         {
    8465           6 :             TIFFSetField(l_hTIFF, TIFFTAG_LZMAPRESET, poDS->m_nLZMAPreset);
    8466             :         }
    8467             :     }
    8468        2130 :     if (l_nCompression == COMPRESSION_ZSTD ||
    8469        2119 :         l_nCompression == COMPRESSION_LERC)
    8470             :     {
    8471          82 :         if (poDS->m_nZSTDLevel != -1)
    8472             :         {
    8473           8 :             TIFFSetField(l_hTIFF, TIFFTAG_ZSTD_LEVEL, poDS->m_nZSTDLevel);
    8474             :         }
    8475             :     }
    8476        2130 :     if (l_nCompression == COMPRESSION_LERC)
    8477             :     {
    8478          71 :         TIFFSetField(l_hTIFF, TIFFTAG_LERC_MAXZERROR, poDS->m_dfMaxZError);
    8479             :     }
    8480             : #if HAVE_JXL
    8481        2130 :     if (l_nCompression == COMPRESSION_JXL ||
    8482        2130 :         l_nCompression == COMPRESSION_JXL_DNG_1_7)
    8483             :     {
    8484          91 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_LOSSYNESS,
    8485          91 :                      poDS->m_bJXLLossless ? JXL_LOSSLESS : JXL_LOSSY);
    8486          91 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_EFFORT, poDS->m_nJXLEffort);
    8487          91 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_DISTANCE,
    8488          91 :                      static_cast<double>(poDS->m_fJXLDistance));
    8489          91 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_ALPHA_DISTANCE,
    8490          91 :                      static_cast<double>(poDS->m_fJXLAlphaDistance));
    8491             :     }
    8492             : #endif
    8493        2130 :     if (l_nCompression == COMPRESSION_WEBP)
    8494             :     {
    8495          14 :         if (poDS->m_nWebPLevel != -1)
    8496             :         {
    8497          14 :             TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LEVEL, poDS->m_nWebPLevel);
    8498             :         }
    8499             : 
    8500          14 :         if (poDS->m_bWebPLossless)
    8501             :         {
    8502           5 :             TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LOSSLESS, poDS->m_bWebPLossless);
    8503             :         }
    8504             :     }
    8505             : 
    8506             :     /* -------------------------------------------------------------------- */
    8507             :     /*      Do we want to ensure all blocks get written out on close to     */
    8508             :     /*      avoid sparse files?                                             */
    8509             :     /* -------------------------------------------------------------------- */
    8510        2130 :     if (!CPLFetchBool(papszOptions, "SPARSE_OK", false))
    8511        2102 :         poDS->m_bFillEmptyTilesAtClosing = true;
    8512             : 
    8513        2130 :     poDS->m_bWriteEmptyTiles =
    8514        4039 :         (bCopySrcOverviews && poDS->m_bFillEmptyTilesAtClosing) || bStreaming ||
    8515        1909 :         (poDS->m_nCompression != COMPRESSION_NONE &&
    8516         310 :          poDS->m_bFillEmptyTilesAtClosing);
    8517             :     // Only required for people writing non-compressed striped files in the
    8518             :     // rightorder and wanting all tstrips to be written in the same order
    8519             :     // so that the end result can be memory mapped without knowledge of each
    8520             :     // strip offset
    8521        2130 :     if (CPLTestBool(CSLFetchNameValueDef(
    8522        4260 :             papszOptions, "WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")) ||
    8523        2130 :         CPLTestBool(CSLFetchNameValueDef(
    8524             :             papszOptions, "@WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")))
    8525             :     {
    8526           0 :         poDS->m_bWriteEmptyTiles = true;
    8527             :     }
    8528             : 
    8529             :     // Precreate (internal) mask, so that the IBuildOverviews() below
    8530             :     // has a chance to create also the overviews of the mask.
    8531        2130 :     CPLErr eErr = CE_None;
    8532             : 
    8533        2130 :     if (bCreateMask)
    8534             :     {
    8535          38 :         eErr = poDS->CreateMaskBand(nMaskFlags);
    8536          38 :         if (poDS->m_poMaskDS)
    8537             :         {
    8538          37 :             poDS->m_poMaskDS->m_bFillEmptyTilesAtClosing =
    8539          37 :                 poDS->m_bFillEmptyTilesAtClosing;
    8540          37 :             poDS->m_poMaskDS->m_bWriteEmptyTiles = poDS->m_bWriteEmptyTiles;
    8541             :         }
    8542             :     }
    8543             : 
    8544             :     /* -------------------------------------------------------------------- */
    8545             :     /*      Create and then copy existing overviews if requested            */
    8546             :     /*  We do it such that all the IFDs are at the beginning of the file,   */
    8547             :     /*  and that the imagery data for the smallest overview is written      */
    8548             :     /*  first, that way the file is more usable when embedded in a          */
    8549             :     /*  compressed stream.                                                  */
    8550             :     /* -------------------------------------------------------------------- */
    8551             : 
    8552             :     // For scaled progress due to overview copying.
    8553        2130 :     const int nBandsWidthMask = l_nBands + (bCreateMask ? 1 : 0);
    8554        2130 :     double dfTotalPixels =
    8555        2130 :         static_cast<double>(nXSize) * nYSize * nBandsWidthMask;
    8556        2130 :     double dfCurPixels = 0;
    8557             : 
    8558        2130 :     if (eErr == CE_None && bCopySrcOverviews)
    8559             :     {
    8560           0 :         std::unique_ptr<GDALDataset> poMaskOvrDS;
    8561             :         const char *pszMaskOvrDS =
    8562         222 :             CSLFetchNameValue(papszOptions, "@MASK_OVERVIEW_DATASET");
    8563         222 :         if (pszMaskOvrDS)
    8564             :         {
    8565           6 :             poMaskOvrDS.reset(GDALDataset::Open(pszMaskOvrDS));
    8566           6 :             if (!poMaskOvrDS)
    8567             :             {
    8568           0 :                 l_fpL->CancelCreation();
    8569           0 :                 return nullptr;
    8570             :             }
    8571           6 :             if (poMaskOvrDS->GetRasterCount() != 1)
    8572             :             {
    8573           0 :                 l_fpL->CancelCreation();
    8574           0 :                 return nullptr;
    8575             :             }
    8576             :         }
    8577         222 :         if (nSrcOverviews)
    8578             :         {
    8579          71 :             eErr = poDS->CreateOverviewsFromSrcOverviews(poSrcDS, poOvrDS.get(),
    8580             :                                                          nSrcOverviews);
    8581             : 
    8582         207 :             if (eErr == CE_None &&
    8583          71 :                 (poMaskOvrDS != nullptr ||
    8584          65 :                  (poSrcDS->GetRasterBand(1)->GetOverview(0) &&
    8585          35 :                   poSrcDS->GetRasterBand(1)->GetOverview(0)->GetMaskFlags() ==
    8586             :                       GMF_PER_DATASET)))
    8587             :             {
    8588          19 :                 int nOvrBlockXSize = 0;
    8589          19 :                 int nOvrBlockYSize = 0;
    8590          19 :                 GTIFFGetOverviewBlockSize(
    8591          19 :                     GDALRasterBand::ToHandle(poDS->GetRasterBand(1)),
    8592             :                     &nOvrBlockXSize, &nOvrBlockYSize, nullptr, nullptr);
    8593          19 :                 eErr = poDS->CreateInternalMaskOverviews(nOvrBlockXSize,
    8594             :                                                          nOvrBlockYSize);
    8595             :             }
    8596             :         }
    8597             : 
    8598         222 :         TIFFForceStrileArrayWriting(poDS->m_hTIFF);
    8599             : 
    8600         222 :         if (poDS->m_poMaskDS)
    8601             :         {
    8602          27 :             TIFFForceStrileArrayWriting(poDS->m_poMaskDS->m_hTIFF);
    8603             :         }
    8604             : 
    8605         353 :         for (auto &poIterOvrDS : poDS->m_apoOverviewDS)
    8606             :         {
    8607         131 :             TIFFForceStrileArrayWriting(poIterOvrDS->m_hTIFF);
    8608             : 
    8609         131 :             if (poIterOvrDS->m_poMaskDS)
    8610             :             {
    8611          32 :                 TIFFForceStrileArrayWriting(poIterOvrDS->m_poMaskDS->m_hTIFF);
    8612             :             }
    8613             :         }
    8614             : 
    8615         222 :         if (eErr == CE_None && nSrcOverviews)
    8616             :         {
    8617          71 :             if (poDS->m_apoOverviewDS.size() !=
    8618          71 :                 static_cast<size_t>(nSrcOverviews))
    8619             :             {
    8620           0 :                 ReportError(
    8621             :                     pszFilename, CE_Failure, CPLE_AppDefined,
    8622             :                     "Did only manage to instantiate %d overview levels, "
    8623             :                     "whereas source contains %d",
    8624           0 :                     static_cast<int>(poDS->m_apoOverviewDS.size()),
    8625             :                     nSrcOverviews);
    8626           0 :                 eErr = CE_Failure;
    8627             :             }
    8628             : 
    8629         202 :             for (int i = 0; eErr == CE_None && i < nSrcOverviews; ++i)
    8630             :             {
    8631             :                 GDALRasterBand *poOvrBand =
    8632             :                     poOvrDS
    8633         207 :                         ? (i == 0
    8634          76 :                                ? poOvrDS->GetRasterBand(1)
    8635          40 :                                : poOvrDS->GetRasterBand(1)->GetOverview(i - 1))
    8636         186 :                         : poSrcDS->GetRasterBand(1)->GetOverview(i);
    8637             :                 const double dfOvrPixels =
    8638         131 :                     static_cast<double>(poOvrBand->GetXSize()) *
    8639         131 :                     poOvrBand->GetYSize();
    8640         131 :                 dfTotalPixels += dfOvrPixels * l_nBands;
    8641         244 :                 if (poOvrBand->GetMaskFlags() == GMF_PER_DATASET ||
    8642         113 :                     poMaskOvrDS != nullptr)
    8643             :                 {
    8644          32 :                     dfTotalPixels += dfOvrPixels;
    8645             :                 }
    8646          99 :                 else if (i == 0 && poDS->GetRasterBand(1)->GetMaskFlags() ==
    8647             :                                        GMF_PER_DATASET)
    8648             :                 {
    8649           1 :                     ReportError(pszFilename, CE_Warning, CPLE_AppDefined,
    8650             :                                 "Source dataset has a mask band on full "
    8651             :                                 "resolution, overviews on the regular bands, "
    8652             :                                 "but lacks overviews on the mask band.");
    8653             :                 }
    8654             :             }
    8655             : 
    8656             :             // Now copy the imagery.
    8657             :             // Begin with the smallest overview.
    8658          71 :             for (int iOvrLevel = nSrcOverviews - 1;
    8659         201 :                  eErr == CE_None && iOvrLevel >= 0; --iOvrLevel)
    8660             :             {
    8661         130 :                 auto poDstDS = poDS->m_apoOverviewDS[iOvrLevel].get();
    8662             : 
    8663             :                 // Create a fake dataset with the source overview level so that
    8664             :                 // GDALDatasetCopyWholeRaster can cope with it.
    8665             :                 GDALDataset *poSrcOvrDS =
    8666             :                     poOvrDS
    8667         170 :                         ? (iOvrLevel == 0 ? poOvrDS.get()
    8668          40 :                                           : GDALCreateOverviewDataset(
    8669             :                                                 poOvrDS.get(), iOvrLevel - 1,
    8670             :                                                 /* bThisLevelOnly = */ true))
    8671          54 :                         : GDALCreateOverviewDataset(
    8672             :                               poSrcDS, iOvrLevel,
    8673         130 :                               /* bThisLevelOnly = */ true);
    8674             :                 GDALRasterBand *poSrcOvrBand =
    8675         206 :                     poOvrDS ? (iOvrLevel == 0
    8676          76 :                                    ? poOvrDS->GetRasterBand(1)
    8677          80 :                                    : poOvrDS->GetRasterBand(1)->GetOverview(
    8678          40 :                                          iOvrLevel - 1))
    8679         184 :                             : poSrcDS->GetRasterBand(1)->GetOverview(iOvrLevel);
    8680             :                 double dfNextCurPixels =
    8681             :                     dfCurPixels +
    8682         130 :                     static_cast<double>(poSrcOvrBand->GetXSize()) *
    8683         130 :                         poSrcOvrBand->GetYSize() * l_nBands;
    8684             : 
    8685         130 :                 poDstDS->m_bBlockOrderRowMajor = true;
    8686         130 :                 poDstDS->m_bLeaderSizeAsUInt4 = true;
    8687         130 :                 poDstDS->m_bTrailerRepeatedLast4BytesRepeated = true;
    8688         130 :                 poDstDS->m_bFillEmptyTilesAtClosing =
    8689         130 :                     poDS->m_bFillEmptyTilesAtClosing;
    8690         130 :                 poDstDS->m_bWriteEmptyTiles = poDS->m_bWriteEmptyTiles;
    8691         130 :                 poDstDS->m_bTileInterleave = poDS->m_bTileInterleave;
    8692         130 :                 GDALRasterBand *poSrcMaskBand = nullptr;
    8693         130 :                 if (poDstDS->m_poMaskDS)
    8694             :                 {
    8695          32 :                     poDstDS->m_poMaskDS->m_bBlockOrderRowMajor = true;
    8696          32 :                     poDstDS->m_poMaskDS->m_bLeaderSizeAsUInt4 = true;
    8697          32 :                     poDstDS->m_poMaskDS->m_bTrailerRepeatedLast4BytesRepeated =
    8698             :                         true;
    8699          64 :                     poDstDS->m_poMaskDS->m_bFillEmptyTilesAtClosing =
    8700          32 :                         poDS->m_bFillEmptyTilesAtClosing;
    8701          64 :                     poDstDS->m_poMaskDS->m_bWriteEmptyTiles =
    8702          32 :                         poDS->m_bWriteEmptyTiles;
    8703             : 
    8704          32 :                     poSrcMaskBand =
    8705             :                         poMaskOvrDS
    8706          46 :                             ? (iOvrLevel == 0
    8707          14 :                                    ? poMaskOvrDS->GetRasterBand(1)
    8708          16 :                                    : poMaskOvrDS->GetRasterBand(1)->GetOverview(
    8709           8 :                                          iOvrLevel - 1))
    8710          50 :                             : poSrcOvrBand->GetMaskBand();
    8711             :                 }
    8712             : 
    8713         130 :                 if (poDstDS->m_poMaskDS)
    8714             :                 {
    8715          32 :                     dfNextCurPixels +=
    8716          32 :                         static_cast<double>(poSrcOvrBand->GetXSize()) *
    8717          32 :                         poSrcOvrBand->GetYSize();
    8718             :                 }
    8719             :                 void *pScaledData =
    8720         130 :                     GDALCreateScaledProgress(dfCurPixels / dfTotalPixels,
    8721             :                                              dfNextCurPixels / dfTotalPixels,
    8722             :                                              pfnProgress, pProgressData);
    8723             : 
    8724         130 :                 eErr = CopyImageryAndMask(poDstDS, poSrcOvrDS, poSrcMaskBand,
    8725             :                                           GDALScaledProgress, pScaledData);
    8726             : 
    8727         130 :                 dfCurPixels = dfNextCurPixels;
    8728         130 :                 GDALDestroyScaledProgress(pScaledData);
    8729             : 
    8730         130 :                 if (poSrcOvrDS != poOvrDS.get())
    8731          94 :                     delete poSrcOvrDS;
    8732         130 :                 poSrcOvrDS = nullptr;
    8733             :             }
    8734             :         }
    8735             :     }
    8736             : 
    8737             :     /* -------------------------------------------------------------------- */
    8738             :     /*      Copy actual imagery.                                            */
    8739             :     /* -------------------------------------------------------------------- */
    8740        2130 :     double dfNextCurPixels =
    8741        2130 :         dfCurPixels + static_cast<double>(nXSize) * nYSize * l_nBands;
    8742        2130 :     void *pScaledData = GDALCreateScaledProgress(
    8743             :         dfCurPixels / dfTotalPixels, dfNextCurPixels / dfTotalPixels,
    8744             :         pfnProgress, pProgressData);
    8745             : 
    8746             : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
    8747        2130 :     bool bTryCopy = true;
    8748             : #endif
    8749             : 
    8750             : #ifdef HAVE_LIBJPEG
    8751        2130 :     if (bCopyFromJPEG)
    8752             :     {
    8753          12 :         eErr = GTIFF_CopyFromJPEG(poDS.get(), poSrcDS, pfnProgress,
    8754             :                                   pProgressData, bTryCopy);
    8755             : 
    8756             :         // In case of failure in the decompression step, try normal copy.
    8757          12 :         if (bTryCopy)
    8758           0 :             eErr = CE_None;
    8759             :     }
    8760             : #endif
    8761             : 
    8762             : #ifdef JPEG_DIRECT_COPY
    8763             :     if (bDirectCopyFromJPEG)
    8764             :     {
    8765             :         eErr = GTIFF_DirectCopyFromJPEG(poDS.get(), poSrcDS, pfnProgress,
    8766             :                                         pProgressData, bTryCopy);
    8767             : 
    8768             :         // In case of failure in the reading step, try normal copy.
    8769             :         if (bTryCopy)
    8770             :             eErr = CE_None;
    8771             :     }
    8772             : #endif
    8773             : 
    8774        2130 :     bool bWriteMask = true;
    8775        2130 :     if (
    8776             : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
    8777        4248 :         bTryCopy &&
    8778             : #endif
    8779        2118 :         (poDS->m_bTreatAsSplit || poDS->m_bTreatAsSplitBitmap))
    8780             :     {
    8781             :         // For split bands, we use TIFFWriteScanline() interface.
    8782           9 :         CPLAssert(poDS->m_nBitsPerSample == 8 || poDS->m_nBitsPerSample == 1);
    8783             : 
    8784           9 :         if (poDS->m_nPlanarConfig == PLANARCONFIG_CONTIG && poDS->nBands > 1)
    8785             :         {
    8786             :             GByte *pabyScanline = static_cast<GByte *>(
    8787           3 :                 VSI_MALLOC_VERBOSE(TIFFScanlineSize(l_hTIFF)));
    8788           3 :             if (pabyScanline == nullptr)
    8789           0 :                 eErr = CE_Failure;
    8790        9052 :             for (int j = 0; j < nYSize && eErr == CE_None; ++j)
    8791             :             {
    8792       18098 :                 eErr = poSrcDS->RasterIO(GF_Read, 0, j, nXSize, 1, pabyScanline,
    8793             :                                          nXSize, 1, GDT_UInt8, l_nBands,
    8794        9049 :                                          nullptr, poDS->nBands, 0, 1, nullptr);
    8795       18098 :                 if (eErr == CE_None &&
    8796        9049 :                     TIFFWriteScanline(l_hTIFF, pabyScanline, j, 0) == -1)
    8797             :                 {
    8798           0 :                     ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    8799             :                                 "TIFFWriteScanline() failed.");
    8800           0 :                     eErr = CE_Failure;
    8801             :                 }
    8802        9049 :                 if (!GDALScaledProgress((j + 1) * 1.0 / nYSize, nullptr,
    8803             :                                         pScaledData))
    8804           0 :                     eErr = CE_Failure;
    8805             :             }
    8806           3 :             CPLFree(pabyScanline);
    8807             :         }
    8808             :         else
    8809             :         {
    8810             :             GByte *pabyScanline =
    8811           6 :                 static_cast<GByte *>(VSI_MALLOC_VERBOSE(nXSize));
    8812           6 :             if (pabyScanline == nullptr)
    8813           0 :                 eErr = CE_Failure;
    8814             :             else
    8815           6 :                 eErr = CE_None;
    8816          14 :             for (int iBand = 1; iBand <= l_nBands && eErr == CE_None; ++iBand)
    8817             :             {
    8818       48211 :                 for (int j = 0; j < nYSize && eErr == CE_None; ++j)
    8819             :                 {
    8820       48203 :                     eErr = poSrcDS->GetRasterBand(iBand)->RasterIO(
    8821             :                         GF_Read, 0, j, nXSize, 1, pabyScanline, nXSize, 1,
    8822             :                         GDT_UInt8, 0, 0, nullptr);
    8823       48203 :                     if (poDS->m_bTreatAsSplitBitmap)
    8824             :                     {
    8825     7225210 :                         for (int i = 0; i < nXSize; ++i)
    8826             :                         {
    8827     7216010 :                             const GByte byVal = pabyScanline[i];
    8828     7216010 :                             if ((i & 0x7) == 0)
    8829      902001 :                                 pabyScanline[i >> 3] = 0;
    8830     7216010 :                             if (byVal)
    8831     7097220 :                                 pabyScanline[i >> 3] |= 0x80 >> (i & 0x7);
    8832             :                         }
    8833             :                     }
    8834       96406 :                     if (eErr == CE_None &&
    8835       48203 :                         TIFFWriteScanline(l_hTIFF, pabyScanline, j,
    8836       48203 :                                           static_cast<uint16_t>(iBand - 1)) ==
    8837             :                             -1)
    8838             :                     {
    8839           0 :                         ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    8840             :                                     "TIFFWriteScanline() failed.");
    8841           0 :                         eErr = CE_Failure;
    8842             :                     }
    8843       48203 :                     if (!GDALScaledProgress((j + 1 + (iBand - 1) * nYSize) *
    8844       48203 :                                                 1.0 / (l_nBands * nYSize),
    8845             :                                             nullptr, pScaledData))
    8846           0 :                         eErr = CE_Failure;
    8847             :                 }
    8848             :             }
    8849           6 :             CPLFree(pabyScanline);
    8850             :         }
    8851             : 
    8852             :         // Necessary to be able to read the file without re-opening.
    8853           9 :         TIFFSizeProc pfnSizeProc = TIFFGetSizeProc(l_hTIFF);
    8854             : 
    8855           9 :         TIFFFlushData(l_hTIFF);
    8856             : 
    8857           9 :         toff_t nNewDirOffset = pfnSizeProc(TIFFClientdata(l_hTIFF));
    8858           9 :         if ((nNewDirOffset % 2) == 1)
    8859           5 :             ++nNewDirOffset;
    8860             : 
    8861           9 :         TIFFFlush(l_hTIFF);
    8862             : 
    8863           9 :         if (poDS->m_nDirOffset != TIFFCurrentDirOffset(l_hTIFF))
    8864             :         {
    8865           0 :             poDS->m_nDirOffset = nNewDirOffset;
    8866           0 :             CPLDebug("GTiff", "directory moved during flush.");
    8867             :         }
    8868             :     }
    8869        2121 :     else if (
    8870             : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
    8871        2109 :         bTryCopy &&
    8872             : #endif
    8873             :         eErr == CE_None)
    8874             :     {
    8875        2108 :         const char *papszCopyWholeRasterOptions[3] = {nullptr, nullptr,
    8876             :                                                       nullptr};
    8877        2108 :         int iNextOption = 0;
    8878        2108 :         papszCopyWholeRasterOptions[iNextOption++] = "SKIP_HOLES=YES";
    8879        2108 :         if (l_nCompression != COMPRESSION_NONE)
    8880             :         {
    8881         497 :             papszCopyWholeRasterOptions[iNextOption++] = "COMPRESSED=YES";
    8882             :         }
    8883             : 
    8884             :         // For streaming with separate, we really want that bands are written
    8885             :         // after each other, even if the source is pixel interleaved.
    8886        1611 :         else if (bStreaming && poDS->m_nPlanarConfig == PLANARCONFIG_SEPARATE)
    8887             :         {
    8888           1 :             papszCopyWholeRasterOptions[iNextOption++] = "INTERLEAVE=BAND";
    8889             :         }
    8890             : 
    8891        2108 :         if (bCopySrcOverviews || bTileInterleaving)
    8892             :         {
    8893         222 :             poDS->m_bBlockOrderRowMajor = true;
    8894         222 :             poDS->m_bLeaderSizeAsUInt4 = bCopySrcOverviews;
    8895         222 :             poDS->m_bTrailerRepeatedLast4BytesRepeated = bCopySrcOverviews;
    8896         222 :             if (poDS->m_poMaskDS)
    8897             :             {
    8898          27 :                 poDS->m_poMaskDS->m_bBlockOrderRowMajor = true;
    8899          27 :                 poDS->m_poMaskDS->m_bLeaderSizeAsUInt4 = bCopySrcOverviews;
    8900          27 :                 poDS->m_poMaskDS->m_bTrailerRepeatedLast4BytesRepeated =
    8901             :                     bCopySrcOverviews;
    8902          27 :                 GDALDestroyScaledProgress(pScaledData);
    8903             :                 pScaledData =
    8904          27 :                     GDALCreateScaledProgress(dfCurPixels / dfTotalPixels, 1.0,
    8905             :                                              pfnProgress, pProgressData);
    8906             :             }
    8907             : 
    8908         222 :             eErr = CopyImageryAndMask(poDS.get(), poSrcDS,
    8909         222 :                                       poSrcDS->GetRasterBand(1)->GetMaskBand(),
    8910             :                                       GDALScaledProgress, pScaledData);
    8911         222 :             if (poDS->m_poMaskDS)
    8912             :             {
    8913          27 :                 bWriteMask = false;
    8914             :             }
    8915             :         }
    8916             :         else
    8917             :         {
    8918        1886 :             eErr = GDALDatasetCopyWholeRaster(GDALDataset::ToHandle(poSrcDS),
    8919        1886 :                                               GDALDataset::ToHandle(poDS.get()),
    8920             :                                               papszCopyWholeRasterOptions,
    8921             :                                               GDALScaledProgress, pScaledData);
    8922             :         }
    8923             :     }
    8924             : 
    8925        2130 :     GDALDestroyScaledProgress(pScaledData);
    8926             : 
    8927        2130 :     if (eErr == CE_None && !bStreaming && bWriteMask)
    8928             :     {
    8929        2081 :         pScaledData = GDALCreateScaledProgress(dfNextCurPixels / dfTotalPixels,
    8930             :                                                1.0, pfnProgress, pProgressData);
    8931        2081 :         if (poDS->m_poMaskDS)
    8932             :         {
    8933          10 :             const char *l_papszOptions[2] = {"COMPRESSED=YES", nullptr};
    8934          10 :             eErr = GDALRasterBandCopyWholeRaster(
    8935          10 :                 poSrcDS->GetRasterBand(1)->GetMaskBand(),
    8936          10 :                 poDS->GetRasterBand(1)->GetMaskBand(),
    8937             :                 const_cast<char **>(l_papszOptions), GDALScaledProgress,
    8938             :                 pScaledData);
    8939             :         }
    8940             :         else
    8941             :         {
    8942        2071 :             eErr = GDALDriver::DefaultCopyMasks(poSrcDS, poDS.get(), bStrict,
    8943             :                                                 nullptr, GDALScaledProgress,
    8944             :                                                 pScaledData);
    8945             :         }
    8946        2081 :         GDALDestroyScaledProgress(pScaledData);
    8947             :     }
    8948             : 
    8949        2130 :     poDS->m_bWriteCOGLayout = false;
    8950             : 
    8951        4242 :     if (eErr == CE_None &&
    8952        2112 :         CPLTestBool(CSLFetchNameValueDef(poDS->m_papszCreationOptions,
    8953             :                                          "@FLUSHCACHE", "NO")))
    8954             :     {
    8955         175 :         if (poDS->FlushCache(false) != CE_None)
    8956             :         {
    8957           0 :             eErr = CE_Failure;
    8958             :         }
    8959             :     }
    8960             : 
    8961        2130 :     if (eErr == CE_Failure)
    8962             :     {
    8963          18 :         if (CPLTestBool(CPLGetConfigOption("GTIFF_DELETE_ON_ERROR", "YES")))
    8964             :         {
    8965          17 :             l_fpL->CancelCreation();
    8966          17 :             poDS.reset();
    8967             : 
    8968          17 :             if (!bStreaming)
    8969             :             {
    8970             :                 // Should really delete more carefully.
    8971          17 :                 VSIUnlink(pszFilename);
    8972             :             }
    8973             :         }
    8974             :         else
    8975             :         {
    8976           1 :             poDS.reset();
    8977             :         }
    8978             :     }
    8979             : 
    8980        2130 :     return poDS.release();
    8981             : }
    8982             : 
    8983             : /************************************************************************/
    8984             : /*                           SetSpatialRef()                            */
    8985             : /************************************************************************/
    8986             : 
    8987        1524 : CPLErr GTiffDataset::SetSpatialRef(const OGRSpatialReference *poSRS)
    8988             : 
    8989             : {
    8990        1524 :     if (m_bStreamingOut && m_bCrystalized)
    8991             :     {
    8992           1 :         ReportError(CE_Failure, CPLE_NotSupported,
    8993             :                     "Cannot modify projection at that point in "
    8994             :                     "a streamed output file");
    8995           1 :         return CE_Failure;
    8996             :     }
    8997             : 
    8998        1523 :     LoadGeoreferencingAndPamIfNeeded();
    8999        1523 :     LookForProjection();
    9000             : 
    9001        1523 :     CPLErr eErr = CE_None;
    9002        1523 :     if (eAccess == GA_Update)
    9003             :     {
    9004        1525 :         if ((m_eProfile == GTiffProfile::BASELINE) &&
    9005           7 :             (GetPamFlags() & GPF_DISABLED) == 0)
    9006             :         {
    9007           7 :             eErr = GDALPamDataset::SetSpatialRef(poSRS);
    9008             :         }
    9009             :         else
    9010             :         {
    9011        1511 :             if (GDALPamDataset::GetSpatialRef() != nullptr)
    9012             :             {
    9013             :                 // Cancel any existing SRS from PAM file.
    9014           1 :                 GDALPamDataset::SetSpatialRef(nullptr);
    9015             :             }
    9016        1511 :             m_bGeoTIFFInfoChanged = true;
    9017             :         }
    9018             :     }
    9019             :     else
    9020             :     {
    9021           5 :         CPLDebug("GTIFF", "SetSpatialRef() goes to PAM instead of TIFF tags");
    9022           5 :         eErr = GDALPamDataset::SetSpatialRef(poSRS);
    9023             :     }
    9024             : 
    9025        1523 :     if (eErr == CE_None)
    9026             :     {
    9027        1523 :         if (poSRS == nullptr || poSRS->IsEmpty())
    9028             :         {
    9029          14 :             if (!m_oSRS.IsEmpty())
    9030             :             {
    9031           4 :                 m_bForceUnsetProjection = true;
    9032             :             }
    9033          14 :             m_oSRS.Clear();
    9034             :         }
    9035             :         else
    9036             :         {
    9037        1509 :             m_oSRS = *poSRS;
    9038        1509 :             m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    9039             :         }
    9040             :     }
    9041             : 
    9042        1523 :     return eErr;
    9043             : }
    9044             : 
    9045             : /************************************************************************/
    9046             : /*                          SetGeoTransform()                           */
    9047             : /************************************************************************/
    9048             : 
    9049        1837 : CPLErr GTiffDataset::SetGeoTransform(const GDALGeoTransform &gt)
    9050             : 
    9051             : {
    9052        1837 :     if (m_bStreamingOut && m_bCrystalized)
    9053             :     {
    9054           1 :         ReportError(CE_Failure, CPLE_NotSupported,
    9055             :                     "Cannot modify geotransform at that point in a "
    9056             :                     "streamed output file");
    9057           1 :         return CE_Failure;
    9058             :     }
    9059             : 
    9060        1836 :     LoadGeoreferencingAndPamIfNeeded();
    9061             : 
    9062        1836 :     CPLErr eErr = CE_None;
    9063        1836 :     if (eAccess == GA_Update)
    9064             :     {
    9065        1830 :         if (!m_aoGCPs.empty())
    9066             :         {
    9067           1 :             ReportError(CE_Warning, CPLE_AppDefined,
    9068             :                         "GCPs previously set are going to be cleared "
    9069             :                         "due to the setting of a geotransform.");
    9070           1 :             m_bForceUnsetGTOrGCPs = true;
    9071           1 :             m_aoGCPs.clear();
    9072             :         }
    9073        1829 :         else if (gt.xorig == 0.0 && gt.xscale == 0.0 && gt.xrot == 0.0 &&
    9074           2 :                  gt.yorig == 0.0 && gt.yrot == 0.0 && gt.yscale == 0.0)
    9075             :         {
    9076           2 :             if (m_bGeoTransformValid)
    9077             :             {
    9078           2 :                 m_bForceUnsetGTOrGCPs = true;
    9079           2 :                 m_bGeoTIFFInfoChanged = true;
    9080             :             }
    9081           2 :             m_bGeoTransformValid = false;
    9082           2 :             m_gt = gt;
    9083           2 :             return CE_None;
    9084             :         }
    9085             : 
    9086        3665 :         if ((m_eProfile == GTiffProfile::BASELINE) &&
    9087           9 :             !CPLFetchBool(m_papszCreationOptions, "TFW", false) &&
    9088        1842 :             !CPLFetchBool(m_papszCreationOptions, "WORLDFILE", false) &&
    9089           5 :             (GetPamFlags() & GPF_DISABLED) == 0)
    9090             :         {
    9091           5 :             eErr = GDALPamDataset::SetGeoTransform(gt);
    9092             :         }
    9093             :         else
    9094             :         {
    9095             :             // Cancel any existing geotransform from PAM file.
    9096        1823 :             GDALPamDataset::DeleteGeoTransform();
    9097        1823 :             m_bGeoTIFFInfoChanged = true;
    9098             :         }
    9099             :     }
    9100             :     else
    9101             :     {
    9102           6 :         CPLDebug("GTIFF", "SetGeoTransform() goes to PAM instead of TIFF tags");
    9103           6 :         eErr = GDALPamDataset::SetGeoTransform(gt);
    9104             :     }
    9105             : 
    9106        1834 :     if (eErr == CE_None)
    9107             :     {
    9108        1834 :         m_gt = gt;
    9109        1834 :         m_bGeoTransformValid = true;
    9110             :     }
    9111             : 
    9112        1834 :     return eErr;
    9113             : }
    9114             : 
    9115             : /************************************************************************/
    9116             : /*                              SetGCPs()                               */
    9117             : /************************************************************************/
    9118             : 
    9119          23 : CPLErr GTiffDataset::SetGCPs(int nGCPCountIn, const GDAL_GCP *pasGCPListIn,
    9120             :                              const OGRSpatialReference *poGCPSRS)
    9121             : {
    9122          23 :     CPLErr eErr = CE_None;
    9123          23 :     LoadGeoreferencingAndPamIfNeeded();
    9124          23 :     LookForProjection();
    9125             : 
    9126          23 :     if (eAccess == GA_Update)
    9127             :     {
    9128          21 :         if (!m_aoGCPs.empty() && nGCPCountIn == 0)
    9129             :         {
    9130           3 :             m_bForceUnsetGTOrGCPs = true;
    9131             :         }
    9132          18 :         else if (nGCPCountIn > 0 && m_bGeoTransformValid)
    9133             :         {
    9134           5 :             ReportError(CE_Warning, CPLE_AppDefined,
    9135             :                         "A geotransform previously set is going to be cleared "
    9136             :                         "due to the setting of GCPs.");
    9137           5 :             m_gt = GDALGeoTransform();
    9138           5 :             m_bGeoTransformValid = false;
    9139           5 :             m_bForceUnsetGTOrGCPs = true;
    9140             :         }
    9141          21 :         if ((m_eProfile == GTiffProfile::BASELINE) &&
    9142           0 :             (GetPamFlags() & GPF_DISABLED) == 0)
    9143             :         {
    9144           0 :             eErr = GDALPamDataset::SetGCPs(nGCPCountIn, pasGCPListIn, poGCPSRS);
    9145             :         }
    9146             :         else
    9147             :         {
    9148          21 :             if (nGCPCountIn > knMAX_GCP_COUNT)
    9149             :             {
    9150           2 :                 if (GDALPamDataset::GetGCPCount() == 0 && !m_aoGCPs.empty())
    9151             :                 {
    9152           1 :                     m_bForceUnsetGTOrGCPs = true;
    9153             :                 }
    9154           2 :                 ReportError(CE_Warning, CPLE_AppDefined,
    9155             :                             "Trying to write %d GCPs, whereas the maximum "
    9156             :                             "supported in GeoTIFF tag is %d. "
    9157             :                             "Falling back to writing them to PAM",
    9158             :                             nGCPCountIn, knMAX_GCP_COUNT);
    9159           2 :                 eErr = GDALPamDataset::SetGCPs(nGCPCountIn, pasGCPListIn,
    9160             :                                                poGCPSRS);
    9161             :             }
    9162          19 :             else if (GDALPamDataset::GetGCPCount() > 0)
    9163             :             {
    9164             :                 // Cancel any existing GCPs from PAM file.
    9165           1 :                 GDALPamDataset::SetGCPs(
    9166             :                     0, nullptr,
    9167             :                     static_cast<const OGRSpatialReference *>(nullptr));
    9168             :             }
    9169          21 :             m_bGeoTIFFInfoChanged = true;
    9170             :         }
    9171             :     }
    9172             :     else
    9173             :     {
    9174           2 :         CPLDebug("GTIFF", "SetGCPs() goes to PAM instead of TIFF tags");
    9175           2 :         eErr = GDALPamDataset::SetGCPs(nGCPCountIn, pasGCPListIn, poGCPSRS);
    9176             :     }
    9177             : 
    9178          23 :     if (eErr == CE_None)
    9179             :     {
    9180          23 :         if (poGCPSRS == nullptr || poGCPSRS->IsEmpty())
    9181             :         {
    9182          12 :             if (!m_oSRS.IsEmpty())
    9183             :             {
    9184           5 :                 m_bForceUnsetProjection = true;
    9185             :             }
    9186          12 :             m_oSRS.Clear();
    9187             :         }
    9188             :         else
    9189             :         {
    9190          11 :             m_oSRS = *poGCPSRS;
    9191          11 :             m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    9192             :         }
    9193             : 
    9194          23 :         m_aoGCPs = gdal::GCP::fromC(pasGCPListIn, nGCPCountIn);
    9195             :     }
    9196             : 
    9197          23 :     return eErr;
    9198             : }
    9199             : 
    9200             : /************************************************************************/
    9201             : /*                            SetMetadata()                             */
    9202             : /************************************************************************/
    9203        2720 : CPLErr GTiffDataset::SetMetadata(CSLConstList papszMD, const char *pszDomain)
    9204             : 
    9205             : {
    9206        2720 :     LoadGeoreferencingAndPamIfNeeded();
    9207             : 
    9208        2720 :     if (m_bStreamingOut && m_bCrystalized)
    9209             :     {
    9210           1 :         ReportError(
    9211             :             CE_Failure, CPLE_NotSupported,
    9212             :             "Cannot modify metadata at that point in a streamed output file");
    9213           1 :         return CE_Failure;
    9214             :     }
    9215             : 
    9216        2719 :     if (pszDomain && EQUAL(pszDomain, "json:ISIS3"))
    9217             :     {
    9218           5 :         m_oISIS3Metadata.Deinit();
    9219           5 :         m_oMapISIS3MetadataItems.clear();
    9220             :     }
    9221             : 
    9222        2719 :     CPLErr eErr = CE_None;
    9223        2719 :     if (eAccess == GA_Update)
    9224             :     {
    9225        2716 :         if (pszDomain != nullptr && EQUAL(pszDomain, MD_DOMAIN_RPC))
    9226             :         {
    9227             :             // So that a subsequent GetMetadata() wouldn't override our new
    9228             :             // values
    9229          22 :             LoadMetadata();
    9230          22 :             m_bForceUnsetRPC = (CSLCount(papszMD) == 0);
    9231             :         }
    9232             : 
    9233        2716 :         if ((papszMD != nullptr) && (pszDomain != nullptr) &&
    9234        1879 :             EQUAL(pszDomain, "COLOR_PROFILE"))
    9235             :         {
    9236           0 :             m_bColorProfileMetadataChanged = true;
    9237             :         }
    9238        2716 :         else if (pszDomain == nullptr || !EQUAL(pszDomain, "_temporary_"))
    9239             :         {
    9240        2716 :             m_bMetadataChanged = true;
    9241             :             // Cancel any existing metadata from PAM file.
    9242        2716 :             if (GDALPamDataset::GetMetadata(pszDomain) != nullptr)
    9243           1 :                 GDALPamDataset::SetMetadata(nullptr, pszDomain);
    9244             :         }
    9245             : 
    9246        5396 :         if ((pszDomain == nullptr || EQUAL(pszDomain, "")) &&
    9247        2680 :             CSLFetchNameValue(papszMD, GDALMD_AREA_OR_POINT) != nullptr)
    9248             :         {
    9249        2044 :             const char *pszPrevValue = GetMetadataItem(GDALMD_AREA_OR_POINT);
    9250             :             const char *pszNewValue =
    9251        2044 :                 CSLFetchNameValue(papszMD, GDALMD_AREA_OR_POINT);
    9252        2044 :             if (pszPrevValue == nullptr || pszNewValue == nullptr ||
    9253        1621 :                 !EQUAL(pszPrevValue, pszNewValue))
    9254             :             {
    9255         427 :                 LookForProjection();
    9256         427 :                 m_bGeoTIFFInfoChanged = true;
    9257             :             }
    9258             :         }
    9259             : 
    9260        2716 :         if (pszDomain != nullptr && EQUAL(pszDomain, "xml:XMP"))
    9261             :         {
    9262           2 :             if (papszMD != nullptr && *papszMD != nullptr)
    9263             :             {
    9264           1 :                 int nTagSize = static_cast<int>(strlen(*papszMD));
    9265           1 :                 TIFFSetField(m_hTIFF, TIFFTAG_XMLPACKET, nTagSize, *papszMD);
    9266             :             }
    9267             :             else
    9268             :             {
    9269           1 :                 TIFFUnsetField(m_hTIFF, TIFFTAG_XMLPACKET);
    9270             :             }
    9271             :         }
    9272             :     }
    9273             :     else
    9274             :     {
    9275           3 :         CPLDebug(
    9276             :             "GTIFF",
    9277             :             "GTiffDataset::SetMetadata() goes to PAM instead of TIFF tags");
    9278           3 :         eErr = GDALPamDataset::SetMetadata(papszMD, pszDomain);
    9279             :     }
    9280             : 
    9281        2719 :     if (eErr == CE_None)
    9282             :     {
    9283        2719 :         eErr = m_oGTiffMDMD.SetMetadata(papszMD, pszDomain);
    9284             :     }
    9285        2719 :     return eErr;
    9286             : }
    9287             : 
    9288             : /************************************************************************/
    9289             : /*                          SetMetadataItem()                           */
    9290             : /************************************************************************/
    9291             : 
    9292        5880 : CPLErr GTiffDataset::SetMetadataItem(const char *pszName, const char *pszValue,
    9293             :                                      const char *pszDomain)
    9294             : 
    9295             : {
    9296        5880 :     LoadGeoreferencingAndPamIfNeeded();
    9297             : 
    9298        5880 :     if (m_bStreamingOut && m_bCrystalized)
    9299             :     {
    9300           1 :         ReportError(
    9301             :             CE_Failure, CPLE_NotSupported,
    9302             :             "Cannot modify metadata at that point in a streamed output file");
    9303           1 :         return CE_Failure;
    9304             :     }
    9305             : 
    9306        5879 :     if (pszDomain && EQUAL(pszDomain, "json:ISIS3"))
    9307             :     {
    9308           1 :         ReportError(CE_Failure, CPLE_NotSupported,
    9309             :                     "Updating part of json:ISIS3 is not supported. "
    9310             :                     "Use SetMetadata() instead");
    9311           1 :         return CE_Failure;
    9312             :     }
    9313             : 
    9314        5878 :     CPLErr eErr = CE_None;
    9315        5878 :     if (eAccess == GA_Update)
    9316             :     {
    9317        5871 :         if ((pszDomain != nullptr) && EQUAL(pszDomain, "COLOR_PROFILE"))
    9318             :         {
    9319           8 :             m_bColorProfileMetadataChanged = true;
    9320             :         }
    9321        5863 :         else if (pszDomain == nullptr || !EQUAL(pszDomain, "_temporary_"))
    9322             :         {
    9323        5863 :             m_bMetadataChanged = true;
    9324             :             // Cancel any existing metadata from PAM file.
    9325        5863 :             if (GDALPamDataset::GetMetadataItem(pszName, pszDomain) != nullptr)
    9326           1 :                 GDALPamDataset::SetMetadataItem(pszName, nullptr, pszDomain);
    9327             :         }
    9328             : 
    9329        5871 :         if ((pszDomain == nullptr || EQUAL(pszDomain, "")) &&
    9330          84 :             pszName != nullptr && EQUAL(pszName, GDALMD_AREA_OR_POINT))
    9331             :         {
    9332           7 :             LookForProjection();
    9333           7 :             m_bGeoTIFFInfoChanged = true;
    9334             :         }
    9335             :     }
    9336             :     else
    9337             :     {
    9338           7 :         CPLDebug(
    9339             :             "GTIFF",
    9340             :             "GTiffDataset::SetMetadataItem() goes to PAM instead of TIFF tags");
    9341           7 :         eErr = GDALPamDataset::SetMetadataItem(pszName, pszValue, pszDomain);
    9342             :     }
    9343             : 
    9344        5878 :     if (eErr == CE_None)
    9345             :     {
    9346        5878 :         eErr = m_oGTiffMDMD.SetMetadataItem(pszName, pszValue, pszDomain);
    9347             :     }
    9348             : 
    9349        5878 :     return eErr;
    9350             : }
    9351             : 
    9352             : /************************************************************************/
    9353             : /*                           CreateMaskBand()                           */
    9354             : /************************************************************************/
    9355             : 
    9356         100 : CPLErr GTiffDataset::CreateMaskBand(int nFlagsIn)
    9357             : {
    9358         100 :     ScanDirectories();
    9359             : 
    9360         100 :     if (m_poMaskDS != nullptr)
    9361             :     {
    9362           1 :         ReportError(CE_Failure, CPLE_AppDefined,
    9363             :                     "This TIFF dataset has already an internal mask band");
    9364           1 :         return CE_Failure;
    9365             :     }
    9366          99 :     else if (MustCreateInternalMask())
    9367             :     {
    9368          86 :         if (nFlagsIn != GMF_PER_DATASET)
    9369             :         {
    9370           1 :             ReportError(CE_Failure, CPLE_AppDefined,
    9371             :                         "The only flag value supported for internal mask is "
    9372             :                         "GMF_PER_DATASET");
    9373           1 :             return CE_Failure;
    9374             :         }
    9375             : 
    9376          85 :         int l_nCompression = COMPRESSION_PACKBITS;
    9377          85 :         if (strstr(GDALGetMetadataItem(GDALGetDriverByName("GTiff"),
    9378             :                                        GDAL_DMD_CREATIONOPTIONLIST, nullptr),
    9379          85 :                    "<Value>DEFLATE</Value>") != nullptr)
    9380          85 :             l_nCompression = COMPRESSION_ADOBE_DEFLATE;
    9381             : 
    9382             :         /* --------------------------------------------------------------------
    9383             :          */
    9384             :         /*      If we don't have read access, then create the mask externally.
    9385             :          */
    9386             :         /* --------------------------------------------------------------------
    9387             :          */
    9388          85 :         if (GetAccess() != GA_Update)
    9389             :         {
    9390           1 :             ReportError(CE_Warning, CPLE_AppDefined,
    9391             :                         "File open for read-only accessing, "
    9392             :                         "creating mask externally.");
    9393             : 
    9394           1 :             return GDALPamDataset::CreateMaskBand(nFlagsIn);
    9395             :         }
    9396             : 
    9397          84 :         if (m_bLayoutIFDSBeforeData && !m_bKnownIncompatibleEdition &&
    9398           0 :             !m_bWriteKnownIncompatibleEdition)
    9399             :         {
    9400           0 :             ReportError(CE_Warning, CPLE_AppDefined,
    9401             :                         "Adding a mask invalidates the "
    9402             :                         "LAYOUT=IFDS_BEFORE_DATA property");
    9403           0 :             m_bKnownIncompatibleEdition = true;
    9404           0 :             m_bWriteKnownIncompatibleEdition = true;
    9405             :         }
    9406             : 
    9407          84 :         bool bIsOverview = false;
    9408          84 :         uint32_t nSubType = 0;
    9409          84 :         if (TIFFGetField(m_hTIFF, TIFFTAG_SUBFILETYPE, &nSubType))
    9410             :         {
    9411           8 :             bIsOverview = (nSubType & FILETYPE_REDUCEDIMAGE) != 0;
    9412             : 
    9413           8 :             if ((nSubType & FILETYPE_MASK) != 0)
    9414             :             {
    9415           0 :                 ReportError(CE_Failure, CPLE_AppDefined,
    9416             :                             "Cannot create a mask on a TIFF mask IFD !");
    9417           0 :                 return CE_Failure;
    9418             :             }
    9419             :         }
    9420             : 
    9421          84 :         const int bIsTiled = TIFFIsTiled(m_hTIFF);
    9422             : 
    9423          84 :         FlushDirectory();
    9424             : 
    9425          84 :         const toff_t nOffset = GTIFFWriteDirectory(
    9426             :             m_hTIFF,
    9427             :             bIsOverview ? FILETYPE_REDUCEDIMAGE | FILETYPE_MASK : FILETYPE_MASK,
    9428             :             nRasterXSize, nRasterYSize, 1, PLANARCONFIG_CONTIG, 1,
    9429             :             m_nBlockXSize, m_nBlockYSize, bIsTiled, l_nCompression,
    9430             :             PHOTOMETRIC_MASK, PREDICTOR_NONE, SAMPLEFORMAT_UINT, nullptr,
    9431             :             nullptr, nullptr, 0, nullptr, "", nullptr, nullptr, nullptr,
    9432          84 :             nullptr, m_bWriteCOGLayout);
    9433             : 
    9434          84 :         ReloadDirectory();
    9435             : 
    9436          84 :         if (nOffset == 0)
    9437           0 :             return CE_Failure;
    9438             : 
    9439          84 :         m_poMaskDS = std::make_shared<GTiffDataset>();
    9440          84 :         m_poMaskDS->eAccess = GA_Update;
    9441          84 :         m_poMaskDS->m_poBaseDS = this;
    9442          84 :         m_poMaskDS->m_poImageryDS = this;
    9443          84 :         m_poMaskDS->ShareLockWithParentDataset(this);
    9444          84 :         m_poMaskDS->m_osFilename = m_osFilename;
    9445          84 :         m_poMaskDS->m_bPromoteTo8Bits = CPLTestBool(
    9446             :             CPLGetConfigOption("GDAL_TIFF_INTERNAL_MASK_TO_8BIT", "YES"));
    9447          84 :         return m_poMaskDS->OpenOffset(VSI_TIFFOpenChild(m_hTIFF), nOffset,
    9448          84 :                                       GA_Update);
    9449             :     }
    9450             : 
    9451          13 :     return GDALPamDataset::CreateMaskBand(nFlagsIn);
    9452             : }
    9453             : 
    9454             : /************************************************************************/
    9455             : /*                       MustCreateInternalMask()                       */
    9456             : /************************************************************************/
    9457             : 
    9458         137 : bool GTiffDataset::MustCreateInternalMask()
    9459             : {
    9460         137 :     return CPLTestBool(CPLGetConfigOption("GDAL_TIFF_INTERNAL_MASK", "YES"));
    9461             : }
    9462             : 
    9463             : /************************************************************************/
    9464             : /*                           CreateMaskBand()                           */
    9465             : /************************************************************************/
    9466             : 
    9467          29 : CPLErr GTiffRasterBand::CreateMaskBand(int nFlagsIn)
    9468             : {
    9469          29 :     m_poGDS->ScanDirectories();
    9470             : 
    9471          29 :     if (m_poGDS->m_poMaskDS != nullptr)
    9472             :     {
    9473           5 :         ReportError(CE_Failure, CPLE_AppDefined,
    9474             :                     "This TIFF dataset has already an internal mask band");
    9475           5 :         return CE_Failure;
    9476             :     }
    9477             : 
    9478             :     const char *pszGDAL_TIFF_INTERNAL_MASK =
    9479          24 :         CPLGetConfigOption("GDAL_TIFF_INTERNAL_MASK", nullptr);
    9480          27 :     if ((pszGDAL_TIFF_INTERNAL_MASK &&
    9481          24 :          CPLTestBool(pszGDAL_TIFF_INTERNAL_MASK)) ||
    9482             :         nFlagsIn == GMF_PER_DATASET)
    9483             :     {
    9484          16 :         return m_poGDS->CreateMaskBand(nFlagsIn);
    9485             :     }
    9486             : 
    9487           8 :     return GDALPamRasterBand::CreateMaskBand(nFlagsIn);
    9488             : }
    9489             : 
    9490             : /************************************************************************/
    9491             : /*                            ClampCTEntry()                            */
    9492             : /************************************************************************/
    9493             : 
    9494      236415 : /* static */ unsigned short GTiffDataset::ClampCTEntry(int iColor, int iComp,
    9495             :                                                        int nCTEntryVal,
    9496             :                                                        int nMultFactor)
    9497             : {
    9498      236415 :     const int nVal = nCTEntryVal * nMultFactor;
    9499      236415 :     if (nVal < 0)
    9500             :     {
    9501           0 :         CPLError(CE_Warning, CPLE_AppDefined,
    9502             :                  "Color table entry [%d][%d] = %d, clamped to 0", iColor, iComp,
    9503             :                  nCTEntryVal);
    9504           0 :         return 0;
    9505             :     }
    9506      236415 :     if (nVal > 65535)
    9507             :     {
    9508           2 :         CPLError(CE_Warning, CPLE_AppDefined,
    9509             :                  "Color table entry [%d][%d] = %d, clamped to 65535", iColor,
    9510             :                  iComp, nCTEntryVal);
    9511           2 :         return 65535;
    9512             :     }
    9513      236413 :     return static_cast<unsigned short>(nVal);
    9514             : }

Generated by: LCOV version 1.14