LCOV - code coverage report
Current view: top level - frmts/gtiff - gtiffdataset_write.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 3685 4031 91.4 %
Date: 2025-01-18 12:42:00 Functions: 108 132 81.8 %

          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             : 
      18             : #include <cassert>
      19             : #include <cerrno>
      20             : 
      21             : #include <algorithm>
      22             : #include <cmath>
      23             : #include <limits>
      24             : #include <memory>
      25             : #include <mutex>
      26             : #include <set>
      27             : #include <string>
      28             : #include <tuple>
      29             : #include <utility>
      30             : 
      31             : #include "cpl_error.h"
      32             : #include "cpl_error_internal.h"  // CPLErrorHandlerAccumulatorStruct
      33             : #include "cpl_md5.h"
      34             : #include "cpl_vsi.h"
      35             : #include "cpl_vsi_virtual.h"
      36             : #include "cpl_worker_thread_pool.h"
      37             : #include "fetchbufferdirectio.h"
      38             : #include "gdal_mdreader.h"          // GDALWriteRPCTXTFile()
      39             : #include "gdal_priv_templates.hpp"  // GDALIsValueInRange<>
      40             : #include "gdal_thread_pool.h"       // GDALGetGlobalThreadPool()
      41             : #include "geovalues.h"              // RasterPixelIsPoint
      42             : #include "gt_jpeg_copy.h"
      43             : #include "gt_overview.h"  // GTIFFBuildOverviewMetadata()
      44             : #include "quant_table_md5sum.h"
      45             : #include "quant_table_md5sum_jpeg9e.h"
      46             : #include "tif_jxl.h"
      47             : #include "tifvsi.h"
      48             : #include "xtiffio.h"
      49             : 
      50             : #if LIFFLIB_VERSION > 20230908 || defined(INTERNAL_LIBTIFF)
      51             : /* libtiff < 4.6.1 doesn't generate a LERC mask for multi-band contig configuration */
      52             : #define LIBTIFF_MULTIBAND_LERC_NAN_OK
      53             : #endif
      54             : 
      55             : static const int knGTIFFJpegTablesModeDefault = JPEGTABLESMODE_QUANT;
      56             : 
      57             : static constexpr const char szPROFILE_BASELINE[] = "BASELINE";
      58             : static constexpr const char szPROFILE_GeoTIFF[] = "GeoTIFF";
      59             : static constexpr const char szPROFILE_GDALGeoTIFF[] = "GDALGeoTIFF";
      60             : 
      61             : // Due to libgeotiff/xtiff.c declaring TIFFTAG_GEOTIEPOINTS with field_readcount
      62             : // and field_writecount == -1 == TIFF_VARIABLE, we are limited to writing
      63             : // 65535 values in that tag. That could potentially be overcome by changing the tag
      64             : // declaration to using TIFF_VARIABLE2 where the count is a uint32_t.
      65             : constexpr int knMAX_GCP_COUNT =
      66             :     static_cast<int>(std::numeric_limits<uint16_t>::max() / 6);
      67             : 
      68             : enum
      69             : {
      70             :     ENDIANNESS_NATIVE,
      71             :     ENDIANNESS_LITTLE,
      72             :     ENDIANNESS_BIG
      73             : };
      74             : 
      75       12102 : static signed char GTiffGetWebPLevel(CSLConstList papszOptions)
      76             : {
      77       12102 :     int nWebPLevel = DEFAULT_WEBP_LEVEL;
      78       12102 :     const char *pszValue = CSLFetchNameValue(papszOptions, "WEBP_LEVEL");
      79       12102 :     if (pszValue != nullptr)
      80             :     {
      81          51 :         nWebPLevel = atoi(pszValue);
      82          51 :         if (!(nWebPLevel >= 1 && nWebPLevel <= 100))
      83             :         {
      84           0 :             CPLError(CE_Warning, CPLE_IllegalArg,
      85             :                      "WEBP_LEVEL=%s value not recognised, ignoring.", pszValue);
      86           0 :             nWebPLevel = DEFAULT_WEBP_LEVEL;
      87             :         }
      88             :     }
      89       12102 :     return static_cast<signed char>(nWebPLevel);
      90             : }
      91             : 
      92       12108 : static bool GTiffGetWebPLossless(CSLConstList papszOptions)
      93             : {
      94       12108 :     return CPLFetchBool(papszOptions, "WEBP_LOSSLESS", false);
      95             : }
      96             : 
      97       12174 : static double GTiffGetLERCMaxZError(CSLConstList papszOptions)
      98             : {
      99       12174 :     return CPLAtof(CSLFetchNameValueDef(papszOptions, "MAX_Z_ERROR", "0.0"));
     100             : }
     101             : 
     102        5127 : static double GTiffGetLERCMaxZErrorOverview(CSLConstList papszOptions)
     103             : {
     104        5127 :     return CPLAtof(CSLFetchNameValueDef(
     105             :         papszOptions, "MAX_Z_ERROR_OVERVIEW",
     106        5127 :         CSLFetchNameValueDef(papszOptions, "MAX_Z_ERROR", "0.0")));
     107             : }
     108             : 
     109             : #if HAVE_JXL
     110       12175 : static bool GTiffGetJXLLossless(CSLConstList papszOptions)
     111             : {
     112       12175 :     return CPLTestBool(
     113       12175 :         CSLFetchNameValueDef(papszOptions, "JXL_LOSSLESS", "TRUE"));
     114             : }
     115             : 
     116       12175 : static uint32_t GTiffGetJXLEffort(CSLConstList papszOptions)
     117             : {
     118       12175 :     return atoi(CSLFetchNameValueDef(papszOptions, "JXL_EFFORT", "5"));
     119             : }
     120             : 
     121       12095 : static float GTiffGetJXLDistance(CSLConstList papszOptions)
     122             : {
     123             :     return static_cast<float>(
     124       12095 :         CPLAtof(CSLFetchNameValueDef(papszOptions, "JXL_DISTANCE", "1.0")));
     125             : }
     126             : 
     127       12175 : static float GTiffGetJXLAlphaDistance(CSLConstList papszOptions)
     128             : {
     129       12175 :     return static_cast<float>(CPLAtof(
     130       12175 :         CSLFetchNameValueDef(papszOptions, "JXL_ALPHA_DISTANCE", "-1.0")));
     131             : }
     132             : 
     133             : #endif
     134             : 
     135             : /************************************************************************/
     136             : /*                           FillEmptyTiles()                           */
     137             : /************************************************************************/
     138             : 
     139        5257 : CPLErr GTiffDataset::FillEmptyTiles()
     140             : 
     141             : {
     142             :     /* -------------------------------------------------------------------- */
     143             :     /*      How many blocks are there in this file?                         */
     144             :     /* -------------------------------------------------------------------- */
     145       10514 :     const int nBlockCount = m_nPlanarConfig == PLANARCONFIG_SEPARATE
     146        5257 :                                 ? m_nBlocksPerBand * nBands
     147             :                                 : m_nBlocksPerBand;
     148             : 
     149             :     /* -------------------------------------------------------------------- */
     150             :     /*      Fetch block maps.                                               */
     151             :     /* -------------------------------------------------------------------- */
     152        5257 :     toff_t *panByteCounts = nullptr;
     153             : 
     154        5257 :     if (TIFFIsTiled(m_hTIFF))
     155         918 :         TIFFGetField(m_hTIFF, TIFFTAG_TILEBYTECOUNTS, &panByteCounts);
     156             :     else
     157        4339 :         TIFFGetField(m_hTIFF, TIFFTAG_STRIPBYTECOUNTS, &panByteCounts);
     158             : 
     159        5257 :     if (panByteCounts == nullptr)
     160             :     {
     161             :         // Got here with libtiff 3.9.3 and tiff_write_8 test.
     162           0 :         ReportError(CE_Failure, CPLE_AppDefined,
     163             :                     "FillEmptyTiles() failed because panByteCounts == NULL");
     164           0 :         return CE_Failure;
     165             :     }
     166             : 
     167             :     /* -------------------------------------------------------------------- */
     168             :     /*      Prepare a blank data buffer to write for uninitialized blocks.  */
     169             :     /* -------------------------------------------------------------------- */
     170             :     const GPtrDiff_t nBlockBytes =
     171        5257 :         TIFFIsTiled(m_hTIFF) ? static_cast<GPtrDiff_t>(TIFFTileSize(m_hTIFF))
     172        4339 :                              : static_cast<GPtrDiff_t>(TIFFStripSize(m_hTIFF));
     173             : 
     174        5257 :     GByte *pabyData = static_cast<GByte *>(VSI_CALLOC_VERBOSE(nBlockBytes, 1));
     175        5257 :     if (pabyData == nullptr)
     176             :     {
     177           0 :         return CE_Failure;
     178             :     }
     179             : 
     180             :     // Force tiles completely filled with the nodata value to be written.
     181        5257 :     m_bWriteEmptyTiles = true;
     182             : 
     183             :     /* -------------------------------------------------------------------- */
     184             :     /*      If set, fill data buffer with no data value.                    */
     185             :     /* -------------------------------------------------------------------- */
     186        5257 :     if ((m_bNoDataSet && m_dfNoDataValue != 0.0) ||
     187        5030 :         (m_bNoDataSetAsInt64 && m_nNoDataValueInt64 != 0) ||
     188        5028 :         (m_bNoDataSetAsUInt64 && m_nNoDataValueUInt64 != 0))
     189             :     {
     190         231 :         const GDALDataType eDataType = GetRasterBand(1)->GetRasterDataType();
     191         231 :         const int nDataTypeSize = GDALGetDataTypeSizeBytes(eDataType);
     192         231 :         if (nDataTypeSize &&
     193         231 :             nDataTypeSize * 8 == static_cast<int>(m_nBitsPerSample))
     194             :         {
     195         220 :             if (m_bNoDataSetAsInt64)
     196             :             {
     197           3 :                 GDALCopyWords64(&m_nNoDataValueInt64, GDT_Int64, 0, pabyData,
     198             :                                 eDataType, nDataTypeSize,
     199           3 :                                 nBlockBytes / nDataTypeSize);
     200             :             }
     201         217 :             else if (m_bNoDataSetAsUInt64)
     202             :             {
     203           2 :                 GDALCopyWords64(&m_nNoDataValueUInt64, GDT_UInt64, 0, pabyData,
     204             :                                 eDataType, nDataTypeSize,
     205           2 :                                 nBlockBytes / nDataTypeSize);
     206             :             }
     207             :             else
     208             :             {
     209         215 :                 double dfNoData = m_dfNoDataValue;
     210         215 :                 GDALCopyWords64(&dfNoData, GDT_Float64, 0, pabyData, eDataType,
     211         215 :                                 nDataTypeSize, nBlockBytes / nDataTypeSize);
     212         220 :             }
     213             :         }
     214          11 :         else if (nDataTypeSize)
     215             :         {
     216             :             // Handle non power-of-two depths.
     217             :             // Ideally make a packed buffer, but that is a bit tedious,
     218             :             // so use the normal I/O interfaces.
     219             : 
     220          11 :             CPLFree(pabyData);
     221             : 
     222          11 :             pabyData = static_cast<GByte *>(VSI_MALLOC3_VERBOSE(
     223             :                 m_nBlockXSize, m_nBlockYSize, nDataTypeSize));
     224          11 :             if (pabyData == nullptr)
     225           0 :                 return CE_Failure;
     226          11 :             if (m_bNoDataSetAsInt64)
     227             :             {
     228           0 :                 GDALCopyWords64(&m_nNoDataValueInt64, GDT_Int64, 0, pabyData,
     229             :                                 eDataType, nDataTypeSize,
     230           0 :                                 static_cast<GPtrDiff_t>(m_nBlockXSize) *
     231           0 :                                     m_nBlockYSize);
     232             :             }
     233          11 :             else if (m_bNoDataSetAsUInt64)
     234             :             {
     235           0 :                 GDALCopyWords64(&m_nNoDataValueUInt64, GDT_UInt64, 0, pabyData,
     236             :                                 eDataType, nDataTypeSize,
     237           0 :                                 static_cast<GPtrDiff_t>(m_nBlockXSize) *
     238           0 :                                     m_nBlockYSize);
     239             :             }
     240             :             else
     241             :             {
     242          11 :                 GDALCopyWords64(&m_dfNoDataValue, GDT_Float64, 0, pabyData,
     243             :                                 eDataType, nDataTypeSize,
     244          11 :                                 static_cast<GPtrDiff_t>(m_nBlockXSize) *
     245          11 :                                     m_nBlockYSize);
     246             :             }
     247          11 :             CPLErr eErr = CE_None;
     248          46 :             for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
     249             :             {
     250          35 :                 if (panByteCounts[iBlock] == 0)
     251             :                 {
     252          18 :                     if (m_nPlanarConfig == PLANARCONFIG_SEPARATE || nBands == 1)
     253             :                     {
     254          24 :                         if (GetRasterBand(1 + iBlock / m_nBlocksPerBand)
     255          12 :                                 ->WriteBlock((iBlock % m_nBlocksPerBand) %
     256          12 :                                                  m_nBlocksPerRow,
     257          12 :                                              (iBlock % m_nBlocksPerBand) /
     258          12 :                                                  m_nBlocksPerRow,
     259          12 :                                              pabyData) != CE_None)
     260             :                         {
     261           0 :                             eErr = CE_Failure;
     262             :                         }
     263             :                     }
     264             :                     else
     265             :                     {
     266             :                         // In contig case, don't directly call WriteBlock(), as
     267             :                         // it could cause useless decompression-recompression.
     268           6 :                         const int nXOff =
     269           6 :                             (iBlock % m_nBlocksPerRow) * m_nBlockXSize;
     270           6 :                         const int nYOff =
     271           6 :                             (iBlock / m_nBlocksPerRow) * m_nBlockYSize;
     272           6 :                         const int nXSize =
     273           6 :                             (nXOff + m_nBlockXSize <= nRasterXSize)
     274           6 :                                 ? m_nBlockXSize
     275           2 :                                 : nRasterXSize - nXOff;
     276           6 :                         const int nYSize =
     277           6 :                             (nYOff + m_nBlockYSize <= nRasterYSize)
     278           6 :                                 ? m_nBlockYSize
     279           3 :                                 : nRasterYSize - nYOff;
     280          18 :                         for (int iBand = 1; iBand <= nBands; ++iBand)
     281             :                         {
     282          12 :                             if (GetRasterBand(iBand)->RasterIO(
     283             :                                     GF_Write, nXOff, nYOff, nXSize, nYSize,
     284             :                                     pabyData, nXSize, nYSize, eDataType, 0, 0,
     285          12 :                                     nullptr) != CE_None)
     286             :                             {
     287           0 :                                 eErr = CE_Failure;
     288             :                             }
     289             :                         }
     290             :                     }
     291             :                 }
     292             :             }
     293          11 :             CPLFree(pabyData);
     294          11 :             return eErr;
     295         220 :         }
     296             :     }
     297             : 
     298             :     /* -------------------------------------------------------------------- */
     299             :     /*      When we must fill with zeroes, try to create non-sparse file    */
     300             :     /*      w.r.t TIFF spec ... as a sparse file w.r.t filesystem, ie by    */
     301             :     /*      seeking to end of file instead of writing zero blocks.          */
     302             :     /* -------------------------------------------------------------------- */
     303        5026 :     else if (m_nCompression == COMPRESSION_NONE && (m_nBitsPerSample % 8) == 0)
     304             :     {
     305        3760 :         CPLErr eErr = CE_None;
     306             :         // Only use libtiff to write the first sparse block to ensure that it
     307             :         // will serialize offset and count arrays back to disk.
     308        3760 :         int nCountBlocksToZero = 0;
     309     2274640 :         for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
     310             :         {
     311     2270880 :             if (panByteCounts[iBlock] == 0)
     312             :             {
     313     2180830 :                 if (nCountBlocksToZero == 0)
     314             :                 {
     315         926 :                     const bool bWriteEmptyTilesBak = m_bWriteEmptyTiles;
     316         926 :                     m_bWriteEmptyTiles = true;
     317         926 :                     const bool bOK = WriteEncodedTileOrStrip(iBlock, pabyData,
     318         926 :                                                              FALSE) == CE_None;
     319         926 :                     m_bWriteEmptyTiles = bWriteEmptyTilesBak;
     320         926 :                     if (!bOK)
     321             :                     {
     322           2 :                         eErr = CE_Failure;
     323           2 :                         break;
     324             :                     }
     325             :                 }
     326     2180830 :                 nCountBlocksToZero++;
     327             :             }
     328             :         }
     329        3760 :         CPLFree(pabyData);
     330             : 
     331        3760 :         --nCountBlocksToZero;
     332             : 
     333             :         // And then seek to end of file for other ones.
     334        3760 :         if (nCountBlocksToZero > 0)
     335             :         {
     336         306 :             toff_t *panByteOffsets = nullptr;
     337             : 
     338         306 :             if (TIFFIsTiled(m_hTIFF))
     339          86 :                 TIFFGetField(m_hTIFF, TIFFTAG_TILEOFFSETS, &panByteOffsets);
     340             :             else
     341         220 :                 TIFFGetField(m_hTIFF, TIFFTAG_STRIPOFFSETS, &panByteOffsets);
     342             : 
     343         306 :             if (panByteOffsets == nullptr)
     344             :             {
     345           0 :                 ReportError(
     346             :                     CE_Failure, CPLE_AppDefined,
     347             :                     "FillEmptyTiles() failed because panByteOffsets == NULL");
     348           0 :                 return CE_Failure;
     349             :             }
     350             : 
     351         306 :             VSILFILE *fpTIF = VSI_TIFFGetVSILFile(TIFFClientdata(m_hTIFF));
     352         306 :             VSIFSeekL(fpTIF, 0, SEEK_END);
     353         306 :             const vsi_l_offset nOffset = VSIFTellL(fpTIF);
     354             : 
     355         306 :             vsi_l_offset iBlockToZero = 0;
     356     2187930 :             for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
     357             :             {
     358     2187630 :                 if (panByteCounts[iBlock] == 0)
     359             :                 {
     360     2179900 :                     panByteOffsets[iBlock] = static_cast<toff_t>(
     361     2179900 :                         nOffset + iBlockToZero * nBlockBytes);
     362     2179900 :                     panByteCounts[iBlock] = nBlockBytes;
     363     2179900 :                     iBlockToZero++;
     364             :                 }
     365             :             }
     366         306 :             CPLAssert(iBlockToZero ==
     367             :                       static_cast<vsi_l_offset>(nCountBlocksToZero));
     368             : 
     369         306 :             if (VSIFTruncateL(fpTIF, nOffset + iBlockToZero * nBlockBytes) != 0)
     370             :             {
     371           0 :                 eErr = CE_Failure;
     372           0 :                 ReportError(CE_Failure, CPLE_FileIO,
     373             :                             "Cannot initialize empty blocks");
     374             :             }
     375             :         }
     376             : 
     377        3760 :         return eErr;
     378             :     }
     379             : 
     380             :     /* -------------------------------------------------------------------- */
     381             :     /*      Check all blocks, writing out data for uninitialized blocks.    */
     382             :     /* -------------------------------------------------------------------- */
     383             : 
     384        1486 :     GByte *pabyRaw = nullptr;
     385        1486 :     vsi_l_offset nRawSize = 0;
     386        1486 :     CPLErr eErr = CE_None;
     387       42642 :     for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
     388             :     {
     389       41163 :         if (panByteCounts[iBlock] == 0)
     390             :         {
     391        8627 :             if (pabyRaw == nullptr)
     392             :             {
     393        1562 :                 if (WriteEncodedTileOrStrip(iBlock, pabyData, FALSE) != CE_None)
     394             :                 {
     395           7 :                     eErr = CE_Failure;
     396           7 :                     break;
     397             :                 }
     398             : 
     399        1555 :                 vsi_l_offset nOffset = 0;
     400        1555 :                 if (!IsBlockAvailable(iBlock, &nOffset, &nRawSize, nullptr))
     401           0 :                     break;
     402             : 
     403             :                 // When using compression, get back the compressed block
     404             :                 // so we can use the raw API to write it faster.
     405        1555 :                 if (m_nCompression != COMPRESSION_NONE)
     406             :                 {
     407             :                     pabyRaw = static_cast<GByte *>(
     408         355 :                         VSI_MALLOC_VERBOSE(static_cast<size_t>(nRawSize)));
     409         355 :                     if (pabyRaw)
     410             :                     {
     411             :                         VSILFILE *fp =
     412         355 :                             VSI_TIFFGetVSILFile(TIFFClientdata(m_hTIFF));
     413         355 :                         const vsi_l_offset nCurOffset = VSIFTellL(fp);
     414         355 :                         VSIFSeekL(fp, nOffset, SEEK_SET);
     415         355 :                         VSIFReadL(pabyRaw, 1, static_cast<size_t>(nRawSize),
     416             :                                   fp);
     417         355 :                         VSIFSeekL(fp, nCurOffset, SEEK_SET);
     418             :                     }
     419             :                 }
     420             :             }
     421             :             else
     422             :             {
     423        7065 :                 WriteRawStripOrTile(iBlock, pabyRaw,
     424             :                                     static_cast<GPtrDiff_t>(nRawSize));
     425             :             }
     426             :         }
     427             :     }
     428             : 
     429        1486 :     CPLFree(pabyData);
     430        1486 :     VSIFree(pabyRaw);
     431        1486 :     return eErr;
     432             : }
     433             : 
     434             : /************************************************************************/
     435             : /*                         HasOnlyNoData()                              */
     436             : /************************************************************************/
     437             : 
     438       35011 : bool GTiffDataset::HasOnlyNoData(const void *pBuffer, int nWidth, int nHeight,
     439             :                                  int nLineStride, int nComponents)
     440             : {
     441       35011 :     if (m_nSampleFormat == SAMPLEFORMAT_COMPLEXINT ||
     442       35011 :         m_nSampleFormat == SAMPLEFORMAT_COMPLEXIEEEFP)
     443           0 :         return false;
     444       35011 :     if (m_bNoDataSetAsInt64 || m_bNoDataSetAsUInt64)
     445           2 :         return false;  // FIXME: over pessimistic
     446       70018 :     return GDALBufferHasOnlyNoData(
     447       35009 :         pBuffer, m_bNoDataSet ? m_dfNoDataValue : 0.0, nWidth, nHeight,
     448       35009 :         nLineStride, nComponents, m_nBitsPerSample,
     449       35009 :         m_nSampleFormat == SAMPLEFORMAT_UINT  ? GSF_UNSIGNED_INT
     450         735 :         : m_nSampleFormat == SAMPLEFORMAT_INT ? GSF_SIGNED_INT
     451       35009 :                                               : GSF_FLOATING_POINT);
     452             : }
     453             : 
     454             : /************************************************************************/
     455             : /*                     IsFirstPixelEqualToNoData()                      */
     456             : /************************************************************************/
     457             : 
     458      158335 : inline bool GTiffDataset::IsFirstPixelEqualToNoData(const void *pBuffer)
     459             : {
     460      158335 :     const GDALDataType eDT = GetRasterBand(1)->GetRasterDataType();
     461      158337 :     const double dfEffectiveNoData = (m_bNoDataSet) ? m_dfNoDataValue : 0.0;
     462      158337 :     if (m_bNoDataSetAsInt64 || m_bNoDataSetAsUInt64)
     463           0 :         return true;  // FIXME: over pessimistic
     464      158339 :     if (m_nBitsPerSample == 8 ||
     465       49679 :         (m_nBitsPerSample < 8 && dfEffectiveNoData == 0))
     466             :     {
     467      110685 :         if (eDT == GDT_Int8)
     468             :         {
     469          40 :             return GDALIsValueInRange<signed char>(dfEffectiveNoData) &&
     470          20 :                    *(static_cast<const signed char *>(pBuffer)) ==
     471          40 :                        static_cast<signed char>(dfEffectiveNoData);
     472             :         }
     473      221303 :         return GDALIsValueInRange<GByte>(dfEffectiveNoData) &&
     474      110638 :                *(static_cast<const GByte *>(pBuffer)) ==
     475      221303 :                    static_cast<GByte>(dfEffectiveNoData);
     476             :     }
     477       47654 :     if (m_nBitsPerSample == 16 && eDT == GDT_UInt16)
     478             :     {
     479        3500 :         return GDALIsValueInRange<GUInt16>(dfEffectiveNoData) &&
     480        1750 :                *(static_cast<const GUInt16 *>(pBuffer)) ==
     481        3500 :                    static_cast<GUInt16>(dfEffectiveNoData);
     482             :     }
     483       45904 :     if (m_nBitsPerSample == 16 && eDT == GDT_Int16)
     484             :     {
     485        6648 :         return GDALIsValueInRange<GInt16>(dfEffectiveNoData) &&
     486        3324 :                *(static_cast<const GInt16 *>(pBuffer)) ==
     487        6648 :                    static_cast<GInt16>(dfEffectiveNoData);
     488             :     }
     489       42580 :     if (m_nBitsPerSample == 32 && eDT == GDT_UInt32)
     490             :     {
     491         148 :         return GDALIsValueInRange<GUInt32>(dfEffectiveNoData) &&
     492          74 :                *(static_cast<const GUInt32 *>(pBuffer)) ==
     493         148 :                    static_cast<GUInt32>(dfEffectiveNoData);
     494             :     }
     495       42506 :     if (m_nBitsPerSample == 32 && eDT == GDT_Int32)
     496             :     {
     497         258 :         return GDALIsValueInRange<GInt32>(dfEffectiveNoData) &&
     498         129 :                *(static_cast<const GInt32 *>(pBuffer)) ==
     499         258 :                    static_cast<GInt32>(dfEffectiveNoData);
     500             :     }
     501       42377 :     if (m_nBitsPerSample == 64 && eDT == GDT_UInt64)
     502             :     {
     503          10 :         return GDALIsValueInRange<std::uint64_t>(dfEffectiveNoData) &&
     504           5 :                *(static_cast<const std::uint64_t *>(pBuffer)) ==
     505          10 :                    static_cast<std::uint64_t>(dfEffectiveNoData);
     506             :     }
     507       42372 :     if (m_nBitsPerSample == 64 && eDT == GDT_Int64)
     508             :     {
     509          12 :         return GDALIsValueInRange<std::int64_t>(dfEffectiveNoData) &&
     510           6 :                *(static_cast<const std::int64_t *>(pBuffer)) ==
     511          12 :                    static_cast<std::int64_t>(dfEffectiveNoData);
     512             :     }
     513       42366 :     if (m_nBitsPerSample == 32 && eDT == GDT_Float32)
     514             :     {
     515       40976 :         if (std::isnan(m_dfNoDataValue))
     516           3 :             return CPL_TO_BOOL(
     517           6 :                 std::isnan(*(static_cast<const float *>(pBuffer))));
     518       81929 :         return GDALIsValueInRange<float>(dfEffectiveNoData) &&
     519       40959 :                *(static_cast<const float *>(pBuffer)) ==
     520       81914 :                    static_cast<float>(dfEffectiveNoData);
     521             :     }
     522        1390 :     if (m_nBitsPerSample == 64 && eDT == GDT_Float64)
     523             :     {
     524         213 :         if (std::isnan(dfEffectiveNoData))
     525           0 :             return CPL_TO_BOOL(
     526           0 :                 std::isnan(*(static_cast<const double *>(pBuffer))));
     527         213 :         return *(static_cast<const double *>(pBuffer)) == dfEffectiveNoData;
     528             :     }
     529        1177 :     return false;
     530             : }
     531             : 
     532             : /************************************************************************/
     533             : /*                      WriteDealWithLercAndNan()                       */
     534             : /************************************************************************/
     535             : 
     536             : template <typename T>
     537           0 : void GTiffDataset::WriteDealWithLercAndNan(T *pBuffer, int nActualBlockWidth,
     538             :                                            int nActualBlockHeight,
     539             :                                            int nStrileHeight)
     540             : {
     541             :     // This method does 2 things:
     542             :     // - warn the user if he tries to write NaN values with libtiff < 4.6.1
     543             :     //   and multi-band PlanarConfig=Contig configuration
     544             :     // - and in right-most and bottom-most tiles, replace non accessible
     545             :     //   pixel values by a safe one.
     546             : 
     547           0 :     const auto fPaddingValue =
     548             : #if !defined(LIBTIFF_MULTIBAND_LERC_NAN_OK)
     549             :         m_nPlanarConfig == PLANARCONFIG_CONTIG && nBands > 1
     550             :             ? 0
     551             :             :
     552             : #endif
     553             :             std::numeric_limits<T>::quiet_NaN();
     554             : 
     555           0 :     const int nBandsPerStrile =
     556           0 :         m_nPlanarConfig == PLANARCONFIG_CONTIG ? nBands : 1;
     557           0 :     for (int j = 0; j < nActualBlockHeight; ++j)
     558             :     {
     559             : #if !defined(LIBTIFF_MULTIBAND_LERC_NAN_OK)
     560             :         static bool bHasWarned = false;
     561             :         if (m_nPlanarConfig == PLANARCONFIG_CONTIG && nBands > 1 && !bHasWarned)
     562             :         {
     563             :             for (int i = 0; i < nActualBlockWidth * nBandsPerStrile; ++i)
     564             :             {
     565             :                 if (std::isnan(
     566             :                         pBuffer[j * m_nBlockXSize * nBandsPerStrile + i]))
     567             :                 {
     568             :                     bHasWarned = true;
     569             :                     CPLError(CE_Warning, CPLE_AppDefined,
     570             :                              "libtiff < 4.6.1 does not handle properly NaN "
     571             :                              "values for multi-band PlanarConfig=Contig "
     572             :                              "configuration. As a workaround, you can set the "
     573             :                              "INTERLEAVE=BAND creation option.");
     574             :                     break;
     575             :                 }
     576             :             }
     577             :         }
     578             : #endif
     579           0 :         for (int i = nActualBlockWidth * nBandsPerStrile;
     580           0 :              i < m_nBlockXSize * nBandsPerStrile; ++i)
     581             :         {
     582           0 :             pBuffer[j * m_nBlockXSize * nBandsPerStrile + i] = fPaddingValue;
     583             :         }
     584             :     }
     585           0 :     for (int j = nActualBlockHeight; j < nStrileHeight; ++j)
     586             :     {
     587           0 :         for (int i = 0; i < m_nBlockXSize * nBandsPerStrile; ++i)
     588             :         {
     589           0 :             pBuffer[j * m_nBlockXSize * nBandsPerStrile + i] = fPaddingValue;
     590             :         }
     591             :     }
     592           0 : }
     593             : 
     594             : /************************************************************************/
     595             : /*                        WriteEncodedTile()                            */
     596             : /************************************************************************/
     597             : 
     598       46839 : bool GTiffDataset::WriteEncodedTile(uint32_t tile, GByte *pabyData,
     599             :                                     int bPreserveDataBuffer)
     600             : {
     601             : 
     602       46839 :     const int iColumn = (tile % m_nBlocksPerBand) % m_nBlocksPerRow;
     603       46839 :     const int iRow = (tile % m_nBlocksPerBand) / m_nBlocksPerRow;
     604             : 
     605       93678 :     const int nActualBlockWidth = (iColumn == m_nBlocksPerRow - 1)
     606       46839 :                                       ? nRasterXSize - iColumn * m_nBlockXSize
     607             :                                       : m_nBlockXSize;
     608       93678 :     const int nActualBlockHeight = (iRow == m_nBlocksPerColumn - 1)
     609       46839 :                                        ? nRasterYSize - iRow * m_nBlockYSize
     610             :                                        : m_nBlockYSize;
     611             : 
     612             :     /* -------------------------------------------------------------------- */
     613             :     /*      Don't write empty blocks in some cases.                         */
     614             :     /* -------------------------------------------------------------------- */
     615       46839 :     if (!m_bWriteEmptyTiles && IsFirstPixelEqualToNoData(pabyData))
     616             :     {
     617        1839 :         if (!IsBlockAvailable(tile, nullptr, nullptr, nullptr))
     618             :         {
     619        1839 :             const int nComponents =
     620        1839 :                 m_nPlanarConfig == PLANARCONFIG_CONTIG ? nBands : 1;
     621             : 
     622        1839 :             if (HasOnlyNoData(pabyData, nActualBlockWidth, nActualBlockHeight,
     623             :                               m_nBlockXSize, nComponents))
     624             :             {
     625        1153 :                 return true;
     626             :             }
     627             :         }
     628             :     }
     629             : 
     630             :     // Is this a partial right edge or bottom edge tile?
     631       89082 :     const bool bPartialTile = (nActualBlockWidth < m_nBlockXSize) ||
     632       43396 :                               (nActualBlockHeight < m_nBlockYSize);
     633             : 
     634             :     const bool bIsLercFloatingPoint =
     635       45754 :         m_nCompression == COMPRESSION_LERC &&
     636          66 :         (GetRasterBand(1)->GetRasterDataType() == GDT_Float32 ||
     637          64 :          GetRasterBand(1)->GetRasterDataType() == GDT_Float64);
     638             : 
     639             :     // Do we need to spread edge values right or down for a partial
     640             :     // JPEG encoded tile?  We do this to avoid edge artifacts.
     641             :     // We also need to be careful with LERC and NaN values
     642       45688 :     const bool bNeedTempBuffer =
     643       49296 :         bPartialTile &&
     644        3608 :         (m_nCompression == COMPRESSION_JPEG || bIsLercFloatingPoint);
     645             : 
     646             :     // If we need to fill out the tile, or if we want to prevent
     647             :     // TIFFWriteEncodedTile from altering the buffer as part of
     648             :     // byte swapping the data on write then we will need a temporary
     649             :     // working buffer.  If not, we can just do a direct write.
     650       45688 :     const GPtrDiff_t cc = static_cast<GPtrDiff_t>(TIFFTileSize(m_hTIFF));
     651             : 
     652       59245 :     if (bPreserveDataBuffer &&
     653       13559 :         (TIFFIsByteSwapped(m_hTIFF) || bNeedTempBuffer || m_panMaskOffsetLsb))
     654             :     {
     655         176 :         if (m_pabyTempWriteBuffer == nullptr)
     656             :         {
     657          39 :             m_pabyTempWriteBuffer = CPLMalloc(cc);
     658             :         }
     659         176 :         memcpy(m_pabyTempWriteBuffer, pabyData, cc);
     660             : 
     661         176 :         pabyData = static_cast<GByte *>(m_pabyTempWriteBuffer);
     662             :     }
     663             : 
     664             :     // Perform tile fill if needed.
     665             :     // TODO: we should also handle the case of nBitsPerSample == 12
     666             :     // but this is more involved.
     667       45686 :     if (bPartialTile && m_nCompression == COMPRESSION_JPEG &&
     668         131 :         m_nBitsPerSample == 8)
     669             :     {
     670         129 :         const int nComponents =
     671         129 :             m_nPlanarConfig == PLANARCONFIG_CONTIG ? nBands : 1;
     672             : 
     673         129 :         CPLDebug("GTiff", "Filling out jpeg edge tile on write.");
     674             : 
     675         129 :         const int nRightPixelsToFill =
     676         129 :             iColumn == m_nBlocksPerRow - 1
     677         129 :                 ? m_nBlockXSize * (iColumn + 1) - nRasterXSize
     678             :                 : 0;
     679         129 :         const int nBottomPixelsToFill =
     680         129 :             iRow == m_nBlocksPerColumn - 1
     681         129 :                 ? m_nBlockYSize * (iRow + 1) - nRasterYSize
     682             :                 : 0;
     683             : 
     684             :         // Fill out to the right.
     685         129 :         const int iSrcX = m_nBlockXSize - nRightPixelsToFill - 1;
     686             : 
     687       11072 :         for (int iX = iSrcX + 1; iX < m_nBlockXSize; ++iX)
     688             :         {
     689     3244860 :             for (int iY = 0; iY < m_nBlockYSize; ++iY)
     690             :             {
     691     3233920 :                 memcpy(pabyData +
     692     3233920 :                            (static_cast<GPtrDiff_t>(m_nBlockXSize) * iY + iX) *
     693     3233920 :                                nComponents,
     694     3233920 :                        pabyData + (static_cast<GPtrDiff_t>(m_nBlockXSize) * iY +
     695     3233920 :                                    iSrcX) *
     696     3233920 :                                       nComponents,
     697             :                        nComponents);
     698             :             }
     699             :         }
     700             : 
     701             :         // Now fill out the bottom.
     702         129 :         const int iSrcY = m_nBlockYSize - nBottomPixelsToFill - 1;
     703       16293 :         for (int iY = iSrcY + 1; iY < m_nBlockYSize; ++iY)
     704             :         {
     705       16164 :             memcpy(pabyData + static_cast<GPtrDiff_t>(m_nBlockXSize) *
     706       16164 :                                   nComponents * iY,
     707       16164 :                    pabyData + static_cast<GPtrDiff_t>(m_nBlockXSize) *
     708       16164 :                                   nComponents * iSrcY,
     709       16164 :                    static_cast<GPtrDiff_t>(m_nBlockXSize) * nComponents);
     710             :         }
     711             :     }
     712             : 
     713       45686 :     if (bIsLercFloatingPoint &&
     714             :         (bPartialTile
     715             : #if !defined(LIBTIFF_MULTIBAND_LERC_NAN_OK)
     716             :          /* libtiff < 4.6.1 doesn't generate a LERC mask for multi-band contig configuration */
     717             :          || (m_nPlanarConfig == PLANARCONFIG_CONTIG && nBands > 1)
     718             : #endif
     719             :              ))
     720             :     {
     721           0 :         if (GetRasterBand(1)->GetRasterDataType() == GDT_Float32)
     722           0 :             WriteDealWithLercAndNan(reinterpret_cast<float *>(pabyData),
     723             :                                     nActualBlockWidth, nActualBlockHeight,
     724             :                                     m_nBlockYSize);
     725             :         else
     726           0 :             WriteDealWithLercAndNan(reinterpret_cast<double *>(pabyData),
     727             :                                     nActualBlockWidth, nActualBlockHeight,
     728             :                                     m_nBlockYSize);
     729             :     }
     730             : 
     731       45686 :     if (m_panMaskOffsetLsb)
     732             :     {
     733           0 :         const int iBand = m_nPlanarConfig == PLANARCONFIG_SEPARATE
     734           0 :                               ? static_cast<int>(tile) / m_nBlocksPerBand
     735             :                               : -1;
     736           0 :         DiscardLsb(pabyData, cc, iBand);
     737             :     }
     738             : 
     739       45686 :     if (m_bStreamingOut)
     740             :     {
     741          17 :         if (tile != static_cast<uint32_t>(m_nLastWrittenBlockId + 1))
     742             :         {
     743           1 :             ReportError(CE_Failure, CPLE_NotSupported,
     744             :                         "Attempt to write block %d whereas %d was expected",
     745           1 :                         tile, m_nLastWrittenBlockId + 1);
     746           1 :             return false;
     747             :         }
     748          16 :         if (static_cast<GPtrDiff_t>(VSIFWriteL(pabyData, 1, cc, m_fpToWrite)) !=
     749             :             cc)
     750             :         {
     751           0 :             ReportError(CE_Failure, CPLE_FileIO,
     752             :                         "Could not write " CPL_FRMT_GUIB " bytes",
     753             :                         static_cast<GUIntBig>(cc));
     754           0 :             return false;
     755             :         }
     756          16 :         m_nLastWrittenBlockId = tile;
     757          16 :         return true;
     758             :     }
     759             : 
     760             :     /* -------------------------------------------------------------------- */
     761             :     /*      Should we do compression in a worker thread ?                   */
     762             :     /* -------------------------------------------------------------------- */
     763       45669 :     if (SubmitCompressionJob(tile, pabyData, cc, m_nBlockYSize))
     764       19283 :         return true;
     765             : 
     766       26384 :     return TIFFWriteEncodedTile(m_hTIFF, tile, pabyData, cc) == cc;
     767             : }
     768             : 
     769             : /************************************************************************/
     770             : /*                        WriteEncodedStrip()                           */
     771             : /************************************************************************/
     772             : 
     773      158716 : bool GTiffDataset::WriteEncodedStrip(uint32_t strip, GByte *pabyData,
     774             :                                      int bPreserveDataBuffer)
     775             : {
     776      158716 :     GPtrDiff_t cc = static_cast<GPtrDiff_t>(TIFFStripSize(m_hTIFF));
     777      158728 :     const auto ccFull = cc;
     778             : 
     779             :     /* -------------------------------------------------------------------- */
     780             :     /*      If this is the last strip in the image, and is partial, then    */
     781             :     /*      we need to trim the number of scanlines written to the          */
     782             :     /*      amount of valid data we have. (#2748)                           */
     783             :     /* -------------------------------------------------------------------- */
     784      158728 :     const int nStripWithinBand = strip % m_nBlocksPerBand;
     785      158728 :     int nStripHeight = m_nRowsPerStrip;
     786             : 
     787      158728 :     if (nStripWithinBand * nStripHeight > GetRasterYSize() - nStripHeight)
     788             :     {
     789        1116 :         nStripHeight = GetRasterYSize() - nStripWithinBand * m_nRowsPerStrip;
     790        1116 :         cc = (cc / m_nRowsPerStrip) * nStripHeight;
     791        2232 :         CPLDebug("GTiff",
     792             :                  "Adjusted bytes to write from " CPL_FRMT_GUIB
     793             :                  " to " CPL_FRMT_GUIB ".",
     794        1116 :                  static_cast<GUIntBig>(TIFFStripSize(m_hTIFF)),
     795             :                  static_cast<GUIntBig>(cc));
     796             :     }
     797             : 
     798             :     /* -------------------------------------------------------------------- */
     799             :     /*      Don't write empty blocks in some cases.                         */
     800             :     /* -------------------------------------------------------------------- */
     801      158731 :     if (!m_bWriteEmptyTiles && IsFirstPixelEqualToNoData(pabyData))
     802             :     {
     803       33342 :         if (!IsBlockAvailable(strip, nullptr, nullptr, nullptr))
     804             :         {
     805       33172 :             const int nComponents =
     806       33172 :                 m_nPlanarConfig == PLANARCONFIG_CONTIG ? nBands : 1;
     807             : 
     808       33172 :             if (HasOnlyNoData(pabyData, m_nBlockXSize, nStripHeight,
     809             :                               m_nBlockXSize, nComponents))
     810             :             {
     811       24114 :                 return true;
     812             :             }
     813             :         }
     814             :     }
     815             : 
     816             :     /* -------------------------------------------------------------------- */
     817             :     /*      TIFFWriteEncodedStrip can alter the passed buffer if            */
     818             :     /*      byte-swapping is necessary so we use a temporary buffer         */
     819             :     /*      before calling it.                                              */
     820             :     /* -------------------------------------------------------------------- */
     821      221918 :     if (bPreserveDataBuffer &&
     822       87304 :         (TIFFIsByteSwapped(m_hTIFF) || m_panMaskOffsetLsb))
     823             :     {
     824         294 :         if (m_pabyTempWriteBuffer == nullptr)
     825             :         {
     826         126 :             m_pabyTempWriteBuffer = CPLMalloc(ccFull);
     827             :         }
     828         294 :         memcpy(m_pabyTempWriteBuffer, pabyData, cc);
     829         294 :         pabyData = static_cast<GByte *>(m_pabyTempWriteBuffer);
     830             :     }
     831             : 
     832             : #if !defined(LIBTIFF_MULTIBAND_LERC_NAN_OK)
     833             :     const bool bIsLercFloatingPoint =
     834             :         m_nCompression == COMPRESSION_LERC &&
     835             :         (GetRasterBand(1)->GetRasterDataType() == GDT_Float32 ||
     836             :          GetRasterBand(1)->GetRasterDataType() == GDT_Float64);
     837             :     if (bIsLercFloatingPoint &&
     838             :         /* libtiff < 4.6.1 doesn't generate a LERC mask for multi-band contig configuration */
     839             :         m_nPlanarConfig == PLANARCONFIG_CONTIG && nBands > 1)
     840             :     {
     841             :         if (GetRasterBand(1)->GetRasterDataType() == GDT_Float32)
     842             :             WriteDealWithLercAndNan(reinterpret_cast<float *>(pabyData),
     843             :                                     m_nBlockXSize, nStripHeight, nStripHeight);
     844             :         else
     845             :             WriteDealWithLercAndNan(reinterpret_cast<double *>(pabyData),
     846             :                                     m_nBlockXSize, nStripHeight, nStripHeight);
     847             :     }
     848             : #endif
     849             : 
     850      134614 :     if (m_panMaskOffsetLsb)
     851             :     {
     852         366 :         int iBand = m_nPlanarConfig == PLANARCONFIG_SEPARATE
     853         183 :                         ? static_cast<int>(strip) / m_nBlocksPerBand
     854             :                         : -1;
     855         183 :         DiscardLsb(pabyData, cc, iBand);
     856             :     }
     857             : 
     858      134619 :     if (m_bStreamingOut)
     859             :     {
     860        1408 :         if (strip != static_cast<uint32_t>(m_nLastWrittenBlockId + 1))
     861             :         {
     862           1 :             ReportError(CE_Failure, CPLE_NotSupported,
     863             :                         "Attempt to write block %d whereas %d was expected",
     864           1 :                         strip, m_nLastWrittenBlockId + 1);
     865           1 :             return false;
     866             :         }
     867        1407 :         if (static_cast<GPtrDiff_t>(VSIFWriteL(pabyData, 1, cc, m_fpToWrite)) !=
     868             :             cc)
     869             :         {
     870           0 :             ReportError(CE_Failure, CPLE_FileIO,
     871             :                         "Could not write " CPL_FRMT_GUIB " bytes",
     872             :                         static_cast<GUIntBig>(cc));
     873           0 :             return false;
     874             :         }
     875        1407 :         m_nLastWrittenBlockId = strip;
     876        1407 :         return true;
     877             :     }
     878             : 
     879             :     /* -------------------------------------------------------------------- */
     880             :     /*      Should we do compression in a worker thread ?                   */
     881             :     /* -------------------------------------------------------------------- */
     882      133211 :     if (SubmitCompressionJob(strip, pabyData, cc, nStripHeight))
     883        6703 :         return true;
     884             : 
     885      126490 :     return TIFFWriteEncodedStrip(m_hTIFF, strip, pabyData, cc) == cc;
     886             : }
     887             : 
     888             : /************************************************************************/
     889             : /*                        InitCompressionThreads()                      */
     890             : /************************************************************************/
     891             : 
     892       24890 : void GTiffDataset::InitCompressionThreads(bool bUpdateMode,
     893             :                                           CSLConstList papszOptions)
     894             : {
     895             :     // Raster == tile, then no need for threads
     896       24890 :     if (m_nBlockXSize == nRasterXSize && m_nBlockYSize == nRasterYSize)
     897       17204 :         return;
     898             : 
     899        7686 :     const char *pszValue = CSLFetchNameValue(papszOptions, "NUM_THREADS");
     900        7678 :     if (pszValue == nullptr)
     901        7613 :         pszValue = CPLGetConfigOption("GDAL_NUM_THREADS", nullptr);
     902        7682 :     if (pszValue)
     903             :     {
     904             :         int nThreads =
     905          79 :             EQUAL(pszValue, "ALL_CPUS") ? CPLGetNumCPUs() : atoi(pszValue);
     906          79 :         if (nThreads > 1024)
     907           0 :             nThreads = 1024;  // to please Coverity
     908          79 :         if (nThreads > 1)
     909             :         {
     910          87 :             if ((bUpdateMode && m_nCompression != COMPRESSION_NONE) ||
     911          15 :                 (nBands >= 1 && IsMultiThreadedReadCompatible()))
     912             :             {
     913          66 :                 CPLDebug("GTiff",
     914             :                          "Using up to %d threads for compression/decompression",
     915             :                          nThreads);
     916             : 
     917          66 :                 m_poThreadPool = GDALGetGlobalThreadPool(nThreads);
     918          66 :                 if (bUpdateMode && m_poThreadPool)
     919          57 :                     m_poCompressQueue = m_poThreadPool->CreateJobQueue();
     920             : 
     921          66 :                 if (m_poCompressQueue != nullptr)
     922             :                 {
     923             :                     // Add a margin of an extra job w.r.t thread number
     924             :                     // so as to optimize compression time (enables the main
     925             :                     // thread to do boring I/O while all CPUs are working).
     926          57 :                     m_asCompressionJobs.resize(nThreads + 1);
     927          57 :                     memset(&m_asCompressionJobs[0], 0,
     928          57 :                            m_asCompressionJobs.size() *
     929             :                                sizeof(GTiffCompressionJob));
     930          57 :                     for (int i = 0;
     931         276 :                          i < static_cast<int>(m_asCompressionJobs.size()); ++i)
     932             :                     {
     933         438 :                         m_asCompressionJobs[i].pszTmpFilename =
     934         219 :                             CPLStrdup(VSIMemGenerateHiddenFilename(
     935             :                                 CPLSPrintf("thread_job_%d.tif", i)));
     936         219 :                         m_asCompressionJobs[i].nStripOrTile = -1;
     937             :                     }
     938             : 
     939             :                     // This is kind of a hack, but basically using
     940             :                     // TIFFWriteRawStrip/Tile and then TIFFReadEncodedStrip/Tile
     941             :                     // does not work on a newly created file, because
     942             :                     // TIFF_MYBUFFER is not set in tif_flags
     943             :                     // (if using TIFFWriteEncodedStrip/Tile first,
     944             :                     // TIFFWriteBufferSetup() is automatically called).
     945             :                     // This should likely rather fixed in libtiff itself.
     946          57 :                     CPL_IGNORE_RET_VAL(
     947          57 :                         TIFFWriteBufferSetup(m_hTIFF, nullptr, -1));
     948             :                 }
     949             :             }
     950             :         }
     951           7 :         else if (nThreads < 0 ||
     952           7 :                  (!EQUAL(pszValue, "0") && !EQUAL(pszValue, "1") &&
     953           3 :                   !EQUAL(pszValue, "ALL_CPUS")))
     954             :         {
     955           3 :             ReportError(CE_Warning, CPLE_AppDefined,
     956             :                         "Invalid value for NUM_THREADS: %s", pszValue);
     957             :         }
     958             :     }
     959             : }
     960             : 
     961             : /************************************************************************/
     962             : /*                      ThreadCompressionFunc()                         */
     963             : /************************************************************************/
     964             : 
     965       25998 : void GTiffDataset::ThreadCompressionFunc(void *pData)
     966             : {
     967       25998 :     GTiffCompressionJob *psJob = static_cast<GTiffCompressionJob *>(pData);
     968       25998 :     GTiffDataset *poDS = psJob->poDS;
     969             : 
     970       25998 :     VSILFILE *fpTmp = VSIFOpenL(psJob->pszTmpFilename, "wb+");
     971       25998 :     TIFF *hTIFFTmp = VSI_TIFFOpen(
     972       51996 :         psJob->pszTmpFilename, psJob->bTIFFIsBigEndian ? "wb+" : "wl+", fpTmp);
     973       25997 :     CPLAssert(hTIFFTmp != nullptr);
     974       25997 :     TIFFSetField(hTIFFTmp, TIFFTAG_IMAGEWIDTH, poDS->m_nBlockXSize);
     975       25994 :     TIFFSetField(hTIFFTmp, TIFFTAG_IMAGELENGTH, psJob->nHeight);
     976       25996 :     TIFFSetField(hTIFFTmp, TIFFTAG_BITSPERSAMPLE, poDS->m_nBitsPerSample);
     977       25995 :     TIFFSetField(hTIFFTmp, TIFFTAG_COMPRESSION, poDS->m_nCompression);
     978       25998 :     TIFFSetField(hTIFFTmp, TIFFTAG_PHOTOMETRIC, poDS->m_nPhotometric);
     979       25997 :     TIFFSetField(hTIFFTmp, TIFFTAG_SAMPLEFORMAT, poDS->m_nSampleFormat);
     980       25997 :     TIFFSetField(hTIFFTmp, TIFFTAG_SAMPLESPERPIXEL, poDS->m_nSamplesPerPixel);
     981       25997 :     TIFFSetField(hTIFFTmp, TIFFTAG_ROWSPERSTRIP, poDS->m_nBlockYSize);
     982       25995 :     TIFFSetField(hTIFFTmp, TIFFTAG_PLANARCONFIG, poDS->m_nPlanarConfig);
     983       25997 :     if (psJob->nPredictor != PREDICTOR_NONE)
     984         260 :         TIFFSetField(hTIFFTmp, TIFFTAG_PREDICTOR, psJob->nPredictor);
     985       25999 :     if (poDS->m_nCompression == COMPRESSION_LERC)
     986             :     {
     987          24 :         TIFFSetField(hTIFFTmp, TIFFTAG_LERC_PARAMETERS, 2,
     988          24 :                      poDS->m_anLercAddCompressionAndVersion);
     989             :     }
     990       25999 :     if (psJob->nExtraSampleCount)
     991             :     {
     992         283 :         TIFFSetField(hTIFFTmp, TIFFTAG_EXTRASAMPLES, psJob->nExtraSampleCount,
     993             :                      psJob->pExtraSamples);
     994             :     }
     995             : 
     996       25999 :     poDS->RestoreVolatileParameters(hTIFFTmp);
     997             : 
     998       51993 :     bool bOK = TIFFWriteEncodedStrip(hTIFFTmp, 0, psJob->pabyBuffer,
     999       25998 :                                      psJob->nBufferSize) == psJob->nBufferSize;
    1000             : 
    1001       25995 :     toff_t nOffset = 0;
    1002       25995 :     if (bOK)
    1003             :     {
    1004       25995 :         toff_t *panOffsets = nullptr;
    1005       25995 :         toff_t *panByteCounts = nullptr;
    1006       25995 :         TIFFGetField(hTIFFTmp, TIFFTAG_STRIPOFFSETS, &panOffsets);
    1007       25994 :         TIFFGetField(hTIFFTmp, TIFFTAG_STRIPBYTECOUNTS, &panByteCounts);
    1008             : 
    1009       25993 :         nOffset = panOffsets[0];
    1010       25993 :         psJob->nCompressedBufferSize =
    1011       25993 :             static_cast<GPtrDiff_t>(panByteCounts[0]);
    1012             :     }
    1013             :     else
    1014             :     {
    1015           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    1016             :                  "Error when compressing strip/tile %d", psJob->nStripOrTile);
    1017             :     }
    1018             : 
    1019       25993 :     XTIFFClose(hTIFFTmp);
    1020       25996 :     if (VSIFCloseL(fpTmp) != 0)
    1021             :     {
    1022           0 :         if (bOK)
    1023             :         {
    1024           0 :             bOK = false;
    1025           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    1026             :                      "Error when compressing strip/tile %d",
    1027             :                      psJob->nStripOrTile);
    1028             :         }
    1029             :     }
    1030             : 
    1031       25990 :     if (bOK)
    1032             :     {
    1033       25990 :         vsi_l_offset nFileSize = 0;
    1034             :         GByte *pabyCompressedBuffer =
    1035       25990 :             VSIGetMemFileBuffer(psJob->pszTmpFilename, &nFileSize, FALSE);
    1036       25998 :         CPLAssert(static_cast<vsi_l_offset>(
    1037             :                       nOffset + psJob->nCompressedBufferSize) <= nFileSize);
    1038       25998 :         psJob->pabyCompressedBuffer = pabyCompressedBuffer + nOffset;
    1039             :     }
    1040             :     else
    1041             :     {
    1042           0 :         psJob->pabyCompressedBuffer = nullptr;
    1043           0 :         psJob->nCompressedBufferSize = 0;
    1044             :     }
    1045             : 
    1046       25998 :     auto poMainDS = poDS->m_poBaseDS ? poDS->m_poBaseDS : poDS;
    1047       25998 :     if (poMainDS->m_poCompressQueue)
    1048             :     {
    1049        1575 :         std::lock_guard oLock(poMainDS->m_oCompressThreadPoolMutex);
    1050        1575 :         psJob->bReady = true;
    1051             :     }
    1052       25998 : }
    1053             : 
    1054             : /************************************************************************/
    1055             : /*                        WriteRawStripOrTile()                         */
    1056             : /************************************************************************/
    1057             : 
    1058       33063 : void GTiffDataset::WriteRawStripOrTile(int nStripOrTile,
    1059             :                                        GByte *pabyCompressedBuffer,
    1060             :                                        GPtrDiff_t nCompressedBufferSize)
    1061             : {
    1062             : #ifdef DEBUG_VERBOSE
    1063             :     CPLDebug("GTIFF", "Writing raw strip/tile %d, size " CPL_FRMT_GUIB,
    1064             :              nStripOrTile, static_cast<GUIntBig>(nCompressedBufferSize));
    1065             : #endif
    1066       33063 :     toff_t *panOffsets = nullptr;
    1067       33063 :     toff_t *panByteCounts = nullptr;
    1068       33063 :     bool bWriteAtEnd = true;
    1069       33063 :     bool bWriteLeader = m_bLeaderSizeAsUInt4;
    1070       33063 :     bool bWriteTrailer = m_bTrailerRepeatedLast4BytesRepeated;
    1071       33063 :     if (TIFFGetField(m_hTIFF,
    1072       33063 :                      TIFFIsTiled(m_hTIFF) ? TIFFTAG_TILEOFFSETS
    1073             :                                           : TIFFTAG_STRIPOFFSETS,
    1074       33063 :                      &panOffsets) &&
    1075       33063 :         panOffsets != nullptr && panOffsets[nStripOrTile] != 0)
    1076             :     {
    1077             :         // Forces TIFFAppendStrip() to consider if the location of the
    1078             :         // tile/strip can be reused or if the strile should be written at end of
    1079             :         // file.
    1080         360 :         TIFFSetWriteOffset(m_hTIFF, 0);
    1081             : 
    1082         360 :         if (m_bBlockOrderRowMajor)
    1083             :         {
    1084         264 :             if (TIFFGetField(m_hTIFF,
    1085         264 :                              TIFFIsTiled(m_hTIFF) ? TIFFTAG_TILEBYTECOUNTS
    1086             :                                                   : TIFFTAG_STRIPBYTECOUNTS,
    1087         528 :                              &panByteCounts) &&
    1088         264 :                 panByteCounts != nullptr)
    1089             :             {
    1090         264 :                 if (static_cast<GUIntBig>(nCompressedBufferSize) >
    1091         264 :                     panByteCounts[nStripOrTile])
    1092             :                 {
    1093           8 :                     GTiffDataset *poRootDS = m_poBaseDS ? m_poBaseDS : this;
    1094           8 :                     if (!poRootDS->m_bKnownIncompatibleEdition &&
    1095           8 :                         !poRootDS->m_bWriteKnownIncompatibleEdition)
    1096             :                     {
    1097           8 :                         ReportError(
    1098             :                             CE_Warning, CPLE_AppDefined,
    1099             :                             "A strile cannot be rewritten in place, which "
    1100             :                             "invalidates the BLOCK_ORDER optimization.");
    1101           8 :                         poRootDS->m_bKnownIncompatibleEdition = true;
    1102           8 :                         poRootDS->m_bWriteKnownIncompatibleEdition = true;
    1103             :                     }
    1104             :                 }
    1105             :                 // For mask interleaving, if the size is not exactly the same,
    1106             :                 // completely give up (we could potentially move the mask in
    1107             :                 // case the imagery is smaller)
    1108         256 :                 else if (m_poMaskDS && m_bMaskInterleavedWithImagery &&
    1109           0 :                          static_cast<GUIntBig>(nCompressedBufferSize) !=
    1110           0 :                              panByteCounts[nStripOrTile])
    1111             :                 {
    1112           0 :                     GTiffDataset *poRootDS = m_poBaseDS ? m_poBaseDS : this;
    1113           0 :                     if (!poRootDS->m_bKnownIncompatibleEdition &&
    1114           0 :                         !poRootDS->m_bWriteKnownIncompatibleEdition)
    1115             :                     {
    1116           0 :                         ReportError(
    1117             :                             CE_Warning, CPLE_AppDefined,
    1118             :                             "A strile cannot be rewritten in place, which "
    1119             :                             "invalidates the MASK_INTERLEAVED_WITH_IMAGERY "
    1120             :                             "optimization.");
    1121           0 :                         poRootDS->m_bKnownIncompatibleEdition = true;
    1122           0 :                         poRootDS->m_bWriteKnownIncompatibleEdition = true;
    1123             :                     }
    1124           0 :                     bWriteLeader = false;
    1125           0 :                     bWriteTrailer = false;
    1126           0 :                     if (m_bLeaderSizeAsUInt4)
    1127             :                     {
    1128             :                         // If there was a valid leader, invalidat it
    1129           0 :                         VSI_TIFFSeek(m_hTIFF, panOffsets[nStripOrTile] - 4,
    1130             :                                      SEEK_SET);
    1131             :                         uint32_t nOldSize;
    1132           0 :                         VSIFReadL(&nOldSize, 1, 4,
    1133             :                                   VSI_TIFFGetVSILFile(TIFFClientdata(m_hTIFF)));
    1134           0 :                         CPL_LSBPTR32(&nOldSize);
    1135           0 :                         if (nOldSize == panByteCounts[nStripOrTile])
    1136             :                         {
    1137           0 :                             uint32_t nInvalidatedSize = 0;
    1138           0 :                             VSI_TIFFSeek(m_hTIFF, panOffsets[nStripOrTile] - 4,
    1139             :                                          SEEK_SET);
    1140           0 :                             VSI_TIFFWrite(m_hTIFF, &nInvalidatedSize,
    1141             :                                           sizeof(nInvalidatedSize));
    1142             :                         }
    1143           0 :                     }
    1144             :                 }
    1145             :                 else
    1146             :                 {
    1147         256 :                     bWriteAtEnd = false;
    1148             :                 }
    1149             :             }
    1150             :         }
    1151             :     }
    1152       33063 :     if (bWriteLeader &&
    1153       24440 :         static_cast<GUIntBig>(nCompressedBufferSize) <= 0xFFFFFFFFU)
    1154             :     {
    1155             :         // cppcheck-suppress knownConditionTrueFalse
    1156       24440 :         if (bWriteAtEnd)
    1157             :         {
    1158       24184 :             VSI_TIFFSeek(m_hTIFF, 0, SEEK_END);
    1159             :         }
    1160             :         else
    1161             :         {
    1162             :             // If we rewrite an existing strile in place with an existing
    1163             :             // leader, check that the leader is valid, before rewriting it. And
    1164             :             // if it is not valid, then do not write the trailer, as we could
    1165             :             // corrupt other data.
    1166         256 :             VSI_TIFFSeek(m_hTIFF, panOffsets[nStripOrTile] - 4, SEEK_SET);
    1167             :             uint32_t nOldSize;
    1168         256 :             VSIFReadL(&nOldSize, 1, 4,
    1169             :                       VSI_TIFFGetVSILFile(TIFFClientdata(m_hTIFF)));
    1170         256 :             CPL_LSBPTR32(&nOldSize);
    1171         256 :             bWriteLeader =
    1172         256 :                 panByteCounts && nOldSize == panByteCounts[nStripOrTile];
    1173         256 :             bWriteTrailer = bWriteLeader;
    1174         256 :             VSI_TIFFSeek(m_hTIFF, panOffsets[nStripOrTile] - 4, SEEK_SET);
    1175             :         }
    1176             :         // cppcheck-suppress knownConditionTrueFalse
    1177       24440 :         if (bWriteLeader)
    1178             :         {
    1179       24440 :             uint32_t nSize = static_cast<uint32_t>(nCompressedBufferSize);
    1180       24440 :             CPL_LSBPTR32(&nSize);
    1181       24440 :             if (!VSI_TIFFWrite(m_hTIFF, &nSize, sizeof(nSize)))
    1182           0 :                 m_bWriteError = true;
    1183             :         }
    1184             :     }
    1185             :     tmsize_t written;
    1186       33063 :     if (TIFFIsTiled(m_hTIFF))
    1187       25672 :         written = TIFFWriteRawTile(m_hTIFF, nStripOrTile, pabyCompressedBuffer,
    1188             :                                    nCompressedBufferSize);
    1189             :     else
    1190        7391 :         written = TIFFWriteRawStrip(m_hTIFF, nStripOrTile, pabyCompressedBuffer,
    1191             :                                     nCompressedBufferSize);
    1192       33063 :     if (written != nCompressedBufferSize)
    1193          12 :         m_bWriteError = true;
    1194       33063 :     if (bWriteTrailer &&
    1195       24440 :         static_cast<GUIntBig>(nCompressedBufferSize) <= 0xFFFFFFFFU)
    1196             :     {
    1197       24440 :         GByte abyLastBytes[4] = {};
    1198       24440 :         if (nCompressedBufferSize >= 4)
    1199       24440 :             memcpy(abyLastBytes,
    1200       24440 :                    pabyCompressedBuffer + nCompressedBufferSize - 4, 4);
    1201             :         else
    1202           0 :             memcpy(abyLastBytes, pabyCompressedBuffer, nCompressedBufferSize);
    1203       24440 :         if (!VSI_TIFFWrite(m_hTIFF, abyLastBytes, 4))
    1204           0 :             m_bWriteError = true;
    1205             :     }
    1206       33063 : }
    1207             : 
    1208             : /************************************************************************/
    1209             : /*                        WaitCompletionForJobIdx()                     */
    1210             : /************************************************************************/
    1211             : 
    1212        1575 : void GTiffDataset::WaitCompletionForJobIdx(int i)
    1213             : {
    1214        1575 :     auto poMainDS = m_poBaseDS ? m_poBaseDS : this;
    1215        1575 :     auto poQueue = poMainDS->m_poCompressQueue.get();
    1216        1575 :     auto &oQueue = poMainDS->m_asQueueJobIdx;
    1217        1575 :     auto &asJobs = poMainDS->m_asCompressionJobs;
    1218        1575 :     auto &mutex = poMainDS->m_oCompressThreadPoolMutex;
    1219             : 
    1220        1575 :     CPLAssert(i >= 0 && static_cast<size_t>(i) < asJobs.size());
    1221        1575 :     CPLAssert(asJobs[i].nStripOrTile >= 0);
    1222        1575 :     CPLAssert(!oQueue.empty());
    1223             : 
    1224        1575 :     bool bHasWarned = false;
    1225             :     while (true)
    1226             :     {
    1227             :         bool bReady;
    1228             :         {
    1229        2159 :             std::lock_guard oLock(mutex);
    1230        2159 :             bReady = asJobs[i].bReady;
    1231             :         }
    1232        2159 :         if (!bReady)
    1233             :         {
    1234         584 :             if (!bHasWarned)
    1235             :             {
    1236         309 :                 CPLDebug("GTIFF",
    1237             :                          "Waiting for worker job to finish handling block %d",
    1238         309 :                          asJobs[i].nStripOrTile);
    1239         309 :                 bHasWarned = true;
    1240             :             }
    1241         584 :             poQueue->GetPool()->WaitEvent();
    1242             :         }
    1243             :         else
    1244             :         {
    1245        1575 :             break;
    1246             :         }
    1247         584 :     }
    1248             : 
    1249        1575 :     if (asJobs[i].nCompressedBufferSize)
    1250             :     {
    1251        3150 :         asJobs[i].poDS->WriteRawStripOrTile(asJobs[i].nStripOrTile,
    1252        1575 :                                             asJobs[i].pabyCompressedBuffer,
    1253        1575 :                                             asJobs[i].nCompressedBufferSize);
    1254             :     }
    1255        1575 :     asJobs[i].pabyCompressedBuffer = nullptr;
    1256        1575 :     asJobs[i].nBufferSize = 0;
    1257             :     {
    1258             :         // Likely useless, but makes Coverity happy
    1259        1575 :         std::lock_guard oLock(mutex);
    1260        1575 :         asJobs[i].bReady = false;
    1261             :     }
    1262        1575 :     asJobs[i].nStripOrTile = -1;
    1263        1575 :     oQueue.pop();
    1264        1575 : }
    1265             : 
    1266             : /************************************************************************/
    1267             : /*                        WaitCompletionForBlock()                      */
    1268             : /************************************************************************/
    1269             : 
    1270     2287460 : void GTiffDataset::WaitCompletionForBlock(int nBlockId)
    1271             : {
    1272     2287460 :     auto poQueue = m_poBaseDS ? m_poBaseDS->m_poCompressQueue.get()
    1273     2273160 :                               : m_poCompressQueue.get();
    1274             :     // cppcheck-suppress constVariableReference
    1275     2287400 :     auto &oQueue = m_poBaseDS ? m_poBaseDS->m_asQueueJobIdx : m_asQueueJobIdx;
    1276             :     // cppcheck-suppress constVariableReference
    1277     2273100 :     auto &asJobs =
    1278     2287400 :         m_poBaseDS ? m_poBaseDS->m_asCompressionJobs : m_asCompressionJobs;
    1279             : 
    1280     2287400 :     if (poQueue != nullptr && !oQueue.empty())
    1281             :     {
    1282        1062 :         for (int i = 0; i < static_cast<int>(asJobs.size()); ++i)
    1283             :         {
    1284         885 :             if (asJobs[i].poDS == this && asJobs[i].nStripOrTile == nBlockId)
    1285             :             {
    1286         126 :                 while (!oQueue.empty() &&
    1287          63 :                        !(asJobs[oQueue.front()].poDS == this &&
    1288          63 :                          asJobs[oQueue.front()].nStripOrTile == nBlockId))
    1289             :                 {
    1290           0 :                     WaitCompletionForJobIdx(oQueue.front());
    1291             :                 }
    1292          63 :                 CPLAssert(!oQueue.empty() &&
    1293             :                           asJobs[oQueue.front()].poDS == this &&
    1294             :                           asJobs[oQueue.front()].nStripOrTile == nBlockId);
    1295          63 :                 WaitCompletionForJobIdx(oQueue.front());
    1296             :             }
    1297             :         }
    1298             :     }
    1299     2287400 : }
    1300             : 
    1301             : /************************************************************************/
    1302             : /*                      SubmitCompressionJob()                          */
    1303             : /************************************************************************/
    1304             : 
    1305      178880 : bool GTiffDataset::SubmitCompressionJob(int nStripOrTile, GByte *pabyData,
    1306             :                                         GPtrDiff_t cc, int nHeight)
    1307             : {
    1308             :     /* -------------------------------------------------------------------- */
    1309             :     /*      Should we do compression in a worker thread ?                   */
    1310             :     /* -------------------------------------------------------------------- */
    1311      178880 :     auto poQueue = m_poBaseDS ? m_poBaseDS->m_poCompressQueue.get()
    1312      166546 :                               : m_poCompressQueue.get();
    1313             : 
    1314      178862 :     if (poQueue && m_nCompression == COMPRESSION_NONE)
    1315             :     {
    1316             :         // We don't do multi-threaded compression for uncompressed...
    1317             :         // but we must wait for other related compression tasks (e.g mask)
    1318             :         // to be completed
    1319           0 :         poQueue->WaitCompletion();
    1320             : 
    1321             :         // Flush remaining data
    1322             :         // cppcheck-suppress constVariableReference
    1323           0 :         auto &oQueue =
    1324           0 :             m_poBaseDS ? m_poBaseDS->m_asQueueJobIdx : m_asQueueJobIdx;
    1325           0 :         while (!oQueue.empty())
    1326             :         {
    1327           0 :             WaitCompletionForJobIdx(oQueue.front());
    1328             :         }
    1329             :     }
    1330             : 
    1331             :     const auto SetupJob =
    1332      120208 :         [this, pabyData, cc, nHeight, nStripOrTile](GTiffCompressionJob &sJob)
    1333             :     {
    1334       25998 :         sJob.poDS = this;
    1335       25998 :         sJob.bTIFFIsBigEndian = CPL_TO_BOOL(TIFFIsBigEndian(m_hTIFF));
    1336       25998 :         sJob.pabyBuffer = static_cast<GByte *>(CPLRealloc(sJob.pabyBuffer, cc));
    1337       25998 :         memcpy(sJob.pabyBuffer, pabyData, cc);
    1338       25998 :         sJob.nBufferSize = cc;
    1339       25998 :         sJob.nHeight = nHeight;
    1340       25998 :         sJob.nStripOrTile = nStripOrTile;
    1341       25998 :         sJob.nPredictor = PREDICTOR_NONE;
    1342       25998 :         if (GTIFFSupportsPredictor(m_nCompression))
    1343             :         {
    1344       16216 :             TIFFGetField(m_hTIFF, TIFFTAG_PREDICTOR, &sJob.nPredictor);
    1345             :         }
    1346             : 
    1347       25998 :         sJob.pExtraSamples = nullptr;
    1348       25998 :         sJob.nExtraSampleCount = 0;
    1349       25998 :         TIFFGetField(m_hTIFF, TIFFTAG_EXTRASAMPLES, &sJob.nExtraSampleCount,
    1350             :                      &sJob.pExtraSamples);
    1351      204860 :     };
    1352             : 
    1353      178862 :     if (poQueue == nullptr || !(m_nCompression == COMPRESSION_ADOBE_DEFLATE ||
    1354         806 :                                 m_nCompression == COMPRESSION_LZW ||
    1355          78 :                                 m_nCompression == COMPRESSION_PACKBITS ||
    1356          72 :                                 m_nCompression == COMPRESSION_LZMA ||
    1357          62 :                                 m_nCompression == COMPRESSION_ZSTD ||
    1358          52 :                                 m_nCompression == COMPRESSION_LERC ||
    1359          46 :                                 m_nCompression == COMPRESSION_JXL ||
    1360          46 :                                 m_nCompression == COMPRESSION_JXL_DNG_1_7 ||
    1361          28 :                                 m_nCompression == COMPRESSION_WEBP ||
    1362          18 :                                 m_nCompression == COMPRESSION_JPEG))
    1363             :     {
    1364      177287 :         if (m_bBlockOrderRowMajor || m_bLeaderSizeAsUInt4 ||
    1365      152868 :             m_bTrailerRepeatedLast4BytesRepeated)
    1366             :         {
    1367             :             GTiffCompressionJob sJob;
    1368       24421 :             memset(&sJob, 0, sizeof(sJob));
    1369       24421 :             SetupJob(sJob);
    1370       24423 :             sJob.pszTmpFilename =
    1371       24423 :                 CPLStrdup(VSIMemGenerateHiddenFilename("temp.tif"));
    1372             : 
    1373       24423 :             ThreadCompressionFunc(&sJob);
    1374             : 
    1375       24423 :             if (sJob.nCompressedBufferSize)
    1376             :             {
    1377       24423 :                 sJob.poDS->WriteRawStripOrTile(sJob.nStripOrTile,
    1378             :                                                sJob.pabyCompressedBuffer,
    1379             :                                                sJob.nCompressedBufferSize);
    1380             :             }
    1381             : 
    1382       24423 :             CPLFree(sJob.pabyBuffer);
    1383       24423 :             VSIUnlink(sJob.pszTmpFilename);
    1384       24423 :             CPLFree(sJob.pszTmpFilename);
    1385       24423 :             return sJob.nCompressedBufferSize > 0 && !m_bWriteError;
    1386             :         }
    1387             : 
    1388      152866 :         return false;
    1389             :     }
    1390             : 
    1391        1575 :     auto poMainDS = m_poBaseDS ? m_poBaseDS : this;
    1392        1575 :     auto &oQueue = poMainDS->m_asQueueJobIdx;
    1393        1575 :     auto &asJobs = poMainDS->m_asCompressionJobs;
    1394             : 
    1395        1575 :     int nNextCompressionJobAvail = -1;
    1396             : 
    1397        1575 :     if (oQueue.size() == asJobs.size())
    1398             :     {
    1399        1443 :         CPLAssert(!oQueue.empty());
    1400        1443 :         nNextCompressionJobAvail = oQueue.front();
    1401        1443 :         WaitCompletionForJobIdx(nNextCompressionJobAvail);
    1402             :     }
    1403             :     else
    1404             :     {
    1405         132 :         const int nJobs = static_cast<int>(asJobs.size());
    1406         323 :         for (int i = 0; i < nJobs; ++i)
    1407             :         {
    1408         323 :             if (asJobs[i].nBufferSize == 0)
    1409             :             {
    1410         132 :                 nNextCompressionJobAvail = i;
    1411         132 :                 break;
    1412             :             }
    1413             :         }
    1414             :     }
    1415        1575 :     CPLAssert(nNextCompressionJobAvail >= 0);
    1416             : 
    1417        1575 :     GTiffCompressionJob *psJob = &asJobs[nNextCompressionJobAvail];
    1418        1575 :     SetupJob(*psJob);
    1419        1575 :     poQueue->SubmitJob(ThreadCompressionFunc, psJob);
    1420        1575 :     oQueue.push(nNextCompressionJobAvail);
    1421             : 
    1422        1575 :     return true;
    1423             : }
    1424             : 
    1425             : /************************************************************************/
    1426             : /*                          DiscardLsb()                                */
    1427             : /************************************************************************/
    1428             : 
    1429         272 : template <class T> bool MustNotDiscardLsb(T value, bool bHasNoData, T nodata)
    1430             : {
    1431         272 :     return bHasNoData && value == nodata;
    1432             : }
    1433             : 
    1434             : template <>
    1435          44 : bool MustNotDiscardLsb<float>(float value, bool bHasNoData, float nodata)
    1436             : {
    1437          44 :     return (bHasNoData && value == nodata) || !std::isfinite(value);
    1438             : }
    1439             : 
    1440             : template <>
    1441          44 : bool MustNotDiscardLsb<double>(double value, bool bHasNoData, double nodata)
    1442             : {
    1443          44 :     return (bHasNoData && value == nodata) || !std::isfinite(value);
    1444             : }
    1445             : 
    1446             : template <class T> T AdjustValue(T value, uint64_t nRoundUpBitTest);
    1447             : 
    1448          10 : template <class T> T AdjustValueInt(T value, uint64_t nRoundUpBitTest)
    1449             : {
    1450          10 :     if (value >=
    1451          10 :         static_cast<T>(std::numeric_limits<T>::max() - (nRoundUpBitTest << 1)))
    1452           0 :         return static_cast<T>(value - (nRoundUpBitTest << 1));
    1453          10 :     return static_cast<T>(value + (nRoundUpBitTest << 1));
    1454             : }
    1455             : 
    1456           0 : template <> int8_t AdjustValue<int8_t>(int8_t value, uint64_t nRoundUpBitTest)
    1457             : {
    1458           0 :     return AdjustValueInt(value, nRoundUpBitTest);
    1459             : }
    1460             : 
    1461             : template <>
    1462           2 : uint8_t AdjustValue<uint8_t>(uint8_t value, uint64_t nRoundUpBitTest)
    1463             : {
    1464           2 :     return AdjustValueInt(value, nRoundUpBitTest);
    1465             : }
    1466             : 
    1467             : template <>
    1468           2 : int16_t AdjustValue<int16_t>(int16_t value, uint64_t nRoundUpBitTest)
    1469             : {
    1470           2 :     return AdjustValueInt(value, nRoundUpBitTest);
    1471             : }
    1472             : 
    1473             : template <>
    1474           2 : uint16_t AdjustValue<uint16_t>(uint16_t value, uint64_t nRoundUpBitTest)
    1475             : {
    1476           2 :     return AdjustValueInt(value, nRoundUpBitTest);
    1477             : }
    1478             : 
    1479             : template <>
    1480           2 : int32_t AdjustValue<int32_t>(int32_t value, uint64_t nRoundUpBitTest)
    1481             : {
    1482           2 :     return AdjustValueInt(value, nRoundUpBitTest);
    1483             : }
    1484             : 
    1485             : template <>
    1486           2 : uint32_t AdjustValue<uint32_t>(uint32_t value, uint64_t nRoundUpBitTest)
    1487             : {
    1488           2 :     return AdjustValueInt(value, nRoundUpBitTest);
    1489             : }
    1490             : 
    1491             : template <>
    1492           0 : int64_t AdjustValue<int64_t>(int64_t value, uint64_t nRoundUpBitTest)
    1493             : {
    1494           0 :     return AdjustValueInt(value, nRoundUpBitTest);
    1495             : }
    1496             : 
    1497             : template <>
    1498           0 : uint64_t AdjustValue<uint64_t>(uint64_t value, uint64_t nRoundUpBitTest)
    1499             : {
    1500           0 :     return AdjustValueInt(value, nRoundUpBitTest);
    1501             : }
    1502             : 
    1503           0 : template <> float AdjustValue<float>(float value, uint64_t)
    1504             : {
    1505           0 :     return std::nextafter(value, std::numeric_limits<float>::max());
    1506             : }
    1507             : 
    1508           0 : template <> double AdjustValue<double>(double value, uint64_t)
    1509             : {
    1510           0 :     return std::nextafter(value, std::numeric_limits<double>::max());
    1511             : }
    1512             : 
    1513             : template <class Teffective, class T>
    1514             : T RoundValueDiscardLsb(const void *ptr, uint64_t nMask,
    1515             :                        uint64_t nRoundUpBitTest);
    1516             : 
    1517             : template <class T>
    1518          16 : T RoundValueDiscardLsbUnsigned(const void *ptr, uint64_t nMask,
    1519             :                                uint64_t nRoundUpBitTest)
    1520             : {
    1521          32 :     if ((*reinterpret_cast<const T *>(ptr) & nMask) >
    1522          16 :         static_cast<uint64_t>(std::numeric_limits<T>::max()) -
    1523          16 :             (nRoundUpBitTest << 1U))
    1524             :     {
    1525           4 :         return static_cast<T>(std::numeric_limits<T>::max() & nMask);
    1526             :     }
    1527          12 :     const uint64_t newval =
    1528          12 :         (*reinterpret_cast<const T *>(ptr) & nMask) + (nRoundUpBitTest << 1U);
    1529          12 :     return static_cast<T>(newval);
    1530             : }
    1531             : 
    1532             : template <class T>
    1533          18 : T RoundValueDiscardLsbSigned(const void *ptr, uint64_t nMask,
    1534             :                              uint64_t nRoundUpBitTest)
    1535             : {
    1536          18 :     T oldval = *reinterpret_cast<const T *>(ptr);
    1537          18 :     if (oldval < 0)
    1538             :     {
    1539           4 :         return static_cast<T>(oldval & nMask);
    1540             :     }
    1541          14 :     const uint64_t newval =
    1542          14 :         (*reinterpret_cast<const T *>(ptr) & nMask) + (nRoundUpBitTest << 1U);
    1543          14 :     if (newval > static_cast<uint64_t>(std::numeric_limits<T>::max()))
    1544           4 :         return static_cast<T>(std::numeric_limits<T>::max() & nMask);
    1545          10 :     return static_cast<T>(newval);
    1546             : }
    1547             : 
    1548             : template <>
    1549          11 : uint16_t RoundValueDiscardLsb<uint16_t, uint16_t>(const void *ptr,
    1550             :                                                   uint64_t nMask,
    1551             :                                                   uint64_t nRoundUpBitTest)
    1552             : {
    1553          11 :     return RoundValueDiscardLsbUnsigned<uint16_t>(ptr, nMask, nRoundUpBitTest);
    1554             : }
    1555             : 
    1556             : template <>
    1557           5 : uint32_t RoundValueDiscardLsb<uint32_t, uint32_t>(const void *ptr,
    1558             :                                                   uint64_t nMask,
    1559             :                                                   uint64_t nRoundUpBitTest)
    1560             : {
    1561           5 :     return RoundValueDiscardLsbUnsigned<uint32_t>(ptr, nMask, nRoundUpBitTest);
    1562             : }
    1563             : 
    1564             : template <>
    1565           0 : uint64_t RoundValueDiscardLsb<uint64_t, uint64_t>(const void *ptr,
    1566             :                                                   uint64_t nMask,
    1567             :                                                   uint64_t nRoundUpBitTest)
    1568             : {
    1569           0 :     return RoundValueDiscardLsbUnsigned<uint64_t>(ptr, nMask, nRoundUpBitTest);
    1570             : }
    1571             : 
    1572             : template <>
    1573           0 : int8_t RoundValueDiscardLsb<int8_t, int8_t>(const void *ptr, uint64_t nMask,
    1574             :                                             uint64_t nRoundUpBitTest)
    1575             : {
    1576           0 :     return RoundValueDiscardLsbSigned<int8_t>(ptr, nMask, nRoundUpBitTest);
    1577             : }
    1578             : 
    1579             : template <>
    1580          13 : int16_t RoundValueDiscardLsb<int16_t, int16_t>(const void *ptr, uint64_t nMask,
    1581             :                                                uint64_t nRoundUpBitTest)
    1582             : {
    1583          13 :     return RoundValueDiscardLsbSigned<int16_t>(ptr, nMask, nRoundUpBitTest);
    1584             : }
    1585             : 
    1586             : template <>
    1587           5 : int32_t RoundValueDiscardLsb<int32_t, int32_t>(const void *ptr, uint64_t nMask,
    1588             :                                                uint64_t nRoundUpBitTest)
    1589             : {
    1590           5 :     return RoundValueDiscardLsbSigned<int32_t>(ptr, nMask, nRoundUpBitTest);
    1591             : }
    1592             : 
    1593             : template <>
    1594           0 : int64_t RoundValueDiscardLsb<int64_t, int64_t>(const void *ptr, uint64_t nMask,
    1595             :                                                uint64_t nRoundUpBitTest)
    1596             : {
    1597           0 :     return RoundValueDiscardLsbSigned<int64_t>(ptr, nMask, nRoundUpBitTest);
    1598             : }
    1599             : 
    1600             : template <>
    1601           0 : uint32_t RoundValueDiscardLsb<float, uint32_t>(const void *ptr, uint64_t nMask,
    1602             :                                                uint64_t nRoundUpBitTest)
    1603             : {
    1604           0 :     return RoundValueDiscardLsbUnsigned<uint32_t>(ptr, nMask, nRoundUpBitTest);
    1605             : }
    1606             : 
    1607             : template <>
    1608           0 : uint64_t RoundValueDiscardLsb<double, uint64_t>(const void *ptr, uint64_t nMask,
    1609             :                                                 uint64_t nRoundUpBitTest)
    1610             : {
    1611           0 :     return RoundValueDiscardLsbUnsigned<uint64_t>(ptr, nMask, nRoundUpBitTest);
    1612             : }
    1613             : 
    1614             : template <class Teffective, class T>
    1615         145 : static void DiscardLsbT(GByte *pabyBuffer, size_t nBytes, int iBand, int nBands,
    1616             :                         uint16_t nPlanarConfig,
    1617             :                         const GTiffDataset::MaskOffset *panMaskOffsetLsb,
    1618             :                         bool bHasNoData, Teffective nNoDataValue)
    1619             : {
    1620             :     static_assert(sizeof(Teffective) == sizeof(T),
    1621             :                   "sizeof(Teffective) == sizeof(T)");
    1622         145 :     if (nPlanarConfig == PLANARCONFIG_SEPARATE)
    1623             :     {
    1624          98 :         const auto nMask = panMaskOffsetLsb[iBand].nMask;
    1625          98 :         const auto nRoundUpBitTest = panMaskOffsetLsb[iBand].nRoundUpBitTest;
    1626         196 :         for (size_t i = 0; i < nBytes / sizeof(T); ++i)
    1627             :         {
    1628          98 :             if (MustNotDiscardLsb(reinterpret_cast<Teffective *>(pabyBuffer)[i],
    1629             :                                   bHasNoData, nNoDataValue))
    1630             :             {
    1631          22 :                 continue;
    1632             :             }
    1633             : 
    1634          76 :             if (reinterpret_cast<T *>(pabyBuffer)[i] & nRoundUpBitTest)
    1635             :             {
    1636          30 :                 reinterpret_cast<T *>(pabyBuffer)[i] =
    1637          15 :                     RoundValueDiscardLsb<Teffective, T>(
    1638          15 :                         &(reinterpret_cast<T *>(pabyBuffer)[i]), nMask,
    1639             :                         nRoundUpBitTest);
    1640             :             }
    1641             :             else
    1642             :             {
    1643          61 :                 reinterpret_cast<T *>(pabyBuffer)[i] = static_cast<T>(
    1644          61 :                     reinterpret_cast<T *>(pabyBuffer)[i] & nMask);
    1645             :             }
    1646             : 
    1647             :             // Make sure that by discarding LSB we don't end up to a value
    1648             :             // that is no the nodata value
    1649          76 :             if (MustNotDiscardLsb(reinterpret_cast<Teffective *>(pabyBuffer)[i],
    1650             :                                   bHasNoData, nNoDataValue))
    1651             :             {
    1652           8 :                 reinterpret_cast<Teffective *>(pabyBuffer)[i] =
    1653           4 :                     AdjustValue(nNoDataValue, nRoundUpBitTest);
    1654             :             }
    1655             :         }
    1656             :     }
    1657             :     else
    1658             :     {
    1659          94 :         for (size_t i = 0; i < nBytes / sizeof(T); i += nBands)
    1660             :         {
    1661         147 :             for (int j = 0; j < nBands; ++j)
    1662             :             {
    1663         100 :                 if (MustNotDiscardLsb(
    1664         100 :                         reinterpret_cast<Teffective *>(pabyBuffer)[i + j],
    1665             :                         bHasNoData, nNoDataValue))
    1666             :                 {
    1667          14 :                     continue;
    1668             :                 }
    1669             : 
    1670          86 :                 if (reinterpret_cast<T *>(pabyBuffer)[i + j] &
    1671          86 :                     panMaskOffsetLsb[j].nRoundUpBitTest)
    1672             :                 {
    1673          38 :                     reinterpret_cast<T *>(pabyBuffer)[i + j] =
    1674          19 :                         RoundValueDiscardLsb<Teffective, T>(
    1675          19 :                             &(reinterpret_cast<T *>(pabyBuffer)[i + j]),
    1676          19 :                             panMaskOffsetLsb[j].nMask,
    1677          19 :                             panMaskOffsetLsb[j].nRoundUpBitTest);
    1678             :                 }
    1679             :                 else
    1680             :                 {
    1681          67 :                     reinterpret_cast<T *>(pabyBuffer)[i + j] = static_cast<T>(
    1682          67 :                         (reinterpret_cast<T *>(pabyBuffer)[i + j] &
    1683          67 :                          panMaskOffsetLsb[j].nMask));
    1684             :                 }
    1685             : 
    1686             :                 // Make sure that by discarding LSB we don't end up to a value
    1687             :                 // that is no the nodata value
    1688          86 :                 if (MustNotDiscardLsb(
    1689          86 :                         reinterpret_cast<Teffective *>(pabyBuffer)[i + j],
    1690             :                         bHasNoData, nNoDataValue))
    1691             :                 {
    1692           8 :                     reinterpret_cast<Teffective *>(pabyBuffer)[i + j] =
    1693           4 :                         AdjustValue(nNoDataValue,
    1694           4 :                                     panMaskOffsetLsb[j].nRoundUpBitTest);
    1695             :                 }
    1696             :             }
    1697             :         }
    1698             :     }
    1699         145 : }
    1700             : 
    1701         183 : static void DiscardLsb(GByte *pabyBuffer, GPtrDiff_t nBytes, int iBand,
    1702             :                        int nBands, uint16_t nSampleFormat,
    1703             :                        uint16_t nBitsPerSample, uint16_t nPlanarConfig,
    1704             :                        const GTiffDataset::MaskOffset *panMaskOffsetLsb,
    1705             :                        bool bHasNoData, double dfNoDataValue)
    1706             : {
    1707         183 :     if (nBitsPerSample == 8 && nSampleFormat == SAMPLEFORMAT_UINT)
    1708             :     {
    1709          38 :         uint8_t nNoDataValue = 0;
    1710          38 :         if (bHasNoData && GDALIsValueExactAs<uint8_t>(dfNoDataValue))
    1711             :         {
    1712           6 :             nNoDataValue = static_cast<uint8_t>(dfNoDataValue);
    1713             :         }
    1714             :         else
    1715             :         {
    1716          32 :             bHasNoData = false;
    1717             :         }
    1718          38 :         if (nPlanarConfig == PLANARCONFIG_SEPARATE)
    1719             :         {
    1720          25 :             const auto nMask =
    1721          25 :                 static_cast<unsigned>(panMaskOffsetLsb[iBand].nMask);
    1722          25 :             const auto nRoundUpBitTest =
    1723          25 :                 static_cast<unsigned>(panMaskOffsetLsb[iBand].nRoundUpBitTest);
    1724          50 :             for (decltype(nBytes) i = 0; i < nBytes; ++i)
    1725             :             {
    1726          25 :                 if (bHasNoData && pabyBuffer[i] == nNoDataValue)
    1727           3 :                     continue;
    1728             : 
    1729             :                 // Keep 255 in case it is alpha.
    1730          22 :                 if (pabyBuffer[i] != 255)
    1731             :                 {
    1732          21 :                     if (pabyBuffer[i] & nRoundUpBitTest)
    1733           5 :                         pabyBuffer[i] = static_cast<GByte>(
    1734           5 :                             std::min(255U, (pabyBuffer[i] & nMask) +
    1735           5 :                                                (nRoundUpBitTest << 1U)));
    1736             :                     else
    1737          16 :                         pabyBuffer[i] =
    1738          16 :                             static_cast<GByte>(pabyBuffer[i] & nMask);
    1739             : 
    1740             :                     // Make sure that by discarding LSB we don't end up to a
    1741             :                     // value that is no the nodata value
    1742          21 :                     if (bHasNoData && pabyBuffer[i] == nNoDataValue)
    1743           2 :                         pabyBuffer[i] =
    1744           1 :                             AdjustValue(nNoDataValue, nRoundUpBitTest);
    1745             :                 }
    1746             :             }
    1747             :         }
    1748             :         else
    1749             :         {
    1750          26 :             for (decltype(nBytes) i = 0; i < nBytes; i += nBands)
    1751             :             {
    1752          42 :                 for (int j = 0; j < nBands; ++j)
    1753             :                 {
    1754          29 :                     if (bHasNoData && pabyBuffer[i + j] == nNoDataValue)
    1755           2 :                         continue;
    1756             : 
    1757             :                     // Keep 255 in case it is alpha.
    1758          27 :                     if (pabyBuffer[i + j] != 255)
    1759             :                     {
    1760          25 :                         if (pabyBuffer[i + j] &
    1761          25 :                             panMaskOffsetLsb[j].nRoundUpBitTest)
    1762             :                         {
    1763           6 :                             pabyBuffer[i + j] = static_cast<GByte>(std::min(
    1764          12 :                                 255U,
    1765           6 :                                 (pabyBuffer[i + j] &
    1766             :                                  static_cast<unsigned>(
    1767           6 :                                      panMaskOffsetLsb[j].nMask)) +
    1768             :                                     (static_cast<unsigned>(
    1769           6 :                                          panMaskOffsetLsb[j].nRoundUpBitTest)
    1770           6 :                                      << 1U)));
    1771             :                         }
    1772             :                         else
    1773             :                         {
    1774          19 :                             pabyBuffer[i + j] = static_cast<GByte>(
    1775          19 :                                 pabyBuffer[i + j] & panMaskOffsetLsb[j].nMask);
    1776             :                         }
    1777             : 
    1778             :                         // Make sure that by discarding LSB we don't end up to a
    1779             :                         // value that is no the nodata value
    1780          25 :                         if (bHasNoData && pabyBuffer[i + j] == nNoDataValue)
    1781           1 :                             pabyBuffer[i + j] = AdjustValue(
    1782             :                                 nNoDataValue,
    1783           1 :                                 panMaskOffsetLsb[j].nRoundUpBitTest);
    1784             :                     }
    1785             :                 }
    1786             :             }
    1787          38 :         }
    1788             :     }
    1789         145 :     else if (nBitsPerSample == 8 && nSampleFormat == SAMPLEFORMAT_INT)
    1790             :     {
    1791           0 :         int8_t nNoDataValue = 0;
    1792           0 :         if (bHasNoData && GDALIsValueExactAs<int8_t>(dfNoDataValue))
    1793             :         {
    1794           0 :             nNoDataValue = static_cast<int8_t>(dfNoDataValue);
    1795             :         }
    1796             :         else
    1797             :         {
    1798           0 :             bHasNoData = false;
    1799             :         }
    1800           0 :         DiscardLsbT<int8_t, int8_t>(pabyBuffer, nBytes, iBand, nBands,
    1801             :                                     nPlanarConfig, panMaskOffsetLsb, bHasNoData,
    1802           0 :                                     nNoDataValue);
    1803             :     }
    1804         145 :     else if (nBitsPerSample == 16 && nSampleFormat == SAMPLEFORMAT_INT)
    1805             :     {
    1806          48 :         int16_t nNoDataValue = 0;
    1807          48 :         if (bHasNoData && GDALIsValueExactAs<int16_t>(dfNoDataValue))
    1808             :         {
    1809           6 :             nNoDataValue = static_cast<int16_t>(dfNoDataValue);
    1810             :         }
    1811             :         else
    1812             :         {
    1813          42 :             bHasNoData = false;
    1814             :         }
    1815          48 :         DiscardLsbT<int16_t, int16_t>(pabyBuffer, nBytes, iBand, nBands,
    1816             :                                       nPlanarConfig, panMaskOffsetLsb,
    1817          48 :                                       bHasNoData, nNoDataValue);
    1818             :     }
    1819          97 :     else if (nBitsPerSample == 16 && nSampleFormat == SAMPLEFORMAT_UINT)
    1820             :     {
    1821          33 :         uint16_t nNoDataValue = 0;
    1822          33 :         if (bHasNoData && GDALIsValueExactAs<uint16_t>(dfNoDataValue))
    1823             :         {
    1824           6 :             nNoDataValue = static_cast<uint16_t>(dfNoDataValue);
    1825             :         }
    1826             :         else
    1827             :         {
    1828          27 :             bHasNoData = false;
    1829             :         }
    1830          33 :         DiscardLsbT<uint16_t, uint16_t>(pabyBuffer, nBytes, iBand, nBands,
    1831             :                                         nPlanarConfig, panMaskOffsetLsb,
    1832          33 :                                         bHasNoData, nNoDataValue);
    1833             :     }
    1834          64 :     else if (nBitsPerSample == 32 && nSampleFormat == SAMPLEFORMAT_INT)
    1835             :     {
    1836          13 :         int32_t nNoDataValue = 0;
    1837          13 :         if (bHasNoData && GDALIsValueExactAs<int32_t>(dfNoDataValue))
    1838             :         {
    1839           6 :             nNoDataValue = static_cast<int32_t>(dfNoDataValue);
    1840             :         }
    1841             :         else
    1842             :         {
    1843           7 :             bHasNoData = false;
    1844             :         }
    1845          13 :         DiscardLsbT<int32_t, int32_t>(pabyBuffer, nBytes, iBand, nBands,
    1846             :                                       nPlanarConfig, panMaskOffsetLsb,
    1847          13 :                                       bHasNoData, nNoDataValue);
    1848             :     }
    1849          51 :     else if (nBitsPerSample == 32 && nSampleFormat == SAMPLEFORMAT_UINT)
    1850             :     {
    1851          13 :         uint32_t nNoDataValue = 0;
    1852          13 :         if (bHasNoData && GDALIsValueExactAs<uint32_t>(dfNoDataValue))
    1853             :         {
    1854           6 :             nNoDataValue = static_cast<uint32_t>(dfNoDataValue);
    1855             :         }
    1856             :         else
    1857             :         {
    1858           7 :             bHasNoData = false;
    1859             :         }
    1860          13 :         DiscardLsbT<uint32_t, uint32_t>(pabyBuffer, nBytes, iBand, nBands,
    1861             :                                         nPlanarConfig, panMaskOffsetLsb,
    1862          13 :                                         bHasNoData, nNoDataValue);
    1863             :     }
    1864          38 :     else if (nBitsPerSample == 64 && nSampleFormat == SAMPLEFORMAT_INT)
    1865             :     {
    1866             :         // FIXME: we should not rely on dfNoDataValue when we support native
    1867             :         // data type for nodata
    1868           0 :         int64_t nNoDataValue = 0;
    1869           0 :         if (bHasNoData && GDALIsValueExactAs<int64_t>(dfNoDataValue))
    1870             :         {
    1871           0 :             nNoDataValue = static_cast<int64_t>(dfNoDataValue);
    1872             :         }
    1873             :         else
    1874             :         {
    1875           0 :             bHasNoData = false;
    1876             :         }
    1877           0 :         DiscardLsbT<int64_t, int64_t>(pabyBuffer, nBytes, iBand, nBands,
    1878             :                                       nPlanarConfig, panMaskOffsetLsb,
    1879           0 :                                       bHasNoData, nNoDataValue);
    1880             :     }
    1881          38 :     else if (nBitsPerSample == 64 && nSampleFormat == SAMPLEFORMAT_UINT)
    1882             :     {
    1883             :         // FIXME: we should not rely on dfNoDataValue when we support native
    1884             :         // data type for nodata
    1885           0 :         uint64_t nNoDataValue = 0;
    1886           0 :         if (bHasNoData && GDALIsValueExactAs<uint64_t>(dfNoDataValue))
    1887             :         {
    1888           0 :             nNoDataValue = static_cast<uint64_t>(dfNoDataValue);
    1889             :         }
    1890             :         else
    1891             :         {
    1892           0 :             bHasNoData = false;
    1893             :         }
    1894           0 :         DiscardLsbT<uint64_t, uint64_t>(pabyBuffer, nBytes, iBand, nBands,
    1895             :                                         nPlanarConfig, panMaskOffsetLsb,
    1896           0 :                                         bHasNoData, nNoDataValue);
    1897             :     }
    1898          38 :     else if (nBitsPerSample == 32 && nSampleFormat == SAMPLEFORMAT_IEEEFP)
    1899             :     {
    1900          19 :         float fNoDataValue = static_cast<float>(dfNoDataValue);
    1901          19 :         DiscardLsbT<float, uint32_t>(pabyBuffer, nBytes, iBand, nBands,
    1902             :                                      nPlanarConfig, panMaskOffsetLsb,
    1903          19 :                                      bHasNoData, fNoDataValue);
    1904             :     }
    1905          19 :     else if (nBitsPerSample == 64 && nSampleFormat == SAMPLEFORMAT_IEEEFP)
    1906             :     {
    1907          19 :         DiscardLsbT<double, uint64_t>(pabyBuffer, nBytes, iBand, nBands,
    1908             :                                       nPlanarConfig, panMaskOffsetLsb,
    1909             :                                       bHasNoData, dfNoDataValue);
    1910             :     }
    1911         183 : }
    1912             : 
    1913         183 : void GTiffDataset::DiscardLsb(GByte *pabyBuffer, GPtrDiff_t nBytes,
    1914             :                               int iBand) const
    1915             : {
    1916         183 :     ::DiscardLsb(pabyBuffer, nBytes, iBand, nBands, m_nSampleFormat,
    1917         183 :                  m_nBitsPerSample, m_nPlanarConfig, m_panMaskOffsetLsb,
    1918         183 :                  m_bNoDataSet, m_dfNoDataValue);
    1919         183 : }
    1920             : 
    1921             : /************************************************************************/
    1922             : /*                  WriteEncodedTileOrStrip()                           */
    1923             : /************************************************************************/
    1924             : 
    1925      205563 : CPLErr GTiffDataset::WriteEncodedTileOrStrip(uint32_t tile_or_strip, void *data,
    1926             :                                              int bPreserveDataBuffer)
    1927             : {
    1928      205563 :     CPLErr eErr = CE_None;
    1929             : 
    1930      205563 :     if (TIFFIsTiled(m_hTIFF))
    1931             :     {
    1932       46839 :         if (!(WriteEncodedTile(tile_or_strip, static_cast<GByte *>(data),
    1933             :                                bPreserveDataBuffer)))
    1934             :         {
    1935          14 :             eErr = CE_Failure;
    1936             :         }
    1937             :     }
    1938             :     else
    1939             :     {
    1940      158720 :         if (!(WriteEncodedStrip(tile_or_strip, static_cast<GByte *>(data),
    1941             :                                 bPreserveDataBuffer)))
    1942             :         {
    1943           8 :             eErr = CE_Failure;
    1944             :         }
    1945             :     }
    1946             : 
    1947      205548 :     return eErr;
    1948             : }
    1949             : 
    1950             : /************************************************************************/
    1951             : /*                           FlushBlockBuf()                            */
    1952             : /************************************************************************/
    1953             : 
    1954        8108 : CPLErr GTiffDataset::FlushBlockBuf()
    1955             : 
    1956             : {
    1957        8108 :     if (m_nLoadedBlock < 0 || !m_bLoadedBlockDirty)
    1958           0 :         return CE_None;
    1959             : 
    1960        8108 :     m_bLoadedBlockDirty = false;
    1961             : 
    1962             :     const CPLErr eErr =
    1963        8108 :         WriteEncodedTileOrStrip(m_nLoadedBlock, m_pabyBlockBuf, true);
    1964        8108 :     if (eErr != CE_None)
    1965             :     {
    1966           0 :         ReportError(CE_Failure, CPLE_AppDefined,
    1967             :                     "WriteEncodedTile/Strip() failed.");
    1968           0 :         m_bWriteError = true;
    1969             :     }
    1970             : 
    1971        8108 :     return eErr;
    1972             : }
    1973             : 
    1974             : /************************************************************************/
    1975             : /*                   GTiffFillStreamableOffsetAndCount()                */
    1976             : /************************************************************************/
    1977             : 
    1978           8 : static void GTiffFillStreamableOffsetAndCount(TIFF *hTIFF, int nSize)
    1979             : {
    1980           8 :     uint32_t nXSize = 0;
    1981           8 :     uint32_t nYSize = 0;
    1982           8 :     TIFFGetField(hTIFF, TIFFTAG_IMAGEWIDTH, &nXSize);
    1983           8 :     TIFFGetField(hTIFF, TIFFTAG_IMAGELENGTH, &nYSize);
    1984           8 :     const bool bIsTiled = CPL_TO_BOOL(TIFFIsTiled(hTIFF));
    1985             :     const int nBlockCount =
    1986           8 :         bIsTiled ? TIFFNumberOfTiles(hTIFF) : TIFFNumberOfStrips(hTIFF);
    1987             : 
    1988           8 :     toff_t *panOffset = nullptr;
    1989           8 :     TIFFGetField(hTIFF, bIsTiled ? TIFFTAG_TILEOFFSETS : TIFFTAG_STRIPOFFSETS,
    1990             :                  &panOffset);
    1991           8 :     toff_t *panSize = nullptr;
    1992           8 :     TIFFGetField(hTIFF,
    1993             :                  bIsTiled ? TIFFTAG_TILEBYTECOUNTS : TIFFTAG_STRIPBYTECOUNTS,
    1994             :                  &panSize);
    1995           8 :     toff_t nOffset = nSize;
    1996             :     // Trick to avoid clang static analyzer raising false positive about
    1997             :     // divide by zero later.
    1998           8 :     int nBlocksPerBand = 1;
    1999           8 :     uint32_t nRowsPerStrip = 0;
    2000           8 :     if (!bIsTiled)
    2001             :     {
    2002           6 :         TIFFGetField(hTIFF, TIFFTAG_ROWSPERSTRIP, &nRowsPerStrip);
    2003           6 :         if (nRowsPerStrip > static_cast<uint32_t>(nYSize))
    2004           0 :             nRowsPerStrip = nYSize;
    2005           6 :         nBlocksPerBand = DIV_ROUND_UP(nYSize, nRowsPerStrip);
    2006             :     }
    2007        2947 :     for (int i = 0; i < nBlockCount; ++i)
    2008             :     {
    2009             :         GPtrDiff_t cc = bIsTiled
    2010        2939 :                             ? static_cast<GPtrDiff_t>(TIFFTileSize(hTIFF))
    2011        2907 :                             : static_cast<GPtrDiff_t>(TIFFStripSize(hTIFF));
    2012        2939 :         if (!bIsTiled)
    2013             :         {
    2014             :             /* --------------------------------------------------------------------
    2015             :              */
    2016             :             /*      If this is the last strip in the image, and is partial, then
    2017             :              */
    2018             :             /*      we need to trim the number of scanlines written to the */
    2019             :             /*      amount of valid data we have. (#2748) */
    2020             :             /* --------------------------------------------------------------------
    2021             :              */
    2022        2907 :             int nStripWithinBand = i % nBlocksPerBand;
    2023        2907 :             if (nStripWithinBand * nRowsPerStrip > nYSize - nRowsPerStrip)
    2024             :             {
    2025           1 :                 cc = (cc / nRowsPerStrip) *
    2026           1 :                      (nYSize - nStripWithinBand * nRowsPerStrip);
    2027             :             }
    2028             :         }
    2029        2939 :         panOffset[i] = nOffset;
    2030        2939 :         panSize[i] = cc;
    2031        2939 :         nOffset += cc;
    2032             :     }
    2033           8 : }
    2034             : 
    2035             : /************************************************************************/
    2036             : /*                             Crystalize()                             */
    2037             : /*                                                                      */
    2038             : /*      Make sure that the directory information is written out for     */
    2039             : /*      a new file, require before writing any imagery data.            */
    2040             : /************************************************************************/
    2041             : 
    2042     2506400 : void GTiffDataset::Crystalize()
    2043             : 
    2044             : {
    2045     2506400 :     if (m_bCrystalized)
    2046     2503250 :         return;
    2047             : 
    2048             :     // TODO: libtiff writes extended tags in the order they are specified
    2049             :     // and not in increasing order.
    2050        3153 :     WriteMetadata(this, m_hTIFF, true, m_eProfile, m_pszFilename,
    2051        3153 :                   m_papszCreationOptions);
    2052        3147 :     WriteGeoTIFFInfo();
    2053        3147 :     if (m_bNoDataSet)
    2054         267 :         WriteNoDataValue(m_hTIFF, m_dfNoDataValue);
    2055        2880 :     else if (m_bNoDataSetAsInt64)
    2056           1 :         WriteNoDataValue(m_hTIFF, m_nNoDataValueInt64);
    2057        2879 :     else if (m_bNoDataSetAsUInt64)
    2058           1 :         WriteNoDataValue(m_hTIFF, m_nNoDataValueUInt64);
    2059             : 
    2060        3147 :     m_bMetadataChanged = false;
    2061        3147 :     m_bGeoTIFFInfoChanged = false;
    2062        3147 :     m_bNoDataChanged = false;
    2063        3147 :     m_bNeedsRewrite = false;
    2064             : 
    2065        3147 :     m_bCrystalized = true;
    2066             : 
    2067        3147 :     TIFFWriteCheck(m_hTIFF, TIFFIsTiled(m_hTIFF), "GTiffDataset::Crystalize");
    2068             : 
    2069        3147 :     TIFFWriteDirectory(m_hTIFF);
    2070        3147 :     if (m_bStreamingOut)
    2071             :     {
    2072             :         // We need to write twice the directory to be sure that custom
    2073             :         // TIFF tags are correctly sorted and that padding bytes have been
    2074             :         // added.
    2075           3 :         TIFFSetDirectory(m_hTIFF, 0);
    2076           3 :         TIFFWriteDirectory(m_hTIFF);
    2077             : 
    2078           3 :         if (VSIFSeekL(m_fpL, 0, SEEK_END) != 0)
    2079             :         {
    2080           0 :             ReportError(CE_Failure, CPLE_FileIO, "Could not seek");
    2081             :         }
    2082           3 :         const int nSize = static_cast<int>(VSIFTellL(m_fpL));
    2083             : 
    2084           3 :         TIFFSetDirectory(m_hTIFF, 0);
    2085           3 :         GTiffFillStreamableOffsetAndCount(m_hTIFF, nSize);
    2086           3 :         TIFFWriteDirectory(m_hTIFF);
    2087             : 
    2088           3 :         vsi_l_offset nDataLength = 0;
    2089             :         void *pabyBuffer =
    2090           3 :             VSIGetMemFileBuffer(m_pszTmpFilename, &nDataLength, FALSE);
    2091           3 :         if (static_cast<int>(VSIFWriteL(
    2092           3 :                 pabyBuffer, 1, static_cast<int>(nDataLength), m_fpToWrite)) !=
    2093             :             static_cast<int>(nDataLength))
    2094             :         {
    2095           0 :             ReportError(CE_Failure, CPLE_FileIO, "Could not write %d bytes",
    2096             :                         static_cast<int>(nDataLength));
    2097             :         }
    2098             :         // In case of single strip file, there's a libtiff check that would
    2099             :         // issue a warning since the file hasn't the required size.
    2100           3 :         CPLPushErrorHandler(CPLQuietErrorHandler);
    2101           3 :         TIFFSetDirectory(m_hTIFF, 0);
    2102           3 :         CPLPopErrorHandler();
    2103             :     }
    2104             :     else
    2105             :     {
    2106        3144 :         const tdir_t nNumberOfDirs = TIFFNumberOfDirectories(m_hTIFF);
    2107        3144 :         if (nNumberOfDirs > 0)
    2108             :         {
    2109        3144 :             TIFFSetDirectory(m_hTIFF, static_cast<tdir_t>(nNumberOfDirs - 1));
    2110             :         }
    2111             :     }
    2112             : 
    2113        3147 :     RestoreVolatileParameters(m_hTIFF);
    2114             : 
    2115        3147 :     m_nDirOffset = TIFFCurrentDirOffset(m_hTIFF);
    2116             : }
    2117             : 
    2118             : /************************************************************************/
    2119             : /*                             FlushCache()                             */
    2120             : /*                                                                      */
    2121             : /*      We override this so we can also flush out local tiff strip      */
    2122             : /*      cache if need be.                                               */
    2123             : /************************************************************************/
    2124             : 
    2125        4157 : CPLErr GTiffDataset::FlushCache(bool bAtClosing)
    2126             : 
    2127             : {
    2128        4157 :     return FlushCacheInternal(bAtClosing, true);
    2129             : }
    2130             : 
    2131       35996 : CPLErr GTiffDataset::FlushCacheInternal(bool bAtClosing, bool bFlushDirectory)
    2132             : {
    2133       35996 :     if (m_bIsFinalized)
    2134           1 :         return CE_None;
    2135             : 
    2136       35995 :     CPLErr eErr = GDALPamDataset::FlushCache(bAtClosing);
    2137             : 
    2138       35996 :     if (m_bLoadedBlockDirty && m_nLoadedBlock != -1)
    2139             :     {
    2140         249 :         if (FlushBlockBuf() != CE_None)
    2141           0 :             eErr = CE_Failure;
    2142             :     }
    2143             : 
    2144       35996 :     CPLFree(m_pabyBlockBuf);
    2145       35996 :     m_pabyBlockBuf = nullptr;
    2146       35996 :     m_nLoadedBlock = -1;
    2147       35996 :     m_bLoadedBlockDirty = false;
    2148             : 
    2149             :     // Finish compression
    2150       35996 :     auto poQueue = m_poBaseDS ? m_poBaseDS->m_poCompressQueue.get()
    2151       33884 :                               : m_poCompressQueue.get();
    2152       35995 :     if (poQueue)
    2153             :     {
    2154         159 :         poQueue->WaitCompletion();
    2155             : 
    2156             :         // Flush remaining data
    2157             :         // cppcheck-suppress constVariableReference
    2158             : 
    2159         159 :         auto &oQueue =
    2160         159 :             m_poBaseDS ? m_poBaseDS->m_asQueueJobIdx : m_asQueueJobIdx;
    2161         228 :         while (!oQueue.empty())
    2162             :         {
    2163          69 :             WaitCompletionForJobIdx(oQueue.front());
    2164             :         }
    2165             :     }
    2166             : 
    2167       35995 :     if (bFlushDirectory && GetAccess() == GA_Update)
    2168             :     {
    2169       10510 :         if (FlushDirectory() != CE_None)
    2170          12 :             eErr = CE_Failure;
    2171             :     }
    2172       35996 :     return eErr;
    2173             : }
    2174             : 
    2175             : /************************************************************************/
    2176             : /*                           FlushDirectory()                           */
    2177             : /************************************************************************/
    2178             : 
    2179       17055 : CPLErr GTiffDataset::FlushDirectory()
    2180             : 
    2181             : {
    2182       17055 :     CPLErr eErr = CE_None;
    2183             : 
    2184         485 :     const auto ReloadAllOtherDirectories = [this]()
    2185             :     {
    2186         238 :         const auto poBaseDS = m_poBaseDS ? m_poBaseDS : this;
    2187         238 :         if (poBaseDS->m_papoOverviewDS)
    2188             :         {
    2189          12 :             for (int i = 0; i < poBaseDS->m_nOverviewCount; ++i)
    2190             :             {
    2191           3 :                 if (poBaseDS->m_papoOverviewDS[i]->m_bCrystalized &&
    2192           3 :                     poBaseDS->m_papoOverviewDS[i] != this)
    2193             :                 {
    2194           3 :                     poBaseDS->m_papoOverviewDS[i]->ReloadDirectory(true);
    2195             :                 }
    2196             : 
    2197           3 :                 if (poBaseDS->m_papoOverviewDS[i]->m_poMaskDS &&
    2198           0 :                     poBaseDS->m_papoOverviewDS[i]->m_poMaskDS != this &&
    2199           0 :                     poBaseDS->m_papoOverviewDS[i]->m_poMaskDS->m_bCrystalized)
    2200             :                 {
    2201           0 :                     poBaseDS->m_papoOverviewDS[i]->m_poMaskDS->ReloadDirectory(
    2202             :                         true);
    2203             :                 }
    2204             :             }
    2205             :         }
    2206         238 :         if (poBaseDS->m_poMaskDS && poBaseDS->m_poMaskDS != this &&
    2207           0 :             poBaseDS->m_poMaskDS->m_bCrystalized)
    2208             :         {
    2209           0 :             poBaseDS->m_poMaskDS->ReloadDirectory(true);
    2210             :         }
    2211         238 :         if (poBaseDS->m_bCrystalized && poBaseDS != this)
    2212             :         {
    2213           6 :             poBaseDS->ReloadDirectory(true);
    2214             :         }
    2215       17293 :     };
    2216             : 
    2217       17055 :     if (eAccess == GA_Update)
    2218             :     {
    2219       11963 :         if (m_bMetadataChanged)
    2220             :         {
    2221         134 :             m_bNeedsRewrite =
    2222         268 :                 WriteMetadata(this, m_hTIFF, true, m_eProfile, m_pszFilename,
    2223         134 :                               m_papszCreationOptions);
    2224         134 :             m_bMetadataChanged = false;
    2225             : 
    2226         134 :             if (m_bForceUnsetRPC)
    2227             :             {
    2228           5 :                 double *padfRPCTag = nullptr;
    2229             :                 uint16_t nCount;
    2230           5 :                 if (TIFFGetField(m_hTIFF, TIFFTAG_RPCCOEFFICIENT, &nCount,
    2231           5 :                                  &padfRPCTag))
    2232             :                 {
    2233           3 :                     std::vector<double> zeroes(92);
    2234           3 :                     TIFFSetField(m_hTIFF, TIFFTAG_RPCCOEFFICIENT, 92,
    2235             :                                  zeroes.data());
    2236           3 :                     TIFFUnsetField(m_hTIFF, TIFFTAG_RPCCOEFFICIENT);
    2237           3 :                     m_bNeedsRewrite = true;
    2238             :                 }
    2239             : 
    2240           5 :                 GDALWriteRPCTXTFile(m_pszFilename, nullptr);
    2241           5 :                 GDALWriteRPBFile(m_pszFilename, nullptr);
    2242             :             }
    2243             :         }
    2244             : 
    2245       11963 :         if (m_bGeoTIFFInfoChanged)
    2246             :         {
    2247         120 :             WriteGeoTIFFInfo();
    2248         120 :             m_bGeoTIFFInfoChanged = false;
    2249             :         }
    2250             : 
    2251       11963 :         if (m_bNoDataChanged)
    2252             :         {
    2253          30 :             if (m_bNoDataSet)
    2254             :             {
    2255          29 :                 WriteNoDataValue(m_hTIFF, m_dfNoDataValue);
    2256             :             }
    2257           1 :             else if (m_bNoDataSetAsInt64)
    2258             :             {
    2259           0 :                 WriteNoDataValue(m_hTIFF, m_nNoDataValueInt64);
    2260             :             }
    2261           1 :             else if (m_bNoDataSetAsUInt64)
    2262             :             {
    2263           0 :                 WriteNoDataValue(m_hTIFF, m_nNoDataValueUInt64);
    2264             :             }
    2265             :             else
    2266             :             {
    2267           1 :                 UnsetNoDataValue(m_hTIFF);
    2268             :             }
    2269          30 :             m_bNeedsRewrite = true;
    2270          30 :             m_bNoDataChanged = false;
    2271             :         }
    2272             : 
    2273       11963 :         if (m_bNeedsRewrite)
    2274             :         {
    2275         260 :             if (!m_bCrystalized)
    2276             :             {
    2277          25 :                 Crystalize();
    2278             :             }
    2279             :             else
    2280             :             {
    2281         235 :                 const TIFFSizeProc pfnSizeProc = TIFFGetSizeProc(m_hTIFF);
    2282             : 
    2283         235 :                 m_nDirOffset = pfnSizeProc(TIFFClientdata(m_hTIFF));
    2284         235 :                 if ((m_nDirOffset % 2) == 1)
    2285          50 :                     ++m_nDirOffset;
    2286             : 
    2287         235 :                 if (TIFFRewriteDirectory(m_hTIFF) == 0)
    2288           0 :                     eErr = CE_Failure;
    2289             : 
    2290         235 :                 TIFFSetSubDirectory(m_hTIFF, m_nDirOffset);
    2291             : 
    2292         235 :                 ReloadAllOtherDirectories();
    2293             : 
    2294         235 :                 if (m_bLayoutIFDSBeforeData && m_bBlockOrderRowMajor &&
    2295           1 :                     m_bLeaderSizeAsUInt4 &&
    2296           1 :                     m_bTrailerRepeatedLast4BytesRepeated &&
    2297           1 :                     !m_bKnownIncompatibleEdition &&
    2298           1 :                     !m_bWriteKnownIncompatibleEdition)
    2299             :                 {
    2300           1 :                     ReportError(CE_Warning, CPLE_AppDefined,
    2301             :                                 "The IFD has been rewritten at the end of "
    2302             :                                 "the file, which breaks COG layout.");
    2303           1 :                     m_bKnownIncompatibleEdition = true;
    2304           1 :                     m_bWriteKnownIncompatibleEdition = true;
    2305             :                 }
    2306             :             }
    2307             : 
    2308         260 :             m_bNeedsRewrite = false;
    2309             :         }
    2310             :     }
    2311             : 
    2312             :     // There are some circumstances in which we can reach this point
    2313             :     // without having made this our directory (SetDirectory()) in which
    2314             :     // case we should not risk a flush.
    2315       29018 :     if (GetAccess() == GA_Update &&
    2316       11963 :         TIFFCurrentDirOffset(m_hTIFF) == m_nDirOffset)
    2317             :     {
    2318       11963 :         const TIFFSizeProc pfnSizeProc = TIFFGetSizeProc(m_hTIFF);
    2319             : 
    2320       11963 :         toff_t nNewDirOffset = pfnSizeProc(TIFFClientdata(m_hTIFF));
    2321       11963 :         if ((nNewDirOffset % 2) == 1)
    2322        2985 :             ++nNewDirOffset;
    2323             : 
    2324       11963 :         if (TIFFFlush(m_hTIFF) == 0)
    2325          12 :             eErr = CE_Failure;
    2326             : 
    2327       11963 :         if (m_nDirOffset != TIFFCurrentDirOffset(m_hTIFF))
    2328             :         {
    2329           3 :             m_nDirOffset = nNewDirOffset;
    2330           3 :             ReloadAllOtherDirectories();
    2331           3 :             CPLDebug("GTiff",
    2332             :                      "directory moved during flush in FlushDirectory()");
    2333             :         }
    2334             :     }
    2335             : 
    2336       17055 :     SetDirectory();
    2337       17055 :     return eErr;
    2338             : }
    2339             : 
    2340             : /************************************************************************/
    2341             : /*                           CleanOverviews()                           */
    2342             : /************************************************************************/
    2343             : 
    2344           3 : CPLErr GTiffDataset::CleanOverviews()
    2345             : 
    2346             : {
    2347           3 :     CPLAssert(!m_poBaseDS);
    2348             : 
    2349           3 :     ScanDirectories();
    2350             : 
    2351           3 :     FlushDirectory();
    2352             : 
    2353             :     /* -------------------------------------------------------------------- */
    2354             :     /*      Cleanup overviews objects, and get offsets to all overview      */
    2355             :     /*      directories.                                                    */
    2356             :     /* -------------------------------------------------------------------- */
    2357           6 :     std::vector<toff_t> anOvDirOffsets;
    2358             : 
    2359           6 :     for (int i = 0; i < m_nOverviewCount; ++i)
    2360             :     {
    2361           3 :         anOvDirOffsets.push_back(m_papoOverviewDS[i]->m_nDirOffset);
    2362           3 :         if (m_papoOverviewDS[i]->m_poMaskDS)
    2363           1 :             anOvDirOffsets.push_back(
    2364           1 :                 m_papoOverviewDS[i]->m_poMaskDS->m_nDirOffset);
    2365           3 :         delete m_papoOverviewDS[i];
    2366             :     }
    2367             : 
    2368             :     /* -------------------------------------------------------------------- */
    2369             :     /*      Loop through all the directories, translating the offsets       */
    2370             :     /*      into indexes we can use with TIFFUnlinkDirectory().             */
    2371             :     /* -------------------------------------------------------------------- */
    2372           6 :     std::vector<uint16_t> anOvDirIndexes;
    2373           3 :     int iThisOffset = 1;
    2374             : 
    2375           3 :     TIFFSetDirectory(m_hTIFF, 0);
    2376             : 
    2377             :     while (true)
    2378             :     {
    2379          20 :         for (toff_t nOffset : anOvDirOffsets)
    2380             :         {
    2381          12 :             if (nOffset == TIFFCurrentDirOffset(m_hTIFF))
    2382             :             {
    2383           4 :                 anOvDirIndexes.push_back(static_cast<uint16_t>(iThisOffset));
    2384             :             }
    2385             :         }
    2386             : 
    2387           8 :         if (TIFFLastDirectory(m_hTIFF))
    2388           3 :             break;
    2389             : 
    2390           5 :         TIFFReadDirectory(m_hTIFF);
    2391           5 :         ++iThisOffset;
    2392           5 :     }
    2393             : 
    2394             :     /* -------------------------------------------------------------------- */
    2395             :     /*      Actually unlink the target directories.  Note that we do        */
    2396             :     /*      this from last to first so as to avoid renumbering any of       */
    2397             :     /*      the earlier directories we need to remove.                      */
    2398             :     /* -------------------------------------------------------------------- */
    2399           7 :     while (!anOvDirIndexes.empty())
    2400             :     {
    2401           4 :         TIFFUnlinkDirectory(m_hTIFF, anOvDirIndexes.back());
    2402           4 :         anOvDirIndexes.pop_back();
    2403             :     }
    2404             : 
    2405           3 :     CPLFree(m_papoOverviewDS);
    2406           3 :     m_nOverviewCount = 0;
    2407           3 :     m_papoOverviewDS = nullptr;
    2408             : 
    2409           3 :     if (m_poMaskDS)
    2410             :     {
    2411           1 :         CPLFree(m_poMaskDS->m_papoOverviewDS);
    2412           1 :         m_poMaskDS->m_nOverviewCount = 0;
    2413           1 :         m_poMaskDS->m_papoOverviewDS = nullptr;
    2414             :     }
    2415             : 
    2416           3 :     if (!SetDirectory())
    2417           0 :         return CE_Failure;
    2418             : 
    2419           3 :     return CE_None;
    2420             : }
    2421             : 
    2422             : /************************************************************************/
    2423             : /*                   RegisterNewOverviewDataset()                       */
    2424             : /************************************************************************/
    2425             : 
    2426         470 : CPLErr GTiffDataset::RegisterNewOverviewDataset(toff_t nOverviewOffset,
    2427             :                                                 int l_nJpegQuality,
    2428             :                                                 CSLConstList papszOptions)
    2429             : {
    2430         470 :     if (m_nOverviewCount == 127)
    2431           0 :         return CE_Failure;
    2432             : 
    2433             :     const auto GetOptionValue =
    2434        2820 :         [papszOptions](const char *pszOptionKey, const char *pszConfigOptionKey,
    2435        5639 :                        const char **ppszKeyUsed = nullptr)
    2436             :     {
    2437        2820 :         const char *pszVal = CSLFetchNameValue(papszOptions, pszOptionKey);
    2438        2820 :         if (pszVal)
    2439             :         {
    2440           1 :             if (ppszKeyUsed)
    2441           1 :                 *ppszKeyUsed = pszOptionKey;
    2442           1 :             return pszVal;
    2443             :         }
    2444        2819 :         pszVal = CSLFetchNameValue(papszOptions, pszConfigOptionKey);
    2445        2819 :         if (pszVal)
    2446             :         {
    2447           0 :             if (ppszKeyUsed)
    2448           0 :                 *ppszKeyUsed = pszConfigOptionKey;
    2449           0 :             return pszVal;
    2450             :         }
    2451        2819 :         pszVal = CPLGetConfigOption(pszConfigOptionKey, nullptr);
    2452        2819 :         if (pszVal && ppszKeyUsed)
    2453          13 :             *ppszKeyUsed = pszConfigOptionKey;
    2454        2819 :         return pszVal;
    2455         470 :     };
    2456             : 
    2457         470 :     int nZLevel = m_nZLevel;
    2458         470 :     if (const char *opt = GetOptionValue("ZLEVEL", "ZLEVEL_OVERVIEW"))
    2459             :     {
    2460           4 :         nZLevel = atoi(opt);
    2461             :     }
    2462             : 
    2463         470 :     int nZSTDLevel = m_nZSTDLevel;
    2464         470 :     if (const char *opt = GetOptionValue("ZSTD_LEVEL", "ZSTD_LEVEL_OVERVIEW"))
    2465             :     {
    2466           4 :         nZSTDLevel = atoi(opt);
    2467             :     }
    2468             : 
    2469         470 :     bool bWebpLossless = m_bWebPLossless;
    2470             :     const char *pszWebPLosslessOverview =
    2471         470 :         GetOptionValue("WEBP_LOSSLESS", "WEBP_LOSSLESS_OVERVIEW");
    2472         470 :     if (pszWebPLosslessOverview)
    2473             :     {
    2474           2 :         bWebpLossless = CPLTestBool(pszWebPLosslessOverview);
    2475             :     }
    2476             : 
    2477         470 :     int nWebpLevel = m_nWebPLevel;
    2478         470 :     const char *pszKeyWebpLevel = "";
    2479         470 :     if (const char *opt = GetOptionValue("WEBP_LEVEL", "WEBP_LEVEL_OVERVIEW",
    2480             :                                          &pszKeyWebpLevel))
    2481             :     {
    2482          14 :         if (pszWebPLosslessOverview == nullptr && m_bWebPLossless)
    2483             :         {
    2484           1 :             CPLDebug("GTiff",
    2485             :                      "%s specified, but not WEBP_LOSSLESS_OVERVIEW. "
    2486             :                      "Assuming WEBP_LOSSLESS_OVERVIEW=NO",
    2487             :                      pszKeyWebpLevel);
    2488           1 :             bWebpLossless = false;
    2489             :         }
    2490          13 :         else if (bWebpLossless)
    2491             :         {
    2492           0 :             CPLError(CE_Warning, CPLE_AppDefined,
    2493             :                      "%s is specified, but WEBP_LOSSLESS_OVERVIEW=YES. "
    2494             :                      "%s will be ignored.",
    2495             :                      pszKeyWebpLevel, pszKeyWebpLevel);
    2496             :         }
    2497          14 :         nWebpLevel = atoi(opt);
    2498             :     }
    2499             : 
    2500         470 :     double dfMaxZError = m_dfMaxZErrorOverview;
    2501         470 :     if (const char *opt = GetOptionValue("MAX_Z_ERROR", "MAX_Z_ERROR_OVERVIEW"))
    2502             :     {
    2503          20 :         dfMaxZError = CPLAtof(opt);
    2504             :     }
    2505             : 
    2506         470 :     GTiffDataset *poODS = new GTiffDataset();
    2507         470 :     poODS->ShareLockWithParentDataset(this);
    2508         470 :     poODS->m_pszFilename = CPLStrdup(m_pszFilename);
    2509         470 :     const char *pszSparseOK = GetOptionValue("SPARSE_OK", "SPARSE_OK_OVERVIEW");
    2510         470 :     if (pszSparseOK && CPLTestBool(pszSparseOK))
    2511             :     {
    2512           1 :         poODS->m_bWriteEmptyTiles = false;
    2513           1 :         poODS->m_bFillEmptyTilesAtClosing = false;
    2514             :     }
    2515             :     else
    2516             :     {
    2517         469 :         poODS->m_bWriteEmptyTiles = m_bWriteEmptyTiles;
    2518         469 :         poODS->m_bFillEmptyTilesAtClosing = m_bFillEmptyTilesAtClosing;
    2519             :     }
    2520         470 :     poODS->m_nJpegQuality = static_cast<signed char>(l_nJpegQuality);
    2521         470 :     poODS->m_nWebPLevel = static_cast<signed char>(nWebpLevel);
    2522         470 :     poODS->m_nZLevel = static_cast<signed char>(nZLevel);
    2523         470 :     poODS->m_nLZMAPreset = m_nLZMAPreset;
    2524         470 :     poODS->m_nZSTDLevel = static_cast<signed char>(nZSTDLevel);
    2525         470 :     poODS->m_bWebPLossless = bWebpLossless;
    2526         470 :     poODS->m_nJpegTablesMode = m_nJpegTablesMode;
    2527         470 :     poODS->m_dfMaxZError = dfMaxZError;
    2528         470 :     poODS->m_dfMaxZErrorOverview = dfMaxZError;
    2529         470 :     memcpy(poODS->m_anLercAddCompressionAndVersion,
    2530         470 :            m_anLercAddCompressionAndVersion,
    2531             :            sizeof(m_anLercAddCompressionAndVersion));
    2532             : #ifdef HAVE_JXL
    2533         470 :     poODS->m_bJXLLossless = m_bJXLLossless;
    2534         470 :     poODS->m_fJXLDistance = m_fJXLDistance;
    2535         470 :     poODS->m_fJXLAlphaDistance = m_fJXLAlphaDistance;
    2536         470 :     poODS->m_nJXLEffort = m_nJXLEffort;
    2537             : #endif
    2538             : 
    2539         470 :     if (poODS->OpenOffset(VSI_TIFFOpenChild(m_hTIFF), nOverviewOffset,
    2540         470 :                           GA_Update) != CE_None)
    2541             :     {
    2542           0 :         delete poODS;
    2543           0 :         return CE_Failure;
    2544             :     }
    2545             : 
    2546             :     // Assign color interpretation from main dataset
    2547         470 :     const int l_nBands = GetRasterCount();
    2548        1398 :     for (int i = 1; i <= l_nBands; i++)
    2549             :     {
    2550         928 :         auto poBand = dynamic_cast<GTiffRasterBand *>(poODS->GetRasterBand(i));
    2551         928 :         if (poBand)
    2552         928 :             poBand->m_eBandInterp = GetRasterBand(i)->GetColorInterpretation();
    2553             :     }
    2554             : 
    2555             :     // Do that now that m_nCompression is set
    2556         470 :     poODS->RestoreVolatileParameters(poODS->m_hTIFF);
    2557             : 
    2558         470 :     ++m_nOverviewCount;
    2559         470 :     m_papoOverviewDS = static_cast<GTiffDataset **>(
    2560         470 :         CPLRealloc(m_papoOverviewDS, m_nOverviewCount * (sizeof(void *))));
    2561         470 :     m_papoOverviewDS[m_nOverviewCount - 1] = poODS;
    2562         470 :     poODS->m_poBaseDS = this;
    2563         470 :     poODS->m_bIsOverview = true;
    2564         470 :     return CE_None;
    2565             : }
    2566             : 
    2567             : /************************************************************************/
    2568             : /*                     CreateTIFFColorTable()                           */
    2569             : /************************************************************************/
    2570             : 
    2571          12 : static void CreateTIFFColorTable(
    2572             :     GDALColorTable *poColorTable, int nBits, int nColorTableMultiplier,
    2573             :     std::vector<unsigned short> &anTRed, std::vector<unsigned short> &anTGreen,
    2574             :     std::vector<unsigned short> &anTBlue, unsigned short *&panRed,
    2575             :     unsigned short *&panGreen, unsigned short *&panBlue)
    2576             : {
    2577             :     int nColors;
    2578             : 
    2579          12 :     if (nBits == 8)
    2580          12 :         nColors = 256;
    2581           0 :     else if (nBits < 8)
    2582           0 :         nColors = 1 << nBits;
    2583             :     else
    2584           0 :         nColors = 65536;
    2585             : 
    2586          12 :     anTRed.resize(nColors, 0);
    2587          12 :     anTGreen.resize(nColors, 0);
    2588          12 :     anTBlue.resize(nColors, 0);
    2589             : 
    2590        3084 :     for (int iColor = 0; iColor < nColors; ++iColor)
    2591             :     {
    2592        3072 :         if (iColor < poColorTable->GetColorEntryCount())
    2593             :         {
    2594             :             GDALColorEntry sRGB;
    2595             : 
    2596        3072 :             poColorTable->GetColorEntryAsRGB(iColor, &sRGB);
    2597             : 
    2598        3072 :             anTRed[iColor] = GTiffDataset::ClampCTEntry(iColor, 1, sRGB.c1,
    2599             :                                                         nColorTableMultiplier);
    2600        3072 :             anTGreen[iColor] = GTiffDataset::ClampCTEntry(
    2601        3072 :                 iColor, 2, sRGB.c2, nColorTableMultiplier);
    2602        3072 :             anTBlue[iColor] = GTiffDataset::ClampCTEntry(iColor, 3, sRGB.c3,
    2603             :                                                          nColorTableMultiplier);
    2604             :         }
    2605             :         else
    2606             :         {
    2607           0 :             anTRed[iColor] = 0;
    2608           0 :             anTGreen[iColor] = 0;
    2609           0 :             anTBlue[iColor] = 0;
    2610             :         }
    2611             :     }
    2612             : 
    2613          12 :     panRed = &(anTRed[0]);
    2614          12 :     panGreen = &(anTGreen[0]);
    2615          12 :     panBlue = &(anTBlue[0]);
    2616          12 : }
    2617             : 
    2618             : /************************************************************************/
    2619             : /*                        GetOverviewParameters()                       */
    2620             : /************************************************************************/
    2621             : 
    2622         294 : bool GTiffDataset::GetOverviewParameters(
    2623             :     int &nCompression, uint16_t &nPlanarConfig, uint16_t &nPredictor,
    2624             :     uint16_t &nPhotometric, int &nOvrJpegQuality, std::string &osNoData,
    2625             :     uint16_t *&panExtraSampleValues, uint16_t &nExtraSamples,
    2626             :     CSLConstList papszOptions) const
    2627             : {
    2628             :     const auto GetOptionValue =
    2629         968 :         [papszOptions](const char *pszOptionKey, const char *pszConfigOptionKey,
    2630        1932 :                        const char **ppszKeyUsed = nullptr)
    2631             :     {
    2632         968 :         const char *pszVal = CSLFetchNameValue(papszOptions, pszOptionKey);
    2633         968 :         if (pszVal)
    2634             :         {
    2635           4 :             if (ppszKeyUsed)
    2636           4 :                 *ppszKeyUsed = pszOptionKey;
    2637           4 :             return pszVal;
    2638             :         }
    2639         964 :         pszVal = CSLFetchNameValue(papszOptions, pszConfigOptionKey);
    2640         964 :         if (pszVal)
    2641             :         {
    2642           4 :             if (ppszKeyUsed)
    2643           4 :                 *ppszKeyUsed = pszConfigOptionKey;
    2644           4 :             return pszVal;
    2645             :         }
    2646         960 :         pszVal = CPLGetConfigOption(pszConfigOptionKey, nullptr);
    2647         960 :         if (pszVal && ppszKeyUsed)
    2648          47 :             *ppszKeyUsed = pszConfigOptionKey;
    2649         960 :         return pszVal;
    2650         294 :     };
    2651             : 
    2652             :     /* -------------------------------------------------------------------- */
    2653             :     /*      Determine compression method.                                   */
    2654             :     /* -------------------------------------------------------------------- */
    2655         294 :     nCompression = m_nCompression;
    2656         294 :     const char *pszOptionKey = "";
    2657             :     const char *pszCompressValue =
    2658         294 :         GetOptionValue("COMPRESS", "COMPRESS_OVERVIEW", &pszOptionKey);
    2659         294 :     if (pszCompressValue != nullptr)
    2660             :     {
    2661          45 :         nCompression =
    2662          45 :             GTIFFGetCompressionMethod(pszCompressValue, pszOptionKey);
    2663          45 :         if (nCompression < 0)
    2664             :         {
    2665           0 :             nCompression = m_nCompression;
    2666             :         }
    2667             :     }
    2668             : 
    2669             :     /* -------------------------------------------------------------------- */
    2670             :     /*      Determine planar configuration.                                 */
    2671             :     /* -------------------------------------------------------------------- */
    2672         294 :     nPlanarConfig = m_nPlanarConfig;
    2673         294 :     if (nCompression == COMPRESSION_WEBP)
    2674             :     {
    2675          11 :         nPlanarConfig = PLANARCONFIG_CONTIG;
    2676             :     }
    2677             :     const char *pszInterleave =
    2678         294 :         GetOptionValue("INTERLEAVE", "INTERLEAVE_OVERVIEW", &pszOptionKey);
    2679         294 :     if (pszInterleave != nullptr && pszInterleave[0] != '\0')
    2680             :     {
    2681           2 :         if (EQUAL(pszInterleave, "PIXEL"))
    2682           1 :             nPlanarConfig = PLANARCONFIG_CONTIG;
    2683           1 :         else if (EQUAL(pszInterleave, "BAND"))
    2684           1 :             nPlanarConfig = PLANARCONFIG_SEPARATE;
    2685             :         else
    2686             :         {
    2687           0 :             CPLError(CE_Warning, CPLE_AppDefined,
    2688             :                      "%s=%s unsupported, "
    2689             :                      "value must be PIXEL or BAND. ignoring",
    2690             :                      pszOptionKey, pszInterleave);
    2691             :         }
    2692             :     }
    2693             : 
    2694             :     /* -------------------------------------------------------------------- */
    2695             :     /*      Determine predictor tag                                         */
    2696             :     /* -------------------------------------------------------------------- */
    2697         294 :     nPredictor = PREDICTOR_NONE;
    2698         294 :     if (GTIFFSupportsPredictor(nCompression))
    2699             :     {
    2700             :         const char *pszPredictor =
    2701          59 :             GetOptionValue("PREDICTOR", "PREDICTOR_OVERVIEW");
    2702          59 :         if (pszPredictor != nullptr)
    2703             :         {
    2704           1 :             nPredictor = static_cast<uint16_t>(atoi(pszPredictor));
    2705             :         }
    2706          58 :         else if (GTIFFSupportsPredictor(m_nCompression))
    2707          57 :             TIFFGetField(m_hTIFF, TIFFTAG_PREDICTOR, &nPredictor);
    2708             :     }
    2709             : 
    2710             :     /* -------------------------------------------------------------------- */
    2711             :     /*      Determine photometric tag                                       */
    2712             :     /* -------------------------------------------------------------------- */
    2713         294 :     if (m_nPhotometric == PHOTOMETRIC_YCBCR && nCompression != COMPRESSION_JPEG)
    2714           1 :         nPhotometric = PHOTOMETRIC_RGB;
    2715             :     else
    2716         293 :         nPhotometric = m_nPhotometric;
    2717             :     const char *pszPhotometric =
    2718         294 :         GetOptionValue("PHOTOMETRIC", "PHOTOMETRIC_OVERVIEW", &pszOptionKey);
    2719         294 :     if (!GTIFFUpdatePhotometric(pszPhotometric, pszOptionKey, nCompression,
    2720         294 :                                 pszInterleave, nBands, nPhotometric,
    2721             :                                 nPlanarConfig))
    2722             :     {
    2723           0 :         return false;
    2724             :     }
    2725             : 
    2726             :     /* -------------------------------------------------------------------- */
    2727             :     /*      Determine JPEG quality                                          */
    2728             :     /* -------------------------------------------------------------------- */
    2729         294 :     nOvrJpegQuality = m_nJpegQuality;
    2730         294 :     if (nCompression == COMPRESSION_JPEG)
    2731             :     {
    2732             :         const char *pszJPEGQuality =
    2733          27 :             GetOptionValue("JPEG_QUALITY", "JPEG_QUALITY_OVERVIEW");
    2734          27 :         if (pszJPEGQuality != nullptr)
    2735             :         {
    2736           9 :             nOvrJpegQuality = atoi(pszJPEGQuality);
    2737             :         }
    2738             :     }
    2739             : 
    2740             :     /* -------------------------------------------------------------------- */
    2741             :     /*      Set nodata.                                                     */
    2742             :     /* -------------------------------------------------------------------- */
    2743         294 :     if (m_bNoDataSet)
    2744             :     {
    2745          16 :         osNoData = GTiffFormatGDALNoDataTagValue(m_dfNoDataValue);
    2746             :     }
    2747             : 
    2748             :     /* -------------------------------------------------------------------- */
    2749             :     /*      Fetch extra sample tag                                          */
    2750             :     /* -------------------------------------------------------------------- */
    2751         294 :     panExtraSampleValues = nullptr;
    2752         294 :     nExtraSamples = 0;
    2753         294 :     if (TIFFGetField(m_hTIFF, TIFFTAG_EXTRASAMPLES, &nExtraSamples,
    2754         294 :                      &panExtraSampleValues))
    2755             :     {
    2756             :         uint16_t *panExtraSampleValuesNew = static_cast<uint16_t *>(
    2757          39 :             CPLMalloc(nExtraSamples * sizeof(uint16_t)));
    2758          39 :         memcpy(panExtraSampleValuesNew, panExtraSampleValues,
    2759          39 :                nExtraSamples * sizeof(uint16_t));
    2760          39 :         panExtraSampleValues = panExtraSampleValuesNew;
    2761             :     }
    2762             :     else
    2763             :     {
    2764         255 :         panExtraSampleValues = nullptr;
    2765         255 :         nExtraSamples = 0;
    2766             :     }
    2767             : 
    2768         294 :     return true;
    2769             : }
    2770             : 
    2771             : /************************************************************************/
    2772             : /*                  CreateOverviewsFromSrcOverviews()                   */
    2773             : /************************************************************************/
    2774             : 
    2775             : // If poOvrDS is not null, it is used and poSrcDS is ignored.
    2776             : 
    2777          57 : CPLErr GTiffDataset::CreateOverviewsFromSrcOverviews(GDALDataset *poSrcDS,
    2778             :                                                      GDALDataset *poOvrDS,
    2779             :                                                      int nOverviews)
    2780             : {
    2781          57 :     CPLAssert(poSrcDS->GetRasterCount() != 0);
    2782          57 :     CPLAssert(m_nOverviewCount == 0);
    2783             : 
    2784          57 :     ScanDirectories();
    2785             : 
    2786          57 :     FlushDirectory();
    2787             : 
    2788          57 :     int nOvBitsPerSample = m_nBitsPerSample;
    2789             : 
    2790             :     /* -------------------------------------------------------------------- */
    2791             :     /*      Do we need some metadata for the overviews?                     */
    2792             :     /* -------------------------------------------------------------------- */
    2793         114 :     CPLString osMetadata;
    2794             : 
    2795          57 :     GTIFFBuildOverviewMetadata("NONE", this, false, osMetadata);
    2796             : 
    2797             :     int nCompression;
    2798             :     uint16_t nPlanarConfig;
    2799             :     uint16_t nPredictor;
    2800             :     uint16_t nPhotometric;
    2801             :     int nOvrJpegQuality;
    2802         114 :     std::string osNoData;
    2803          57 :     uint16_t *panExtraSampleValues = nullptr;
    2804          57 :     uint16_t nExtraSamples = 0;
    2805          57 :     if (!GetOverviewParameters(nCompression, nPlanarConfig, nPredictor,
    2806             :                                nPhotometric, nOvrJpegQuality, osNoData,
    2807             :                                panExtraSampleValues, nExtraSamples,
    2808             :                                /*papszOptions=*/nullptr))
    2809             :     {
    2810           0 :         return CE_Failure;
    2811             :     }
    2812             : 
    2813             :     /* -------------------------------------------------------------------- */
    2814             :     /*      Do we have a palette?  If so, create a TIFF compatible version. */
    2815             :     /* -------------------------------------------------------------------- */
    2816         114 :     std::vector<unsigned short> anTRed;
    2817         114 :     std::vector<unsigned short> anTGreen;
    2818          57 :     std::vector<unsigned short> anTBlue;
    2819          57 :     unsigned short *panRed = nullptr;
    2820          57 :     unsigned short *panGreen = nullptr;
    2821          57 :     unsigned short *panBlue = nullptr;
    2822             : 
    2823          57 :     if (nPhotometric == PHOTOMETRIC_PALETTE && m_poColorTable != nullptr)
    2824             :     {
    2825           0 :         if (m_nColorTableMultiplier == 0)
    2826           0 :             m_nColorTableMultiplier = DEFAULT_COLOR_TABLE_MULTIPLIER_257;
    2827             : 
    2828           0 :         CreateTIFFColorTable(m_poColorTable.get(), nOvBitsPerSample,
    2829             :                              m_nColorTableMultiplier, anTRed, anTGreen, anTBlue,
    2830             :                              panRed, panGreen, panBlue);
    2831             :     }
    2832             : 
    2833          57 :     int nOvrBlockXSize = 0;
    2834          57 :     int nOvrBlockYSize = 0;
    2835          57 :     GTIFFGetOverviewBlockSize(GDALRasterBand::ToHandle(GetRasterBand(1)),
    2836             :                               &nOvrBlockXSize, &nOvrBlockYSize);
    2837             : 
    2838          57 :     CPLErr eErr = CE_None;
    2839             : 
    2840         169 :     for (int i = 0; i < nOverviews && eErr == CE_None; ++i)
    2841             :     {
    2842             :         GDALRasterBand *poOvrBand =
    2843         149 :             poOvrDS ? ((i == 0) ? poOvrDS->GetRasterBand(1)
    2844          37 :                                 : poOvrDS->GetRasterBand(1)->GetOverview(i - 1))
    2845          44 :                     : poSrcDS->GetRasterBand(1)->GetOverview(i);
    2846             : 
    2847         112 :         int nOXSize = poOvrBand->GetXSize();
    2848         112 :         int nOYSize = poOvrBand->GetYSize();
    2849             : 
    2850         224 :         toff_t nOverviewOffset = GTIFFWriteDirectory(
    2851             :             m_hTIFF, FILETYPE_REDUCEDIMAGE, nOXSize, nOYSize, nOvBitsPerSample,
    2852         112 :             nPlanarConfig, m_nSamplesPerPixel, nOvrBlockXSize, nOvrBlockYSize,
    2853         112 :             TRUE, nCompression, nPhotometric, m_nSampleFormat, nPredictor,
    2854             :             panRed, panGreen, panBlue, nExtraSamples, panExtraSampleValues,
    2855             :             osMetadata,
    2856         112 :             nOvrJpegQuality >= 0 ? CPLSPrintf("%d", nOvrJpegQuality) : nullptr,
    2857         112 :             CPLSPrintf("%d", m_nJpegTablesMode),
    2858           2 :             osNoData.empty() ? nullptr : osNoData.c_str(),
    2859         112 :             m_anLercAddCompressionAndVersion, m_bWriteCOGLayout);
    2860             : 
    2861         112 :         if (nOverviewOffset == 0)
    2862           0 :             eErr = CE_Failure;
    2863             :         else
    2864         112 :             eErr = RegisterNewOverviewDataset(nOverviewOffset, nOvrJpegQuality,
    2865             :                                               nullptr);
    2866             :     }
    2867             : 
    2868             :     // For directory reloading, so that the chaining to the next directory is
    2869             :     // reloaded, as well as compression parameters.
    2870          57 :     ReloadDirectory();
    2871             : 
    2872          57 :     CPLFree(panExtraSampleValues);
    2873          57 :     panExtraSampleValues = nullptr;
    2874             : 
    2875          57 :     return eErr;
    2876             : }
    2877             : 
    2878             : /************************************************************************/
    2879             : /*                       CreateInternalMaskOverviews()                  */
    2880             : /************************************************************************/
    2881             : 
    2882         251 : CPLErr GTiffDataset::CreateInternalMaskOverviews(int nOvrBlockXSize,
    2883             :                                                  int nOvrBlockYSize)
    2884             : {
    2885         251 :     ScanDirectories();
    2886             : 
    2887             :     /* -------------------------------------------------------------------- */
    2888             :     /*      Create overviews for the mask.                                  */
    2889             :     /* -------------------------------------------------------------------- */
    2890         251 :     CPLErr eErr = CE_None;
    2891             : 
    2892         251 :     if (m_poMaskDS != nullptr && m_poMaskDS->GetRasterCount() == 1)
    2893             :     {
    2894             :         int nMaskOvrCompression;
    2895          33 :         if (strstr(GDALGetMetadataItem(GDALGetDriverByName("GTiff"),
    2896             :                                        GDAL_DMD_CREATIONOPTIONLIST, nullptr),
    2897          33 :                    "<Value>DEFLATE</Value>") != nullptr)
    2898          33 :             nMaskOvrCompression = COMPRESSION_ADOBE_DEFLATE;
    2899             :         else
    2900           0 :             nMaskOvrCompression = COMPRESSION_PACKBITS;
    2901             : 
    2902          95 :         for (int i = 0; i < m_nOverviewCount; ++i)
    2903             :         {
    2904          62 :             if (m_papoOverviewDS[i]->m_poMaskDS == nullptr)
    2905             :             {
    2906         100 :                 const toff_t nOverviewOffset = GTIFFWriteDirectory(
    2907             :                     m_hTIFF, FILETYPE_REDUCEDIMAGE | FILETYPE_MASK,
    2908          50 :                     m_papoOverviewDS[i]->nRasterXSize,
    2909          50 :                     m_papoOverviewDS[i]->nRasterYSize, 1, PLANARCONFIG_CONTIG,
    2910             :                     1, nOvrBlockXSize, nOvrBlockYSize, TRUE,
    2911             :                     nMaskOvrCompression, PHOTOMETRIC_MASK, SAMPLEFORMAT_UINT,
    2912             :                     PREDICTOR_NONE, nullptr, nullptr, nullptr, 0, nullptr, "",
    2913          50 :                     nullptr, nullptr, nullptr, nullptr, m_bWriteCOGLayout);
    2914             : 
    2915          50 :                 if (nOverviewOffset == 0)
    2916             :                 {
    2917           0 :                     eErr = CE_Failure;
    2918           0 :                     continue;
    2919             :                 }
    2920             : 
    2921          50 :                 GTiffDataset *poODS = new GTiffDataset();
    2922          50 :                 poODS->ShareLockWithParentDataset(this);
    2923          50 :                 poODS->m_pszFilename = CPLStrdup(m_pszFilename);
    2924          50 :                 if (poODS->OpenOffset(VSI_TIFFOpenChild(m_hTIFF),
    2925          50 :                                       nOverviewOffset, GA_Update) != CE_None)
    2926             :                 {
    2927           0 :                     delete poODS;
    2928           0 :                     eErr = CE_Failure;
    2929             :                 }
    2930             :                 else
    2931             :                 {
    2932          50 :                     poODS->m_bPromoteTo8Bits = CPLTestBool(CPLGetConfigOption(
    2933             :                         "GDAL_TIFF_INTERNAL_MASK_TO_8BIT", "YES"));
    2934          50 :                     poODS->m_poBaseDS = this;
    2935          50 :                     poODS->m_poImageryDS = m_papoOverviewDS[i];
    2936          50 :                     m_papoOverviewDS[i]->m_poMaskDS = poODS;
    2937          50 :                     ++m_poMaskDS->m_nOverviewCount;
    2938         100 :                     m_poMaskDS->m_papoOverviewDS =
    2939         100 :                         static_cast<GTiffDataset **>(CPLRealloc(
    2940          50 :                             m_poMaskDS->m_papoOverviewDS,
    2941          50 :                             m_poMaskDS->m_nOverviewCount * (sizeof(void *))));
    2942          50 :                     m_poMaskDS
    2943          50 :                         ->m_papoOverviewDS[m_poMaskDS->m_nOverviewCount - 1] =
    2944             :                         poODS;
    2945             :                 }
    2946             :             }
    2947             :         }
    2948             :     }
    2949             : 
    2950         251 :     ReloadDirectory();
    2951             : 
    2952         251 :     return eErr;
    2953             : }
    2954             : 
    2955             : /************************************************************************/
    2956             : /*                          IBuildOverviews()                           */
    2957             : /************************************************************************/
    2958             : 
    2959         385 : CPLErr GTiffDataset::IBuildOverviews(const char *pszResampling, int nOverviews,
    2960             :                                      const int *panOverviewList, int nBandsIn,
    2961             :                                      const int *panBandList,
    2962             :                                      GDALProgressFunc pfnProgress,
    2963             :                                      void *pProgressData,
    2964             :                                      CSLConstList papszOptions)
    2965             : 
    2966             : {
    2967         385 :     ScanDirectories();
    2968             : 
    2969             :     // Make implicit JPEG overviews invisible, but do not destroy
    2970             :     // them in case they are already used (not sure that the client
    2971             :     // has the right to do that.  Behavior maybe undefined in GDAL API.
    2972         385 :     m_nJPEGOverviewCount = 0;
    2973             : 
    2974             :     /* -------------------------------------------------------------------- */
    2975             :     /*      If RRD or external OVR overviews requested, then invoke         */
    2976             :     /*      generic handling.                                               */
    2977             :     /* -------------------------------------------------------------------- */
    2978         385 :     bool bUseGenericHandling = false;
    2979             : 
    2980         385 :     if (CPLTestBool(CSLFetchNameValueDef(
    2981         768 :             papszOptions, "USE_RRD", CPLGetConfigOption("USE_RRD", "NO"))) ||
    2982         383 :         CPLTestBool(
    2983             :             CSLFetchNameValueDef(papszOptions, "TIFF_USE_OVR",
    2984             :                                  CPLGetConfigOption("TIFF_USE_OVR", "NO"))))
    2985             :     {
    2986           2 :         bUseGenericHandling = true;
    2987             :     }
    2988             : 
    2989             :     /* -------------------------------------------------------------------- */
    2990             :     /*      If we don't have read access, then create the overviews         */
    2991             :     /*      externally.                                                     */
    2992             :     /* -------------------------------------------------------------------- */
    2993         385 :     if (GetAccess() != GA_Update)
    2994             :     {
    2995         140 :         CPLDebug("GTiff", "File open for read-only accessing, "
    2996             :                           "creating overviews externally.");
    2997             : 
    2998         140 :         bUseGenericHandling = true;
    2999             :     }
    3000             : 
    3001         385 :     if (bUseGenericHandling)
    3002             :     {
    3003         142 :         if (m_nOverviewCount != 0)
    3004             :         {
    3005           0 :             ReportError(CE_Failure, CPLE_NotSupported,
    3006             :                         "Cannot add external overviews when there are already "
    3007             :                         "internal overviews");
    3008           0 :             return CE_Failure;
    3009             :         }
    3010             : 
    3011         142 :         CPLStringList aosOptions(papszOptions);
    3012         142 :         if (!m_bWriteEmptyTiles)
    3013             :         {
    3014           1 :             aosOptions.SetNameValue("SPARSE_OK", "YES");
    3015             :         }
    3016             : 
    3017         142 :         CPLErr eErr = GDALDataset::IBuildOverviews(
    3018             :             pszResampling, nOverviews, panOverviewList, nBandsIn, panBandList,
    3019         142 :             pfnProgress, pProgressData, aosOptions);
    3020         142 :         if (eErr == CE_None && m_poMaskDS)
    3021             :         {
    3022           1 :             ReportError(
    3023             :                 CE_Warning, CPLE_NotSupported,
    3024             :                 "Building external overviews whereas there is an internal "
    3025             :                 "mask is not fully supported. "
    3026             :                 "The overviews of the non-mask bands will be created, "
    3027             :                 "but not the overviews of the mask band.");
    3028             :         }
    3029         142 :         return eErr;
    3030             :     }
    3031             : 
    3032             :     /* -------------------------------------------------------------------- */
    3033             :     /*      Our TIFF overview support currently only works safely if all    */
    3034             :     /*      bands are handled at the same time.                             */
    3035             :     /* -------------------------------------------------------------------- */
    3036         243 :     if (nBandsIn != GetRasterCount())
    3037             :     {
    3038           0 :         ReportError(CE_Failure, CPLE_NotSupported,
    3039             :                     "Generation of overviews in TIFF currently only "
    3040             :                     "supported when operating on all bands.  "
    3041             :                     "Operation failed.");
    3042           0 :         return CE_Failure;
    3043             :     }
    3044             : 
    3045             :     /* -------------------------------------------------------------------- */
    3046             :     /*      If zero overviews were requested, we need to clear all          */
    3047             :     /*      existing overviews.                                             */
    3048             :     /* -------------------------------------------------------------------- */
    3049         243 :     if (nOverviews == 0)
    3050             :     {
    3051           6 :         if (m_nOverviewCount == 0)
    3052           3 :             return GDALDataset::IBuildOverviews(
    3053             :                 pszResampling, nOverviews, panOverviewList, nBandsIn,
    3054           3 :                 panBandList, pfnProgress, pProgressData, papszOptions);
    3055             : 
    3056           3 :         return CleanOverviews();
    3057             :     }
    3058             : 
    3059         237 :     CPLErr eErr = CE_None;
    3060             : 
    3061             :     /* -------------------------------------------------------------------- */
    3062             :     /*      Initialize progress counter.                                    */
    3063             :     /* -------------------------------------------------------------------- */
    3064         237 :     if (!pfnProgress(0.0, nullptr, pProgressData))
    3065             :     {
    3066           0 :         ReportError(CE_Failure, CPLE_UserInterrupt, "User terminated");
    3067           0 :         return CE_Failure;
    3068             :     }
    3069             : 
    3070         237 :     FlushDirectory();
    3071             : 
    3072             :     /* -------------------------------------------------------------------- */
    3073             :     /*      If we are averaging bit data to grayscale we need to create     */
    3074             :     /*      8bit overviews.                                                 */
    3075             :     /* -------------------------------------------------------------------- */
    3076         237 :     int nOvBitsPerSample = m_nBitsPerSample;
    3077             : 
    3078         237 :     if (STARTS_WITH_CI(pszResampling, "AVERAGE_BIT2"))
    3079           2 :         nOvBitsPerSample = 8;
    3080             : 
    3081             :     /* -------------------------------------------------------------------- */
    3082             :     /*      Do we need some metadata for the overviews?                     */
    3083             :     /* -------------------------------------------------------------------- */
    3084         474 :     CPLString osMetadata;
    3085             : 
    3086         237 :     const bool bIsForMaskBand = nBands == 1 && GetRasterBand(1)->IsMaskBand();
    3087         237 :     GTIFFBuildOverviewMetadata(pszResampling, this, bIsForMaskBand, osMetadata);
    3088             : 
    3089             :     int nCompression;
    3090             :     uint16_t nPlanarConfig;
    3091             :     uint16_t nPredictor;
    3092             :     uint16_t nPhotometric;
    3093             :     int nOvrJpegQuality;
    3094         474 :     std::string osNoData;
    3095         237 :     uint16_t *panExtraSampleValues = nullptr;
    3096         237 :     uint16_t nExtraSamples = 0;
    3097         237 :     if (!GetOverviewParameters(nCompression, nPlanarConfig, nPredictor,
    3098             :                                nPhotometric, nOvrJpegQuality, osNoData,
    3099             :                                panExtraSampleValues, nExtraSamples,
    3100             :                                papszOptions))
    3101             :     {
    3102           0 :         return CE_Failure;
    3103             :     }
    3104             : 
    3105             :     /* -------------------------------------------------------------------- */
    3106             :     /*      Do we have a palette?  If so, create a TIFF compatible version. */
    3107             :     /* -------------------------------------------------------------------- */
    3108         474 :     std::vector<unsigned short> anTRed;
    3109         474 :     std::vector<unsigned short> anTGreen;
    3110         474 :     std::vector<unsigned short> anTBlue;
    3111         237 :     unsigned short *panRed = nullptr;
    3112         237 :     unsigned short *panGreen = nullptr;
    3113         237 :     unsigned short *panBlue = nullptr;
    3114             : 
    3115         237 :     if (nPhotometric == PHOTOMETRIC_PALETTE && m_poColorTable != nullptr)
    3116             :     {
    3117          12 :         if (m_nColorTableMultiplier == 0)
    3118           0 :             m_nColorTableMultiplier = DEFAULT_COLOR_TABLE_MULTIPLIER_257;
    3119             : 
    3120          12 :         CreateTIFFColorTable(m_poColorTable.get(), nOvBitsPerSample,
    3121             :                              m_nColorTableMultiplier, anTRed, anTGreen, anTBlue,
    3122             :                              panRed, panGreen, panBlue);
    3123             :     }
    3124             : 
    3125             :     /* -------------------------------------------------------------------- */
    3126             :     /*      Establish which of the overview levels we already have, and     */
    3127             :     /*      which are new.  We assume that band 1 of the file is            */
    3128             :     /*      representative.                                                 */
    3129             :     /* -------------------------------------------------------------------- */
    3130         237 :     int nOvrBlockXSize = 0;
    3131         237 :     int nOvrBlockYSize = 0;
    3132         237 :     GTIFFGetOverviewBlockSize(GDALRasterBand::ToHandle(GetRasterBand(1)),
    3133             :                               &nOvrBlockXSize, &nOvrBlockYSize);
    3134         474 :     std::vector<bool> abRequireNewOverview(nOverviews, true);
    3135         650 :     for (int i = 0; i < nOverviews && eErr == CE_None; ++i)
    3136             :     {
    3137         735 :         for (int j = 0; j < m_nOverviewCount && eErr == CE_None; ++j)
    3138             :         {
    3139         377 :             GTiffDataset *poODS = m_papoOverviewDS[j];
    3140             : 
    3141             :             const int nOvFactor =
    3142         377 :                 GDALComputeOvFactor(poODS->GetRasterXSize(), GetRasterXSize(),
    3143             :                                     poODS->GetRasterYSize(), GetRasterYSize());
    3144             : 
    3145             :             // If we already have a 1x1 overview and this new one would result
    3146             :             // in it too, then don't create it.
    3147         416 :             if (poODS->GetRasterXSize() == 1 && poODS->GetRasterYSize() == 1 &&
    3148          21 :                 (GetRasterXSize() + panOverviewList[i] - 1) /
    3149          21 :                         panOverviewList[i] ==
    3150         416 :                     1 &&
    3151          21 :                 (GetRasterYSize() + panOverviewList[i] - 1) /
    3152          21 :                         panOverviewList[i] ==
    3153             :                     1)
    3154             :             {
    3155          21 :                 abRequireNewOverview[i] = false;
    3156          21 :                 break;
    3157             :             }
    3158             : 
    3159         680 :             if (nOvFactor == panOverviewList[i] ||
    3160         324 :                 nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
    3161             :                                                 GetRasterXSize(),
    3162             :                                                 GetRasterYSize()))
    3163             :             {
    3164          34 :                 abRequireNewOverview[i] = false;
    3165          34 :                 break;
    3166             :             }
    3167             :         }
    3168             : 
    3169         413 :         if (abRequireNewOverview[i])
    3170             :         {
    3171         358 :             if (m_bLayoutIFDSBeforeData && !m_bKnownIncompatibleEdition &&
    3172           0 :                 !m_bWriteKnownIncompatibleEdition)
    3173             :             {
    3174           0 :                 ReportError(CE_Warning, CPLE_AppDefined,
    3175             :                             "Adding new overviews invalidates the "
    3176             :                             "LAYOUT=IFDS_BEFORE_DATA property");
    3177           0 :                 m_bKnownIncompatibleEdition = true;
    3178           0 :                 m_bWriteKnownIncompatibleEdition = true;
    3179             :             }
    3180             : 
    3181         358 :             const int nOXSize = (GetRasterXSize() + panOverviewList[i] - 1) /
    3182         358 :                                 panOverviewList[i];
    3183         358 :             const int nOYSize = (GetRasterYSize() + panOverviewList[i] - 1) /
    3184         358 :                                 panOverviewList[i];
    3185             : 
    3186         716 :             const toff_t nOverviewOffset = GTIFFWriteDirectory(
    3187             :                 m_hTIFF, FILETYPE_REDUCEDIMAGE, nOXSize, nOYSize,
    3188         358 :                 nOvBitsPerSample, nPlanarConfig, m_nSamplesPerPixel,
    3189             :                 nOvrBlockXSize, nOvrBlockYSize, TRUE, nCompression,
    3190         358 :                 nPhotometric, m_nSampleFormat, nPredictor, panRed, panGreen,
    3191             :                 panBlue, nExtraSamples, panExtraSampleValues, osMetadata,
    3192         358 :                 nOvrJpegQuality >= 0 ? CPLSPrintf("%d", nOvrJpegQuality)
    3193             :                                      : nullptr,
    3194         358 :                 CPLSPrintf("%d", m_nJpegTablesMode),
    3195          24 :                 osNoData.empty() ? nullptr : osNoData.c_str(),
    3196         358 :                 m_anLercAddCompressionAndVersion, false);
    3197             : 
    3198         358 :             if (nOverviewOffset == 0)
    3199           0 :                 eErr = CE_Failure;
    3200             :             else
    3201         358 :                 eErr = RegisterNewOverviewDataset(
    3202             :                     nOverviewOffset, nOvrJpegQuality, papszOptions);
    3203             :         }
    3204             :     }
    3205             : 
    3206         237 :     CPLFree(panExtraSampleValues);
    3207         237 :     panExtraSampleValues = nullptr;
    3208             : 
    3209         237 :     ReloadDirectory();
    3210             : 
    3211             :     /* -------------------------------------------------------------------- */
    3212             :     /*      Create overviews for the mask.                                  */
    3213             :     /* -------------------------------------------------------------------- */
    3214         237 :     if (eErr != CE_None)
    3215           0 :         return eErr;
    3216             : 
    3217         237 :     eErr = CreateInternalMaskOverviews(nOvrBlockXSize, nOvrBlockYSize);
    3218             : 
    3219             :     /* -------------------------------------------------------------------- */
    3220             :     /*      Refresh overviews for the mask                                  */
    3221             :     /* -------------------------------------------------------------------- */
    3222             :     const bool bHasInternalMask =
    3223         237 :         m_poMaskDS != nullptr && m_poMaskDS->GetRasterCount() == 1;
    3224             :     const bool bHasExternalMask =
    3225         237 :         !bHasInternalMask && oOvManager.HaveMaskFile();
    3226         237 :     const bool bHasMask = bHasInternalMask || bHasExternalMask;
    3227             : 
    3228         237 :     if (bHasInternalMask)
    3229             :     {
    3230          19 :         int nMaskOverviews = 0;
    3231             : 
    3232             :         GDALRasterBand **papoOverviewBands = static_cast<GDALRasterBand **>(
    3233          19 :             CPLCalloc(sizeof(void *), m_nOverviewCount));
    3234          54 :         for (int i = 0; i < m_nOverviewCount; ++i)
    3235             :         {
    3236          35 :             if (m_papoOverviewDS[i]->m_poMaskDS != nullptr)
    3237             :             {
    3238          35 :                 papoOverviewBands[nMaskOverviews++] =
    3239          35 :                     m_papoOverviewDS[i]->m_poMaskDS->GetRasterBand(1);
    3240             :             }
    3241             :         }
    3242             : 
    3243          38 :         void *pScaledProgressData = GDALCreateScaledProgress(
    3244          19 :             0, 1.0 / (nBands + 1), pfnProgress, pProgressData);
    3245          38 :         eErr = GDALRegenerateOverviewsEx(
    3246          19 :             m_poMaskDS->GetRasterBand(1), nMaskOverviews,
    3247             :             reinterpret_cast<GDALRasterBandH *>(papoOverviewBands),
    3248             :             pszResampling, GDALScaledProgress, pScaledProgressData,
    3249             :             papszOptions);
    3250          19 :         GDALDestroyScaledProgress(pScaledProgressData);
    3251          19 :         CPLFree(papoOverviewBands);
    3252             :     }
    3253         218 :     else if (bHasExternalMask)
    3254             :     {
    3255           4 :         void *pScaledProgressData = GDALCreateScaledProgress(
    3256           2 :             0, 1.0 / (nBands + 1), pfnProgress, pProgressData);
    3257           2 :         eErr = oOvManager.BuildOverviewsMask(
    3258             :             pszResampling, nOverviews, panOverviewList, GDALScaledProgress,
    3259             :             pScaledProgressData, papszOptions);
    3260           2 :         GDALDestroyScaledProgress(pScaledProgressData);
    3261             :     }
    3262             : 
    3263             :     // If we have an alpha band, we want it to be generated before downsampling
    3264             :     // other bands
    3265         237 :     bool bHasAlphaBand = false;
    3266         667 :     for (int iBand = 0; iBand < nBands; iBand++)
    3267             :     {
    3268         430 :         if (papoBands[iBand]->GetColorInterpretation() == GCI_AlphaBand)
    3269          18 :             bHasAlphaBand = true;
    3270             :     }
    3271             : 
    3272             :     /* -------------------------------------------------------------------- */
    3273             :     /*      Refresh old overviews that were listed.                         */
    3274             :     /* -------------------------------------------------------------------- */
    3275         237 :     const auto poColorTable = GetRasterBand(panBandList[0])->GetColorTable();
    3276          15 :     if ((m_nPlanarConfig == PLANARCONFIG_CONTIG || bHasAlphaBand) &&
    3277         224 :         GDALDataTypeIsComplex(
    3278         224 :             GetRasterBand(panBandList[0])->GetRasterDataType()) == FALSE &&
    3279          12 :         (poColorTable == nullptr || STARTS_WITH_CI(pszResampling, "NEAR") ||
    3280         475 :          poColorTable->IsIdentity()) &&
    3281         216 :         (STARTS_WITH_CI(pszResampling, "NEAR") ||
    3282         114 :          EQUAL(pszResampling, "AVERAGE") || EQUAL(pszResampling, "RMS") ||
    3283          48 :          EQUAL(pszResampling, "GAUSS") || EQUAL(pszResampling, "CUBIC") ||
    3284          29 :          EQUAL(pszResampling, "CUBICSPLINE") ||
    3285          28 :          EQUAL(pszResampling, "LANCZOS") || EQUAL(pszResampling, "BILINEAR") ||
    3286          24 :          EQUAL(pszResampling, "MODE")))
    3287             :     {
    3288             :         // In the case of pixel interleaved compressed overviews, we want to
    3289             :         // generate the overviews for all the bands block by block, and not
    3290             :         // band after band, in order to write the block once and not loose
    3291             :         // space in the TIFF file.  We also use that logic for uncompressed
    3292             :         // overviews, since GDALRegenerateOverviewsMultiBand() will be able to
    3293             :         // trigger cascading overview regeneration even in the presence
    3294             :         // of an alpha band.
    3295             : 
    3296         195 :         int nNewOverviews = 0;
    3297             : 
    3298             :         GDALRasterBand ***papapoOverviewBands = static_cast<GDALRasterBand ***>(
    3299         195 :             CPLCalloc(sizeof(void *), nBandsIn));
    3300             :         GDALRasterBand **papoBandList =
    3301         195 :             static_cast<GDALRasterBand **>(CPLCalloc(sizeof(void *), nBandsIn));
    3302         547 :         for (int iBand = 0; iBand < nBandsIn; ++iBand)
    3303             :         {
    3304         352 :             GDALRasterBand *poBand = GetRasterBand(panBandList[iBand]);
    3305             : 
    3306         352 :             papoBandList[iBand] = poBand;
    3307         704 :             papapoOverviewBands[iBand] = static_cast<GDALRasterBand **>(
    3308         352 :                 CPLCalloc(sizeof(void *), poBand->GetOverviewCount()));
    3309             : 
    3310         352 :             int iCurOverview = 0;
    3311             :             std::vector<bool> abAlreadyUsedOverviewBand(
    3312         352 :                 poBand->GetOverviewCount(), false);
    3313             : 
    3314        1006 :             for (int i = 0; i < nOverviews; ++i)
    3315             :             {
    3316        1147 :                 for (int j = 0; j < poBand->GetOverviewCount(); ++j)
    3317             :                 {
    3318        1133 :                     if (abAlreadyUsedOverviewBand[j])
    3319         493 :                         continue;
    3320             : 
    3321             :                     int nOvFactor;
    3322         640 :                     GDALRasterBand *poOverview = poBand->GetOverview(j);
    3323             : 
    3324         640 :                     nOvFactor = GDALComputeOvFactor(
    3325             :                         poOverview->GetXSize(), poBand->GetXSize(),
    3326             :                         poOverview->GetYSize(), poBand->GetYSize());
    3327             : 
    3328         640 :                     GDALCopyNoDataValue(poOverview, poBand);
    3329             : 
    3330         649 :                     if (nOvFactor == panOverviewList[i] ||
    3331           9 :                         nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
    3332             :                                                         poBand->GetXSize(),
    3333             :                                                         poBand->GetYSize()))
    3334             :                     {
    3335         640 :                         if (iBand == 0)
    3336             :                         {
    3337             :                             const auto osNewResampling =
    3338         654 :                                 GDALGetNormalizedOvrResampling(pszResampling);
    3339             :                             const char *pszExistingResampling =
    3340         327 :                                 poOverview->GetMetadataItem("RESAMPLING");
    3341         654 :                             if (pszExistingResampling &&
    3342         327 :                                 pszExistingResampling != osNewResampling)
    3343             :                             {
    3344           2 :                                 poOverview->SetMetadataItem(
    3345           2 :                                     "RESAMPLING", osNewResampling.c_str());
    3346             :                             }
    3347             :                         }
    3348             : 
    3349         640 :                         abAlreadyUsedOverviewBand[j] = true;
    3350         640 :                         CPLAssert(iCurOverview < poBand->GetOverviewCount());
    3351         640 :                         papapoOverviewBands[iBand][iCurOverview] = poOverview;
    3352         640 :                         ++iCurOverview;
    3353         640 :                         break;
    3354             :                     }
    3355             :                 }
    3356             :             }
    3357             : 
    3358         352 :             if (nNewOverviews == 0)
    3359             :             {
    3360         195 :                 nNewOverviews = iCurOverview;
    3361             :             }
    3362         157 :             else if (nNewOverviews != iCurOverview)
    3363             :             {
    3364           0 :                 CPLAssert(false);
    3365             :                 return CE_Failure;
    3366             :             }
    3367             :         }
    3368             : 
    3369             :         void *pScaledProgressData =
    3370         195 :             bHasMask ? GDALCreateScaledProgress(1.0 / (nBands + 1), 1.0,
    3371             :                                                 pfnProgress, pProgressData)
    3372         174 :                      : GDALCreateScaledProgress(0.0, 1.0, pfnProgress,
    3373         195 :                                                 pProgressData);
    3374         195 :         GDALRegenerateOverviewsMultiBand(nBandsIn, papoBandList, nNewOverviews,
    3375             :                                          papapoOverviewBands, pszResampling,
    3376             :                                          GDALScaledProgress,
    3377             :                                          pScaledProgressData, papszOptions);
    3378         195 :         GDALDestroyScaledProgress(pScaledProgressData);
    3379             : 
    3380         547 :         for (int iBand = 0; iBand < nBandsIn; ++iBand)
    3381             :         {
    3382         352 :             CPLFree(papapoOverviewBands[iBand]);
    3383             :         }
    3384         195 :         CPLFree(papapoOverviewBands);
    3385         195 :         CPLFree(papoBandList);
    3386             :     }
    3387             :     else
    3388             :     {
    3389             :         GDALRasterBand **papoOverviewBands = static_cast<GDALRasterBand **>(
    3390          42 :             CPLCalloc(sizeof(void *), nOverviews));
    3391             : 
    3392          42 :         const int iBandOffset = bHasMask ? 1 : 0;
    3393             : 
    3394         120 :         for (int iBand = 0; iBand < nBandsIn && eErr == CE_None; ++iBand)
    3395             :         {
    3396          78 :             GDALRasterBand *poBand = GetRasterBand(panBandList[iBand]);
    3397          78 :             if (poBand == nullptr)
    3398             :             {
    3399           0 :                 eErr = CE_Failure;
    3400           0 :                 break;
    3401             :             }
    3402             : 
    3403             :             std::vector<bool> abAlreadyUsedOverviewBand(
    3404         156 :                 poBand->GetOverviewCount(), false);
    3405             : 
    3406          78 :             int nNewOverviews = 0;
    3407         216 :             for (int i = 0; i < nOverviews; ++i)
    3408             :             {
    3409         336 :                 for (int j = 0; j < poBand->GetOverviewCount(); ++j)
    3410             :                 {
    3411         315 :                     if (abAlreadyUsedOverviewBand[j])
    3412         176 :                         continue;
    3413             : 
    3414         139 :                     GDALRasterBand *poOverview = poBand->GetOverview(j);
    3415             : 
    3416         139 :                     GDALCopyNoDataValue(poOverview, poBand);
    3417             : 
    3418         139 :                     const int nOvFactor = GDALComputeOvFactor(
    3419             :                         poOverview->GetXSize(), poBand->GetXSize(),
    3420             :                         poOverview->GetYSize(), poBand->GetYSize());
    3421             : 
    3422         171 :                     if (nOvFactor == panOverviewList[i] ||
    3423          32 :                         nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
    3424             :                                                         poBand->GetXSize(),
    3425             :                                                         poBand->GetYSize()))
    3426             :                     {
    3427         117 :                         if (iBand == 0)
    3428             :                         {
    3429             :                             const auto osNewResampling =
    3430         130 :                                 GDALGetNormalizedOvrResampling(pszResampling);
    3431             :                             const char *pszExistingResampling =
    3432          65 :                                 poOverview->GetMetadataItem("RESAMPLING");
    3433          98 :                             if (pszExistingResampling &&
    3434          33 :                                 pszExistingResampling != osNewResampling)
    3435             :                             {
    3436           1 :                                 poOverview->SetMetadataItem(
    3437           1 :                                     "RESAMPLING", osNewResampling.c_str());
    3438             :                             }
    3439             :                         }
    3440             : 
    3441         117 :                         abAlreadyUsedOverviewBand[j] = true;
    3442         117 :                         CPLAssert(nNewOverviews < poBand->GetOverviewCount());
    3443         117 :                         papoOverviewBands[nNewOverviews++] = poOverview;
    3444         117 :                         break;
    3445             :                     }
    3446             :                 }
    3447             :             }
    3448             : 
    3449         156 :             void *pScaledProgressData = GDALCreateScaledProgress(
    3450          78 :                 (iBand + iBandOffset) /
    3451          78 :                     static_cast<double>(nBandsIn + iBandOffset),
    3452          78 :                 (iBand + iBandOffset + 1) /
    3453          78 :                     static_cast<double>(nBandsIn + iBandOffset),
    3454             :                 pfnProgress, pProgressData);
    3455             : 
    3456          78 :             eErr = GDALRegenerateOverviewsEx(
    3457             :                 poBand, nNewOverviews,
    3458             :                 reinterpret_cast<GDALRasterBandH *>(papoOverviewBands),
    3459             :                 pszResampling, GDALScaledProgress, pScaledProgressData,
    3460             :                 papszOptions);
    3461             : 
    3462          78 :             GDALDestroyScaledProgress(pScaledProgressData);
    3463             :         }
    3464             : 
    3465             :         /* --------------------------------------------------------------------
    3466             :          */
    3467             :         /*      Cleanup */
    3468             :         /* --------------------------------------------------------------------
    3469             :          */
    3470          42 :         CPLFree(papoOverviewBands);
    3471             :     }
    3472             : 
    3473         237 :     pfnProgress(1.0, nullptr, pProgressData);
    3474             : 
    3475         237 :     return eErr;
    3476             : }
    3477             : 
    3478             : /************************************************************************/
    3479             : /*                      GTiffWriteDummyGeokeyDirectory()                */
    3480             : /************************************************************************/
    3481             : 
    3482        1355 : static void GTiffWriteDummyGeokeyDirectory(TIFF *hTIFF)
    3483             : {
    3484             :     // If we have existing geokeys, try to wipe them
    3485             :     // by writing a dummy geokey directory. (#2546)
    3486        1355 :     uint16_t *panVI = nullptr;
    3487        1355 :     uint16_t nKeyCount = 0;
    3488             : 
    3489        1355 :     if (TIFFGetField(hTIFF, TIFFTAG_GEOKEYDIRECTORY, &nKeyCount, &panVI))
    3490             :     {
    3491          20 :         GUInt16 anGKVersionInfo[4] = {1, 1, 0, 0};
    3492          20 :         double adfDummyDoubleParams[1] = {0.0};
    3493          20 :         TIFFSetField(hTIFF, TIFFTAG_GEOKEYDIRECTORY, 4, anGKVersionInfo);
    3494          20 :         TIFFSetField(hTIFF, TIFFTAG_GEODOUBLEPARAMS, 1, adfDummyDoubleParams);
    3495          20 :         TIFFSetField(hTIFF, TIFFTAG_GEOASCIIPARAMS, "");
    3496             :     }
    3497        1355 : }
    3498             : 
    3499             : /************************************************************************/
    3500             : /*                    IsSRSCompatibleOfGeoTIFF()                        */
    3501             : /************************************************************************/
    3502             : 
    3503        2803 : static bool IsSRSCompatibleOfGeoTIFF(const OGRSpatialReference *poSRS,
    3504             :                                      GTIFFKeysFlavorEnum eGeoTIFFKeysFlavor)
    3505             : {
    3506        2803 :     char *pszWKT = nullptr;
    3507        2803 :     if ((poSRS->IsGeographic() || poSRS->IsProjected()) && !poSRS->IsCompound())
    3508             :     {
    3509        2785 :         const char *pszAuthName = poSRS->GetAuthorityName(nullptr);
    3510        2785 :         const char *pszAuthCode = poSRS->GetAuthorityCode(nullptr);
    3511        2785 :         if (pszAuthName && pszAuthCode && EQUAL(pszAuthName, "EPSG"))
    3512        2265 :             return true;
    3513             :     }
    3514             :     OGRErr eErr;
    3515             :     {
    3516        1076 :         CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
    3517        1076 :         if (poSRS->IsDerivedGeographic() ||
    3518         538 :             (poSRS->IsProjected() && !poSRS->IsCompound() &&
    3519          70 :              poSRS->GetAxesCount() == 3))
    3520             :         {
    3521           0 :             eErr = OGRERR_FAILURE;
    3522             :         }
    3523             :         else
    3524             :         {
    3525             :             // Geographic3D CRS can't be exported to WKT1, but are
    3526             :             // valid GeoTIFF 1.1
    3527         538 :             const char *const apszOptions[] = {
    3528         538 :                 poSRS->IsGeographic() ? nullptr : "FORMAT=WKT1", nullptr};
    3529         538 :             eErr = poSRS->exportToWkt(&pszWKT, apszOptions);
    3530         538 :             if (eErr == OGRERR_FAILURE && poSRS->IsProjected() &&
    3531             :                 eGeoTIFFKeysFlavor == GEOTIFF_KEYS_ESRI_PE)
    3532             :             {
    3533           0 :                 CPLFree(pszWKT);
    3534           0 :                 const char *const apszOptionsESRIWKT[] = {"FORMAT=WKT1_ESRI",
    3535             :                                                           nullptr};
    3536           0 :                 eErr = poSRS->exportToWkt(&pszWKT, apszOptionsESRIWKT);
    3537             :             }
    3538             :         }
    3539             :     }
    3540         538 :     const bool bCompatibleOfGeoTIFF =
    3541        1075 :         (eErr == OGRERR_NONE && pszWKT != nullptr &&
    3542         537 :          strstr(pszWKT, "custom_proj4") == nullptr);
    3543         538 :     CPLFree(pszWKT);
    3544         538 :     return bCompatibleOfGeoTIFF;
    3545             : }
    3546             : 
    3547             : /************************************************************************/
    3548             : /*                          WriteGeoTIFFInfo()                          */
    3549             : /************************************************************************/
    3550             : 
    3551        3267 : void GTiffDataset::WriteGeoTIFFInfo()
    3552             : 
    3553             : {
    3554        3267 :     bool bPixelIsPoint = false;
    3555        3267 :     bool bPointGeoIgnore = false;
    3556             : 
    3557             :     const char *pszAreaOrPoint =
    3558        3267 :         GTiffDataset::GetMetadataItem(GDALMD_AREA_OR_POINT);
    3559        3267 :     if (pszAreaOrPoint && EQUAL(pszAreaOrPoint, GDALMD_AOP_POINT))
    3560             :     {
    3561          17 :         bPixelIsPoint = true;
    3562             :         bPointGeoIgnore =
    3563          17 :             CPLTestBool(CPLGetConfigOption("GTIFF_POINT_GEO_IGNORE", "FALSE"));
    3564             :     }
    3565             : 
    3566        3267 :     if (m_bForceUnsetGTOrGCPs)
    3567             :     {
    3568          11 :         m_bNeedsRewrite = true;
    3569          11 :         m_bForceUnsetGTOrGCPs = false;
    3570             : 
    3571          11 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOPIXELSCALE);
    3572          11 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOTIEPOINTS);
    3573          11 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOTRANSMATRIX);
    3574             :     }
    3575             : 
    3576        3267 :     if (m_bForceUnsetProjection)
    3577             :     {
    3578           8 :         m_bNeedsRewrite = true;
    3579           8 :         m_bForceUnsetProjection = false;
    3580             : 
    3581           8 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOKEYDIRECTORY);
    3582           8 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEODOUBLEPARAMS);
    3583           8 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOASCIIPARAMS);
    3584             :     }
    3585             : 
    3586             :     /* -------------------------------------------------------------------- */
    3587             :     /*      Write geotransform if valid.                                    */
    3588             :     /* -------------------------------------------------------------------- */
    3589        3267 :     if (m_bGeoTransformValid)
    3590             :     {
    3591        1553 :         m_bNeedsRewrite = true;
    3592             : 
    3593             :         /* --------------------------------------------------------------------
    3594             :          */
    3595             :         /*      Clear old tags to ensure we don't end up with conflicting */
    3596             :         /*      information. (#2625) */
    3597             :         /* --------------------------------------------------------------------
    3598             :          */
    3599        1553 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOPIXELSCALE);
    3600        1553 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOTIEPOINTS);
    3601        1553 :         TIFFUnsetField(m_hTIFF, TIFFTAG_GEOTRANSMATRIX);
    3602             : 
    3603             :         /* --------------------------------------------------------------------
    3604             :          */
    3605             :         /*      Write the transform.  If we have a normal north-up image we */
    3606             :         /*      use the tiepoint plus pixelscale otherwise we use a matrix. */
    3607             :         /* --------------------------------------------------------------------
    3608             :          */
    3609        1553 :         if (m_adfGeoTransform[2] == 0.0 && m_adfGeoTransform[4] == 0.0 &&
    3610        1535 :             m_adfGeoTransform[5] < 0.0)
    3611             :         {
    3612        1497 :             double dfOffset = 0.0;
    3613        1497 :             if (m_eProfile != GTiffProfile::BASELINE)
    3614             :             {
    3615             :                 // In the case the SRS has a vertical component and we have
    3616             :                 // a single band, encode its scale/offset in the GeoTIFF tags
    3617        1491 :                 int bHasScale = FALSE;
    3618        1491 :                 double dfScale = GetRasterBand(1)->GetScale(&bHasScale);
    3619        1491 :                 int bHasOffset = FALSE;
    3620        1491 :                 dfOffset = GetRasterBand(1)->GetOffset(&bHasOffset);
    3621             :                 const bool bApplyScaleOffset =
    3622        1491 :                     m_oSRS.IsVertical() && GetRasterCount() == 1;
    3623        1491 :                 if (bApplyScaleOffset && !bHasScale)
    3624           0 :                     dfScale = 1.0;
    3625        1491 :                 if (!bApplyScaleOffset || !bHasOffset)
    3626        1488 :                     dfOffset = 0.0;
    3627             :                 const double adfPixelScale[3] = {
    3628        1491 :                     m_adfGeoTransform[1], fabs(m_adfGeoTransform[5]),
    3629        1491 :                     bApplyScaleOffset ? dfScale : 0.0};
    3630        1491 :                 TIFFSetField(m_hTIFF, TIFFTAG_GEOPIXELSCALE, 3, adfPixelScale);
    3631             :             }
    3632             : 
    3633        1497 :             double adfTiePoints[6] = {
    3634        1497 :                 0.0,     0.0, 0.0, m_adfGeoTransform[0], m_adfGeoTransform[3],
    3635        1497 :                 dfOffset};
    3636             : 
    3637        1497 :             if (bPixelIsPoint && !bPointGeoIgnore)
    3638             :             {
    3639          13 :                 adfTiePoints[3] +=
    3640          13 :                     m_adfGeoTransform[1] * 0.5 + m_adfGeoTransform[2] * 0.5;
    3641          13 :                 adfTiePoints[4] +=
    3642          13 :                     m_adfGeoTransform[4] * 0.5 + m_adfGeoTransform[5] * 0.5;
    3643             :             }
    3644             : 
    3645        1497 :             if (m_eProfile != GTiffProfile::BASELINE)
    3646        1497 :                 TIFFSetField(m_hTIFF, TIFFTAG_GEOTIEPOINTS, 6, adfTiePoints);
    3647             :         }
    3648             :         else
    3649             :         {
    3650          56 :             double adfMatrix[16] = {};
    3651             : 
    3652          56 :             adfMatrix[0] = m_adfGeoTransform[1];
    3653          56 :             adfMatrix[1] = m_adfGeoTransform[2];
    3654          56 :             adfMatrix[3] = m_adfGeoTransform[0];
    3655          56 :             adfMatrix[4] = m_adfGeoTransform[4];
    3656          56 :             adfMatrix[5] = m_adfGeoTransform[5];
    3657          56 :             adfMatrix[7] = m_adfGeoTransform[3];
    3658          56 :             adfMatrix[15] = 1.0;
    3659             : 
    3660          56 :             if (bPixelIsPoint && !bPointGeoIgnore)
    3661             :             {
    3662           0 :                 adfMatrix[3] +=
    3663           0 :                     m_adfGeoTransform[1] * 0.5 + m_adfGeoTransform[2] * 0.5;
    3664           0 :                 adfMatrix[7] +=
    3665           0 :                     m_adfGeoTransform[4] * 0.5 + m_adfGeoTransform[5] * 0.5;
    3666             :             }
    3667             : 
    3668          56 :             if (m_eProfile != GTiffProfile::BASELINE)
    3669          56 :                 TIFFSetField(m_hTIFF, TIFFTAG_GEOTRANSMATRIX, 16, adfMatrix);
    3670             :         }
    3671             : 
    3672             :         // Do we need a world file?
    3673        1553 :         if (CPLFetchBool(m_papszCreationOptions, "TFW", false))
    3674           7 :             GDALWriteWorldFile(m_pszFilename, "tfw", m_adfGeoTransform);
    3675        1546 :         else if (CPLFetchBool(m_papszCreationOptions, "WORLDFILE", false))
    3676           2 :             GDALWriteWorldFile(m_pszFilename, "wld", m_adfGeoTransform);
    3677             :     }
    3678        1727 :     else if (GetGCPCount() > 0 && GetGCPCount() <= knMAX_GCP_COUNT &&
    3679          13 :              m_eProfile != GTiffProfile::BASELINE)
    3680             :     {
    3681          13 :         m_bNeedsRewrite = true;
    3682             : 
    3683             :         double *padfTiePoints = static_cast<double *>(
    3684          13 :             CPLMalloc(6 * sizeof(double) * GetGCPCount()));
    3685             : 
    3686          68 :         for (size_t iGCP = 0; iGCP < m_aoGCPs.size(); ++iGCP)
    3687             :         {
    3688             : 
    3689          55 :             padfTiePoints[iGCP * 6 + 0] = m_aoGCPs[iGCP].Pixel();
    3690          55 :             padfTiePoints[iGCP * 6 + 1] = m_aoGCPs[iGCP].Line();
    3691          55 :             padfTiePoints[iGCP * 6 + 2] = 0;
    3692          55 :             padfTiePoints[iGCP * 6 + 3] = m_aoGCPs[iGCP].X();
    3693          55 :             padfTiePoints[iGCP * 6 + 4] = m_aoGCPs[iGCP].Y();
    3694          55 :             padfTiePoints[iGCP * 6 + 5] = m_aoGCPs[iGCP].Z();
    3695             : 
    3696          55 :             if (bPixelIsPoint && !bPointGeoIgnore)
    3697             :             {
    3698           0 :                 padfTiePoints[iGCP * 6 + 0] += 0.5;
    3699           0 :                 padfTiePoints[iGCP * 6 + 1] += 0.5;
    3700             :             }
    3701             :         }
    3702             : 
    3703          13 :         TIFFSetField(m_hTIFF, TIFFTAG_GEOTIEPOINTS, 6 * GetGCPCount(),
    3704             :                      padfTiePoints);
    3705          13 :         CPLFree(padfTiePoints);
    3706             :     }
    3707             : 
    3708             :     /* -------------------------------------------------------------------- */
    3709             :     /*      Write out projection definition.                                */
    3710             :     /* -------------------------------------------------------------------- */
    3711        3267 :     const bool bHasProjection = !m_oSRS.IsEmpty();
    3712        3267 :     if ((bHasProjection || bPixelIsPoint) &&
    3713        1359 :         m_eProfile != GTiffProfile::BASELINE)
    3714             :     {
    3715        1355 :         m_bNeedsRewrite = true;
    3716             : 
    3717             :         // If we have existing geokeys, try to wipe them
    3718             :         // by writing a dummy geokey directory. (#2546)
    3719        1355 :         GTiffWriteDummyGeokeyDirectory(m_hTIFF);
    3720             : 
    3721        1355 :         GTIF *psGTIF = GTiffDataset::GTIFNew(m_hTIFF);
    3722             : 
    3723             :         // Set according to coordinate system.
    3724        1355 :         if (bHasProjection)
    3725             :         {
    3726        1354 :             if (IsSRSCompatibleOfGeoTIFF(&m_oSRS, m_eGeoTIFFKeysFlavor))
    3727             :             {
    3728        1352 :                 GTIFSetFromOGISDefnEx(psGTIF,
    3729             :                                       OGRSpatialReference::ToHandle(&m_oSRS),
    3730             :                                       m_eGeoTIFFKeysFlavor, m_eGeoTIFFVersion);
    3731             :             }
    3732             :             else
    3733             :             {
    3734           2 :                 GDALPamDataset::SetSpatialRef(&m_oSRS);
    3735             :             }
    3736             :         }
    3737             : 
    3738        1355 :         if (bPixelIsPoint)
    3739             :         {
    3740          17 :             GTIFKeySet(psGTIF, GTRasterTypeGeoKey, TYPE_SHORT, 1,
    3741             :                        RasterPixelIsPoint);
    3742             :         }
    3743             : 
    3744        1355 :         GTIFWriteKeys(psGTIF);
    3745        1355 :         GTIFFree(psGTIF);
    3746             :     }
    3747        3267 : }
    3748             : 
    3749             : /************************************************************************/
    3750             : /*                         AppendMetadataItem()                         */
    3751             : /************************************************************************/
    3752             : 
    3753        3457 : static void AppendMetadataItem(CPLXMLNode **ppsRoot, CPLXMLNode **ppsTail,
    3754             :                                const char *pszKey, const char *pszValue,
    3755             :                                int nBand, const char *pszRole,
    3756             :                                const char *pszDomain)
    3757             : 
    3758             : {
    3759             :     /* -------------------------------------------------------------------- */
    3760             :     /*      Create the Item element, and subcomponents.                     */
    3761             :     /* -------------------------------------------------------------------- */
    3762        3457 :     CPLXMLNode *psItem = CPLCreateXMLNode(nullptr, CXT_Element, "Item");
    3763        3457 :     CPLCreateXMLNode(CPLCreateXMLNode(psItem, CXT_Attribute, "name"), CXT_Text,
    3764             :                      pszKey);
    3765             : 
    3766        3457 :     if (nBand > 0)
    3767             :     {
    3768         816 :         char szBandId[32] = {};
    3769         816 :         snprintf(szBandId, sizeof(szBandId), "%d", nBand - 1);
    3770         816 :         CPLCreateXMLNode(CPLCreateXMLNode(psItem, CXT_Attribute, "sample"),
    3771             :                          CXT_Text, szBandId);
    3772             :     }
    3773             : 
    3774        3457 :     if (pszRole != nullptr)
    3775         336 :         CPLCreateXMLNode(CPLCreateXMLNode(psItem, CXT_Attribute, "role"),
    3776             :                          CXT_Text, pszRole);
    3777             : 
    3778        3457 :     if (pszDomain != nullptr && strlen(pszDomain) > 0)
    3779         947 :         CPLCreateXMLNode(CPLCreateXMLNode(psItem, CXT_Attribute, "domain"),
    3780             :                          CXT_Text, pszDomain);
    3781             : 
    3782             :     // Note: this escaping should not normally be done, as the serialization
    3783             :     // of the tree to XML also does it, so we end up width double XML escaping,
    3784             :     // but keep it for backward compatibility.
    3785        3457 :     char *pszEscapedItemValue = CPLEscapeString(pszValue, -1, CPLES_XML);
    3786        3457 :     CPLCreateXMLNode(psItem, CXT_Text, pszEscapedItemValue);
    3787        3457 :     CPLFree(pszEscapedItemValue);
    3788             : 
    3789             :     /* -------------------------------------------------------------------- */
    3790             :     /*      Create root, if missing.                                        */
    3791             :     /* -------------------------------------------------------------------- */
    3792        3457 :     if (*ppsRoot == nullptr)
    3793         599 :         *ppsRoot = CPLCreateXMLNode(nullptr, CXT_Element, "GDALMetadata");
    3794             : 
    3795             :     /* -------------------------------------------------------------------- */
    3796             :     /*      Append item to tail.  We keep track of the tail to avoid        */
    3797             :     /*      O(nsquared) time as the list gets longer.                       */
    3798             :     /* -------------------------------------------------------------------- */
    3799        3457 :     if (*ppsTail == nullptr)
    3800         599 :         CPLAddXMLChild(*ppsRoot, psItem);
    3801             :     else
    3802        2858 :         CPLAddXMLSibling(*ppsTail, psItem);
    3803             : 
    3804        3457 :     *ppsTail = psItem;
    3805        3457 : }
    3806             : 
    3807             : /************************************************************************/
    3808             : /*                         WriteMDMetadata()                            */
    3809             : /************************************************************************/
    3810             : 
    3811      206550 : static void WriteMDMetadata(GDALMultiDomainMetadata *poMDMD, TIFF *hTIFF,
    3812             :                             CPLXMLNode **ppsRoot, CPLXMLNode **ppsTail,
    3813             :                             int nBand, GTiffProfile eProfile)
    3814             : 
    3815             : {
    3816             : 
    3817             :     /* ==================================================================== */
    3818             :     /*      Process each domain.                                            */
    3819             :     /* ==================================================================== */
    3820      206550 :     CSLConstList papszDomainList = poMDMD->GetDomainList();
    3821      212125 :     for (int iDomain = 0; papszDomainList && papszDomainList[iDomain];
    3822             :          ++iDomain)
    3823             :     {
    3824        5575 :         CSLConstList papszMD = poMDMD->GetMetadata(papszDomainList[iDomain]);
    3825        5575 :         bool bIsXML = false;
    3826             : 
    3827        5575 :         if (EQUAL(papszDomainList[iDomain], "IMAGE_STRUCTURE") ||
    3828        2213 :             EQUAL(papszDomainList[iDomain], "DERIVED_SUBDATASETS"))
    3829        3365 :             continue;  // Ignored.
    3830        2210 :         if (EQUAL(papszDomainList[iDomain], "COLOR_PROFILE"))
    3831           3 :             continue;  // Handled elsewhere.
    3832        2207 :         if (EQUAL(papszDomainList[iDomain], MD_DOMAIN_RPC))
    3833           7 :             continue;  // Handled elsewhere.
    3834        2201 :         if (EQUAL(papszDomainList[iDomain], "xml:ESRI") &&
    3835           1 :             CPLTestBool(CPLGetConfigOption("ESRI_XML_PAM", "NO")))
    3836           1 :             continue;  // Handled elsewhere.
    3837        2199 :         if (EQUAL(papszDomainList[iDomain], "xml:XMP"))
    3838           2 :             continue;  // Handled in SetMetadata.
    3839             : 
    3840        2197 :         if (STARTS_WITH_CI(papszDomainList[iDomain], "xml:"))
    3841           2 :             bIsXML = true;
    3842             : 
    3843             :         /* --------------------------------------------------------------------
    3844             :          */
    3845             :         /*      Process each item in this domain. */
    3846             :         /* --------------------------------------------------------------------
    3847             :          */
    3848        6792 :         for (int iItem = 0; papszMD && papszMD[iItem]; ++iItem)
    3849             :         {
    3850        4595 :             const char *pszItemValue = nullptr;
    3851        4595 :             char *pszItemName = nullptr;
    3852             : 
    3853        4595 :             if (bIsXML)
    3854             :             {
    3855           2 :                 pszItemName = CPLStrdup("doc");
    3856           2 :                 pszItemValue = papszMD[iItem];
    3857             :             }
    3858             :             else
    3859             :             {
    3860        4593 :                 pszItemValue = CPLParseNameValue(papszMD[iItem], &pszItemName);
    3861        4593 :                 if (pszItemName == nullptr)
    3862             :                 {
    3863          49 :                     CPLDebug("GTiff", "Invalid metadata item : %s",
    3864          49 :                              papszMD[iItem]);
    3865          49 :                     continue;
    3866             :                 }
    3867             :             }
    3868             : 
    3869             :             /* --------------------------------------------------------------------
    3870             :              */
    3871             :             /*      Convert into XML item or handle as a special TIFF tag. */
    3872             :             /* --------------------------------------------------------------------
    3873             :              */
    3874        4546 :             if (strlen(papszDomainList[iDomain]) == 0 && nBand == 0 &&
    3875        3502 :                 (STARTS_WITH_CI(pszItemName, "TIFFTAG_") ||
    3876        3441 :                  (EQUAL(pszItemName, "GEO_METADATA") &&
    3877        3440 :                   eProfile == GTiffProfile::GDALGEOTIFF) ||
    3878        3440 :                  (EQUAL(pszItemName, "TIFF_RSID") &&
    3879             :                   eProfile == GTiffProfile::GDALGEOTIFF)))
    3880             :             {
    3881          63 :                 if (EQUAL(pszItemName, "TIFFTAG_RESOLUTIONUNIT"))
    3882             :                 {
    3883             :                     // ResolutionUnit can't be 0, which is the default if
    3884             :                     // atoi() fails.  Set to 1=Unknown.
    3885           9 :                     int v = atoi(pszItemValue);
    3886           9 :                     if (!v)
    3887           1 :                         v = RESUNIT_NONE;
    3888           9 :                     TIFFSetField(hTIFF, TIFFTAG_RESOLUTIONUNIT, v);
    3889             :                 }
    3890             :                 else
    3891             :                 {
    3892          54 :                     bool bFoundTag = false;
    3893          54 :                     size_t iTag = 0;  // Used after for.
    3894          54 :                     const auto *pasTIFFTags = GTiffDataset::GetTIFFTags();
    3895         286 :                     for (; pasTIFFTags[iTag].pszTagName; ++iTag)
    3896             :                     {
    3897         286 :                         if (EQUAL(pszItemName, pasTIFFTags[iTag].pszTagName))
    3898             :                         {
    3899          54 :                             bFoundTag = true;
    3900          54 :                             break;
    3901             :                         }
    3902             :                     }
    3903             : 
    3904          54 :                     if (bFoundTag &&
    3905          54 :                         pasTIFFTags[iTag].eType == GTIFFTAGTYPE_STRING)
    3906          33 :                         TIFFSetField(hTIFF, pasTIFFTags[iTag].nTagVal,
    3907             :                                      pszItemValue);
    3908          21 :                     else if (bFoundTag &&
    3909          21 :                              pasTIFFTags[iTag].eType == GTIFFTAGTYPE_FLOAT)
    3910          16 :                         TIFFSetField(hTIFF, pasTIFFTags[iTag].nTagVal,
    3911             :                                      CPLAtof(pszItemValue));
    3912           5 :                     else if (bFoundTag &&
    3913           5 :                              pasTIFFTags[iTag].eType == GTIFFTAGTYPE_SHORT)
    3914           4 :                         TIFFSetField(hTIFF, pasTIFFTags[iTag].nTagVal,
    3915             :                                      atoi(pszItemValue));
    3916           1 :                     else if (bFoundTag && pasTIFFTags[iTag].eType ==
    3917             :                                               GTIFFTAGTYPE_BYTE_STRING)
    3918             :                     {
    3919           1 :                         uint32_t nLen =
    3920           1 :                             static_cast<uint32_t>(strlen(pszItemValue));
    3921           1 :                         if (nLen)
    3922             :                         {
    3923           1 :                             TIFFSetField(hTIFF, pasTIFFTags[iTag].nTagVal, nLen,
    3924             :                                          pszItemValue);
    3925           1 :                         }
    3926             :                     }
    3927             :                     else
    3928           0 :                         CPLError(CE_Warning, CPLE_NotSupported,
    3929             :                                  "%s metadata item is unhandled and "
    3930             :                                  "will not be written",
    3931             :                                  pszItemName);
    3932          63 :                 }
    3933             :             }
    3934        4483 :             else if (nBand == 0 && EQUAL(pszItemName, GDALMD_AREA_OR_POINT))
    3935             :             {
    3936             :                 /* Do nothing, handled elsewhere. */;
    3937             :             }
    3938             :             else
    3939             :             {
    3940        2742 :                 AppendMetadataItem(ppsRoot, ppsTail, pszItemName, pszItemValue,
    3941        2742 :                                    nBand, nullptr, papszDomainList[iDomain]);
    3942             :             }
    3943             : 
    3944        4546 :             CPLFree(pszItemName);
    3945             :         }
    3946             : 
    3947             :         /* --------------------------------------------------------------------
    3948             :          */
    3949             :         /*      Remove TIFFTAG_xxxxxx that are already set but no longer in */
    3950             :         /*      the metadata list (#5619) */
    3951             :         /* --------------------------------------------------------------------
    3952             :          */
    3953        2197 :         if (strlen(papszDomainList[iDomain]) == 0 && nBand == 0)
    3954             :         {
    3955        2004 :             const auto *pasTIFFTags = GTiffDataset::GetTIFFTags();
    3956       30060 :             for (size_t iTag = 0; pasTIFFTags[iTag].pszTagName; ++iTag)
    3957             :             {
    3958       28056 :                 uint32_t nCount = 0;
    3959       28056 :                 char *pszText = nullptr;
    3960       28056 :                 int16_t nVal = 0;
    3961       28056 :                 float fVal = 0.0f;
    3962             :                 const char *pszVal =
    3963       28056 :                     CSLFetchNameValue(papszMD, pasTIFFTags[iTag].pszTagName);
    3964       56049 :                 if (pszVal == nullptr &&
    3965       27993 :                     ((pasTIFFTags[iTag].eType == GTIFFTAGTYPE_STRING &&
    3966       15999 :                       TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal,
    3967       27985 :                                    &pszText)) ||
    3968       27985 :                      (pasTIFFTags[iTag].eType == GTIFFTAGTYPE_SHORT &&
    3969        5999 :                       TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal, &nVal)) ||
    3970       27982 :                      (pasTIFFTags[iTag].eType == GTIFFTAGTYPE_FLOAT &&
    3971        3992 :                       TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal, &fVal)) ||
    3972       27981 :                      (pasTIFFTags[iTag].eType == GTIFFTAGTYPE_BYTE_STRING &&
    3973        2003 :                       TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal, &nCount,
    3974             :                                    &pszText))))
    3975             :                 {
    3976          13 :                     TIFFUnsetField(hTIFF, pasTIFFTags[iTag].nTagVal);
    3977             :                 }
    3978             :             }
    3979             :         }
    3980             :     }
    3981      206550 : }
    3982             : 
    3983             : /************************************************************************/
    3984             : /*                           WriteRPC()                                 */
    3985             : /************************************************************************/
    3986             : 
    3987        7055 : void GTiffDataset::WriteRPC(GDALDataset *poSrcDS, TIFF *l_hTIFF,
    3988             :                             int bSrcIsGeoTIFF, GTiffProfile eProfile,
    3989             :                             const char *pszTIFFFilename,
    3990             :                             CSLConstList papszCreationOptions,
    3991             :                             bool bWriteOnlyInPAMIfNeeded)
    3992             : {
    3993             :     /* -------------------------------------------------------------------- */
    3994             :     /*      Handle RPC data written to TIFF RPCCoefficient tag, RPB file,   */
    3995             :     /*      RPCTEXT file or PAM.                                            */
    3996             :     /* -------------------------------------------------------------------- */
    3997        7055 :     char **papszRPCMD = poSrcDS->GetMetadata(MD_DOMAIN_RPC);
    3998        7055 :     if (papszRPCMD != nullptr)
    3999             :     {
    4000          32 :         bool bRPCSerializedOtherWay = false;
    4001             : 
    4002          32 :         if (eProfile == GTiffProfile::GDALGEOTIFF)
    4003             :         {
    4004          20 :             if (!bWriteOnlyInPAMIfNeeded)
    4005          11 :                 GTiffDatasetWriteRPCTag(l_hTIFF, papszRPCMD);
    4006          20 :             bRPCSerializedOtherWay = true;
    4007             :         }
    4008             : 
    4009             :         // Write RPB file if explicitly asked, or if a non GDAL specific
    4010             :         // profile is selected and RPCTXT is not asked.
    4011             :         bool bRPBExplicitlyAsked =
    4012          32 :             CPLFetchBool(papszCreationOptions, "RPB", false);
    4013             :         bool bRPBExplicitlyDenied =
    4014          32 :             !CPLFetchBool(papszCreationOptions, "RPB", true);
    4015          44 :         if ((eProfile != GTiffProfile::GDALGEOTIFF &&
    4016          12 :              !CPLFetchBool(papszCreationOptions, "RPCTXT", false) &&
    4017          44 :              !bRPBExplicitlyDenied) ||
    4018             :             bRPBExplicitlyAsked)
    4019             :         {
    4020           8 :             if (!bWriteOnlyInPAMIfNeeded)
    4021           4 :                 GDALWriteRPBFile(pszTIFFFilename, papszRPCMD);
    4022           8 :             bRPCSerializedOtherWay = true;
    4023             :         }
    4024             : 
    4025          32 :         if (CPLFetchBool(papszCreationOptions, "RPCTXT", false))
    4026             :         {
    4027           2 :             if (!bWriteOnlyInPAMIfNeeded)
    4028           1 :                 GDALWriteRPCTXTFile(pszTIFFFilename, papszRPCMD);
    4029           2 :             bRPCSerializedOtherWay = true;
    4030             :         }
    4031             : 
    4032          32 :         if (!bRPCSerializedOtherWay && bWriteOnlyInPAMIfNeeded && bSrcIsGeoTIFF)
    4033           1 :             cpl::down_cast<GTiffDataset *>(poSrcDS)
    4034           1 :                 ->GDALPamDataset::SetMetadata(papszRPCMD, MD_DOMAIN_RPC);
    4035             :     }
    4036        7055 : }
    4037             : 
    4038             : /************************************************************************/
    4039             : /*                           WriteMetadata()                            */
    4040             : /************************************************************************/
    4041             : 
    4042        5182 : bool GTiffDataset::WriteMetadata(GDALDataset *poSrcDS, TIFF *l_hTIFF,
    4043             :                                  bool bSrcIsGeoTIFF, GTiffProfile eProfile,
    4044             :                                  const char *pszTIFFFilename,
    4045             :                                  CSLConstList papszCreationOptions,
    4046             :                                  bool bExcludeRPBandIMGFileWriting)
    4047             : 
    4048             : {
    4049             :     /* -------------------------------------------------------------------- */
    4050             :     /*      Convert all the remaining metadata into a simple XML            */
    4051             :     /*      format.                                                         */
    4052             :     /* -------------------------------------------------------------------- */
    4053        5182 :     CPLXMLNode *psRoot = nullptr;
    4054        5182 :     CPLXMLNode *psTail = nullptr;
    4055             : 
    4056             :     const char *pszCopySrcMDD =
    4057        5182 :         CSLFetchNameValueDef(papszCreationOptions, "COPY_SRC_MDD", "AUTO");
    4058             :     char **papszSrcMDD =
    4059        5182 :         CSLFetchNameValueMultiple(papszCreationOptions, "SRC_MDD");
    4060             : 
    4061        5182 :     if (bSrcIsGeoTIFF)
    4062             :     {
    4063        3287 :         GTiffDataset *poSrcDSGTiff = cpl::down_cast<GTiffDataset *>(poSrcDS);
    4064        3287 :         assert(poSrcDSGTiff);
    4065        3287 :         WriteMDMetadata(&poSrcDSGTiff->m_oGTiffMDMD, l_hTIFF, &psRoot, &psTail,
    4066             :                         0, eProfile);
    4067             :     }
    4068             :     else
    4069             :     {
    4070        1895 :         if (EQUAL(pszCopySrcMDD, "AUTO") || CPLTestBool(pszCopySrcMDD) ||
    4071             :             papszSrcMDD)
    4072             :         {
    4073        3784 :             GDALMultiDomainMetadata l_oMDMD;
    4074        1892 :             CSLConstList papszMD = poSrcDS->GetMetadata();
    4075        1896 :             if (CSLCount(papszMD) > 0 &&
    4076           4 :                 (!papszSrcMDD || CSLFindString(papszSrcMDD, "") >= 0 ||
    4077           2 :                  CSLFindString(papszSrcMDD, "_DEFAULT_") >= 0))
    4078             :             {
    4079        1484 :                 l_oMDMD.SetMetadata(papszMD);
    4080             :             }
    4081             : 
    4082        1892 :             if ((!EQUAL(pszCopySrcMDD, "AUTO") && CPLTestBool(pszCopySrcMDD)) ||
    4083             :                 papszSrcMDD)
    4084             :             {
    4085           9 :                 char **papszDomainList = poSrcDS->GetMetadataDomainList();
    4086          39 :                 for (CSLConstList papszIter = papszDomainList;
    4087          39 :                      papszIter && *papszIter; ++papszIter)
    4088             :                 {
    4089          30 :                     const char *pszDomain = *papszIter;
    4090          46 :                     if (pszDomain[0] != 0 &&
    4091          16 :                         (!papszSrcMDD ||
    4092          16 :                          CSLFindString(papszSrcMDD, pszDomain) >= 0))
    4093             :                     {
    4094          12 :                         l_oMDMD.SetMetadata(poSrcDS->GetMetadata(pszDomain),
    4095             :                                             pszDomain);
    4096             :                     }
    4097             :                 }
    4098           9 :                 CSLDestroy(papszDomainList);
    4099             :             }
    4100             : 
    4101        1892 :             WriteMDMetadata(&l_oMDMD, l_hTIFF, &psRoot, &psTail, 0, eProfile);
    4102             :         }
    4103             :     }
    4104             : 
    4105        5182 :     if (!bExcludeRPBandIMGFileWriting)
    4106             :     {
    4107        5176 :         WriteRPC(poSrcDS, l_hTIFF, bSrcIsGeoTIFF, eProfile, pszTIFFFilename,
    4108             :                  papszCreationOptions);
    4109             : 
    4110             :         /* --------------------------------------------------------------------
    4111             :          */
    4112             :         /*      Handle metadata data written to an IMD file. */
    4113             :         /* --------------------------------------------------------------------
    4114             :          */
    4115        5176 :         char **papszIMDMD = poSrcDS->GetMetadata(MD_DOMAIN_IMD);
    4116        5176 :         if (papszIMDMD != nullptr)
    4117             :         {
    4118          20 :             GDALWriteIMDFile(pszTIFFFilename, papszIMDMD);
    4119             :         }
    4120             :     }
    4121             : 
    4122        5182 :     uint16_t nPhotometric = 0;
    4123        5182 :     if (!TIFFGetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, &(nPhotometric)))
    4124           1 :         nPhotometric = PHOTOMETRIC_MINISBLACK;
    4125             : 
    4126        5182 :     const bool bStandardColorInterp = GTIFFIsStandardColorInterpretation(
    4127             :         GDALDataset::ToHandle(poSrcDS), nPhotometric, papszCreationOptions);
    4128             : 
    4129             :     /* -------------------------------------------------------------------- */
    4130             :     /*      We also need to address band specific metadata, and special     */
    4131             :     /*      "role" metadata.                                                */
    4132             :     /* -------------------------------------------------------------------- */
    4133      211154 :     for (int nBand = 1; nBand <= poSrcDS->GetRasterCount(); ++nBand)
    4134             :     {
    4135      205972 :         GDALRasterBand *poBand = poSrcDS->GetRasterBand(nBand);
    4136             : 
    4137      205972 :         if (bSrcIsGeoTIFF)
    4138             :         {
    4139             :             GTiffRasterBand *poSrcBandGTiff =
    4140      201283 :                 cpl::down_cast<GTiffRasterBand *>(poBand);
    4141      201283 :             assert(poSrcBandGTiff);
    4142      201283 :             WriteMDMetadata(&poSrcBandGTiff->m_oGTiffMDMD, l_hTIFF, &psRoot,
    4143             :                             &psTail, nBand, eProfile);
    4144             :         }
    4145             :         else
    4146             :         {
    4147        9378 :             GDALMultiDomainMetadata l_oMDMD;
    4148        4689 :             bool bOMDMDSet = false;
    4149             : 
    4150        4689 :             if (EQUAL(pszCopySrcMDD, "AUTO") && !papszSrcMDD)
    4151             :             {
    4152       14031 :                 for (const char *pszDomain : {"", "IMAGERY"})
    4153             :                 {
    4154        9354 :                     if (CSLConstList papszMD = poBand->GetMetadata(pszDomain))
    4155             :                     {
    4156          86 :                         if (papszMD[0])
    4157             :                         {
    4158          86 :                             bOMDMDSet = true;
    4159          86 :                             l_oMDMD.SetMetadata(papszMD, pszDomain);
    4160             :                         }
    4161             :                     }
    4162        4677 :                 }
    4163             :             }
    4164          12 :             else if (CPLTestBool(pszCopySrcMDD) || papszSrcMDD)
    4165             :             {
    4166           9 :                 char **papszDomainList = poBand->GetMetadataDomainList();
    4167           3 :                 for (const char *pszDomain :
    4168          15 :                      cpl::Iterate(CSLConstList(papszDomainList)))
    4169             :                 {
    4170           9 :                     if (pszDomain[0] != 0 &&
    4171           5 :                         !EQUAL(pszDomain, "IMAGE_STRUCTURE") &&
    4172           2 :                         (!papszSrcMDD ||
    4173           2 :                          CSLFindString(papszSrcMDD, pszDomain) >= 0))
    4174             :                     {
    4175           2 :                         bOMDMDSet = true;
    4176           2 :                         l_oMDMD.SetMetadata(poBand->GetMetadata(pszDomain),
    4177             :                                             pszDomain);
    4178             :                     }
    4179             :                 }
    4180           9 :                 CSLDestroy(papszDomainList);
    4181             :             }
    4182             : 
    4183        4689 :             if (bOMDMDSet)
    4184             :             {
    4185          88 :                 WriteMDMetadata(&l_oMDMD, l_hTIFF, &psRoot, &psTail, nBand,
    4186             :                                 eProfile);
    4187             :             }
    4188             :         }
    4189             : 
    4190      205972 :         const double dfOffset = poBand->GetOffset();
    4191      205972 :         const double dfScale = poBand->GetScale();
    4192      205972 :         bool bGeoTIFFScaleOffsetInZ = false;
    4193             :         double adfGeoTransform[6];
    4194             :         // Check if we have already encoded scale/offset in the GeoTIFF tags
    4195      205972 :         if (poSrcDS->GetGeoTransform(adfGeoTransform) == CE_None &&
    4196        5367 :             adfGeoTransform[2] == 0.0 && adfGeoTransform[4] == 0.0 &&
    4197        5355 :             adfGeoTransform[5] < 0.0 && poSrcDS->GetSpatialRef() &&
    4198      211346 :             poSrcDS->GetSpatialRef()->IsVertical() &&
    4199           7 :             poSrcDS->GetRasterCount() == 1)
    4200             :         {
    4201           7 :             bGeoTIFFScaleOffsetInZ = true;
    4202             :         }
    4203             : 
    4204      205972 :         if ((dfOffset != 0.0 || dfScale != 1.0) && !bGeoTIFFScaleOffsetInZ)
    4205             :         {
    4206          25 :             char szValue[128] = {};
    4207             : 
    4208          25 :             CPLsnprintf(szValue, sizeof(szValue), "%.17g", dfOffset);
    4209          25 :             AppendMetadataItem(&psRoot, &psTail, "OFFSET", szValue, nBand,
    4210             :                                "offset", "");
    4211          25 :             CPLsnprintf(szValue, sizeof(szValue), "%.17g", dfScale);
    4212          25 :             AppendMetadataItem(&psRoot, &psTail, "SCALE", szValue, nBand,
    4213             :                                "scale", "");
    4214             :         }
    4215             : 
    4216      205972 :         const char *pszUnitType = poBand->GetUnitType();
    4217      205972 :         if (pszUnitType != nullptr && pszUnitType[0] != '\0')
    4218             :         {
    4219          39 :             bool bWriteUnit = true;
    4220          39 :             auto poSRS = poSrcDS->GetSpatialRef();
    4221          39 :             if (poSRS && poSRS->IsCompound())
    4222             :             {
    4223           2 :                 const char *pszVertUnit = nullptr;
    4224           2 :                 poSRS->GetTargetLinearUnits("COMPD_CS|VERT_CS", &pszVertUnit);
    4225           2 :                 if (pszVertUnit && EQUAL(pszVertUnit, pszUnitType))
    4226             :                 {
    4227           2 :                     bWriteUnit = false;
    4228             :                 }
    4229             :             }
    4230          39 :             if (bWriteUnit)
    4231             :             {
    4232          37 :                 AppendMetadataItem(&psRoot, &psTail, "UNITTYPE", pszUnitType,
    4233             :                                    nBand, "unittype", "");
    4234             :             }
    4235             :         }
    4236             : 
    4237      205972 :         if (strlen(poBand->GetDescription()) > 0)
    4238             :         {
    4239          15 :             AppendMetadataItem(&psRoot, &psTail, "DESCRIPTION",
    4240          15 :                                poBand->GetDescription(), nBand, "description",
    4241             :                                "");
    4242             :         }
    4243             : 
    4244      206174 :         if (!bStandardColorInterp &&
    4245         202 :             !(nBand <= 3 && EQUAL(CSLFetchNameValueDef(papszCreationOptions,
    4246             :                                                        "PHOTOMETRIC", ""),
    4247             :                                   "RGB")))
    4248             :         {
    4249         234 :             AppendMetadataItem(&psRoot, &psTail, "COLORINTERP",
    4250             :                                GDALGetColorInterpretationName(
    4251         234 :                                    poBand->GetColorInterpretation()),
    4252             :                                nBand, "colorinterp", "");
    4253             :         }
    4254             :     }
    4255             : 
    4256        5182 :     CSLDestroy(papszSrcMDD);
    4257             : 
    4258             :     const char *pszTilingSchemeName =
    4259        5182 :         CSLFetchNameValue(papszCreationOptions, "@TILING_SCHEME_NAME");
    4260        5182 :     if (pszTilingSchemeName)
    4261             :     {
    4262          23 :         AppendMetadataItem(&psRoot, &psTail, "NAME", pszTilingSchemeName, 0,
    4263             :                            nullptr, "TILING_SCHEME");
    4264             : 
    4265          23 :         const char *pszZoomLevel = CSLFetchNameValue(
    4266             :             papszCreationOptions, "@TILING_SCHEME_ZOOM_LEVEL");
    4267          23 :         if (pszZoomLevel)
    4268             :         {
    4269          23 :             AppendMetadataItem(&psRoot, &psTail, "ZOOM_LEVEL", pszZoomLevel, 0,
    4270             :                                nullptr, "TILING_SCHEME");
    4271             :         }
    4272             : 
    4273          23 :         const char *pszAlignedLevels = CSLFetchNameValue(
    4274             :             papszCreationOptions, "@TILING_SCHEME_ALIGNED_LEVELS");
    4275          23 :         if (pszAlignedLevels)
    4276             :         {
    4277           4 :             AppendMetadataItem(&psRoot, &psTail, "ALIGNED_LEVELS",
    4278             :                                pszAlignedLevels, 0, nullptr, "TILING_SCHEME");
    4279             :         }
    4280             :     }
    4281             : 
    4282             :     /* -------------------------------------------------------------------- */
    4283             :     /*      Write information about some codecs.                            */
    4284             :     /* -------------------------------------------------------------------- */
    4285        5182 :     if (CPLTestBool(
    4286             :             CPLGetConfigOption("GTIFF_WRITE_IMAGE_STRUCTURE_METADATA", "YES")))
    4287             :     {
    4288             :         const char *pszCompress =
    4289        5177 :             CSLFetchNameValue(papszCreationOptions, "COMPRESS");
    4290        5177 :         if (pszCompress && EQUAL(pszCompress, "WEBP"))
    4291             :         {
    4292          31 :             if (GTiffGetWebPLossless(papszCreationOptions))
    4293             :             {
    4294           6 :                 AppendMetadataItem(&psRoot, &psTail,
    4295             :                                    "COMPRESSION_REVERSIBILITY", "LOSSLESS", 0,
    4296             :                                    nullptr, "IMAGE_STRUCTURE");
    4297             :             }
    4298             :             else
    4299             :             {
    4300          25 :                 AppendMetadataItem(
    4301             :                     &psRoot, &psTail, "WEBP_LEVEL",
    4302          25 :                     CPLSPrintf("%d", GTiffGetWebPLevel(papszCreationOptions)),
    4303             :                     0, nullptr, "IMAGE_STRUCTURE");
    4304             :             }
    4305             :         }
    4306        5146 :         else if (pszCompress && STARTS_WITH_CI(pszCompress, "LERC"))
    4307             :         {
    4308             :             const double dfMaxZError =
    4309          97 :                 GTiffGetLERCMaxZError(papszCreationOptions);
    4310             :             const double dfMaxZErrorOverview =
    4311          97 :                 GTiffGetLERCMaxZErrorOverview(papszCreationOptions);
    4312          97 :             if (dfMaxZError == 0.0 && dfMaxZErrorOverview == 0.0)
    4313             :             {
    4314          83 :                 AppendMetadataItem(&psRoot, &psTail,
    4315             :                                    "COMPRESSION_REVERSIBILITY", "LOSSLESS", 0,
    4316             :                                    nullptr, "IMAGE_STRUCTURE");
    4317             :             }
    4318             :             else
    4319             :             {
    4320          14 :                 AppendMetadataItem(&psRoot, &psTail, "MAX_Z_ERROR",
    4321             :                                    CSLFetchNameValueDef(papszCreationOptions,
    4322             :                                                         "MAX_Z_ERROR", ""),
    4323             :                                    0, nullptr, "IMAGE_STRUCTURE");
    4324          14 :                 if (dfMaxZError != dfMaxZErrorOverview)
    4325             :                 {
    4326           3 :                     AppendMetadataItem(
    4327             :                         &psRoot, &psTail, "MAX_Z_ERROR_OVERVIEW",
    4328             :                         CSLFetchNameValueDef(papszCreationOptions,
    4329             :                                              "MAX_Z_ERROR_OVERVIEW", ""),
    4330             :                         0, nullptr, "IMAGE_STRUCTURE");
    4331             :                 }
    4332          97 :             }
    4333             :         }
    4334             : #if HAVE_JXL
    4335        5049 :         else if (pszCompress && EQUAL(pszCompress, "JXL"))
    4336             :         {
    4337          98 :             float fDistance = 0.0f;
    4338          98 :             if (GTiffGetJXLLossless(papszCreationOptions))
    4339             :             {
    4340          80 :                 AppendMetadataItem(&psRoot, &psTail,
    4341             :                                    "COMPRESSION_REVERSIBILITY", "LOSSLESS", 0,
    4342             :                                    nullptr, "IMAGE_STRUCTURE");
    4343             :             }
    4344             :             else
    4345             :             {
    4346          18 :                 fDistance = GTiffGetJXLDistance(papszCreationOptions);
    4347          18 :                 AppendMetadataItem(&psRoot, &psTail, "JXL_DISTANCE",
    4348             :                                    CPLSPrintf("%f", fDistance), 0, nullptr,
    4349             :                                    "IMAGE_STRUCTURE");
    4350             :             }
    4351             :             const float fAlphaDistance =
    4352          98 :                 GTiffGetJXLAlphaDistance(papszCreationOptions);
    4353          98 :             if (fAlphaDistance >= 0.0f && fAlphaDistance != fDistance)
    4354             :             {
    4355           2 :                 AppendMetadataItem(&psRoot, &psTail, "JXL_ALPHA_DISTANCE",
    4356             :                                    CPLSPrintf("%f", fAlphaDistance), 0, nullptr,
    4357             :                                    "IMAGE_STRUCTURE");
    4358             :             }
    4359          98 :             AppendMetadataItem(
    4360             :                 &psRoot, &psTail, "JXL_EFFORT",
    4361             :                 CPLSPrintf("%d", GTiffGetJXLEffort(papszCreationOptions)), 0,
    4362             :                 nullptr, "IMAGE_STRUCTURE");
    4363             :         }
    4364             : #endif
    4365             :     }
    4366             : 
    4367             :     /* -------------------------------------------------------------------- */
    4368             :     /*      Write out the generic XML metadata if there is any.             */
    4369             :     /* -------------------------------------------------------------------- */
    4370        5182 :     if (psRoot != nullptr)
    4371             :     {
    4372         599 :         bool bRet = true;
    4373             : 
    4374         599 :         if (eProfile == GTiffProfile::GDALGEOTIFF)
    4375             :         {
    4376         582 :             char *pszXML_MD = CPLSerializeXMLTree(psRoot);
    4377         582 :             TIFFSetField(l_hTIFF, TIFFTAG_GDAL_METADATA, pszXML_MD);
    4378         582 :             CPLFree(pszXML_MD);
    4379             :         }
    4380             :         else
    4381             :         {
    4382          17 :             if (bSrcIsGeoTIFF)
    4383          11 :                 cpl::down_cast<GTiffDataset *>(poSrcDS)->PushMetadataToPam();
    4384             :             else
    4385           6 :                 bRet = false;
    4386             :         }
    4387             : 
    4388         599 :         CPLDestroyXMLNode(psRoot);
    4389             : 
    4390         599 :         return bRet;
    4391             :     }
    4392             : 
    4393             :     // If we have no more metadata but it existed before,
    4394             :     // remove the GDAL_METADATA tag.
    4395        4583 :     if (eProfile == GTiffProfile::GDALGEOTIFF)
    4396             :     {
    4397        4559 :         char *pszText = nullptr;
    4398        4559 :         if (TIFFGetField(l_hTIFF, TIFFTAG_GDAL_METADATA, &pszText))
    4399             :         {
    4400           6 :             TIFFUnsetField(l_hTIFF, TIFFTAG_GDAL_METADATA);
    4401             :         }
    4402             :     }
    4403             : 
    4404        4583 :     return true;
    4405             : }
    4406             : 
    4407             : /************************************************************************/
    4408             : /*                         PushMetadataToPam()                          */
    4409             : /*                                                                      */
    4410             : /*      When producing a strict profile TIFF or if our aggregate        */
    4411             : /*      metadata is too big for a single tiff tag we may end up         */
    4412             : /*      needing to write it via the PAM mechanisms.  This method        */
    4413             : /*      copies all the appropriate metadata into the PAM level          */
    4414             : /*      metadata object but with special care to avoid copying          */
    4415             : /*      metadata handled in other ways in TIFF format.                  */
    4416             : /************************************************************************/
    4417             : 
    4418          20 : void GTiffDataset::PushMetadataToPam()
    4419             : 
    4420             : {
    4421          20 :     if (GetPamFlags() & GPF_DISABLED)
    4422           0 :         return;
    4423             : 
    4424          20 :     const bool bStandardColorInterp = GTIFFIsStandardColorInterpretation(
    4425          20 :         GDALDataset::ToHandle(this), m_nPhotometric, m_papszCreationOptions);
    4426             : 
    4427          66 :     for (int nBand = 0; nBand <= GetRasterCount(); ++nBand)
    4428             :     {
    4429          46 :         GDALMultiDomainMetadata *poSrcMDMD = nullptr;
    4430          46 :         GTiffRasterBand *poBand = nullptr;
    4431             : 
    4432          46 :         if (nBand == 0)
    4433             :         {
    4434          20 :             poSrcMDMD = &(this->m_oGTiffMDMD);
    4435             :         }
    4436             :         else
    4437             :         {
    4438          26 :             poBand = cpl::down_cast<GTiffRasterBand *>(GetRasterBand(nBand));
    4439          26 :             poSrcMDMD = &(poBand->m_oGTiffMDMD);
    4440             :         }
    4441             : 
    4442             :         /* --------------------------------------------------------------------
    4443             :          */
    4444             :         /*      Loop over the available domains. */
    4445             :         /* --------------------------------------------------------------------
    4446             :          */
    4447          46 :         CSLConstList papszDomainList = poSrcMDMD->GetDomainList();
    4448          96 :         for (int iDomain = 0; papszDomainList && papszDomainList[iDomain];
    4449             :              ++iDomain)
    4450             :         {
    4451          50 :             char **papszMD = poSrcMDMD->GetMetadata(papszDomainList[iDomain]);
    4452             : 
    4453          50 :             if (EQUAL(papszDomainList[iDomain], MD_DOMAIN_RPC) ||
    4454          50 :                 EQUAL(papszDomainList[iDomain], MD_DOMAIN_IMD) ||
    4455          50 :                 EQUAL(papszDomainList[iDomain], "_temporary_") ||
    4456          50 :                 EQUAL(papszDomainList[iDomain], "IMAGE_STRUCTURE") ||
    4457          30 :                 EQUAL(papszDomainList[iDomain], "COLOR_PROFILE"))
    4458          20 :                 continue;
    4459             : 
    4460          30 :             papszMD = CSLDuplicate(papszMD);
    4461             : 
    4462         105 :             for (int i = CSLCount(papszMD) - 1; i >= 0; --i)
    4463             :             {
    4464          75 :                 if (STARTS_WITH_CI(papszMD[i], "TIFFTAG_") ||
    4465          75 :                     EQUALN(papszMD[i], GDALMD_AREA_OR_POINT,
    4466             :                            strlen(GDALMD_AREA_OR_POINT)))
    4467           4 :                     papszMD = CSLRemoveStrings(papszMD, i, 1, nullptr);
    4468             :             }
    4469             : 
    4470          30 :             if (nBand == 0)
    4471          16 :                 GDALPamDataset::SetMetadata(papszMD, papszDomainList[iDomain]);
    4472             :             else
    4473          14 :                 poBand->GDALPamRasterBand::SetMetadata(
    4474          14 :                     papszMD, papszDomainList[iDomain]);
    4475             : 
    4476          30 :             CSLDestroy(papszMD);
    4477             :         }
    4478             : 
    4479             :         /* --------------------------------------------------------------------
    4480             :          */
    4481             :         /*      Handle some "special domain" stuff. */
    4482             :         /* --------------------------------------------------------------------
    4483             :          */
    4484          46 :         if (poBand != nullptr)
    4485             :         {
    4486          26 :             poBand->GDALPamRasterBand::SetOffset(poBand->GetOffset());
    4487          26 :             poBand->GDALPamRasterBand::SetScale(poBand->GetScale());
    4488          26 :             poBand->GDALPamRasterBand::SetUnitType(poBand->GetUnitType());
    4489          26 :             poBand->GDALPamRasterBand::SetDescription(poBand->GetDescription());
    4490          26 :             if (!bStandardColorInterp)
    4491             :             {
    4492           3 :                 poBand->GDALPamRasterBand::SetColorInterpretation(
    4493           3 :                     poBand->GetColorInterpretation());
    4494             :             }
    4495             :         }
    4496             :     }
    4497          20 :     MarkPamDirty();
    4498             : }
    4499             : 
    4500             : /************************************************************************/
    4501             : /*                         WriteNoDataValue()                           */
    4502             : /************************************************************************/
    4503             : 
    4504         385 : void GTiffDataset::WriteNoDataValue(TIFF *hTIFF, double dfNoData)
    4505             : 
    4506             : {
    4507         770 :     CPLString osVal(GTiffFormatGDALNoDataTagValue(dfNoData));
    4508         385 :     TIFFSetField(hTIFF, TIFFTAG_GDAL_NODATA, osVal.c_str());
    4509         385 : }
    4510             : 
    4511           2 : void GTiffDataset::WriteNoDataValue(TIFF *hTIFF, int64_t nNoData)
    4512             : 
    4513             : {
    4514           2 :     TIFFSetField(hTIFF, TIFFTAG_GDAL_NODATA,
    4515             :                  CPLSPrintf(CPL_FRMT_GIB, static_cast<GIntBig>(nNoData)));
    4516           2 : }
    4517             : 
    4518           2 : void GTiffDataset::WriteNoDataValue(TIFF *hTIFF, uint64_t nNoData)
    4519             : 
    4520             : {
    4521           2 :     TIFFSetField(hTIFF, TIFFTAG_GDAL_NODATA,
    4522             :                  CPLSPrintf(CPL_FRMT_GUIB, static_cast<GUIntBig>(nNoData)));
    4523           2 : }
    4524             : 
    4525             : /************************************************************************/
    4526             : /*                         UnsetNoDataValue()                           */
    4527             : /************************************************************************/
    4528             : 
    4529           1 : void GTiffDataset::UnsetNoDataValue(TIFF *l_hTIFF)
    4530             : 
    4531             : {
    4532           1 :     TIFFUnsetField(l_hTIFF, TIFFTAG_GDAL_NODATA);
    4533           1 : }
    4534             : 
    4535             : /************************************************************************/
    4536             : /*                             SaveICCProfile()                         */
    4537             : /*                                                                      */
    4538             : /*      Save ICC Profile or colorimetric data into file                 */
    4539             : /* pDS:                                                                 */
    4540             : /*      Dataset that contains the metadata with the ICC or colorimetric */
    4541             : /*      data. If this argument is specified, all other arguments are    */
    4542             : /*      ignored. Set them to NULL or 0.                                 */
    4543             : /* hTIFF:                                                               */
    4544             : /*      Pointer to TIFF handle. Only needed if pDS is NULL or           */
    4545             : /*      pDS->m_hTIFF is NULL.                                             */
    4546             : /* papszParamList:                                                       */
    4547             : /*      Options containing the ICC profile or colorimetric metadata.    */
    4548             : /*      Ignored if pDS is not NULL.                                     */
    4549             : /* nBitsPerSample:                                                      */
    4550             : /*      Bits per sample. Ignored if pDS is not NULL.                    */
    4551             : /************************************************************************/
    4552             : 
    4553        6997 : void GTiffDataset::SaveICCProfile(GTiffDataset *pDS, TIFF *l_hTIFF,
    4554             :                                   char **papszParamList,
    4555             :                                   uint32_t l_nBitsPerSample)
    4556             : {
    4557        6997 :     if ((pDS != nullptr) && (pDS->eAccess != GA_Update))
    4558           0 :         return;
    4559             : 
    4560        6997 :     if (l_hTIFF == nullptr)
    4561             :     {
    4562           2 :         if (pDS == nullptr)
    4563           0 :             return;
    4564             : 
    4565           2 :         l_hTIFF = pDS->m_hTIFF;
    4566           2 :         if (l_hTIFF == nullptr)
    4567           0 :             return;
    4568             :     }
    4569             : 
    4570        6997 :     if ((papszParamList == nullptr) && (pDS == nullptr))
    4571        2407 :         return;
    4572             : 
    4573        4590 :     const char *pszValue = nullptr;
    4574        4590 :     if (pDS != nullptr)
    4575           2 :         pszValue = pDS->GetMetadataItem("SOURCE_ICC_PROFILE", "COLOR_PROFILE");
    4576             :     else
    4577        4588 :         pszValue = CSLFetchNameValue(papszParamList, "SOURCE_ICC_PROFILE");
    4578        4590 :     if (pszValue != nullptr)
    4579             :     {
    4580           8 :         char *pEmbedBuffer = CPLStrdup(pszValue);
    4581             :         int32_t nEmbedLen =
    4582           8 :             CPLBase64DecodeInPlace(reinterpret_cast<GByte *>(pEmbedBuffer));
    4583             : 
    4584           8 :         TIFFSetField(l_hTIFF, TIFFTAG_ICCPROFILE, nEmbedLen, pEmbedBuffer);
    4585             : 
    4586           8 :         CPLFree(pEmbedBuffer);
    4587             :     }
    4588             :     else
    4589             :     {
    4590             :         // Output colorimetric data.
    4591        4582 :         float pCHR[6] = {};     // Primaries.
    4592        4582 :         uint16_t pTXR[6] = {};  // Transfer range.
    4593        4582 :         const char *pszCHRNames[] = {"SOURCE_PRIMARIES_RED",
    4594             :                                      "SOURCE_PRIMARIES_GREEN",
    4595             :                                      "SOURCE_PRIMARIES_BLUE"};
    4596        4582 :         const char *pszTXRNames[] = {"TIFFTAG_TRANSFERRANGE_BLACK",
    4597             :                                      "TIFFTAG_TRANSFERRANGE_WHITE"};
    4598             : 
    4599             :         // Output chromacities.
    4600        4582 :         bool bOutputCHR = true;
    4601        4597 :         for (int i = 0; i < 3 && bOutputCHR; ++i)
    4602             :         {
    4603        4592 :             if (pDS != nullptr)
    4604             :                 pszValue =
    4605           3 :                     pDS->GetMetadataItem(pszCHRNames[i], "COLOR_PROFILE");
    4606             :             else
    4607        4589 :                 pszValue = CSLFetchNameValue(papszParamList, pszCHRNames[i]);
    4608        4592 :             if (pszValue == nullptr)
    4609             :             {
    4610        4577 :                 bOutputCHR = false;
    4611        4577 :                 break;
    4612             :             }
    4613             : 
    4614          15 :             char **papszTokens = CSLTokenizeString2(pszValue, ",",
    4615             :                                                     CSLT_ALLOWEMPTYTOKENS |
    4616             :                                                         CSLT_STRIPLEADSPACES |
    4617             :                                                         CSLT_STRIPENDSPACES);
    4618             : 
    4619          15 :             if (CSLCount(papszTokens) != 3)
    4620             :             {
    4621           0 :                 bOutputCHR = false;
    4622           0 :                 CSLDestroy(papszTokens);
    4623           0 :                 break;
    4624             :             }
    4625             : 
    4626          60 :             for (int j = 0; j < 3; ++j)
    4627             :             {
    4628          45 :                 float v = static_cast<float>(CPLAtof(papszTokens[j]));
    4629             : 
    4630          45 :                 if (j == 2)
    4631             :                 {
    4632             :                     // Last term of xyY color must be 1.0.
    4633          15 :                     if (v != 1.0)
    4634             :                     {
    4635           0 :                         bOutputCHR = false;
    4636           0 :                         break;
    4637             :                     }
    4638             :                 }
    4639             :                 else
    4640             :                 {
    4641          30 :                     pCHR[i * 2 + j] = v;
    4642             :                 }
    4643             :             }
    4644             : 
    4645          15 :             CSLDestroy(papszTokens);
    4646             :         }
    4647             : 
    4648        4582 :         if (bOutputCHR)
    4649             :         {
    4650           5 :             TIFFSetField(l_hTIFF, TIFFTAG_PRIMARYCHROMATICITIES, pCHR);
    4651             :         }
    4652             : 
    4653             :         // Output whitepoint.
    4654        4582 :         if (pDS != nullptr)
    4655             :             pszValue =
    4656           1 :                 pDS->GetMetadataItem("SOURCE_WHITEPOINT", "COLOR_PROFILE");
    4657             :         else
    4658        4581 :             pszValue = CSLFetchNameValue(papszParamList, "SOURCE_WHITEPOINT");
    4659        4582 :         if (pszValue != nullptr)
    4660             :         {
    4661           5 :             char **papszTokens = CSLTokenizeString2(pszValue, ",",
    4662             :                                                     CSLT_ALLOWEMPTYTOKENS |
    4663             :                                                         CSLT_STRIPLEADSPACES |
    4664             :                                                         CSLT_STRIPENDSPACES);
    4665             : 
    4666           5 :             bool bOutputWhitepoint = true;
    4667           5 :             float pWP[2] = {0.0f, 0.0f};  // Whitepoint
    4668           5 :             if (CSLCount(papszTokens) != 3)
    4669             :             {
    4670           0 :                 bOutputWhitepoint = false;
    4671             :             }
    4672             :             else
    4673             :             {
    4674          20 :                 for (int j = 0; j < 3; ++j)
    4675             :                 {
    4676          15 :                     const float v = static_cast<float>(CPLAtof(papszTokens[j]));
    4677             : 
    4678          15 :                     if (j == 2)
    4679             :                     {
    4680             :                         // Last term of xyY color must be 1.0.
    4681           5 :                         if (v != 1.0)
    4682             :                         {
    4683           0 :                             bOutputWhitepoint = false;
    4684           0 :                             break;
    4685             :                         }
    4686             :                     }
    4687             :                     else
    4688             :                     {
    4689          10 :                         pWP[j] = v;
    4690             :                     }
    4691             :                 }
    4692             :             }
    4693           5 :             CSLDestroy(papszTokens);
    4694             : 
    4695           5 :             if (bOutputWhitepoint)
    4696             :             {
    4697           5 :                 TIFFSetField(l_hTIFF, TIFFTAG_WHITEPOINT, pWP);
    4698             :             }
    4699             :         }
    4700             : 
    4701             :         // Set transfer function metadata.
    4702        4582 :         char const *pszTFRed = nullptr;
    4703        4582 :         if (pDS != nullptr)
    4704           1 :             pszTFRed = pDS->GetMetadataItem("TIFFTAG_TRANSFERFUNCTION_RED",
    4705             :                                             "COLOR_PROFILE");
    4706             :         else
    4707        4581 :             pszTFRed = CSLFetchNameValue(papszParamList,
    4708             :                                          "TIFFTAG_TRANSFERFUNCTION_RED");
    4709             : 
    4710        4582 :         char const *pszTFGreen = nullptr;
    4711        4582 :         if (pDS != nullptr)
    4712           1 :             pszTFGreen = pDS->GetMetadataItem("TIFFTAG_TRANSFERFUNCTION_GREEN",
    4713             :                                               "COLOR_PROFILE");
    4714             :         else
    4715        4581 :             pszTFGreen = CSLFetchNameValue(papszParamList,
    4716             :                                            "TIFFTAG_TRANSFERFUNCTION_GREEN");
    4717             : 
    4718        4582 :         char const *pszTFBlue = nullptr;
    4719        4582 :         if (pDS != nullptr)
    4720           1 :             pszTFBlue = pDS->GetMetadataItem("TIFFTAG_TRANSFERFUNCTION_BLUE",
    4721             :                                              "COLOR_PROFILE");
    4722             :         else
    4723        4581 :             pszTFBlue = CSLFetchNameValue(papszParamList,
    4724             :                                           "TIFFTAG_TRANSFERFUNCTION_BLUE");
    4725             : 
    4726        4582 :         if ((pszTFRed != nullptr) && (pszTFGreen != nullptr) &&
    4727             :             (pszTFBlue != nullptr))
    4728             :         {
    4729             :             // Get length of table.
    4730           4 :             const int nTransferFunctionLength =
    4731           4 :                 1 << ((pDS != nullptr) ? pDS->m_nBitsPerSample
    4732             :                                        : l_nBitsPerSample);
    4733             : 
    4734           4 :             char **papszTokensRed = CSLTokenizeString2(
    4735             :                 pszTFRed, ",",
    4736             :                 CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
    4737             :                     CSLT_STRIPENDSPACES);
    4738           4 :             char **papszTokensGreen = CSLTokenizeString2(
    4739             :                 pszTFGreen, ",",
    4740             :                 CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
    4741             :                     CSLT_STRIPENDSPACES);
    4742           4 :             char **papszTokensBlue = CSLTokenizeString2(
    4743             :                 pszTFBlue, ",",
    4744             :                 CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
    4745             :                     CSLT_STRIPENDSPACES);
    4746             : 
    4747           4 :             if ((CSLCount(papszTokensRed) == nTransferFunctionLength) &&
    4748           8 :                 (CSLCount(papszTokensGreen) == nTransferFunctionLength) &&
    4749           4 :                 (CSLCount(papszTokensBlue) == nTransferFunctionLength))
    4750             :             {
    4751             :                 uint16_t *pTransferFuncRed = static_cast<uint16_t *>(
    4752           4 :                     CPLMalloc(sizeof(uint16_t) * nTransferFunctionLength));
    4753             :                 uint16_t *pTransferFuncGreen = static_cast<uint16_t *>(
    4754           4 :                     CPLMalloc(sizeof(uint16_t) * nTransferFunctionLength));
    4755             :                 uint16_t *pTransferFuncBlue = static_cast<uint16_t *>(
    4756           4 :                     CPLMalloc(sizeof(uint16_t) * nTransferFunctionLength));
    4757             : 
    4758             :                 // Convert our table in string format into int16_t format.
    4759        1028 :                 for (int i = 0; i < nTransferFunctionLength; ++i)
    4760             :                 {
    4761        1024 :                     pTransferFuncRed[i] =
    4762        1024 :                         static_cast<uint16_t>(atoi(papszTokensRed[i]));
    4763        1024 :                     pTransferFuncGreen[i] =
    4764        1024 :                         static_cast<uint16_t>(atoi(papszTokensGreen[i]));
    4765        1024 :                     pTransferFuncBlue[i] =
    4766        1024 :                         static_cast<uint16_t>(atoi(papszTokensBlue[i]));
    4767             :                 }
    4768             : 
    4769           4 :                 TIFFSetField(l_hTIFF, TIFFTAG_TRANSFERFUNCTION,
    4770             :                              pTransferFuncRed, pTransferFuncGreen,
    4771             :                              pTransferFuncBlue);
    4772             : 
    4773           4 :                 CPLFree(pTransferFuncRed);
    4774           4 :                 CPLFree(pTransferFuncGreen);
    4775           4 :                 CPLFree(pTransferFuncBlue);
    4776             :             }
    4777             : 
    4778           4 :             CSLDestroy(papszTokensRed);
    4779           4 :             CSLDestroy(papszTokensGreen);
    4780           4 :             CSLDestroy(papszTokensBlue);
    4781             :         }
    4782             : 
    4783             :         // Output transfer range.
    4784        4582 :         bool bOutputTransferRange = true;
    4785        4582 :         for (int i = 0; (i < 2) && bOutputTransferRange; ++i)
    4786             :         {
    4787        4582 :             if (pDS != nullptr)
    4788             :                 pszValue =
    4789           1 :                     pDS->GetMetadataItem(pszTXRNames[i], "COLOR_PROFILE");
    4790             :             else
    4791        4581 :                 pszValue = CSLFetchNameValue(papszParamList, pszTXRNames[i]);
    4792        4582 :             if (pszValue == nullptr)
    4793             :             {
    4794        4582 :                 bOutputTransferRange = false;
    4795        4582 :                 break;
    4796             :             }
    4797             : 
    4798           0 :             char **papszTokens = CSLTokenizeString2(pszValue, ",",
    4799             :                                                     CSLT_ALLOWEMPTYTOKENS |
    4800             :                                                         CSLT_STRIPLEADSPACES |
    4801             :                                                         CSLT_STRIPENDSPACES);
    4802             : 
    4803           0 :             if (CSLCount(papszTokens) != 3)
    4804             :             {
    4805           0 :                 bOutputTransferRange = false;
    4806           0 :                 CSLDestroy(papszTokens);
    4807           0 :                 break;
    4808             :             }
    4809             : 
    4810           0 :             for (int j = 0; j < 3; ++j)
    4811             :             {
    4812           0 :                 pTXR[i + j * 2] = static_cast<uint16_t>(atoi(papszTokens[j]));
    4813             :             }
    4814             : 
    4815           0 :             CSLDestroy(papszTokens);
    4816             :         }
    4817             : 
    4818        4582 :         if (bOutputTransferRange)
    4819             :         {
    4820           0 :             const int TIFFTAG_TRANSFERRANGE = 0x0156;
    4821           0 :             TIFFSetField(l_hTIFF, TIFFTAG_TRANSFERRANGE, pTXR);
    4822             :         }
    4823             :     }
    4824             : }
    4825             : 
    4826       12077 : static signed char GTiffGetLZMAPreset(char **papszOptions)
    4827             : {
    4828       12077 :     int nLZMAPreset = -1;
    4829       12077 :     const char *pszValue = CSLFetchNameValue(papszOptions, "LZMA_PRESET");
    4830       12077 :     if (pszValue != nullptr)
    4831             :     {
    4832          20 :         nLZMAPreset = atoi(pszValue);
    4833          20 :         if (!(nLZMAPreset >= 0 && nLZMAPreset <= 9))
    4834             :         {
    4835           0 :             CPLError(CE_Warning, CPLE_IllegalArg,
    4836             :                      "LZMA_PRESET=%s value not recognised, ignoring.",
    4837             :                      pszValue);
    4838           0 :             nLZMAPreset = -1;
    4839             :         }
    4840             :     }
    4841       12077 :     return static_cast<signed char>(nLZMAPreset);
    4842             : }
    4843             : 
    4844       12077 : static signed char GTiffGetZSTDPreset(char **papszOptions)
    4845             : {
    4846       12077 :     int nZSTDLevel = -1;
    4847       12077 :     const char *pszValue = CSLFetchNameValue(papszOptions, "ZSTD_LEVEL");
    4848       12077 :     if (pszValue != nullptr)
    4849             :     {
    4850          24 :         nZSTDLevel = atoi(pszValue);
    4851          24 :         if (!(nZSTDLevel >= 1 && nZSTDLevel <= 22))
    4852             :         {
    4853           0 :             CPLError(CE_Warning, CPLE_IllegalArg,
    4854             :                      "ZSTD_LEVEL=%s value not recognised, ignoring.", pszValue);
    4855           0 :             nZSTDLevel = -1;
    4856             :         }
    4857             :     }
    4858       12077 :     return static_cast<signed char>(nZSTDLevel);
    4859             : }
    4860             : 
    4861       12077 : static signed char GTiffGetZLevel(char **papszOptions)
    4862             : {
    4863       12077 :     int nZLevel = -1;
    4864       12077 :     const char *pszValue = CSLFetchNameValue(papszOptions, "ZLEVEL");
    4865       12077 :     if (pszValue != nullptr)
    4866             :     {
    4867          44 :         nZLevel = atoi(pszValue);
    4868             : #ifdef TIFFTAG_DEFLATE_SUBCODEC
    4869          44 :         constexpr int nMaxLevel = 12;
    4870             : #ifndef LIBDEFLATE_SUPPORT
    4871             :         if (nZLevel > 9 && nZLevel <= nMaxLevel)
    4872             :         {
    4873             :             CPLDebug("GTiff",
    4874             :                      "ZLEVEL=%d not supported in a non-libdeflate enabled "
    4875             :                      "libtiff build. Capping to 9",
    4876             :                      nZLevel);
    4877             :             nZLevel = 9;
    4878             :         }
    4879             : #endif
    4880             : #else
    4881             :         constexpr int nMaxLevel = 9;
    4882             : #endif
    4883          44 :         if (nZLevel < 1 || nZLevel > nMaxLevel)
    4884             :         {
    4885           0 :             CPLError(CE_Warning, CPLE_IllegalArg,
    4886             :                      "ZLEVEL=%s value not recognised, ignoring.", pszValue);
    4887           0 :             nZLevel = -1;
    4888             :         }
    4889             :     }
    4890       12077 :     return static_cast<signed char>(nZLevel);
    4891             : }
    4892             : 
    4893       12077 : static signed char GTiffGetJpegQuality(char **papszOptions)
    4894             : {
    4895       12077 :     int nJpegQuality = -1;
    4896       12077 :     const char *pszValue = CSLFetchNameValue(papszOptions, "JPEG_QUALITY");
    4897       12077 :     if (pszValue != nullptr)
    4898             :     {
    4899        1939 :         nJpegQuality = atoi(pszValue);
    4900        1939 :         if (nJpegQuality < 1 || nJpegQuality > 100)
    4901             :         {
    4902           0 :             CPLError(CE_Warning, CPLE_IllegalArg,
    4903             :                      "JPEG_QUALITY=%s value not recognised, ignoring.",
    4904             :                      pszValue);
    4905           0 :             nJpegQuality = -1;
    4906             :         }
    4907             :     }
    4908       12077 :     return static_cast<signed char>(nJpegQuality);
    4909             : }
    4910             : 
    4911       12077 : static signed char GTiffGetJpegTablesMode(char **papszOptions)
    4912             : {
    4913       12077 :     return static_cast<signed char>(atoi(
    4914             :         CSLFetchNameValueDef(papszOptions, "JPEGTABLESMODE",
    4915       12077 :                              CPLSPrintf("%d", knGTIFFJpegTablesModeDefault))));
    4916             : }
    4917             : 
    4918             : /************************************************************************/
    4919             : /*                        GetDiscardLsbOption()                         */
    4920             : /************************************************************************/
    4921             : 
    4922        5030 : static GTiffDataset::MaskOffset *GetDiscardLsbOption(TIFF *hTIFF,
    4923             :                                                      char **papszOptions)
    4924             : {
    4925        5030 :     const char *pszBits = CSLFetchNameValue(papszOptions, "DISCARD_LSB");
    4926        5030 :     if (pszBits == nullptr)
    4927        4908 :         return nullptr;
    4928             : 
    4929         122 :     uint16_t nPhotometric = 0;
    4930         122 :     TIFFGetFieldDefaulted(hTIFF, TIFFTAG_PHOTOMETRIC, &nPhotometric);
    4931             : 
    4932         122 :     uint16_t nBitsPerSample = 0;
    4933         122 :     if (!TIFFGetField(hTIFF, TIFFTAG_BITSPERSAMPLE, &nBitsPerSample))
    4934           0 :         nBitsPerSample = 1;
    4935             : 
    4936         122 :     uint16_t nSamplesPerPixel = 0;
    4937         122 :     if (!TIFFGetField(hTIFF, TIFFTAG_SAMPLESPERPIXEL, &nSamplesPerPixel))
    4938           0 :         nSamplesPerPixel = 1;
    4939             : 
    4940         122 :     uint16_t nSampleFormat = 0;
    4941         122 :     if (!TIFFGetField(hTIFF, TIFFTAG_SAMPLEFORMAT, &nSampleFormat))
    4942           0 :         nSampleFormat = SAMPLEFORMAT_UINT;
    4943             : 
    4944         122 :     if (nPhotometric == PHOTOMETRIC_PALETTE)
    4945             :     {
    4946           1 :         CPLError(CE_Warning, CPLE_AppDefined,
    4947             :                  "DISCARD_LSB ignored on a paletted image");
    4948           1 :         return nullptr;
    4949             :     }
    4950         121 :     if (!(nBitsPerSample == 8 || nBitsPerSample == 16 || nBitsPerSample == 32 ||
    4951          13 :           nBitsPerSample == 64))
    4952             :     {
    4953           1 :         CPLError(CE_Warning, CPLE_AppDefined,
    4954             :                  "DISCARD_LSB ignored on non 8, 16, 32 or 64 bits images");
    4955           1 :         return nullptr;
    4956             :     }
    4957             : 
    4958         240 :     const CPLStringList aosTokens(CSLTokenizeString2(pszBits, ",", 0));
    4959         120 :     const int nTokens = aosTokens.size();
    4960         120 :     GTiffDataset::MaskOffset *panMaskOffsetLsb = nullptr;
    4961         120 :     if (nTokens == 1 || nTokens == nSamplesPerPixel)
    4962             :     {
    4963             :         panMaskOffsetLsb = static_cast<GTiffDataset::MaskOffset *>(
    4964         119 :             CPLCalloc(nSamplesPerPixel, sizeof(GTiffDataset::MaskOffset)));
    4965         374 :         for (int i = 0; i < nSamplesPerPixel; ++i)
    4966             :         {
    4967         255 :             const int nBits = atoi(aosTokens[nTokens == 1 ? 0 : i]);
    4968         510 :             const int nMaxBits = (nSampleFormat == SAMPLEFORMAT_IEEEFP)
    4969         484 :                                      ? ((nBitsPerSample == 32)   ? 23 - 1
    4970          26 :                                         : (nBitsPerSample == 64) ? 53 - 1
    4971             :                                                                  : 0)
    4972         203 :                                  : nSampleFormat == SAMPLEFORMAT_INT
    4973         203 :                                      ? nBitsPerSample - 2
    4974         119 :                                      : nBitsPerSample - 1;
    4975             : 
    4976         255 :             if (nBits < 0 || nBits > nMaxBits)
    4977             :             {
    4978           0 :                 CPLError(
    4979             :                     CE_Warning, CPLE_AppDefined,
    4980             :                     "DISCARD_LSB ignored: values should be in [0,%d] range",
    4981             :                     nMaxBits);
    4982           0 :                 VSIFree(panMaskOffsetLsb);
    4983           0 :                 return nullptr;
    4984             :             }
    4985         255 :             panMaskOffsetLsb[i].nMask =
    4986         255 :                 ~((static_cast<uint64_t>(1) << nBits) - 1);
    4987         255 :             if (nBits > 1)
    4988             :             {
    4989         249 :                 panMaskOffsetLsb[i].nRoundUpBitTest = static_cast<uint64_t>(1)
    4990         249 :                                                       << (nBits - 1);
    4991             :             }
    4992         119 :         }
    4993             :     }
    4994             :     else
    4995             :     {
    4996           1 :         CPLError(CE_Warning, CPLE_AppDefined,
    4997             :                  "DISCARD_LSB ignored: wrong number of components");
    4998             :     }
    4999         120 :     return panMaskOffsetLsb;
    5000             : }
    5001             : 
    5002        5030 : void GTiffDataset::GetDiscardLsbOption(char **papszOptions)
    5003             : {
    5004        5030 :     m_panMaskOffsetLsb = ::GetDiscardLsbOption(m_hTIFF, papszOptions);
    5005        5030 : }
    5006             : 
    5007             : /************************************************************************/
    5008             : /*                             GetProfile()                             */
    5009             : /************************************************************************/
    5010             : 
    5011       12121 : static GTiffProfile GetProfile(const char *pszProfile)
    5012             : {
    5013       12121 :     GTiffProfile eProfile = GTiffProfile::GDALGEOTIFF;
    5014       12121 :     if (pszProfile != nullptr)
    5015             :     {
    5016          70 :         if (EQUAL(pszProfile, szPROFILE_BASELINE))
    5017          50 :             eProfile = GTiffProfile::BASELINE;
    5018          20 :         else if (EQUAL(pszProfile, szPROFILE_GeoTIFF))
    5019          18 :             eProfile = GTiffProfile::GEOTIFF;
    5020           2 :         else if (!EQUAL(pszProfile, szPROFILE_GDALGeoTIFF))
    5021             :         {
    5022           0 :             CPLError(CE_Warning, CPLE_NotSupported,
    5023             :                      "Unsupported value for PROFILE: %s", pszProfile);
    5024             :         }
    5025             :     }
    5026       12121 :     return eProfile;
    5027             : }
    5028             : 
    5029             : /************************************************************************/
    5030             : /*                            GTiffCreate()                             */
    5031             : /*                                                                      */
    5032             : /*      Shared functionality between GTiffDataset::Create() and         */
    5033             : /*      GTiffCreateCopy() for creating TIFF file based on a set of      */
    5034             : /*      options and a configuration.                                    */
    5035             : /************************************************************************/
    5036             : 
    5037        7063 : TIFF *GTiffDataset::CreateLL(const char *pszFilename, int nXSize, int nYSize,
    5038             :                              int l_nBands, GDALDataType eType,
    5039             :                              double dfExtraSpaceForOverviews,
    5040             :                              int nColorTableMultiplier, char **papszParamList,
    5041             :                              VSILFILE **pfpL, CPLString &l_osTmpFilename)
    5042             : 
    5043             : {
    5044        7063 :     GTiffOneTimeInit();
    5045             : 
    5046             :     /* -------------------------------------------------------------------- */
    5047             :     /*      Blow on a few errors.                                           */
    5048             :     /* -------------------------------------------------------------------- */
    5049        7063 :     if (nXSize < 1 || nYSize < 1 || l_nBands < 1)
    5050             :     {
    5051           1 :         ReportError(
    5052             :             pszFilename, CE_Failure, CPLE_AppDefined,
    5053             :             "Attempt to create %dx%dx%d TIFF file, but width, height and bands"
    5054             :             "must be positive.",
    5055             :             nXSize, nYSize, l_nBands);
    5056             : 
    5057           1 :         return nullptr;
    5058             :     }
    5059             : 
    5060        7062 :     if (l_nBands > 65535)
    5061             :     {
    5062           1 :         ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    5063             :                     "Attempt to create %dx%dx%d TIFF file, but bands "
    5064             :                     "must be lesser or equal to 65535.",
    5065             :                     nXSize, nYSize, l_nBands);
    5066             : 
    5067           1 :         return nullptr;
    5068             :     }
    5069             : 
    5070             :     /* -------------------------------------------------------------------- */
    5071             :     /*      Setup values based on options.                                  */
    5072             :     /* -------------------------------------------------------------------- */
    5073             :     const GTiffProfile eProfile =
    5074        7061 :         GetProfile(CSLFetchNameValue(papszParamList, "PROFILE"));
    5075             : 
    5076        7061 :     const bool bTiled = CPLFetchBool(papszParamList, "TILED", false);
    5077             : 
    5078        7061 :     int l_nBlockXSize = 0;
    5079        7061 :     const char *pszValue = CSLFetchNameValue(papszParamList, "BLOCKXSIZE");
    5080        7061 :     if (pszValue != nullptr)
    5081             :     {
    5082         341 :         l_nBlockXSize = atoi(pszValue);
    5083         341 :         if (l_nBlockXSize < 0)
    5084             :         {
    5085           0 :             ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
    5086             :                         "Invalid value for BLOCKXSIZE");
    5087           0 :             return nullptr;
    5088             :         }
    5089             :     }
    5090             : 
    5091        7061 :     int l_nBlockYSize = 0;
    5092        7061 :     pszValue = CSLFetchNameValue(papszParamList, "BLOCKYSIZE");
    5093        7061 :     if (pszValue != nullptr)
    5094             :     {
    5095        2410 :         l_nBlockYSize = atoi(pszValue);
    5096        2410 :         if (l_nBlockYSize < 0)
    5097             :         {
    5098           0 :             ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
    5099             :                         "Invalid value for BLOCKYSIZE");
    5100           0 :             return nullptr;
    5101             :         }
    5102             :     }
    5103             : 
    5104        7061 :     if (bTiled)
    5105             :     {
    5106         636 :         if (l_nBlockXSize == 0)
    5107         304 :             l_nBlockXSize = 256;
    5108             : 
    5109         636 :         if (l_nBlockYSize == 0)
    5110         302 :             l_nBlockYSize = 256;
    5111             :     }
    5112             : 
    5113        7061 :     int nPlanar = 0;
    5114        7061 :     pszValue = CSLFetchNameValue(papszParamList, "INTERLEAVE");
    5115        7061 :     if (pszValue != nullptr)
    5116             :     {
    5117         534 :         if (EQUAL(pszValue, "PIXEL"))
    5118         231 :             nPlanar = PLANARCONFIG_CONTIG;
    5119         303 :         else if (EQUAL(pszValue, "BAND"))
    5120             :         {
    5121         302 :             nPlanar = PLANARCONFIG_SEPARATE;
    5122             :         }
    5123             :         else
    5124             :         {
    5125           1 :             ReportError(
    5126             :                 pszFilename, CE_Failure, CPLE_IllegalArg,
    5127             :                 "INTERLEAVE=%s unsupported, value must be PIXEL or BAND.",
    5128             :                 pszValue);
    5129           1 :             return nullptr;
    5130             :         }
    5131             :     }
    5132             :     else
    5133             :     {
    5134        6527 :         nPlanar = PLANARCONFIG_CONTIG;
    5135             :     }
    5136             : 
    5137        7060 :     int l_nCompression = COMPRESSION_NONE;
    5138        7060 :     pszValue = CSLFetchNameValue(papszParamList, "COMPRESS");
    5139        7060 :     if (pszValue != nullptr)
    5140             :     {
    5141        3091 :         l_nCompression = GTIFFGetCompressionMethod(pszValue, "COMPRESS");
    5142        3091 :         if (l_nCompression < 0)
    5143           0 :             return nullptr;
    5144             :     }
    5145             : 
    5146        7060 :     constexpr int JPEG_MAX_DIMENSION = 65500;  // Defined in jpeglib.h
    5147        7060 :     constexpr int WEBP_MAX_DIMENSION = 16383;
    5148             : 
    5149             :     const struct
    5150             :     {
    5151             :         int nCodecID;
    5152             :         const char *pszCodecName;
    5153             :         int nMaxDim;
    5154        7060 :     } asLimitations[] = {
    5155             :         {COMPRESSION_JPEG, "JPEG", JPEG_MAX_DIMENSION},
    5156             :         {COMPRESSION_WEBP, "WEBP", WEBP_MAX_DIMENSION},
    5157             :     };
    5158             : 
    5159       21168 :     for (const auto &sLimitation : asLimitations)
    5160             :     {
    5161       14116 :         if (l_nCompression == sLimitation.nCodecID && !bTiled &&
    5162        2075 :             nXSize > sLimitation.nMaxDim)
    5163             :         {
    5164           2 :             ReportError(
    5165             :                 pszFilename, CE_Failure, CPLE_IllegalArg,
    5166             :                 "COMPRESS=%s is only compatible of un-tiled images whose "
    5167             :                 "width is lesser or equal to %d pixels. "
    5168             :                 "To overcome this limitation, set the TILED=YES creation "
    5169             :                 "option.",
    5170           2 :                 sLimitation.pszCodecName, sLimitation.nMaxDim);
    5171           2 :             return nullptr;
    5172             :         }
    5173       14114 :         else if (l_nCompression == sLimitation.nCodecID && bTiled &&
    5174          51 :                  l_nBlockXSize > sLimitation.nMaxDim)
    5175             :         {
    5176           2 :             ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
    5177             :                         "COMPRESS=%s is only compatible of tiled images whose "
    5178             :                         "BLOCKXSIZE is lesser or equal to %d pixels.",
    5179           2 :                         sLimitation.pszCodecName, sLimitation.nMaxDim);
    5180           2 :             return nullptr;
    5181             :         }
    5182       14112 :         else if (l_nCompression == sLimitation.nCodecID &&
    5183        2122 :                  l_nBlockYSize > sLimitation.nMaxDim)
    5184             :         {
    5185           4 :             ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
    5186             :                         "COMPRESS=%s is only compatible of images whose "
    5187             :                         "BLOCKYSIZE is lesser or equal to %d pixels. "
    5188             :                         "To overcome this limitation, set the TILED=YES "
    5189             :                         "creation option",
    5190           4 :                         sLimitation.pszCodecName, sLimitation.nMaxDim);
    5191           4 :             return nullptr;
    5192             :         }
    5193             :     }
    5194             : 
    5195             :     /* -------------------------------------------------------------------- */
    5196             :     /*      How many bits per sample?  We have a special case if NBITS      */
    5197             :     /*      specified for GDT_Byte, GDT_UInt16, GDT_UInt32.                 */
    5198             :     /* -------------------------------------------------------------------- */
    5199        7052 :     int l_nBitsPerSample = GDALGetDataTypeSizeBits(eType);
    5200        7052 :     if (CSLFetchNameValue(papszParamList, "NBITS") != nullptr)
    5201             :     {
    5202        1751 :         int nMinBits = 0;
    5203        1751 :         int nMaxBits = 0;
    5204        1751 :         l_nBitsPerSample = atoi(CSLFetchNameValue(papszParamList, "NBITS"));
    5205        1751 :         if (eType == GDT_Byte)
    5206             :         {
    5207         529 :             nMinBits = 1;
    5208         529 :             nMaxBits = 8;
    5209             :         }
    5210        1222 :         else if (eType == GDT_UInt16)
    5211             :         {
    5212        1202 :             nMinBits = 9;
    5213        1202 :             nMaxBits = 16;
    5214             :         }
    5215          20 :         else if (eType == GDT_UInt32)
    5216             :         {
    5217          14 :             nMinBits = 17;
    5218          14 :             nMaxBits = 32;
    5219             :         }
    5220           6 :         else if (eType == GDT_Float32)
    5221             :         {
    5222           6 :             if (l_nBitsPerSample != 16 && l_nBitsPerSample != 32)
    5223             :             {
    5224           1 :                 ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
    5225             :                             "Only NBITS=16 is supported for data type Float32");
    5226           1 :                 l_nBitsPerSample = GDALGetDataTypeSizeBits(eType);
    5227             :             }
    5228             :         }
    5229             :         else
    5230             :         {
    5231           0 :             ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
    5232             :                         "NBITS is not supported for data type %s",
    5233             :                         GDALGetDataTypeName(eType));
    5234           0 :             l_nBitsPerSample = GDALGetDataTypeSizeBits(eType);
    5235             :         }
    5236             : 
    5237        1751 :         if (nMinBits != 0)
    5238             :         {
    5239        1745 :             if (l_nBitsPerSample < nMinBits)
    5240             :             {
    5241           2 :                 ReportError(
    5242             :                     pszFilename, CE_Warning, CPLE_AppDefined,
    5243             :                     "NBITS=%d is invalid for data type %s. Using NBITS=%d",
    5244             :                     l_nBitsPerSample, GDALGetDataTypeName(eType), nMinBits);
    5245           2 :                 l_nBitsPerSample = nMinBits;
    5246             :             }
    5247        1743 :             else if (l_nBitsPerSample > nMaxBits)
    5248             :             {
    5249           3 :                 ReportError(
    5250             :                     pszFilename, CE_Warning, CPLE_AppDefined,
    5251             :                     "NBITS=%d is invalid for data type %s. Using NBITS=%d",
    5252             :                     l_nBitsPerSample, GDALGetDataTypeName(eType), nMaxBits);
    5253           3 :                 l_nBitsPerSample = nMaxBits;
    5254             :             }
    5255             :         }
    5256             :     }
    5257             : 
    5258             : #ifdef HAVE_JXL
    5259        7052 :     if ((l_nCompression == COMPRESSION_JXL ||
    5260         103 :          l_nCompression == COMPRESSION_JXL_DNG_1_7) &&
    5261             :         eType != GDT_Float32)
    5262             :     {
    5263             :         // Reflects tif_jxl's GetJXLDataType()
    5264          82 :         if (eType != GDT_Byte && eType != GDT_UInt16)
    5265             :         {
    5266           1 :             ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5267             :                         "Data type %s not supported for JXL compression. Only "
    5268             :                         "Byte, UInt16, Float32 are supported",
    5269             :                         GDALGetDataTypeName(eType));
    5270           2 :             return nullptr;
    5271             :         }
    5272             : 
    5273             :         const struct
    5274             :         {
    5275             :             GDALDataType eDT;
    5276             :             int nBitsPerSample;
    5277          81 :         } asSupportedDTBitsPerSample[] = {
    5278             :             {GDT_Byte, 8},
    5279             :             {GDT_UInt16, 16},
    5280             :         };
    5281             : 
    5282         241 :         for (const auto &sSupportedDTBitsPerSample : asSupportedDTBitsPerSample)
    5283             :         {
    5284         161 :             if (eType == sSupportedDTBitsPerSample.eDT &&
    5285          81 :                 l_nBitsPerSample != sSupportedDTBitsPerSample.nBitsPerSample)
    5286             :             {
    5287           1 :                 ReportError(
    5288             :                     pszFilename, CE_Failure, CPLE_NotSupported,
    5289             :                     "Bits per sample=%d not supported for JXL compression. "
    5290             :                     "Only %d is supported for %s data type.",
    5291           1 :                     l_nBitsPerSample, sSupportedDTBitsPerSample.nBitsPerSample,
    5292             :                     GDALGetDataTypeName(eType));
    5293           1 :                 return nullptr;
    5294             :             }
    5295             :         }
    5296             :     }
    5297             : #endif
    5298             : 
    5299        7050 :     int nPredictor = PREDICTOR_NONE;
    5300        7050 :     pszValue = CSLFetchNameValue(papszParamList, "PREDICTOR");
    5301        7050 :     if (pszValue != nullptr)
    5302             :     {
    5303          27 :         nPredictor = atoi(pszValue);
    5304             :     }
    5305             : 
    5306        7050 :     if (nPredictor != PREDICTOR_NONE &&
    5307          14 :         l_nCompression != COMPRESSION_ADOBE_DEFLATE &&
    5308           2 :         l_nCompression != COMPRESSION_LZW &&
    5309           2 :         l_nCompression != COMPRESSION_LZMA &&
    5310             :         l_nCompression != COMPRESSION_ZSTD)
    5311             :     {
    5312           1 :         ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
    5313             :                     "PREDICTOR option is ignored for COMPRESS=%s. "
    5314             :                     "Only valid for DEFLATE, LZW, LZMA or ZSTD",
    5315             :                     CSLFetchNameValueDef(papszParamList, "COMPRESS", "NONE"));
    5316             :     }
    5317             : 
    5318             :     // Do early checks as libtiff will only error out when starting to write.
    5319        7075 :     else if (nPredictor != PREDICTOR_NONE &&
    5320          26 :              CPLTestBool(
    5321             :                  CPLGetConfigOption("GDAL_GTIFF_PREDICTOR_CHECKS", "YES")))
    5322             :     {
    5323             : #if (TIFFLIB_VERSION > 20210416) || defined(INTERNAL_LIBTIFF)
    5324             : #define HAVE_PREDICTOR_2_FOR_64BIT
    5325             : #endif
    5326          26 :         if (nPredictor == 2)
    5327             :         {
    5328          22 :             if (l_nBitsPerSample != 8 && l_nBitsPerSample != 16 &&
    5329             :                 l_nBitsPerSample != 32
    5330             : #ifdef HAVE_PREDICTOR_2_FOR_64BIT
    5331           2 :                 && l_nBitsPerSample != 64
    5332             : #endif
    5333             :             )
    5334             :             {
    5335             : #if !defined(HAVE_PREDICTOR_2_FOR_64BIT)
    5336             :                 if (l_nBitsPerSample == 64)
    5337             :                 {
    5338             :                     ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    5339             :                                 "PREDICTOR=2 is supported on 64 bit samples "
    5340             :                                 "starting with libtiff > 4.3.0.");
    5341             :                 }
    5342             :                 else
    5343             : #endif
    5344             :                 {
    5345           2 :                     const int nBITSHint = (l_nBitsPerSample < 8)    ? 8
    5346           1 :                                           : (l_nBitsPerSample < 16) ? 16
    5347           0 :                                           : (l_nBitsPerSample < 32) ? 32
    5348             :                                                                     : 64;
    5349           1 :                     ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    5350             : #ifdef HAVE_PREDICTOR_2_FOR_64BIT
    5351             :                                 "PREDICTOR=2 is only supported with 8/16/32/64 "
    5352             :                                 "bit samples. You can specify the NBITS=%d "
    5353             :                                 "creation option to promote to the closest "
    5354             :                                 "supported bits per sample value.",
    5355             : #else
    5356             :                                 "PREDICTOR=2 is only supported with 8/16/32 "
    5357             :                                 "bit samples. You can specify the NBITS=%d "
    5358             :                                 "creation option to promote to the closest "
    5359             :                                 "supported bits per sample value.",
    5360             : #endif
    5361             :                                 nBITSHint);
    5362             :                 }
    5363           1 :                 return nullptr;
    5364             :             }
    5365             :         }
    5366           4 :         else if (nPredictor == 3)
    5367             :         {
    5368           3 :             if (eType != GDT_Float32 && eType != GDT_Float64)
    5369             :             {
    5370           1 :                 ReportError(
    5371             :                     pszFilename, CE_Failure, CPLE_AppDefined,
    5372             :                     "PREDICTOR=3 is only supported with Float32 or Float64.");
    5373           1 :                 return nullptr;
    5374             :             }
    5375             :         }
    5376             :         else
    5377             :         {
    5378           1 :             ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    5379             :                         "PREDICTOR=%s is not supported.", pszValue);
    5380           1 :             return nullptr;
    5381             :         }
    5382             :     }
    5383             : 
    5384        7047 :     const int l_nZLevel = GTiffGetZLevel(papszParamList);
    5385        7047 :     const int l_nLZMAPreset = GTiffGetLZMAPreset(papszParamList);
    5386        7047 :     const int l_nZSTDLevel = GTiffGetZSTDPreset(papszParamList);
    5387        7047 :     const int l_nWebPLevel = GTiffGetWebPLevel(papszParamList);
    5388        7047 :     const bool l_bWebPLossless = GTiffGetWebPLossless(papszParamList);
    5389        7047 :     const int l_nJpegQuality = GTiffGetJpegQuality(papszParamList);
    5390        7047 :     const int l_nJpegTablesMode = GTiffGetJpegTablesMode(papszParamList);
    5391        7047 :     const double l_dfMaxZError = GTiffGetLERCMaxZError(papszParamList);
    5392             : #if HAVE_JXL
    5393        7047 :     const bool l_bJXLLossless = GTiffGetJXLLossless(papszParamList);
    5394        7047 :     const uint32_t l_nJXLEffort = GTiffGetJXLEffort(papszParamList);
    5395        7047 :     const float l_fJXLDistance = GTiffGetJXLDistance(papszParamList);
    5396        7047 :     const float l_fJXLAlphaDistance = GTiffGetJXLAlphaDistance(papszParamList);
    5397             : #endif
    5398             :     /* -------------------------------------------------------------------- */
    5399             :     /*      Streaming related code                                          */
    5400             :     /* -------------------------------------------------------------------- */
    5401       14094 :     const CPLString osOriFilename(pszFilename);
    5402       14094 :     bool bStreaming = strcmp(pszFilename, "/vsistdout/") == 0 ||
    5403        7047 :                       CPLFetchBool(papszParamList, "STREAMABLE_OUTPUT", false);
    5404             : #ifdef S_ISFIFO
    5405        7047 :     if (!bStreaming)
    5406             :     {
    5407             :         VSIStatBufL sStat;
    5408        7035 :         if (VSIStatExL(pszFilename, &sStat,
    5409        7913 :                        VSI_STAT_EXISTS_FLAG | VSI_STAT_NATURE_FLAG) == 0 &&
    5410         878 :             S_ISFIFO(sStat.st_mode))
    5411             :         {
    5412           0 :             bStreaming = true;
    5413             :         }
    5414             :     }
    5415             : #endif
    5416        7047 :     if (bStreaming && !EQUAL("NONE", CSLFetchNameValueDef(papszParamList,
    5417             :                                                           "COMPRESS", "NONE")))
    5418             :     {
    5419           1 :         ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5420             :                     "Streaming only supported to uncompressed TIFF");
    5421           1 :         return nullptr;
    5422             :     }
    5423        7046 :     if (bStreaming && CPLFetchBool(papszParamList, "SPARSE_OK", false))
    5424             :     {
    5425           1 :         ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5426             :                     "Streaming not supported with SPARSE_OK");
    5427           1 :         return nullptr;
    5428             :     }
    5429             :     const bool bCopySrcOverviews =
    5430        7045 :         CPLFetchBool(papszParamList, "COPY_SRC_OVERVIEWS", false);
    5431        7045 :     if (bStreaming && bCopySrcOverviews)
    5432             :     {
    5433           1 :         ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5434             :                     "Streaming not supported with COPY_SRC_OVERVIEWS");
    5435           1 :         return nullptr;
    5436             :     }
    5437        7044 :     if (bStreaming)
    5438             :     {
    5439           9 :         l_osTmpFilename = VSIMemGenerateHiddenFilename("vsistdout.tif");
    5440           9 :         pszFilename = l_osTmpFilename.c_str();
    5441             :     }
    5442             : 
    5443             :     /* -------------------------------------------------------------------- */
    5444             :     /*      Compute the uncompressed size.                                  */
    5445             :     /* -------------------------------------------------------------------- */
    5446        7044 :     const unsigned nTileXCount =
    5447        7044 :         bTiled ? DIV_ROUND_UP(nXSize, l_nBlockXSize) : 0;
    5448        7044 :     const unsigned nTileYCount =
    5449        7044 :         bTiled ? DIV_ROUND_UP(nYSize, l_nBlockYSize) : 0;
    5450             :     const double dfUncompressedImageSize =
    5451        7044 :         (bTiled ? (static_cast<double>(nTileXCount) * nTileYCount *
    5452         632 :                    l_nBlockXSize * l_nBlockYSize)
    5453        6412 :                 : (nXSize * static_cast<double>(nYSize))) *
    5454        7044 :             l_nBands * GDALGetDataTypeSizeBytes(eType) +
    5455        7044 :         dfExtraSpaceForOverviews;
    5456             : 
    5457             :     /* -------------------------------------------------------------------- */
    5458             :     /*      Should the file be created as a bigtiff file?                   */
    5459             :     /* -------------------------------------------------------------------- */
    5460        7044 :     const char *pszBIGTIFF = CSLFetchNameValue(papszParamList, "BIGTIFF");
    5461             : 
    5462        7044 :     if (pszBIGTIFF == nullptr)
    5463        6670 :         pszBIGTIFF = "IF_NEEDED";
    5464             : 
    5465        7044 :     bool bCreateBigTIFF = false;
    5466        7044 :     if (EQUAL(pszBIGTIFF, "IF_NEEDED"))
    5467             :     {
    5468        6671 :         if (l_nCompression == COMPRESSION_NONE &&
    5469             :             dfUncompressedImageSize > 4200000000.0)
    5470          16 :             bCreateBigTIFF = true;
    5471             :     }
    5472         373 :     else if (EQUAL(pszBIGTIFF, "IF_SAFER"))
    5473             :     {
    5474         355 :         if (dfUncompressedImageSize > 2000000000.0)
    5475           1 :             bCreateBigTIFF = true;
    5476             :     }
    5477             :     else
    5478             :     {
    5479          18 :         bCreateBigTIFF = CPLTestBool(pszBIGTIFF);
    5480          18 :         if (!bCreateBigTIFF && l_nCompression == COMPRESSION_NONE &&
    5481             :             dfUncompressedImageSize > 4200000000.0)
    5482             :         {
    5483           2 :             ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5484             :                         "The TIFF file will be larger than 4GB, so BigTIFF is "
    5485             :                         "necessary.  Creation failed.");
    5486           2 :             return nullptr;
    5487             :         }
    5488             :     }
    5489             : 
    5490        7042 :     if (bCreateBigTIFF)
    5491          31 :         CPLDebug("GTiff", "File being created as a BigTIFF.");
    5492             : 
    5493             :     /* -------------------------------------------------------------------- */
    5494             :     /*      Sanity check.                                                   */
    5495             :     /* -------------------------------------------------------------------- */
    5496        7042 :     if (bTiled)
    5497             :     {
    5498             :         // libtiff implementation limitation
    5499         632 :         if (nTileXCount > 0x80000000U / (bCreateBigTIFF ? 8 : 4) / nTileYCount)
    5500             :         {
    5501           1 :             ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5502             :                         "File too large regarding tile size. This would result "
    5503             :                         "in a file with tile arrays larger than 2GB");
    5504           1 :             return nullptr;
    5505             :         }
    5506             :     }
    5507             : 
    5508             :     /* -------------------------------------------------------------------- */
    5509             :     /*      Check free space (only for big, non sparse, uncompressed)       */
    5510             :     /* -------------------------------------------------------------------- */
    5511        3968 :     if (l_nCompression == COMPRESSION_NONE && dfUncompressedImageSize >= 1e9 &&
    5512          18 :         !CPLFetchBool(papszParamList, "SPARSE_OK", false) &&
    5513           3 :         osOriFilename != "/vsistdout/" &&
    5514       11012 :         osOriFilename != "/vsistdout_redirect/" &&
    5515           3 :         CPLTestBool(CPLGetConfigOption("CHECK_DISK_FREE_SPACE", "TRUE")))
    5516             :     {
    5517             :         GIntBig nFreeDiskSpace =
    5518           2 :             VSIGetDiskFreeSpace(CPLGetDirnameSafe(pszFilename).c_str());
    5519           2 :         if (nFreeDiskSpace >= 0 && nFreeDiskSpace < dfUncompressedImageSize)
    5520             :         {
    5521           1 :             ReportError(pszFilename, CE_Failure, CPLE_FileIO,
    5522             :                         "Free disk space available is " CPL_FRMT_GIB " bytes, "
    5523             :                         "whereas " CPL_FRMT_GIB " are at least necessary. "
    5524             :                         "You can disable this check by defining the "
    5525             :                         "CHECK_DISK_FREE_SPACE configuration option to FALSE.",
    5526             :                         nFreeDiskSpace,
    5527             :                         static_cast<GIntBig>(dfUncompressedImageSize));
    5528           1 :             return nullptr;
    5529             :         }
    5530             :     }
    5531             : 
    5532             :     /* -------------------------------------------------------------------- */
    5533             :     /*      Check if the user wishes a particular endianness                */
    5534             :     /* -------------------------------------------------------------------- */
    5535             : 
    5536        7040 :     int eEndianness = ENDIANNESS_NATIVE;
    5537        7040 :     pszValue = CSLFetchNameValue(papszParamList, "ENDIANNESS");
    5538        7040 :     if (pszValue == nullptr)
    5539        6977 :         pszValue = CPLGetConfigOption("GDAL_TIFF_ENDIANNESS", nullptr);
    5540        7040 :     if (pszValue != nullptr)
    5541             :     {
    5542         123 :         if (EQUAL(pszValue, "LITTLE"))
    5543             :         {
    5544          36 :             eEndianness = ENDIANNESS_LITTLE;
    5545             :         }
    5546          87 :         else if (EQUAL(pszValue, "BIG"))
    5547             :         {
    5548           1 :             eEndianness = ENDIANNESS_BIG;
    5549             :         }
    5550          86 :         else if (EQUAL(pszValue, "INVERTED"))
    5551             :         {
    5552             : #ifdef CPL_LSB
    5553          82 :             eEndianness = ENDIANNESS_BIG;
    5554             : #else
    5555             :             eEndianness = ENDIANNESS_LITTLE;
    5556             : #endif
    5557             :         }
    5558           4 :         else if (!EQUAL(pszValue, "NATIVE"))
    5559             :         {
    5560           1 :             ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
    5561             :                         "ENDIANNESS=%s not supported. Defaulting to NATIVE",
    5562             :                         pszValue);
    5563             :         }
    5564             :     }
    5565             : 
    5566             :     /* -------------------------------------------------------------------- */
    5567             :     /*      Try opening the dataset.                                        */
    5568             :     /* -------------------------------------------------------------------- */
    5569             : 
    5570             :     const bool bAppend =
    5571        7040 :         CPLFetchBool(papszParamList, "APPEND_SUBDATASET", false);
    5572             : 
    5573        7040 :     char szOpeningFlag[5] = {};
    5574        7040 :     strcpy(szOpeningFlag, bAppend ? "r+" : "w+");
    5575        7040 :     if (bCreateBigTIFF)
    5576          29 :         strcat(szOpeningFlag, "8");
    5577        7040 :     if (eEndianness == ENDIANNESS_BIG)
    5578          83 :         strcat(szOpeningFlag, "b");
    5579        6957 :     else if (eEndianness == ENDIANNESS_LITTLE)
    5580          36 :         strcat(szOpeningFlag, "l");
    5581             : 
    5582        7040 :     VSIErrorReset();
    5583        7040 :     VSILFILE *l_fpL = VSIFOpenExL(pszFilename, bAppend ? "r+b" : "w+b", true);
    5584        7040 :     if (l_fpL == nullptr)
    5585             :     {
    5586          15 :         VSIToCPLErrorWithMsg(CE_Failure, CPLE_OpenFailed,
    5587          30 :                              std::string("Attempt to create new tiff file `")
    5588          15 :                                  .append(pszFilename)
    5589          15 :                                  .append("' failed")
    5590             :                                  .c_str());
    5591          15 :         return nullptr;
    5592             :     }
    5593        7025 :     TIFF *l_hTIFF = VSI_TIFFOpen(pszFilename, szOpeningFlag, l_fpL);
    5594        7025 :     if (l_hTIFF == nullptr)
    5595             :     {
    5596           2 :         if (CPLGetLastErrorNo() == 0)
    5597           0 :             CPLError(CE_Failure, CPLE_OpenFailed,
    5598             :                      "Attempt to create new tiff file `%s' "
    5599             :                      "failed in XTIFFOpen().",
    5600             :                      pszFilename);
    5601           2 :         CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    5602           2 :         return nullptr;
    5603             :     }
    5604             : 
    5605        7023 :     if (bAppend)
    5606             :     {
    5607             :         // This is a bit of a hack to cause (*tif->tif_cleanup)(tif); to be
    5608             :         // called. See https://trac.osgeo.org/gdal/ticket/2055
    5609           5 :         TIFFSetField(l_hTIFF, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
    5610           5 :         TIFFFreeDirectory(l_hTIFF);
    5611           5 :         TIFFCreateDirectory(l_hTIFF);
    5612             :     }
    5613             : 
    5614             :     /* -------------------------------------------------------------------- */
    5615             :     /*      Do we have a custom pixel type (just used for signed byte now). */
    5616             :     /* -------------------------------------------------------------------- */
    5617        7023 :     const char *pszPixelType = CSLFetchNameValue(papszParamList, "PIXELTYPE");
    5618        7023 :     if (pszPixelType == nullptr)
    5619        7015 :         pszPixelType = "";
    5620        7023 :     if (eType == GDT_Byte && EQUAL(pszPixelType, "SIGNEDBYTE"))
    5621             :     {
    5622           8 :         CPLError(CE_Warning, CPLE_AppDefined,
    5623             :                  "Using PIXELTYPE=SIGNEDBYTE with Byte data type is deprecated "
    5624             :                  "(but still works). "
    5625             :                  "Using Int8 data type instead is now recommended.");
    5626             :     }
    5627             : 
    5628             :     /* -------------------------------------------------------------------- */
    5629             :     /*      Setup some standard flags.                                      */
    5630             :     /* -------------------------------------------------------------------- */
    5631        7023 :     TIFFSetField(l_hTIFF, TIFFTAG_IMAGEWIDTH, nXSize);
    5632        7023 :     TIFFSetField(l_hTIFF, TIFFTAG_IMAGELENGTH, nYSize);
    5633        7023 :     TIFFSetField(l_hTIFF, TIFFTAG_BITSPERSAMPLE, l_nBitsPerSample);
    5634             : 
    5635        7023 :     uint16_t l_nSampleFormat = 0;
    5636        7023 :     if ((eType == GDT_Byte && EQUAL(pszPixelType, "SIGNEDBYTE")) ||
    5637        6995 :         eType == GDT_Int8 || eType == GDT_Int16 || eType == GDT_Int32 ||
    5638             :         eType == GDT_Int64)
    5639         301 :         l_nSampleFormat = SAMPLEFORMAT_INT;
    5640        6722 :     else if (eType == GDT_CInt16 || eType == GDT_CInt32)
    5641         133 :         l_nSampleFormat = SAMPLEFORMAT_COMPLEXINT;
    5642        6589 :     else if (eType == GDT_Float32 || eType == GDT_Float64)
    5643         516 :         l_nSampleFormat = SAMPLEFORMAT_IEEEFP;
    5644        6073 :     else if (eType == GDT_CFloat32 || eType == GDT_CFloat64)
    5645         128 :         l_nSampleFormat = SAMPLEFORMAT_COMPLEXIEEEFP;
    5646             :     else
    5647        5945 :         l_nSampleFormat = SAMPLEFORMAT_UINT;
    5648             : 
    5649        7023 :     TIFFSetField(l_hTIFF, TIFFTAG_SAMPLEFORMAT, l_nSampleFormat);
    5650        7023 :     TIFFSetField(l_hTIFF, TIFFTAG_SAMPLESPERPIXEL, l_nBands);
    5651        7023 :     TIFFSetField(l_hTIFF, TIFFTAG_PLANARCONFIG, nPlanar);
    5652             : 
    5653             :     /* -------------------------------------------------------------------- */
    5654             :     /*      Setup Photometric Interpretation. Take this value from the user */
    5655             :     /*      passed option or guess correct value otherwise.                 */
    5656             :     /* -------------------------------------------------------------------- */
    5657        7023 :     int nSamplesAccountedFor = 1;
    5658        7023 :     bool bForceColorTable = false;
    5659             : 
    5660        7023 :     pszValue = CSLFetchNameValue(papszParamList, "PHOTOMETRIC");
    5661        7023 :     if (pszValue != nullptr)
    5662             :     {
    5663        1872 :         if (EQUAL(pszValue, "MINISBLACK"))
    5664          14 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    5665        1858 :         else if (EQUAL(pszValue, "MINISWHITE"))
    5666             :         {
    5667           2 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
    5668             :         }
    5669        1856 :         else if (EQUAL(pszValue, "PALETTE"))
    5670             :         {
    5671           5 :             if (eType == GDT_Byte || eType == GDT_UInt16)
    5672             :             {
    5673           4 :                 TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
    5674           4 :                 nSamplesAccountedFor = 1;
    5675           4 :                 bForceColorTable = true;
    5676             :             }
    5677             :             else
    5678             :             {
    5679           1 :                 ReportError(
    5680             :                     pszFilename, CE_Warning, CPLE_AppDefined,
    5681             :                     "PHOTOMETRIC=PALETTE only compatible with Byte or UInt16");
    5682             :             }
    5683             :         }
    5684        1851 :         else if (EQUAL(pszValue, "RGB"))
    5685             :         {
    5686        1107 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
    5687        1107 :             nSamplesAccountedFor = 3;
    5688             :         }
    5689         744 :         else if (EQUAL(pszValue, "CMYK"))
    5690             :         {
    5691          10 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_SEPARATED);
    5692          10 :             nSamplesAccountedFor = 4;
    5693             :         }
    5694         734 :         else if (EQUAL(pszValue, "YCBCR"))
    5695             :         {
    5696             :             // Because of subsampling, setting YCBCR without JPEG compression
    5697             :             // leads to a crash currently. Would need to make
    5698             :             // GTiffRasterBand::IWriteBlock() aware of subsampling so that it
    5699             :             // doesn't overrun buffer size returned by libtiff.
    5700         733 :             if (l_nCompression != COMPRESSION_JPEG)
    5701             :             {
    5702           1 :                 ReportError(
    5703             :                     pszFilename, CE_Failure, CPLE_NotSupported,
    5704             :                     "Currently, PHOTOMETRIC=YCBCR requires COMPRESS=JPEG");
    5705           1 :                 XTIFFClose(l_hTIFF);
    5706           1 :                 CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    5707           1 :                 return nullptr;
    5708             :             }
    5709             : 
    5710         732 :             if (nPlanar == PLANARCONFIG_SEPARATE)
    5711             :             {
    5712           1 :                 ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    5713             :                             "PHOTOMETRIC=YCBCR requires INTERLEAVE=PIXEL");
    5714           1 :                 XTIFFClose(l_hTIFF);
    5715           1 :                 CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    5716           1 :                 return nullptr;
    5717             :             }
    5718             : 
    5719             :             // YCBCR strictly requires 3 bands. Not less, not more Issue an
    5720             :             // explicit error message as libtiff one is a bit cryptic:
    5721             :             // TIFFVStripSize64:Invalid td_samplesperpixel value.
    5722         731 :             if (l_nBands != 3)
    5723             :             {
    5724           1 :                 ReportError(
    5725             :                     pszFilename, CE_Failure, CPLE_NotSupported,
    5726             :                     "PHOTOMETRIC=YCBCR not supported on a %d-band raster: "
    5727             :                     "only compatible of a 3-band (RGB) raster",
    5728             :                     l_nBands);
    5729           1 :                 XTIFFClose(l_hTIFF);
    5730           1 :                 CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    5731           1 :                 return nullptr;
    5732             :             }
    5733             : 
    5734         730 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);
    5735         730 :             nSamplesAccountedFor = 3;
    5736             : 
    5737             :             // Explicitly register the subsampling so that JPEGFixupTags
    5738             :             // is a no-op (helps for cloud optimized geotiffs)
    5739         730 :             TIFFSetField(l_hTIFF, TIFFTAG_YCBCRSUBSAMPLING, 2, 2);
    5740             :         }
    5741           1 :         else if (EQUAL(pszValue, "CIELAB"))
    5742             :         {
    5743           0 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_CIELAB);
    5744           0 :             nSamplesAccountedFor = 3;
    5745             :         }
    5746           1 :         else if (EQUAL(pszValue, "ICCLAB"))
    5747             :         {
    5748           0 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_ICCLAB);
    5749           0 :             nSamplesAccountedFor = 3;
    5750             :         }
    5751           1 :         else if (EQUAL(pszValue, "ITULAB"))
    5752             :         {
    5753           0 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_ITULAB);
    5754           0 :             nSamplesAccountedFor = 3;
    5755             :         }
    5756             :         else
    5757             :         {
    5758           1 :             ReportError(pszFilename, CE_Warning, CPLE_IllegalArg,
    5759             :                         "PHOTOMETRIC=%s value not recognised, ignoring.  "
    5760             :                         "Set the Photometric Interpretation as MINISBLACK.",
    5761             :                         pszValue);
    5762           1 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    5763             :         }
    5764             : 
    5765        1869 :         if (l_nBands < nSamplesAccountedFor)
    5766             :         {
    5767           1 :             ReportError(pszFilename, CE_Warning, CPLE_IllegalArg,
    5768             :                         "PHOTOMETRIC=%s value does not correspond to number "
    5769             :                         "of bands (%d), ignoring.  "
    5770             :                         "Set the Photometric Interpretation as MINISBLACK.",
    5771             :                         pszValue, l_nBands);
    5772           1 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    5773             :         }
    5774             :     }
    5775             :     else
    5776             :     {
    5777             :         // If image contains 3 or 4 bands and datatype is Byte then we will
    5778             :         // assume it is RGB. In all other cases assume it is MINISBLACK.
    5779        5151 :         if (l_nBands == 3 && eType == GDT_Byte)
    5780             :         {
    5781         252 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
    5782         252 :             nSamplesAccountedFor = 3;
    5783             :         }
    5784        4899 :         else if (l_nBands == 4 && eType == GDT_Byte)
    5785             :         {
    5786             :             uint16_t v[1] = {
    5787         717 :                 GTiffGetAlphaValue(CSLFetchNameValue(papszParamList, "ALPHA"),
    5788         717 :                                    DEFAULT_ALPHA_TYPE)};
    5789             : 
    5790         717 :             TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, 1, v);
    5791         717 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
    5792         717 :             nSamplesAccountedFor = 4;
    5793             :         }
    5794             :         else
    5795             :         {
    5796        4182 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    5797        4182 :             nSamplesAccountedFor = 1;
    5798             :         }
    5799             :     }
    5800             : 
    5801             :     /* -------------------------------------------------------------------- */
    5802             :     /*      If there are extra samples, we need to mark them with an        */
    5803             :     /*      appropriate extrasamples definition here.                       */
    5804             :     /* -------------------------------------------------------------------- */
    5805        7020 :     if (l_nBands > nSamplesAccountedFor)
    5806             :     {
    5807        1141 :         const int nExtraSamples = l_nBands - nSamplesAccountedFor;
    5808             : 
    5809             :         uint16_t *v = static_cast<uint16_t *>(
    5810        1141 :             CPLMalloc(sizeof(uint16_t) * nExtraSamples));
    5811             : 
    5812        1141 :         v[0] = GTiffGetAlphaValue(CSLFetchNameValue(papszParamList, "ALPHA"),
    5813             :                                   EXTRASAMPLE_UNSPECIFIED);
    5814             : 
    5815      198819 :         for (int i = 1; i < nExtraSamples; ++i)
    5816      197678 :             v[i] = EXTRASAMPLE_UNSPECIFIED;
    5817             : 
    5818        1141 :         TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, nExtraSamples, v);
    5819             : 
    5820        1141 :         CPLFree(v);
    5821             :     }
    5822             : 
    5823             :     // Set the ICC color profile.
    5824        7020 :     if (eProfile != GTiffProfile::BASELINE)
    5825             :     {
    5826        6995 :         SaveICCProfile(nullptr, l_hTIFF, papszParamList, l_nBitsPerSample);
    5827             :     }
    5828             : 
    5829             :     // Set the compression method before asking the default strip size
    5830             :     // This is useful when translating to a JPEG-In-TIFF file where
    5831             :     // the default strip size is 8 or 16 depending on the photometric value.
    5832        7020 :     TIFFSetField(l_hTIFF, TIFFTAG_COMPRESSION, l_nCompression);
    5833             : 
    5834        7020 :     if (l_nCompression == COMPRESSION_LERC)
    5835             :     {
    5836             :         const char *pszCompress =
    5837          97 :             CSLFetchNameValueDef(papszParamList, "COMPRESS", "");
    5838          97 :         if (EQUAL(pszCompress, "LERC_DEFLATE"))
    5839             :         {
    5840          16 :             TIFFSetField(l_hTIFF, TIFFTAG_LERC_ADD_COMPRESSION,
    5841             :                          LERC_ADD_COMPRESSION_DEFLATE);
    5842             :         }
    5843          81 :         else if (EQUAL(pszCompress, "LERC_ZSTD"))
    5844             :         {
    5845          14 :             if (TIFFSetField(l_hTIFF, TIFFTAG_LERC_ADD_COMPRESSION,
    5846          14 :                              LERC_ADD_COMPRESSION_ZSTD) != 1)
    5847             :             {
    5848           0 :                 XTIFFClose(l_hTIFF);
    5849           0 :                 CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    5850           0 :                 return nullptr;
    5851             :             }
    5852             :         }
    5853             :     }
    5854             :     // TODO later: take into account LERC version
    5855             : 
    5856             :     /* -------------------------------------------------------------------- */
    5857             :     /*      Setup tiling/stripping flags.                                   */
    5858             :     /* -------------------------------------------------------------------- */
    5859        7020 :     if (bTiled)
    5860             :     {
    5861        1256 :         if (!TIFFSetField(l_hTIFF, TIFFTAG_TILEWIDTH, l_nBlockXSize) ||
    5862         628 :             !TIFFSetField(l_hTIFF, TIFFTAG_TILELENGTH, l_nBlockYSize))
    5863             :         {
    5864           1 :             XTIFFClose(l_hTIFF);
    5865           1 :             CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    5866           1 :             return nullptr;
    5867             :         }
    5868             :     }
    5869             :     else
    5870             :     {
    5871        6392 :         const uint32_t l_nRowsPerStrip = std::min(
    5872             :             nYSize, l_nBlockYSize == 0
    5873        6392 :                         ? static_cast<int>(TIFFDefaultStripSize(l_hTIFF, 0))
    5874        6392 :                         : l_nBlockYSize);
    5875             : 
    5876        6392 :         TIFFSetField(l_hTIFF, TIFFTAG_ROWSPERSTRIP, l_nRowsPerStrip);
    5877             :     }
    5878             : 
    5879             :     /* -------------------------------------------------------------------- */
    5880             :     /*      Set compression related tags.                                   */
    5881             :     /* -------------------------------------------------------------------- */
    5882        7019 :     if (GTIFFSupportsPredictor(l_nCompression))
    5883         720 :         TIFFSetField(l_hTIFF, TIFFTAG_PREDICTOR, nPredictor);
    5884        7019 :     if (l_nCompression == COMPRESSION_ADOBE_DEFLATE ||
    5885             :         l_nCompression == COMPRESSION_LERC)
    5886             :     {
    5887         259 :         GTiffSetDeflateSubCodec(l_hTIFF);
    5888             : 
    5889         259 :         if (l_nZLevel != -1)
    5890          22 :             TIFFSetField(l_hTIFF, TIFFTAG_ZIPQUALITY, l_nZLevel);
    5891             :     }
    5892        7019 :     if (l_nCompression == COMPRESSION_JPEG && l_nJpegQuality != -1)
    5893        1905 :         TIFFSetField(l_hTIFF, TIFFTAG_JPEGQUALITY, l_nJpegQuality);
    5894        7019 :     if (l_nCompression == COMPRESSION_LZMA && l_nLZMAPreset != -1)
    5895          10 :         TIFFSetField(l_hTIFF, TIFFTAG_LZMAPRESET, l_nLZMAPreset);
    5896        7019 :     if ((l_nCompression == COMPRESSION_ZSTD ||
    5897         134 :          l_nCompression == COMPRESSION_LERC) &&
    5898             :         l_nZSTDLevel != -1)
    5899          12 :         TIFFSetField(l_hTIFF, TIFFTAG_ZSTD_LEVEL, l_nZSTDLevel);
    5900        7019 :     if (l_nCompression == COMPRESSION_LERC)
    5901             :     {
    5902          97 :         TIFFSetField(l_hTIFF, TIFFTAG_LERC_MAXZERROR, l_dfMaxZError);
    5903             :     }
    5904             : #if HAVE_JXL
    5905        7019 :     if (l_nCompression == COMPRESSION_JXL ||
    5906             :         l_nCompression == COMPRESSION_JXL_DNG_1_7)
    5907             :     {
    5908         101 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_LOSSYNESS,
    5909             :                      l_bJXLLossless ? JXL_LOSSLESS : JXL_LOSSY);
    5910         101 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_EFFORT, l_nJXLEffort);
    5911         101 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_DISTANCE, l_fJXLDistance);
    5912         101 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_ALPHA_DISTANCE, l_fJXLAlphaDistance);
    5913             :     }
    5914             : #endif
    5915        7019 :     if (l_nCompression == COMPRESSION_WEBP)
    5916          33 :         TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LEVEL, l_nWebPLevel);
    5917        7019 :     if (l_nCompression == COMPRESSION_WEBP && l_bWebPLossless)
    5918           7 :         TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LOSSLESS, 1);
    5919             : 
    5920        7019 :     if (l_nCompression == COMPRESSION_JPEG)
    5921        2083 :         TIFFSetField(l_hTIFF, TIFFTAG_JPEGTABLESMODE, l_nJpegTablesMode);
    5922             : 
    5923             :     /* -------------------------------------------------------------------- */
    5924             :     /*      If we forced production of a file with photometric=palette,     */
    5925             :     /*      we need to push out a default color table.                      */
    5926             :     /* -------------------------------------------------------------------- */
    5927        7019 :     if (bForceColorTable)
    5928             :     {
    5929           4 :         const int nColors = eType == GDT_Byte ? 256 : 65536;
    5930             : 
    5931             :         unsigned short *panTRed = static_cast<unsigned short *>(
    5932           4 :             CPLMalloc(sizeof(unsigned short) * nColors));
    5933             :         unsigned short *panTGreen = static_cast<unsigned short *>(
    5934           4 :             CPLMalloc(sizeof(unsigned short) * nColors));
    5935             :         unsigned short *panTBlue = static_cast<unsigned short *>(
    5936           4 :             CPLMalloc(sizeof(unsigned short) * nColors));
    5937             : 
    5938        1028 :         for (int iColor = 0; iColor < nColors; ++iColor)
    5939             :         {
    5940        1024 :             if (eType == GDT_Byte)
    5941             :             {
    5942        1024 :                 panTRed[iColor] = GTiffDataset::ClampCTEntry(
    5943             :                     iColor, 1, iColor, nColorTableMultiplier);
    5944        1024 :                 panTGreen[iColor] = GTiffDataset::ClampCTEntry(
    5945             :                     iColor, 2, iColor, nColorTableMultiplier);
    5946        1024 :                 panTBlue[iColor] = GTiffDataset::ClampCTEntry(
    5947             :                     iColor, 3, iColor, nColorTableMultiplier);
    5948             :             }
    5949             :             else
    5950             :             {
    5951           0 :                 panTRed[iColor] = static_cast<unsigned short>(iColor);
    5952           0 :                 panTGreen[iColor] = static_cast<unsigned short>(iColor);
    5953           0 :                 panTBlue[iColor] = static_cast<unsigned short>(iColor);
    5954             :             }
    5955             :         }
    5956             : 
    5957           4 :         TIFFSetField(l_hTIFF, TIFFTAG_COLORMAP, panTRed, panTGreen, panTBlue);
    5958             : 
    5959           4 :         CPLFree(panTRed);
    5960           4 :         CPLFree(panTGreen);
    5961           4 :         CPLFree(panTBlue);
    5962             :     }
    5963             : 
    5964             :     // This trick
    5965             :     // creates a temporary in-memory file and fetches its JPEG tables so that
    5966             :     // we can directly set them, before tif_jpeg.c compute them at the first
    5967             :     // strip/tile writing, which is too late, since we have already crystalized
    5968             :     // the directory. This way we avoid a directory rewriting.
    5969        9102 :     if (l_nCompression == COMPRESSION_JPEG &&
    5970        2083 :         CPLTestBool(
    5971             :             CSLFetchNameValueDef(papszParamList, "WRITE_JPEGTABLE_TAG", "YES")))
    5972             :     {
    5973        1012 :         GTiffWriteJPEGTables(
    5974             :             l_hTIFF, CSLFetchNameValue(papszParamList, "PHOTOMETRIC"),
    5975             :             CSLFetchNameValue(papszParamList, "JPEG_QUALITY"),
    5976             :             CSLFetchNameValue(papszParamList, "JPEGTABLESMODE"));
    5977             :     }
    5978             : 
    5979        7019 :     *pfpL = l_fpL;
    5980             : 
    5981        7019 :     return l_hTIFF;
    5982             : }
    5983             : 
    5984             : /************************************************************************/
    5985             : /*                            GuessJPEGQuality()                        */
    5986             : /*                                                                      */
    5987             : /*      Guess JPEG quality from JPEGTABLES tag.                         */
    5988             : /************************************************************************/
    5989             : 
    5990        3895 : static const GByte *GTIFFFindNextTable(const GByte *paby, GByte byMarker,
    5991             :                                        int nLen, int *pnLenTable)
    5992             : {
    5993        8057 :     for (int i = 0; i + 1 < nLen;)
    5994             :     {
    5995        8057 :         if (paby[i] != 0xFF)
    5996           0 :             return nullptr;
    5997        8057 :         ++i;
    5998        8057 :         if (paby[i] == 0xD8)
    5999             :         {
    6000        3141 :             ++i;
    6001        3141 :             continue;
    6002             :         }
    6003        4916 :         if (i + 2 >= nLen)
    6004         849 :             return nullptr;
    6005        4067 :         int nMarkerLen = paby[i + 1] * 256 + paby[i + 2];
    6006        4067 :         if (i + 1 + nMarkerLen >= nLen)
    6007           0 :             return nullptr;
    6008        4067 :         if (paby[i] == byMarker)
    6009             :         {
    6010        3046 :             if (pnLenTable)
    6011        2494 :                 *pnLenTable = nMarkerLen;
    6012        3046 :             return paby + i + 1;
    6013             :         }
    6014        1021 :         i += 1 + nMarkerLen;
    6015             :     }
    6016           0 :     return nullptr;
    6017             : }
    6018             : 
    6019             : constexpr GByte MARKER_HUFFMAN_TABLE = 0xC4;
    6020             : constexpr GByte MARKER_QUANT_TABLE = 0xDB;
    6021             : 
    6022             : // We assume that if there are several quantization tables, they are
    6023             : // in the same order. Which is a reasonable assumption for updating
    6024             : // a file generated by ourselves.
    6025         904 : static bool GTIFFQuantizationTablesEqual(const GByte *paby1, int nLen1,
    6026             :                                          const GByte *paby2, int nLen2)
    6027             : {
    6028         904 :     bool bFound = false;
    6029             :     while (true)
    6030             :     {
    6031         945 :         int nLenTable1 = 0;
    6032         945 :         int nLenTable2 = 0;
    6033             :         const GByte *paby1New =
    6034         945 :             GTIFFFindNextTable(paby1, MARKER_QUANT_TABLE, nLen1, &nLenTable1);
    6035             :         const GByte *paby2New =
    6036         945 :             GTIFFFindNextTable(paby2, MARKER_QUANT_TABLE, nLen2, &nLenTable2);
    6037         945 :         if (paby1New == nullptr && paby2New == nullptr)
    6038         904 :             return bFound;
    6039         911 :         if (paby1New == nullptr || paby2New == nullptr)
    6040           0 :             return false;
    6041         911 :         if (nLenTable1 != nLenTable2)
    6042         207 :             return false;
    6043         704 :         if (memcmp(paby1New, paby2New, nLenTable1) != 0)
    6044         663 :             return false;
    6045          41 :         paby1New += nLenTable1;
    6046          41 :         paby2New += nLenTable2;
    6047          41 :         nLen1 -= static_cast<int>(paby1New - paby1);
    6048          41 :         nLen2 -= static_cast<int>(paby2New - paby2);
    6049          41 :         paby1 = paby1New;
    6050          41 :         paby2 = paby2New;
    6051          41 :         bFound = true;
    6052          41 :     }
    6053             : }
    6054             : 
    6055             : // Guess the JPEG quality by comparing against the MD5Sum of precomputed
    6056             : // quantization tables
    6057         417 : static int GuessJPEGQualityFromMD5(const uint8_t md5JPEGQuantTable[][16],
    6058             :                                    const GByte *const pabyJPEGTable,
    6059             :                                    int nJPEGTableSize)
    6060             : {
    6061         417 :     int nRemainingLen = nJPEGTableSize;
    6062         417 :     const GByte *pabyCur = pabyJPEGTable;
    6063             : 
    6064             :     struct CPLMD5Context context;
    6065         417 :     CPLMD5Init(&context);
    6066             : 
    6067             :     while (true)
    6068             :     {
    6069        1089 :         int nLenTable = 0;
    6070        1089 :         const GByte *pabyNew = GTIFFFindNextTable(pabyCur, MARKER_QUANT_TABLE,
    6071             :                                                   nRemainingLen, &nLenTable);
    6072        1089 :         if (pabyNew == nullptr)
    6073         417 :             break;
    6074         672 :         CPLMD5Update(&context, pabyNew, nLenTable);
    6075         672 :         pabyNew += nLenTable;
    6076         672 :         nRemainingLen -= static_cast<int>(pabyNew - pabyCur);
    6077         672 :         pabyCur = pabyNew;
    6078         672 :     }
    6079             : 
    6080             :     GByte digest[16];
    6081         417 :     CPLMD5Final(digest, &context);
    6082             : 
    6083       29446 :     for (int i = 0; i < 100; i++)
    6084             :     {
    6085       29443 :         if (memcmp(md5JPEGQuantTable[i], digest, 16) == 0)
    6086             :         {
    6087         414 :             return i + 1;
    6088             :         }
    6089             :     }
    6090           3 :     return -1;
    6091             : }
    6092             : 
    6093         472 : int GTiffDataset::GuessJPEGQuality(bool &bOutHasQuantizationTable,
    6094             :                                    bool &bOutHasHuffmanTable)
    6095             : {
    6096         472 :     CPLAssert(m_nCompression == COMPRESSION_JPEG);
    6097         472 :     uint32_t nJPEGTableSize = 0;
    6098         472 :     void *pJPEGTable = nullptr;
    6099         472 :     if (!TIFFGetField(m_hTIFF, TIFFTAG_JPEGTABLES, &nJPEGTableSize,
    6100             :                       &pJPEGTable))
    6101             :     {
    6102          14 :         bOutHasQuantizationTable = false;
    6103          14 :         bOutHasHuffmanTable = false;
    6104          14 :         return -1;
    6105             :     }
    6106             : 
    6107         458 :     bOutHasQuantizationTable =
    6108         458 :         GTIFFFindNextTable(static_cast<const GByte *>(pJPEGTable),
    6109             :                            MARKER_QUANT_TABLE, nJPEGTableSize,
    6110         458 :                            nullptr) != nullptr;
    6111         458 :     bOutHasHuffmanTable =
    6112         458 :         GTIFFFindNextTable(static_cast<const GByte *>(pJPEGTable),
    6113             :                            MARKER_HUFFMAN_TABLE, nJPEGTableSize,
    6114         458 :                            nullptr) != nullptr;
    6115         458 :     if (!bOutHasQuantizationTable)
    6116           7 :         return -1;
    6117             : 
    6118         451 :     if ((nBands == 1 && m_nBitsPerSample == 8) ||
    6119         390 :         (nBands == 3 && m_nBitsPerSample == 8 &&
    6120         344 :          m_nPhotometric == PHOTOMETRIC_RGB) ||
    6121         301 :         (nBands == 4 && m_nBitsPerSample == 8 &&
    6122          27 :          m_nPhotometric == PHOTOMETRIC_SEPARATED))
    6123             :     {
    6124         162 :         return GuessJPEGQualityFromMD5(md5JPEGQuantTable_generic_8bit,
    6125             :                                        static_cast<const GByte *>(pJPEGTable),
    6126         162 :                                        static_cast<int>(nJPEGTableSize));
    6127             :     }
    6128             : 
    6129         289 :     if (nBands == 3 && m_nBitsPerSample == 8 &&
    6130         255 :         m_nPhotometric == PHOTOMETRIC_YCBCR)
    6131             :     {
    6132             :         int nRet =
    6133         255 :             GuessJPEGQualityFromMD5(md5JPEGQuantTable_3_YCBCR_8bit,
    6134             :                                     static_cast<const GByte *>(pJPEGTable),
    6135             :                                     static_cast<int>(nJPEGTableSize));
    6136         255 :         if (nRet < 0)
    6137             :         {
    6138             :             // libjpeg 9e has modified the YCbCr quantization tables.
    6139             :             nRet =
    6140           0 :                 GuessJPEGQualityFromMD5(md5JPEGQuantTable_3_YCBCR_8bit_jpeg9e,
    6141             :                                         static_cast<const GByte *>(pJPEGTable),
    6142             :                                         static_cast<int>(nJPEGTableSize));
    6143             :         }
    6144         255 :         return nRet;
    6145             :     }
    6146             : 
    6147          34 :     char **papszLocalParameters = nullptr;
    6148             :     papszLocalParameters =
    6149          34 :         CSLSetNameValue(papszLocalParameters, "COMPRESS", "JPEG");
    6150          34 :     if (m_nPhotometric == PHOTOMETRIC_YCBCR)
    6151             :         papszLocalParameters =
    6152           7 :             CSLSetNameValue(papszLocalParameters, "PHOTOMETRIC", "YCBCR");
    6153          27 :     else if (m_nPhotometric == PHOTOMETRIC_SEPARATED)
    6154             :         papszLocalParameters =
    6155           0 :             CSLSetNameValue(papszLocalParameters, "PHOTOMETRIC", "CMYK");
    6156             :     papszLocalParameters =
    6157          34 :         CSLSetNameValue(papszLocalParameters, "BLOCKYSIZE", "16");
    6158          34 :     if (m_nBitsPerSample == 12)
    6159             :         papszLocalParameters =
    6160          16 :             CSLSetNameValue(papszLocalParameters, "NBITS", "12");
    6161             : 
    6162             :     const CPLString osTmpFilenameIn(
    6163          34 :         VSIMemGenerateHiddenFilename("gtiffdataset_guess_jpeg_quality_tmp"));
    6164             : 
    6165          34 :     int nRet = -1;
    6166         938 :     for (int nQuality = 0; nQuality <= 100 && nRet < 0; ++nQuality)
    6167             :     {
    6168         904 :         VSILFILE *fpTmp = nullptr;
    6169         904 :         if (nQuality == 0)
    6170             :             papszLocalParameters =
    6171          34 :                 CSLSetNameValue(papszLocalParameters, "JPEG_QUALITY", "75");
    6172             :         else
    6173             :             papszLocalParameters =
    6174         870 :                 CSLSetNameValue(papszLocalParameters, "JPEG_QUALITY",
    6175             :                                 CPLSPrintf("%d", nQuality));
    6176             : 
    6177         904 :         CPLPushErrorHandler(CPLQuietErrorHandler);
    6178         904 :         CPLString osTmp;
    6179             :         TIFF *hTIFFTmp =
    6180         904 :             CreateLL(osTmpFilenameIn, 16, 16, (nBands <= 4) ? nBands : 1,
    6181             :                      GetRasterBand(1)->GetRasterDataType(), 0.0, 0,
    6182             :                      papszLocalParameters, &fpTmp, osTmp);
    6183         904 :         CPLPopErrorHandler();
    6184         904 :         if (!hTIFFTmp)
    6185             :         {
    6186           0 :             break;
    6187             :         }
    6188             : 
    6189         904 :         TIFFWriteCheck(hTIFFTmp, FALSE, "CreateLL");
    6190         904 :         TIFFWriteDirectory(hTIFFTmp);
    6191         904 :         TIFFSetDirectory(hTIFFTmp, 0);
    6192             :         // Now reset jpegcolormode.
    6193        1196 :         if (m_nPhotometric == PHOTOMETRIC_YCBCR &&
    6194         292 :             CPLTestBool(CPLGetConfigOption("CONVERT_YCBCR_TO_RGB", "YES")))
    6195             :         {
    6196         292 :             TIFFSetField(hTIFFTmp, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
    6197             :         }
    6198             : 
    6199         904 :         GByte abyZeroData[(16 * 16 * 4 * 3) / 2] = {};
    6200         904 :         const int nBlockSize =
    6201         904 :             (16 * 16 * ((nBands <= 4) ? nBands : 1) * m_nBitsPerSample) / 8;
    6202         904 :         TIFFWriteEncodedStrip(hTIFFTmp, 0, abyZeroData, nBlockSize);
    6203             : 
    6204         904 :         uint32_t nJPEGTableSizeTry = 0;
    6205         904 :         void *pJPEGTableTry = nullptr;
    6206         904 :         if (TIFFGetField(hTIFFTmp, TIFFTAG_JPEGTABLES, &nJPEGTableSizeTry,
    6207         904 :                          &pJPEGTableTry))
    6208             :         {
    6209         904 :             if (GTIFFQuantizationTablesEqual(
    6210             :                     static_cast<GByte *>(pJPEGTable), nJPEGTableSize,
    6211             :                     static_cast<GByte *>(pJPEGTableTry), nJPEGTableSizeTry))
    6212             :             {
    6213          34 :                 nRet = (nQuality == 0) ? 75 : nQuality;
    6214             :             }
    6215             :         }
    6216             : 
    6217         904 :         XTIFFClose(hTIFFTmp);
    6218         904 :         CPL_IGNORE_RET_VAL(VSIFCloseL(fpTmp));
    6219             :     }
    6220             : 
    6221          34 :     CSLDestroy(papszLocalParameters);
    6222          34 :     VSIUnlink(osTmpFilenameIn);
    6223             : 
    6224          34 :     return nRet;
    6225             : }
    6226             : 
    6227             : /************************************************************************/
    6228             : /*               SetJPEGQualityAndTablesModeFromFile()                  */
    6229             : /************************************************************************/
    6230             : 
    6231         163 : void GTiffDataset::SetJPEGQualityAndTablesModeFromFile(
    6232             :     int nQuality, bool bHasQuantizationTable, bool bHasHuffmanTable)
    6233             : {
    6234         163 :     if (nQuality > 0)
    6235             :     {
    6236         156 :         CPLDebug("GTiff", "Guessed JPEG quality to be %d", nQuality);
    6237         156 :         m_nJpegQuality = static_cast<signed char>(nQuality);
    6238         156 :         TIFFSetField(m_hTIFF, TIFFTAG_JPEGQUALITY, nQuality);
    6239             : 
    6240             :         // This means we will use the quantization tables from the
    6241             :         // JpegTables tag.
    6242         156 :         m_nJpegTablesMode = JPEGTABLESMODE_QUANT;
    6243             :     }
    6244             :     else
    6245             :     {
    6246           7 :         uint32_t nJPEGTableSize = 0;
    6247           7 :         void *pJPEGTable = nullptr;
    6248           7 :         if (!TIFFGetField(m_hTIFF, TIFFTAG_JPEGTABLES, &nJPEGTableSize,
    6249             :                           &pJPEGTable))
    6250             :         {
    6251           4 :             toff_t *panByteCounts = nullptr;
    6252           8 :             const int nBlockCount = m_nPlanarConfig == PLANARCONFIG_SEPARATE
    6253           4 :                                         ? m_nBlocksPerBand * nBands
    6254             :                                         : m_nBlocksPerBand;
    6255           4 :             if (TIFFIsTiled(m_hTIFF))
    6256           1 :                 TIFFGetField(m_hTIFF, TIFFTAG_TILEBYTECOUNTS, &panByteCounts);
    6257             :             else
    6258           3 :                 TIFFGetField(m_hTIFF, TIFFTAG_STRIPBYTECOUNTS, &panByteCounts);
    6259             : 
    6260           4 :             bool bFoundNonEmptyBlock = false;
    6261           4 :             if (panByteCounts != nullptr)
    6262             :             {
    6263          56 :                 for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
    6264             :                 {
    6265          53 :                     if (panByteCounts[iBlock] != 0)
    6266             :                     {
    6267           1 :                         bFoundNonEmptyBlock = true;
    6268           1 :                         break;
    6269             :                     }
    6270             :                 }
    6271             :             }
    6272           4 :             if (bFoundNonEmptyBlock)
    6273             :             {
    6274           1 :                 CPLDebug("GTiff", "Could not guess JPEG quality. "
    6275             :                                   "JPEG tables are missing, so going in "
    6276             :                                   "TIFFTAG_JPEGTABLESMODE = 0/2 mode");
    6277             :                 // Write quantization tables in each strile.
    6278           1 :                 m_nJpegTablesMode = 0;
    6279             :             }
    6280             :         }
    6281             :         else
    6282             :         {
    6283           3 :             if (bHasQuantizationTable)
    6284             :             {
    6285             :                 // FIXME in libtiff: this is likely going to cause issues
    6286             :                 // since libtiff will reuse in each strile the number of
    6287             :                 // the global quantization table, which is invalid.
    6288           1 :                 CPLDebug("GTiff",
    6289             :                          "Could not guess JPEG quality although JPEG "
    6290             :                          "quantization tables are present, so going in "
    6291             :                          "TIFFTAG_JPEGTABLESMODE = 0/2 mode");
    6292             :             }
    6293             :             else
    6294             :             {
    6295           2 :                 CPLDebug("GTiff",
    6296             :                          "Could not guess JPEG quality since JPEG "
    6297             :                          "quantization tables are not present, so going in "
    6298             :                          "TIFFTAG_JPEGTABLESMODE = 0/2 mode");
    6299             :             }
    6300             : 
    6301             :             // Write quantization tables in each strile.
    6302           3 :             m_nJpegTablesMode = 0;
    6303             :         }
    6304             :     }
    6305         163 :     if (bHasHuffmanTable)
    6306             :     {
    6307             :         // If there are Huffman tables in header use them, otherwise
    6308             :         // if we use optimized tables, libtiff will currently reuse
    6309             :         // the number of the Huffman tables of the header for the
    6310             :         // optimized version of each strile, which is illegal.
    6311          23 :         m_nJpegTablesMode |= JPEGTABLESMODE_HUFF;
    6312             :     }
    6313         163 :     if (m_nJpegTablesMode >= 0)
    6314         161 :         TIFFSetField(m_hTIFF, TIFFTAG_JPEGTABLESMODE, m_nJpegTablesMode);
    6315         163 : }
    6316             : 
    6317             : /************************************************************************/
    6318             : /*                               Create()                               */
    6319             : /*                                                                      */
    6320             : /*      Create a new GeoTIFF or TIFF file.                              */
    6321             : /************************************************************************/
    6322             : 
    6323        3178 : GDALDataset *GTiffDataset::Create(const char *pszFilename, int nXSize,
    6324             :                                   int nYSize, int l_nBands, GDALDataType eType,
    6325             :                                   char **papszParamList)
    6326             : 
    6327             : {
    6328        3178 :     VSILFILE *l_fpL = nullptr;
    6329        6356 :     CPLString l_osTmpFilename;
    6330             : 
    6331             :     const int nColorTableMultiplier = std::max(
    6332        6356 :         1,
    6333        6356 :         std::min(257,
    6334        3178 :                  atoi(CSLFetchNameValueDef(
    6335             :                      papszParamList, "COLOR_TABLE_MULTIPLIER",
    6336        3178 :                      CPLSPrintf("%d", DEFAULT_COLOR_TABLE_MULTIPLIER_257)))));
    6337             : 
    6338             :     /* -------------------------------------------------------------------- */
    6339             :     /*      Create the underlying TIFF file.                                */
    6340             :     /* -------------------------------------------------------------------- */
    6341        3178 :     TIFF *l_hTIFF = CreateLL(pszFilename, nXSize, nYSize, l_nBands, eType, 0,
    6342             :                              nColorTableMultiplier, papszParamList, &l_fpL,
    6343             :                              l_osTmpFilename);
    6344        3178 :     const bool bStreaming = !l_osTmpFilename.empty();
    6345             : 
    6346        3178 :     if (l_hTIFF == nullptr)
    6347          30 :         return nullptr;
    6348             : 
    6349             :     /* -------------------------------------------------------------------- */
    6350             :     /*      Create the new GTiffDataset object.                             */
    6351             :     /* -------------------------------------------------------------------- */
    6352        3148 :     GTiffDataset *poDS = new GTiffDataset();
    6353        3148 :     poDS->m_hTIFF = l_hTIFF;
    6354        3148 :     poDS->m_fpL = l_fpL;
    6355        3148 :     if (bStreaming)
    6356             :     {
    6357           4 :         poDS->m_bStreamingOut = true;
    6358           4 :         poDS->m_pszTmpFilename = CPLStrdup(l_osTmpFilename);
    6359           4 :         poDS->m_fpToWrite = VSIFOpenL(pszFilename, "wb");
    6360           4 :         if (poDS->m_fpToWrite == nullptr)
    6361             :         {
    6362           1 :             VSIUnlink(l_osTmpFilename);
    6363           1 :             delete poDS;
    6364           1 :             return nullptr;
    6365             :         }
    6366             :     }
    6367        3147 :     poDS->nRasterXSize = nXSize;
    6368        3147 :     poDS->nRasterYSize = nYSize;
    6369        3147 :     poDS->eAccess = GA_Update;
    6370             : 
    6371        3147 :     poDS->m_nColorTableMultiplier = nColorTableMultiplier;
    6372             : 
    6373        3147 :     poDS->m_bCrystalized = false;
    6374        3147 :     poDS->m_nSamplesPerPixel = static_cast<uint16_t>(l_nBands);
    6375        3147 :     poDS->m_pszFilename = CPLStrdup(pszFilename);
    6376             : 
    6377             :     // Don't try to load external metadata files (#6597).
    6378        3147 :     poDS->m_bIMDRPCMetadataLoaded = true;
    6379             : 
    6380             :     // Avoid premature crystalization that will cause directory re-writing if
    6381             :     // GetProjectionRef() or GetGeoTransform() are called on the newly created
    6382             :     // GeoTIFF.
    6383        3147 :     poDS->m_bLookedForProjection = true;
    6384             : 
    6385        3147 :     TIFFGetField(l_hTIFF, TIFFTAG_SAMPLEFORMAT, &(poDS->m_nSampleFormat));
    6386        3147 :     TIFFGetField(l_hTIFF, TIFFTAG_PLANARCONFIG, &(poDS->m_nPlanarConfig));
    6387             :     // Weird that we need this, but otherwise we get a Valgrind warning on
    6388             :     // tiff_write_124.
    6389        3147 :     if (!TIFFGetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, &(poDS->m_nPhotometric)))
    6390           1 :         poDS->m_nPhotometric = PHOTOMETRIC_MINISBLACK;
    6391        3147 :     TIFFGetField(l_hTIFF, TIFFTAG_BITSPERSAMPLE, &(poDS->m_nBitsPerSample));
    6392        3147 :     TIFFGetField(l_hTIFF, TIFFTAG_COMPRESSION, &(poDS->m_nCompression));
    6393             : 
    6394        3147 :     if (TIFFIsTiled(l_hTIFF))
    6395             :     {
    6396         320 :         TIFFGetField(l_hTIFF, TIFFTAG_TILEWIDTH, &(poDS->m_nBlockXSize));
    6397         320 :         TIFFGetField(l_hTIFF, TIFFTAG_TILELENGTH, &(poDS->m_nBlockYSize));
    6398             :     }
    6399             :     else
    6400             :     {
    6401        2827 :         if (!TIFFGetField(l_hTIFF, TIFFTAG_ROWSPERSTRIP,
    6402             :                           &(poDS->m_nRowsPerStrip)))
    6403           0 :             poDS->m_nRowsPerStrip = 1;  // Dummy value.
    6404             : 
    6405        2827 :         poDS->m_nBlockXSize = nXSize;
    6406        2827 :         poDS->m_nBlockYSize =
    6407        2827 :             std::min(static_cast<int>(poDS->m_nRowsPerStrip), nYSize);
    6408             :     }
    6409             : 
    6410        3147 :     if (!poDS->ComputeBlocksPerColRowAndBand(l_nBands))
    6411             :     {
    6412           0 :         delete poDS;
    6413           0 :         return nullptr;
    6414             :     }
    6415             : 
    6416        3147 :     poDS->m_eProfile = GetProfile(CSLFetchNameValue(papszParamList, "PROFILE"));
    6417             : 
    6418             :     /* -------------------------------------------------------------------- */
    6419             :     /*      YCbCr JPEG compressed images should be translated on the fly    */
    6420             :     /*      to RGB by libtiff/libjpeg unless specifically requested         */
    6421             :     /*      otherwise.                                                      */
    6422             :     /* -------------------------------------------------------------------- */
    6423        6328 :     if (poDS->m_nCompression == COMPRESSION_JPEG &&
    6424        3168 :         poDS->m_nPhotometric == PHOTOMETRIC_YCBCR &&
    6425          21 :         CPLTestBool(CPLGetConfigOption("CONVERT_YCBCR_TO_RGB", "YES")))
    6426             :     {
    6427          21 :         int nColorMode = 0;
    6428             : 
    6429          21 :         poDS->SetMetadataItem("SOURCE_COLOR_SPACE", "YCbCr", "IMAGE_STRUCTURE");
    6430          42 :         if (!TIFFGetField(l_hTIFF, TIFFTAG_JPEGCOLORMODE, &nColorMode) ||
    6431          21 :             nColorMode != JPEGCOLORMODE_RGB)
    6432          21 :             TIFFSetField(l_hTIFF, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
    6433             :     }
    6434             : 
    6435        3147 :     if (poDS->m_nCompression == COMPRESSION_LERC)
    6436             :     {
    6437          26 :         uint32_t nLercParamCount = 0;
    6438          26 :         uint32_t *panLercParams = nullptr;
    6439          26 :         if (TIFFGetField(l_hTIFF, TIFFTAG_LERC_PARAMETERS, &nLercParamCount,
    6440          52 :                          &panLercParams) &&
    6441          26 :             nLercParamCount == 2)
    6442             :         {
    6443          26 :             memcpy(poDS->m_anLercAddCompressionAndVersion, panLercParams,
    6444             :                    sizeof(poDS->m_anLercAddCompressionAndVersion));
    6445             :         }
    6446             :     }
    6447             : 
    6448             :     /* -------------------------------------------------------------------- */
    6449             :     /*      Read palette back as a color table if it has one.               */
    6450             :     /* -------------------------------------------------------------------- */
    6451        3147 :     unsigned short *panRed = nullptr;
    6452        3147 :     unsigned short *panGreen = nullptr;
    6453        3147 :     unsigned short *panBlue = nullptr;
    6454             : 
    6455        3151 :     if (poDS->m_nPhotometric == PHOTOMETRIC_PALETTE &&
    6456           4 :         TIFFGetField(l_hTIFF, TIFFTAG_COLORMAP, &panRed, &panGreen, &panBlue))
    6457             :     {
    6458             : 
    6459           4 :         poDS->m_poColorTable = std::make_unique<GDALColorTable>();
    6460             : 
    6461           4 :         const int nColorCount = 1 << poDS->m_nBitsPerSample;
    6462             : 
    6463        1028 :         for (int iColor = nColorCount - 1; iColor >= 0; iColor--)
    6464             :         {
    6465        1024 :             const GDALColorEntry oEntry = {
    6466        1024 :                 static_cast<short>(panRed[iColor] / nColorTableMultiplier),
    6467        1024 :                 static_cast<short>(panGreen[iColor] / nColorTableMultiplier),
    6468        1024 :                 static_cast<short>(panBlue[iColor] / nColorTableMultiplier),
    6469        1024 :                 static_cast<short>(255)};
    6470             : 
    6471        1024 :             poDS->m_poColorTable->SetColorEntry(iColor, &oEntry);
    6472             :         }
    6473             :     }
    6474             : 
    6475             :     /* -------------------------------------------------------------------- */
    6476             :     /*      Do we want to ensure all blocks get written out on close to     */
    6477             :     /*      avoid sparse files?                                             */
    6478             :     /* -------------------------------------------------------------------- */
    6479        3147 :     if (!CPLFetchBool(papszParamList, "SPARSE_OK", false))
    6480        3041 :         poDS->m_bFillEmptyTilesAtClosing = true;
    6481             : 
    6482        3147 :     poDS->m_bWriteEmptyTiles =
    6483        3816 :         bStreaming || (poDS->m_nCompression != COMPRESSION_NONE &&
    6484         669 :                        poDS->m_bFillEmptyTilesAtClosing);
    6485             :     // Only required for people writing non-compressed striped files in the
    6486             :     // right order and wanting all tstrips to be written in the same order
    6487             :     // so that the end result can be memory mapped without knowledge of each
    6488             :     // strip offset.
    6489        3147 :     if (CPLTestBool(CSLFetchNameValueDef(
    6490        6294 :             papszParamList, "WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")) ||
    6491        3147 :         CPLTestBool(CSLFetchNameValueDef(
    6492             :             papszParamList, "@WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")))
    6493             :     {
    6494          19 :         poDS->m_bWriteEmptyTiles = true;
    6495             :     }
    6496             : 
    6497             :     /* -------------------------------------------------------------------- */
    6498             :     /*      Preserve creation options for consulting later (for instance    */
    6499             :     /*      to decide if a TFW file should be written).                     */
    6500             :     /* -------------------------------------------------------------------- */
    6501        3147 :     poDS->m_papszCreationOptions = CSLDuplicate(papszParamList);
    6502             : 
    6503        3147 :     poDS->m_nZLevel = GTiffGetZLevel(papszParamList);
    6504        3147 :     poDS->m_nLZMAPreset = GTiffGetLZMAPreset(papszParamList);
    6505        3147 :     poDS->m_nZSTDLevel = GTiffGetZSTDPreset(papszParamList);
    6506        3147 :     poDS->m_nWebPLevel = GTiffGetWebPLevel(papszParamList);
    6507        3147 :     poDS->m_bWebPLossless = GTiffGetWebPLossless(papszParamList);
    6508        3149 :     if (poDS->m_nWebPLevel != 100 && poDS->m_bWebPLossless &&
    6509           2 :         CSLFetchNameValue(papszParamList, "WEBP_LEVEL"))
    6510             :     {
    6511           0 :         CPLError(CE_Warning, CPLE_AppDefined,
    6512             :                  "WEBP_LEVEL is specified, but WEBP_LOSSLESS=YES. "
    6513             :                  "WEBP_LEVEL will be ignored.");
    6514             :     }
    6515        3147 :     poDS->m_nJpegQuality = GTiffGetJpegQuality(papszParamList);
    6516        3147 :     poDS->m_nJpegTablesMode = GTiffGetJpegTablesMode(papszParamList);
    6517        3147 :     poDS->m_dfMaxZError = GTiffGetLERCMaxZError(papszParamList);
    6518        3147 :     poDS->m_dfMaxZErrorOverview = GTiffGetLERCMaxZErrorOverview(papszParamList);
    6519             : #if HAVE_JXL
    6520        3147 :     poDS->m_bJXLLossless = GTiffGetJXLLossless(papszParamList);
    6521        3147 :     poDS->m_nJXLEffort = GTiffGetJXLEffort(papszParamList);
    6522        3147 :     poDS->m_fJXLDistance = GTiffGetJXLDistance(papszParamList);
    6523        3147 :     poDS->m_fJXLAlphaDistance = GTiffGetJXLAlphaDistance(papszParamList);
    6524             : #endif
    6525        3147 :     poDS->InitCreationOrOpenOptions(true, papszParamList);
    6526             : 
    6527             :     /* -------------------------------------------------------------------- */
    6528             :     /*      Create band information objects.                                */
    6529             :     /* -------------------------------------------------------------------- */
    6530      204175 :     for (int iBand = 0; iBand < l_nBands; ++iBand)
    6531             :     {
    6532      201028 :         if (poDS->m_nBitsPerSample == 8 ||
    6533        1467 :             (poDS->m_nBitsPerSample == 16 && eType != GDT_Float32) ||
    6534         960 :             poDS->m_nBitsPerSample == 32 || poDS->m_nBitsPerSample == 64 ||
    6535         127 :             poDS->m_nBitsPerSample == 128)
    6536             :         {
    6537      200961 :             poDS->SetBand(iBand + 1, new GTiffRasterBand(poDS, iBand + 1));
    6538             :         }
    6539             :         else
    6540             :         {
    6541          67 :             poDS->SetBand(iBand + 1, new GTiffOddBitsBand(poDS, iBand + 1));
    6542         134 :             poDS->GetRasterBand(iBand + 1)->SetMetadataItem(
    6543         134 :                 "NBITS", CPLString().Printf("%d", poDS->m_nBitsPerSample),
    6544          67 :                 "IMAGE_STRUCTURE");
    6545             :         }
    6546             :     }
    6547             : 
    6548        3147 :     poDS->GetDiscardLsbOption(papszParamList);
    6549             : 
    6550        3147 :     if (poDS->m_nPlanarConfig == PLANARCONFIG_CONTIG && l_nBands != 1)
    6551         563 :         poDS->SetMetadataItem("INTERLEAVE", "PIXEL", "IMAGE_STRUCTURE");
    6552             :     else
    6553        2584 :         poDS->SetMetadataItem("INTERLEAVE", "BAND", "IMAGE_STRUCTURE");
    6554             : 
    6555        3147 :     poDS->oOvManager.Initialize(poDS, pszFilename);
    6556             : 
    6557        3147 :     return poDS;
    6558             : }
    6559             : 
    6560             : /************************************************************************/
    6561             : /*                           CopyImageryAndMask()                       */
    6562             : /************************************************************************/
    6563             : 
    6564         271 : CPLErr GTiffDataset::CopyImageryAndMask(GTiffDataset *poDstDS,
    6565             :                                         GDALDataset *poSrcDS,
    6566             :                                         GDALRasterBand *poSrcMaskBand,
    6567             :                                         GDALProgressFunc pfnProgress,
    6568             :                                         void *pProgressData)
    6569             : {
    6570         271 :     CPLErr eErr = CE_None;
    6571             : 
    6572         271 :     const auto eType = poDstDS->GetRasterBand(1)->GetRasterDataType();
    6573         271 :     const int nDataTypeSize = GDALGetDataTypeSizeBytes(eType);
    6574         271 :     const int l_nBands = poDstDS->GetRasterCount();
    6575             :     void *pBlockBuffer =
    6576         271 :         VSI_MALLOC3_VERBOSE(poDstDS->m_nBlockXSize, poDstDS->m_nBlockYSize,
    6577             :                             cpl::fits_on<int>(l_nBands * nDataTypeSize));
    6578         271 :     if (pBlockBuffer == nullptr)
    6579             :     {
    6580           0 :         eErr = CE_Failure;
    6581             :     }
    6582         271 :     const int nYSize = poDstDS->nRasterYSize;
    6583         271 :     const int nXSize = poDstDS->nRasterXSize;
    6584         271 :     const int nBlocks = poDstDS->m_nBlocksPerBand;
    6585             : 
    6586         271 :     CPLAssert(l_nBands == 1 || poDstDS->m_nPlanarConfig == PLANARCONFIG_CONTIG);
    6587             : 
    6588             :     const bool bIsOddBand =
    6589         271 :         dynamic_cast<GTiffOddBitsBand *>(poDstDS->GetRasterBand(1)) != nullptr;
    6590             : 
    6591         271 :     if (poDstDS->m_poMaskDS)
    6592             :     {
    6593          49 :         CPLAssert(poDstDS->m_poMaskDS->m_nBlockXSize == poDstDS->m_nBlockXSize);
    6594          49 :         CPLAssert(poDstDS->m_poMaskDS->m_nBlockYSize == poDstDS->m_nBlockYSize);
    6595             :     }
    6596             : 
    6597         271 :     int iBlock = 0;
    6598        7022 :     for (int iY = 0, nYBlock = 0; iY < nYSize && eErr == CE_None;
    6599        6751 :          iY = ((nYSize - iY < poDstDS->m_nBlockYSize)
    6600        6751 :                    ? nYSize
    6601        6553 :                    : iY + poDstDS->m_nBlockYSize),
    6602             :              nYBlock++)
    6603             :     {
    6604        6751 :         const int nReqYSize = std::min(nYSize - iY, poDstDS->m_nBlockYSize);
    6605       26376 :         for (int iX = 0, nXBlock = 0; iX < nXSize && eErr == CE_None;
    6606       19625 :              iX = ((nXSize - iX < poDstDS->m_nBlockXSize)
    6607       19625 :                        ? nXSize
    6608       19359 :                        : iX + poDstDS->m_nBlockXSize),
    6609             :                  nXBlock++)
    6610             :         {
    6611       19625 :             const int nReqXSize = std::min(nXSize - iX, poDstDS->m_nBlockXSize);
    6612       19625 :             if (nReqXSize < poDstDS->m_nBlockXSize ||
    6613       19359 :                 nReqYSize < poDstDS->m_nBlockYSize)
    6614             :             {
    6615         416 :                 memset(pBlockBuffer, 0,
    6616         416 :                        static_cast<size_t>(poDstDS->m_nBlockXSize) *
    6617         416 :                            poDstDS->m_nBlockYSize * l_nBands * nDataTypeSize);
    6618             :             }
    6619             : 
    6620       19625 :             if (!bIsOddBand)
    6621             :             {
    6622       39128 :                 eErr = poSrcDS->RasterIO(
    6623             :                     GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
    6624             :                     nReqXSize, nReqYSize, eType, l_nBands, nullptr,
    6625       19564 :                     static_cast<GSpacing>(nDataTypeSize) * l_nBands,
    6626       19564 :                     static_cast<GSpacing>(nDataTypeSize) * l_nBands *
    6627       19564 :                         poDstDS->m_nBlockXSize,
    6628             :                     nDataTypeSize, nullptr);
    6629       19564 :                 if (eErr == CE_None)
    6630             :                 {
    6631       19563 :                     eErr = poDstDS->WriteEncodedTileOrStrip(
    6632             :                         iBlock, pBlockBuffer, false);
    6633             :                 }
    6634             :             }
    6635             :             else
    6636             :             {
    6637             :                 // In the odd bit case, this is a bit messy to ensure
    6638             :                 // the strile gets written synchronously.
    6639             :                 // We load the content of the n-1 bands in the cache,
    6640             :                 // and for the last band we invoke WriteBlock() directly
    6641             :                 // We also force FlushBlockBuf()
    6642         122 :                 std::vector<GDALRasterBlock *> apoLockedBlocks;
    6643          91 :                 for (int i = 0; eErr == CE_None && i < l_nBands - 1; i++)
    6644             :                 {
    6645             :                     auto poBlock =
    6646          30 :                         poDstDS->GetRasterBand(i + 1)->GetLockedBlockRef(
    6647          30 :                             nXBlock, nYBlock, TRUE);
    6648          30 :                     if (poBlock)
    6649             :                     {
    6650          60 :                         eErr = poSrcDS->GetRasterBand(i + 1)->RasterIO(
    6651             :                             GF_Read, iX, iY, nReqXSize, nReqYSize,
    6652             :                             poBlock->GetDataRef(), nReqXSize, nReqYSize, eType,
    6653             :                             nDataTypeSize,
    6654          30 :                             static_cast<GSpacing>(nDataTypeSize) *
    6655          30 :                                 poDstDS->m_nBlockXSize,
    6656             :                             nullptr);
    6657          30 :                         poBlock->MarkDirty();
    6658          30 :                         apoLockedBlocks.emplace_back(poBlock);
    6659             :                     }
    6660             :                     else
    6661             :                     {
    6662           0 :                         eErr = CE_Failure;
    6663             :                     }
    6664             :                 }
    6665          61 :                 if (eErr == CE_None)
    6666             :                 {
    6667         122 :                     eErr = poSrcDS->GetRasterBand(l_nBands)->RasterIO(
    6668             :                         GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
    6669             :                         nReqXSize, nReqYSize, eType, nDataTypeSize,
    6670          61 :                         static_cast<GSpacing>(nDataTypeSize) *
    6671          61 :                             poDstDS->m_nBlockXSize,
    6672             :                         nullptr);
    6673             :                 }
    6674          61 :                 if (eErr == CE_None)
    6675             :                 {
    6676             :                     // Avoid any attempt to load from disk
    6677          61 :                     poDstDS->m_nLoadedBlock = iBlock;
    6678          61 :                     eErr = poDstDS->GetRasterBand(l_nBands)->WriteBlock(
    6679             :                         nXBlock, nYBlock, pBlockBuffer);
    6680          61 :                     if (eErr == CE_None)
    6681          61 :                         eErr = poDstDS->FlushBlockBuf();
    6682             :                 }
    6683          91 :                 for (auto poBlock : apoLockedBlocks)
    6684             :                 {
    6685          30 :                     poBlock->MarkClean();
    6686          30 :                     poBlock->DropLock();
    6687             :                 }
    6688             :             }
    6689             : 
    6690       19625 :             if (eErr == CE_None && poDstDS->m_poMaskDS)
    6691             :             {
    6692        4626 :                 if (nReqXSize < poDstDS->m_nBlockXSize ||
    6693        4592 :                     nReqYSize < poDstDS->m_nBlockYSize)
    6694             :                 {
    6695          62 :                     memset(pBlockBuffer, 0,
    6696          62 :                            static_cast<size_t>(poDstDS->m_nBlockXSize) *
    6697          62 :                                poDstDS->m_nBlockYSize);
    6698             :                 }
    6699        9252 :                 eErr = poSrcMaskBand->RasterIO(
    6700             :                     GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
    6701        4626 :                     nReqXSize, nReqYSize, GDT_Byte, 1, poDstDS->m_nBlockXSize,
    6702             :                     nullptr);
    6703        4626 :                 if (eErr == CE_None)
    6704             :                 {
    6705             :                     // Avoid any attempt to load from disk
    6706        4626 :                     poDstDS->m_poMaskDS->m_nLoadedBlock = iBlock;
    6707        4626 :                     eErr = poDstDS->m_poMaskDS->GetRasterBand(1)->WriteBlock(
    6708             :                         nXBlock, nYBlock, pBlockBuffer);
    6709        4626 :                     if (eErr == CE_None)
    6710        4626 :                         eErr = poDstDS->m_poMaskDS->FlushBlockBuf();
    6711             :                 }
    6712             :             }
    6713       19625 :             if (poDstDS->m_bWriteError)
    6714           6 :                 eErr = CE_Failure;
    6715             : 
    6716       19625 :             iBlock++;
    6717       39250 :             if (pfnProgress &&
    6718       19625 :                 !pfnProgress(static_cast<double>(iBlock) / nBlocks, nullptr,
    6719             :                              pProgressData))
    6720             :             {
    6721           0 :                 eErr = CE_Failure;
    6722             :             }
    6723             :         }
    6724             :     }
    6725         271 :     poDstDS->FlushCache(false);  // mostly to wait for thread completion
    6726         271 :     VSIFree(pBlockBuffer);
    6727             : 
    6728         271 :     return eErr;
    6729             : }
    6730             : 
    6731             : /************************************************************************/
    6732             : /*                             CreateCopy()                             */
    6733             : /************************************************************************/
    6734             : 
    6735        1915 : GDALDataset *GTiffDataset::CreateCopy(const char *pszFilename,
    6736             :                                       GDALDataset *poSrcDS, int bStrict,
    6737             :                                       char **papszOptions,
    6738             :                                       GDALProgressFunc pfnProgress,
    6739             :                                       void *pProgressData)
    6740             : 
    6741             : {
    6742        1915 :     if (poSrcDS->GetRasterCount() == 0)
    6743             :     {
    6744           2 :         ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    6745             :                     "Unable to export GeoTIFF files with zero bands.");
    6746           2 :         return nullptr;
    6747             :     }
    6748             : 
    6749        1913 :     GDALRasterBand *const poPBand = poSrcDS->GetRasterBand(1);
    6750        1913 :     GDALDataType eType = poPBand->GetRasterDataType();
    6751             : 
    6752             :     /* -------------------------------------------------------------------- */
    6753             :     /*      Check, whether all bands in input dataset has the same type.    */
    6754             :     /* -------------------------------------------------------------------- */
    6755        1913 :     const int l_nBands = poSrcDS->GetRasterCount();
    6756        4711 :     for (int iBand = 2; iBand <= l_nBands; ++iBand)
    6757             :     {
    6758        2798 :         if (eType != poSrcDS->GetRasterBand(iBand)->GetRasterDataType())
    6759             :         {
    6760           0 :             if (bStrict)
    6761             :             {
    6762           0 :                 ReportError(
    6763             :                     pszFilename, CE_Failure, CPLE_AppDefined,
    6764             :                     "Unable to export GeoTIFF file with different datatypes "
    6765             :                     "per different bands. All bands should have the same "
    6766             :                     "types in TIFF.");
    6767           0 :                 return nullptr;
    6768             :             }
    6769             :             else
    6770             :             {
    6771           0 :                 ReportError(
    6772             :                     pszFilename, CE_Warning, CPLE_AppDefined,
    6773             :                     "Unable to export GeoTIFF file with different datatypes "
    6774             :                     "per different bands. All bands should have the same "
    6775             :                     "types in TIFF.");
    6776             :             }
    6777             :         }
    6778             :     }
    6779             : 
    6780             :     /* -------------------------------------------------------------------- */
    6781             :     /*      Capture the profile.                                            */
    6782             :     /* -------------------------------------------------------------------- */
    6783             :     const GTiffProfile eProfile =
    6784        1913 :         GetProfile(CSLFetchNameValue(papszOptions, "PROFILE"));
    6785             : 
    6786        1913 :     const bool bGeoTIFF = eProfile != GTiffProfile::BASELINE;
    6787             : 
    6788             :     /* -------------------------------------------------------------------- */
    6789             :     /*      Special handling for NBITS.  Copy from band metadata if found.  */
    6790             :     /* -------------------------------------------------------------------- */
    6791        1913 :     char **papszCreateOptions = CSLDuplicate(papszOptions);
    6792             : 
    6793        1913 :     if (poPBand->GetMetadataItem("NBITS", "IMAGE_STRUCTURE") != nullptr &&
    6794        1932 :         atoi(poPBand->GetMetadataItem("NBITS", "IMAGE_STRUCTURE")) > 0 &&
    6795          19 :         CSLFetchNameValue(papszCreateOptions, "NBITS") == nullptr)
    6796             :     {
    6797           5 :         papszCreateOptions = CSLSetNameValue(
    6798             :             papszCreateOptions, "NBITS",
    6799           5 :             poPBand->GetMetadataItem("NBITS", "IMAGE_STRUCTURE"));
    6800             :     }
    6801             : 
    6802        1913 :     if (CSLFetchNameValue(papszOptions, "PIXELTYPE") == nullptr &&
    6803             :         eType == GDT_Byte)
    6804             :     {
    6805        1619 :         poPBand->EnablePixelTypeSignedByteWarning(false);
    6806             :         const char *pszPixelType =
    6807        1619 :             poPBand->GetMetadataItem("PIXELTYPE", "IMAGE_STRUCTURE");
    6808        1619 :         poPBand->EnablePixelTypeSignedByteWarning(true);
    6809        1619 :         if (pszPixelType)
    6810             :         {
    6811           1 :             papszCreateOptions =
    6812           1 :                 CSLSetNameValue(papszCreateOptions, "PIXELTYPE", pszPixelType);
    6813             :         }
    6814             :     }
    6815             : 
    6816             :     /* -------------------------------------------------------------------- */
    6817             :     /*      Color profile.  Copy from band metadata if found.              */
    6818             :     /* -------------------------------------------------------------------- */
    6819        1913 :     if (bGeoTIFF)
    6820             :     {
    6821        1896 :         const char *pszOptionsMD[] = {"SOURCE_ICC_PROFILE",
    6822             :                                       "SOURCE_PRIMARIES_RED",
    6823             :                                       "SOURCE_PRIMARIES_GREEN",
    6824             :                                       "SOURCE_PRIMARIES_BLUE",
    6825             :                                       "SOURCE_WHITEPOINT",
    6826             :                                       "TIFFTAG_TRANSFERFUNCTION_RED",
    6827             :                                       "TIFFTAG_TRANSFERFUNCTION_GREEN",
    6828             :                                       "TIFFTAG_TRANSFERFUNCTION_BLUE",
    6829             :                                       "TIFFTAG_TRANSFERRANGE_BLACK",
    6830             :                                       "TIFFTAG_TRANSFERRANGE_WHITE",
    6831             :                                       nullptr};
    6832             : 
    6833             :         // Copy all the tags.  Options will override tags in the source.
    6834        1896 :         int i = 0;
    6835       20836 :         while (pszOptionsMD[i] != nullptr)
    6836             :         {
    6837             :             char const *pszMD =
    6838       18942 :                 CSLFetchNameValue(papszOptions, pszOptionsMD[i]);
    6839       18942 :             if (pszMD == nullptr)
    6840             :                 pszMD =
    6841       18934 :                     poSrcDS->GetMetadataItem(pszOptionsMD[i], "COLOR_PROFILE");
    6842             : 
    6843       18942 :             if ((pszMD != nullptr) && !EQUAL(pszMD, ""))
    6844             :             {
    6845          16 :                 papszCreateOptions =
    6846          16 :                     CSLSetNameValue(papszCreateOptions, pszOptionsMD[i], pszMD);
    6847             : 
    6848             :                 // If an ICC profile exists, other tags are not needed.
    6849          16 :                 if (EQUAL(pszOptionsMD[i], "SOURCE_ICC_PROFILE"))
    6850           2 :                     break;
    6851             :             }
    6852             : 
    6853       18940 :             ++i;
    6854             :         }
    6855             :     }
    6856             : 
    6857        1913 :     double dfExtraSpaceForOverviews = 0;
    6858             :     const bool bCopySrcOverviews =
    6859        1913 :         CPLFetchBool(papszCreateOptions, "COPY_SRC_OVERVIEWS", false);
    6860        1913 :     std::unique_ptr<GDALDataset> poOvrDS;
    6861        1913 :     int nSrcOverviews = 0;
    6862        1913 :     if (bCopySrcOverviews)
    6863             :     {
    6864             :         const char *pszOvrDS =
    6865         172 :             CSLFetchNameValue(papszCreateOptions, "@OVERVIEW_DATASET");
    6866         172 :         if (pszOvrDS)
    6867             :         {
    6868             :             // Empty string is used by COG driver to indicate that we want
    6869             :             // to ignore source overviews.
    6870          34 :             if (!EQUAL(pszOvrDS, ""))
    6871             :             {
    6872          32 :                 poOvrDS.reset(GDALDataset::Open(pszOvrDS));
    6873          32 :                 if (!poOvrDS)
    6874             :                 {
    6875           0 :                     CSLDestroy(papszCreateOptions);
    6876           0 :                     return nullptr;
    6877             :                 }
    6878          32 :                 if (poOvrDS->GetRasterCount() != l_nBands)
    6879             :                 {
    6880           0 :                     CSLDestroy(papszCreateOptions);
    6881           0 :                     return nullptr;
    6882             :                 }
    6883          32 :                 nSrcOverviews =
    6884          32 :                     poOvrDS->GetRasterBand(1)->GetOverviewCount() + 1;
    6885             :             }
    6886             :         }
    6887             :         else
    6888             :         {
    6889         138 :             nSrcOverviews = poSrcDS->GetRasterBand(1)->GetOverviewCount();
    6890             :         }
    6891             : 
    6892             :         // Limit number of overviews if specified
    6893             :         const char *pszOverviewCount =
    6894         172 :             CSLFetchNameValue(papszCreateOptions, "@OVERVIEW_COUNT");
    6895         172 :         if (pszOverviewCount)
    6896           8 :             nSrcOverviews =
    6897           8 :                 std::max(0, std::min(nSrcOverviews, atoi(pszOverviewCount)));
    6898             : 
    6899         172 :         if (nSrcOverviews)
    6900             :         {
    6901         166 :             for (int j = 1; j <= l_nBands; ++j)
    6902             :             {
    6903             :                 const int nOtherBandOverviewCount =
    6904         108 :                     poOvrDS ? poOvrDS->GetRasterBand(j)->GetOverviewCount() + 1
    6905         149 :                             : poSrcDS->GetRasterBand(j)->GetOverviewCount();
    6906         108 :                 if (nOtherBandOverviewCount < nSrcOverviews)
    6907             :                 {
    6908           1 :                     ReportError(
    6909             :                         pszFilename, CE_Failure, CPLE_NotSupported,
    6910             :                         "COPY_SRC_OVERVIEWS cannot be used when the bands have "
    6911             :                         "not the same number of overview levels.");
    6912           1 :                     CSLDestroy(papszCreateOptions);
    6913           1 :                     return nullptr;
    6914             :                 }
    6915         334 :                 for (int i = 0; i < nSrcOverviews; ++i)
    6916             :                 {
    6917             :                     GDALRasterBand *poOvrBand =
    6918             :                         poOvrDS
    6919         325 :                             ? (i == 0 ? poOvrDS->GetRasterBand(j)
    6920         192 :                                       : poOvrDS->GetRasterBand(j)->GetOverview(
    6921          96 :                                             i - 1))
    6922         295 :                             : poSrcDS->GetRasterBand(j)->GetOverview(i);
    6923         229 :                     if (poOvrBand == nullptr)
    6924             :                     {
    6925           1 :                         ReportError(
    6926             :                             pszFilename, CE_Failure, CPLE_NotSupported,
    6927             :                             "COPY_SRC_OVERVIEWS cannot be used when one "
    6928             :                             "overview band is NULL.");
    6929           1 :                         CSLDestroy(papszCreateOptions);
    6930           1 :                         return nullptr;
    6931             :                     }
    6932             :                     GDALRasterBand *poOvrFirstBand =
    6933             :                         poOvrDS
    6934         324 :                             ? (i == 0 ? poOvrDS->GetRasterBand(1)
    6935         192 :                                       : poOvrDS->GetRasterBand(1)->GetOverview(
    6936          96 :                                             i - 1))
    6937         293 :                             : poSrcDS->GetRasterBand(1)->GetOverview(i);
    6938         455 :                     if (poOvrBand->GetXSize() != poOvrFirstBand->GetXSize() ||
    6939         227 :                         poOvrBand->GetYSize() != poOvrFirstBand->GetYSize())
    6940             :                     {
    6941           1 :                         ReportError(
    6942             :                             pszFilename, CE_Failure, CPLE_NotSupported,
    6943             :                             "COPY_SRC_OVERVIEWS cannot be used when the "
    6944             :                             "overview bands have not the same dimensions "
    6945             :                             "among bands.");
    6946           1 :                         CSLDestroy(papszCreateOptions);
    6947           1 :                         return nullptr;
    6948             :                     }
    6949             :                 }
    6950             :             }
    6951             : 
    6952         172 :             for (int i = 0; i < nSrcOverviews; ++i)
    6953             :             {
    6954             :                 GDALRasterBand *poOvrFirstBand =
    6955             :                     poOvrDS
    6956         184 :                         ? (i == 0
    6957          70 :                                ? poOvrDS->GetRasterBand(1)
    6958          38 :                                : poOvrDS->GetRasterBand(1)->GetOverview(i - 1))
    6959         158 :                         : poSrcDS->GetRasterBand(1)->GetOverview(i);
    6960         114 :                 dfExtraSpaceForOverviews +=
    6961         114 :                     static_cast<double>(poOvrFirstBand->GetXSize()) *
    6962         114 :                     poOvrFirstBand->GetYSize();
    6963             :             }
    6964          58 :             dfExtraSpaceForOverviews *=
    6965          58 :                 l_nBands * GDALGetDataTypeSizeBytes(eType);
    6966             :         }
    6967             :         else
    6968             :         {
    6969         111 :             CPLDebug("GTiff", "No source overviews to copy");
    6970             :         }
    6971             :     }
    6972             : 
    6973             : /* -------------------------------------------------------------------- */
    6974             : /*      Should we use optimized way of copying from an input JPEG       */
    6975             : /*      dataset?                                                        */
    6976             : /* -------------------------------------------------------------------- */
    6977             : 
    6978             : // TODO(schwehr): Refactor bDirectCopyFromJPEG to be a const.
    6979             : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
    6980        1910 :     bool bDirectCopyFromJPEG = false;
    6981             : #endif
    6982             : 
    6983             :     // Note: JPEG_DIRECT_COPY is not defined by default, because it is mainly
    6984             :     // useful for debugging purposes.
    6985             : #ifdef JPEG_DIRECT_COPY
    6986             :     if (CPLFetchBool(papszCreateOptions, "JPEG_DIRECT_COPY", false) &&
    6987             :         GTIFF_CanDirectCopyFromJPEG(poSrcDS, papszCreateOptions))
    6988             :     {
    6989             :         CPLDebug("GTiff", "Using special direct copy mode from a JPEG dataset");
    6990             : 
    6991             :         bDirectCopyFromJPEG = true;
    6992             :     }
    6993             : #endif
    6994             : 
    6995             : #ifdef HAVE_LIBJPEG
    6996        1910 :     bool bCopyFromJPEG = false;
    6997             : 
    6998             :     // When CreateCopy'ing() from a JPEG dataset, and asking for COMPRESS=JPEG,
    6999             :     // use DCT coefficients (unless other options are incompatible, like
    7000             :     // strip/tile dimensions, specifying JPEG_QUALITY option, incompatible
    7001             :     // PHOTOMETRIC with the source colorspace, etc.) to avoid the lossy steps
    7002             :     // involved by decompression/recompression.
    7003        3820 :     if (!bDirectCopyFromJPEG &&
    7004        1910 :         GTIFF_CanCopyFromJPEG(poSrcDS, papszCreateOptions))
    7005             :     {
    7006          12 :         CPLDebug("GTiff", "Using special copy mode from a JPEG dataset");
    7007             : 
    7008          12 :         bCopyFromJPEG = true;
    7009             :     }
    7010             : #endif
    7011             : 
    7012             :     /* -------------------------------------------------------------------- */
    7013             :     /*      If the source is RGB, then set the PHOTOMETRIC=RGB value        */
    7014             :     /* -------------------------------------------------------------------- */
    7015             : 
    7016             :     const bool bForcePhotometric =
    7017        1910 :         CSLFetchNameValue(papszOptions, "PHOTOMETRIC") != nullptr;
    7018             : 
    7019        1172 :     if (l_nBands >= 3 && !bForcePhotometric &&
    7020             : #ifdef HAVE_LIBJPEG
    7021        1134 :         !bCopyFromJPEG &&
    7022             : #endif
    7023        1128 :         poSrcDS->GetRasterBand(1)->GetColorInterpretation() == GCI_RedBand &&
    7024        4116 :         poSrcDS->GetRasterBand(2)->GetColorInterpretation() == GCI_GreenBand &&
    7025        1034 :         poSrcDS->GetRasterBand(3)->GetColorInterpretation() == GCI_BlueBand)
    7026             :     {
    7027        1028 :         papszCreateOptions =
    7028        1028 :             CSLSetNameValue(papszCreateOptions, "PHOTOMETRIC", "RGB");
    7029             :     }
    7030             : 
    7031             :     /* -------------------------------------------------------------------- */
    7032             :     /*      Create the file.                                                */
    7033             :     /* -------------------------------------------------------------------- */
    7034        1910 :     VSILFILE *l_fpL = nullptr;
    7035        3820 :     CPLString l_osTmpFilename;
    7036             : 
    7037        1910 :     const int nXSize = poSrcDS->GetRasterXSize();
    7038        1910 :     const int nYSize = poSrcDS->GetRasterYSize();
    7039             : 
    7040             :     const int nColorTableMultiplier = std::max(
    7041        3820 :         1,
    7042        3820 :         std::min(257,
    7043        1910 :                  atoi(CSLFetchNameValueDef(
    7044             :                      papszOptions, "COLOR_TABLE_MULTIPLIER",
    7045        1910 :                      CPLSPrintf("%d", DEFAULT_COLOR_TABLE_MULTIPLIER_257)))));
    7046             : 
    7047        1910 :     TIFF *l_hTIFF = CreateLL(pszFilename, nXSize, nYSize, l_nBands, eType,
    7048             :                              dfExtraSpaceForOverviews, nColorTableMultiplier,
    7049             :                              papszCreateOptions, &l_fpL, l_osTmpFilename);
    7050        1910 :     const bool bStreaming = !l_osTmpFilename.empty();
    7051             : 
    7052        1910 :     CSLDestroy(papszCreateOptions);
    7053        1910 :     papszCreateOptions = nullptr;
    7054             : 
    7055        1910 :     if (l_hTIFF == nullptr)
    7056             :     {
    7057          14 :         if (bStreaming)
    7058           0 :             VSIUnlink(l_osTmpFilename);
    7059          14 :         return nullptr;
    7060             :     }
    7061             : 
    7062        1896 :     uint16_t l_nPlanarConfig = 0;
    7063        1896 :     TIFFGetField(l_hTIFF, TIFFTAG_PLANARCONFIG, &l_nPlanarConfig);
    7064             : 
    7065        1896 :     uint16_t l_nCompression = 0;
    7066             : 
    7067        1896 :     if (!TIFFGetField(l_hTIFF, TIFFTAG_COMPRESSION, &(l_nCompression)))
    7068           0 :         l_nCompression = COMPRESSION_NONE;
    7069             : 
    7070             :     /* -------------------------------------------------------------------- */
    7071             :     /*      Set the alpha channel if we find one.                           */
    7072             :     /* -------------------------------------------------------------------- */
    7073        1896 :     uint16_t *extraSamples = nullptr;
    7074        1896 :     uint16_t nExtraSamples = 0;
    7075        1896 :     if (TIFFGetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, &nExtraSamples,
    7076        2129 :                      &extraSamples) &&
    7077         233 :         nExtraSamples > 0)
    7078             :     {
    7079             :         // We need to allocate a new array as (current) libtiff
    7080             :         // versions will not like that we reuse the array we got from
    7081             :         // TIFFGetField().
    7082             :         uint16_t *pasNewExtraSamples = static_cast<uint16_t *>(
    7083         233 :             CPLMalloc(nExtraSamples * sizeof(uint16_t)));
    7084         233 :         memcpy(pasNewExtraSamples, extraSamples,
    7085         233 :                nExtraSamples * sizeof(uint16_t));
    7086         233 :         const char *pszAlpha = CPLGetConfigOption(
    7087             :             "GTIFF_ALPHA", CSLFetchNameValue(papszOptions, "ALPHA"));
    7088             :         const uint16_t nAlpha =
    7089         233 :             GTiffGetAlphaValue(pszAlpha, DEFAULT_ALPHA_TYPE);
    7090         233 :         const int nBaseSamples = l_nBands - nExtraSamples;
    7091         816 :         for (int iExtraBand = nBaseSamples + 1; iExtraBand <= l_nBands;
    7092             :              iExtraBand++)
    7093             :         {
    7094         583 :             if (poSrcDS->GetRasterBand(iExtraBand)->GetColorInterpretation() ==
    7095             :                 GCI_AlphaBand)
    7096             :             {
    7097         132 :                 pasNewExtraSamples[iExtraBand - nBaseSamples - 1] = nAlpha;
    7098         132 :                 if (!pszAlpha)
    7099             :                 {
    7100             :                     // Use the ALPHA metadata item from the source band, when
    7101             :                     // present, if no explicit ALPHA creation option
    7102         260 :                     pasNewExtraSamples[iExtraBand - nBaseSamples - 1] =
    7103         130 :                         GTiffGetAlphaValue(
    7104         130 :                             poSrcDS->GetRasterBand(iExtraBand)
    7105         130 :                                 ->GetMetadataItem("ALPHA", "IMAGE_STRUCTURE"),
    7106             :                             nAlpha);
    7107             :                 }
    7108             :             }
    7109             :         }
    7110         233 :         TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, nExtraSamples,
    7111             :                      pasNewExtraSamples);
    7112             : 
    7113         233 :         CPLFree(pasNewExtraSamples);
    7114             :     }
    7115             : 
    7116             :     /* -------------------------------------------------------------------- */
    7117             :     /*      If the output is jpeg compressed, and the input is RGB make     */
    7118             :     /*      sure we note that.                                              */
    7119             :     /* -------------------------------------------------------------------- */
    7120             : 
    7121        1896 :     if (l_nCompression == COMPRESSION_JPEG)
    7122             :     {
    7123         130 :         if (l_nBands >= 3 &&
    7124          56 :             (poSrcDS->GetRasterBand(1)->GetColorInterpretation() ==
    7125           0 :              GCI_YCbCr_YBand) &&
    7126           0 :             (poSrcDS->GetRasterBand(2)->GetColorInterpretation() ==
    7127         130 :              GCI_YCbCr_CbBand) &&
    7128           0 :             (poSrcDS->GetRasterBand(3)->GetColorInterpretation() ==
    7129             :              GCI_YCbCr_CrBand))
    7130             :         {
    7131             :             // Do nothing.
    7132             :         }
    7133             :         else
    7134             :         {
    7135             :             // Assume RGB if it is not explicitly YCbCr.
    7136          74 :             CPLDebug("GTiff", "Setting JPEGCOLORMODE_RGB");
    7137          74 :             TIFFSetField(l_hTIFF, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
    7138             :         }
    7139             :     }
    7140             : 
    7141             :     /* -------------------------------------------------------------------- */
    7142             :     /*      Does the source image consist of one band, with a palette?      */
    7143             :     /*      If so, copy over.                                               */
    7144             :     /* -------------------------------------------------------------------- */
    7145        1243 :     if ((l_nBands == 1 || l_nBands == 2) &&
    7146        3139 :         poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr &&
    7147             :         eType == GDT_Byte)
    7148             :     {
    7149          10 :         unsigned short anTRed[256] = {0};
    7150          10 :         unsigned short anTGreen[256] = {0};
    7151          10 :         unsigned short anTBlue[256] = {0};
    7152          10 :         GDALColorTable *poCT = poSrcDS->GetRasterBand(1)->GetColorTable();
    7153             : 
    7154        2570 :         for (int iColor = 0; iColor < 256; ++iColor)
    7155             :         {
    7156        2560 :             if (iColor < poCT->GetColorEntryCount())
    7157             :             {
    7158        1505 :                 GDALColorEntry sRGB = {0, 0, 0, 0};
    7159             : 
    7160        1505 :                 poCT->GetColorEntryAsRGB(iColor, &sRGB);
    7161             : 
    7162        3010 :                 anTRed[iColor] = GTiffDataset::ClampCTEntry(
    7163        1505 :                     iColor, 1, sRGB.c1, nColorTableMultiplier);
    7164        3010 :                 anTGreen[iColor] = GTiffDataset::ClampCTEntry(
    7165        1505 :                     iColor, 2, sRGB.c2, nColorTableMultiplier);
    7166        1505 :                 anTBlue[iColor] = GTiffDataset::ClampCTEntry(
    7167        1505 :                     iColor, 3, sRGB.c3, nColorTableMultiplier);
    7168             :             }
    7169             :             else
    7170             :             {
    7171        1055 :                 anTRed[iColor] = 0;
    7172        1055 :                 anTGreen[iColor] = 0;
    7173        1055 :                 anTBlue[iColor] = 0;
    7174             :             }
    7175             :         }
    7176             : 
    7177          10 :         if (!bForcePhotometric)
    7178          10 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
    7179          10 :         TIFFSetField(l_hTIFF, TIFFTAG_COLORMAP, anTRed, anTGreen, anTBlue);
    7180             :     }
    7181        1242 :     else if ((l_nBands == 1 || l_nBands == 2) &&
    7182        3128 :              poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr &&
    7183             :              eType == GDT_UInt16)
    7184             :     {
    7185             :         unsigned short *panTRed = static_cast<unsigned short *>(
    7186           1 :             CPLMalloc(65536 * sizeof(unsigned short)));
    7187             :         unsigned short *panTGreen = static_cast<unsigned short *>(
    7188           1 :             CPLMalloc(65536 * sizeof(unsigned short)));
    7189             :         unsigned short *panTBlue = static_cast<unsigned short *>(
    7190           1 :             CPLMalloc(65536 * sizeof(unsigned short)));
    7191             : 
    7192           1 :         GDALColorTable *poCT = poSrcDS->GetRasterBand(1)->GetColorTable();
    7193             : 
    7194       65537 :         for (int iColor = 0; iColor < 65536; ++iColor)
    7195             :         {
    7196       65536 :             if (iColor < poCT->GetColorEntryCount())
    7197             :             {
    7198       65536 :                 GDALColorEntry sRGB = {0, 0, 0, 0};
    7199             : 
    7200       65536 :                 poCT->GetColorEntryAsRGB(iColor, &sRGB);
    7201             : 
    7202      131072 :                 panTRed[iColor] = GTiffDataset::ClampCTEntry(
    7203       65536 :                     iColor, 1, sRGB.c1, nColorTableMultiplier);
    7204      131072 :                 panTGreen[iColor] = GTiffDataset::ClampCTEntry(
    7205       65536 :                     iColor, 2, sRGB.c2, nColorTableMultiplier);
    7206       65536 :                 panTBlue[iColor] = GTiffDataset::ClampCTEntry(
    7207       65536 :                     iColor, 3, sRGB.c3, nColorTableMultiplier);
    7208             :             }
    7209             :             else
    7210             :             {
    7211           0 :                 panTRed[iColor] = 0;
    7212           0 :                 panTGreen[iColor] = 0;
    7213           0 :                 panTBlue[iColor] = 0;
    7214             :             }
    7215             :         }
    7216             : 
    7217           1 :         if (!bForcePhotometric)
    7218           1 :             TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
    7219           1 :         TIFFSetField(l_hTIFF, TIFFTAG_COLORMAP, panTRed, panTGreen, panTBlue);
    7220             : 
    7221           1 :         CPLFree(panTRed);
    7222           1 :         CPLFree(panTGreen);
    7223           1 :         CPLFree(panTBlue);
    7224             :     }
    7225        1885 :     else if (poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr)
    7226           1 :         ReportError(
    7227             :             pszFilename, CE_Failure, CPLE_AppDefined,
    7228             :             "Unable to export color table to GeoTIFF file.  Color tables "
    7229             :             "can only be written to 1 band or 2 bands Byte or "
    7230             :             "UInt16 GeoTIFF files.");
    7231             : 
    7232        1896 :     if (l_nCompression == COMPRESSION_JPEG)
    7233             :     {
    7234          74 :         uint16_t l_nPhotometric = 0;
    7235          74 :         TIFFGetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, &l_nPhotometric);
    7236             :         // Check done in tif_jpeg.c later, but not with a very clear error
    7237             :         // message
    7238          74 :         if (l_nPhotometric == PHOTOMETRIC_PALETTE)
    7239             :         {
    7240           1 :             ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
    7241             :                         "JPEG compression not supported with paletted image");
    7242           1 :             XTIFFClose(l_hTIFF);
    7243           1 :             VSIUnlink(l_osTmpFilename);
    7244           1 :             CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    7245           1 :             return nullptr;
    7246             :         }
    7247             :     }
    7248             : 
    7249        1967 :     if (l_nBands == 2 &&
    7250        1895 :         poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr &&
    7251           0 :         (eType == GDT_Byte || eType == GDT_UInt16))
    7252             :     {
    7253           1 :         uint16_t v[1] = {EXTRASAMPLE_UNASSALPHA};
    7254             : 
    7255           1 :         TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, 1, v);
    7256             :     }
    7257             : 
    7258        1895 :     const int nMaskFlags = poSrcDS->GetRasterBand(1)->GetMaskFlags();
    7259        1895 :     bool bCreateMask = false;
    7260        3790 :     CPLString osHiddenStructuralMD;
    7261        1895 :     if ((l_nBands == 1 || l_nPlanarConfig == PLANARCONFIG_CONTIG) &&
    7262             :         bCopySrcOverviews)
    7263             :     {
    7264         165 :         osHiddenStructuralMD += "LAYOUT=IFDS_BEFORE_DATA\n";
    7265         165 :         osHiddenStructuralMD += "BLOCK_ORDER=ROW_MAJOR\n";
    7266         165 :         osHiddenStructuralMD += "BLOCK_LEADER=SIZE_AS_UINT4\n";
    7267         165 :         osHiddenStructuralMD += "BLOCK_TRAILER=LAST_4_BYTES_REPEATED\n";
    7268             :         osHiddenStructuralMD +=
    7269         165 :             "KNOWN_INCOMPATIBLE_EDITION=NO\n ";  // Final space intended, so
    7270             :                                                  // this can be replaced by YES
    7271             :     }
    7272        1895 :     if (!(nMaskFlags & (GMF_ALL_VALID | GMF_ALPHA | GMF_NODATA)) &&
    7273          31 :         (nMaskFlags & GMF_PER_DATASET) && !bStreaming)
    7274             :     {
    7275          28 :         bCreateMask = true;
    7276          55 :         if (GTiffDataset::MustCreateInternalMask() &&
    7277          27 :             !osHiddenStructuralMD.empty())
    7278             :         {
    7279          22 :             osHiddenStructuralMD += "MASK_INTERLEAVED_WITH_IMAGERY=YES\n";
    7280             :         }
    7281             :     }
    7282        1895 :     if (!osHiddenStructuralMD.empty())
    7283             :     {
    7284         165 :         const int nHiddenMDSize = static_cast<int>(osHiddenStructuralMD.size());
    7285             :         osHiddenStructuralMD =
    7286         165 :             CPLOPrintf("GDAL_STRUCTURAL_METADATA_SIZE=%06d bytes\n",
    7287         330 :                        nHiddenMDSize) +
    7288         165 :             osHiddenStructuralMD;
    7289         165 :         VSI_TIFFWrite(l_hTIFF, osHiddenStructuralMD.c_str(),
    7290             :                       osHiddenStructuralMD.size());
    7291             :     }
    7292             : 
    7293             :     // FIXME? libtiff writes extended tags in the order they are specified
    7294             :     // and not in increasing order.
    7295             : 
    7296             :     /* -------------------------------------------------------------------- */
    7297             :     /*      Transfer some TIFF specific metadata, if available.             */
    7298             :     /*      The return value will tell us if we need to try again later with*/
    7299             :     /*      PAM because the profile doesn't allow to write some metadata    */
    7300             :     /*      as TIFF tag                                                     */
    7301             :     /* -------------------------------------------------------------------- */
    7302        1895 :     const bool bHasWrittenMDInGeotiffTAG = GTiffDataset::WriteMetadata(
    7303             :         poSrcDS, l_hTIFF, false, eProfile, pszFilename, papszOptions);
    7304             : 
    7305             :     /* -------------------------------------------------------------------- */
    7306             :     /*      Write NoData value, if exist.                                   */
    7307             :     /* -------------------------------------------------------------------- */
    7308        1895 :     if (eProfile == GTiffProfile::GDALGEOTIFF)
    7309             :     {
    7310        1874 :         int bSuccess = FALSE;
    7311        1874 :         GDALRasterBand *poFirstBand = poSrcDS->GetRasterBand(1);
    7312        1874 :         if (poFirstBand->GetRasterDataType() == GDT_Int64)
    7313             :         {
    7314           2 :             const auto nNoData = poFirstBand->GetNoDataValueAsInt64(&bSuccess);
    7315           2 :             if (bSuccess)
    7316           1 :                 GTiffDataset::WriteNoDataValue(l_hTIFF, nNoData);
    7317             :         }
    7318        1872 :         else if (poFirstBand->GetRasterDataType() == GDT_UInt64)
    7319             :         {
    7320           2 :             const auto nNoData = poFirstBand->GetNoDataValueAsUInt64(&bSuccess);
    7321           2 :             if (bSuccess)
    7322           1 :                 GTiffDataset::WriteNoDataValue(l_hTIFF, nNoData);
    7323             :         }
    7324             :         else
    7325             :         {
    7326        1870 :             const auto dfNoData = poFirstBand->GetNoDataValue(&bSuccess);
    7327        1870 :             if (bSuccess)
    7328          89 :                 GTiffDataset::WriteNoDataValue(l_hTIFF, dfNoData);
    7329             :         }
    7330             :     }
    7331             : 
    7332             :     /* -------------------------------------------------------------------- */
    7333             :     /*      Are we addressing PixelIsPoint mode?                            */
    7334             :     /* -------------------------------------------------------------------- */
    7335        1895 :     bool bPixelIsPoint = false;
    7336        1895 :     bool bPointGeoIgnore = false;
    7337             : 
    7338        3211 :     if (poSrcDS->GetMetadataItem(GDALMD_AREA_OR_POINT) &&
    7339        1316 :         EQUAL(poSrcDS->GetMetadataItem(GDALMD_AREA_OR_POINT), GDALMD_AOP_POINT))
    7340             :     {
    7341          12 :         bPixelIsPoint = true;
    7342             :         bPointGeoIgnore =
    7343          12 :             CPLTestBool(CPLGetConfigOption("GTIFF_POINT_GEO_IGNORE", "FALSE"));
    7344             :     }
    7345             : 
    7346             :     /* -------------------------------------------------------------------- */
    7347             :     /*      Write affine transform if it is meaningful.                     */
    7348             :     /* -------------------------------------------------------------------- */
    7349        1895 :     const OGRSpatialReference *l_poSRS = nullptr;
    7350        1895 :     double l_adfGeoTransform[6] = {0.0};
    7351             : 
    7352        1895 :     if (poSrcDS->GetGeoTransform(l_adfGeoTransform) == CE_None)
    7353             :     {
    7354        1458 :         if (bGeoTIFF)
    7355             :         {
    7356        1453 :             l_poSRS = poSrcDS->GetSpatialRef();
    7357             : 
    7358        1453 :             if (l_adfGeoTransform[2] == 0.0 && l_adfGeoTransform[4] == 0.0 &&
    7359        1450 :                 l_adfGeoTransform[5] < 0.0)
    7360             :             {
    7361        1447 :                 double dfOffset = 0.0;
    7362             :                 {
    7363             :                     // In the case the SRS has a vertical component and we have
    7364             :                     // a single band, encode its scale/offset in the GeoTIFF
    7365             :                     // tags
    7366        1447 :                     int bHasScale = FALSE;
    7367             :                     double dfScale =
    7368        1447 :                         poSrcDS->GetRasterBand(1)->GetScale(&bHasScale);
    7369        1447 :                     int bHasOffset = FALSE;
    7370             :                     dfOffset =
    7371        1447 :                         poSrcDS->GetRasterBand(1)->GetOffset(&bHasOffset);
    7372             :                     const bool bApplyScaleOffset =
    7373        1451 :                         l_poSRS && l_poSRS->IsVertical() &&
    7374           4 :                         poSrcDS->GetRasterCount() == 1;
    7375        1447 :                     if (bApplyScaleOffset && !bHasScale)
    7376           0 :                         dfScale = 1.0;
    7377        1447 :                     if (!bApplyScaleOffset || !bHasOffset)
    7378        1443 :                         dfOffset = 0.0;
    7379             :                     const double adfPixelScale[3] = {
    7380        1447 :                         l_adfGeoTransform[1], fabs(l_adfGeoTransform[5]),
    7381        1447 :                         bApplyScaleOffset ? dfScale : 0.0};
    7382             : 
    7383        1447 :                     TIFFSetField(l_hTIFF, TIFFTAG_GEOPIXELSCALE, 3,
    7384             :                                  adfPixelScale);
    7385             :                 }
    7386             : 
    7387        1447 :                 double adfTiePoints[6] = {0.0,
    7388             :                                           0.0,
    7389             :                                           0.0,
    7390        1447 :                                           l_adfGeoTransform[0],
    7391        1447 :                                           l_adfGeoTransform[3],
    7392        1447 :                                           dfOffset};
    7393             : 
    7394        1447 :                 if (bPixelIsPoint && !bPointGeoIgnore)
    7395             :                 {
    7396           8 :                     adfTiePoints[3] +=
    7397           8 :                         l_adfGeoTransform[1] * 0.5 + l_adfGeoTransform[2] * 0.5;
    7398           8 :                     adfTiePoints[4] +=
    7399           8 :                         l_adfGeoTransform[4] * 0.5 + l_adfGeoTransform[5] * 0.5;
    7400             :                 }
    7401             : 
    7402        1447 :                 TIFFSetField(l_hTIFF, TIFFTAG_GEOTIEPOINTS, 6, adfTiePoints);
    7403             :             }
    7404             :             else
    7405             :             {
    7406           6 :                 double adfMatrix[16] = {0.0};
    7407             : 
    7408           6 :                 adfMatrix[0] = l_adfGeoTransform[1];
    7409           6 :                 adfMatrix[1] = l_adfGeoTransform[2];
    7410           6 :                 adfMatrix[3] = l_adfGeoTransform[0];
    7411           6 :                 adfMatrix[4] = l_adfGeoTransform[4];
    7412           6 :                 adfMatrix[5] = l_adfGeoTransform[5];
    7413           6 :                 adfMatrix[7] = l_adfGeoTransform[3];
    7414           6 :                 adfMatrix[15] = 1.0;
    7415             : 
    7416           6 :                 if (bPixelIsPoint && !bPointGeoIgnore)
    7417             :                 {
    7418           0 :                     adfMatrix[3] +=
    7419           0 :                         l_adfGeoTransform[1] * 0.5 + l_adfGeoTransform[2] * 0.5;
    7420           0 :                     adfMatrix[7] +=
    7421           0 :                         l_adfGeoTransform[4] * 0.5 + l_adfGeoTransform[5] * 0.5;
    7422             :                 }
    7423             : 
    7424           6 :                 TIFFSetField(l_hTIFF, TIFFTAG_GEOTRANSMATRIX, 16, adfMatrix);
    7425             :             }
    7426             :         }
    7427             : 
    7428             :         /* --------------------------------------------------------------------
    7429             :          */
    7430             :         /*      Do we need a TFW file? */
    7431             :         /* --------------------------------------------------------------------
    7432             :          */
    7433        1458 :         if (CPLFetchBool(papszOptions, "TFW", false))
    7434           2 :             GDALWriteWorldFile(pszFilename, "tfw", l_adfGeoTransform);
    7435        1456 :         else if (CPLFetchBool(papszOptions, "WORLDFILE", false))
    7436           1 :             GDALWriteWorldFile(pszFilename, "wld", l_adfGeoTransform);
    7437             :     }
    7438             : 
    7439             :     /* -------------------------------------------------------------------- */
    7440             :     /*      Otherwise write tiepoints if they are available.                */
    7441             :     /* -------------------------------------------------------------------- */
    7442         437 :     else if (poSrcDS->GetGCPCount() > 0 && bGeoTIFF)
    7443             :     {
    7444          12 :         const GDAL_GCP *pasGCPs = poSrcDS->GetGCPs();
    7445             :         double *padfTiePoints = static_cast<double *>(
    7446          12 :             CPLMalloc(6 * sizeof(double) * poSrcDS->GetGCPCount()));
    7447             : 
    7448          60 :         for (int iGCP = 0; iGCP < poSrcDS->GetGCPCount(); ++iGCP)
    7449             :         {
    7450             : 
    7451          48 :             padfTiePoints[iGCP * 6 + 0] = pasGCPs[iGCP].dfGCPPixel;
    7452          48 :             padfTiePoints[iGCP * 6 + 1] = pasGCPs[iGCP].dfGCPLine;
    7453          48 :             padfTiePoints[iGCP * 6 + 2] = 0;
    7454          48 :             padfTiePoints[iGCP * 6 + 3] = pasGCPs[iGCP].dfGCPX;
    7455          48 :             padfTiePoints[iGCP * 6 + 4] = pasGCPs[iGCP].dfGCPY;
    7456          48 :             padfTiePoints[iGCP * 6 + 5] = pasGCPs[iGCP].dfGCPZ;
    7457             : 
    7458          48 :             if (bPixelIsPoint && !bPointGeoIgnore)
    7459             :             {
    7460           4 :                 padfTiePoints[iGCP * 6 + 0] -= 0.5;
    7461           4 :                 padfTiePoints[iGCP * 6 + 1] -= 0.5;
    7462             :             }
    7463             :         }
    7464             : 
    7465          12 :         TIFFSetField(l_hTIFF, TIFFTAG_GEOTIEPOINTS, 6 * poSrcDS->GetGCPCount(),
    7466             :                      padfTiePoints);
    7467          12 :         CPLFree(padfTiePoints);
    7468             : 
    7469          12 :         l_poSRS = poSrcDS->GetGCPSpatialRef();
    7470             : 
    7471          24 :         if (CPLFetchBool(papszOptions, "TFW", false) ||
    7472          12 :             CPLFetchBool(papszOptions, "WORLDFILE", false))
    7473             :         {
    7474           0 :             ReportError(
    7475             :                 pszFilename, CE_Warning, CPLE_AppDefined,
    7476             :                 "TFW=ON or WORLDFILE=ON creation options are ignored when "
    7477             :                 "GCPs are available");
    7478             :         }
    7479             :     }
    7480             :     else
    7481             :     {
    7482         425 :         l_poSRS = poSrcDS->GetSpatialRef();
    7483             :     }
    7484             : 
    7485             :     /* -------------------------------------------------------------------- */
    7486             :     /*      Copy xml:XMP data                                               */
    7487             :     /* -------------------------------------------------------------------- */
    7488        1895 :     char **papszXMP = poSrcDS->GetMetadata("xml:XMP");
    7489        1895 :     if (papszXMP != nullptr && *papszXMP != nullptr)
    7490             :     {
    7491           9 :         int nTagSize = static_cast<int>(strlen(*papszXMP));
    7492           9 :         TIFFSetField(l_hTIFF, TIFFTAG_XMLPACKET, nTagSize, *papszXMP);
    7493             :     }
    7494             : 
    7495             :     /* -------------------------------------------------------------------- */
    7496             :     /*      Write the projection information, if possible.                  */
    7497             :     /* -------------------------------------------------------------------- */
    7498        1895 :     const bool bHasProjection = l_poSRS != nullptr;
    7499        1895 :     bool bExportSRSToPAM = false;
    7500        1895 :     if ((bHasProjection || bPixelIsPoint) && bGeoTIFF)
    7501             :     {
    7502        1449 :         GTIF *psGTIF = GTiffDataset::GTIFNew(l_hTIFF);
    7503             : 
    7504        1449 :         if (bHasProjection)
    7505             :         {
    7506        1449 :             const auto eGeoTIFFKeysFlavor = GetGTIFFKeysFlavor(papszOptions);
    7507        1449 :             if (IsSRSCompatibleOfGeoTIFF(l_poSRS, eGeoTIFFKeysFlavor))
    7508             :             {
    7509        1449 :                 GTIFSetFromOGISDefnEx(
    7510             :                     psGTIF,
    7511             :                     OGRSpatialReference::ToHandle(
    7512             :                         const_cast<OGRSpatialReference *>(l_poSRS)),
    7513             :                     eGeoTIFFKeysFlavor, GetGeoTIFFVersion(papszOptions));
    7514             :             }
    7515             :             else
    7516             :             {
    7517           0 :                 bExportSRSToPAM = true;
    7518             :             }
    7519             :         }
    7520             : 
    7521        1449 :         if (bPixelIsPoint)
    7522             :         {
    7523          12 :             GTIFKeySet(psGTIF, GTRasterTypeGeoKey, TYPE_SHORT, 1,
    7524             :                        RasterPixelIsPoint);
    7525             :         }
    7526             : 
    7527        1449 :         GTIFWriteKeys(psGTIF);
    7528        1449 :         GTIFFree(psGTIF);
    7529             :     }
    7530             : 
    7531        1895 :     bool l_bDontReloadFirstBlock = false;
    7532             : 
    7533             : #ifdef HAVE_LIBJPEG
    7534        1895 :     if (bCopyFromJPEG)
    7535             :     {
    7536          12 :         GTIFF_CopyFromJPEG_WriteAdditionalTags(l_hTIFF, poSrcDS);
    7537             :     }
    7538             : #endif
    7539             : 
    7540             :     /* -------------------------------------------------------------------- */
    7541             :     /*      Cleanup                                                         */
    7542             :     /* -------------------------------------------------------------------- */
    7543        1895 :     if (bCopySrcOverviews)
    7544             :     {
    7545         166 :         TIFFDeferStrileArrayWriting(l_hTIFF);
    7546             :     }
    7547        1895 :     TIFFWriteCheck(l_hTIFF, TIFFIsTiled(l_hTIFF), "GTiffCreateCopy()");
    7548        1895 :     TIFFWriteDirectory(l_hTIFF);
    7549        1895 :     if (bStreaming)
    7550             :     {
    7551             :         // We need to write twice the directory to be sure that custom
    7552             :         // TIFF tags are correctly sorted and that padding bytes have been
    7553             :         // added.
    7554           5 :         TIFFSetDirectory(l_hTIFF, 0);
    7555           5 :         TIFFWriteDirectory(l_hTIFF);
    7556             : 
    7557           5 :         if (VSIFSeekL(l_fpL, 0, SEEK_END) != 0)
    7558           0 :             ReportError(pszFilename, CE_Failure, CPLE_FileIO, "Cannot seek");
    7559           5 :         const int nSize = static_cast<int>(VSIFTellL(l_fpL));
    7560             : 
    7561           5 :         vsi_l_offset nDataLength = 0;
    7562           5 :         VSIGetMemFileBuffer(l_osTmpFilename, &nDataLength, FALSE);
    7563           5 :         TIFFSetDirectory(l_hTIFF, 0);
    7564           5 :         GTiffFillStreamableOffsetAndCount(l_hTIFF, nSize);
    7565           5 :         TIFFWriteDirectory(l_hTIFF);
    7566             :     }
    7567        1895 :     const auto nDirCount = TIFFNumberOfDirectories(l_hTIFF);
    7568        1895 :     if (nDirCount >= 1)
    7569             :     {
    7570        1888 :         TIFFSetDirectory(l_hTIFF, static_cast<tdir_t>(nDirCount - 1));
    7571             :     }
    7572        1895 :     const toff_t l_nDirOffset = TIFFCurrentDirOffset(l_hTIFF);
    7573        1895 :     TIFFFlush(l_hTIFF);
    7574        1895 :     XTIFFClose(l_hTIFF);
    7575             : 
    7576        1895 :     VSIFSeekL(l_fpL, 0, SEEK_SET);
    7577             : 
    7578             :     // fpStreaming will assigned to the instance and not closed here.
    7579        1895 :     VSILFILE *fpStreaming = nullptr;
    7580        1895 :     if (bStreaming)
    7581             :     {
    7582           5 :         vsi_l_offset nDataLength = 0;
    7583             :         void *pabyBuffer =
    7584           5 :             VSIGetMemFileBuffer(l_osTmpFilename, &nDataLength, FALSE);
    7585           5 :         fpStreaming = VSIFOpenL(pszFilename, "wb");
    7586           5 :         if (fpStreaming == nullptr)
    7587             :         {
    7588           1 :             VSIUnlink(l_osTmpFilename);
    7589           1 :             CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    7590           1 :             return nullptr;
    7591             :         }
    7592           4 :         if (static_cast<vsi_l_offset>(VSIFWriteL(pabyBuffer, 1,
    7593             :                                                  static_cast<int>(nDataLength),
    7594           4 :                                                  fpStreaming)) != nDataLength)
    7595             :         {
    7596           0 :             ReportError(pszFilename, CE_Failure, CPLE_FileIO,
    7597             :                         "Could not write %d bytes",
    7598             :                         static_cast<int>(nDataLength));
    7599           0 :             CPL_IGNORE_RET_VAL(VSIFCloseL(fpStreaming));
    7600           0 :             VSIUnlink(l_osTmpFilename);
    7601           0 :             CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    7602           0 :             return nullptr;
    7603             :         }
    7604             :     }
    7605             : 
    7606             :     /* -------------------------------------------------------------------- */
    7607             :     /*      Re-open as a dataset and copy over missing metadata using       */
    7608             :     /*      PAM facilities.                                                 */
    7609             :     /* -------------------------------------------------------------------- */
    7610        1894 :     l_hTIFF = VSI_TIFFOpen(bStreaming ? l_osTmpFilename.c_str() : pszFilename,
    7611             :                            "r+", l_fpL);
    7612        1894 :     if (l_hTIFF == nullptr)
    7613             :     {
    7614          11 :         if (bStreaming)
    7615           0 :             VSIUnlink(l_osTmpFilename);
    7616          11 :         CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
    7617          11 :         return nullptr;
    7618             :     }
    7619             : 
    7620             :     /* -------------------------------------------------------------------- */
    7621             :     /*      Create a corresponding GDALDataset.                             */
    7622             :     /* -------------------------------------------------------------------- */
    7623        1883 :     GTiffDataset *poDS = new GTiffDataset();
    7624        1883 :     poDS->SetDescription(pszFilename);
    7625        1883 :     poDS->eAccess = GA_Update;
    7626        1883 :     poDS->m_pszFilename = CPLStrdup(pszFilename);
    7627        1883 :     poDS->m_fpL = l_fpL;
    7628        1883 :     poDS->m_bIMDRPCMetadataLoaded = true;
    7629        1883 :     poDS->m_nColorTableMultiplier = nColorTableMultiplier;
    7630             : 
    7631        1883 :     const bool bAppend = CPLFetchBool(papszOptions, "APPEND_SUBDATASET", false);
    7632        3765 :     if (poDS->OpenOffset(l_hTIFF,
    7633        1882 :                          bAppend ? l_nDirOffset : TIFFCurrentDirOffset(l_hTIFF),
    7634             :                          GA_Update,
    7635             :                          false,  // bAllowRGBAInterface
    7636             :                          true    // bReadGeoTransform
    7637        1883 :                          ) != CE_None)
    7638             :     {
    7639           0 :         delete poDS;
    7640           0 :         if (bStreaming)
    7641           0 :             VSIUnlink(l_osTmpFilename);
    7642           0 :         return nullptr;
    7643             :     }
    7644             : 
    7645             :     // Legacy... Patch back GDT_Int8 type to GDT_Byte if the user used
    7646             :     // PIXELTYPE=SIGNEDBYTE
    7647        1883 :     const char *pszPixelType = CSLFetchNameValue(papszOptions, "PIXELTYPE");
    7648        1883 :     if (pszPixelType == nullptr)
    7649        1878 :         pszPixelType = "";
    7650        1883 :     if (eType == GDT_Byte && EQUAL(pszPixelType, "SIGNEDBYTE"))
    7651             :     {
    7652          10 :         for (int i = 0; i < poDS->nBands; ++i)
    7653             :         {
    7654           5 :             auto poBand = static_cast<GTiffRasterBand *>(poDS->papoBands[i]);
    7655           5 :             poBand->eDataType = GDT_Byte;
    7656           5 :             poBand->EnablePixelTypeSignedByteWarning(false);
    7657           5 :             poBand->SetMetadataItem("PIXELTYPE", "SIGNEDBYTE",
    7658             :                                     "IMAGE_STRUCTURE");
    7659           5 :             poBand->EnablePixelTypeSignedByteWarning(true);
    7660             :         }
    7661             :     }
    7662             : 
    7663        1883 :     poDS->oOvManager.Initialize(poDS, pszFilename);
    7664             : 
    7665        1883 :     if (bStreaming)
    7666             :     {
    7667           4 :         VSIUnlink(l_osTmpFilename);
    7668           4 :         poDS->m_fpToWrite = fpStreaming;
    7669             :     }
    7670        1883 :     poDS->m_eProfile = eProfile;
    7671             : 
    7672        1883 :     int nCloneInfoFlags = GCIF_PAM_DEFAULT & ~GCIF_MASK;
    7673             : 
    7674             :     // If we explicitly asked not to tag the alpha band as such, do not
    7675             :     // reintroduce this alpha color interpretation in PAM.
    7676        1883 :     if (poSrcDS->GetRasterBand(l_nBands)->GetColorInterpretation() ==
    7677        1997 :             GCI_AlphaBand &&
    7678         114 :         GTiffGetAlphaValue(
    7679             :             CPLGetConfigOption("GTIFF_ALPHA",
    7680             :                                CSLFetchNameValue(papszOptions, "ALPHA")),
    7681             :             DEFAULT_ALPHA_TYPE) == EXTRASAMPLE_UNSPECIFIED)
    7682             :     {
    7683           1 :         nCloneInfoFlags &= ~GCIF_COLORINTERP;
    7684             :     }
    7685             :     // Ignore source band color interpretation if requesting PHOTOMETRIC=RGB
    7686        3051 :     else if (l_nBands >= 3 &&
    7687        1169 :              EQUAL(CSLFetchNameValueDef(papszOptions, "PHOTOMETRIC", ""),
    7688             :                    "RGB"))
    7689             :     {
    7690          28 :         for (int i = 1; i <= 3; i++)
    7691             :         {
    7692          21 :             poDS->GetRasterBand(i)->SetColorInterpretation(
    7693          21 :                 static_cast<GDALColorInterp>(GCI_RedBand + (i - 1)));
    7694             :         }
    7695           7 :         nCloneInfoFlags &= ~GCIF_COLORINTERP;
    7696           9 :         if (!(l_nBands == 4 &&
    7697           2 :               CSLFetchNameValue(papszOptions, "ALPHA") != nullptr))
    7698             :         {
    7699          15 :             for (int i = 4; i <= l_nBands; i++)
    7700             :             {
    7701          18 :                 poDS->GetRasterBand(i)->SetColorInterpretation(
    7702           9 :                     poSrcDS->GetRasterBand(i)->GetColorInterpretation());
    7703             :             }
    7704             :         }
    7705             :     }
    7706             : 
    7707             :     CPLString osOldGTIFF_REPORT_COMPD_CSVal(
    7708        3766 :         CPLGetConfigOption("GTIFF_REPORT_COMPD_CS", ""));
    7709        1883 :     CPLSetThreadLocalConfigOption("GTIFF_REPORT_COMPD_CS", "YES");
    7710        1883 :     poDS->CloneInfo(poSrcDS, nCloneInfoFlags);
    7711        1883 :     CPLSetThreadLocalConfigOption("GTIFF_REPORT_COMPD_CS",
    7712        1883 :                                   osOldGTIFF_REPORT_COMPD_CSVal.empty()
    7713             :                                       ? nullptr
    7714           0 :                                       : osOldGTIFF_REPORT_COMPD_CSVal.c_str());
    7715             : 
    7716        1900 :     if ((!bGeoTIFF || bExportSRSToPAM) &&
    7717          17 :         (poDS->GetPamFlags() & GPF_DISABLED) == 0)
    7718             :     {
    7719             :         // Copy georeferencing info to PAM if the profile is not GeoTIFF
    7720          16 :         poDS->GDALPamDataset::SetSpatialRef(poDS->GetSpatialRef());
    7721             :         double adfGeoTransform[6];
    7722          16 :         if (poDS->GetGeoTransform(adfGeoTransform) == CE_None)
    7723             :         {
    7724           5 :             poDS->GDALPamDataset::SetGeoTransform(adfGeoTransform);
    7725             :         }
    7726          16 :         poDS->GDALPamDataset::SetGCPs(poDS->GetGCPCount(), poDS->GetGCPs(),
    7727             :                                       poDS->GetGCPSpatialRef());
    7728             :     }
    7729             : 
    7730        1883 :     poDS->m_papszCreationOptions = CSLDuplicate(papszOptions);
    7731        1883 :     poDS->m_bDontReloadFirstBlock = l_bDontReloadFirstBlock;
    7732             : 
    7733             :     /* -------------------------------------------------------------------- */
    7734             :     /*      CloneInfo() does not merge metadata, it just replaces it        */
    7735             :     /*      totally.  So we have to merge it.                               */
    7736             :     /* -------------------------------------------------------------------- */
    7737             : 
    7738        1883 :     char **papszSRC_MD = poSrcDS->GetMetadata();
    7739        1883 :     char **papszDST_MD = CSLDuplicate(poDS->GetMetadata());
    7740             : 
    7741        1883 :     papszDST_MD = CSLMerge(papszDST_MD, papszSRC_MD);
    7742             : 
    7743        1883 :     poDS->SetMetadata(papszDST_MD);
    7744        1883 :     CSLDestroy(papszDST_MD);
    7745             : 
    7746             :     // Depending on the PHOTOMETRIC tag, the TIFF file may not have the same
    7747             :     // band count as the source. Will fail later in GDALDatasetCopyWholeRaster
    7748             :     // anyway.
    7749        6558 :     for (int nBand = 1;
    7750        6558 :          nBand <= std::min(poDS->GetRasterCount(), poSrcDS->GetRasterCount());
    7751             :          ++nBand)
    7752             :     {
    7753        4675 :         GDALRasterBand *poSrcBand = poSrcDS->GetRasterBand(nBand);
    7754        4675 :         GDALRasterBand *poDstBand = poDS->GetRasterBand(nBand);
    7755        4675 :         papszSRC_MD = poSrcBand->GetMetadata();
    7756        4675 :         papszDST_MD = CSLDuplicate(poDstBand->GetMetadata());
    7757             : 
    7758        4675 :         papszDST_MD = CSLMerge(papszDST_MD, papszSRC_MD);
    7759             : 
    7760        4675 :         poDstBand->SetMetadata(papszDST_MD);
    7761        4675 :         CSLDestroy(papszDST_MD);
    7762             : 
    7763        4675 :         char **papszCatNames = poSrcBand->GetCategoryNames();
    7764        4675 :         if (nullptr != papszCatNames)
    7765           0 :             poDstBand->SetCategoryNames(papszCatNames);
    7766             :     }
    7767             : 
    7768        1883 :     l_hTIFF = static_cast<TIFF *>(poDS->GetInternalHandle(nullptr));
    7769             : 
    7770             :     /* -------------------------------------------------------------------- */
    7771             :     /*      Handle forcing xml:ESRI data to be written to PAM.              */
    7772             :     /* -------------------------------------------------------------------- */
    7773        1883 :     if (CPLTestBool(CPLGetConfigOption("ESRI_XML_PAM", "NO")))
    7774             :     {
    7775           1 :         char **papszESRIMD = poSrcDS->GetMetadata("xml:ESRI");
    7776           1 :         if (papszESRIMD)
    7777             :         {
    7778           1 :             poDS->SetMetadata(papszESRIMD, "xml:ESRI");
    7779             :         }
    7780             :     }
    7781             : 
    7782             :     /* -------------------------------------------------------------------- */
    7783             :     /*      Second chance: now that we have a PAM dataset, it is possible   */
    7784             :     /*      to write metadata that we could not write as a TIFF tag.        */
    7785             :     /* -------------------------------------------------------------------- */
    7786        1883 :     if (!bHasWrittenMDInGeotiffTAG && !bStreaming)
    7787             :     {
    7788           6 :         GTiffDataset::WriteMetadata(
    7789             :             poDS, l_hTIFF, true, eProfile, pszFilename, papszOptions,
    7790             :             true /* don't write RPC and IMD file again */);
    7791             :     }
    7792             : 
    7793        1883 :     if (!bStreaming)
    7794        1879 :         GTiffDataset::WriteRPC(poDS, l_hTIFF, true, eProfile, pszFilename,
    7795             :                                papszOptions,
    7796             :                                true /* write only in PAM AND if needed */);
    7797             : 
    7798             :     // Propagate ISIS3 or VICAR metadata, but only as PAM metadata.
    7799        5649 :     for (const char *pszMDD : {"json:ISIS3", "json:VICAR"})
    7800             :     {
    7801        3766 :         char **papszMD = poSrcDS->GetMetadata(pszMDD);
    7802        3766 :         if (papszMD)
    7803             :         {
    7804           3 :             poDS->SetMetadata(papszMD, pszMDD);
    7805           3 :             poDS->PushMetadataToPam();
    7806             :         }
    7807             :     }
    7808             : 
    7809        1883 :     poDS->m_bWriteCOGLayout = bCopySrcOverviews;
    7810             : 
    7811             :     // To avoid unnecessary directory rewriting.
    7812        1883 :     poDS->m_bMetadataChanged = false;
    7813        1883 :     poDS->m_bGeoTIFFInfoChanged = false;
    7814        1883 :     poDS->m_bNoDataChanged = false;
    7815        1883 :     poDS->m_bForceUnsetGTOrGCPs = false;
    7816        1883 :     poDS->m_bForceUnsetProjection = false;
    7817        1883 :     poDS->m_bStreamingOut = bStreaming;
    7818             : 
    7819             :     // Don't try to load external metadata files (#6597).
    7820        1883 :     poDS->m_bIMDRPCMetadataLoaded = true;
    7821             : 
    7822             :     // We must re-set the compression level at this point, since it has been
    7823             :     // lost a few lines above when closing the newly create TIFF file The
    7824             :     // TIFFTAG_ZIPQUALITY & TIFFTAG_JPEGQUALITY are not store in the TIFF file.
    7825             :     // They are just TIFF session parameters.
    7826             : 
    7827        1883 :     poDS->m_nZLevel = GTiffGetZLevel(papszOptions);
    7828        1883 :     poDS->m_nLZMAPreset = GTiffGetLZMAPreset(papszOptions);
    7829        1883 :     poDS->m_nZSTDLevel = GTiffGetZSTDPreset(papszOptions);
    7830        1883 :     poDS->m_nWebPLevel = GTiffGetWebPLevel(papszOptions);
    7831        1883 :     poDS->m_bWebPLossless = GTiffGetWebPLossless(papszOptions);
    7832        1886 :     if (poDS->m_nWebPLevel != 100 && poDS->m_bWebPLossless &&
    7833           3 :         CSLFetchNameValue(papszOptions, "WEBP_LEVEL"))
    7834             :     {
    7835           0 :         CPLError(CE_Warning, CPLE_AppDefined,
    7836             :                  "WEBP_LEVEL is specified, but WEBP_LOSSLESS=YES. "
    7837             :                  "WEBP_LEVEL will be ignored.");
    7838             :     }
    7839        1883 :     poDS->m_nJpegQuality = GTiffGetJpegQuality(papszOptions);
    7840        1883 :     poDS->m_nJpegTablesMode = GTiffGetJpegTablesMode(papszOptions);
    7841        1883 :     poDS->GetDiscardLsbOption(papszOptions);
    7842        1883 :     poDS->m_dfMaxZError = GTiffGetLERCMaxZError(papszOptions);
    7843        1883 :     poDS->m_dfMaxZErrorOverview = GTiffGetLERCMaxZErrorOverview(papszOptions);
    7844             : #if HAVE_JXL
    7845        1883 :     poDS->m_bJXLLossless = GTiffGetJXLLossless(papszOptions);
    7846        1883 :     poDS->m_nJXLEffort = GTiffGetJXLEffort(papszOptions);
    7847        1883 :     poDS->m_fJXLDistance = GTiffGetJXLDistance(papszOptions);
    7848        1883 :     poDS->m_fJXLAlphaDistance = GTiffGetJXLAlphaDistance(papszOptions);
    7849             : #endif
    7850        1883 :     poDS->InitCreationOrOpenOptions(true, papszOptions);
    7851             : 
    7852        1883 :     if (l_nCompression == COMPRESSION_ADOBE_DEFLATE ||
    7853        1858 :         l_nCompression == COMPRESSION_LERC)
    7854             :     {
    7855          96 :         GTiffSetDeflateSubCodec(l_hTIFF);
    7856             : 
    7857          96 :         if (poDS->m_nZLevel != -1)
    7858             :         {
    7859          12 :             TIFFSetField(l_hTIFF, TIFFTAG_ZIPQUALITY, poDS->m_nZLevel);
    7860             :         }
    7861             :     }
    7862        1883 :     if (l_nCompression == COMPRESSION_JPEG)
    7863             :     {
    7864          73 :         if (poDS->m_nJpegQuality != -1)
    7865             :         {
    7866           9 :             TIFFSetField(l_hTIFF, TIFFTAG_JPEGQUALITY, poDS->m_nJpegQuality);
    7867             :         }
    7868          73 :         TIFFSetField(l_hTIFF, TIFFTAG_JPEGTABLESMODE, poDS->m_nJpegTablesMode);
    7869             :     }
    7870        1883 :     if (l_nCompression == COMPRESSION_LZMA)
    7871             :     {
    7872           7 :         if (poDS->m_nLZMAPreset != -1)
    7873             :         {
    7874           6 :             TIFFSetField(l_hTIFF, TIFFTAG_LZMAPRESET, poDS->m_nLZMAPreset);
    7875             :         }
    7876             :     }
    7877        1883 :     if (l_nCompression == COMPRESSION_ZSTD ||
    7878        1876 :         l_nCompression == COMPRESSION_LERC)
    7879             :     {
    7880          78 :         if (poDS->m_nZSTDLevel != -1)
    7881             :         {
    7882           8 :             TIFFSetField(l_hTIFF, TIFFTAG_ZSTD_LEVEL, poDS->m_nZSTDLevel);
    7883             :         }
    7884             :     }
    7885        1883 :     if (l_nCompression == COMPRESSION_LERC)
    7886             :     {
    7887          71 :         TIFFSetField(l_hTIFF, TIFFTAG_LERC_MAXZERROR, poDS->m_dfMaxZError);
    7888             :     }
    7889             : #if HAVE_JXL
    7890        1883 :     if (l_nCompression == COMPRESSION_JXL ||
    7891        1883 :         l_nCompression == COMPRESSION_JXL_DNG_1_7)
    7892             :     {
    7893          88 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_LOSSYNESS,
    7894          88 :                      poDS->m_bJXLLossless ? JXL_LOSSLESS : JXL_LOSSY);
    7895          88 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_EFFORT, poDS->m_nJXLEffort);
    7896          88 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_DISTANCE, poDS->m_fJXLDistance);
    7897          88 :         TIFFSetField(l_hTIFF, TIFFTAG_JXL_ALPHA_DISTANCE,
    7898          88 :                      poDS->m_fJXLAlphaDistance);
    7899             :     }
    7900             : #endif
    7901        1883 :     if (l_nCompression == COMPRESSION_WEBP)
    7902             :     {
    7903          14 :         if (poDS->m_nWebPLevel != -1)
    7904             :         {
    7905          14 :             TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LEVEL, poDS->m_nWebPLevel);
    7906             :         }
    7907             : 
    7908          14 :         if (poDS->m_bWebPLossless)
    7909             :         {
    7910           5 :             TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LOSSLESS, poDS->m_bWebPLossless);
    7911             :         }
    7912             :     }
    7913             : 
    7914             :     /* -------------------------------------------------------------------- */
    7915             :     /*      Do we want to ensure all blocks get written out on close to     */
    7916             :     /*      avoid sparse files?                                             */
    7917             :     /* -------------------------------------------------------------------- */
    7918        1883 :     if (!CPLFetchBool(papszOptions, "SPARSE_OK", false))
    7919        1855 :         poDS->m_bFillEmptyTilesAtClosing = true;
    7920             : 
    7921        1883 :     poDS->m_bWriteEmptyTiles =
    7922        3604 :         (bCopySrcOverviews && poDS->m_bFillEmptyTilesAtClosing) || bStreaming ||
    7923        1721 :         (poDS->m_nCompression != COMPRESSION_NONE &&
    7924         268 :          poDS->m_bFillEmptyTilesAtClosing);
    7925             :     // Only required for people writing non-compressed striped files in the
    7926             :     // rightorder and wanting all tstrips to be written in the same order
    7927             :     // so that the end result can be memory mapped without knowledge of each
    7928             :     // strip offset
    7929        1883 :     if (CPLTestBool(CSLFetchNameValueDef(
    7930        3766 :             papszOptions, "WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")) ||
    7931        1883 :         CPLTestBool(CSLFetchNameValueDef(
    7932             :             papszOptions, "@WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")))
    7933             :     {
    7934           0 :         poDS->m_bWriteEmptyTiles = true;
    7935             :     }
    7936             : 
    7937             :     // Precreate (internal) mask, so that the IBuildOverviews() below
    7938             :     // has a chance to create also the overviews of the mask.
    7939        1883 :     CPLErr eErr = CE_None;
    7940             : 
    7941        1883 :     if (bCreateMask)
    7942             :     {
    7943          28 :         eErr = poDS->CreateMaskBand(nMaskFlags);
    7944          28 :         if (poDS->m_poMaskDS)
    7945             :         {
    7946          27 :             poDS->m_poMaskDS->m_bFillEmptyTilesAtClosing =
    7947          27 :                 poDS->m_bFillEmptyTilesAtClosing;
    7948          27 :             poDS->m_poMaskDS->m_bWriteEmptyTiles = poDS->m_bWriteEmptyTiles;
    7949             :         }
    7950             :     }
    7951             : 
    7952             :     /* -------------------------------------------------------------------- */
    7953             :     /*      Create and then copy existing overviews if requested            */
    7954             :     /*  We do it such that all the IFDs are at the beginning of the file,   */
    7955             :     /*  and that the imagery data for the smallest overview is written      */
    7956             :     /*  first, that way the file is more usable when embedded in a          */
    7957             :     /*  compressed stream.                                                  */
    7958             :     /* -------------------------------------------------------------------- */
    7959             : 
    7960             :     // For scaled progress due to overview copying.
    7961        1883 :     const int nBandsWidthMask = l_nBands + (bCreateMask ? 1 : 0);
    7962        1883 :     double dfTotalPixels =
    7963        1883 :         static_cast<double>(nXSize) * nYSize * nBandsWidthMask;
    7964        1883 :     double dfCurPixels = 0;
    7965             : 
    7966        1883 :     if (eErr == CE_None && bCopySrcOverviews)
    7967             :     {
    7968           0 :         std::unique_ptr<GDALDataset> poMaskOvrDS;
    7969             :         const char *pszMaskOvrDS =
    7970         163 :             CSLFetchNameValue(papszOptions, "@MASK_OVERVIEW_DATASET");
    7971         163 :         if (pszMaskOvrDS)
    7972             :         {
    7973           8 :             poMaskOvrDS.reset(GDALDataset::Open(pszMaskOvrDS));
    7974           8 :             if (!poMaskOvrDS)
    7975             :             {
    7976           0 :                 delete poDS;
    7977           0 :                 return nullptr;
    7978             :             }
    7979           8 :             if (poMaskOvrDS->GetRasterCount() != 1)
    7980             :             {
    7981           0 :                 delete poDS;
    7982           0 :                 return nullptr;
    7983             :             }
    7984             :         }
    7985         163 :         if (nSrcOverviews)
    7986             :         {
    7987          57 :             eErr = poDS->CreateOverviewsFromSrcOverviews(poSrcDS, poOvrDS.get(),
    7988             :                                                          nSrcOverviews);
    7989             : 
    7990         163 :             if (eErr == CE_None &&
    7991          57 :                 (poMaskOvrDS != nullptr ||
    7992          49 :                  (poSrcDS->GetRasterBand(1)->GetOverview(0) &&
    7993          26 :                   poSrcDS->GetRasterBand(1)->GetOverview(0)->GetMaskFlags() ==
    7994             :                       GMF_PER_DATASET)))
    7995             :             {
    7996          14 :                 int nOvrBlockXSize = 0;
    7997          14 :                 int nOvrBlockYSize = 0;
    7998          14 :                 GTIFFGetOverviewBlockSize(
    7999             :                     GDALRasterBand::ToHandle(poDS->GetRasterBand(1)),
    8000             :                     &nOvrBlockXSize, &nOvrBlockYSize);
    8001          14 :                 eErr = poDS->CreateInternalMaskOverviews(nOvrBlockXSize,
    8002             :                                                          nOvrBlockYSize);
    8003             :             }
    8004             :         }
    8005             : 
    8006         163 :         TIFFForceStrileArrayWriting(poDS->m_hTIFF);
    8007             : 
    8008         163 :         if (poDS->m_poMaskDS)
    8009             :         {
    8010          22 :             TIFFForceStrileArrayWriting(poDS->m_poMaskDS->m_hTIFF);
    8011             :         }
    8012             : 
    8013         275 :         for (int i = 0; i < poDS->m_nOverviewCount; i++)
    8014             :         {
    8015         112 :             TIFFForceStrileArrayWriting(poDS->m_papoOverviewDS[i]->m_hTIFF);
    8016             : 
    8017         112 :             if (poDS->m_papoOverviewDS[i]->m_poMaskDS)
    8018             :             {
    8019          27 :                 TIFFForceStrileArrayWriting(
    8020          27 :                     poDS->m_papoOverviewDS[i]->m_poMaskDS->m_hTIFF);
    8021             :             }
    8022             :         }
    8023             : 
    8024         163 :         if (eErr == CE_None && nSrcOverviews)
    8025             :         {
    8026          57 :             if (poDS->m_nOverviewCount != nSrcOverviews)
    8027             :             {
    8028           0 :                 ReportError(
    8029             :                     pszFilename, CE_Failure, CPLE_AppDefined,
    8030             :                     "Did only manage to instantiate %d overview levels, "
    8031             :                     "whereas source contains %d",
    8032           0 :                     poDS->m_nOverviewCount, nSrcOverviews);
    8033           0 :                 eErr = CE_Failure;
    8034             :             }
    8035             : 
    8036         169 :             for (int i = 0; eErr == CE_None && i < nSrcOverviews; ++i)
    8037             :             {
    8038             :                 GDALRasterBand *poOvrBand =
    8039             :                     poOvrDS
    8040         180 :                         ? (i == 0
    8041          68 :                                ? poOvrDS->GetRasterBand(1)
    8042          37 :                                : poOvrDS->GetRasterBand(1)->GetOverview(i - 1))
    8043         156 :                         : poSrcDS->GetRasterBand(1)->GetOverview(i);
    8044             :                 const double dfOvrPixels =
    8045         112 :                     static_cast<double>(poOvrBand->GetXSize()) *
    8046         112 :                     poOvrBand->GetYSize();
    8047         112 :                 dfTotalPixels += dfOvrPixels * l_nBands;
    8048         213 :                 if (poOvrBand->GetMaskFlags() == GMF_PER_DATASET ||
    8049         101 :                     poMaskOvrDS != nullptr)
    8050             :                 {
    8051          27 :                     dfTotalPixels += dfOvrPixels;
    8052             :                 }
    8053          85 :                 else if (i == 0 && poDS->GetRasterBand(1)->GetMaskFlags() ==
    8054             :                                        GMF_PER_DATASET)
    8055             :                 {
    8056           1 :                     ReportError(pszFilename, CE_Warning, CPLE_AppDefined,
    8057             :                                 "Source dataset has a mask band on full "
    8058             :                                 "resolution, overviews on the regular bands, "
    8059             :                                 "but lacks overviews on the mask band.");
    8060             :                 }
    8061             :             }
    8062             : 
    8063          57 :             char *papszCopyWholeRasterOptions[2] = {nullptr, nullptr};
    8064          57 :             if (l_nCompression != COMPRESSION_NONE)
    8065          49 :                 papszCopyWholeRasterOptions[0] =
    8066             :                     const_cast<char *>("COMPRESSED=YES");
    8067             :             // Now copy the imagery.
    8068             :             // Begin with the smallest overview.
    8069          57 :             for (int iOvrLevel = nSrcOverviews - 1;
    8070         168 :                  eErr == CE_None && iOvrLevel >= 0; --iOvrLevel)
    8071             :             {
    8072         111 :                 auto poDstDS = poDS->m_papoOverviewDS[iOvrLevel];
    8073             : 
    8074             :                 // Create a fake dataset with the source overview level so that
    8075             :                 // GDALDatasetCopyWholeRaster can cope with it.
    8076             :                 GDALDataset *poSrcOvrDS =
    8077             :                     poOvrDS
    8078         148 :                         ? (iOvrLevel == 0 ? poOvrDS.get()
    8079          37 :                                           : GDALCreateOverviewDataset(
    8080             :                                                 poOvrDS.get(), iOvrLevel - 1,
    8081             :                                                 /* bThisLevelOnly = */ true))
    8082          43 :                         : GDALCreateOverviewDataset(
    8083             :                               poSrcDS, iOvrLevel,
    8084         111 :                               /* bThisLevelOnly = */ true);
    8085             :                 GDALRasterBand *poSrcOvrBand =
    8086         179 :                     poOvrDS ? (iOvrLevel == 0
    8087          68 :                                    ? poOvrDS->GetRasterBand(1)
    8088          74 :                                    : poOvrDS->GetRasterBand(1)->GetOverview(
    8089          37 :                                          iOvrLevel - 1))
    8090         154 :                             : poSrcDS->GetRasterBand(1)->GetOverview(iOvrLevel);
    8091             :                 double dfNextCurPixels =
    8092             :                     dfCurPixels +
    8093         111 :                     static_cast<double>(poSrcOvrBand->GetXSize()) *
    8094         111 :                         poSrcOvrBand->GetYSize() * l_nBands;
    8095             : 
    8096         111 :                 poDstDS->m_bBlockOrderRowMajor = true;
    8097         111 :                 poDstDS->m_bLeaderSizeAsUInt4 = true;
    8098         111 :                 poDstDS->m_bTrailerRepeatedLast4BytesRepeated = true;
    8099         111 :                 poDstDS->m_bFillEmptyTilesAtClosing =
    8100         111 :                     poDS->m_bFillEmptyTilesAtClosing;
    8101         111 :                 poDstDS->m_bWriteEmptyTiles = poDS->m_bWriteEmptyTiles;
    8102         111 :                 GDALRasterBand *poSrcMaskBand = nullptr;
    8103         111 :                 if (poDstDS->m_poMaskDS)
    8104             :                 {
    8105          27 :                     poDstDS->m_poMaskDS->m_bBlockOrderRowMajor = true;
    8106          27 :                     poDstDS->m_poMaskDS->m_bLeaderSizeAsUInt4 = true;
    8107          27 :                     poDstDS->m_poMaskDS->m_bTrailerRepeatedLast4BytesRepeated =
    8108             :                         true;
    8109          27 :                     poDstDS->m_poMaskDS->m_bFillEmptyTilesAtClosing =
    8110          27 :                         poDS->m_bFillEmptyTilesAtClosing;
    8111          27 :                     poDstDS->m_poMaskDS->m_bWriteEmptyTiles =
    8112          27 :                         poDS->m_bWriteEmptyTiles;
    8113             : 
    8114          27 :                     poSrcMaskBand =
    8115             :                         poMaskOvrDS
    8116          43 :                             ? (iOvrLevel == 0
    8117          16 :                                    ? poMaskOvrDS->GetRasterBand(1)
    8118          16 :                                    : poMaskOvrDS->GetRasterBand(1)->GetOverview(
    8119           8 :                                          iOvrLevel - 1))
    8120          38 :                             : poSrcOvrBand->GetMaskBand();
    8121             :                 }
    8122             : 
    8123         111 :                 if (l_nBands == 1 ||
    8124          56 :                     poDstDS->m_nPlanarConfig == PLANARCONFIG_CONTIG)
    8125             :                 {
    8126         110 :                     if (poDstDS->m_poMaskDS)
    8127             :                     {
    8128          27 :                         dfNextCurPixels +=
    8129          27 :                             static_cast<double>(poSrcOvrBand->GetXSize()) *
    8130          27 :                             poSrcOvrBand->GetYSize();
    8131             :                     }
    8132         110 :                     void *pScaledData = GDALCreateScaledProgress(
    8133             :                         dfCurPixels / dfTotalPixels,
    8134             :                         dfNextCurPixels / dfTotalPixels, pfnProgress,
    8135             :                         pProgressData);
    8136             : 
    8137             :                     eErr =
    8138         110 :                         CopyImageryAndMask(poDstDS, poSrcOvrDS, poSrcMaskBand,
    8139             :                                            GDALScaledProgress, pScaledData);
    8140             : 
    8141         110 :                     dfCurPixels = dfNextCurPixels;
    8142         110 :                     GDALDestroyScaledProgress(pScaledData);
    8143             :                 }
    8144             :                 else
    8145             :                 {
    8146           1 :                     void *pScaledData = GDALCreateScaledProgress(
    8147             :                         dfCurPixels / dfTotalPixels,
    8148             :                         dfNextCurPixels / dfTotalPixels, pfnProgress,
    8149             :                         pProgressData);
    8150             : 
    8151           1 :                     eErr = GDALDatasetCopyWholeRaster(
    8152             :                         GDALDataset::ToHandle(poSrcOvrDS),
    8153             :                         GDALDataset::ToHandle(poDstDS),
    8154             :                         papszCopyWholeRasterOptions, GDALScaledProgress,
    8155             :                         pScaledData);
    8156             : 
    8157           1 :                     dfCurPixels = dfNextCurPixels;
    8158           1 :                     GDALDestroyScaledProgress(pScaledData);
    8159             : 
    8160           1 :                     poDstDS->FlushCache(false);
    8161             : 
    8162             :                     // Copy mask of the overview.
    8163           1 :                     if (eErr == CE_None &&
    8164           1 :                         (poMaskOvrDS ||
    8165           2 :                          poSrcOvrBand->GetMaskFlags() == GMF_PER_DATASET) &&
    8166           0 :                         poDstDS->m_poMaskDS != nullptr)
    8167             :                     {
    8168           0 :                         dfNextCurPixels +=
    8169           0 :                             static_cast<double>(poSrcOvrBand->GetXSize()) *
    8170           0 :                             poSrcOvrBand->GetYSize();
    8171           0 :                         pScaledData = GDALCreateScaledProgress(
    8172             :                             dfCurPixels / dfTotalPixels,
    8173             :                             dfNextCurPixels / dfTotalPixels, pfnProgress,
    8174             :                             pProgressData);
    8175           0 :                         eErr = GDALRasterBandCopyWholeRaster(
    8176             :                             poSrcMaskBand,
    8177           0 :                             poDstDS->m_poMaskDS->GetRasterBand(1),
    8178             :                             papszCopyWholeRasterOptions, GDALScaledProgress,
    8179             :                             pScaledData);
    8180           0 :                         dfCurPixels = dfNextCurPixels;
    8181           0 :                         GDALDestroyScaledProgress(pScaledData);
    8182           0 :                         poDstDS->m_poMaskDS->FlushCache(false);
    8183             :                     }
    8184             :                 }
    8185             : 
    8186         111 :                 if (poSrcOvrDS != poOvrDS.get())
    8187          80 :                     delete poSrcOvrDS;
    8188         111 :                 poSrcOvrDS = nullptr;
    8189             :             }
    8190             :         }
    8191             :     }
    8192             : 
    8193             :     /* -------------------------------------------------------------------- */
    8194             :     /*      Copy actual imagery.                                            */
    8195             :     /* -------------------------------------------------------------------- */
    8196        1883 :     double dfNextCurPixels =
    8197        1883 :         dfCurPixels + static_cast<double>(nXSize) * nYSize * l_nBands;
    8198        1883 :     void *pScaledData = GDALCreateScaledProgress(
    8199             :         dfCurPixels / dfTotalPixels, dfNextCurPixels / dfTotalPixels,
    8200             :         pfnProgress, pProgressData);
    8201             : 
    8202             : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
    8203        1883 :     bool bTryCopy = true;
    8204             : #endif
    8205             : 
    8206             : #ifdef HAVE_LIBJPEG
    8207        1883 :     if (bCopyFromJPEG)
    8208             :     {
    8209          12 :         eErr = GTIFF_CopyFromJPEG(poDS, poSrcDS, pfnProgress, pProgressData,
    8210             :                                   bTryCopy);
    8211             : 
    8212             :         // In case of failure in the decompression step, try normal copy.
    8213          12 :         if (bTryCopy)
    8214           0 :             eErr = CE_None;
    8215             :     }
    8216             : #endif
    8217             : 
    8218             : #ifdef JPEG_DIRECT_COPY
    8219             :     if (bDirectCopyFromJPEG)
    8220             :     {
    8221             :         eErr = GTIFF_DirectCopyFromJPEG(poDS, poSrcDS, pfnProgress,
    8222             :                                         pProgressData, bTryCopy);
    8223             : 
    8224             :         // In case of failure in the reading step, try normal copy.
    8225             :         if (bTryCopy)
    8226             :             eErr = CE_None;
    8227             :     }
    8228             : #endif
    8229             : 
    8230        1883 :     bool bWriteMask = true;
    8231        1883 :     if (
    8232             : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
    8233        1871 :         bTryCopy &&
    8234             : #endif
    8235        1871 :         (poDS->m_bTreatAsSplit || poDS->m_bTreatAsSplitBitmap))
    8236             :     {
    8237             :         // For split bands, we use TIFFWriteScanline() interface.
    8238           9 :         CPLAssert(poDS->m_nBitsPerSample == 8 || poDS->m_nBitsPerSample == 1);
    8239             : 
    8240           9 :         if (poDS->m_nPlanarConfig == PLANARCONFIG_CONTIG && poDS->nBands > 1)
    8241             :         {
    8242             :             GByte *pabyScanline = static_cast<GByte *>(
    8243           3 :                 VSI_MALLOC_VERBOSE(TIFFScanlineSize(l_hTIFF)));
    8244           3 :             if (pabyScanline == nullptr)
    8245           0 :                 eErr = CE_Failure;
    8246        9052 :             for (int j = 0; j < nYSize && eErr == CE_None; ++j)
    8247             :             {
    8248       18098 :                 eErr = poSrcDS->RasterIO(GF_Read, 0, j, nXSize, 1, pabyScanline,
    8249             :                                          nXSize, 1, GDT_Byte, l_nBands, nullptr,
    8250        9049 :                                          poDS->nBands, 0, 1, nullptr);
    8251       18098 :                 if (eErr == CE_None &&
    8252        9049 :                     TIFFWriteScanline(l_hTIFF, pabyScanline, j, 0) == -1)
    8253             :                 {
    8254           0 :                     ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    8255             :                                 "TIFFWriteScanline() failed.");
    8256           0 :                     eErr = CE_Failure;
    8257             :                 }
    8258        9049 :                 if (!GDALScaledProgress((j + 1) * 1.0 / nYSize, nullptr,
    8259             :                                         pScaledData))
    8260           0 :                     eErr = CE_Failure;
    8261             :             }
    8262           3 :             CPLFree(pabyScanline);
    8263             :         }
    8264             :         else
    8265             :         {
    8266             :             GByte *pabyScanline =
    8267           6 :                 static_cast<GByte *>(VSI_MALLOC_VERBOSE(nXSize));
    8268           6 :             if (pabyScanline == nullptr)
    8269           0 :                 eErr = CE_Failure;
    8270             :             else
    8271           6 :                 eErr = CE_None;
    8272          14 :             for (int iBand = 1; iBand <= l_nBands && eErr == CE_None; ++iBand)
    8273             :             {
    8274       48211 :                 for (int j = 0; j < nYSize && eErr == CE_None; ++j)
    8275             :                 {
    8276       48203 :                     eErr = poSrcDS->GetRasterBand(iBand)->RasterIO(
    8277             :                         GF_Read, 0, j, nXSize, 1, pabyScanline, nXSize, 1,
    8278             :                         GDT_Byte, 0, 0, nullptr);
    8279       48203 :                     if (poDS->m_bTreatAsSplitBitmap)
    8280             :                     {
    8281     7225210 :                         for (int i = 0; i < nXSize; ++i)
    8282             :                         {
    8283     7216010 :                             const GByte byVal = pabyScanline[i];
    8284     7216010 :                             if ((i & 0x7) == 0)
    8285      902001 :                                 pabyScanline[i >> 3] = 0;
    8286     7216010 :                             if (byVal)
    8287     7097220 :                                 pabyScanline[i >> 3] |= 0x80 >> (i & 0x7);
    8288             :                         }
    8289             :                     }
    8290       96406 :                     if (eErr == CE_None &&
    8291       48203 :                         TIFFWriteScanline(l_hTIFF, pabyScanline, j,
    8292       48203 :                                           static_cast<uint16_t>(iBand - 1)) ==
    8293             :                             -1)
    8294             :                     {
    8295           0 :                         ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
    8296             :                                     "TIFFWriteScanline() failed.");
    8297           0 :                         eErr = CE_Failure;
    8298             :                     }
    8299       48203 :                     if (!GDALScaledProgress((j + 1 + (iBand - 1) * nYSize) *
    8300       48203 :                                                 1.0 / (l_nBands * nYSize),
    8301             :                                             nullptr, pScaledData))
    8302           0 :                         eErr = CE_Failure;
    8303             :                 }
    8304             :             }
    8305           6 :             CPLFree(pabyScanline);
    8306             :         }
    8307             : 
    8308             :         // Necessary to be able to read the file without re-opening.
    8309           9 :         TIFFSizeProc pfnSizeProc = TIFFGetSizeProc(l_hTIFF);
    8310             : 
    8311           9 :         TIFFFlushData(l_hTIFF);
    8312             : 
    8313           9 :         toff_t nNewDirOffset = pfnSizeProc(TIFFClientdata(l_hTIFF));
    8314           9 :         if ((nNewDirOffset % 2) == 1)
    8315           5 :             ++nNewDirOffset;
    8316             : 
    8317           9 :         TIFFFlush(l_hTIFF);
    8318             : 
    8319           9 :         if (poDS->m_nDirOffset != TIFFCurrentDirOffset(l_hTIFF))
    8320             :         {
    8321           0 :             poDS->m_nDirOffset = nNewDirOffset;
    8322           0 :             CPLDebug("GTiff", "directory moved during flush.");
    8323           9 :         }
    8324             :     }
    8325        1874 :     else if (
    8326             : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
    8327        1862 :         bTryCopy &&
    8328             : #endif
    8329             :         eErr == CE_None)
    8330             :     {
    8331        1861 :         const char *papszCopyWholeRasterOptions[3] = {nullptr, nullptr,
    8332             :                                                       nullptr};
    8333        1861 :         int iNextOption = 0;
    8334        1861 :         papszCopyWholeRasterOptions[iNextOption++] = "SKIP_HOLES=YES";
    8335        1861 :         if (l_nCompression != COMPRESSION_NONE)
    8336             :         {
    8337         398 :             papszCopyWholeRasterOptions[iNextOption++] = "COMPRESSED=YES";
    8338             :         }
    8339             :         // For streaming with separate, we really want that bands are written
    8340             :         // after each other, even if the source is pixel interleaved.
    8341        1463 :         else if (bStreaming && poDS->m_nPlanarConfig == PLANARCONFIG_SEPARATE)
    8342             :         {
    8343           1 :             papszCopyWholeRasterOptions[iNextOption++] = "INTERLEAVE=BAND";
    8344             :         }
    8345             : 
    8346        1861 :         if (bCopySrcOverviews &&
    8347          88 :             (l_nBands == 1 || poDS->m_nPlanarConfig == PLANARCONFIG_CONTIG))
    8348             :         {
    8349         161 :             poDS->m_bBlockOrderRowMajor = true;
    8350         161 :             poDS->m_bLeaderSizeAsUInt4 = true;
    8351         161 :             poDS->m_bTrailerRepeatedLast4BytesRepeated = true;
    8352         161 :             if (poDS->m_poMaskDS)
    8353             :             {
    8354          22 :                 poDS->m_poMaskDS->m_bBlockOrderRowMajor = true;
    8355          22 :                 poDS->m_poMaskDS->m_bLeaderSizeAsUInt4 = true;
    8356          22 :                 poDS->m_poMaskDS->m_bTrailerRepeatedLast4BytesRepeated = true;
    8357             :             }
    8358             : 
    8359         161 :             if (poDS->m_poMaskDS)
    8360             :             {
    8361          22 :                 GDALDestroyScaledProgress(pScaledData);
    8362             :                 pScaledData =
    8363          22 :                     GDALCreateScaledProgress(dfCurPixels / dfTotalPixels, 1.0,
    8364             :                                              pfnProgress, pProgressData);
    8365             :             }
    8366             : 
    8367         161 :             eErr = CopyImageryAndMask(poDS, poSrcDS,
    8368         161 :                                       poSrcDS->GetRasterBand(1)->GetMaskBand(),
    8369             :                                       GDALScaledProgress, pScaledData);
    8370         161 :             if (poDS->m_poMaskDS)
    8371             :             {
    8372          22 :                 bWriteMask = false;
    8373             :             }
    8374             :         }
    8375             :         else
    8376             :         {
    8377        1700 :             eErr = GDALDatasetCopyWholeRaster(
    8378             :                 /* (GDALDatasetH) */ poSrcDS,
    8379             :                 /* (GDALDatasetH) */ poDS, papszCopyWholeRasterOptions,
    8380             :                 GDALScaledProgress, pScaledData);
    8381             :         }
    8382             :     }
    8383             : 
    8384        1883 :     GDALDestroyScaledProgress(pScaledData);
    8385             : 
    8386        1883 :     if (eErr == CE_None && !bStreaming && bWriteMask)
    8387             :     {
    8388        1842 :         pScaledData = GDALCreateScaledProgress(dfNextCurPixels / dfTotalPixels,
    8389             :                                                1.0, pfnProgress, pProgressData);
    8390        1842 :         if (poDS->m_poMaskDS)
    8391             :         {
    8392           5 :             const char *l_papszOptions[2] = {"COMPRESSED=YES", nullptr};
    8393           5 :             eErr = GDALRasterBandCopyWholeRaster(
    8394           5 :                 poSrcDS->GetRasterBand(1)->GetMaskBand(),
    8395           5 :                 poDS->GetRasterBand(1)->GetMaskBand(),
    8396             :                 const_cast<char **>(l_papszOptions), GDALScaledProgress,
    8397             :                 pScaledData);
    8398             :         }
    8399             :         else
    8400             :         {
    8401             :             eErr =
    8402        1837 :                 GDALDriver::DefaultCopyMasks(poSrcDS, poDS, bStrict, nullptr,
    8403             :                                              GDALScaledProgress, pScaledData);
    8404             :         }
    8405        1842 :         GDALDestroyScaledProgress(pScaledData);
    8406             :     }
    8407             : 
    8408        1883 :     poDS->m_bWriteCOGLayout = false;
    8409             : 
    8410        1883 :     if (eErr == CE_Failure)
    8411             :     {
    8412          15 :         delete poDS;
    8413          15 :         poDS = nullptr;
    8414             : 
    8415          15 :         if (CPLTestBool(CPLGetConfigOption("GTIFF_DELETE_ON_ERROR", "YES")))
    8416             :         {
    8417          14 :             if (!bStreaming)
    8418             :             {
    8419             :                 // Should really delete more carefully.
    8420          14 :                 VSIUnlink(pszFilename);
    8421             :             }
    8422             :         }
    8423             :     }
    8424             : 
    8425        1883 :     return poDS;
    8426             : }
    8427             : 
    8428             : /************************************************************************/
    8429             : /*                           SetSpatialRef()                            */
    8430             : /************************************************************************/
    8431             : 
    8432        1364 : CPLErr GTiffDataset::SetSpatialRef(const OGRSpatialReference *poSRS)
    8433             : 
    8434             : {
    8435        1364 :     if (m_bStreamingOut && m_bCrystalized)
    8436             :     {
    8437           1 :         ReportError(CE_Failure, CPLE_NotSupported,
    8438             :                     "Cannot modify projection at that point in "
    8439             :                     "a streamed output file");
    8440           1 :         return CE_Failure;
    8441             :     }
    8442             : 
    8443        1363 :     LoadGeoreferencingAndPamIfNeeded();
    8444        1363 :     LookForProjection();
    8445             : 
    8446        1363 :     CPLErr eErr = CE_None;
    8447        1363 :     if (eAccess == GA_Update)
    8448             :     {
    8449        1365 :         if ((m_eProfile == GTiffProfile::BASELINE) &&
    8450           7 :             (GetPamFlags() & GPF_DISABLED) == 0)
    8451             :         {
    8452           7 :             eErr = GDALPamDataset::SetSpatialRef(poSRS);
    8453             :         }
    8454             :         else
    8455             :         {
    8456        1351 :             if (GDALPamDataset::GetSpatialRef() != nullptr)
    8457             :             {
    8458             :                 // Cancel any existing SRS from PAM file.
    8459           1 :                 GDALPamDataset::SetSpatialRef(nullptr);
    8460             :             }
    8461        1351 :             m_bGeoTIFFInfoChanged = true;
    8462             :         }
    8463             :     }
    8464             :     else
    8465             :     {
    8466           5 :         CPLDebug("GTIFF", "SetSpatialRef() goes to PAM instead of TIFF tags");
    8467           5 :         eErr = GDALPamDataset::SetSpatialRef(poSRS);
    8468             :     }
    8469             : 
    8470        1363 :     if (eErr == CE_None)
    8471             :     {
    8472        1363 :         if (poSRS == nullptr || poSRS->IsEmpty())
    8473             :         {
    8474          16 :             if (!m_oSRS.IsEmpty())
    8475             :             {
    8476           4 :                 m_bForceUnsetProjection = true;
    8477             :             }
    8478          16 :             m_oSRS.Clear();
    8479             :         }
    8480             :         else
    8481             :         {
    8482        1347 :             m_oSRS = *poSRS;
    8483        1347 :             m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    8484             :         }
    8485             :     }
    8486             : 
    8487        1363 :     return eErr;
    8488             : }
    8489             : 
    8490             : /************************************************************************/
    8491             : /*                          SetGeoTransform()                           */
    8492             : /************************************************************************/
    8493             : 
    8494        1561 : CPLErr GTiffDataset::SetGeoTransform(double *padfTransform)
    8495             : 
    8496             : {
    8497        1561 :     if (m_bStreamingOut && m_bCrystalized)
    8498             :     {
    8499           1 :         ReportError(CE_Failure, CPLE_NotSupported,
    8500             :                     "Cannot modify geotransform at that point in a "
    8501             :                     "streamed output file");
    8502           1 :         return CE_Failure;
    8503             :     }
    8504             : 
    8505        1560 :     LoadGeoreferencingAndPamIfNeeded();
    8506             : 
    8507        1560 :     CPLErr eErr = CE_None;
    8508        1560 :     if (eAccess == GA_Update)
    8509             :     {
    8510        1554 :         if (!m_aoGCPs.empty())
    8511             :         {
    8512           1 :             ReportError(CE_Warning, CPLE_AppDefined,
    8513             :                         "GCPs previously set are going to be cleared "
    8514             :                         "due to the setting of a geotransform.");
    8515           1 :             m_bForceUnsetGTOrGCPs = true;
    8516           1 :             m_aoGCPs.clear();
    8517             :         }
    8518        1553 :         else if (padfTransform[0] == 0.0 && padfTransform[1] == 0.0 &&
    8519           2 :                  padfTransform[2] == 0.0 && padfTransform[3] == 0.0 &&
    8520           2 :                  padfTransform[4] == 0.0 && padfTransform[5] == 0.0)
    8521             :         {
    8522           2 :             if (m_bGeoTransformValid)
    8523             :             {
    8524           2 :                 m_bForceUnsetGTOrGCPs = true;
    8525           2 :                 m_bGeoTIFFInfoChanged = true;
    8526             :             }
    8527           2 :             m_bGeoTransformValid = false;
    8528           2 :             memcpy(m_adfGeoTransform, padfTransform, sizeof(double) * 6);
    8529           2 :             return CE_None;
    8530             :         }
    8531             : 
    8532        3113 :         if ((m_eProfile == GTiffProfile::BASELINE) &&
    8533           9 :             !CPLFetchBool(m_papszCreationOptions, "TFW", false) &&
    8534        1566 :             !CPLFetchBool(m_papszCreationOptions, "WORLDFILE", false) &&
    8535           5 :             (GetPamFlags() & GPF_DISABLED) == 0)
    8536             :         {
    8537           5 :             eErr = GDALPamDataset::SetGeoTransform(padfTransform);
    8538             :         }
    8539             :         else
    8540             :         {
    8541             :             // Cancel any existing geotransform from PAM file.
    8542        1547 :             GDALPamDataset::DeleteGeoTransform();
    8543        1547 :             m_bGeoTIFFInfoChanged = true;
    8544             :         }
    8545             :     }
    8546             :     else
    8547             :     {
    8548           6 :         CPLDebug("GTIFF", "SetGeoTransform() goes to PAM instead of TIFF tags");
    8549           6 :         eErr = GDALPamDataset::SetGeoTransform(padfTransform);
    8550             :     }
    8551             : 
    8552        1558 :     if (eErr == CE_None)
    8553             :     {
    8554        1558 :         memcpy(m_adfGeoTransform, padfTransform, sizeof(double) * 6);
    8555        1558 :         m_bGeoTransformValid = true;
    8556             :     }
    8557             : 
    8558        1558 :     return eErr;
    8559             : }
    8560             : 
    8561             : /************************************************************************/
    8562             : /*                               SetGCPs()                              */
    8563             : /************************************************************************/
    8564             : 
    8565          21 : CPLErr GTiffDataset::SetGCPs(int nGCPCountIn, const GDAL_GCP *pasGCPListIn,
    8566             :                              const OGRSpatialReference *poGCPSRS)
    8567             : {
    8568          21 :     CPLErr eErr = CE_None;
    8569          21 :     LoadGeoreferencingAndPamIfNeeded();
    8570          21 :     LookForProjection();
    8571             : 
    8572          21 :     if (eAccess == GA_Update)
    8573             :     {
    8574          19 :         if (!m_aoGCPs.empty() && nGCPCountIn == 0)
    8575             :         {
    8576           3 :             m_bForceUnsetGTOrGCPs = true;
    8577             :         }
    8578          16 :         else if (nGCPCountIn > 0 && m_bGeoTransformValid)
    8579             :         {
    8580           4 :             ReportError(CE_Warning, CPLE_AppDefined,
    8581             :                         "A geotransform previously set is going to be cleared "
    8582             :                         "due to the setting of GCPs.");
    8583           4 :             m_adfGeoTransform[0] = 0.0;
    8584           4 :             m_adfGeoTransform[1] = 1.0;
    8585           4 :             m_adfGeoTransform[2] = 0.0;
    8586           4 :             m_adfGeoTransform[3] = 0.0;
    8587           4 :             m_adfGeoTransform[4] = 0.0;
    8588           4 :             m_adfGeoTransform[5] = 1.0;
    8589           4 :             m_bGeoTransformValid = false;
    8590           4 :             m_bForceUnsetGTOrGCPs = true;
    8591             :         }
    8592          19 :         if ((m_eProfile == GTiffProfile::BASELINE) &&
    8593           0 :             (GetPamFlags() & GPF_DISABLED) == 0)
    8594             :         {
    8595           0 :             eErr = GDALPamDataset::SetGCPs(nGCPCountIn, pasGCPListIn, poGCPSRS);
    8596             :         }
    8597             :         else
    8598             :         {
    8599          19 :             if (nGCPCountIn > knMAX_GCP_COUNT)
    8600             :             {
    8601           2 :                 if (GDALPamDataset::GetGCPCount() == 0 && !m_aoGCPs.empty())
    8602             :                 {
    8603           1 :                     m_bForceUnsetGTOrGCPs = true;
    8604             :                 }
    8605           2 :                 ReportError(CE_Warning, CPLE_AppDefined,
    8606             :                             "Trying to write %d GCPs, whereas the maximum "
    8607             :                             "supported in GeoTIFF tag is %d. "
    8608             :                             "Falling back to writing them to PAM",
    8609             :                             nGCPCountIn, knMAX_GCP_COUNT);
    8610           2 :                 eErr = GDALPamDataset::SetGCPs(nGCPCountIn, pasGCPListIn,
    8611             :                                                poGCPSRS);
    8612             :             }
    8613          17 :             else if (GDALPamDataset::GetGCPCount() > 0)
    8614             :             {
    8615             :                 // Cancel any existing GCPs from PAM file.
    8616           1 :                 GDALPamDataset::SetGCPs(
    8617             :                     0, nullptr,
    8618             :                     static_cast<const OGRSpatialReference *>(nullptr));
    8619             :             }
    8620          19 :             m_bGeoTIFFInfoChanged = true;
    8621             :         }
    8622             :     }
    8623             :     else
    8624             :     {
    8625           2 :         CPLDebug("GTIFF", "SetGCPs() goes to PAM instead of TIFF tags");
    8626           2 :         eErr = GDALPamDataset::SetGCPs(nGCPCountIn, pasGCPListIn, poGCPSRS);
    8627             :     }
    8628             : 
    8629          21 :     if (eErr == CE_None)
    8630             :     {
    8631          21 :         if (poGCPSRS == nullptr || poGCPSRS->IsEmpty())
    8632             :         {
    8633          11 :             if (!m_oSRS.IsEmpty())
    8634             :             {
    8635           4 :                 m_bForceUnsetProjection = true;
    8636             :             }
    8637          11 :             m_oSRS.Clear();
    8638             :         }
    8639             :         else
    8640             :         {
    8641          10 :             m_oSRS = *poGCPSRS;
    8642          10 :             m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    8643             :         }
    8644             : 
    8645          21 :         m_aoGCPs = gdal::GCP::fromC(pasGCPListIn, nGCPCountIn);
    8646             :     }
    8647             : 
    8648          21 :     return eErr;
    8649             : }
    8650             : 
    8651             : /************************************************************************/
    8652             : /*                            SetMetadata()                             */
    8653             : /************************************************************************/
    8654        2451 : CPLErr GTiffDataset::SetMetadata(char **papszMD, const char *pszDomain)
    8655             : 
    8656             : {
    8657        2451 :     LoadGeoreferencingAndPamIfNeeded();
    8658             : 
    8659        2451 :     if (m_bStreamingOut && m_bCrystalized)
    8660             :     {
    8661           1 :         ReportError(
    8662             :             CE_Failure, CPLE_NotSupported,
    8663             :             "Cannot modify metadata at that point in a streamed output file");
    8664           1 :         return CE_Failure;
    8665             :     }
    8666             : 
    8667        2450 :     CPLErr eErr = CE_None;
    8668        2450 :     if (eAccess == GA_Update)
    8669             :     {
    8670        2448 :         if (pszDomain != nullptr && EQUAL(pszDomain, MD_DOMAIN_RPC))
    8671             :         {
    8672             :             // So that a subsequent GetMetadata() wouldn't override our new
    8673             :             // values
    8674          22 :             LoadMetadata();
    8675          22 :             m_bForceUnsetRPC = (CSLCount(papszMD) == 0);
    8676             :         }
    8677             : 
    8678        2448 :         if ((papszMD != nullptr) && (pszDomain != nullptr) &&
    8679        1656 :             EQUAL(pszDomain, "COLOR_PROFILE"))
    8680             :         {
    8681           0 :             m_bColorProfileMetadataChanged = true;
    8682             :         }
    8683        2448 :         else if (pszDomain == nullptr || !EQUAL(pszDomain, "_temporary_"))
    8684             :         {
    8685        2448 :             m_bMetadataChanged = true;
    8686             :             // Cancel any existing metadata from PAM file.
    8687        2448 :             if (GDALPamDataset::GetMetadata(pszDomain) != nullptr)
    8688           1 :                 GDALPamDataset::SetMetadata(nullptr, pszDomain);
    8689             :         }
    8690             : 
    8691        4859 :         if ((pszDomain == nullptr || EQUAL(pszDomain, "")) &&
    8692        2411 :             CSLFetchNameValue(papszMD, GDALMD_AREA_OR_POINT) != nullptr)
    8693             :         {
    8694        1795 :             const char *pszPrevValue = GetMetadataItem(GDALMD_AREA_OR_POINT);
    8695             :             const char *pszNewValue =
    8696        1795 :                 CSLFetchNameValue(papszMD, GDALMD_AREA_OR_POINT);
    8697        1795 :             if (pszPrevValue == nullptr || pszNewValue == nullptr ||
    8698        1394 :                 !EQUAL(pszPrevValue, pszNewValue))
    8699             :             {
    8700         405 :                 LookForProjection();
    8701         405 :                 m_bGeoTIFFInfoChanged = true;
    8702             :             }
    8703             :         }
    8704             : 
    8705        2448 :         if (pszDomain != nullptr && EQUAL(pszDomain, "xml:XMP"))
    8706             :         {
    8707           2 :             if (papszMD != nullptr && *papszMD != nullptr)
    8708             :             {
    8709           1 :                 int nTagSize = static_cast<int>(strlen(*papszMD));
    8710           1 :                 TIFFSetField(m_hTIFF, TIFFTAG_XMLPACKET, nTagSize, *papszMD);
    8711             :             }
    8712             :             else
    8713             :             {
    8714           1 :                 TIFFUnsetField(m_hTIFF, TIFFTAG_XMLPACKET);
    8715             :             }
    8716             :         }
    8717             :     }
    8718             :     else
    8719             :     {
    8720           2 :         CPLDebug(
    8721             :             "GTIFF",
    8722             :             "GTiffDataset::SetMetadata() goes to PAM instead of TIFF tags");
    8723           2 :         eErr = GDALPamDataset::SetMetadata(papszMD, pszDomain);
    8724             :     }
    8725             : 
    8726        2450 :     if (eErr == CE_None)
    8727             :     {
    8728        2450 :         eErr = m_oGTiffMDMD.SetMetadata(papszMD, pszDomain);
    8729             :     }
    8730        2450 :     return eErr;
    8731             : }
    8732             : 
    8733             : /************************************************************************/
    8734             : /*                          SetMetadataItem()                           */
    8735             : /************************************************************************/
    8736             : 
    8737        3262 : CPLErr GTiffDataset::SetMetadataItem(const char *pszName, const char *pszValue,
    8738             :                                      const char *pszDomain)
    8739             : 
    8740             : {
    8741        3262 :     LoadGeoreferencingAndPamIfNeeded();
    8742             : 
    8743        3262 :     if (m_bStreamingOut && m_bCrystalized)
    8744             :     {
    8745           1 :         ReportError(
    8746             :             CE_Failure, CPLE_NotSupported,
    8747             :             "Cannot modify metadata at that point in a streamed output file");
    8748           1 :         return CE_Failure;
    8749             :     }
    8750             : 
    8751        3261 :     CPLErr eErr = CE_None;
    8752        3261 :     if (eAccess == GA_Update)
    8753             :     {
    8754        3254 :         if ((pszDomain != nullptr) && EQUAL(pszDomain, "COLOR_PROFILE"))
    8755             :         {
    8756           8 :             m_bColorProfileMetadataChanged = true;
    8757             :         }
    8758        3246 :         else if (pszDomain == nullptr || !EQUAL(pszDomain, "_temporary_"))
    8759             :         {
    8760        3246 :             m_bMetadataChanged = true;
    8761             :             // Cancel any existing metadata from PAM file.
    8762        3246 :             if (GDALPamDataset::GetMetadataItem(pszName, pszDomain) != nullptr)
    8763           1 :                 GDALPamDataset::SetMetadataItem(pszName, nullptr, pszDomain);
    8764             :         }
    8765             : 
    8766        3254 :         if ((pszDomain == nullptr || EQUAL(pszDomain, "")) &&
    8767          70 :             pszName != nullptr && EQUAL(pszName, GDALMD_AREA_OR_POINT))
    8768             :         {
    8769           7 :             LookForProjection();
    8770           7 :             m_bGeoTIFFInfoChanged = true;
    8771             :         }
    8772             :     }
    8773             :     else
    8774             :     {
    8775           7 :         CPLDebug(
    8776             :             "GTIFF",
    8777             :             "GTiffDataset::SetMetadataItem() goes to PAM instead of TIFF tags");
    8778           7 :         eErr = GDALPamDataset::SetMetadataItem(pszName, pszValue, pszDomain);
    8779             :     }
    8780             : 
    8781        3261 :     if (eErr == CE_None)
    8782             :     {
    8783        3261 :         eErr = m_oGTiffMDMD.SetMetadataItem(pszName, pszValue, pszDomain);
    8784             :     }
    8785             : 
    8786        3261 :     return eErr;
    8787             : }
    8788             : 
    8789             : /************************************************************************/
    8790             : /*                         CreateMaskBand()                             */
    8791             : /************************************************************************/
    8792             : 
    8793          85 : CPLErr GTiffDataset::CreateMaskBand(int nFlagsIn)
    8794             : {
    8795          85 :     ScanDirectories();
    8796             : 
    8797          85 :     if (m_poMaskDS != nullptr)
    8798             :     {
    8799           1 :         ReportError(CE_Failure, CPLE_AppDefined,
    8800             :                     "This TIFF dataset has already an internal mask band");
    8801           1 :         return CE_Failure;
    8802             :     }
    8803          84 :     else if (MustCreateInternalMask())
    8804             :     {
    8805          71 :         if (nFlagsIn != GMF_PER_DATASET)
    8806             :         {
    8807           1 :             ReportError(CE_Failure, CPLE_AppDefined,
    8808             :                         "The only flag value supported for internal mask is "
    8809             :                         "GMF_PER_DATASET");
    8810           1 :             return CE_Failure;
    8811             :         }
    8812             : 
    8813          70 :         int l_nCompression = COMPRESSION_PACKBITS;
    8814          70 :         if (strstr(GDALGetMetadataItem(GDALGetDriverByName("GTiff"),
    8815             :                                        GDAL_DMD_CREATIONOPTIONLIST, nullptr),
    8816          70 :                    "<Value>DEFLATE</Value>") != nullptr)
    8817          70 :             l_nCompression = COMPRESSION_ADOBE_DEFLATE;
    8818             : 
    8819             :         /* --------------------------------------------------------------------
    8820             :          */
    8821             :         /*      If we don't have read access, then create the mask externally.
    8822             :          */
    8823             :         /* --------------------------------------------------------------------
    8824             :          */
    8825          70 :         if (GetAccess() != GA_Update)
    8826             :         {
    8827           1 :             ReportError(CE_Warning, CPLE_AppDefined,
    8828             :                         "File open for read-only accessing, "
    8829             :                         "creating mask externally.");
    8830             : 
    8831           1 :             return GDALPamDataset::CreateMaskBand(nFlagsIn);
    8832             :         }
    8833             : 
    8834          69 :         if (m_bLayoutIFDSBeforeData && !m_bKnownIncompatibleEdition &&
    8835           0 :             !m_bWriteKnownIncompatibleEdition)
    8836             :         {
    8837           0 :             ReportError(CE_Warning, CPLE_AppDefined,
    8838             :                         "Adding a mask invalidates the "
    8839             :                         "LAYOUT=IFDS_BEFORE_DATA property");
    8840           0 :             m_bKnownIncompatibleEdition = true;
    8841           0 :             m_bWriteKnownIncompatibleEdition = true;
    8842             :         }
    8843             : 
    8844          69 :         bool bIsOverview = false;
    8845          69 :         uint32_t nSubType = 0;
    8846          69 :         if (TIFFGetField(m_hTIFF, TIFFTAG_SUBFILETYPE, &nSubType))
    8847             :         {
    8848           8 :             bIsOverview = (nSubType & FILETYPE_REDUCEDIMAGE) != 0;
    8849             : 
    8850           8 :             if ((nSubType & FILETYPE_MASK) != 0)
    8851             :             {
    8852           0 :                 ReportError(CE_Failure, CPLE_AppDefined,
    8853             :                             "Cannot create a mask on a TIFF mask IFD !");
    8854           0 :                 return CE_Failure;
    8855             :             }
    8856             :         }
    8857             : 
    8858          69 :         const int bIsTiled = TIFFIsTiled(m_hTIFF);
    8859             : 
    8860          69 :         FlushDirectory();
    8861             : 
    8862          69 :         const toff_t nOffset = GTIFFWriteDirectory(
    8863             :             m_hTIFF,
    8864             :             bIsOverview ? FILETYPE_REDUCEDIMAGE | FILETYPE_MASK : FILETYPE_MASK,
    8865             :             nRasterXSize, nRasterYSize, 1, PLANARCONFIG_CONTIG, 1,
    8866             :             m_nBlockXSize, m_nBlockYSize, bIsTiled, l_nCompression,
    8867             :             PHOTOMETRIC_MASK, PREDICTOR_NONE, SAMPLEFORMAT_UINT, nullptr,
    8868             :             nullptr, nullptr, 0, nullptr, "", nullptr, nullptr, nullptr,
    8869          69 :             nullptr, m_bWriteCOGLayout);
    8870             : 
    8871          69 :         ReloadDirectory();
    8872             : 
    8873          69 :         if (nOffset == 0)
    8874           0 :             return CE_Failure;
    8875             : 
    8876          69 :         m_poMaskDS = new GTiffDataset();
    8877          69 :         m_poMaskDS->m_poBaseDS = this;
    8878          69 :         m_poMaskDS->m_poImageryDS = this;
    8879          69 :         m_poMaskDS->ShareLockWithParentDataset(this);
    8880          69 :         m_poMaskDS->m_bPromoteTo8Bits = CPLTestBool(
    8881             :             CPLGetConfigOption("GDAL_TIFF_INTERNAL_MASK_TO_8BIT", "YES"));
    8882          69 :         if (m_poMaskDS->OpenOffset(VSI_TIFFOpenChild(m_hTIFF), nOffset,
    8883          69 :                                    GA_Update) != CE_None)
    8884             :         {
    8885           0 :             delete m_poMaskDS;
    8886           0 :             m_poMaskDS = nullptr;
    8887           0 :             return CE_Failure;
    8888             :         }
    8889             : 
    8890          69 :         return CE_None;
    8891             :     }
    8892             : 
    8893          13 :     return GDALPamDataset::CreateMaskBand(nFlagsIn);
    8894             : }
    8895             : 
    8896             : /************************************************************************/
    8897             : /*                        MustCreateInternalMask()                      */
    8898             : /************************************************************************/
    8899             : 
    8900         112 : bool GTiffDataset::MustCreateInternalMask()
    8901             : {
    8902         112 :     return CPLTestBool(CPLGetConfigOption("GDAL_TIFF_INTERNAL_MASK", "YES"));
    8903             : }
    8904             : 
    8905             : /************************************************************************/
    8906             : /*                         CreateMaskBand()                             */
    8907             : /************************************************************************/
    8908             : 
    8909          27 : CPLErr GTiffRasterBand::CreateMaskBand(int nFlagsIn)
    8910             : {
    8911          27 :     m_poGDS->ScanDirectories();
    8912             : 
    8913          27 :     if (m_poGDS->m_poMaskDS != nullptr)
    8914             :     {
    8915           5 :         ReportError(CE_Failure, CPLE_AppDefined,
    8916             :                     "This TIFF dataset has already an internal mask band");
    8917           5 :         return CE_Failure;
    8918             :     }
    8919             : 
    8920             :     const char *pszGDAL_TIFF_INTERNAL_MASK =
    8921          22 :         CPLGetConfigOption("GDAL_TIFF_INTERNAL_MASK", nullptr);
    8922          25 :     if ((pszGDAL_TIFF_INTERNAL_MASK &&
    8923          22 :          CPLTestBool(pszGDAL_TIFF_INTERNAL_MASK)) ||
    8924             :         nFlagsIn == GMF_PER_DATASET)
    8925             :     {
    8926          15 :         return m_poGDS->CreateMaskBand(nFlagsIn);
    8927             :     }
    8928             : 
    8929           7 :     return GDALPamRasterBand::CreateMaskBand(nFlagsIn);
    8930             : }
    8931             : 
    8932             : /************************************************************************/
    8933             : /*                          ClampCTEntry()                              */
    8934             : /************************************************************************/
    8935             : 
    8936      227439 : /* static */ unsigned short GTiffDataset::ClampCTEntry(int iColor, int iComp,
    8937             :                                                        int nCTEntryVal,
    8938             :                                                        int nMultFactor)
    8939             : {
    8940      227439 :     const int nVal = nCTEntryVal * nMultFactor;
    8941      227439 :     if (nVal < 0)
    8942             :     {
    8943           0 :         CPLError(CE_Warning, CPLE_AppDefined,
    8944             :                  "Color table entry [%d][%d] = %d, clamped to 0", iColor, iComp,
    8945             :                  nCTEntryVal);
    8946           0 :         return 0;
    8947             :     }
    8948      227439 :     if (nVal > 65535)
    8949             :     {
    8950           2 :         CPLError(CE_Warning, CPLE_AppDefined,
    8951             :                  "Color table entry [%d][%d] = %d, clamped to 65535", iColor,
    8952             :                  iComp, nCTEntryVal);
    8953           2 :         return 65535;
    8954             :     }
    8955      227437 :     return static_cast<unsigned short>(nVal);
    8956             : }

Generated by: LCOV version 1.14