Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: GeoTIFF Driver
4 : * Purpose: Write/set operations on GTiffDataset
5 : * Author: Frank Warmerdam, warmerdam@pobox.com
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 1998, 2002, Frank Warmerdam <warmerdam@pobox.com>
9 : * Copyright (c) 2007-2015, Even Rouault <even dot rouault at spatialys dot com>
10 : *
11 : * SPDX-License-Identifier: MIT
12 : ****************************************************************************/
13 :
14 : #include "gtiffdataset.h"
15 : #include "gtiffrasterband.h"
16 : #include "gtiffoddbitsband.h"
17 : #include "gtiffjpegoverviewds.h"
18 :
19 : #include <cassert>
20 : #include <cerrno>
21 :
22 : #include <algorithm>
23 : #include <cmath>
24 : #include <limits>
25 : #include <memory>
26 : #include <mutex>
27 : #include <set>
28 : #include <string>
29 : #include <tuple>
30 : #include <utility>
31 :
32 : #include "cpl_error.h"
33 : #include "cpl_error_internal.h" // CPLErrorHandlerAccumulatorStruct
34 : #include "cpl_float.h"
35 : #include "cpl_md5.h"
36 : #include "cpl_vsi.h"
37 : #include "cpl_vsi_virtual.h"
38 : #include "cpl_worker_thread_pool.h"
39 : #include "fetchbufferdirectio.h"
40 : #include "gdal_mdreader.h" // GDALWriteRPCTXTFile()
41 : #include "gdal_priv.h"
42 : #include "gdal_priv_templates.hpp" // GDALIsValueInRange<>
43 : #include "gdal_thread_pool.h" // GDALGetGlobalThreadPool()
44 : #include "geovalues.h" // RasterPixelIsPoint
45 : #include "gt_jpeg_copy.h"
46 : #include "gt_overview.h" // GTIFFBuildOverviewMetadata()
47 : #include "quant_table_md5sum.h"
48 : #include "quant_table_md5sum_jpeg9e.h"
49 : #include "tif_jxl.h"
50 : #include "tifvsi.h"
51 : #include "xtiffio.h"
52 :
53 : #if LIFFLIB_VERSION > 20230908 || defined(INTERNAL_LIBTIFF)
54 : /* libtiff < 4.6.1 doesn't generate a LERC mask for multi-band contig configuration */
55 : #define LIBTIFF_MULTIBAND_LERC_NAN_OK
56 : #endif
57 :
58 : static const int knGTIFFJpegTablesModeDefault = JPEGTABLESMODE_QUANT;
59 :
60 : static constexpr const char szPROFILE_BASELINE[] = "BASELINE";
61 : static constexpr const char szPROFILE_GeoTIFF[] = "GeoTIFF";
62 : static constexpr const char szPROFILE_GDALGeoTIFF[] = "GDALGeoTIFF";
63 :
64 : // Due to libgeotiff/xtiff.c declaring TIFFTAG_GEOTIEPOINTS with field_readcount
65 : // and field_writecount == -1 == TIFF_VARIABLE, we are limited to writing
66 : // 65535 values in that tag. That could potentially be overcome by changing the tag
67 : // declaration to using TIFF_VARIABLE2 where the count is a uint32_t.
68 : constexpr int knMAX_GCP_COUNT =
69 : static_cast<int>(std::numeric_limits<uint16_t>::max() / 6);
70 :
71 : enum
72 : {
73 : ENDIANNESS_NATIVE,
74 : ENDIANNESS_LITTLE,
75 : ENDIANNESS_BIG
76 : };
77 :
78 17996 : static signed char GTiffGetWebPLevel(CSLConstList papszOptions)
79 : {
80 17996 : int nWebPLevel = DEFAULT_WEBP_LEVEL;
81 17996 : const char *pszValue = CSLFetchNameValue(papszOptions, "WEBP_LEVEL");
82 17996 : if (pszValue != nullptr)
83 : {
84 51 : nWebPLevel = atoi(pszValue);
85 51 : if (!(nWebPLevel >= 1 && nWebPLevel <= 100))
86 : {
87 0 : CPLError(CE_Warning, CPLE_IllegalArg,
88 : "WEBP_LEVEL=%s value not recognised, ignoring.", pszValue);
89 0 : nWebPLevel = DEFAULT_WEBP_LEVEL;
90 : }
91 : }
92 17996 : return static_cast<signed char>(nWebPLevel);
93 : }
94 :
95 18002 : static bool GTiffGetWebPLossless(CSLConstList papszOptions)
96 : {
97 18002 : return CPLFetchBool(papszOptions, "WEBP_LOSSLESS", false);
98 : }
99 :
100 18068 : static double GTiffGetLERCMaxZError(CSLConstList papszOptions)
101 : {
102 18068 : return CPLAtof(CSLFetchNameValueDef(papszOptions, "MAX_Z_ERROR", "0.0"));
103 : }
104 :
105 8071 : static double GTiffGetLERCMaxZErrorOverview(CSLConstList papszOptions)
106 : {
107 8071 : return CPLAtof(CSLFetchNameValueDef(
108 : papszOptions, "MAX_Z_ERROR_OVERVIEW",
109 8071 : CSLFetchNameValueDef(papszOptions, "MAX_Z_ERROR", "0.0")));
110 : }
111 :
112 : #if HAVE_JXL
113 18072 : static bool GTiffGetJXLLossless(CSLConstList papszOptions,
114 : bool *pbIsSpecified = nullptr)
115 : {
116 18072 : const char *pszVal = CSLFetchNameValue(papszOptions, "JXL_LOSSLESS");
117 18072 : if (pbIsSpecified)
118 9997 : *pbIsSpecified = pszVal != nullptr;
119 18072 : return pszVal == nullptr || CPLTestBool(pszVal);
120 : }
121 :
122 18072 : static uint32_t GTiffGetJXLEffort(CSLConstList papszOptions)
123 : {
124 18072 : return atoi(CSLFetchNameValueDef(papszOptions, "JXL_EFFORT", "5"));
125 : }
126 :
127 17990 : static float GTiffGetJXLDistance(CSLConstList papszOptions,
128 : bool *pbIsSpecified = nullptr)
129 : {
130 17990 : const char *pszVal = CSLFetchNameValue(papszOptions, "JXL_DISTANCE");
131 17990 : if (pbIsSpecified)
132 9997 : *pbIsSpecified = pszVal != nullptr;
133 17990 : return pszVal == nullptr ? 1.0f : static_cast<float>(CPLAtof(pszVal));
134 : }
135 :
136 18072 : static float GTiffGetJXLAlphaDistance(CSLConstList papszOptions,
137 : bool *pbIsSpecified = nullptr)
138 : {
139 18072 : const char *pszVal = CSLFetchNameValue(papszOptions, "JXL_ALPHA_DISTANCE");
140 18072 : if (pbIsSpecified)
141 9997 : *pbIsSpecified = pszVal != nullptr;
142 18072 : return pszVal == nullptr ? -1.0f : static_cast<float>(CPLAtof(pszVal));
143 : }
144 :
145 : #endif
146 :
147 : /************************************************************************/
148 : /* FillEmptyTiles() */
149 : /************************************************************************/
150 :
151 8260 : CPLErr GTiffDataset::FillEmptyTiles()
152 :
153 : {
154 : /* -------------------------------------------------------------------- */
155 : /* How many blocks are there in this file? */
156 : /* -------------------------------------------------------------------- */
157 16520 : const int nBlockCount = m_nPlanarConfig == PLANARCONFIG_SEPARATE
158 8260 : ? m_nBlocksPerBand * nBands
159 : : m_nBlocksPerBand;
160 :
161 : /* -------------------------------------------------------------------- */
162 : /* Fetch block maps. */
163 : /* -------------------------------------------------------------------- */
164 8260 : toff_t *panByteCounts = nullptr;
165 :
166 8260 : if (TIFFIsTiled(m_hTIFF))
167 1157 : TIFFGetField(m_hTIFF, TIFFTAG_TILEBYTECOUNTS, &panByteCounts);
168 : else
169 7103 : TIFFGetField(m_hTIFF, TIFFTAG_STRIPBYTECOUNTS, &panByteCounts);
170 :
171 8260 : if (panByteCounts == nullptr)
172 : {
173 : // Got here with libtiff 3.9.3 and tiff_write_8 test.
174 0 : ReportError(CE_Failure, CPLE_AppDefined,
175 : "FillEmptyTiles() failed because panByteCounts == NULL");
176 0 : return CE_Failure;
177 : }
178 :
179 : /* -------------------------------------------------------------------- */
180 : /* Prepare a blank data buffer to write for uninitialized blocks. */
181 : /* -------------------------------------------------------------------- */
182 : const GPtrDiff_t nBlockBytes =
183 8260 : TIFFIsTiled(m_hTIFF) ? static_cast<GPtrDiff_t>(TIFFTileSize(m_hTIFF))
184 7103 : : static_cast<GPtrDiff_t>(TIFFStripSize(m_hTIFF));
185 :
186 8260 : GByte *pabyData = static_cast<GByte *>(VSI_CALLOC_VERBOSE(nBlockBytes, 1));
187 8260 : if (pabyData == nullptr)
188 : {
189 0 : return CE_Failure;
190 : }
191 :
192 : // Force tiles completely filled with the nodata value to be written.
193 8260 : m_bWriteEmptyTiles = true;
194 :
195 : /* -------------------------------------------------------------------- */
196 : /* If set, fill data buffer with no data value. */
197 : /* -------------------------------------------------------------------- */
198 8260 : if ((m_bNoDataSet && m_dfNoDataValue != 0.0) ||
199 7978 : (m_bNoDataSetAsInt64 && m_nNoDataValueInt64 != 0) ||
200 7973 : (m_bNoDataSetAsUInt64 && m_nNoDataValueUInt64 != 0))
201 : {
202 292 : const GDALDataType eDataType = GetRasterBand(1)->GetRasterDataType();
203 292 : const int nDataTypeSize = GDALGetDataTypeSizeBytes(eDataType);
204 292 : if (nDataTypeSize &&
205 292 : nDataTypeSize * 8 == static_cast<int>(m_nBitsPerSample))
206 : {
207 281 : if (m_bNoDataSetAsInt64)
208 : {
209 6 : GDALCopyWords64(&m_nNoDataValueInt64, GDT_Int64, 0, pabyData,
210 : eDataType, nDataTypeSize,
211 6 : nBlockBytes / nDataTypeSize);
212 : }
213 275 : else if (m_bNoDataSetAsUInt64)
214 : {
215 5 : GDALCopyWords64(&m_nNoDataValueUInt64, GDT_UInt64, 0, pabyData,
216 : eDataType, nDataTypeSize,
217 5 : nBlockBytes / nDataTypeSize);
218 : }
219 : else
220 : {
221 270 : double dfNoData = m_dfNoDataValue;
222 270 : GDALCopyWords64(&dfNoData, GDT_Float64, 0, pabyData, eDataType,
223 270 : nDataTypeSize, nBlockBytes / nDataTypeSize);
224 281 : }
225 : }
226 11 : else if (nDataTypeSize)
227 : {
228 : // Handle non power-of-two depths.
229 : // Ideally make a packed buffer, but that is a bit tedious,
230 : // so use the normal I/O interfaces.
231 :
232 11 : CPLFree(pabyData);
233 :
234 11 : pabyData = static_cast<GByte *>(VSI_MALLOC3_VERBOSE(
235 : m_nBlockXSize, m_nBlockYSize, nDataTypeSize));
236 11 : if (pabyData == nullptr)
237 0 : return CE_Failure;
238 11 : if (m_bNoDataSetAsInt64)
239 : {
240 0 : GDALCopyWords64(&m_nNoDataValueInt64, GDT_Int64, 0, pabyData,
241 : eDataType, nDataTypeSize,
242 0 : static_cast<GPtrDiff_t>(m_nBlockXSize) *
243 0 : m_nBlockYSize);
244 : }
245 11 : else if (m_bNoDataSetAsUInt64)
246 : {
247 0 : GDALCopyWords64(&m_nNoDataValueUInt64, GDT_UInt64, 0, pabyData,
248 : eDataType, nDataTypeSize,
249 0 : static_cast<GPtrDiff_t>(m_nBlockXSize) *
250 0 : m_nBlockYSize);
251 : }
252 : else
253 : {
254 11 : GDALCopyWords64(&m_dfNoDataValue, GDT_Float64, 0, pabyData,
255 : eDataType, nDataTypeSize,
256 11 : static_cast<GPtrDiff_t>(m_nBlockXSize) *
257 11 : m_nBlockYSize);
258 : }
259 11 : CPLErr eErr = CE_None;
260 46 : for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
261 : {
262 35 : if (panByteCounts[iBlock] == 0)
263 : {
264 18 : if (m_nPlanarConfig == PLANARCONFIG_SEPARATE || nBands == 1)
265 : {
266 24 : if (GetRasterBand(1 + iBlock / m_nBlocksPerBand)
267 12 : ->WriteBlock((iBlock % m_nBlocksPerBand) %
268 12 : m_nBlocksPerRow,
269 12 : (iBlock % m_nBlocksPerBand) /
270 12 : m_nBlocksPerRow,
271 12 : pabyData) != CE_None)
272 : {
273 0 : eErr = CE_Failure;
274 : }
275 : }
276 : else
277 : {
278 : // In contig case, don't directly call WriteBlock(), as
279 : // it could cause useless decompression-recompression.
280 6 : const int nXOff =
281 6 : (iBlock % m_nBlocksPerRow) * m_nBlockXSize;
282 6 : const int nYOff =
283 6 : (iBlock / m_nBlocksPerRow) * m_nBlockYSize;
284 6 : const int nXSize =
285 6 : (nXOff + m_nBlockXSize <= nRasterXSize)
286 6 : ? m_nBlockXSize
287 2 : : nRasterXSize - nXOff;
288 6 : const int nYSize =
289 6 : (nYOff + m_nBlockYSize <= nRasterYSize)
290 6 : ? m_nBlockYSize
291 3 : : nRasterYSize - nYOff;
292 18 : for (int iBand = 1; iBand <= nBands; ++iBand)
293 : {
294 12 : if (GetRasterBand(iBand)->RasterIO(
295 : GF_Write, nXOff, nYOff, nXSize, nYSize,
296 : pabyData, nXSize, nYSize, eDataType, 0, 0,
297 12 : nullptr) != CE_None)
298 : {
299 0 : eErr = CE_Failure;
300 : }
301 : }
302 : }
303 : }
304 : }
305 11 : CPLFree(pabyData);
306 11 : return eErr;
307 281 : }
308 : }
309 :
310 : /* -------------------------------------------------------------------- */
311 : /* When we must fill with zeroes, try to create non-sparse file */
312 : /* w.r.t TIFF spec ... as a sparse file w.r.t filesystem, ie by */
313 : /* seeking to end of file instead of writing zero blocks. */
314 : /* -------------------------------------------------------------------- */
315 7968 : else if (m_nCompression == COMPRESSION_NONE && (m_nBitsPerSample % 8) == 0)
316 : {
317 6387 : CPLErr eErr = CE_None;
318 : // Only use libtiff to write the first sparse block to ensure that it
319 : // will serialize offset and count arrays back to disk.
320 6387 : int nCountBlocksToZero = 0;
321 2297880 : for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
322 : {
323 2291490 : if (panByteCounts[iBlock] == 0)
324 : {
325 2195960 : if (nCountBlocksToZero == 0)
326 : {
327 1139 : const bool bWriteEmptyTilesBak = m_bWriteEmptyTiles;
328 1139 : m_bWriteEmptyTiles = true;
329 1139 : const bool bOK = WriteEncodedTileOrStrip(iBlock, pabyData,
330 1139 : FALSE) == CE_None;
331 1139 : m_bWriteEmptyTiles = bWriteEmptyTilesBak;
332 1139 : if (!bOK)
333 : {
334 2 : eErr = CE_Failure;
335 2 : break;
336 : }
337 : }
338 2195960 : nCountBlocksToZero++;
339 : }
340 : }
341 6387 : CPLFree(pabyData);
342 :
343 6387 : --nCountBlocksToZero;
344 :
345 : // And then seek to end of file for other ones.
346 6387 : if (nCountBlocksToZero > 0)
347 : {
348 348 : toff_t *panByteOffsets = nullptr;
349 :
350 348 : if (TIFFIsTiled(m_hTIFF))
351 101 : TIFFGetField(m_hTIFF, TIFFTAG_TILEOFFSETS, &panByteOffsets);
352 : else
353 247 : TIFFGetField(m_hTIFF, TIFFTAG_STRIPOFFSETS, &panByteOffsets);
354 :
355 348 : if (panByteOffsets == nullptr)
356 : {
357 0 : ReportError(
358 : CE_Failure, CPLE_AppDefined,
359 : "FillEmptyTiles() failed because panByteOffsets == NULL");
360 0 : return CE_Failure;
361 : }
362 :
363 348 : VSILFILE *fpTIF = VSI_TIFFGetVSILFile(TIFFClientdata(m_hTIFF));
364 348 : VSIFSeekL(fpTIF, 0, SEEK_END);
365 348 : const vsi_l_offset nOffset = VSIFTellL(fpTIF);
366 :
367 348 : vsi_l_offset iBlockToZero = 0;
368 2204060 : for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
369 : {
370 2203710 : if (panByteCounts[iBlock] == 0)
371 : {
372 2194820 : panByteOffsets[iBlock] = static_cast<toff_t>(
373 2194820 : nOffset + iBlockToZero * nBlockBytes);
374 2194820 : panByteCounts[iBlock] = nBlockBytes;
375 2194820 : iBlockToZero++;
376 : }
377 : }
378 348 : CPLAssert(iBlockToZero ==
379 : static_cast<vsi_l_offset>(nCountBlocksToZero));
380 :
381 348 : if (VSIFTruncateL(fpTIF, nOffset + iBlockToZero * nBlockBytes) != 0)
382 : {
383 0 : eErr = CE_Failure;
384 0 : ReportError(CE_Failure, CPLE_FileIO,
385 : "Cannot initialize empty blocks");
386 : }
387 : }
388 :
389 6387 : return eErr;
390 : }
391 :
392 : /* -------------------------------------------------------------------- */
393 : /* Check all blocks, writing out data for uninitialized blocks. */
394 : /* -------------------------------------------------------------------- */
395 :
396 1862 : GByte *pabyRaw = nullptr;
397 1862 : vsi_l_offset nRawSize = 0;
398 1862 : CPLErr eErr = CE_None;
399 56590 : for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
400 : {
401 54735 : if (panByteCounts[iBlock] == 0)
402 : {
403 17528 : if (pabyRaw == nullptr)
404 : {
405 10166 : if (WriteEncodedTileOrStrip(iBlock, pabyData, FALSE) != CE_None)
406 : {
407 7 : eErr = CE_Failure;
408 7 : break;
409 : }
410 :
411 10159 : vsi_l_offset nOffset = 0;
412 10159 : if (!IsBlockAvailable(iBlock, &nOffset, &nRawSize, nullptr))
413 0 : break;
414 :
415 : // When using compression, get back the compressed block
416 : // so we can use the raw API to write it faster.
417 10159 : if (m_nCompression != COMPRESSION_NONE)
418 : {
419 : pabyRaw = static_cast<GByte *>(
420 485 : VSI_MALLOC_VERBOSE(static_cast<size_t>(nRawSize)));
421 485 : if (pabyRaw)
422 : {
423 : VSILFILE *fp =
424 485 : VSI_TIFFGetVSILFile(TIFFClientdata(m_hTIFF));
425 485 : const vsi_l_offset nCurOffset = VSIFTellL(fp);
426 485 : VSIFSeekL(fp, nOffset, SEEK_SET);
427 485 : VSIFReadL(pabyRaw, 1, static_cast<size_t>(nRawSize),
428 : fp);
429 485 : VSIFSeekL(fp, nCurOffset, SEEK_SET);
430 : }
431 : }
432 : }
433 : else
434 : {
435 7362 : WriteRawStripOrTile(iBlock, pabyRaw,
436 : static_cast<GPtrDiff_t>(nRawSize));
437 : }
438 : }
439 : }
440 :
441 1862 : CPLFree(pabyData);
442 1862 : VSIFree(pabyRaw);
443 1862 : return eErr;
444 : }
445 :
446 : /************************************************************************/
447 : /* HasOnlyNoData() */
448 : /************************************************************************/
449 :
450 42915 : bool GTiffDataset::HasOnlyNoData(const void *pBuffer, int nWidth, int nHeight,
451 : int nLineStride, int nComponents)
452 : {
453 42915 : if (m_nSampleFormat == SAMPLEFORMAT_COMPLEXINT ||
454 42915 : m_nSampleFormat == SAMPLEFORMAT_COMPLEXIEEEFP)
455 0 : return false;
456 42915 : if (m_bNoDataSetAsInt64 || m_bNoDataSetAsUInt64)
457 6 : return false; // FIXME: over pessimistic
458 85818 : return GDALBufferHasOnlyNoData(
459 42909 : pBuffer, m_bNoDataSet ? m_dfNoDataValue : 0.0, nWidth, nHeight,
460 42909 : nLineStride, nComponents, m_nBitsPerSample,
461 42909 : m_nSampleFormat == SAMPLEFORMAT_UINT ? GSF_UNSIGNED_INT
462 4926 : : m_nSampleFormat == SAMPLEFORMAT_INT ? GSF_SIGNED_INT
463 42909 : : GSF_FLOATING_POINT);
464 : }
465 :
466 : /************************************************************************/
467 : /* IsFirstPixelEqualToNoData() */
468 : /************************************************************************/
469 :
470 169393 : inline bool GTiffDataset::IsFirstPixelEqualToNoData(const void *pBuffer)
471 : {
472 169393 : const GDALDataType eDT = GetRasterBand(1)->GetRasterDataType();
473 169393 : const double dfEffectiveNoData = (m_bNoDataSet) ? m_dfNoDataValue : 0.0;
474 169393 : if (m_bNoDataSetAsInt64 || m_bNoDataSetAsUInt64)
475 10 : return true; // FIXME: over pessimistic
476 169383 : if (m_nBitsPerSample == 8 ||
477 59036 : (m_nBitsPerSample < 8 && dfEffectiveNoData == 0))
478 : {
479 113793 : if (eDT == GDT_Int8)
480 : {
481 278 : return GDALIsValueInRange<signed char>(dfEffectiveNoData) &&
482 139 : *(static_cast<const signed char *>(pBuffer)) ==
483 278 : static_cast<signed char>(dfEffectiveNoData);
484 : }
485 227277 : return GDALIsValueInRange<GByte>(dfEffectiveNoData) &&
486 113623 : *(static_cast<const GByte *>(pBuffer)) ==
487 227277 : static_cast<GByte>(dfEffectiveNoData);
488 : }
489 55590 : if (m_nBitsPerSample == 16 && eDT == GDT_UInt16)
490 : {
491 4714 : return GDALIsValueInRange<GUInt16>(dfEffectiveNoData) &&
492 2357 : *(static_cast<const GUInt16 *>(pBuffer)) ==
493 4714 : static_cast<GUInt16>(dfEffectiveNoData);
494 : }
495 53233 : if (m_nBitsPerSample == 16 && eDT == GDT_Int16)
496 : {
497 8476 : return GDALIsValueInRange<GInt16>(dfEffectiveNoData) &&
498 4238 : *(static_cast<const GInt16 *>(pBuffer)) ==
499 8476 : static_cast<GInt16>(dfEffectiveNoData);
500 : }
501 48995 : if (m_nBitsPerSample == 32 && eDT == GDT_UInt32)
502 : {
503 378 : return GDALIsValueInRange<GUInt32>(dfEffectiveNoData) &&
504 189 : *(static_cast<const GUInt32 *>(pBuffer)) ==
505 378 : static_cast<GUInt32>(dfEffectiveNoData);
506 : }
507 48806 : if (m_nBitsPerSample == 32 && eDT == GDT_Int32)
508 : {
509 506 : return GDALIsValueInRange<GInt32>(dfEffectiveNoData) &&
510 253 : *(static_cast<const GInt32 *>(pBuffer)) ==
511 506 : static_cast<GInt32>(dfEffectiveNoData);
512 : }
513 48553 : if (m_nBitsPerSample == 64 && eDT == GDT_UInt64)
514 : {
515 234 : return GDALIsValueInRange<std::uint64_t>(dfEffectiveNoData) &&
516 117 : *(static_cast<const std::uint64_t *>(pBuffer)) ==
517 234 : static_cast<std::uint64_t>(dfEffectiveNoData);
518 : }
519 48436 : if (m_nBitsPerSample == 64 && eDT == GDT_Int64)
520 : {
521 236 : return GDALIsValueInRange<std::int64_t>(dfEffectiveNoData) &&
522 118 : *(static_cast<const std::int64_t *>(pBuffer)) ==
523 236 : static_cast<std::int64_t>(dfEffectiveNoData);
524 : }
525 48318 : if (m_nBitsPerSample == 32 && eDT == GDT_Float32)
526 : {
527 41191 : if (std::isnan(m_dfNoDataValue))
528 3 : return std::isnan(*(static_cast<const float *>(pBuffer)));
529 82376 : return GDALIsValueInRange<float>(dfEffectiveNoData) &&
530 41188 : *(static_cast<const float *>(pBuffer)) ==
531 82376 : static_cast<float>(dfEffectiveNoData);
532 : }
533 7127 : if (m_nBitsPerSample == 64 && eDT == GDT_Float64)
534 : {
535 4454 : if (std::isnan(dfEffectiveNoData))
536 3 : return std::isnan(*(static_cast<const double *>(pBuffer)));
537 4451 : return *(static_cast<const double *>(pBuffer)) == dfEffectiveNoData;
538 : }
539 2673 : return false;
540 : }
541 :
542 : /************************************************************************/
543 : /* WriteDealWithLercAndNan() */
544 : /************************************************************************/
545 :
546 : template <typename T>
547 0 : void GTiffDataset::WriteDealWithLercAndNan(T *pBuffer, int nActualBlockWidth,
548 : int nActualBlockHeight,
549 : int nStrileHeight)
550 : {
551 : // This method does 2 things:
552 : // - warn the user if he tries to write NaN values with libtiff < 4.6.1
553 : // and multi-band PlanarConfig=Contig configuration
554 : // - and in right-most and bottom-most tiles, replace non accessible
555 : // pixel values by a safe one.
556 :
557 0 : const auto fPaddingValue =
558 : #if !defined(LIBTIFF_MULTIBAND_LERC_NAN_OK)
559 : m_nPlanarConfig == PLANARCONFIG_CONTIG && nBands > 1
560 : ? 0
561 : :
562 : #endif
563 : std::numeric_limits<T>::quiet_NaN();
564 :
565 0 : const int nBandsPerStrile =
566 0 : m_nPlanarConfig == PLANARCONFIG_CONTIG ? nBands : 1;
567 0 : for (int j = 0; j < nActualBlockHeight; ++j)
568 : {
569 : #if !defined(LIBTIFF_MULTIBAND_LERC_NAN_OK)
570 : static bool bHasWarned = false;
571 : if (m_nPlanarConfig == PLANARCONFIG_CONTIG && nBands > 1 && !bHasWarned)
572 : {
573 : for (int i = 0; i < nActualBlockWidth * nBandsPerStrile; ++i)
574 : {
575 : if (std::isnan(
576 : pBuffer[j * m_nBlockXSize * nBandsPerStrile + i]))
577 : {
578 : bHasWarned = true;
579 : CPLError(CE_Warning, CPLE_AppDefined,
580 : "libtiff < 4.6.1 does not handle properly NaN "
581 : "values for multi-band PlanarConfig=Contig "
582 : "configuration. As a workaround, you can set the "
583 : "INTERLEAVE=BAND creation option.");
584 : break;
585 : }
586 : }
587 : }
588 : #endif
589 0 : for (int i = nActualBlockWidth * nBandsPerStrile;
590 0 : i < m_nBlockXSize * nBandsPerStrile; ++i)
591 : {
592 0 : pBuffer[j * m_nBlockXSize * nBandsPerStrile + i] = fPaddingValue;
593 : }
594 : }
595 0 : for (int j = nActualBlockHeight; j < nStrileHeight; ++j)
596 : {
597 0 : for (int i = 0; i < m_nBlockXSize * nBandsPerStrile; ++i)
598 : {
599 0 : pBuffer[j * m_nBlockXSize * nBandsPerStrile + i] = fPaddingValue;
600 : }
601 : }
602 0 : }
603 :
604 : /************************************************************************/
605 : /* WriteEncodedTile() */
606 : /************************************************************************/
607 :
608 50586 : bool GTiffDataset::WriteEncodedTile(uint32_t tile, GByte *pabyData,
609 : int bPreserveDataBuffer)
610 : {
611 50586 : const int iColumn = (tile % m_nBlocksPerBand) % m_nBlocksPerRow;
612 50586 : const int iRow = (tile % m_nBlocksPerBand) / m_nBlocksPerRow;
613 :
614 101172 : const int nActualBlockWidth = (iColumn == m_nBlocksPerRow - 1)
615 50586 : ? nRasterXSize - iColumn * m_nBlockXSize
616 : : m_nBlockXSize;
617 101172 : const int nActualBlockHeight = (iRow == m_nBlocksPerColumn - 1)
618 50586 : ? nRasterYSize - iRow * m_nBlockYSize
619 : : m_nBlockYSize;
620 :
621 : /* -------------------------------------------------------------------- */
622 : /* Don't write empty blocks in some cases. */
623 : /* -------------------------------------------------------------------- */
624 50586 : if (!m_bWriteEmptyTiles && IsFirstPixelEqualToNoData(pabyData))
625 : {
626 1975 : if (!IsBlockAvailable(tile, nullptr, nullptr, nullptr))
627 : {
628 1975 : const int nComponents =
629 1975 : m_nPlanarConfig == PLANARCONFIG_CONTIG ? nBands : 1;
630 :
631 1975 : if (HasOnlyNoData(pabyData, nActualBlockWidth, nActualBlockHeight,
632 : m_nBlockXSize, nComponents))
633 : {
634 1200 : return true;
635 : }
636 : }
637 : }
638 :
639 : // Is this a partial right edge or bottom edge tile?
640 95726 : const bool bPartialTile = (nActualBlockWidth < m_nBlockXSize) ||
641 46340 : (nActualBlockHeight < m_nBlockYSize);
642 :
643 : const bool bIsLercFloatingPoint =
644 49452 : m_nCompression == COMPRESSION_LERC &&
645 66 : (GetRasterBand(1)->GetRasterDataType() == GDT_Float32 ||
646 64 : GetRasterBand(1)->GetRasterDataType() == GDT_Float64);
647 :
648 : // Do we need to spread edge values right or down for a partial
649 : // JPEG encoded tile? We do this to avoid edge artifacts.
650 : // We also need to be careful with LERC and NaN values
651 49386 : const bool bNeedTempBuffer =
652 54129 : bPartialTile &&
653 4743 : (m_nCompression == COMPRESSION_JPEG || bIsLercFloatingPoint);
654 :
655 : // If we need to fill out the tile, or if we want to prevent
656 : // TIFFWriteEncodedTile from altering the buffer as part of
657 : // byte swapping the data on write then we will need a temporary
658 : // working buffer. If not, we can just do a direct write.
659 49386 : const GPtrDiff_t cc = static_cast<GPtrDiff_t>(TIFFTileSize(m_hTIFF));
660 :
661 63718 : if (bPreserveDataBuffer &&
662 14332 : (TIFFIsByteSwapped(m_hTIFF) || bNeedTempBuffer || m_panMaskOffsetLsb))
663 : {
664 158 : if (m_pabyTempWriteBuffer == nullptr)
665 : {
666 35 : m_pabyTempWriteBuffer = CPLMalloc(cc);
667 : }
668 158 : memcpy(m_pabyTempWriteBuffer, pabyData, cc);
669 :
670 158 : pabyData = static_cast<GByte *>(m_pabyTempWriteBuffer);
671 : }
672 :
673 : // Perform tile fill if needed.
674 : // TODO: we should also handle the case of nBitsPerSample == 12
675 : // but this is more involved.
676 49386 : if (bPartialTile && m_nCompression == COMPRESSION_JPEG &&
677 134 : m_nBitsPerSample == 8)
678 : {
679 132 : const int nComponents =
680 132 : m_nPlanarConfig == PLANARCONFIG_CONTIG ? nBands : 1;
681 :
682 132 : CPLDebug("GTiff", "Filling out jpeg edge tile on write.");
683 :
684 132 : const int nRightPixelsToFill =
685 132 : iColumn == m_nBlocksPerRow - 1
686 132 : ? m_nBlockXSize * (iColumn + 1) - nRasterXSize
687 : : 0;
688 132 : const int nBottomPixelsToFill =
689 132 : iRow == m_nBlocksPerColumn - 1
690 132 : ? m_nBlockYSize * (iRow + 1) - nRasterYSize
691 : : 0;
692 :
693 : // Fill out to the right.
694 132 : const int iSrcX = m_nBlockXSize - nRightPixelsToFill - 1;
695 :
696 12461 : for (int iX = iSrcX + 1; iX < m_nBlockXSize; ++iX)
697 : {
698 3955880 : for (int iY = 0; iY < m_nBlockYSize; ++iY)
699 : {
700 3943550 : memcpy(pabyData +
701 3943550 : (static_cast<GPtrDiff_t>(m_nBlockXSize) * iY + iX) *
702 3943550 : nComponents,
703 3943550 : pabyData + (static_cast<GPtrDiff_t>(m_nBlockXSize) * iY +
704 3943550 : iSrcX) *
705 3943550 : nComponents,
706 : nComponents);
707 : }
708 : }
709 :
710 : // Now fill out the bottom.
711 132 : const int iSrcY = m_nBlockYSize - nBottomPixelsToFill - 1;
712 17682 : for (int iY = iSrcY + 1; iY < m_nBlockYSize; ++iY)
713 : {
714 17550 : memcpy(pabyData + static_cast<GPtrDiff_t>(m_nBlockXSize) *
715 17550 : nComponents * iY,
716 17550 : pabyData + static_cast<GPtrDiff_t>(m_nBlockXSize) *
717 17550 : nComponents * iSrcY,
718 17550 : static_cast<GPtrDiff_t>(m_nBlockXSize) * nComponents);
719 : }
720 : }
721 :
722 49386 : if (bIsLercFloatingPoint &&
723 : (bPartialTile
724 : #if !defined(LIBTIFF_MULTIBAND_LERC_NAN_OK)
725 : /* libtiff < 4.6.1 doesn't generate a LERC mask for multi-band contig configuration */
726 : || (m_nPlanarConfig == PLANARCONFIG_CONTIG && nBands > 1)
727 : #endif
728 : ))
729 : {
730 0 : if (GetRasterBand(1)->GetRasterDataType() == GDT_Float32)
731 0 : WriteDealWithLercAndNan(reinterpret_cast<float *>(pabyData),
732 : nActualBlockWidth, nActualBlockHeight,
733 : m_nBlockYSize);
734 : else
735 0 : WriteDealWithLercAndNan(reinterpret_cast<double *>(pabyData),
736 : nActualBlockWidth, nActualBlockHeight,
737 : m_nBlockYSize);
738 : }
739 :
740 49386 : if (m_panMaskOffsetLsb)
741 : {
742 0 : const int iBand = m_nPlanarConfig == PLANARCONFIG_SEPARATE
743 0 : ? static_cast<int>(tile) / m_nBlocksPerBand
744 : : -1;
745 0 : DiscardLsb(pabyData, cc, iBand);
746 : }
747 :
748 49386 : if (m_bStreamingOut)
749 : {
750 17 : if (tile != static_cast<uint32_t>(m_nLastWrittenBlockId + 1))
751 : {
752 1 : ReportError(CE_Failure, CPLE_NotSupported,
753 : "Attempt to write block %d whereas %d was expected",
754 1 : tile, m_nLastWrittenBlockId + 1);
755 1 : return false;
756 : }
757 16 : if (static_cast<GPtrDiff_t>(VSIFWriteL(pabyData, 1, cc, m_fpToWrite)) !=
758 : cc)
759 : {
760 0 : ReportError(CE_Failure, CPLE_FileIO,
761 : "Could not write " CPL_FRMT_GUIB " bytes",
762 : static_cast<GUIntBig>(cc));
763 0 : return false;
764 : }
765 16 : m_nLastWrittenBlockId = tile;
766 16 : return true;
767 : }
768 :
769 : /* -------------------------------------------------------------------- */
770 : /* Should we do compression in a worker thread ? */
771 : /* -------------------------------------------------------------------- */
772 49369 : if (SubmitCompressionJob(tile, pabyData, cc, m_nBlockYSize))
773 19945 : return true;
774 :
775 29424 : return TIFFWriteEncodedTile(m_hTIFF, tile, pabyData, cc) == cc;
776 : }
777 :
778 : /************************************************************************/
779 : /* WriteEncodedStrip() */
780 : /************************************************************************/
781 :
782 178411 : bool GTiffDataset::WriteEncodedStrip(uint32_t strip, GByte *pabyData,
783 : int bPreserveDataBuffer)
784 : {
785 178411 : GPtrDiff_t cc = static_cast<GPtrDiff_t>(TIFFStripSize(m_hTIFF));
786 178411 : const auto ccFull = cc;
787 :
788 : /* -------------------------------------------------------------------- */
789 : /* If this is the last strip in the image, and is partial, then */
790 : /* we need to trim the number of scanlines written to the */
791 : /* amount of valid data we have. (#2748) */
792 : /* -------------------------------------------------------------------- */
793 178411 : const int nStripWithinBand = strip % m_nBlocksPerBand;
794 178411 : int nStripHeight = m_nRowsPerStrip;
795 :
796 178411 : if (nStripWithinBand * nStripHeight > GetRasterYSize() - nStripHeight)
797 : {
798 384 : nStripHeight = GetRasterYSize() - nStripWithinBand * m_nRowsPerStrip;
799 384 : cc = (cc / m_nRowsPerStrip) * nStripHeight;
800 768 : CPLDebug("GTiff",
801 : "Adjusted bytes to write from " CPL_FRMT_GUIB
802 : " to " CPL_FRMT_GUIB ".",
803 384 : static_cast<GUIntBig>(TIFFStripSize(m_hTIFF)),
804 : static_cast<GUIntBig>(cc));
805 : }
806 :
807 : /* -------------------------------------------------------------------- */
808 : /* Don't write empty blocks in some cases. */
809 : /* -------------------------------------------------------------------- */
810 178411 : if (!m_bWriteEmptyTiles && IsFirstPixelEqualToNoData(pabyData))
811 : {
812 41114 : if (!IsBlockAvailable(strip, nullptr, nullptr, nullptr))
813 : {
814 40940 : const int nComponents =
815 40940 : m_nPlanarConfig == PLANARCONFIG_CONTIG ? nBands : 1;
816 :
817 40940 : if (HasOnlyNoData(pabyData, m_nBlockXSize, nStripHeight,
818 : m_nBlockXSize, nComponents))
819 : {
820 28307 : return true;
821 : }
822 : }
823 : }
824 :
825 : /* -------------------------------------------------------------------- */
826 : /* TIFFWriteEncodedStrip can alter the passed buffer if */
827 : /* byte-swapping is necessary so we use a temporary buffer */
828 : /* before calling it. */
829 : /* -------------------------------------------------------------------- */
830 239715 : if (bPreserveDataBuffer &&
831 89611 : (TIFFIsByteSwapped(m_hTIFF) || m_panMaskOffsetLsb))
832 : {
833 294 : if (m_pabyTempWriteBuffer == nullptr)
834 : {
835 126 : m_pabyTempWriteBuffer = CPLMalloc(ccFull);
836 : }
837 294 : memcpy(m_pabyTempWriteBuffer, pabyData, cc);
838 294 : pabyData = static_cast<GByte *>(m_pabyTempWriteBuffer);
839 : }
840 :
841 : #if !defined(LIBTIFF_MULTIBAND_LERC_NAN_OK)
842 : const bool bIsLercFloatingPoint =
843 : m_nCompression == COMPRESSION_LERC &&
844 : (GetRasterBand(1)->GetRasterDataType() == GDT_Float32 ||
845 : GetRasterBand(1)->GetRasterDataType() == GDT_Float64);
846 : if (bIsLercFloatingPoint &&
847 : /* libtiff < 4.6.1 doesn't generate a LERC mask for multi-band contig configuration */
848 : m_nPlanarConfig == PLANARCONFIG_CONTIG && nBands > 1)
849 : {
850 : if (GetRasterBand(1)->GetRasterDataType() == GDT_Float32)
851 : WriteDealWithLercAndNan(reinterpret_cast<float *>(pabyData),
852 : m_nBlockXSize, nStripHeight, nStripHeight);
853 : else
854 : WriteDealWithLercAndNan(reinterpret_cast<double *>(pabyData),
855 : m_nBlockXSize, nStripHeight, nStripHeight);
856 : }
857 : #endif
858 :
859 150104 : if (m_panMaskOffsetLsb)
860 : {
861 366 : int iBand = m_nPlanarConfig == PLANARCONFIG_SEPARATE
862 183 : ? static_cast<int>(strip) / m_nBlocksPerBand
863 : : -1;
864 183 : DiscardLsb(pabyData, cc, iBand);
865 : }
866 :
867 150104 : if (m_bStreamingOut)
868 : {
869 1408 : if (strip != static_cast<uint32_t>(m_nLastWrittenBlockId + 1))
870 : {
871 1 : ReportError(CE_Failure, CPLE_NotSupported,
872 : "Attempt to write block %d whereas %d was expected",
873 1 : strip, m_nLastWrittenBlockId + 1);
874 1 : return false;
875 : }
876 1407 : if (static_cast<GPtrDiff_t>(VSIFWriteL(pabyData, 1, cc, m_fpToWrite)) !=
877 : cc)
878 : {
879 0 : ReportError(CE_Failure, CPLE_FileIO,
880 : "Could not write " CPL_FRMT_GUIB " bytes",
881 : static_cast<GUIntBig>(cc));
882 0 : return false;
883 : }
884 1407 : m_nLastWrittenBlockId = strip;
885 1407 : return true;
886 : }
887 :
888 : /* -------------------------------------------------------------------- */
889 : /* Should we do compression in a worker thread ? */
890 : /* -------------------------------------------------------------------- */
891 148696 : if (SubmitCompressionJob(strip, pabyData, cc, nStripHeight))
892 6725 : return true;
893 :
894 141971 : return TIFFWriteEncodedStrip(m_hTIFF, strip, pabyData, cc) == cc;
895 : }
896 :
897 : /************************************************************************/
898 : /* InitCompressionThreads() */
899 : /************************************************************************/
900 :
901 32297 : void GTiffDataset::InitCompressionThreads(bool bUpdateMode,
902 : CSLConstList papszOptions)
903 : {
904 : // Raster == tile, then no need for threads
905 32297 : if (m_nBlockXSize == nRasterXSize && m_nBlockYSize == nRasterYSize)
906 23602 : return;
907 :
908 8695 : const char *pszNumThreads = "";
909 8695 : bool bOK = false;
910 8695 : const int nThreads = GDALGetNumThreads(
911 : papszOptions, "NUM_THREADS", GDAL_DEFAULT_MAX_THREAD_COUNT,
912 : /* bDefaultToAllCPUs=*/false, &pszNumThreads, &bOK);
913 8695 : if (nThreads > 1)
914 : {
915 106 : if ((bUpdateMode && m_nCompression != COMPRESSION_NONE) ||
916 24 : (nBands >= 1 && IsMultiThreadedReadCompatible()))
917 : {
918 77 : CPLDebug("GTiff",
919 : "Using up to %d threads for compression/decompression",
920 : nThreads);
921 :
922 77 : m_poThreadPool = GDALGetGlobalThreadPool(nThreads);
923 77 : if (bUpdateMode && m_poThreadPool)
924 58 : m_poCompressQueue = m_poThreadPool->CreateJobQueue();
925 :
926 77 : if (m_poCompressQueue != nullptr)
927 : {
928 : // Add a margin of an extra job w.r.t thread number
929 : // so as to optimize compression time (enables the main
930 : // thread to do boring I/O while all CPUs are working).
931 58 : m_asCompressionJobs.resize(nThreads + 1);
932 58 : memset(&m_asCompressionJobs[0], 0,
933 58 : m_asCompressionJobs.size() *
934 : sizeof(GTiffCompressionJob));
935 58 : for (int i = 0;
936 280 : i < static_cast<int>(m_asCompressionJobs.size()); ++i)
937 : {
938 444 : m_asCompressionJobs[i].pszTmpFilename =
939 222 : CPLStrdup(VSIMemGenerateHiddenFilename(
940 : CPLSPrintf("thread_job_%d.tif", i)));
941 222 : m_asCompressionJobs[i].nStripOrTile = -1;
942 : }
943 :
944 : // This is kind of a hack, but basically using
945 : // TIFFWriteRawStrip/Tile and then TIFFReadEncodedStrip/Tile
946 : // does not work on a newly created file, because
947 : // TIFF_MYBUFFER is not set in tif_flags
948 : // (if using TIFFWriteEncodedStrip/Tile first,
949 : // TIFFWriteBufferSetup() is automatically called).
950 : // This should likely rather fixed in libtiff itself.
951 58 : CPL_IGNORE_RET_VAL(TIFFWriteBufferSetup(m_hTIFF, nullptr, -1));
952 : }
953 : }
954 : }
955 8613 : else if (!bOK)
956 : {
957 3 : ReportError(CE_Warning, CPLE_AppDefined,
958 : "Invalid value for NUM_THREADS: %s", pszNumThreads);
959 : }
960 : }
961 :
962 : /************************************************************************/
963 : /* ThreadCompressionFunc() */
964 : /************************************************************************/
965 :
966 26682 : void GTiffDataset::ThreadCompressionFunc(void *pData)
967 : {
968 26682 : GTiffCompressionJob *psJob = static_cast<GTiffCompressionJob *>(pData);
969 26682 : GTiffDataset *poDS = psJob->poDS;
970 :
971 26682 : VSILFILE *fpTmp = VSIFOpenL(psJob->pszTmpFilename, "wb+");
972 26682 : TIFF *hTIFFTmp = VSI_TIFFOpen(
973 53364 : psJob->pszTmpFilename, psJob->bTIFFIsBigEndian ? "wb+" : "wl+", fpTmp);
974 26682 : CPLAssert(hTIFFTmp != nullptr);
975 26682 : TIFFSetField(hTIFFTmp, TIFFTAG_IMAGEWIDTH, poDS->m_nBlockXSize);
976 26682 : TIFFSetField(hTIFFTmp, TIFFTAG_IMAGELENGTH, psJob->nHeight);
977 26682 : TIFFSetField(hTIFFTmp, TIFFTAG_BITSPERSAMPLE, poDS->m_nBitsPerSample);
978 26682 : TIFFSetField(hTIFFTmp, TIFFTAG_COMPRESSION, poDS->m_nCompression);
979 26682 : TIFFSetField(hTIFFTmp, TIFFTAG_PHOTOMETRIC, poDS->m_nPhotometric);
980 26682 : TIFFSetField(hTIFFTmp, TIFFTAG_SAMPLEFORMAT, poDS->m_nSampleFormat);
981 26682 : TIFFSetField(hTIFFTmp, TIFFTAG_SAMPLESPERPIXEL, poDS->m_nSamplesPerPixel);
982 26682 : TIFFSetField(hTIFFTmp, TIFFTAG_ROWSPERSTRIP, poDS->m_nBlockYSize);
983 26682 : TIFFSetField(hTIFFTmp, TIFFTAG_PLANARCONFIG, poDS->m_nPlanarConfig);
984 26682 : if (psJob->nPredictor != PREDICTOR_NONE)
985 263 : TIFFSetField(hTIFFTmp, TIFFTAG_PREDICTOR, psJob->nPredictor);
986 26682 : if (poDS->m_nCompression == COMPRESSION_LERC)
987 : {
988 24 : TIFFSetField(hTIFFTmp, TIFFTAG_LERC_PARAMETERS, 2,
989 24 : poDS->m_anLercAddCompressionAndVersion);
990 : }
991 26682 : if (psJob->nExtraSampleCount)
992 : {
993 352 : TIFFSetField(hTIFFTmp, TIFFTAG_EXTRASAMPLES, psJob->nExtraSampleCount,
994 : psJob->pExtraSamples);
995 : }
996 :
997 26682 : poDS->RestoreVolatileParameters(hTIFFTmp);
998 :
999 53364 : bool bOK = TIFFWriteEncodedStrip(hTIFFTmp, 0, psJob->pabyBuffer,
1000 26682 : psJob->nBufferSize) == psJob->nBufferSize;
1001 :
1002 26682 : toff_t nOffset = 0;
1003 26682 : if (bOK)
1004 : {
1005 26682 : toff_t *panOffsets = nullptr;
1006 26682 : toff_t *panByteCounts = nullptr;
1007 26682 : TIFFGetField(hTIFFTmp, TIFFTAG_STRIPOFFSETS, &panOffsets);
1008 26682 : TIFFGetField(hTIFFTmp, TIFFTAG_STRIPBYTECOUNTS, &panByteCounts);
1009 :
1010 26682 : nOffset = panOffsets[0];
1011 26682 : psJob->nCompressedBufferSize =
1012 26682 : static_cast<GPtrDiff_t>(panByteCounts[0]);
1013 : }
1014 : else
1015 : {
1016 0 : CPLError(CE_Failure, CPLE_AppDefined,
1017 : "Error when compressing strip/tile %d", psJob->nStripOrTile);
1018 : }
1019 :
1020 26682 : XTIFFClose(hTIFFTmp);
1021 26682 : if (VSIFCloseL(fpTmp) != 0)
1022 : {
1023 0 : if (bOK)
1024 : {
1025 0 : bOK = false;
1026 0 : CPLError(CE_Failure, CPLE_AppDefined,
1027 : "Error when compressing strip/tile %d",
1028 : psJob->nStripOrTile);
1029 : }
1030 : }
1031 :
1032 26682 : if (bOK)
1033 : {
1034 26682 : vsi_l_offset nFileSize = 0;
1035 : GByte *pabyCompressedBuffer =
1036 26682 : VSIGetMemFileBuffer(psJob->pszTmpFilename, &nFileSize, FALSE);
1037 26682 : CPLAssert(static_cast<vsi_l_offset>(
1038 : nOffset + psJob->nCompressedBufferSize) <= nFileSize);
1039 26682 : psJob->pabyCompressedBuffer = pabyCompressedBuffer + nOffset;
1040 : }
1041 : else
1042 : {
1043 0 : psJob->pabyCompressedBuffer = nullptr;
1044 0 : psJob->nCompressedBufferSize = 0;
1045 : }
1046 :
1047 26682 : auto poMainDS = poDS->m_poBaseDS ? poDS->m_poBaseDS : poDS;
1048 26682 : if (poMainDS->m_poCompressQueue)
1049 : {
1050 1576 : std::lock_guard oLock(poMainDS->m_oCompressThreadPoolMutex);
1051 1576 : psJob->bReady = true;
1052 : }
1053 26682 : }
1054 :
1055 : /************************************************************************/
1056 : /* WriteRawStripOrTile() */
1057 : /************************************************************************/
1058 :
1059 34044 : void GTiffDataset::WriteRawStripOrTile(int nStripOrTile,
1060 : GByte *pabyCompressedBuffer,
1061 : GPtrDiff_t nCompressedBufferSize)
1062 : {
1063 : #ifdef DEBUG_VERBOSE
1064 : CPLDebug("GTIFF", "Writing raw strip/tile %d, size " CPL_FRMT_GUIB,
1065 : nStripOrTile, static_cast<GUIntBig>(nCompressedBufferSize));
1066 : #endif
1067 34044 : toff_t *panOffsets = nullptr;
1068 34044 : toff_t *panByteCounts = nullptr;
1069 34044 : bool bWriteAtEnd = true;
1070 34044 : bool bWriteLeader = m_bLeaderSizeAsUInt4;
1071 34044 : bool bWriteTrailer = m_bTrailerRepeatedLast4BytesRepeated;
1072 34044 : if (TIFFGetField(m_hTIFF,
1073 34044 : TIFFIsTiled(m_hTIFF) ? TIFFTAG_TILEOFFSETS
1074 : : TIFFTAG_STRIPOFFSETS,
1075 34044 : &panOffsets) &&
1076 34044 : panOffsets != nullptr && panOffsets[nStripOrTile] != 0)
1077 : {
1078 : // Forces TIFFAppendStrip() to consider if the location of the
1079 : // tile/strip can be reused or if the strile should be written at end of
1080 : // file.
1081 360 : TIFFSetWriteOffset(m_hTIFF, 0);
1082 :
1083 360 : if (m_bBlockOrderRowMajor)
1084 : {
1085 264 : if (TIFFGetField(m_hTIFF,
1086 264 : TIFFIsTiled(m_hTIFF) ? TIFFTAG_TILEBYTECOUNTS
1087 : : TIFFTAG_STRIPBYTECOUNTS,
1088 528 : &panByteCounts) &&
1089 264 : panByteCounts != nullptr)
1090 : {
1091 264 : if (static_cast<GUIntBig>(nCompressedBufferSize) >
1092 264 : panByteCounts[nStripOrTile])
1093 : {
1094 8 : GTiffDataset *poRootDS = m_poBaseDS ? m_poBaseDS : this;
1095 8 : if (!poRootDS->m_bKnownIncompatibleEdition &&
1096 8 : !poRootDS->m_bWriteKnownIncompatibleEdition)
1097 : {
1098 8 : ReportError(
1099 : CE_Warning, CPLE_AppDefined,
1100 : "A strile cannot be rewritten in place, which "
1101 : "invalidates the BLOCK_ORDER optimization.");
1102 8 : poRootDS->m_bKnownIncompatibleEdition = true;
1103 8 : poRootDS->m_bWriteKnownIncompatibleEdition = true;
1104 : }
1105 : }
1106 : // For mask interleaving, if the size is not exactly the same,
1107 : // completely give up (we could potentially move the mask in
1108 : // case the imagery is smaller)
1109 256 : else if (m_poMaskDS && m_bMaskInterleavedWithImagery &&
1110 0 : static_cast<GUIntBig>(nCompressedBufferSize) !=
1111 0 : panByteCounts[nStripOrTile])
1112 : {
1113 0 : GTiffDataset *poRootDS = m_poBaseDS ? m_poBaseDS : this;
1114 0 : if (!poRootDS->m_bKnownIncompatibleEdition &&
1115 0 : !poRootDS->m_bWriteKnownIncompatibleEdition)
1116 : {
1117 0 : ReportError(
1118 : CE_Warning, CPLE_AppDefined,
1119 : "A strile cannot be rewritten in place, which "
1120 : "invalidates the MASK_INTERLEAVED_WITH_IMAGERY "
1121 : "optimization.");
1122 0 : poRootDS->m_bKnownIncompatibleEdition = true;
1123 0 : poRootDS->m_bWriteKnownIncompatibleEdition = true;
1124 : }
1125 0 : bWriteLeader = false;
1126 0 : bWriteTrailer = false;
1127 0 : if (m_bLeaderSizeAsUInt4)
1128 : {
1129 : // If there was a valid leader, invalidat it
1130 0 : VSI_TIFFSeek(m_hTIFF, panOffsets[nStripOrTile] - 4,
1131 : SEEK_SET);
1132 : uint32_t nOldSize;
1133 0 : VSIFReadL(&nOldSize, 1, 4,
1134 : VSI_TIFFGetVSILFile(TIFFClientdata(m_hTIFF)));
1135 0 : CPL_LSBPTR32(&nOldSize);
1136 0 : if (nOldSize == panByteCounts[nStripOrTile])
1137 : {
1138 0 : uint32_t nInvalidatedSize = 0;
1139 0 : VSI_TIFFSeek(m_hTIFF, panOffsets[nStripOrTile] - 4,
1140 : SEEK_SET);
1141 0 : VSI_TIFFWrite(m_hTIFF, &nInvalidatedSize,
1142 : sizeof(nInvalidatedSize));
1143 : }
1144 : }
1145 : }
1146 : else
1147 : {
1148 256 : bWriteAtEnd = false;
1149 : }
1150 : }
1151 : }
1152 : }
1153 34044 : if (bWriteLeader &&
1154 25111 : static_cast<GUIntBig>(nCompressedBufferSize) <= 0xFFFFFFFFU)
1155 : {
1156 : // cppcheck-suppress knownConditionTrueFalse
1157 25111 : if (bWriteAtEnd)
1158 : {
1159 24855 : VSI_TIFFSeek(m_hTIFF, 0, SEEK_END);
1160 : }
1161 : else
1162 : {
1163 : // If we rewrite an existing strile in place with an existing
1164 : // leader, check that the leader is valid, before rewriting it. And
1165 : // if it is not valid, then do not write the trailer, as we could
1166 : // corrupt other data.
1167 256 : VSI_TIFFSeek(m_hTIFF, panOffsets[nStripOrTile] - 4, SEEK_SET);
1168 : uint32_t nOldSize;
1169 256 : VSIFReadL(&nOldSize, 1, 4,
1170 : VSI_TIFFGetVSILFile(TIFFClientdata(m_hTIFF)));
1171 256 : CPL_LSBPTR32(&nOldSize);
1172 256 : bWriteLeader =
1173 256 : panByteCounts && nOldSize == panByteCounts[nStripOrTile];
1174 256 : bWriteTrailer = bWriteLeader;
1175 256 : VSI_TIFFSeek(m_hTIFF, panOffsets[nStripOrTile] - 4, SEEK_SET);
1176 : }
1177 : // cppcheck-suppress knownConditionTrueFalse
1178 25111 : if (bWriteLeader)
1179 : {
1180 25111 : uint32_t nSize = static_cast<uint32_t>(nCompressedBufferSize);
1181 25111 : CPL_LSBPTR32(&nSize);
1182 25111 : if (!VSI_TIFFWrite(m_hTIFF, &nSize, sizeof(nSize)))
1183 0 : m_bWriteError = true;
1184 : }
1185 : }
1186 : tmsize_t written;
1187 34044 : if (TIFFIsTiled(m_hTIFF))
1188 26371 : written = TIFFWriteRawTile(m_hTIFF, nStripOrTile, pabyCompressedBuffer,
1189 : nCompressedBufferSize);
1190 : else
1191 7673 : written = TIFFWriteRawStrip(m_hTIFF, nStripOrTile, pabyCompressedBuffer,
1192 : nCompressedBufferSize);
1193 34044 : if (written != nCompressedBufferSize)
1194 12 : m_bWriteError = true;
1195 34044 : if (bWriteTrailer &&
1196 25111 : static_cast<GUIntBig>(nCompressedBufferSize) <= 0xFFFFFFFFU)
1197 : {
1198 25111 : GByte abyLastBytes[4] = {};
1199 25111 : if (nCompressedBufferSize >= 4)
1200 25111 : memcpy(abyLastBytes,
1201 25111 : pabyCompressedBuffer + nCompressedBufferSize - 4, 4);
1202 : else
1203 0 : memcpy(abyLastBytes, pabyCompressedBuffer, nCompressedBufferSize);
1204 25111 : if (!VSI_TIFFWrite(m_hTIFF, abyLastBytes, 4))
1205 0 : m_bWriteError = true;
1206 : }
1207 34044 : }
1208 :
1209 : /************************************************************************/
1210 : /* WaitCompletionForJobIdx() */
1211 : /************************************************************************/
1212 :
1213 1576 : void GTiffDataset::WaitCompletionForJobIdx(int i)
1214 : {
1215 1576 : auto poMainDS = m_poBaseDS ? m_poBaseDS : this;
1216 1576 : auto poQueue = poMainDS->m_poCompressQueue.get();
1217 1576 : auto &oQueue = poMainDS->m_asQueueJobIdx;
1218 1576 : auto &asJobs = poMainDS->m_asCompressionJobs;
1219 1576 : auto &mutex = poMainDS->m_oCompressThreadPoolMutex;
1220 :
1221 1576 : CPLAssert(i >= 0 && static_cast<size_t>(i) < asJobs.size());
1222 1576 : CPLAssert(asJobs[i].nStripOrTile >= 0);
1223 1576 : CPLAssert(!oQueue.empty());
1224 :
1225 1576 : bool bHasWarned = false;
1226 : while (true)
1227 : {
1228 : bool bReady;
1229 : {
1230 2281 : std::lock_guard oLock(mutex);
1231 2281 : bReady = asJobs[i].bReady;
1232 : }
1233 2281 : if (!bReady)
1234 : {
1235 705 : if (!bHasWarned)
1236 : {
1237 447 : CPLDebug("GTIFF",
1238 : "Waiting for worker job to finish handling block %d",
1239 447 : asJobs[i].nStripOrTile);
1240 447 : bHasWarned = true;
1241 : }
1242 705 : poQueue->GetPool()->WaitEvent();
1243 : }
1244 : else
1245 : {
1246 1576 : break;
1247 : }
1248 705 : }
1249 :
1250 1576 : if (asJobs[i].nCompressedBufferSize)
1251 : {
1252 3152 : asJobs[i].poDS->WriteRawStripOrTile(asJobs[i].nStripOrTile,
1253 1576 : asJobs[i].pabyCompressedBuffer,
1254 1576 : asJobs[i].nCompressedBufferSize);
1255 : }
1256 1576 : asJobs[i].pabyCompressedBuffer = nullptr;
1257 1576 : asJobs[i].nBufferSize = 0;
1258 : {
1259 : // Likely useless, but makes Coverity happy
1260 1576 : std::lock_guard oLock(mutex);
1261 1576 : asJobs[i].bReady = false;
1262 : }
1263 1576 : asJobs[i].nStripOrTile = -1;
1264 1576 : oQueue.pop();
1265 1576 : }
1266 :
1267 : /************************************************************************/
1268 : /* WaitCompletionForBlock() */
1269 : /************************************************************************/
1270 :
1271 2323140 : void GTiffDataset::WaitCompletionForBlock(int nBlockId)
1272 : {
1273 2323140 : auto poQueue = m_poBaseDS ? m_poBaseDS->m_poCompressQueue.get()
1274 2303880 : : m_poCompressQueue.get();
1275 : // cppcheck-suppress constVariableReference
1276 2323140 : auto &oQueue = m_poBaseDS ? m_poBaseDS->m_asQueueJobIdx : m_asQueueJobIdx;
1277 : // cppcheck-suppress constVariableReference
1278 2303880 : auto &asJobs =
1279 2323140 : m_poBaseDS ? m_poBaseDS->m_asCompressionJobs : m_asCompressionJobs;
1280 :
1281 2323140 : if (poQueue != nullptr && !oQueue.empty())
1282 : {
1283 1066 : for (int i = 0; i < static_cast<int>(asJobs.size()); ++i)
1284 : {
1285 888 : if (asJobs[i].poDS == this && asJobs[i].nStripOrTile == nBlockId)
1286 : {
1287 128 : while (!oQueue.empty() &&
1288 64 : !(asJobs[oQueue.front()].poDS == this &&
1289 64 : asJobs[oQueue.front()].nStripOrTile == nBlockId))
1290 : {
1291 0 : WaitCompletionForJobIdx(oQueue.front());
1292 : }
1293 64 : CPLAssert(!oQueue.empty() &&
1294 : asJobs[oQueue.front()].poDS == this &&
1295 : asJobs[oQueue.front()].nStripOrTile == nBlockId);
1296 64 : WaitCompletionForJobIdx(oQueue.front());
1297 : }
1298 : }
1299 : }
1300 2323140 : }
1301 :
1302 : /************************************************************************/
1303 : /* SubmitCompressionJob() */
1304 : /************************************************************************/
1305 :
1306 198065 : bool GTiffDataset::SubmitCompressionJob(int nStripOrTile, GByte *pabyData,
1307 : GPtrDiff_t cc, int nHeight)
1308 : {
1309 : /* -------------------------------------------------------------------- */
1310 : /* Should we do compression in a worker thread ? */
1311 : /* -------------------------------------------------------------------- */
1312 198065 : auto poQueue = m_poBaseDS ? m_poBaseDS->m_poCompressQueue.get()
1313 183991 : : m_poCompressQueue.get();
1314 :
1315 198065 : if (poQueue && m_nCompression == COMPRESSION_NONE)
1316 : {
1317 : // We don't do multi-threaded compression for uncompressed...
1318 : // but we must wait for other related compression tasks (e.g mask)
1319 : // to be completed
1320 0 : poQueue->WaitCompletion();
1321 :
1322 : // Flush remaining data
1323 : // cppcheck-suppress constVariableReference
1324 0 : auto &oQueue =
1325 0 : m_poBaseDS ? m_poBaseDS->m_asQueueJobIdx : m_asQueueJobIdx;
1326 0 : while (!oQueue.empty())
1327 : {
1328 0 : WaitCompletionForJobIdx(oQueue.front());
1329 : }
1330 : }
1331 :
1332 : const auto SetupJob =
1333 123564 : [this, pabyData, cc, nHeight, nStripOrTile](GTiffCompressionJob &sJob)
1334 : {
1335 26682 : sJob.poDS = this;
1336 26682 : sJob.bTIFFIsBigEndian = CPL_TO_BOOL(TIFFIsBigEndian(m_hTIFF));
1337 : GByte *pabyBuffer =
1338 26682 : static_cast<GByte *>(VSI_REALLOC_VERBOSE(sJob.pabyBuffer, cc));
1339 26682 : if (!pabyBuffer)
1340 0 : return false;
1341 26682 : sJob.pabyBuffer = pabyBuffer;
1342 26682 : memcpy(sJob.pabyBuffer, pabyData, cc);
1343 26682 : sJob.nBufferSize = cc;
1344 26682 : sJob.nHeight = nHeight;
1345 26682 : sJob.nStripOrTile = nStripOrTile;
1346 26682 : sJob.nPredictor = PREDICTOR_NONE;
1347 26682 : if (GTIFFSupportsPredictor(m_nCompression))
1348 : {
1349 16836 : TIFFGetField(m_hTIFF, TIFFTAG_PREDICTOR, &sJob.nPredictor);
1350 : }
1351 :
1352 26682 : sJob.pExtraSamples = nullptr;
1353 26682 : sJob.nExtraSampleCount = 0;
1354 26682 : TIFFGetField(m_hTIFF, TIFFTAG_EXTRASAMPLES, &sJob.nExtraSampleCount,
1355 : &sJob.pExtraSamples);
1356 26682 : return true;
1357 198065 : };
1358 :
1359 198065 : if (poQueue == nullptr || !(m_nCompression == COMPRESSION_ADOBE_DEFLATE ||
1360 806 : m_nCompression == COMPRESSION_LZW ||
1361 78 : m_nCompression == COMPRESSION_PACKBITS ||
1362 72 : m_nCompression == COMPRESSION_LZMA ||
1363 62 : m_nCompression == COMPRESSION_ZSTD ||
1364 52 : m_nCompression == COMPRESSION_LERC ||
1365 46 : m_nCompression == COMPRESSION_JXL ||
1366 46 : m_nCompression == COMPRESSION_JXL_DNG_1_7 ||
1367 28 : m_nCompression == COMPRESSION_WEBP ||
1368 18 : m_nCompression == COMPRESSION_JPEG))
1369 : {
1370 196489 : if (m_bBlockOrderRowMajor || m_bLeaderSizeAsUInt4 ||
1371 171383 : m_bTrailerRepeatedLast4BytesRepeated)
1372 : {
1373 : GTiffCompressionJob sJob;
1374 25106 : memset(&sJob, 0, sizeof(sJob));
1375 25106 : if (SetupJob(sJob))
1376 : {
1377 25106 : sJob.pszTmpFilename =
1378 25106 : CPLStrdup(VSIMemGenerateHiddenFilename("temp.tif"));
1379 :
1380 25106 : ThreadCompressionFunc(&sJob);
1381 :
1382 25106 : if (sJob.nCompressedBufferSize)
1383 : {
1384 25106 : sJob.poDS->WriteRawStripOrTile(sJob.nStripOrTile,
1385 : sJob.pabyCompressedBuffer,
1386 : sJob.nCompressedBufferSize);
1387 : }
1388 :
1389 25106 : CPLFree(sJob.pabyBuffer);
1390 25106 : VSIUnlink(sJob.pszTmpFilename);
1391 25106 : CPLFree(sJob.pszTmpFilename);
1392 25106 : return sJob.nCompressedBufferSize > 0 && !m_bWriteError;
1393 : }
1394 : }
1395 :
1396 171383 : return false;
1397 : }
1398 :
1399 1576 : auto poMainDS = m_poBaseDS ? m_poBaseDS : this;
1400 1576 : auto &oQueue = poMainDS->m_asQueueJobIdx;
1401 1576 : auto &asJobs = poMainDS->m_asCompressionJobs;
1402 :
1403 1576 : int nNextCompressionJobAvail = -1;
1404 :
1405 1576 : if (oQueue.size() == asJobs.size())
1406 : {
1407 1443 : CPLAssert(!oQueue.empty());
1408 1443 : nNextCompressionJobAvail = oQueue.front();
1409 1443 : WaitCompletionForJobIdx(nNextCompressionJobAvail);
1410 : }
1411 : else
1412 : {
1413 133 : const int nJobs = static_cast<int>(asJobs.size());
1414 324 : for (int i = 0; i < nJobs; ++i)
1415 : {
1416 324 : if (asJobs[i].nBufferSize == 0)
1417 : {
1418 133 : nNextCompressionJobAvail = i;
1419 133 : break;
1420 : }
1421 : }
1422 : }
1423 1576 : CPLAssert(nNextCompressionJobAvail >= 0);
1424 :
1425 1576 : GTiffCompressionJob *psJob = &asJobs[nNextCompressionJobAvail];
1426 1576 : bool bOK = SetupJob(*psJob);
1427 1576 : if (bOK)
1428 : {
1429 1576 : poQueue->SubmitJob(ThreadCompressionFunc, psJob);
1430 1576 : oQueue.push(nNextCompressionJobAvail);
1431 : }
1432 :
1433 1576 : return bOK;
1434 : }
1435 :
1436 : /************************************************************************/
1437 : /* DiscardLsb() */
1438 : /************************************************************************/
1439 :
1440 272 : template <class T> bool MustNotDiscardLsb(T value, bool bHasNoData, T nodata)
1441 : {
1442 272 : return bHasNoData && value == nodata;
1443 : }
1444 :
1445 : template <>
1446 44 : bool MustNotDiscardLsb<float>(float value, bool bHasNoData, float nodata)
1447 : {
1448 44 : return (bHasNoData && value == nodata) || !std::isfinite(value);
1449 : }
1450 :
1451 : template <>
1452 44 : bool MustNotDiscardLsb<double>(double value, bool bHasNoData, double nodata)
1453 : {
1454 44 : return (bHasNoData && value == nodata) || !std::isfinite(value);
1455 : }
1456 :
1457 : template <class T> T AdjustValue(T value, uint64_t nRoundUpBitTest);
1458 :
1459 : template <class T>
1460 10 : CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW T AdjustValueInt(T value,
1461 : uint64_t nRoundUpBitTest)
1462 : {
1463 10 : if (value >=
1464 10 : static_cast<T>(std::numeric_limits<T>::max() - (nRoundUpBitTest << 1)))
1465 0 : return static_cast<T>(value - (nRoundUpBitTest << 1));
1466 10 : return static_cast<T>(value + (nRoundUpBitTest << 1));
1467 : }
1468 :
1469 0 : template <> int8_t AdjustValue<int8_t>(int8_t value, uint64_t nRoundUpBitTest)
1470 : {
1471 0 : return AdjustValueInt(value, nRoundUpBitTest);
1472 : }
1473 :
1474 : template <>
1475 2 : uint8_t AdjustValue<uint8_t>(uint8_t value, uint64_t nRoundUpBitTest)
1476 : {
1477 2 : return AdjustValueInt(value, nRoundUpBitTest);
1478 : }
1479 :
1480 : template <>
1481 2 : int16_t AdjustValue<int16_t>(int16_t value, uint64_t nRoundUpBitTest)
1482 : {
1483 2 : return AdjustValueInt(value, nRoundUpBitTest);
1484 : }
1485 :
1486 : template <>
1487 2 : uint16_t AdjustValue<uint16_t>(uint16_t value, uint64_t nRoundUpBitTest)
1488 : {
1489 2 : return AdjustValueInt(value, nRoundUpBitTest);
1490 : }
1491 :
1492 : template <>
1493 2 : int32_t AdjustValue<int32_t>(int32_t value, uint64_t nRoundUpBitTest)
1494 : {
1495 2 : return AdjustValueInt(value, nRoundUpBitTest);
1496 : }
1497 :
1498 : template <>
1499 2 : uint32_t AdjustValue<uint32_t>(uint32_t value, uint64_t nRoundUpBitTest)
1500 : {
1501 2 : return AdjustValueInt(value, nRoundUpBitTest);
1502 : }
1503 :
1504 : template <>
1505 0 : int64_t AdjustValue<int64_t>(int64_t value, uint64_t nRoundUpBitTest)
1506 : {
1507 0 : return AdjustValueInt(value, nRoundUpBitTest);
1508 : }
1509 :
1510 : template <>
1511 0 : uint64_t AdjustValue<uint64_t>(uint64_t value, uint64_t nRoundUpBitTest)
1512 : {
1513 0 : return AdjustValueInt(value, nRoundUpBitTest);
1514 : }
1515 :
1516 0 : template <> GFloat16 AdjustValue<GFloat16>(GFloat16 value, uint64_t)
1517 : {
1518 : using std::nextafter;
1519 0 : return nextafter(value, cpl::NumericLimits<GFloat16>::max());
1520 : }
1521 :
1522 0 : template <> float AdjustValue<float>(float value, uint64_t)
1523 : {
1524 0 : return std::nextafter(value, std::numeric_limits<float>::max());
1525 : }
1526 :
1527 0 : template <> double AdjustValue<double>(double value, uint64_t)
1528 : {
1529 0 : return std::nextafter(value, std::numeric_limits<double>::max());
1530 : }
1531 :
1532 : template <class Teffective, class T>
1533 : T RoundValueDiscardLsb(const void *ptr, uint64_t nMask,
1534 : uint64_t nRoundUpBitTest);
1535 :
1536 : template <class T>
1537 16 : T RoundValueDiscardLsbUnsigned(const void *ptr, uint64_t nMask,
1538 : uint64_t nRoundUpBitTest)
1539 : {
1540 32 : if ((*reinterpret_cast<const T *>(ptr) & nMask) >
1541 16 : static_cast<uint64_t>(std::numeric_limits<T>::max()) -
1542 16 : (nRoundUpBitTest << 1U))
1543 : {
1544 4 : return static_cast<T>(std::numeric_limits<T>::max() & nMask);
1545 : }
1546 12 : const uint64_t newval =
1547 12 : (*reinterpret_cast<const T *>(ptr) & nMask) + (nRoundUpBitTest << 1U);
1548 12 : return static_cast<T>(newval);
1549 : }
1550 :
1551 : template <class T>
1552 18 : T RoundValueDiscardLsbSigned(const void *ptr, uint64_t nMask,
1553 : uint64_t nRoundUpBitTest)
1554 : {
1555 18 : T oldval = *reinterpret_cast<const T *>(ptr);
1556 18 : if (oldval < 0)
1557 : {
1558 4 : return static_cast<T>(oldval & nMask);
1559 : }
1560 14 : const uint64_t newval =
1561 14 : (*reinterpret_cast<const T *>(ptr) & nMask) + (nRoundUpBitTest << 1U);
1562 14 : if (newval > static_cast<uint64_t>(std::numeric_limits<T>::max()))
1563 4 : return static_cast<T>(std::numeric_limits<T>::max() & nMask);
1564 10 : return static_cast<T>(newval);
1565 : }
1566 :
1567 : template <>
1568 11 : uint16_t RoundValueDiscardLsb<uint16_t, uint16_t>(const void *ptr,
1569 : uint64_t nMask,
1570 : uint64_t nRoundUpBitTest)
1571 : {
1572 11 : return RoundValueDiscardLsbUnsigned<uint16_t>(ptr, nMask, nRoundUpBitTest);
1573 : }
1574 :
1575 : template <>
1576 5 : uint32_t RoundValueDiscardLsb<uint32_t, uint32_t>(const void *ptr,
1577 : uint64_t nMask,
1578 : uint64_t nRoundUpBitTest)
1579 : {
1580 5 : return RoundValueDiscardLsbUnsigned<uint32_t>(ptr, nMask, nRoundUpBitTest);
1581 : }
1582 :
1583 : template <>
1584 0 : uint64_t RoundValueDiscardLsb<uint64_t, uint64_t>(const void *ptr,
1585 : uint64_t nMask,
1586 : uint64_t nRoundUpBitTest)
1587 : {
1588 0 : return RoundValueDiscardLsbUnsigned<uint64_t>(ptr, nMask, nRoundUpBitTest);
1589 : }
1590 :
1591 : template <>
1592 0 : int8_t RoundValueDiscardLsb<int8_t, int8_t>(const void *ptr, uint64_t nMask,
1593 : uint64_t nRoundUpBitTest)
1594 : {
1595 0 : return RoundValueDiscardLsbSigned<int8_t>(ptr, nMask, nRoundUpBitTest);
1596 : }
1597 :
1598 : template <>
1599 13 : int16_t RoundValueDiscardLsb<int16_t, int16_t>(const void *ptr, uint64_t nMask,
1600 : uint64_t nRoundUpBitTest)
1601 : {
1602 13 : return RoundValueDiscardLsbSigned<int16_t>(ptr, nMask, nRoundUpBitTest);
1603 : }
1604 :
1605 : template <>
1606 5 : int32_t RoundValueDiscardLsb<int32_t, int32_t>(const void *ptr, uint64_t nMask,
1607 : uint64_t nRoundUpBitTest)
1608 : {
1609 5 : return RoundValueDiscardLsbSigned<int32_t>(ptr, nMask, nRoundUpBitTest);
1610 : }
1611 :
1612 : template <>
1613 0 : int64_t RoundValueDiscardLsb<int64_t, int64_t>(const void *ptr, uint64_t nMask,
1614 : uint64_t nRoundUpBitTest)
1615 : {
1616 0 : return RoundValueDiscardLsbSigned<int64_t>(ptr, nMask, nRoundUpBitTest);
1617 : }
1618 :
1619 : template <>
1620 0 : uint16_t RoundValueDiscardLsb<GFloat16, uint16_t>(const void *ptr,
1621 : uint64_t nMask,
1622 : uint64_t nRoundUpBitTest)
1623 : {
1624 0 : return RoundValueDiscardLsbUnsigned<uint16_t>(ptr, nMask, nRoundUpBitTest);
1625 : }
1626 :
1627 : template <>
1628 0 : uint32_t RoundValueDiscardLsb<float, uint32_t>(const void *ptr, uint64_t nMask,
1629 : uint64_t nRoundUpBitTest)
1630 : {
1631 0 : return RoundValueDiscardLsbUnsigned<uint32_t>(ptr, nMask, nRoundUpBitTest);
1632 : }
1633 :
1634 : template <>
1635 0 : uint64_t RoundValueDiscardLsb<double, uint64_t>(const void *ptr, uint64_t nMask,
1636 : uint64_t nRoundUpBitTest)
1637 : {
1638 0 : return RoundValueDiscardLsbUnsigned<uint64_t>(ptr, nMask, nRoundUpBitTest);
1639 : }
1640 :
1641 : template <class Teffective, class T>
1642 145 : static void DiscardLsbT(GByte *pabyBuffer, size_t nBytes, int iBand, int nBands,
1643 : uint16_t nPlanarConfig,
1644 : const GTiffDataset::MaskOffset *panMaskOffsetLsb,
1645 : bool bHasNoData, Teffective nNoDataValue)
1646 : {
1647 : static_assert(sizeof(Teffective) == sizeof(T),
1648 : "sizeof(Teffective) == sizeof(T)");
1649 145 : if (nPlanarConfig == PLANARCONFIG_SEPARATE)
1650 : {
1651 98 : const auto nMask = panMaskOffsetLsb[iBand].nMask;
1652 98 : const auto nRoundUpBitTest = panMaskOffsetLsb[iBand].nRoundUpBitTest;
1653 196 : for (size_t i = 0; i < nBytes / sizeof(T); ++i)
1654 : {
1655 98 : if (MustNotDiscardLsb(reinterpret_cast<Teffective *>(pabyBuffer)[i],
1656 : bHasNoData, nNoDataValue))
1657 : {
1658 22 : continue;
1659 : }
1660 :
1661 76 : if (reinterpret_cast<T *>(pabyBuffer)[i] & nRoundUpBitTest)
1662 : {
1663 30 : reinterpret_cast<T *>(pabyBuffer)[i] =
1664 15 : RoundValueDiscardLsb<Teffective, T>(
1665 15 : &(reinterpret_cast<T *>(pabyBuffer)[i]), nMask,
1666 : nRoundUpBitTest);
1667 : }
1668 : else
1669 : {
1670 61 : reinterpret_cast<T *>(pabyBuffer)[i] = static_cast<T>(
1671 61 : reinterpret_cast<T *>(pabyBuffer)[i] & nMask);
1672 : }
1673 :
1674 : // Make sure that by discarding LSB we don't end up to a value
1675 : // that is no the nodata value
1676 76 : if (MustNotDiscardLsb(reinterpret_cast<Teffective *>(pabyBuffer)[i],
1677 : bHasNoData, nNoDataValue))
1678 : {
1679 8 : reinterpret_cast<Teffective *>(pabyBuffer)[i] =
1680 4 : AdjustValue(nNoDataValue, nRoundUpBitTest);
1681 : }
1682 : }
1683 : }
1684 : else
1685 : {
1686 94 : for (size_t i = 0; i < nBytes / sizeof(T); i += nBands)
1687 : {
1688 147 : for (int j = 0; j < nBands; ++j)
1689 : {
1690 100 : if (MustNotDiscardLsb(
1691 100 : reinterpret_cast<Teffective *>(pabyBuffer)[i + j],
1692 : bHasNoData, nNoDataValue))
1693 : {
1694 14 : continue;
1695 : }
1696 :
1697 86 : if (reinterpret_cast<T *>(pabyBuffer)[i + j] &
1698 86 : panMaskOffsetLsb[j].nRoundUpBitTest)
1699 : {
1700 38 : reinterpret_cast<T *>(pabyBuffer)[i + j] =
1701 19 : RoundValueDiscardLsb<Teffective, T>(
1702 19 : &(reinterpret_cast<T *>(pabyBuffer)[i + j]),
1703 19 : panMaskOffsetLsb[j].nMask,
1704 19 : panMaskOffsetLsb[j].nRoundUpBitTest);
1705 : }
1706 : else
1707 : {
1708 67 : reinterpret_cast<T *>(pabyBuffer)[i + j] = static_cast<T>(
1709 67 : (reinterpret_cast<T *>(pabyBuffer)[i + j] &
1710 67 : panMaskOffsetLsb[j].nMask));
1711 : }
1712 :
1713 : // Make sure that by discarding LSB we don't end up to a value
1714 : // that is no the nodata value
1715 86 : if (MustNotDiscardLsb(
1716 86 : reinterpret_cast<Teffective *>(pabyBuffer)[i + j],
1717 : bHasNoData, nNoDataValue))
1718 : {
1719 8 : reinterpret_cast<Teffective *>(pabyBuffer)[i + j] =
1720 4 : AdjustValue(nNoDataValue,
1721 4 : panMaskOffsetLsb[j].nRoundUpBitTest);
1722 : }
1723 : }
1724 : }
1725 : }
1726 145 : }
1727 :
1728 183 : static void DiscardLsb(GByte *pabyBuffer, GPtrDiff_t nBytes, int iBand,
1729 : int nBands, uint16_t nSampleFormat,
1730 : uint16_t nBitsPerSample, uint16_t nPlanarConfig,
1731 : const GTiffDataset::MaskOffset *panMaskOffsetLsb,
1732 : bool bHasNoData, double dfNoDataValue)
1733 : {
1734 183 : if (nBitsPerSample == 8 && nSampleFormat == SAMPLEFORMAT_UINT)
1735 : {
1736 38 : uint8_t nNoDataValue = 0;
1737 38 : if (bHasNoData && GDALIsValueExactAs<uint8_t>(dfNoDataValue))
1738 : {
1739 6 : nNoDataValue = static_cast<uint8_t>(dfNoDataValue);
1740 : }
1741 : else
1742 : {
1743 32 : bHasNoData = false;
1744 : }
1745 38 : if (nPlanarConfig == PLANARCONFIG_SEPARATE)
1746 : {
1747 25 : const auto nMask =
1748 25 : static_cast<unsigned>(panMaskOffsetLsb[iBand].nMask);
1749 25 : const auto nRoundUpBitTest =
1750 25 : static_cast<unsigned>(panMaskOffsetLsb[iBand].nRoundUpBitTest);
1751 50 : for (decltype(nBytes) i = 0; i < nBytes; ++i)
1752 : {
1753 25 : if (bHasNoData && pabyBuffer[i] == nNoDataValue)
1754 3 : continue;
1755 :
1756 : // Keep 255 in case it is alpha.
1757 22 : if (pabyBuffer[i] != 255)
1758 : {
1759 21 : if (pabyBuffer[i] & nRoundUpBitTest)
1760 5 : pabyBuffer[i] = static_cast<GByte>(
1761 5 : std::min(255U, (pabyBuffer[i] & nMask) +
1762 5 : (nRoundUpBitTest << 1U)));
1763 : else
1764 16 : pabyBuffer[i] =
1765 16 : static_cast<GByte>(pabyBuffer[i] & nMask);
1766 :
1767 : // Make sure that by discarding LSB we don't end up to a
1768 : // value that is no the nodata value
1769 21 : if (bHasNoData && pabyBuffer[i] == nNoDataValue)
1770 2 : pabyBuffer[i] =
1771 1 : AdjustValue(nNoDataValue, nRoundUpBitTest);
1772 : }
1773 : }
1774 : }
1775 : else
1776 : {
1777 26 : for (decltype(nBytes) i = 0; i < nBytes; i += nBands)
1778 : {
1779 42 : for (int j = 0; j < nBands; ++j)
1780 : {
1781 29 : if (bHasNoData && pabyBuffer[i + j] == nNoDataValue)
1782 2 : continue;
1783 :
1784 : // Keep 255 in case it is alpha.
1785 27 : if (pabyBuffer[i + j] != 255)
1786 : {
1787 25 : if (pabyBuffer[i + j] &
1788 25 : panMaskOffsetLsb[j].nRoundUpBitTest)
1789 : {
1790 6 : pabyBuffer[i + j] = static_cast<GByte>(std::min(
1791 12 : 255U,
1792 6 : (pabyBuffer[i + j] &
1793 : static_cast<unsigned>(
1794 6 : panMaskOffsetLsb[j].nMask)) +
1795 : (static_cast<unsigned>(
1796 6 : panMaskOffsetLsb[j].nRoundUpBitTest)
1797 6 : << 1U)));
1798 : }
1799 : else
1800 : {
1801 19 : pabyBuffer[i + j] = static_cast<GByte>(
1802 19 : pabyBuffer[i + j] & panMaskOffsetLsb[j].nMask);
1803 : }
1804 :
1805 : // Make sure that by discarding LSB we don't end up to a
1806 : // value that is no the nodata value
1807 25 : if (bHasNoData && pabyBuffer[i + j] == nNoDataValue)
1808 1 : pabyBuffer[i + j] = AdjustValue(
1809 : nNoDataValue,
1810 1 : panMaskOffsetLsb[j].nRoundUpBitTest);
1811 : }
1812 : }
1813 : }
1814 38 : }
1815 : }
1816 145 : else if (nBitsPerSample == 8 && nSampleFormat == SAMPLEFORMAT_INT)
1817 : {
1818 0 : int8_t nNoDataValue = 0;
1819 0 : if (bHasNoData && GDALIsValueExactAs<int8_t>(dfNoDataValue))
1820 : {
1821 0 : nNoDataValue = static_cast<int8_t>(dfNoDataValue);
1822 : }
1823 : else
1824 : {
1825 0 : bHasNoData = false;
1826 : }
1827 0 : DiscardLsbT<int8_t, int8_t>(pabyBuffer, nBytes, iBand, nBands,
1828 : nPlanarConfig, panMaskOffsetLsb, bHasNoData,
1829 0 : nNoDataValue);
1830 : }
1831 145 : else if (nBitsPerSample == 16 && nSampleFormat == SAMPLEFORMAT_INT)
1832 : {
1833 48 : int16_t nNoDataValue = 0;
1834 48 : if (bHasNoData && GDALIsValueExactAs<int16_t>(dfNoDataValue))
1835 : {
1836 6 : nNoDataValue = static_cast<int16_t>(dfNoDataValue);
1837 : }
1838 : else
1839 : {
1840 42 : bHasNoData = false;
1841 : }
1842 48 : DiscardLsbT<int16_t, int16_t>(pabyBuffer, nBytes, iBand, nBands,
1843 : nPlanarConfig, panMaskOffsetLsb,
1844 48 : bHasNoData, nNoDataValue);
1845 : }
1846 97 : else if (nBitsPerSample == 16 && nSampleFormat == SAMPLEFORMAT_UINT)
1847 : {
1848 33 : uint16_t nNoDataValue = 0;
1849 33 : if (bHasNoData && GDALIsValueExactAs<uint16_t>(dfNoDataValue))
1850 : {
1851 6 : nNoDataValue = static_cast<uint16_t>(dfNoDataValue);
1852 : }
1853 : else
1854 : {
1855 27 : bHasNoData = false;
1856 : }
1857 33 : DiscardLsbT<uint16_t, uint16_t>(pabyBuffer, nBytes, iBand, nBands,
1858 : nPlanarConfig, panMaskOffsetLsb,
1859 33 : bHasNoData, nNoDataValue);
1860 : }
1861 64 : else if (nBitsPerSample == 32 && nSampleFormat == SAMPLEFORMAT_INT)
1862 : {
1863 13 : int32_t nNoDataValue = 0;
1864 13 : if (bHasNoData && GDALIsValueExactAs<int32_t>(dfNoDataValue))
1865 : {
1866 6 : nNoDataValue = static_cast<int32_t>(dfNoDataValue);
1867 : }
1868 : else
1869 : {
1870 7 : bHasNoData = false;
1871 : }
1872 13 : DiscardLsbT<int32_t, int32_t>(pabyBuffer, nBytes, iBand, nBands,
1873 : nPlanarConfig, panMaskOffsetLsb,
1874 13 : bHasNoData, nNoDataValue);
1875 : }
1876 51 : else if (nBitsPerSample == 32 && nSampleFormat == SAMPLEFORMAT_UINT)
1877 : {
1878 13 : uint32_t nNoDataValue = 0;
1879 13 : if (bHasNoData && GDALIsValueExactAs<uint32_t>(dfNoDataValue))
1880 : {
1881 6 : nNoDataValue = static_cast<uint32_t>(dfNoDataValue);
1882 : }
1883 : else
1884 : {
1885 7 : bHasNoData = false;
1886 : }
1887 13 : DiscardLsbT<uint32_t, uint32_t>(pabyBuffer, nBytes, iBand, nBands,
1888 : nPlanarConfig, panMaskOffsetLsb,
1889 13 : bHasNoData, nNoDataValue);
1890 : }
1891 38 : else if (nBitsPerSample == 64 && nSampleFormat == SAMPLEFORMAT_INT)
1892 : {
1893 : // FIXME: we should not rely on dfNoDataValue when we support native
1894 : // data type for nodata
1895 0 : int64_t nNoDataValue = 0;
1896 0 : if (bHasNoData && GDALIsValueExactAs<int64_t>(dfNoDataValue))
1897 : {
1898 0 : nNoDataValue = static_cast<int64_t>(dfNoDataValue);
1899 : }
1900 : else
1901 : {
1902 0 : bHasNoData = false;
1903 : }
1904 0 : DiscardLsbT<int64_t, int64_t>(pabyBuffer, nBytes, iBand, nBands,
1905 : nPlanarConfig, panMaskOffsetLsb,
1906 0 : bHasNoData, nNoDataValue);
1907 : }
1908 38 : else if (nBitsPerSample == 64 && nSampleFormat == SAMPLEFORMAT_UINT)
1909 : {
1910 : // FIXME: we should not rely on dfNoDataValue when we support native
1911 : // data type for nodata
1912 0 : uint64_t nNoDataValue = 0;
1913 0 : if (bHasNoData && GDALIsValueExactAs<uint64_t>(dfNoDataValue))
1914 : {
1915 0 : nNoDataValue = static_cast<uint64_t>(dfNoDataValue);
1916 : }
1917 : else
1918 : {
1919 0 : bHasNoData = false;
1920 : }
1921 0 : DiscardLsbT<uint64_t, uint64_t>(pabyBuffer, nBytes, iBand, nBands,
1922 : nPlanarConfig, panMaskOffsetLsb,
1923 0 : bHasNoData, nNoDataValue);
1924 : }
1925 38 : else if (nBitsPerSample == 16 && nSampleFormat == SAMPLEFORMAT_IEEEFP)
1926 : {
1927 0 : const GFloat16 fNoDataValue = static_cast<GFloat16>(dfNoDataValue);
1928 0 : DiscardLsbT<GFloat16, uint16_t>(pabyBuffer, nBytes, iBand, nBands,
1929 : nPlanarConfig, panMaskOffsetLsb,
1930 0 : bHasNoData, fNoDataValue);
1931 : }
1932 38 : else if (nBitsPerSample == 32 && nSampleFormat == SAMPLEFORMAT_IEEEFP)
1933 : {
1934 19 : const float fNoDataValue = static_cast<float>(dfNoDataValue);
1935 19 : DiscardLsbT<float, uint32_t>(pabyBuffer, nBytes, iBand, nBands,
1936 : nPlanarConfig, panMaskOffsetLsb,
1937 19 : bHasNoData, fNoDataValue);
1938 : }
1939 19 : else if (nBitsPerSample == 64 && nSampleFormat == SAMPLEFORMAT_IEEEFP)
1940 : {
1941 19 : DiscardLsbT<double, uint64_t>(pabyBuffer, nBytes, iBand, nBands,
1942 : nPlanarConfig, panMaskOffsetLsb,
1943 : bHasNoData, dfNoDataValue);
1944 : }
1945 183 : }
1946 :
1947 183 : void GTiffDataset::DiscardLsb(GByte *pabyBuffer, GPtrDiff_t nBytes,
1948 : int iBand) const
1949 : {
1950 183 : ::DiscardLsb(pabyBuffer, nBytes, iBand, nBands, m_nSampleFormat,
1951 183 : m_nBitsPerSample, m_nPlanarConfig, m_panMaskOffsetLsb,
1952 183 : m_bNoDataSet, m_dfNoDataValue);
1953 183 : }
1954 :
1955 : /************************************************************************/
1956 : /* WriteEncodedTileOrStrip() */
1957 : /************************************************************************/
1958 :
1959 228997 : CPLErr GTiffDataset::WriteEncodedTileOrStrip(uint32_t tile_or_strip, void *data,
1960 : int bPreserveDataBuffer)
1961 : {
1962 228997 : CPLErr eErr = CE_None;
1963 :
1964 228997 : if (TIFFIsTiled(m_hTIFF))
1965 : {
1966 50586 : if (!(WriteEncodedTile(tile_or_strip, static_cast<GByte *>(data),
1967 : bPreserveDataBuffer)))
1968 : {
1969 14 : eErr = CE_Failure;
1970 : }
1971 : }
1972 : else
1973 : {
1974 178411 : if (!(WriteEncodedStrip(tile_or_strip, static_cast<GByte *>(data),
1975 : bPreserveDataBuffer)))
1976 : {
1977 8 : eErr = CE_Failure;
1978 : }
1979 : }
1980 :
1981 228997 : return eErr;
1982 : }
1983 :
1984 : /************************************************************************/
1985 : /* FlushBlockBuf() */
1986 : /************************************************************************/
1987 :
1988 9627 : CPLErr GTiffDataset::FlushBlockBuf()
1989 :
1990 : {
1991 9627 : if (m_nLoadedBlock < 0 || !m_bLoadedBlockDirty)
1992 0 : return CE_None;
1993 :
1994 9627 : m_bLoadedBlockDirty = false;
1995 :
1996 : const CPLErr eErr =
1997 9627 : WriteEncodedTileOrStrip(m_nLoadedBlock, m_pabyBlockBuf, true);
1998 9627 : if (eErr != CE_None)
1999 : {
2000 0 : ReportError(CE_Failure, CPLE_AppDefined,
2001 : "WriteEncodedTile/Strip() failed.");
2002 0 : m_bWriteError = true;
2003 : }
2004 :
2005 9627 : return eErr;
2006 : }
2007 :
2008 : /************************************************************************/
2009 : /* GTiffFillStreamableOffsetAndCount() */
2010 : /************************************************************************/
2011 :
2012 8 : static void GTiffFillStreamableOffsetAndCount(TIFF *hTIFF, int nSize)
2013 : {
2014 8 : uint32_t nXSize = 0;
2015 8 : uint32_t nYSize = 0;
2016 8 : TIFFGetField(hTIFF, TIFFTAG_IMAGEWIDTH, &nXSize);
2017 8 : TIFFGetField(hTIFF, TIFFTAG_IMAGELENGTH, &nYSize);
2018 8 : const bool bIsTiled = CPL_TO_BOOL(TIFFIsTiled(hTIFF));
2019 : const int nBlockCount =
2020 8 : bIsTiled ? TIFFNumberOfTiles(hTIFF) : TIFFNumberOfStrips(hTIFF);
2021 :
2022 8 : toff_t *panOffset = nullptr;
2023 8 : TIFFGetField(hTIFF, bIsTiled ? TIFFTAG_TILEOFFSETS : TIFFTAG_STRIPOFFSETS,
2024 : &panOffset);
2025 8 : toff_t *panSize = nullptr;
2026 8 : TIFFGetField(hTIFF,
2027 : bIsTiled ? TIFFTAG_TILEBYTECOUNTS : TIFFTAG_STRIPBYTECOUNTS,
2028 : &panSize);
2029 8 : toff_t nOffset = nSize;
2030 : // Trick to avoid clang static analyzer raising false positive about
2031 : // divide by zero later.
2032 8 : int nBlocksPerBand = 1;
2033 8 : uint32_t nRowsPerStrip = 0;
2034 8 : if (!bIsTiled)
2035 : {
2036 6 : TIFFGetField(hTIFF, TIFFTAG_ROWSPERSTRIP, &nRowsPerStrip);
2037 6 : if (nRowsPerStrip > static_cast<uint32_t>(nYSize))
2038 0 : nRowsPerStrip = nYSize;
2039 6 : nBlocksPerBand = DIV_ROUND_UP(nYSize, nRowsPerStrip);
2040 : }
2041 2947 : for (int i = 0; i < nBlockCount; ++i)
2042 : {
2043 : GPtrDiff_t cc = bIsTiled
2044 2939 : ? static_cast<GPtrDiff_t>(TIFFTileSize(hTIFF))
2045 2907 : : static_cast<GPtrDiff_t>(TIFFStripSize(hTIFF));
2046 2939 : if (!bIsTiled)
2047 : {
2048 : /* --------------------------------------------------------------------
2049 : */
2050 : /* If this is the last strip in the image, and is partial, then
2051 : */
2052 : /* we need to trim the number of scanlines written to the */
2053 : /* amount of valid data we have. (#2748) */
2054 : /* --------------------------------------------------------------------
2055 : */
2056 2907 : int nStripWithinBand = i % nBlocksPerBand;
2057 2907 : if (nStripWithinBand * nRowsPerStrip > nYSize - nRowsPerStrip)
2058 : {
2059 1 : cc = (cc / nRowsPerStrip) *
2060 1 : (nYSize - nStripWithinBand * nRowsPerStrip);
2061 : }
2062 : }
2063 2939 : panOffset[i] = nOffset;
2064 2939 : panSize[i] = cc;
2065 2939 : nOffset += cc;
2066 : }
2067 8 : }
2068 :
2069 : /************************************************************************/
2070 : /* Crystalize() */
2071 : /* */
2072 : /* Make sure that the directory information is written out for */
2073 : /* a new file, require before writing any imagery data. */
2074 : /************************************************************************/
2075 :
2076 2659100 : void GTiffDataset::Crystalize()
2077 :
2078 : {
2079 2659100 : if (m_bCrystalized)
2080 2653280 : return;
2081 :
2082 : // TODO: libtiff writes extended tags in the order they are specified
2083 : // and not in increasing order.
2084 5819 : WriteMetadata(this, m_hTIFF, true, m_eProfile, m_osFilename.c_str(),
2085 5819 : m_papszCreationOptions);
2086 5819 : WriteGeoTIFFInfo();
2087 5819 : if (m_bNoDataSet)
2088 359 : WriteNoDataValue(m_hTIFF, m_dfNoDataValue);
2089 5460 : else if (m_bNoDataSetAsInt64)
2090 4 : WriteNoDataValue(m_hTIFF, m_nNoDataValueInt64);
2091 5456 : else if (m_bNoDataSetAsUInt64)
2092 4 : WriteNoDataValue(m_hTIFF, m_nNoDataValueUInt64);
2093 :
2094 5819 : m_bMetadataChanged = false;
2095 5819 : m_bGeoTIFFInfoChanged = false;
2096 5819 : m_bNoDataChanged = false;
2097 5819 : m_bNeedsRewrite = false;
2098 :
2099 5819 : m_bCrystalized = true;
2100 :
2101 5819 : TIFFWriteCheck(m_hTIFF, TIFFIsTiled(m_hTIFF), "GTiffDataset::Crystalize");
2102 :
2103 5819 : TIFFWriteDirectory(m_hTIFF);
2104 5819 : if (m_bStreamingOut)
2105 : {
2106 : // We need to write twice the directory to be sure that custom
2107 : // TIFF tags are correctly sorted and that padding bytes have been
2108 : // added.
2109 3 : TIFFSetDirectory(m_hTIFF, 0);
2110 3 : TIFFWriteDirectory(m_hTIFF);
2111 :
2112 3 : if (VSIFSeekL(m_fpL, 0, SEEK_END) != 0)
2113 : {
2114 0 : ReportError(CE_Failure, CPLE_FileIO, "Could not seek");
2115 : }
2116 3 : const int nSize = static_cast<int>(VSIFTellL(m_fpL));
2117 :
2118 3 : TIFFSetDirectory(m_hTIFF, 0);
2119 3 : GTiffFillStreamableOffsetAndCount(m_hTIFF, nSize);
2120 3 : TIFFWriteDirectory(m_hTIFF);
2121 :
2122 3 : vsi_l_offset nDataLength = 0;
2123 : void *pabyBuffer =
2124 3 : VSIGetMemFileBuffer(m_pszTmpFilename, &nDataLength, FALSE);
2125 3 : if (static_cast<int>(VSIFWriteL(
2126 3 : pabyBuffer, 1, static_cast<int>(nDataLength), m_fpToWrite)) !=
2127 : static_cast<int>(nDataLength))
2128 : {
2129 0 : ReportError(CE_Failure, CPLE_FileIO, "Could not write %d bytes",
2130 : static_cast<int>(nDataLength));
2131 : }
2132 : // In case of single strip file, there's a libtiff check that would
2133 : // issue a warning since the file hasn't the required size.
2134 3 : CPLPushErrorHandler(CPLQuietErrorHandler);
2135 3 : TIFFSetDirectory(m_hTIFF, 0);
2136 3 : CPLPopErrorHandler();
2137 : }
2138 : else
2139 : {
2140 5816 : const tdir_t nNumberOfDirs = TIFFNumberOfDirectories(m_hTIFF);
2141 5816 : if (nNumberOfDirs > 0)
2142 : {
2143 5816 : TIFFSetDirectory(m_hTIFF, static_cast<tdir_t>(nNumberOfDirs - 1));
2144 : }
2145 : }
2146 :
2147 5819 : RestoreVolatileParameters(m_hTIFF);
2148 :
2149 5819 : m_nDirOffset = TIFFCurrentDirOffset(m_hTIFF);
2150 : }
2151 :
2152 : /************************************************************************/
2153 : /* FlushCache() */
2154 : /* */
2155 : /* We override this so we can also flush out local tiff strip */
2156 : /* cache if need be. */
2157 : /************************************************************************/
2158 :
2159 4631 : CPLErr GTiffDataset::FlushCache(bool bAtClosing)
2160 :
2161 : {
2162 4631 : return FlushCacheInternal(bAtClosing, true);
2163 : }
2164 :
2165 47056 : CPLErr GTiffDataset::FlushCacheInternal(bool bAtClosing, bool bFlushDirectory)
2166 : {
2167 47056 : if (m_bIsFinalized)
2168 3 : return CE_None;
2169 :
2170 47053 : CPLErr eErr = GDALPamDataset::FlushCache(bAtClosing);
2171 :
2172 47053 : if (m_bLoadedBlockDirty && m_nLoadedBlock != -1)
2173 : {
2174 288 : if (FlushBlockBuf() != CE_None)
2175 0 : eErr = CE_Failure;
2176 : }
2177 :
2178 47053 : CPLFree(m_pabyBlockBuf);
2179 47053 : m_pabyBlockBuf = nullptr;
2180 47053 : m_nLoadedBlock = -1;
2181 47053 : m_bLoadedBlockDirty = false;
2182 :
2183 : // Finish compression
2184 47053 : auto poQueue = m_poBaseDS ? m_poBaseDS->m_poCompressQueue.get()
2185 44668 : : m_poCompressQueue.get();
2186 47053 : if (poQueue)
2187 : {
2188 161 : poQueue->WaitCompletion();
2189 :
2190 : // Flush remaining data
2191 : // cppcheck-suppress constVariableReference
2192 :
2193 161 : auto &oQueue =
2194 161 : m_poBaseDS ? m_poBaseDS->m_asQueueJobIdx : m_asQueueJobIdx;
2195 230 : while (!oQueue.empty())
2196 : {
2197 69 : WaitCompletionForJobIdx(oQueue.front());
2198 : }
2199 : }
2200 :
2201 47053 : if (bFlushDirectory && GetAccess() == GA_Update)
2202 : {
2203 14144 : if (FlushDirectory() != CE_None)
2204 12 : eErr = CE_Failure;
2205 : }
2206 47053 : return eErr;
2207 : }
2208 :
2209 : /************************************************************************/
2210 : /* FlushDirectory() */
2211 : /************************************************************************/
2212 :
2213 22220 : CPLErr GTiffDataset::FlushDirectory()
2214 :
2215 : {
2216 22220 : CPLErr eErr = CE_None;
2217 :
2218 688 : const auto ReloadAllOtherDirectories = [this]()
2219 : {
2220 339 : const auto poBaseDS = m_poBaseDS ? m_poBaseDS : this;
2221 342 : for (auto &poOvrDS : poBaseDS->m_apoOverviewDS)
2222 : {
2223 3 : if (poOvrDS->m_bCrystalized && poOvrDS.get() != this)
2224 : {
2225 3 : poOvrDS->ReloadDirectory(true);
2226 : }
2227 :
2228 3 : if (poOvrDS->m_poMaskDS && poOvrDS->m_poMaskDS.get() != this &&
2229 0 : poOvrDS->m_poMaskDS->m_bCrystalized)
2230 : {
2231 0 : poOvrDS->m_poMaskDS->ReloadDirectory(true);
2232 : }
2233 : }
2234 339 : if (poBaseDS->m_poMaskDS && poBaseDS->m_poMaskDS.get() != this &&
2235 0 : poBaseDS->m_poMaskDS->m_bCrystalized)
2236 : {
2237 0 : poBaseDS->m_poMaskDS->ReloadDirectory(true);
2238 : }
2239 339 : if (poBaseDS->m_bCrystalized && poBaseDS != this)
2240 : {
2241 7 : poBaseDS->ReloadDirectory(true);
2242 : }
2243 339 : };
2244 :
2245 22220 : if (eAccess == GA_Update)
2246 : {
2247 15859 : if (m_bMetadataChanged)
2248 : {
2249 202 : m_bNeedsRewrite =
2250 202 : WriteMetadata(this, m_hTIFF, true, m_eProfile,
2251 202 : m_osFilename.c_str(), m_papszCreationOptions);
2252 202 : m_bMetadataChanged = false;
2253 :
2254 202 : if (m_bForceUnsetRPC)
2255 : {
2256 5 : double *padfRPCTag = nullptr;
2257 : uint16_t nCount;
2258 5 : if (TIFFGetField(m_hTIFF, TIFFTAG_RPCCOEFFICIENT, &nCount,
2259 5 : &padfRPCTag))
2260 : {
2261 3 : std::vector<double> zeroes(92);
2262 3 : TIFFSetField(m_hTIFF, TIFFTAG_RPCCOEFFICIENT, 92,
2263 : zeroes.data());
2264 3 : TIFFUnsetField(m_hTIFF, TIFFTAG_RPCCOEFFICIENT);
2265 3 : m_bNeedsRewrite = true;
2266 : }
2267 :
2268 5 : if (m_poBaseDS == nullptr)
2269 : {
2270 5 : GDALWriteRPCTXTFile(m_osFilename.c_str(), nullptr);
2271 5 : GDALWriteRPBFile(m_osFilename.c_str(), nullptr);
2272 : }
2273 : }
2274 : }
2275 :
2276 15859 : if (m_bGeoTIFFInfoChanged)
2277 : {
2278 147 : WriteGeoTIFFInfo();
2279 147 : m_bGeoTIFFInfoChanged = false;
2280 : }
2281 :
2282 15859 : if (m_bNoDataChanged)
2283 : {
2284 53 : if (m_bNoDataSet)
2285 : {
2286 37 : WriteNoDataValue(m_hTIFF, m_dfNoDataValue);
2287 : }
2288 16 : else if (m_bNoDataSetAsInt64)
2289 : {
2290 0 : WriteNoDataValue(m_hTIFF, m_nNoDataValueInt64);
2291 : }
2292 16 : else if (m_bNoDataSetAsUInt64)
2293 : {
2294 0 : WriteNoDataValue(m_hTIFF, m_nNoDataValueUInt64);
2295 : }
2296 : else
2297 : {
2298 16 : UnsetNoDataValue(m_hTIFF);
2299 : }
2300 53 : m_bNeedsRewrite = true;
2301 53 : m_bNoDataChanged = false;
2302 : }
2303 :
2304 15859 : if (m_bNeedsRewrite)
2305 : {
2306 365 : if (!m_bCrystalized)
2307 : {
2308 29 : Crystalize();
2309 : }
2310 : else
2311 : {
2312 336 : const TIFFSizeProc pfnSizeProc = TIFFGetSizeProc(m_hTIFF);
2313 :
2314 336 : m_nDirOffset = pfnSizeProc(TIFFClientdata(m_hTIFF));
2315 336 : if ((m_nDirOffset % 2) == 1)
2316 72 : ++m_nDirOffset;
2317 :
2318 336 : if (TIFFRewriteDirectory(m_hTIFF) == 0)
2319 0 : eErr = CE_Failure;
2320 :
2321 336 : TIFFSetSubDirectory(m_hTIFF, m_nDirOffset);
2322 :
2323 336 : ReloadAllOtherDirectories();
2324 :
2325 336 : if (m_bLayoutIFDSBeforeData && m_bBlockOrderRowMajor &&
2326 2 : m_bLeaderSizeAsUInt4 &&
2327 2 : m_bTrailerRepeatedLast4BytesRepeated &&
2328 2 : !m_bKnownIncompatibleEdition &&
2329 2 : !m_bWriteKnownIncompatibleEdition)
2330 : {
2331 2 : ReportError(CE_Warning, CPLE_AppDefined,
2332 : "The IFD has been rewritten at the end of "
2333 : "the file, which breaks COG layout.");
2334 2 : m_bKnownIncompatibleEdition = true;
2335 2 : m_bWriteKnownIncompatibleEdition = true;
2336 : }
2337 : }
2338 :
2339 365 : m_bNeedsRewrite = false;
2340 : }
2341 : }
2342 :
2343 : // There are some circumstances in which we can reach this point
2344 : // without having made this our directory (SetDirectory()) in which
2345 : // case we should not risk a flush.
2346 38079 : if (GetAccess() == GA_Update &&
2347 15859 : TIFFCurrentDirOffset(m_hTIFF) == m_nDirOffset)
2348 : {
2349 15859 : const TIFFSizeProc pfnSizeProc = TIFFGetSizeProc(m_hTIFF);
2350 :
2351 15859 : toff_t nNewDirOffset = pfnSizeProc(TIFFClientdata(m_hTIFF));
2352 15859 : if ((nNewDirOffset % 2) == 1)
2353 3544 : ++nNewDirOffset;
2354 :
2355 15859 : if (TIFFFlush(m_hTIFF) == 0)
2356 12 : eErr = CE_Failure;
2357 :
2358 15859 : if (m_nDirOffset != TIFFCurrentDirOffset(m_hTIFF))
2359 : {
2360 3 : m_nDirOffset = nNewDirOffset;
2361 3 : ReloadAllOtherDirectories();
2362 3 : CPLDebug("GTiff",
2363 : "directory moved during flush in FlushDirectory()");
2364 : }
2365 : }
2366 :
2367 22220 : SetDirectory();
2368 22220 : return eErr;
2369 : }
2370 :
2371 : /************************************************************************/
2372 : /* CleanOverviews() */
2373 : /************************************************************************/
2374 :
2375 5 : CPLErr GTiffDataset::CleanOverviews()
2376 :
2377 : {
2378 5 : CPLAssert(!m_poBaseDS);
2379 :
2380 5 : ScanDirectories();
2381 :
2382 5 : FlushDirectory();
2383 :
2384 : /* -------------------------------------------------------------------- */
2385 : /* Cleanup overviews objects, and get offsets to all overview */
2386 : /* directories. */
2387 : /* -------------------------------------------------------------------- */
2388 10 : std::vector<toff_t> anOvDirOffsets;
2389 :
2390 10 : for (auto &poOvrDS : m_apoOverviewDS)
2391 : {
2392 5 : anOvDirOffsets.push_back(poOvrDS->m_nDirOffset);
2393 5 : if (poOvrDS->m_poMaskDS)
2394 1 : anOvDirOffsets.push_back(poOvrDS->m_poMaskDS->m_nDirOffset);
2395 : }
2396 5 : m_apoOverviewDS.clear();
2397 :
2398 : /* -------------------------------------------------------------------- */
2399 : /* Loop through all the directories, translating the offsets */
2400 : /* into indexes we can use with TIFFUnlinkDirectory(). */
2401 : /* -------------------------------------------------------------------- */
2402 10 : std::vector<uint16_t> anOvDirIndexes;
2403 5 : int iThisOffset = 1;
2404 :
2405 5 : TIFFSetDirectory(m_hTIFF, 0);
2406 :
2407 : while (true)
2408 : {
2409 28 : for (toff_t nOffset : anOvDirOffsets)
2410 : {
2411 16 : if (nOffset == TIFFCurrentDirOffset(m_hTIFF))
2412 : {
2413 6 : anOvDirIndexes.push_back(static_cast<uint16_t>(iThisOffset));
2414 : }
2415 : }
2416 :
2417 12 : if (TIFFLastDirectory(m_hTIFF))
2418 5 : break;
2419 :
2420 7 : TIFFReadDirectory(m_hTIFF);
2421 7 : ++iThisOffset;
2422 7 : }
2423 :
2424 : /* -------------------------------------------------------------------- */
2425 : /* Actually unlink the target directories. Note that we do */
2426 : /* this from last to first so as to avoid renumbering any of */
2427 : /* the earlier directories we need to remove. */
2428 : /* -------------------------------------------------------------------- */
2429 11 : while (!anOvDirIndexes.empty())
2430 : {
2431 6 : TIFFUnlinkDirectory(m_hTIFF, anOvDirIndexes.back());
2432 6 : anOvDirIndexes.pop_back();
2433 : }
2434 :
2435 5 : if (m_poMaskDS)
2436 : {
2437 1 : m_poMaskDS->m_apoOverviewDS.clear();
2438 : }
2439 :
2440 5 : if (!SetDirectory())
2441 0 : return CE_Failure;
2442 :
2443 5 : return CE_None;
2444 : }
2445 :
2446 : /************************************************************************/
2447 : /* RegisterNewOverviewDataset() */
2448 : /************************************************************************/
2449 :
2450 516 : CPLErr GTiffDataset::RegisterNewOverviewDataset(toff_t nOverviewOffset,
2451 : int l_nJpegQuality,
2452 : CSLConstList papszOptions)
2453 : {
2454 : const auto GetOptionValue =
2455 5676 : [papszOptions](const char *pszOptionKey, const char *pszConfigOptionKey,
2456 11351 : const char **ppszKeyUsed = nullptr)
2457 : {
2458 5676 : const char *pszVal = CSLFetchNameValue(papszOptions, pszOptionKey);
2459 5676 : if (pszVal)
2460 : {
2461 1 : if (ppszKeyUsed)
2462 1 : *ppszKeyUsed = pszOptionKey;
2463 1 : return pszVal;
2464 : }
2465 5675 : pszVal = CSLFetchNameValue(papszOptions, pszConfigOptionKey);
2466 5675 : if (pszVal)
2467 : {
2468 0 : if (ppszKeyUsed)
2469 0 : *ppszKeyUsed = pszConfigOptionKey;
2470 0 : return pszVal;
2471 : }
2472 5675 : if (pszConfigOptionKey)
2473 : {
2474 5675 : pszVal = CPLGetConfigOption(pszConfigOptionKey, nullptr);
2475 5675 : if (pszVal && ppszKeyUsed)
2476 13 : *ppszKeyUsed = pszConfigOptionKey;
2477 : }
2478 5675 : return pszVal;
2479 516 : };
2480 :
2481 516 : int nZLevel = m_nZLevel;
2482 516 : if (const char *opt = GetOptionValue("ZLEVEL", "ZLEVEL_OVERVIEW"))
2483 : {
2484 4 : nZLevel = atoi(opt);
2485 : }
2486 :
2487 516 : int nZSTDLevel = m_nZSTDLevel;
2488 516 : if (const char *opt = GetOptionValue("ZSTD_LEVEL", "ZSTD_LEVEL_OVERVIEW"))
2489 : {
2490 4 : nZSTDLevel = atoi(opt);
2491 : }
2492 :
2493 516 : bool bWebpLossless = m_bWebPLossless;
2494 : const char *pszWebPLosslessOverview =
2495 516 : GetOptionValue("WEBP_LOSSLESS", "WEBP_LOSSLESS_OVERVIEW");
2496 516 : if (pszWebPLosslessOverview)
2497 : {
2498 2 : bWebpLossless = CPLTestBool(pszWebPLosslessOverview);
2499 : }
2500 :
2501 516 : int nWebpLevel = m_nWebPLevel;
2502 516 : const char *pszKeyWebpLevel = "";
2503 516 : if (const char *opt = GetOptionValue("WEBP_LEVEL", "WEBP_LEVEL_OVERVIEW",
2504 : &pszKeyWebpLevel))
2505 : {
2506 14 : if (pszWebPLosslessOverview == nullptr && m_bWebPLossless)
2507 : {
2508 1 : CPLDebug("GTiff",
2509 : "%s specified, but not WEBP_LOSSLESS_OVERVIEW. "
2510 : "Assuming WEBP_LOSSLESS_OVERVIEW=NO",
2511 : pszKeyWebpLevel);
2512 1 : bWebpLossless = false;
2513 : }
2514 13 : else if (bWebpLossless)
2515 : {
2516 0 : CPLError(CE_Warning, CPLE_AppDefined,
2517 : "%s is specified, but WEBP_LOSSLESS_OVERVIEW=YES. "
2518 : "%s will be ignored.",
2519 : pszKeyWebpLevel, pszKeyWebpLevel);
2520 : }
2521 14 : nWebpLevel = atoi(opt);
2522 : }
2523 :
2524 516 : double dfMaxZError = m_dfMaxZErrorOverview;
2525 516 : if (const char *opt = GetOptionValue("MAX_Z_ERROR", "MAX_Z_ERROR_OVERVIEW"))
2526 : {
2527 20 : dfMaxZError = CPLAtof(opt);
2528 : }
2529 :
2530 516 : signed char nJpegTablesMode = m_nJpegTablesMode;
2531 516 : if (const char *opt =
2532 516 : GetOptionValue("JPEG_TABLESMODE", "JPEG_TABLESMODE_OVERVIEW"))
2533 : {
2534 0 : nJpegTablesMode = static_cast<signed char>(atoi(opt));
2535 : }
2536 :
2537 : #ifdef HAVE_JXL
2538 516 : bool bJXLLossless = m_bJXLLossless;
2539 516 : if (const char *opt =
2540 516 : GetOptionValue("JXL_LOSSLESS", "JXL_LOSSLESS_OVERVIEW"))
2541 : {
2542 0 : bJXLLossless = CPLTestBool(opt);
2543 : }
2544 :
2545 516 : float fJXLDistance = m_fJXLDistance;
2546 516 : if (const char *opt =
2547 516 : GetOptionValue("JXL_DISTANCE", "JXL_DISTANCE_OVERVIEW"))
2548 : {
2549 0 : fJXLDistance = static_cast<float>(CPLAtof(opt));
2550 : }
2551 :
2552 516 : float fJXLAlphaDistance = m_fJXLAlphaDistance;
2553 516 : if (const char *opt =
2554 516 : GetOptionValue("JXL_ALPHA_DISTANCE", "JXL_ALPHA_DISTANCE_OVERVIEW"))
2555 : {
2556 0 : fJXLAlphaDistance = static_cast<float>(CPLAtof(opt));
2557 : }
2558 :
2559 516 : int nJXLEffort = m_nJXLEffort;
2560 516 : if (const char *opt = GetOptionValue("JXL_EFFORT", "JXL_EFFORT_OVERVIEW"))
2561 : {
2562 0 : nJXLEffort = atoi(opt);
2563 : }
2564 : #endif
2565 :
2566 1032 : auto poODS = std::make_shared<GTiffDataset>();
2567 516 : poODS->ShareLockWithParentDataset(this);
2568 516 : poODS->eAccess = GA_Update;
2569 516 : poODS->m_osFilename = m_osFilename;
2570 516 : const char *pszSparseOK = GetOptionValue("SPARSE_OK", "SPARSE_OK_OVERVIEW");
2571 516 : if (pszSparseOK && CPLTestBool(pszSparseOK))
2572 : {
2573 1 : poODS->m_bWriteEmptyTiles = false;
2574 1 : poODS->m_bFillEmptyTilesAtClosing = false;
2575 : }
2576 : else
2577 : {
2578 515 : poODS->m_bWriteEmptyTiles = m_bWriteEmptyTiles;
2579 515 : poODS->m_bFillEmptyTilesAtClosing = m_bFillEmptyTilesAtClosing;
2580 : }
2581 516 : poODS->m_nJpegQuality = static_cast<signed char>(l_nJpegQuality);
2582 516 : poODS->m_nWebPLevel = static_cast<signed char>(nWebpLevel);
2583 516 : poODS->m_nZLevel = static_cast<signed char>(nZLevel);
2584 516 : poODS->m_nLZMAPreset = m_nLZMAPreset;
2585 516 : poODS->m_nZSTDLevel = static_cast<signed char>(nZSTDLevel);
2586 516 : poODS->m_bWebPLossless = bWebpLossless;
2587 516 : poODS->m_nJpegTablesMode = nJpegTablesMode;
2588 516 : poODS->m_dfMaxZError = dfMaxZError;
2589 516 : poODS->m_dfMaxZErrorOverview = dfMaxZError;
2590 1032 : memcpy(poODS->m_anLercAddCompressionAndVersion,
2591 516 : m_anLercAddCompressionAndVersion,
2592 : sizeof(m_anLercAddCompressionAndVersion));
2593 : #ifdef HAVE_JXL
2594 516 : poODS->m_bJXLLossless = bJXLLossless;
2595 516 : poODS->m_fJXLDistance = fJXLDistance;
2596 516 : poODS->m_fJXLAlphaDistance = fJXLAlphaDistance;
2597 516 : poODS->m_nJXLEffort = nJXLEffort;
2598 : #endif
2599 :
2600 516 : if (poODS->OpenOffset(VSI_TIFFOpenChild(m_hTIFF), nOverviewOffset,
2601 516 : GA_Update) != CE_None)
2602 : {
2603 0 : return CE_Failure;
2604 : }
2605 :
2606 : // Assign color interpretation from main dataset
2607 516 : const int l_nBands = GetRasterCount();
2608 1537 : for (int i = 1; i <= l_nBands; i++)
2609 : {
2610 1021 : auto poBand = dynamic_cast<GTiffRasterBand *>(poODS->GetRasterBand(i));
2611 1021 : if (poBand)
2612 1021 : poBand->m_eBandInterp = GetRasterBand(i)->GetColorInterpretation();
2613 : }
2614 :
2615 : // Do that now that m_nCompression is set
2616 516 : poODS->RestoreVolatileParameters(poODS->m_hTIFF);
2617 :
2618 516 : poODS->m_poBaseDS = this;
2619 516 : poODS->m_bIsOverview = true;
2620 :
2621 516 : m_apoOverviewDS.push_back(std::move(poODS));
2622 516 : return CE_None;
2623 : }
2624 :
2625 : /************************************************************************/
2626 : /* CreateTIFFColorTable() */
2627 : /************************************************************************/
2628 :
2629 12 : static void CreateTIFFColorTable(
2630 : GDALColorTable *poColorTable, int nBits, int nColorTableMultiplier,
2631 : std::vector<unsigned short> &anTRed, std::vector<unsigned short> &anTGreen,
2632 : std::vector<unsigned short> &anTBlue, unsigned short *&panRed,
2633 : unsigned short *&panGreen, unsigned short *&panBlue)
2634 : {
2635 : int nColors;
2636 :
2637 12 : if (nBits == 8)
2638 12 : nColors = 256;
2639 0 : else if (nBits < 8)
2640 0 : nColors = 1 << nBits;
2641 : else
2642 0 : nColors = 65536;
2643 :
2644 12 : anTRed.resize(nColors, 0);
2645 12 : anTGreen.resize(nColors, 0);
2646 12 : anTBlue.resize(nColors, 0);
2647 :
2648 3084 : for (int iColor = 0; iColor < nColors; ++iColor)
2649 : {
2650 3072 : if (iColor < poColorTable->GetColorEntryCount())
2651 : {
2652 : GDALColorEntry sRGB;
2653 :
2654 3072 : poColorTable->GetColorEntryAsRGB(iColor, &sRGB);
2655 :
2656 3072 : anTRed[iColor] = GTiffDataset::ClampCTEntry(iColor, 1, sRGB.c1,
2657 : nColorTableMultiplier);
2658 3072 : anTGreen[iColor] = GTiffDataset::ClampCTEntry(
2659 3072 : iColor, 2, sRGB.c2, nColorTableMultiplier);
2660 3072 : anTBlue[iColor] = GTiffDataset::ClampCTEntry(iColor, 3, sRGB.c3,
2661 : nColorTableMultiplier);
2662 : }
2663 : else
2664 : {
2665 0 : anTRed[iColor] = 0;
2666 0 : anTGreen[iColor] = 0;
2667 0 : anTBlue[iColor] = 0;
2668 : }
2669 : }
2670 :
2671 12 : panRed = &(anTRed[0]);
2672 12 : panGreen = &(anTGreen[0]);
2673 12 : panBlue = &(anTBlue[0]);
2674 12 : }
2675 :
2676 : /************************************************************************/
2677 : /* GetOverviewParameters() */
2678 : /************************************************************************/
2679 :
2680 331 : bool GTiffDataset::GetOverviewParameters(
2681 : int &nCompression, uint16_t &nPlanarConfig, uint16_t &nPredictor,
2682 : uint16_t &nPhotometric, int &nOvrJpegQuality, std::string &osNoData,
2683 : uint16_t *&panExtraSampleValues, uint16_t &nExtraSamples,
2684 : CSLConstList papszOptions) const
2685 : {
2686 : const auto GetOptionValue =
2687 1098 : [papszOptions](const char *pszOptionKey, const char *pszConfigOptionKey,
2688 2188 : const char **ppszKeyUsed = nullptr)
2689 : {
2690 1098 : const char *pszVal = CSLFetchNameValue(papszOptions, pszOptionKey);
2691 1098 : if (pszVal)
2692 : {
2693 8 : if (ppszKeyUsed)
2694 8 : *ppszKeyUsed = pszOptionKey;
2695 8 : return pszVal;
2696 : }
2697 1090 : pszVal = CSLFetchNameValue(papszOptions, pszConfigOptionKey);
2698 1090 : if (pszVal)
2699 : {
2700 0 : if (ppszKeyUsed)
2701 0 : *ppszKeyUsed = pszConfigOptionKey;
2702 0 : return pszVal;
2703 : }
2704 1090 : pszVal = CPLGetConfigOption(pszConfigOptionKey, nullptr);
2705 1090 : if (pszVal && ppszKeyUsed)
2706 60 : *ppszKeyUsed = pszConfigOptionKey;
2707 1090 : return pszVal;
2708 331 : };
2709 :
2710 : /* -------------------------------------------------------------------- */
2711 : /* Determine compression method. */
2712 : /* -------------------------------------------------------------------- */
2713 331 : nCompression = m_nCompression;
2714 331 : const char *pszOptionKey = "";
2715 : const char *pszCompressValue =
2716 331 : GetOptionValue("COMPRESS", "COMPRESS_OVERVIEW", &pszOptionKey);
2717 331 : if (pszCompressValue != nullptr)
2718 : {
2719 58 : nCompression =
2720 58 : GTIFFGetCompressionMethod(pszCompressValue, pszOptionKey);
2721 58 : if (nCompression < 0)
2722 : {
2723 0 : nCompression = m_nCompression;
2724 : }
2725 : }
2726 :
2727 : /* -------------------------------------------------------------------- */
2728 : /* Determine planar configuration. */
2729 : /* -------------------------------------------------------------------- */
2730 331 : nPlanarConfig = m_nPlanarConfig;
2731 331 : if (nCompression == COMPRESSION_WEBP)
2732 : {
2733 11 : nPlanarConfig = PLANARCONFIG_CONTIG;
2734 : }
2735 : const char *pszInterleave =
2736 331 : GetOptionValue(GDALMD_INTERLEAVE, "INTERLEAVE_OVERVIEW", &pszOptionKey);
2737 331 : if (pszInterleave != nullptr && pszInterleave[0] != '\0')
2738 : {
2739 2 : if (EQUAL(pszInterleave, "PIXEL"))
2740 1 : nPlanarConfig = PLANARCONFIG_CONTIG;
2741 1 : else if (EQUAL(pszInterleave, "BAND"))
2742 1 : nPlanarConfig = PLANARCONFIG_SEPARATE;
2743 : else
2744 : {
2745 0 : CPLError(CE_Warning, CPLE_AppDefined,
2746 : "%s=%s unsupported, "
2747 : "value must be PIXEL or BAND. ignoring",
2748 : pszOptionKey, pszInterleave);
2749 : }
2750 : }
2751 :
2752 : /* -------------------------------------------------------------------- */
2753 : /* Determine predictor tag */
2754 : /* -------------------------------------------------------------------- */
2755 331 : nPredictor = PREDICTOR_NONE;
2756 331 : if (GTIFFSupportsPredictor(nCompression))
2757 : {
2758 : const char *pszPredictor =
2759 78 : GetOptionValue("PREDICTOR", "PREDICTOR_OVERVIEW");
2760 78 : if (pszPredictor != nullptr)
2761 : {
2762 1 : nPredictor = static_cast<uint16_t>(atoi(pszPredictor));
2763 : }
2764 77 : else if (GTIFFSupportsPredictor(m_nCompression))
2765 76 : TIFFGetField(m_hTIFF, TIFFTAG_PREDICTOR, &nPredictor);
2766 : }
2767 :
2768 : /* -------------------------------------------------------------------- */
2769 : /* Determine photometric tag */
2770 : /* -------------------------------------------------------------------- */
2771 331 : if (m_nPhotometric == PHOTOMETRIC_YCBCR && nCompression != COMPRESSION_JPEG)
2772 1 : nPhotometric = PHOTOMETRIC_RGB;
2773 : else
2774 330 : nPhotometric = m_nPhotometric;
2775 : const char *pszPhotometric =
2776 331 : GetOptionValue("PHOTOMETRIC", "PHOTOMETRIC_OVERVIEW", &pszOptionKey);
2777 331 : if (!GTIFFUpdatePhotometric(pszPhotometric, pszOptionKey, nCompression,
2778 331 : pszInterleave, nBands, nPhotometric,
2779 : nPlanarConfig))
2780 : {
2781 0 : return false;
2782 : }
2783 :
2784 : /* -------------------------------------------------------------------- */
2785 : /* Determine JPEG quality */
2786 : /* -------------------------------------------------------------------- */
2787 331 : nOvrJpegQuality = m_nJpegQuality;
2788 331 : if (nCompression == COMPRESSION_JPEG)
2789 : {
2790 : const char *pszJPEGQuality =
2791 27 : GetOptionValue("JPEG_QUALITY", "JPEG_QUALITY_OVERVIEW");
2792 27 : if (pszJPEGQuality != nullptr)
2793 : {
2794 9 : nOvrJpegQuality = atoi(pszJPEGQuality);
2795 : }
2796 : }
2797 :
2798 : /* -------------------------------------------------------------------- */
2799 : /* Set nodata. */
2800 : /* -------------------------------------------------------------------- */
2801 331 : if (m_bNoDataSet)
2802 : {
2803 17 : osNoData = GTiffFormatGDALNoDataTagValue(m_dfNoDataValue);
2804 : }
2805 :
2806 : /* -------------------------------------------------------------------- */
2807 : /* Fetch extra sample tag */
2808 : /* -------------------------------------------------------------------- */
2809 331 : panExtraSampleValues = nullptr;
2810 331 : nExtraSamples = 0;
2811 331 : if (TIFFGetField(m_hTIFF, TIFFTAG_EXTRASAMPLES, &nExtraSamples,
2812 331 : &panExtraSampleValues))
2813 : {
2814 : uint16_t *panExtraSampleValuesNew = static_cast<uint16_t *>(
2815 41 : CPLMalloc(nExtraSamples * sizeof(uint16_t)));
2816 41 : memcpy(panExtraSampleValuesNew, panExtraSampleValues,
2817 41 : nExtraSamples * sizeof(uint16_t));
2818 41 : panExtraSampleValues = panExtraSampleValuesNew;
2819 : }
2820 : else
2821 : {
2822 290 : panExtraSampleValues = nullptr;
2823 290 : nExtraSamples = 0;
2824 : }
2825 :
2826 331 : return true;
2827 : }
2828 :
2829 : /************************************************************************/
2830 : /* CreateOverviewsFromSrcOverviews() */
2831 : /************************************************************************/
2832 :
2833 : // If poOvrDS is not null, it is used and poSrcDS is ignored.
2834 :
2835 71 : CPLErr GTiffDataset::CreateOverviewsFromSrcOverviews(GDALDataset *poSrcDS,
2836 : GDALDataset *poOvrDS,
2837 : int nOverviews)
2838 : {
2839 71 : CPLAssert(poSrcDS->GetRasterCount() != 0);
2840 71 : CPLAssert(m_apoOverviewDS.empty());
2841 :
2842 71 : ScanDirectories();
2843 :
2844 71 : FlushDirectory();
2845 :
2846 71 : int nOvBitsPerSample = m_nBitsPerSample;
2847 :
2848 : /* -------------------------------------------------------------------- */
2849 : /* Do we need some metadata for the overviews? */
2850 : /* -------------------------------------------------------------------- */
2851 142 : CPLString osMetadata;
2852 :
2853 71 : GTIFFBuildOverviewMetadata("NONE", this, false, osMetadata);
2854 :
2855 : int nCompression;
2856 : uint16_t nPlanarConfig;
2857 : uint16_t nPredictor;
2858 : uint16_t nPhotometric;
2859 : int nOvrJpegQuality;
2860 142 : std::string osNoData;
2861 71 : uint16_t *panExtraSampleValues = nullptr;
2862 71 : uint16_t nExtraSamples = 0;
2863 71 : if (!GetOverviewParameters(nCompression, nPlanarConfig, nPredictor,
2864 : nPhotometric, nOvrJpegQuality, osNoData,
2865 : panExtraSampleValues, nExtraSamples,
2866 : /*papszOptions=*/nullptr))
2867 : {
2868 0 : return CE_Failure;
2869 : }
2870 :
2871 : /* -------------------------------------------------------------------- */
2872 : /* Do we have a palette? If so, create a TIFF compatible version. */
2873 : /* -------------------------------------------------------------------- */
2874 142 : std::vector<unsigned short> anTRed;
2875 142 : std::vector<unsigned short> anTGreen;
2876 71 : std::vector<unsigned short> anTBlue;
2877 71 : unsigned short *panRed = nullptr;
2878 71 : unsigned short *panGreen = nullptr;
2879 71 : unsigned short *panBlue = nullptr;
2880 :
2881 71 : if (nPhotometric == PHOTOMETRIC_PALETTE && m_poColorTable != nullptr)
2882 : {
2883 0 : if (m_nColorTableMultiplier == 0)
2884 0 : m_nColorTableMultiplier = DEFAULT_COLOR_TABLE_MULTIPLIER_257;
2885 :
2886 0 : CreateTIFFColorTable(m_poColorTable.get(), nOvBitsPerSample,
2887 : m_nColorTableMultiplier, anTRed, anTGreen, anTBlue,
2888 : panRed, panGreen, panBlue);
2889 : }
2890 :
2891 71 : int nOvrBlockXSize = 0;
2892 71 : int nOvrBlockYSize = 0;
2893 71 : GTIFFGetOverviewBlockSize(GDALRasterBand::ToHandle(GetRasterBand(1)),
2894 : &nOvrBlockXSize, &nOvrBlockYSize, nullptr,
2895 : nullptr);
2896 :
2897 71 : CPLErr eErr = CE_None;
2898 :
2899 202 : for (int i = 0; i < nOverviews && eErr == CE_None; ++i)
2900 : {
2901 : GDALRasterBand *poOvrBand =
2902 171 : poOvrDS ? ((i == 0) ? poOvrDS->GetRasterBand(1)
2903 40 : : poOvrDS->GetRasterBand(1)->GetOverview(i - 1))
2904 55 : : poSrcDS->GetRasterBand(1)->GetOverview(i);
2905 :
2906 131 : int nOXSize = poOvrBand->GetXSize();
2907 131 : int nOYSize = poOvrBand->GetYSize();
2908 :
2909 262 : toff_t nOverviewOffset = GTIFFWriteDirectory(
2910 : m_hTIFF, FILETYPE_REDUCEDIMAGE, nOXSize, nOYSize, nOvBitsPerSample,
2911 131 : nPlanarConfig, m_nSamplesPerPixel, nOvrBlockXSize, nOvrBlockYSize,
2912 131 : TRUE, nCompression, nPhotometric, m_nSampleFormat, nPredictor,
2913 : panRed, panGreen, panBlue, nExtraSamples, panExtraSampleValues,
2914 : osMetadata,
2915 131 : nOvrJpegQuality >= 0 ? CPLSPrintf("%d", nOvrJpegQuality) : nullptr,
2916 131 : CPLSPrintf("%d", m_nJpegTablesMode),
2917 2 : osNoData.empty() ? nullptr : osNoData.c_str(),
2918 131 : m_anLercAddCompressionAndVersion, m_bWriteCOGLayout);
2919 :
2920 131 : if (nOverviewOffset == 0)
2921 0 : eErr = CE_Failure;
2922 : else
2923 131 : eErr = RegisterNewOverviewDataset(nOverviewOffset, nOvrJpegQuality,
2924 : nullptr);
2925 : }
2926 :
2927 : // For directory reloading, so that the chaining to the next directory is
2928 : // reloaded, as well as compression parameters.
2929 71 : ReloadDirectory();
2930 :
2931 71 : CPLFree(panExtraSampleValues);
2932 71 : panExtraSampleValues = nullptr;
2933 :
2934 71 : return eErr;
2935 : }
2936 :
2937 : /************************************************************************/
2938 : /* CreateInternalMaskOverviews() */
2939 : /************************************************************************/
2940 :
2941 274 : CPLErr GTiffDataset::CreateInternalMaskOverviews(int nOvrBlockXSize,
2942 : int nOvrBlockYSize)
2943 : {
2944 274 : ScanDirectories();
2945 :
2946 : /* -------------------------------------------------------------------- */
2947 : /* Create overviews for the mask. */
2948 : /* -------------------------------------------------------------------- */
2949 274 : CPLErr eErr = CE_None;
2950 :
2951 274 : if (m_poMaskDS != nullptr && m_poMaskDS->GetRasterCount() == 1)
2952 : {
2953 : int nMaskOvrCompression;
2954 42 : if (strstr(GDALGetMetadataItem(GDALGetDriverByName("GTiff"),
2955 : GDAL_DMD_CREATIONOPTIONLIST, nullptr),
2956 42 : "<Value>DEFLATE</Value>") != nullptr)
2957 42 : nMaskOvrCompression = COMPRESSION_ADOBE_DEFLATE;
2958 : else
2959 0 : nMaskOvrCompression = COMPRESSION_PACKBITS;
2960 :
2961 113 : for (auto &poOvrDS : m_apoOverviewDS)
2962 : {
2963 71 : if (poOvrDS->m_poMaskDS == nullptr)
2964 : {
2965 59 : const toff_t nOverviewOffset = GTIFFWriteDirectory(
2966 : m_hTIFF, FILETYPE_REDUCEDIMAGE | FILETYPE_MASK,
2967 59 : poOvrDS->nRasterXSize, poOvrDS->nRasterYSize, 1,
2968 : PLANARCONFIG_CONTIG, 1, nOvrBlockXSize, nOvrBlockYSize,
2969 : TRUE, nMaskOvrCompression, PHOTOMETRIC_MASK,
2970 : SAMPLEFORMAT_UINT, PREDICTOR_NONE, nullptr, nullptr,
2971 : nullptr, 0, nullptr, "", nullptr, nullptr, nullptr, nullptr,
2972 59 : m_bWriteCOGLayout);
2973 :
2974 59 : if (nOverviewOffset == 0)
2975 : {
2976 0 : eErr = CE_Failure;
2977 0 : continue;
2978 : }
2979 :
2980 118 : auto poMaskODS = std::make_shared<GTiffDataset>();
2981 59 : poMaskODS->eAccess = GA_Update;
2982 59 : poMaskODS->ShareLockWithParentDataset(this);
2983 59 : poMaskODS->m_osFilename = m_osFilename;
2984 59 : if (poMaskODS->OpenOffset(VSI_TIFFOpenChild(m_hTIFF),
2985 : nOverviewOffset,
2986 59 : GA_Update) != CE_None)
2987 : {
2988 0 : eErr = CE_Failure;
2989 : }
2990 : else
2991 : {
2992 118 : poMaskODS->m_bPromoteTo8Bits =
2993 59 : CPLTestBool(CPLGetConfigOption(
2994 : "GDAL_TIFF_INTERNAL_MASK_TO_8BIT", "YES"));
2995 59 : poMaskODS->m_poBaseDS = this;
2996 59 : poMaskODS->m_poImageryDS = poOvrDS.get();
2997 59 : poOvrDS->m_poMaskDS = poMaskODS;
2998 59 : m_poMaskDS->m_apoOverviewDS.push_back(std::move(poMaskODS));
2999 : }
3000 : }
3001 : }
3002 : }
3003 :
3004 274 : ReloadDirectory();
3005 :
3006 274 : return eErr;
3007 : }
3008 :
3009 : /************************************************************************/
3010 : /* AddOverviews() */
3011 : /************************************************************************/
3012 :
3013 : CPLErr
3014 13 : GTiffDataset::AddOverviews(const std::vector<GDALDataset *> &apoSrcOvrDSIn,
3015 : GDALProgressFunc pfnProgress, void *pProgressData,
3016 : CSLConstList papszOptions)
3017 : {
3018 : /* -------------------------------------------------------------------- */
3019 : /* If we don't have read access, then create the overviews */
3020 : /* externally. */
3021 : /* -------------------------------------------------------------------- */
3022 13 : if (GetAccess() != GA_Update)
3023 : {
3024 4 : CPLDebug("GTiff", "File open for read-only accessing, "
3025 : "creating overviews externally.");
3026 :
3027 4 : CPLErr eErr = GDALDataset::AddOverviews(apoSrcOvrDSIn, pfnProgress,
3028 : pProgressData, papszOptions);
3029 4 : if (eErr == CE_None && m_poMaskDS)
3030 : {
3031 0 : ReportError(
3032 : CE_Warning, CPLE_NotSupported,
3033 : "Building external overviews whereas there is an internal "
3034 : "mask is not fully supported. "
3035 : "The overviews of the non-mask bands will be created, "
3036 : "but not the overviews of the mask band.");
3037 : }
3038 4 : return eErr;
3039 : }
3040 :
3041 18 : std::vector<GDALDataset *> apoSrcOvrDS = apoSrcOvrDSIn;
3042 : // Sort overviews by descending size
3043 9 : std::sort(apoSrcOvrDS.begin(), apoSrcOvrDS.end(),
3044 0 : [](const GDALDataset *poDS1, const GDALDataset *poDS2)
3045 0 : { return poDS1->GetRasterXSize() > poDS2->GetRasterXSize(); });
3046 :
3047 9 : if (!GDALDefaultOverviews::CheckSrcOverviewsConsistencyWithBase(
3048 : this, apoSrcOvrDS))
3049 5 : return CE_Failure;
3050 :
3051 4 : ScanDirectories();
3052 :
3053 : // Make implicit JPEG overviews invisible, but do not destroy
3054 : // them in case they are already used (not sure that the client
3055 : // has the right to do that). Behavior maybe undefined in GDAL API.
3056 4 : std::swap(m_apoJPEGOverviewDSOld, m_apoJPEGOverviewDS);
3057 4 : m_apoJPEGOverviewDS.clear();
3058 :
3059 4 : FlushDirectory();
3060 :
3061 : /* -------------------------------------------------------------------- */
3062 : /* If we are averaging bit data to grayscale we need to create */
3063 : /* 8bit overviews. */
3064 : /* -------------------------------------------------------------------- */
3065 4 : int nOvBitsPerSample = m_nBitsPerSample;
3066 :
3067 : /* -------------------------------------------------------------------- */
3068 : /* Do we need some metadata for the overviews? */
3069 : /* -------------------------------------------------------------------- */
3070 8 : CPLString osMetadata;
3071 :
3072 4 : const bool bIsForMaskBand = nBands == 1 && GetRasterBand(1)->IsMaskBand();
3073 4 : GTIFFBuildOverviewMetadata(/* resampling = */ "", this, bIsForMaskBand,
3074 : osMetadata);
3075 :
3076 : int nCompression;
3077 : uint16_t nPlanarConfig;
3078 : uint16_t nPredictor;
3079 : uint16_t nPhotometric;
3080 : int nOvrJpegQuality;
3081 8 : std::string osNoData;
3082 4 : uint16_t *panExtraSampleValues = nullptr;
3083 4 : uint16_t nExtraSamples = 0;
3084 4 : if (!GetOverviewParameters(nCompression, nPlanarConfig, nPredictor,
3085 : nPhotometric, nOvrJpegQuality, osNoData,
3086 : panExtraSampleValues, nExtraSamples,
3087 : papszOptions))
3088 : {
3089 0 : return CE_Failure;
3090 : }
3091 :
3092 : /* -------------------------------------------------------------------- */
3093 : /* Do we have a palette? If so, create a TIFF compatible version. */
3094 : /* -------------------------------------------------------------------- */
3095 8 : std::vector<unsigned short> anTRed;
3096 8 : std::vector<unsigned short> anTGreen;
3097 4 : std::vector<unsigned short> anTBlue;
3098 4 : unsigned short *panRed = nullptr;
3099 4 : unsigned short *panGreen = nullptr;
3100 4 : unsigned short *panBlue = nullptr;
3101 :
3102 4 : if (nPhotometric == PHOTOMETRIC_PALETTE && m_poColorTable != nullptr)
3103 : {
3104 0 : if (m_nColorTableMultiplier == 0)
3105 0 : m_nColorTableMultiplier = DEFAULT_COLOR_TABLE_MULTIPLIER_257;
3106 :
3107 0 : CreateTIFFColorTable(m_poColorTable.get(), nOvBitsPerSample,
3108 : m_nColorTableMultiplier, anTRed, anTGreen, anTBlue,
3109 : panRed, panGreen, panBlue);
3110 : }
3111 :
3112 : /* -------------------------------------------------------------------- */
3113 : /* Establish which of the overview levels we already have, and */
3114 : /* which are new. We assume that band 1 of the file is */
3115 : /* representative. */
3116 : /* -------------------------------------------------------------------- */
3117 4 : int nOvrBlockXSize = 0;
3118 4 : int nOvrBlockYSize = 0;
3119 4 : GTIFFGetOverviewBlockSize(GDALRasterBand::ToHandle(GetRasterBand(1)),
3120 : &nOvrBlockXSize, &nOvrBlockYSize, papszOptions,
3121 : "BLOCKSIZE");
3122 :
3123 4 : CPLErr eErr = CE_None;
3124 8 : for (const auto *poSrcOvrDS : apoSrcOvrDS)
3125 : {
3126 4 : bool bFound = false;
3127 4 : for (auto &poOvrDS : m_apoOverviewDS)
3128 : {
3129 4 : if (poOvrDS->GetRasterXSize() == poSrcOvrDS->GetRasterXSize() &&
3130 2 : poOvrDS->GetRasterYSize() == poSrcOvrDS->GetRasterYSize())
3131 : {
3132 2 : bFound = true;
3133 2 : break;
3134 : }
3135 : }
3136 4 : if (!bFound && eErr == CE_None)
3137 : {
3138 2 : if (m_bLayoutIFDSBeforeData && !m_bKnownIncompatibleEdition &&
3139 0 : !m_bWriteKnownIncompatibleEdition)
3140 : {
3141 0 : ReportError(CE_Warning, CPLE_AppDefined,
3142 : "Adding new overviews invalidates the "
3143 : "LAYOUT=IFDS_BEFORE_DATA property");
3144 0 : m_bKnownIncompatibleEdition = true;
3145 0 : m_bWriteKnownIncompatibleEdition = true;
3146 : }
3147 :
3148 6 : const toff_t nOverviewOffset = GTIFFWriteDirectory(
3149 : m_hTIFF, FILETYPE_REDUCEDIMAGE, poSrcOvrDS->GetRasterXSize(),
3150 : poSrcOvrDS->GetRasterYSize(), nOvBitsPerSample, nPlanarConfig,
3151 2 : m_nSamplesPerPixel, nOvrBlockXSize, nOvrBlockYSize, TRUE,
3152 2 : nCompression, nPhotometric, m_nSampleFormat, nPredictor, panRed,
3153 : panGreen, panBlue, nExtraSamples, panExtraSampleValues,
3154 : osMetadata,
3155 2 : nOvrJpegQuality >= 0 ? CPLSPrintf("%d", nOvrJpegQuality)
3156 : : nullptr,
3157 2 : CPLSPrintf("%d", m_nJpegTablesMode),
3158 0 : osNoData.empty() ? nullptr : osNoData.c_str(),
3159 2 : m_anLercAddCompressionAndVersion, false);
3160 :
3161 2 : if (nOverviewOffset == 0)
3162 0 : eErr = CE_Failure;
3163 : else
3164 2 : eErr = RegisterNewOverviewDataset(
3165 : nOverviewOffset, nOvrJpegQuality, papszOptions);
3166 : }
3167 : }
3168 :
3169 4 : CPLFree(panExtraSampleValues);
3170 4 : panExtraSampleValues = nullptr;
3171 :
3172 4 : ReloadDirectory();
3173 :
3174 4 : if (!pfnProgress)
3175 2 : pfnProgress = GDALDummyProgress;
3176 :
3177 : // almost 0, but not 0 to please Coverity Scan
3178 4 : double dfTotalPixels = std::numeric_limits<double>::min();
3179 8 : for (const auto *poSrcOvrDS : apoSrcOvrDS)
3180 : {
3181 4 : dfTotalPixels += static_cast<double>(poSrcOvrDS->GetRasterXSize()) *
3182 4 : poSrcOvrDS->GetRasterYSize();
3183 : }
3184 :
3185 : // Copy source datasets into target overview datasets
3186 4 : double dfCurPixels = 0;
3187 8 : for (auto *poSrcOvrDS : apoSrcOvrDS)
3188 : {
3189 4 : GDALDataset *poDstOvrDS = nullptr;
3190 4 : for (auto &poOvrDS : m_apoOverviewDS)
3191 : {
3192 8 : if (poOvrDS->GetRasterXSize() == poSrcOvrDS->GetRasterXSize() &&
3193 4 : poOvrDS->GetRasterYSize() == poSrcOvrDS->GetRasterYSize())
3194 : {
3195 4 : poDstOvrDS = poOvrDS.get();
3196 4 : break;
3197 : }
3198 : }
3199 4 : if (eErr == CE_None && poDstOvrDS)
3200 : {
3201 : const double dfThisPixels =
3202 4 : static_cast<double>(poSrcOvrDS->GetRasterXSize()) *
3203 4 : poSrcOvrDS->GetRasterYSize();
3204 8 : void *pScaledProgressData = GDALCreateScaledProgress(
3205 : dfCurPixels / dfTotalPixels,
3206 4 : (dfCurPixels + dfThisPixels) / dfTotalPixels, pfnProgress,
3207 : pProgressData);
3208 4 : dfCurPixels += dfThisPixels;
3209 4 : eErr = GDALDatasetCopyWholeRaster(GDALDataset::ToHandle(poSrcOvrDS),
3210 : GDALDataset::ToHandle(poDstOvrDS),
3211 : nullptr, GDALScaledProgress,
3212 : pScaledProgressData);
3213 4 : GDALDestroyScaledProgress(pScaledProgressData);
3214 : }
3215 : }
3216 :
3217 4 : return eErr;
3218 : }
3219 :
3220 : /************************************************************************/
3221 : /* IBuildOverviews() */
3222 : /************************************************************************/
3223 :
3224 412 : CPLErr GTiffDataset::IBuildOverviews(const char *pszResampling, int nOverviews,
3225 : const int *panOverviewList, int nBandsIn,
3226 : const int *panBandList,
3227 : GDALProgressFunc pfnProgress,
3228 : void *pProgressData,
3229 : CSLConstList papszOptions)
3230 :
3231 : {
3232 412 : ScanDirectories();
3233 :
3234 : // Make implicit JPEG overviews invisible, but do not destroy
3235 : // them in case they are already used (not sure that the client
3236 : // has the right to do that. Behavior maybe undefined in GDAL API.
3237 412 : std::swap(m_apoJPEGOverviewDSOld, m_apoJPEGOverviewDS);
3238 412 : m_apoJPEGOverviewDS.clear();
3239 :
3240 : /* -------------------------------------------------------------------- */
3241 : /* If RRD or external OVR overviews requested, then invoke */
3242 : /* generic handling. */
3243 : /* -------------------------------------------------------------------- */
3244 412 : bool bUseGenericHandling = false;
3245 412 : bool bUseRRD = false;
3246 824 : CPLStringList aosOptions(papszOptions);
3247 :
3248 412 : const char *pszLocation = CSLFetchNameValue(papszOptions, "LOCATION");
3249 412 : if (pszLocation && EQUAL(pszLocation, "EXTERNAL"))
3250 : {
3251 1 : bUseGenericHandling = true;
3252 : }
3253 411 : else if (pszLocation && EQUAL(pszLocation, "INTERNAL"))
3254 : {
3255 0 : if (GetAccess() != GA_Update)
3256 : {
3257 0 : CPLError(CE_Failure, CPLE_AppDefined,
3258 : "Cannot create internal overviews on file opened in "
3259 : "read-only mode");
3260 0 : return CE_Failure;
3261 : }
3262 : }
3263 411 : else if (pszLocation && EQUAL(pszLocation, "RRD"))
3264 : {
3265 3 : bUseGenericHandling = true;
3266 3 : bUseRRD = true;
3267 3 : aosOptions.SetNameValue("USE_RRD", "YES");
3268 : }
3269 : // Legacy
3270 408 : else if ((bUseRRD = CPLTestBool(
3271 : CSLFetchNameValueDef(papszOptions, "USE_RRD",
3272 816 : CPLGetConfigOption("USE_RRD", "NO")))) ||
3273 408 : CPLTestBool(CSLFetchNameValueDef(
3274 : papszOptions, "TIFF_USE_OVR",
3275 : CPLGetConfigOption("TIFF_USE_OVR", "NO"))))
3276 : {
3277 0 : bUseGenericHandling = true;
3278 : }
3279 :
3280 : /* -------------------------------------------------------------------- */
3281 : /* If we don't have read access, then create the overviews */
3282 : /* externally. */
3283 : /* -------------------------------------------------------------------- */
3284 412 : if (GetAccess() != GA_Update)
3285 : {
3286 145 : CPLDebug("GTiff", "File open for read-only accessing, "
3287 : "creating overviews externally.");
3288 :
3289 145 : bUseGenericHandling = true;
3290 : }
3291 :
3292 412 : if (bUseGenericHandling)
3293 : {
3294 148 : if (!m_apoOverviewDS.empty())
3295 : {
3296 0 : ReportError(CE_Failure, CPLE_NotSupported,
3297 : "Cannot add external overviews when there are already "
3298 : "internal overviews");
3299 0 : return CE_Failure;
3300 : }
3301 :
3302 148 : if (!m_bWriteEmptyTiles && !bUseRRD)
3303 : {
3304 1 : aosOptions.SetNameValue("SPARSE_OK", "YES");
3305 : }
3306 :
3307 148 : return GDALDataset::IBuildOverviews(
3308 : pszResampling, nOverviews, panOverviewList, nBandsIn, panBandList,
3309 148 : pfnProgress, pProgressData, aosOptions);
3310 : }
3311 :
3312 : /* -------------------------------------------------------------------- */
3313 : /* Our TIFF overview support currently only works safely if all */
3314 : /* bands are handled at the same time. */
3315 : /* -------------------------------------------------------------------- */
3316 264 : if (nBandsIn != GetRasterCount())
3317 : {
3318 0 : ReportError(CE_Failure, CPLE_NotSupported,
3319 : "Generation of overviews in TIFF currently only "
3320 : "supported when operating on all bands. "
3321 : "Operation failed.");
3322 0 : return CE_Failure;
3323 : }
3324 :
3325 : /* -------------------------------------------------------------------- */
3326 : /* If zero overviews were requested, we need to clear all */
3327 : /* existing overviews. */
3328 : /* -------------------------------------------------------------------- */
3329 264 : if (nOverviews == 0)
3330 : {
3331 8 : if (m_apoOverviewDS.empty())
3332 3 : return GDALDataset::IBuildOverviews(
3333 : pszResampling, nOverviews, panOverviewList, nBandsIn,
3334 3 : panBandList, pfnProgress, pProgressData, papszOptions);
3335 :
3336 5 : return CleanOverviews();
3337 : }
3338 :
3339 256 : CPLErr eErr = CE_None;
3340 :
3341 : /* -------------------------------------------------------------------- */
3342 : /* Initialize progress counter. */
3343 : /* -------------------------------------------------------------------- */
3344 256 : if (!pfnProgress(0.0, nullptr, pProgressData))
3345 : {
3346 0 : ReportError(CE_Failure, CPLE_UserInterrupt, "User terminated");
3347 0 : return CE_Failure;
3348 : }
3349 :
3350 256 : FlushDirectory();
3351 :
3352 : /* -------------------------------------------------------------------- */
3353 : /* If we are averaging bit data to grayscale we need to create */
3354 : /* 8bit overviews. */
3355 : /* -------------------------------------------------------------------- */
3356 256 : int nOvBitsPerSample = m_nBitsPerSample;
3357 :
3358 256 : if (STARTS_WITH_CI(pszResampling, "AVERAGE_BIT2"))
3359 2 : nOvBitsPerSample = 8;
3360 :
3361 : /* -------------------------------------------------------------------- */
3362 : /* Do we need some metadata for the overviews? */
3363 : /* -------------------------------------------------------------------- */
3364 512 : CPLString osMetadata;
3365 :
3366 256 : const bool bIsForMaskBand = nBands == 1 && GetRasterBand(1)->IsMaskBand();
3367 256 : GTIFFBuildOverviewMetadata(pszResampling, this, bIsForMaskBand, osMetadata);
3368 :
3369 : int nCompression;
3370 : uint16_t nPlanarConfig;
3371 : uint16_t nPredictor;
3372 : uint16_t nPhotometric;
3373 : int nOvrJpegQuality;
3374 512 : std::string osNoData;
3375 256 : uint16_t *panExtraSampleValues = nullptr;
3376 256 : uint16_t nExtraSamples = 0;
3377 256 : if (!GetOverviewParameters(nCompression, nPlanarConfig, nPredictor,
3378 : nPhotometric, nOvrJpegQuality, osNoData,
3379 : panExtraSampleValues, nExtraSamples,
3380 : papszOptions))
3381 : {
3382 0 : return CE_Failure;
3383 : }
3384 :
3385 : /* -------------------------------------------------------------------- */
3386 : /* Do we have a palette? If so, create a TIFF compatible version. */
3387 : /* -------------------------------------------------------------------- */
3388 512 : std::vector<unsigned short> anTRed;
3389 512 : std::vector<unsigned short> anTGreen;
3390 512 : std::vector<unsigned short> anTBlue;
3391 256 : unsigned short *panRed = nullptr;
3392 256 : unsigned short *panGreen = nullptr;
3393 256 : unsigned short *panBlue = nullptr;
3394 :
3395 256 : if (nPhotometric == PHOTOMETRIC_PALETTE && m_poColorTable != nullptr)
3396 : {
3397 12 : if (m_nColorTableMultiplier == 0)
3398 0 : m_nColorTableMultiplier = DEFAULT_COLOR_TABLE_MULTIPLIER_257;
3399 :
3400 12 : CreateTIFFColorTable(m_poColorTable.get(), nOvBitsPerSample,
3401 : m_nColorTableMultiplier, anTRed, anTGreen, anTBlue,
3402 : panRed, panGreen, panBlue);
3403 : }
3404 :
3405 : /* -------------------------------------------------------------------- */
3406 : /* Establish which of the overview levels we already have, and */
3407 : /* which are new. We assume that band 1 of the file is */
3408 : /* representative. */
3409 : /* -------------------------------------------------------------------- */
3410 256 : int nOvrBlockXSize = 0;
3411 256 : int nOvrBlockYSize = 0;
3412 256 : GTIFFGetOverviewBlockSize(GDALRasterBand::ToHandle(GetRasterBand(1)),
3413 : &nOvrBlockXSize, &nOvrBlockYSize, papszOptions,
3414 : "BLOCKSIZE");
3415 512 : std::vector<bool> abRequireNewOverview(nOverviews, true);
3416 695 : for (int i = 0; i < nOverviews && eErr == CE_None; ++i)
3417 : {
3418 771 : for (auto &poODS : m_apoOverviewDS)
3419 : {
3420 : const int nOvFactor =
3421 776 : GDALComputeOvFactor(poODS->GetRasterXSize(), GetRasterXSize(),
3422 388 : poODS->GetRasterYSize(), GetRasterYSize());
3423 :
3424 : // If we already have a 1x1 overview and this new one would result
3425 : // in it too, then don't create it.
3426 448 : if (poODS->GetRasterXSize() == 1 && poODS->GetRasterYSize() == 1 &&
3427 448 : DIV_ROUND_UP(GetRasterXSize(), panOverviewList[i]) == 1 &&
3428 21 : DIV_ROUND_UP(GetRasterYSize(), panOverviewList[i]) == 1)
3429 : {
3430 21 : abRequireNewOverview[i] = false;
3431 21 : break;
3432 : }
3433 :
3434 699 : if (nOvFactor == panOverviewList[i] ||
3435 332 : nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
3436 : GetRasterXSize(),
3437 : GetRasterYSize()))
3438 : {
3439 35 : abRequireNewOverview[i] = false;
3440 35 : break;
3441 : }
3442 : }
3443 :
3444 439 : if (abRequireNewOverview[i])
3445 : {
3446 383 : if (m_bLayoutIFDSBeforeData && !m_bKnownIncompatibleEdition &&
3447 2 : !m_bWriteKnownIncompatibleEdition)
3448 : {
3449 2 : ReportError(CE_Warning, CPLE_AppDefined,
3450 : "Adding new overviews invalidates the "
3451 : "LAYOUT=IFDS_BEFORE_DATA property");
3452 2 : m_bKnownIncompatibleEdition = true;
3453 2 : m_bWriteKnownIncompatibleEdition = true;
3454 : }
3455 :
3456 : const int nOXSize =
3457 383 : DIV_ROUND_UP(GetRasterXSize(), panOverviewList[i]);
3458 : const int nOYSize =
3459 383 : DIV_ROUND_UP(GetRasterYSize(), panOverviewList[i]);
3460 :
3461 766 : const toff_t nOverviewOffset = GTIFFWriteDirectory(
3462 : m_hTIFF, FILETYPE_REDUCEDIMAGE, nOXSize, nOYSize,
3463 383 : nOvBitsPerSample, nPlanarConfig, m_nSamplesPerPixel,
3464 : nOvrBlockXSize, nOvrBlockYSize, TRUE, nCompression,
3465 383 : nPhotometric, m_nSampleFormat, nPredictor, panRed, panGreen,
3466 : panBlue, nExtraSamples, panExtraSampleValues, osMetadata,
3467 383 : nOvrJpegQuality >= 0 ? CPLSPrintf("%d", nOvrJpegQuality)
3468 : : nullptr,
3469 383 : CPLSPrintf("%d", m_nJpegTablesMode),
3470 25 : osNoData.empty() ? nullptr : osNoData.c_str(),
3471 383 : m_anLercAddCompressionAndVersion, false);
3472 :
3473 383 : if (nOverviewOffset == 0)
3474 0 : eErr = CE_Failure;
3475 : else
3476 383 : eErr = RegisterNewOverviewDataset(
3477 : nOverviewOffset, nOvrJpegQuality, papszOptions);
3478 : }
3479 : }
3480 :
3481 256 : CPLFree(panExtraSampleValues);
3482 256 : panExtraSampleValues = nullptr;
3483 :
3484 256 : ReloadDirectory();
3485 :
3486 : /* -------------------------------------------------------------------- */
3487 : /* Create overviews for the mask. */
3488 : /* -------------------------------------------------------------------- */
3489 256 : if (eErr != CE_None)
3490 0 : return eErr;
3491 :
3492 256 : eErr = CreateInternalMaskOverviews(nOvrBlockXSize, nOvrBlockYSize);
3493 :
3494 : /* -------------------------------------------------------------------- */
3495 : /* Refresh overviews for the mask */
3496 : /* -------------------------------------------------------------------- */
3497 : const bool bHasInternalMask =
3498 256 : m_poMaskDS != nullptr && m_poMaskDS->GetRasterCount() == 1;
3499 : const bool bHasExternalMask =
3500 256 : !bHasInternalMask && oOvManager.HaveMaskFile();
3501 256 : const bool bHasMask = bHasInternalMask || bHasExternalMask;
3502 :
3503 256 : if (bHasInternalMask)
3504 : {
3505 48 : std::vector<GDALRasterBandH> ahOverviewBands;
3506 64 : for (auto &poOvrDS : m_apoOverviewDS)
3507 : {
3508 40 : if (poOvrDS->m_poMaskDS != nullptr)
3509 : {
3510 40 : ahOverviewBands.push_back(GDALRasterBand::ToHandle(
3511 40 : poOvrDS->m_poMaskDS->GetRasterBand(1)));
3512 : }
3513 : }
3514 :
3515 48 : void *pScaledProgressData = GDALCreateScaledProgress(
3516 24 : 0, 1.0 / (nBands + 1), pfnProgress, pProgressData);
3517 24 : eErr = GDALRegenerateOverviewsEx(
3518 24 : m_poMaskDS->GetRasterBand(1),
3519 24 : static_cast<int>(ahOverviewBands.size()), ahOverviewBands.data(),
3520 : pszResampling, GDALScaledProgress, pScaledProgressData,
3521 : papszOptions);
3522 24 : GDALDestroyScaledProgress(pScaledProgressData);
3523 : }
3524 232 : else if (bHasExternalMask)
3525 : {
3526 4 : void *pScaledProgressData = GDALCreateScaledProgress(
3527 2 : 0, 1.0 / (nBands + 1), pfnProgress, pProgressData);
3528 2 : eErr = oOvManager.BuildOverviewsMask(
3529 : pszResampling, nOverviews, panOverviewList, GDALScaledProgress,
3530 : pScaledProgressData, papszOptions);
3531 2 : GDALDestroyScaledProgress(pScaledProgressData);
3532 : }
3533 :
3534 : // If we have an alpha band, we want it to be generated before downsampling
3535 : // other bands
3536 256 : bool bHasAlphaBand = false;
3537 66260 : for (int iBand = 0; iBand < nBands; iBand++)
3538 : {
3539 66004 : if (papoBands[iBand]->GetColorInterpretation() == GCI_AlphaBand)
3540 19 : bHasAlphaBand = true;
3541 : }
3542 :
3543 : /* -------------------------------------------------------------------- */
3544 : /* Refresh old overviews that were listed. */
3545 : /* -------------------------------------------------------------------- */
3546 256 : const auto poColorTable = GetRasterBand(panBandList[0])->GetColorTable();
3547 21 : if ((m_nPlanarConfig == PLANARCONFIG_CONTIG || bHasAlphaBand) &&
3548 237 : GDALDataTypeIsComplex(
3549 237 : GetRasterBand(panBandList[0])->GetRasterDataType()) == FALSE &&
3550 12 : (poColorTable == nullptr || STARTS_WITH_CI(pszResampling, "NEAR") ||
3551 513 : poColorTable->IsIdentity()) &&
3552 229 : (STARTS_WITH_CI(pszResampling, "NEAR") ||
3553 119 : EQUAL(pszResampling, "AVERAGE") || EQUAL(pszResampling, "RMS") ||
3554 49 : EQUAL(pszResampling, "GAUSS") || EQUAL(pszResampling, "CUBIC") ||
3555 30 : EQUAL(pszResampling, "CUBICSPLINE") ||
3556 29 : EQUAL(pszResampling, "LANCZOS") || EQUAL(pszResampling, "BILINEAR") ||
3557 25 : EQUAL(pszResampling, "MODE")))
3558 : {
3559 : // In the case of pixel interleaved compressed overviews, we want to
3560 : // generate the overviews for all the bands block by block, and not
3561 : // band after band, in order to write the block once and not loose
3562 : // space in the TIFF file. We also use that logic for uncompressed
3563 : // overviews, since GDALRegenerateOverviewsMultiBand() will be able to
3564 : // trigger cascading overview regeneration even in the presence
3565 : // of an alpha band.
3566 :
3567 207 : int nNewOverviews = 0;
3568 :
3569 : GDALRasterBand ***papapoOverviewBands = static_cast<GDALRasterBand ***>(
3570 207 : CPLCalloc(sizeof(void *), nBandsIn));
3571 : GDALRasterBand **papoBandList =
3572 207 : static_cast<GDALRasterBand **>(CPLCalloc(sizeof(void *), nBandsIn));
3573 66114 : for (int iBand = 0; iBand < nBandsIn; ++iBand)
3574 : {
3575 65907 : GDALRasterBand *poBand = GetRasterBand(panBandList[iBand]);
3576 :
3577 65907 : papoBandList[iBand] = poBand;
3578 131814 : papapoOverviewBands[iBand] = static_cast<GDALRasterBand **>(
3579 65907 : CPLCalloc(sizeof(void *), poBand->GetOverviewCount()));
3580 :
3581 65907 : int iCurOverview = 0;
3582 : std::vector<bool> abAlreadyUsedOverviewBand(
3583 65907 : poBand->GetOverviewCount(), false);
3584 :
3585 132099 : for (int i = 0; i < nOverviews; ++i)
3586 : {
3587 66651 : for (int j = 0; j < poBand->GetOverviewCount(); ++j)
3588 : {
3589 66636 : if (abAlreadyUsedOverviewBand[j])
3590 458 : continue;
3591 :
3592 : int nOvFactor;
3593 66178 : GDALRasterBand *poOverview = poBand->GetOverview(j);
3594 :
3595 66178 : nOvFactor = GDALComputeOvFactor(
3596 : poOverview->GetXSize(), poBand->GetXSize(),
3597 : poOverview->GetYSize(), poBand->GetYSize());
3598 :
3599 66178 : GDALCopyNoDataValue(poOverview, poBand);
3600 :
3601 66179 : if (nOvFactor == panOverviewList[i] ||
3602 1 : nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
3603 : poBand->GetXSize(),
3604 : poBand->GetYSize()))
3605 : {
3606 66177 : if (iBand == 0)
3607 : {
3608 : const auto osNewResampling =
3609 666 : GDALGetNormalizedOvrResampling(pszResampling);
3610 : const char *pszExistingResampling =
3611 333 : poOverview->GetMetadataItem("RESAMPLING");
3612 666 : if (pszExistingResampling &&
3613 333 : pszExistingResampling != osNewResampling)
3614 : {
3615 2 : poOverview->SetMetadataItem(
3616 2 : "RESAMPLING", osNewResampling.c_str());
3617 : }
3618 : }
3619 :
3620 66177 : abAlreadyUsedOverviewBand[j] = true;
3621 66177 : CPLAssert(iCurOverview < poBand->GetOverviewCount());
3622 66177 : papapoOverviewBands[iBand][iCurOverview] = poOverview;
3623 66177 : ++iCurOverview;
3624 66177 : break;
3625 : }
3626 : }
3627 : }
3628 :
3629 65907 : if (nNewOverviews == 0)
3630 : {
3631 207 : nNewOverviews = iCurOverview;
3632 : }
3633 65700 : else if (nNewOverviews != iCurOverview)
3634 : {
3635 0 : CPLAssert(false);
3636 : return CE_Failure;
3637 : }
3638 : }
3639 :
3640 : void *pScaledProgressData =
3641 207 : bHasMask ? GDALCreateScaledProgress(1.0 / (nBands + 1), 1.0,
3642 : pfnProgress, pProgressData)
3643 181 : : GDALCreateScaledProgress(0.0, 1.0, pfnProgress,
3644 207 : pProgressData);
3645 207 : GDALRegenerateOverviewsMultiBand(nBandsIn, papoBandList, nNewOverviews,
3646 : papapoOverviewBands, pszResampling,
3647 : GDALScaledProgress,
3648 : pScaledProgressData, papszOptions);
3649 207 : GDALDestroyScaledProgress(pScaledProgressData);
3650 :
3651 66114 : for (int iBand = 0; iBand < nBandsIn; ++iBand)
3652 : {
3653 65907 : CPLFree(papapoOverviewBands[iBand]);
3654 : }
3655 207 : CPLFree(papapoOverviewBands);
3656 207 : CPLFree(papoBandList);
3657 : }
3658 : else
3659 : {
3660 : GDALRasterBand **papoOverviewBands = static_cast<GDALRasterBand **>(
3661 49 : CPLCalloc(sizeof(void *), nOverviews));
3662 :
3663 49 : const int iBandOffset = bHasMask ? 1 : 0;
3664 :
3665 146 : for (int iBand = 0; iBand < nBandsIn && eErr == CE_None; ++iBand)
3666 : {
3667 97 : GDALRasterBand *poBand = GetRasterBand(panBandList[iBand]);
3668 97 : if (poBand == nullptr)
3669 : {
3670 0 : eErr = CE_Failure;
3671 0 : break;
3672 : }
3673 :
3674 : std::vector<bool> abAlreadyUsedOverviewBand(
3675 194 : poBand->GetOverviewCount(), false);
3676 :
3677 97 : int nNewOverviews = 0;
3678 290 : for (int i = 0; i < nOverviews; ++i)
3679 : {
3680 451 : for (int j = 0; j < poBand->GetOverviewCount(); ++j)
3681 : {
3682 433 : if (abAlreadyUsedOverviewBand[j])
3683 257 : continue;
3684 :
3685 176 : GDALRasterBand *poOverview = poBand->GetOverview(j);
3686 :
3687 176 : GDALCopyNoDataValue(poOverview, poBand);
3688 :
3689 176 : const int nOvFactor = GDALComputeOvFactor(
3690 : poOverview->GetXSize(), poBand->GetXSize(),
3691 : poOverview->GetYSize(), poBand->GetYSize());
3692 :
3693 177 : if (nOvFactor == panOverviewList[i] ||
3694 1 : nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
3695 : poBand->GetXSize(),
3696 : poBand->GetYSize()))
3697 : {
3698 175 : if (iBand == 0)
3699 : {
3700 : const auto osNewResampling =
3701 170 : GDALGetNormalizedOvrResampling(pszResampling);
3702 : const char *pszExistingResampling =
3703 85 : poOverview->GetMetadataItem("RESAMPLING");
3704 137 : if (pszExistingResampling &&
3705 52 : pszExistingResampling != osNewResampling)
3706 : {
3707 1 : poOverview->SetMetadataItem(
3708 1 : "RESAMPLING", osNewResampling.c_str());
3709 : }
3710 : }
3711 :
3712 175 : abAlreadyUsedOverviewBand[j] = true;
3713 175 : CPLAssert(nNewOverviews < poBand->GetOverviewCount());
3714 175 : papoOverviewBands[nNewOverviews++] = poOverview;
3715 175 : break;
3716 : }
3717 : }
3718 : }
3719 :
3720 194 : void *pScaledProgressData = GDALCreateScaledProgress(
3721 97 : (iBand + iBandOffset) /
3722 97 : static_cast<double>(nBandsIn + iBandOffset),
3723 97 : (iBand + iBandOffset + 1) /
3724 97 : static_cast<double>(nBandsIn + iBandOffset),
3725 : pfnProgress, pProgressData);
3726 :
3727 97 : eErr = GDALRegenerateOverviewsEx(
3728 : poBand, nNewOverviews,
3729 : reinterpret_cast<GDALRasterBandH *>(papoOverviewBands),
3730 : pszResampling, GDALScaledProgress, pScaledProgressData,
3731 : papszOptions);
3732 :
3733 97 : GDALDestroyScaledProgress(pScaledProgressData);
3734 : }
3735 :
3736 : /* --------------------------------------------------------------------
3737 : */
3738 : /* Cleanup */
3739 : /* --------------------------------------------------------------------
3740 : */
3741 49 : CPLFree(papoOverviewBands);
3742 : }
3743 :
3744 256 : pfnProgress(1.0, nullptr, pProgressData);
3745 :
3746 256 : return eErr;
3747 : }
3748 :
3749 : /************************************************************************/
3750 : /* GTiffWriteDummyGeokeyDirectory() */
3751 : /************************************************************************/
3752 :
3753 1554 : static void GTiffWriteDummyGeokeyDirectory(TIFF *hTIFF)
3754 : {
3755 : // If we have existing geokeys, try to wipe them
3756 : // by writing a dummy geokey directory. (#2546)
3757 1554 : uint16_t *panVI = nullptr;
3758 1554 : uint16_t nKeyCount = 0;
3759 :
3760 1554 : if (TIFFGetField(hTIFF, TIFFTAG_GEOKEYDIRECTORY, &nKeyCount, &panVI))
3761 : {
3762 27 : GUInt16 anGKVersionInfo[4] = {1, 1, 0, 0};
3763 27 : double adfDummyDoubleParams[1] = {0.0};
3764 27 : TIFFSetField(hTIFF, TIFFTAG_GEOKEYDIRECTORY, 4, anGKVersionInfo);
3765 27 : TIFFSetField(hTIFF, TIFFTAG_GEODOUBLEPARAMS, 1, adfDummyDoubleParams);
3766 27 : TIFFSetField(hTIFF, TIFFTAG_GEOASCIIPARAMS, "");
3767 : }
3768 1554 : }
3769 :
3770 : /************************************************************************/
3771 : /* IsSRSCompatibleOfGeoTIFF() */
3772 : /************************************************************************/
3773 :
3774 3254 : static bool IsSRSCompatibleOfGeoTIFF(const OGRSpatialReference *poSRS,
3775 : GTIFFKeysFlavorEnum eGeoTIFFKeysFlavor)
3776 : {
3777 3254 : char *pszWKT = nullptr;
3778 3254 : if ((poSRS->IsGeographic() || poSRS->IsProjected()) && !poSRS->IsCompound())
3779 : {
3780 3236 : const char *pszAuthName = poSRS->GetAuthorityName();
3781 3236 : const char *pszAuthCode = poSRS->GetAuthorityCode();
3782 3236 : if (pszAuthName && pszAuthCode && EQUAL(pszAuthName, "EPSG"))
3783 2665 : return true;
3784 : }
3785 : OGRErr eErr;
3786 : {
3787 1178 : CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
3788 1178 : if (poSRS->IsDerivedGeographic() ||
3789 589 : (poSRS->IsProjected() && !poSRS->IsCompound() &&
3790 70 : poSRS->GetAxesCount() == 3))
3791 : {
3792 0 : eErr = OGRERR_FAILURE;
3793 : }
3794 : else
3795 : {
3796 : // Geographic3D CRS can't be exported to WKT1, but are
3797 : // valid GeoTIFF 1.1
3798 589 : const char *const apszOptions[] = {
3799 589 : poSRS->IsGeographic() ? nullptr : "FORMAT=WKT1", nullptr};
3800 589 : eErr = poSRS->exportToWkt(&pszWKT, apszOptions);
3801 589 : if (eErr == OGRERR_FAILURE && poSRS->IsProjected() &&
3802 : eGeoTIFFKeysFlavor == GEOTIFF_KEYS_ESRI_PE)
3803 : {
3804 0 : CPLFree(pszWKT);
3805 0 : const char *const apszOptionsESRIWKT[] = {"FORMAT=WKT1_ESRI",
3806 : nullptr};
3807 0 : eErr = poSRS->exportToWkt(&pszWKT, apszOptionsESRIWKT);
3808 : }
3809 : }
3810 : }
3811 589 : const bool bCompatibleOfGeoTIFF =
3812 1177 : (eErr == OGRERR_NONE && pszWKT != nullptr &&
3813 588 : strstr(pszWKT, "custom_proj4") == nullptr);
3814 589 : CPLFree(pszWKT);
3815 589 : return bCompatibleOfGeoTIFF;
3816 : }
3817 :
3818 : /************************************************************************/
3819 : /* WriteGeoTIFFInfo() */
3820 : /************************************************************************/
3821 :
3822 5966 : void GTiffDataset::WriteGeoTIFFInfo()
3823 :
3824 : {
3825 5966 : bool bPixelIsPoint = false;
3826 5966 : bool bPointGeoIgnore = false;
3827 :
3828 : const char *pszAreaOrPoint =
3829 5966 : GTiffDataset::GetMetadataItem(GDALMD_AREA_OR_POINT);
3830 5966 : if (pszAreaOrPoint && EQUAL(pszAreaOrPoint, GDALMD_AOP_POINT))
3831 : {
3832 19 : bPixelIsPoint = true;
3833 : bPointGeoIgnore =
3834 19 : CPLTestBool(CPLGetConfigOption("GTIFF_POINT_GEO_IGNORE", "FALSE"));
3835 : }
3836 :
3837 5966 : if (m_bForceUnsetGTOrGCPs)
3838 : {
3839 11 : m_bNeedsRewrite = true;
3840 11 : m_bForceUnsetGTOrGCPs = false;
3841 :
3842 11 : TIFFUnsetField(m_hTIFF, TIFFTAG_GEOPIXELSCALE);
3843 11 : TIFFUnsetField(m_hTIFF, TIFFTAG_GEOTIEPOINTS);
3844 11 : TIFFUnsetField(m_hTIFF, TIFFTAG_GEOTRANSMATRIX);
3845 : }
3846 :
3847 5966 : if (m_bForceUnsetProjection)
3848 : {
3849 8 : m_bNeedsRewrite = true;
3850 8 : m_bForceUnsetProjection = false;
3851 :
3852 8 : TIFFUnsetField(m_hTIFF, TIFFTAG_GEOKEYDIRECTORY);
3853 8 : TIFFUnsetField(m_hTIFF, TIFFTAG_GEODOUBLEPARAMS);
3854 8 : TIFFUnsetField(m_hTIFF, TIFFTAG_GEOASCIIPARAMS);
3855 : }
3856 :
3857 : /* -------------------------------------------------------------------- */
3858 : /* Write geotransform if valid. */
3859 : /* -------------------------------------------------------------------- */
3860 5966 : if (m_bGeoTransformValid)
3861 : {
3862 1882 : m_bNeedsRewrite = true;
3863 :
3864 : /* --------------------------------------------------------------------
3865 : */
3866 : /* Clear old tags to ensure we don't end up with conflicting */
3867 : /* information. (#2625) */
3868 : /* --------------------------------------------------------------------
3869 : */
3870 1882 : TIFFUnsetField(m_hTIFF, TIFFTAG_GEOPIXELSCALE);
3871 1882 : TIFFUnsetField(m_hTIFF, TIFFTAG_GEOTIEPOINTS);
3872 1882 : TIFFUnsetField(m_hTIFF, TIFFTAG_GEOTRANSMATRIX);
3873 :
3874 : /* --------------------------------------------------------------------
3875 : */
3876 : /* Write the transform. If we have a normal north-up image we */
3877 : /* use the tiepoint plus pixelscale otherwise we use a matrix. */
3878 : /* --------------------------------------------------------------------
3879 : */
3880 1882 : if (m_gt.xrot == 0.0 && m_gt.yrot == 0.0 && m_gt.yscale < 0.0)
3881 : {
3882 1789 : double dfOffset = 0.0;
3883 1789 : if (m_eProfile != GTiffProfile::BASELINE)
3884 : {
3885 : // In the case the SRS has a vertical component and we have
3886 : // a single band, encode its scale/offset in the GeoTIFF tags
3887 1783 : int bHasScale = FALSE;
3888 1783 : double dfScale = GetRasterBand(1)->GetScale(&bHasScale);
3889 1783 : int bHasOffset = FALSE;
3890 1783 : dfOffset = GetRasterBand(1)->GetOffset(&bHasOffset);
3891 : const bool bApplyScaleOffset =
3892 1783 : m_oSRS.IsVertical() && GetRasterCount() == 1;
3893 1783 : if (bApplyScaleOffset && !bHasScale)
3894 0 : dfScale = 1.0;
3895 1783 : if (!bApplyScaleOffset || !bHasOffset)
3896 1780 : dfOffset = 0.0;
3897 1783 : const double adfPixelScale[3] = {m_gt.xscale, fabs(m_gt.yscale),
3898 1783 : bApplyScaleOffset ? dfScale
3899 1783 : : 0.0};
3900 1783 : TIFFSetField(m_hTIFF, TIFFTAG_GEOPIXELSCALE, 3, adfPixelScale);
3901 : }
3902 :
3903 1789 : double adfTiePoints[6] = {0.0, 0.0, 0.0,
3904 1789 : m_gt.xorig, m_gt.yorig, dfOffset};
3905 :
3906 1789 : if (bPixelIsPoint && !bPointGeoIgnore)
3907 : {
3908 15 : adfTiePoints[3] += m_gt.xscale * 0.5 + m_gt.xrot * 0.5;
3909 15 : adfTiePoints[4] += m_gt.yrot * 0.5 + m_gt.yscale * 0.5;
3910 : }
3911 :
3912 1789 : if (m_eProfile != GTiffProfile::BASELINE)
3913 1789 : TIFFSetField(m_hTIFF, TIFFTAG_GEOTIEPOINTS, 6, adfTiePoints);
3914 : }
3915 : else
3916 : {
3917 93 : double adfMatrix[16] = {};
3918 :
3919 93 : adfMatrix[0] = m_gt.xscale;
3920 93 : adfMatrix[1] = m_gt.xrot;
3921 93 : adfMatrix[3] = m_gt.xorig;
3922 93 : adfMatrix[4] = m_gt.yrot;
3923 93 : adfMatrix[5] = m_gt.yscale;
3924 93 : adfMatrix[7] = m_gt.yorig;
3925 93 : adfMatrix[15] = 1.0;
3926 :
3927 93 : if (bPixelIsPoint && !bPointGeoIgnore)
3928 : {
3929 0 : adfMatrix[3] += m_gt.xscale * 0.5 + m_gt.xrot * 0.5;
3930 0 : adfMatrix[7] += m_gt.yrot * 0.5 + m_gt.yscale * 0.5;
3931 : }
3932 :
3933 93 : if (m_eProfile != GTiffProfile::BASELINE)
3934 93 : TIFFSetField(m_hTIFF, TIFFTAG_GEOTRANSMATRIX, 16, adfMatrix);
3935 : }
3936 :
3937 1882 : if (m_poBaseDS == nullptr)
3938 : {
3939 : // Do we need a world file?
3940 1882 : if (CPLFetchBool(m_papszCreationOptions, "TFW", false))
3941 7 : GDALWriteWorldFile(m_osFilename.c_str(), "tfw", m_gt.data());
3942 1875 : else if (CPLFetchBool(m_papszCreationOptions, "WORLDFILE", false))
3943 2 : GDALWriteWorldFile(m_osFilename.c_str(), "wld", m_gt.data());
3944 : }
3945 : }
3946 4097 : else if (GetGCPCount() > 0 && GetGCPCount() <= knMAX_GCP_COUNT &&
3947 13 : m_eProfile != GTiffProfile::BASELINE)
3948 : {
3949 13 : m_bNeedsRewrite = true;
3950 :
3951 : double *padfTiePoints = static_cast<double *>(
3952 13 : CPLMalloc(6 * sizeof(double) * GetGCPCount()));
3953 :
3954 69 : for (size_t iGCP = 0; iGCP < m_aoGCPs.size(); ++iGCP)
3955 : {
3956 :
3957 56 : padfTiePoints[iGCP * 6 + 0] = m_aoGCPs[iGCP].Pixel();
3958 56 : padfTiePoints[iGCP * 6 + 1] = m_aoGCPs[iGCP].Line();
3959 56 : padfTiePoints[iGCP * 6 + 2] = 0;
3960 56 : padfTiePoints[iGCP * 6 + 3] = m_aoGCPs[iGCP].X();
3961 56 : padfTiePoints[iGCP * 6 + 4] = m_aoGCPs[iGCP].Y();
3962 56 : padfTiePoints[iGCP * 6 + 5] = m_aoGCPs[iGCP].Z();
3963 :
3964 56 : if (bPixelIsPoint && !bPointGeoIgnore)
3965 : {
3966 0 : padfTiePoints[iGCP * 6 + 0] += 0.5;
3967 0 : padfTiePoints[iGCP * 6 + 1] += 0.5;
3968 : }
3969 : }
3970 :
3971 13 : TIFFSetField(m_hTIFF, TIFFTAG_GEOTIEPOINTS, 6 * GetGCPCount(),
3972 : padfTiePoints);
3973 13 : CPLFree(padfTiePoints);
3974 : }
3975 :
3976 : /* -------------------------------------------------------------------- */
3977 : /* Write out projection definition. */
3978 : /* -------------------------------------------------------------------- */
3979 5966 : const bool bHasProjection = !m_oSRS.IsEmpty();
3980 5966 : if ((bHasProjection || bPixelIsPoint) &&
3981 1558 : m_eProfile != GTiffProfile::BASELINE)
3982 : {
3983 1554 : m_bNeedsRewrite = true;
3984 :
3985 : // If we have existing geokeys, try to wipe them
3986 : // by writing a dummy geokey directory. (#2546)
3987 1554 : GTiffWriteDummyGeokeyDirectory(m_hTIFF);
3988 :
3989 1554 : GTIF *psGTIF = GTiffDataset::GTIFNew(m_hTIFF);
3990 :
3991 : // Set according to coordinate system.
3992 1554 : if (bHasProjection)
3993 : {
3994 1553 : if (IsSRSCompatibleOfGeoTIFF(&m_oSRS, m_eGeoTIFFKeysFlavor))
3995 : {
3996 1551 : GTIFSetFromOGISDefnEx(psGTIF,
3997 : OGRSpatialReference::ToHandle(&m_oSRS),
3998 : m_eGeoTIFFKeysFlavor, m_eGeoTIFFVersion);
3999 : }
4000 : else
4001 : {
4002 2 : GDALPamDataset::SetSpatialRef(&m_oSRS);
4003 : }
4004 : }
4005 :
4006 1554 : if (bPixelIsPoint)
4007 : {
4008 19 : GTIFKeySet(psGTIF, GTRasterTypeGeoKey, TYPE_SHORT, 1,
4009 : RasterPixelIsPoint);
4010 : }
4011 :
4012 1554 : GTIFWriteKeys(psGTIF);
4013 1554 : GTIFFree(psGTIF);
4014 : }
4015 5966 : }
4016 :
4017 : /************************************************************************/
4018 : /* AppendMetadataItem() */
4019 : /************************************************************************/
4020 :
4021 3898 : static void AppendMetadataItem(CPLXMLNode **ppsRoot, CPLXMLNode **ppsTail,
4022 : const char *pszKey, const char *pszValue,
4023 : CPLXMLNode *psValueNode, int nBand,
4024 : const char *pszRole, const char *pszDomain)
4025 :
4026 : {
4027 3898 : CPLAssert(pszValue || psValueNode);
4028 3898 : CPLAssert(!(pszValue && psValueNode));
4029 :
4030 : /* -------------------------------------------------------------------- */
4031 : /* Create the Item element, and subcomponents. */
4032 : /* -------------------------------------------------------------------- */
4033 3898 : CPLXMLNode *psItem = CPLCreateXMLNode(nullptr, CXT_Element, "Item");
4034 3898 : CPLAddXMLAttributeAndValue(psItem, "name", pszKey);
4035 :
4036 3898 : if (nBand > 0)
4037 : {
4038 1167 : char szBandId[32] = {};
4039 1167 : snprintf(szBandId, sizeof(szBandId), "%d", nBand - 1);
4040 1167 : CPLAddXMLAttributeAndValue(psItem, "sample", szBandId);
4041 : }
4042 :
4043 3898 : if (pszRole != nullptr)
4044 384 : CPLAddXMLAttributeAndValue(psItem, "role", pszRole);
4045 :
4046 3898 : if (pszDomain != nullptr && strlen(pszDomain) > 0)
4047 1012 : CPLAddXMLAttributeAndValue(psItem, "domain", pszDomain);
4048 :
4049 3898 : if (pszValue)
4050 : {
4051 : // Note: this escaping should not normally be done, as the serialization
4052 : // of the tree to XML also does it, so we end up width double XML escaping,
4053 : // but keep it for backward compatibility.
4054 3877 : char *pszEscapedItemValue = CPLEscapeString(pszValue, -1, CPLES_XML);
4055 3877 : CPLCreateXMLNode(psItem, CXT_Text, pszEscapedItemValue);
4056 3877 : CPLFree(pszEscapedItemValue);
4057 : }
4058 : else
4059 : {
4060 21 : CPLAddXMLChild(psItem, psValueNode);
4061 : }
4062 :
4063 : /* -------------------------------------------------------------------- */
4064 : /* Create root, if missing. */
4065 : /* -------------------------------------------------------------------- */
4066 3898 : if (*ppsRoot == nullptr)
4067 766 : *ppsRoot = CPLCreateXMLNode(nullptr, CXT_Element, "GDALMetadata");
4068 :
4069 : /* -------------------------------------------------------------------- */
4070 : /* Append item to tail. We keep track of the tail to avoid */
4071 : /* O(nsquared) time as the list gets longer. */
4072 : /* -------------------------------------------------------------------- */
4073 3898 : if (*ppsTail == nullptr)
4074 766 : CPLAddXMLChild(*ppsRoot, psItem);
4075 : else
4076 3132 : CPLAddXMLSibling(*ppsTail, psItem);
4077 :
4078 3898 : *ppsTail = psItem;
4079 3898 : }
4080 :
4081 : /************************************************************************/
4082 : /* AppendMetadataItem() */
4083 : /************************************************************************/
4084 :
4085 3877 : static void AppendMetadataItem(CPLXMLNode **ppsRoot, CPLXMLNode **ppsTail,
4086 : const char *pszKey, const char *pszValue,
4087 : int nBand, const char *pszRole,
4088 : const char *pszDomain)
4089 :
4090 : {
4091 3877 : AppendMetadataItem(ppsRoot, ppsTail, pszKey, pszValue, nullptr, nBand,
4092 : pszRole, pszDomain);
4093 3877 : }
4094 :
4095 : /************************************************************************/
4096 : /* WriteMDMetadata() */
4097 : /************************************************************************/
4098 :
4099 311327 : static void WriteMDMetadata(GDALMultiDomainMetadata *poMDMD, TIFF *hTIFF,
4100 : CPLXMLNode **ppsRoot, CPLXMLNode **ppsTail,
4101 : int nBand, GTiffProfile eProfile)
4102 :
4103 : {
4104 :
4105 : /* ==================================================================== */
4106 : /* Process each domain. */
4107 : /* ==================================================================== */
4108 311327 : CSLConstList papszDomainList = poMDMD->GetDomainList();
4109 319962 : for (int iDomain = 0; papszDomainList && papszDomainList[iDomain];
4110 : ++iDomain)
4111 : {
4112 8635 : CSLConstList papszMD = poMDMD->GetMetadata(papszDomainList[iDomain]);
4113 8635 : bool bIsXMLOrJSON = false;
4114 :
4115 8635 : if (EQUAL(papszDomainList[iDomain], GDAL_MDD_IMAGE_STRUCTURE) ||
4116 2523 : EQUAL(papszDomainList[iDomain], "DERIVED_SUBDATASETS"))
4117 6115 : continue; // Ignored.
4118 2520 : if (EQUAL(papszDomainList[iDomain], "COLOR_PROFILE"))
4119 3 : continue; // Handled elsewhere.
4120 2517 : if (EQUAL(papszDomainList[iDomain], GDAL_MDD_RPC))
4121 7 : continue; // Handled elsewhere.
4122 2511 : if (EQUAL(papszDomainList[iDomain], "xml:ESRI") &&
4123 1 : CPLTestBool(CPLGetConfigOption("ESRI_XML_PAM", "NO")))
4124 1 : continue; // Handled elsewhere.
4125 2509 : if (EQUAL(papszDomainList[iDomain], "xml:XMP"))
4126 2 : continue; // Handled in SetMetadata.
4127 :
4128 2507 : if (STARTS_WITH_CI(papszDomainList[iDomain], "xml:") ||
4129 2505 : STARTS_WITH_CI(papszDomainList[iDomain], "json:"))
4130 : {
4131 12 : bIsXMLOrJSON = true;
4132 : }
4133 :
4134 : /* --------------------------------------------------------------------
4135 : */
4136 : /* Process each item in this domain. */
4137 : /* --------------------------------------------------------------------
4138 : */
4139 7643 : for (int iItem = 0; papszMD && papszMD[iItem]; ++iItem)
4140 : {
4141 5136 : const char *pszItemValue = nullptr;
4142 5136 : char *pszItemName = nullptr;
4143 :
4144 5136 : if (bIsXMLOrJSON)
4145 : {
4146 11 : pszItemName = CPLStrdup("doc");
4147 11 : pszItemValue = papszMD[iItem];
4148 : }
4149 : else
4150 : {
4151 5125 : pszItemValue = CPLParseNameValue(papszMD[iItem], &pszItemName);
4152 5125 : if (pszItemName == nullptr)
4153 : {
4154 49 : CPLDebug("GTiff", "Invalid metadata item : %s",
4155 49 : papszMD[iItem]);
4156 49 : continue;
4157 : }
4158 : }
4159 :
4160 : /* --------------------------------------------------------------------
4161 : */
4162 : /* Convert into XML item or handle as a special TIFF tag. */
4163 : /* --------------------------------------------------------------------
4164 : */
4165 5087 : if (strlen(papszDomainList[iDomain]) == 0 && nBand == 0 &&
4166 3731 : (STARTS_WITH_CI(pszItemName, "TIFFTAG_") ||
4167 3670 : (EQUAL(pszItemName, "GEO_METADATA") &&
4168 3669 : eProfile == GTiffProfile::GDALGEOTIFF) ||
4169 3669 : (EQUAL(pszItemName, "TIFF_RSID") &&
4170 : eProfile == GTiffProfile::GDALGEOTIFF)))
4171 : {
4172 63 : if (EQUAL(pszItemName, "TIFFTAG_RESOLUTIONUNIT"))
4173 : {
4174 : // ResolutionUnit can't be 0, which is the default if
4175 : // atoi() fails. Set to 1=Unknown.
4176 9 : int v = atoi(pszItemValue);
4177 9 : if (!v)
4178 1 : v = RESUNIT_NONE;
4179 9 : TIFFSetField(hTIFF, TIFFTAG_RESOLUTIONUNIT, v);
4180 : }
4181 : else
4182 : {
4183 54 : bool bFoundTag = false;
4184 54 : size_t iTag = 0; // Used after for.
4185 54 : const auto *pasTIFFTags = GTiffDataset::GetTIFFTags();
4186 286 : for (; pasTIFFTags[iTag].pszTagName; ++iTag)
4187 : {
4188 286 : if (EQUAL(pszItemName, pasTIFFTags[iTag].pszTagName))
4189 : {
4190 54 : bFoundTag = true;
4191 54 : break;
4192 : }
4193 : }
4194 :
4195 54 : if (bFoundTag &&
4196 54 : pasTIFFTags[iTag].eType == GTIFFTAGTYPE_STRING)
4197 33 : TIFFSetField(hTIFF, pasTIFFTags[iTag].nTagVal,
4198 : pszItemValue);
4199 21 : else if (bFoundTag &&
4200 21 : pasTIFFTags[iTag].eType == GTIFFTAGTYPE_FLOAT)
4201 16 : TIFFSetField(hTIFF, pasTIFFTags[iTag].nTagVal,
4202 : CPLAtof(pszItemValue));
4203 5 : else if (bFoundTag &&
4204 5 : pasTIFFTags[iTag].eType == GTIFFTAGTYPE_SHORT)
4205 4 : TIFFSetField(hTIFF, pasTIFFTags[iTag].nTagVal,
4206 : atoi(pszItemValue));
4207 1 : else if (bFoundTag && pasTIFFTags[iTag].eType ==
4208 : GTIFFTAGTYPE_BYTE_STRING)
4209 : {
4210 1 : uint32_t nLen =
4211 1 : static_cast<uint32_t>(strlen(pszItemValue));
4212 1 : if (nLen)
4213 : {
4214 1 : TIFFSetField(hTIFF, pasTIFFTags[iTag].nTagVal, nLen,
4215 : pszItemValue);
4216 1 : }
4217 : }
4218 : else
4219 0 : CPLError(CE_Warning, CPLE_NotSupported,
4220 : "%s metadata item is unhandled and "
4221 : "will not be written",
4222 : pszItemName);
4223 63 : }
4224 : }
4225 5024 : else if (nBand == 0 && EQUAL(pszItemName, GDALMD_AREA_OR_POINT))
4226 : {
4227 : /* Do nothing, handled elsewhere. */;
4228 : }
4229 : else
4230 : {
4231 3081 : AppendMetadataItem(ppsRoot, ppsTail, pszItemName, pszItemValue,
4232 3081 : nBand, nullptr, papszDomainList[iDomain]);
4233 : }
4234 :
4235 5087 : CPLFree(pszItemName);
4236 : }
4237 :
4238 : /* --------------------------------------------------------------------
4239 : */
4240 : /* Remove TIFFTAG_xxxxxx that are already set but no longer in */
4241 : /* the metadata list (#5619) */
4242 : /* --------------------------------------------------------------------
4243 : */
4244 2507 : if (strlen(papszDomainList[iDomain]) == 0 && nBand == 0)
4245 : {
4246 2220 : const auto *pasTIFFTags = GTiffDataset::GetTIFFTags();
4247 33300 : for (size_t iTag = 0; pasTIFFTags[iTag].pszTagName; ++iTag)
4248 : {
4249 31080 : uint32_t nCount = 0;
4250 31080 : char *pszText = nullptr;
4251 31080 : int16_t nVal = 0;
4252 31080 : float fVal = 0.0f;
4253 : const char *pszVal =
4254 31080 : CSLFetchNameValue(papszMD, pasTIFFTags[iTag].pszTagName);
4255 62097 : if (pszVal == nullptr &&
4256 31017 : ((pasTIFFTags[iTag].eType == GTIFFTAGTYPE_STRING &&
4257 17727 : TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal,
4258 31009 : &pszText)) ||
4259 31009 : (pasTIFFTags[iTag].eType == GTIFFTAGTYPE_SHORT &&
4260 6647 : TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal, &nVal)) ||
4261 31006 : (pasTIFFTags[iTag].eType == GTIFFTAGTYPE_FLOAT &&
4262 4424 : TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal, &fVal)) ||
4263 31005 : (pasTIFFTags[iTag].eType == GTIFFTAGTYPE_BYTE_STRING &&
4264 2219 : TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal, &nCount,
4265 : &pszText))))
4266 : {
4267 13 : TIFFUnsetField(hTIFF, pasTIFFTags[iTag].nTagVal);
4268 : }
4269 : }
4270 : }
4271 : }
4272 311327 : }
4273 :
4274 : /************************************************************************/
4275 : /* WriteRPC() */
4276 : /************************************************************************/
4277 :
4278 10334 : void GTiffDataset::WriteRPC(GDALDataset *poSrcDS, TIFF *l_hTIFF,
4279 : int bSrcIsGeoTIFF, GTiffProfile eProfile,
4280 : const char *pszTIFFFilename,
4281 : CSLConstList papszCreationOptions,
4282 : bool bWriteOnlyInPAMIfNeeded)
4283 : {
4284 : /* -------------------------------------------------------------------- */
4285 : /* Handle RPC data written to TIFF RPCCoefficient tag, RPB file, */
4286 : /* RPCTEXT file or PAM. */
4287 : /* -------------------------------------------------------------------- */
4288 10334 : CSLConstList papszRPCMD = poSrcDS->GetMetadata(GDAL_MDD_RPC);
4289 10334 : if (papszRPCMD != nullptr)
4290 : {
4291 32 : bool bRPCSerializedOtherWay = false;
4292 :
4293 32 : if (eProfile == GTiffProfile::GDALGEOTIFF)
4294 : {
4295 20 : if (!bWriteOnlyInPAMIfNeeded)
4296 11 : GTiffDatasetWriteRPCTag(l_hTIFF, papszRPCMD);
4297 20 : bRPCSerializedOtherWay = true;
4298 : }
4299 :
4300 : // Write RPB file if explicitly asked, or if a non GDAL specific
4301 : // profile is selected and RPCTXT is not asked.
4302 : bool bRPBExplicitlyAsked =
4303 32 : CPLFetchBool(papszCreationOptions, "RPB", false);
4304 : bool bRPBExplicitlyDenied =
4305 32 : !CPLFetchBool(papszCreationOptions, "RPB", true);
4306 44 : if ((eProfile != GTiffProfile::GDALGEOTIFF &&
4307 12 : !CPLFetchBool(papszCreationOptions, "RPCTXT", false) &&
4308 44 : !bRPBExplicitlyDenied) ||
4309 : bRPBExplicitlyAsked)
4310 : {
4311 8 : if (!bWriteOnlyInPAMIfNeeded)
4312 4 : GDALWriteRPBFile(pszTIFFFilename, papszRPCMD);
4313 8 : bRPCSerializedOtherWay = true;
4314 : }
4315 :
4316 32 : if (CPLFetchBool(papszCreationOptions, "RPCTXT", false))
4317 : {
4318 2 : if (!bWriteOnlyInPAMIfNeeded)
4319 1 : GDALWriteRPCTXTFile(pszTIFFFilename, papszRPCMD);
4320 2 : bRPCSerializedOtherWay = true;
4321 : }
4322 :
4323 32 : if (!bRPCSerializedOtherWay && bWriteOnlyInPAMIfNeeded && bSrcIsGeoTIFF)
4324 1 : cpl::down_cast<GTiffDataset *>(poSrcDS)
4325 1 : ->GDALPamDataset::SetMetadata(papszRPCMD, GDAL_MDD_RPC);
4326 : }
4327 10334 : }
4328 :
4329 : /************************************************************************/
4330 : /* WriteMetadata() */
4331 : /************************************************************************/
4332 :
4333 8194 : bool GTiffDataset::WriteMetadata(GDALDataset *poSrcDS, TIFF *l_hTIFF,
4334 : bool bSrcIsGeoTIFF, GTiffProfile eProfile,
4335 : const char *pszTIFFFilename,
4336 : CSLConstList papszCreationOptions,
4337 : bool bExcludeRPBandIMGFileWriting)
4338 :
4339 : {
4340 : /* -------------------------------------------------------------------- */
4341 : /* Convert all the remaining metadata into a simple XML */
4342 : /* format. */
4343 : /* -------------------------------------------------------------------- */
4344 8194 : CPLXMLNode *psRoot = nullptr;
4345 8194 : CPLXMLNode *psTail = nullptr;
4346 :
4347 : const char *pszCopySrcMDD =
4348 8194 : CSLFetchNameValueDef(papszCreationOptions, "COPY_SRC_MDD", "AUTO");
4349 : char **papszSrcMDD =
4350 8194 : CSLFetchNameValueMultiple(papszCreationOptions, "SRC_MDD");
4351 :
4352 : GTiffDataset *poSrcDSGTiff =
4353 8194 : bSrcIsGeoTIFF ? cpl::down_cast<GTiffDataset *>(poSrcDS) : nullptr;
4354 :
4355 8194 : if (poSrcDSGTiff)
4356 : {
4357 6027 : WriteMDMetadata(&poSrcDSGTiff->m_oGTiffMDMD, l_hTIFF, &psRoot, &psTail,
4358 : 0, eProfile);
4359 : }
4360 : else
4361 : {
4362 2167 : if (EQUAL(pszCopySrcMDD, "AUTO") || CPLTestBool(pszCopySrcMDD) ||
4363 : papszSrcMDD)
4364 : {
4365 4328 : GDALMultiDomainMetadata l_oMDMD;
4366 : {
4367 2164 : CSLConstList papszMD = poSrcDS->GetMetadata();
4368 2168 : if (CSLCount(papszMD) > 0 &&
4369 4 : (!papszSrcMDD || CSLFindString(papszSrcMDD, "") >= 0 ||
4370 2 : CSLFindString(papszSrcMDD, "_DEFAULT_") >= 0))
4371 : {
4372 1646 : l_oMDMD.SetMetadata(papszMD);
4373 : }
4374 : }
4375 :
4376 2164 : if (EQUAL(pszCopySrcMDD, "AUTO") && !papszSrcMDD)
4377 : {
4378 : // Propagate ISIS3 or VICAR metadata
4379 6465 : for (const char *pszMDD : {"json:ISIS3", "json:VICAR"})
4380 : {
4381 4310 : CSLConstList papszMD = poSrcDS->GetMetadata(pszMDD);
4382 4310 : if (papszMD)
4383 : {
4384 5 : l_oMDMD.SetMetadata(papszMD, pszMDD);
4385 : }
4386 : }
4387 : }
4388 :
4389 2164 : if ((!EQUAL(pszCopySrcMDD, "AUTO") && CPLTestBool(pszCopySrcMDD)) ||
4390 : papszSrcMDD)
4391 : {
4392 9 : char **papszDomainList = poSrcDS->GetMetadataDomainList();
4393 39 : for (CSLConstList papszIter = papszDomainList;
4394 39 : papszIter && *papszIter; ++papszIter)
4395 : {
4396 30 : const char *pszDomain = *papszIter;
4397 46 : if (pszDomain[0] != 0 &&
4398 16 : (!papszSrcMDD ||
4399 16 : CSLFindString(papszSrcMDD, pszDomain) >= 0))
4400 : {
4401 12 : l_oMDMD.SetMetadata(poSrcDS->GetMetadata(pszDomain),
4402 : pszDomain);
4403 : }
4404 : }
4405 9 : CSLDestroy(papszDomainList);
4406 : }
4407 :
4408 2164 : WriteMDMetadata(&l_oMDMD, l_hTIFF, &psRoot, &psTail, 0, eProfile);
4409 : }
4410 : }
4411 :
4412 8194 : if (!bExcludeRPBandIMGFileWriting &&
4413 6021 : (!poSrcDSGTiff || poSrcDSGTiff->m_poBaseDS == nullptr))
4414 : {
4415 8183 : WriteRPC(poSrcDS, l_hTIFF, bSrcIsGeoTIFF, eProfile, pszTIFFFilename,
4416 : papszCreationOptions);
4417 :
4418 : /* ------------------------------------------------------------------ */
4419 : /* Handle metadata data written to an IMD file. */
4420 : /* ------------------------------------------------------------------ */
4421 8183 : CSLConstList papszIMDMD = poSrcDS->GetMetadata(GDAL_MDD_IMD);
4422 8183 : if (papszIMDMD != nullptr)
4423 : {
4424 20 : GDALWriteIMDFile(pszTIFFFilename, papszIMDMD);
4425 : }
4426 : }
4427 :
4428 8194 : uint16_t nPhotometric = 0;
4429 8194 : if (!TIFFGetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, &(nPhotometric)))
4430 1 : nPhotometric = PHOTOMETRIC_MINISBLACK;
4431 :
4432 8194 : const bool bStandardColorInterp = GTIFFIsStandardColorInterpretation(
4433 : GDALDataset::ToHandle(poSrcDS), nPhotometric, papszCreationOptions);
4434 :
4435 : /* -------------------------------------------------------------------- */
4436 : /* We also need to address band specific metadata, and special */
4437 : /* "role" metadata. */
4438 : /* -------------------------------------------------------------------- */
4439 316336 : for (int nBand = 1; nBand <= poSrcDS->GetRasterCount(); ++nBand)
4440 : {
4441 308142 : GDALRasterBand *poBand = poSrcDS->GetRasterBand(nBand);
4442 :
4443 308142 : if (bSrcIsGeoTIFF)
4444 : {
4445 : GTiffRasterBand *poSrcBandGTiff =
4446 303044 : cpl::down_cast<GTiffRasterBand *>(poBand);
4447 303044 : assert(poSrcBandGTiff);
4448 303044 : WriteMDMetadata(&poSrcBandGTiff->m_oGTiffMDMD, l_hTIFF, &psRoot,
4449 : &psTail, nBand, eProfile);
4450 : }
4451 : else
4452 : {
4453 10196 : GDALMultiDomainMetadata l_oMDMD;
4454 5098 : bool bOMDMDSet = false;
4455 :
4456 5098 : if (EQUAL(pszCopySrcMDD, "AUTO") && !papszSrcMDD)
4457 : {
4458 15258 : for (const char *pszDomain : {"", GDAL_MDD_IMAGERY})
4459 : {
4460 10172 : if (CSLConstList papszMD = poBand->GetMetadata(pszDomain))
4461 : {
4462 90 : if (papszMD[0])
4463 : {
4464 90 : bOMDMDSet = true;
4465 90 : l_oMDMD.SetMetadata(papszMD, pszDomain);
4466 : }
4467 : }
4468 5086 : }
4469 : }
4470 12 : else if (CPLTestBool(pszCopySrcMDD) || papszSrcMDD)
4471 : {
4472 9 : char **papszDomainList = poBand->GetMetadataDomainList();
4473 3 : for (const char *pszDomain :
4474 15 : cpl::Iterate(CSLConstList(papszDomainList)))
4475 : {
4476 9 : if (pszDomain[0] != 0 &&
4477 5 : !EQUAL(pszDomain, GDAL_MDD_IMAGE_STRUCTURE) &&
4478 2 : (!papszSrcMDD ||
4479 2 : CSLFindString(papszSrcMDD, pszDomain) >= 0))
4480 : {
4481 2 : bOMDMDSet = true;
4482 2 : l_oMDMD.SetMetadata(poBand->GetMetadata(pszDomain),
4483 : pszDomain);
4484 : }
4485 : }
4486 9 : CSLDestroy(papszDomainList);
4487 : }
4488 :
4489 5098 : if (bOMDMDSet)
4490 : {
4491 92 : WriteMDMetadata(&l_oMDMD, l_hTIFF, &psRoot, &psTail, nBand,
4492 : eProfile);
4493 : }
4494 : }
4495 :
4496 308142 : const double dfOffset = poBand->GetOffset();
4497 308142 : const double dfScale = poBand->GetScale();
4498 308142 : bool bGeoTIFFScaleOffsetInZ = false;
4499 308142 : GDALGeoTransform gt;
4500 : // Check if we have already encoded scale/offset in the GeoTIFF tags
4501 314398 : if (poSrcDS->GetGeoTransform(gt) == CE_None && gt.xrot == 0.0 &&
4502 6240 : gt.yrot == 0.0 && gt.yscale < 0.0 && poSrcDS->GetSpatialRef() &&
4503 314405 : poSrcDS->GetSpatialRef()->IsVertical() &&
4504 7 : poSrcDS->GetRasterCount() == 1)
4505 : {
4506 7 : bGeoTIFFScaleOffsetInZ = true;
4507 : }
4508 :
4509 308142 : if ((dfOffset != 0.0 || dfScale != 1.0) && !bGeoTIFFScaleOffsetInZ)
4510 : {
4511 25 : char szValue[128] = {};
4512 :
4513 25 : CPLsnprintf(szValue, sizeof(szValue), "%.17g", dfOffset);
4514 25 : AppendMetadataItem(&psRoot, &psTail, "OFFSET", szValue, nBand,
4515 : "offset", "");
4516 25 : CPLsnprintf(szValue, sizeof(szValue), "%.17g", dfScale);
4517 25 : AppendMetadataItem(&psRoot, &psTail, "SCALE", szValue, nBand,
4518 : "scale", "");
4519 : }
4520 :
4521 308142 : const char *pszUnitType = poBand->GetUnitType();
4522 308142 : if (pszUnitType != nullptr && pszUnitType[0] != '\0')
4523 : {
4524 40 : bool bWriteUnit = true;
4525 40 : auto poSRS = poSrcDS->GetSpatialRef();
4526 40 : if (poSRS && poSRS->IsCompound())
4527 : {
4528 2 : const char *pszVertUnit = nullptr;
4529 2 : poSRS->GetTargetLinearUnits("COMPD_CS|VERT_CS", &pszVertUnit);
4530 2 : if (pszVertUnit && EQUAL(pszVertUnit, pszUnitType))
4531 : {
4532 2 : bWriteUnit = false;
4533 : }
4534 : }
4535 40 : if (bWriteUnit)
4536 : {
4537 38 : AppendMetadataItem(&psRoot, &psTail, "UNITTYPE", pszUnitType,
4538 : nBand, "unittype", "");
4539 : }
4540 : }
4541 :
4542 308142 : if (strlen(poBand->GetDescription()) > 0)
4543 : {
4544 25 : AppendMetadataItem(&psRoot, &psTail, "DESCRIPTION",
4545 25 : poBand->GetDescription(), nBand, "description",
4546 : "");
4547 : }
4548 :
4549 308359 : if (!bStandardColorInterp &&
4550 217 : !(nBand <= 3 && EQUAL(CSLFetchNameValueDef(papszCreationOptions,
4551 : "PHOTOMETRIC", ""),
4552 : "RGB")))
4553 : {
4554 250 : AppendMetadataItem(&psRoot, &psTail, "COLORINTERP",
4555 : GDALGetColorInterpretationName(
4556 250 : poBand->GetColorInterpretation()),
4557 : nBand, "colorinterp", "");
4558 : }
4559 : }
4560 :
4561 8194 : CSLDestroy(papszSrcMDD);
4562 :
4563 : const char *pszTilingSchemeName =
4564 8194 : CSLFetchNameValue(papszCreationOptions, "@TILING_SCHEME_NAME");
4565 8194 : if (pszTilingSchemeName)
4566 : {
4567 23 : AppendMetadataItem(&psRoot, &psTail, "NAME", pszTilingSchemeName, 0,
4568 : nullptr, "TILING_SCHEME");
4569 :
4570 23 : const char *pszZoomLevel = CSLFetchNameValue(
4571 : papszCreationOptions, "@TILING_SCHEME_ZOOM_LEVEL");
4572 23 : if (pszZoomLevel)
4573 : {
4574 23 : AppendMetadataItem(&psRoot, &psTail, "ZOOM_LEVEL", pszZoomLevel, 0,
4575 : nullptr, "TILING_SCHEME");
4576 : }
4577 :
4578 23 : const char *pszAlignedLevels = CSLFetchNameValue(
4579 : papszCreationOptions, "@TILING_SCHEME_ALIGNED_LEVELS");
4580 23 : if (pszAlignedLevels)
4581 : {
4582 4 : AppendMetadataItem(&psRoot, &psTail, "ALIGNED_LEVELS",
4583 : pszAlignedLevels, 0, nullptr, "TILING_SCHEME");
4584 : }
4585 : }
4586 :
4587 8194 : if (const char *pszOverviewResampling =
4588 8194 : CSLFetchNameValue(papszCreationOptions, "@OVERVIEW_RESAMPLING"))
4589 : {
4590 41 : AppendMetadataItem(&psRoot, &psTail, "OVERVIEW_RESAMPLING",
4591 : pszOverviewResampling, 0, nullptr,
4592 : GDAL_MDD_IMAGE_STRUCTURE);
4593 : }
4594 :
4595 : /* -------------------------------------------------------------------- */
4596 : /* Write information about some codecs. */
4597 : /* -------------------------------------------------------------------- */
4598 8194 : if (CPLTestBool(
4599 : CPLGetConfigOption("GTIFF_WRITE_IMAGE_STRUCTURE_METADATA", "YES")))
4600 : {
4601 : const char *pszTileInterleave =
4602 8189 : CSLFetchNameValue(papszCreationOptions, "@TILE_INTERLEAVE");
4603 8189 : if (pszTileInterleave && CPLTestBool(pszTileInterleave))
4604 : {
4605 7 : AppendMetadataItem(&psRoot, &psTail, GDALMD_INTERLEAVE, "TILE", 0,
4606 : nullptr, GDAL_MDD_IMAGE_STRUCTURE);
4607 : }
4608 :
4609 : const char *pszCompress =
4610 8189 : CSLFetchNameValue(papszCreationOptions, "COMPRESS");
4611 8189 : if (pszCompress && EQUAL(pszCompress, "WEBP"))
4612 : {
4613 31 : if (GTiffGetWebPLossless(papszCreationOptions))
4614 : {
4615 6 : AppendMetadataItem(&psRoot, &psTail,
4616 : "COMPRESSION_REVERSIBILITY", "LOSSLESS", 0,
4617 : nullptr, GDAL_MDD_IMAGE_STRUCTURE);
4618 : }
4619 : else
4620 : {
4621 25 : AppendMetadataItem(
4622 : &psRoot, &psTail, "WEBP_LEVEL",
4623 25 : CPLSPrintf("%d", GTiffGetWebPLevel(papszCreationOptions)),
4624 : 0, nullptr, GDAL_MDD_IMAGE_STRUCTURE);
4625 : }
4626 : }
4627 8158 : else if (pszCompress && STARTS_WITH_CI(pszCompress, "LERC"))
4628 : {
4629 : const double dfMaxZError =
4630 97 : GTiffGetLERCMaxZError(papszCreationOptions);
4631 : const double dfMaxZErrorOverview =
4632 97 : GTiffGetLERCMaxZErrorOverview(papszCreationOptions);
4633 97 : if (dfMaxZError == 0.0 && dfMaxZErrorOverview == 0.0)
4634 : {
4635 83 : AppendMetadataItem(&psRoot, &psTail,
4636 : "COMPRESSION_REVERSIBILITY", "LOSSLESS", 0,
4637 : nullptr, GDAL_MDD_IMAGE_STRUCTURE);
4638 : }
4639 : else
4640 : {
4641 14 : AppendMetadataItem(&psRoot, &psTail, "MAX_Z_ERROR",
4642 : CSLFetchNameValueDef(papszCreationOptions,
4643 : "MAX_Z_ERROR", ""),
4644 : 0, nullptr, GDAL_MDD_IMAGE_STRUCTURE);
4645 14 : if (dfMaxZError != dfMaxZErrorOverview)
4646 : {
4647 3 : AppendMetadataItem(
4648 : &psRoot, &psTail, "MAX_Z_ERROR_OVERVIEW",
4649 : CSLFetchNameValueDef(papszCreationOptions,
4650 : "MAX_Z_ERROR_OVERVIEW", ""),
4651 : 0, nullptr, GDAL_MDD_IMAGE_STRUCTURE);
4652 : }
4653 97 : }
4654 : }
4655 : #if HAVE_JXL
4656 8061 : else if (pszCompress && EQUAL(pszCompress, "JXL"))
4657 : {
4658 101 : float fDistance = 0.0f;
4659 101 : if (GTiffGetJXLLossless(papszCreationOptions))
4660 : {
4661 82 : AppendMetadataItem(&psRoot, &psTail,
4662 : "COMPRESSION_REVERSIBILITY", "LOSSLESS", 0,
4663 : nullptr, GDAL_MDD_IMAGE_STRUCTURE);
4664 : }
4665 : else
4666 : {
4667 19 : fDistance = GTiffGetJXLDistance(papszCreationOptions);
4668 19 : AppendMetadataItem(
4669 : &psRoot, &psTail, "JXL_DISTANCE",
4670 : CPLSPrintf("%f", static_cast<double>(fDistance)), 0,
4671 : nullptr, GDAL_MDD_IMAGE_STRUCTURE);
4672 : }
4673 : const float fAlphaDistance =
4674 101 : GTiffGetJXLAlphaDistance(papszCreationOptions);
4675 101 : if (fAlphaDistance >= 0.0f && fAlphaDistance != fDistance)
4676 : {
4677 2 : AppendMetadataItem(
4678 : &psRoot, &psTail, "JXL_ALPHA_DISTANCE",
4679 : CPLSPrintf("%f", static_cast<double>(fAlphaDistance)), 0,
4680 : nullptr, GDAL_MDD_IMAGE_STRUCTURE);
4681 : }
4682 101 : AppendMetadataItem(
4683 : &psRoot, &psTail, "JXL_EFFORT",
4684 : CPLSPrintf("%d", GTiffGetJXLEffort(papszCreationOptions)), 0,
4685 : nullptr, GDAL_MDD_IMAGE_STRUCTURE);
4686 : }
4687 : #endif
4688 : }
4689 :
4690 8194 : if (!CPLTestBool(CPLGetConfigOption("GTIFF_WRITE_RAT_TO_PAM", "NO")))
4691 : {
4692 316330 : for (int nBand = 1; nBand <= poSrcDS->GetRasterCount(); ++nBand)
4693 : {
4694 308139 : GDALRasterAttributeTable *poRAT = nullptr;
4695 308139 : if (poSrcDSGTiff)
4696 : {
4697 303042 : auto poBand = cpl::down_cast<GTiffRasterBand *>(
4698 : poSrcDSGTiff->GetRasterBand(nBand));
4699 : // Scenario of https://github.com/OSGeo/gdal/issues/13930
4700 : // Do not try to fetch the RAT from auxiliary files if creating
4701 : // a new GeoTIFF file
4702 303042 : if (poBand->m_bRATSet)
4703 106 : poRAT = poBand->GetDefaultRAT();
4704 : }
4705 : else
4706 : {
4707 5097 : poRAT = poSrcDS->GetRasterBand(nBand)->GetDefaultRAT();
4708 : }
4709 308139 : if (poRAT)
4710 : {
4711 23 : auto psSerializedRAT = poRAT->Serialize();
4712 23 : if (psSerializedRAT)
4713 : {
4714 21 : AppendMetadataItem(
4715 : &psRoot, &psTail, DEFAULT_RASTER_ATTRIBUTE_TABLE,
4716 : nullptr, psSerializedRAT, nBand, RAT_ROLE, nullptr);
4717 : }
4718 : }
4719 : }
4720 : }
4721 :
4722 : /* -------------------------------------------------------------------- */
4723 : /* Write out the generic XML metadata if there is any. */
4724 : /* -------------------------------------------------------------------- */
4725 8194 : if (psRoot != nullptr)
4726 : {
4727 766 : bool bRet = true;
4728 :
4729 766 : if (eProfile == GTiffProfile::GDALGEOTIFF)
4730 : {
4731 749 : char *pszXML_MD = CPLSerializeXMLTree(psRoot);
4732 749 : TIFFSetField(l_hTIFF, TIFFTAG_GDAL_METADATA, pszXML_MD);
4733 749 : CPLFree(pszXML_MD);
4734 : }
4735 : else
4736 : {
4737 17 : if (bSrcIsGeoTIFF)
4738 11 : cpl::down_cast<GTiffDataset *>(poSrcDS)->PushMetadataToPam();
4739 : else
4740 6 : bRet = false;
4741 : }
4742 :
4743 766 : CPLDestroyXMLNode(psRoot);
4744 :
4745 766 : return bRet;
4746 : }
4747 :
4748 : // If we have no more metadata but it existed before,
4749 : // remove the GDAL_METADATA tag.
4750 7428 : if (eProfile == GTiffProfile::GDALGEOTIFF)
4751 : {
4752 7404 : char *pszText = nullptr;
4753 7404 : if (TIFFGetField(l_hTIFF, TIFFTAG_GDAL_METADATA, &pszText))
4754 : {
4755 7 : TIFFUnsetField(l_hTIFF, TIFFTAG_GDAL_METADATA);
4756 : }
4757 : }
4758 :
4759 7428 : return true;
4760 : }
4761 :
4762 : /************************************************************************/
4763 : /* PushMetadataToPam() */
4764 : /* */
4765 : /* When producing a strict profile TIFF or if our aggregate */
4766 : /* metadata is too big for a single tiff tag we may end up */
4767 : /* needing to write it via the PAM mechanisms. This method */
4768 : /* copies all the appropriate metadata into the PAM level */
4769 : /* metadata object but with special care to avoid copying */
4770 : /* metadata handled in other ways in TIFF format. */
4771 : /************************************************************************/
4772 :
4773 17 : void GTiffDataset::PushMetadataToPam()
4774 :
4775 : {
4776 17 : if (GetPamFlags() & GPF_DISABLED)
4777 0 : return;
4778 :
4779 17 : const bool bStandardColorInterp = GTIFFIsStandardColorInterpretation(
4780 17 : GDALDataset::ToHandle(this), m_nPhotometric, m_papszCreationOptions);
4781 :
4782 55 : for (int nBand = 0; nBand <= GetRasterCount(); ++nBand)
4783 : {
4784 38 : GDALMultiDomainMetadata *poSrcMDMD = nullptr;
4785 38 : GTiffRasterBand *poBand = nullptr;
4786 :
4787 38 : if (nBand == 0)
4788 : {
4789 17 : poSrcMDMD = &(this->m_oGTiffMDMD);
4790 : }
4791 : else
4792 : {
4793 21 : poBand = cpl::down_cast<GTiffRasterBand *>(GetRasterBand(nBand));
4794 21 : poSrcMDMD = &(poBand->m_oGTiffMDMD);
4795 : }
4796 :
4797 : /* --------------------------------------------------------------------
4798 : */
4799 : /* Loop over the available domains. */
4800 : /* --------------------------------------------------------------------
4801 : */
4802 38 : CSLConstList papszDomainList = poSrcMDMD->GetDomainList();
4803 74 : for (int iDomain = 0; papszDomainList && papszDomainList[iDomain];
4804 : ++iDomain)
4805 : {
4806 36 : char **papszMD = poSrcMDMD->GetMetadata(papszDomainList[iDomain]);
4807 :
4808 36 : if (EQUAL(papszDomainList[iDomain], GDAL_MDD_RPC) ||
4809 36 : EQUAL(papszDomainList[iDomain], GDAL_MDD_IMD) ||
4810 36 : EQUAL(papszDomainList[iDomain], "_temporary_") ||
4811 36 : EQUAL(papszDomainList[iDomain], GDAL_MDD_IMAGE_STRUCTURE) ||
4812 19 : EQUAL(papszDomainList[iDomain], "COLOR_PROFILE"))
4813 17 : continue;
4814 :
4815 19 : papszMD = CSLDuplicate(papszMD);
4816 :
4817 69 : for (int i = CSLCount(papszMD) - 1; i >= 0; --i)
4818 : {
4819 50 : if (STARTS_WITH_CI(papszMD[i], "TIFFTAG_") ||
4820 50 : EQUALN(papszMD[i], GDALMD_AREA_OR_POINT,
4821 : strlen(GDALMD_AREA_OR_POINT)))
4822 4 : papszMD = CSLRemoveStrings(papszMD, i, 1, nullptr);
4823 : }
4824 :
4825 19 : if (!poBand)
4826 10 : GDALPamDataset::SetMetadata(papszMD, papszDomainList[iDomain]);
4827 : else
4828 9 : poBand->GDALPamRasterBand::SetMetadata(
4829 9 : papszMD, papszDomainList[iDomain]);
4830 :
4831 19 : CSLDestroy(papszMD);
4832 : }
4833 :
4834 : /* --------------------------------------------------------------------
4835 : */
4836 : /* Handle some "special domain" stuff. */
4837 : /* --------------------------------------------------------------------
4838 : */
4839 38 : if (poBand != nullptr)
4840 : {
4841 21 : poBand->GDALPamRasterBand::SetOffset(poBand->GetOffset());
4842 21 : poBand->GDALPamRasterBand::SetScale(poBand->GetScale());
4843 21 : poBand->GDALPamRasterBand::SetUnitType(poBand->GetUnitType());
4844 21 : poBand->GDALPamRasterBand::SetDescription(poBand->GetDescription());
4845 21 : if (!bStandardColorInterp)
4846 : {
4847 3 : poBand->GDALPamRasterBand::SetColorInterpretation(
4848 3 : poBand->GetColorInterpretation());
4849 : }
4850 : }
4851 : }
4852 17 : MarkPamDirty();
4853 : }
4854 :
4855 : /************************************************************************/
4856 : /* WriteNoDataValue() */
4857 : /************************************************************************/
4858 :
4859 546 : void GTiffDataset::WriteNoDataValue(TIFF *hTIFF, double dfNoData)
4860 :
4861 : {
4862 1092 : CPLString osVal(GTiffFormatGDALNoDataTagValue(dfNoData));
4863 546 : TIFFSetField(hTIFF, TIFFTAG_GDAL_NODATA, osVal.c_str());
4864 546 : }
4865 :
4866 5 : void GTiffDataset::WriteNoDataValue(TIFF *hTIFF, int64_t nNoData)
4867 :
4868 : {
4869 5 : TIFFSetField(hTIFF, TIFFTAG_GDAL_NODATA,
4870 : CPLSPrintf(CPL_FRMT_GIB, static_cast<GIntBig>(nNoData)));
4871 5 : }
4872 :
4873 5 : void GTiffDataset::WriteNoDataValue(TIFF *hTIFF, uint64_t nNoData)
4874 :
4875 : {
4876 5 : TIFFSetField(hTIFF, TIFFTAG_GDAL_NODATA,
4877 : CPLSPrintf(CPL_FRMT_GUIB, static_cast<GUIntBig>(nNoData)));
4878 5 : }
4879 :
4880 : /************************************************************************/
4881 : /* UnsetNoDataValue() */
4882 : /************************************************************************/
4883 :
4884 16 : void GTiffDataset::UnsetNoDataValue(TIFF *l_hTIFF)
4885 :
4886 : {
4887 16 : TIFFUnsetField(l_hTIFF, TIFFTAG_GDAL_NODATA);
4888 16 : }
4889 :
4890 : /************************************************************************/
4891 : /* SaveICCProfile() */
4892 : /* */
4893 : /* Save ICC Profile or colorimetric data into file */
4894 : /* pDS: */
4895 : /* Dataset that contains the metadata with the ICC or colorimetric */
4896 : /* data. If this argument is specified, all other arguments are */
4897 : /* ignored. Set them to NULL or 0. */
4898 : /* hTIFF: */
4899 : /* Pointer to TIFF handle. Only needed if pDS is NULL or */
4900 : /* pDS->m_hTIFF is NULL. */
4901 : /* papszParamList: */
4902 : /* Options containing the ICC profile or colorimetric metadata. */
4903 : /* Ignored if pDS is not NULL. */
4904 : /* nBitsPerSample: */
4905 : /* Bits per sample. Ignored if pDS is not NULL. */
4906 : /************************************************************************/
4907 :
4908 9938 : void GTiffDataset::SaveICCProfile(GTiffDataset *pDS, TIFF *l_hTIFF,
4909 : CSLConstList papszParamList,
4910 : uint32_t l_nBitsPerSample)
4911 : {
4912 9938 : if ((pDS != nullptr) && (pDS->eAccess != GA_Update))
4913 0 : return;
4914 :
4915 9938 : if (l_hTIFF == nullptr)
4916 : {
4917 2 : if (pDS == nullptr)
4918 0 : return;
4919 :
4920 2 : l_hTIFF = pDS->m_hTIFF;
4921 2 : if (l_hTIFF == nullptr)
4922 0 : return;
4923 : }
4924 :
4925 9938 : if ((papszParamList == nullptr) && (pDS == nullptr))
4926 4951 : return;
4927 :
4928 : const char *pszICCProfile =
4929 : (pDS != nullptr)
4930 4987 : ? pDS->GetMetadataItem("SOURCE_ICC_PROFILE", "COLOR_PROFILE")
4931 4985 : : CSLFetchNameValue(papszParamList, "SOURCE_ICC_PROFILE");
4932 4987 : if (pszICCProfile != nullptr)
4933 : {
4934 8 : char *pEmbedBuffer = CPLStrdup(pszICCProfile);
4935 : int32_t nEmbedLen =
4936 8 : CPLBase64DecodeInPlace(reinterpret_cast<GByte *>(pEmbedBuffer));
4937 :
4938 8 : TIFFSetField(l_hTIFF, TIFFTAG_ICCPROFILE, nEmbedLen, pEmbedBuffer);
4939 :
4940 8 : CPLFree(pEmbedBuffer);
4941 : }
4942 : else
4943 : {
4944 : // Output colorimetric data.
4945 4979 : float pCHR[6] = {}; // Primaries.
4946 4979 : uint16_t pTXR[6] = {}; // Transfer range.
4947 4979 : const char *pszCHRNames[] = {"SOURCE_PRIMARIES_RED",
4948 : "SOURCE_PRIMARIES_GREEN",
4949 : "SOURCE_PRIMARIES_BLUE"};
4950 4979 : const char *pszTXRNames[] = {"TIFFTAG_TRANSFERRANGE_BLACK",
4951 : "TIFFTAG_TRANSFERRANGE_WHITE"};
4952 :
4953 : // Output chromacities.
4954 4979 : bool bOutputCHR = true;
4955 4994 : for (int i = 0; i < 3 && bOutputCHR; ++i)
4956 : {
4957 : const char *pszColorProfile =
4958 : (pDS != nullptr)
4959 4989 : ? pDS->GetMetadataItem(pszCHRNames[i], "COLOR_PROFILE")
4960 4986 : : CSLFetchNameValue(papszParamList, pszCHRNames[i]);
4961 4989 : if (pszColorProfile == nullptr)
4962 : {
4963 4974 : bOutputCHR = false;
4964 4974 : break;
4965 : }
4966 :
4967 : const CPLStringList aosTokens(CSLTokenizeString2(
4968 : pszColorProfile, ",",
4969 : CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
4970 15 : CSLT_STRIPENDSPACES));
4971 :
4972 15 : if (aosTokens.size() != 3)
4973 : {
4974 0 : bOutputCHR = false;
4975 0 : break;
4976 : }
4977 :
4978 60 : for (int j = 0; j < 3; ++j)
4979 : {
4980 45 : float v = static_cast<float>(CPLAtof(aosTokens[j]));
4981 :
4982 45 : if (j == 2)
4983 : {
4984 : // Last term of xyY color must be 1.0.
4985 15 : if (v != 1.0f)
4986 : {
4987 0 : bOutputCHR = false;
4988 0 : break;
4989 : }
4990 : }
4991 : else
4992 : {
4993 30 : pCHR[i * 2 + j] = v;
4994 : }
4995 : }
4996 : }
4997 :
4998 4979 : if (bOutputCHR)
4999 : {
5000 5 : TIFFSetField(l_hTIFF, TIFFTAG_PRIMARYCHROMATICITIES, pCHR);
5001 : }
5002 :
5003 : // Output whitepoint.
5004 : const char *pszSourceWhitePoint =
5005 : (pDS != nullptr)
5006 4979 : ? pDS->GetMetadataItem("SOURCE_WHITEPOINT", "COLOR_PROFILE")
5007 4978 : : CSLFetchNameValue(papszParamList, "SOURCE_WHITEPOINT");
5008 4979 : if (pszSourceWhitePoint != nullptr)
5009 : {
5010 : const CPLStringList aosTokens(CSLTokenizeString2(
5011 : pszSourceWhitePoint, ",",
5012 : CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
5013 10 : CSLT_STRIPENDSPACES));
5014 :
5015 5 : bool bOutputWhitepoint = true;
5016 5 : float pWP[2] = {0.0f, 0.0f}; // Whitepoint
5017 5 : if (aosTokens.size() != 3)
5018 : {
5019 0 : bOutputWhitepoint = false;
5020 : }
5021 : else
5022 : {
5023 20 : for (int j = 0; j < 3; ++j)
5024 : {
5025 15 : const float v = static_cast<float>(CPLAtof(aosTokens[j]));
5026 :
5027 15 : if (j == 2)
5028 : {
5029 : // Last term of xyY color must be 1.0.
5030 5 : if (v != 1.0f)
5031 : {
5032 0 : bOutputWhitepoint = false;
5033 0 : break;
5034 : }
5035 : }
5036 : else
5037 : {
5038 10 : pWP[j] = v;
5039 : }
5040 : }
5041 : }
5042 :
5043 5 : if (bOutputWhitepoint)
5044 : {
5045 5 : TIFFSetField(l_hTIFF, TIFFTAG_WHITEPOINT, pWP);
5046 : }
5047 : }
5048 :
5049 : // Set transfer function metadata.
5050 : char const *pszTFRed =
5051 : (pDS != nullptr)
5052 4979 : ? pDS->GetMetadataItem("TIFFTAG_TRANSFERFUNCTION_RED",
5053 : "COLOR_PROFILE")
5054 4978 : : CSLFetchNameValue(papszParamList,
5055 4979 : "TIFFTAG_TRANSFERFUNCTION_RED");
5056 :
5057 : char const *pszTFGreen =
5058 : (pDS != nullptr)
5059 4979 : ? pDS->GetMetadataItem("TIFFTAG_TRANSFERFUNCTION_GREEN",
5060 : "COLOR_PROFILE")
5061 4978 : : CSLFetchNameValue(papszParamList,
5062 4979 : "TIFFTAG_TRANSFERFUNCTION_GREEN");
5063 :
5064 : char const *pszTFBlue =
5065 : (pDS != nullptr)
5066 4979 : ? pDS->GetMetadataItem("TIFFTAG_TRANSFERFUNCTION_BLUE",
5067 : "COLOR_PROFILE")
5068 4978 : : CSLFetchNameValue(papszParamList,
5069 4979 : "TIFFTAG_TRANSFERFUNCTION_BLUE");
5070 :
5071 4979 : if ((pszTFRed != nullptr) && (pszTFGreen != nullptr) &&
5072 : (pszTFBlue != nullptr))
5073 : {
5074 : // Get length of table.
5075 4 : const int nTransferFunctionLength =
5076 4 : 1 << ((pDS != nullptr) ? pDS->m_nBitsPerSample
5077 : : l_nBitsPerSample);
5078 :
5079 : const CPLStringList aosTokensRed(CSLTokenizeString2(
5080 : pszTFRed, ",",
5081 : CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
5082 8 : CSLT_STRIPENDSPACES));
5083 : const CPLStringList aosTokensGreen(CSLTokenizeString2(
5084 : pszTFGreen, ",",
5085 : CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
5086 8 : CSLT_STRIPENDSPACES));
5087 : const CPLStringList aosTokensBlue(CSLTokenizeString2(
5088 : pszTFBlue, ",",
5089 : CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
5090 8 : CSLT_STRIPENDSPACES));
5091 :
5092 4 : if ((aosTokensRed.size() == nTransferFunctionLength) &&
5093 8 : (aosTokensGreen.size() == nTransferFunctionLength) &&
5094 4 : (aosTokensBlue.size() == nTransferFunctionLength))
5095 : {
5096 : std::vector<uint16_t> anTransferFuncRed(
5097 8 : nTransferFunctionLength);
5098 : std::vector<uint16_t> anTransferFuncGreen(
5099 8 : nTransferFunctionLength);
5100 : std::vector<uint16_t> anTransferFuncBlue(
5101 8 : nTransferFunctionLength);
5102 :
5103 : // Convert our table in string format into int16_t format.
5104 1028 : for (int i = 0; i < nTransferFunctionLength; ++i)
5105 : {
5106 2048 : anTransferFuncRed[i] =
5107 1024 : static_cast<uint16_t>(atoi(aosTokensRed[i]));
5108 2048 : anTransferFuncGreen[i] =
5109 1024 : static_cast<uint16_t>(atoi(aosTokensGreen[i]));
5110 1024 : anTransferFuncBlue[i] =
5111 1024 : static_cast<uint16_t>(atoi(aosTokensBlue[i]));
5112 : }
5113 :
5114 4 : TIFFSetField(
5115 : l_hTIFF, TIFFTAG_TRANSFERFUNCTION, anTransferFuncRed.data(),
5116 : anTransferFuncGreen.data(), anTransferFuncBlue.data());
5117 : }
5118 : }
5119 :
5120 : // Output transfer range.
5121 4979 : bool bOutputTransferRange = true;
5122 4979 : for (int i = 0; (i < 2) && bOutputTransferRange; ++i)
5123 : {
5124 : const char *pszTXRVal =
5125 : (pDS != nullptr)
5126 4979 : ? pDS->GetMetadataItem(pszTXRNames[i], "COLOR_PROFILE")
5127 4978 : : CSLFetchNameValue(papszParamList, pszTXRNames[i]);
5128 4979 : if (pszTXRVal == nullptr)
5129 : {
5130 4979 : bOutputTransferRange = false;
5131 4979 : break;
5132 : }
5133 :
5134 : const CPLStringList aosTokens(CSLTokenizeString2(
5135 : pszTXRVal, ",",
5136 : CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
5137 0 : CSLT_STRIPENDSPACES));
5138 :
5139 0 : if (aosTokens.size() != 3)
5140 : {
5141 0 : bOutputTransferRange = false;
5142 0 : break;
5143 : }
5144 :
5145 0 : for (int j = 0; j < 3; ++j)
5146 : {
5147 0 : pTXR[i + j * 2] = static_cast<uint16_t>(atoi(aosTokens[j]));
5148 : }
5149 : }
5150 :
5151 4979 : if (bOutputTransferRange)
5152 : {
5153 0 : const int TIFFTAG_TRANSFERRANGE = 0x0156;
5154 0 : TIFFSetField(l_hTIFF, TIFFTAG_TRANSFERRANGE, pTXR);
5155 : }
5156 : }
5157 : }
5158 :
5159 17971 : static signed char GTiffGetLZMAPreset(CSLConstList papszOptions)
5160 : {
5161 17971 : int nLZMAPreset = -1;
5162 17971 : const char *pszValue = CSLFetchNameValue(papszOptions, "LZMA_PRESET");
5163 17971 : if (pszValue != nullptr)
5164 : {
5165 20 : nLZMAPreset = atoi(pszValue);
5166 20 : if (!(nLZMAPreset >= 0 && nLZMAPreset <= 9))
5167 : {
5168 0 : CPLError(CE_Warning, CPLE_IllegalArg,
5169 : "LZMA_PRESET=%s value not recognised, ignoring.",
5170 : pszValue);
5171 0 : nLZMAPreset = -1;
5172 : }
5173 : }
5174 17971 : return static_cast<signed char>(nLZMAPreset);
5175 : }
5176 :
5177 17971 : static signed char GTiffGetZSTDPreset(CSLConstList papszOptions)
5178 : {
5179 17971 : int nZSTDLevel = -1;
5180 17971 : const char *pszValue = CSLFetchNameValue(papszOptions, "ZSTD_LEVEL");
5181 17971 : if (pszValue != nullptr)
5182 : {
5183 24 : nZSTDLevel = atoi(pszValue);
5184 24 : if (!(nZSTDLevel >= 1 && nZSTDLevel <= 22))
5185 : {
5186 0 : CPLError(CE_Warning, CPLE_IllegalArg,
5187 : "ZSTD_LEVEL=%s value not recognised, ignoring.", pszValue);
5188 0 : nZSTDLevel = -1;
5189 : }
5190 : }
5191 17971 : return static_cast<signed char>(nZSTDLevel);
5192 : }
5193 :
5194 17971 : static signed char GTiffGetZLevel(CSLConstList papszOptions)
5195 : {
5196 17971 : int nZLevel = -1;
5197 17971 : const char *pszValue = CSLFetchNameValue(papszOptions, "ZLEVEL");
5198 17971 : if (pszValue != nullptr)
5199 : {
5200 44 : nZLevel = atoi(pszValue);
5201 : #ifdef TIFFTAG_DEFLATE_SUBCODEC
5202 44 : constexpr int nMaxLevel = 12;
5203 : #ifndef LIBDEFLATE_SUPPORT
5204 : if (nZLevel > 9 && nZLevel <= nMaxLevel)
5205 : {
5206 : CPLDebug("GTiff",
5207 : "ZLEVEL=%d not supported in a non-libdeflate enabled "
5208 : "libtiff build. Capping to 9",
5209 : nZLevel);
5210 : nZLevel = 9;
5211 : }
5212 : #endif
5213 : #else
5214 : constexpr int nMaxLevel = 9;
5215 : #endif
5216 44 : if (nZLevel < 1 || nZLevel > nMaxLevel)
5217 : {
5218 0 : CPLError(CE_Warning, CPLE_IllegalArg,
5219 : "ZLEVEL=%s value not recognised, ignoring.", pszValue);
5220 0 : nZLevel = -1;
5221 : }
5222 : }
5223 17971 : return static_cast<signed char>(nZLevel);
5224 : }
5225 :
5226 17971 : static signed char GTiffGetJpegQuality(CSLConstList papszOptions)
5227 : {
5228 17971 : int nJpegQuality = -1;
5229 17971 : const char *pszValue = CSLFetchNameValue(papszOptions, "JPEG_QUALITY");
5230 17971 : if (pszValue != nullptr)
5231 : {
5232 1939 : nJpegQuality = atoi(pszValue);
5233 1939 : if (nJpegQuality < 1 || nJpegQuality > 100)
5234 : {
5235 0 : CPLError(CE_Warning, CPLE_IllegalArg,
5236 : "JPEG_QUALITY=%s value not recognised, ignoring.",
5237 : pszValue);
5238 0 : nJpegQuality = -1;
5239 : }
5240 : }
5241 17971 : return static_cast<signed char>(nJpegQuality);
5242 : }
5243 :
5244 17971 : static signed char GTiffGetJpegTablesMode(CSLConstList papszOptions)
5245 : {
5246 17971 : return static_cast<signed char>(atoi(
5247 : CSLFetchNameValueDef(papszOptions, "JPEGTABLESMODE",
5248 17971 : CPLSPrintf("%d", knGTIFFJpegTablesModeDefault))));
5249 : }
5250 :
5251 : /************************************************************************/
5252 : /* GetDiscardLsbOption() */
5253 : /************************************************************************/
5254 :
5255 7974 : static GTiffDataset::MaskOffset *GetDiscardLsbOption(TIFF *hTIFF,
5256 : CSLConstList papszOptions)
5257 : {
5258 7974 : const char *pszBits = CSLFetchNameValue(papszOptions, "DISCARD_LSB");
5259 7974 : if (pszBits == nullptr)
5260 7852 : return nullptr;
5261 :
5262 122 : uint16_t nPhotometric = 0;
5263 122 : TIFFGetFieldDefaulted(hTIFF, TIFFTAG_PHOTOMETRIC, &nPhotometric);
5264 :
5265 122 : uint16_t nBitsPerSample = 0;
5266 122 : if (!TIFFGetField(hTIFF, TIFFTAG_BITSPERSAMPLE, &nBitsPerSample))
5267 0 : nBitsPerSample = 1;
5268 :
5269 122 : uint16_t nSamplesPerPixel = 0;
5270 122 : if (!TIFFGetField(hTIFF, TIFFTAG_SAMPLESPERPIXEL, &nSamplesPerPixel))
5271 0 : nSamplesPerPixel = 1;
5272 :
5273 122 : uint16_t nSampleFormat = 0;
5274 122 : if (!TIFFGetField(hTIFF, TIFFTAG_SAMPLEFORMAT, &nSampleFormat))
5275 0 : nSampleFormat = SAMPLEFORMAT_UINT;
5276 :
5277 122 : if (nPhotometric == PHOTOMETRIC_PALETTE)
5278 : {
5279 1 : CPLError(CE_Warning, CPLE_AppDefined,
5280 : "DISCARD_LSB ignored on a paletted image");
5281 1 : return nullptr;
5282 : }
5283 121 : if (!(nBitsPerSample == 8 || nBitsPerSample == 16 || nBitsPerSample == 32 ||
5284 13 : nBitsPerSample == 64))
5285 : {
5286 1 : CPLError(CE_Warning, CPLE_AppDefined,
5287 : "DISCARD_LSB ignored on non 8, 16, 32 or 64 bits images");
5288 1 : return nullptr;
5289 : }
5290 :
5291 240 : const CPLStringList aosTokens(CSLTokenizeString2(pszBits, ",", 0));
5292 120 : const int nTokens = aosTokens.size();
5293 120 : GTiffDataset::MaskOffset *panMaskOffsetLsb = nullptr;
5294 120 : if (nTokens == 1 || nTokens == nSamplesPerPixel)
5295 : {
5296 : panMaskOffsetLsb = static_cast<GTiffDataset::MaskOffset *>(
5297 119 : CPLCalloc(nSamplesPerPixel, sizeof(GTiffDataset::MaskOffset)));
5298 374 : for (int i = 0; i < nSamplesPerPixel; ++i)
5299 : {
5300 255 : const int nBits = atoi(aosTokens[nTokens == 1 ? 0 : i]);
5301 510 : const int nMaxBits = (nSampleFormat == SAMPLEFORMAT_IEEEFP)
5302 510 : ? ((nBitsPerSample == 16) ? 11 - 1
5303 78 : : (nBitsPerSample == 32) ? 23 - 1
5304 26 : : (nBitsPerSample == 64) ? 53 - 1
5305 : : 0)
5306 203 : : nSampleFormat == SAMPLEFORMAT_INT
5307 203 : ? nBitsPerSample - 2
5308 119 : : nBitsPerSample - 1;
5309 :
5310 255 : if (nBits < 0 || nBits > nMaxBits)
5311 : {
5312 0 : CPLError(
5313 : CE_Warning, CPLE_AppDefined,
5314 : "DISCARD_LSB ignored: values should be in [0,%d] range",
5315 : nMaxBits);
5316 0 : VSIFree(panMaskOffsetLsb);
5317 0 : return nullptr;
5318 : }
5319 255 : panMaskOffsetLsb[i].nMask =
5320 255 : ~((static_cast<uint64_t>(1) << nBits) - 1);
5321 255 : if (nBits > 1)
5322 : {
5323 249 : panMaskOffsetLsb[i].nRoundUpBitTest = static_cast<uint64_t>(1)
5324 249 : << (nBits - 1);
5325 : }
5326 119 : }
5327 : }
5328 : else
5329 : {
5330 1 : CPLError(CE_Warning, CPLE_AppDefined,
5331 : "DISCARD_LSB ignored: wrong number of components");
5332 : }
5333 120 : return panMaskOffsetLsb;
5334 : }
5335 :
5336 7974 : void GTiffDataset::GetDiscardLsbOption(CSLConstList papszOptions)
5337 : {
5338 7974 : m_panMaskOffsetLsb = ::GetDiscardLsbOption(m_hTIFF, papszOptions);
5339 7974 : }
5340 :
5341 : /************************************************************************/
5342 : /* GetProfile() */
5343 : /************************************************************************/
5344 :
5345 18022 : static GTiffProfile GetProfile(const char *pszProfile)
5346 : {
5347 18022 : GTiffProfile eProfile = GTiffProfile::GDALGEOTIFF;
5348 18022 : if (pszProfile != nullptr)
5349 : {
5350 70 : if (EQUAL(pszProfile, szPROFILE_BASELINE))
5351 50 : eProfile = GTiffProfile::BASELINE;
5352 20 : else if (EQUAL(pszProfile, szPROFILE_GeoTIFF))
5353 18 : eProfile = GTiffProfile::GEOTIFF;
5354 2 : else if (!EQUAL(pszProfile, szPROFILE_GDALGeoTIFF))
5355 : {
5356 0 : CPLError(CE_Warning, CPLE_NotSupported,
5357 : "Unsupported value for PROFILE: %s", pszProfile);
5358 : }
5359 : }
5360 18022 : return eProfile;
5361 : }
5362 :
5363 : /************************************************************************/
5364 : /* GTiffCreate() */
5365 : /* */
5366 : /* Shared functionality between GTiffDataset::Create() and */
5367 : /* GTiffCreateCopy() for creating TIFF file based on a set of */
5368 : /* options and a configuration. */
5369 : /************************************************************************/
5370 :
5371 10017 : TIFF *GTiffDataset::CreateLL(const char *pszFilename, int nXSize, int nYSize,
5372 : int l_nBands, GDALDataType eType,
5373 : double dfExtraSpaceForOverviews,
5374 : int nColorTableMultiplier,
5375 : CSLConstList papszParamList, VSILFILE **pfpL,
5376 : CPLString &l_osTmpFilename, bool bCreateCopy,
5377 : bool &bTileInterleavingOut)
5378 :
5379 : {
5380 10017 : bTileInterleavingOut = false;
5381 :
5382 10017 : GTiffOneTimeInit();
5383 :
5384 : /* -------------------------------------------------------------------- */
5385 : /* Blow on a few errors. */
5386 : /* -------------------------------------------------------------------- */
5387 10017 : if (nXSize < 1 || nYSize < 1 || l_nBands < 1)
5388 : {
5389 2 : ReportError(
5390 : pszFilename, CE_Failure, CPLE_AppDefined,
5391 : "Attempt to create %dx%dx%d TIFF file, but width, height and bands "
5392 : "must be positive.",
5393 : nXSize, nYSize, l_nBands);
5394 :
5395 2 : return nullptr;
5396 : }
5397 :
5398 10015 : if (l_nBands > 65535)
5399 : {
5400 1 : ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
5401 : "Attempt to create %dx%dx%d TIFF file, but bands "
5402 : "must be lesser or equal to 65535.",
5403 : nXSize, nYSize, l_nBands);
5404 :
5405 1 : return nullptr;
5406 : }
5407 :
5408 : /* -------------------------------------------------------------------- */
5409 : /* Setup values based on options. */
5410 : /* -------------------------------------------------------------------- */
5411 : const GTiffProfile eProfile =
5412 10014 : GetProfile(CSLFetchNameValue(papszParamList, "PROFILE"));
5413 :
5414 10014 : const bool bTiled = CPLFetchBool(papszParamList, "TILED", false);
5415 :
5416 10014 : int l_nBlockXSize = 0;
5417 10014 : if (const char *pszValue = CSLFetchNameValue(papszParamList, "BLOCKXSIZE"))
5418 : {
5419 474 : l_nBlockXSize = atoi(pszValue);
5420 474 : if (l_nBlockXSize < 0)
5421 : {
5422 0 : ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
5423 : "Invalid value for BLOCKXSIZE");
5424 0 : return nullptr;
5425 : }
5426 474 : if (!bTiled)
5427 : {
5428 9 : ReportError(pszFilename, CE_Warning, CPLE_IllegalArg,
5429 : "BLOCKXSIZE can only be used with TILED=YES");
5430 : }
5431 465 : else if (l_nBlockXSize % 16 != 0)
5432 : {
5433 1 : ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
5434 : "BLOCKXSIZE must be a multiple of 16");
5435 1 : return nullptr;
5436 : }
5437 : }
5438 :
5439 10013 : int l_nBlockYSize = 0;
5440 10013 : if (const char *pszValue = CSLFetchNameValue(papszParamList, "BLOCKYSIZE"))
5441 : {
5442 2585 : l_nBlockYSize = atoi(pszValue);
5443 2585 : if (l_nBlockYSize < 0)
5444 : {
5445 0 : ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
5446 : "Invalid value for BLOCKYSIZE");
5447 0 : return nullptr;
5448 : }
5449 2585 : if (bTiled && (l_nBlockYSize % 16 != 0))
5450 : {
5451 2 : ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
5452 : "BLOCKYSIZE must be a multiple of 16");
5453 2 : return nullptr;
5454 : }
5455 : }
5456 :
5457 10011 : if (bTiled)
5458 : {
5459 824 : if (l_nBlockXSize == 0)
5460 361 : l_nBlockXSize = 256;
5461 :
5462 824 : if (l_nBlockYSize == 0)
5463 361 : l_nBlockYSize = 256;
5464 : }
5465 :
5466 10011 : int nPlanar = 0;
5467 :
5468 : // Hidden @TILE_INTERLEAVE=YES parameter used by the COG driver
5469 10011 : if (bCreateCopy && CPLTestBool(CSLFetchNameValueDef(
5470 : papszParamList, "@TILE_INTERLEAVE", "NO")))
5471 : {
5472 7 : bTileInterleavingOut = true;
5473 7 : nPlanar = PLANARCONFIG_SEPARATE;
5474 : }
5475 : else
5476 : {
5477 10004 : if (const char *pszValue =
5478 10004 : CSLFetchNameValue(papszParamList, GDALMD_INTERLEAVE))
5479 : {
5480 1583 : if (EQUAL(pszValue, "PIXEL"))
5481 : {
5482 409 : nPlanar = PLANARCONFIG_CONTIG;
5483 : }
5484 1174 : else if (EQUAL(pszValue, "BAND"))
5485 : {
5486 1173 : nPlanar = PLANARCONFIG_SEPARATE;
5487 : }
5488 : else
5489 : {
5490 1 : ReportError(
5491 : pszFilename, CE_Failure, CPLE_IllegalArg,
5492 : "INTERLEAVE=%s unsupported, value must be PIXEL or BAND.",
5493 : pszValue);
5494 1 : return nullptr;
5495 : }
5496 : }
5497 : else
5498 : {
5499 8421 : nPlanar = PLANARCONFIG_CONTIG;
5500 : }
5501 : }
5502 :
5503 10010 : int l_nCompression = COMPRESSION_NONE;
5504 10010 : if (const char *pszValue = CSLFetchNameValue(papszParamList, "COMPRESS"))
5505 : {
5506 3363 : l_nCompression = GTIFFGetCompressionMethod(pszValue, "COMPRESS");
5507 3363 : if (l_nCompression < 0)
5508 0 : return nullptr;
5509 : }
5510 :
5511 10010 : constexpr int JPEG_MAX_DIMENSION = 65500; // Defined in jpeglib.h
5512 10010 : constexpr int WEBP_MAX_DIMENSION = 16383;
5513 :
5514 : const struct
5515 : {
5516 : int nCodecID;
5517 : const char *pszCodecName;
5518 : int nMaxDim;
5519 10010 : } asLimitations[] = {
5520 : {COMPRESSION_JPEG, "JPEG", JPEG_MAX_DIMENSION},
5521 : {COMPRESSION_WEBP, "WEBP", WEBP_MAX_DIMENSION},
5522 : };
5523 :
5524 30018 : for (const auto &sLimitation : asLimitations)
5525 : {
5526 20016 : if (l_nCompression == sLimitation.nCodecID && !bTiled &&
5527 2074 : nXSize > sLimitation.nMaxDim)
5528 : {
5529 2 : ReportError(
5530 : pszFilename, CE_Failure, CPLE_IllegalArg,
5531 : "COMPRESS=%s is only compatible with un-tiled images whose "
5532 : "width is lesser or equal to %d pixels. "
5533 : "To overcome this limitation, set the TILED=YES creation "
5534 : "option.",
5535 2 : sLimitation.pszCodecName, sLimitation.nMaxDim);
5536 2 : return nullptr;
5537 : }
5538 20014 : else if (l_nCompression == sLimitation.nCodecID && bTiled &&
5539 52 : l_nBlockXSize > sLimitation.nMaxDim)
5540 : {
5541 2 : ReportError(
5542 : pszFilename, CE_Failure, CPLE_IllegalArg,
5543 : "COMPRESS=%s is only compatible with tiled images whose "
5544 : "BLOCKXSIZE is lesser or equal to %d pixels.",
5545 2 : sLimitation.pszCodecName, sLimitation.nMaxDim);
5546 2 : return nullptr;
5547 : }
5548 20012 : else if (l_nCompression == sLimitation.nCodecID &&
5549 2122 : l_nBlockYSize > sLimitation.nMaxDim)
5550 : {
5551 4 : ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
5552 : "COMPRESS=%s is only compatible with images whose "
5553 : "BLOCKYSIZE is lesser or equal to %d pixels. "
5554 : "To overcome this limitation, set the TILED=YES "
5555 : "creation option",
5556 4 : sLimitation.pszCodecName, sLimitation.nMaxDim);
5557 4 : return nullptr;
5558 : }
5559 : }
5560 :
5561 : /* -------------------------------------------------------------------- */
5562 : /* How many bits per sample? We have a special case if NBITS */
5563 : /* specified for GDT_UInt8, GDT_UInt16, GDT_UInt32. */
5564 : /* -------------------------------------------------------------------- */
5565 10002 : int l_nBitsPerSample = GDALGetDataTypeSizeBits(eType);
5566 10002 : if (CSLFetchNameValue(papszParamList, GDALMD_NBITS) != nullptr)
5567 : {
5568 1758 : int nMinBits = 0;
5569 1758 : int nMaxBits = 0;
5570 : l_nBitsPerSample =
5571 1758 : atoi(CSLFetchNameValue(papszParamList, GDALMD_NBITS));
5572 1758 : if (eType == GDT_UInt8)
5573 : {
5574 527 : nMinBits = 1;
5575 527 : nMaxBits = 8;
5576 : }
5577 1231 : else if (eType == GDT_UInt16)
5578 : {
5579 1213 : nMinBits = 9;
5580 1213 : nMaxBits = 16;
5581 : }
5582 18 : else if (eType == GDT_UInt32)
5583 : {
5584 14 : nMinBits = 17;
5585 14 : nMaxBits = 32;
5586 : }
5587 4 : else if (eType == GDT_Float32)
5588 : {
5589 4 : if (l_nBitsPerSample != 16 && l_nBitsPerSample != 32)
5590 : {
5591 1 : ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
5592 : "Only NBITS=16 is supported for data type Float32");
5593 1 : l_nBitsPerSample = GDALGetDataTypeSizeBits(eType);
5594 : }
5595 : }
5596 : else
5597 : {
5598 0 : ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
5599 : "NBITS is not supported for data type %s",
5600 : GDALGetDataTypeName(eType));
5601 0 : l_nBitsPerSample = GDALGetDataTypeSizeBits(eType);
5602 : }
5603 :
5604 1758 : if (nMinBits != 0)
5605 : {
5606 1754 : if (l_nBitsPerSample < nMinBits)
5607 : {
5608 2 : ReportError(
5609 : pszFilename, CE_Warning, CPLE_AppDefined,
5610 : "NBITS=%d is invalid for data type %s. Using NBITS=%d",
5611 : l_nBitsPerSample, GDALGetDataTypeName(eType), nMinBits);
5612 2 : l_nBitsPerSample = nMinBits;
5613 : }
5614 1752 : else if (l_nBitsPerSample > nMaxBits)
5615 : {
5616 3 : ReportError(
5617 : pszFilename, CE_Warning, CPLE_AppDefined,
5618 : "NBITS=%d is invalid for data type %s. Using NBITS=%d",
5619 : l_nBitsPerSample, GDALGetDataTypeName(eType), nMaxBits);
5620 3 : l_nBitsPerSample = nMaxBits;
5621 : }
5622 : }
5623 : }
5624 :
5625 : #ifdef HAVE_JXL
5626 10002 : if ((l_nCompression == COMPRESSION_JXL ||
5627 106 : l_nCompression == COMPRESSION_JXL_DNG_1_7) &&
5628 105 : eType != GDT_Float16 && eType != GDT_Float32)
5629 : {
5630 : // Reflects tif_jxl's GetJXLDataType()
5631 85 : if (eType != GDT_UInt8 && eType != GDT_UInt16)
5632 : {
5633 1 : ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
5634 : "Data type %s not supported for JXL compression. Only "
5635 : "Byte, UInt16, Float16, Float32 are supported",
5636 : GDALGetDataTypeName(eType));
5637 2 : return nullptr;
5638 : }
5639 :
5640 : const struct
5641 : {
5642 : GDALDataType eDT;
5643 : int nBitsPerSample;
5644 84 : } asSupportedDTBitsPerSample[] = {
5645 : {GDT_UInt8, 8},
5646 : {GDT_UInt16, 16},
5647 : };
5648 :
5649 250 : for (const auto &sSupportedDTBitsPerSample : asSupportedDTBitsPerSample)
5650 : {
5651 167 : if (eType == sSupportedDTBitsPerSample.eDT &&
5652 84 : l_nBitsPerSample != sSupportedDTBitsPerSample.nBitsPerSample)
5653 : {
5654 1 : ReportError(
5655 : pszFilename, CE_Failure, CPLE_NotSupported,
5656 : "Bits per sample=%d not supported for JXL compression. "
5657 : "Only %d is supported for %s data type.",
5658 1 : l_nBitsPerSample, sSupportedDTBitsPerSample.nBitsPerSample,
5659 : GDALGetDataTypeName(eType));
5660 1 : return nullptr;
5661 : }
5662 : }
5663 : }
5664 : #endif
5665 :
5666 10000 : int nPredictor = PREDICTOR_NONE;
5667 10000 : const char *pszPredictor = CSLFetchNameValue(papszParamList, "PREDICTOR");
5668 10000 : if (pszPredictor)
5669 : {
5670 31 : nPredictor = atoi(pszPredictor);
5671 : }
5672 :
5673 10000 : if (nPredictor != PREDICTOR_NONE &&
5674 18 : l_nCompression != COMPRESSION_ADOBE_DEFLATE &&
5675 2 : l_nCompression != COMPRESSION_LZW &&
5676 2 : l_nCompression != COMPRESSION_LZMA &&
5677 : l_nCompression != COMPRESSION_ZSTD)
5678 : {
5679 1 : ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
5680 : "PREDICTOR option is ignored for COMPRESS=%s. "
5681 : "Only valid for DEFLATE, LZW, LZMA or ZSTD",
5682 : CSLFetchNameValueDef(papszParamList, "COMPRESS", "NONE"));
5683 : }
5684 :
5685 : // Do early checks as libtiff will only error out when starting to write.
5686 10029 : else if (nPredictor != PREDICTOR_NONE &&
5687 30 : CPLTestBool(
5688 : CPLGetConfigOption("GDAL_GTIFF_PREDICTOR_CHECKS", "YES")))
5689 : {
5690 : #if (TIFFLIB_VERSION > 20210416) || defined(INTERNAL_LIBTIFF)
5691 : #define HAVE_PREDICTOR_2_FOR_64BIT
5692 : #endif
5693 30 : if (nPredictor == 2)
5694 : {
5695 24 : if (l_nBitsPerSample != 8 && l_nBitsPerSample != 16 &&
5696 : l_nBitsPerSample != 32
5697 : #ifdef HAVE_PREDICTOR_2_FOR_64BIT
5698 2 : && l_nBitsPerSample != 64
5699 : #endif
5700 : )
5701 : {
5702 : #if !defined(HAVE_PREDICTOR_2_FOR_64BIT)
5703 : if (l_nBitsPerSample == 64)
5704 : {
5705 : ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
5706 : "PREDICTOR=2 is supported on 64 bit samples "
5707 : "starting with libtiff > 4.3.0.");
5708 : }
5709 : else
5710 : #endif
5711 : {
5712 2 : const int nBITSHint = (l_nBitsPerSample < 8) ? 8
5713 1 : : (l_nBitsPerSample < 16) ? 16
5714 0 : : (l_nBitsPerSample < 32) ? 32
5715 : : 64;
5716 1 : ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
5717 : #ifdef HAVE_PREDICTOR_2_FOR_64BIT
5718 : "PREDICTOR=2 is only supported with 8/16/32/64 "
5719 : "bit samples. You can specify the NBITS=%d "
5720 : "creation option to promote to the closest "
5721 : "supported bits per sample value.",
5722 : #else
5723 : "PREDICTOR=2 is only supported with 8/16/32 "
5724 : "bit samples. You can specify the NBITS=%d "
5725 : "creation option to promote to the closest "
5726 : "supported bits per sample value.",
5727 : #endif
5728 : nBITSHint);
5729 : }
5730 1 : return nullptr;
5731 : }
5732 : }
5733 6 : else if (nPredictor == 3)
5734 : {
5735 5 : if (eType != GDT_Float16 && eType != GDT_Float32 &&
5736 : eType != GDT_Float64)
5737 : {
5738 1 : ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
5739 : "PREDICTOR=3 is only supported with Float16, "
5740 : "Float32 or Float64.");
5741 1 : return nullptr;
5742 : }
5743 : }
5744 : else
5745 : {
5746 1 : ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
5747 : "PREDICTOR=%s is not supported.", pszPredictor);
5748 1 : return nullptr;
5749 : }
5750 : }
5751 :
5752 9997 : const int l_nZLevel = GTiffGetZLevel(papszParamList);
5753 9997 : const int l_nLZMAPreset = GTiffGetLZMAPreset(papszParamList);
5754 9997 : const int l_nZSTDLevel = GTiffGetZSTDPreset(papszParamList);
5755 9997 : const int l_nWebPLevel = GTiffGetWebPLevel(papszParamList);
5756 9997 : const bool l_bWebPLossless = GTiffGetWebPLossless(papszParamList);
5757 9997 : const int l_nJpegQuality = GTiffGetJpegQuality(papszParamList);
5758 9997 : const int l_nJpegTablesMode = GTiffGetJpegTablesMode(papszParamList);
5759 9997 : const double l_dfMaxZError = GTiffGetLERCMaxZError(papszParamList);
5760 : #if HAVE_JXL
5761 9997 : bool bJXLLosslessSpecified = false;
5762 : const bool l_bJXLLossless =
5763 9997 : GTiffGetJXLLossless(papszParamList, &bJXLLosslessSpecified);
5764 9997 : const uint32_t l_nJXLEffort = GTiffGetJXLEffort(papszParamList);
5765 9997 : bool bJXLDistanceSpecified = false;
5766 : const float l_fJXLDistance =
5767 9997 : GTiffGetJXLDistance(papszParamList, &bJXLDistanceSpecified);
5768 9997 : if (bJXLDistanceSpecified && l_bJXLLossless)
5769 : {
5770 1 : ReportError(pszFilename, CE_Warning, CPLE_AppDefined,
5771 : "JXL_DISTANCE creation option is ignored, given %s "
5772 : "JXL_LOSSLESS=YES",
5773 : bJXLLosslessSpecified ? "(explicit)" : "(implicit)");
5774 : }
5775 9997 : bool bJXLAlphaDistanceSpecified = false;
5776 : const float l_fJXLAlphaDistance =
5777 9997 : GTiffGetJXLAlphaDistance(papszParamList, &bJXLAlphaDistanceSpecified);
5778 9997 : if (bJXLAlphaDistanceSpecified && l_bJXLLossless)
5779 : {
5780 1 : ReportError(pszFilename, CE_Warning, CPLE_AppDefined,
5781 : "JXL_ALPHA_DISTANCE creation option is ignored, given %s "
5782 : "JXL_LOSSLESS=YES",
5783 : bJXLLosslessSpecified ? "(explicit)" : "(implicit)");
5784 : }
5785 : #endif
5786 : /* -------------------------------------------------------------------- */
5787 : /* Streaming related code */
5788 : /* -------------------------------------------------------------------- */
5789 19994 : const CPLString osOriFilename(pszFilename);
5790 19994 : bool bStreaming = strcmp(pszFilename, "/vsistdout/") == 0 ||
5791 9997 : CPLFetchBool(papszParamList, "STREAMABLE_OUTPUT", false);
5792 : #ifdef S_ISFIFO
5793 9997 : if (!bStreaming)
5794 : {
5795 : VSIStatBufL sStat;
5796 9985 : if (VSIStatExL(pszFilename, &sStat,
5797 10864 : VSI_STAT_EXISTS_FLAG | VSI_STAT_NATURE_FLAG) == 0 &&
5798 879 : S_ISFIFO(sStat.st_mode))
5799 : {
5800 0 : bStreaming = true;
5801 : }
5802 : }
5803 : #endif
5804 9997 : if (bStreaming && !EQUAL("NONE", CSLFetchNameValueDef(papszParamList,
5805 : "COMPRESS", "NONE")))
5806 : {
5807 1 : ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
5808 : "Streaming only supported to uncompressed TIFF");
5809 1 : return nullptr;
5810 : }
5811 9996 : if (bStreaming && CPLFetchBool(papszParamList, "SPARSE_OK", false))
5812 : {
5813 1 : ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
5814 : "Streaming not supported with SPARSE_OK");
5815 1 : return nullptr;
5816 : }
5817 : const bool bCopySrcOverviews =
5818 9995 : CPLFetchBool(papszParamList, "COPY_SRC_OVERVIEWS", false);
5819 9995 : if (bStreaming && bCopySrcOverviews)
5820 : {
5821 1 : ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
5822 : "Streaming not supported with COPY_SRC_OVERVIEWS");
5823 1 : return nullptr;
5824 : }
5825 9994 : if (bStreaming)
5826 : {
5827 9 : l_osTmpFilename = VSIMemGenerateHiddenFilename("vsistdout.tif");
5828 9 : pszFilename = l_osTmpFilename.c_str();
5829 : }
5830 :
5831 : /* -------------------------------------------------------------------- */
5832 : /* Compute the uncompressed size. */
5833 : /* -------------------------------------------------------------------- */
5834 9994 : const unsigned nTileXCount =
5835 9994 : bTiled ? DIV_ROUND_UP(nXSize, l_nBlockXSize) : 0;
5836 9994 : const unsigned nTileYCount =
5837 9994 : bTiled ? DIV_ROUND_UP(nYSize, l_nBlockYSize) : 0;
5838 : const double dfUncompressedImageSize =
5839 9994 : (bTiled ? (static_cast<double>(nTileXCount) * nTileYCount *
5840 820 : l_nBlockXSize * l_nBlockYSize)
5841 9174 : : (nXSize * static_cast<double>(nYSize))) *
5842 9994 : l_nBands * GDALGetDataTypeSizeBytes(eType) +
5843 9994 : dfExtraSpaceForOverviews;
5844 :
5845 : /* -------------------------------------------------------------------- */
5846 : /* Should the file be created as a bigtiff file? */
5847 : /* -------------------------------------------------------------------- */
5848 9994 : const char *pszBIGTIFF = CSLFetchNameValue(papszParamList, "BIGTIFF");
5849 :
5850 9994 : if (pszBIGTIFF == nullptr)
5851 9511 : pszBIGTIFF = "IF_NEEDED";
5852 :
5853 9994 : bool bCreateBigTIFF = false;
5854 9994 : if (EQUAL(pszBIGTIFF, "IF_NEEDED"))
5855 : {
5856 9512 : if (l_nCompression == COMPRESSION_NONE &&
5857 : dfUncompressedImageSize > 4200000000.0)
5858 17 : bCreateBigTIFF = true;
5859 : }
5860 482 : else if (EQUAL(pszBIGTIFF, "IF_SAFER"))
5861 : {
5862 425 : if (dfUncompressedImageSize > 2000000000.0)
5863 1 : bCreateBigTIFF = true;
5864 : }
5865 : else
5866 : {
5867 57 : bCreateBigTIFF = CPLTestBool(pszBIGTIFF);
5868 57 : if (!bCreateBigTIFF && l_nCompression == COMPRESSION_NONE &&
5869 : dfUncompressedImageSize > 4200000000.0)
5870 : {
5871 2 : ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
5872 : "The TIFF file will be larger than 4GB, so BigTIFF is "
5873 : "necessary. Creation failed.");
5874 2 : return nullptr;
5875 : }
5876 : }
5877 :
5878 9992 : if (bCreateBigTIFF)
5879 71 : CPLDebug("GTiff", "File being created as a BigTIFF.");
5880 :
5881 : /* -------------------------------------------------------------------- */
5882 : /* Sanity check. */
5883 : /* -------------------------------------------------------------------- */
5884 9992 : if (bTiled)
5885 : {
5886 : // libtiff implementation limitation
5887 820 : if (nTileXCount > 0x80000000U / (bCreateBigTIFF ? 8 : 4) / nTileYCount)
5888 : {
5889 3 : ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
5890 : "File too large regarding tile size. This would result "
5891 : "in a file with tile arrays larger than 2GB");
5892 3 : return nullptr;
5893 : }
5894 : }
5895 :
5896 : /* -------------------------------------------------------------------- */
5897 : /* Check free space (only for big, non sparse) */
5898 : /* -------------------------------------------------------------------- */
5899 9989 : const double dfLikelyFloorOfFinalSize =
5900 : l_nCompression == COMPRESSION_NONE
5901 9989 : ? dfUncompressedImageSize
5902 : :
5903 : /* For compressed, we target 1% as the most optimistic reduction factor! */
5904 : 0.01 * dfUncompressedImageSize;
5905 10011 : if (dfLikelyFloorOfFinalSize >= 1e9 &&
5906 22 : !CPLFetchBool(papszParamList, "SPARSE_OK", false) &&
5907 5 : osOriFilename != "/vsistdout/" &&
5908 10016 : osOriFilename != "/vsistdout_redirect/" &&
5909 5 : CPLTestBool(CPLGetConfigOption("CHECK_DISK_FREE_SPACE", "TRUE")))
5910 : {
5911 : const GIntBig nFreeDiskSpace =
5912 4 : VSIGetDiskFreeSpace(CPLGetDirnameSafe(pszFilename).c_str());
5913 4 : if (nFreeDiskSpace >= 0 && nFreeDiskSpace < dfLikelyFloorOfFinalSize)
5914 : {
5915 6 : ReportError(
5916 : pszFilename, CE_Failure, CPLE_FileIO,
5917 : "Free disk space available is %s, "
5918 : "whereas %s are %s necessary. "
5919 : "You can disable this check by defining the "
5920 : "CHECK_DISK_FREE_SPACE configuration option to FALSE.",
5921 4 : CPLFormatReadableFileSize(static_cast<uint64_t>(nFreeDiskSpace))
5922 : .c_str(),
5923 4 : CPLFormatReadableFileSize(dfLikelyFloorOfFinalSize).c_str(),
5924 : l_nCompression == COMPRESSION_NONE
5925 : ? "at least"
5926 : : "likely at least (probably more)");
5927 2 : return nullptr;
5928 : }
5929 : }
5930 :
5931 : /* -------------------------------------------------------------------- */
5932 : /* Check if the user wishes a particular endianness */
5933 : /* -------------------------------------------------------------------- */
5934 :
5935 9987 : int eEndianness = ENDIANNESS_NATIVE;
5936 9987 : const char *pszEndianness = CSLFetchNameValue(papszParamList, "ENDIANNESS");
5937 9987 : if (pszEndianness == nullptr)
5938 9924 : pszEndianness = CPLGetConfigOption("GDAL_TIFF_ENDIANNESS", nullptr);
5939 9987 : if (pszEndianness != nullptr)
5940 : {
5941 123 : if (EQUAL(pszEndianness, "LITTLE"))
5942 : {
5943 36 : eEndianness = ENDIANNESS_LITTLE;
5944 : }
5945 87 : else if (EQUAL(pszEndianness, "BIG"))
5946 : {
5947 1 : eEndianness = ENDIANNESS_BIG;
5948 : }
5949 86 : else if (EQUAL(pszEndianness, "INVERTED"))
5950 : {
5951 : #ifdef CPL_LSB
5952 82 : eEndianness = ENDIANNESS_BIG;
5953 : #else
5954 : eEndianness = ENDIANNESS_LITTLE;
5955 : #endif
5956 : }
5957 4 : else if (!EQUAL(pszEndianness, "NATIVE"))
5958 : {
5959 1 : ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
5960 : "ENDIANNESS=%s not supported. Defaulting to NATIVE",
5961 : pszEndianness);
5962 : }
5963 : }
5964 :
5965 : /* -------------------------------------------------------------------- */
5966 : /* Try opening the dataset. */
5967 : /* -------------------------------------------------------------------- */
5968 :
5969 : const bool bAppend =
5970 9987 : CPLFetchBool(papszParamList, "APPEND_SUBDATASET", false);
5971 :
5972 9987 : char szOpeningFlag[5] = {};
5973 9987 : strcpy(szOpeningFlag, bAppend ? "r+" : "w+");
5974 9987 : if (bCreateBigTIFF)
5975 68 : strcat(szOpeningFlag, "8");
5976 9987 : if (eEndianness == ENDIANNESS_BIG)
5977 83 : strcat(szOpeningFlag, "b");
5978 9904 : else if (eEndianness == ENDIANNESS_LITTLE)
5979 36 : strcat(szOpeningFlag, "l");
5980 :
5981 9987 : VSIErrorReset();
5982 9987 : const bool bOnlyVisibleAtCloseTime = CPLTestBool(CSLFetchNameValueDef(
5983 : papszParamList, "@CREATE_ONLY_VISIBLE_AT_CLOSE_TIME", "NO"));
5984 9987 : const bool bSuppressASAP = CPLTestBool(
5985 : CSLFetchNameValueDef(papszParamList, "@SUPPRESS_ASAP", "NO"));
5986 : auto l_fpL =
5987 9987 : (bOnlyVisibleAtCloseTime || bSuppressASAP) && !bAppend
5988 10057 : ? VSIFileManager::GetHandler(pszFilename)
5989 140 : ->CreateOnlyVisibleAtCloseTime(pszFilename, true, nullptr)
5990 70 : .release()
5991 19904 : : VSIFilesystemHandler::OpenStatic(pszFilename,
5992 : bAppend ? "r+b" : "w+b", true)
5993 9987 : .release();
5994 9987 : if (l_fpL == nullptr)
5995 : {
5996 21 : VSIToCPLErrorWithMsg(CE_Failure, CPLE_OpenFailed,
5997 42 : std::string("Attempt to create new tiff file `")
5998 21 : .append(pszFilename)
5999 21 : .append("' failed")
6000 : .c_str());
6001 21 : return nullptr;
6002 : }
6003 :
6004 9966 : if (bSuppressASAP)
6005 : {
6006 40 : l_fpL->CancelCreation();
6007 : }
6008 :
6009 9966 : TIFF *l_hTIFF = VSI_TIFFOpen(pszFilename, szOpeningFlag, l_fpL);
6010 9966 : if (l_hTIFF == nullptr)
6011 : {
6012 2 : if (CPLGetLastErrorNo() == 0)
6013 0 : CPLError(CE_Failure, CPLE_OpenFailed,
6014 : "Attempt to create new tiff file `%s' "
6015 : "failed in XTIFFOpen().",
6016 : pszFilename);
6017 2 : l_fpL->CancelCreation();
6018 2 : CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
6019 2 : return nullptr;
6020 : }
6021 :
6022 9964 : if (bAppend)
6023 : {
6024 : #if !(defined(INTERNAL_LIBTIFF) || TIFFLIB_VERSION > 20240911)
6025 : // This is a bit of a hack to cause (*tif->tif_cleanup)(tif); to be
6026 : // called. See https://trac.osgeo.org/gdal/ticket/2055
6027 : // Fixed in libtiff > 4.7.0
6028 : TIFFSetField(l_hTIFF, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
6029 : TIFFFreeDirectory(l_hTIFF);
6030 : #endif
6031 6 : TIFFCreateDirectory(l_hTIFF);
6032 : }
6033 :
6034 : /* -------------------------------------------------------------------- */
6035 : /* Do we have a custom pixel type (just used for signed byte now). */
6036 : /* -------------------------------------------------------------------- */
6037 9964 : const char *pszPixelType = CSLFetchNameValue(papszParamList, "PIXELTYPE");
6038 9964 : if (pszPixelType == nullptr)
6039 9956 : pszPixelType = "";
6040 9964 : if (eType == GDT_UInt8 && EQUAL(pszPixelType, "SIGNEDBYTE"))
6041 : {
6042 8 : CPLError(CE_Warning, CPLE_AppDefined,
6043 : "Using PIXELTYPE=SIGNEDBYTE with Byte data type is deprecated "
6044 : "(but still works). "
6045 : "Using Int8 data type instead is now recommended.");
6046 : }
6047 :
6048 : /* -------------------------------------------------------------------- */
6049 : /* Setup some standard flags. */
6050 : /* -------------------------------------------------------------------- */
6051 9964 : TIFFSetField(l_hTIFF, TIFFTAG_IMAGEWIDTH, nXSize);
6052 9964 : TIFFSetField(l_hTIFF, TIFFTAG_IMAGELENGTH, nYSize);
6053 9964 : TIFFSetField(l_hTIFF, TIFFTAG_BITSPERSAMPLE, l_nBitsPerSample);
6054 :
6055 9964 : uint16_t l_nSampleFormat = 0;
6056 9964 : if ((eType == GDT_UInt8 && EQUAL(pszPixelType, "SIGNEDBYTE")) ||
6057 9815 : eType == GDT_Int8 || eType == GDT_Int16 || eType == GDT_Int32 ||
6058 : eType == GDT_Int64)
6059 808 : l_nSampleFormat = SAMPLEFORMAT_INT;
6060 9156 : else if (eType == GDT_CInt16 || eType == GDT_CInt32)
6061 363 : l_nSampleFormat = SAMPLEFORMAT_COMPLEXINT;
6062 8793 : else if (eType == GDT_Float16 || eType == GDT_Float32 ||
6063 : eType == GDT_Float64)
6064 1173 : l_nSampleFormat = SAMPLEFORMAT_IEEEFP;
6065 7620 : else if (eType == GDT_CFloat16 || eType == GDT_CFloat32 ||
6066 : eType == GDT_CFloat64)
6067 471 : l_nSampleFormat = SAMPLEFORMAT_COMPLEXIEEEFP;
6068 : else
6069 7149 : l_nSampleFormat = SAMPLEFORMAT_UINT;
6070 :
6071 9964 : TIFFSetField(l_hTIFF, TIFFTAG_SAMPLEFORMAT, l_nSampleFormat);
6072 9964 : TIFFSetField(l_hTIFF, TIFFTAG_SAMPLESPERPIXEL, l_nBands);
6073 9964 : TIFFSetField(l_hTIFF, TIFFTAG_PLANARCONFIG, nPlanar);
6074 :
6075 : /* -------------------------------------------------------------------- */
6076 : /* Setup Photometric Interpretation. Take this value from the user */
6077 : /* passed option or guess correct value otherwise. */
6078 : /* -------------------------------------------------------------------- */
6079 9964 : int nSamplesAccountedFor = 1;
6080 9964 : bool bForceColorTable = false;
6081 :
6082 9964 : if (const char *pszValue = CSLFetchNameValue(papszParamList, "PHOTOMETRIC"))
6083 : {
6084 1914 : if (EQUAL(pszValue, "MINISBLACK"))
6085 14 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
6086 1900 : else if (EQUAL(pszValue, "MINISWHITE"))
6087 : {
6088 2 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
6089 : }
6090 1898 : else if (EQUAL(pszValue, "PALETTE"))
6091 : {
6092 5 : if (eType == GDT_UInt8 || eType == GDT_UInt16)
6093 : {
6094 4 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
6095 4 : nSamplesAccountedFor = 1;
6096 4 : bForceColorTable = true;
6097 : }
6098 : else
6099 : {
6100 1 : ReportError(
6101 : pszFilename, CE_Warning, CPLE_AppDefined,
6102 : "PHOTOMETRIC=PALETTE only compatible with Byte or UInt16");
6103 : }
6104 : }
6105 1893 : else if (EQUAL(pszValue, "RGB"))
6106 : {
6107 1153 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
6108 1153 : nSamplesAccountedFor = 3;
6109 : }
6110 740 : else if (EQUAL(pszValue, "CMYK"))
6111 : {
6112 10 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_SEPARATED);
6113 10 : nSamplesAccountedFor = 4;
6114 : }
6115 730 : else if (EQUAL(pszValue, "YCBCR"))
6116 : {
6117 : // Because of subsampling, setting YCBCR without JPEG compression
6118 : // leads to a crash currently. Would need to make
6119 : // GTiffRasterBand::IWriteBlock() aware of subsampling so that it
6120 : // doesn't overrun buffer size returned by libtiff.
6121 729 : if (l_nCompression != COMPRESSION_JPEG)
6122 : {
6123 1 : ReportError(
6124 : pszFilename, CE_Failure, CPLE_NotSupported,
6125 : "Currently, PHOTOMETRIC=YCBCR requires COMPRESS=JPEG");
6126 1 : XTIFFClose(l_hTIFF);
6127 1 : l_fpL->CancelCreation();
6128 1 : CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
6129 1 : return nullptr;
6130 : }
6131 :
6132 728 : if (nPlanar == PLANARCONFIG_SEPARATE)
6133 : {
6134 1 : ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
6135 : "PHOTOMETRIC=YCBCR requires INTERLEAVE=PIXEL");
6136 1 : XTIFFClose(l_hTIFF);
6137 1 : l_fpL->CancelCreation();
6138 1 : CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
6139 1 : return nullptr;
6140 : }
6141 :
6142 : // YCBCR strictly requires 3 bands. Not less, not more Issue an
6143 : // explicit error message as libtiff one is a bit cryptic:
6144 : // TIFFVStripSize64:Invalid td_samplesperpixel value.
6145 727 : if (l_nBands != 3)
6146 : {
6147 1 : ReportError(
6148 : pszFilename, CE_Failure, CPLE_NotSupported,
6149 : "PHOTOMETRIC=YCBCR not supported on a %d-band raster: "
6150 : "only compatible with 3-band (RGB) rasters",
6151 : l_nBands);
6152 1 : XTIFFClose(l_hTIFF);
6153 1 : l_fpL->CancelCreation();
6154 1 : CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
6155 1 : return nullptr;
6156 : }
6157 :
6158 726 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);
6159 726 : nSamplesAccountedFor = 3;
6160 :
6161 : // Explicitly register the subsampling so that JPEGFixupTags
6162 : // is a no-op (helps for cloud optimized geotiffs)
6163 726 : TIFFSetField(l_hTIFF, TIFFTAG_YCBCRSUBSAMPLING, 2, 2);
6164 : }
6165 1 : else if (EQUAL(pszValue, "CIELAB"))
6166 : {
6167 0 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_CIELAB);
6168 0 : nSamplesAccountedFor = 3;
6169 : }
6170 1 : else if (EQUAL(pszValue, "ICCLAB"))
6171 : {
6172 0 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_ICCLAB);
6173 0 : nSamplesAccountedFor = 3;
6174 : }
6175 1 : else if (EQUAL(pszValue, "ITULAB"))
6176 : {
6177 0 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_ITULAB);
6178 0 : nSamplesAccountedFor = 3;
6179 : }
6180 : else
6181 : {
6182 1 : ReportError(pszFilename, CE_Warning, CPLE_IllegalArg,
6183 : "PHOTOMETRIC=%s value not recognised, ignoring. "
6184 : "Set the Photometric Interpretation as MINISBLACK.",
6185 : pszValue);
6186 1 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
6187 : }
6188 :
6189 1911 : if (l_nBands < nSamplesAccountedFor)
6190 : {
6191 1 : ReportError(pszFilename, CE_Warning, CPLE_IllegalArg,
6192 : "PHOTOMETRIC=%s value does not correspond to number "
6193 : "of bands (%d), ignoring. "
6194 : "Set the Photometric Interpretation as MINISBLACK.",
6195 : pszValue, l_nBands);
6196 1 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
6197 : }
6198 : }
6199 : else
6200 : {
6201 : // If image contains 3 or 4 bands and datatype is Byte then we will
6202 : // assume it is RGB. In all other cases assume it is MINISBLACK.
6203 8050 : if (l_nBands == 3 && eType == GDT_UInt8)
6204 : {
6205 323 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
6206 323 : nSamplesAccountedFor = 3;
6207 : }
6208 7727 : else if (l_nBands == 4 && eType == GDT_UInt8)
6209 : {
6210 : uint16_t v[1] = {
6211 723 : GTiffGetAlphaValue(CSLFetchNameValue(papszParamList, "ALPHA"),
6212 723 : DEFAULT_ALPHA_TYPE)};
6213 :
6214 723 : TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, 1, v);
6215 723 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
6216 723 : nSamplesAccountedFor = 4;
6217 : }
6218 : else
6219 : {
6220 7004 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
6221 7004 : nSamplesAccountedFor = 1;
6222 : }
6223 : }
6224 :
6225 : /* -------------------------------------------------------------------- */
6226 : /* If there are extra samples, we need to mark them with an */
6227 : /* appropriate extrasamples definition here. */
6228 : /* -------------------------------------------------------------------- */
6229 9961 : if (l_nBands > nSamplesAccountedFor)
6230 : {
6231 1404 : const int nExtraSamples = l_nBands - nSamplesAccountedFor;
6232 :
6233 : uint16_t *v = static_cast<uint16_t *>(
6234 1404 : CPLMalloc(sizeof(uint16_t) * nExtraSamples));
6235 :
6236 1404 : v[0] = GTiffGetAlphaValue(CSLFetchNameValue(papszParamList, "ALPHA"),
6237 : EXTRASAMPLE_UNSPECIFIED);
6238 :
6239 297715 : for (int i = 1; i < nExtraSamples; ++i)
6240 296311 : v[i] = EXTRASAMPLE_UNSPECIFIED;
6241 :
6242 1404 : TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, nExtraSamples, v);
6243 :
6244 1404 : CPLFree(v);
6245 : }
6246 :
6247 : // Set the ICC color profile.
6248 9961 : if (eProfile != GTiffProfile::BASELINE)
6249 : {
6250 9936 : SaveICCProfile(nullptr, l_hTIFF, papszParamList, l_nBitsPerSample);
6251 : }
6252 :
6253 : // Set the compression method before asking the default strip size
6254 : // This is useful when translating to a JPEG-In-TIFF file where
6255 : // the default strip size is 8 or 16 depending on the photometric value.
6256 9961 : TIFFSetField(l_hTIFF, TIFFTAG_COMPRESSION, l_nCompression);
6257 :
6258 9961 : if (l_nCompression == COMPRESSION_LERC)
6259 : {
6260 : const char *pszCompress =
6261 97 : CSLFetchNameValueDef(papszParamList, "COMPRESS", "");
6262 97 : if (EQUAL(pszCompress, "LERC_DEFLATE"))
6263 : {
6264 16 : TIFFSetField(l_hTIFF, TIFFTAG_LERC_ADD_COMPRESSION,
6265 : LERC_ADD_COMPRESSION_DEFLATE);
6266 : }
6267 81 : else if (EQUAL(pszCompress, "LERC_ZSTD"))
6268 : {
6269 14 : if (TIFFSetField(l_hTIFF, TIFFTAG_LERC_ADD_COMPRESSION,
6270 14 : LERC_ADD_COMPRESSION_ZSTD) != 1)
6271 : {
6272 0 : XTIFFClose(l_hTIFF);
6273 0 : l_fpL->CancelCreation();
6274 0 : CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
6275 0 : return nullptr;
6276 : }
6277 : }
6278 : }
6279 : // TODO later: take into account LERC version
6280 :
6281 : /* -------------------------------------------------------------------- */
6282 : /* Setup tiling/stripping flags. */
6283 : /* -------------------------------------------------------------------- */
6284 9961 : if (bTiled)
6285 : {
6286 1620 : if (!TIFFSetField(l_hTIFF, TIFFTAG_TILEWIDTH, l_nBlockXSize) ||
6287 810 : !TIFFSetField(l_hTIFF, TIFFTAG_TILELENGTH, l_nBlockYSize))
6288 : {
6289 0 : XTIFFClose(l_hTIFF);
6290 0 : l_fpL->CancelCreation();
6291 0 : CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
6292 0 : return nullptr;
6293 : }
6294 : }
6295 : else
6296 : {
6297 9151 : const uint32_t l_nRowsPerStrip = std::min(
6298 : nYSize, l_nBlockYSize == 0
6299 9151 : ? static_cast<int>(TIFFDefaultStripSize(l_hTIFF, 0))
6300 9151 : : l_nBlockYSize);
6301 :
6302 9151 : TIFFSetField(l_hTIFF, TIFFTAG_ROWSPERSTRIP, l_nRowsPerStrip);
6303 : }
6304 :
6305 : /* -------------------------------------------------------------------- */
6306 : /* Set compression related tags. */
6307 : /* -------------------------------------------------------------------- */
6308 9961 : if (GTIFFSupportsPredictor(l_nCompression))
6309 982 : TIFFSetField(l_hTIFF, TIFFTAG_PREDICTOR, nPredictor);
6310 9961 : if (l_nCompression == COMPRESSION_ADOBE_DEFLATE ||
6311 : l_nCompression == COMPRESSION_LERC)
6312 : {
6313 281 : GTiffSetDeflateSubCodec(l_hTIFF);
6314 :
6315 281 : if (l_nZLevel != -1)
6316 22 : TIFFSetField(l_hTIFF, TIFFTAG_ZIPQUALITY, l_nZLevel);
6317 : }
6318 9961 : if (l_nCompression == COMPRESSION_JPEG && l_nJpegQuality != -1)
6319 1905 : TIFFSetField(l_hTIFF, TIFFTAG_JPEGQUALITY, l_nJpegQuality);
6320 9961 : if (l_nCompression == COMPRESSION_LZMA && l_nLZMAPreset != -1)
6321 10 : TIFFSetField(l_hTIFF, TIFFTAG_LZMAPRESET, l_nLZMAPreset);
6322 9961 : if ((l_nCompression == COMPRESSION_ZSTD ||
6323 201 : l_nCompression == COMPRESSION_LERC) &&
6324 : l_nZSTDLevel != -1)
6325 12 : TIFFSetField(l_hTIFF, TIFFTAG_ZSTD_LEVEL, l_nZSTDLevel);
6326 9961 : if (l_nCompression == COMPRESSION_LERC)
6327 : {
6328 97 : TIFFSetField(l_hTIFF, TIFFTAG_LERC_MAXZERROR, l_dfMaxZError);
6329 : }
6330 : #if HAVE_JXL
6331 9961 : if (l_nCompression == COMPRESSION_JXL ||
6332 : l_nCompression == COMPRESSION_JXL_DNG_1_7)
6333 : {
6334 104 : TIFFSetField(l_hTIFF, TIFFTAG_JXL_LOSSYNESS,
6335 : l_bJXLLossless ? JXL_LOSSLESS : JXL_LOSSY);
6336 104 : TIFFSetField(l_hTIFF, TIFFTAG_JXL_EFFORT, l_nJXLEffort);
6337 104 : TIFFSetField(l_hTIFF, TIFFTAG_JXL_DISTANCE,
6338 : static_cast<double>(l_fJXLDistance));
6339 104 : TIFFSetField(l_hTIFF, TIFFTAG_JXL_ALPHA_DISTANCE,
6340 : static_cast<double>(l_fJXLAlphaDistance));
6341 : }
6342 : #endif
6343 9961 : if (l_nCompression == COMPRESSION_WEBP)
6344 33 : TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LEVEL, l_nWebPLevel);
6345 9961 : if (l_nCompression == COMPRESSION_WEBP && l_bWebPLossless)
6346 7 : TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LOSSLESS, 1);
6347 :
6348 9961 : if (l_nCompression == COMPRESSION_JPEG)
6349 2083 : TIFFSetField(l_hTIFF, TIFFTAG_JPEGTABLESMODE, l_nJpegTablesMode);
6350 :
6351 : /* -------------------------------------------------------------------- */
6352 : /* If we forced production of a file with photometric=palette, */
6353 : /* we need to push out a default color table. */
6354 : /* -------------------------------------------------------------------- */
6355 9961 : if (bForceColorTable)
6356 : {
6357 4 : const int nColors = eType == GDT_UInt8 ? 256 : 65536;
6358 :
6359 : unsigned short *panTRed = static_cast<unsigned short *>(
6360 4 : CPLMalloc(sizeof(unsigned short) * nColors));
6361 : unsigned short *panTGreen = static_cast<unsigned short *>(
6362 4 : CPLMalloc(sizeof(unsigned short) * nColors));
6363 : unsigned short *panTBlue = static_cast<unsigned short *>(
6364 4 : CPLMalloc(sizeof(unsigned short) * nColors));
6365 :
6366 1028 : for (int iColor = 0; iColor < nColors; ++iColor)
6367 : {
6368 1024 : if (eType == GDT_UInt8)
6369 : {
6370 1024 : panTRed[iColor] = GTiffDataset::ClampCTEntry(
6371 : iColor, 1, iColor, nColorTableMultiplier);
6372 1024 : panTGreen[iColor] = GTiffDataset::ClampCTEntry(
6373 : iColor, 2, iColor, nColorTableMultiplier);
6374 1024 : panTBlue[iColor] = GTiffDataset::ClampCTEntry(
6375 : iColor, 3, iColor, nColorTableMultiplier);
6376 : }
6377 : else
6378 : {
6379 0 : panTRed[iColor] = static_cast<unsigned short>(iColor);
6380 0 : panTGreen[iColor] = static_cast<unsigned short>(iColor);
6381 0 : panTBlue[iColor] = static_cast<unsigned short>(iColor);
6382 : }
6383 : }
6384 :
6385 4 : TIFFSetField(l_hTIFF, TIFFTAG_COLORMAP, panTRed, panTGreen, panTBlue);
6386 :
6387 4 : CPLFree(panTRed);
6388 4 : CPLFree(panTGreen);
6389 4 : CPLFree(panTBlue);
6390 : }
6391 :
6392 : // This trick
6393 : // creates a temporary in-memory file and fetches its JPEG tables so that
6394 : // we can directly set them, before tif_jpeg.c compute them at the first
6395 : // strip/tile writing, which is too late, since we have already crystalized
6396 : // the directory. This way we avoid a directory rewriting.
6397 12044 : if (l_nCompression == COMPRESSION_JPEG &&
6398 2083 : CPLTestBool(
6399 : CSLFetchNameValueDef(papszParamList, "WRITE_JPEGTABLE_TAG", "YES")))
6400 : {
6401 1014 : GTiffWriteJPEGTables(
6402 : l_hTIFF, CSLFetchNameValue(papszParamList, "PHOTOMETRIC"),
6403 : CSLFetchNameValue(papszParamList, "JPEG_QUALITY"),
6404 : CSLFetchNameValue(papszParamList, "JPEGTABLESMODE"));
6405 : }
6406 :
6407 9961 : *pfpL = l_fpL;
6408 :
6409 9961 : return l_hTIFF;
6410 : }
6411 :
6412 : /************************************************************************/
6413 : /* GuessJPEGQuality() */
6414 : /* */
6415 : /* Guess JPEG quality from JPEGTABLES tag. */
6416 : /************************************************************************/
6417 :
6418 3841 : static const GByte *GTIFFFindNextTable(const GByte *paby, GByte byMarker,
6419 : int nLen, int *pnLenTable)
6420 : {
6421 7949 : for (int i = 0; i + 1 < nLen;)
6422 : {
6423 7949 : if (paby[i] != 0xFF)
6424 0 : return nullptr;
6425 7949 : ++i;
6426 7949 : if (paby[i] == 0xD8)
6427 : {
6428 3111 : ++i;
6429 3111 : continue;
6430 : }
6431 4838 : if (i + 2 >= nLen)
6432 829 : return nullptr;
6433 4009 : int nMarkerLen = paby[i + 1] * 256 + paby[i + 2];
6434 4009 : if (i + 1 + nMarkerLen >= nLen)
6435 0 : return nullptr;
6436 4009 : if (paby[i] == byMarker)
6437 : {
6438 3012 : if (pnLenTable)
6439 2470 : *pnLenTable = nMarkerLen;
6440 3012 : return paby + i + 1;
6441 : }
6442 997 : i += 1 + nMarkerLen;
6443 : }
6444 0 : return nullptr;
6445 : }
6446 :
6447 : constexpr GByte MARKER_HUFFMAN_TABLE = 0xC4;
6448 : constexpr GByte MARKER_QUANT_TABLE = 0xDB;
6449 :
6450 : // We assume that if there are several quantization tables, they are
6451 : // in the same order. Which is a reasonable assumption for updating
6452 : // a file generated by ourselves.
6453 904 : static bool GTIFFQuantizationTablesEqual(const GByte *paby1, int nLen1,
6454 : const GByte *paby2, int nLen2)
6455 : {
6456 904 : bool bFound = false;
6457 : while (true)
6458 : {
6459 945 : int nLenTable1 = 0;
6460 945 : int nLenTable2 = 0;
6461 : const GByte *paby1New =
6462 945 : GTIFFFindNextTable(paby1, MARKER_QUANT_TABLE, nLen1, &nLenTable1);
6463 : const GByte *paby2New =
6464 945 : GTIFFFindNextTable(paby2, MARKER_QUANT_TABLE, nLen2, &nLenTable2);
6465 945 : if (paby1New == nullptr && paby2New == nullptr)
6466 904 : return bFound;
6467 911 : if (paby1New == nullptr || paby2New == nullptr)
6468 0 : return false;
6469 911 : if (nLenTable1 != nLenTable2)
6470 207 : return false;
6471 704 : if (memcmp(paby1New, paby2New, nLenTable1) != 0)
6472 663 : return false;
6473 41 : paby1New += nLenTable1;
6474 41 : paby2New += nLenTable2;
6475 41 : nLen1 -= static_cast<int>(paby1New - paby1);
6476 41 : nLen2 -= static_cast<int>(paby2New - paby2);
6477 41 : paby1 = paby1New;
6478 41 : paby2 = paby2New;
6479 41 : bFound = true;
6480 41 : }
6481 : }
6482 :
6483 : // Guess the JPEG quality by comparing against the MD5Sum of precomputed
6484 : // quantization tables
6485 407 : static int GuessJPEGQualityFromMD5(const uint8_t md5JPEGQuantTable[][16],
6486 : const GByte *const pabyJPEGTable,
6487 : int nJPEGTableSize)
6488 : {
6489 407 : int nRemainingLen = nJPEGTableSize;
6490 407 : const GByte *pabyCur = pabyJPEGTable;
6491 :
6492 : struct CPLMD5Context context;
6493 407 : CPLMD5Init(&context);
6494 :
6495 : while (true)
6496 : {
6497 1055 : int nLenTable = 0;
6498 1055 : const GByte *pabyNew = GTIFFFindNextTable(pabyCur, MARKER_QUANT_TABLE,
6499 : nRemainingLen, &nLenTable);
6500 1055 : if (pabyNew == nullptr)
6501 407 : break;
6502 648 : CPLMD5Update(&context, pabyNew, nLenTable);
6503 648 : pabyNew += nLenTable;
6504 648 : nRemainingLen -= static_cast<int>(pabyNew - pabyCur);
6505 648 : pabyCur = pabyNew;
6506 648 : }
6507 :
6508 : GByte digest[16];
6509 407 : CPLMD5Final(digest, &context);
6510 :
6511 28696 : for (int i = 0; i < 100; i++)
6512 : {
6513 28693 : if (memcmp(md5JPEGQuantTable[i], digest, 16) == 0)
6514 : {
6515 404 : return i + 1;
6516 : }
6517 : }
6518 3 : return -1;
6519 : }
6520 :
6521 462 : int GTiffDataset::GuessJPEGQuality(bool &bOutHasQuantizationTable,
6522 : bool &bOutHasHuffmanTable)
6523 : {
6524 462 : CPLAssert(m_nCompression == COMPRESSION_JPEG);
6525 462 : uint32_t nJPEGTableSize = 0;
6526 462 : void *pJPEGTable = nullptr;
6527 462 : if (!TIFFGetField(m_hTIFF, TIFFTAG_JPEGTABLES, &nJPEGTableSize,
6528 : &pJPEGTable))
6529 : {
6530 14 : bOutHasQuantizationTable = false;
6531 14 : bOutHasHuffmanTable = false;
6532 14 : return -1;
6533 : }
6534 :
6535 448 : bOutHasQuantizationTable =
6536 448 : GTIFFFindNextTable(static_cast<const GByte *>(pJPEGTable),
6537 : MARKER_QUANT_TABLE, nJPEGTableSize,
6538 448 : nullptr) != nullptr;
6539 448 : bOutHasHuffmanTable =
6540 448 : GTIFFFindNextTable(static_cast<const GByte *>(pJPEGTable),
6541 : MARKER_HUFFMAN_TABLE, nJPEGTableSize,
6542 448 : nullptr) != nullptr;
6543 448 : if (!bOutHasQuantizationTable)
6544 7 : return -1;
6545 :
6546 441 : if ((nBands == 1 && m_nBitsPerSample == 8) ||
6547 381 : (nBands == 3 && m_nBitsPerSample == 8 &&
6548 335 : m_nPhotometric == PHOTOMETRIC_RGB) ||
6549 287 : (nBands == 4 && m_nBitsPerSample == 8 &&
6550 27 : m_nPhotometric == PHOTOMETRIC_SEPARATED))
6551 : {
6552 166 : return GuessJPEGQualityFromMD5(md5JPEGQuantTable_generic_8bit,
6553 : static_cast<const GByte *>(pJPEGTable),
6554 166 : static_cast<int>(nJPEGTableSize));
6555 : }
6556 :
6557 275 : if (nBands == 3 && m_nBitsPerSample == 8 &&
6558 241 : m_nPhotometric == PHOTOMETRIC_YCBCR)
6559 : {
6560 : int nRet =
6561 241 : GuessJPEGQualityFromMD5(md5JPEGQuantTable_3_YCBCR_8bit,
6562 : static_cast<const GByte *>(pJPEGTable),
6563 : static_cast<int>(nJPEGTableSize));
6564 241 : if (nRet < 0)
6565 : {
6566 : // libjpeg 9e has modified the YCbCr quantization tables.
6567 : nRet =
6568 0 : GuessJPEGQualityFromMD5(md5JPEGQuantTable_3_YCBCR_8bit_jpeg9e,
6569 : static_cast<const GByte *>(pJPEGTable),
6570 : static_cast<int>(nJPEGTableSize));
6571 : }
6572 241 : return nRet;
6573 : }
6574 :
6575 34 : char **papszLocalParameters = nullptr;
6576 : papszLocalParameters =
6577 34 : CSLSetNameValue(papszLocalParameters, "COMPRESS", "JPEG");
6578 34 : if (m_nPhotometric == PHOTOMETRIC_YCBCR)
6579 : papszLocalParameters =
6580 7 : CSLSetNameValue(papszLocalParameters, "PHOTOMETRIC", "YCBCR");
6581 27 : else if (m_nPhotometric == PHOTOMETRIC_SEPARATED)
6582 : papszLocalParameters =
6583 0 : CSLSetNameValue(papszLocalParameters, "PHOTOMETRIC", "CMYK");
6584 : papszLocalParameters =
6585 34 : CSLSetNameValue(papszLocalParameters, "BLOCKYSIZE", "16");
6586 34 : if (m_nBitsPerSample == 12)
6587 : papszLocalParameters =
6588 16 : CSLSetNameValue(papszLocalParameters, GDALMD_NBITS, "12");
6589 :
6590 : const CPLString osTmpFilenameIn(
6591 34 : VSIMemGenerateHiddenFilename("gtiffdataset_guess_jpeg_quality_tmp"));
6592 :
6593 34 : int nRet = -1;
6594 938 : for (int nQuality = 0; nQuality <= 100 && nRet < 0; ++nQuality)
6595 : {
6596 904 : VSILFILE *fpTmp = nullptr;
6597 904 : if (nQuality == 0)
6598 : papszLocalParameters =
6599 34 : CSLSetNameValue(papszLocalParameters, "JPEG_QUALITY", "75");
6600 : else
6601 : papszLocalParameters =
6602 870 : CSLSetNameValue(papszLocalParameters, "JPEG_QUALITY",
6603 : CPLSPrintf("%d", nQuality));
6604 :
6605 904 : CPLPushErrorHandler(CPLQuietErrorHandler);
6606 904 : CPLString osTmp;
6607 : bool bTileInterleaving;
6608 1808 : TIFF *hTIFFTmp = CreateLL(
6609 904 : osTmpFilenameIn, 16, 16, (nBands <= 4) ? nBands : 1,
6610 : GetRasterBand(1)->GetRasterDataType(), 0.0, 0, papszLocalParameters,
6611 : &fpTmp, osTmp, /* bCreateCopy=*/false, bTileInterleaving);
6612 904 : CPLPopErrorHandler();
6613 904 : if (!hTIFFTmp)
6614 : {
6615 0 : break;
6616 : }
6617 :
6618 904 : TIFFWriteCheck(hTIFFTmp, FALSE, "CreateLL");
6619 904 : TIFFWriteDirectory(hTIFFTmp);
6620 904 : TIFFSetDirectory(hTIFFTmp, 0);
6621 : // Now reset jpegcolormode.
6622 1196 : if (m_nPhotometric == PHOTOMETRIC_YCBCR &&
6623 292 : CPLTestBool(CPLGetConfigOption("CONVERT_YCBCR_TO_RGB", "YES")))
6624 : {
6625 292 : TIFFSetField(hTIFFTmp, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
6626 : }
6627 :
6628 904 : GByte abyZeroData[(16 * 16 * 4 * 3) / 2] = {};
6629 904 : const int nBlockSize =
6630 904 : (16 * 16 * ((nBands <= 4) ? nBands : 1) * m_nBitsPerSample) / 8;
6631 904 : TIFFWriteEncodedStrip(hTIFFTmp, 0, abyZeroData, nBlockSize);
6632 :
6633 904 : uint32_t nJPEGTableSizeTry = 0;
6634 904 : void *pJPEGTableTry = nullptr;
6635 904 : if (TIFFGetField(hTIFFTmp, TIFFTAG_JPEGTABLES, &nJPEGTableSizeTry,
6636 904 : &pJPEGTableTry))
6637 : {
6638 904 : if (GTIFFQuantizationTablesEqual(
6639 : static_cast<GByte *>(pJPEGTable), nJPEGTableSize,
6640 : static_cast<GByte *>(pJPEGTableTry), nJPEGTableSizeTry))
6641 : {
6642 34 : nRet = (nQuality == 0) ? 75 : nQuality;
6643 : }
6644 : }
6645 :
6646 904 : XTIFFClose(hTIFFTmp);
6647 904 : CPL_IGNORE_RET_VAL(VSIFCloseL(fpTmp));
6648 : }
6649 :
6650 34 : CSLDestroy(papszLocalParameters);
6651 34 : VSIUnlink(osTmpFilenameIn);
6652 :
6653 34 : return nRet;
6654 : }
6655 :
6656 : /************************************************************************/
6657 : /* SetJPEGQualityAndTablesModeFromFile() */
6658 : /************************************************************************/
6659 :
6660 161 : void GTiffDataset::SetJPEGQualityAndTablesModeFromFile(
6661 : int nQuality, bool bHasQuantizationTable, bool bHasHuffmanTable)
6662 : {
6663 161 : if (nQuality > 0)
6664 : {
6665 154 : CPLDebug("GTiff", "Guessed JPEG quality to be %d", nQuality);
6666 154 : m_nJpegQuality = static_cast<signed char>(nQuality);
6667 154 : TIFFSetField(m_hTIFF, TIFFTAG_JPEGQUALITY, nQuality);
6668 :
6669 : // This means we will use the quantization tables from the
6670 : // JpegTables tag.
6671 154 : m_nJpegTablesMode = JPEGTABLESMODE_QUANT;
6672 : }
6673 : else
6674 : {
6675 7 : uint32_t nJPEGTableSize = 0;
6676 7 : void *pJPEGTable = nullptr;
6677 7 : if (!TIFFGetField(m_hTIFF, TIFFTAG_JPEGTABLES, &nJPEGTableSize,
6678 : &pJPEGTable))
6679 : {
6680 4 : toff_t *panByteCounts = nullptr;
6681 8 : const int nBlockCount = m_nPlanarConfig == PLANARCONFIG_SEPARATE
6682 4 : ? m_nBlocksPerBand * nBands
6683 : : m_nBlocksPerBand;
6684 4 : if (TIFFIsTiled(m_hTIFF))
6685 1 : TIFFGetField(m_hTIFF, TIFFTAG_TILEBYTECOUNTS, &panByteCounts);
6686 : else
6687 3 : TIFFGetField(m_hTIFF, TIFFTAG_STRIPBYTECOUNTS, &panByteCounts);
6688 :
6689 4 : bool bFoundNonEmptyBlock = false;
6690 4 : if (panByteCounts != nullptr)
6691 : {
6692 56 : for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
6693 : {
6694 53 : if (panByteCounts[iBlock] != 0)
6695 : {
6696 1 : bFoundNonEmptyBlock = true;
6697 1 : break;
6698 : }
6699 : }
6700 : }
6701 4 : if (bFoundNonEmptyBlock)
6702 : {
6703 1 : CPLDebug("GTiff", "Could not guess JPEG quality. "
6704 : "JPEG tables are missing, so going in "
6705 : "TIFFTAG_JPEGTABLESMODE = 0/2 mode");
6706 : // Write quantization tables in each strile.
6707 1 : m_nJpegTablesMode = 0;
6708 : }
6709 : }
6710 : else
6711 : {
6712 3 : if (bHasQuantizationTable)
6713 : {
6714 : // FIXME in libtiff: this is likely going to cause issues
6715 : // since libtiff will reuse in each strile the number of
6716 : // the global quantization table, which is invalid.
6717 1 : CPLDebug("GTiff",
6718 : "Could not guess JPEG quality although JPEG "
6719 : "quantization tables are present, so going in "
6720 : "TIFFTAG_JPEGTABLESMODE = 0/2 mode");
6721 : }
6722 : else
6723 : {
6724 2 : CPLDebug("GTiff",
6725 : "Could not guess JPEG quality since JPEG "
6726 : "quantization tables are not present, so going in "
6727 : "TIFFTAG_JPEGTABLESMODE = 0/2 mode");
6728 : }
6729 :
6730 : // Write quantization tables in each strile.
6731 3 : m_nJpegTablesMode = 0;
6732 : }
6733 : }
6734 161 : if (bHasHuffmanTable)
6735 : {
6736 : // If there are Huffman tables in header use them, otherwise
6737 : // if we use optimized tables, libtiff will currently reuse
6738 : // the number of the Huffman tables of the header for the
6739 : // optimized version of each strile, which is illegal.
6740 23 : m_nJpegTablesMode |= JPEGTABLESMODE_HUFF;
6741 : }
6742 161 : if (m_nJpegTablesMode >= 0)
6743 159 : TIFFSetField(m_hTIFF, TIFFTAG_JPEGTABLESMODE, m_nJpegTablesMode);
6744 161 : }
6745 :
6746 : /************************************************************************/
6747 : /* Create() */
6748 : /* */
6749 : /* Create a new GeoTIFF or TIFF file. */
6750 : /************************************************************************/
6751 :
6752 5858 : GDALDataset *GTiffDataset::Create(const char *pszFilename, int nXSize,
6753 : int nYSize, int l_nBands, GDALDataType eType,
6754 : CSLConstList papszParamList)
6755 :
6756 : {
6757 5858 : VSILFILE *l_fpL = nullptr;
6758 11716 : CPLString l_osTmpFilename;
6759 :
6760 : const int nColorTableMultiplier = std::max(
6761 11716 : 1,
6762 11716 : std::min(257,
6763 5858 : atoi(CSLFetchNameValueDef(
6764 : papszParamList, "COLOR_TABLE_MULTIPLIER",
6765 5858 : CPLSPrintf("%d", DEFAULT_COLOR_TABLE_MULTIPLIER_257)))));
6766 :
6767 : /* -------------------------------------------------------------------- */
6768 : /* Create the underlying TIFF file. */
6769 : /* -------------------------------------------------------------------- */
6770 : bool bTileInterleaving;
6771 : TIFF *l_hTIFF =
6772 5858 : CreateLL(pszFilename, nXSize, nYSize, l_nBands, eType, 0,
6773 : nColorTableMultiplier, papszParamList, &l_fpL, l_osTmpFilename,
6774 : /* bCreateCopy=*/false, bTileInterleaving);
6775 5858 : const bool bStreaming = !l_osTmpFilename.empty();
6776 :
6777 5858 : if (l_hTIFF == nullptr)
6778 38 : return nullptr;
6779 :
6780 : /* -------------------------------------------------------------------- */
6781 : /* Create the new GTiffDataset object. */
6782 : /* -------------------------------------------------------------------- */
6783 11640 : auto poDS = std::make_unique<GTiffDataset>();
6784 5820 : poDS->m_hTIFF = l_hTIFF;
6785 5820 : poDS->m_fpL = l_fpL;
6786 5820 : const bool bSuppressASAP = CPLTestBool(
6787 : CSLFetchNameValueDef(papszParamList, "@SUPPRESS_ASAP", "NO"));
6788 5820 : if (bSuppressASAP)
6789 36 : poDS->MarkSuppressOnClose();
6790 5820 : if (bStreaming)
6791 : {
6792 4 : poDS->m_bStreamingOut = true;
6793 4 : poDS->m_pszTmpFilename = CPLStrdup(l_osTmpFilename);
6794 4 : poDS->m_fpToWrite = VSIFOpenL(pszFilename, "wb");
6795 4 : if (poDS->m_fpToWrite == nullptr)
6796 : {
6797 1 : VSIUnlink(l_osTmpFilename);
6798 1 : return nullptr;
6799 : }
6800 : }
6801 5819 : poDS->nRasterXSize = nXSize;
6802 5819 : poDS->nRasterYSize = nYSize;
6803 5819 : poDS->eAccess = GA_Update;
6804 :
6805 : // This will avoid GTiffDataset::GetSiblingFiles() to trigger a directory
6806 : // listing, which is potentially costly and only makes sense when opening
6807 : // new files, not creating new ones. Helps for scenario like
6808 : // https://github.com/OSGeo/gdal/issues/13930
6809 5819 : poDS->m_bHasGotSiblingFiles = true;
6810 :
6811 5819 : poDS->m_nColorTableMultiplier = nColorTableMultiplier;
6812 :
6813 5819 : poDS->m_bCrystalized = false;
6814 5819 : poDS->m_nSamplesPerPixel = static_cast<uint16_t>(l_nBands);
6815 5819 : poDS->m_osFilename = pszFilename;
6816 :
6817 : // Don't try to load external metadata files (#6597).
6818 5819 : poDS->m_bIMDRPCMetadataLoaded = true;
6819 :
6820 : // Avoid premature crystalization that will cause directory re-writing if
6821 : // GetProjectionRef() or GetGeoTransform() are called on the newly created
6822 : // GeoTIFF.
6823 5819 : poDS->m_bLookedForProjection = true;
6824 :
6825 5819 : TIFFGetField(l_hTIFF, TIFFTAG_SAMPLEFORMAT, &(poDS->m_nSampleFormat));
6826 5819 : TIFFGetField(l_hTIFF, TIFFTAG_PLANARCONFIG, &(poDS->m_nPlanarConfig));
6827 : // Weird that we need this, but otherwise we get a Valgrind warning on
6828 : // tiff_write_124.
6829 5819 : if (!TIFFGetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, &(poDS->m_nPhotometric)))
6830 1 : poDS->m_nPhotometric = PHOTOMETRIC_MINISBLACK;
6831 5819 : TIFFGetField(l_hTIFF, TIFFTAG_BITSPERSAMPLE, &(poDS->m_nBitsPerSample));
6832 5819 : TIFFGetField(l_hTIFF, TIFFTAG_COMPRESSION, &(poDS->m_nCompression));
6833 :
6834 5819 : if (TIFFIsTiled(l_hTIFF))
6835 : {
6836 408 : TIFFGetField(l_hTIFF, TIFFTAG_TILEWIDTH, &(poDS->m_nBlockXSize));
6837 408 : TIFFGetField(l_hTIFF, TIFFTAG_TILELENGTH, &(poDS->m_nBlockYSize));
6838 : }
6839 : else
6840 : {
6841 5411 : if (!TIFFGetField(l_hTIFF, TIFFTAG_ROWSPERSTRIP,
6842 5411 : &(poDS->m_nRowsPerStrip)))
6843 0 : poDS->m_nRowsPerStrip = 1; // Dummy value.
6844 :
6845 5411 : poDS->m_nBlockXSize = nXSize;
6846 10822 : poDS->m_nBlockYSize =
6847 5411 : std::min(static_cast<int>(poDS->m_nRowsPerStrip), nYSize);
6848 : }
6849 :
6850 5819 : if (!poDS->ComputeBlocksPerColRowAndBand(l_nBands))
6851 : {
6852 0 : poDS->m_fpL->CancelCreation();
6853 0 : return nullptr;
6854 : }
6855 :
6856 5819 : poDS->m_eProfile = GetProfile(CSLFetchNameValue(papszParamList, "PROFILE"));
6857 :
6858 : /* -------------------------------------------------------------------- */
6859 : /* YCbCr JPEG compressed images should be translated on the fly */
6860 : /* to RGB by libtiff/libjpeg unless specifically requested */
6861 : /* otherwise. */
6862 : /* -------------------------------------------------------------------- */
6863 5819 : if (poDS->m_nCompression == COMPRESSION_JPEG &&
6864 5840 : poDS->m_nPhotometric == PHOTOMETRIC_YCBCR &&
6865 21 : CPLTestBool(CPLGetConfigOption("CONVERT_YCBCR_TO_RGB", "YES")))
6866 : {
6867 21 : int nColorMode = 0;
6868 :
6869 21 : poDS->SetMetadataItem("SOURCE_COLOR_SPACE", "YCbCr",
6870 : GDAL_MDD_IMAGE_STRUCTURE);
6871 42 : if (!TIFFGetField(l_hTIFF, TIFFTAG_JPEGCOLORMODE, &nColorMode) ||
6872 21 : nColorMode != JPEGCOLORMODE_RGB)
6873 21 : TIFFSetField(l_hTIFF, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
6874 : }
6875 :
6876 5819 : if (poDS->m_nCompression == COMPRESSION_LERC)
6877 : {
6878 26 : uint32_t nLercParamCount = 0;
6879 26 : uint32_t *panLercParams = nullptr;
6880 26 : if (TIFFGetField(l_hTIFF, TIFFTAG_LERC_PARAMETERS, &nLercParamCount,
6881 52 : &panLercParams) &&
6882 26 : nLercParamCount == 2)
6883 : {
6884 26 : memcpy(poDS->m_anLercAddCompressionAndVersion, panLercParams,
6885 : sizeof(poDS->m_anLercAddCompressionAndVersion));
6886 : }
6887 : }
6888 :
6889 : /* -------------------------------------------------------------------- */
6890 : /* Read palette back as a color table if it has one. */
6891 : /* -------------------------------------------------------------------- */
6892 5819 : unsigned short *panRed = nullptr;
6893 5819 : unsigned short *panGreen = nullptr;
6894 5819 : unsigned short *panBlue = nullptr;
6895 :
6896 5823 : if (poDS->m_nPhotometric == PHOTOMETRIC_PALETTE &&
6897 4 : TIFFGetField(l_hTIFF, TIFFTAG_COLORMAP, &panRed, &panGreen, &panBlue))
6898 : {
6899 :
6900 4 : poDS->m_poColorTable = std::make_unique<GDALColorTable>();
6901 :
6902 4 : const int nColorCount = 1 << poDS->m_nBitsPerSample;
6903 :
6904 1028 : for (int iColor = nColorCount - 1; iColor >= 0; iColor--)
6905 : {
6906 1024 : const GDALColorEntry oEntry = {
6907 1024 : static_cast<short>(panRed[iColor] / nColorTableMultiplier),
6908 1024 : static_cast<short>(panGreen[iColor] / nColorTableMultiplier),
6909 1024 : static_cast<short>(panBlue[iColor] / nColorTableMultiplier),
6910 1024 : static_cast<short>(255)};
6911 :
6912 1024 : poDS->m_poColorTable->SetColorEntry(iColor, &oEntry);
6913 : }
6914 : }
6915 :
6916 : /* -------------------------------------------------------------------- */
6917 : /* Do we want to ensure all blocks get written out on close to */
6918 : /* avoid sparse files? */
6919 : /* -------------------------------------------------------------------- */
6920 5819 : if (!CPLFetchBool(papszParamList, "SPARSE_OK", false))
6921 5709 : poDS->m_bFillEmptyTilesAtClosing = true;
6922 :
6923 5819 : poDS->m_bWriteEmptyTiles =
6924 6646 : bStreaming || (poDS->m_nCompression != COMPRESSION_NONE &&
6925 827 : poDS->m_bFillEmptyTilesAtClosing);
6926 : // Only required for people writing non-compressed striped files in the
6927 : // right order and wanting all tstrips to be written in the same order
6928 : // so that the end result can be memory mapped without knowledge of each
6929 : // strip offset.
6930 5819 : if (CPLTestBool(CSLFetchNameValueDef(
6931 11638 : papszParamList, "WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")) ||
6932 5819 : CPLTestBool(CSLFetchNameValueDef(
6933 : papszParamList, "@WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")))
6934 : {
6935 26 : poDS->m_bWriteEmptyTiles = true;
6936 : }
6937 :
6938 : /* -------------------------------------------------------------------- */
6939 : /* Preserve creation options for consulting later (for instance */
6940 : /* to decide if a TFW file should be written). */
6941 : /* -------------------------------------------------------------------- */
6942 5819 : poDS->m_papszCreationOptions = CSLDuplicate(papszParamList);
6943 :
6944 5819 : poDS->m_nZLevel = GTiffGetZLevel(papszParamList);
6945 5819 : poDS->m_nLZMAPreset = GTiffGetLZMAPreset(papszParamList);
6946 5819 : poDS->m_nZSTDLevel = GTiffGetZSTDPreset(papszParamList);
6947 5819 : poDS->m_nWebPLevel = GTiffGetWebPLevel(papszParamList);
6948 5819 : poDS->m_bWebPLossless = GTiffGetWebPLossless(papszParamList);
6949 5821 : if (poDS->m_nWebPLevel != 100 && poDS->m_bWebPLossless &&
6950 2 : CSLFetchNameValue(papszParamList, "WEBP_LEVEL"))
6951 : {
6952 0 : CPLError(CE_Warning, CPLE_AppDefined,
6953 : "WEBP_LEVEL is specified, but WEBP_LOSSLESS=YES. "
6954 : "WEBP_LEVEL will be ignored.");
6955 : }
6956 5819 : poDS->m_nJpegQuality = GTiffGetJpegQuality(papszParamList);
6957 5819 : poDS->m_nJpegTablesMode = GTiffGetJpegTablesMode(papszParamList);
6958 5819 : poDS->m_dfMaxZError = GTiffGetLERCMaxZError(papszParamList);
6959 5819 : poDS->m_dfMaxZErrorOverview = GTiffGetLERCMaxZErrorOverview(papszParamList);
6960 : #if HAVE_JXL
6961 5819 : poDS->m_bJXLLossless = GTiffGetJXLLossless(papszParamList);
6962 5819 : poDS->m_nJXLEffort = GTiffGetJXLEffort(papszParamList);
6963 5819 : poDS->m_fJXLDistance = GTiffGetJXLDistance(papszParamList);
6964 5819 : poDS->m_fJXLAlphaDistance = GTiffGetJXLAlphaDistance(papszParamList);
6965 : #endif
6966 5819 : poDS->InitCreationOrOpenOptions(true, papszParamList);
6967 :
6968 : /* -------------------------------------------------------------------- */
6969 : /* Create band information objects. */
6970 : /* -------------------------------------------------------------------- */
6971 308526 : for (int iBand = 0; iBand < l_nBands; ++iBand)
6972 : {
6973 371968 : if (poDS->m_nBitsPerSample == 8 || poDS->m_nBitsPerSample == 16 ||
6974 372220 : poDS->m_nBitsPerSample == 32 || poDS->m_nBitsPerSample == 64 ||
6975 252 : poDS->m_nBitsPerSample == 128)
6976 : {
6977 605262 : poDS->SetBand(iBand + 1, std::make_unique<GTiffRasterBand>(
6978 605262 : poDS.get(), iBand + 1));
6979 : }
6980 : else
6981 : {
6982 152 : poDS->SetBand(iBand + 1, std::make_unique<GTiffOddBitsBand>(
6983 76 : poDS.get(), iBand + 1));
6984 152 : poDS->GetRasterBand(iBand + 1)->SetMetadataItem(
6985 152 : GDALMD_NBITS, CPLString().Printf("%d", poDS->m_nBitsPerSample),
6986 76 : GDAL_MDD_IMAGE_STRUCTURE);
6987 : }
6988 : }
6989 :
6990 5819 : poDS->GetDiscardLsbOption(papszParamList);
6991 :
6992 5819 : if (poDS->m_nPlanarConfig == PLANARCONFIG_CONTIG && l_nBands != 1)
6993 865 : poDS->SetMetadataItem(GDALMD_INTERLEAVE, "PIXEL",
6994 : GDAL_MDD_IMAGE_STRUCTURE);
6995 : else
6996 4954 : poDS->SetMetadataItem(GDALMD_INTERLEAVE, "BAND",
6997 : GDAL_MDD_IMAGE_STRUCTURE);
6998 :
6999 5819 : poDS->oOvManager.Initialize(poDS.get(), pszFilename);
7000 :
7001 5819 : return poDS.release();
7002 : }
7003 :
7004 : /************************************************************************/
7005 : /* CopyImageryAndMask() */
7006 : /************************************************************************/
7007 :
7008 355 : CPLErr GTiffDataset::CopyImageryAndMask(GTiffDataset *poDstDS,
7009 : GDALDataset *poSrcDS,
7010 : GDALRasterBand *poSrcMaskBand,
7011 : GDALProgressFunc pfnProgress,
7012 : void *pProgressData)
7013 : {
7014 355 : CPLErr eErr = CE_None;
7015 :
7016 355 : const auto eType = poDstDS->GetRasterBand(1)->GetRasterDataType();
7017 355 : const int nDataTypeSize = GDALGetDataTypeSizeBytes(eType);
7018 355 : const int l_nBands = poDstDS->GetRasterCount();
7019 : GByte *pBlockBuffer = static_cast<GByte *>(
7020 355 : VSI_MALLOC3_VERBOSE(poDstDS->m_nBlockXSize, poDstDS->m_nBlockYSize,
7021 : cpl::fits_on<int>(l_nBands * nDataTypeSize)));
7022 355 : if (pBlockBuffer == nullptr)
7023 : {
7024 0 : eErr = CE_Failure;
7025 : }
7026 355 : const int nYSize = poDstDS->nRasterYSize;
7027 355 : const int nXSize = poDstDS->nRasterXSize;
7028 : const bool bIsOddBand =
7029 355 : dynamic_cast<GTiffOddBitsBand *>(poDstDS->GetRasterBand(1)) != nullptr;
7030 :
7031 355 : if (poDstDS->m_poMaskDS)
7032 : {
7033 58 : CPLAssert(poDstDS->m_poMaskDS->m_nBlockXSize == poDstDS->m_nBlockXSize);
7034 58 : CPLAssert(poDstDS->m_poMaskDS->m_nBlockYSize == poDstDS->m_nBlockYSize);
7035 : }
7036 :
7037 355 : if (poDstDS->m_nPlanarConfig == PLANARCONFIG_SEPARATE &&
7038 59 : !poDstDS->m_bTileInterleave)
7039 : {
7040 46 : int iBlock = 0;
7041 46 : const int nBlocks = poDstDS->m_nBlocksPerBand *
7042 46 : (l_nBands + (poDstDS->m_poMaskDS ? 1 : 0));
7043 199 : for (int i = 0; eErr == CE_None && i < l_nBands; i++)
7044 : {
7045 351 : for (int iY = 0; iY < nYSize && eErr == CE_None;
7046 198 : iY = ((nYSize - iY < poDstDS->m_nBlockYSize)
7047 198 : ? nYSize
7048 59 : : iY + poDstDS->m_nBlockYSize))
7049 : {
7050 : const int nReqYSize =
7051 198 : std::min(nYSize - iY, poDstDS->m_nBlockYSize);
7052 501 : for (int iX = 0; iX < nXSize && eErr == CE_None;
7053 303 : iX = ((nXSize - iX < poDstDS->m_nBlockXSize)
7054 303 : ? nXSize
7055 155 : : iX + poDstDS->m_nBlockXSize))
7056 : {
7057 : const int nReqXSize =
7058 303 : std::min(nXSize - iX, poDstDS->m_nBlockXSize);
7059 303 : if (nReqXSize < poDstDS->m_nBlockXSize ||
7060 155 : nReqYSize < poDstDS->m_nBlockYSize)
7061 : {
7062 193 : memset(pBlockBuffer, 0,
7063 193 : static_cast<size_t>(poDstDS->m_nBlockXSize) *
7064 193 : poDstDS->m_nBlockYSize * nDataTypeSize);
7065 : }
7066 303 : eErr = poSrcDS->GetRasterBand(i + 1)->RasterIO(
7067 : GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
7068 : nReqXSize, nReqYSize, eType, nDataTypeSize,
7069 303 : static_cast<GSpacing>(nDataTypeSize) *
7070 303 : poDstDS->m_nBlockXSize,
7071 : nullptr);
7072 303 : if (eErr == CE_None)
7073 : {
7074 303 : eErr = poDstDS->WriteEncodedTileOrStrip(
7075 : iBlock, pBlockBuffer, false);
7076 : }
7077 :
7078 303 : iBlock++;
7079 606 : if (pfnProgress &&
7080 303 : !pfnProgress(static_cast<double>(iBlock) / nBlocks,
7081 : nullptr, pProgressData))
7082 : {
7083 0 : eErr = CE_Failure;
7084 : }
7085 :
7086 303 : if (poDstDS->m_bWriteError)
7087 0 : eErr = CE_Failure;
7088 : }
7089 : }
7090 : }
7091 46 : if (poDstDS->m_poMaskDS && eErr == CE_None)
7092 : {
7093 6 : int iBlockMask = 0;
7094 17 : for (int iY = 0, nYBlock = 0; iY < nYSize && eErr == CE_None;
7095 11 : iY = ((nYSize - iY < poDstDS->m_nBlockYSize)
7096 11 : ? nYSize
7097 5 : : iY + poDstDS->m_nBlockYSize),
7098 : nYBlock++)
7099 : {
7100 : const int nReqYSize =
7101 11 : std::min(nYSize - iY, poDstDS->m_nBlockYSize);
7102 49 : for (int iX = 0, nXBlock = 0; iX < nXSize && eErr == CE_None;
7103 38 : iX = ((nXSize - iX < poDstDS->m_nBlockXSize)
7104 38 : ? nXSize
7105 30 : : iX + poDstDS->m_nBlockXSize),
7106 : nXBlock++)
7107 : {
7108 : const int nReqXSize =
7109 38 : std::min(nXSize - iX, poDstDS->m_nBlockXSize);
7110 38 : if (nReqXSize < poDstDS->m_nBlockXSize ||
7111 30 : nReqYSize < poDstDS->m_nBlockYSize)
7112 : {
7113 16 : memset(pBlockBuffer, 0,
7114 16 : static_cast<size_t>(poDstDS->m_nBlockXSize) *
7115 16 : poDstDS->m_nBlockYSize);
7116 : }
7117 76 : eErr = poSrcMaskBand->RasterIO(
7118 : GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
7119 : nReqXSize, nReqYSize, GDT_UInt8, 1,
7120 38 : poDstDS->m_nBlockXSize, nullptr);
7121 38 : if (eErr == CE_None)
7122 : {
7123 : // Avoid any attempt to load from disk
7124 38 : poDstDS->m_poMaskDS->m_nLoadedBlock = iBlockMask;
7125 : eErr =
7126 38 : poDstDS->m_poMaskDS->GetRasterBand(1)->WriteBlock(
7127 : nXBlock, nYBlock, pBlockBuffer);
7128 38 : if (eErr == CE_None)
7129 38 : eErr = poDstDS->m_poMaskDS->FlushBlockBuf();
7130 : }
7131 :
7132 38 : iBlockMask++;
7133 76 : if (pfnProgress &&
7134 38 : !pfnProgress(static_cast<double>(iBlock + iBlockMask) /
7135 : nBlocks,
7136 : nullptr, pProgressData))
7137 : {
7138 0 : eErr = CE_Failure;
7139 : }
7140 :
7141 38 : if (poDstDS->m_poMaskDS->m_bWriteError)
7142 0 : eErr = CE_Failure;
7143 : }
7144 : }
7145 46 : }
7146 : }
7147 : else
7148 : {
7149 309 : int iBlock = 0;
7150 309 : const int nBlocks = poDstDS->m_nBlocksPerBand;
7151 7126 : for (int iY = 0, nYBlock = 0; iY < nYSize && eErr == CE_None;
7152 6817 : iY = ((nYSize - iY < poDstDS->m_nBlockYSize)
7153 6817 : ? nYSize
7154 6584 : : iY + poDstDS->m_nBlockYSize),
7155 : nYBlock++)
7156 : {
7157 6817 : const int nReqYSize = std::min(nYSize - iY, poDstDS->m_nBlockYSize);
7158 26643 : for (int iX = 0, nXBlock = 0; iX < nXSize && eErr == CE_None;
7159 19826 : iX = ((nXSize - iX < poDstDS->m_nBlockXSize)
7160 19826 : ? nXSize
7161 19505 : : iX + poDstDS->m_nBlockXSize),
7162 : nXBlock++)
7163 : {
7164 : const int nReqXSize =
7165 19826 : std::min(nXSize - iX, poDstDS->m_nBlockXSize);
7166 19826 : if (nReqXSize < poDstDS->m_nBlockXSize ||
7167 19505 : nReqYSize < poDstDS->m_nBlockYSize)
7168 : {
7169 506 : memset(pBlockBuffer, 0,
7170 506 : static_cast<size_t>(poDstDS->m_nBlockXSize) *
7171 506 : poDstDS->m_nBlockYSize * l_nBands *
7172 506 : nDataTypeSize);
7173 : }
7174 :
7175 19826 : if (poDstDS->m_bTileInterleave)
7176 : {
7177 114 : eErr = poSrcDS->RasterIO(
7178 : GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
7179 : nReqXSize, nReqYSize, eType, l_nBands, nullptr,
7180 : nDataTypeSize,
7181 57 : static_cast<GSpacing>(nDataTypeSize) *
7182 57 : poDstDS->m_nBlockXSize,
7183 57 : static_cast<GSpacing>(nDataTypeSize) *
7184 57 : poDstDS->m_nBlockXSize * poDstDS->m_nBlockYSize,
7185 : nullptr);
7186 57 : if (eErr == CE_None)
7187 : {
7188 228 : for (int i = 0; eErr == CE_None && i < l_nBands; i++)
7189 : {
7190 171 : eErr = poDstDS->WriteEncodedTileOrStrip(
7191 171 : iBlock + i * poDstDS->m_nBlocksPerBand,
7192 171 : pBlockBuffer + static_cast<size_t>(i) *
7193 171 : poDstDS->m_nBlockXSize *
7194 171 : poDstDS->m_nBlockYSize *
7195 171 : nDataTypeSize,
7196 : false);
7197 : }
7198 : }
7199 : }
7200 19769 : else if (!bIsOddBand)
7201 : {
7202 39416 : eErr = poSrcDS->RasterIO(
7203 : GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
7204 : nReqXSize, nReqYSize, eType, l_nBands, nullptr,
7205 19708 : static_cast<GSpacing>(nDataTypeSize) * l_nBands,
7206 19708 : static_cast<GSpacing>(nDataTypeSize) * l_nBands *
7207 19708 : poDstDS->m_nBlockXSize,
7208 : nDataTypeSize, nullptr);
7209 19708 : if (eErr == CE_None)
7210 : {
7211 19707 : eErr = poDstDS->WriteEncodedTileOrStrip(
7212 : iBlock, pBlockBuffer, false);
7213 : }
7214 : }
7215 : else
7216 : {
7217 : // In the odd bit case, this is a bit messy to ensure
7218 : // the strile gets written synchronously.
7219 : // We load the content of the n-1 bands in the cache,
7220 : // and for the last band we invoke WriteBlock() directly
7221 : // We also force FlushBlockBuf()
7222 122 : std::vector<GDALRasterBlock *> apoLockedBlocks;
7223 91 : for (int i = 0; eErr == CE_None && i < l_nBands - 1; i++)
7224 : {
7225 : auto poBlock =
7226 30 : poDstDS->GetRasterBand(i + 1)->GetLockedBlockRef(
7227 30 : nXBlock, nYBlock, TRUE);
7228 30 : if (poBlock)
7229 : {
7230 60 : eErr = poSrcDS->GetRasterBand(i + 1)->RasterIO(
7231 : GF_Read, iX, iY, nReqXSize, nReqYSize,
7232 : poBlock->GetDataRef(), nReqXSize, nReqYSize,
7233 : eType, nDataTypeSize,
7234 30 : static_cast<GSpacing>(nDataTypeSize) *
7235 30 : poDstDS->m_nBlockXSize,
7236 : nullptr);
7237 30 : poBlock->MarkDirty();
7238 30 : apoLockedBlocks.emplace_back(poBlock);
7239 : }
7240 : else
7241 : {
7242 0 : eErr = CE_Failure;
7243 : }
7244 : }
7245 61 : if (eErr == CE_None)
7246 : {
7247 122 : eErr = poSrcDS->GetRasterBand(l_nBands)->RasterIO(
7248 : GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
7249 : nReqXSize, nReqYSize, eType, nDataTypeSize,
7250 61 : static_cast<GSpacing>(nDataTypeSize) *
7251 61 : poDstDS->m_nBlockXSize,
7252 : nullptr);
7253 : }
7254 61 : if (eErr == CE_None)
7255 : {
7256 : // Avoid any attempt to load from disk
7257 61 : poDstDS->m_nLoadedBlock = iBlock;
7258 61 : eErr = poDstDS->GetRasterBand(l_nBands)->WriteBlock(
7259 : nXBlock, nYBlock, pBlockBuffer);
7260 61 : if (eErr == CE_None)
7261 61 : eErr = poDstDS->FlushBlockBuf();
7262 : }
7263 91 : for (auto poBlock : apoLockedBlocks)
7264 : {
7265 30 : poBlock->MarkClean();
7266 30 : poBlock->DropLock();
7267 : }
7268 : }
7269 :
7270 19826 : if (eErr == CE_None && poDstDS->m_poMaskDS)
7271 : {
7272 4662 : if (nReqXSize < poDstDS->m_nBlockXSize ||
7273 4620 : nReqYSize < poDstDS->m_nBlockYSize)
7274 : {
7275 79 : memset(pBlockBuffer, 0,
7276 79 : static_cast<size_t>(poDstDS->m_nBlockXSize) *
7277 79 : poDstDS->m_nBlockYSize);
7278 : }
7279 9324 : eErr = poSrcMaskBand->RasterIO(
7280 : GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
7281 : nReqXSize, nReqYSize, GDT_UInt8, 1,
7282 4662 : poDstDS->m_nBlockXSize, nullptr);
7283 4662 : if (eErr == CE_None)
7284 : {
7285 : // Avoid any attempt to load from disk
7286 4662 : poDstDS->m_poMaskDS->m_nLoadedBlock = iBlock;
7287 : eErr =
7288 4662 : poDstDS->m_poMaskDS->GetRasterBand(1)->WriteBlock(
7289 : nXBlock, nYBlock, pBlockBuffer);
7290 4662 : if (eErr == CE_None)
7291 4662 : eErr = poDstDS->m_poMaskDS->FlushBlockBuf();
7292 : }
7293 : }
7294 19826 : if (poDstDS->m_bWriteError)
7295 6 : eErr = CE_Failure;
7296 :
7297 19826 : iBlock++;
7298 39652 : if (pfnProgress &&
7299 19826 : !pfnProgress(static_cast<double>(iBlock) / nBlocks, nullptr,
7300 : pProgressData))
7301 : {
7302 0 : eErr = CE_Failure;
7303 : }
7304 : }
7305 : }
7306 : }
7307 :
7308 355 : poDstDS->FlushCache(false); // mostly to wait for thread completion
7309 355 : VSIFree(pBlockBuffer);
7310 :
7311 355 : return eErr;
7312 : }
7313 :
7314 : /************************************************************************/
7315 : /* CreateCopy() */
7316 : /************************************************************************/
7317 :
7318 2191 : GDALDataset *GTiffDataset::CreateCopy(const char *pszFilename,
7319 : GDALDataset *poSrcDS, int bStrict,
7320 : CSLConstList papszOptions,
7321 : GDALProgressFunc pfnProgress,
7322 : void *pProgressData)
7323 :
7324 : {
7325 2191 : if (poSrcDS->GetRasterCount() == 0)
7326 : {
7327 2 : ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
7328 : "Unable to export GeoTIFF files with zero bands.");
7329 2 : return nullptr;
7330 : }
7331 :
7332 2189 : GDALRasterBand *const poPBand = poSrcDS->GetRasterBand(1);
7333 2189 : GDALDataType eType = poPBand->GetRasterDataType();
7334 :
7335 : /* -------------------------------------------------------------------- */
7336 : /* Check, whether all bands in input dataset has the same type. */
7337 : /* -------------------------------------------------------------------- */
7338 2189 : const int l_nBands = poSrcDS->GetRasterCount();
7339 5124 : for (int iBand = 2; iBand <= l_nBands; ++iBand)
7340 : {
7341 2935 : if (eType != poSrcDS->GetRasterBand(iBand)->GetRasterDataType())
7342 : {
7343 0 : if (bStrict)
7344 : {
7345 0 : ReportError(
7346 : pszFilename, CE_Failure, CPLE_AppDefined,
7347 : "Unable to export GeoTIFF file with different datatypes "
7348 : "per different bands. All bands should have the same "
7349 : "types in TIFF.");
7350 0 : return nullptr;
7351 : }
7352 : else
7353 : {
7354 0 : ReportError(
7355 : pszFilename, CE_Warning, CPLE_AppDefined,
7356 : "Unable to export GeoTIFF file with different datatypes "
7357 : "per different bands. All bands should have the same "
7358 : "types in TIFF.");
7359 : }
7360 : }
7361 : }
7362 :
7363 : /* -------------------------------------------------------------------- */
7364 : /* Capture the profile. */
7365 : /* -------------------------------------------------------------------- */
7366 : const GTiffProfile eProfile =
7367 2189 : GetProfile(CSLFetchNameValue(papszOptions, "PROFILE"));
7368 :
7369 2189 : const bool bGeoTIFF = eProfile != GTiffProfile::BASELINE;
7370 :
7371 : /* -------------------------------------------------------------------- */
7372 : /* Special handling for NBITS. Copy from band metadata if found. */
7373 : /* -------------------------------------------------------------------- */
7374 2189 : char **papszCreateOptions = CSLDuplicate(papszOptions);
7375 :
7376 2189 : if (poPBand->GetMetadataItem(GDALMD_NBITS, GDAL_MDD_IMAGE_STRUCTURE) !=
7377 17 : nullptr &&
7378 17 : atoi(poPBand->GetMetadataItem(GDALMD_NBITS, GDAL_MDD_IMAGE_STRUCTURE)) >
7379 2206 : 0 &&
7380 17 : CSLFetchNameValue(papszCreateOptions, GDALMD_NBITS) == nullptr)
7381 : {
7382 3 : papszCreateOptions = CSLSetNameValue(
7383 : papszCreateOptions, GDALMD_NBITS,
7384 3 : poPBand->GetMetadataItem(GDALMD_NBITS, GDAL_MDD_IMAGE_STRUCTURE));
7385 : }
7386 :
7387 2189 : if (CSLFetchNameValue(papszOptions, "PIXELTYPE") == nullptr &&
7388 : eType == GDT_UInt8)
7389 : {
7390 1814 : poPBand->EnablePixelTypeSignedByteWarning(false);
7391 : const char *pszPixelType =
7392 1814 : poPBand->GetMetadataItem("PIXELTYPE", GDAL_MDD_IMAGE_STRUCTURE);
7393 1814 : poPBand->EnablePixelTypeSignedByteWarning(true);
7394 1814 : if (pszPixelType)
7395 : {
7396 1 : papszCreateOptions =
7397 1 : CSLSetNameValue(papszCreateOptions, "PIXELTYPE", pszPixelType);
7398 : }
7399 : }
7400 :
7401 : /* -------------------------------------------------------------------- */
7402 : /* Color profile. Copy from band metadata if found. */
7403 : /* -------------------------------------------------------------------- */
7404 2189 : if (bGeoTIFF)
7405 : {
7406 2172 : const char *pszOptionsMD[] = {"SOURCE_ICC_PROFILE",
7407 : "SOURCE_PRIMARIES_RED",
7408 : "SOURCE_PRIMARIES_GREEN",
7409 : "SOURCE_PRIMARIES_BLUE",
7410 : "SOURCE_WHITEPOINT",
7411 : "TIFFTAG_TRANSFERFUNCTION_RED",
7412 : "TIFFTAG_TRANSFERFUNCTION_GREEN",
7413 : "TIFFTAG_TRANSFERFUNCTION_BLUE",
7414 : "TIFFTAG_TRANSFERRANGE_BLACK",
7415 : "TIFFTAG_TRANSFERRANGE_WHITE",
7416 : nullptr};
7417 :
7418 : // Copy all the tags. Options will override tags in the source.
7419 2172 : int i = 0;
7420 23872 : while (pszOptionsMD[i] != nullptr)
7421 : {
7422 : char const *pszMD =
7423 21702 : CSLFetchNameValue(papszOptions, pszOptionsMD[i]);
7424 21702 : if (pszMD == nullptr)
7425 : pszMD =
7426 21694 : poSrcDS->GetMetadataItem(pszOptionsMD[i], "COLOR_PROFILE");
7427 :
7428 21702 : if ((pszMD != nullptr) && !EQUAL(pszMD, ""))
7429 : {
7430 16 : papszCreateOptions =
7431 16 : CSLSetNameValue(papszCreateOptions, pszOptionsMD[i], pszMD);
7432 :
7433 : // If an ICC profile exists, other tags are not needed.
7434 16 : if (EQUAL(pszOptionsMD[i], "SOURCE_ICC_PROFILE"))
7435 2 : break;
7436 : }
7437 :
7438 21700 : ++i;
7439 : }
7440 : }
7441 :
7442 2189 : double dfExtraSpaceForOverviews = 0;
7443 : const bool bCopySrcOverviews =
7444 2189 : CPLFetchBool(papszCreateOptions, "COPY_SRC_OVERVIEWS", false);
7445 2189 : std::unique_ptr<GDALDataset> poOvrDS;
7446 2189 : int nSrcOverviews = 0;
7447 2189 : if (bCopySrcOverviews)
7448 : {
7449 : const char *pszOvrDS =
7450 234 : CSLFetchNameValue(papszCreateOptions, "@OVERVIEW_DATASET");
7451 234 : if (pszOvrDS)
7452 : {
7453 : // Empty string is used by COG driver to indicate that we want
7454 : // to ignore source overviews.
7455 39 : if (!EQUAL(pszOvrDS, ""))
7456 : {
7457 37 : poOvrDS.reset(GDALDataset::Open(pszOvrDS));
7458 37 : if (!poOvrDS)
7459 : {
7460 0 : CSLDestroy(papszCreateOptions);
7461 0 : return nullptr;
7462 : }
7463 37 : if (poOvrDS->GetRasterCount() != l_nBands)
7464 : {
7465 0 : CSLDestroy(papszCreateOptions);
7466 0 : return nullptr;
7467 : }
7468 37 : nSrcOverviews =
7469 37 : poOvrDS->GetRasterBand(1)->GetOverviewCount() + 1;
7470 : }
7471 : }
7472 : else
7473 : {
7474 195 : nSrcOverviews = poSrcDS->GetRasterBand(1)->GetOverviewCount();
7475 : }
7476 :
7477 : // Limit number of overviews if specified
7478 : const char *pszOverviewCount =
7479 234 : CSLFetchNameValue(papszCreateOptions, "@OVERVIEW_COUNT");
7480 234 : if (pszOverviewCount)
7481 8 : nSrcOverviews =
7482 8 : std::max(0, std::min(nSrcOverviews, atoi(pszOverviewCount)));
7483 :
7484 234 : if (nSrcOverviews)
7485 : {
7486 208 : for (int j = 1; j <= l_nBands; ++j)
7487 : {
7488 : const int nOtherBandOverviewCount =
7489 136 : poOvrDS ? poOvrDS->GetRasterBand(j)->GetOverviewCount() + 1
7490 200 : : poSrcDS->GetRasterBand(j)->GetOverviewCount();
7491 136 : if (nOtherBandOverviewCount < nSrcOverviews)
7492 : {
7493 1 : ReportError(
7494 : pszFilename, CE_Failure, CPLE_NotSupported,
7495 : "COPY_SRC_OVERVIEWS cannot be used when the bands have "
7496 : "not the same number of overview levels.");
7497 1 : CSLDestroy(papszCreateOptions);
7498 1 : return nullptr;
7499 : }
7500 395 : for (int i = 0; i < nSrcOverviews; ++i)
7501 : {
7502 : GDALRasterBand *poOvrBand =
7503 : poOvrDS
7504 361 : ? (i == 0 ? poOvrDS->GetRasterBand(j)
7505 198 : : poOvrDS->GetRasterBand(j)->GetOverview(
7506 99 : i - 1))
7507 353 : : poSrcDS->GetRasterBand(j)->GetOverview(i);
7508 262 : if (poOvrBand == nullptr)
7509 : {
7510 1 : ReportError(
7511 : pszFilename, CE_Failure, CPLE_NotSupported,
7512 : "COPY_SRC_OVERVIEWS cannot be used when one "
7513 : "overview band is NULL.");
7514 1 : CSLDestroy(papszCreateOptions);
7515 1 : return nullptr;
7516 : }
7517 : GDALRasterBand *poOvrFirstBand =
7518 : poOvrDS
7519 360 : ? (i == 0 ? poOvrDS->GetRasterBand(1)
7520 198 : : poOvrDS->GetRasterBand(1)->GetOverview(
7521 99 : i - 1))
7522 351 : : poSrcDS->GetRasterBand(1)->GetOverview(i);
7523 521 : if (poOvrBand->GetXSize() != poOvrFirstBand->GetXSize() ||
7524 260 : poOvrBand->GetYSize() != poOvrFirstBand->GetYSize())
7525 : {
7526 1 : ReportError(
7527 : pszFilename, CE_Failure, CPLE_NotSupported,
7528 : "COPY_SRC_OVERVIEWS cannot be used when the "
7529 : "overview bands have not the same dimensions "
7530 : "among bands.");
7531 1 : CSLDestroy(papszCreateOptions);
7532 1 : return nullptr;
7533 : }
7534 : }
7535 : }
7536 :
7537 205 : for (int i = 0; i < nSrcOverviews; ++i)
7538 : {
7539 : GDALRasterBand *poOvrFirstBand =
7540 : poOvrDS
7541 211 : ? (i == 0
7542 78 : ? poOvrDS->GetRasterBand(1)
7543 41 : : poOvrDS->GetRasterBand(1)->GetOverview(i - 1))
7544 188 : : poSrcDS->GetRasterBand(1)->GetOverview(i);
7545 133 : dfExtraSpaceForOverviews +=
7546 133 : static_cast<double>(poOvrFirstBand->GetXSize()) *
7547 133 : poOvrFirstBand->GetYSize();
7548 : }
7549 72 : dfExtraSpaceForOverviews *=
7550 72 : l_nBands * GDALGetDataTypeSizeBytes(eType);
7551 : }
7552 : else
7553 : {
7554 159 : CPLDebug("GTiff", "No source overviews to copy");
7555 : }
7556 : }
7557 :
7558 : /* -------------------------------------------------------------------- */
7559 : /* Should we use optimized way of copying from an input JPEG */
7560 : /* dataset? */
7561 : /* -------------------------------------------------------------------- */
7562 :
7563 : // TODO(schwehr): Refactor bDirectCopyFromJPEG to be a const.
7564 : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
7565 2186 : bool bDirectCopyFromJPEG = false;
7566 : #endif
7567 :
7568 : // Note: JPEG_DIRECT_COPY is not defined by default, because it is mainly
7569 : // useful for debugging purposes.
7570 : #ifdef JPEG_DIRECT_COPY
7571 : if (CPLFetchBool(papszCreateOptions, "JPEG_DIRECT_COPY", false) &&
7572 : GTIFF_CanDirectCopyFromJPEG(poSrcDS, papszCreateOptions))
7573 : {
7574 : CPLDebug("GTiff", "Using special direct copy mode from a JPEG dataset");
7575 :
7576 : bDirectCopyFromJPEG = true;
7577 : }
7578 : #endif
7579 :
7580 : #ifdef HAVE_LIBJPEG
7581 2186 : bool bCopyFromJPEG = false;
7582 :
7583 : // When CreateCopy'ing() from a JPEG dataset, and asking for COMPRESS=JPEG,
7584 : // use DCT coefficients (unless other options are incompatible, like
7585 : // strip/tile dimensions, specifying JPEG_QUALITY option, incompatible
7586 : // PHOTOMETRIC with the source colorspace, etc.) to avoid the lossy steps
7587 : // involved by decompression/recompression.
7588 4372 : if (!bDirectCopyFromJPEG &&
7589 2186 : GTIFF_CanCopyFromJPEG(poSrcDS, papszCreateOptions))
7590 : {
7591 12 : CPLDebug("GTiff", "Using special copy mode from a JPEG dataset");
7592 :
7593 12 : bCopyFromJPEG = true;
7594 : }
7595 : #endif
7596 :
7597 : /* -------------------------------------------------------------------- */
7598 : /* If the source is RGB, then set the PHOTOMETRIC=RGB value */
7599 : /* -------------------------------------------------------------------- */
7600 :
7601 : const bool bForcePhotometric =
7602 2186 : CSLFetchNameValue(papszOptions, "PHOTOMETRIC") != nullptr;
7603 :
7604 1232 : if (l_nBands >= 3 && !bForcePhotometric &&
7605 : #ifdef HAVE_LIBJPEG
7606 1194 : !bCopyFromJPEG &&
7607 : #endif
7608 1188 : poSrcDS->GetRasterBand(1)->GetColorInterpretation() == GCI_RedBand &&
7609 4494 : poSrcDS->GetRasterBand(2)->GetColorInterpretation() == GCI_GreenBand &&
7610 1076 : poSrcDS->GetRasterBand(3)->GetColorInterpretation() == GCI_BlueBand)
7611 : {
7612 1070 : papszCreateOptions =
7613 1070 : CSLSetNameValue(papszCreateOptions, "PHOTOMETRIC", "RGB");
7614 : }
7615 :
7616 : /* -------------------------------------------------------------------- */
7617 : /* Create the file. */
7618 : /* -------------------------------------------------------------------- */
7619 2186 : VSILFILE *l_fpL = nullptr;
7620 4372 : CPLString l_osTmpFilename;
7621 :
7622 2186 : const int nXSize = poSrcDS->GetRasterXSize();
7623 2186 : const int nYSize = poSrcDS->GetRasterYSize();
7624 :
7625 : const int nColorTableMultiplier = std::max(
7626 4372 : 1,
7627 4372 : std::min(257,
7628 2186 : atoi(CSLFetchNameValueDef(
7629 : papszOptions, "COLOR_TABLE_MULTIPLIER",
7630 2186 : CPLSPrintf("%d", DEFAULT_COLOR_TABLE_MULTIPLIER_257)))));
7631 :
7632 2186 : bool bTileInterleaving = false;
7633 2186 : TIFF *l_hTIFF = CreateLL(pszFilename, nXSize, nYSize, l_nBands, eType,
7634 : dfExtraSpaceForOverviews, nColorTableMultiplier,
7635 : papszCreateOptions, &l_fpL, l_osTmpFilename,
7636 : /* bCreateCopy = */ true, bTileInterleaving);
7637 2186 : const bool bStreaming = !l_osTmpFilename.empty();
7638 :
7639 2186 : CSLDestroy(papszCreateOptions);
7640 2186 : papszCreateOptions = nullptr;
7641 :
7642 2186 : if (l_hTIFF == nullptr)
7643 : {
7644 18 : if (bStreaming)
7645 0 : VSIUnlink(l_osTmpFilename);
7646 18 : return nullptr;
7647 : }
7648 :
7649 2168 : uint16_t l_nPlanarConfig = 0;
7650 2168 : TIFFGetField(l_hTIFF, TIFFTAG_PLANARCONFIG, &l_nPlanarConfig);
7651 :
7652 2168 : uint16_t l_nCompression = 0;
7653 :
7654 2168 : if (!TIFFGetField(l_hTIFF, TIFFTAG_COMPRESSION, &(l_nCompression)))
7655 0 : l_nCompression = COMPRESSION_NONE;
7656 :
7657 : /* -------------------------------------------------------------------- */
7658 : /* Set the alpha channel if we find one. */
7659 : /* -------------------------------------------------------------------- */
7660 2168 : uint16_t *extraSamples = nullptr;
7661 2168 : uint16_t nExtraSamples = 0;
7662 2168 : if (TIFFGetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, &nExtraSamples,
7663 2433 : &extraSamples) &&
7664 265 : nExtraSamples > 0)
7665 : {
7666 : // We need to allocate a new array as (current) libtiff
7667 : // versions will not like that we reuse the array we got from
7668 : // TIFFGetField().
7669 : uint16_t *pasNewExtraSamples = static_cast<uint16_t *>(
7670 265 : CPLMalloc(nExtraSamples * sizeof(uint16_t)));
7671 265 : memcpy(pasNewExtraSamples, extraSamples,
7672 265 : nExtraSamples * sizeof(uint16_t));
7673 265 : const char *pszAlpha = CPLGetConfigOption(
7674 : "GTIFF_ALPHA", CSLFetchNameValue(papszOptions, "ALPHA"));
7675 : const uint16_t nAlpha =
7676 265 : GTiffGetAlphaValue(pszAlpha, DEFAULT_ALPHA_TYPE);
7677 265 : const int nBaseSamples = l_nBands - nExtraSamples;
7678 895 : for (int iExtraBand = nBaseSamples + 1; iExtraBand <= l_nBands;
7679 : iExtraBand++)
7680 : {
7681 630 : if (poSrcDS->GetRasterBand(iExtraBand)->GetColorInterpretation() ==
7682 : GCI_AlphaBand)
7683 : {
7684 145 : pasNewExtraSamples[iExtraBand - nBaseSamples - 1] = nAlpha;
7685 145 : if (!pszAlpha)
7686 : {
7687 : // Use the ALPHA metadata item from the source band, when
7688 : // present, if no explicit ALPHA creation option
7689 286 : pasNewExtraSamples[iExtraBand - nBaseSamples - 1] =
7690 143 : GTiffGetAlphaValue(
7691 143 : poSrcDS->GetRasterBand(iExtraBand)
7692 : ->GetMetadataItem("ALPHA",
7693 143 : GDAL_MDD_IMAGE_STRUCTURE),
7694 : nAlpha);
7695 : }
7696 : }
7697 : }
7698 265 : TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, nExtraSamples,
7699 : pasNewExtraSamples);
7700 :
7701 265 : CPLFree(pasNewExtraSamples);
7702 : }
7703 :
7704 : /* -------------------------------------------------------------------- */
7705 : /* If the output is jpeg compressed, and the input is RGB make */
7706 : /* sure we note that. */
7707 : /* -------------------------------------------------------------------- */
7708 :
7709 2168 : if (l_nCompression == COMPRESSION_JPEG)
7710 : {
7711 134 : if (l_nBands >= 3 &&
7712 58 : (poSrcDS->GetRasterBand(1)->GetColorInterpretation() ==
7713 0 : GCI_YCbCr_YBand) &&
7714 0 : (poSrcDS->GetRasterBand(2)->GetColorInterpretation() ==
7715 134 : GCI_YCbCr_CbBand) &&
7716 0 : (poSrcDS->GetRasterBand(3)->GetColorInterpretation() ==
7717 : GCI_YCbCr_CrBand))
7718 : {
7719 : // Do nothing.
7720 : }
7721 : else
7722 : {
7723 : // Assume RGB if it is not explicitly YCbCr.
7724 76 : CPLDebug("GTiff", "Setting JPEGCOLORMODE_RGB");
7725 76 : TIFFSetField(l_hTIFF, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
7726 : }
7727 : }
7728 :
7729 : /* -------------------------------------------------------------------- */
7730 : /* Does the source image consist of one band, with a palette? */
7731 : /* If so, copy over. */
7732 : /* -------------------------------------------------------------------- */
7733 1318 : if ((l_nBands == 1 || l_nBands == 2) &&
7734 3486 : poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr &&
7735 : eType == GDT_UInt8)
7736 : {
7737 21 : unsigned short anTRed[256] = {0};
7738 21 : unsigned short anTGreen[256] = {0};
7739 21 : unsigned short anTBlue[256] = {0};
7740 21 : GDALColorTable *poCT = poSrcDS->GetRasterBand(1)->GetColorTable();
7741 :
7742 5397 : for (int iColor = 0; iColor < 256; ++iColor)
7743 : {
7744 5376 : if (iColor < poCT->GetColorEntryCount())
7745 : {
7746 4241 : GDALColorEntry sRGB = {0, 0, 0, 0};
7747 :
7748 4241 : poCT->GetColorEntryAsRGB(iColor, &sRGB);
7749 :
7750 8482 : anTRed[iColor] = GTiffDataset::ClampCTEntry(
7751 4241 : iColor, 1, sRGB.c1, nColorTableMultiplier);
7752 8482 : anTGreen[iColor] = GTiffDataset::ClampCTEntry(
7753 4241 : iColor, 2, sRGB.c2, nColorTableMultiplier);
7754 4241 : anTBlue[iColor] = GTiffDataset::ClampCTEntry(
7755 4241 : iColor, 3, sRGB.c3, nColorTableMultiplier);
7756 : }
7757 : else
7758 : {
7759 1135 : anTRed[iColor] = 0;
7760 1135 : anTGreen[iColor] = 0;
7761 1135 : anTBlue[iColor] = 0;
7762 : }
7763 : }
7764 :
7765 21 : if (!bForcePhotometric)
7766 21 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
7767 21 : TIFFSetField(l_hTIFF, TIFFTAG_COLORMAP, anTRed, anTGreen, anTBlue);
7768 : }
7769 1317 : else if ((l_nBands == 1 || l_nBands == 2) &&
7770 3464 : poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr &&
7771 : eType == GDT_UInt16)
7772 : {
7773 : unsigned short *panTRed = static_cast<unsigned short *>(
7774 1 : CPLMalloc(65536 * sizeof(unsigned short)));
7775 : unsigned short *panTGreen = static_cast<unsigned short *>(
7776 1 : CPLMalloc(65536 * sizeof(unsigned short)));
7777 : unsigned short *panTBlue = static_cast<unsigned short *>(
7778 1 : CPLMalloc(65536 * sizeof(unsigned short)));
7779 :
7780 1 : GDALColorTable *poCT = poSrcDS->GetRasterBand(1)->GetColorTable();
7781 :
7782 65537 : for (int iColor = 0; iColor < 65536; ++iColor)
7783 : {
7784 65536 : if (iColor < poCT->GetColorEntryCount())
7785 : {
7786 65536 : GDALColorEntry sRGB = {0, 0, 0, 0};
7787 :
7788 65536 : poCT->GetColorEntryAsRGB(iColor, &sRGB);
7789 :
7790 131072 : panTRed[iColor] = GTiffDataset::ClampCTEntry(
7791 65536 : iColor, 1, sRGB.c1, nColorTableMultiplier);
7792 131072 : panTGreen[iColor] = GTiffDataset::ClampCTEntry(
7793 65536 : iColor, 2, sRGB.c2, nColorTableMultiplier);
7794 65536 : panTBlue[iColor] = GTiffDataset::ClampCTEntry(
7795 65536 : iColor, 3, sRGB.c3, nColorTableMultiplier);
7796 : }
7797 : else
7798 : {
7799 0 : panTRed[iColor] = 0;
7800 0 : panTGreen[iColor] = 0;
7801 0 : panTBlue[iColor] = 0;
7802 : }
7803 : }
7804 :
7805 1 : if (!bForcePhotometric)
7806 1 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
7807 1 : TIFFSetField(l_hTIFF, TIFFTAG_COLORMAP, panTRed, panTGreen, panTBlue);
7808 :
7809 1 : CPLFree(panTRed);
7810 1 : CPLFree(panTGreen);
7811 1 : CPLFree(panTBlue);
7812 : }
7813 2146 : else if (poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr)
7814 1 : ReportError(
7815 : pszFilename, CE_Failure, CPLE_AppDefined,
7816 : "Unable to export color table to GeoTIFF file. Color tables "
7817 : "can only be written to 1 band or 2 bands Byte or "
7818 : "UInt16 GeoTIFF files.");
7819 :
7820 2168 : if (l_nCompression == COMPRESSION_JPEG)
7821 : {
7822 76 : uint16_t l_nPhotometric = 0;
7823 76 : TIFFGetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, &l_nPhotometric);
7824 : // Check done in tif_jpeg.c later, but not with a very clear error
7825 : // message
7826 76 : if (l_nPhotometric == PHOTOMETRIC_PALETTE)
7827 : {
7828 1 : ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
7829 : "JPEG compression not supported with paletted image");
7830 1 : XTIFFClose(l_hTIFF);
7831 1 : VSIUnlink(l_osTmpFilename);
7832 1 : CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
7833 1 : return nullptr;
7834 : }
7835 : }
7836 :
7837 2254 : if (l_nBands == 2 &&
7838 2167 : poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr &&
7839 0 : (eType == GDT_UInt8 || eType == GDT_UInt16))
7840 : {
7841 1 : uint16_t v[1] = {EXTRASAMPLE_UNASSALPHA};
7842 :
7843 1 : TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, 1, v);
7844 : }
7845 :
7846 2167 : const int nMaskFlags = poSrcDS->GetRasterBand(1)->GetMaskFlags();
7847 2167 : bool bCreateMask = false;
7848 4334 : CPLString osHiddenStructuralMD;
7849 : const char *pszInterleave =
7850 2167 : CSLFetchNameValueDef(papszOptions, GDALMD_INTERLEAVE, "PIXEL");
7851 2395 : if (bCopySrcOverviews &&
7852 228 : CPLTestBool(CSLFetchNameValueDef(papszOptions, "TILED", "NO")))
7853 : {
7854 216 : osHiddenStructuralMD += "LAYOUT=IFDS_BEFORE_DATA\n";
7855 216 : osHiddenStructuralMD += "BLOCK_ORDER=ROW_MAJOR\n";
7856 216 : osHiddenStructuralMD += "BLOCK_LEADER=SIZE_AS_UINT4\n";
7857 216 : osHiddenStructuralMD += "BLOCK_TRAILER=LAST_4_BYTES_REPEATED\n";
7858 216 : if (l_nBands > 1 && !EQUAL(pszInterleave, "PIXEL"))
7859 : {
7860 22 : osHiddenStructuralMD += "INTERLEAVE=";
7861 22 : osHiddenStructuralMD += CPLString(pszInterleave).toupper();
7862 22 : osHiddenStructuralMD += "\n";
7863 : }
7864 : osHiddenStructuralMD +=
7865 216 : "KNOWN_INCOMPATIBLE_EDITION=NO\n "; // Final space intended, so
7866 : // this can be replaced by YES
7867 : }
7868 2167 : if (!(nMaskFlags & (GMF_ALL_VALID | GMF_ALPHA | GMF_NODATA)) &&
7869 42 : (nMaskFlags & GMF_PER_DATASET) && !bStreaming)
7870 : {
7871 38 : bCreateMask = true;
7872 38 : if (GTiffDataset::MustCreateInternalMask() &&
7873 38 : !osHiddenStructuralMD.empty() && EQUAL(pszInterleave, "PIXEL"))
7874 : {
7875 21 : osHiddenStructuralMD += "MASK_INTERLEAVED_WITH_IMAGERY=YES\n";
7876 : }
7877 : }
7878 2383 : if (!osHiddenStructuralMD.empty() &&
7879 216 : CPLTestBool(CPLGetConfigOption("GTIFF_WRITE_COG_GHOST_AREA", "YES")))
7880 : {
7881 215 : const int nHiddenMDSize = static_cast<int>(osHiddenStructuralMD.size());
7882 : osHiddenStructuralMD =
7883 215 : CPLOPrintf("GDAL_STRUCTURAL_METADATA_SIZE=%06d bytes\n",
7884 430 : nHiddenMDSize) +
7885 215 : osHiddenStructuralMD;
7886 215 : VSI_TIFFWrite(l_hTIFF, osHiddenStructuralMD.c_str(),
7887 : osHiddenStructuralMD.size());
7888 : }
7889 :
7890 : // FIXME? libtiff writes extended tags in the order they are specified
7891 : // and not in increasing order.
7892 :
7893 : /* -------------------------------------------------------------------- */
7894 : /* Transfer some TIFF specific metadata, if available. */
7895 : /* The return value will tell us if we need to try again later with*/
7896 : /* PAM because the profile doesn't allow to write some metadata */
7897 : /* as TIFF tag */
7898 : /* -------------------------------------------------------------------- */
7899 2167 : const bool bHasWrittenMDInGeotiffTAG = GTiffDataset::WriteMetadata(
7900 : poSrcDS, l_hTIFF, false, eProfile, pszFilename, papszOptions);
7901 :
7902 : /* -------------------------------------------------------------------- */
7903 : /* Write NoData value, if exist. */
7904 : /* -------------------------------------------------------------------- */
7905 2167 : if (eProfile == GTiffProfile::GDALGEOTIFF)
7906 : {
7907 2146 : int bSuccess = FALSE;
7908 2146 : GDALRasterBand *poFirstBand = poSrcDS->GetRasterBand(1);
7909 2146 : if (poFirstBand->GetRasterDataType() == GDT_Int64)
7910 : {
7911 4 : const auto nNoData = poFirstBand->GetNoDataValueAsInt64(&bSuccess);
7912 4 : if (bSuccess)
7913 1 : GTiffDataset::WriteNoDataValue(l_hTIFF, nNoData);
7914 : }
7915 2142 : else if (poFirstBand->GetRasterDataType() == GDT_UInt64)
7916 : {
7917 4 : const auto nNoData = poFirstBand->GetNoDataValueAsUInt64(&bSuccess);
7918 4 : if (bSuccess)
7919 1 : GTiffDataset::WriteNoDataValue(l_hTIFF, nNoData);
7920 : }
7921 : else
7922 : {
7923 2138 : const auto dfNoData = poFirstBand->GetNoDataValue(&bSuccess);
7924 2138 : if (bSuccess)
7925 150 : GTiffDataset::WriteNoDataValue(l_hTIFF, dfNoData);
7926 : }
7927 : }
7928 :
7929 : /* -------------------------------------------------------------------- */
7930 : /* Are we addressing PixelIsPoint mode? */
7931 : /* -------------------------------------------------------------------- */
7932 2167 : bool bPixelIsPoint = false;
7933 2167 : bool bPointGeoIgnore = false;
7934 :
7935 3646 : if (poSrcDS->GetMetadataItem(GDALMD_AREA_OR_POINT) &&
7936 1479 : EQUAL(poSrcDS->GetMetadataItem(GDALMD_AREA_OR_POINT), GDALMD_AOP_POINT))
7937 : {
7938 10 : bPixelIsPoint = true;
7939 : bPointGeoIgnore =
7940 10 : CPLTestBool(CPLGetConfigOption("GTIFF_POINT_GEO_IGNORE", "FALSE"));
7941 : }
7942 :
7943 : /* -------------------------------------------------------------------- */
7944 : /* Write affine transform if it is meaningful. */
7945 : /* -------------------------------------------------------------------- */
7946 2167 : const OGRSpatialReference *l_poSRS = nullptr;
7947 2167 : GDALGeoTransform l_gt;
7948 2167 : if (poSrcDS->GetGeoTransform(l_gt) == CE_None)
7949 : {
7950 1728 : if (bGeoTIFF)
7951 : {
7952 1723 : l_poSRS = poSrcDS->GetSpatialRef();
7953 :
7954 1723 : if (l_gt.xrot == 0.0 && l_gt.yrot == 0.0 && l_gt.yscale < 0.0)
7955 : {
7956 1715 : double dfOffset = 0.0;
7957 : {
7958 : // In the case the SRS has a vertical component and we have
7959 : // a single band, encode its scale/offset in the GeoTIFF
7960 : // tags
7961 1715 : int bHasScale = FALSE;
7962 : double dfScale =
7963 1715 : poSrcDS->GetRasterBand(1)->GetScale(&bHasScale);
7964 1715 : int bHasOffset = FALSE;
7965 : dfOffset =
7966 1715 : poSrcDS->GetRasterBand(1)->GetOffset(&bHasOffset);
7967 : const bool bApplyScaleOffset =
7968 1719 : l_poSRS && l_poSRS->IsVertical() &&
7969 4 : poSrcDS->GetRasterCount() == 1;
7970 1715 : if (bApplyScaleOffset && !bHasScale)
7971 0 : dfScale = 1.0;
7972 1715 : if (!bApplyScaleOffset || !bHasOffset)
7973 1711 : dfOffset = 0.0;
7974 : const double adfPixelScale[3] = {
7975 1715 : l_gt.xscale, fabs(l_gt.yscale),
7976 1715 : bApplyScaleOffset ? dfScale : 0.0};
7977 :
7978 1715 : TIFFSetField(l_hTIFF, TIFFTAG_GEOPIXELSCALE, 3,
7979 : adfPixelScale);
7980 : }
7981 :
7982 1715 : double adfTiePoints[6] = {0.0, 0.0, 0.0,
7983 1715 : l_gt.xorig, l_gt.yorig, dfOffset};
7984 :
7985 1715 : if (bPixelIsPoint && !bPointGeoIgnore)
7986 : {
7987 6 : adfTiePoints[3] += l_gt.xscale * 0.5 + l_gt.xrot * 0.5;
7988 6 : adfTiePoints[4] += l_gt.yrot * 0.5 + l_gt.yscale * 0.5;
7989 : }
7990 :
7991 1715 : TIFFSetField(l_hTIFF, TIFFTAG_GEOTIEPOINTS, 6, adfTiePoints);
7992 : }
7993 : else
7994 : {
7995 8 : double adfMatrix[16] = {0.0};
7996 :
7997 8 : adfMatrix[0] = l_gt.xscale;
7998 8 : adfMatrix[1] = l_gt.xrot;
7999 8 : adfMatrix[3] = l_gt.xorig;
8000 8 : adfMatrix[4] = l_gt.yrot;
8001 8 : adfMatrix[5] = l_gt.yscale;
8002 8 : adfMatrix[7] = l_gt.yorig;
8003 8 : adfMatrix[15] = 1.0;
8004 :
8005 8 : if (bPixelIsPoint && !bPointGeoIgnore)
8006 : {
8007 0 : adfMatrix[3] += l_gt.xscale * 0.5 + l_gt.xrot * 0.5;
8008 0 : adfMatrix[7] += l_gt.yrot * 0.5 + l_gt.yscale * 0.5;
8009 : }
8010 :
8011 8 : TIFFSetField(l_hTIFF, TIFFTAG_GEOTRANSMATRIX, 16, adfMatrix);
8012 : }
8013 : }
8014 :
8015 : /* --------------------------------------------------------------------
8016 : */
8017 : /* Do we need a TFW file? */
8018 : /* --------------------------------------------------------------------
8019 : */
8020 1728 : if (CPLFetchBool(papszOptions, "TFW", false))
8021 2 : GDALWriteWorldFile(pszFilename, "tfw", l_gt.data());
8022 1726 : else if (CPLFetchBool(papszOptions, "WORLDFILE", false))
8023 1 : GDALWriteWorldFile(pszFilename, "wld", l_gt.data());
8024 : }
8025 :
8026 : /* -------------------------------------------------------------------- */
8027 : /* Otherwise write tiepoints if they are available. */
8028 : /* -------------------------------------------------------------------- */
8029 439 : else if (poSrcDS->GetGCPCount() > 0 && bGeoTIFF)
8030 : {
8031 11 : const GDAL_GCP *pasGCPs = poSrcDS->GetGCPs();
8032 : double *padfTiePoints = static_cast<double *>(
8033 11 : CPLMalloc(6 * sizeof(double) * poSrcDS->GetGCPCount()));
8034 :
8035 55 : for (int iGCP = 0; iGCP < poSrcDS->GetGCPCount(); ++iGCP)
8036 : {
8037 :
8038 44 : padfTiePoints[iGCP * 6 + 0] = pasGCPs[iGCP].dfGCPPixel;
8039 44 : padfTiePoints[iGCP * 6 + 1] = pasGCPs[iGCP].dfGCPLine;
8040 44 : padfTiePoints[iGCP * 6 + 2] = 0;
8041 44 : padfTiePoints[iGCP * 6 + 3] = pasGCPs[iGCP].dfGCPX;
8042 44 : padfTiePoints[iGCP * 6 + 4] = pasGCPs[iGCP].dfGCPY;
8043 44 : padfTiePoints[iGCP * 6 + 5] = pasGCPs[iGCP].dfGCPZ;
8044 :
8045 44 : if (bPixelIsPoint && !bPointGeoIgnore)
8046 : {
8047 4 : padfTiePoints[iGCP * 6 + 0] -= 0.5;
8048 4 : padfTiePoints[iGCP * 6 + 1] -= 0.5;
8049 : }
8050 : }
8051 :
8052 11 : TIFFSetField(l_hTIFF, TIFFTAG_GEOTIEPOINTS, 6 * poSrcDS->GetGCPCount(),
8053 : padfTiePoints);
8054 11 : CPLFree(padfTiePoints);
8055 :
8056 11 : l_poSRS = poSrcDS->GetGCPSpatialRef();
8057 :
8058 22 : if (CPLFetchBool(papszOptions, "TFW", false) ||
8059 11 : CPLFetchBool(papszOptions, "WORLDFILE", false))
8060 : {
8061 0 : ReportError(
8062 : pszFilename, CE_Warning, CPLE_AppDefined,
8063 : "TFW=ON or WORLDFILE=ON creation options are ignored when "
8064 : "GCPs are available");
8065 : }
8066 : }
8067 : else
8068 : {
8069 428 : l_poSRS = poSrcDS->GetSpatialRef();
8070 : }
8071 :
8072 : /* -------------------------------------------------------------------- */
8073 : /* Copy xml:XMP data */
8074 : /* -------------------------------------------------------------------- */
8075 2167 : CSLConstList papszXMP = poSrcDS->GetMetadata("xml:XMP");
8076 2167 : if (papszXMP != nullptr && *papszXMP != nullptr)
8077 : {
8078 9 : int nTagSize = static_cast<int>(strlen(*papszXMP));
8079 9 : TIFFSetField(l_hTIFF, TIFFTAG_XMLPACKET, nTagSize, *papszXMP);
8080 : }
8081 :
8082 : /* -------------------------------------------------------------------- */
8083 : /* Write the projection information, if possible. */
8084 : /* -------------------------------------------------------------------- */
8085 2167 : const bool bHasProjection = l_poSRS != nullptr;
8086 2167 : bool bExportSRSToPAM = false;
8087 2167 : if ((bHasProjection || bPixelIsPoint) && bGeoTIFF)
8088 : {
8089 1701 : GTIF *psGTIF = GTiffDataset::GTIFNew(l_hTIFF);
8090 :
8091 1701 : if (bHasProjection)
8092 : {
8093 1701 : const auto eGeoTIFFKeysFlavor = GetGTIFFKeysFlavor(papszOptions);
8094 1701 : if (IsSRSCompatibleOfGeoTIFF(l_poSRS, eGeoTIFFKeysFlavor))
8095 : {
8096 1701 : GTIFSetFromOGISDefnEx(
8097 : psGTIF,
8098 : OGRSpatialReference::ToHandle(
8099 : const_cast<OGRSpatialReference *>(l_poSRS)),
8100 : eGeoTIFFKeysFlavor, GetGeoTIFFVersion(papszOptions));
8101 : }
8102 : else
8103 : {
8104 0 : bExportSRSToPAM = true;
8105 : }
8106 : }
8107 :
8108 1701 : if (bPixelIsPoint)
8109 : {
8110 10 : GTIFKeySet(psGTIF, GTRasterTypeGeoKey, TYPE_SHORT, 1,
8111 : RasterPixelIsPoint);
8112 : }
8113 :
8114 1701 : GTIFWriteKeys(psGTIF);
8115 1701 : GTIFFree(psGTIF);
8116 : }
8117 :
8118 2167 : bool l_bDontReloadFirstBlock = false;
8119 :
8120 : #ifdef HAVE_LIBJPEG
8121 2167 : if (bCopyFromJPEG)
8122 : {
8123 12 : GTIFF_CopyFromJPEG_WriteAdditionalTags(l_hTIFF, poSrcDS);
8124 : }
8125 : #endif
8126 :
8127 : /* -------------------------------------------------------------------- */
8128 : /* Cleanup */
8129 : /* -------------------------------------------------------------------- */
8130 2167 : if (bCopySrcOverviews)
8131 : {
8132 228 : TIFFDeferStrileArrayWriting(l_hTIFF);
8133 : }
8134 2167 : TIFFWriteCheck(l_hTIFF, TIFFIsTiled(l_hTIFF), "GTiffCreateCopy()");
8135 2167 : TIFFWriteDirectory(l_hTIFF);
8136 2167 : if (bStreaming)
8137 : {
8138 : // We need to write twice the directory to be sure that custom
8139 : // TIFF tags are correctly sorted and that padding bytes have been
8140 : // added.
8141 5 : TIFFSetDirectory(l_hTIFF, 0);
8142 5 : TIFFWriteDirectory(l_hTIFF);
8143 :
8144 5 : if (VSIFSeekL(l_fpL, 0, SEEK_END) != 0)
8145 0 : ReportError(pszFilename, CE_Failure, CPLE_FileIO, "Cannot seek");
8146 5 : const int nSize = static_cast<int>(VSIFTellL(l_fpL));
8147 :
8148 5 : vsi_l_offset nDataLength = 0;
8149 5 : VSIGetMemFileBuffer(l_osTmpFilename, &nDataLength, FALSE);
8150 5 : TIFFSetDirectory(l_hTIFF, 0);
8151 5 : GTiffFillStreamableOffsetAndCount(l_hTIFF, nSize);
8152 5 : TIFFWriteDirectory(l_hTIFF);
8153 : }
8154 2167 : const auto nDirCount = TIFFNumberOfDirectories(l_hTIFF);
8155 2167 : if (nDirCount >= 1)
8156 : {
8157 2160 : TIFFSetDirectory(l_hTIFF, static_cast<tdir_t>(nDirCount - 1));
8158 : }
8159 2167 : const toff_t l_nDirOffset = TIFFCurrentDirOffset(l_hTIFF);
8160 2167 : TIFFFlush(l_hTIFF);
8161 2167 : XTIFFClose(l_hTIFF);
8162 :
8163 2167 : VSIFSeekL(l_fpL, 0, SEEK_SET);
8164 :
8165 : // fpStreaming will assigned to the instance and not closed here.
8166 2167 : VSILFILE *fpStreaming = nullptr;
8167 2167 : if (bStreaming)
8168 : {
8169 5 : vsi_l_offset nDataLength = 0;
8170 : void *pabyBuffer =
8171 5 : VSIGetMemFileBuffer(l_osTmpFilename, &nDataLength, FALSE);
8172 5 : fpStreaming = VSIFOpenL(pszFilename, "wb");
8173 5 : if (fpStreaming == nullptr)
8174 : {
8175 1 : VSIUnlink(l_osTmpFilename);
8176 1 : CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
8177 1 : return nullptr;
8178 : }
8179 4 : if (static_cast<vsi_l_offset>(VSIFWriteL(pabyBuffer, 1,
8180 : static_cast<int>(nDataLength),
8181 4 : fpStreaming)) != nDataLength)
8182 : {
8183 0 : ReportError(pszFilename, CE_Failure, CPLE_FileIO,
8184 : "Could not write %d bytes",
8185 : static_cast<int>(nDataLength));
8186 0 : CPL_IGNORE_RET_VAL(VSIFCloseL(fpStreaming));
8187 0 : VSIUnlink(l_osTmpFilename);
8188 0 : CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
8189 0 : return nullptr;
8190 : }
8191 : }
8192 :
8193 : /* -------------------------------------------------------------------- */
8194 : /* Re-open as a dataset and copy over missing metadata using */
8195 : /* PAM facilities. */
8196 : /* -------------------------------------------------------------------- */
8197 2166 : l_hTIFF = VSI_TIFFOpen(bStreaming ? l_osTmpFilename.c_str() : pszFilename,
8198 : "r+", l_fpL);
8199 2166 : if (l_hTIFF == nullptr)
8200 : {
8201 11 : if (bStreaming)
8202 0 : VSIUnlink(l_osTmpFilename);
8203 11 : l_fpL->CancelCreation();
8204 11 : CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
8205 11 : return nullptr;
8206 : }
8207 :
8208 : /* -------------------------------------------------------------------- */
8209 : /* Create a corresponding GDALDataset. */
8210 : /* -------------------------------------------------------------------- */
8211 4310 : auto poDS = std::make_unique<GTiffDataset>();
8212 : const bool bSuppressASAP =
8213 2155 : CPLTestBool(CSLFetchNameValueDef(papszOptions, "@SUPPRESS_ASAP", "NO"));
8214 2155 : if (bSuppressASAP)
8215 4 : poDS->MarkSuppressOnClose();
8216 2155 : poDS->SetDescription(pszFilename);
8217 2155 : poDS->eAccess = GA_Update;
8218 2155 : poDS->m_osFilename = pszFilename;
8219 2155 : poDS->m_fpL = l_fpL;
8220 2155 : poDS->m_bIMDRPCMetadataLoaded = true;
8221 2155 : poDS->m_nColorTableMultiplier = nColorTableMultiplier;
8222 2155 : poDS->m_bTileInterleave = bTileInterleaving;
8223 :
8224 2155 : if (bTileInterleaving)
8225 : {
8226 7 : poDS->m_oGTiffMDMD.SetMetadataItem(GDALMD_INTERLEAVE, "TILE",
8227 : GDAL_MDD_IMAGE_STRUCTURE);
8228 : }
8229 :
8230 2155 : const bool bAppend = CPLFetchBool(papszOptions, "APPEND_SUBDATASET", false);
8231 4309 : if (poDS->OpenOffset(l_hTIFF,
8232 2154 : bAppend ? l_nDirOffset : TIFFCurrentDirOffset(l_hTIFF),
8233 : GA_Update,
8234 : false, // bAllowRGBAInterface
8235 : true // bReadGeoTransform
8236 2155 : ) != CE_None)
8237 : {
8238 0 : l_fpL->CancelCreation();
8239 0 : poDS.reset();
8240 0 : if (bStreaming)
8241 0 : VSIUnlink(l_osTmpFilename);
8242 0 : return nullptr;
8243 : }
8244 :
8245 : // Legacy... Patch back GDT_Int8 type to GDT_UInt8 if the user used
8246 : // PIXELTYPE=SIGNEDBYTE
8247 2155 : const char *pszPixelType = CSLFetchNameValue(papszOptions, "PIXELTYPE");
8248 2155 : if (pszPixelType == nullptr)
8249 2150 : pszPixelType = "";
8250 2155 : if (eType == GDT_UInt8 && EQUAL(pszPixelType, "SIGNEDBYTE"))
8251 : {
8252 10 : for (int i = 0; i < poDS->nBands; ++i)
8253 : {
8254 5 : auto poBand = static_cast<GTiffRasterBand *>(poDS->papoBands[i]);
8255 5 : poBand->eDataType = GDT_UInt8;
8256 5 : poBand->EnablePixelTypeSignedByteWarning(false);
8257 5 : poBand->SetMetadataItem("PIXELTYPE", "SIGNEDBYTE",
8258 : GDAL_MDD_IMAGE_STRUCTURE);
8259 5 : poBand->EnablePixelTypeSignedByteWarning(true);
8260 : }
8261 : }
8262 :
8263 2155 : poDS->oOvManager.Initialize(poDS.get(), pszFilename);
8264 :
8265 2155 : if (bStreaming)
8266 : {
8267 4 : VSIUnlink(l_osTmpFilename);
8268 4 : poDS->m_fpToWrite = fpStreaming;
8269 : }
8270 2155 : poDS->m_eProfile = eProfile;
8271 :
8272 2155 : int nCloneInfoFlags = GCIF_PAM_DEFAULT & ~GCIF_MASK;
8273 :
8274 : // If we explicitly asked not to tag the alpha band as such, do not
8275 : // reintroduce this alpha color interpretation in PAM.
8276 2155 : if (poSrcDS->GetRasterBand(l_nBands)->GetColorInterpretation() ==
8277 2283 : GCI_AlphaBand &&
8278 128 : GTiffGetAlphaValue(
8279 : CPLGetConfigOption("GTIFF_ALPHA",
8280 : CSLFetchNameValue(papszOptions, "ALPHA")),
8281 : DEFAULT_ALPHA_TYPE) == EXTRASAMPLE_UNSPECIFIED)
8282 : {
8283 1 : nCloneInfoFlags &= ~GCIF_COLORINTERP;
8284 : }
8285 : // Ignore source band color interpretation if requesting PHOTOMETRIC=RGB
8286 3383 : else if (l_nBands >= 3 &&
8287 1229 : EQUAL(CSLFetchNameValueDef(papszOptions, "PHOTOMETRIC", ""),
8288 : "RGB"))
8289 : {
8290 28 : for (int i = 1; i <= 3; i++)
8291 : {
8292 21 : poDS->GetRasterBand(i)->SetColorInterpretation(
8293 21 : static_cast<GDALColorInterp>(GCI_RedBand + (i - 1)));
8294 : }
8295 7 : nCloneInfoFlags &= ~GCIF_COLORINTERP;
8296 9 : if (!(l_nBands == 4 &&
8297 2 : CSLFetchNameValue(papszOptions, "ALPHA") != nullptr))
8298 : {
8299 15 : for (int i = 4; i <= l_nBands; i++)
8300 : {
8301 18 : poDS->GetRasterBand(i)->SetColorInterpretation(
8302 9 : poSrcDS->GetRasterBand(i)->GetColorInterpretation());
8303 : }
8304 : }
8305 : }
8306 :
8307 : CPLString osOldGTIFF_REPORT_COMPD_CSVal(
8308 4310 : CPLGetConfigOption("GTIFF_REPORT_COMPD_CS", ""));
8309 2155 : CPLSetThreadLocalConfigOption("GTIFF_REPORT_COMPD_CS", "YES");
8310 2155 : poDS->CloneInfo(poSrcDS, nCloneInfoFlags);
8311 2155 : CPLSetThreadLocalConfigOption("GTIFF_REPORT_COMPD_CS",
8312 2155 : osOldGTIFF_REPORT_COMPD_CSVal.empty()
8313 : ? nullptr
8314 0 : : osOldGTIFF_REPORT_COMPD_CSVal.c_str());
8315 :
8316 2172 : if ((!bGeoTIFF || bExportSRSToPAM) &&
8317 17 : (poDS->GetPamFlags() & GPF_DISABLED) == 0)
8318 : {
8319 : // Copy georeferencing info to PAM if the profile is not GeoTIFF
8320 16 : poDS->GDALPamDataset::SetSpatialRef(poDS->GetSpatialRef());
8321 16 : GDALGeoTransform gt;
8322 16 : if (poDS->GetGeoTransform(gt) == CE_None)
8323 : {
8324 5 : poDS->GDALPamDataset::SetGeoTransform(gt);
8325 : }
8326 16 : poDS->GDALPamDataset::SetGCPs(poDS->GetGCPCount(), poDS->GetGCPs(),
8327 : poDS->GetGCPSpatialRef());
8328 : }
8329 :
8330 2155 : poDS->m_papszCreationOptions = CSLDuplicate(papszOptions);
8331 2155 : poDS->m_bDontReloadFirstBlock = l_bDontReloadFirstBlock;
8332 :
8333 : /* -------------------------------------------------------------------- */
8334 : /* CloneInfo() does not merge metadata, it just replaces it */
8335 : /* totally. So we have to merge it. */
8336 : /* -------------------------------------------------------------------- */
8337 :
8338 2155 : CSLConstList papszSRC_MD = poSrcDS->GetMetadata();
8339 2155 : char **papszDST_MD = CSLDuplicate(poDS->GetMetadata());
8340 :
8341 2155 : papszDST_MD = CSLMerge(papszDST_MD, papszSRC_MD);
8342 :
8343 2155 : poDS->SetMetadata(papszDST_MD);
8344 2155 : CSLDestroy(papszDST_MD);
8345 :
8346 : // Depending on the PHOTOMETRIC tag, the TIFF file may not have the same
8347 : // band count as the source. Will fail later in GDALDatasetCopyWholeRaster
8348 : // anyway.
8349 7239 : for (int nBand = 1;
8350 7239 : nBand <= std::min(poDS->GetRasterCount(), poSrcDS->GetRasterCount());
8351 : ++nBand)
8352 : {
8353 5084 : GDALRasterBand *poSrcBand = poSrcDS->GetRasterBand(nBand);
8354 5084 : GDALRasterBand *poDstBand = poDS->GetRasterBand(nBand);
8355 5084 : papszSRC_MD = poSrcBand->GetMetadata();
8356 5084 : papszDST_MD = CSLDuplicate(poDstBand->GetMetadata());
8357 :
8358 5084 : papszDST_MD = CSLMerge(papszDST_MD, papszSRC_MD);
8359 :
8360 5084 : poDstBand->SetMetadata(papszDST_MD);
8361 5084 : CSLDestroy(papszDST_MD);
8362 :
8363 5084 : char **papszCatNames = poSrcBand->GetCategoryNames();
8364 5084 : if (nullptr != papszCatNames)
8365 0 : poDstBand->SetCategoryNames(papszCatNames);
8366 : }
8367 :
8368 2155 : l_hTIFF = static_cast<TIFF *>(poDS->GetInternalHandle("TIFF_HANDLE"));
8369 :
8370 : /* -------------------------------------------------------------------- */
8371 : /* Handle forcing xml:ESRI data to be written to PAM. */
8372 : /* -------------------------------------------------------------------- */
8373 2155 : if (CPLTestBool(CPLGetConfigOption("ESRI_XML_PAM", "NO")))
8374 : {
8375 1 : CSLConstList papszESRIMD = poSrcDS->GetMetadata("xml:ESRI");
8376 1 : if (papszESRIMD)
8377 : {
8378 1 : poDS->SetMetadata(papszESRIMD, "xml:ESRI");
8379 : }
8380 : }
8381 :
8382 : /* -------------------------------------------------------------------- */
8383 : /* Second chance: now that we have a PAM dataset, it is possible */
8384 : /* to write metadata that we could not write as a TIFF tag. */
8385 : /* -------------------------------------------------------------------- */
8386 2155 : if (!bHasWrittenMDInGeotiffTAG && !bStreaming)
8387 : {
8388 6 : GTiffDataset::WriteMetadata(
8389 6 : poDS.get(), l_hTIFF, true, eProfile, pszFilename, papszOptions,
8390 : true /* don't write RPC and IMD file again */);
8391 : }
8392 :
8393 2155 : if (!bStreaming)
8394 2151 : GTiffDataset::WriteRPC(poDS.get(), l_hTIFF, true, eProfile, pszFilename,
8395 : papszOptions,
8396 : true /* write only in PAM AND if needed */);
8397 :
8398 2155 : poDS->m_bWriteCOGLayout = bCopySrcOverviews;
8399 :
8400 : // To avoid unnecessary directory rewriting.
8401 2155 : poDS->m_bMetadataChanged = false;
8402 2155 : poDS->m_bGeoTIFFInfoChanged = false;
8403 2155 : poDS->m_bNoDataChanged = false;
8404 2155 : poDS->m_bForceUnsetGTOrGCPs = false;
8405 2155 : poDS->m_bForceUnsetProjection = false;
8406 2155 : poDS->m_bStreamingOut = bStreaming;
8407 :
8408 : // Don't try to load external metadata files (#6597).
8409 2155 : poDS->m_bIMDRPCMetadataLoaded = true;
8410 :
8411 : // We must re-set the compression level at this point, since it has been
8412 : // lost a few lines above when closing the newly create TIFF file The
8413 : // TIFFTAG_ZIPQUALITY & TIFFTAG_JPEGQUALITY are not store in the TIFF file.
8414 : // They are just TIFF session parameters.
8415 :
8416 2155 : poDS->m_nZLevel = GTiffGetZLevel(papszOptions);
8417 2155 : poDS->m_nLZMAPreset = GTiffGetLZMAPreset(papszOptions);
8418 2155 : poDS->m_nZSTDLevel = GTiffGetZSTDPreset(papszOptions);
8419 2155 : poDS->m_nWebPLevel = GTiffGetWebPLevel(papszOptions);
8420 2155 : poDS->m_bWebPLossless = GTiffGetWebPLossless(papszOptions);
8421 2158 : if (poDS->m_nWebPLevel != 100 && poDS->m_bWebPLossless &&
8422 3 : CSLFetchNameValue(papszOptions, "WEBP_LEVEL"))
8423 : {
8424 0 : CPLError(CE_Warning, CPLE_AppDefined,
8425 : "WEBP_LEVEL is specified, but WEBP_LOSSLESS=YES. "
8426 : "WEBP_LEVEL will be ignored.");
8427 : }
8428 2155 : poDS->m_nJpegQuality = GTiffGetJpegQuality(papszOptions);
8429 2155 : poDS->m_nJpegTablesMode = GTiffGetJpegTablesMode(papszOptions);
8430 2155 : poDS->GetDiscardLsbOption(papszOptions);
8431 2155 : poDS->m_dfMaxZError = GTiffGetLERCMaxZError(papszOptions);
8432 2155 : poDS->m_dfMaxZErrorOverview = GTiffGetLERCMaxZErrorOverview(papszOptions);
8433 : #if HAVE_JXL
8434 2155 : poDS->m_bJXLLossless = GTiffGetJXLLossless(papszOptions);
8435 2155 : poDS->m_nJXLEffort = GTiffGetJXLEffort(papszOptions);
8436 2155 : poDS->m_fJXLDistance = GTiffGetJXLDistance(papszOptions);
8437 2155 : poDS->m_fJXLAlphaDistance = GTiffGetJXLAlphaDistance(papszOptions);
8438 : #endif
8439 2155 : poDS->InitCreationOrOpenOptions(true, papszOptions);
8440 :
8441 2155 : if (l_nCompression == COMPRESSION_ADOBE_DEFLATE ||
8442 2127 : l_nCompression == COMPRESSION_LERC)
8443 : {
8444 99 : GTiffSetDeflateSubCodec(l_hTIFF);
8445 :
8446 99 : if (poDS->m_nZLevel != -1)
8447 : {
8448 12 : TIFFSetField(l_hTIFF, TIFFTAG_ZIPQUALITY, poDS->m_nZLevel);
8449 : }
8450 : }
8451 2155 : if (l_nCompression == COMPRESSION_JPEG)
8452 : {
8453 75 : if (poDS->m_nJpegQuality != -1)
8454 : {
8455 9 : TIFFSetField(l_hTIFF, TIFFTAG_JPEGQUALITY, poDS->m_nJpegQuality);
8456 : }
8457 75 : TIFFSetField(l_hTIFF, TIFFTAG_JPEGTABLESMODE, poDS->m_nJpegTablesMode);
8458 : }
8459 2155 : if (l_nCompression == COMPRESSION_LZMA)
8460 : {
8461 7 : if (poDS->m_nLZMAPreset != -1)
8462 : {
8463 6 : TIFFSetField(l_hTIFF, TIFFTAG_LZMAPRESET, poDS->m_nLZMAPreset);
8464 : }
8465 : }
8466 2155 : if (l_nCompression == COMPRESSION_ZSTD ||
8467 2135 : l_nCompression == COMPRESSION_LERC)
8468 : {
8469 91 : if (poDS->m_nZSTDLevel != -1)
8470 : {
8471 8 : TIFFSetField(l_hTIFF, TIFFTAG_ZSTD_LEVEL, poDS->m_nZSTDLevel);
8472 : }
8473 : }
8474 2155 : if (l_nCompression == COMPRESSION_LERC)
8475 : {
8476 71 : TIFFSetField(l_hTIFF, TIFFTAG_LERC_MAXZERROR, poDS->m_dfMaxZError);
8477 : }
8478 : #if HAVE_JXL
8479 2155 : if (l_nCompression == COMPRESSION_JXL ||
8480 2155 : l_nCompression == COMPRESSION_JXL_DNG_1_7)
8481 : {
8482 91 : TIFFSetField(l_hTIFF, TIFFTAG_JXL_LOSSYNESS,
8483 91 : poDS->m_bJXLLossless ? JXL_LOSSLESS : JXL_LOSSY);
8484 91 : TIFFSetField(l_hTIFF, TIFFTAG_JXL_EFFORT, poDS->m_nJXLEffort);
8485 91 : TIFFSetField(l_hTIFF, TIFFTAG_JXL_DISTANCE,
8486 91 : static_cast<double>(poDS->m_fJXLDistance));
8487 91 : TIFFSetField(l_hTIFF, TIFFTAG_JXL_ALPHA_DISTANCE,
8488 91 : static_cast<double>(poDS->m_fJXLAlphaDistance));
8489 : }
8490 : #endif
8491 2155 : if (l_nCompression == COMPRESSION_WEBP)
8492 : {
8493 14 : if (poDS->m_nWebPLevel != -1)
8494 : {
8495 14 : TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LEVEL, poDS->m_nWebPLevel);
8496 : }
8497 :
8498 14 : if (poDS->m_bWebPLossless)
8499 : {
8500 5 : TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LOSSLESS, poDS->m_bWebPLossless);
8501 : }
8502 : }
8503 :
8504 : /* -------------------------------------------------------------------- */
8505 : /* Do we want to ensure all blocks get written out on close to */
8506 : /* avoid sparse files? */
8507 : /* -------------------------------------------------------------------- */
8508 2155 : if (!CPLFetchBool(papszOptions, "SPARSE_OK", false))
8509 2127 : poDS->m_bFillEmptyTilesAtClosing = true;
8510 :
8511 2155 : poDS->m_bWriteEmptyTiles =
8512 4086 : (bCopySrcOverviews && poDS->m_bFillEmptyTilesAtClosing) || bStreaming ||
8513 1931 : (poDS->m_nCompression != COMPRESSION_NONE &&
8514 317 : poDS->m_bFillEmptyTilesAtClosing);
8515 : // Only required for people writing non-compressed striped files in the
8516 : // rightorder and wanting all tstrips to be written in the same order
8517 : // so that the end result can be memory mapped without knowledge of each
8518 : // strip offset
8519 2155 : if (CPLTestBool(CSLFetchNameValueDef(
8520 4310 : papszOptions, "WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")) ||
8521 2155 : CPLTestBool(CSLFetchNameValueDef(
8522 : papszOptions, "@WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")))
8523 : {
8524 0 : poDS->m_bWriteEmptyTiles = true;
8525 : }
8526 :
8527 : // Precreate (internal) mask, so that the IBuildOverviews() below
8528 : // has a chance to create also the overviews of the mask.
8529 2155 : CPLErr eErr = CE_None;
8530 :
8531 2155 : if (bCreateMask)
8532 : {
8533 38 : eErr = poDS->CreateMaskBand(nMaskFlags);
8534 38 : if (poDS->m_poMaskDS)
8535 : {
8536 37 : poDS->m_poMaskDS->m_bFillEmptyTilesAtClosing =
8537 37 : poDS->m_bFillEmptyTilesAtClosing;
8538 37 : poDS->m_poMaskDS->m_bWriteEmptyTiles = poDS->m_bWriteEmptyTiles;
8539 : }
8540 : }
8541 :
8542 : /* -------------------------------------------------------------------- */
8543 : /* Create and then copy existing overviews if requested */
8544 : /* We do it such that all the IFDs are at the beginning of the file, */
8545 : /* and that the imagery data for the smallest overview is written */
8546 : /* first, that way the file is more usable when embedded in a */
8547 : /* compressed stream. */
8548 : /* -------------------------------------------------------------------- */
8549 :
8550 : // For scaled progress due to overview copying.
8551 2155 : const int nBandsWidthMask = l_nBands + (bCreateMask ? 1 : 0);
8552 2155 : double dfTotalPixels =
8553 2155 : static_cast<double>(nXSize) * nYSize * nBandsWidthMask;
8554 2155 : double dfCurPixels = 0;
8555 :
8556 2155 : if (eErr == CE_None && bCopySrcOverviews)
8557 : {
8558 0 : std::unique_ptr<GDALDataset> poMaskOvrDS;
8559 : const char *pszMaskOvrDS =
8560 225 : CSLFetchNameValue(papszOptions, "@MASK_OVERVIEW_DATASET");
8561 225 : if (pszMaskOvrDS)
8562 : {
8563 6 : poMaskOvrDS.reset(GDALDataset::Open(pszMaskOvrDS));
8564 6 : if (!poMaskOvrDS)
8565 : {
8566 0 : l_fpL->CancelCreation();
8567 0 : return nullptr;
8568 : }
8569 6 : if (poMaskOvrDS->GetRasterCount() != 1)
8570 : {
8571 0 : l_fpL->CancelCreation();
8572 0 : return nullptr;
8573 : }
8574 : }
8575 225 : if (nSrcOverviews)
8576 : {
8577 71 : eErr = poDS->CreateOverviewsFromSrcOverviews(poSrcDS, poOvrDS.get(),
8578 : nSrcOverviews);
8579 :
8580 207 : if (eErr == CE_None &&
8581 71 : (poMaskOvrDS != nullptr ||
8582 65 : (poSrcDS->GetRasterBand(1)->GetOverview(0) &&
8583 35 : poSrcDS->GetRasterBand(1)->GetOverview(0)->GetMaskFlags() ==
8584 : GMF_PER_DATASET)))
8585 : {
8586 18 : int nOvrBlockXSize = 0;
8587 18 : int nOvrBlockYSize = 0;
8588 18 : GTIFFGetOverviewBlockSize(
8589 18 : GDALRasterBand::ToHandle(poDS->GetRasterBand(1)),
8590 : &nOvrBlockXSize, &nOvrBlockYSize, nullptr, nullptr);
8591 18 : eErr = poDS->CreateInternalMaskOverviews(nOvrBlockXSize,
8592 : nOvrBlockYSize);
8593 : }
8594 : }
8595 :
8596 225 : TIFFForceStrileArrayWriting(poDS->m_hTIFF);
8597 :
8598 225 : if (poDS->m_poMaskDS)
8599 : {
8600 27 : TIFFForceStrileArrayWriting(poDS->m_poMaskDS->m_hTIFF);
8601 : }
8602 :
8603 356 : for (auto &poIterOvrDS : poDS->m_apoOverviewDS)
8604 : {
8605 131 : TIFFForceStrileArrayWriting(poIterOvrDS->m_hTIFF);
8606 :
8607 131 : if (poIterOvrDS->m_poMaskDS)
8608 : {
8609 31 : TIFFForceStrileArrayWriting(poIterOvrDS->m_poMaskDS->m_hTIFF);
8610 : }
8611 : }
8612 :
8613 225 : if (eErr == CE_None && nSrcOverviews)
8614 : {
8615 71 : if (poDS->m_apoOverviewDS.size() !=
8616 71 : static_cast<size_t>(nSrcOverviews))
8617 : {
8618 0 : ReportError(
8619 : pszFilename, CE_Failure, CPLE_AppDefined,
8620 : "Did only manage to instantiate %d overview levels, "
8621 : "whereas source contains %d",
8622 0 : static_cast<int>(poDS->m_apoOverviewDS.size()),
8623 : nSrcOverviews);
8624 0 : eErr = CE_Failure;
8625 : }
8626 :
8627 202 : for (int i = 0; eErr == CE_None && i < nSrcOverviews; ++i)
8628 : {
8629 : GDALRasterBand *poOvrBand =
8630 : poOvrDS
8631 207 : ? (i == 0
8632 76 : ? poOvrDS->GetRasterBand(1)
8633 40 : : poOvrDS->GetRasterBand(1)->GetOverview(i - 1))
8634 186 : : poSrcDS->GetRasterBand(1)->GetOverview(i);
8635 : const double dfOvrPixels =
8636 131 : static_cast<double>(poOvrBand->GetXSize()) *
8637 131 : poOvrBand->GetYSize();
8638 131 : dfTotalPixels += dfOvrPixels * l_nBands;
8639 245 : if (poOvrBand->GetMaskFlags() == GMF_PER_DATASET ||
8640 114 : poMaskOvrDS != nullptr)
8641 : {
8642 31 : dfTotalPixels += dfOvrPixels;
8643 : }
8644 100 : else if (i == 0 && poDS->GetRasterBand(1)->GetMaskFlags() ==
8645 : GMF_PER_DATASET)
8646 : {
8647 2 : ReportError(pszFilename, CE_Warning, CPLE_AppDefined,
8648 : "Source dataset has a mask band on full "
8649 : "resolution, overviews on the regular bands, "
8650 : "but lacks overviews on the mask band.");
8651 : }
8652 : }
8653 :
8654 : // Now copy the imagery.
8655 : // Begin with the smallest overview.
8656 71 : for (int iOvrLevel = nSrcOverviews - 1;
8657 201 : eErr == CE_None && iOvrLevel >= 0; --iOvrLevel)
8658 : {
8659 130 : auto poDstDS = poDS->m_apoOverviewDS[iOvrLevel].get();
8660 :
8661 : // Create a fake dataset with the source overview level so that
8662 : // GDALDatasetCopyWholeRaster can cope with it.
8663 : GDALDataset *poSrcOvrDS =
8664 : poOvrDS
8665 170 : ? (iOvrLevel == 0 ? poOvrDS.get()
8666 40 : : GDALCreateOverviewDataset(
8667 : poOvrDS.get(), iOvrLevel - 1,
8668 : /* bThisLevelOnly = */ true))
8669 54 : : GDALCreateOverviewDataset(
8670 : poSrcDS, iOvrLevel,
8671 130 : /* bThisLevelOnly = */ true);
8672 : GDALRasterBand *poSrcOvrBand =
8673 206 : poOvrDS ? (iOvrLevel == 0
8674 76 : ? poOvrDS->GetRasterBand(1)
8675 80 : : poOvrDS->GetRasterBand(1)->GetOverview(
8676 40 : iOvrLevel - 1))
8677 184 : : poSrcDS->GetRasterBand(1)->GetOverview(iOvrLevel);
8678 : double dfNextCurPixels =
8679 : dfCurPixels +
8680 130 : static_cast<double>(poSrcOvrBand->GetXSize()) *
8681 130 : poSrcOvrBand->GetYSize() * l_nBands;
8682 :
8683 130 : poDstDS->m_bBlockOrderRowMajor = true;
8684 130 : poDstDS->m_bLeaderSizeAsUInt4 = true;
8685 130 : poDstDS->m_bTrailerRepeatedLast4BytesRepeated = true;
8686 130 : poDstDS->m_bFillEmptyTilesAtClosing =
8687 130 : poDS->m_bFillEmptyTilesAtClosing;
8688 130 : poDstDS->m_bWriteEmptyTiles = poDS->m_bWriteEmptyTiles;
8689 130 : poDstDS->m_bTileInterleave = poDS->m_bTileInterleave;
8690 130 : GDALRasterBand *poSrcMaskBand = nullptr;
8691 130 : if (poDstDS->m_poMaskDS)
8692 : {
8693 31 : poDstDS->m_poMaskDS->m_bBlockOrderRowMajor = true;
8694 31 : poDstDS->m_poMaskDS->m_bLeaderSizeAsUInt4 = true;
8695 31 : poDstDS->m_poMaskDS->m_bTrailerRepeatedLast4BytesRepeated =
8696 : true;
8697 62 : poDstDS->m_poMaskDS->m_bFillEmptyTilesAtClosing =
8698 31 : poDS->m_bFillEmptyTilesAtClosing;
8699 62 : poDstDS->m_poMaskDS->m_bWriteEmptyTiles =
8700 31 : poDS->m_bWriteEmptyTiles;
8701 :
8702 31 : poSrcMaskBand =
8703 : poMaskOvrDS
8704 45 : ? (iOvrLevel == 0
8705 14 : ? poMaskOvrDS->GetRasterBand(1)
8706 16 : : poMaskOvrDS->GetRasterBand(1)->GetOverview(
8707 8 : iOvrLevel - 1))
8708 48 : : poSrcOvrBand->GetMaskBand();
8709 : }
8710 :
8711 130 : if (poDstDS->m_poMaskDS)
8712 : {
8713 31 : dfNextCurPixels +=
8714 31 : static_cast<double>(poSrcOvrBand->GetXSize()) *
8715 31 : poSrcOvrBand->GetYSize();
8716 : }
8717 : void *pScaledData =
8718 130 : GDALCreateScaledProgress(dfCurPixels / dfTotalPixels,
8719 : dfNextCurPixels / dfTotalPixels,
8720 : pfnProgress, pProgressData);
8721 :
8722 130 : eErr = CopyImageryAndMask(poDstDS, poSrcOvrDS, poSrcMaskBand,
8723 : GDALScaledProgress, pScaledData);
8724 :
8725 130 : dfCurPixels = dfNextCurPixels;
8726 130 : GDALDestroyScaledProgress(pScaledData);
8727 :
8728 130 : if (poSrcOvrDS != poOvrDS.get())
8729 94 : delete poSrcOvrDS;
8730 130 : poSrcOvrDS = nullptr;
8731 : }
8732 : }
8733 : }
8734 :
8735 : /* -------------------------------------------------------------------- */
8736 : /* Copy actual imagery. */
8737 : /* -------------------------------------------------------------------- */
8738 2155 : double dfNextCurPixels =
8739 2155 : dfCurPixels + static_cast<double>(nXSize) * nYSize * l_nBands;
8740 2155 : void *pScaledData = GDALCreateScaledProgress(
8741 : dfCurPixels / dfTotalPixels, dfNextCurPixels / dfTotalPixels,
8742 : pfnProgress, pProgressData);
8743 :
8744 : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
8745 2155 : bool bTryCopy = true;
8746 : #endif
8747 :
8748 : #ifdef HAVE_LIBJPEG
8749 2155 : if (bCopyFromJPEG)
8750 : {
8751 12 : eErr = GTIFF_CopyFromJPEG(poDS.get(), poSrcDS, pfnProgress,
8752 : pProgressData, bTryCopy);
8753 :
8754 : // In case of failure in the decompression step, try normal copy.
8755 12 : if (bTryCopy)
8756 0 : eErr = CE_None;
8757 : }
8758 : #endif
8759 :
8760 : #ifdef JPEG_DIRECT_COPY
8761 : if (bDirectCopyFromJPEG)
8762 : {
8763 : eErr = GTIFF_DirectCopyFromJPEG(poDS.get(), poSrcDS, pfnProgress,
8764 : pProgressData, bTryCopy);
8765 :
8766 : // In case of failure in the reading step, try normal copy.
8767 : if (bTryCopy)
8768 : eErr = CE_None;
8769 : }
8770 : #endif
8771 :
8772 2155 : bool bWriteMask = true;
8773 2155 : if (
8774 : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
8775 4298 : bTryCopy &&
8776 : #endif
8777 2143 : (poDS->m_bTreatAsSplit || poDS->m_bTreatAsSplitBitmap))
8778 : {
8779 : // For split bands, we use TIFFWriteScanline() interface.
8780 9 : CPLAssert(poDS->m_nBitsPerSample == 8 || poDS->m_nBitsPerSample == 1);
8781 :
8782 9 : if (poDS->m_nPlanarConfig == PLANARCONFIG_CONTIG && poDS->nBands > 1)
8783 : {
8784 : GByte *pabyScanline = static_cast<GByte *>(
8785 3 : VSI_MALLOC_VERBOSE(TIFFScanlineSize(l_hTIFF)));
8786 3 : if (pabyScanline == nullptr)
8787 0 : eErr = CE_Failure;
8788 9052 : for (int j = 0; j < nYSize && eErr == CE_None; ++j)
8789 : {
8790 18098 : eErr = poSrcDS->RasterIO(GF_Read, 0, j, nXSize, 1, pabyScanline,
8791 : nXSize, 1, GDT_UInt8, l_nBands,
8792 9049 : nullptr, poDS->nBands, 0, 1, nullptr);
8793 18098 : if (eErr == CE_None &&
8794 9049 : TIFFWriteScanline(l_hTIFF, pabyScanline, j, 0) == -1)
8795 : {
8796 0 : ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
8797 : "TIFFWriteScanline() failed.");
8798 0 : eErr = CE_Failure;
8799 : }
8800 9049 : if (!GDALScaledProgress((j + 1) * 1.0 / nYSize, nullptr,
8801 : pScaledData))
8802 0 : eErr = CE_Failure;
8803 : }
8804 3 : CPLFree(pabyScanline);
8805 : }
8806 : else
8807 : {
8808 : GByte *pabyScanline =
8809 6 : static_cast<GByte *>(VSI_MALLOC_VERBOSE(nXSize));
8810 6 : if (pabyScanline == nullptr)
8811 0 : eErr = CE_Failure;
8812 : else
8813 6 : eErr = CE_None;
8814 14 : for (int iBand = 1; iBand <= l_nBands && eErr == CE_None; ++iBand)
8815 : {
8816 48211 : for (int j = 0; j < nYSize && eErr == CE_None; ++j)
8817 : {
8818 48203 : eErr = poSrcDS->GetRasterBand(iBand)->RasterIO(
8819 : GF_Read, 0, j, nXSize, 1, pabyScanline, nXSize, 1,
8820 : GDT_UInt8, 0, 0, nullptr);
8821 48203 : if (poDS->m_bTreatAsSplitBitmap)
8822 : {
8823 7225210 : for (int i = 0; i < nXSize; ++i)
8824 : {
8825 7216010 : const GByte byVal = pabyScanline[i];
8826 7216010 : if ((i & 0x7) == 0)
8827 902001 : pabyScanline[i >> 3] = 0;
8828 7216010 : if (byVal)
8829 7097220 : pabyScanline[i >> 3] |= 0x80 >> (i & 0x7);
8830 : }
8831 : }
8832 96406 : if (eErr == CE_None &&
8833 48203 : TIFFWriteScanline(l_hTIFF, pabyScanline, j,
8834 48203 : static_cast<uint16_t>(iBand - 1)) ==
8835 : -1)
8836 : {
8837 0 : ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
8838 : "TIFFWriteScanline() failed.");
8839 0 : eErr = CE_Failure;
8840 : }
8841 48203 : if (!GDALScaledProgress((j + 1 + (iBand - 1) * nYSize) *
8842 48203 : 1.0 / (l_nBands * nYSize),
8843 : nullptr, pScaledData))
8844 0 : eErr = CE_Failure;
8845 : }
8846 : }
8847 6 : CPLFree(pabyScanline);
8848 : }
8849 :
8850 : // Necessary to be able to read the file without re-opening.
8851 9 : TIFFSizeProc pfnSizeProc = TIFFGetSizeProc(l_hTIFF);
8852 :
8853 9 : TIFFFlushData(l_hTIFF);
8854 :
8855 9 : toff_t nNewDirOffset = pfnSizeProc(TIFFClientdata(l_hTIFF));
8856 9 : if ((nNewDirOffset % 2) == 1)
8857 5 : ++nNewDirOffset;
8858 :
8859 9 : TIFFFlush(l_hTIFF);
8860 :
8861 9 : if (poDS->m_nDirOffset != TIFFCurrentDirOffset(l_hTIFF))
8862 : {
8863 0 : poDS->m_nDirOffset = nNewDirOffset;
8864 0 : CPLDebug("GTiff", "directory moved during flush.");
8865 : }
8866 : }
8867 2146 : else if (
8868 : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
8869 2134 : bTryCopy &&
8870 : #endif
8871 : eErr == CE_None)
8872 : {
8873 2133 : const char *papszCopyWholeRasterOptions[3] = {nullptr, nullptr,
8874 : nullptr};
8875 2133 : int iNextOption = 0;
8876 2133 : papszCopyWholeRasterOptions[iNextOption++] = "SKIP_HOLES=YES";
8877 2133 : if (l_nCompression != COMPRESSION_NONE)
8878 : {
8879 507 : papszCopyWholeRasterOptions[iNextOption++] = "COMPRESSED=YES";
8880 : }
8881 :
8882 : // For streaming with separate, we really want that bands are written
8883 : // after each other, even if the source is pixel interleaved.
8884 1626 : else if (bStreaming && poDS->m_nPlanarConfig == PLANARCONFIG_SEPARATE)
8885 : {
8886 1 : papszCopyWholeRasterOptions[iNextOption++] = "INTERLEAVE=BAND";
8887 : }
8888 :
8889 2133 : if (bCopySrcOverviews || bTileInterleaving)
8890 : {
8891 225 : poDS->m_bBlockOrderRowMajor = true;
8892 225 : poDS->m_bLeaderSizeAsUInt4 = bCopySrcOverviews;
8893 225 : poDS->m_bTrailerRepeatedLast4BytesRepeated = bCopySrcOverviews;
8894 225 : if (poDS->m_poMaskDS)
8895 : {
8896 27 : poDS->m_poMaskDS->m_bBlockOrderRowMajor = true;
8897 27 : poDS->m_poMaskDS->m_bLeaderSizeAsUInt4 = bCopySrcOverviews;
8898 27 : poDS->m_poMaskDS->m_bTrailerRepeatedLast4BytesRepeated =
8899 : bCopySrcOverviews;
8900 27 : GDALDestroyScaledProgress(pScaledData);
8901 : pScaledData =
8902 27 : GDALCreateScaledProgress(dfCurPixels / dfTotalPixels, 1.0,
8903 : pfnProgress, pProgressData);
8904 : }
8905 :
8906 225 : eErr = CopyImageryAndMask(poDS.get(), poSrcDS,
8907 225 : poSrcDS->GetRasterBand(1)->GetMaskBand(),
8908 : GDALScaledProgress, pScaledData);
8909 225 : if (poDS->m_poMaskDS)
8910 : {
8911 27 : bWriteMask = false;
8912 : }
8913 : }
8914 : else
8915 : {
8916 1908 : eErr = GDALDatasetCopyWholeRaster(GDALDataset::ToHandle(poSrcDS),
8917 1908 : GDALDataset::ToHandle(poDS.get()),
8918 : papszCopyWholeRasterOptions,
8919 : GDALScaledProgress, pScaledData);
8920 : }
8921 : }
8922 :
8923 2155 : GDALDestroyScaledProgress(pScaledData);
8924 :
8925 2155 : if (eErr == CE_None && !bStreaming && bWriteMask)
8926 : {
8927 2106 : pScaledData = GDALCreateScaledProgress(dfNextCurPixels / dfTotalPixels,
8928 : 1.0, pfnProgress, pProgressData);
8929 2106 : if (poDS->m_poMaskDS)
8930 : {
8931 10 : const char *l_papszOptions[2] = {"COMPRESSED=YES", nullptr};
8932 10 : eErr = GDALRasterBandCopyWholeRaster(
8933 10 : poSrcDS->GetRasterBand(1)->GetMaskBand(),
8934 10 : poDS->GetRasterBand(1)->GetMaskBand(),
8935 : const_cast<char **>(l_papszOptions), GDALScaledProgress,
8936 : pScaledData);
8937 : }
8938 : else
8939 : {
8940 2096 : eErr = GDALDriver::DefaultCopyMasks(poSrcDS, poDS.get(), bStrict,
8941 : nullptr, GDALScaledProgress,
8942 : pScaledData);
8943 : }
8944 2106 : GDALDestroyScaledProgress(pScaledData);
8945 : }
8946 :
8947 2155 : poDS->m_bWriteCOGLayout = false;
8948 :
8949 4292 : if (eErr == CE_None &&
8950 2137 : CPLTestBool(CSLFetchNameValueDef(poDS->m_papszCreationOptions,
8951 : "@FLUSHCACHE", "NO")))
8952 : {
8953 176 : if (poDS->FlushCache(false) != CE_None)
8954 : {
8955 0 : eErr = CE_Failure;
8956 : }
8957 : }
8958 :
8959 2155 : if (eErr == CE_Failure)
8960 : {
8961 18 : if (CPLTestBool(CPLGetConfigOption("GTIFF_DELETE_ON_ERROR", "YES")))
8962 : {
8963 17 : l_fpL->CancelCreation();
8964 17 : poDS.reset();
8965 :
8966 17 : if (!bStreaming)
8967 : {
8968 : // Should really delete more carefully.
8969 17 : VSIUnlink(pszFilename);
8970 : }
8971 : }
8972 : else
8973 : {
8974 1 : poDS.reset();
8975 : }
8976 : }
8977 :
8978 2155 : return poDS.release();
8979 : }
8980 :
8981 : /************************************************************************/
8982 : /* SetSpatialRef() */
8983 : /************************************************************************/
8984 :
8985 1556 : CPLErr GTiffDataset::SetSpatialRef(const OGRSpatialReference *poSRS)
8986 :
8987 : {
8988 1556 : if (m_bStreamingOut && m_bCrystalized)
8989 : {
8990 1 : ReportError(CE_Failure, CPLE_NotSupported,
8991 : "Cannot modify projection at that point in "
8992 : "a streamed output file");
8993 1 : return CE_Failure;
8994 : }
8995 :
8996 1555 : LoadGeoreferencingAndPamIfNeeded();
8997 1555 : LookForProjection();
8998 :
8999 1555 : CPLErr eErr = CE_None;
9000 1555 : if (eAccess == GA_Update)
9001 : {
9002 1557 : if ((m_eProfile == GTiffProfile::BASELINE) &&
9003 7 : (GetPamFlags() & GPF_DISABLED) == 0)
9004 : {
9005 7 : eErr = GDALPamDataset::SetSpatialRef(poSRS);
9006 : }
9007 : else
9008 : {
9009 1543 : if (GDALPamDataset::GetSpatialRef() != nullptr)
9010 : {
9011 : // Cancel any existing SRS from PAM file.
9012 1 : GDALPamDataset::SetSpatialRef(nullptr);
9013 : }
9014 1543 : m_bGeoTIFFInfoChanged = true;
9015 : }
9016 : }
9017 : else
9018 : {
9019 5 : CPLDebug("GTIFF", "SetSpatialRef() goes to PAM instead of TIFF tags");
9020 5 : eErr = GDALPamDataset::SetSpatialRef(poSRS);
9021 : }
9022 :
9023 1555 : if (eErr == CE_None)
9024 : {
9025 1555 : if (poSRS == nullptr || poSRS->IsEmpty())
9026 : {
9027 14 : if (!m_oSRS.IsEmpty())
9028 : {
9029 4 : m_bForceUnsetProjection = true;
9030 : }
9031 14 : m_oSRS.Clear();
9032 : }
9033 : else
9034 : {
9035 1541 : m_oSRS = *poSRS;
9036 1541 : m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
9037 : }
9038 : }
9039 :
9040 1555 : return eErr;
9041 : }
9042 :
9043 : /************************************************************************/
9044 : /* SetGeoTransform() */
9045 : /************************************************************************/
9046 :
9047 1884 : CPLErr GTiffDataset::SetGeoTransform(const GDALGeoTransform >)
9048 :
9049 : {
9050 1884 : if (m_bStreamingOut && m_bCrystalized)
9051 : {
9052 1 : ReportError(CE_Failure, CPLE_NotSupported,
9053 : "Cannot modify geotransform at that point in a "
9054 : "streamed output file");
9055 1 : return CE_Failure;
9056 : }
9057 :
9058 1883 : LoadGeoreferencingAndPamIfNeeded();
9059 :
9060 1883 : CPLErr eErr = CE_None;
9061 1883 : if (eAccess == GA_Update)
9062 : {
9063 1877 : if (!m_aoGCPs.empty())
9064 : {
9065 1 : ReportError(CE_Warning, CPLE_AppDefined,
9066 : "GCPs previously set are going to be cleared "
9067 : "due to the setting of a geotransform.");
9068 1 : m_bForceUnsetGTOrGCPs = true;
9069 1 : m_aoGCPs.clear();
9070 : }
9071 1876 : else if (gt.xorig == 0.0 && gt.xscale == 0.0 && gt.xrot == 0.0 &&
9072 2 : gt.yorig == 0.0 && gt.yrot == 0.0 && gt.yscale == 0.0)
9073 : {
9074 2 : if (m_bGeoTransformValid)
9075 : {
9076 2 : m_bForceUnsetGTOrGCPs = true;
9077 2 : m_bGeoTIFFInfoChanged = true;
9078 : }
9079 2 : m_bGeoTransformValid = false;
9080 2 : m_gt = gt;
9081 2 : return CE_None;
9082 : }
9083 :
9084 3759 : if ((m_eProfile == GTiffProfile::BASELINE) &&
9085 9 : !CPLFetchBool(m_papszCreationOptions, "TFW", false) &&
9086 1889 : !CPLFetchBool(m_papszCreationOptions, "WORLDFILE", false) &&
9087 5 : (GetPamFlags() & GPF_DISABLED) == 0)
9088 : {
9089 5 : eErr = GDALPamDataset::SetGeoTransform(gt);
9090 : }
9091 : else
9092 : {
9093 : // Cancel any existing geotransform from PAM file.
9094 1870 : GDALPamDataset::DeleteGeoTransform();
9095 1870 : m_bGeoTIFFInfoChanged = true;
9096 : }
9097 : }
9098 : else
9099 : {
9100 6 : CPLDebug("GTIFF", "SetGeoTransform() goes to PAM instead of TIFF tags");
9101 6 : eErr = GDALPamDataset::SetGeoTransform(gt);
9102 : }
9103 :
9104 1881 : if (eErr == CE_None)
9105 : {
9106 1881 : m_gt = gt;
9107 1881 : m_bGeoTransformValid = true;
9108 : }
9109 :
9110 1881 : return eErr;
9111 : }
9112 :
9113 : /************************************************************************/
9114 : /* SetGCPs() */
9115 : /************************************************************************/
9116 :
9117 22 : CPLErr GTiffDataset::SetGCPs(int nGCPCountIn, const GDAL_GCP *pasGCPListIn,
9118 : const OGRSpatialReference *poGCPSRS)
9119 : {
9120 22 : CPLErr eErr = CE_None;
9121 22 : LoadGeoreferencingAndPamIfNeeded();
9122 22 : LookForProjection();
9123 :
9124 22 : if (eAccess == GA_Update)
9125 : {
9126 20 : if (!m_aoGCPs.empty() && nGCPCountIn == 0)
9127 : {
9128 3 : m_bForceUnsetGTOrGCPs = true;
9129 : }
9130 17 : else if (nGCPCountIn > 0 && m_bGeoTransformValid)
9131 : {
9132 5 : ReportError(CE_Warning, CPLE_AppDefined,
9133 : "A geotransform previously set is going to be cleared "
9134 : "due to the setting of GCPs.");
9135 5 : m_gt = GDALGeoTransform();
9136 5 : m_bGeoTransformValid = false;
9137 5 : m_bForceUnsetGTOrGCPs = true;
9138 : }
9139 20 : if ((m_eProfile == GTiffProfile::BASELINE) &&
9140 0 : (GetPamFlags() & GPF_DISABLED) == 0)
9141 : {
9142 0 : eErr = GDALPamDataset::SetGCPs(nGCPCountIn, pasGCPListIn, poGCPSRS);
9143 : }
9144 : else
9145 : {
9146 20 : if (nGCPCountIn > knMAX_GCP_COUNT)
9147 : {
9148 2 : if (GDALPamDataset::GetGCPCount() == 0 && !m_aoGCPs.empty())
9149 : {
9150 1 : m_bForceUnsetGTOrGCPs = true;
9151 : }
9152 2 : ReportError(CE_Warning, CPLE_AppDefined,
9153 : "Trying to write %d GCPs, whereas the maximum "
9154 : "supported in GeoTIFF tag is %d. "
9155 : "Falling back to writing them to PAM",
9156 : nGCPCountIn, knMAX_GCP_COUNT);
9157 2 : eErr = GDALPamDataset::SetGCPs(nGCPCountIn, pasGCPListIn,
9158 : poGCPSRS);
9159 : }
9160 18 : else if (GDALPamDataset::GetGCPCount() > 0)
9161 : {
9162 : // Cancel any existing GCPs from PAM file.
9163 1 : GDALPamDataset::SetGCPs(
9164 : 0, nullptr,
9165 : static_cast<const OGRSpatialReference *>(nullptr));
9166 : }
9167 20 : m_bGeoTIFFInfoChanged = true;
9168 : }
9169 : }
9170 : else
9171 : {
9172 2 : CPLDebug("GTIFF", "SetGCPs() goes to PAM instead of TIFF tags");
9173 2 : eErr = GDALPamDataset::SetGCPs(nGCPCountIn, pasGCPListIn, poGCPSRS);
9174 : }
9175 :
9176 22 : if (eErr == CE_None)
9177 : {
9178 22 : if (poGCPSRS == nullptr || poGCPSRS->IsEmpty())
9179 : {
9180 11 : if (!m_oSRS.IsEmpty())
9181 : {
9182 5 : m_bForceUnsetProjection = true;
9183 : }
9184 11 : m_oSRS.Clear();
9185 : }
9186 : else
9187 : {
9188 11 : m_oSRS = *poGCPSRS;
9189 11 : m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
9190 : }
9191 :
9192 22 : m_aoGCPs = gdal::GCP::fromC(pasGCPListIn, nGCPCountIn);
9193 : }
9194 :
9195 22 : return eErr;
9196 : }
9197 :
9198 : /************************************************************************/
9199 : /* SetMetadata() */
9200 : /************************************************************************/
9201 2751 : CPLErr GTiffDataset::SetMetadata(CSLConstList papszMD, const char *pszDomain)
9202 :
9203 : {
9204 2751 : LoadGeoreferencingAndPamIfNeeded();
9205 :
9206 2751 : if (m_bStreamingOut && m_bCrystalized)
9207 : {
9208 1 : ReportError(
9209 : CE_Failure, CPLE_NotSupported,
9210 : "Cannot modify metadata at that point in a streamed output file");
9211 1 : return CE_Failure;
9212 : }
9213 :
9214 2750 : if (pszDomain && EQUAL(pszDomain, "json:ISIS3"))
9215 : {
9216 5 : m_oISIS3Metadata.Deinit();
9217 5 : m_oMapISIS3MetadataItems.clear();
9218 : }
9219 :
9220 2750 : CPLErr eErr = CE_None;
9221 2750 : if (eAccess == GA_Update)
9222 : {
9223 2747 : if (pszDomain != nullptr && EQUAL(pszDomain, GDAL_MDD_RPC))
9224 : {
9225 : // So that a subsequent GetMetadata() wouldn't override our new
9226 : // values
9227 22 : LoadMetadata();
9228 22 : m_bForceUnsetRPC = (CSLCount(papszMD) == 0);
9229 : }
9230 :
9231 2747 : if ((papszMD != nullptr) && (pszDomain != nullptr) &&
9232 1905 : EQUAL(pszDomain, "COLOR_PROFILE"))
9233 : {
9234 0 : m_bColorProfileMetadataChanged = true;
9235 : }
9236 2747 : else if (pszDomain == nullptr || !EQUAL(pszDomain, "_temporary_"))
9237 : {
9238 2747 : m_bMetadataChanged = true;
9239 : // Cancel any existing metadata from PAM file.
9240 2747 : if (GDALPamDataset::GetMetadata(pszDomain) != nullptr)
9241 1 : GDALPamDataset::SetMetadata(nullptr, pszDomain);
9242 : }
9243 :
9244 5458 : if ((pszDomain == nullptr || EQUAL(pszDomain, "")) &&
9245 2711 : CSLFetchNameValue(papszMD, GDALMD_AREA_OR_POINT) != nullptr)
9246 : {
9247 2076 : const char *pszPrevValue = GetMetadataItem(GDALMD_AREA_OR_POINT);
9248 : const char *pszNewValue =
9249 2076 : CSLFetchNameValue(papszMD, GDALMD_AREA_OR_POINT);
9250 2076 : if (pszPrevValue == nullptr || pszNewValue == nullptr ||
9251 1647 : !EQUAL(pszPrevValue, pszNewValue))
9252 : {
9253 433 : LookForProjection();
9254 433 : m_bGeoTIFFInfoChanged = true;
9255 : }
9256 : }
9257 :
9258 2747 : if (pszDomain != nullptr && EQUAL(pszDomain, "xml:XMP"))
9259 : {
9260 2 : if (papszMD != nullptr && *papszMD != nullptr)
9261 : {
9262 1 : int nTagSize = static_cast<int>(strlen(*papszMD));
9263 1 : TIFFSetField(m_hTIFF, TIFFTAG_XMLPACKET, nTagSize, *papszMD);
9264 : }
9265 : else
9266 : {
9267 1 : TIFFUnsetField(m_hTIFF, TIFFTAG_XMLPACKET);
9268 : }
9269 : }
9270 : }
9271 : else
9272 : {
9273 3 : CPLDebug(
9274 : "GTIFF",
9275 : "GTiffDataset::SetMetadata() goes to PAM instead of TIFF tags");
9276 3 : eErr = GDALPamDataset::SetMetadata(papszMD, pszDomain);
9277 : }
9278 :
9279 2750 : if (eErr == CE_None)
9280 : {
9281 2750 : eErr = m_oGTiffMDMD.SetMetadata(papszMD, pszDomain);
9282 : }
9283 2750 : return eErr;
9284 : }
9285 :
9286 : /************************************************************************/
9287 : /* SetMetadataItem() */
9288 : /************************************************************************/
9289 :
9290 5950 : CPLErr GTiffDataset::SetMetadataItem(const char *pszName, const char *pszValue,
9291 : const char *pszDomain)
9292 :
9293 : {
9294 5950 : LoadGeoreferencingAndPamIfNeeded();
9295 :
9296 5950 : if (m_bStreamingOut && m_bCrystalized)
9297 : {
9298 1 : ReportError(
9299 : CE_Failure, CPLE_NotSupported,
9300 : "Cannot modify metadata at that point in a streamed output file");
9301 1 : return CE_Failure;
9302 : }
9303 :
9304 5949 : if (pszDomain && EQUAL(pszDomain, "json:ISIS3"))
9305 : {
9306 1 : ReportError(CE_Failure, CPLE_NotSupported,
9307 : "Updating part of json:ISIS3 is not supported. "
9308 : "Use SetMetadata() instead");
9309 1 : return CE_Failure;
9310 : }
9311 :
9312 5948 : CPLErr eErr = CE_None;
9313 5948 : if (eAccess == GA_Update)
9314 : {
9315 5941 : if ((pszDomain != nullptr) && EQUAL(pszDomain, "COLOR_PROFILE"))
9316 : {
9317 8 : m_bColorProfileMetadataChanged = true;
9318 : }
9319 5933 : else if (pszDomain == nullptr || !EQUAL(pszDomain, "_temporary_"))
9320 : {
9321 5933 : m_bMetadataChanged = true;
9322 : // Cancel any existing metadata from PAM file.
9323 5933 : if (GDALPamDataset::GetMetadataItem(pszName, pszDomain) != nullptr)
9324 1 : GDALPamDataset::SetMetadataItem(pszName, nullptr, pszDomain);
9325 : }
9326 :
9327 5941 : if ((pszDomain == nullptr || EQUAL(pszDomain, "")) &&
9328 85 : pszName != nullptr && EQUAL(pszName, GDALMD_AREA_OR_POINT))
9329 : {
9330 7 : LookForProjection();
9331 7 : m_bGeoTIFFInfoChanged = true;
9332 : }
9333 : }
9334 : else
9335 : {
9336 7 : CPLDebug(
9337 : "GTIFF",
9338 : "GTiffDataset::SetMetadataItem() goes to PAM instead of TIFF tags");
9339 7 : eErr = GDALPamDataset::SetMetadataItem(pszName, pszValue, pszDomain);
9340 : }
9341 :
9342 5948 : if (eErr == CE_None)
9343 : {
9344 5948 : eErr = m_oGTiffMDMD.SetMetadataItem(pszName, pszValue, pszDomain);
9345 : }
9346 :
9347 5948 : return eErr;
9348 : }
9349 :
9350 : /************************************************************************/
9351 : /* CreateMaskBand() */
9352 : /************************************************************************/
9353 :
9354 100 : CPLErr GTiffDataset::CreateMaskBand(int nFlagsIn)
9355 : {
9356 100 : ScanDirectories();
9357 :
9358 100 : if (m_poMaskDS != nullptr)
9359 : {
9360 1 : ReportError(CE_Failure, CPLE_AppDefined,
9361 : "This TIFF dataset already has an internal mask band");
9362 1 : return CE_Failure;
9363 : }
9364 99 : else if (MustCreateInternalMask())
9365 : {
9366 86 : if (nFlagsIn != GMF_PER_DATASET)
9367 : {
9368 1 : ReportError(CE_Failure, CPLE_AppDefined,
9369 : "The only flag value supported for internal mask is "
9370 : "GMF_PER_DATASET");
9371 1 : return CE_Failure;
9372 : }
9373 :
9374 85 : int l_nCompression = COMPRESSION_PACKBITS;
9375 85 : if (strstr(GDALGetMetadataItem(GDALGetDriverByName("GTiff"),
9376 : GDAL_DMD_CREATIONOPTIONLIST, nullptr),
9377 85 : "<Value>DEFLATE</Value>") != nullptr)
9378 85 : l_nCompression = COMPRESSION_ADOBE_DEFLATE;
9379 :
9380 : /* --------------------------------------------------------------------
9381 : */
9382 : /* If we don't have read access, then create the mask externally.
9383 : */
9384 : /* --------------------------------------------------------------------
9385 : */
9386 85 : if (GetAccess() != GA_Update)
9387 : {
9388 1 : ReportError(CE_Warning, CPLE_AppDefined,
9389 : "File open for read-only accessing, "
9390 : "creating mask externally.");
9391 :
9392 1 : return GDALPamDataset::CreateMaskBand(nFlagsIn);
9393 : }
9394 :
9395 84 : if (m_bLayoutIFDSBeforeData && !m_bKnownIncompatibleEdition &&
9396 0 : !m_bWriteKnownIncompatibleEdition)
9397 : {
9398 0 : ReportError(CE_Warning, CPLE_AppDefined,
9399 : "Adding a mask invalidates the "
9400 : "LAYOUT=IFDS_BEFORE_DATA property");
9401 0 : m_bKnownIncompatibleEdition = true;
9402 0 : m_bWriteKnownIncompatibleEdition = true;
9403 : }
9404 :
9405 84 : bool bIsOverview = false;
9406 84 : uint32_t nSubType = 0;
9407 84 : if (TIFFGetField(m_hTIFF, TIFFTAG_SUBFILETYPE, &nSubType))
9408 : {
9409 8 : bIsOverview = (nSubType & FILETYPE_REDUCEDIMAGE) != 0;
9410 :
9411 8 : if ((nSubType & FILETYPE_MASK) != 0)
9412 : {
9413 0 : ReportError(CE_Failure, CPLE_AppDefined,
9414 : "Cannot create a mask on a TIFF mask IFD !");
9415 0 : return CE_Failure;
9416 : }
9417 : }
9418 :
9419 84 : const int bIsTiled = TIFFIsTiled(m_hTIFF);
9420 :
9421 84 : FlushDirectory();
9422 :
9423 84 : const toff_t nOffset = GTIFFWriteDirectory(
9424 : m_hTIFF,
9425 : bIsOverview ? FILETYPE_REDUCEDIMAGE | FILETYPE_MASK : FILETYPE_MASK,
9426 : nRasterXSize, nRasterYSize, 1, PLANARCONFIG_CONTIG, 1,
9427 : m_nBlockXSize, m_nBlockYSize, bIsTiled, l_nCompression,
9428 : PHOTOMETRIC_MASK, PREDICTOR_NONE, SAMPLEFORMAT_UINT, nullptr,
9429 : nullptr, nullptr, 0, nullptr, "", nullptr, nullptr, nullptr,
9430 84 : nullptr, m_bWriteCOGLayout);
9431 :
9432 84 : ReloadDirectory();
9433 :
9434 84 : if (nOffset == 0)
9435 0 : return CE_Failure;
9436 :
9437 84 : m_poMaskDS = std::make_shared<GTiffDataset>();
9438 84 : m_poMaskDS->eAccess = GA_Update;
9439 84 : m_poMaskDS->m_poBaseDS = this;
9440 84 : m_poMaskDS->m_poImageryDS = this;
9441 84 : m_poMaskDS->ShareLockWithParentDataset(this);
9442 84 : m_poMaskDS->m_osFilename = m_osFilename;
9443 84 : m_poMaskDS->m_bPromoteTo8Bits = CPLTestBool(
9444 : CPLGetConfigOption("GDAL_TIFF_INTERNAL_MASK_TO_8BIT", "YES"));
9445 84 : return m_poMaskDS->OpenOffset(VSI_TIFFOpenChild(m_hTIFF), nOffset,
9446 84 : GA_Update);
9447 : }
9448 :
9449 13 : return GDALPamDataset::CreateMaskBand(nFlagsIn);
9450 : }
9451 :
9452 : /************************************************************************/
9453 : /* MustCreateInternalMask() */
9454 : /************************************************************************/
9455 :
9456 137 : bool GTiffDataset::MustCreateInternalMask()
9457 : {
9458 137 : return CPLTestBool(CPLGetConfigOption("GDAL_TIFF_INTERNAL_MASK", "YES"));
9459 : }
9460 :
9461 : /************************************************************************/
9462 : /* CreateMaskBand() */
9463 : /************************************************************************/
9464 :
9465 30 : CPLErr GTiffRasterBand::CreateMaskBand(int nFlagsIn)
9466 : {
9467 30 : m_poGDS->ScanDirectories();
9468 :
9469 30 : if (m_poGDS->m_poMaskDS != nullptr)
9470 : {
9471 5 : ReportError(CE_Failure, CPLE_AppDefined,
9472 : "This TIFF dataset already has an internal mask band");
9473 5 : return CE_Failure;
9474 : }
9475 :
9476 : const char *pszGDAL_TIFF_INTERNAL_MASK =
9477 25 : CPLGetConfigOption("GDAL_TIFF_INTERNAL_MASK", nullptr);
9478 28 : if ((pszGDAL_TIFF_INTERNAL_MASK &&
9479 25 : CPLTestBool(pszGDAL_TIFF_INTERNAL_MASK)) ||
9480 : nFlagsIn == GMF_PER_DATASET)
9481 : {
9482 16 : return m_poGDS->CreateMaskBand(nFlagsIn);
9483 : }
9484 :
9485 9 : return GDALPamRasterBand::CreateMaskBand(nFlagsIn);
9486 : }
9487 :
9488 : /************************************************************************/
9489 : /* ClampCTEntry() */
9490 : /************************************************************************/
9491 :
9492 236415 : /* static */ unsigned short GTiffDataset::ClampCTEntry(int iColor, int iComp,
9493 : int nCTEntryVal,
9494 : int nMultFactor)
9495 : {
9496 236415 : const int nVal = nCTEntryVal * nMultFactor;
9497 236415 : if (nVal < 0)
9498 : {
9499 0 : CPLError(CE_Warning, CPLE_AppDefined,
9500 : "Color table entry [%d][%d] = %d, clamped to 0", iColor, iComp,
9501 : nCTEntryVal);
9502 0 : return 0;
9503 : }
9504 236415 : if (nVal > 65535)
9505 : {
9506 2 : CPLError(CE_Warning, CPLE_AppDefined,
9507 : "Color table entry [%d][%d] = %d, clamped to 65535", iColor,
9508 : iComp, nCTEntryVal);
9509 2 : return 65535;
9510 : }
9511 236413 : return static_cast<unsigned short>(nVal);
9512 : }
|