Coverage Report

Created: 2025-11-16 06:25

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