LCOV - code coverage report
Current view: top level - frmts/gtiff - gtiffdataset_write.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 3893 4250 91.6 %
Date: 2026-05-25 22:21:53 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       17876 : static signed char GTiffGetWebPLevel(CSLConstList papszOptions)
      79             : {
      80       17876 :     int nWebPLevel = DEFAULT_WEBP_LEVEL;
      81       17876 :     const char *pszValue = CSLFetchNameValue(papszOptions, "WEBP_LEVEL");
      82       17876 :     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       17876 :     return static_cast<signed char>(nWebPLevel);
      93             : }
      94             : 
      95       17882 : static bool GTiffGetWebPLossless(CSLConstList papszOptions)
      96             : {
      97       17882 :     return CPLFetchBool(papszOptions, "WEBP_LOSSLESS", false);
      98             : }
      99             : 
     100       17948 : static double GTiffGetLERCMaxZError(CSLConstList papszOptions)
     101             : {
     102       17948 :     return CPLAtof(CSLFetchNameValueDef(papszOptions, "MAX_Z_ERROR", "0.0"));
     103             : }
     104             : 
     105        8011 : static double GTiffGetLERCMaxZErrorOverview(CSLConstList papszOptions)
     106             : {
     107        8011 :     return CPLAtof(CSLFetchNameValueDef(
     108             :         papszOptions, "MAX_Z_ERROR_OVERVIEW",
     109        8011 :         CSLFetchNameValueDef(papszOptions, "MAX_Z_ERROR", "0.0")));
     110             : }
     111             : 
     112             : #if HAVE_JXL
     113       17952 : static bool GTiffGetJXLLossless(CSLConstList papszOptions,
     114             :                                 bool *pbIsSpecified = nullptr)
     115             : {
     116       17952 :     const char *pszVal = CSLFetchNameValue(papszOptions, "JXL_LOSSLESS");
     117       17952 :     if (pbIsSpecified)
     118        9937 :         *pbIsSpecified = pszVal != nullptr;
     119       17952 :     return pszVal == nullptr || CPLTestBool(pszVal);
     120             : }
     121             : 
     122       17952 : static uint32_t GTiffGetJXLEffort(CSLConstList papszOptions)
     123             : {
     124       17952 :     return atoi(CSLFetchNameValueDef(papszOptions, "JXL_EFFORT", "5"));
     125             : }
     126             : 
     127       17870 : static float GTiffGetJXLDistance(CSLConstList papszOptions,
     128             :                                  bool *pbIsSpecified = nullptr)
     129             : {
     130       17870 :     const char *pszVal = CSLFetchNameValue(papszOptions, "JXL_DISTANCE");
     131       17870 :     if (pbIsSpecified)
     132        9937 :         *pbIsSpecified = pszVal != nullptr;
     133       17870 :     return pszVal == nullptr ? 1.0f : static_cast<float>(CPLAtof(pszVal));
     134             : }
     135             : 
     136       17952 : static float GTiffGetJXLAlphaDistance(CSLConstList papszOptions,
     137             :                                       bool *pbIsSpecified = nullptr)
     138             : {
     139       17952 :     const char *pszVal = CSLFetchNameValue(papszOptions, "JXL_ALPHA_DISTANCE");
     140       17952 :     if (pbIsSpecified)
     141        9937 :         *pbIsSpecified = pszVal != nullptr;
     142       17952 :     return pszVal == nullptr ? -1.0f : static_cast<float>(CPLAtof(pszVal));
     143             : }
     144             : 
     145             : #endif
     146             : 
     147             : /************************************************************************/
     148             : /*                           FillEmptyTiles()                           */
     149             : /************************************************************************/
     150             : 
     151        8201 : CPLErr GTiffDataset::FillEmptyTiles()
     152             : 
     153             : {
     154             :     /* -------------------------------------------------------------------- */
     155             :     /*      How many blocks are there in this file?                         */
     156             :     /* -------------------------------------------------------------------- */
     157       16402 :     const int nBlockCount = m_nPlanarConfig == PLANARCONFIG_SEPARATE
     158        8201 :                                 ? m_nBlocksPerBand * nBands
     159             :                                 : m_nBlocksPerBand;
     160             : 
     161             :     /* -------------------------------------------------------------------- */
     162             :     /*      Fetch block maps.                                               */
     163             :     /* -------------------------------------------------------------------- */
     164        8201 :     toff_t *panByteCounts = nullptr;
     165             : 
     166        8201 :     if (TIFFIsTiled(m_hTIFF))
     167        1149 :         TIFFGetField(m_hTIFF, TIFFTAG_TILEBYTECOUNTS, &panByteCounts);
     168             :     else
     169        7052 :         TIFFGetField(m_hTIFF, TIFFTAG_STRIPBYTECOUNTS, &panByteCounts);
     170             : 
     171        8201 :     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        8201 :         TIFFIsTiled(m_hTIFF) ? static_cast<GPtrDiff_t>(TIFFTileSize(m_hTIFF))
     184        7052 :                              : static_cast<GPtrDiff_t>(TIFFStripSize(m_hTIFF));
     185             : 
     186        8201 :     GByte *pabyData = static_cast<GByte *>(VSI_CALLOC_VERBOSE(nBlockBytes, 1));
     187        8201 :     if (pabyData == nullptr)
     188             :     {
     189           0 :         return CE_Failure;
     190             :     }
     191             : 
     192             :     // Force tiles completely filled with the nodata value to be written.
     193        8201 :     m_bWriteEmptyTiles = true;
     194             : 
     195             :     /* -------------------------------------------------------------------- */
     196             :     /*      If set, fill data buffer with no data value.                    */
     197             :     /* -------------------------------------------------------------------- */
     198        8201 :     if ((m_bNoDataSet && m_dfNoDataValue != 0.0) ||
     199        7933 :         (m_bNoDataSetAsInt64 && m_nNoDataValueInt64 != 0) ||
     200        7928 :         (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        7923 :     else if (m_nCompression == COMPRESSION_NONE && (m_nBitsPerSample % 8) == 0)
     316             :     {
     317        6350 :         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        6350 :         int nCountBlocksToZero = 0;
     321     2330100 :         for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
     322             :         {
     323     2323750 :             if (panByteCounts[iBlock] == 0)
     324             :             {
     325     2228340 :                 if (nCountBlocksToZero == 0)
     326             :                 {
     327        1137 :                     const bool bWriteEmptyTilesBak = m_bWriteEmptyTiles;
     328        1137 :                     m_bWriteEmptyTiles = true;
     329        1137 :                     const bool bOK = WriteEncodedTileOrStrip(iBlock, pabyData,
     330        1137 :                                                              FALSE) == CE_None;
     331        1137 :                     m_bWriteEmptyTiles = bWriteEmptyTilesBak;
     332        1137 :                     if (!bOK)
     333             :                     {
     334           2 :                         eErr = CE_Failure;
     335           2 :                         break;
     336             :                     }
     337             :                 }
     338     2228340 :                 nCountBlocksToZero++;
     339             :             }
     340             :         }
     341        6350 :         CPLFree(pabyData);
     342             : 
     343        6350 :         --nCountBlocksToZero;
     344             : 
     345             :         // And then seek to end of file for other ones.
     346        6350 :         if (nCountBlocksToZero > 0)
     347             :         {
     348         348 :             toff_t *panByteOffsets = nullptr;
     349             : 
     350         348 :             if (TIFFIsTiled(m_hTIFF))
     351         101 :                 TIFFGetField(m_hTIFF, TIFFTAG_TILEOFFSETS, &panByteOffsets);
     352             :             else
     353         247 :                 TIFFGetField(m_hTIFF, TIFFTAG_STRIPOFFSETS, &panByteOffsets);
     354             : 
     355         348 :             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         348 :             VSILFILE *fpTIF = VSI_TIFFGetVSILFile(TIFFClientdata(m_hTIFF));
     364         348 :             VSIFSeekL(fpTIF, 0, SEEK_END);
     365         348 :             const vsi_l_offset nOffset = VSIFTellL(fpTIF);
     366             : 
     367         348 :             vsi_l_offset iBlockToZero = 0;
     368     2236440 :             for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
     369             :             {
     370     2236090 :                 if (panByteCounts[iBlock] == 0)
     371             :                 {
     372     2227200 :                     panByteOffsets[iBlock] = static_cast<toff_t>(
     373     2227200 :                         nOffset + iBlockToZero * nBlockBytes);
     374     2227200 :                     panByteCounts[iBlock] = nBlockBytes;
     375     2227200 :                     iBlockToZero++;
     376             :                 }
     377             :             }
     378         348 :             CPLAssert(iBlockToZero ==
     379             :                       static_cast<vsi_l_offset>(nCountBlocksToZero));
     380             : 
     381         348 :             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        6350 :         return eErr;
     390             :     }
     391             : 
     392             :     /* -------------------------------------------------------------------- */
     393             :     /*      Check all blocks, writing out data for uninitialized blocks.    */
     394             :     /* -------------------------------------------------------------------- */
     395             : 
     396        1840 :     GByte *pabyRaw = nullptr;
     397        1840 :     vsi_l_offset nRawSize = 0;
     398        1840 :     CPLErr eErr = CE_None;
     399       56544 :     for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
     400             :     {
     401       54711 :         if (panByteCounts[iBlock] == 0)
     402             :         {
     403       17570 :             if (pabyRaw == nullptr)
     404             :             {
     405       10167 :                 if (WriteEncodedTileOrStrip(iBlock, pabyData, FALSE) != CE_None)
     406             :                 {
     407           7 :                     eErr = CE_Failure;
     408           7 :                     break;
     409             :                 }
     410             : 
     411       10160 :                 vsi_l_offset nOffset = 0;
     412       10160 :                 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       10160 :                 if (m_nCompression != COMPRESSION_NONE)
     418             :                 {
     419             :                     pabyRaw = static_cast<GByte *>(
     420         486 :                         VSI_MALLOC_VERBOSE(static_cast<size_t>(nRawSize)));
     421         486 :                     if (pabyRaw)
     422             :                     {
     423             :                         VSILFILE *fp =
     424         486 :                             VSI_TIFFGetVSILFile(TIFFClientdata(m_hTIFF));
     425         486 :                         const vsi_l_offset nCurOffset = VSIFTellL(fp);
     426         486 :                         VSIFSeekL(fp, nOffset, SEEK_SET);
     427         486 :                         VSIFReadL(pabyRaw, 1, static_cast<size_t>(nRawSize),
     428             :                                   fp);
     429         486 :                         VSIFSeekL(fp, nCurOffset, SEEK_SET);
     430             :                     }
     431             :                 }
     432             :             }
     433             :             else
     434             :             {
     435        7403 :                 WriteRawStripOrTile(iBlock, pabyRaw,
     436             :                                     static_cast<GPtrDiff_t>(nRawSize));
     437             :             }
     438             :         }
     439             :     }
     440             : 
     441        1840 :     CPLFree(pabyData);
     442        1840 :     VSIFree(pabyRaw);
     443        1840 :     return eErr;
     444             : }
     445             : 
     446             : /************************************************************************/
     447             : /*                           HasOnlyNoData()                            */
     448             : /************************************************************************/
     449             : 
     450       42814 : bool GTiffDataset::HasOnlyNoData(const void *pBuffer, int nWidth, int nHeight,
     451             :                                  int nLineStride, int nComponents)
     452             : {
     453       42814 :     if (m_nSampleFormat == SAMPLEFORMAT_COMPLEXINT ||
     454       42814 :         m_nSampleFormat == SAMPLEFORMAT_COMPLEXIEEEFP)
     455           0 :         return false;
     456       42814 :     if (m_bNoDataSetAsInt64 || m_bNoDataSetAsUInt64)
     457           6 :         return false;  // FIXME: over pessimistic
     458       85616 :     return GDALBufferHasOnlyNoData(
     459       42808 :         pBuffer, m_bNoDataSet ? m_dfNoDataValue : 0.0, nWidth, nHeight,
     460       42808 :         nLineStride, nComponents, m_nBitsPerSample,
     461       42808 :         m_nSampleFormat == SAMPLEFORMAT_UINT  ? GSF_UNSIGNED_INT
     462        4826 :         : m_nSampleFormat == SAMPLEFORMAT_INT ? GSF_SIGNED_INT
     463       42808 :                                               : GSF_FLOATING_POINT);
     464             : }
     465             : 
     466             : /************************************************************************/
     467             : /*                     IsFirstPixelEqualToNoData()                      */
     468             : /************************************************************************/
     469             : 
     470      169255 : inline bool GTiffDataset::IsFirstPixelEqualToNoData(const void *pBuffer)
     471             : {
     472      169255 :     const GDALDataType eDT = GetRasterBand(1)->GetRasterDataType();
     473      169255 :     const double dfEffectiveNoData = (m_bNoDataSet) ? m_dfNoDataValue : 0.0;
     474      169255 :     if (m_bNoDataSetAsInt64 || m_bNoDataSetAsUInt64)
     475          10 :         return true;  // FIXME: over pessimistic
     476      169245 :     if (m_nBitsPerSample == 8 ||
     477       58919 :         (m_nBitsPerSample < 8 && dfEffectiveNoData == 0))
     478             :     {
     479      113772 :         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      227235 :         return GDALIsValueInRange<GByte>(dfEffectiveNoData) &&
     486      113602 :                *(static_cast<const GByte *>(pBuffer)) ==
     487      227235 :                    static_cast<GByte>(dfEffectiveNoData);
     488             :     }
     489       55473 :     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       53130 :     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       48892 :     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       48703 :     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       48450 :     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       48333 :     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       48215 :     if (m_nBitsPerSample == 32 && eDT == GDT_Float32)
     526             :     {
     527       41191 :         if (std::isnan(m_dfNoDataValue))
     528           3 :             return std::isnan(*(static_cast<const float *>(pBuffer)));
     529       82376 :         return GDALIsValueInRange<float>(dfEffectiveNoData) &&
     530       41188 :                *(static_cast<const float *>(pBuffer)) ==
     531       82376 :                    static_cast<float>(dfEffectiveNoData);
     532             :     }
     533        7024 :     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        2673 :     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       50505 : bool GTiffDataset::WriteEncodedTile(uint32_t tile, GByte *pabyData,
     609             :                                     int bPreserveDataBuffer)
     610             : {
     611       50505 :     const int iColumn = (tile % m_nBlocksPerBand) % m_nBlocksPerRow;
     612       50505 :     const int iRow = (tile % m_nBlocksPerBand) / m_nBlocksPerRow;
     613             : 
     614      101010 :     const int nActualBlockWidth = (iColumn == m_nBlocksPerRow - 1)
     615       50505 :                                       ? nRasterXSize - iColumn * m_nBlockXSize
     616             :                                       : m_nBlockXSize;
     617      101010 :     const int nActualBlockHeight = (iRow == m_nBlocksPerColumn - 1)
     618       50505 :                                        ? nRasterYSize - iRow * m_nBlockYSize
     619             :                                        : m_nBlockYSize;
     620             : 
     621             :     /* -------------------------------------------------------------------- */
     622             :     /*      Don't write empty blocks in some cases.                         */
     623             :     /* -------------------------------------------------------------------- */
     624       50505 :     if (!m_bWriteEmptyTiles && IsFirstPixelEqualToNoData(pabyData))
     625             :     {
     626        1975 :         if (!IsBlockAvailable(tile, nullptr, nullptr, nullptr))
     627             :         {
     628        1975 :             const int nComponents =
     629        1975 :                 m_nPlanarConfig == PLANARCONFIG_CONTIG ? nBands : 1;
     630             : 
     631        1975 :             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       95599 :     const bool bPartialTile = (nActualBlockWidth < m_nBlockXSize) ||
     641       46294 :                               (nActualBlockHeight < m_nBlockYSize);
     642             : 
     643             :     const bool bIsLercFloatingPoint =
     644       49371 :         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       49305 :     const bool bNeedTempBuffer =
     652       54011 :         bPartialTile &&
     653        4706 :         (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       49305 :     const GPtrDiff_t cc = static_cast<GPtrDiff_t>(TIFFTileSize(m_hTIFF));
     660             : 
     661       63614 :     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       49305 :     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       49305 :     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       49305 :     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       49305 :     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       49288 :     if (SubmitCompressionJob(tile, pabyData, cc, m_nBlockYSize))
     773       19943 :         return true;
     774             : 
     775       29345 :     return TIFFWriteEncodedTile(m_hTIFF, tile, pabyData, cc) == cc;
     776             : }
     777             : 
     778             : /************************************************************************/
     779             : /*                         WriteEncodedStrip()                          */
     780             : /************************************************************************/
     781             : 
     782      178271 : bool GTiffDataset::WriteEncodedStrip(uint32_t strip, GByte *pabyData,
     783             :                                      int bPreserveDataBuffer)
     784             : {
     785      178271 :     GPtrDiff_t cc = static_cast<GPtrDiff_t>(TIFFStripSize(m_hTIFF));
     786      178271 :     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      178271 :     const int nStripWithinBand = strip % m_nBlocksPerBand;
     794      178271 :     int nStripHeight = m_nRowsPerStrip;
     795             : 
     796      178271 :     if (nStripWithinBand * nStripHeight > GetRasterYSize() - nStripHeight)
     797             :     {
     798         387 :         nStripHeight = GetRasterYSize() - nStripWithinBand * m_nRowsPerStrip;
     799         387 :         cc = (cc / m_nRowsPerStrip) * nStripHeight;
     800         774 :         CPLDebug("GTiff",
     801             :                  "Adjusted bytes to write from " CPL_FRMT_GUIB
     802             :                  " to " CPL_FRMT_GUIB ".",
     803         387 :                  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      178271 :     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       28307 :                 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      239486 :     if (bPreserveDataBuffer &&
     831       89522 :         (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      149964 :     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      149964 :     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      148556 :     if (SubmitCompressionJob(strip, pabyData, cc, nStripHeight))
     892        6727 :         return true;
     893             : 
     894      141829 :     return TIFFWriteEncodedStrip(m_hTIFF, strip, pabyData, cc) == cc;
     895             : }
     896             : 
     897             : /************************************************************************/
     898             : /*                       InitCompressionThreads()                       */
     899             : /************************************************************************/
     900             : 
     901       32111 : void GTiffDataset::InitCompressionThreads(bool bUpdateMode,
     902             :                                           CSLConstList papszOptions)
     903             : {
     904             :     // Raster == tile, then no need for threads
     905       32111 :     if (m_nBlockXSize == nRasterXSize && m_nBlockYSize == nRasterYSize)
     906       23483 :         return;
     907             : 
     908        8628 :     const char *pszNumThreads = "";
     909        8628 :     bool bOK = false;
     910        8628 :     const int nThreads = GDALGetNumThreads(
     911             :         papszOptions, "NUM_THREADS", GDAL_DEFAULT_MAX_THREAD_COUNT,
     912             :         /* bDefaultToAllCPUs=*/false, &pszNumThreads, &bOK);
     913        8628 :     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        8546 :     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       26682 : void GTiffDataset::ThreadCompressionFunc(void *pData)
     967             : {
     968       26682 :     GTiffCompressionJob *psJob = static_cast<GTiffCompressionJob *>(pData);
     969       26682 :     GTiffDataset *poDS = psJob->poDS;
     970             : 
     971       26682 :     VSILFILE *fpTmp = VSIFOpenL(psJob->pszTmpFilename, "wb+");
     972       26682 :     TIFF *hTIFFTmp = VSI_TIFFOpen(
     973       53364 :         psJob->pszTmpFilename, psJob->bTIFFIsBigEndian ? "wb+" : "wl+", fpTmp);
     974       26682 :     CPLAssert(hTIFFTmp != nullptr);
     975       26682 :     TIFFSetField(hTIFFTmp, TIFFTAG_IMAGEWIDTH, poDS->m_nBlockXSize);
     976       26682 :     TIFFSetField(hTIFFTmp, TIFFTAG_IMAGELENGTH, psJob->nHeight);
     977       26682 :     TIFFSetField(hTIFFTmp, TIFFTAG_BITSPERSAMPLE, poDS->m_nBitsPerSample);
     978       26682 :     TIFFSetField(hTIFFTmp, TIFFTAG_COMPRESSION, poDS->m_nCompression);
     979       26682 :     TIFFSetField(hTIFFTmp, TIFFTAG_PHOTOMETRIC, poDS->m_nPhotometric);
     980       26682 :     TIFFSetField(hTIFFTmp, TIFFTAG_SAMPLEFORMAT, poDS->m_nSampleFormat);
     981       26682 :     TIFFSetField(hTIFFTmp, TIFFTAG_SAMPLESPERPIXEL, poDS->m_nSamplesPerPixel);
     982       26682 :     TIFFSetField(hTIFFTmp, TIFFTAG_ROWSPERSTRIP, poDS->m_nBlockYSize);
     983       26682 :     TIFFSetField(hTIFFTmp, TIFFTAG_PLANARCONFIG, poDS->m_nPlanarConfig);
     984       26682 :     if (psJob->nPredictor != PREDICTOR_NONE)
     985         263 :         TIFFSetField(hTIFFTmp, TIFFTAG_PREDICTOR, psJob->nPredictor);
     986       26682 :     if (poDS->m_nCompression == COMPRESSION_LERC)
     987             :     {
     988          24 :         TIFFSetField(hTIFFTmp, TIFFTAG_LERC_PARAMETERS, 2,
     989          24 :                      poDS->m_anLercAddCompressionAndVersion);
     990             :     }
     991       26682 :     if (psJob->nExtraSampleCount)
     992             :     {
     993         352 :         TIFFSetField(hTIFFTmp, TIFFTAG_EXTRASAMPLES, psJob->nExtraSampleCount,
     994             :                      psJob->pExtraSamples);
     995             :     }
     996             : 
     997       26682 :     poDS->RestoreVolatileParameters(hTIFFTmp);
     998             : 
     999       53364 :     bool bOK = TIFFWriteEncodedStrip(hTIFFTmp, 0, psJob->pabyBuffer,
    1000       26682 :                                      psJob->nBufferSize) == psJob->nBufferSize;
    1001             : 
    1002       26682 :     toff_t nOffset = 0;
    1003       26682 :     if (bOK)
    1004             :     {
    1005       26682 :         toff_t *panOffsets = nullptr;
    1006       26682 :         toff_t *panByteCounts = nullptr;
    1007       26682 :         TIFFGetField(hTIFFTmp, TIFFTAG_STRIPOFFSETS, &panOffsets);
    1008       26682 :         TIFFGetField(hTIFFTmp, TIFFTAG_STRIPBYTECOUNTS, &panByteCounts);
    1009             : 
    1010       26682 :         nOffset = panOffsets[0];
    1011       26682 :         psJob->nCompressedBufferSize =
    1012       26682 :             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       26682 :     XTIFFClose(hTIFFTmp);
    1021       26682 :     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       26682 :     if (bOK)
    1033             :     {
    1034       26682 :         vsi_l_offset nFileSize = 0;
    1035             :         GByte *pabyCompressedBuffer =
    1036       26682 :             VSIGetMemFileBuffer(psJob->pszTmpFilename, &nFileSize, FALSE);
    1037       26682 :         CPLAssert(static_cast<vsi_l_offset>(
    1038             :                       nOffset + psJob->nCompressedBufferSize) <= nFileSize);
    1039       26682 :         psJob->pabyCompressedBuffer = pabyCompressedBuffer + nOffset;
    1040             :     }
    1041             :     else
    1042             :     {
    1043           0 :         psJob->pabyCompressedBuffer = nullptr;
    1044           0 :         psJob->nCompressedBufferSize = 0;
    1045             :     }
    1046             : 
    1047       26682 :     auto poMainDS = poDS->m_poBaseDS ? poDS->m_poBaseDS : poDS;
    1048       26682 :     if (poMainDS->m_poCompressQueue)
    1049             :     {
    1050        1576 :         std::lock_guard oLock(poMainDS->m_oCompressThreadPoolMutex);
    1051        1576 :         psJob->bReady = true;
    1052             :     }
    1053       26682 : }
    1054             : 
    1055             : /************************************************************************/
    1056             : /*                        WriteRawStripOrTile()                         */
    1057             : /************************************************************************/
    1058             : 
    1059       34085 : 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       34085 :     toff_t *panOffsets = nullptr;
    1068       34085 :     toff_t *panByteCounts = nullptr;
    1069       34085 :     bool bWriteAtEnd = true;
    1070       34085 :     bool bWriteLeader = m_bLeaderSizeAsUInt4;
    1071       34085 :     bool bWriteTrailer = m_bTrailerRepeatedLast4BytesRepeated;
    1072       34085 :     if (TIFFGetField(m_hTIFF,
    1073       34085 :                      TIFFIsTiled(m_hTIFF) ? TIFFTAG_TILEOFFSETS
    1074             :                                           : TIFFTAG_STRIPOFFSETS,
    1075       34085 :                      &panOffsets) &&
    1076       34085 :         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       34085 :     if (bWriteLeader &&
    1154       25111 :         static_cast<GUIntBig>(nCompressedBufferSize) <= 0xFFFFFFFFU)
    1155             :     {
    1156             :         // cppcheck-suppress knownConditionTrueFalse
    1157       25111 :         if (bWriteAtEnd)
    1158             :         {
    1159       24855 :             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       25111 :         if (bWriteLeader)
    1179             :         {
    1180       25111 :             uint32_t nSize = static_cast<uint32_t>(nCompressedBufferSize);
    1181       25111 :             CPL_LSBPTR32(&nSize);
    1182       25111 :             if (!VSI_TIFFWrite(m_hTIFF, &nSize, sizeof(nSize)))
    1183           0 :                 m_bWriteError = true;
    1184             :         }
    1185             :     }
    1186             :     tmsize_t written;
    1187       34085 :     if (TIFFIsTiled(m_hTIFF))
    1188       26410 :         written = TIFFWriteRawTile(m_hTIFF, nStripOrTile, pabyCompressedBuffer,
    1189             :                                    nCompressedBufferSize);
    1190             :     else
    1191        7675 :         written = TIFFWriteRawStrip(m_hTIFF, nStripOrTile, pabyCompressedBuffer,
    1192             :                                     nCompressedBufferSize);
    1193       34085 :     if (written != nCompressedBufferSize)
    1194          12 :         m_bWriteError = true;
    1195       34085 :     if (bWriteTrailer &&
    1196       25111 :         static_cast<GUIntBig>(nCompressedBufferSize) <= 0xFFFFFFFFU)
    1197             :     {
    1198       25111 :         GByte abyLastBytes[4] = {};
    1199       25111 :         if (nCompressedBufferSize >= 4)
    1200       25111 :             memcpy(abyLastBytes,
    1201       25111 :                    pabyCompressedBuffer + nCompressedBufferSize - 4, 4);
    1202             :         else
    1203           0 :             memcpy(abyLastBytes, pabyCompressedBuffer, nCompressedBufferSize);
    1204       25111 :         if (!VSI_TIFFWrite(m_hTIFF, abyLastBytes, 4))
    1205           0 :             m_bWriteError = true;
    1206             :     }
    1207       34085 : }
    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        2282 :             std::lock_guard oLock(mutex);
    1231        2282 :             bReady = asJobs[i].bReady;
    1232             :         }
    1233        2282 :         if (!bReady)
    1234             :         {
    1235         706 :             if (!bHasWarned)
    1236             :             {
    1237         449 :                 CPLDebug("GTIFF",
    1238             :                          "Waiting for worker job to finish handling block %d",
    1239         449 :                          asJobs[i].nStripOrTile);
    1240         449 :                 bHasWarned = true;
    1241             :             }
    1242         706 :             poQueue->GetPool()->WaitEvent();
    1243             :         }
    1244             :         else
    1245             :         {
    1246        1576 :             break;
    1247             :         }
    1248         706 :     }
    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     2320440 : void GTiffDataset::WaitCompletionForBlock(int nBlockId)
    1272             : {
    1273     2320440 :     auto poQueue = m_poBaseDS ? m_poBaseDS->m_poCompressQueue.get()
    1274     2301200 :                               : m_poCompressQueue.get();
    1275             :     // cppcheck-suppress constVariableReference
    1276     2320440 :     auto &oQueue = m_poBaseDS ? m_poBaseDS->m_asQueueJobIdx : m_asQueueJobIdx;
    1277             :     // cppcheck-suppress constVariableReference
    1278     2301200 :     auto &asJobs =
    1279     2320440 :         m_poBaseDS ? m_poBaseDS->m_asCompressionJobs : m_asCompressionJobs;
    1280             : 
    1281     2320440 :     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     2320440 : }
    1301             : 
    1302             : /************************************************************************/
    1303             : /*                        SubmitCompressionJob()                        */
    1304             : /************************************************************************/
    1305             : 
    1306      197844 : 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      197844 :     auto poQueue = m_poBaseDS ? m_poBaseDS->m_poCompressQueue.get()
    1313      183791 :                               : m_poCompressQueue.get();
    1314             : 
    1315      197844 :     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      123563 :         [this, pabyData, cc, nHeight, nStripOrTile](GTiffCompressionJob &sJob)
    1334             :     {
    1335       26682 :         sJob.poDS = this;
    1336       26682 :         sJob.bTIFFIsBigEndian = CPL_TO_BOOL(TIFFIsBigEndian(m_hTIFF));
    1337             :         GByte *pabyBuffer =
    1338       26682 :             static_cast<GByte *>(VSI_REALLOC_VERBOSE(sJob.pabyBuffer, cc));
    1339       26682 :         if (!pabyBuffer)
    1340           0 :             return false;
    1341       26682 :         sJob.pabyBuffer = pabyBuffer;
    1342       26682 :         memcpy(sJob.pabyBuffer, pabyData, cc);
    1343       26682 :         sJob.nBufferSize = cc;
    1344       26682 :         sJob.nHeight = nHeight;
    1345       26682 :         sJob.nStripOrTile = nStripOrTile;
    1346       26682 :         sJob.nPredictor = PREDICTOR_NONE;
    1347       26682 :         if (GTIFFSupportsPredictor(m_nCompression))
    1348             :         {
    1349       16835 :             TIFFGetField(m_hTIFF, TIFFTAG_PREDICTOR, &sJob.nPredictor);
    1350             :         }
    1351             : 
    1352       26682 :         sJob.pExtraSamples = nullptr;
    1353       26682 :         sJob.nExtraSampleCount = 0;
    1354       26682 :         TIFFGetField(m_hTIFF, TIFFTAG_EXTRASAMPLES, &sJob.nExtraSampleCount,
    1355             :                      &sJob.pExtraSamples);
    1356       26682 :         return true;
    1357      197844 :     };
    1358             : 
    1359      197844 :     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      196268 :         if (m_bBlockOrderRowMajor || m_bLeaderSizeAsUInt4 ||
    1371      171162 :             m_bTrailerRepeatedLast4BytesRepeated)
    1372             :         {
    1373             :             GTiffCompressionJob sJob;
    1374       25106 :             memset(&sJob, 0, sizeof(sJob));
    1375       25106 :             if (SetupJob(sJob))
    1376             :             {
    1377       25106 :                 sJob.pszTmpFilename =
    1378       25106 :                     CPLStrdup(VSIMemGenerateHiddenFilename("temp.tif"));
    1379             : 
    1380       25106 :                 ThreadCompressionFunc(&sJob);
    1381             : 
    1382       25106 :                 if (sJob.nCompressedBufferSize)
    1383             :                 {
    1384       25106 :                     sJob.poDS->WriteRawStripOrTile(sJob.nStripOrTile,
    1385             :                                                    sJob.pabyCompressedBuffer,
    1386             :                                                    sJob.nCompressedBufferSize);
    1387             :                 }
    1388             : 
    1389       25106 :                 CPLFree(sJob.pabyBuffer);
    1390       25106 :                 VSIUnlink(sJob.pszTmpFilename);
    1391       25106 :                 CPLFree(sJob.pszTmpFilename);
    1392       25106 :                 return sJob.nCompressedBufferSize > 0 && !m_bWriteError;
    1393             :             }
    1394             :         }
    1395             : 
    1396      171162 :         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      228776 : CPLErr GTiffDataset::WriteEncodedTileOrStrip(uint32_t tile_or_strip, void *data,
    1958             :                                              int bPreserveDataBuffer)
    1959             : {
    1960      228776 :     CPLErr eErr = CE_None;
    1961             : 
    1962      228776 :     if (TIFFIsTiled(m_hTIFF))
    1963             :     {
    1964       50505 :         if (!(WriteEncodedTile(tile_or_strip, static_cast<GByte *>(data),
    1965             :                                bPreserveDataBuffer)))
    1966             :         {
    1967          14 :             eErr = CE_Failure;
    1968             :         }
    1969             :     }
    1970             :     else
    1971             :     {
    1972      178271 :         if (!(WriteEncodedStrip(tile_or_strip, static_cast<GByte *>(data),
    1973             :                                 bPreserveDataBuffer)))
    1974             :         {
    1975           8 :             eErr = CE_Failure;
    1976             :         }
    1977             :     }
    1978             : 
    1979      228776 :     return eErr;
    1980             : }
    1981             : 
    1982             : /************************************************************************/
    1983             : /*                           FlushBlockBuf()                            */
    1984             : /************************************************************************/
    1985             : 
    1986        9616 : CPLErr GTiffDataset::FlushBlockBuf()
    1987             : 
    1988             : {
    1989        9616 :     if (m_nLoadedBlock < 0 || !m_bLoadedBlockDirty)
    1990           0 :         return CE_None;
    1991             : 
    1992        9616 :     m_bLoadedBlockDirty = false;
    1993             : 
    1994             :     const CPLErr eErr =
    1995        9616 :         WriteEncodedTileOrStrip(m_nLoadedBlock, m_pabyBlockBuf, true);
    1996        9616 :     if (eErr != CE_None)
    1997             :     {
    1998           0 :         ReportError(CE_Failure, CPLE_AppDefined,
    1999             :                     "WriteEncodedTile/Strip() failed.");
    2000           0 :         m_bWriteError = true;
    2001             :     }
    2002             : 
    2003        9616 :     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     2655610 : void GTiffDataset::Crystalize()
    2075             : 
    2076             : {
    2077     2655610 :     if (m_bCrystalized)
    2078     2649820 :         return;
    2079             : 
    2080             :     // TODO: libtiff writes extended tags in the order they are specified
    2081             :     // and not in increasing order.
    2082        5783 :     WriteMetadata(this, m_hTIFF, true, m_eProfile, m_osFilename.c_str(),
    2083        5783 :                   m_papszCreationOptions);
    2084        5783 :     WriteGeoTIFFInfo();
    2085        5783 :     if (m_bNoDataSet)
    2086         340 :         WriteNoDataValue(m_hTIFF, m_dfNoDataValue);
    2087        5443 :     else if (m_bNoDataSetAsInt64)
    2088           4 :         WriteNoDataValue(m_hTIFF, m_nNoDataValueInt64);
    2089        5439 :     else if (m_bNoDataSetAsUInt64)
    2090           4 :         WriteNoDataValue(m_hTIFF, m_nNoDataValueUInt64);
    2091             : 
    2092        5783 :     m_bMetadataChanged = false;
    2093        5783 :     m_bGeoTIFFInfoChanged = false;
    2094        5783 :     m_bNoDataChanged = false;
    2095        5783 :     m_bNeedsRewrite = false;
    2096             : 
    2097        5783 :     m_bCrystalized = true;
    2098             : 
    2099        5783 :     TIFFWriteCheck(m_hTIFF, TIFFIsTiled(m_hTIFF), "GTiffDataset::Crystalize");
    2100             : 
    2101        5783 :     TIFFWriteDirectory(m_hTIFF);
    2102        5783 :     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        5780 :         const tdir_t nNumberOfDirs = TIFFNumberOfDirectories(m_hTIFF);
    2139        5780 :         if (nNumberOfDirs > 0)
    2140             :         {
    2141        5780 :             TIFFSetDirectory(m_hTIFF, static_cast<tdir_t>(nNumberOfDirs - 1));
    2142             :         }
    2143             :     }
    2144             : 
    2145        5783 :     RestoreVolatileParameters(m_hTIFF);
    2146             : 
    2147        5783 :     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        4570 : CPLErr GTiffDataset::FlushCache(bool bAtClosing)
    2158             : 
    2159             : {
    2160        4570 :     return FlushCacheInternal(bAtClosing, true);
    2161             : }
    2162             : 
    2163       46717 : CPLErr GTiffDataset::FlushCacheInternal(bool bAtClosing, bool bFlushDirectory)
    2164             : {
    2165       46717 :     if (m_bIsFinalized)
    2166           2 :         return CE_None;
    2167             : 
    2168       46715 :     CPLErr eErr = GDALPamDataset::FlushCache(bAtClosing);
    2169             : 
    2170       46715 :     if (m_bLoadedBlockDirty && m_nLoadedBlock != -1)
    2171             :     {
    2172         275 :         if (FlushBlockBuf() != CE_None)
    2173           0 :             eErr = CE_Failure;
    2174             :     }
    2175             : 
    2176       46715 :     CPLFree(m_pabyBlockBuf);
    2177       46715 :     m_pabyBlockBuf = nullptr;
    2178       46715 :     m_nLoadedBlock = -1;
    2179       46715 :     m_bLoadedBlockDirty = false;
    2180             : 
    2181             :     // Finish compression
    2182       46715 :     auto poQueue = m_poBaseDS ? m_poBaseDS->m_poCompressQueue.get()
    2183       44365 :                               : m_poCompressQueue.get();
    2184       46715 :     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       46715 :     if (bFlushDirectory && GetAccess() == GA_Update)
    2200             :     {
    2201       13977 :         if (FlushDirectory() != CE_None)
    2202          12 :             eErr = CE_Failure;
    2203             :     }
    2204       46715 :     return eErr;
    2205             : }
    2206             : 
    2207             : /************************************************************************/
    2208             : /*                           FlushDirectory()                           */
    2209             : /************************************************************************/
    2210             : 
    2211       21972 : CPLErr GTiffDataset::FlushDirectory()
    2212             : 
    2213             : {
    2214       21972 :     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       21972 :     if (eAccess == GA_Update)
    2244             :     {
    2245       15670 :         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       15670 :         if (m_bGeoTIFFInfoChanged)
    2275             :         {
    2276         145 :             WriteGeoTIFFInfo();
    2277         145 :             m_bGeoTIFFInfoChanged = false;
    2278             :         }
    2279             : 
    2280       15670 :         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       15670 :         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       37642 :     if (GetAccess() == GA_Update &&
    2345       15670 :         TIFFCurrentDirOffset(m_hTIFF) == m_nDirOffset)
    2346             :     {
    2347       15670 :         const TIFFSizeProc pfnSizeProc = TIFFGetSizeProc(m_hTIFF);
    2348             : 
    2349       15670 :         toff_t nNewDirOffset = pfnSizeProc(TIFFClientdata(m_hTIFF));
    2350       15670 :         if ((nNewDirOffset % 2) == 1)
    2351        3448 :             ++nNewDirOffset;
    2352             : 
    2353       15670 :         if (TIFFFlush(m_hTIFF) == 0)
    2354          12 :             eErr = CE_Failure;
    2355             : 
    2356       15670 :         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       21972 :     SetDirectory();
    2366       21972 :     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         515 : CPLErr GTiffDataset::RegisterNewOverviewDataset(toff_t nOverviewOffset,
    2449             :                                                 int l_nJpegQuality,
    2450             :                                                 CSLConstList papszOptions)
    2451             : {
    2452             :     const auto GetOptionValue =
    2453        5665 :         [papszOptions](const char *pszOptionKey, const char *pszConfigOptionKey,
    2454       11329 :                        const char **ppszKeyUsed = nullptr)
    2455             :     {
    2456        5665 :         const char *pszVal = CSLFetchNameValue(papszOptions, pszOptionKey);
    2457        5665 :         if (pszVal)
    2458             :         {
    2459           1 :             if (ppszKeyUsed)
    2460           1 :                 *ppszKeyUsed = pszOptionKey;
    2461           1 :             return pszVal;
    2462             :         }
    2463        5664 :         pszVal = CSLFetchNameValue(papszOptions, pszConfigOptionKey);
    2464        5664 :         if (pszVal)
    2465             :         {
    2466           0 :             if (ppszKeyUsed)
    2467           0 :                 *ppszKeyUsed = pszConfigOptionKey;
    2468           0 :             return pszVal;
    2469             :         }
    2470        5664 :         if (pszConfigOptionKey)
    2471             :         {
    2472        5664 :             pszVal = CPLGetConfigOption(pszConfigOptionKey, nullptr);
    2473        5664 :             if (pszVal && ppszKeyUsed)
    2474          13 :                 *ppszKeyUsed = pszConfigOptionKey;
    2475             :         }
    2476        5664 :         return pszVal;
    2477         515 :     };
    2478             : 
    2479         515 :     int nZLevel = m_nZLevel;
    2480         515 :     if (const char *opt = GetOptionValue("ZLEVEL", "ZLEVEL_OVERVIEW"))
    2481             :     {
    2482           4 :         nZLevel = atoi(opt);
    2483             :     }
    2484             : 
    2485         515 :     int nZSTDLevel = m_nZSTDLevel;
    2486         515 :     if (const char *opt = GetOptionValue("ZSTD_LEVEL", "ZSTD_LEVEL_OVERVIEW"))
    2487             :     {
    2488           4 :         nZSTDLevel = atoi(opt);
    2489             :     }
    2490             : 
    2491         515 :     bool bWebpLossless = m_bWebPLossless;
    2492             :     const char *pszWebPLosslessOverview =
    2493         515 :         GetOptionValue("WEBP_LOSSLESS", "WEBP_LOSSLESS_OVERVIEW");
    2494         515 :     if (pszWebPLosslessOverview)
    2495             :     {
    2496           2 :         bWebpLossless = CPLTestBool(pszWebPLosslessOverview);
    2497             :     }
    2498             : 
    2499         515 :     int nWebpLevel = m_nWebPLevel;
    2500         515 :     const char *pszKeyWebpLevel = "";
    2501         515 :     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         515 :     double dfMaxZError = m_dfMaxZErrorOverview;
    2523         515 :     if (const char *opt = GetOptionValue("MAX_Z_ERROR", "MAX_Z_ERROR_OVERVIEW"))
    2524             :     {
    2525          20 :         dfMaxZError = CPLAtof(opt);
    2526             :     }
    2527             : 
    2528         515 :     signed char nJpegTablesMode = m_nJpegTablesMode;
    2529         515 :     if (const char *opt =
    2530         515 :             GetOptionValue("JPEG_TABLESMODE", "JPEG_TABLESMODE_OVERVIEW"))
    2531             :     {
    2532           0 :         nJpegTablesMode = static_cast<signed char>(atoi(opt));
    2533             :     }
    2534             : 
    2535             : #ifdef HAVE_JXL
    2536         515 :     bool bJXLLossless = m_bJXLLossless;
    2537         515 :     if (const char *opt =
    2538         515 :             GetOptionValue("JXL_LOSSLESS", "JXL_LOSSLESS_OVERVIEW"))
    2539             :     {
    2540           0 :         bJXLLossless = CPLTestBool(opt);
    2541             :     }
    2542             : 
    2543         515 :     float fJXLDistance = m_fJXLDistance;
    2544         515 :     if (const char *opt =
    2545         515 :             GetOptionValue("JXL_DISTANCE", "JXL_DISTANCE_OVERVIEW"))
    2546             :     {
    2547           0 :         fJXLDistance = static_cast<float>(CPLAtof(opt));
    2548             :     }
    2549             : 
    2550         515 :     float fJXLAlphaDistance = m_fJXLAlphaDistance;
    2551         515 :     if (const char *opt =
    2552         515 :             GetOptionValue("JXL_ALPHA_DISTANCE", "JXL_ALPHA_DISTANCE_OVERVIEW"))
    2553             :     {
    2554           0 :         fJXLAlphaDistance = static_cast<float>(CPLAtof(opt));
    2555             :     }
    2556             : 
    2557         515 :     int nJXLEffort = m_nJXLEffort;
    2558         515 :     if (const char *opt = GetOptionValue("JXL_EFFORT", "JXL_EFFORT_OVERVIEW"))
    2559             :     {
    2560           0 :         nJXLEffort = atoi(opt);
    2561             :     }
    2562             : #endif
    2563             : 
    2564        1030 :     auto poODS = std::make_shared<GTiffDataset>();
    2565         515 :     poODS->ShareLockWithParentDataset(this);
    2566         515 :     poODS->eAccess = GA_Update;
    2567         515 :     poODS->m_osFilename = m_osFilename;
    2568         515 :     const char *pszSparseOK = GetOptionValue("SPARSE_OK", "SPARSE_OK_OVERVIEW");
    2569         515 :     if (pszSparseOK && CPLTestBool(pszSparseOK))
    2570             :     {
    2571           1 :         poODS->m_bWriteEmptyTiles = false;
    2572           1 :         poODS->m_bFillEmptyTilesAtClosing = false;
    2573             :     }
    2574             :     else
    2575             :     {
    2576         514 :         poODS->m_bWriteEmptyTiles = m_bWriteEmptyTiles;
    2577         514 :         poODS->m_bFillEmptyTilesAtClosing = m_bFillEmptyTilesAtClosing;
    2578             :     }
    2579         515 :     poODS->m_nJpegQuality = static_cast<signed char>(l_nJpegQuality);
    2580         515 :     poODS->m_nWebPLevel = static_cast<signed char>(nWebpLevel);
    2581         515 :     poODS->m_nZLevel = static_cast<signed char>(nZLevel);
    2582         515 :     poODS->m_nLZMAPreset = m_nLZMAPreset;
    2583         515 :     poODS->m_nZSTDLevel = static_cast<signed char>(nZSTDLevel);
    2584         515 :     poODS->m_bWebPLossless = bWebpLossless;
    2585         515 :     poODS->m_nJpegTablesMode = nJpegTablesMode;
    2586         515 :     poODS->m_dfMaxZError = dfMaxZError;
    2587         515 :     poODS->m_dfMaxZErrorOverview = dfMaxZError;
    2588        1030 :     memcpy(poODS->m_anLercAddCompressionAndVersion,
    2589         515 :            m_anLercAddCompressionAndVersion,
    2590             :            sizeof(m_anLercAddCompressionAndVersion));
    2591             : #ifdef HAVE_JXL
    2592         515 :     poODS->m_bJXLLossless = bJXLLossless;
    2593         515 :     poODS->m_fJXLDistance = fJXLDistance;
    2594         515 :     poODS->m_fJXLAlphaDistance = fJXLAlphaDistance;
    2595         515 :     poODS->m_nJXLEffort = nJXLEffort;
    2596             : #endif
    2597             : 
    2598         515 :     if (poODS->OpenOffset(VSI_TIFFOpenChild(m_hTIFF), nOverviewOffset,
    2599         515 :                           GA_Update) != CE_None)
    2600             :     {
    2601           0 :         return CE_Failure;
    2602             :     }
    2603             : 
    2604             :     // Assign color interpretation from main dataset
    2605         515 :     const int l_nBands = GetRasterCount();
    2606        1534 :     for (int i = 1; i <= l_nBands; i++)
    2607             :     {
    2608        1019 :         auto poBand = dynamic_cast<GTiffRasterBand *>(poODS->GetRasterBand(i));
    2609        1019 :         if (poBand)
    2610        1019 :             poBand->m_eBandInterp = GetRasterBand(i)->GetColorInterpretation();
    2611             :     }
    2612             : 
    2613             :     // Do that now that m_nCompression is set
    2614         515 :     poODS->RestoreVolatileParameters(poODS->m_hTIFF);
    2615             : 
    2616         515 :     poODS->m_poBaseDS = this;
    2617         515 :     poODS->m_bIsOverview = true;
    2618             : 
    2619         515 :     m_apoOverviewDS.push_back(std::move(poODS));
    2620         515 :     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         330 : 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        1094 :         [papszOptions](const char *pszOptionKey, const char *pszConfigOptionKey,
    2686        2180 :                        const char **ppszKeyUsed = nullptr)
    2687             :     {
    2688        1094 :         const char *pszVal = CSLFetchNameValue(papszOptions, pszOptionKey);
    2689        1094 :         if (pszVal)
    2690             :         {
    2691           8 :             if (ppszKeyUsed)
    2692           8 :                 *ppszKeyUsed = pszOptionKey;
    2693           8 :             return pszVal;
    2694             :         }
    2695        1086 :         pszVal = CSLFetchNameValue(papszOptions, pszConfigOptionKey);
    2696        1086 :         if (pszVal)
    2697             :         {
    2698           0 :             if (ppszKeyUsed)
    2699           0 :                 *ppszKeyUsed = pszConfigOptionKey;
    2700           0 :             return pszVal;
    2701             :         }
    2702        1086 :         pszVal = CPLGetConfigOption(pszConfigOptionKey, nullptr);
    2703        1086 :         if (pszVal && ppszKeyUsed)
    2704          60 :             *ppszKeyUsed = pszConfigOptionKey;
    2705        1086 :         return pszVal;
    2706         330 :     };
    2707             : 
    2708             :     /* -------------------------------------------------------------------- */
    2709             :     /*      Determine compression method.                                   */
    2710             :     /* -------------------------------------------------------------------- */
    2711         330 :     nCompression = m_nCompression;
    2712         330 :     const char *pszOptionKey = "";
    2713             :     const char *pszCompressValue =
    2714         330 :         GetOptionValue("COMPRESS", "COMPRESS_OVERVIEW", &pszOptionKey);
    2715         330 :     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         330 :     nPlanarConfig = m_nPlanarConfig;
    2729         330 :     if (nCompression == COMPRESSION_WEBP)
    2730             :     {
    2731          11 :         nPlanarConfig = PLANARCONFIG_CONTIG;
    2732             :     }
    2733             :     const char *pszInterleave =
    2734         330 :         GetOptionValue("INTERLEAVE", "INTERLEAVE_OVERVIEW", &pszOptionKey);
    2735         330 :     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         330 :     nPredictor = PREDICTOR_NONE;
    2754         330 :     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         330 :     if (m_nPhotometric == PHOTOMETRIC_YCBCR && nCompression != COMPRESSION_JPEG)
    2770           1 :         nPhotometric = PHOTOMETRIC_RGB;
    2771             :     else
    2772         329 :         nPhotometric = m_nPhotometric;
    2773             :     const char *pszPhotometric =
    2774         330 :         GetOptionValue("PHOTOMETRIC", "PHOTOMETRIC_OVERVIEW", &pszOptionKey);
    2775         330 :     if (!GTIFFUpdatePhotometric(pszPhotometric, pszOptionKey, nCompression,
    2776         330 :                                 pszInterleave, nBands, nPhotometric,
    2777             :                                 nPlanarConfig))
    2778             :     {
    2779           0 :         return false;
    2780             :     }
    2781             : 
    2782             :     /* -------------------------------------------------------------------- */
    2783             :     /*      Determine JPEG quality                                          */
    2784             :     /* -------------------------------------------------------------------- */
    2785         330 :     nOvrJpegQuality = m_nJpegQuality;
    2786         330 :     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         330 :     if (m_bNoDataSet)
    2800             :     {
    2801          17 :         osNoData = GTiffFormatGDALNoDataTagValue(m_dfNoDataValue);
    2802             :     }
    2803             : 
    2804             :     /* -------------------------------------------------------------------- */
    2805             :     /*      Fetch extra sample tag                                          */
    2806             :     /* -------------------------------------------------------------------- */
    2807         330 :     panExtraSampleValues = nullptr;
    2808         330 :     nExtraSamples = 0;
    2809         330 :     if (TIFFGetField(m_hTIFF, TIFFTAG_EXTRASAMPLES, &nExtraSamples,
    2810         330 :                      &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         290 :         panExtraSampleValues = nullptr;
    2821         290 :         nExtraSamples = 0;
    2822             :     }
    2823             : 
    2824         330 :     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         274 : CPLErr GTiffDataset::CreateInternalMaskOverviews(int nOvrBlockXSize,
    2940             :                                                  int nOvrBlockYSize)
    2941             : {
    2942         274 :     ScanDirectories();
    2943             : 
    2944             :     /* -------------------------------------------------------------------- */
    2945             :     /*      Create overviews for the mask.                                  */
    2946             :     /* -------------------------------------------------------------------- */
    2947         274 :     CPLErr eErr = CE_None;
    2948             : 
    2949         274 :     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         274 :     ReloadDirectory();
    3003             : 
    3004         274 :     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         409 : 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         409 :     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         409 :     std::swap(m_apoJPEGOverviewDSOld, m_apoJPEGOverviewDS);
    3236         409 :     m_apoJPEGOverviewDS.clear();
    3237             : 
    3238             :     /* -------------------------------------------------------------------- */
    3239             :     /*      If RRD or external OVR overviews requested, then invoke         */
    3240             :     /*      generic handling.                                               */
    3241             :     /* -------------------------------------------------------------------- */
    3242         409 :     bool bUseGenericHandling = false;
    3243         409 :     bool bUseRRD = false;
    3244         818 :     CPLStringList aosOptions(papszOptions);
    3245             : 
    3246         409 :     const char *pszLocation = CSLFetchNameValue(papszOptions, "LOCATION");
    3247         409 :     if (pszLocation && EQUAL(pszLocation, "EXTERNAL"))
    3248             :     {
    3249           1 :         bUseGenericHandling = true;
    3250             :     }
    3251         408 :     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         408 :     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         405 :     else if ((bUseRRD = CPLTestBool(
    3269             :                   CSLFetchNameValueDef(papszOptions, "USE_RRD",
    3270         810 :                                        CPLGetConfigOption("USE_RRD", "NO")))) ||
    3271         405 :              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         409 :     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         409 :     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         263 :     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         263 :     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         255 :     CPLErr eErr = CE_None;
    3348             : 
    3349             :     /* -------------------------------------------------------------------- */
    3350             :     /*      Initialize progress counter.                                    */
    3351             :     /* -------------------------------------------------------------------- */
    3352         255 :     if (!pfnProgress(0.0, nullptr, pProgressData))
    3353             :     {
    3354           0 :         ReportError(CE_Failure, CPLE_UserInterrupt, "User terminated");
    3355           0 :         return CE_Failure;
    3356             :     }
    3357             : 
    3358         255 :     FlushDirectory();
    3359             : 
    3360             :     /* -------------------------------------------------------------------- */
    3361             :     /*      If we are averaging bit data to grayscale we need to create     */
    3362             :     /*      8bit overviews.                                                 */
    3363             :     /* -------------------------------------------------------------------- */
    3364         255 :     int nOvBitsPerSample = m_nBitsPerSample;
    3365             : 
    3366         255 :     if (STARTS_WITH_CI(pszResampling, "AVERAGE_BIT2"))
    3367           2 :         nOvBitsPerSample = 8;
    3368             : 
    3369             :     /* -------------------------------------------------------------------- */
    3370             :     /*      Do we need some metadata for the overviews?                     */
    3371             :     /* -------------------------------------------------------------------- */
    3372         510 :     CPLString osMetadata;
    3373             : 
    3374         255 :     const bool bIsForMaskBand = nBands == 1 && GetRasterBand(1)->IsMaskBand();
    3375         255 :     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         510 :     std::string osNoData;
    3383         255 :     uint16_t *panExtraSampleValues = nullptr;
    3384         255 :     uint16_t nExtraSamples = 0;
    3385         255 :     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         510 :     std::vector<unsigned short> anTRed;
    3397         510 :     std::vector<unsigned short> anTGreen;
    3398         510 :     std::vector<unsigned short> anTBlue;
    3399         255 :     unsigned short *panRed = nullptr;
    3400         255 :     unsigned short *panGreen = nullptr;
    3401         255 :     unsigned short *panBlue = nullptr;
    3402             : 
    3403         255 :     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         255 :     int nOvrBlockXSize = 0;
    3419         255 :     int nOvrBlockYSize = 0;
    3420         255 :     GTIFFGetOverviewBlockSize(GDALRasterBand::ToHandle(GetRasterBand(1)),
    3421             :                               &nOvrBlockXSize, &nOvrBlockYSize, papszOptions,
    3422             :                               "BLOCKSIZE");
    3423         510 :     std::vector<bool> abRequireNewOverview(nOverviews, true);
    3424         693 :     for (int i = 0; i < nOverviews && eErr == CE_None; ++i)
    3425             :     {
    3426         770 :         for (auto &poODS : m_apoOverviewDS)
    3427             :         {
    3428             :             const int nOvFactor =
    3429         776 :                 GDALComputeOvFactor(poODS->GetRasterXSize(), GetRasterXSize(),
    3430         388 :                                     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         448 :             if (poODS->GetRasterXSize() == 1 && poODS->GetRasterYSize() == 1 &&
    3435         448 :                 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         699 :             if (nOvFactor == panOverviewList[i] ||
    3443         332 :                 nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
    3444             :                                                 GetRasterXSize(),
    3445             :                                                 GetRasterYSize()))
    3446             :             {
    3447          35 :                 abRequireNewOverview[i] = false;
    3448          35 :                 break;
    3449             :             }
    3450             :         }
    3451             : 
    3452         438 :         if (abRequireNewOverview[i])
    3453             :         {
    3454         382 :             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         382 :                 DIV_ROUND_UP(GetRasterXSize(), panOverviewList[i]);
    3466             :             const int nOYSize =
    3467         382 :                 DIV_ROUND_UP(GetRasterYSize(), panOverviewList[i]);
    3468             : 
    3469         764 :             const toff_t nOverviewOffset = GTIFFWriteDirectory(
    3470             :                 m_hTIFF, FILETYPE_REDUCEDIMAGE, nOXSize, nOYSize,
    3471         382 :                 nOvBitsPerSample, nPlanarConfig, m_nSamplesPerPixel,
    3472             :                 nOvrBlockXSize, nOvrBlockYSize, TRUE, nCompression,
    3473         382 :                 nPhotometric, m_nSampleFormat, nPredictor, panRed, panGreen,
    3474             :                 panBlue, nExtraSamples, panExtraSampleValues, osMetadata,
    3475         382 :                 nOvrJpegQuality >= 0 ? CPLSPrintf("%d", nOvrJpegQuality)
    3476             :                                      : nullptr,
    3477         382 :                 CPLSPrintf("%d", m_nJpegTablesMode),
    3478          25 :                 osNoData.empty() ? nullptr : osNoData.c_str(),
    3479         382 :                 m_anLercAddCompressionAndVersion, false);
    3480             : 
    3481         382 :             if (nOverviewOffset == 0)
    3482           0 :                 eErr = CE_Failure;
    3483             :             else
    3484         382 :                 eErr = RegisterNewOverviewDataset(
    3485             :                     nOverviewOffset, nOvrJpegQuality, papszOptions);
    3486             :         }
    3487             :     }
    3488             : 
    3489         255 :     CPLFree(panExtraSampleValues);
    3490         255 :     panExtraSampleValues = nullptr;
    3491             : 
    3492         255 :     ReloadDirectory();
    3493             : 
    3494             :     /* -------------------------------------------------------------------- */
    3495             :     /*      Create overviews for the mask.                                  */
    3496             :     /* -------------------------------------------------------------------- */
    3497         255 :     if (eErr != CE_None)
    3498           0 :         return eErr;
    3499             : 
    3500         255 :     eErr = CreateInternalMaskOverviews(nOvrBlockXSize, nOvrBlockYSize);
    3501             : 
    3502             :     /* -------------------------------------------------------------------- */
    3503             :     /*      Refresh overviews for the mask                                  */
    3504             :     /* -------------------------------------------------------------------- */
    3505             :     const bool bHasInternalMask =
    3506         255 :         m_poMaskDS != nullptr && m_poMaskDS->GetRasterCount() == 1;
    3507             :     const bool bHasExternalMask =
    3508         255 :         !bHasInternalMask && oOvManager.HaveMaskFile();
    3509         255 :     const bool bHasMask = bHasInternalMask || bHasExternalMask;
    3510             : 
    3511         255 :     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         231 :     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         255 :     bool bHasAlphaBand = false;
    3545       66257 :     for (int iBand = 0; iBand < nBands; iBand++)
    3546             :     {
    3547       66002 :         if (papoBands[iBand]->GetColorInterpretation() == GCI_AlphaBand)
    3548          18 :             bHasAlphaBand = true;
    3549             :     }
    3550             : 
    3551             :     /* -------------------------------------------------------------------- */
    3552             :     /*      Refresh old overviews that were listed.                         */
    3553             :     /* -------------------------------------------------------------------- */
    3554         255 :     const auto poColorTable = GetRasterBand(panBandList[0])->GetColorTable();
    3555          21 :     if ((m_nPlanarConfig == PLANARCONFIG_CONTIG || bHasAlphaBand) &&
    3556         236 :         GDALDataTypeIsComplex(
    3557         236 :             GetRasterBand(panBandList[0])->GetRasterDataType()) == FALSE &&
    3558          12 :         (poColorTable == nullptr || STARTS_WITH_CI(pszResampling, "NEAR") ||
    3559         511 :          poColorTable->IsIdentity()) &&
    3560         228 :         (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         206 :         int nNewOverviews = 0;
    3576             : 
    3577             :         GDALRasterBand ***papapoOverviewBands = static_cast<GDALRasterBand ***>(
    3578         206 :             CPLCalloc(sizeof(void *), nBandsIn));
    3579             :         GDALRasterBand **papoBandList =
    3580         206 :             static_cast<GDALRasterBand **>(CPLCalloc(sizeof(void *), nBandsIn));
    3581       66111 :         for (int iBand = 0; iBand < nBandsIn; ++iBand)
    3582             :         {
    3583       65905 :             GDALRasterBand *poBand = GetRasterBand(panBandList[iBand]);
    3584             : 
    3585       65905 :             papoBandList[iBand] = poBand;
    3586      131810 :             papapoOverviewBands[iBand] = static_cast<GDALRasterBand **>(
    3587       65905 :                 CPLCalloc(sizeof(void *), poBand->GetOverviewCount()));
    3588             : 
    3589       65905 :             int iCurOverview = 0;
    3590             :             std::vector<bool> abAlreadyUsedOverviewBand(
    3591       65905 :                 poBand->GetOverviewCount(), false);
    3592             : 
    3593      132095 :             for (int i = 0; i < nOverviews; ++i)
    3594             :             {
    3595       66649 :                 for (int j = 0; j < poBand->GetOverviewCount(); ++j)
    3596             :                 {
    3597       66634 :                     if (abAlreadyUsedOverviewBand[j])
    3598         458 :                         continue;
    3599             : 
    3600             :                     int nOvFactor;
    3601       66176 :                     GDALRasterBand *poOverview = poBand->GetOverview(j);
    3602             : 
    3603       66176 :                     nOvFactor = GDALComputeOvFactor(
    3604             :                         poOverview->GetXSize(), poBand->GetXSize(),
    3605             :                         poOverview->GetYSize(), poBand->GetYSize());
    3606             : 
    3607       66176 :                     GDALCopyNoDataValue(poOverview, poBand);
    3608             : 
    3609       66177 :                     if (nOvFactor == panOverviewList[i] ||
    3610           1 :                         nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
    3611             :                                                         poBand->GetXSize(),
    3612             :                                                         poBand->GetYSize()))
    3613             :                     {
    3614       66175 :                         if (iBand == 0)
    3615             :                         {
    3616             :                             const auto osNewResampling =
    3617         664 :                                 GDALGetNormalizedOvrResampling(pszResampling);
    3618             :                             const char *pszExistingResampling =
    3619         332 :                                 poOverview->GetMetadataItem("RESAMPLING");
    3620         664 :                             if (pszExistingResampling &&
    3621         332 :                                 pszExistingResampling != osNewResampling)
    3622             :                             {
    3623           2 :                                 poOverview->SetMetadataItem(
    3624           2 :                                     "RESAMPLING", osNewResampling.c_str());
    3625             :                             }
    3626             :                         }
    3627             : 
    3628       66175 :                         abAlreadyUsedOverviewBand[j] = true;
    3629       66175 :                         CPLAssert(iCurOverview < poBand->GetOverviewCount());
    3630       66175 :                         papapoOverviewBands[iBand][iCurOverview] = poOverview;
    3631       66175 :                         ++iCurOverview;
    3632       66175 :                         break;
    3633             :                     }
    3634             :                 }
    3635             :             }
    3636             : 
    3637       65905 :             if (nNewOverviews == 0)
    3638             :             {
    3639         206 :                 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         206 :             bHasMask ? GDALCreateScaledProgress(1.0 / (nBands + 1), 1.0,
    3650             :                                                 pfnProgress, pProgressData)
    3651         180 :                      : GDALCreateScaledProgress(0.0, 1.0, pfnProgress,
    3652         206 :                                                 pProgressData);
    3653         206 :         GDALRegenerateOverviewsMultiBand(nBandsIn, papoBandList, nNewOverviews,
    3654             :                                          papapoOverviewBands, pszResampling,
    3655             :                                          GDALScaledProgress,
    3656             :                                          pScaledProgressData, papszOptions);
    3657         206 :         GDALDestroyScaledProgress(pScaledProgressData);
    3658             : 
    3659       66111 :         for (int iBand = 0; iBand < nBandsIn; ++iBand)
    3660             :         {
    3661       65905 :             CPLFree(papapoOverviewBands[iBand]);
    3662             :         }
    3663         206 :         CPLFree(papapoOverviewBands);
    3664         206 :         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         255 :     pfnProgress(1.0, nullptr, pProgressData);
    3753             : 
    3754         255 :     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        3197 : static bool IsSRSCompatibleOfGeoTIFF(const OGRSpatialReference *poSRS,
    3783             :                                      GTIFFKeysFlavorEnum eGeoTIFFKeysFlavor)
    3784             : {
    3785        3197 :     char *pszWKT = nullptr;
    3786        3197 :     if ((poSRS->IsGeographic() || poSRS->IsProjected()) && !poSRS->IsCompound())
    3787             :     {
    3788        3179 :         const char *pszAuthName = poSRS->GetAuthorityName();
    3789        3179 :         const char *pszAuthCode = poSRS->GetAuthorityCode();
    3790        3179 :         if (pszAuthName && pszAuthCode && EQUAL(pszAuthName, "EPSG"))
    3791        2608 :             return true;
    3792             :     }
    3793             :     OGRErr eErr;
    3794             :     {
    3795        1178 :         CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
    3796        1178 :         if (poSRS->IsDerivedGeographic() ||
    3797         589 :             (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         589 :             const char *const apszOptions[] = {
    3807         589 :                 poSRS->IsGeographic() ? nullptr : "FORMAT=WKT1", nullptr};
    3808         589 :             eErr = poSRS->exportToWkt(&pszWKT, apszOptions);
    3809         589 :             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         589 :     const bool bCompatibleOfGeoTIFF =
    3820        1177 :         (eErr == OGRERR_NONE && pszWKT != nullptr &&
    3821         588 :          strstr(pszWKT, "custom_proj4") == nullptr);
    3822         589 :     CPLFree(pszWKT);
    3823         589 :     return bCompatibleOfGeoTIFF;
    3824             : }
    3825             : 
    3826             : /************************************************************************/
    3827             : /*                          WriteGeoTIFFInfo()                          */
    3828             : /************************************************************************/
    3829             : 
    3830        5928 : void GTiffDataset::WriteGeoTIFFInfo()
    3831             : 
    3832             : {
    3833        5928 :     bool bPixelIsPoint = false;
    3834        5928 :     bool bPointGeoIgnore = false;
    3835             : 
    3836             :     const char *pszAreaOrPoint =
    3837        5928 :         GTiffDataset::GetMetadataItem(GDALMD_AREA_OR_POINT);
    3838        5928 :     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        5928 :     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        5928 :     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        5928 :     if (m_bGeoTransformValid)
    3869             :     {
    3870        1849 :         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        1849 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOPIXELSCALE);
    3879        1849 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOTIEPOINTS);
    3880        1849 :         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        1849 :         if (m_gt.xrot == 0.0 && m_gt.yrot == 0.0 && m_gt.yscale < 0.0)
    3889             :         {
    3890        1756 :             double dfOffset = 0.0;
    3891        1756 :             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        1750 :                 int bHasScale = FALSE;
    3896        1750 :                 double dfScale = GetRasterBand(1)->GetScale(&bHasScale);
    3897        1750 :                 int bHasOffset = FALSE;
    3898        1750 :                 dfOffset = GetRasterBand(1)->GetOffset(&bHasOffset);
    3899             :                 const bool bApplyScaleOffset =
    3900        1750 :                     m_oSRS.IsVertical() && GetRasterCount() == 1;
    3901        1750 :                 if (bApplyScaleOffset && !bHasScale)
    3902           0 :                     dfScale = 1.0;
    3903        1750 :                 if (!bApplyScaleOffset || !bHasOffset)
    3904        1747 :                     dfOffset = 0.0;
    3905        1750 :                 const double adfPixelScale[3] = {m_gt.xscale, fabs(m_gt.yscale),
    3906        1750 :                                                  bApplyScaleOffset ? dfScale
    3907        1750 :                                                                    : 0.0};
    3908        1750 :                 TIFFSetField(m_hTIFF, TIFFTAG_GEOPIXELSCALE, 3, adfPixelScale);
    3909             :             }
    3910             : 
    3911        1756 :             double adfTiePoints[6] = {0.0,        0.0,        0.0,
    3912        1756 :                                       m_gt.xorig, m_gt.yorig, dfOffset};
    3913             : 
    3914        1756 :             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        1756 :             if (m_eProfile != GTiffProfile::BASELINE)
    3921        1756 :                 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        1849 :         if (m_poBaseDS == nullptr)
    3946             :         {
    3947             :             // Do we need a world file?
    3948        1849 :             if (CPLFetchBool(m_papszCreationOptions, "TFW", false))
    3949           7 :                 GDALWriteWorldFile(m_osFilename.c_str(), "tfw", m_gt.data());
    3950        1842 :             else if (CPLFetchBool(m_papszCreationOptions, "WORLDFILE", false))
    3951           2 :                 GDALWriteWorldFile(m_osFilename.c_str(), "wld", m_gt.data());
    3952             :         }
    3953             :     }
    3954        4092 :     else if (GetGCPCount() > 0 && GetGCPCount() <= knMAX_GCP_COUNT &&
    3955          13 :              m_eProfile != GTiffProfile::BASELINE)
    3956             :     {
    3957          13 :         m_bNeedsRewrite = true;
    3958             : 
    3959             :         double *padfTiePoints = static_cast<double *>(
    3960          13 :             CPLMalloc(6 * sizeof(double) * GetGCPCount()));
    3961             : 
    3962          69 :         for (size_t iGCP = 0; iGCP < m_aoGCPs.size(); ++iGCP)
    3963             :         {
    3964             : 
    3965          56 :             padfTiePoints[iGCP * 6 + 0] = m_aoGCPs[iGCP].Pixel();
    3966          56 :             padfTiePoints[iGCP * 6 + 1] = m_aoGCPs[iGCP].Line();
    3967          56 :             padfTiePoints[iGCP * 6 + 2] = 0;
    3968          56 :             padfTiePoints[iGCP * 6 + 3] = m_aoGCPs[iGCP].X();
    3969          56 :             padfTiePoints[iGCP * 6 + 4] = m_aoGCPs[iGCP].Y();
    3970          56 :             padfTiePoints[iGCP * 6 + 5] = m_aoGCPs[iGCP].Z();
    3971             : 
    3972          56 :             if (bPixelIsPoint && !bPointGeoIgnore)
    3973             :             {
    3974           0 :                 padfTiePoints[iGCP * 6 + 0] += 0.5;
    3975           0 :                 padfTiePoints[iGCP * 6 + 1] += 0.5;
    3976             :             }
    3977             :         }
    3978             : 
    3979          13 :         TIFFSetField(m_hTIFF, TIFFTAG_GEOTIEPOINTS, 6 * GetGCPCount(),
    3980             :                      padfTiePoints);
    3981          13 :         CPLFree(padfTiePoints);
    3982             :     }
    3983             : 
    3984             :     /* -------------------------------------------------------------------- */
    3985             :     /*      Write out projection definition.                                */
    3986             :     /* -------------------------------------------------------------------- */
    3987        5928 :     const bool bHasProjection = !m_oSRS.IsEmpty();
    3988        5928 :     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        5928 : }
    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      311213 : 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      311213 :     CSLConstList papszDomainList = poMDMD->GetDomainList();
    4117      319778 :     for (int iDomain = 0; papszDomainList && papszDomainList[iDomain];
    4118             :          ++iDomain)
    4119             :     {
    4120        8565 :         CSLConstList papszMD = poMDMD->GetMetadata(papszDomainList[iDomain]);
    4121        8565 :         bool bIsXMLOrJSON = false;
    4122             : 
    4123        8565 :         if (EQUAL(papszDomainList[iDomain], "IMAGE_STRUCTURE") ||
    4124        2490 :             EQUAL(papszDomainList[iDomain], "DERIVED_SUBDATASETS"))
    4125        6078 :             continue;  // Ignored.
    4126        2487 :         if (EQUAL(papszDomainList[iDomain], "COLOR_PROFILE"))
    4127           3 :             continue;  // Handled elsewhere.
    4128        2484 :         if (EQUAL(papszDomainList[iDomain], MD_DOMAIN_RPC))
    4129           7 :             continue;  // Handled elsewhere.
    4130        2478 :         if (EQUAL(papszDomainList[iDomain], "xml:ESRI") &&
    4131           1 :             CPLTestBool(CPLGetConfigOption("ESRI_XML_PAM", "NO")))
    4132           1 :             continue;  // Handled elsewhere.
    4133        2476 :         if (EQUAL(papszDomainList[iDomain], "xml:XMP"))
    4134           2 :             continue;  // Handled in SetMetadata.
    4135             : 
    4136        2474 :         if (STARTS_WITH_CI(papszDomainList[iDomain], "xml:") ||
    4137        2472 :             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        7575 :         for (int iItem = 0; papszMD && papszMD[iItem]; ++iItem)
    4148             :         {
    4149        5101 :             const char *pszItemValue = nullptr;
    4150        5101 :             char *pszItemName = nullptr;
    4151             : 
    4152        5101 :             if (bIsXMLOrJSON)
    4153             :             {
    4154          11 :                 pszItemName = CPLStrdup("doc");
    4155          11 :                 pszItemValue = papszMD[iItem];
    4156             :             }
    4157             :             else
    4158             :             {
    4159        5090 :                 pszItemValue = CPLParseNameValue(papszMD[iItem], &pszItemName);
    4160        5090 :                 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        5052 :             if (strlen(papszDomainList[iDomain]) == 0 && nBand == 0 &&
    4174        3697 :                 (STARTS_WITH_CI(pszItemName, "TIFFTAG_") ||
    4175        3636 :                  (EQUAL(pszItemName, "GEO_METADATA") &&
    4176        3635 :                   eProfile == GTiffProfile::GDALGEOTIFF) ||
    4177        3635 :                  (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        4989 :             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        5052 :             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        2474 :         if (strlen(papszDomainList[iDomain]) == 0 && nBand == 0)
    4253             :         {
    4254        2188 :             const auto *pasTIFFTags = GTiffDataset::GetTIFFTags();
    4255       32820 :             for (size_t iTag = 0; pasTIFFTags[iTag].pszTagName; ++iTag)
    4256             :             {
    4257       30632 :                 uint32_t nCount = 0;
    4258       30632 :                 char *pszText = nullptr;
    4259       30632 :                 int16_t nVal = 0;
    4260       30632 :                 float fVal = 0.0f;
    4261             :                 const char *pszVal =
    4262       30632 :                     CSLFetchNameValue(papszMD, pasTIFFTags[iTag].pszTagName);
    4263       61201 :                 if (pszVal == nullptr &&
    4264       30569 :                     ((pasTIFFTags[iTag].eType == GTIFFTAGTYPE_STRING &&
    4265       17471 :                       TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal,
    4266       30561 :                                    &pszText)) ||
    4267       30561 :                      (pasTIFFTags[iTag].eType == GTIFFTAGTYPE_SHORT &&
    4268        6551 :                       TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal, &nVal)) ||
    4269       30558 :                      (pasTIFFTags[iTag].eType == GTIFFTAGTYPE_FLOAT &&
    4270        4360 :                       TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal, &fVal)) ||
    4271       30557 :                      (pasTIFFTags[iTag].eType == GTIFFTAGTYPE_BYTE_STRING &&
    4272        2187 :                       TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal, &nCount,
    4273             :                                    &pszText))))
    4274             :                 {
    4275          13 :                     TIFFUnsetField(hTIFF, pasTIFFTags[iTag].nTagVal);
    4276             :                 }
    4277             :             }
    4278             :         }
    4279             :     }
    4280      311213 : }
    4281             : 
    4282             : /************************************************************************/
    4283             : /*                              WriteRPC()                              */
    4284             : /************************************************************************/
    4285             : 
    4286       10249 : 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       10249 :     CSLConstList papszRPCMD = poSrcDS->GetMetadata(MD_DOMAIN_RPC);
    4297       10249 :     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       10249 : }
    4336             : 
    4337             : /************************************************************************/
    4338             : /*                           WriteMetadata()                            */
    4339             : /************************************************************************/
    4340             : 
    4341        8133 : 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        8133 :     CPLXMLNode *psRoot = nullptr;
    4353        8133 :     CPLXMLNode *psTail = nullptr;
    4354             : 
    4355             :     const char *pszCopySrcMDD =
    4356        8133 :         CSLFetchNameValueDef(papszCreationOptions, "COPY_SRC_MDD", "AUTO");
    4357             :     char **papszSrcMDD =
    4358        8133 :         CSLFetchNameValueMultiple(papszCreationOptions, "SRC_MDD");
    4359             : 
    4360             :     GTiffDataset *poSrcDSGTiff =
    4361        8133 :         bSrcIsGeoTIFF ? cpl::down_cast<GTiffDataset *>(poSrcDS) : nullptr;
    4362             : 
    4363        8133 :     if (poSrcDSGTiff)
    4364             :     {
    4365        5990 :         WriteMDMetadata(&poSrcDSGTiff->m_oGTiffMDMD, l_hTIFF, &psRoot, &psTail,
    4366             :                         0, eProfile);
    4367             :     }
    4368             :     else
    4369             :     {
    4370        2143 :         if (EQUAL(pszCopySrcMDD, "AUTO") || CPLTestBool(pszCopySrcMDD) ||
    4371             :             papszSrcMDD)
    4372             :         {
    4373        4280 :             GDALMultiDomainMetadata l_oMDMD;
    4374             :             {
    4375        2140 :                 CSLConstList papszMD = poSrcDS->GetMetadata();
    4376        2144 :                 if (CSLCount(papszMD) > 0 &&
    4377           4 :                     (!papszSrcMDD || CSLFindString(papszSrcMDD, "") >= 0 ||
    4378           2 :                      CSLFindString(papszSrcMDD, "_DEFAULT_") >= 0))
    4379             :                 {
    4380        1622 :                     l_oMDMD.SetMetadata(papszMD);
    4381             :                 }
    4382             :             }
    4383             : 
    4384        2140 :             if (EQUAL(pszCopySrcMDD, "AUTO") && !papszSrcMDD)
    4385             :             {
    4386             :                 // Propagate ISIS3 or VICAR metadata
    4387        6393 :                 for (const char *pszMDD : {"json:ISIS3", "json:VICAR"})
    4388             :                 {
    4389        4262 :                     CSLConstList papszMD = poSrcDS->GetMetadata(pszMDD);
    4390        4262 :                     if (papszMD)
    4391             :                     {
    4392           5 :                         l_oMDMD.SetMetadata(papszMD, pszMDD);
    4393             :                     }
    4394             :                 }
    4395             :             }
    4396             : 
    4397        2140 :             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        2140 :             WriteMDMetadata(&l_oMDMD, l_hTIFF, &psRoot, &psTail, 0, eProfile);
    4417             :         }
    4418             :     }
    4419             : 
    4420        8133 :     if (!bExcludeRPBandIMGFileWriting &&
    4421        5984 :         (!poSrcDSGTiff || poSrcDSGTiff->m_poBaseDS == nullptr))
    4422             :     {
    4423        8122 :         WriteRPC(poSrcDS, l_hTIFF, bSrcIsGeoTIFF, eProfile, pszTIFFFilename,
    4424             :                  papszCreationOptions);
    4425             : 
    4426             :         /* ------------------------------------------------------------------ */
    4427             :         /*      Handle metadata data written to an IMD file. */
    4428             :         /* ------------------------------------------------------------------ */
    4429        8122 :         CSLConstList papszIMDMD = poSrcDS->GetMetadata(MD_DOMAIN_IMD);
    4430        8122 :         if (papszIMDMD != nullptr)
    4431             :         {
    4432          20 :             GDALWriteIMDFile(pszTIFFFilename, papszIMDMD);
    4433             :         }
    4434             :     }
    4435             : 
    4436        8133 :     uint16_t nPhotometric = 0;
    4437        8133 :     if (!TIFFGetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, &(nPhotometric)))
    4438           1 :         nPhotometric = PHOTOMETRIC_MINISBLACK;
    4439             : 
    4440        8133 :     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      316197 :     for (int nBand = 1; nBand <= poSrcDS->GetRasterCount(); ++nBand)
    4448             :     {
    4449      308064 :         GDALRasterBand *poBand = poSrcDS->GetRasterBand(nBand);
    4450             : 
    4451      308064 :         if (bSrcIsGeoTIFF)
    4452             :         {
    4453             :             GTiffRasterBand *poSrcBandGTiff =
    4454      302992 :                 cpl::down_cast<GTiffRasterBand *>(poBand);
    4455      302992 :             assert(poSrcBandGTiff);
    4456      302992 :             WriteMDMetadata(&poSrcBandGTiff->m_oGTiffMDMD, l_hTIFF, &psRoot,
    4457             :                             &psTail, nBand, eProfile);
    4458             :         }
    4459             :         else
    4460             :         {
    4461       10144 :             GDALMultiDomainMetadata l_oMDMD;
    4462        5072 :             bool bOMDMDSet = false;
    4463             : 
    4464        5072 :             if (EQUAL(pszCopySrcMDD, "AUTO") && !papszSrcMDD)
    4465             :             {
    4466       15180 :                 for (const char *pszDomain : {"", "IMAGERY"})
    4467             :                 {
    4468       10120 :                     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        5060 :                 }
    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        5072 :             if (bOMDMDSet)
    4498             :             {
    4499          91 :                 WriteMDMetadata(&l_oMDMD, l_hTIFF, &psRoot, &psTail, nBand,
    4500             :                                 eProfile);
    4501             :             }
    4502             :         }
    4503             : 
    4504      308064 :         const double dfOffset = poBand->GetOffset();
    4505      308064 :         const double dfScale = poBand->GetScale();
    4506      308064 :         bool bGeoTIFFScaleOffsetInZ = false;
    4507      308064 :         GDALGeoTransform gt;
    4508             :         // Check if we have already encoded scale/offset in the GeoTIFF tags
    4509      314248 :         if (poSrcDS->GetGeoTransform(gt) == CE_None && gt.xrot == 0.0 &&
    4510        6168 :             gt.yrot == 0.0 && gt.yscale < 0.0 && poSrcDS->GetSpatialRef() &&
    4511      314255 :             poSrcDS->GetSpatialRef()->IsVertical() &&
    4512           7 :             poSrcDS->GetRasterCount() == 1)
    4513             :         {
    4514           7 :             bGeoTIFFScaleOffsetInZ = true;
    4515             :         }
    4516             : 
    4517      308064 :         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      308064 :         const char *pszUnitType = poBand->GetUnitType();
    4530      308064 :         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      308064 :         if (strlen(poBand->GetDescription()) > 0)
    4551             :         {
    4552          24 :             AppendMetadataItem(&psRoot, &psTail, "DESCRIPTION",
    4553          24 :                                poBand->GetDescription(), nBand, "description",
    4554             :                                "");
    4555             :         }
    4556             : 
    4557      308281 :         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        8133 :     CSLDestroy(papszSrcMDD);
    4570             : 
    4571             :     const char *pszTilingSchemeName =
    4572        8133 :         CSLFetchNameValue(papszCreationOptions, "@TILING_SCHEME_NAME");
    4573        8133 :     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        8133 :     if (const char *pszOverviewResampling =
    4596        8133 :             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        8133 :     if (CPLTestBool(
    4607             :             CPLGetConfigOption("GTIFF_WRITE_IMAGE_STRUCTURE_METADATA", "YES")))
    4608             :     {
    4609             :         const char *pszTileInterleave =
    4610        8128 :             CSLFetchNameValue(papszCreationOptions, "@TILE_INTERLEAVE");
    4611        8128 :         if (pszTileInterleave && CPLTestBool(pszTileInterleave))
    4612             :         {
    4613           7 :             AppendMetadataItem(&psRoot, &psTail, "INTERLEAVE", "TILE", 0,
    4614             :                                nullptr, "IMAGE_STRUCTURE");
    4615             :         }
    4616             : 
    4617             :         const char *pszCompress =
    4618        8128 :             CSLFetchNameValue(papszCreationOptions, "COMPRESS");
    4619        8128 :         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        8097 :         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        8000 :         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        8133 :     if (!CPLTestBool(CPLGetConfigOption("GTIFF_WRITE_RAT_TO_PAM", "NO")))
    4699             :     {
    4700      316191 :         for (int nBand = 1; nBand <= poSrcDS->GetRasterCount(); ++nBand)
    4701             :         {
    4702      308061 :             GDALRasterAttributeTable *poRAT = nullptr;
    4703      308061 :             if (poSrcDSGTiff)
    4704             :             {
    4705      302990 :                 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      302990 :                 if (poBand->m_bRATSet)
    4711         106 :                     poRAT = poBand->GetDefaultRAT();
    4712             :             }
    4713             :             else
    4714             :             {
    4715        5071 :                 poRAT = poSrcDS->GetRasterBand(nBand)->GetDefaultRAT();
    4716             :             }
    4717      308061 :             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        8133 :     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        7369 :     if (eProfile == GTiffProfile::GDALGEOTIFF)
    4759             :     {
    4760        7345 :         char *pszText = nullptr;
    4761        7345 :         if (TIFFGetField(l_hTIFF, TIFFTAG_GDAL_METADATA, &pszText))
    4762             :         {
    4763           7 :             TIFFUnsetField(l_hTIFF, TIFFTAG_GDAL_METADATA);
    4764             :         }
    4765             :     }
    4766             : 
    4767        7369 :     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 (!poBand)
    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         522 : void GTiffDataset::WriteNoDataValue(TIFF *hTIFF, double dfNoData)
    4868             : 
    4869             : {
    4870        1044 :     CPLString osVal(GTiffFormatGDALNoDataTagValue(dfNoData));
    4871         522 :     TIFFSetField(hTIFF, TIFFTAG_GDAL_NODATA, osVal.c_str());
    4872         522 : }
    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        9878 : void GTiffDataset::SaveICCProfile(GTiffDataset *pDS, TIFF *l_hTIFF,
    4917             :                                   CSLConstList papszParamList,
    4918             :                                   uint32_t l_nBitsPerSample)
    4919             : {
    4920        9878 :     if ((pDS != nullptr) && (pDS->eAccess != GA_Update))
    4921           0 :         return;
    4922             : 
    4923        9878 :     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        9878 :     if ((papszParamList == nullptr) && (pDS == nullptr))
    4934        4905 :         return;
    4935             : 
    4936             :     const char *pszICCProfile =
    4937             :         (pDS != nullptr)
    4938        4973 :             ? pDS->GetMetadataItem("SOURCE_ICC_PROFILE", "COLOR_PROFILE")
    4939        4971 :             : CSLFetchNameValue(papszParamList, "SOURCE_ICC_PROFILE");
    4940        4973 :     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        4965 :         float pCHR[6] = {};     // Primaries.
    4954        4965 :         uint16_t pTXR[6] = {};  // Transfer range.
    4955        4965 :         const char *pszCHRNames[] = {"SOURCE_PRIMARIES_RED",
    4956             :                                      "SOURCE_PRIMARIES_GREEN",
    4957             :                                      "SOURCE_PRIMARIES_BLUE"};
    4958        4965 :         const char *pszTXRNames[] = {"TIFFTAG_TRANSFERRANGE_BLACK",
    4959             :                                      "TIFFTAG_TRANSFERRANGE_WHITE"};
    4960             : 
    4961             :         // Output chromacities.
    4962        4965 :         bool bOutputCHR = true;
    4963        4980 :         for (int i = 0; i < 3 && bOutputCHR; ++i)
    4964             :         {
    4965             :             const char *pszColorProfile =
    4966             :                 (pDS != nullptr)
    4967        4975 :                     ? pDS->GetMetadataItem(pszCHRNames[i], "COLOR_PROFILE")
    4968        4972 :                     : CSLFetchNameValue(papszParamList, pszCHRNames[i]);
    4969        4975 :             if (pszColorProfile == nullptr)
    4970             :             {
    4971        4960 :                 bOutputCHR = false;
    4972        4960 :                 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        4965 :         if (bOutputCHR)
    5007             :         {
    5008           5 :             TIFFSetField(l_hTIFF, TIFFTAG_PRIMARYCHROMATICITIES, pCHR);
    5009             :         }
    5010             : 
    5011             :         // Output whitepoint.
    5012             :         const char *pszSourceWhitePoint =
    5013             :             (pDS != nullptr)
    5014        4965 :                 ? pDS->GetMetadataItem("SOURCE_WHITEPOINT", "COLOR_PROFILE")
    5015        4964 :                 : CSLFetchNameValue(papszParamList, "SOURCE_WHITEPOINT");
    5016        4965 :         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        4965 :                 ? pDS->GetMetadataItem("TIFFTAG_TRANSFERFUNCTION_RED",
    5061             :                                        "COLOR_PROFILE")
    5062        4964 :                 : CSLFetchNameValue(papszParamList,
    5063        4965 :                                     "TIFFTAG_TRANSFERFUNCTION_RED");
    5064             : 
    5065             :         char const *pszTFGreen =
    5066             :             (pDS != nullptr)
    5067        4965 :                 ? pDS->GetMetadataItem("TIFFTAG_TRANSFERFUNCTION_GREEN",
    5068             :                                        "COLOR_PROFILE")
    5069        4964 :                 : CSLFetchNameValue(papszParamList,
    5070        4965 :                                     "TIFFTAG_TRANSFERFUNCTION_GREEN");
    5071             : 
    5072             :         char const *pszTFBlue =
    5073             :             (pDS != nullptr)
    5074        4965 :                 ? pDS->GetMetadataItem("TIFFTAG_TRANSFERFUNCTION_BLUE",
    5075             :                                        "COLOR_PROFILE")
    5076        4964 :                 : CSLFetchNameValue(papszParamList,
    5077        4965 :                                     "TIFFTAG_TRANSFERFUNCTION_BLUE");
    5078             : 
    5079        4965 :         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        4965 :         bool bOutputTransferRange = true;
    5130        4965 :         for (int i = 0; (i < 2) && bOutputTransferRange; ++i)
    5131             :         {
    5132             :             const char *pszTXRVal =
    5133             :                 (pDS != nullptr)
    5134        4965 :                     ? pDS->GetMetadataItem(pszTXRNames[i], "COLOR_PROFILE")
    5135        4964 :                     : CSLFetchNameValue(papszParamList, pszTXRNames[i]);
    5136        4965 :             if (pszTXRVal == nullptr)
    5137             :             {
    5138        4965 :                 bOutputTransferRange = false;
    5139        4965 :                 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        4965 :         if (bOutputTransferRange)
    5160             :         {
    5161           0 :             const int TIFFTAG_TRANSFERRANGE = 0x0156;
    5162           0 :             TIFFSetField(l_hTIFF, TIFFTAG_TRANSFERRANGE, pTXR);
    5163             :         }
    5164             :     }
    5165             : }
    5166             : 
    5167       17851 : static signed char GTiffGetLZMAPreset(CSLConstList papszOptions)
    5168             : {
    5169       17851 :     int nLZMAPreset = -1;
    5170       17851 :     const char *pszValue = CSLFetchNameValue(papszOptions, "LZMA_PRESET");
    5171       17851 :     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       17851 :     return static_cast<signed char>(nLZMAPreset);
    5183             : }
    5184             : 
    5185       17851 : static signed char GTiffGetZSTDPreset(CSLConstList papszOptions)
    5186             : {
    5187       17851 :     int nZSTDLevel = -1;
    5188       17851 :     const char *pszValue = CSLFetchNameValue(papszOptions, "ZSTD_LEVEL");
    5189       17851 :     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       17851 :     return static_cast<signed char>(nZSTDLevel);
    5200             : }
    5201             : 
    5202       17851 : static signed char GTiffGetZLevel(CSLConstList papszOptions)
    5203             : {
    5204       17851 :     int nZLevel = -1;
    5205       17851 :     const char *pszValue = CSLFetchNameValue(papszOptions, "ZLEVEL");
    5206       17851 :     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       17851 :     return static_cast<signed char>(nZLevel);
    5232             : }
    5233             : 
    5234       17851 : static signed char GTiffGetJpegQuality(CSLConstList papszOptions)
    5235             : {
    5236       17851 :     int nJpegQuality = -1;
    5237       17851 :     const char *pszValue = CSLFetchNameValue(papszOptions, "JPEG_QUALITY");
    5238       17851 :     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       17851 :     return static_cast<signed char>(nJpegQuality);
    5250             : }
    5251             : 
    5252       17851 : static signed char GTiffGetJpegTablesMode(CSLConstList papszOptions)
    5253             : {
    5254       17851 :     return static_cast<signed char>(atoi(
    5255             :         CSLFetchNameValueDef(papszOptions, "JPEGTABLESMODE",
    5256       17851 :                              CPLSPrintf("%d", knGTIFFJpegTablesModeDefault))));
    5257             : }
    5258             : 
    5259             : /************************************************************************/
    5260             : /*                        GetDiscardLsbOption()                         */
    5261             : /************************************************************************/
    5262             : 
    5263        7914 : static GTiffDataset::MaskOffset *GetDiscardLsbOption(TIFF *hTIFF,
    5264             :                                                      CSLConstList papszOptions)
    5265             : {
    5266        7914 :     const char *pszBits = CSLFetchNameValue(papszOptions, "DISCARD_LSB");
    5267        7914 :     if (pszBits == nullptr)
    5268        7792 :         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        7914 : void GTiffDataset::GetDiscardLsbOption(CSLConstList papszOptions)
    5345             : {
    5346        7914 :     m_panMaskOffsetLsb = ::GetDiscardLsbOption(m_hTIFF, papszOptions);
    5347        7914 : }
    5348             : 
    5349             : /************************************************************************/
    5350             : /*                             GetProfile()                             */
    5351             : /************************************************************************/
    5352             : 
    5353       17902 : static GTiffProfile GetProfile(const char *pszProfile)
    5354             : {
    5355       17902 :     GTiffProfile eProfile = GTiffProfile::GDALGEOTIFF;
    5356       17902 :     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       17902 :     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        9957 : 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        9957 :     bTileInterleavingOut = false;
    5389             : 
    5390        9957 :     GTiffOneTimeInit();
    5391             : 
    5392             :     /* -------------------------------------------------------------------- */
    5393             :     /*      Blow on a few errors.                                           */
    5394             :     /* -------------------------------------------------------------------- */
    5395        9957 :     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        9955 :     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        9954 :         GetProfile(CSLFetchNameValue(papszParamList, "PROFILE"));
    5421             : 
    5422        9954 :     const bool bTiled = CPLFetchBool(papszParamList, "TILED", false);
    5423             : 
    5424        9954 :     int l_nBlockXSize = 0;
    5425        9954 :     if (const char *pszValue = CSLFetchNameValue(papszParamList, "BLOCKXSIZE"))
    5426             :     {
    5427         473 :         l_nBlockXSize = atoi(pszValue);
    5428         473 :         if (l_nBlockXSize < 0)
    5429             :         {
    5430           0 :             ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
    5431             :                         "Invalid value for BLOCKXSIZE");
    5432           0 :             return nullptr;
    5433             :         }
    5434         473 :         if (!bTiled)
    5435             :         {
    5436           9 :             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        9953 :     int l_nBlockYSize = 0;
    5448        9953 :     if (const char *pszValue = CSLFetchNameValue(papszParamList, "BLOCKYSIZE"))
    5449             :     {
    5450        2584 :         l_nBlockYSize = atoi(pszValue);
    5451        2584 :         if (l_nBlockYSize < 0)
    5452             :         {
    5453           0 :             ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
    5454             :                         "Invalid value for BLOCKYSIZE");
    5455           0 :             return nullptr;
    5456             :         }
    5457        2584 :         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        9951 :     if (bTiled)
    5466             :     {
    5467         815 :         if (l_nBlockXSize == 0)
    5468         353 :             l_nBlockXSize = 256;
    5469             : 
    5470         815 :         if (l_nBlockYSize == 0)
    5471         353 :             l_nBlockYSize = 256;
    5472             :     }
    5473             : 
    5474        9951 :     int nPlanar = 0;
    5475             : 
    5476             :     // Hidden @TILE_INTERLEAVE=YES parameter used by the COG driver
    5477        9951 :     if (bCreateCopy && CPLTestBool(CSLFetchNameValueDef(
    5478             :                            papszParamList, "@TILE_INTERLEAVE", "NO")))
    5479             :     {
    5480           7 :         bTileInterleavingOut = true;
    5481           7 :         nPlanar = PLANARCONFIG_SEPARATE;
    5482             :     }
    5483             :     else
    5484             :     {
    5485        9944 :         if (const char *pszValue =
    5486        9944 :                 CSLFetchNameValue(papszParamList, "INTERLEAVE"))
    5487             :         {
    5488        1581 :             if (EQUAL(pszValue, "PIXEL"))
    5489             :             {
    5490         409 :                 nPlanar = PLANARCONFIG_CONTIG;
    5491             :             }
    5492        1172 :             else if (EQUAL(pszValue, "BAND"))
    5493             :             {
    5494        1171 :                 nPlanar = PLANARCONFIG_SEPARATE;
    5495             :             }
    5496             :             else
    5497             :             {
    5498           1 :                 ReportError(
    5499             :                     pszFilename, CE_Failure, CPLE_IllegalArg,
    5500             :                     "INTERLEAVE=%s unsupported, value must be PIXEL or BAND.",
    5501             :                     pszValue);
    5502           1 :                 return nullptr;
    5503             :             }
    5504             :         }
    5505             :         else
    5506             :         {
    5507        8363 :             nPlanar = PLANARCONFIG_CONTIG;
    5508             :         }
    5509             :     }
    5510             : 
    5511        9950 :     int l_nCompression = COMPRESSION_NONE;
    5512        9950 :     if (const char *pszValue = CSLFetchNameValue(papszParamList, "COMPRESS"))
    5513             :     {
    5514        3354 :         l_nCompression = GTIFFGetCompressionMethod(pszValue, "COMPRESS");
    5515        3354 :         if (l_nCompression < 0)
    5516           0 :             return nullptr;
    5517             :     }
    5518             : 
    5519        9950 :     constexpr int JPEG_MAX_DIMENSION = 65500;  // Defined in jpeglib.h
    5520        9950 :     constexpr int WEBP_MAX_DIMENSION = 16383;
    5521             : 
    5522             :     const struct
    5523             :     {
    5524             :         int nCodecID;
    5525             :         const char *pszCodecName;
    5526             :         int nMaxDim;
    5527        9950 :     } asLimitations[] = {
    5528             :         {COMPRESSION_JPEG, "JPEG", JPEG_MAX_DIMENSION},
    5529             :         {COMPRESSION_WEBP, "WEBP", WEBP_MAX_DIMENSION},
    5530             :     };
    5531             : 
    5532       29838 :     for (const auto &sLimitation : asLimitations)
    5533             :     {
    5534       19896 :         if (l_nCompression == sLimitation.nCodecID && !bTiled &&
    5535        2074 :             nXSize > sLimitation.nMaxDim)
    5536             :         {
    5537           2 :             ReportError(
    5538             :                 pszFilename, CE_Failure, CPLE_IllegalArg,
    5539             :                 "COMPRESS=%s is only compatible of un-tiled images whose "
    5540             :                 "width is lesser or equal to %d pixels. "
    5541             :                 "To overcome this limitation, set the TILED=YES creation "
    5542             :                 "option.",
    5543           2 :                 sLimitation.pszCodecName, sLimitation.nMaxDim);
    5544           2 :             return nullptr;
    5545             :         }
    5546       19894 :         else if (l_nCompression == sLimitation.nCodecID && bTiled &&
    5547          52 :                  l_nBlockXSize > sLimitation.nMaxDim)
    5548             :         {
    5549           2 :             ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
    5550             :                         "COMPRESS=%s is only compatible of tiled images whose "
    5551             :                         "BLOCKXSIZE is lesser or equal to %d pixels.",
    5552           2 :                         sLimitation.pszCodecName, sLimitation.nMaxDim);
    5553           2 :             return nullptr;
    5554             :         }
    5555       19892 :         else if (l_nCompression == sLimitation.nCodecID &&
    5556        2122 :                  l_nBlockYSize > sLimitation.nMaxDim)
    5557             :         {
    5558           4 :             ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
    5559             :                         "COMPRESS=%s is only compatible of images whose "
    5560             :                         "BLOCKYSIZE is lesser or equal to %d pixels. "
    5561             :                         "To overcome this limitation, set the TILED=YES "
    5562             :                         "creation option",
    5563           4 :                         sLimitation.pszCodecName, sLimitation.nMaxDim);
    5564           4 :             return nullptr;
    5565             :         }
    5566             :     }
    5567             : 
    5568             :     /* -------------------------------------------------------------------- */
    5569             :     /*      How many bits per sample?  We have a special case if NBITS      */
    5570             :     /*      specified for GDT_UInt8, GDT_UInt16, GDT_UInt32.                 */
    5571             :     /* -------------------------------------------------------------------- */
    5572        9942 :     int l_nBitsPerSample = GDALGetDataTypeSizeBits(eType);
    5573        9942 :     if (CSLFetchNameValue(papszParamList, "NBITS") != nullptr)
    5574             :     {
    5575        1758 :         int nMinBits = 0;
    5576        1758 :         int nMaxBits = 0;
    5577        1758 :         l_nBitsPerSample = atoi(CSLFetchNameValue(papszParamList, "NBITS"));
    5578        1758 :         if (eType == GDT_UInt8)
    5579             :         {
    5580         527 :             nMinBits = 1;
    5581         527 :             nMaxBits = 8;
    5582             :         }
    5583        1231 :         else if (eType == GDT_UInt16)
    5584             :         {
    5585        1213 :             nMinBits = 9;
    5586        1213 :             nMaxBits = 16;
    5587             :         }
    5588          18 :         else if (eType == GDT_UInt32)
    5589             :         {
    5590          14 :             nMinBits = 17;
    5591          14 :             nMaxBits = 32;
    5592             :         }
    5593           4 :         else if (eType == GDT_Float32)
    5594             :         {
    5595           4 :             if (l_nBitsPerSample != 16 && l_nBitsPerSample != 32)
    5596             :             {
    5597           1 :                 ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
    5598             :                             "Only NBITS=16 is supported for data type Float32");
    5599           1 :                 l_nBitsPerSample = GDALGetDataTypeSizeBits(eType);
    5600             :             }
    5601             :         }
    5602             :         else
    5603             :         {
    5604           0 :             ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
    5605             :                         "NBITS is not supported for data type %s",
    5606             :                         GDALGetDataTypeName(eType));
    5607           0 :             l_nBitsPerSample = GDALGetDataTypeSizeBits(eType);
    5608             :         }
    5609             : 
    5610        1758 :         if (nMinBits != 0)
    5611             :         {
    5612        1754 :             if (l_nBitsPerSample < nMinBits)
    5613             :             {
    5614           2 :                 ReportError(
    5615             :                     pszFilename, CE_Warning, CPLE_AppDefined,
    5616             :                     "NBITS=%d is invalid for data type %s. Using NBITS=%d",
    5617             :                     l_nBitsPerSample, GDALGetDataTypeName(eType), nMinBits);
    5618           2 :                 l_nBitsPerSample = nMinBits;
    5619             :             }
    5620        1752 :             else if (l_nBitsPerSample > nMaxBits)
    5621             :             {
    5622           3 :                 ReportError(
    5623             :                     pszFilename, CE_Warning, CPLE_AppDefined,
    5624             :                     "NBITS=%d is invalid for data type %s. Using NBITS=%d",
    5625             :                     l_nBitsPerSample, GDALGetDataTypeName(eType), nMaxBits);
    5626           3 :                 l_nBitsPerSample = nMaxBits;
    5627             :             }
    5628             :         }
    5629             :     }
    5630             : 
    5631             : #ifdef HAVE_JXL
    5632        9942 :     if ((l_nCompression == COMPRESSION_JXL ||
    5633         106 :          l_nCompression == COMPRESSION_JXL_DNG_1_7) &&
    5634         105 :         eType != GDT_Float16 && eType != GDT_Float32)
    5635             :     {
    5636             :         // Reflects tif_jxl's GetJXLDataType()
    5637          85 :         if (eType != GDT_UInt8 && eType != GDT_UInt16)
    5638             :         {
    5639           1 :             ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5640             :                         "Data type %s not supported for JXL compression. Only "
    5641             :                         "Byte, UInt16, Float16, Float32 are supported",
    5642             :                         GDALGetDataTypeName(eType));
    5643           2 :             return nullptr;
    5644             :         }
    5645             : 
    5646             :         const struct
    5647             :         {
    5648             :             GDALDataType eDT;
    5649             :             int nBitsPerSample;
    5650          84 :         } asSupportedDTBitsPerSample[] = {
    5651             :             {GDT_UInt8, 8},
    5652             :             {GDT_UInt16, 16},
    5653             :         };
    5654             : 
    5655         250 :         for (const auto &sSupportedDTBitsPerSample : asSupportedDTBitsPerSample)
    5656             :         {
    5657         167 :             if (eType == sSupportedDTBitsPerSample.eDT &&
    5658          84 :                 l_nBitsPerSample != sSupportedDTBitsPerSample.nBitsPerSample)
    5659             :             {
    5660           1 :                 ReportError(
    5661             :                     pszFilename, CE_Failure, CPLE_NotSupported,
    5662             :                     "Bits per sample=%d not supported for JXL compression. "
    5663             :                     "Only %d is supported for %s data type.",
    5664           1 :                     l_nBitsPerSample, sSupportedDTBitsPerSample.nBitsPerSample,
    5665             :                     GDALGetDataTypeName(eType));
    5666           1 :                 return nullptr;
    5667             :             }
    5668             :         }
    5669             :     }
    5670             : #endif
    5671             : 
    5672        9940 :     int nPredictor = PREDICTOR_NONE;
    5673        9940 :     const char *pszPredictor = CSLFetchNameValue(papszParamList, "PREDICTOR");
    5674        9940 :     if (pszPredictor)
    5675             :     {
    5676          31 :         nPredictor = atoi(pszPredictor);
    5677             :     }
    5678             : 
    5679        9940 :     if (nPredictor != PREDICTOR_NONE &&
    5680          18 :         l_nCompression != COMPRESSION_ADOBE_DEFLATE &&
    5681           2 :         l_nCompression != COMPRESSION_LZW &&
    5682           2 :         l_nCompression != COMPRESSION_LZMA &&
    5683             :         l_nCompression != COMPRESSION_ZSTD)
    5684             :     {
    5685           1 :         ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
    5686             :                     "PREDICTOR option is ignored for COMPRESS=%s. "
    5687             :                     "Only valid for DEFLATE, LZW, LZMA or ZSTD",
    5688             :                     CSLFetchNameValueDef(papszParamList, "COMPRESS", "NONE"));
    5689             :     }
    5690             : 
    5691             :     // Do early checks as libtiff will only error out when starting to write.
    5692        9969 :     else if (nPredictor != PREDICTOR_NONE &&
    5693          30 :              CPLTestBool(
    5694             :                  CPLGetConfigOption("GDAL_GTIFF_PREDICTOR_CHECKS", "YES")))
    5695             :     {
    5696             : #if (TIFFLIB_VERSION > 20210416) || defined(INTERNAL_LIBTIFF)
    5697             : #define HAVE_PREDICTOR_2_FOR_64BIT
    5698             : #endif
    5699          30 :         if (nPredictor == 2)
    5700             :         {
    5701          24 :             if (l_nBitsPerSample != 8 && l_nBitsPerSample != 16 &&
    5702             :                 l_nBitsPerSample != 32
    5703             : #ifdef HAVE_PREDICTOR_2_FOR_64BIT
    5704           2 :                 && l_nBitsPerSample != 64
    5705             : #endif
    5706             :             )
    5707             :             {
    5708             : #if !defined(HAVE_PREDICTOR_2_FOR_64BIT)
    5709             :                 if (l_nBitsPerSample == 64)
    5710             :                 {
    5711             :                     ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    5712             :                                 "PREDICTOR=2 is supported on 64 bit samples "
    5713             :                                 "starting with libtiff > 4.3.0.");
    5714             :                 }
    5715             :                 else
    5716             : #endif
    5717             :                 {
    5718           2 :                     const int nBITSHint = (l_nBitsPerSample < 8)    ? 8
    5719           1 :                                           : (l_nBitsPerSample < 16) ? 16
    5720           0 :                                           : (l_nBitsPerSample < 32) ? 32
    5721             :                                                                     : 64;
    5722           1 :                     ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    5723             : #ifdef HAVE_PREDICTOR_2_FOR_64BIT
    5724             :                                 "PREDICTOR=2 is only supported with 8/16/32/64 "
    5725             :                                 "bit samples. You can specify the NBITS=%d "
    5726             :                                 "creation option to promote to the closest "
    5727             :                                 "supported bits per sample value.",
    5728             : #else
    5729             :                                 "PREDICTOR=2 is only supported with 8/16/32 "
    5730             :                                 "bit samples. You can specify the NBITS=%d "
    5731             :                                 "creation option to promote to the closest "
    5732             :                                 "supported bits per sample value.",
    5733             : #endif
    5734             :                                 nBITSHint);
    5735             :                 }
    5736           1 :                 return nullptr;
    5737             :             }
    5738             :         }
    5739           6 :         else if (nPredictor == 3)
    5740             :         {
    5741           5 :             if (eType != GDT_Float16 && eType != GDT_Float32 &&
    5742             :                 eType != GDT_Float64)
    5743             :             {
    5744           1 :                 ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    5745             :                             "PREDICTOR=3 is only supported with Float16, "
    5746             :                             "Float32 or Float64.");
    5747           1 :                 return nullptr;
    5748             :             }
    5749             :         }
    5750             :         else
    5751             :         {
    5752           1 :             ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    5753             :                         "PREDICTOR=%s is not supported.", pszPredictor);
    5754           1 :             return nullptr;
    5755             :         }
    5756             :     }
    5757             : 
    5758        9937 :     const int l_nZLevel = GTiffGetZLevel(papszParamList);
    5759        9937 :     const int l_nLZMAPreset = GTiffGetLZMAPreset(papszParamList);
    5760        9937 :     const int l_nZSTDLevel = GTiffGetZSTDPreset(papszParamList);
    5761        9937 :     const int l_nWebPLevel = GTiffGetWebPLevel(papszParamList);
    5762        9937 :     const bool l_bWebPLossless = GTiffGetWebPLossless(papszParamList);
    5763        9937 :     const int l_nJpegQuality = GTiffGetJpegQuality(papszParamList);
    5764        9937 :     const int l_nJpegTablesMode = GTiffGetJpegTablesMode(papszParamList);
    5765        9937 :     const double l_dfMaxZError = GTiffGetLERCMaxZError(papszParamList);
    5766             : #if HAVE_JXL
    5767        9937 :     bool bJXLLosslessSpecified = false;
    5768             :     const bool l_bJXLLossless =
    5769        9937 :         GTiffGetJXLLossless(papszParamList, &bJXLLosslessSpecified);
    5770        9937 :     const uint32_t l_nJXLEffort = GTiffGetJXLEffort(papszParamList);
    5771        9937 :     bool bJXLDistanceSpecified = false;
    5772             :     const float l_fJXLDistance =
    5773        9937 :         GTiffGetJXLDistance(papszParamList, &bJXLDistanceSpecified);
    5774        9937 :     if (bJXLDistanceSpecified && l_bJXLLossless)
    5775             :     {
    5776           1 :         ReportError(pszFilename, CE_Warning, CPLE_AppDefined,
    5777             :                     "JXL_DISTANCE creation option is ignored, given %s "
    5778             :                     "JXL_LOSSLESS=YES",
    5779             :                     bJXLLosslessSpecified ? "(explicit)" : "(implicit)");
    5780             :     }
    5781        9937 :     bool bJXLAlphaDistanceSpecified = false;
    5782             :     const float l_fJXLAlphaDistance =
    5783        9937 :         GTiffGetJXLAlphaDistance(papszParamList, &bJXLAlphaDistanceSpecified);
    5784        9937 :     if (bJXLAlphaDistanceSpecified && l_bJXLLossless)
    5785             :     {
    5786           1 :         ReportError(pszFilename, CE_Warning, CPLE_AppDefined,
    5787             :                     "JXL_ALPHA_DISTANCE creation option is ignored, given %s "
    5788             :                     "JXL_LOSSLESS=YES",
    5789             :                     bJXLLosslessSpecified ? "(explicit)" : "(implicit)");
    5790             :     }
    5791             : #endif
    5792             :     /* -------------------------------------------------------------------- */
    5793             :     /*      Streaming related code                                          */
    5794             :     /* -------------------------------------------------------------------- */
    5795       19874 :     const CPLString osOriFilename(pszFilename);
    5796       19874 :     bool bStreaming = strcmp(pszFilename, "/vsistdout/") == 0 ||
    5797        9937 :                       CPLFetchBool(papszParamList, "STREAMABLE_OUTPUT", false);
    5798             : #ifdef S_ISFIFO
    5799        9937 :     if (!bStreaming)
    5800             :     {
    5801             :         VSIStatBufL sStat;
    5802        9925 :         if (VSIStatExL(pszFilename, &sStat,
    5803       10804 :                        VSI_STAT_EXISTS_FLAG | VSI_STAT_NATURE_FLAG) == 0 &&
    5804         879 :             S_ISFIFO(sStat.st_mode))
    5805             :         {
    5806           0 :             bStreaming = true;
    5807             :         }
    5808             :     }
    5809             : #endif
    5810        9937 :     if (bStreaming && !EQUAL("NONE", CSLFetchNameValueDef(papszParamList,
    5811             :                                                           "COMPRESS", "NONE")))
    5812             :     {
    5813           1 :         ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5814             :                     "Streaming only supported to uncompressed TIFF");
    5815           1 :         return nullptr;
    5816             :     }
    5817        9936 :     if (bStreaming && CPLFetchBool(papszParamList, "SPARSE_OK", false))
    5818             :     {
    5819           1 :         ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5820             :                     "Streaming not supported with SPARSE_OK");
    5821           1 :         return nullptr;
    5822             :     }
    5823             :     const bool bCopySrcOverviews =
    5824        9935 :         CPLFetchBool(papszParamList, "COPY_SRC_OVERVIEWS", false);
    5825        9935 :     if (bStreaming && bCopySrcOverviews)
    5826             :     {
    5827           1 :         ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5828             :                     "Streaming not supported with COPY_SRC_OVERVIEWS");
    5829           1 :         return nullptr;
    5830             :     }
    5831        9934 :     if (bStreaming)
    5832             :     {
    5833           9 :         l_osTmpFilename = VSIMemGenerateHiddenFilename("vsistdout.tif");
    5834           9 :         pszFilename = l_osTmpFilename.c_str();
    5835             :     }
    5836             : 
    5837             :     /* -------------------------------------------------------------------- */
    5838             :     /*      Compute the uncompressed size.                                  */
    5839             :     /* -------------------------------------------------------------------- */
    5840        9934 :     const unsigned nTileXCount =
    5841        9934 :         bTiled ? DIV_ROUND_UP(nXSize, l_nBlockXSize) : 0;
    5842        9934 :     const unsigned nTileYCount =
    5843        9934 :         bTiled ? DIV_ROUND_UP(nYSize, l_nBlockYSize) : 0;
    5844             :     const double dfUncompressedImageSize =
    5845        9934 :         (bTiled ? (static_cast<double>(nTileXCount) * nTileYCount *
    5846         811 :                    l_nBlockXSize * l_nBlockYSize)
    5847        9123 :                 : (nXSize * static_cast<double>(nYSize))) *
    5848        9934 :             l_nBands * GDALGetDataTypeSizeBytes(eType) +
    5849        9934 :         dfExtraSpaceForOverviews;
    5850             : 
    5851             :     /* -------------------------------------------------------------------- */
    5852             :     /*      Should the file be created as a bigtiff file?                   */
    5853             :     /* -------------------------------------------------------------------- */
    5854        9934 :     const char *pszBIGTIFF = CSLFetchNameValue(papszParamList, "BIGTIFF");
    5855             : 
    5856        9934 :     if (pszBIGTIFF == nullptr)
    5857        9487 :         pszBIGTIFF = "IF_NEEDED";
    5858             : 
    5859        9934 :     bool bCreateBigTIFF = false;
    5860        9934 :     if (EQUAL(pszBIGTIFF, "IF_NEEDED"))
    5861             :     {
    5862        9488 :         if (l_nCompression == COMPRESSION_NONE &&
    5863             :             dfUncompressedImageSize > 4200000000.0)
    5864          17 :             bCreateBigTIFF = true;
    5865             :     }
    5866         446 :     else if (EQUAL(pszBIGTIFF, "IF_SAFER"))
    5867             :     {
    5868         425 :         if (dfUncompressedImageSize > 2000000000.0)
    5869           1 :             bCreateBigTIFF = true;
    5870             :     }
    5871             :     else
    5872             :     {
    5873          21 :         bCreateBigTIFF = CPLTestBool(pszBIGTIFF);
    5874          21 :         if (!bCreateBigTIFF && l_nCompression == COMPRESSION_NONE &&
    5875             :             dfUncompressedImageSize > 4200000000.0)
    5876             :         {
    5877           2 :             ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5878             :                         "The TIFF file will be larger than 4GB, so BigTIFF is "
    5879             :                         "necessary.  Creation failed.");
    5880           2 :             return nullptr;
    5881             :         }
    5882             :     }
    5883             : 
    5884        9932 :     if (bCreateBigTIFF)
    5885          35 :         CPLDebug("GTiff", "File being created as a BigTIFF.");
    5886             : 
    5887             :     /* -------------------------------------------------------------------- */
    5888             :     /*      Sanity check.                                                   */
    5889             :     /* -------------------------------------------------------------------- */
    5890        9932 :     if (bTiled)
    5891             :     {
    5892             :         // libtiff implementation limitation
    5893         811 :         if (nTileXCount > 0x80000000U / (bCreateBigTIFF ? 8 : 4) / nTileYCount)
    5894             :         {
    5895           3 :             ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5896             :                         "File too large regarding tile size. This would result "
    5897             :                         "in a file with tile arrays larger than 2GB");
    5898           3 :             return nullptr;
    5899             :         }
    5900             :     }
    5901             : 
    5902             :     /* -------------------------------------------------------------------- */
    5903             :     /*      Check free space (only for big, non sparse)                     */
    5904             :     /* -------------------------------------------------------------------- */
    5905        9929 :     const double dfLikelyFloorOfFinalSize =
    5906             :         l_nCompression == COMPRESSION_NONE
    5907        9929 :             ? dfUncompressedImageSize
    5908             :             :
    5909             :             /* For compressed, we target 1% as the most optimistic reduction factor! */
    5910             :             0.01 * dfUncompressedImageSize;
    5911        9951 :     if (dfLikelyFloorOfFinalSize >= 1e9 &&
    5912          22 :         !CPLFetchBool(papszParamList, "SPARSE_OK", false) &&
    5913           5 :         osOriFilename != "/vsistdout/" &&
    5914        9956 :         osOriFilename != "/vsistdout_redirect/" &&
    5915           5 :         CPLTestBool(CPLGetConfigOption("CHECK_DISK_FREE_SPACE", "TRUE")))
    5916             :     {
    5917             :         const GIntBig nFreeDiskSpace =
    5918           4 :             VSIGetDiskFreeSpace(CPLGetDirnameSafe(pszFilename).c_str());
    5919           4 :         if (nFreeDiskSpace >= 0 && nFreeDiskSpace < dfLikelyFloorOfFinalSize)
    5920             :         {
    5921           6 :             ReportError(
    5922             :                 pszFilename, CE_Failure, CPLE_FileIO,
    5923             :                 "Free disk space available is %s, "
    5924             :                 "whereas %s are %s necessary. "
    5925             :                 "You can disable this check by defining the "
    5926             :                 "CHECK_DISK_FREE_SPACE configuration option to FALSE.",
    5927           4 :                 CPLFormatReadableFileSize(static_cast<uint64_t>(nFreeDiskSpace))
    5928             :                     .c_str(),
    5929           4 :                 CPLFormatReadableFileSize(dfLikelyFloorOfFinalSize).c_str(),
    5930             :                 l_nCompression == COMPRESSION_NONE
    5931             :                     ? "at least"
    5932             :                     : "likely at least (probably more)");
    5933           2 :             return nullptr;
    5934             :         }
    5935             :     }
    5936             : 
    5937             :     /* -------------------------------------------------------------------- */
    5938             :     /*      Check if the user wishes a particular endianness                */
    5939             :     /* -------------------------------------------------------------------- */
    5940             : 
    5941        9927 :     int eEndianness = ENDIANNESS_NATIVE;
    5942        9927 :     const char *pszEndianness = CSLFetchNameValue(papszParamList, "ENDIANNESS");
    5943        9927 :     if (pszEndianness == nullptr)
    5944        9864 :         pszEndianness = CPLGetConfigOption("GDAL_TIFF_ENDIANNESS", nullptr);
    5945        9927 :     if (pszEndianness != nullptr)
    5946             :     {
    5947         123 :         if (EQUAL(pszEndianness, "LITTLE"))
    5948             :         {
    5949          36 :             eEndianness = ENDIANNESS_LITTLE;
    5950             :         }
    5951          87 :         else if (EQUAL(pszEndianness, "BIG"))
    5952             :         {
    5953           1 :             eEndianness = ENDIANNESS_BIG;
    5954             :         }
    5955          86 :         else if (EQUAL(pszEndianness, "INVERTED"))
    5956             :         {
    5957             : #ifdef CPL_LSB
    5958          82 :             eEndianness = ENDIANNESS_BIG;
    5959             : #else
    5960             :             eEndianness = ENDIANNESS_LITTLE;
    5961             : #endif
    5962             :         }
    5963           4 :         else if (!EQUAL(pszEndianness, "NATIVE"))
    5964             :         {
    5965           1 :             ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
    5966             :                         "ENDIANNESS=%s not supported. Defaulting to NATIVE",
    5967             :                         pszEndianness);
    5968             :         }
    5969             :     }
    5970             : 
    5971             :     /* -------------------------------------------------------------------- */
    5972             :     /*      Try opening the dataset.                                        */
    5973             :     /* -------------------------------------------------------------------- */
    5974             : 
    5975             :     const bool bAppend =
    5976        9927 :         CPLFetchBool(papszParamList, "APPEND_SUBDATASET", false);
    5977             : 
    5978        9927 :     char szOpeningFlag[5] = {};
    5979        9927 :     strcpy(szOpeningFlag, bAppend ? "r+" : "w+");
    5980        9927 :     if (bCreateBigTIFF)
    5981          32 :         strcat(szOpeningFlag, "8");
    5982        9927 :     if (eEndianness == ENDIANNESS_BIG)
    5983          83 :         strcat(szOpeningFlag, "b");
    5984        9844 :     else if (eEndianness == ENDIANNESS_LITTLE)
    5985          36 :         strcat(szOpeningFlag, "l");
    5986             : 
    5987        9927 :     VSIErrorReset();
    5988        9927 :     const bool bOnlyVisibleAtCloseTime = CPLTestBool(CSLFetchNameValueDef(
    5989             :         papszParamList, "@CREATE_ONLY_VISIBLE_AT_CLOSE_TIME", "NO"));
    5990        9927 :     const bool bSuppressASAP = CPLTestBool(
    5991             :         CSLFetchNameValueDef(papszParamList, "@SUPPRESS_ASAP", "NO"));
    5992             :     auto l_fpL =
    5993        9927 :         (bOnlyVisibleAtCloseTime || bSuppressASAP) && !bAppend
    5994        9997 :             ? VSIFileManager::GetHandler(pszFilename)
    5995         140 :                   ->CreateOnlyVisibleAtCloseTime(pszFilename, true, nullptr)
    5996          70 :                   .release()
    5997       19784 :             : VSIFilesystemHandler::OpenStatic(pszFilename,
    5998             :                                                bAppend ? "r+b" : "w+b", true)
    5999        9927 :                   .release();
    6000        9927 :     if (l_fpL == nullptr)
    6001             :     {
    6002          21 :         VSIToCPLErrorWithMsg(CE_Failure, CPLE_OpenFailed,
    6003          42 :                              std::string("Attempt to create new tiff file `")
    6004          21 :                                  .append(pszFilename)
    6005          21 :                                  .append("' failed")
    6006             :                                  .c_str());
    6007          21 :         return nullptr;
    6008             :     }
    6009             : 
    6010        9906 :     if (bSuppressASAP)
    6011             :     {
    6012          40 :         l_fpL->CancelCreation();
    6013             :     }
    6014             : 
    6015        9906 :     TIFF *l_hTIFF = VSI_TIFFOpen(pszFilename, szOpeningFlag, l_fpL);
    6016        9906 :     if (l_hTIFF == nullptr)
    6017             :     {
    6018           2 :         if (CPLGetLastErrorNo() == 0)
    6019           0 :             CPLError(CE_Failure, CPLE_OpenFailed,
    6020             :                      "Attempt to create new tiff file `%s' "
    6021             :                      "failed in XTIFFOpen().",
    6022             :                      pszFilename);
    6023           2 :         l_fpL->CancelCreation();
    6024           2 :         CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    6025           2 :         return nullptr;
    6026             :     }
    6027             : 
    6028        9904 :     if (bAppend)
    6029             :     {
    6030             : #if !(defined(INTERNAL_LIBTIFF) || TIFFLIB_VERSION > 20240911)
    6031             :         // This is a bit of a hack to cause (*tif->tif_cleanup)(tif); to be
    6032             :         // called. See https://trac.osgeo.org/gdal/ticket/2055
    6033             :         // Fixed in libtiff > 4.7.0
    6034             :         TIFFSetField(l_hTIFF, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
    6035             :         TIFFFreeDirectory(l_hTIFF);
    6036             : #endif
    6037           6 :         TIFFCreateDirectory(l_hTIFF);
    6038             :     }
    6039             : 
    6040             :     /* -------------------------------------------------------------------- */
    6041             :     /*      Do we have a custom pixel type (just used for signed byte now). */
    6042             :     /* -------------------------------------------------------------------- */
    6043        9904 :     const char *pszPixelType = CSLFetchNameValue(papszParamList, "PIXELTYPE");
    6044        9904 :     if (pszPixelType == nullptr)
    6045        9896 :         pszPixelType = "";
    6046        9904 :     if (eType == GDT_UInt8 && EQUAL(pszPixelType, "SIGNEDBYTE"))
    6047             :     {
    6048           8 :         CPLError(CE_Warning, CPLE_AppDefined,
    6049             :                  "Using PIXELTYPE=SIGNEDBYTE with Byte data type is deprecated "
    6050             :                  "(but still works). "
    6051             :                  "Using Int8 data type instead is now recommended.");
    6052             :     }
    6053             : 
    6054             :     /* -------------------------------------------------------------------- */
    6055             :     /*      Setup some standard flags.                                      */
    6056             :     /* -------------------------------------------------------------------- */
    6057        9904 :     TIFFSetField(l_hTIFF, TIFFTAG_IMAGEWIDTH, nXSize);
    6058        9904 :     TIFFSetField(l_hTIFF, TIFFTAG_IMAGELENGTH, nYSize);
    6059        9904 :     TIFFSetField(l_hTIFF, TIFFTAG_BITSPERSAMPLE, l_nBitsPerSample);
    6060             : 
    6061        9904 :     uint16_t l_nSampleFormat = 0;
    6062        9904 :     if ((eType == GDT_UInt8 && EQUAL(pszPixelType, "SIGNEDBYTE")) ||
    6063        9755 :         eType == GDT_Int8 || eType == GDT_Int16 || eType == GDT_Int32 ||
    6064             :         eType == GDT_Int64)
    6065         808 :         l_nSampleFormat = SAMPLEFORMAT_INT;
    6066        9096 :     else if (eType == GDT_CInt16 || eType == GDT_CInt32)
    6067         363 :         l_nSampleFormat = SAMPLEFORMAT_COMPLEXINT;
    6068        8733 :     else if (eType == GDT_Float16 || eType == GDT_Float32 ||
    6069             :              eType == GDT_Float64)
    6070        1160 :         l_nSampleFormat = SAMPLEFORMAT_IEEEFP;
    6071        7573 :     else if (eType == GDT_CFloat16 || eType == GDT_CFloat32 ||
    6072             :              eType == GDT_CFloat64)
    6073         471 :         l_nSampleFormat = SAMPLEFORMAT_COMPLEXIEEEFP;
    6074             :     else
    6075        7102 :         l_nSampleFormat = SAMPLEFORMAT_UINT;
    6076             : 
    6077        9904 :     TIFFSetField(l_hTIFF, TIFFTAG_SAMPLEFORMAT, l_nSampleFormat);
    6078        9904 :     TIFFSetField(l_hTIFF, TIFFTAG_SAMPLESPERPIXEL, l_nBands);
    6079        9904 :     TIFFSetField(l_hTIFF, TIFFTAG_PLANARCONFIG, nPlanar);
    6080             : 
    6081             :     /* -------------------------------------------------------------------- */
    6082             :     /*      Setup Photometric Interpretation. Take this value from the user */
    6083             :     /*      passed option or guess correct value otherwise.                 */
    6084             :     /* -------------------------------------------------------------------- */
    6085        9904 :     int nSamplesAccountedFor = 1;
    6086        9904 :     bool bForceColorTable = false;
    6087             : 
    6088        9904 :     if (const char *pszValue = CSLFetchNameValue(papszParamList, "PHOTOMETRIC"))
    6089             :     {
    6090        1913 :         if (EQUAL(pszValue, "MINISBLACK"))
    6091          14 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    6092        1899 :         else if (EQUAL(pszValue, "MINISWHITE"))
    6093             :         {
    6094           2 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
    6095             :         }
    6096        1897 :         else if (EQUAL(pszValue, "PALETTE"))
    6097             :         {
    6098           5 :             if (eType == GDT_UInt8 || eType == GDT_UInt16)
    6099             :             {
    6100           4 :                 TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
    6101           4 :                 nSamplesAccountedFor = 1;
    6102           4 :                 bForceColorTable = true;
    6103             :             }
    6104             :             else
    6105             :             {
    6106           1 :                 ReportError(
    6107             :                     pszFilename, CE_Warning, CPLE_AppDefined,
    6108             :                     "PHOTOMETRIC=PALETTE only compatible with Byte or UInt16");
    6109             :             }
    6110             :         }
    6111        1892 :         else if (EQUAL(pszValue, "RGB"))
    6112             :         {
    6113        1152 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
    6114        1152 :             nSamplesAccountedFor = 3;
    6115             :         }
    6116         740 :         else if (EQUAL(pszValue, "CMYK"))
    6117             :         {
    6118          10 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_SEPARATED);
    6119          10 :             nSamplesAccountedFor = 4;
    6120             :         }
    6121         730 :         else if (EQUAL(pszValue, "YCBCR"))
    6122             :         {
    6123             :             // Because of subsampling, setting YCBCR without JPEG compression
    6124             :             // leads to a crash currently. Would need to make
    6125             :             // GTiffRasterBand::IWriteBlock() aware of subsampling so that it
    6126             :             // doesn't overrun buffer size returned by libtiff.
    6127         729 :             if (l_nCompression != COMPRESSION_JPEG)
    6128             :             {
    6129           1 :                 ReportError(
    6130             :                     pszFilename, CE_Failure, CPLE_NotSupported,
    6131             :                     "Currently, PHOTOMETRIC=YCBCR requires COMPRESS=JPEG");
    6132           1 :                 XTIFFClose(l_hTIFF);
    6133           1 :                 l_fpL->CancelCreation();
    6134           1 :                 CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    6135           1 :                 return nullptr;
    6136             :             }
    6137             : 
    6138         728 :             if (nPlanar == PLANARCONFIG_SEPARATE)
    6139             :             {
    6140           1 :                 ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    6141             :                             "PHOTOMETRIC=YCBCR requires INTERLEAVE=PIXEL");
    6142           1 :                 XTIFFClose(l_hTIFF);
    6143           1 :                 l_fpL->CancelCreation();
    6144           1 :                 CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    6145           1 :                 return nullptr;
    6146             :             }
    6147             : 
    6148             :             // YCBCR strictly requires 3 bands. Not less, not more Issue an
    6149             :             // explicit error message as libtiff one is a bit cryptic:
    6150             :             // TIFFVStripSize64:Invalid td_samplesperpixel value.
    6151         727 :             if (l_nBands != 3)
    6152             :             {
    6153           1 :                 ReportError(
    6154             :                     pszFilename, CE_Failure, CPLE_NotSupported,
    6155             :                     "PHOTOMETRIC=YCBCR not supported on a %d-band raster: "
    6156             :                     "only compatible of a 3-band (RGB) raster",
    6157             :                     l_nBands);
    6158           1 :                 XTIFFClose(l_hTIFF);
    6159           1 :                 l_fpL->CancelCreation();
    6160           1 :                 CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    6161           1 :                 return nullptr;
    6162             :             }
    6163             : 
    6164         726 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);
    6165         726 :             nSamplesAccountedFor = 3;
    6166             : 
    6167             :             // Explicitly register the subsampling so that JPEGFixupTags
    6168             :             // is a no-op (helps for cloud optimized geotiffs)
    6169         726 :             TIFFSetField(l_hTIFF, TIFFTAG_YCBCRSUBSAMPLING, 2, 2);
    6170             :         }
    6171           1 :         else if (EQUAL(pszValue, "CIELAB"))
    6172             :         {
    6173           0 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_CIELAB);
    6174           0 :             nSamplesAccountedFor = 3;
    6175             :         }
    6176           1 :         else if (EQUAL(pszValue, "ICCLAB"))
    6177             :         {
    6178           0 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_ICCLAB);
    6179           0 :             nSamplesAccountedFor = 3;
    6180             :         }
    6181           1 :         else if (EQUAL(pszValue, "ITULAB"))
    6182             :         {
    6183           0 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_ITULAB);
    6184           0 :             nSamplesAccountedFor = 3;
    6185             :         }
    6186             :         else
    6187             :         {
    6188           1 :             ReportError(pszFilename, CE_Warning, CPLE_IllegalArg,
    6189             :                         "PHOTOMETRIC=%s value not recognised, ignoring.  "
    6190             :                         "Set the Photometric Interpretation as MINISBLACK.",
    6191             :                         pszValue);
    6192           1 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    6193             :         }
    6194             : 
    6195        1910 :         if (l_nBands < nSamplesAccountedFor)
    6196             :         {
    6197           1 :             ReportError(pszFilename, CE_Warning, CPLE_IllegalArg,
    6198             :                         "PHOTOMETRIC=%s value does not correspond to number "
    6199             :                         "of bands (%d), ignoring.  "
    6200             :                         "Set the Photometric Interpretation as MINISBLACK.",
    6201             :                         pszValue, l_nBands);
    6202           1 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    6203             :         }
    6204             :     }
    6205             :     else
    6206             :     {
    6207             :         // If image contains 3 or 4 bands and datatype is Byte then we will
    6208             :         // assume it is RGB. In all other cases assume it is MINISBLACK.
    6209        7991 :         if (l_nBands == 3 && eType == GDT_UInt8)
    6210             :         {
    6211         323 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
    6212         323 :             nSamplesAccountedFor = 3;
    6213             :         }
    6214        7668 :         else if (l_nBands == 4 && eType == GDT_UInt8)
    6215             :         {
    6216             :             uint16_t v[1] = {
    6217         723 :                 GTiffGetAlphaValue(CSLFetchNameValue(papszParamList, "ALPHA"),
    6218         723 :                                    DEFAULT_ALPHA_TYPE)};
    6219             : 
    6220         723 :             TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, 1, v);
    6221         723 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
    6222         723 :             nSamplesAccountedFor = 4;
    6223             :         }
    6224             :         else
    6225             :         {
    6226        6945 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    6227        6945 :             nSamplesAccountedFor = 1;
    6228             :         }
    6229             :     }
    6230             : 
    6231             :     /* -------------------------------------------------------------------- */
    6232             :     /*      If there are extra samples, we need to mark them with an        */
    6233             :     /*      appropriate extrasamples definition here.                       */
    6234             :     /* -------------------------------------------------------------------- */
    6235        9901 :     if (l_nBands > nSamplesAccountedFor)
    6236             :     {
    6237        1389 :         const int nExtraSamples = l_nBands - nSamplesAccountedFor;
    6238             : 
    6239             :         uint16_t *v = static_cast<uint16_t *>(
    6240        1389 :             CPLMalloc(sizeof(uint16_t) * nExtraSamples));
    6241             : 
    6242        1389 :         v[0] = GTiffGetAlphaValue(CSLFetchNameValue(papszParamList, "ALPHA"),
    6243             :                                   EXTRASAMPLE_UNSPECIFIED);
    6244             : 
    6245      297700 :         for (int i = 1; i < nExtraSamples; ++i)
    6246      296311 :             v[i] = EXTRASAMPLE_UNSPECIFIED;
    6247             : 
    6248        1389 :         TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, nExtraSamples, v);
    6249             : 
    6250        1389 :         CPLFree(v);
    6251             :     }
    6252             : 
    6253             :     // Set the ICC color profile.
    6254        9901 :     if (eProfile != GTiffProfile::BASELINE)
    6255             :     {
    6256        9876 :         SaveICCProfile(nullptr, l_hTIFF, papszParamList, l_nBitsPerSample);
    6257             :     }
    6258             : 
    6259             :     // Set the compression method before asking the default strip size
    6260             :     // This is useful when translating to a JPEG-In-TIFF file where
    6261             :     // the default strip size is 8 or 16 depending on the photometric value.
    6262        9901 :     TIFFSetField(l_hTIFF, TIFFTAG_COMPRESSION, l_nCompression);
    6263             : 
    6264        9901 :     if (l_nCompression == COMPRESSION_LERC)
    6265             :     {
    6266             :         const char *pszCompress =
    6267          97 :             CSLFetchNameValueDef(papszParamList, "COMPRESS", "");
    6268          97 :         if (EQUAL(pszCompress, "LERC_DEFLATE"))
    6269             :         {
    6270          16 :             TIFFSetField(l_hTIFF, TIFFTAG_LERC_ADD_COMPRESSION,
    6271             :                          LERC_ADD_COMPRESSION_DEFLATE);
    6272             :         }
    6273          81 :         else if (EQUAL(pszCompress, "LERC_ZSTD"))
    6274             :         {
    6275          14 :             if (TIFFSetField(l_hTIFF, TIFFTAG_LERC_ADD_COMPRESSION,
    6276          14 :                              LERC_ADD_COMPRESSION_ZSTD) != 1)
    6277             :             {
    6278           0 :                 XTIFFClose(l_hTIFF);
    6279           0 :                 l_fpL->CancelCreation();
    6280           0 :                 CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    6281           0 :                 return nullptr;
    6282             :             }
    6283             :         }
    6284             :     }
    6285             :     // TODO later: take into account LERC version
    6286             : 
    6287             :     /* -------------------------------------------------------------------- */
    6288             :     /*      Setup tiling/stripping flags.                                   */
    6289             :     /* -------------------------------------------------------------------- */
    6290        9901 :     if (bTiled)
    6291             :     {
    6292        1602 :         if (!TIFFSetField(l_hTIFF, TIFFTAG_TILEWIDTH, l_nBlockXSize) ||
    6293         801 :             !TIFFSetField(l_hTIFF, TIFFTAG_TILELENGTH, l_nBlockYSize))
    6294             :         {
    6295           0 :             XTIFFClose(l_hTIFF);
    6296           0 :             l_fpL->CancelCreation();
    6297           0 :             CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    6298           0 :             return nullptr;
    6299             :         }
    6300             :     }
    6301             :     else
    6302             :     {
    6303        9100 :         const uint32_t l_nRowsPerStrip = std::min(
    6304             :             nYSize, l_nBlockYSize == 0
    6305        9100 :                         ? static_cast<int>(TIFFDefaultStripSize(l_hTIFF, 0))
    6306        9100 :                         : l_nBlockYSize);
    6307             : 
    6308        9100 :         TIFFSetField(l_hTIFF, TIFFTAG_ROWSPERSTRIP, l_nRowsPerStrip);
    6309             :     }
    6310             : 
    6311             :     /* -------------------------------------------------------------------- */
    6312             :     /*      Set compression related tags.                                   */
    6313             :     /* -------------------------------------------------------------------- */
    6314        9901 :     if (GTIFFSupportsPredictor(l_nCompression))
    6315         973 :         TIFFSetField(l_hTIFF, TIFFTAG_PREDICTOR, nPredictor);
    6316        9901 :     if (l_nCompression == COMPRESSION_ADOBE_DEFLATE ||
    6317             :         l_nCompression == COMPRESSION_LERC)
    6318             :     {
    6319         280 :         GTiffSetDeflateSubCodec(l_hTIFF);
    6320             : 
    6321         280 :         if (l_nZLevel != -1)
    6322          22 :             TIFFSetField(l_hTIFF, TIFFTAG_ZIPQUALITY, l_nZLevel);
    6323             :     }
    6324        9901 :     if (l_nCompression == COMPRESSION_JPEG && l_nJpegQuality != -1)
    6325        1905 :         TIFFSetField(l_hTIFF, TIFFTAG_JPEGQUALITY, l_nJpegQuality);
    6326        9901 :     if (l_nCompression == COMPRESSION_LZMA && l_nLZMAPreset != -1)
    6327          10 :         TIFFSetField(l_hTIFF, TIFFTAG_LZMAPRESET, l_nLZMAPreset);
    6328        9901 :     if ((l_nCompression == COMPRESSION_ZSTD ||
    6329         194 :          l_nCompression == COMPRESSION_LERC) &&
    6330             :         l_nZSTDLevel != -1)
    6331          12 :         TIFFSetField(l_hTIFF, TIFFTAG_ZSTD_LEVEL, l_nZSTDLevel);
    6332        9901 :     if (l_nCompression == COMPRESSION_LERC)
    6333             :     {
    6334          97 :         TIFFSetField(l_hTIFF, TIFFTAG_LERC_MAXZERROR, l_dfMaxZError);
    6335             :     }
    6336             : #if HAVE_JXL
    6337        9901 :     if (l_nCompression == COMPRESSION_JXL ||
    6338             :         l_nCompression == COMPRESSION_JXL_DNG_1_7)
    6339             :     {
    6340         104 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_LOSSYNESS,
    6341             :                      l_bJXLLossless ? JXL_LOSSLESS : JXL_LOSSY);
    6342         104 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_EFFORT, l_nJXLEffort);
    6343         104 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_DISTANCE,
    6344             :                      static_cast<double>(l_fJXLDistance));
    6345         104 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_ALPHA_DISTANCE,
    6346             :                      static_cast<double>(l_fJXLAlphaDistance));
    6347             :     }
    6348             : #endif
    6349        9901 :     if (l_nCompression == COMPRESSION_WEBP)
    6350          33 :         TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LEVEL, l_nWebPLevel);
    6351        9901 :     if (l_nCompression == COMPRESSION_WEBP && l_bWebPLossless)
    6352           7 :         TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LOSSLESS, 1);
    6353             : 
    6354        9901 :     if (l_nCompression == COMPRESSION_JPEG)
    6355        2083 :         TIFFSetField(l_hTIFF, TIFFTAG_JPEGTABLESMODE, l_nJpegTablesMode);
    6356             : 
    6357             :     /* -------------------------------------------------------------------- */
    6358             :     /*      If we forced production of a file with photometric=palette,     */
    6359             :     /*      we need to push out a default color table.                      */
    6360             :     /* -------------------------------------------------------------------- */
    6361        9901 :     if (bForceColorTable)
    6362             :     {
    6363           4 :         const int nColors = eType == GDT_UInt8 ? 256 : 65536;
    6364             : 
    6365             :         unsigned short *panTRed = static_cast<unsigned short *>(
    6366           4 :             CPLMalloc(sizeof(unsigned short) * nColors));
    6367             :         unsigned short *panTGreen = static_cast<unsigned short *>(
    6368           4 :             CPLMalloc(sizeof(unsigned short) * nColors));
    6369             :         unsigned short *panTBlue = static_cast<unsigned short *>(
    6370           4 :             CPLMalloc(sizeof(unsigned short) * nColors));
    6371             : 
    6372        1028 :         for (int iColor = 0; iColor < nColors; ++iColor)
    6373             :         {
    6374        1024 :             if (eType == GDT_UInt8)
    6375             :             {
    6376        1024 :                 panTRed[iColor] = GTiffDataset::ClampCTEntry(
    6377             :                     iColor, 1, iColor, nColorTableMultiplier);
    6378        1024 :                 panTGreen[iColor] = GTiffDataset::ClampCTEntry(
    6379             :                     iColor, 2, iColor, nColorTableMultiplier);
    6380        1024 :                 panTBlue[iColor] = GTiffDataset::ClampCTEntry(
    6381             :                     iColor, 3, iColor, nColorTableMultiplier);
    6382             :             }
    6383             :             else
    6384             :             {
    6385           0 :                 panTRed[iColor] = static_cast<unsigned short>(iColor);
    6386           0 :                 panTGreen[iColor] = static_cast<unsigned short>(iColor);
    6387           0 :                 panTBlue[iColor] = static_cast<unsigned short>(iColor);
    6388             :             }
    6389             :         }
    6390             : 
    6391           4 :         TIFFSetField(l_hTIFF, TIFFTAG_COLORMAP, panTRed, panTGreen, panTBlue);
    6392             : 
    6393           4 :         CPLFree(panTRed);
    6394           4 :         CPLFree(panTGreen);
    6395           4 :         CPLFree(panTBlue);
    6396             :     }
    6397             : 
    6398             :     // This trick
    6399             :     // creates a temporary in-memory file and fetches its JPEG tables so that
    6400             :     // we can directly set them, before tif_jpeg.c compute them at the first
    6401             :     // strip/tile writing, which is too late, since we have already crystalized
    6402             :     // the directory. This way we avoid a directory rewriting.
    6403       11984 :     if (l_nCompression == COMPRESSION_JPEG &&
    6404        2083 :         CPLTestBool(
    6405             :             CSLFetchNameValueDef(papszParamList, "WRITE_JPEGTABLE_TAG", "YES")))
    6406             :     {
    6407        1014 :         GTiffWriteJPEGTables(
    6408             :             l_hTIFF, CSLFetchNameValue(papszParamList, "PHOTOMETRIC"),
    6409             :             CSLFetchNameValue(papszParamList, "JPEG_QUALITY"),
    6410             :             CSLFetchNameValue(papszParamList, "JPEGTABLESMODE"));
    6411             :     }
    6412             : 
    6413        9901 :     *pfpL = l_fpL;
    6414             : 
    6415        9901 :     return l_hTIFF;
    6416             : }
    6417             : 
    6418             : /************************************************************************/
    6419             : /*                            GuessJPEGQuality()                        */
    6420             : /*                                                                      */
    6421             : /*      Guess JPEG quality from JPEGTABLES tag.                         */
    6422             : /************************************************************************/
    6423             : 
    6424        3850 : static const GByte *GTIFFFindNextTable(const GByte *paby, GByte byMarker,
    6425             :                                        int nLen, int *pnLenTable)
    6426             : {
    6427        7967 :     for (int i = 0; i + 1 < nLen;)
    6428             :     {
    6429        7967 :         if (paby[i] != 0xFF)
    6430           0 :             return nullptr;
    6431        7967 :         ++i;
    6432        7967 :         if (paby[i] == 0xD8)
    6433             :         {
    6434        3117 :             ++i;
    6435        3117 :             continue;
    6436             :         }
    6437        4850 :         if (i + 2 >= nLen)
    6438         833 :             return nullptr;
    6439        4017 :         int nMarkerLen = paby[i + 1] * 256 + paby[i + 2];
    6440        4017 :         if (i + 1 + nMarkerLen >= nLen)
    6441           0 :             return nullptr;
    6442        4017 :         if (paby[i] == byMarker)
    6443             :         {
    6444        3017 :             if (pnLenTable)
    6445        2473 :                 *pnLenTable = nMarkerLen;
    6446        3017 :             return paby + i + 1;
    6447             :         }
    6448        1000 :         i += 1 + nMarkerLen;
    6449             :     }
    6450           0 :     return nullptr;
    6451             : }
    6452             : 
    6453             : constexpr GByte MARKER_HUFFMAN_TABLE = 0xC4;
    6454             : constexpr GByte MARKER_QUANT_TABLE = 0xDB;
    6455             : 
    6456             : // We assume that if there are several quantization tables, they are
    6457             : // in the same order. Which is a reasonable assumption for updating
    6458             : // a file generated by ourselves.
    6459         904 : static bool GTIFFQuantizationTablesEqual(const GByte *paby1, int nLen1,
    6460             :                                          const GByte *paby2, int nLen2)
    6461             : {
    6462         904 :     bool bFound = false;
    6463             :     while (true)
    6464             :     {
    6465         945 :         int nLenTable1 = 0;
    6466         945 :         int nLenTable2 = 0;
    6467             :         const GByte *paby1New =
    6468         945 :             GTIFFFindNextTable(paby1, MARKER_QUANT_TABLE, nLen1, &nLenTable1);
    6469             :         const GByte *paby2New =
    6470         945 :             GTIFFFindNextTable(paby2, MARKER_QUANT_TABLE, nLen2, &nLenTable2);
    6471         945 :         if (paby1New == nullptr && paby2New == nullptr)
    6472         904 :             return bFound;
    6473         911 :         if (paby1New == nullptr || paby2New == nullptr)
    6474           0 :             return false;
    6475         911 :         if (nLenTable1 != nLenTable2)
    6476         207 :             return false;
    6477         704 :         if (memcmp(paby1New, paby2New, nLenTable1) != 0)
    6478         663 :             return false;
    6479          41 :         paby1New += nLenTable1;
    6480          41 :         paby2New += nLenTable2;
    6481          41 :         nLen1 -= static_cast<int>(paby1New - paby1);
    6482          41 :         nLen2 -= static_cast<int>(paby2New - paby2);
    6483          41 :         paby1 = paby1New;
    6484          41 :         paby2 = paby2New;
    6485          41 :         bFound = true;
    6486          41 :     }
    6487             : }
    6488             : 
    6489             : // Guess the JPEG quality by comparing against the MD5Sum of precomputed
    6490             : // quantization tables
    6491         409 : static int GuessJPEGQualityFromMD5(const uint8_t md5JPEGQuantTable[][16],
    6492             :                                    const GByte *const pabyJPEGTable,
    6493             :                                    int nJPEGTableSize)
    6494             : {
    6495         409 :     int nRemainingLen = nJPEGTableSize;
    6496         409 :     const GByte *pabyCur = pabyJPEGTable;
    6497             : 
    6498             :     struct CPLMD5Context context;
    6499         409 :     CPLMD5Init(&context);
    6500             : 
    6501             :     while (true)
    6502             :     {
    6503        1060 :         int nLenTable = 0;
    6504        1060 :         const GByte *pabyNew = GTIFFFindNextTable(pabyCur, MARKER_QUANT_TABLE,
    6505             :                                                   nRemainingLen, &nLenTable);
    6506        1060 :         if (pabyNew == nullptr)
    6507         409 :             break;
    6508         651 :         CPLMD5Update(&context, pabyNew, nLenTable);
    6509         651 :         pabyNew += nLenTable;
    6510         651 :         nRemainingLen -= static_cast<int>(pabyNew - pabyCur);
    6511         651 :         pabyCur = pabyNew;
    6512         651 :     }
    6513             : 
    6514             :     GByte digest[16];
    6515         409 :     CPLMD5Final(digest, &context);
    6516             : 
    6517       28846 :     for (int i = 0; i < 100; i++)
    6518             :     {
    6519       28843 :         if (memcmp(md5JPEGQuantTable[i], digest, 16) == 0)
    6520             :         {
    6521         406 :             return i + 1;
    6522             :         }
    6523             :     }
    6524           3 :     return -1;
    6525             : }
    6526             : 
    6527         464 : int GTiffDataset::GuessJPEGQuality(bool &bOutHasQuantizationTable,
    6528             :                                    bool &bOutHasHuffmanTable)
    6529             : {
    6530         464 :     CPLAssert(m_nCompression == COMPRESSION_JPEG);
    6531         464 :     uint32_t nJPEGTableSize = 0;
    6532         464 :     void *pJPEGTable = nullptr;
    6533         464 :     if (!TIFFGetField(m_hTIFF, TIFFTAG_JPEGTABLES, &nJPEGTableSize,
    6534             :                       &pJPEGTable))
    6535             :     {
    6536          14 :         bOutHasQuantizationTable = false;
    6537          14 :         bOutHasHuffmanTable = false;
    6538          14 :         return -1;
    6539             :     }
    6540             : 
    6541         450 :     bOutHasQuantizationTable =
    6542         450 :         GTIFFFindNextTable(static_cast<const GByte *>(pJPEGTable),
    6543             :                            MARKER_QUANT_TABLE, nJPEGTableSize,
    6544         450 :                            nullptr) != nullptr;
    6545         450 :     bOutHasHuffmanTable =
    6546         450 :         GTIFFFindNextTable(static_cast<const GByte *>(pJPEGTable),
    6547             :                            MARKER_HUFFMAN_TABLE, nJPEGTableSize,
    6548         450 :                            nullptr) != nullptr;
    6549         450 :     if (!bOutHasQuantizationTable)
    6550           7 :         return -1;
    6551             : 
    6552         443 :     if ((nBands == 1 && m_nBitsPerSample == 8) ||
    6553         382 :         (nBands == 3 && m_nBitsPerSample == 8 &&
    6554         336 :          m_nPhotometric == PHOTOMETRIC_RGB) ||
    6555         288 :         (nBands == 4 && m_nBitsPerSample == 8 &&
    6556          27 :          m_nPhotometric == PHOTOMETRIC_SEPARATED))
    6557             :     {
    6558         167 :         return GuessJPEGQualityFromMD5(md5JPEGQuantTable_generic_8bit,
    6559             :                                        static_cast<const GByte *>(pJPEGTable),
    6560         167 :                                        static_cast<int>(nJPEGTableSize));
    6561             :     }
    6562             : 
    6563         276 :     if (nBands == 3 && m_nBitsPerSample == 8 &&
    6564         242 :         m_nPhotometric == PHOTOMETRIC_YCBCR)
    6565             :     {
    6566             :         int nRet =
    6567         242 :             GuessJPEGQualityFromMD5(md5JPEGQuantTable_3_YCBCR_8bit,
    6568             :                                     static_cast<const GByte *>(pJPEGTable),
    6569             :                                     static_cast<int>(nJPEGTableSize));
    6570         242 :         if (nRet < 0)
    6571             :         {
    6572             :             // libjpeg 9e has modified the YCbCr quantization tables.
    6573             :             nRet =
    6574           0 :                 GuessJPEGQualityFromMD5(md5JPEGQuantTable_3_YCBCR_8bit_jpeg9e,
    6575             :                                         static_cast<const GByte *>(pJPEGTable),
    6576             :                                         static_cast<int>(nJPEGTableSize));
    6577             :         }
    6578         242 :         return nRet;
    6579             :     }
    6580             : 
    6581          34 :     char **papszLocalParameters = nullptr;
    6582             :     papszLocalParameters =
    6583          34 :         CSLSetNameValue(papszLocalParameters, "COMPRESS", "JPEG");
    6584          34 :     if (m_nPhotometric == PHOTOMETRIC_YCBCR)
    6585             :         papszLocalParameters =
    6586           7 :             CSLSetNameValue(papszLocalParameters, "PHOTOMETRIC", "YCBCR");
    6587          27 :     else if (m_nPhotometric == PHOTOMETRIC_SEPARATED)
    6588             :         papszLocalParameters =
    6589           0 :             CSLSetNameValue(papszLocalParameters, "PHOTOMETRIC", "CMYK");
    6590             :     papszLocalParameters =
    6591          34 :         CSLSetNameValue(papszLocalParameters, "BLOCKYSIZE", "16");
    6592          34 :     if (m_nBitsPerSample == 12)
    6593             :         papszLocalParameters =
    6594          16 :             CSLSetNameValue(papszLocalParameters, "NBITS", "12");
    6595             : 
    6596             :     const CPLString osTmpFilenameIn(
    6597          34 :         VSIMemGenerateHiddenFilename("gtiffdataset_guess_jpeg_quality_tmp"));
    6598             : 
    6599          34 :     int nRet = -1;
    6600         938 :     for (int nQuality = 0; nQuality <= 100 && nRet < 0; ++nQuality)
    6601             :     {
    6602         904 :         VSILFILE *fpTmp = nullptr;
    6603         904 :         if (nQuality == 0)
    6604             :             papszLocalParameters =
    6605          34 :                 CSLSetNameValue(papszLocalParameters, "JPEG_QUALITY", "75");
    6606             :         else
    6607             :             papszLocalParameters =
    6608         870 :                 CSLSetNameValue(papszLocalParameters, "JPEG_QUALITY",
    6609             :                                 CPLSPrintf("%d", nQuality));
    6610             : 
    6611         904 :         CPLPushErrorHandler(CPLQuietErrorHandler);
    6612         904 :         CPLString osTmp;
    6613             :         bool bTileInterleaving;
    6614        1808 :         TIFF *hTIFFTmp = CreateLL(
    6615         904 :             osTmpFilenameIn, 16, 16, (nBands <= 4) ? nBands : 1,
    6616             :             GetRasterBand(1)->GetRasterDataType(), 0.0, 0, papszLocalParameters,
    6617             :             &fpTmp, osTmp, /* bCreateCopy=*/false, bTileInterleaving);
    6618         904 :         CPLPopErrorHandler();
    6619         904 :         if (!hTIFFTmp)
    6620             :         {
    6621           0 :             break;
    6622             :         }
    6623             : 
    6624         904 :         TIFFWriteCheck(hTIFFTmp, FALSE, "CreateLL");
    6625         904 :         TIFFWriteDirectory(hTIFFTmp);
    6626         904 :         TIFFSetDirectory(hTIFFTmp, 0);
    6627             :         // Now reset jpegcolormode.
    6628        1196 :         if (m_nPhotometric == PHOTOMETRIC_YCBCR &&
    6629         292 :             CPLTestBool(CPLGetConfigOption("CONVERT_YCBCR_TO_RGB", "YES")))
    6630             :         {
    6631         292 :             TIFFSetField(hTIFFTmp, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
    6632             :         }
    6633             : 
    6634         904 :         GByte abyZeroData[(16 * 16 * 4 * 3) / 2] = {};
    6635         904 :         const int nBlockSize =
    6636         904 :             (16 * 16 * ((nBands <= 4) ? nBands : 1) * m_nBitsPerSample) / 8;
    6637         904 :         TIFFWriteEncodedStrip(hTIFFTmp, 0, abyZeroData, nBlockSize);
    6638             : 
    6639         904 :         uint32_t nJPEGTableSizeTry = 0;
    6640         904 :         void *pJPEGTableTry = nullptr;
    6641         904 :         if (TIFFGetField(hTIFFTmp, TIFFTAG_JPEGTABLES, &nJPEGTableSizeTry,
    6642         904 :                          &pJPEGTableTry))
    6643             :         {
    6644         904 :             if (GTIFFQuantizationTablesEqual(
    6645             :                     static_cast<GByte *>(pJPEGTable), nJPEGTableSize,
    6646             :                     static_cast<GByte *>(pJPEGTableTry), nJPEGTableSizeTry))
    6647             :             {
    6648          34 :                 nRet = (nQuality == 0) ? 75 : nQuality;
    6649             :             }
    6650             :         }
    6651             : 
    6652         904 :         XTIFFClose(hTIFFTmp);
    6653         904 :         CPL_IGNORE_RET_VAL(VSIFCloseL(fpTmp));
    6654             :     }
    6655             : 
    6656          34 :     CSLDestroy(papszLocalParameters);
    6657          34 :     VSIUnlink(osTmpFilenameIn);
    6658             : 
    6659          34 :     return nRet;
    6660             : }
    6661             : 
    6662             : /************************************************************************/
    6663             : /*                SetJPEGQualityAndTablesModeFromFile()                 */
    6664             : /************************************************************************/
    6665             : 
    6666         161 : void GTiffDataset::SetJPEGQualityAndTablesModeFromFile(
    6667             :     int nQuality, bool bHasQuantizationTable, bool bHasHuffmanTable)
    6668             : {
    6669         161 :     if (nQuality > 0)
    6670             :     {
    6671         154 :         CPLDebug("GTiff", "Guessed JPEG quality to be %d", nQuality);
    6672         154 :         m_nJpegQuality = static_cast<signed char>(nQuality);
    6673         154 :         TIFFSetField(m_hTIFF, TIFFTAG_JPEGQUALITY, nQuality);
    6674             : 
    6675             :         // This means we will use the quantization tables from the
    6676             :         // JpegTables tag.
    6677         154 :         m_nJpegTablesMode = JPEGTABLESMODE_QUANT;
    6678             :     }
    6679             :     else
    6680             :     {
    6681           7 :         uint32_t nJPEGTableSize = 0;
    6682           7 :         void *pJPEGTable = nullptr;
    6683           7 :         if (!TIFFGetField(m_hTIFF, TIFFTAG_JPEGTABLES, &nJPEGTableSize,
    6684             :                           &pJPEGTable))
    6685             :         {
    6686           4 :             toff_t *panByteCounts = nullptr;
    6687           8 :             const int nBlockCount = m_nPlanarConfig == PLANARCONFIG_SEPARATE
    6688           4 :                                         ? m_nBlocksPerBand * nBands
    6689             :                                         : m_nBlocksPerBand;
    6690           4 :             if (TIFFIsTiled(m_hTIFF))
    6691           1 :                 TIFFGetField(m_hTIFF, TIFFTAG_TILEBYTECOUNTS, &panByteCounts);
    6692             :             else
    6693           3 :                 TIFFGetField(m_hTIFF, TIFFTAG_STRIPBYTECOUNTS, &panByteCounts);
    6694             : 
    6695           4 :             bool bFoundNonEmptyBlock = false;
    6696           4 :             if (panByteCounts != nullptr)
    6697             :             {
    6698          56 :                 for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
    6699             :                 {
    6700          53 :                     if (panByteCounts[iBlock] != 0)
    6701             :                     {
    6702           1 :                         bFoundNonEmptyBlock = true;
    6703           1 :                         break;
    6704             :                     }
    6705             :                 }
    6706             :             }
    6707           4 :             if (bFoundNonEmptyBlock)
    6708             :             {
    6709           1 :                 CPLDebug("GTiff", "Could not guess JPEG quality. "
    6710             :                                   "JPEG tables are missing, so going in "
    6711             :                                   "TIFFTAG_JPEGTABLESMODE = 0/2 mode");
    6712             :                 // Write quantization tables in each strile.
    6713           1 :                 m_nJpegTablesMode = 0;
    6714             :             }
    6715             :         }
    6716             :         else
    6717             :         {
    6718           3 :             if (bHasQuantizationTable)
    6719             :             {
    6720             :                 // FIXME in libtiff: this is likely going to cause issues
    6721             :                 // since libtiff will reuse in each strile the number of
    6722             :                 // the global quantization table, which is invalid.
    6723           1 :                 CPLDebug("GTiff",
    6724             :                          "Could not guess JPEG quality although JPEG "
    6725             :                          "quantization tables are present, so going in "
    6726             :                          "TIFFTAG_JPEGTABLESMODE = 0/2 mode");
    6727             :             }
    6728             :             else
    6729             :             {
    6730           2 :                 CPLDebug("GTiff",
    6731             :                          "Could not guess JPEG quality since JPEG "
    6732             :                          "quantization tables are not present, so going in "
    6733             :                          "TIFFTAG_JPEGTABLESMODE = 0/2 mode");
    6734             :             }
    6735             : 
    6736             :             // Write quantization tables in each strile.
    6737           3 :             m_nJpegTablesMode = 0;
    6738             :         }
    6739             :     }
    6740         161 :     if (bHasHuffmanTable)
    6741             :     {
    6742             :         // If there are Huffman tables in header use them, otherwise
    6743             :         // if we use optimized tables, libtiff will currently reuse
    6744             :         // the number of the Huffman tables of the header for the
    6745             :         // optimized version of each strile, which is illegal.
    6746          23 :         m_nJpegTablesMode |= JPEGTABLESMODE_HUFF;
    6747             :     }
    6748         161 :     if (m_nJpegTablesMode >= 0)
    6749         159 :         TIFFSetField(m_hTIFF, TIFFTAG_JPEGTABLESMODE, m_nJpegTablesMode);
    6750         161 : }
    6751             : 
    6752             : /************************************************************************/
    6753             : /*                               Create()                               */
    6754             : /*                                                                      */
    6755             : /*      Create a new GeoTIFF or TIFF file.                              */
    6756             : /************************************************************************/
    6757             : 
    6758        5822 : GDALDataset *GTiffDataset::Create(const char *pszFilename, int nXSize,
    6759             :                                   int nYSize, int l_nBands, GDALDataType eType,
    6760             :                                   CSLConstList papszParamList)
    6761             : 
    6762             : {
    6763        5822 :     VSILFILE *l_fpL = nullptr;
    6764       11644 :     CPLString l_osTmpFilename;
    6765             : 
    6766             :     const int nColorTableMultiplier = std::max(
    6767       11644 :         1,
    6768       11644 :         std::min(257,
    6769        5822 :                  atoi(CSLFetchNameValueDef(
    6770             :                      papszParamList, "COLOR_TABLE_MULTIPLIER",
    6771        5822 :                      CPLSPrintf("%d", DEFAULT_COLOR_TABLE_MULTIPLIER_257)))));
    6772             : 
    6773             :     /* -------------------------------------------------------------------- */
    6774             :     /*      Create the underlying TIFF file.                                */
    6775             :     /* -------------------------------------------------------------------- */
    6776             :     bool bTileInterleaving;
    6777             :     TIFF *l_hTIFF =
    6778        5822 :         CreateLL(pszFilename, nXSize, nYSize, l_nBands, eType, 0,
    6779             :                  nColorTableMultiplier, papszParamList, &l_fpL, l_osTmpFilename,
    6780             :                  /* bCreateCopy=*/false, bTileInterleaving);
    6781        5822 :     const bool bStreaming = !l_osTmpFilename.empty();
    6782             : 
    6783        5822 :     if (l_hTIFF == nullptr)
    6784          38 :         return nullptr;
    6785             : 
    6786             :     /* -------------------------------------------------------------------- */
    6787             :     /*      Create the new GTiffDataset object.                             */
    6788             :     /* -------------------------------------------------------------------- */
    6789       11568 :     auto poDS = std::make_unique<GTiffDataset>();
    6790        5784 :     poDS->m_hTIFF = l_hTIFF;
    6791        5784 :     poDS->m_fpL = l_fpL;
    6792        5784 :     const bool bSuppressASAP = CPLTestBool(
    6793             :         CSLFetchNameValueDef(papszParamList, "@SUPPRESS_ASAP", "NO"));
    6794        5784 :     if (bSuppressASAP)
    6795          36 :         poDS->MarkSuppressOnClose();
    6796        5784 :     if (bStreaming)
    6797             :     {
    6798           4 :         poDS->m_bStreamingOut = true;
    6799           4 :         poDS->m_pszTmpFilename = CPLStrdup(l_osTmpFilename);
    6800           4 :         poDS->m_fpToWrite = VSIFOpenL(pszFilename, "wb");
    6801           4 :         if (poDS->m_fpToWrite == nullptr)
    6802             :         {
    6803           1 :             VSIUnlink(l_osTmpFilename);
    6804           1 :             return nullptr;
    6805             :         }
    6806             :     }
    6807        5783 :     poDS->nRasterXSize = nXSize;
    6808        5783 :     poDS->nRasterYSize = nYSize;
    6809        5783 :     poDS->eAccess = GA_Update;
    6810             : 
    6811             :     // This will avoid GTiffDataset::GetSiblingFiles() to trigger a directory
    6812             :     // listing, which is potentially costly and only makes sense when opening
    6813             :     // new files, not creating new ones. Helps for scenario like
    6814             :     // https://github.com/OSGeo/gdal/issues/13930
    6815        5783 :     poDS->m_bHasGotSiblingFiles = true;
    6816             : 
    6817        5783 :     poDS->m_nColorTableMultiplier = nColorTableMultiplier;
    6818             : 
    6819        5783 :     poDS->m_bCrystalized = false;
    6820        5783 :     poDS->m_nSamplesPerPixel = static_cast<uint16_t>(l_nBands);
    6821        5783 :     poDS->m_osFilename = pszFilename;
    6822             : 
    6823             :     // Don't try to load external metadata files (#6597).
    6824        5783 :     poDS->m_bIMDRPCMetadataLoaded = true;
    6825             : 
    6826             :     // Avoid premature crystalization that will cause directory re-writing if
    6827             :     // GetProjectionRef() or GetGeoTransform() are called on the newly created
    6828             :     // GeoTIFF.
    6829        5783 :     poDS->m_bLookedForProjection = true;
    6830             : 
    6831        5783 :     TIFFGetField(l_hTIFF, TIFFTAG_SAMPLEFORMAT, &(poDS->m_nSampleFormat));
    6832        5783 :     TIFFGetField(l_hTIFF, TIFFTAG_PLANARCONFIG, &(poDS->m_nPlanarConfig));
    6833             :     // Weird that we need this, but otherwise we get a Valgrind warning on
    6834             :     // tiff_write_124.
    6835        5783 :     if (!TIFFGetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, &(poDS->m_nPhotometric)))
    6836           1 :         poDS->m_nPhotometric = PHOTOMETRIC_MINISBLACK;
    6837        5783 :     TIFFGetField(l_hTIFF, TIFFTAG_BITSPERSAMPLE, &(poDS->m_nBitsPerSample));
    6838        5783 :     TIFFGetField(l_hTIFF, TIFFTAG_COMPRESSION, &(poDS->m_nCompression));
    6839             : 
    6840        5783 :     if (TIFFIsTiled(l_hTIFF))
    6841             :     {
    6842         407 :         TIFFGetField(l_hTIFF, TIFFTAG_TILEWIDTH, &(poDS->m_nBlockXSize));
    6843         407 :         TIFFGetField(l_hTIFF, TIFFTAG_TILELENGTH, &(poDS->m_nBlockYSize));
    6844             :     }
    6845             :     else
    6846             :     {
    6847        5376 :         if (!TIFFGetField(l_hTIFF, TIFFTAG_ROWSPERSTRIP,
    6848        5376 :                           &(poDS->m_nRowsPerStrip)))
    6849           0 :             poDS->m_nRowsPerStrip = 1;  // Dummy value.
    6850             : 
    6851        5376 :         poDS->m_nBlockXSize = nXSize;
    6852       10752 :         poDS->m_nBlockYSize =
    6853        5376 :             std::min(static_cast<int>(poDS->m_nRowsPerStrip), nYSize);
    6854             :     }
    6855             : 
    6856        5783 :     if (!poDS->ComputeBlocksPerColRowAndBand(l_nBands))
    6857             :     {
    6858           0 :         poDS->m_fpL->CancelCreation();
    6859           0 :         return nullptr;
    6860             :     }
    6861             : 
    6862        5783 :     poDS->m_eProfile = GetProfile(CSLFetchNameValue(papszParamList, "PROFILE"));
    6863             : 
    6864             :     /* -------------------------------------------------------------------- */
    6865             :     /*      YCbCr JPEG compressed images should be translated on the fly    */
    6866             :     /*      to RGB by libtiff/libjpeg unless specifically requested         */
    6867             :     /*      otherwise.                                                      */
    6868             :     /* -------------------------------------------------------------------- */
    6869        5783 :     if (poDS->m_nCompression == COMPRESSION_JPEG &&
    6870        5804 :         poDS->m_nPhotometric == PHOTOMETRIC_YCBCR &&
    6871          21 :         CPLTestBool(CPLGetConfigOption("CONVERT_YCBCR_TO_RGB", "YES")))
    6872             :     {
    6873          21 :         int nColorMode = 0;
    6874             : 
    6875          21 :         poDS->SetMetadataItem("SOURCE_COLOR_SPACE", "YCbCr", "IMAGE_STRUCTURE");
    6876          42 :         if (!TIFFGetField(l_hTIFF, TIFFTAG_JPEGCOLORMODE, &nColorMode) ||
    6877          21 :             nColorMode != JPEGCOLORMODE_RGB)
    6878          21 :             TIFFSetField(l_hTIFF, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
    6879             :     }
    6880             : 
    6881        5783 :     if (poDS->m_nCompression == COMPRESSION_LERC)
    6882             :     {
    6883          26 :         uint32_t nLercParamCount = 0;
    6884          26 :         uint32_t *panLercParams = nullptr;
    6885          26 :         if (TIFFGetField(l_hTIFF, TIFFTAG_LERC_PARAMETERS, &nLercParamCount,
    6886          52 :                          &panLercParams) &&
    6887          26 :             nLercParamCount == 2)
    6888             :         {
    6889          26 :             memcpy(poDS->m_anLercAddCompressionAndVersion, panLercParams,
    6890             :                    sizeof(poDS->m_anLercAddCompressionAndVersion));
    6891             :         }
    6892             :     }
    6893             : 
    6894             :     /* -------------------------------------------------------------------- */
    6895             :     /*      Read palette back as a color table if it has one.               */
    6896             :     /* -------------------------------------------------------------------- */
    6897        5783 :     unsigned short *panRed = nullptr;
    6898        5783 :     unsigned short *panGreen = nullptr;
    6899        5783 :     unsigned short *panBlue = nullptr;
    6900             : 
    6901        5787 :     if (poDS->m_nPhotometric == PHOTOMETRIC_PALETTE &&
    6902           4 :         TIFFGetField(l_hTIFF, TIFFTAG_COLORMAP, &panRed, &panGreen, &panBlue))
    6903             :     {
    6904             : 
    6905           4 :         poDS->m_poColorTable = std::make_unique<GDALColorTable>();
    6906             : 
    6907           4 :         const int nColorCount = 1 << poDS->m_nBitsPerSample;
    6908             : 
    6909        1028 :         for (int iColor = nColorCount - 1; iColor >= 0; iColor--)
    6910             :         {
    6911        1024 :             const GDALColorEntry oEntry = {
    6912        1024 :                 static_cast<short>(panRed[iColor] / nColorTableMultiplier),
    6913        1024 :                 static_cast<short>(panGreen[iColor] / nColorTableMultiplier),
    6914        1024 :                 static_cast<short>(panBlue[iColor] / nColorTableMultiplier),
    6915        1024 :                 static_cast<short>(255)};
    6916             : 
    6917        1024 :             poDS->m_poColorTable->SetColorEntry(iColor, &oEntry);
    6918             :         }
    6919             :     }
    6920             : 
    6921             :     /* -------------------------------------------------------------------- */
    6922             :     /*      Do we want to ensure all blocks get written out on close to     */
    6923             :     /*      avoid sparse files?                                             */
    6924             :     /* -------------------------------------------------------------------- */
    6925        5783 :     if (!CPLFetchBool(papszParamList, "SPARSE_OK", false))
    6926        5673 :         poDS->m_bFillEmptyTilesAtClosing = true;
    6927             : 
    6928        5783 :     poDS->m_bWriteEmptyTiles =
    6929        6609 :         bStreaming || (poDS->m_nCompression != COMPRESSION_NONE &&
    6930         826 :                        poDS->m_bFillEmptyTilesAtClosing);
    6931             :     // Only required for people writing non-compressed striped files in the
    6932             :     // right order and wanting all tstrips to be written in the same order
    6933             :     // so that the end result can be memory mapped without knowledge of each
    6934             :     // strip offset.
    6935        5783 :     if (CPLTestBool(CSLFetchNameValueDef(
    6936       11566 :             papszParamList, "WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")) ||
    6937        5783 :         CPLTestBool(CSLFetchNameValueDef(
    6938             :             papszParamList, "@WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")))
    6939             :     {
    6940          26 :         poDS->m_bWriteEmptyTiles = true;
    6941             :     }
    6942             : 
    6943             :     /* -------------------------------------------------------------------- */
    6944             :     /*      Preserve creation options for consulting later (for instance    */
    6945             :     /*      to decide if a TFW file should be written).                     */
    6946             :     /* -------------------------------------------------------------------- */
    6947        5783 :     poDS->m_papszCreationOptions = CSLDuplicate(papszParamList);
    6948             : 
    6949        5783 :     poDS->m_nZLevel = GTiffGetZLevel(papszParamList);
    6950        5783 :     poDS->m_nLZMAPreset = GTiffGetLZMAPreset(papszParamList);
    6951        5783 :     poDS->m_nZSTDLevel = GTiffGetZSTDPreset(papszParamList);
    6952        5783 :     poDS->m_nWebPLevel = GTiffGetWebPLevel(papszParamList);
    6953        5783 :     poDS->m_bWebPLossless = GTiffGetWebPLossless(papszParamList);
    6954        5785 :     if (poDS->m_nWebPLevel != 100 && poDS->m_bWebPLossless &&
    6955           2 :         CSLFetchNameValue(papszParamList, "WEBP_LEVEL"))
    6956             :     {
    6957           0 :         CPLError(CE_Warning, CPLE_AppDefined,
    6958             :                  "WEBP_LEVEL is specified, but WEBP_LOSSLESS=YES. "
    6959             :                  "WEBP_LEVEL will be ignored.");
    6960             :     }
    6961        5783 :     poDS->m_nJpegQuality = GTiffGetJpegQuality(papszParamList);
    6962        5783 :     poDS->m_nJpegTablesMode = GTiffGetJpegTablesMode(papszParamList);
    6963        5783 :     poDS->m_dfMaxZError = GTiffGetLERCMaxZError(papszParamList);
    6964        5783 :     poDS->m_dfMaxZErrorOverview = GTiffGetLERCMaxZErrorOverview(papszParamList);
    6965             : #if HAVE_JXL
    6966        5783 :     poDS->m_bJXLLossless = GTiffGetJXLLossless(papszParamList);
    6967        5783 :     poDS->m_nJXLEffort = GTiffGetJXLEffort(papszParamList);
    6968        5783 :     poDS->m_fJXLDistance = GTiffGetJXLDistance(papszParamList);
    6969        5783 :     poDS->m_fJXLAlphaDistance = GTiffGetJXLAlphaDistance(papszParamList);
    6970             : #endif
    6971        5783 :     poDS->InitCreationOrOpenOptions(true, papszParamList);
    6972             : 
    6973             :     /* -------------------------------------------------------------------- */
    6974             :     /*      Create band information objects.                                */
    6975             :     /* -------------------------------------------------------------------- */
    6976      308439 :     for (int iBand = 0; iBand < l_nBands; ++iBand)
    6977             :     {
    6978      371881 :         if (poDS->m_nBitsPerSample == 8 || poDS->m_nBitsPerSample == 16 ||
    6979      372133 :             poDS->m_nBitsPerSample == 32 || poDS->m_nBitsPerSample == 64 ||
    6980         252 :             poDS->m_nBitsPerSample == 128)
    6981             :         {
    6982      605160 :             poDS->SetBand(iBand + 1, std::make_unique<GTiffRasterBand>(
    6983      605160 :                                          poDS.get(), iBand + 1));
    6984             :         }
    6985             :         else
    6986             :         {
    6987         152 :             poDS->SetBand(iBand + 1, std::make_unique<GTiffOddBitsBand>(
    6988          76 :                                          poDS.get(), iBand + 1));
    6989         152 :             poDS->GetRasterBand(iBand + 1)->SetMetadataItem(
    6990         152 :                 "NBITS", CPLString().Printf("%d", poDS->m_nBitsPerSample),
    6991          76 :                 "IMAGE_STRUCTURE");
    6992             :         }
    6993             :     }
    6994             : 
    6995        5783 :     poDS->GetDiscardLsbOption(papszParamList);
    6996             : 
    6997        5783 :     if (poDS->m_nPlanarConfig == PLANARCONFIG_CONTIG && l_nBands != 1)
    6998         851 :         poDS->SetMetadataItem("INTERLEAVE", "PIXEL", "IMAGE_STRUCTURE");
    6999             :     else
    7000        4932 :         poDS->SetMetadataItem("INTERLEAVE", "BAND", "IMAGE_STRUCTURE");
    7001             : 
    7002        5783 :     poDS->oOvManager.Initialize(poDS.get(), pszFilename);
    7003             : 
    7004        5783 :     return poDS.release();
    7005             : }
    7006             : 
    7007             : /************************************************************************/
    7008             : /*                         CopyImageryAndMask()                         */
    7009             : /************************************************************************/
    7010             : 
    7011         354 : CPLErr GTiffDataset::CopyImageryAndMask(GTiffDataset *poDstDS,
    7012             :                                         GDALDataset *poSrcDS,
    7013             :                                         GDALRasterBand *poSrcMaskBand,
    7014             :                                         GDALProgressFunc pfnProgress,
    7015             :                                         void *pProgressData)
    7016             : {
    7017         354 :     CPLErr eErr = CE_None;
    7018             : 
    7019         354 :     const auto eType = poDstDS->GetRasterBand(1)->GetRasterDataType();
    7020         354 :     const int nDataTypeSize = GDALGetDataTypeSizeBytes(eType);
    7021         354 :     const int l_nBands = poDstDS->GetRasterCount();
    7022             :     GByte *pBlockBuffer = static_cast<GByte *>(
    7023         354 :         VSI_MALLOC3_VERBOSE(poDstDS->m_nBlockXSize, poDstDS->m_nBlockYSize,
    7024             :                             cpl::fits_on<int>(l_nBands * nDataTypeSize)));
    7025         354 :     if (pBlockBuffer == nullptr)
    7026             :     {
    7027           0 :         eErr = CE_Failure;
    7028             :     }
    7029         354 :     const int nYSize = poDstDS->nRasterYSize;
    7030         354 :     const int nXSize = poDstDS->nRasterXSize;
    7031             :     const bool bIsOddBand =
    7032         354 :         dynamic_cast<GTiffOddBitsBand *>(poDstDS->GetRasterBand(1)) != nullptr;
    7033             : 
    7034         354 :     if (poDstDS->m_poMaskDS)
    7035             :     {
    7036          59 :         CPLAssert(poDstDS->m_poMaskDS->m_nBlockXSize == poDstDS->m_nBlockXSize);
    7037          59 :         CPLAssert(poDstDS->m_poMaskDS->m_nBlockYSize == poDstDS->m_nBlockYSize);
    7038             :     }
    7039             : 
    7040         354 :     if (poDstDS->m_nPlanarConfig == PLANARCONFIG_SEPARATE &&
    7041          58 :         !poDstDS->m_bTileInterleave)
    7042             :     {
    7043          45 :         int iBlock = 0;
    7044          45 :         const int nBlocks = poDstDS->m_nBlocksPerBand *
    7045          45 :                             (l_nBands + (poDstDS->m_poMaskDS ? 1 : 0));
    7046         195 :         for (int i = 0; eErr == CE_None && i < l_nBands; i++)
    7047             :         {
    7048         345 :             for (int iY = 0; iY < nYSize && eErr == CE_None;
    7049         195 :                  iY = ((nYSize - iY < poDstDS->m_nBlockYSize)
    7050         195 :                            ? nYSize
    7051          59 :                            : iY + poDstDS->m_nBlockYSize))
    7052             :             {
    7053             :                 const int nReqYSize =
    7054         195 :                     std::min(nYSize - iY, poDstDS->m_nBlockYSize);
    7055         495 :                 for (int iX = 0; iX < nXSize && eErr == CE_None;
    7056         300 :                      iX = ((nXSize - iX < poDstDS->m_nBlockXSize)
    7057         300 :                                ? nXSize
    7058         155 :                                : iX + poDstDS->m_nBlockXSize))
    7059             :                 {
    7060             :                     const int nReqXSize =
    7061         300 :                         std::min(nXSize - iX, poDstDS->m_nBlockXSize);
    7062         300 :                     if (nReqXSize < poDstDS->m_nBlockXSize ||
    7063         155 :                         nReqYSize < poDstDS->m_nBlockYSize)
    7064             :                     {
    7065         190 :                         memset(pBlockBuffer, 0,
    7066         190 :                                static_cast<size_t>(poDstDS->m_nBlockXSize) *
    7067         190 :                                    poDstDS->m_nBlockYSize * nDataTypeSize);
    7068             :                     }
    7069         300 :                     eErr = poSrcDS->GetRasterBand(i + 1)->RasterIO(
    7070             :                         GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
    7071             :                         nReqXSize, nReqYSize, eType, nDataTypeSize,
    7072         300 :                         static_cast<GSpacing>(nDataTypeSize) *
    7073         300 :                             poDstDS->m_nBlockXSize,
    7074             :                         nullptr);
    7075         300 :                     if (eErr == CE_None)
    7076             :                     {
    7077         300 :                         eErr = poDstDS->WriteEncodedTileOrStrip(
    7078             :                             iBlock, pBlockBuffer, false);
    7079             :                     }
    7080             : 
    7081         300 :                     iBlock++;
    7082         600 :                     if (pfnProgress &&
    7083         300 :                         !pfnProgress(static_cast<double>(iBlock) / nBlocks,
    7084             :                                      nullptr, pProgressData))
    7085             :                     {
    7086           0 :                         eErr = CE_Failure;
    7087             :                     }
    7088             : 
    7089         300 :                     if (poDstDS->m_bWriteError)
    7090           0 :                         eErr = CE_Failure;
    7091             :                 }
    7092             :             }
    7093             :         }
    7094          45 :         if (poDstDS->m_poMaskDS && eErr == CE_None)
    7095             :         {
    7096           6 :             int iBlockMask = 0;
    7097          17 :             for (int iY = 0, nYBlock = 0; iY < nYSize && eErr == CE_None;
    7098          11 :                  iY = ((nYSize - iY < poDstDS->m_nBlockYSize)
    7099          11 :                            ? nYSize
    7100           5 :                            : iY + poDstDS->m_nBlockYSize),
    7101             :                      nYBlock++)
    7102             :             {
    7103             :                 const int nReqYSize =
    7104          11 :                     std::min(nYSize - iY, poDstDS->m_nBlockYSize);
    7105          49 :                 for (int iX = 0, nXBlock = 0; iX < nXSize && eErr == CE_None;
    7106          38 :                      iX = ((nXSize - iX < poDstDS->m_nBlockXSize)
    7107          38 :                                ? nXSize
    7108          30 :                                : iX + poDstDS->m_nBlockXSize),
    7109             :                          nXBlock++)
    7110             :                 {
    7111             :                     const int nReqXSize =
    7112          38 :                         std::min(nXSize - iX, poDstDS->m_nBlockXSize);
    7113          38 :                     if (nReqXSize < poDstDS->m_nBlockXSize ||
    7114          30 :                         nReqYSize < poDstDS->m_nBlockYSize)
    7115             :                     {
    7116          16 :                         memset(pBlockBuffer, 0,
    7117          16 :                                static_cast<size_t>(poDstDS->m_nBlockXSize) *
    7118          16 :                                    poDstDS->m_nBlockYSize);
    7119             :                     }
    7120          76 :                     eErr = poSrcMaskBand->RasterIO(
    7121             :                         GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
    7122             :                         nReqXSize, nReqYSize, GDT_UInt8, 1,
    7123          38 :                         poDstDS->m_nBlockXSize, nullptr);
    7124          38 :                     if (eErr == CE_None)
    7125             :                     {
    7126             :                         // Avoid any attempt to load from disk
    7127          38 :                         poDstDS->m_poMaskDS->m_nLoadedBlock = iBlockMask;
    7128             :                         eErr =
    7129          38 :                             poDstDS->m_poMaskDS->GetRasterBand(1)->WriteBlock(
    7130             :                                 nXBlock, nYBlock, pBlockBuffer);
    7131          38 :                         if (eErr == CE_None)
    7132          38 :                             eErr = poDstDS->m_poMaskDS->FlushBlockBuf();
    7133             :                     }
    7134             : 
    7135          38 :                     iBlockMask++;
    7136          76 :                     if (pfnProgress &&
    7137          38 :                         !pfnProgress(static_cast<double>(iBlock + iBlockMask) /
    7138             :                                          nBlocks,
    7139             :                                      nullptr, pProgressData))
    7140             :                     {
    7141           0 :                         eErr = CE_Failure;
    7142             :                     }
    7143             : 
    7144          38 :                     if (poDstDS->m_poMaskDS->m_bWriteError)
    7145           0 :                         eErr = CE_Failure;
    7146             :                 }
    7147             :             }
    7148          45 :         }
    7149             :     }
    7150             :     else
    7151             :     {
    7152         309 :         int iBlock = 0;
    7153         309 :         const int nBlocks = poDstDS->m_nBlocksPerBand;
    7154        7127 :         for (int iY = 0, nYBlock = 0; iY < nYSize && eErr == CE_None;
    7155        6818 :              iY = ((nYSize - iY < poDstDS->m_nBlockYSize)
    7156        6818 :                        ? nYSize
    7157        6584 :                        : iY + poDstDS->m_nBlockYSize),
    7158             :                  nYBlock++)
    7159             :         {
    7160        6818 :             const int nReqYSize = std::min(nYSize - iY, poDstDS->m_nBlockYSize);
    7161       26645 :             for (int iX = 0, nXBlock = 0; iX < nXSize && eErr == CE_None;
    7162       19827 :                  iX = ((nXSize - iX < poDstDS->m_nBlockXSize)
    7163       19827 :                            ? nXSize
    7164       19506 :                            : iX + poDstDS->m_nBlockXSize),
    7165             :                      nXBlock++)
    7166             :             {
    7167             :                 const int nReqXSize =
    7168       19827 :                     std::min(nXSize - iX, poDstDS->m_nBlockXSize);
    7169       19827 :                 if (nReqXSize < poDstDS->m_nBlockXSize ||
    7170       19506 :                     nReqYSize < poDstDS->m_nBlockYSize)
    7171             :                 {
    7172         507 :                     memset(pBlockBuffer, 0,
    7173         507 :                            static_cast<size_t>(poDstDS->m_nBlockXSize) *
    7174         507 :                                poDstDS->m_nBlockYSize * l_nBands *
    7175         507 :                                nDataTypeSize);
    7176             :                 }
    7177             : 
    7178       19827 :                 if (poDstDS->m_bTileInterleave)
    7179             :                 {
    7180         114 :                     eErr = poSrcDS->RasterIO(
    7181             :                         GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
    7182             :                         nReqXSize, nReqYSize, eType, l_nBands, nullptr,
    7183             :                         nDataTypeSize,
    7184          57 :                         static_cast<GSpacing>(nDataTypeSize) *
    7185          57 :                             poDstDS->m_nBlockXSize,
    7186          57 :                         static_cast<GSpacing>(nDataTypeSize) *
    7187          57 :                             poDstDS->m_nBlockXSize * poDstDS->m_nBlockYSize,
    7188             :                         nullptr);
    7189          57 :                     if (eErr == CE_None)
    7190             :                     {
    7191         228 :                         for (int i = 0; eErr == CE_None && i < l_nBands; i++)
    7192             :                         {
    7193         171 :                             eErr = poDstDS->WriteEncodedTileOrStrip(
    7194         171 :                                 iBlock + i * poDstDS->m_nBlocksPerBand,
    7195         171 :                                 pBlockBuffer + static_cast<size_t>(i) *
    7196         171 :                                                    poDstDS->m_nBlockXSize *
    7197         171 :                                                    poDstDS->m_nBlockYSize *
    7198         171 :                                                    nDataTypeSize,
    7199             :                                 false);
    7200             :                         }
    7201             :                     }
    7202             :                 }
    7203       19770 :                 else if (!bIsOddBand)
    7204             :                 {
    7205       39418 :                     eErr = poSrcDS->RasterIO(
    7206             :                         GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
    7207             :                         nReqXSize, nReqYSize, eType, l_nBands, nullptr,
    7208       19709 :                         static_cast<GSpacing>(nDataTypeSize) * l_nBands,
    7209       19709 :                         static_cast<GSpacing>(nDataTypeSize) * l_nBands *
    7210       19709 :                             poDstDS->m_nBlockXSize,
    7211             :                         nDataTypeSize, nullptr);
    7212       19709 :                     if (eErr == CE_None)
    7213             :                     {
    7214       19708 :                         eErr = poDstDS->WriteEncodedTileOrStrip(
    7215             :                             iBlock, pBlockBuffer, false);
    7216             :                     }
    7217             :                 }
    7218             :                 else
    7219             :                 {
    7220             :                     // In the odd bit case, this is a bit messy to ensure
    7221             :                     // the strile gets written synchronously.
    7222             :                     // We load the content of the n-1 bands in the cache,
    7223             :                     // and for the last band we invoke WriteBlock() directly
    7224             :                     // We also force FlushBlockBuf()
    7225         122 :                     std::vector<GDALRasterBlock *> apoLockedBlocks;
    7226          91 :                     for (int i = 0; eErr == CE_None && i < l_nBands - 1; i++)
    7227             :                     {
    7228             :                         auto poBlock =
    7229          30 :                             poDstDS->GetRasterBand(i + 1)->GetLockedBlockRef(
    7230          30 :                                 nXBlock, nYBlock, TRUE);
    7231          30 :                         if (poBlock)
    7232             :                         {
    7233          60 :                             eErr = poSrcDS->GetRasterBand(i + 1)->RasterIO(
    7234             :                                 GF_Read, iX, iY, nReqXSize, nReqYSize,
    7235             :                                 poBlock->GetDataRef(), nReqXSize, nReqYSize,
    7236             :                                 eType, nDataTypeSize,
    7237          30 :                                 static_cast<GSpacing>(nDataTypeSize) *
    7238          30 :                                     poDstDS->m_nBlockXSize,
    7239             :                                 nullptr);
    7240          30 :                             poBlock->MarkDirty();
    7241          30 :                             apoLockedBlocks.emplace_back(poBlock);
    7242             :                         }
    7243             :                         else
    7244             :                         {
    7245           0 :                             eErr = CE_Failure;
    7246             :                         }
    7247             :                     }
    7248          61 :                     if (eErr == CE_None)
    7249             :                     {
    7250         122 :                         eErr = poSrcDS->GetRasterBand(l_nBands)->RasterIO(
    7251             :                             GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
    7252             :                             nReqXSize, nReqYSize, eType, nDataTypeSize,
    7253          61 :                             static_cast<GSpacing>(nDataTypeSize) *
    7254          61 :                                 poDstDS->m_nBlockXSize,
    7255             :                             nullptr);
    7256             :                     }
    7257          61 :                     if (eErr == CE_None)
    7258             :                     {
    7259             :                         // Avoid any attempt to load from disk
    7260          61 :                         poDstDS->m_nLoadedBlock = iBlock;
    7261          61 :                         eErr = poDstDS->GetRasterBand(l_nBands)->WriteBlock(
    7262             :                             nXBlock, nYBlock, pBlockBuffer);
    7263          61 :                         if (eErr == CE_None)
    7264          61 :                             eErr = poDstDS->FlushBlockBuf();
    7265             :                     }
    7266          91 :                     for (auto poBlock : apoLockedBlocks)
    7267             :                     {
    7268          30 :                         poBlock->MarkClean();
    7269          30 :                         poBlock->DropLock();
    7270             :                     }
    7271             :                 }
    7272             : 
    7273       19827 :                 if (eErr == CE_None && poDstDS->m_poMaskDS)
    7274             :                 {
    7275        4664 :                     if (nReqXSize < poDstDS->m_nBlockXSize ||
    7276        4621 :                         nReqYSize < poDstDS->m_nBlockYSize)
    7277             :                     {
    7278          81 :                         memset(pBlockBuffer, 0,
    7279          81 :                                static_cast<size_t>(poDstDS->m_nBlockXSize) *
    7280          81 :                                    poDstDS->m_nBlockYSize);
    7281             :                     }
    7282        9328 :                     eErr = poSrcMaskBand->RasterIO(
    7283             :                         GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
    7284             :                         nReqXSize, nReqYSize, GDT_UInt8, 1,
    7285        4664 :                         poDstDS->m_nBlockXSize, nullptr);
    7286        4664 :                     if (eErr == CE_None)
    7287             :                     {
    7288             :                         // Avoid any attempt to load from disk
    7289        4664 :                         poDstDS->m_poMaskDS->m_nLoadedBlock = iBlock;
    7290             :                         eErr =
    7291        4664 :                             poDstDS->m_poMaskDS->GetRasterBand(1)->WriteBlock(
    7292             :                                 nXBlock, nYBlock, pBlockBuffer);
    7293        4664 :                         if (eErr == CE_None)
    7294        4664 :                             eErr = poDstDS->m_poMaskDS->FlushBlockBuf();
    7295             :                     }
    7296             :                 }
    7297       19827 :                 if (poDstDS->m_bWriteError)
    7298           6 :                     eErr = CE_Failure;
    7299             : 
    7300       19827 :                 iBlock++;
    7301       39654 :                 if (pfnProgress &&
    7302       19827 :                     !pfnProgress(static_cast<double>(iBlock) / nBlocks, nullptr,
    7303             :                                  pProgressData))
    7304             :                 {
    7305           0 :                     eErr = CE_Failure;
    7306             :                 }
    7307             :             }
    7308             :         }
    7309             :     }
    7310             : 
    7311         354 :     poDstDS->FlushCache(false);  // mostly to wait for thread completion
    7312         354 :     VSIFree(pBlockBuffer);
    7313             : 
    7314         354 :     return eErr;
    7315             : }
    7316             : 
    7317             : /************************************************************************/
    7318             : /*                             CreateCopy()                             */
    7319             : /************************************************************************/
    7320             : 
    7321        2167 : GDALDataset *GTiffDataset::CreateCopy(const char *pszFilename,
    7322             :                                       GDALDataset *poSrcDS, int bStrict,
    7323             :                                       CSLConstList papszOptions,
    7324             :                                       GDALProgressFunc pfnProgress,
    7325             :                                       void *pProgressData)
    7326             : 
    7327             : {
    7328        2167 :     if (poSrcDS->GetRasterCount() == 0)
    7329             :     {
    7330           2 :         ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    7331             :                     "Unable to export GeoTIFF files with zero bands.");
    7332           2 :         return nullptr;
    7333             :     }
    7334             : 
    7335        2165 :     GDALRasterBand *const poPBand = poSrcDS->GetRasterBand(1);
    7336        2165 :     GDALDataType eType = poPBand->GetRasterDataType();
    7337             : 
    7338             :     /* -------------------------------------------------------------------- */
    7339             :     /*      Check, whether all bands in input dataset has the same type.    */
    7340             :     /* -------------------------------------------------------------------- */
    7341        2165 :     const int l_nBands = poSrcDS->GetRasterCount();
    7342        5098 :     for (int iBand = 2; iBand <= l_nBands; ++iBand)
    7343             :     {
    7344        2933 :         if (eType != poSrcDS->GetRasterBand(iBand)->GetRasterDataType())
    7345             :         {
    7346           0 :             if (bStrict)
    7347             :             {
    7348           0 :                 ReportError(
    7349             :                     pszFilename, CE_Failure, CPLE_AppDefined,
    7350             :                     "Unable to export GeoTIFF file with different datatypes "
    7351             :                     "per different bands. All bands should have the same "
    7352             :                     "types in TIFF.");
    7353           0 :                 return nullptr;
    7354             :             }
    7355             :             else
    7356             :             {
    7357           0 :                 ReportError(
    7358             :                     pszFilename, CE_Warning, CPLE_AppDefined,
    7359             :                     "Unable to export GeoTIFF file with different datatypes "
    7360             :                     "per different bands. All bands should have the same "
    7361             :                     "types in TIFF.");
    7362             :             }
    7363             :         }
    7364             :     }
    7365             : 
    7366             :     /* -------------------------------------------------------------------- */
    7367             :     /*      Capture the profile.                                            */
    7368             :     /* -------------------------------------------------------------------- */
    7369             :     const GTiffProfile eProfile =
    7370        2165 :         GetProfile(CSLFetchNameValue(papszOptions, "PROFILE"));
    7371             : 
    7372        2165 :     const bool bGeoTIFF = eProfile != GTiffProfile::BASELINE;
    7373             : 
    7374             :     /* -------------------------------------------------------------------- */
    7375             :     /*      Special handling for NBITS.  Copy from band metadata if found.  */
    7376             :     /* -------------------------------------------------------------------- */
    7377        2165 :     char **papszCreateOptions = CSLDuplicate(papszOptions);
    7378             : 
    7379        2165 :     if (poPBand->GetMetadataItem("NBITS", "IMAGE_STRUCTURE") != nullptr &&
    7380        2182 :         atoi(poPBand->GetMetadataItem("NBITS", "IMAGE_STRUCTURE")) > 0 &&
    7381          17 :         CSLFetchNameValue(papszCreateOptions, "NBITS") == nullptr)
    7382             :     {
    7383           3 :         papszCreateOptions = CSLSetNameValue(
    7384             :             papszCreateOptions, "NBITS",
    7385           3 :             poPBand->GetMetadataItem("NBITS", "IMAGE_STRUCTURE"));
    7386             :     }
    7387             : 
    7388        2165 :     if (CSLFetchNameValue(papszOptions, "PIXELTYPE") == nullptr &&
    7389             :         eType == GDT_UInt8)
    7390             :     {
    7391        1795 :         poPBand->EnablePixelTypeSignedByteWarning(false);
    7392             :         const char *pszPixelType =
    7393        1795 :             poPBand->GetMetadataItem("PIXELTYPE", "IMAGE_STRUCTURE");
    7394        1795 :         poPBand->EnablePixelTypeSignedByteWarning(true);
    7395        1795 :         if (pszPixelType)
    7396             :         {
    7397           1 :             papszCreateOptions =
    7398           1 :                 CSLSetNameValue(papszCreateOptions, "PIXELTYPE", pszPixelType);
    7399             :         }
    7400             :     }
    7401             : 
    7402             :     /* -------------------------------------------------------------------- */
    7403             :     /*      Color profile.  Copy from band metadata if found.              */
    7404             :     /* -------------------------------------------------------------------- */
    7405        2165 :     if (bGeoTIFF)
    7406             :     {
    7407        2148 :         const char *pszOptionsMD[] = {"SOURCE_ICC_PROFILE",
    7408             :                                       "SOURCE_PRIMARIES_RED",
    7409             :                                       "SOURCE_PRIMARIES_GREEN",
    7410             :                                       "SOURCE_PRIMARIES_BLUE",
    7411             :                                       "SOURCE_WHITEPOINT",
    7412             :                                       "TIFFTAG_TRANSFERFUNCTION_RED",
    7413             :                                       "TIFFTAG_TRANSFERFUNCTION_GREEN",
    7414             :                                       "TIFFTAG_TRANSFERFUNCTION_BLUE",
    7415             :                                       "TIFFTAG_TRANSFERRANGE_BLACK",
    7416             :                                       "TIFFTAG_TRANSFERRANGE_WHITE",
    7417             :                                       nullptr};
    7418             : 
    7419             :         // Copy all the tags.  Options will override tags in the source.
    7420        2148 :         int i = 0;
    7421       23608 :         while (pszOptionsMD[i] != nullptr)
    7422             :         {
    7423             :             char const *pszMD =
    7424       21462 :                 CSLFetchNameValue(papszOptions, pszOptionsMD[i]);
    7425       21462 :             if (pszMD == nullptr)
    7426             :                 pszMD =
    7427       21454 :                     poSrcDS->GetMetadataItem(pszOptionsMD[i], "COLOR_PROFILE");
    7428             : 
    7429       21462 :             if ((pszMD != nullptr) && !EQUAL(pszMD, ""))
    7430             :             {
    7431          16 :                 papszCreateOptions =
    7432          16 :                     CSLSetNameValue(papszCreateOptions, pszOptionsMD[i], pszMD);
    7433             : 
    7434             :                 // If an ICC profile exists, other tags are not needed.
    7435          16 :                 if (EQUAL(pszOptionsMD[i], "SOURCE_ICC_PROFILE"))
    7436           2 :                     break;
    7437             :             }
    7438             : 
    7439       21460 :             ++i;
    7440             :         }
    7441             :     }
    7442             : 
    7443        2165 :     double dfExtraSpaceForOverviews = 0;
    7444             :     const bool bCopySrcOverviews =
    7445        2165 :         CPLFetchBool(papszCreateOptions, "COPY_SRC_OVERVIEWS", false);
    7446        2165 :     std::unique_ptr<GDALDataset> poOvrDS;
    7447        2165 :     int nSrcOverviews = 0;
    7448        2165 :     if (bCopySrcOverviews)
    7449             :     {
    7450             :         const char *pszOvrDS =
    7451         233 :             CSLFetchNameValue(papszCreateOptions, "@OVERVIEW_DATASET");
    7452         233 :         if (pszOvrDS)
    7453             :         {
    7454             :             // Empty string is used by COG driver to indicate that we want
    7455             :             // to ignore source overviews.
    7456          39 :             if (!EQUAL(pszOvrDS, ""))
    7457             :             {
    7458          37 :                 poOvrDS.reset(GDALDataset::Open(pszOvrDS));
    7459          37 :                 if (!poOvrDS)
    7460             :                 {
    7461           0 :                     CSLDestroy(papszCreateOptions);
    7462           0 :                     return nullptr;
    7463             :                 }
    7464          37 :                 if (poOvrDS->GetRasterCount() != l_nBands)
    7465             :                 {
    7466           0 :                     CSLDestroy(papszCreateOptions);
    7467           0 :                     return nullptr;
    7468             :                 }
    7469          37 :                 nSrcOverviews =
    7470          37 :                     poOvrDS->GetRasterBand(1)->GetOverviewCount() + 1;
    7471             :             }
    7472             :         }
    7473             :         else
    7474             :         {
    7475         194 :             nSrcOverviews = poSrcDS->GetRasterBand(1)->GetOverviewCount();
    7476             :         }
    7477             : 
    7478             :         // Limit number of overviews if specified
    7479             :         const char *pszOverviewCount =
    7480         233 :             CSLFetchNameValue(papszCreateOptions, "@OVERVIEW_COUNT");
    7481         233 :         if (pszOverviewCount)
    7482           8 :             nSrcOverviews =
    7483           8 :                 std::max(0, std::min(nSrcOverviews, atoi(pszOverviewCount)));
    7484             : 
    7485         233 :         if (nSrcOverviews)
    7486             :         {
    7487         208 :             for (int j = 1; j <= l_nBands; ++j)
    7488             :             {
    7489             :                 const int nOtherBandOverviewCount =
    7490         136 :                     poOvrDS ? poOvrDS->GetRasterBand(j)->GetOverviewCount() + 1
    7491         200 :                             : poSrcDS->GetRasterBand(j)->GetOverviewCount();
    7492         136 :                 if (nOtherBandOverviewCount < nSrcOverviews)
    7493             :                 {
    7494           1 :                     ReportError(
    7495             :                         pszFilename, CE_Failure, CPLE_NotSupported,
    7496             :                         "COPY_SRC_OVERVIEWS cannot be used when the bands have "
    7497             :                         "not the same number of overview levels.");
    7498           1 :                     CSLDestroy(papszCreateOptions);
    7499           1 :                     return nullptr;
    7500             :                 }
    7501         395 :                 for (int i = 0; i < nSrcOverviews; ++i)
    7502             :                 {
    7503             :                     GDALRasterBand *poOvrBand =
    7504             :                         poOvrDS
    7505         361 :                             ? (i == 0 ? poOvrDS->GetRasterBand(j)
    7506         198 :                                       : poOvrDS->GetRasterBand(j)->GetOverview(
    7507          99 :                                             i - 1))
    7508         353 :                             : poSrcDS->GetRasterBand(j)->GetOverview(i);
    7509         262 :                     if (poOvrBand == nullptr)
    7510             :                     {
    7511           1 :                         ReportError(
    7512             :                             pszFilename, CE_Failure, CPLE_NotSupported,
    7513             :                             "COPY_SRC_OVERVIEWS cannot be used when one "
    7514             :                             "overview band is NULL.");
    7515           1 :                         CSLDestroy(papszCreateOptions);
    7516           1 :                         return nullptr;
    7517             :                     }
    7518             :                     GDALRasterBand *poOvrFirstBand =
    7519             :                         poOvrDS
    7520         360 :                             ? (i == 0 ? poOvrDS->GetRasterBand(1)
    7521         198 :                                       : poOvrDS->GetRasterBand(1)->GetOverview(
    7522          99 :                                             i - 1))
    7523         351 :                             : poSrcDS->GetRasterBand(1)->GetOverview(i);
    7524         521 :                     if (poOvrBand->GetXSize() != poOvrFirstBand->GetXSize() ||
    7525         260 :                         poOvrBand->GetYSize() != poOvrFirstBand->GetYSize())
    7526             :                     {
    7527           1 :                         ReportError(
    7528             :                             pszFilename, CE_Failure, CPLE_NotSupported,
    7529             :                             "COPY_SRC_OVERVIEWS cannot be used when the "
    7530             :                             "overview bands have not the same dimensions "
    7531             :                             "among bands.");
    7532           1 :                         CSLDestroy(papszCreateOptions);
    7533           1 :                         return nullptr;
    7534             :                     }
    7535             :                 }
    7536             :             }
    7537             : 
    7538         205 :             for (int i = 0; i < nSrcOverviews; ++i)
    7539             :             {
    7540             :                 GDALRasterBand *poOvrFirstBand =
    7541             :                     poOvrDS
    7542         211 :                         ? (i == 0
    7543          78 :                                ? poOvrDS->GetRasterBand(1)
    7544          41 :                                : poOvrDS->GetRasterBand(1)->GetOverview(i - 1))
    7545         188 :                         : poSrcDS->GetRasterBand(1)->GetOverview(i);
    7546         133 :                 dfExtraSpaceForOverviews +=
    7547         133 :                     static_cast<double>(poOvrFirstBand->GetXSize()) *
    7548         133 :                     poOvrFirstBand->GetYSize();
    7549             :             }
    7550          72 :             dfExtraSpaceForOverviews *=
    7551          72 :                 l_nBands * GDALGetDataTypeSizeBytes(eType);
    7552             :         }
    7553             :         else
    7554             :         {
    7555         158 :             CPLDebug("GTiff", "No source overviews to copy");
    7556             :         }
    7557             :     }
    7558             : 
    7559             : /* -------------------------------------------------------------------- */
    7560             : /*      Should we use optimized way of copying from an input JPEG       */
    7561             : /*      dataset?                                                        */
    7562             : /* -------------------------------------------------------------------- */
    7563             : 
    7564             : // TODO(schwehr): Refactor bDirectCopyFromJPEG to be a const.
    7565             : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
    7566        2162 :     bool bDirectCopyFromJPEG = false;
    7567             : #endif
    7568             : 
    7569             :     // Note: JPEG_DIRECT_COPY is not defined by default, because it is mainly
    7570             :     // useful for debugging purposes.
    7571             : #ifdef JPEG_DIRECT_COPY
    7572             :     if (CPLFetchBool(papszCreateOptions, "JPEG_DIRECT_COPY", false) &&
    7573             :         GTIFF_CanDirectCopyFromJPEG(poSrcDS, papszCreateOptions))
    7574             :     {
    7575             :         CPLDebug("GTiff", "Using special direct copy mode from a JPEG dataset");
    7576             : 
    7577             :         bDirectCopyFromJPEG = true;
    7578             :     }
    7579             : #endif
    7580             : 
    7581             : #ifdef HAVE_LIBJPEG
    7582        2162 :     bool bCopyFromJPEG = false;
    7583             : 
    7584             :     // When CreateCopy'ing() from a JPEG dataset, and asking for COMPRESS=JPEG,
    7585             :     // use DCT coefficients (unless other options are incompatible, like
    7586             :     // strip/tile dimensions, specifying JPEG_QUALITY option, incompatible
    7587             :     // PHOTOMETRIC with the source colorspace, etc.) to avoid the lossy steps
    7588             :     // involved by decompression/recompression.
    7589        4324 :     if (!bDirectCopyFromJPEG &&
    7590        2162 :         GTIFF_CanCopyFromJPEG(poSrcDS, papszCreateOptions))
    7591             :     {
    7592          12 :         CPLDebug("GTiff", "Using special copy mode from a JPEG dataset");
    7593             : 
    7594          12 :         bCopyFromJPEG = true;
    7595             :     }
    7596             : #endif
    7597             : 
    7598             :     /* -------------------------------------------------------------------- */
    7599             :     /*      If the source is RGB, then set the PHOTOMETRIC=RGB value        */
    7600             :     /* -------------------------------------------------------------------- */
    7601             : 
    7602             :     const bool bForcePhotometric =
    7603        2162 :         CSLFetchNameValue(papszOptions, "PHOTOMETRIC") != nullptr;
    7604             : 
    7605        1231 :     if (l_nBands >= 3 && !bForcePhotometric &&
    7606             : #ifdef HAVE_LIBJPEG
    7607        1193 :         !bCopyFromJPEG &&
    7608             : #endif
    7609        1187 :         poSrcDS->GetRasterBand(1)->GetColorInterpretation() == GCI_RedBand &&
    7610        4468 :         poSrcDS->GetRasterBand(2)->GetColorInterpretation() == GCI_GreenBand &&
    7611        1075 :         poSrcDS->GetRasterBand(3)->GetColorInterpretation() == GCI_BlueBand)
    7612             :     {
    7613        1069 :         papszCreateOptions =
    7614        1069 :             CSLSetNameValue(papszCreateOptions, "PHOTOMETRIC", "RGB");
    7615             :     }
    7616             : 
    7617             :     /* -------------------------------------------------------------------- */
    7618             :     /*      Create the file.                                                */
    7619             :     /* -------------------------------------------------------------------- */
    7620        2162 :     VSILFILE *l_fpL = nullptr;
    7621        4324 :     CPLString l_osTmpFilename;
    7622             : 
    7623        2162 :     const int nXSize = poSrcDS->GetRasterXSize();
    7624        2162 :     const int nYSize = poSrcDS->GetRasterYSize();
    7625             : 
    7626             :     const int nColorTableMultiplier = std::max(
    7627        4324 :         1,
    7628        4324 :         std::min(257,
    7629        2162 :                  atoi(CSLFetchNameValueDef(
    7630             :                      papszOptions, "COLOR_TABLE_MULTIPLIER",
    7631        2162 :                      CPLSPrintf("%d", DEFAULT_COLOR_TABLE_MULTIPLIER_257)))));
    7632             : 
    7633        2162 :     bool bTileInterleaving = false;
    7634        2162 :     TIFF *l_hTIFF = CreateLL(pszFilename, nXSize, nYSize, l_nBands, eType,
    7635             :                              dfExtraSpaceForOverviews, nColorTableMultiplier,
    7636             :                              papszCreateOptions, &l_fpL, l_osTmpFilename,
    7637             :                              /* bCreateCopy = */ true, bTileInterleaving);
    7638        2162 :     const bool bStreaming = !l_osTmpFilename.empty();
    7639             : 
    7640        2162 :     CSLDestroy(papszCreateOptions);
    7641        2162 :     papszCreateOptions = nullptr;
    7642             : 
    7643        2162 :     if (l_hTIFF == nullptr)
    7644             :     {
    7645          18 :         if (bStreaming)
    7646           0 :             VSIUnlink(l_osTmpFilename);
    7647          18 :         return nullptr;
    7648             :     }
    7649             : 
    7650        2144 :     uint16_t l_nPlanarConfig = 0;
    7651        2144 :     TIFFGetField(l_hTIFF, TIFFTAG_PLANARCONFIG, &l_nPlanarConfig);
    7652             : 
    7653        2144 :     uint16_t l_nCompression = 0;
    7654             : 
    7655        2144 :     if (!TIFFGetField(l_hTIFF, TIFFTAG_COMPRESSION, &(l_nCompression)))
    7656           0 :         l_nCompression = COMPRESSION_NONE;
    7657             : 
    7658             :     /* -------------------------------------------------------------------- */
    7659             :     /*      Set the alpha channel if we find one.                           */
    7660             :     /* -------------------------------------------------------------------- */
    7661        2144 :     uint16_t *extraSamples = nullptr;
    7662        2144 :     uint16_t nExtraSamples = 0;
    7663        2144 :     if (TIFFGetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, &nExtraSamples,
    7664        2409 :                      &extraSamples) &&
    7665         265 :         nExtraSamples > 0)
    7666             :     {
    7667             :         // We need to allocate a new array as (current) libtiff
    7668             :         // versions will not like that we reuse the array we got from
    7669             :         // TIFFGetField().
    7670             :         uint16_t *pasNewExtraSamples = static_cast<uint16_t *>(
    7671         265 :             CPLMalloc(nExtraSamples * sizeof(uint16_t)));
    7672         265 :         memcpy(pasNewExtraSamples, extraSamples,
    7673         265 :                nExtraSamples * sizeof(uint16_t));
    7674         265 :         const char *pszAlpha = CPLGetConfigOption(
    7675             :             "GTIFF_ALPHA", CSLFetchNameValue(papszOptions, "ALPHA"));
    7676             :         const uint16_t nAlpha =
    7677         265 :             GTiffGetAlphaValue(pszAlpha, DEFAULT_ALPHA_TYPE);
    7678         265 :         const int nBaseSamples = l_nBands - nExtraSamples;
    7679         895 :         for (int iExtraBand = nBaseSamples + 1; iExtraBand <= l_nBands;
    7680             :              iExtraBand++)
    7681             :         {
    7682         630 :             if (poSrcDS->GetRasterBand(iExtraBand)->GetColorInterpretation() ==
    7683             :                 GCI_AlphaBand)
    7684             :             {
    7685         145 :                 pasNewExtraSamples[iExtraBand - nBaseSamples - 1] = nAlpha;
    7686         145 :                 if (!pszAlpha)
    7687             :                 {
    7688             :                     // Use the ALPHA metadata item from the source band, when
    7689             :                     // present, if no explicit ALPHA creation option
    7690         286 :                     pasNewExtraSamples[iExtraBand - nBaseSamples - 1] =
    7691         143 :                         GTiffGetAlphaValue(
    7692         143 :                             poSrcDS->GetRasterBand(iExtraBand)
    7693         143 :                                 ->GetMetadataItem("ALPHA", "IMAGE_STRUCTURE"),
    7694             :                             nAlpha);
    7695             :                 }
    7696             :             }
    7697             :         }
    7698         265 :         TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, nExtraSamples,
    7699             :                      pasNewExtraSamples);
    7700             : 
    7701         265 :         CPLFree(pasNewExtraSamples);
    7702             :     }
    7703             : 
    7704             :     /* -------------------------------------------------------------------- */
    7705             :     /*      If the output is jpeg compressed, and the input is RGB make     */
    7706             :     /*      sure we note that.                                              */
    7707             :     /* -------------------------------------------------------------------- */
    7708             : 
    7709        2144 :     if (l_nCompression == COMPRESSION_JPEG)
    7710             :     {
    7711         134 :         if (l_nBands >= 3 &&
    7712          58 :             (poSrcDS->GetRasterBand(1)->GetColorInterpretation() ==
    7713           0 :              GCI_YCbCr_YBand) &&
    7714           0 :             (poSrcDS->GetRasterBand(2)->GetColorInterpretation() ==
    7715         134 :              GCI_YCbCr_CbBand) &&
    7716           0 :             (poSrcDS->GetRasterBand(3)->GetColorInterpretation() ==
    7717             :              GCI_YCbCr_CrBand))
    7718             :         {
    7719             :             // Do nothing.
    7720             :         }
    7721             :         else
    7722             :         {
    7723             :             // Assume RGB if it is not explicitly YCbCr.
    7724          76 :             CPLDebug("GTiff", "Setting JPEGCOLORMODE_RGB");
    7725          76 :             TIFFSetField(l_hTIFF, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
    7726             :         }
    7727             :     }
    7728             : 
    7729             :     /* -------------------------------------------------------------------- */
    7730             :     /*      Does the source image consist of one band, with a palette?      */
    7731             :     /*      If so, copy over.                                               */
    7732             :     /* -------------------------------------------------------------------- */
    7733        1317 :     if ((l_nBands == 1 || l_nBands == 2) &&
    7734        3461 :         poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr &&
    7735             :         eType == GDT_UInt8)
    7736             :     {
    7737          21 :         unsigned short anTRed[256] = {0};
    7738          21 :         unsigned short anTGreen[256] = {0};
    7739          21 :         unsigned short anTBlue[256] = {0};
    7740          21 :         GDALColorTable *poCT = poSrcDS->GetRasterBand(1)->GetColorTable();
    7741             : 
    7742        5397 :         for (int iColor = 0; iColor < 256; ++iColor)
    7743             :         {
    7744        5376 :             if (iColor < poCT->GetColorEntryCount())
    7745             :             {
    7746        4241 :                 GDALColorEntry sRGB = {0, 0, 0, 0};
    7747             : 
    7748        4241 :                 poCT->GetColorEntryAsRGB(iColor, &sRGB);
    7749             : 
    7750        8482 :                 anTRed[iColor] = GTiffDataset::ClampCTEntry(
    7751        4241 :                     iColor, 1, sRGB.c1, nColorTableMultiplier);
    7752        8482 :                 anTGreen[iColor] = GTiffDataset::ClampCTEntry(
    7753        4241 :                     iColor, 2, sRGB.c2, nColorTableMultiplier);
    7754        4241 :                 anTBlue[iColor] = GTiffDataset::ClampCTEntry(
    7755        4241 :                     iColor, 3, sRGB.c3, nColorTableMultiplier);
    7756             :             }
    7757             :             else
    7758             :             {
    7759        1135 :                 anTRed[iColor] = 0;
    7760        1135 :                 anTGreen[iColor] = 0;
    7761        1135 :                 anTBlue[iColor] = 0;
    7762             :             }
    7763             :         }
    7764             : 
    7765          21 :         if (!bForcePhotometric)
    7766          21 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
    7767          21 :         TIFFSetField(l_hTIFF, TIFFTAG_COLORMAP, anTRed, anTGreen, anTBlue);
    7768             :     }
    7769        1316 :     else if ((l_nBands == 1 || l_nBands == 2) &&
    7770        3439 :              poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr &&
    7771             :              eType == GDT_UInt16)
    7772             :     {
    7773             :         unsigned short *panTRed = static_cast<unsigned short *>(
    7774           1 :             CPLMalloc(65536 * sizeof(unsigned short)));
    7775             :         unsigned short *panTGreen = static_cast<unsigned short *>(
    7776           1 :             CPLMalloc(65536 * sizeof(unsigned short)));
    7777             :         unsigned short *panTBlue = static_cast<unsigned short *>(
    7778           1 :             CPLMalloc(65536 * sizeof(unsigned short)));
    7779             : 
    7780           1 :         GDALColorTable *poCT = poSrcDS->GetRasterBand(1)->GetColorTable();
    7781             : 
    7782       65537 :         for (int iColor = 0; iColor < 65536; ++iColor)
    7783             :         {
    7784       65536 :             if (iColor < poCT->GetColorEntryCount())
    7785             :             {
    7786       65536 :                 GDALColorEntry sRGB = {0, 0, 0, 0};
    7787             : 
    7788       65536 :                 poCT->GetColorEntryAsRGB(iColor, &sRGB);
    7789             : 
    7790      131072 :                 panTRed[iColor] = GTiffDataset::ClampCTEntry(
    7791       65536 :                     iColor, 1, sRGB.c1, nColorTableMultiplier);
    7792      131072 :                 panTGreen[iColor] = GTiffDataset::ClampCTEntry(
    7793       65536 :                     iColor, 2, sRGB.c2, nColorTableMultiplier);
    7794       65536 :                 panTBlue[iColor] = GTiffDataset::ClampCTEntry(
    7795       65536 :                     iColor, 3, sRGB.c3, nColorTableMultiplier);
    7796             :             }
    7797             :             else
    7798             :             {
    7799           0 :                 panTRed[iColor] = 0;
    7800           0 :                 panTGreen[iColor] = 0;
    7801           0 :                 panTBlue[iColor] = 0;
    7802             :             }
    7803             :         }
    7804             : 
    7805           1 :         if (!bForcePhotometric)
    7806           1 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
    7807           1 :         TIFFSetField(l_hTIFF, TIFFTAG_COLORMAP, panTRed, panTGreen, panTBlue);
    7808             : 
    7809           1 :         CPLFree(panTRed);
    7810           1 :         CPLFree(panTGreen);
    7811           1 :         CPLFree(panTBlue);
    7812             :     }
    7813        2122 :     else if (poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr)
    7814           1 :         ReportError(
    7815             :             pszFilename, CE_Failure, CPLE_AppDefined,
    7816             :             "Unable to export color table to GeoTIFF file.  Color tables "
    7817             :             "can only be written to 1 band or 2 bands Byte or "
    7818             :             "UInt16 GeoTIFF files.");
    7819             : 
    7820        2144 :     if (l_nCompression == COMPRESSION_JPEG)
    7821             :     {
    7822          76 :         uint16_t l_nPhotometric = 0;
    7823          76 :         TIFFGetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, &l_nPhotometric);
    7824             :         // Check done in tif_jpeg.c later, but not with a very clear error
    7825             :         // message
    7826          76 :         if (l_nPhotometric == PHOTOMETRIC_PALETTE)
    7827             :         {
    7828           1 :             ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    7829             :                         "JPEG compression not supported with paletted image");
    7830           1 :             XTIFFClose(l_hTIFF);
    7831           1 :             VSIUnlink(l_osTmpFilename);
    7832           1 :             CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    7833           1 :             return nullptr;
    7834             :         }
    7835             :     }
    7836             : 
    7837        2230 :     if (l_nBands == 2 &&
    7838        2143 :         poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr &&
    7839           0 :         (eType == GDT_UInt8 || eType == GDT_UInt16))
    7840             :     {
    7841           1 :         uint16_t v[1] = {EXTRASAMPLE_UNASSALPHA};
    7842             : 
    7843           1 :         TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, 1, v);
    7844             :     }
    7845             : 
    7846        2143 :     const int nMaskFlags = poSrcDS->GetRasterBand(1)->GetMaskFlags();
    7847        2143 :     bool bCreateMask = false;
    7848        4286 :     CPLString osHiddenStructuralMD;
    7849             :     const char *pszInterleave =
    7850        2143 :         CSLFetchNameValueDef(papszOptions, "INTERLEAVE", "PIXEL");
    7851        2370 :     if (bCopySrcOverviews &&
    7852         227 :         CPLTestBool(CSLFetchNameValueDef(papszOptions, "TILED", "NO")))
    7853             :     {
    7854         215 :         osHiddenStructuralMD += "LAYOUT=IFDS_BEFORE_DATA\n";
    7855         215 :         osHiddenStructuralMD += "BLOCK_ORDER=ROW_MAJOR\n";
    7856         215 :         osHiddenStructuralMD += "BLOCK_LEADER=SIZE_AS_UINT4\n";
    7857         215 :         osHiddenStructuralMD += "BLOCK_TRAILER=LAST_4_BYTES_REPEATED\n";
    7858         215 :         if (l_nBands > 1 && !EQUAL(pszInterleave, "PIXEL"))
    7859             :         {
    7860          21 :             osHiddenStructuralMD += "INTERLEAVE=";
    7861          21 :             osHiddenStructuralMD += CPLString(pszInterleave).toupper();
    7862          21 :             osHiddenStructuralMD += "\n";
    7863             :         }
    7864             :         osHiddenStructuralMD +=
    7865         215 :             "KNOWN_INCOMPATIBLE_EDITION=NO\n ";  // Final space intended, so
    7866             :                                                  // this can be replaced by YES
    7867             :     }
    7868        2143 :     if (!(nMaskFlags & (GMF_ALL_VALID | GMF_ALPHA | GMF_NODATA)) &&
    7869          42 :         (nMaskFlags & GMF_PER_DATASET) && !bStreaming)
    7870             :     {
    7871          38 :         bCreateMask = true;
    7872          38 :         if (GTiffDataset::MustCreateInternalMask() &&
    7873          38 :             !osHiddenStructuralMD.empty() && EQUAL(pszInterleave, "PIXEL"))
    7874             :         {
    7875          21 :             osHiddenStructuralMD += "MASK_INTERLEAVED_WITH_IMAGERY=YES\n";
    7876             :         }
    7877             :     }
    7878        2358 :     if (!osHiddenStructuralMD.empty() &&
    7879         215 :         CPLTestBool(CPLGetConfigOption("GTIFF_WRITE_COG_GHOST_AREA", "YES")))
    7880             :     {
    7881         214 :         const int nHiddenMDSize = static_cast<int>(osHiddenStructuralMD.size());
    7882             :         osHiddenStructuralMD =
    7883         214 :             CPLOPrintf("GDAL_STRUCTURAL_METADATA_SIZE=%06d bytes\n",
    7884         428 :                        nHiddenMDSize) +
    7885         214 :             osHiddenStructuralMD;
    7886         214 :         VSI_TIFFWrite(l_hTIFF, osHiddenStructuralMD.c_str(),
    7887             :                       osHiddenStructuralMD.size());
    7888             :     }
    7889             : 
    7890             :     // FIXME? libtiff writes extended tags in the order they are specified
    7891             :     // and not in increasing order.
    7892             : 
    7893             :     /* -------------------------------------------------------------------- */
    7894             :     /*      Transfer some TIFF specific metadata, if available.             */
    7895             :     /*      The return value will tell us if we need to try again later with*/
    7896             :     /*      PAM because the profile doesn't allow to write some metadata    */
    7897             :     /*      as TIFF tag                                                     */
    7898             :     /* -------------------------------------------------------------------- */
    7899        2143 :     const bool bHasWrittenMDInGeotiffTAG = GTiffDataset::WriteMetadata(
    7900             :         poSrcDS, l_hTIFF, false, eProfile, pszFilename, papszOptions);
    7901             : 
    7902             :     /* -------------------------------------------------------------------- */
    7903             :     /*      Write NoData value, if exist.                                   */
    7904             :     /* -------------------------------------------------------------------- */
    7905        2143 :     if (eProfile == GTiffProfile::GDALGEOTIFF)
    7906             :     {
    7907        2122 :         int bSuccess = FALSE;
    7908        2122 :         GDALRasterBand *poFirstBand = poSrcDS->GetRasterBand(1);
    7909        2122 :         if (poFirstBand->GetRasterDataType() == GDT_Int64)
    7910             :         {
    7911           4 :             const auto nNoData = poFirstBand->GetNoDataValueAsInt64(&bSuccess);
    7912           4 :             if (bSuccess)
    7913           1 :                 GTiffDataset::WriteNoDataValue(l_hTIFF, nNoData);
    7914             :         }
    7915        2118 :         else if (poFirstBand->GetRasterDataType() == GDT_UInt64)
    7916             :         {
    7917           4 :             const auto nNoData = poFirstBand->GetNoDataValueAsUInt64(&bSuccess);
    7918           4 :             if (bSuccess)
    7919           1 :                 GTiffDataset::WriteNoDataValue(l_hTIFF, nNoData);
    7920             :         }
    7921             :         else
    7922             :         {
    7923        2114 :             const auto dfNoData = poFirstBand->GetNoDataValue(&bSuccess);
    7924        2114 :             if (bSuccess)
    7925         145 :                 GTiffDataset::WriteNoDataValue(l_hTIFF, dfNoData);
    7926             :         }
    7927             :     }
    7928             : 
    7929             :     /* -------------------------------------------------------------------- */
    7930             :     /*      Are we addressing PixelIsPoint mode?                            */
    7931             :     /* -------------------------------------------------------------------- */
    7932        2143 :     bool bPixelIsPoint = false;
    7933        2143 :     bool bPointGeoIgnore = false;
    7934             : 
    7935        3598 :     if (poSrcDS->GetMetadataItem(GDALMD_AREA_OR_POINT) &&
    7936        1455 :         EQUAL(poSrcDS->GetMetadataItem(GDALMD_AREA_OR_POINT), GDALMD_AOP_POINT))
    7937             :     {
    7938          10 :         bPixelIsPoint = true;
    7939             :         bPointGeoIgnore =
    7940          10 :             CPLTestBool(CPLGetConfigOption("GTIFF_POINT_GEO_IGNORE", "FALSE"));
    7941             :     }
    7942             : 
    7943             :     /* -------------------------------------------------------------------- */
    7944             :     /*      Write affine transform if it is meaningful.                     */
    7945             :     /* -------------------------------------------------------------------- */
    7946        2143 :     const OGRSpatialReference *l_poSRS = nullptr;
    7947        2143 :     GDALGeoTransform l_gt;
    7948        2143 :     if (poSrcDS->GetGeoTransform(l_gt) == CE_None)
    7949             :     {
    7950        1704 :         if (bGeoTIFF)
    7951             :         {
    7952        1699 :             l_poSRS = poSrcDS->GetSpatialRef();
    7953             : 
    7954        1699 :             if (l_gt.xrot == 0.0 && l_gt.yrot == 0.0 && l_gt.yscale < 0.0)
    7955             :             {
    7956        1691 :                 double dfOffset = 0.0;
    7957             :                 {
    7958             :                     // In the case the SRS has a vertical component and we have
    7959             :                     // a single band, encode its scale/offset in the GeoTIFF
    7960             :                     // tags
    7961        1691 :                     int bHasScale = FALSE;
    7962             :                     double dfScale =
    7963        1691 :                         poSrcDS->GetRasterBand(1)->GetScale(&bHasScale);
    7964        1691 :                     int bHasOffset = FALSE;
    7965             :                     dfOffset =
    7966        1691 :                         poSrcDS->GetRasterBand(1)->GetOffset(&bHasOffset);
    7967             :                     const bool bApplyScaleOffset =
    7968        1695 :                         l_poSRS && l_poSRS->IsVertical() &&
    7969           4 :                         poSrcDS->GetRasterCount() == 1;
    7970        1691 :                     if (bApplyScaleOffset && !bHasScale)
    7971           0 :                         dfScale = 1.0;
    7972        1691 :                     if (!bApplyScaleOffset || !bHasOffset)
    7973        1687 :                         dfOffset = 0.0;
    7974             :                     const double adfPixelScale[3] = {
    7975        1691 :                         l_gt.xscale, fabs(l_gt.yscale),
    7976        1691 :                         bApplyScaleOffset ? dfScale : 0.0};
    7977             : 
    7978        1691 :                     TIFFSetField(l_hTIFF, TIFFTAG_GEOPIXELSCALE, 3,
    7979             :                                  adfPixelScale);
    7980             :                 }
    7981             : 
    7982        1691 :                 double adfTiePoints[6] = {0.0,        0.0,        0.0,
    7983        1691 :                                           l_gt.xorig, l_gt.yorig, dfOffset};
    7984             : 
    7985        1691 :                 if (bPixelIsPoint && !bPointGeoIgnore)
    7986             :                 {
    7987           6 :                     adfTiePoints[3] += l_gt.xscale * 0.5 + l_gt.xrot * 0.5;
    7988           6 :                     adfTiePoints[4] += l_gt.yrot * 0.5 + l_gt.yscale * 0.5;
    7989             :                 }
    7990             : 
    7991        1691 :                 TIFFSetField(l_hTIFF, TIFFTAG_GEOTIEPOINTS, 6, adfTiePoints);
    7992             :             }
    7993             :             else
    7994             :             {
    7995           8 :                 double adfMatrix[16] = {0.0};
    7996             : 
    7997           8 :                 adfMatrix[0] = l_gt.xscale;
    7998           8 :                 adfMatrix[1] = l_gt.xrot;
    7999           8 :                 adfMatrix[3] = l_gt.xorig;
    8000           8 :                 adfMatrix[4] = l_gt.yrot;
    8001           8 :                 adfMatrix[5] = l_gt.yscale;
    8002           8 :                 adfMatrix[7] = l_gt.yorig;
    8003           8 :                 adfMatrix[15] = 1.0;
    8004             : 
    8005           8 :                 if (bPixelIsPoint && !bPointGeoIgnore)
    8006             :                 {
    8007           0 :                     adfMatrix[3] += l_gt.xscale * 0.5 + l_gt.xrot * 0.5;
    8008           0 :                     adfMatrix[7] += l_gt.yrot * 0.5 + l_gt.yscale * 0.5;
    8009             :                 }
    8010             : 
    8011           8 :                 TIFFSetField(l_hTIFF, TIFFTAG_GEOTRANSMATRIX, 16, adfMatrix);
    8012             :             }
    8013             :         }
    8014             : 
    8015             :         /* --------------------------------------------------------------------
    8016             :          */
    8017             :         /*      Do we need a TFW file? */
    8018             :         /* --------------------------------------------------------------------
    8019             :          */
    8020        1704 :         if (CPLFetchBool(papszOptions, "TFW", false))
    8021           2 :             GDALWriteWorldFile(pszFilename, "tfw", l_gt.data());
    8022        1702 :         else if (CPLFetchBool(papszOptions, "WORLDFILE", false))
    8023           1 :             GDALWriteWorldFile(pszFilename, "wld", l_gt.data());
    8024             :     }
    8025             : 
    8026             :     /* -------------------------------------------------------------------- */
    8027             :     /*      Otherwise write tiepoints if they are available.                */
    8028             :     /* -------------------------------------------------------------------- */
    8029         439 :     else if (poSrcDS->GetGCPCount() > 0 && bGeoTIFF)
    8030             :     {
    8031          11 :         const GDAL_GCP *pasGCPs = poSrcDS->GetGCPs();
    8032             :         double *padfTiePoints = static_cast<double *>(
    8033          11 :             CPLMalloc(6 * sizeof(double) * poSrcDS->GetGCPCount()));
    8034             : 
    8035          55 :         for (int iGCP = 0; iGCP < poSrcDS->GetGCPCount(); ++iGCP)
    8036             :         {
    8037             : 
    8038          44 :             padfTiePoints[iGCP * 6 + 0] = pasGCPs[iGCP].dfGCPPixel;
    8039          44 :             padfTiePoints[iGCP * 6 + 1] = pasGCPs[iGCP].dfGCPLine;
    8040          44 :             padfTiePoints[iGCP * 6 + 2] = 0;
    8041          44 :             padfTiePoints[iGCP * 6 + 3] = pasGCPs[iGCP].dfGCPX;
    8042          44 :             padfTiePoints[iGCP * 6 + 4] = pasGCPs[iGCP].dfGCPY;
    8043          44 :             padfTiePoints[iGCP * 6 + 5] = pasGCPs[iGCP].dfGCPZ;
    8044             : 
    8045          44 :             if (bPixelIsPoint && !bPointGeoIgnore)
    8046             :             {
    8047           4 :                 padfTiePoints[iGCP * 6 + 0] -= 0.5;
    8048           4 :                 padfTiePoints[iGCP * 6 + 1] -= 0.5;
    8049             :             }
    8050             :         }
    8051             : 
    8052          11 :         TIFFSetField(l_hTIFF, TIFFTAG_GEOTIEPOINTS, 6 * poSrcDS->GetGCPCount(),
    8053             :                      padfTiePoints);
    8054          11 :         CPLFree(padfTiePoints);
    8055             : 
    8056          11 :         l_poSRS = poSrcDS->GetGCPSpatialRef();
    8057             : 
    8058          22 :         if (CPLFetchBool(papszOptions, "TFW", false) ||
    8059          11 :             CPLFetchBool(papszOptions, "WORLDFILE", false))
    8060             :         {
    8061           0 :             ReportError(
    8062             :                 pszFilename, CE_Warning, CPLE_AppDefined,
    8063             :                 "TFW=ON or WORLDFILE=ON creation options are ignored when "
    8064             :                 "GCPs are available");
    8065             :         }
    8066             :     }
    8067             :     else
    8068             :     {
    8069         428 :         l_poSRS = poSrcDS->GetSpatialRef();
    8070             :     }
    8071             : 
    8072             :     /* -------------------------------------------------------------------- */
    8073             :     /*      Copy xml:XMP data                                               */
    8074             :     /* -------------------------------------------------------------------- */
    8075        2143 :     CSLConstList papszXMP = poSrcDS->GetMetadata("xml:XMP");
    8076        2143 :     if (papszXMP != nullptr && *papszXMP != nullptr)
    8077             :     {
    8078           9 :         int nTagSize = static_cast<int>(strlen(*papszXMP));
    8079           9 :         TIFFSetField(l_hTIFF, TIFFTAG_XMLPACKET, nTagSize, *papszXMP);
    8080             :     }
    8081             : 
    8082             :     /* -------------------------------------------------------------------- */
    8083             :     /*      Write the projection information, if possible.                  */
    8084             :     /* -------------------------------------------------------------------- */
    8085        2143 :     const bool bHasProjection = l_poSRS != nullptr;
    8086        2143 :     bool bExportSRSToPAM = false;
    8087        2143 :     if ((bHasProjection || bPixelIsPoint) && bGeoTIFF)
    8088             :     {
    8089        1677 :         GTIF *psGTIF = GTiffDataset::GTIFNew(l_hTIFF);
    8090             : 
    8091        1677 :         if (bHasProjection)
    8092             :         {
    8093        1677 :             const auto eGeoTIFFKeysFlavor = GetGTIFFKeysFlavor(papszOptions);
    8094        1677 :             if (IsSRSCompatibleOfGeoTIFF(l_poSRS, eGeoTIFFKeysFlavor))
    8095             :             {
    8096        1677 :                 GTIFSetFromOGISDefnEx(
    8097             :                     psGTIF,
    8098             :                     OGRSpatialReference::ToHandle(
    8099             :                         const_cast<OGRSpatialReference *>(l_poSRS)),
    8100             :                     eGeoTIFFKeysFlavor, GetGeoTIFFVersion(papszOptions));
    8101             :             }
    8102             :             else
    8103             :             {
    8104           0 :                 bExportSRSToPAM = true;
    8105             :             }
    8106             :         }
    8107             : 
    8108        1677 :         if (bPixelIsPoint)
    8109             :         {
    8110          10 :             GTIFKeySet(psGTIF, GTRasterTypeGeoKey, TYPE_SHORT, 1,
    8111             :                        RasterPixelIsPoint);
    8112             :         }
    8113             : 
    8114        1677 :         GTIFWriteKeys(psGTIF);
    8115        1677 :         GTIFFree(psGTIF);
    8116             :     }
    8117             : 
    8118        2143 :     bool l_bDontReloadFirstBlock = false;
    8119             : 
    8120             : #ifdef HAVE_LIBJPEG
    8121        2143 :     if (bCopyFromJPEG)
    8122             :     {
    8123          12 :         GTIFF_CopyFromJPEG_WriteAdditionalTags(l_hTIFF, poSrcDS);
    8124             :     }
    8125             : #endif
    8126             : 
    8127             :     /* -------------------------------------------------------------------- */
    8128             :     /*      Cleanup                                                         */
    8129             :     /* -------------------------------------------------------------------- */
    8130        2143 :     if (bCopySrcOverviews)
    8131             :     {
    8132         227 :         TIFFDeferStrileArrayWriting(l_hTIFF);
    8133             :     }
    8134        2143 :     TIFFWriteCheck(l_hTIFF, TIFFIsTiled(l_hTIFF), "GTiffCreateCopy()");
    8135        2143 :     TIFFWriteDirectory(l_hTIFF);
    8136        2143 :     if (bStreaming)
    8137             :     {
    8138             :         // We need to write twice the directory to be sure that custom
    8139             :         // TIFF tags are correctly sorted and that padding bytes have been
    8140             :         // added.
    8141           5 :         TIFFSetDirectory(l_hTIFF, 0);
    8142           5 :         TIFFWriteDirectory(l_hTIFF);
    8143             : 
    8144           5 :         if (VSIFSeekL(l_fpL, 0, SEEK_END) != 0)
    8145           0 :             ReportError(pszFilename, CE_Failure, CPLE_FileIO, "Cannot seek");
    8146           5 :         const int nSize = static_cast<int>(VSIFTellL(l_fpL));
    8147             : 
    8148           5 :         vsi_l_offset nDataLength = 0;
    8149           5 :         VSIGetMemFileBuffer(l_osTmpFilename, &nDataLength, FALSE);
    8150           5 :         TIFFSetDirectory(l_hTIFF, 0);
    8151           5 :         GTiffFillStreamableOffsetAndCount(l_hTIFF, nSize);
    8152           5 :         TIFFWriteDirectory(l_hTIFF);
    8153             :     }
    8154        2143 :     const auto nDirCount = TIFFNumberOfDirectories(l_hTIFF);
    8155        2143 :     if (nDirCount >= 1)
    8156             :     {
    8157        2136 :         TIFFSetDirectory(l_hTIFF, static_cast<tdir_t>(nDirCount - 1));
    8158             :     }
    8159        2143 :     const toff_t l_nDirOffset = TIFFCurrentDirOffset(l_hTIFF);
    8160        2143 :     TIFFFlush(l_hTIFF);
    8161        2143 :     XTIFFClose(l_hTIFF);
    8162             : 
    8163        2143 :     VSIFSeekL(l_fpL, 0, SEEK_SET);
    8164             : 
    8165             :     // fpStreaming will assigned to the instance and not closed here.
    8166        2143 :     VSILFILE *fpStreaming = nullptr;
    8167        2143 :     if (bStreaming)
    8168             :     {
    8169           5 :         vsi_l_offset nDataLength = 0;
    8170             :         void *pabyBuffer =
    8171           5 :             VSIGetMemFileBuffer(l_osTmpFilename, &nDataLength, FALSE);
    8172           5 :         fpStreaming = VSIFOpenL(pszFilename, "wb");
    8173           5 :         if (fpStreaming == nullptr)
    8174             :         {
    8175           1 :             VSIUnlink(l_osTmpFilename);
    8176           1 :             CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    8177           1 :             return nullptr;
    8178             :         }
    8179           4 :         if (static_cast<vsi_l_offset>(VSIFWriteL(pabyBuffer, 1,
    8180             :                                                  static_cast<int>(nDataLength),
    8181           4 :                                                  fpStreaming)) != nDataLength)
    8182             :         {
    8183           0 :             ReportError(pszFilename, CE_Failure, CPLE_FileIO,
    8184             :                         "Could not write %d bytes",
    8185             :                         static_cast<int>(nDataLength));
    8186           0 :             CPL_IGNORE_RET_VAL(VSIFCloseL(fpStreaming));
    8187           0 :             VSIUnlink(l_osTmpFilename);
    8188           0 :             CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    8189           0 :             return nullptr;
    8190             :         }
    8191             :     }
    8192             : 
    8193             :     /* -------------------------------------------------------------------- */
    8194             :     /*      Re-open as a dataset and copy over missing metadata using       */
    8195             :     /*      PAM facilities.                                                 */
    8196             :     /* -------------------------------------------------------------------- */
    8197        2142 :     l_hTIFF = VSI_TIFFOpen(bStreaming ? l_osTmpFilename.c_str() : pszFilename,
    8198             :                            "r+", l_fpL);
    8199        2142 :     if (l_hTIFF == nullptr)
    8200             :     {
    8201          11 :         if (bStreaming)
    8202           0 :             VSIUnlink(l_osTmpFilename);
    8203          11 :         l_fpL->CancelCreation();
    8204          11 :         CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    8205          11 :         return nullptr;
    8206             :     }
    8207             : 
    8208             :     /* -------------------------------------------------------------------- */
    8209             :     /*      Create a corresponding GDALDataset.                             */
    8210             :     /* -------------------------------------------------------------------- */
    8211        4262 :     auto poDS = std::make_unique<GTiffDataset>();
    8212             :     const bool bSuppressASAP =
    8213        2131 :         CPLTestBool(CSLFetchNameValueDef(papszOptions, "@SUPPRESS_ASAP", "NO"));
    8214        2131 :     if (bSuppressASAP)
    8215           4 :         poDS->MarkSuppressOnClose();
    8216        2131 :     poDS->SetDescription(pszFilename);
    8217        2131 :     poDS->eAccess = GA_Update;
    8218        2131 :     poDS->m_osFilename = pszFilename;
    8219        2131 :     poDS->m_fpL = l_fpL;
    8220        2131 :     poDS->m_bIMDRPCMetadataLoaded = true;
    8221        2131 :     poDS->m_nColorTableMultiplier = nColorTableMultiplier;
    8222        2131 :     poDS->m_bTileInterleave = bTileInterleaving;
    8223             : 
    8224        2131 :     if (bTileInterleaving)
    8225             :     {
    8226           7 :         poDS->m_oGTiffMDMD.SetMetadataItem("INTERLEAVE", "TILE",
    8227             :                                            "IMAGE_STRUCTURE");
    8228             :     }
    8229             : 
    8230        2131 :     const bool bAppend = CPLFetchBool(papszOptions, "APPEND_SUBDATASET", false);
    8231        4261 :     if (poDS->OpenOffset(l_hTIFF,
    8232        2130 :                          bAppend ? l_nDirOffset : TIFFCurrentDirOffset(l_hTIFF),
    8233             :                          GA_Update,
    8234             :                          false,  // bAllowRGBAInterface
    8235             :                          true    // bReadGeoTransform
    8236        2131 :                          ) != CE_None)
    8237             :     {
    8238           0 :         l_fpL->CancelCreation();
    8239           0 :         poDS.reset();
    8240           0 :         if (bStreaming)
    8241           0 :             VSIUnlink(l_osTmpFilename);
    8242           0 :         return nullptr;
    8243             :     }
    8244             : 
    8245             :     // Legacy... Patch back GDT_Int8 type to GDT_UInt8 if the user used
    8246             :     // PIXELTYPE=SIGNEDBYTE
    8247        2131 :     const char *pszPixelType = CSLFetchNameValue(papszOptions, "PIXELTYPE");
    8248        2131 :     if (pszPixelType == nullptr)
    8249        2126 :         pszPixelType = "";
    8250        2131 :     if (eType == GDT_UInt8 && EQUAL(pszPixelType, "SIGNEDBYTE"))
    8251             :     {
    8252          10 :         for (int i = 0; i < poDS->nBands; ++i)
    8253             :         {
    8254           5 :             auto poBand = static_cast<GTiffRasterBand *>(poDS->papoBands[i]);
    8255           5 :             poBand->eDataType = GDT_UInt8;
    8256           5 :             poBand->EnablePixelTypeSignedByteWarning(false);
    8257           5 :             poBand->SetMetadataItem("PIXELTYPE", "SIGNEDBYTE",
    8258             :                                     "IMAGE_STRUCTURE");
    8259           5 :             poBand->EnablePixelTypeSignedByteWarning(true);
    8260             :         }
    8261             :     }
    8262             : 
    8263        2131 :     poDS->oOvManager.Initialize(poDS.get(), pszFilename);
    8264             : 
    8265        2131 :     if (bStreaming)
    8266             :     {
    8267           4 :         VSIUnlink(l_osTmpFilename);
    8268           4 :         poDS->m_fpToWrite = fpStreaming;
    8269             :     }
    8270        2131 :     poDS->m_eProfile = eProfile;
    8271             : 
    8272        2131 :     int nCloneInfoFlags = GCIF_PAM_DEFAULT & ~GCIF_MASK;
    8273             : 
    8274             :     // If we explicitly asked not to tag the alpha band as such, do not
    8275             :     // reintroduce this alpha color interpretation in PAM.
    8276        2131 :     if (poSrcDS->GetRasterBand(l_nBands)->GetColorInterpretation() ==
    8277        2259 :             GCI_AlphaBand &&
    8278         128 :         GTiffGetAlphaValue(
    8279             :             CPLGetConfigOption("GTIFF_ALPHA",
    8280             :                                CSLFetchNameValue(papszOptions, "ALPHA")),
    8281             :             DEFAULT_ALPHA_TYPE) == EXTRASAMPLE_UNSPECIFIED)
    8282             :     {
    8283           1 :         nCloneInfoFlags &= ~GCIF_COLORINTERP;
    8284             :     }
    8285             :     // Ignore source band color interpretation if requesting PHOTOMETRIC=RGB
    8286        3358 :     else if (l_nBands >= 3 &&
    8287        1228 :              EQUAL(CSLFetchNameValueDef(papszOptions, "PHOTOMETRIC", ""),
    8288             :                    "RGB"))
    8289             :     {
    8290          28 :         for (int i = 1; i <= 3; i++)
    8291             :         {
    8292          21 :             poDS->GetRasterBand(i)->SetColorInterpretation(
    8293          21 :                 static_cast<GDALColorInterp>(GCI_RedBand + (i - 1)));
    8294             :         }
    8295           7 :         nCloneInfoFlags &= ~GCIF_COLORINTERP;
    8296           9 :         if (!(l_nBands == 4 &&
    8297           2 :               CSLFetchNameValue(papszOptions, "ALPHA") != nullptr))
    8298             :         {
    8299          15 :             for (int i = 4; i <= l_nBands; i++)
    8300             :             {
    8301          18 :                 poDS->GetRasterBand(i)->SetColorInterpretation(
    8302           9 :                     poSrcDS->GetRasterBand(i)->GetColorInterpretation());
    8303             :             }
    8304             :         }
    8305             :     }
    8306             : 
    8307             :     CPLString osOldGTIFF_REPORT_COMPD_CSVal(
    8308        4262 :         CPLGetConfigOption("GTIFF_REPORT_COMPD_CS", ""));
    8309        2131 :     CPLSetThreadLocalConfigOption("GTIFF_REPORT_COMPD_CS", "YES");
    8310        2131 :     poDS->CloneInfo(poSrcDS, nCloneInfoFlags);
    8311        2131 :     CPLSetThreadLocalConfigOption("GTIFF_REPORT_COMPD_CS",
    8312        2131 :                                   osOldGTIFF_REPORT_COMPD_CSVal.empty()
    8313             :                                       ? nullptr
    8314           0 :                                       : osOldGTIFF_REPORT_COMPD_CSVal.c_str());
    8315             : 
    8316        2148 :     if ((!bGeoTIFF || bExportSRSToPAM) &&
    8317          17 :         (poDS->GetPamFlags() & GPF_DISABLED) == 0)
    8318             :     {
    8319             :         // Copy georeferencing info to PAM if the profile is not GeoTIFF
    8320          16 :         poDS->GDALPamDataset::SetSpatialRef(poDS->GetSpatialRef());
    8321          16 :         GDALGeoTransform gt;
    8322          16 :         if (poDS->GetGeoTransform(gt) == CE_None)
    8323             :         {
    8324           5 :             poDS->GDALPamDataset::SetGeoTransform(gt);
    8325             :         }
    8326          16 :         poDS->GDALPamDataset::SetGCPs(poDS->GetGCPCount(), poDS->GetGCPs(),
    8327             :                                       poDS->GetGCPSpatialRef());
    8328             :     }
    8329             : 
    8330        2131 :     poDS->m_papszCreationOptions = CSLDuplicate(papszOptions);
    8331        2131 :     poDS->m_bDontReloadFirstBlock = l_bDontReloadFirstBlock;
    8332             : 
    8333             :     /* -------------------------------------------------------------------- */
    8334             :     /*      CloneInfo() does not merge metadata, it just replaces it        */
    8335             :     /*      totally.  So we have to merge it.                               */
    8336             :     /* -------------------------------------------------------------------- */
    8337             : 
    8338        2131 :     CSLConstList papszSRC_MD = poSrcDS->GetMetadata();
    8339        2131 :     char **papszDST_MD = CSLDuplicate(poDS->GetMetadata());
    8340             : 
    8341        2131 :     papszDST_MD = CSLMerge(papszDST_MD, papszSRC_MD);
    8342             : 
    8343        2131 :     poDS->SetMetadata(papszDST_MD);
    8344        2131 :     CSLDestroy(papszDST_MD);
    8345             : 
    8346             :     // Depending on the PHOTOMETRIC tag, the TIFF file may not have the same
    8347             :     // band count as the source. Will fail later in GDALDatasetCopyWholeRaster
    8348             :     // anyway.
    8349        7189 :     for (int nBand = 1;
    8350        7189 :          nBand <= std::min(poDS->GetRasterCount(), poSrcDS->GetRasterCount());
    8351             :          ++nBand)
    8352             :     {
    8353        5058 :         GDALRasterBand *poSrcBand = poSrcDS->GetRasterBand(nBand);
    8354        5058 :         GDALRasterBand *poDstBand = poDS->GetRasterBand(nBand);
    8355        5058 :         papszSRC_MD = poSrcBand->GetMetadata();
    8356        5058 :         papszDST_MD = CSLDuplicate(poDstBand->GetMetadata());
    8357             : 
    8358        5058 :         papszDST_MD = CSLMerge(papszDST_MD, papszSRC_MD);
    8359             : 
    8360        5058 :         poDstBand->SetMetadata(papszDST_MD);
    8361        5058 :         CSLDestroy(papszDST_MD);
    8362             : 
    8363        5058 :         char **papszCatNames = poSrcBand->GetCategoryNames();
    8364        5058 :         if (nullptr != papszCatNames)
    8365           0 :             poDstBand->SetCategoryNames(papszCatNames);
    8366             :     }
    8367             : 
    8368        2131 :     l_hTIFF = static_cast<TIFF *>(poDS->GetInternalHandle("TIFF_HANDLE"));
    8369             : 
    8370             :     /* -------------------------------------------------------------------- */
    8371             :     /*      Handle forcing xml:ESRI data to be written to PAM.              */
    8372             :     /* -------------------------------------------------------------------- */
    8373        2131 :     if (CPLTestBool(CPLGetConfigOption("ESRI_XML_PAM", "NO")))
    8374             :     {
    8375           1 :         CSLConstList papszESRIMD = poSrcDS->GetMetadata("xml:ESRI");
    8376           1 :         if (papszESRIMD)
    8377             :         {
    8378           1 :             poDS->SetMetadata(papszESRIMD, "xml:ESRI");
    8379             :         }
    8380             :     }
    8381             : 
    8382             :     /* -------------------------------------------------------------------- */
    8383             :     /*      Second chance: now that we have a PAM dataset, it is possible   */
    8384             :     /*      to write metadata that we could not write as a TIFF tag.        */
    8385             :     /* -------------------------------------------------------------------- */
    8386        2131 :     if (!bHasWrittenMDInGeotiffTAG && !bStreaming)
    8387             :     {
    8388           6 :         GTiffDataset::WriteMetadata(
    8389           6 :             poDS.get(), l_hTIFF, true, eProfile, pszFilename, papszOptions,
    8390             :             true /* don't write RPC and IMD file again */);
    8391             :     }
    8392             : 
    8393        2131 :     if (!bStreaming)
    8394        2127 :         GTiffDataset::WriteRPC(poDS.get(), l_hTIFF, true, eProfile, pszFilename,
    8395             :                                papszOptions,
    8396             :                                true /* write only in PAM AND if needed */);
    8397             : 
    8398        2131 :     poDS->m_bWriteCOGLayout = bCopySrcOverviews;
    8399             : 
    8400             :     // To avoid unnecessary directory rewriting.
    8401        2131 :     poDS->m_bMetadataChanged = false;
    8402        2131 :     poDS->m_bGeoTIFFInfoChanged = false;
    8403        2131 :     poDS->m_bNoDataChanged = false;
    8404        2131 :     poDS->m_bForceUnsetGTOrGCPs = false;
    8405        2131 :     poDS->m_bForceUnsetProjection = false;
    8406        2131 :     poDS->m_bStreamingOut = bStreaming;
    8407             : 
    8408             :     // Don't try to load external metadata files (#6597).
    8409        2131 :     poDS->m_bIMDRPCMetadataLoaded = true;
    8410             : 
    8411             :     // We must re-set the compression level at this point, since it has been
    8412             :     // lost a few lines above when closing the newly create TIFF file The
    8413             :     // TIFFTAG_ZIPQUALITY & TIFFTAG_JPEGQUALITY are not store in the TIFF file.
    8414             :     // They are just TIFF session parameters.
    8415             : 
    8416        2131 :     poDS->m_nZLevel = GTiffGetZLevel(papszOptions);
    8417        2131 :     poDS->m_nLZMAPreset = GTiffGetLZMAPreset(papszOptions);
    8418        2131 :     poDS->m_nZSTDLevel = GTiffGetZSTDPreset(papszOptions);
    8419        2131 :     poDS->m_nWebPLevel = GTiffGetWebPLevel(papszOptions);
    8420        2131 :     poDS->m_bWebPLossless = GTiffGetWebPLossless(papszOptions);
    8421        2134 :     if (poDS->m_nWebPLevel != 100 && poDS->m_bWebPLossless &&
    8422           3 :         CSLFetchNameValue(papszOptions, "WEBP_LEVEL"))
    8423             :     {
    8424           0 :         CPLError(CE_Warning, CPLE_AppDefined,
    8425             :                  "WEBP_LEVEL is specified, but WEBP_LOSSLESS=YES. "
    8426             :                  "WEBP_LEVEL will be ignored.");
    8427             :     }
    8428        2131 :     poDS->m_nJpegQuality = GTiffGetJpegQuality(papszOptions);
    8429        2131 :     poDS->m_nJpegTablesMode = GTiffGetJpegTablesMode(papszOptions);
    8430        2131 :     poDS->GetDiscardLsbOption(papszOptions);
    8431        2131 :     poDS->m_dfMaxZError = GTiffGetLERCMaxZError(papszOptions);
    8432        2131 :     poDS->m_dfMaxZErrorOverview = GTiffGetLERCMaxZErrorOverview(papszOptions);
    8433             : #if HAVE_JXL
    8434        2131 :     poDS->m_bJXLLossless = GTiffGetJXLLossless(papszOptions);
    8435        2131 :     poDS->m_nJXLEffort = GTiffGetJXLEffort(papszOptions);
    8436        2131 :     poDS->m_fJXLDistance = GTiffGetJXLDistance(papszOptions);
    8437        2131 :     poDS->m_fJXLAlphaDistance = GTiffGetJXLAlphaDistance(papszOptions);
    8438             : #endif
    8439        2131 :     poDS->InitCreationOrOpenOptions(true, papszOptions);
    8440             : 
    8441        2131 :     if (l_nCompression == COMPRESSION_ADOBE_DEFLATE ||
    8442        2103 :         l_nCompression == COMPRESSION_LERC)
    8443             :     {
    8444          99 :         GTiffSetDeflateSubCodec(l_hTIFF);
    8445             : 
    8446          99 :         if (poDS->m_nZLevel != -1)
    8447             :         {
    8448          12 :             TIFFSetField(l_hTIFF, TIFFTAG_ZIPQUALITY, poDS->m_nZLevel);
    8449             :         }
    8450             :     }
    8451        2131 :     if (l_nCompression == COMPRESSION_JPEG)
    8452             :     {
    8453          75 :         if (poDS->m_nJpegQuality != -1)
    8454             :         {
    8455           9 :             TIFFSetField(l_hTIFF, TIFFTAG_JPEGQUALITY, poDS->m_nJpegQuality);
    8456             :         }
    8457          75 :         TIFFSetField(l_hTIFF, TIFFTAG_JPEGTABLESMODE, poDS->m_nJpegTablesMode);
    8458             :     }
    8459        2131 :     if (l_nCompression == COMPRESSION_LZMA)
    8460             :     {
    8461           7 :         if (poDS->m_nLZMAPreset != -1)
    8462             :         {
    8463           6 :             TIFFSetField(l_hTIFF, TIFFTAG_LZMAPRESET, poDS->m_nLZMAPreset);
    8464             :         }
    8465             :     }
    8466        2131 :     if (l_nCompression == COMPRESSION_ZSTD ||
    8467        2118 :         l_nCompression == COMPRESSION_LERC)
    8468             :     {
    8469          84 :         if (poDS->m_nZSTDLevel != -1)
    8470             :         {
    8471           8 :             TIFFSetField(l_hTIFF, TIFFTAG_ZSTD_LEVEL, poDS->m_nZSTDLevel);
    8472             :         }
    8473             :     }
    8474        2131 :     if (l_nCompression == COMPRESSION_LERC)
    8475             :     {
    8476          71 :         TIFFSetField(l_hTIFF, TIFFTAG_LERC_MAXZERROR, poDS->m_dfMaxZError);
    8477             :     }
    8478             : #if HAVE_JXL
    8479        2131 :     if (l_nCompression == COMPRESSION_JXL ||
    8480        2131 :         l_nCompression == COMPRESSION_JXL_DNG_1_7)
    8481             :     {
    8482          91 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_LOSSYNESS,
    8483          91 :                      poDS->m_bJXLLossless ? JXL_LOSSLESS : JXL_LOSSY);
    8484          91 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_EFFORT, poDS->m_nJXLEffort);
    8485          91 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_DISTANCE,
    8486          91 :                      static_cast<double>(poDS->m_fJXLDistance));
    8487          91 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_ALPHA_DISTANCE,
    8488          91 :                      static_cast<double>(poDS->m_fJXLAlphaDistance));
    8489             :     }
    8490             : #endif
    8491        2131 :     if (l_nCompression == COMPRESSION_WEBP)
    8492             :     {
    8493          14 :         if (poDS->m_nWebPLevel != -1)
    8494             :         {
    8495          14 :             TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LEVEL, poDS->m_nWebPLevel);
    8496             :         }
    8497             : 
    8498          14 :         if (poDS->m_bWebPLossless)
    8499             :         {
    8500           5 :             TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LOSSLESS, poDS->m_bWebPLossless);
    8501             :         }
    8502             :     }
    8503             : 
    8504             :     /* -------------------------------------------------------------------- */
    8505             :     /*      Do we want to ensure all blocks get written out on close to     */
    8506             :     /*      avoid sparse files?                                             */
    8507             :     /* -------------------------------------------------------------------- */
    8508        2131 :     if (!CPLFetchBool(papszOptions, "SPARSE_OK", false))
    8509        2103 :         poDS->m_bFillEmptyTilesAtClosing = true;
    8510             : 
    8511        2131 :     poDS->m_bWriteEmptyTiles =
    8512        4039 :         (bCopySrcOverviews && poDS->m_bFillEmptyTilesAtClosing) || bStreaming ||
    8513        1908 :         (poDS->m_nCompression != COMPRESSION_NONE &&
    8514         310 :          poDS->m_bFillEmptyTilesAtClosing);
    8515             :     // Only required for people writing non-compressed striped files in the
    8516             :     // rightorder and wanting all tstrips to be written in the same order
    8517             :     // so that the end result can be memory mapped without knowledge of each
    8518             :     // strip offset
    8519        2131 :     if (CPLTestBool(CSLFetchNameValueDef(
    8520        4262 :             papszOptions, "WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")) ||
    8521        2131 :         CPLTestBool(CSLFetchNameValueDef(
    8522             :             papszOptions, "@WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")))
    8523             :     {
    8524           0 :         poDS->m_bWriteEmptyTiles = true;
    8525             :     }
    8526             : 
    8527             :     // Precreate (internal) mask, so that the IBuildOverviews() below
    8528             :     // has a chance to create also the overviews of the mask.
    8529        2131 :     CPLErr eErr = CE_None;
    8530             : 
    8531        2131 :     if (bCreateMask)
    8532             :     {
    8533          38 :         eErr = poDS->CreateMaskBand(nMaskFlags);
    8534          38 :         if (poDS->m_poMaskDS)
    8535             :         {
    8536          37 :             poDS->m_poMaskDS->m_bFillEmptyTilesAtClosing =
    8537          37 :                 poDS->m_bFillEmptyTilesAtClosing;
    8538          37 :             poDS->m_poMaskDS->m_bWriteEmptyTiles = poDS->m_bWriteEmptyTiles;
    8539             :         }
    8540             :     }
    8541             : 
    8542             :     /* -------------------------------------------------------------------- */
    8543             :     /*      Create and then copy existing overviews if requested            */
    8544             :     /*  We do it such that all the IFDs are at the beginning of the file,   */
    8545             :     /*  and that the imagery data for the smallest overview is written      */
    8546             :     /*  first, that way the file is more usable when embedded in a          */
    8547             :     /*  compressed stream.                                                  */
    8548             :     /* -------------------------------------------------------------------- */
    8549             : 
    8550             :     // For scaled progress due to overview copying.
    8551        2131 :     const int nBandsWidthMask = l_nBands + (bCreateMask ? 1 : 0);
    8552        2131 :     double dfTotalPixels =
    8553        2131 :         static_cast<double>(nXSize) * nYSize * nBandsWidthMask;
    8554        2131 :     double dfCurPixels = 0;
    8555             : 
    8556        2131 :     if (eErr == CE_None && bCopySrcOverviews)
    8557             :     {
    8558           0 :         std::unique_ptr<GDALDataset> poMaskOvrDS;
    8559             :         const char *pszMaskOvrDS =
    8560         224 :             CSLFetchNameValue(papszOptions, "@MASK_OVERVIEW_DATASET");
    8561         224 :         if (pszMaskOvrDS)
    8562             :         {
    8563           6 :             poMaskOvrDS.reset(GDALDataset::Open(pszMaskOvrDS));
    8564           6 :             if (!poMaskOvrDS)
    8565             :             {
    8566           0 :                 l_fpL->CancelCreation();
    8567           0 :                 return nullptr;
    8568             :             }
    8569           6 :             if (poMaskOvrDS->GetRasterCount() != 1)
    8570             :             {
    8571           0 :                 l_fpL->CancelCreation();
    8572           0 :                 return nullptr;
    8573             :             }
    8574             :         }
    8575         224 :         if (nSrcOverviews)
    8576             :         {
    8577          71 :             eErr = poDS->CreateOverviewsFromSrcOverviews(poSrcDS, poOvrDS.get(),
    8578             :                                                          nSrcOverviews);
    8579             : 
    8580         207 :             if (eErr == CE_None &&
    8581          71 :                 (poMaskOvrDS != nullptr ||
    8582          65 :                  (poSrcDS->GetRasterBand(1)->GetOverview(0) &&
    8583          35 :                   poSrcDS->GetRasterBand(1)->GetOverview(0)->GetMaskFlags() ==
    8584             :                       GMF_PER_DATASET)))
    8585             :             {
    8586          19 :                 int nOvrBlockXSize = 0;
    8587          19 :                 int nOvrBlockYSize = 0;
    8588          19 :                 GTIFFGetOverviewBlockSize(
    8589          19 :                     GDALRasterBand::ToHandle(poDS->GetRasterBand(1)),
    8590             :                     &nOvrBlockXSize, &nOvrBlockYSize, nullptr, nullptr);
    8591          19 :                 eErr = poDS->CreateInternalMaskOverviews(nOvrBlockXSize,
    8592             :                                                          nOvrBlockYSize);
    8593             :             }
    8594             :         }
    8595             : 
    8596         224 :         TIFFForceStrileArrayWriting(poDS->m_hTIFF);
    8597             : 
    8598         224 :         if (poDS->m_poMaskDS)
    8599             :         {
    8600          27 :             TIFFForceStrileArrayWriting(poDS->m_poMaskDS->m_hTIFF);
    8601             :         }
    8602             : 
    8603         355 :         for (auto &poIterOvrDS : poDS->m_apoOverviewDS)
    8604             :         {
    8605         131 :             TIFFForceStrileArrayWriting(poIterOvrDS->m_hTIFF);
    8606             : 
    8607         131 :             if (poIterOvrDS->m_poMaskDS)
    8608             :             {
    8609          32 :                 TIFFForceStrileArrayWriting(poIterOvrDS->m_poMaskDS->m_hTIFF);
    8610             :             }
    8611             :         }
    8612             : 
    8613         224 :         if (eErr == CE_None && nSrcOverviews)
    8614             :         {
    8615          71 :             if (poDS->m_apoOverviewDS.size() !=
    8616          71 :                 static_cast<size_t>(nSrcOverviews))
    8617             :             {
    8618           0 :                 ReportError(
    8619             :                     pszFilename, CE_Failure, CPLE_AppDefined,
    8620             :                     "Did only manage to instantiate %d overview levels, "
    8621             :                     "whereas source contains %d",
    8622           0 :                     static_cast<int>(poDS->m_apoOverviewDS.size()),
    8623             :                     nSrcOverviews);
    8624           0 :                 eErr = CE_Failure;
    8625             :             }
    8626             : 
    8627         202 :             for (int i = 0; eErr == CE_None && i < nSrcOverviews; ++i)
    8628             :             {
    8629             :                 GDALRasterBand *poOvrBand =
    8630             :                     poOvrDS
    8631         207 :                         ? (i == 0
    8632          76 :                                ? poOvrDS->GetRasterBand(1)
    8633          40 :                                : poOvrDS->GetRasterBand(1)->GetOverview(i - 1))
    8634         186 :                         : poSrcDS->GetRasterBand(1)->GetOverview(i);
    8635             :                 const double dfOvrPixels =
    8636         131 :                     static_cast<double>(poOvrBand->GetXSize()) *
    8637         131 :                     poOvrBand->GetYSize();
    8638         131 :                 dfTotalPixels += dfOvrPixels * l_nBands;
    8639         244 :                 if (poOvrBand->GetMaskFlags() == GMF_PER_DATASET ||
    8640         113 :                     poMaskOvrDS != nullptr)
    8641             :                 {
    8642          32 :                     dfTotalPixels += dfOvrPixels;
    8643             :                 }
    8644          99 :                 else if (i == 0 && poDS->GetRasterBand(1)->GetMaskFlags() ==
    8645             :                                        GMF_PER_DATASET)
    8646             :                 {
    8647           1 :                     ReportError(pszFilename, CE_Warning, CPLE_AppDefined,
    8648             :                                 "Source dataset has a mask band on full "
    8649             :                                 "resolution, overviews on the regular bands, "
    8650             :                                 "but lacks overviews on the mask band.");
    8651             :                 }
    8652             :             }
    8653             : 
    8654             :             // Now copy the imagery.
    8655             :             // Begin with the smallest overview.
    8656          71 :             for (int iOvrLevel = nSrcOverviews - 1;
    8657         201 :                  eErr == CE_None && iOvrLevel >= 0; --iOvrLevel)
    8658             :             {
    8659         130 :                 auto poDstDS = poDS->m_apoOverviewDS[iOvrLevel].get();
    8660             : 
    8661             :                 // Create a fake dataset with the source overview level so that
    8662             :                 // GDALDatasetCopyWholeRaster can cope with it.
    8663             :                 GDALDataset *poSrcOvrDS =
    8664             :                     poOvrDS
    8665         170 :                         ? (iOvrLevel == 0 ? poOvrDS.get()
    8666          40 :                                           : GDALCreateOverviewDataset(
    8667             :                                                 poOvrDS.get(), iOvrLevel - 1,
    8668             :                                                 /* bThisLevelOnly = */ true))
    8669          54 :                         : GDALCreateOverviewDataset(
    8670             :                               poSrcDS, iOvrLevel,
    8671         130 :                               /* bThisLevelOnly = */ true);
    8672             :                 GDALRasterBand *poSrcOvrBand =
    8673         206 :                     poOvrDS ? (iOvrLevel == 0
    8674          76 :                                    ? poOvrDS->GetRasterBand(1)
    8675          80 :                                    : poOvrDS->GetRasterBand(1)->GetOverview(
    8676          40 :                                          iOvrLevel - 1))
    8677         184 :                             : poSrcDS->GetRasterBand(1)->GetOverview(iOvrLevel);
    8678             :                 double dfNextCurPixels =
    8679             :                     dfCurPixels +
    8680         130 :                     static_cast<double>(poSrcOvrBand->GetXSize()) *
    8681         130 :                         poSrcOvrBand->GetYSize() * l_nBands;
    8682             : 
    8683         130 :                 poDstDS->m_bBlockOrderRowMajor = true;
    8684         130 :                 poDstDS->m_bLeaderSizeAsUInt4 = true;
    8685         130 :                 poDstDS->m_bTrailerRepeatedLast4BytesRepeated = true;
    8686         130 :                 poDstDS->m_bFillEmptyTilesAtClosing =
    8687         130 :                     poDS->m_bFillEmptyTilesAtClosing;
    8688         130 :                 poDstDS->m_bWriteEmptyTiles = poDS->m_bWriteEmptyTiles;
    8689         130 :                 poDstDS->m_bTileInterleave = poDS->m_bTileInterleave;
    8690         130 :                 GDALRasterBand *poSrcMaskBand = nullptr;
    8691         130 :                 if (poDstDS->m_poMaskDS)
    8692             :                 {
    8693          32 :                     poDstDS->m_poMaskDS->m_bBlockOrderRowMajor = true;
    8694          32 :                     poDstDS->m_poMaskDS->m_bLeaderSizeAsUInt4 = true;
    8695          32 :                     poDstDS->m_poMaskDS->m_bTrailerRepeatedLast4BytesRepeated =
    8696             :                         true;
    8697          64 :                     poDstDS->m_poMaskDS->m_bFillEmptyTilesAtClosing =
    8698          32 :                         poDS->m_bFillEmptyTilesAtClosing;
    8699          64 :                     poDstDS->m_poMaskDS->m_bWriteEmptyTiles =
    8700          32 :                         poDS->m_bWriteEmptyTiles;
    8701             : 
    8702          32 :                     poSrcMaskBand =
    8703             :                         poMaskOvrDS
    8704          46 :                             ? (iOvrLevel == 0
    8705          14 :                                    ? poMaskOvrDS->GetRasterBand(1)
    8706          16 :                                    : poMaskOvrDS->GetRasterBand(1)->GetOverview(
    8707           8 :                                          iOvrLevel - 1))
    8708          50 :                             : poSrcOvrBand->GetMaskBand();
    8709             :                 }
    8710             : 
    8711         130 :                 if (poDstDS->m_poMaskDS)
    8712             :                 {
    8713          32 :                     dfNextCurPixels +=
    8714          32 :                         static_cast<double>(poSrcOvrBand->GetXSize()) *
    8715          32 :                         poSrcOvrBand->GetYSize();
    8716             :                 }
    8717             :                 void *pScaledData =
    8718         130 :                     GDALCreateScaledProgress(dfCurPixels / dfTotalPixels,
    8719             :                                              dfNextCurPixels / dfTotalPixels,
    8720             :                                              pfnProgress, pProgressData);
    8721             : 
    8722         130 :                 eErr = CopyImageryAndMask(poDstDS, poSrcOvrDS, poSrcMaskBand,
    8723             :                                           GDALScaledProgress, pScaledData);
    8724             : 
    8725         130 :                 dfCurPixels = dfNextCurPixels;
    8726         130 :                 GDALDestroyScaledProgress(pScaledData);
    8727             : 
    8728         130 :                 if (poSrcOvrDS != poOvrDS.get())
    8729          94 :                     delete poSrcOvrDS;
    8730         130 :                 poSrcOvrDS = nullptr;
    8731             :             }
    8732             :         }
    8733             :     }
    8734             : 
    8735             :     /* -------------------------------------------------------------------- */
    8736             :     /*      Copy actual imagery.                                            */
    8737             :     /* -------------------------------------------------------------------- */
    8738        2131 :     double dfNextCurPixels =
    8739        2131 :         dfCurPixels + static_cast<double>(nXSize) * nYSize * l_nBands;
    8740        2131 :     void *pScaledData = GDALCreateScaledProgress(
    8741             :         dfCurPixels / dfTotalPixels, dfNextCurPixels / dfTotalPixels,
    8742             :         pfnProgress, pProgressData);
    8743             : 
    8744             : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
    8745        2131 :     bool bTryCopy = true;
    8746             : #endif
    8747             : 
    8748             : #ifdef HAVE_LIBJPEG
    8749        2131 :     if (bCopyFromJPEG)
    8750             :     {
    8751          12 :         eErr = GTIFF_CopyFromJPEG(poDS.get(), poSrcDS, pfnProgress,
    8752             :                                   pProgressData, bTryCopy);
    8753             : 
    8754             :         // In case of failure in the decompression step, try normal copy.
    8755          12 :         if (bTryCopy)
    8756           0 :             eErr = CE_None;
    8757             :     }
    8758             : #endif
    8759             : 
    8760             : #ifdef JPEG_DIRECT_COPY
    8761             :     if (bDirectCopyFromJPEG)
    8762             :     {
    8763             :         eErr = GTIFF_DirectCopyFromJPEG(poDS.get(), poSrcDS, pfnProgress,
    8764             :                                         pProgressData, bTryCopy);
    8765             : 
    8766             :         // In case of failure in the reading step, try normal copy.
    8767             :         if (bTryCopy)
    8768             :             eErr = CE_None;
    8769             :     }
    8770             : #endif
    8771             : 
    8772        2131 :     bool bWriteMask = true;
    8773        2131 :     if (
    8774             : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
    8775        4250 :         bTryCopy &&
    8776             : #endif
    8777        2119 :         (poDS->m_bTreatAsSplit || poDS->m_bTreatAsSplitBitmap))
    8778             :     {
    8779             :         // For split bands, we use TIFFWriteScanline() interface.
    8780           9 :         CPLAssert(poDS->m_nBitsPerSample == 8 || poDS->m_nBitsPerSample == 1);
    8781             : 
    8782           9 :         if (poDS->m_nPlanarConfig == PLANARCONFIG_CONTIG && poDS->nBands > 1)
    8783             :         {
    8784             :             GByte *pabyScanline = static_cast<GByte *>(
    8785           3 :                 VSI_MALLOC_VERBOSE(TIFFScanlineSize(l_hTIFF)));
    8786           3 :             if (pabyScanline == nullptr)
    8787           0 :                 eErr = CE_Failure;
    8788        9052 :             for (int j = 0; j < nYSize && eErr == CE_None; ++j)
    8789             :             {
    8790       18098 :                 eErr = poSrcDS->RasterIO(GF_Read, 0, j, nXSize, 1, pabyScanline,
    8791             :                                          nXSize, 1, GDT_UInt8, l_nBands,
    8792        9049 :                                          nullptr, poDS->nBands, 0, 1, nullptr);
    8793       18098 :                 if (eErr == CE_None &&
    8794        9049 :                     TIFFWriteScanline(l_hTIFF, pabyScanline, j, 0) == -1)
    8795             :                 {
    8796           0 :                     ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    8797             :                                 "TIFFWriteScanline() failed.");
    8798           0 :                     eErr = CE_Failure;
    8799             :                 }
    8800        9049 :                 if (!GDALScaledProgress((j + 1) * 1.0 / nYSize, nullptr,
    8801             :                                         pScaledData))
    8802           0 :                     eErr = CE_Failure;
    8803             :             }
    8804           3 :             CPLFree(pabyScanline);
    8805             :         }
    8806             :         else
    8807             :         {
    8808             :             GByte *pabyScanline =
    8809           6 :                 static_cast<GByte *>(VSI_MALLOC_VERBOSE(nXSize));
    8810           6 :             if (pabyScanline == nullptr)
    8811           0 :                 eErr = CE_Failure;
    8812             :             else
    8813           6 :                 eErr = CE_None;
    8814          14 :             for (int iBand = 1; iBand <= l_nBands && eErr == CE_None; ++iBand)
    8815             :             {
    8816       48211 :                 for (int j = 0; j < nYSize && eErr == CE_None; ++j)
    8817             :                 {
    8818       48203 :                     eErr = poSrcDS->GetRasterBand(iBand)->RasterIO(
    8819             :                         GF_Read, 0, j, nXSize, 1, pabyScanline, nXSize, 1,
    8820             :                         GDT_UInt8, 0, 0, nullptr);
    8821       48203 :                     if (poDS->m_bTreatAsSplitBitmap)
    8822             :                     {
    8823     7225210 :                         for (int i = 0; i < nXSize; ++i)
    8824             :                         {
    8825     7216010 :                             const GByte byVal = pabyScanline[i];
    8826     7216010 :                             if ((i & 0x7) == 0)
    8827      902001 :                                 pabyScanline[i >> 3] = 0;
    8828     7216010 :                             if (byVal)
    8829     7097220 :                                 pabyScanline[i >> 3] |= 0x80 >> (i & 0x7);
    8830             :                         }
    8831             :                     }
    8832       96406 :                     if (eErr == CE_None &&
    8833       48203 :                         TIFFWriteScanline(l_hTIFF, pabyScanline, j,
    8834       48203 :                                           static_cast<uint16_t>(iBand - 1)) ==
    8835             :                             -1)
    8836             :                     {
    8837           0 :                         ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    8838             :                                     "TIFFWriteScanline() failed.");
    8839           0 :                         eErr = CE_Failure;
    8840             :                     }
    8841       48203 :                     if (!GDALScaledProgress((j + 1 + (iBand - 1) * nYSize) *
    8842       48203 :                                                 1.0 / (l_nBands * nYSize),
    8843             :                                             nullptr, pScaledData))
    8844           0 :                         eErr = CE_Failure;
    8845             :                 }
    8846             :             }
    8847           6 :             CPLFree(pabyScanline);
    8848             :         }
    8849             : 
    8850             :         // Necessary to be able to read the file without re-opening.
    8851           9 :         TIFFSizeProc pfnSizeProc = TIFFGetSizeProc(l_hTIFF);
    8852             : 
    8853           9 :         TIFFFlushData(l_hTIFF);
    8854             : 
    8855           9 :         toff_t nNewDirOffset = pfnSizeProc(TIFFClientdata(l_hTIFF));
    8856           9 :         if ((nNewDirOffset % 2) == 1)
    8857           5 :             ++nNewDirOffset;
    8858             : 
    8859           9 :         TIFFFlush(l_hTIFF);
    8860             : 
    8861           9 :         if (poDS->m_nDirOffset != TIFFCurrentDirOffset(l_hTIFF))
    8862             :         {
    8863           0 :             poDS->m_nDirOffset = nNewDirOffset;
    8864           0 :             CPLDebug("GTiff", "directory moved during flush.");
    8865             :         }
    8866             :     }
    8867        2122 :     else if (
    8868             : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
    8869        2110 :         bTryCopy &&
    8870             : #endif
    8871             :         eErr == CE_None)
    8872             :     {
    8873        2109 :         const char *papszCopyWholeRasterOptions[3] = {nullptr, nullptr,
    8874             :                                                       nullptr};
    8875        2109 :         int iNextOption = 0;
    8876        2109 :         papszCopyWholeRasterOptions[iNextOption++] = "SKIP_HOLES=YES";
    8877        2109 :         if (l_nCompression != COMPRESSION_NONE)
    8878             :         {
    8879         499 :             papszCopyWholeRasterOptions[iNextOption++] = "COMPRESSED=YES";
    8880             :         }
    8881             : 
    8882             :         // For streaming with separate, we really want that bands are written
    8883             :         // after each other, even if the source is pixel interleaved.
    8884        1610 :         else if (bStreaming && poDS->m_nPlanarConfig == PLANARCONFIG_SEPARATE)
    8885             :         {
    8886           1 :             papszCopyWholeRasterOptions[iNextOption++] = "INTERLEAVE=BAND";
    8887             :         }
    8888             : 
    8889        2109 :         if (bCopySrcOverviews || bTileInterleaving)
    8890             :         {
    8891         224 :             poDS->m_bBlockOrderRowMajor = true;
    8892         224 :             poDS->m_bLeaderSizeAsUInt4 = bCopySrcOverviews;
    8893         224 :             poDS->m_bTrailerRepeatedLast4BytesRepeated = bCopySrcOverviews;
    8894         224 :             if (poDS->m_poMaskDS)
    8895             :             {
    8896          27 :                 poDS->m_poMaskDS->m_bBlockOrderRowMajor = true;
    8897          27 :                 poDS->m_poMaskDS->m_bLeaderSizeAsUInt4 = bCopySrcOverviews;
    8898          27 :                 poDS->m_poMaskDS->m_bTrailerRepeatedLast4BytesRepeated =
    8899             :                     bCopySrcOverviews;
    8900          27 :                 GDALDestroyScaledProgress(pScaledData);
    8901             :                 pScaledData =
    8902          27 :                     GDALCreateScaledProgress(dfCurPixels / dfTotalPixels, 1.0,
    8903             :                                              pfnProgress, pProgressData);
    8904             :             }
    8905             : 
    8906         224 :             eErr = CopyImageryAndMask(poDS.get(), poSrcDS,
    8907         224 :                                       poSrcDS->GetRasterBand(1)->GetMaskBand(),
    8908             :                                       GDALScaledProgress, pScaledData);
    8909         224 :             if (poDS->m_poMaskDS)
    8910             :             {
    8911          27 :                 bWriteMask = false;
    8912             :             }
    8913             :         }
    8914             :         else
    8915             :         {
    8916        1885 :             eErr = GDALDatasetCopyWholeRaster(GDALDataset::ToHandle(poSrcDS),
    8917        1885 :                                               GDALDataset::ToHandle(poDS.get()),
    8918             :                                               papszCopyWholeRasterOptions,
    8919             :                                               GDALScaledProgress, pScaledData);
    8920             :         }
    8921             :     }
    8922             : 
    8923        2131 :     GDALDestroyScaledProgress(pScaledData);
    8924             : 
    8925        2131 :     if (eErr == CE_None && !bStreaming && bWriteMask)
    8926             :     {
    8927        2082 :         pScaledData = GDALCreateScaledProgress(dfNextCurPixels / dfTotalPixels,
    8928             :                                                1.0, pfnProgress, pProgressData);
    8929        2082 :         if (poDS->m_poMaskDS)
    8930             :         {
    8931          10 :             const char *l_papszOptions[2] = {"COMPRESSED=YES", nullptr};
    8932          10 :             eErr = GDALRasterBandCopyWholeRaster(
    8933          10 :                 poSrcDS->GetRasterBand(1)->GetMaskBand(),
    8934          10 :                 poDS->GetRasterBand(1)->GetMaskBand(),
    8935             :                 const_cast<char **>(l_papszOptions), GDALScaledProgress,
    8936             :                 pScaledData);
    8937             :         }
    8938             :         else
    8939             :         {
    8940        2072 :             eErr = GDALDriver::DefaultCopyMasks(poSrcDS, poDS.get(), bStrict,
    8941             :                                                 nullptr, GDALScaledProgress,
    8942             :                                                 pScaledData);
    8943             :         }
    8944        2082 :         GDALDestroyScaledProgress(pScaledData);
    8945             :     }
    8946             : 
    8947        2131 :     poDS->m_bWriteCOGLayout = false;
    8948             : 
    8949        4244 :     if (eErr == CE_None &&
    8950        2113 :         CPLTestBool(CSLFetchNameValueDef(poDS->m_papszCreationOptions,
    8951             :                                          "@FLUSHCACHE", "NO")))
    8952             :     {
    8953         175 :         if (poDS->FlushCache(false) != CE_None)
    8954             :         {
    8955           0 :             eErr = CE_Failure;
    8956             :         }
    8957             :     }
    8958             : 
    8959        2131 :     if (eErr == CE_Failure)
    8960             :     {
    8961          18 :         if (CPLTestBool(CPLGetConfigOption("GTIFF_DELETE_ON_ERROR", "YES")))
    8962             :         {
    8963          17 :             l_fpL->CancelCreation();
    8964          17 :             poDS.reset();
    8965             : 
    8966          17 :             if (!bStreaming)
    8967             :             {
    8968             :                 // Should really delete more carefully.
    8969          17 :                 VSIUnlink(pszFilename);
    8970             :             }
    8971             :         }
    8972             :         else
    8973             :         {
    8974           1 :             poDS.reset();
    8975             :         }
    8976             :     }
    8977             : 
    8978        2131 :     return poDS.release();
    8979             : }
    8980             : 
    8981             : /************************************************************************/
    8982             : /*                           SetSpatialRef()                            */
    8983             : /************************************************************************/
    8984             : 
    8985        1524 : CPLErr GTiffDataset::SetSpatialRef(const OGRSpatialReference *poSRS)
    8986             : 
    8987             : {
    8988        1524 :     if (m_bStreamingOut && m_bCrystalized)
    8989             :     {
    8990           1 :         ReportError(CE_Failure, CPLE_NotSupported,
    8991             :                     "Cannot modify projection at that point in "
    8992             :                     "a streamed output file");
    8993           1 :         return CE_Failure;
    8994             :     }
    8995             : 
    8996        1523 :     LoadGeoreferencingAndPamIfNeeded();
    8997        1523 :     LookForProjection();
    8998             : 
    8999        1523 :     CPLErr eErr = CE_None;
    9000        1523 :     if (eAccess == GA_Update)
    9001             :     {
    9002        1525 :         if ((m_eProfile == GTiffProfile::BASELINE) &&
    9003           7 :             (GetPamFlags() & GPF_DISABLED) == 0)
    9004             :         {
    9005           7 :             eErr = GDALPamDataset::SetSpatialRef(poSRS);
    9006             :         }
    9007             :         else
    9008             :         {
    9009        1511 :             if (GDALPamDataset::GetSpatialRef() != nullptr)
    9010             :             {
    9011             :                 // Cancel any existing SRS from PAM file.
    9012           1 :                 GDALPamDataset::SetSpatialRef(nullptr);
    9013             :             }
    9014        1511 :             m_bGeoTIFFInfoChanged = true;
    9015             :         }
    9016             :     }
    9017             :     else
    9018             :     {
    9019           5 :         CPLDebug("GTIFF", "SetSpatialRef() goes to PAM instead of TIFF tags");
    9020           5 :         eErr = GDALPamDataset::SetSpatialRef(poSRS);
    9021             :     }
    9022             : 
    9023        1523 :     if (eErr == CE_None)
    9024             :     {
    9025        1523 :         if (poSRS == nullptr || poSRS->IsEmpty())
    9026             :         {
    9027          14 :             if (!m_oSRS.IsEmpty())
    9028             :             {
    9029           4 :                 m_bForceUnsetProjection = true;
    9030             :             }
    9031          14 :             m_oSRS.Clear();
    9032             :         }
    9033             :         else
    9034             :         {
    9035        1509 :             m_oSRS = *poSRS;
    9036        1509 :             m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    9037             :         }
    9038             :     }
    9039             : 
    9040        1523 :     return eErr;
    9041             : }
    9042             : 
    9043             : /************************************************************************/
    9044             : /*                          SetGeoTransform()                           */
    9045             : /************************************************************************/
    9046             : 
    9047        1853 : CPLErr GTiffDataset::SetGeoTransform(const GDALGeoTransform &gt)
    9048             : 
    9049             : {
    9050        1853 :     if (m_bStreamingOut && m_bCrystalized)
    9051             :     {
    9052           1 :         ReportError(CE_Failure, CPLE_NotSupported,
    9053             :                     "Cannot modify geotransform at that point in a "
    9054             :                     "streamed output file");
    9055           1 :         return CE_Failure;
    9056             :     }
    9057             : 
    9058        1852 :     LoadGeoreferencingAndPamIfNeeded();
    9059             : 
    9060        1852 :     CPLErr eErr = CE_None;
    9061        1852 :     if (eAccess == GA_Update)
    9062             :     {
    9063        1846 :         if (!m_aoGCPs.empty())
    9064             :         {
    9065           1 :             ReportError(CE_Warning, CPLE_AppDefined,
    9066             :                         "GCPs previously set are going to be cleared "
    9067             :                         "due to the setting of a geotransform.");
    9068           1 :             m_bForceUnsetGTOrGCPs = true;
    9069           1 :             m_aoGCPs.clear();
    9070             :         }
    9071        1845 :         else if (gt.xorig == 0.0 && gt.xscale == 0.0 && gt.xrot == 0.0 &&
    9072           2 :                  gt.yorig == 0.0 && gt.yrot == 0.0 && gt.yscale == 0.0)
    9073             :         {
    9074           2 :             if (m_bGeoTransformValid)
    9075             :             {
    9076           2 :                 m_bForceUnsetGTOrGCPs = true;
    9077           2 :                 m_bGeoTIFFInfoChanged = true;
    9078             :             }
    9079           2 :             m_bGeoTransformValid = false;
    9080           2 :             m_gt = gt;
    9081           2 :             return CE_None;
    9082             :         }
    9083             : 
    9084        3697 :         if ((m_eProfile == GTiffProfile::BASELINE) &&
    9085           9 :             !CPLFetchBool(m_papszCreationOptions, "TFW", false) &&
    9086        1858 :             !CPLFetchBool(m_papszCreationOptions, "WORLDFILE", false) &&
    9087           5 :             (GetPamFlags() & GPF_DISABLED) == 0)
    9088             :         {
    9089           5 :             eErr = GDALPamDataset::SetGeoTransform(gt);
    9090             :         }
    9091             :         else
    9092             :         {
    9093             :             // Cancel any existing geotransform from PAM file.
    9094        1839 :             GDALPamDataset::DeleteGeoTransform();
    9095        1839 :             m_bGeoTIFFInfoChanged = true;
    9096             :         }
    9097             :     }
    9098             :     else
    9099             :     {
    9100           6 :         CPLDebug("GTIFF", "SetGeoTransform() goes to PAM instead of TIFF tags");
    9101           6 :         eErr = GDALPamDataset::SetGeoTransform(gt);
    9102             :     }
    9103             : 
    9104        1850 :     if (eErr == CE_None)
    9105             :     {
    9106        1850 :         m_gt = gt;
    9107        1850 :         m_bGeoTransformValid = true;
    9108             :     }
    9109             : 
    9110        1850 :     return eErr;
    9111             : }
    9112             : 
    9113             : /************************************************************************/
    9114             : /*                              SetGCPs()                               */
    9115             : /************************************************************************/
    9116             : 
    9117          22 : CPLErr GTiffDataset::SetGCPs(int nGCPCountIn, const GDAL_GCP *pasGCPListIn,
    9118             :                              const OGRSpatialReference *poGCPSRS)
    9119             : {
    9120          22 :     CPLErr eErr = CE_None;
    9121          22 :     LoadGeoreferencingAndPamIfNeeded();
    9122          22 :     LookForProjection();
    9123             : 
    9124          22 :     if (eAccess == GA_Update)
    9125             :     {
    9126          20 :         if (!m_aoGCPs.empty() && nGCPCountIn == 0)
    9127             :         {
    9128           3 :             m_bForceUnsetGTOrGCPs = true;
    9129             :         }
    9130          17 :         else if (nGCPCountIn > 0 && m_bGeoTransformValid)
    9131             :         {
    9132           5 :             ReportError(CE_Warning, CPLE_AppDefined,
    9133             :                         "A geotransform previously set is going to be cleared "
    9134             :                         "due to the setting of GCPs.");
    9135           5 :             m_gt = GDALGeoTransform();
    9136           5 :             m_bGeoTransformValid = false;
    9137           5 :             m_bForceUnsetGTOrGCPs = true;
    9138             :         }
    9139          20 :         if ((m_eProfile == GTiffProfile::BASELINE) &&
    9140           0 :             (GetPamFlags() & GPF_DISABLED) == 0)
    9141             :         {
    9142           0 :             eErr = GDALPamDataset::SetGCPs(nGCPCountIn, pasGCPListIn, poGCPSRS);
    9143             :         }
    9144             :         else
    9145             :         {
    9146          20 :             if (nGCPCountIn > knMAX_GCP_COUNT)
    9147             :             {
    9148           2 :                 if (GDALPamDataset::GetGCPCount() == 0 && !m_aoGCPs.empty())
    9149             :                 {
    9150           1 :                     m_bForceUnsetGTOrGCPs = true;
    9151             :                 }
    9152           2 :                 ReportError(CE_Warning, CPLE_AppDefined,
    9153             :                             "Trying to write %d GCPs, whereas the maximum "
    9154             :                             "supported in GeoTIFF tag is %d. "
    9155             :                             "Falling back to writing them to PAM",
    9156             :                             nGCPCountIn, knMAX_GCP_COUNT);
    9157           2 :                 eErr = GDALPamDataset::SetGCPs(nGCPCountIn, pasGCPListIn,
    9158             :                                                poGCPSRS);
    9159             :             }
    9160          18 :             else if (GDALPamDataset::GetGCPCount() > 0)
    9161             :             {
    9162             :                 // Cancel any existing GCPs from PAM file.
    9163           1 :                 GDALPamDataset::SetGCPs(
    9164             :                     0, nullptr,
    9165             :                     static_cast<const OGRSpatialReference *>(nullptr));
    9166             :             }
    9167          20 :             m_bGeoTIFFInfoChanged = true;
    9168             :         }
    9169             :     }
    9170             :     else
    9171             :     {
    9172           2 :         CPLDebug("GTIFF", "SetGCPs() goes to PAM instead of TIFF tags");
    9173           2 :         eErr = GDALPamDataset::SetGCPs(nGCPCountIn, pasGCPListIn, poGCPSRS);
    9174             :     }
    9175             : 
    9176          22 :     if (eErr == CE_None)
    9177             :     {
    9178          22 :         if (poGCPSRS == nullptr || poGCPSRS->IsEmpty())
    9179             :         {
    9180          11 :             if (!m_oSRS.IsEmpty())
    9181             :             {
    9182           5 :                 m_bForceUnsetProjection = true;
    9183             :             }
    9184          11 :             m_oSRS.Clear();
    9185             :         }
    9186             :         else
    9187             :         {
    9188          11 :             m_oSRS = *poGCPSRS;
    9189          11 :             m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    9190             :         }
    9191             : 
    9192          22 :         m_aoGCPs = gdal::GCP::fromC(pasGCPListIn, nGCPCountIn);
    9193             :     }
    9194             : 
    9195          22 :     return eErr;
    9196             : }
    9197             : 
    9198             : /************************************************************************/
    9199             : /*                            SetMetadata()                             */
    9200             : /************************************************************************/
    9201        2721 : CPLErr GTiffDataset::SetMetadata(CSLConstList papszMD, const char *pszDomain)
    9202             : 
    9203             : {
    9204        2721 :     LoadGeoreferencingAndPamIfNeeded();
    9205             : 
    9206        2721 :     if (m_bStreamingOut && m_bCrystalized)
    9207             :     {
    9208           1 :         ReportError(
    9209             :             CE_Failure, CPLE_NotSupported,
    9210             :             "Cannot modify metadata at that point in a streamed output file");
    9211           1 :         return CE_Failure;
    9212             :     }
    9213             : 
    9214        2720 :     if (pszDomain && EQUAL(pszDomain, "json:ISIS3"))
    9215             :     {
    9216           5 :         m_oISIS3Metadata.Deinit();
    9217           5 :         m_oMapISIS3MetadataItems.clear();
    9218             :     }
    9219             : 
    9220        2720 :     CPLErr eErr = CE_None;
    9221        2720 :     if (eAccess == GA_Update)
    9222             :     {
    9223        2717 :         if (pszDomain != nullptr && EQUAL(pszDomain, MD_DOMAIN_RPC))
    9224             :         {
    9225             :             // So that a subsequent GetMetadata() wouldn't override our new
    9226             :             // values
    9227          22 :             LoadMetadata();
    9228          22 :             m_bForceUnsetRPC = (CSLCount(papszMD) == 0);
    9229             :         }
    9230             : 
    9231        2717 :         if ((papszMD != nullptr) && (pszDomain != nullptr) &&
    9232        1881 :             EQUAL(pszDomain, "COLOR_PROFILE"))
    9233             :         {
    9234           0 :             m_bColorProfileMetadataChanged = true;
    9235             :         }
    9236        2717 :         else if (pszDomain == nullptr || !EQUAL(pszDomain, "_temporary_"))
    9237             :         {
    9238        2717 :             m_bMetadataChanged = true;
    9239             :             // Cancel any existing metadata from PAM file.
    9240        2717 :             if (GDALPamDataset::GetMetadata(pszDomain) != nullptr)
    9241           1 :                 GDALPamDataset::SetMetadata(nullptr, pszDomain);
    9242             :         }
    9243             : 
    9244        5398 :         if ((pszDomain == nullptr || EQUAL(pszDomain, "")) &&
    9245        2681 :             CSLFetchNameValue(papszMD, GDALMD_AREA_OR_POINT) != nullptr)
    9246             :         {
    9247        2046 :             const char *pszPrevValue = GetMetadataItem(GDALMD_AREA_OR_POINT);
    9248             :             const char *pszNewValue =
    9249        2046 :                 CSLFetchNameValue(papszMD, GDALMD_AREA_OR_POINT);
    9250        2046 :             if (pszPrevValue == nullptr || pszNewValue == nullptr ||
    9251        1623 :                 !EQUAL(pszPrevValue, pszNewValue))
    9252             :             {
    9253         427 :                 LookForProjection();
    9254         427 :                 m_bGeoTIFFInfoChanged = true;
    9255             :             }
    9256             :         }
    9257             : 
    9258        2717 :         if (pszDomain != nullptr && EQUAL(pszDomain, "xml:XMP"))
    9259             :         {
    9260           2 :             if (papszMD != nullptr && *papszMD != nullptr)
    9261             :             {
    9262           1 :                 int nTagSize = static_cast<int>(strlen(*papszMD));
    9263           1 :                 TIFFSetField(m_hTIFF, TIFFTAG_XMLPACKET, nTagSize, *papszMD);
    9264             :             }
    9265             :             else
    9266             :             {
    9267           1 :                 TIFFUnsetField(m_hTIFF, TIFFTAG_XMLPACKET);
    9268             :             }
    9269             :         }
    9270             :     }
    9271             :     else
    9272             :     {
    9273           3 :         CPLDebug(
    9274             :             "GTIFF",
    9275             :             "GTiffDataset::SetMetadata() goes to PAM instead of TIFF tags");
    9276           3 :         eErr = GDALPamDataset::SetMetadata(papszMD, pszDomain);
    9277             :     }
    9278             : 
    9279        2720 :     if (eErr == CE_None)
    9280             :     {
    9281        2720 :         eErr = m_oGTiffMDMD.SetMetadata(papszMD, pszDomain);
    9282             :     }
    9283        2720 :     return eErr;
    9284             : }
    9285             : 
    9286             : /************************************************************************/
    9287             : /*                          SetMetadataItem()                           */
    9288             : /************************************************************************/
    9289             : 
    9290        5913 : CPLErr GTiffDataset::SetMetadataItem(const char *pszName, const char *pszValue,
    9291             :                                      const char *pszDomain)
    9292             : 
    9293             : {
    9294        5913 :     LoadGeoreferencingAndPamIfNeeded();
    9295             : 
    9296        5913 :     if (m_bStreamingOut && m_bCrystalized)
    9297             :     {
    9298           1 :         ReportError(
    9299             :             CE_Failure, CPLE_NotSupported,
    9300             :             "Cannot modify metadata at that point in a streamed output file");
    9301           1 :         return CE_Failure;
    9302             :     }
    9303             : 
    9304        5912 :     if (pszDomain && EQUAL(pszDomain, "json:ISIS3"))
    9305             :     {
    9306           1 :         ReportError(CE_Failure, CPLE_NotSupported,
    9307             :                     "Updating part of json:ISIS3 is not supported. "
    9308             :                     "Use SetMetadata() instead");
    9309           1 :         return CE_Failure;
    9310             :     }
    9311             : 
    9312        5911 :     CPLErr eErr = CE_None;
    9313        5911 :     if (eAccess == GA_Update)
    9314             :     {
    9315        5904 :         if ((pszDomain != nullptr) && EQUAL(pszDomain, "COLOR_PROFILE"))
    9316             :         {
    9317           8 :             m_bColorProfileMetadataChanged = true;
    9318             :         }
    9319        5896 :         else if (pszDomain == nullptr || !EQUAL(pszDomain, "_temporary_"))
    9320             :         {
    9321        5896 :             m_bMetadataChanged = true;
    9322             :             // Cancel any existing metadata from PAM file.
    9323        5896 :             if (GDALPamDataset::GetMetadataItem(pszName, pszDomain) != nullptr)
    9324           1 :                 GDALPamDataset::SetMetadataItem(pszName, nullptr, pszDomain);
    9325             :         }
    9326             : 
    9327        5904 :         if ((pszDomain == nullptr || EQUAL(pszDomain, "")) &&
    9328          84 :             pszName != nullptr && EQUAL(pszName, GDALMD_AREA_OR_POINT))
    9329             :         {
    9330           7 :             LookForProjection();
    9331           7 :             m_bGeoTIFFInfoChanged = true;
    9332             :         }
    9333             :     }
    9334             :     else
    9335             :     {
    9336           7 :         CPLDebug(
    9337             :             "GTIFF",
    9338             :             "GTiffDataset::SetMetadataItem() goes to PAM instead of TIFF tags");
    9339           7 :         eErr = GDALPamDataset::SetMetadataItem(pszName, pszValue, pszDomain);
    9340             :     }
    9341             : 
    9342        5911 :     if (eErr == CE_None)
    9343             :     {
    9344        5911 :         eErr = m_oGTiffMDMD.SetMetadataItem(pszName, pszValue, pszDomain);
    9345             :     }
    9346             : 
    9347        5911 :     return eErr;
    9348             : }
    9349             : 
    9350             : /************************************************************************/
    9351             : /*                           CreateMaskBand()                           */
    9352             : /************************************************************************/
    9353             : 
    9354         100 : CPLErr GTiffDataset::CreateMaskBand(int nFlagsIn)
    9355             : {
    9356         100 :     ScanDirectories();
    9357             : 
    9358         100 :     if (m_poMaskDS != nullptr)
    9359             :     {
    9360           1 :         ReportError(CE_Failure, CPLE_AppDefined,
    9361             :                     "This TIFF dataset has already an internal mask band");
    9362           1 :         return CE_Failure;
    9363             :     }
    9364          99 :     else if (MustCreateInternalMask())
    9365             :     {
    9366          86 :         if (nFlagsIn != GMF_PER_DATASET)
    9367             :         {
    9368           1 :             ReportError(CE_Failure, CPLE_AppDefined,
    9369             :                         "The only flag value supported for internal mask is "
    9370             :                         "GMF_PER_DATASET");
    9371           1 :             return CE_Failure;
    9372             :         }
    9373             : 
    9374          85 :         int l_nCompression = COMPRESSION_PACKBITS;
    9375          85 :         if (strstr(GDALGetMetadataItem(GDALGetDriverByName("GTiff"),
    9376             :                                        GDAL_DMD_CREATIONOPTIONLIST, nullptr),
    9377          85 :                    "<Value>DEFLATE</Value>") != nullptr)
    9378          85 :             l_nCompression = COMPRESSION_ADOBE_DEFLATE;
    9379             : 
    9380             :         /* --------------------------------------------------------------------
    9381             :          */
    9382             :         /*      If we don't have read access, then create the mask externally.
    9383             :          */
    9384             :         /* --------------------------------------------------------------------
    9385             :          */
    9386          85 :         if (GetAccess() != GA_Update)
    9387             :         {
    9388           1 :             ReportError(CE_Warning, CPLE_AppDefined,
    9389             :                         "File open for read-only accessing, "
    9390             :                         "creating mask externally.");
    9391             : 
    9392           1 :             return GDALPamDataset::CreateMaskBand(nFlagsIn);
    9393             :         }
    9394             : 
    9395          84 :         if (m_bLayoutIFDSBeforeData && !m_bKnownIncompatibleEdition &&
    9396           0 :             !m_bWriteKnownIncompatibleEdition)
    9397             :         {
    9398           0 :             ReportError(CE_Warning, CPLE_AppDefined,
    9399             :                         "Adding a mask invalidates the "
    9400             :                         "LAYOUT=IFDS_BEFORE_DATA property");
    9401           0 :             m_bKnownIncompatibleEdition = true;
    9402           0 :             m_bWriteKnownIncompatibleEdition = true;
    9403             :         }
    9404             : 
    9405          84 :         bool bIsOverview = false;
    9406          84 :         uint32_t nSubType = 0;
    9407          84 :         if (TIFFGetField(m_hTIFF, TIFFTAG_SUBFILETYPE, &nSubType))
    9408             :         {
    9409           8 :             bIsOverview = (nSubType & FILETYPE_REDUCEDIMAGE) != 0;
    9410             : 
    9411           8 :             if ((nSubType & FILETYPE_MASK) != 0)
    9412             :             {
    9413           0 :                 ReportError(CE_Failure, CPLE_AppDefined,
    9414             :                             "Cannot create a mask on a TIFF mask IFD !");
    9415           0 :                 return CE_Failure;
    9416             :             }
    9417             :         }
    9418             : 
    9419          84 :         const int bIsTiled = TIFFIsTiled(m_hTIFF);
    9420             : 
    9421          84 :         FlushDirectory();
    9422             : 
    9423          84 :         const toff_t nOffset = GTIFFWriteDirectory(
    9424             :             m_hTIFF,
    9425             :             bIsOverview ? FILETYPE_REDUCEDIMAGE | FILETYPE_MASK : FILETYPE_MASK,
    9426             :             nRasterXSize, nRasterYSize, 1, PLANARCONFIG_CONTIG, 1,
    9427             :             m_nBlockXSize, m_nBlockYSize, bIsTiled, l_nCompression,
    9428             :             PHOTOMETRIC_MASK, PREDICTOR_NONE, SAMPLEFORMAT_UINT, nullptr,
    9429             :             nullptr, nullptr, 0, nullptr, "", nullptr, nullptr, nullptr,
    9430          84 :             nullptr, m_bWriteCOGLayout);
    9431             : 
    9432          84 :         ReloadDirectory();
    9433             : 
    9434          84 :         if (nOffset == 0)
    9435           0 :             return CE_Failure;
    9436             : 
    9437          84 :         m_poMaskDS = std::make_shared<GTiffDataset>();
    9438          84 :         m_poMaskDS->eAccess = GA_Update;
    9439          84 :         m_poMaskDS->m_poBaseDS = this;
    9440          84 :         m_poMaskDS->m_poImageryDS = this;
    9441          84 :         m_poMaskDS->ShareLockWithParentDataset(this);
    9442          84 :         m_poMaskDS->m_osFilename = m_osFilename;
    9443          84 :         m_poMaskDS->m_bPromoteTo8Bits = CPLTestBool(
    9444             :             CPLGetConfigOption("GDAL_TIFF_INTERNAL_MASK_TO_8BIT", "YES"));
    9445          84 :         return m_poMaskDS->OpenOffset(VSI_TIFFOpenChild(m_hTIFF), nOffset,
    9446          84 :                                       GA_Update);
    9447             :     }
    9448             : 
    9449          13 :     return GDALPamDataset::CreateMaskBand(nFlagsIn);
    9450             : }
    9451             : 
    9452             : /************************************************************************/
    9453             : /*                       MustCreateInternalMask()                       */
    9454             : /************************************************************************/
    9455             : 
    9456         137 : bool GTiffDataset::MustCreateInternalMask()
    9457             : {
    9458         137 :     return CPLTestBool(CPLGetConfigOption("GDAL_TIFF_INTERNAL_MASK", "YES"));
    9459             : }
    9460             : 
    9461             : /************************************************************************/
    9462             : /*                           CreateMaskBand()                           */
    9463             : /************************************************************************/
    9464             : 
    9465          29 : CPLErr GTiffRasterBand::CreateMaskBand(int nFlagsIn)
    9466             : {
    9467          29 :     m_poGDS->ScanDirectories();
    9468             : 
    9469          29 :     if (m_poGDS->m_poMaskDS != nullptr)
    9470             :     {
    9471           5 :         ReportError(CE_Failure, CPLE_AppDefined,
    9472             :                     "This TIFF dataset has already an internal mask band");
    9473           5 :         return CE_Failure;
    9474             :     }
    9475             : 
    9476             :     const char *pszGDAL_TIFF_INTERNAL_MASK =
    9477          24 :         CPLGetConfigOption("GDAL_TIFF_INTERNAL_MASK", nullptr);
    9478          27 :     if ((pszGDAL_TIFF_INTERNAL_MASK &&
    9479          24 :          CPLTestBool(pszGDAL_TIFF_INTERNAL_MASK)) ||
    9480             :         nFlagsIn == GMF_PER_DATASET)
    9481             :     {
    9482          16 :         return m_poGDS->CreateMaskBand(nFlagsIn);
    9483             :     }
    9484             : 
    9485           8 :     return GDALPamRasterBand::CreateMaskBand(nFlagsIn);
    9486             : }
    9487             : 
    9488             : /************************************************************************/
    9489             : /*                            ClampCTEntry()                            */
    9490             : /************************************************************************/
    9491             : 
    9492      236415 : /* static */ unsigned short GTiffDataset::ClampCTEntry(int iColor, int iComp,
    9493             :                                                        int nCTEntryVal,
    9494             :                                                        int nMultFactor)
    9495             : {
    9496      236415 :     const int nVal = nCTEntryVal * nMultFactor;
    9497      236415 :     if (nVal < 0)
    9498             :     {
    9499           0 :         CPLError(CE_Warning, CPLE_AppDefined,
    9500             :                  "Color table entry [%d][%d] = %d, clamped to 0", iColor, iComp,
    9501             :                  nCTEntryVal);
    9502           0 :         return 0;
    9503             :     }
    9504      236415 :     if (nVal > 65535)
    9505             :     {
    9506           2 :         CPLError(CE_Warning, CPLE_AppDefined,
    9507             :                  "Color table entry [%d][%d] = %d, clamped to 65535", iColor,
    9508             :                  iComp, nCTEntryVal);
    9509           2 :         return 65535;
    9510             :     }
    9511      236413 :     return static_cast<unsigned short>(nVal);
    9512             : }

Generated by: LCOV version 1.14