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 17622 : static signed char GTiffGetWebPLevel(CSLConstList papszOptions)
79 : {
80 17622 : int nWebPLevel = DEFAULT_WEBP_LEVEL;
81 17622 : const char *pszValue = CSLFetchNameValue(papszOptions, "WEBP_LEVEL");
82 17622 : 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 17622 : return static_cast<signed char>(nWebPLevel);
93 : }
94 :
95 17628 : static bool GTiffGetWebPLossless(CSLConstList papszOptions)
96 : {
97 17628 : return CPLFetchBool(papszOptions, "WEBP_LOSSLESS", false);
98 : }
99 :
100 17694 : static double GTiffGetLERCMaxZError(CSLConstList papszOptions)
101 : {
102 17694 : return CPLAtof(CSLFetchNameValueDef(papszOptions, "MAX_Z_ERROR", "0.0"));
103 : }
104 :
105 7884 : static double GTiffGetLERCMaxZErrorOverview(CSLConstList papszOptions)
106 : {
107 7884 : return CPLAtof(CSLFetchNameValueDef(
108 : papszOptions, "MAX_Z_ERROR_OVERVIEW",
109 7884 : CSLFetchNameValueDef(papszOptions, "MAX_Z_ERROR", "0.0")));
110 : }
111 :
112 : #if HAVE_JXL
113 17698 : static bool GTiffGetJXLLossless(CSLConstList papszOptions,
114 : bool *pbIsSpecified = nullptr)
115 : {
116 17698 : const char *pszVal = CSLFetchNameValue(papszOptions, "JXL_LOSSLESS");
117 17698 : if (pbIsSpecified)
118 9810 : *pbIsSpecified = pszVal != nullptr;
119 17698 : return pszVal == nullptr || CPLTestBool(pszVal);
120 : }
121 :
122 17698 : static uint32_t GTiffGetJXLEffort(CSLConstList papszOptions)
123 : {
124 17698 : return atoi(CSLFetchNameValueDef(papszOptions, "JXL_EFFORT", "5"));
125 : }
126 :
127 17616 : static float GTiffGetJXLDistance(CSLConstList papszOptions,
128 : bool *pbIsSpecified = nullptr)
129 : {
130 17616 : const char *pszVal = CSLFetchNameValue(papszOptions, "JXL_DISTANCE");
131 17616 : if (pbIsSpecified)
132 9810 : *pbIsSpecified = pszVal != nullptr;
133 17616 : return pszVal == nullptr ? 1.0f : static_cast<float>(CPLAtof(pszVal));
134 : }
135 :
136 17698 : static float GTiffGetJXLAlphaDistance(CSLConstList papszOptions,
137 : bool *pbIsSpecified = nullptr)
138 : {
139 17698 : const char *pszVal = CSLFetchNameValue(papszOptions, "JXL_ALPHA_DISTANCE");
140 17698 : if (pbIsSpecified)
141 9810 : *pbIsSpecified = pszVal != nullptr;
142 17698 : return pszVal == nullptr ? -1.0f : static_cast<float>(CPLAtof(pszVal));
143 : }
144 :
145 : #endif
146 :
147 : /************************************************************************/
148 : /* FillEmptyTiles() */
149 : /************************************************************************/
150 :
151 8066 : CPLErr GTiffDataset::FillEmptyTiles()
152 :
153 : {
154 : /* -------------------------------------------------------------------- */
155 : /* How many blocks are there in this file? */
156 : /* -------------------------------------------------------------------- */
157 16132 : const int nBlockCount = m_nPlanarConfig == PLANARCONFIG_SEPARATE
158 8066 : ? m_nBlocksPerBand * nBands
159 : : m_nBlocksPerBand;
160 :
161 : /* -------------------------------------------------------------------- */
162 : /* Fetch block maps. */
163 : /* -------------------------------------------------------------------- */
164 8066 : toff_t *panByteCounts = nullptr;
165 :
166 8066 : if (TIFFIsTiled(m_hTIFF))
167 1121 : TIFFGetField(m_hTIFF, TIFFTAG_TILEBYTECOUNTS, &panByteCounts);
168 : else
169 6945 : TIFFGetField(m_hTIFF, TIFFTAG_STRIPBYTECOUNTS, &panByteCounts);
170 :
171 8066 : 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 8066 : TIFFIsTiled(m_hTIFF) ? static_cast<GPtrDiff_t>(TIFFTileSize(m_hTIFF))
184 6945 : : static_cast<GPtrDiff_t>(TIFFStripSize(m_hTIFF));
185 :
186 8066 : GByte *pabyData = static_cast<GByte *>(VSI_CALLOC_VERBOSE(nBlockBytes, 1));
187 8066 : if (pabyData == nullptr)
188 : {
189 0 : return CE_Failure;
190 : }
191 :
192 : // Force tiles completely filled with the nodata value to be written.
193 8066 : m_bWriteEmptyTiles = true;
194 :
195 : /* -------------------------------------------------------------------- */
196 : /* If set, fill data buffer with no data value. */
197 : /* -------------------------------------------------------------------- */
198 8066 : if ((m_bNoDataSet && m_dfNoDataValue != 0.0) ||
199 7798 : (m_bNoDataSetAsInt64 && m_nNoDataValueInt64 != 0) ||
200 7793 : (m_bNoDataSetAsUInt64 && m_nNoDataValueUInt64 != 0))
201 : {
202 278 : const GDALDataType eDataType = GetRasterBand(1)->GetRasterDataType();
203 278 : const int nDataTypeSize = GDALGetDataTypeSizeBytes(eDataType);
204 278 : if (nDataTypeSize &&
205 278 : nDataTypeSize * 8 == static_cast<int>(m_nBitsPerSample))
206 : {
207 267 : if (m_bNoDataSetAsInt64)
208 : {
209 6 : GDALCopyWords64(&m_nNoDataValueInt64, GDT_Int64, 0, pabyData,
210 : eDataType, nDataTypeSize,
211 6 : nBlockBytes / nDataTypeSize);
212 : }
213 261 : else if (m_bNoDataSetAsUInt64)
214 : {
215 5 : GDALCopyWords64(&m_nNoDataValueUInt64, GDT_UInt64, 0, pabyData,
216 : eDataType, nDataTypeSize,
217 5 : nBlockBytes / nDataTypeSize);
218 : }
219 : else
220 : {
221 256 : double dfNoData = m_dfNoDataValue;
222 256 : GDALCopyWords64(&dfNoData, GDT_Float64, 0, pabyData, eDataType,
223 256 : nDataTypeSize, nBlockBytes / nDataTypeSize);
224 267 : }
225 : }
226 11 : else if (nDataTypeSize)
227 : {
228 : // Handle non power-of-two depths.
229 : // Ideally make a packed buffer, but that is a bit tedious,
230 : // so use the normal I/O interfaces.
231 :
232 11 : CPLFree(pabyData);
233 :
234 11 : pabyData = static_cast<GByte *>(VSI_MALLOC3_VERBOSE(
235 : m_nBlockXSize, m_nBlockYSize, nDataTypeSize));
236 11 : if (pabyData == nullptr)
237 0 : return CE_Failure;
238 11 : if (m_bNoDataSetAsInt64)
239 : {
240 0 : GDALCopyWords64(&m_nNoDataValueInt64, GDT_Int64, 0, pabyData,
241 : eDataType, nDataTypeSize,
242 0 : static_cast<GPtrDiff_t>(m_nBlockXSize) *
243 0 : m_nBlockYSize);
244 : }
245 11 : else if (m_bNoDataSetAsUInt64)
246 : {
247 0 : GDALCopyWords64(&m_nNoDataValueUInt64, GDT_UInt64, 0, pabyData,
248 : eDataType, nDataTypeSize,
249 0 : static_cast<GPtrDiff_t>(m_nBlockXSize) *
250 0 : m_nBlockYSize);
251 : }
252 : else
253 : {
254 11 : GDALCopyWords64(&m_dfNoDataValue, GDT_Float64, 0, pabyData,
255 : eDataType, nDataTypeSize,
256 11 : static_cast<GPtrDiff_t>(m_nBlockXSize) *
257 11 : m_nBlockYSize);
258 : }
259 11 : CPLErr eErr = CE_None;
260 46 : for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
261 : {
262 35 : if (panByteCounts[iBlock] == 0)
263 : {
264 18 : if (m_nPlanarConfig == PLANARCONFIG_SEPARATE || nBands == 1)
265 : {
266 24 : if (GetRasterBand(1 + iBlock / m_nBlocksPerBand)
267 12 : ->WriteBlock((iBlock % m_nBlocksPerBand) %
268 12 : m_nBlocksPerRow,
269 12 : (iBlock % m_nBlocksPerBand) /
270 12 : m_nBlocksPerRow,
271 12 : pabyData) != CE_None)
272 : {
273 0 : eErr = CE_Failure;
274 : }
275 : }
276 : else
277 : {
278 : // In contig case, don't directly call WriteBlock(), as
279 : // it could cause useless decompression-recompression.
280 6 : const int nXOff =
281 6 : (iBlock % m_nBlocksPerRow) * m_nBlockXSize;
282 6 : const int nYOff =
283 6 : (iBlock / m_nBlocksPerRow) * m_nBlockYSize;
284 6 : const int nXSize =
285 6 : (nXOff + m_nBlockXSize <= nRasterXSize)
286 6 : ? m_nBlockXSize
287 2 : : nRasterXSize - nXOff;
288 6 : const int nYSize =
289 6 : (nYOff + m_nBlockYSize <= nRasterYSize)
290 6 : ? m_nBlockYSize
291 3 : : nRasterYSize - nYOff;
292 18 : for (int iBand = 1; iBand <= nBands; ++iBand)
293 : {
294 12 : if (GetRasterBand(iBand)->RasterIO(
295 : GF_Write, nXOff, nYOff, nXSize, nYSize,
296 : pabyData, nXSize, nYSize, eDataType, 0, 0,
297 12 : nullptr) != CE_None)
298 : {
299 0 : eErr = CE_Failure;
300 : }
301 : }
302 : }
303 : }
304 : }
305 11 : CPLFree(pabyData);
306 11 : return eErr;
307 267 : }
308 : }
309 :
310 : /* -------------------------------------------------------------------- */
311 : /* When we must fill with zeroes, try to create non-sparse file */
312 : /* w.r.t TIFF spec ... as a sparse file w.r.t filesystem, ie by */
313 : /* seeking to end of file instead of writing zero blocks. */
314 : /* -------------------------------------------------------------------- */
315 7788 : else if (m_nCompression == COMPRESSION_NONE && (m_nBitsPerSample % 8) == 0)
316 : {
317 6261 : 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 6261 : int nCountBlocksToZero = 0;
321 2320600 : for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
322 : {
323 2314340 : if (panByteCounts[iBlock] == 0)
324 : {
325 2219440 : if (nCountBlocksToZero == 0)
326 : {
327 1100 : const bool bWriteEmptyTilesBak = m_bWriteEmptyTiles;
328 1100 : m_bWriteEmptyTiles = true;
329 1100 : const bool bOK = WriteEncodedTileOrStrip(iBlock, pabyData,
330 1100 : FALSE) == CE_None;
331 1100 : m_bWriteEmptyTiles = bWriteEmptyTilesBak;
332 1100 : if (!bOK)
333 : {
334 2 : eErr = CE_Failure;
335 2 : break;
336 : }
337 : }
338 2219430 : nCountBlocksToZero++;
339 : }
340 : }
341 6261 : CPLFree(pabyData);
342 :
343 6261 : --nCountBlocksToZero;
344 :
345 : // And then seek to end of file for other ones.
346 6261 : if (nCountBlocksToZero > 0)
347 : {
348 335 : toff_t *panByteOffsets = nullptr;
349 :
350 335 : if (TIFFIsTiled(m_hTIFF))
351 92 : TIFFGetField(m_hTIFF, TIFFTAG_TILEOFFSETS, &panByteOffsets);
352 : else
353 243 : TIFFGetField(m_hTIFF, TIFFTAG_STRIPOFFSETS, &panByteOffsets);
354 :
355 335 : 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 335 : VSILFILE *fpTIF = VSI_TIFFGetVSILFile(TIFFClientdata(m_hTIFF));
364 335 : VSIFSeekL(fpTIF, 0, SEEK_END);
365 335 : const vsi_l_offset nOffset = VSIFTellL(fpTIF);
366 :
367 335 : vsi_l_offset iBlockToZero = 0;
368 2227550 : for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
369 : {
370 2227210 : if (panByteCounts[iBlock] == 0)
371 : {
372 2218340 : panByteOffsets[iBlock] = static_cast<toff_t>(
373 2218340 : nOffset + iBlockToZero * nBlockBytes);
374 2218340 : panByteCounts[iBlock] = nBlockBytes;
375 2218340 : iBlockToZero++;
376 : }
377 : }
378 335 : CPLAssert(iBlockToZero ==
379 : static_cast<vsi_l_offset>(nCountBlocksToZero));
380 :
381 335 : 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 6261 : return eErr;
390 : }
391 :
392 : /* -------------------------------------------------------------------- */
393 : /* Check all blocks, writing out data for uninitialized blocks. */
394 : /* -------------------------------------------------------------------- */
395 :
396 1794 : GByte *pabyRaw = nullptr;
397 1794 : vsi_l_offset nRawSize = 0;
398 1794 : CPLErr eErr = CE_None;
399 56225 : for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
400 : {
401 54438 : if (panByteCounts[iBlock] == 0)
402 : {
403 17452 : if (pabyRaw == nullptr)
404 : {
405 10147 : if (WriteEncodedTileOrStrip(iBlock, pabyData, FALSE) != CE_None)
406 : {
407 7 : eErr = CE_Failure;
408 7 : break;
409 : }
410 :
411 10140 : vsi_l_offset nOffset = 0;
412 10140 : 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 10140 : if (m_nCompression != COMPRESSION_NONE)
418 : {
419 : pabyRaw = static_cast<GByte *>(
420 466 : VSI_MALLOC_VERBOSE(static_cast<size_t>(nRawSize)));
421 466 : if (pabyRaw)
422 : {
423 : VSILFILE *fp =
424 466 : VSI_TIFFGetVSILFile(TIFFClientdata(m_hTIFF));
425 466 : const vsi_l_offset nCurOffset = VSIFTellL(fp);
426 466 : VSIFSeekL(fp, nOffset, SEEK_SET);
427 466 : VSIFReadL(pabyRaw, 1, static_cast<size_t>(nRawSize),
428 : fp);
429 466 : VSIFSeekL(fp, nCurOffset, SEEK_SET);
430 : }
431 : }
432 : }
433 : else
434 : {
435 7305 : WriteRawStripOrTile(iBlock, pabyRaw,
436 : static_cast<GPtrDiff_t>(nRawSize));
437 : }
438 : }
439 : }
440 :
441 1794 : CPLFree(pabyData);
442 1794 : VSIFree(pabyRaw);
443 1794 : return eErr;
444 : }
445 :
446 : /************************************************************************/
447 : /* HasOnlyNoData() */
448 : /************************************************************************/
449 :
450 42776 : bool GTiffDataset::HasOnlyNoData(const void *pBuffer, int nWidth, int nHeight,
451 : int nLineStride, int nComponents)
452 : {
453 42776 : if (m_nSampleFormat == SAMPLEFORMAT_COMPLEXINT ||
454 42776 : m_nSampleFormat == SAMPLEFORMAT_COMPLEXIEEEFP)
455 0 : return false;
456 42776 : if (m_bNoDataSetAsInt64 || m_bNoDataSetAsUInt64)
457 6 : return false; // FIXME: over pessimistic
458 85540 : return GDALBufferHasOnlyNoData(
459 42770 : pBuffer, m_bNoDataSet ? m_dfNoDataValue : 0.0, nWidth, nHeight,
460 42770 : nLineStride, nComponents, m_nBitsPerSample,
461 42770 : m_nSampleFormat == SAMPLEFORMAT_UINT ? GSF_UNSIGNED_INT
462 4821 : : m_nSampleFormat == SAMPLEFORMAT_INT ? GSF_SIGNED_INT
463 42770 : : GSF_FLOATING_POINT);
464 : }
465 :
466 : /************************************************************************/
467 : /* IsFirstPixelEqualToNoData() */
468 : /************************************************************************/
469 :
470 168710 : inline bool GTiffDataset::IsFirstPixelEqualToNoData(const void *pBuffer)
471 : {
472 168710 : const GDALDataType eDT = GetRasterBand(1)->GetRasterDataType();
473 168710 : const double dfEffectiveNoData = (m_bNoDataSet) ? m_dfNoDataValue : 0.0;
474 168710 : if (m_bNoDataSetAsInt64 || m_bNoDataSetAsUInt64)
475 10 : return true; // FIXME: over pessimistic
476 168700 : if (m_nBitsPerSample == 8 ||
477 58703 : (m_nBitsPerSample < 8 && dfEffectiveNoData == 0))
478 : {
479 113443 : 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 226577 : return GDALIsValueInRange<GByte>(dfEffectiveNoData) &&
486 113273 : *(static_cast<const GByte *>(pBuffer)) ==
487 226577 : static_cast<GByte>(dfEffectiveNoData);
488 : }
489 55257 : if (m_nBitsPerSample == 16 && eDT == GDT_UInt16)
490 : {
491 4178 : return GDALIsValueInRange<GUInt16>(dfEffectiveNoData) &&
492 2089 : *(static_cast<const GUInt16 *>(pBuffer)) ==
493 4178 : static_cast<GUInt16>(dfEffectiveNoData);
494 : }
495 53168 : if (m_nBitsPerSample == 16 && eDT == GDT_Int16)
496 : {
497 8474 : return GDALIsValueInRange<GInt16>(dfEffectiveNoData) &&
498 4237 : *(static_cast<const GInt16 *>(pBuffer)) ==
499 8474 : static_cast<GInt16>(dfEffectiveNoData);
500 : }
501 48931 : 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 48742 : 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 48489 : 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 48372 : 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 48254 : if (m_nBitsPerSample == 32 && eDT == GDT_Float32)
526 : {
527 41179 : if (std::isnan(m_dfNoDataValue))
528 3 : return CPL_TO_BOOL(
529 6 : std::isnan(*(static_cast<const float *>(pBuffer))));
530 82352 : return GDALIsValueInRange<float>(dfEffectiveNoData) &&
531 41176 : *(static_cast<const float *>(pBuffer)) ==
532 82352 : static_cast<float>(dfEffectiveNoData);
533 : }
534 7075 : if (m_nBitsPerSample == 64 && eDT == GDT_Float64)
535 : {
536 4413 : if (std::isnan(dfEffectiveNoData))
537 3 : return CPL_TO_BOOL(
538 6 : std::isnan(*(static_cast<const double *>(pBuffer))));
539 4410 : return *(static_cast<const double *>(pBuffer)) == dfEffectiveNoData;
540 : }
541 2662 : return false;
542 : }
543 :
544 : /************************************************************************/
545 : /* WriteDealWithLercAndNan() */
546 : /************************************************************************/
547 :
548 : template <typename T>
549 0 : void GTiffDataset::WriteDealWithLercAndNan(T *pBuffer, int nActualBlockWidth,
550 : int nActualBlockHeight,
551 : int nStrileHeight)
552 : {
553 : // This method does 2 things:
554 : // - warn the user if he tries to write NaN values with libtiff < 4.6.1
555 : // and multi-band PlanarConfig=Contig configuration
556 : // - and in right-most and bottom-most tiles, replace non accessible
557 : // pixel values by a safe one.
558 :
559 0 : const auto fPaddingValue =
560 : #if !defined(LIBTIFF_MULTIBAND_LERC_NAN_OK)
561 : m_nPlanarConfig == PLANARCONFIG_CONTIG && nBands > 1
562 : ? 0
563 : :
564 : #endif
565 : std::numeric_limits<T>::quiet_NaN();
566 :
567 0 : const int nBandsPerStrile =
568 0 : m_nPlanarConfig == PLANARCONFIG_CONTIG ? nBands : 1;
569 0 : for (int j = 0; j < nActualBlockHeight; ++j)
570 : {
571 : #if !defined(LIBTIFF_MULTIBAND_LERC_NAN_OK)
572 : static bool bHasWarned = false;
573 : if (m_nPlanarConfig == PLANARCONFIG_CONTIG && nBands > 1 && !bHasWarned)
574 : {
575 : for (int i = 0; i < nActualBlockWidth * nBandsPerStrile; ++i)
576 : {
577 : if (std::isnan(
578 : pBuffer[j * m_nBlockXSize * nBandsPerStrile + i]))
579 : {
580 : bHasWarned = true;
581 : CPLError(CE_Warning, CPLE_AppDefined,
582 : "libtiff < 4.6.1 does not handle properly NaN "
583 : "values for multi-band PlanarConfig=Contig "
584 : "configuration. As a workaround, you can set the "
585 : "INTERLEAVE=BAND creation option.");
586 : break;
587 : }
588 : }
589 : }
590 : #endif
591 0 : for (int i = nActualBlockWidth * nBandsPerStrile;
592 0 : i < m_nBlockXSize * nBandsPerStrile; ++i)
593 : {
594 0 : pBuffer[j * m_nBlockXSize * nBandsPerStrile + i] = fPaddingValue;
595 : }
596 : }
597 0 : for (int j = nActualBlockHeight; j < nStrileHeight; ++j)
598 : {
599 0 : for (int i = 0; i < m_nBlockXSize * nBandsPerStrile; ++i)
600 : {
601 0 : pBuffer[j * m_nBlockXSize * nBandsPerStrile + i] = fPaddingValue;
602 : }
603 : }
604 0 : }
605 :
606 : /************************************************************************/
607 : /* WriteEncodedTile() */
608 : /************************************************************************/
609 :
610 50319 : bool GTiffDataset::WriteEncodedTile(uint32_t tile, GByte *pabyData,
611 : int bPreserveDataBuffer)
612 : {
613 50319 : const int iColumn = (tile % m_nBlocksPerBand) % m_nBlocksPerRow;
614 50319 : const int iRow = (tile % m_nBlocksPerBand) / m_nBlocksPerRow;
615 :
616 100638 : const int nActualBlockWidth = (iColumn == m_nBlocksPerRow - 1)
617 50319 : ? nRasterXSize - iColumn * m_nBlockXSize
618 : : m_nBlockXSize;
619 100638 : const int nActualBlockHeight = (iRow == m_nBlocksPerColumn - 1)
620 50319 : ? nRasterYSize - iRow * m_nBlockYSize
621 : : m_nBlockYSize;
622 :
623 : /* -------------------------------------------------------------------- */
624 : /* Don't write empty blocks in some cases. */
625 : /* -------------------------------------------------------------------- */
626 50319 : if (!m_bWriteEmptyTiles && IsFirstPixelEqualToNoData(pabyData))
627 : {
628 1946 : if (!IsBlockAvailable(tile, nullptr, nullptr, nullptr))
629 : {
630 1946 : const int nComponents =
631 1946 : m_nPlanarConfig == PLANARCONFIG_CONTIG ? nBands : 1;
632 :
633 1946 : if (HasOnlyNoData(pabyData, nActualBlockWidth, nActualBlockHeight,
634 : m_nBlockXSize, nComponents))
635 : {
636 1171 : return true;
637 : }
638 : }
639 : }
640 :
641 : // Is this a partial right edge or bottom edge tile?
642 95320 : const bool bPartialTile = (nActualBlockWidth < m_nBlockXSize) ||
643 46172 : (nActualBlockHeight < m_nBlockYSize);
644 :
645 : const bool bIsLercFloatingPoint =
646 49214 : m_nCompression == COMPRESSION_LERC &&
647 66 : (GetRasterBand(1)->GetRasterDataType() == GDT_Float32 ||
648 64 : GetRasterBand(1)->GetRasterDataType() == GDT_Float64);
649 :
650 : // Do we need to spread edge values right or down for a partial
651 : // JPEG encoded tile? We do this to avoid edge artifacts.
652 : // We also need to be careful with LERC and NaN values
653 49148 : const bool bNeedTempBuffer =
654 53790 : bPartialTile &&
655 4642 : (m_nCompression == COMPRESSION_JPEG || bIsLercFloatingPoint);
656 :
657 : // If we need to fill out the tile, or if we want to prevent
658 : // TIFFWriteEncodedTile from altering the buffer as part of
659 : // byte swapping the data on write then we will need a temporary
660 : // working buffer. If not, we can just do a direct write.
661 49148 : const GPtrDiff_t cc = static_cast<GPtrDiff_t>(TIFFTileSize(m_hTIFF));
662 :
663 63457 : if (bPreserveDataBuffer &&
664 14309 : (TIFFIsByteSwapped(m_hTIFF) || bNeedTempBuffer || m_panMaskOffsetLsb))
665 : {
666 158 : if (m_pabyTempWriteBuffer == nullptr)
667 : {
668 35 : m_pabyTempWriteBuffer = CPLMalloc(cc);
669 : }
670 158 : memcpy(m_pabyTempWriteBuffer, pabyData, cc);
671 :
672 158 : pabyData = static_cast<GByte *>(m_pabyTempWriteBuffer);
673 : }
674 :
675 : // Perform tile fill if needed.
676 : // TODO: we should also handle the case of nBitsPerSample == 12
677 : // but this is more involved.
678 49148 : if (bPartialTile && m_nCompression == COMPRESSION_JPEG &&
679 134 : m_nBitsPerSample == 8)
680 : {
681 132 : const int nComponents =
682 132 : m_nPlanarConfig == PLANARCONFIG_CONTIG ? nBands : 1;
683 :
684 132 : CPLDebug("GTiff", "Filling out jpeg edge tile on write.");
685 :
686 132 : const int nRightPixelsToFill =
687 132 : iColumn == m_nBlocksPerRow - 1
688 132 : ? m_nBlockXSize * (iColumn + 1) - nRasterXSize
689 : : 0;
690 132 : const int nBottomPixelsToFill =
691 132 : iRow == m_nBlocksPerColumn - 1
692 132 : ? m_nBlockYSize * (iRow + 1) - nRasterYSize
693 : : 0;
694 :
695 : // Fill out to the right.
696 132 : const int iSrcX = m_nBlockXSize - nRightPixelsToFill - 1;
697 :
698 12461 : for (int iX = iSrcX + 1; iX < m_nBlockXSize; ++iX)
699 : {
700 3955880 : for (int iY = 0; iY < m_nBlockYSize; ++iY)
701 : {
702 3943550 : memcpy(pabyData +
703 3943550 : (static_cast<GPtrDiff_t>(m_nBlockXSize) * iY + iX) *
704 3943550 : nComponents,
705 3943550 : pabyData + (static_cast<GPtrDiff_t>(m_nBlockXSize) * iY +
706 3943550 : iSrcX) *
707 3943550 : nComponents,
708 : nComponents);
709 : }
710 : }
711 :
712 : // Now fill out the bottom.
713 132 : const int iSrcY = m_nBlockYSize - nBottomPixelsToFill - 1;
714 17682 : for (int iY = iSrcY + 1; iY < m_nBlockYSize; ++iY)
715 : {
716 17550 : memcpy(pabyData + static_cast<GPtrDiff_t>(m_nBlockXSize) *
717 17550 : nComponents * iY,
718 17550 : pabyData + static_cast<GPtrDiff_t>(m_nBlockXSize) *
719 17550 : nComponents * iSrcY,
720 17550 : static_cast<GPtrDiff_t>(m_nBlockXSize) * nComponents);
721 : }
722 : }
723 :
724 49148 : if (bIsLercFloatingPoint &&
725 : (bPartialTile
726 : #if !defined(LIBTIFF_MULTIBAND_LERC_NAN_OK)
727 : /* libtiff < 4.6.1 doesn't generate a LERC mask for multi-band contig configuration */
728 : || (m_nPlanarConfig == PLANARCONFIG_CONTIG && nBands > 1)
729 : #endif
730 : ))
731 : {
732 0 : if (GetRasterBand(1)->GetRasterDataType() == GDT_Float32)
733 0 : WriteDealWithLercAndNan(reinterpret_cast<float *>(pabyData),
734 : nActualBlockWidth, nActualBlockHeight,
735 : m_nBlockYSize);
736 : else
737 0 : WriteDealWithLercAndNan(reinterpret_cast<double *>(pabyData),
738 : nActualBlockWidth, nActualBlockHeight,
739 : m_nBlockYSize);
740 : }
741 :
742 49148 : if (m_panMaskOffsetLsb)
743 : {
744 0 : const int iBand = m_nPlanarConfig == PLANARCONFIG_SEPARATE
745 0 : ? static_cast<int>(tile) / m_nBlocksPerBand
746 : : -1;
747 0 : DiscardLsb(pabyData, cc, iBand);
748 : }
749 :
750 49148 : if (m_bStreamingOut)
751 : {
752 17 : if (tile != static_cast<uint32_t>(m_nLastWrittenBlockId + 1))
753 : {
754 1 : ReportError(CE_Failure, CPLE_NotSupported,
755 : "Attempt to write block %d whereas %d was expected",
756 1 : tile, m_nLastWrittenBlockId + 1);
757 1 : return false;
758 : }
759 16 : if (static_cast<GPtrDiff_t>(VSIFWriteL(pabyData, 1, cc, m_fpToWrite)) !=
760 : cc)
761 : {
762 0 : ReportError(CE_Failure, CPLE_FileIO,
763 : "Could not write " CPL_FRMT_GUIB " bytes",
764 : static_cast<GUIntBig>(cc));
765 0 : return false;
766 : }
767 16 : m_nLastWrittenBlockId = tile;
768 16 : return true;
769 : }
770 :
771 : /* -------------------------------------------------------------------- */
772 : /* Should we do compression in a worker thread ? */
773 : /* -------------------------------------------------------------------- */
774 49131 : if (SubmitCompressionJob(tile, pabyData, cc, m_nBlockYSize))
775 19832 : return true;
776 :
777 29299 : return TIFFWriteEncodedTile(m_hTIFF, tile, pabyData, cc) == cc;
778 : }
779 :
780 : /************************************************************************/
781 : /* WriteEncodedStrip() */
782 : /************************************************************************/
783 :
784 177708 : bool GTiffDataset::WriteEncodedStrip(uint32_t strip, GByte *pabyData,
785 : int bPreserveDataBuffer)
786 : {
787 177708 : GPtrDiff_t cc = static_cast<GPtrDiff_t>(TIFFStripSize(m_hTIFF));
788 177708 : const auto ccFull = cc;
789 :
790 : /* -------------------------------------------------------------------- */
791 : /* If this is the last strip in the image, and is partial, then */
792 : /* we need to trim the number of scanlines written to the */
793 : /* amount of valid data we have. (#2748) */
794 : /* -------------------------------------------------------------------- */
795 177708 : const int nStripWithinBand = strip % m_nBlocksPerBand;
796 177708 : int nStripHeight = m_nRowsPerStrip;
797 :
798 177708 : if (nStripWithinBand * nStripHeight > GetRasterYSize() - nStripHeight)
799 : {
800 383 : nStripHeight = GetRasterYSize() - nStripWithinBand * m_nRowsPerStrip;
801 383 : cc = (cc / m_nRowsPerStrip) * nStripHeight;
802 766 : CPLDebug("GTiff",
803 : "Adjusted bytes to write from " CPL_FRMT_GUIB
804 : " to " CPL_FRMT_GUIB ".",
805 383 : static_cast<GUIntBig>(TIFFStripSize(m_hTIFF)),
806 : static_cast<GUIntBig>(cc));
807 : }
808 :
809 : /* -------------------------------------------------------------------- */
810 : /* Don't write empty blocks in some cases. */
811 : /* -------------------------------------------------------------------- */
812 177708 : if (!m_bWriteEmptyTiles && IsFirstPixelEqualToNoData(pabyData))
813 : {
814 41004 : if (!IsBlockAvailable(strip, nullptr, nullptr, nullptr))
815 : {
816 40830 : const int nComponents =
817 40830 : m_nPlanarConfig == PLANARCONFIG_CONTIG ? nBands : 1;
818 :
819 40830 : if (HasOnlyNoData(pabyData, m_nBlockXSize, nStripHeight,
820 : m_nBlockXSize, nComponents))
821 : {
822 28304 : return true;
823 : }
824 : }
825 : }
826 :
827 : /* -------------------------------------------------------------------- */
828 : /* TIFFWriteEncodedStrip can alter the passed buffer if */
829 : /* byte-swapping is necessary so we use a temporary buffer */
830 : /* before calling it. */
831 : /* -------------------------------------------------------------------- */
832 238866 : if (bPreserveDataBuffer &&
833 89462 : (TIFFIsByteSwapped(m_hTIFF) || m_panMaskOffsetLsb))
834 : {
835 294 : if (m_pabyTempWriteBuffer == nullptr)
836 : {
837 126 : m_pabyTempWriteBuffer = CPLMalloc(ccFull);
838 : }
839 294 : memcpy(m_pabyTempWriteBuffer, pabyData, cc);
840 294 : pabyData = static_cast<GByte *>(m_pabyTempWriteBuffer);
841 : }
842 :
843 : #if !defined(LIBTIFF_MULTIBAND_LERC_NAN_OK)
844 : const bool bIsLercFloatingPoint =
845 : m_nCompression == COMPRESSION_LERC &&
846 : (GetRasterBand(1)->GetRasterDataType() == GDT_Float32 ||
847 : GetRasterBand(1)->GetRasterDataType() == GDT_Float64);
848 : if (bIsLercFloatingPoint &&
849 : /* libtiff < 4.6.1 doesn't generate a LERC mask for multi-band contig configuration */
850 : m_nPlanarConfig == PLANARCONFIG_CONTIG && nBands > 1)
851 : {
852 : if (GetRasterBand(1)->GetRasterDataType() == GDT_Float32)
853 : WriteDealWithLercAndNan(reinterpret_cast<float *>(pabyData),
854 : m_nBlockXSize, nStripHeight, nStripHeight);
855 : else
856 : WriteDealWithLercAndNan(reinterpret_cast<double *>(pabyData),
857 : m_nBlockXSize, nStripHeight, nStripHeight);
858 : }
859 : #endif
860 :
861 149404 : if (m_panMaskOffsetLsb)
862 : {
863 366 : int iBand = m_nPlanarConfig == PLANARCONFIG_SEPARATE
864 183 : ? static_cast<int>(strip) / m_nBlocksPerBand
865 : : -1;
866 183 : DiscardLsb(pabyData, cc, iBand);
867 : }
868 :
869 149404 : if (m_bStreamingOut)
870 : {
871 1408 : if (strip != static_cast<uint32_t>(m_nLastWrittenBlockId + 1))
872 : {
873 1 : ReportError(CE_Failure, CPLE_NotSupported,
874 : "Attempt to write block %d whereas %d was expected",
875 1 : strip, m_nLastWrittenBlockId + 1);
876 1 : return false;
877 : }
878 1407 : if (static_cast<GPtrDiff_t>(VSIFWriteL(pabyData, 1, cc, m_fpToWrite)) !=
879 : cc)
880 : {
881 0 : ReportError(CE_Failure, CPLE_FileIO,
882 : "Could not write " CPL_FRMT_GUIB " bytes",
883 : static_cast<GUIntBig>(cc));
884 0 : return false;
885 : }
886 1407 : m_nLastWrittenBlockId = strip;
887 1407 : return true;
888 : }
889 :
890 : /* -------------------------------------------------------------------- */
891 : /* Should we do compression in a worker thread ? */
892 : /* -------------------------------------------------------------------- */
893 147996 : if (SubmitCompressionJob(strip, pabyData, cc, nStripHeight))
894 6727 : return true;
895 :
896 141269 : return TIFFWriteEncodedStrip(m_hTIFF, strip, pabyData, cc) == cc;
897 : }
898 :
899 : /************************************************************************/
900 : /* InitCompressionThreads() */
901 : /************************************************************************/
902 :
903 31710 : void GTiffDataset::InitCompressionThreads(bool bUpdateMode,
904 : CSLConstList papszOptions)
905 : {
906 : // Raster == tile, then no need for threads
907 31710 : if (m_nBlockXSize == nRasterXSize && m_nBlockYSize == nRasterYSize)
908 23159 : return;
909 :
910 8551 : const char *pszValue = CSLFetchNameValue(papszOptions, "NUM_THREADS");
911 8551 : if (pszValue == nullptr)
912 8486 : pszValue = CPLGetConfigOption("GDAL_NUM_THREADS", nullptr);
913 8551 : if (pszValue)
914 : {
915 : int nThreads =
916 147 : EQUAL(pszValue, "ALL_CPUS") ? CPLGetNumCPUs() : atoi(pszValue);
917 147 : if (nThreads > 1024)
918 0 : nThreads = 1024; // to please Coverity
919 147 : if (nThreads > 1)
920 : {
921 98 : if ((bUpdateMode && m_nCompression != COMPRESSION_NONE) ||
922 20 : (nBands >= 1 && IsMultiThreadedReadCompatible()))
923 : {
924 73 : CPLDebug("GTiff",
925 : "Using up to %d threads for compression/decompression",
926 : nThreads);
927 :
928 73 : m_poThreadPool = GDALGetGlobalThreadPool(nThreads);
929 73 : if (bUpdateMode && m_poThreadPool)
930 58 : m_poCompressQueue = m_poThreadPool->CreateJobQueue();
931 :
932 73 : if (m_poCompressQueue != nullptr)
933 : {
934 : // Add a margin of an extra job w.r.t thread number
935 : // so as to optimize compression time (enables the main
936 : // thread to do boring I/O while all CPUs are working).
937 58 : m_asCompressionJobs.resize(nThreads + 1);
938 58 : memset(&m_asCompressionJobs[0], 0,
939 58 : m_asCompressionJobs.size() *
940 : sizeof(GTiffCompressionJob));
941 58 : for (int i = 0;
942 280 : i < static_cast<int>(m_asCompressionJobs.size()); ++i)
943 : {
944 444 : m_asCompressionJobs[i].pszTmpFilename =
945 222 : CPLStrdup(VSIMemGenerateHiddenFilename(
946 : CPLSPrintf("thread_job_%d.tif", i)));
947 222 : m_asCompressionJobs[i].nStripOrTile = -1;
948 : }
949 :
950 : // This is kind of a hack, but basically using
951 : // TIFFWriteRawStrip/Tile and then TIFFReadEncodedStrip/Tile
952 : // does not work on a newly created file, because
953 : // TIFF_MYBUFFER is not set in tif_flags
954 : // (if using TIFFWriteEncodedStrip/Tile first,
955 : // TIFFWriteBufferSetup() is automatically called).
956 : // This should likely rather fixed in libtiff itself.
957 58 : CPL_IGNORE_RET_VAL(
958 58 : TIFFWriteBufferSetup(m_hTIFF, nullptr, -1));
959 : }
960 : }
961 : }
962 69 : else if (nThreads < 0 ||
963 69 : (!EQUAL(pszValue, "0") && !EQUAL(pszValue, "1") &&
964 3 : !EQUAL(pszValue, "ALL_CPUS")))
965 : {
966 3 : ReportError(CE_Warning, CPLE_AppDefined,
967 : "Invalid value for NUM_THREADS: %s", pszValue);
968 : }
969 : }
970 : }
971 :
972 : /************************************************************************/
973 : /* ThreadCompressionFunc() */
974 : /************************************************************************/
975 :
976 26571 : void GTiffDataset::ThreadCompressionFunc(void *pData)
977 : {
978 26571 : GTiffCompressionJob *psJob = static_cast<GTiffCompressionJob *>(pData);
979 26571 : GTiffDataset *poDS = psJob->poDS;
980 :
981 26571 : VSILFILE *fpTmp = VSIFOpenL(psJob->pszTmpFilename, "wb+");
982 26571 : TIFF *hTIFFTmp = VSI_TIFFOpen(
983 53142 : psJob->pszTmpFilename, psJob->bTIFFIsBigEndian ? "wb+" : "wl+", fpTmp);
984 26571 : CPLAssert(hTIFFTmp != nullptr);
985 26571 : TIFFSetField(hTIFFTmp, TIFFTAG_IMAGEWIDTH, poDS->m_nBlockXSize);
986 26571 : TIFFSetField(hTIFFTmp, TIFFTAG_IMAGELENGTH, psJob->nHeight);
987 26571 : TIFFSetField(hTIFFTmp, TIFFTAG_BITSPERSAMPLE, poDS->m_nBitsPerSample);
988 26571 : TIFFSetField(hTIFFTmp, TIFFTAG_COMPRESSION, poDS->m_nCompression);
989 26571 : TIFFSetField(hTIFFTmp, TIFFTAG_PHOTOMETRIC, poDS->m_nPhotometric);
990 26571 : TIFFSetField(hTIFFTmp, TIFFTAG_SAMPLEFORMAT, poDS->m_nSampleFormat);
991 26571 : TIFFSetField(hTIFFTmp, TIFFTAG_SAMPLESPERPIXEL, poDS->m_nSamplesPerPixel);
992 26571 : TIFFSetField(hTIFFTmp, TIFFTAG_ROWSPERSTRIP, poDS->m_nBlockYSize);
993 26571 : TIFFSetField(hTIFFTmp, TIFFTAG_PLANARCONFIG, poDS->m_nPlanarConfig);
994 26571 : if (psJob->nPredictor != PREDICTOR_NONE)
995 263 : TIFFSetField(hTIFFTmp, TIFFTAG_PREDICTOR, psJob->nPredictor);
996 26571 : if (poDS->m_nCompression == COMPRESSION_LERC)
997 : {
998 24 : TIFFSetField(hTIFFTmp, TIFFTAG_LERC_PARAMETERS, 2,
999 24 : poDS->m_anLercAddCompressionAndVersion);
1000 : }
1001 26571 : if (psJob->nExtraSampleCount)
1002 : {
1003 352 : TIFFSetField(hTIFFTmp, TIFFTAG_EXTRASAMPLES, psJob->nExtraSampleCount,
1004 : psJob->pExtraSamples);
1005 : }
1006 :
1007 26571 : poDS->RestoreVolatileParameters(hTIFFTmp);
1008 :
1009 53142 : bool bOK = TIFFWriteEncodedStrip(hTIFFTmp, 0, psJob->pabyBuffer,
1010 26571 : psJob->nBufferSize) == psJob->nBufferSize;
1011 :
1012 26571 : toff_t nOffset = 0;
1013 26571 : if (bOK)
1014 : {
1015 26571 : toff_t *panOffsets = nullptr;
1016 26571 : toff_t *panByteCounts = nullptr;
1017 26571 : TIFFGetField(hTIFFTmp, TIFFTAG_STRIPOFFSETS, &panOffsets);
1018 26571 : TIFFGetField(hTIFFTmp, TIFFTAG_STRIPBYTECOUNTS, &panByteCounts);
1019 :
1020 26571 : nOffset = panOffsets[0];
1021 26571 : psJob->nCompressedBufferSize =
1022 26571 : static_cast<GPtrDiff_t>(panByteCounts[0]);
1023 : }
1024 : else
1025 : {
1026 0 : CPLError(CE_Failure, CPLE_AppDefined,
1027 : "Error when compressing strip/tile %d", psJob->nStripOrTile);
1028 : }
1029 :
1030 26571 : XTIFFClose(hTIFFTmp);
1031 26571 : if (VSIFCloseL(fpTmp) != 0)
1032 : {
1033 0 : if (bOK)
1034 : {
1035 0 : bOK = false;
1036 0 : CPLError(CE_Failure, CPLE_AppDefined,
1037 : "Error when compressing strip/tile %d",
1038 : psJob->nStripOrTile);
1039 : }
1040 : }
1041 :
1042 26571 : if (bOK)
1043 : {
1044 26571 : vsi_l_offset nFileSize = 0;
1045 : GByte *pabyCompressedBuffer =
1046 26571 : VSIGetMemFileBuffer(psJob->pszTmpFilename, &nFileSize, FALSE);
1047 26571 : CPLAssert(static_cast<vsi_l_offset>(
1048 : nOffset + psJob->nCompressedBufferSize) <= nFileSize);
1049 26571 : psJob->pabyCompressedBuffer = pabyCompressedBuffer + nOffset;
1050 : }
1051 : else
1052 : {
1053 0 : psJob->pabyCompressedBuffer = nullptr;
1054 0 : psJob->nCompressedBufferSize = 0;
1055 : }
1056 :
1057 26571 : auto poMainDS = poDS->m_poBaseDS ? poDS->m_poBaseDS : poDS;
1058 26571 : if (poMainDS->m_poCompressQueue)
1059 : {
1060 1576 : std::lock_guard oLock(poMainDS->m_oCompressThreadPoolMutex);
1061 1576 : psJob->bReady = true;
1062 : }
1063 26571 : }
1064 :
1065 : /************************************************************************/
1066 : /* WriteRawStripOrTile() */
1067 : /************************************************************************/
1068 :
1069 33876 : void GTiffDataset::WriteRawStripOrTile(int nStripOrTile,
1070 : GByte *pabyCompressedBuffer,
1071 : GPtrDiff_t nCompressedBufferSize)
1072 : {
1073 : #ifdef DEBUG_VERBOSE
1074 : CPLDebug("GTIFF", "Writing raw strip/tile %d, size " CPL_FRMT_GUIB,
1075 : nStripOrTile, static_cast<GUIntBig>(nCompressedBufferSize));
1076 : #endif
1077 33876 : toff_t *panOffsets = nullptr;
1078 33876 : toff_t *panByteCounts = nullptr;
1079 33876 : bool bWriteAtEnd = true;
1080 33876 : bool bWriteLeader = m_bLeaderSizeAsUInt4;
1081 33876 : bool bWriteTrailer = m_bTrailerRepeatedLast4BytesRepeated;
1082 33876 : if (TIFFGetField(m_hTIFF,
1083 33876 : TIFFIsTiled(m_hTIFF) ? TIFFTAG_TILEOFFSETS
1084 : : TIFFTAG_STRIPOFFSETS,
1085 33876 : &panOffsets) &&
1086 33876 : panOffsets != nullptr && panOffsets[nStripOrTile] != 0)
1087 : {
1088 : // Forces TIFFAppendStrip() to consider if the location of the
1089 : // tile/strip can be reused or if the strile should be written at end of
1090 : // file.
1091 360 : TIFFSetWriteOffset(m_hTIFF, 0);
1092 :
1093 360 : if (m_bBlockOrderRowMajor)
1094 : {
1095 264 : if (TIFFGetField(m_hTIFF,
1096 264 : TIFFIsTiled(m_hTIFF) ? TIFFTAG_TILEBYTECOUNTS
1097 : : TIFFTAG_STRIPBYTECOUNTS,
1098 528 : &panByteCounts) &&
1099 264 : panByteCounts != nullptr)
1100 : {
1101 264 : if (static_cast<GUIntBig>(nCompressedBufferSize) >
1102 264 : panByteCounts[nStripOrTile])
1103 : {
1104 8 : GTiffDataset *poRootDS = m_poBaseDS ? m_poBaseDS : this;
1105 8 : if (!poRootDS->m_bKnownIncompatibleEdition &&
1106 8 : !poRootDS->m_bWriteKnownIncompatibleEdition)
1107 : {
1108 8 : ReportError(
1109 : CE_Warning, CPLE_AppDefined,
1110 : "A strile cannot be rewritten in place, which "
1111 : "invalidates the BLOCK_ORDER optimization.");
1112 8 : poRootDS->m_bKnownIncompatibleEdition = true;
1113 8 : poRootDS->m_bWriteKnownIncompatibleEdition = true;
1114 : }
1115 : }
1116 : // For mask interleaving, if the size is not exactly the same,
1117 : // completely give up (we could potentially move the mask in
1118 : // case the imagery is smaller)
1119 256 : else if (m_poMaskDS && m_bMaskInterleavedWithImagery &&
1120 0 : static_cast<GUIntBig>(nCompressedBufferSize) !=
1121 0 : panByteCounts[nStripOrTile])
1122 : {
1123 0 : GTiffDataset *poRootDS = m_poBaseDS ? m_poBaseDS : this;
1124 0 : if (!poRootDS->m_bKnownIncompatibleEdition &&
1125 0 : !poRootDS->m_bWriteKnownIncompatibleEdition)
1126 : {
1127 0 : ReportError(
1128 : CE_Warning, CPLE_AppDefined,
1129 : "A strile cannot be rewritten in place, which "
1130 : "invalidates the MASK_INTERLEAVED_WITH_IMAGERY "
1131 : "optimization.");
1132 0 : poRootDS->m_bKnownIncompatibleEdition = true;
1133 0 : poRootDS->m_bWriteKnownIncompatibleEdition = true;
1134 : }
1135 0 : bWriteLeader = false;
1136 0 : bWriteTrailer = false;
1137 0 : if (m_bLeaderSizeAsUInt4)
1138 : {
1139 : // If there was a valid leader, invalidat it
1140 0 : VSI_TIFFSeek(m_hTIFF, panOffsets[nStripOrTile] - 4,
1141 : SEEK_SET);
1142 : uint32_t nOldSize;
1143 0 : VSIFReadL(&nOldSize, 1, 4,
1144 : VSI_TIFFGetVSILFile(TIFFClientdata(m_hTIFF)));
1145 0 : CPL_LSBPTR32(&nOldSize);
1146 0 : if (nOldSize == panByteCounts[nStripOrTile])
1147 : {
1148 0 : uint32_t nInvalidatedSize = 0;
1149 0 : VSI_TIFFSeek(m_hTIFF, panOffsets[nStripOrTile] - 4,
1150 : SEEK_SET);
1151 0 : VSI_TIFFWrite(m_hTIFF, &nInvalidatedSize,
1152 : sizeof(nInvalidatedSize));
1153 : }
1154 : }
1155 : }
1156 : else
1157 : {
1158 256 : bWriteAtEnd = false;
1159 : }
1160 : }
1161 : }
1162 : }
1163 33876 : if (bWriteLeader &&
1164 25000 : static_cast<GUIntBig>(nCompressedBufferSize) <= 0xFFFFFFFFU)
1165 : {
1166 : // cppcheck-suppress knownConditionTrueFalse
1167 25000 : if (bWriteAtEnd)
1168 : {
1169 24744 : VSI_TIFFSeek(m_hTIFF, 0, SEEK_END);
1170 : }
1171 : else
1172 : {
1173 : // If we rewrite an existing strile in place with an existing
1174 : // leader, check that the leader is valid, before rewriting it. And
1175 : // if it is not valid, then do not write the trailer, as we could
1176 : // corrupt other data.
1177 256 : VSI_TIFFSeek(m_hTIFF, panOffsets[nStripOrTile] - 4, SEEK_SET);
1178 : uint32_t nOldSize;
1179 256 : VSIFReadL(&nOldSize, 1, 4,
1180 : VSI_TIFFGetVSILFile(TIFFClientdata(m_hTIFF)));
1181 256 : CPL_LSBPTR32(&nOldSize);
1182 256 : bWriteLeader =
1183 256 : panByteCounts && nOldSize == panByteCounts[nStripOrTile];
1184 256 : bWriteTrailer = bWriteLeader;
1185 256 : VSI_TIFFSeek(m_hTIFF, panOffsets[nStripOrTile] - 4, SEEK_SET);
1186 : }
1187 : // cppcheck-suppress knownConditionTrueFalse
1188 25000 : if (bWriteLeader)
1189 : {
1190 25000 : uint32_t nSize = static_cast<uint32_t>(nCompressedBufferSize);
1191 25000 : CPL_LSBPTR32(&nSize);
1192 25000 : if (!VSI_TIFFWrite(m_hTIFF, &nSize, sizeof(nSize)))
1193 0 : m_bWriteError = true;
1194 : }
1195 : }
1196 : tmsize_t written;
1197 33876 : if (TIFFIsTiled(m_hTIFF))
1198 26221 : written = TIFFWriteRawTile(m_hTIFF, nStripOrTile, pabyCompressedBuffer,
1199 : nCompressedBufferSize);
1200 : else
1201 7655 : written = TIFFWriteRawStrip(m_hTIFF, nStripOrTile, pabyCompressedBuffer,
1202 : nCompressedBufferSize);
1203 33876 : if (written != nCompressedBufferSize)
1204 12 : m_bWriteError = true;
1205 33876 : if (bWriteTrailer &&
1206 25000 : static_cast<GUIntBig>(nCompressedBufferSize) <= 0xFFFFFFFFU)
1207 : {
1208 25000 : GByte abyLastBytes[4] = {};
1209 25000 : if (nCompressedBufferSize >= 4)
1210 25000 : memcpy(abyLastBytes,
1211 25000 : pabyCompressedBuffer + nCompressedBufferSize - 4, 4);
1212 : else
1213 0 : memcpy(abyLastBytes, pabyCompressedBuffer, nCompressedBufferSize);
1214 25000 : if (!VSI_TIFFWrite(m_hTIFF, abyLastBytes, 4))
1215 0 : m_bWriteError = true;
1216 : }
1217 33876 : }
1218 :
1219 : /************************************************************************/
1220 : /* WaitCompletionForJobIdx() */
1221 : /************************************************************************/
1222 :
1223 1576 : void GTiffDataset::WaitCompletionForJobIdx(int i)
1224 : {
1225 1576 : auto poMainDS = m_poBaseDS ? m_poBaseDS : this;
1226 1576 : auto poQueue = poMainDS->m_poCompressQueue.get();
1227 1576 : auto &oQueue = poMainDS->m_asQueueJobIdx;
1228 1576 : auto &asJobs = poMainDS->m_asCompressionJobs;
1229 1576 : auto &mutex = poMainDS->m_oCompressThreadPoolMutex;
1230 :
1231 1576 : CPLAssert(i >= 0 && static_cast<size_t>(i) < asJobs.size());
1232 1576 : CPLAssert(asJobs[i].nStripOrTile >= 0);
1233 1576 : CPLAssert(!oQueue.empty());
1234 :
1235 1576 : bool bHasWarned = false;
1236 : while (true)
1237 : {
1238 : bool bReady;
1239 : {
1240 2294 : std::lock_guard oLock(mutex);
1241 2294 : bReady = asJobs[i].bReady;
1242 : }
1243 2294 : if (!bReady)
1244 : {
1245 718 : if (!bHasWarned)
1246 : {
1247 436 : CPLDebug("GTIFF",
1248 : "Waiting for worker job to finish handling block %d",
1249 436 : asJobs[i].nStripOrTile);
1250 436 : bHasWarned = true;
1251 : }
1252 718 : poQueue->GetPool()->WaitEvent();
1253 : }
1254 : else
1255 : {
1256 1576 : break;
1257 : }
1258 718 : }
1259 :
1260 1576 : if (asJobs[i].nCompressedBufferSize)
1261 : {
1262 3152 : asJobs[i].poDS->WriteRawStripOrTile(asJobs[i].nStripOrTile,
1263 1576 : asJobs[i].pabyCompressedBuffer,
1264 1576 : asJobs[i].nCompressedBufferSize);
1265 : }
1266 1576 : asJobs[i].pabyCompressedBuffer = nullptr;
1267 1576 : asJobs[i].nBufferSize = 0;
1268 : {
1269 : // Likely useless, but makes Coverity happy
1270 1576 : std::lock_guard oLock(mutex);
1271 1576 : asJobs[i].bReady = false;
1272 : }
1273 1576 : asJobs[i].nStripOrTile = -1;
1274 1576 : oQueue.pop();
1275 1576 : }
1276 :
1277 : /************************************************************************/
1278 : /* WaitCompletionForBlock() */
1279 : /************************************************************************/
1280 :
1281 2318510 : void GTiffDataset::WaitCompletionForBlock(int nBlockId)
1282 : {
1283 2318510 : auto poQueue = m_poBaseDS ? m_poBaseDS->m_poCompressQueue.get()
1284 2299290 : : m_poCompressQueue.get();
1285 : // cppcheck-suppress constVariableReference
1286 2318510 : auto &oQueue = m_poBaseDS ? m_poBaseDS->m_asQueueJobIdx : m_asQueueJobIdx;
1287 : // cppcheck-suppress constVariableReference
1288 2299290 : auto &asJobs =
1289 2318510 : m_poBaseDS ? m_poBaseDS->m_asCompressionJobs : m_asCompressionJobs;
1290 :
1291 2318510 : if (poQueue != nullptr && !oQueue.empty())
1292 : {
1293 1066 : for (int i = 0; i < static_cast<int>(asJobs.size()); ++i)
1294 : {
1295 888 : if (asJobs[i].poDS == this && asJobs[i].nStripOrTile == nBlockId)
1296 : {
1297 128 : while (!oQueue.empty() &&
1298 64 : !(asJobs[oQueue.front()].poDS == this &&
1299 64 : asJobs[oQueue.front()].nStripOrTile == nBlockId))
1300 : {
1301 0 : WaitCompletionForJobIdx(oQueue.front());
1302 : }
1303 64 : CPLAssert(!oQueue.empty() &&
1304 : asJobs[oQueue.front()].poDS == this &&
1305 : asJobs[oQueue.front()].nStripOrTile == nBlockId);
1306 64 : WaitCompletionForJobIdx(oQueue.front());
1307 : }
1308 : }
1309 : }
1310 2318510 : }
1311 :
1312 : /************************************************************************/
1313 : /* SubmitCompressionJob() */
1314 : /************************************************************************/
1315 :
1316 197127 : bool GTiffDataset::SubmitCompressionJob(int nStripOrTile, GByte *pabyData,
1317 : GPtrDiff_t cc, int nHeight)
1318 : {
1319 : /* -------------------------------------------------------------------- */
1320 : /* Should we do compression in a worker thread ? */
1321 : /* -------------------------------------------------------------------- */
1322 197127 : auto poQueue = m_poBaseDS ? m_poBaseDS->m_poCompressQueue.get()
1323 183104 : : m_poCompressQueue.get();
1324 :
1325 197127 : if (poQueue && m_nCompression == COMPRESSION_NONE)
1326 : {
1327 : // We don't do multi-threaded compression for uncompressed...
1328 : // but we must wait for other related compression tasks (e.g mask)
1329 : // to be completed
1330 0 : poQueue->WaitCompletion();
1331 :
1332 : // Flush remaining data
1333 : // cppcheck-suppress constVariableReference
1334 0 : auto &oQueue =
1335 0 : m_poBaseDS ? m_poBaseDS->m_asQueueJobIdx : m_asQueueJobIdx;
1336 0 : while (!oQueue.empty())
1337 : {
1338 0 : WaitCompletionForJobIdx(oQueue.front());
1339 : }
1340 : }
1341 :
1342 : const auto SetupJob =
1343 123008 : [this, pabyData, cc, nHeight, nStripOrTile](GTiffCompressionJob &sJob)
1344 : {
1345 26571 : sJob.poDS = this;
1346 26571 : sJob.bTIFFIsBigEndian = CPL_TO_BOOL(TIFFIsBigEndian(m_hTIFF));
1347 : GByte *pabyBuffer =
1348 26571 : static_cast<GByte *>(VSI_REALLOC_VERBOSE(sJob.pabyBuffer, cc));
1349 26571 : if (!pabyBuffer)
1350 0 : return false;
1351 26571 : sJob.pabyBuffer = pabyBuffer;
1352 26571 : memcpy(sJob.pabyBuffer, pabyData, cc);
1353 26571 : sJob.nBufferSize = cc;
1354 26571 : sJob.nHeight = nHeight;
1355 26571 : sJob.nStripOrTile = nStripOrTile;
1356 26571 : sJob.nPredictor = PREDICTOR_NONE;
1357 26571 : if (GTIFFSupportsPredictor(m_nCompression))
1358 : {
1359 16724 : TIFFGetField(m_hTIFF, TIFFTAG_PREDICTOR, &sJob.nPredictor);
1360 : }
1361 :
1362 26571 : sJob.pExtraSamples = nullptr;
1363 26571 : sJob.nExtraSampleCount = 0;
1364 26571 : TIFFGetField(m_hTIFF, TIFFTAG_EXTRASAMPLES, &sJob.nExtraSampleCount,
1365 : &sJob.pExtraSamples);
1366 26571 : return true;
1367 197127 : };
1368 :
1369 197127 : if (poQueue == nullptr || !(m_nCompression == COMPRESSION_ADOBE_DEFLATE ||
1370 806 : m_nCompression == COMPRESSION_LZW ||
1371 78 : m_nCompression == COMPRESSION_PACKBITS ||
1372 72 : m_nCompression == COMPRESSION_LZMA ||
1373 62 : m_nCompression == COMPRESSION_ZSTD ||
1374 52 : m_nCompression == COMPRESSION_LERC ||
1375 46 : m_nCompression == COMPRESSION_JXL ||
1376 46 : m_nCompression == COMPRESSION_JXL_DNG_1_7 ||
1377 28 : m_nCompression == COMPRESSION_WEBP ||
1378 18 : m_nCompression == COMPRESSION_JPEG))
1379 : {
1380 195551 : if (m_bBlockOrderRowMajor || m_bLeaderSizeAsUInt4 ||
1381 170556 : m_bTrailerRepeatedLast4BytesRepeated)
1382 : {
1383 : GTiffCompressionJob sJob;
1384 24995 : memset(&sJob, 0, sizeof(sJob));
1385 24995 : if (SetupJob(sJob))
1386 : {
1387 24995 : sJob.pszTmpFilename =
1388 24995 : CPLStrdup(VSIMemGenerateHiddenFilename("temp.tif"));
1389 :
1390 24995 : ThreadCompressionFunc(&sJob);
1391 :
1392 24995 : if (sJob.nCompressedBufferSize)
1393 : {
1394 24995 : sJob.poDS->WriteRawStripOrTile(sJob.nStripOrTile,
1395 : sJob.pabyCompressedBuffer,
1396 : sJob.nCompressedBufferSize);
1397 : }
1398 :
1399 24995 : CPLFree(sJob.pabyBuffer);
1400 24995 : VSIUnlink(sJob.pszTmpFilename);
1401 24995 : CPLFree(sJob.pszTmpFilename);
1402 24995 : return sJob.nCompressedBufferSize > 0 && !m_bWriteError;
1403 : }
1404 : }
1405 :
1406 170556 : return false;
1407 : }
1408 :
1409 1576 : auto poMainDS = m_poBaseDS ? m_poBaseDS : this;
1410 1576 : auto &oQueue = poMainDS->m_asQueueJobIdx;
1411 1576 : auto &asJobs = poMainDS->m_asCompressionJobs;
1412 :
1413 1576 : int nNextCompressionJobAvail = -1;
1414 :
1415 1576 : if (oQueue.size() == asJobs.size())
1416 : {
1417 1443 : CPLAssert(!oQueue.empty());
1418 1443 : nNextCompressionJobAvail = oQueue.front();
1419 1443 : WaitCompletionForJobIdx(nNextCompressionJobAvail);
1420 : }
1421 : else
1422 : {
1423 133 : const int nJobs = static_cast<int>(asJobs.size());
1424 324 : for (int i = 0; i < nJobs; ++i)
1425 : {
1426 324 : if (asJobs[i].nBufferSize == 0)
1427 : {
1428 133 : nNextCompressionJobAvail = i;
1429 133 : break;
1430 : }
1431 : }
1432 : }
1433 1576 : CPLAssert(nNextCompressionJobAvail >= 0);
1434 :
1435 1576 : GTiffCompressionJob *psJob = &asJobs[nNextCompressionJobAvail];
1436 1576 : bool bOK = SetupJob(*psJob);
1437 1576 : if (bOK)
1438 : {
1439 1576 : poQueue->SubmitJob(ThreadCompressionFunc, psJob);
1440 1576 : oQueue.push(nNextCompressionJobAvail);
1441 : }
1442 :
1443 1576 : return bOK;
1444 : }
1445 :
1446 : /************************************************************************/
1447 : /* DiscardLsb() */
1448 : /************************************************************************/
1449 :
1450 272 : template <class T> bool MustNotDiscardLsb(T value, bool bHasNoData, T nodata)
1451 : {
1452 272 : return bHasNoData && value == nodata;
1453 : }
1454 :
1455 : template <>
1456 44 : bool MustNotDiscardLsb<float>(float value, bool bHasNoData, float nodata)
1457 : {
1458 44 : return (bHasNoData && value == nodata) || !std::isfinite(value);
1459 : }
1460 :
1461 : template <>
1462 44 : bool MustNotDiscardLsb<double>(double value, bool bHasNoData, double nodata)
1463 : {
1464 44 : return (bHasNoData && value == nodata) || !std::isfinite(value);
1465 : }
1466 :
1467 : template <class T> T AdjustValue(T value, uint64_t nRoundUpBitTest);
1468 :
1469 10 : template <class T> T AdjustValueInt(T value, uint64_t nRoundUpBitTest)
1470 : {
1471 10 : if (value >=
1472 10 : static_cast<T>(std::numeric_limits<T>::max() - (nRoundUpBitTest << 1)))
1473 0 : return static_cast<T>(value - (nRoundUpBitTest << 1));
1474 10 : return static_cast<T>(value + (nRoundUpBitTest << 1));
1475 : }
1476 :
1477 0 : template <> int8_t AdjustValue<int8_t>(int8_t value, uint64_t nRoundUpBitTest)
1478 : {
1479 0 : return AdjustValueInt(value, nRoundUpBitTest);
1480 : }
1481 :
1482 : template <>
1483 2 : uint8_t AdjustValue<uint8_t>(uint8_t value, uint64_t nRoundUpBitTest)
1484 : {
1485 2 : return AdjustValueInt(value, nRoundUpBitTest);
1486 : }
1487 :
1488 : template <>
1489 2 : int16_t AdjustValue<int16_t>(int16_t value, uint64_t nRoundUpBitTest)
1490 : {
1491 2 : return AdjustValueInt(value, nRoundUpBitTest);
1492 : }
1493 :
1494 : template <>
1495 2 : uint16_t AdjustValue<uint16_t>(uint16_t value, uint64_t nRoundUpBitTest)
1496 : {
1497 2 : return AdjustValueInt(value, nRoundUpBitTest);
1498 : }
1499 :
1500 : template <>
1501 2 : int32_t AdjustValue<int32_t>(int32_t value, uint64_t nRoundUpBitTest)
1502 : {
1503 2 : return AdjustValueInt(value, nRoundUpBitTest);
1504 : }
1505 :
1506 : template <>
1507 2 : uint32_t AdjustValue<uint32_t>(uint32_t value, uint64_t nRoundUpBitTest)
1508 : {
1509 2 : return AdjustValueInt(value, nRoundUpBitTest);
1510 : }
1511 :
1512 : template <>
1513 0 : int64_t AdjustValue<int64_t>(int64_t value, uint64_t nRoundUpBitTest)
1514 : {
1515 0 : return AdjustValueInt(value, nRoundUpBitTest);
1516 : }
1517 :
1518 : template <>
1519 0 : uint64_t AdjustValue<uint64_t>(uint64_t value, uint64_t nRoundUpBitTest)
1520 : {
1521 0 : return AdjustValueInt(value, nRoundUpBitTest);
1522 : }
1523 :
1524 0 : template <> GFloat16 AdjustValue<GFloat16>(GFloat16 value, uint64_t)
1525 : {
1526 : using std::nextafter;
1527 0 : return nextafter(value, cpl::NumericLimits<GFloat16>::max());
1528 : }
1529 :
1530 0 : template <> float AdjustValue<float>(float value, uint64_t)
1531 : {
1532 0 : return std::nextafter(value, std::numeric_limits<float>::max());
1533 : }
1534 :
1535 0 : template <> double AdjustValue<double>(double value, uint64_t)
1536 : {
1537 0 : return std::nextafter(value, std::numeric_limits<double>::max());
1538 : }
1539 :
1540 : template <class Teffective, class T>
1541 : T RoundValueDiscardLsb(const void *ptr, uint64_t nMask,
1542 : uint64_t nRoundUpBitTest);
1543 :
1544 : template <class T>
1545 16 : T RoundValueDiscardLsbUnsigned(const void *ptr, uint64_t nMask,
1546 : uint64_t nRoundUpBitTest)
1547 : {
1548 32 : if ((*reinterpret_cast<const T *>(ptr) & nMask) >
1549 16 : static_cast<uint64_t>(std::numeric_limits<T>::max()) -
1550 16 : (nRoundUpBitTest << 1U))
1551 : {
1552 4 : return static_cast<T>(std::numeric_limits<T>::max() & nMask);
1553 : }
1554 12 : const uint64_t newval =
1555 12 : (*reinterpret_cast<const T *>(ptr) & nMask) + (nRoundUpBitTest << 1U);
1556 12 : return static_cast<T>(newval);
1557 : }
1558 :
1559 : template <class T>
1560 18 : T RoundValueDiscardLsbSigned(const void *ptr, uint64_t nMask,
1561 : uint64_t nRoundUpBitTest)
1562 : {
1563 18 : T oldval = *reinterpret_cast<const T *>(ptr);
1564 18 : if (oldval < 0)
1565 : {
1566 4 : return static_cast<T>(oldval & nMask);
1567 : }
1568 14 : const uint64_t newval =
1569 14 : (*reinterpret_cast<const T *>(ptr) & nMask) + (nRoundUpBitTest << 1U);
1570 14 : if (newval > static_cast<uint64_t>(std::numeric_limits<T>::max()))
1571 4 : return static_cast<T>(std::numeric_limits<T>::max() & nMask);
1572 10 : return static_cast<T>(newval);
1573 : }
1574 :
1575 : template <>
1576 11 : uint16_t RoundValueDiscardLsb<uint16_t, uint16_t>(const void *ptr,
1577 : uint64_t nMask,
1578 : uint64_t nRoundUpBitTest)
1579 : {
1580 11 : return RoundValueDiscardLsbUnsigned<uint16_t>(ptr, nMask, nRoundUpBitTest);
1581 : }
1582 :
1583 : template <>
1584 5 : uint32_t RoundValueDiscardLsb<uint32_t, uint32_t>(const void *ptr,
1585 : uint64_t nMask,
1586 : uint64_t nRoundUpBitTest)
1587 : {
1588 5 : return RoundValueDiscardLsbUnsigned<uint32_t>(ptr, nMask, nRoundUpBitTest);
1589 : }
1590 :
1591 : template <>
1592 0 : uint64_t RoundValueDiscardLsb<uint64_t, uint64_t>(const void *ptr,
1593 : uint64_t nMask,
1594 : uint64_t nRoundUpBitTest)
1595 : {
1596 0 : return RoundValueDiscardLsbUnsigned<uint64_t>(ptr, nMask, nRoundUpBitTest);
1597 : }
1598 :
1599 : template <>
1600 0 : int8_t RoundValueDiscardLsb<int8_t, int8_t>(const void *ptr, uint64_t nMask,
1601 : uint64_t nRoundUpBitTest)
1602 : {
1603 0 : return RoundValueDiscardLsbSigned<int8_t>(ptr, nMask, nRoundUpBitTest);
1604 : }
1605 :
1606 : template <>
1607 13 : int16_t RoundValueDiscardLsb<int16_t, int16_t>(const void *ptr, uint64_t nMask,
1608 : uint64_t nRoundUpBitTest)
1609 : {
1610 13 : return RoundValueDiscardLsbSigned<int16_t>(ptr, nMask, nRoundUpBitTest);
1611 : }
1612 :
1613 : template <>
1614 5 : int32_t RoundValueDiscardLsb<int32_t, int32_t>(const void *ptr, uint64_t nMask,
1615 : uint64_t nRoundUpBitTest)
1616 : {
1617 5 : return RoundValueDiscardLsbSigned<int32_t>(ptr, nMask, nRoundUpBitTest);
1618 : }
1619 :
1620 : template <>
1621 0 : int64_t RoundValueDiscardLsb<int64_t, int64_t>(const void *ptr, uint64_t nMask,
1622 : uint64_t nRoundUpBitTest)
1623 : {
1624 0 : return RoundValueDiscardLsbSigned<int64_t>(ptr, nMask, nRoundUpBitTest);
1625 : }
1626 :
1627 : template <>
1628 0 : uint16_t RoundValueDiscardLsb<GFloat16, uint16_t>(const void *ptr,
1629 : uint64_t nMask,
1630 : uint64_t nRoundUpBitTest)
1631 : {
1632 0 : return RoundValueDiscardLsbUnsigned<uint16_t>(ptr, nMask, nRoundUpBitTest);
1633 : }
1634 :
1635 : template <>
1636 0 : uint32_t RoundValueDiscardLsb<float, uint32_t>(const void *ptr, uint64_t nMask,
1637 : uint64_t nRoundUpBitTest)
1638 : {
1639 0 : return RoundValueDiscardLsbUnsigned<uint32_t>(ptr, nMask, nRoundUpBitTest);
1640 : }
1641 :
1642 : template <>
1643 0 : uint64_t RoundValueDiscardLsb<double, uint64_t>(const void *ptr, uint64_t nMask,
1644 : uint64_t nRoundUpBitTest)
1645 : {
1646 0 : return RoundValueDiscardLsbUnsigned<uint64_t>(ptr, nMask, nRoundUpBitTest);
1647 : }
1648 :
1649 : template <class Teffective, class T>
1650 145 : static void DiscardLsbT(GByte *pabyBuffer, size_t nBytes, int iBand, int nBands,
1651 : uint16_t nPlanarConfig,
1652 : const GTiffDataset::MaskOffset *panMaskOffsetLsb,
1653 : bool bHasNoData, Teffective nNoDataValue)
1654 : {
1655 : static_assert(sizeof(Teffective) == sizeof(T),
1656 : "sizeof(Teffective) == sizeof(T)");
1657 145 : if (nPlanarConfig == PLANARCONFIG_SEPARATE)
1658 : {
1659 98 : const auto nMask = panMaskOffsetLsb[iBand].nMask;
1660 98 : const auto nRoundUpBitTest = panMaskOffsetLsb[iBand].nRoundUpBitTest;
1661 196 : for (size_t i = 0; i < nBytes / sizeof(T); ++i)
1662 : {
1663 98 : if (MustNotDiscardLsb(reinterpret_cast<Teffective *>(pabyBuffer)[i],
1664 : bHasNoData, nNoDataValue))
1665 : {
1666 22 : continue;
1667 : }
1668 :
1669 76 : if (reinterpret_cast<T *>(pabyBuffer)[i] & nRoundUpBitTest)
1670 : {
1671 30 : reinterpret_cast<T *>(pabyBuffer)[i] =
1672 15 : RoundValueDiscardLsb<Teffective, T>(
1673 15 : &(reinterpret_cast<T *>(pabyBuffer)[i]), nMask,
1674 : nRoundUpBitTest);
1675 : }
1676 : else
1677 : {
1678 61 : reinterpret_cast<T *>(pabyBuffer)[i] = static_cast<T>(
1679 61 : reinterpret_cast<T *>(pabyBuffer)[i] & nMask);
1680 : }
1681 :
1682 : // Make sure that by discarding LSB we don't end up to a value
1683 : // that is no the nodata value
1684 76 : if (MustNotDiscardLsb(reinterpret_cast<Teffective *>(pabyBuffer)[i],
1685 : bHasNoData, nNoDataValue))
1686 : {
1687 8 : reinterpret_cast<Teffective *>(pabyBuffer)[i] =
1688 4 : AdjustValue(nNoDataValue, nRoundUpBitTest);
1689 : }
1690 : }
1691 : }
1692 : else
1693 : {
1694 94 : for (size_t i = 0; i < nBytes / sizeof(T); i += nBands)
1695 : {
1696 147 : for (int j = 0; j < nBands; ++j)
1697 : {
1698 100 : if (MustNotDiscardLsb(
1699 100 : reinterpret_cast<Teffective *>(pabyBuffer)[i + j],
1700 : bHasNoData, nNoDataValue))
1701 : {
1702 14 : continue;
1703 : }
1704 :
1705 86 : if (reinterpret_cast<T *>(pabyBuffer)[i + j] &
1706 86 : panMaskOffsetLsb[j].nRoundUpBitTest)
1707 : {
1708 38 : reinterpret_cast<T *>(pabyBuffer)[i + j] =
1709 19 : RoundValueDiscardLsb<Teffective, T>(
1710 19 : &(reinterpret_cast<T *>(pabyBuffer)[i + j]),
1711 19 : panMaskOffsetLsb[j].nMask,
1712 19 : panMaskOffsetLsb[j].nRoundUpBitTest);
1713 : }
1714 : else
1715 : {
1716 67 : reinterpret_cast<T *>(pabyBuffer)[i + j] = static_cast<T>(
1717 67 : (reinterpret_cast<T *>(pabyBuffer)[i + j] &
1718 67 : panMaskOffsetLsb[j].nMask));
1719 : }
1720 :
1721 : // Make sure that by discarding LSB we don't end up to a value
1722 : // that is no the nodata value
1723 86 : if (MustNotDiscardLsb(
1724 86 : reinterpret_cast<Teffective *>(pabyBuffer)[i + j],
1725 : bHasNoData, nNoDataValue))
1726 : {
1727 8 : reinterpret_cast<Teffective *>(pabyBuffer)[i + j] =
1728 4 : AdjustValue(nNoDataValue,
1729 4 : panMaskOffsetLsb[j].nRoundUpBitTest);
1730 : }
1731 : }
1732 : }
1733 : }
1734 145 : }
1735 :
1736 183 : static void DiscardLsb(GByte *pabyBuffer, GPtrDiff_t nBytes, int iBand,
1737 : int nBands, uint16_t nSampleFormat,
1738 : uint16_t nBitsPerSample, uint16_t nPlanarConfig,
1739 : const GTiffDataset::MaskOffset *panMaskOffsetLsb,
1740 : bool bHasNoData, double dfNoDataValue)
1741 : {
1742 183 : if (nBitsPerSample == 8 && nSampleFormat == SAMPLEFORMAT_UINT)
1743 : {
1744 38 : uint8_t nNoDataValue = 0;
1745 38 : if (bHasNoData && GDALIsValueExactAs<uint8_t>(dfNoDataValue))
1746 : {
1747 6 : nNoDataValue = static_cast<uint8_t>(dfNoDataValue);
1748 : }
1749 : else
1750 : {
1751 32 : bHasNoData = false;
1752 : }
1753 38 : if (nPlanarConfig == PLANARCONFIG_SEPARATE)
1754 : {
1755 25 : const auto nMask =
1756 25 : static_cast<unsigned>(panMaskOffsetLsb[iBand].nMask);
1757 25 : const auto nRoundUpBitTest =
1758 25 : static_cast<unsigned>(panMaskOffsetLsb[iBand].nRoundUpBitTest);
1759 50 : for (decltype(nBytes) i = 0; i < nBytes; ++i)
1760 : {
1761 25 : if (bHasNoData && pabyBuffer[i] == nNoDataValue)
1762 3 : continue;
1763 :
1764 : // Keep 255 in case it is alpha.
1765 22 : if (pabyBuffer[i] != 255)
1766 : {
1767 21 : if (pabyBuffer[i] & nRoundUpBitTest)
1768 5 : pabyBuffer[i] = static_cast<GByte>(
1769 5 : std::min(255U, (pabyBuffer[i] & nMask) +
1770 5 : (nRoundUpBitTest << 1U)));
1771 : else
1772 16 : pabyBuffer[i] =
1773 16 : static_cast<GByte>(pabyBuffer[i] & nMask);
1774 :
1775 : // Make sure that by discarding LSB we don't end up to a
1776 : // value that is no the nodata value
1777 21 : if (bHasNoData && pabyBuffer[i] == nNoDataValue)
1778 2 : pabyBuffer[i] =
1779 1 : AdjustValue(nNoDataValue, nRoundUpBitTest);
1780 : }
1781 : }
1782 : }
1783 : else
1784 : {
1785 26 : for (decltype(nBytes) i = 0; i < nBytes; i += nBands)
1786 : {
1787 42 : for (int j = 0; j < nBands; ++j)
1788 : {
1789 29 : if (bHasNoData && pabyBuffer[i + j] == nNoDataValue)
1790 2 : continue;
1791 :
1792 : // Keep 255 in case it is alpha.
1793 27 : if (pabyBuffer[i + j] != 255)
1794 : {
1795 25 : if (pabyBuffer[i + j] &
1796 25 : panMaskOffsetLsb[j].nRoundUpBitTest)
1797 : {
1798 6 : pabyBuffer[i + j] = static_cast<GByte>(std::min(
1799 12 : 255U,
1800 6 : (pabyBuffer[i + j] &
1801 : static_cast<unsigned>(
1802 6 : panMaskOffsetLsb[j].nMask)) +
1803 : (static_cast<unsigned>(
1804 6 : panMaskOffsetLsb[j].nRoundUpBitTest)
1805 6 : << 1U)));
1806 : }
1807 : else
1808 : {
1809 19 : pabyBuffer[i + j] = static_cast<GByte>(
1810 19 : pabyBuffer[i + j] & panMaskOffsetLsb[j].nMask);
1811 : }
1812 :
1813 : // Make sure that by discarding LSB we don't end up to a
1814 : // value that is no the nodata value
1815 25 : if (bHasNoData && pabyBuffer[i + j] == nNoDataValue)
1816 1 : pabyBuffer[i + j] = AdjustValue(
1817 : nNoDataValue,
1818 1 : panMaskOffsetLsb[j].nRoundUpBitTest);
1819 : }
1820 : }
1821 : }
1822 38 : }
1823 : }
1824 145 : else if (nBitsPerSample == 8 && nSampleFormat == SAMPLEFORMAT_INT)
1825 : {
1826 0 : int8_t nNoDataValue = 0;
1827 0 : if (bHasNoData && GDALIsValueExactAs<int8_t>(dfNoDataValue))
1828 : {
1829 0 : nNoDataValue = static_cast<int8_t>(dfNoDataValue);
1830 : }
1831 : else
1832 : {
1833 0 : bHasNoData = false;
1834 : }
1835 0 : DiscardLsbT<int8_t, int8_t>(pabyBuffer, nBytes, iBand, nBands,
1836 : nPlanarConfig, panMaskOffsetLsb, bHasNoData,
1837 0 : nNoDataValue);
1838 : }
1839 145 : else if (nBitsPerSample == 16 && nSampleFormat == SAMPLEFORMAT_INT)
1840 : {
1841 48 : int16_t nNoDataValue = 0;
1842 48 : if (bHasNoData && GDALIsValueExactAs<int16_t>(dfNoDataValue))
1843 : {
1844 6 : nNoDataValue = static_cast<int16_t>(dfNoDataValue);
1845 : }
1846 : else
1847 : {
1848 42 : bHasNoData = false;
1849 : }
1850 48 : DiscardLsbT<int16_t, int16_t>(pabyBuffer, nBytes, iBand, nBands,
1851 : nPlanarConfig, panMaskOffsetLsb,
1852 48 : bHasNoData, nNoDataValue);
1853 : }
1854 97 : else if (nBitsPerSample == 16 && nSampleFormat == SAMPLEFORMAT_UINT)
1855 : {
1856 33 : uint16_t nNoDataValue = 0;
1857 33 : if (bHasNoData && GDALIsValueExactAs<uint16_t>(dfNoDataValue))
1858 : {
1859 6 : nNoDataValue = static_cast<uint16_t>(dfNoDataValue);
1860 : }
1861 : else
1862 : {
1863 27 : bHasNoData = false;
1864 : }
1865 33 : DiscardLsbT<uint16_t, uint16_t>(pabyBuffer, nBytes, iBand, nBands,
1866 : nPlanarConfig, panMaskOffsetLsb,
1867 33 : bHasNoData, nNoDataValue);
1868 : }
1869 64 : else if (nBitsPerSample == 32 && nSampleFormat == SAMPLEFORMAT_INT)
1870 : {
1871 13 : int32_t nNoDataValue = 0;
1872 13 : if (bHasNoData && GDALIsValueExactAs<int32_t>(dfNoDataValue))
1873 : {
1874 6 : nNoDataValue = static_cast<int32_t>(dfNoDataValue);
1875 : }
1876 : else
1877 : {
1878 7 : bHasNoData = false;
1879 : }
1880 13 : DiscardLsbT<int32_t, int32_t>(pabyBuffer, nBytes, iBand, nBands,
1881 : nPlanarConfig, panMaskOffsetLsb,
1882 13 : bHasNoData, nNoDataValue);
1883 : }
1884 51 : else if (nBitsPerSample == 32 && nSampleFormat == SAMPLEFORMAT_UINT)
1885 : {
1886 13 : uint32_t nNoDataValue = 0;
1887 13 : if (bHasNoData && GDALIsValueExactAs<uint32_t>(dfNoDataValue))
1888 : {
1889 6 : nNoDataValue = static_cast<uint32_t>(dfNoDataValue);
1890 : }
1891 : else
1892 : {
1893 7 : bHasNoData = false;
1894 : }
1895 13 : DiscardLsbT<uint32_t, uint32_t>(pabyBuffer, nBytes, iBand, nBands,
1896 : nPlanarConfig, panMaskOffsetLsb,
1897 13 : bHasNoData, nNoDataValue);
1898 : }
1899 38 : else if (nBitsPerSample == 64 && nSampleFormat == SAMPLEFORMAT_INT)
1900 : {
1901 : // FIXME: we should not rely on dfNoDataValue when we support native
1902 : // data type for nodata
1903 0 : int64_t nNoDataValue = 0;
1904 0 : if (bHasNoData && GDALIsValueExactAs<int64_t>(dfNoDataValue))
1905 : {
1906 0 : nNoDataValue = static_cast<int64_t>(dfNoDataValue);
1907 : }
1908 : else
1909 : {
1910 0 : bHasNoData = false;
1911 : }
1912 0 : DiscardLsbT<int64_t, int64_t>(pabyBuffer, nBytes, iBand, nBands,
1913 : nPlanarConfig, panMaskOffsetLsb,
1914 0 : bHasNoData, nNoDataValue);
1915 : }
1916 38 : else if (nBitsPerSample == 64 && nSampleFormat == SAMPLEFORMAT_UINT)
1917 : {
1918 : // FIXME: we should not rely on dfNoDataValue when we support native
1919 : // data type for nodata
1920 0 : uint64_t nNoDataValue = 0;
1921 0 : if (bHasNoData && GDALIsValueExactAs<uint64_t>(dfNoDataValue))
1922 : {
1923 0 : nNoDataValue = static_cast<uint64_t>(dfNoDataValue);
1924 : }
1925 : else
1926 : {
1927 0 : bHasNoData = false;
1928 : }
1929 0 : DiscardLsbT<uint64_t, uint64_t>(pabyBuffer, nBytes, iBand, nBands,
1930 : nPlanarConfig, panMaskOffsetLsb,
1931 0 : bHasNoData, nNoDataValue);
1932 : }
1933 38 : else if (nBitsPerSample == 16 && nSampleFormat == SAMPLEFORMAT_IEEEFP)
1934 : {
1935 0 : const GFloat16 fNoDataValue = static_cast<GFloat16>(dfNoDataValue);
1936 0 : DiscardLsbT<GFloat16, uint16_t>(pabyBuffer, nBytes, iBand, nBands,
1937 : nPlanarConfig, panMaskOffsetLsb,
1938 0 : bHasNoData, fNoDataValue);
1939 : }
1940 38 : else if (nBitsPerSample == 32 && nSampleFormat == SAMPLEFORMAT_IEEEFP)
1941 : {
1942 19 : const float fNoDataValue = static_cast<float>(dfNoDataValue);
1943 19 : DiscardLsbT<float, uint32_t>(pabyBuffer, nBytes, iBand, nBands,
1944 : nPlanarConfig, panMaskOffsetLsb,
1945 19 : bHasNoData, fNoDataValue);
1946 : }
1947 19 : else if (nBitsPerSample == 64 && nSampleFormat == SAMPLEFORMAT_IEEEFP)
1948 : {
1949 19 : DiscardLsbT<double, uint64_t>(pabyBuffer, nBytes, iBand, nBands,
1950 : nPlanarConfig, panMaskOffsetLsb,
1951 : bHasNoData, dfNoDataValue);
1952 : }
1953 183 : }
1954 :
1955 183 : void GTiffDataset::DiscardLsb(GByte *pabyBuffer, GPtrDiff_t nBytes,
1956 : int iBand) const
1957 : {
1958 183 : ::DiscardLsb(pabyBuffer, nBytes, iBand, nBands, m_nSampleFormat,
1959 183 : m_nBitsPerSample, m_nPlanarConfig, m_panMaskOffsetLsb,
1960 183 : m_bNoDataSet, m_dfNoDataValue);
1961 183 : }
1962 :
1963 : /************************************************************************/
1964 : /* WriteEncodedTileOrStrip() */
1965 : /************************************************************************/
1966 :
1967 228027 : CPLErr GTiffDataset::WriteEncodedTileOrStrip(uint32_t tile_or_strip, void *data,
1968 : int bPreserveDataBuffer)
1969 : {
1970 228027 : CPLErr eErr = CE_None;
1971 :
1972 228027 : if (TIFFIsTiled(m_hTIFF))
1973 : {
1974 50319 : if (!(WriteEncodedTile(tile_or_strip, static_cast<GByte *>(data),
1975 : bPreserveDataBuffer)))
1976 : {
1977 14 : eErr = CE_Failure;
1978 : }
1979 : }
1980 : else
1981 : {
1982 177708 : if (!(WriteEncodedStrip(tile_or_strip, static_cast<GByte *>(data),
1983 : bPreserveDataBuffer)))
1984 : {
1985 8 : eErr = CE_Failure;
1986 : }
1987 : }
1988 :
1989 228027 : return eErr;
1990 : }
1991 :
1992 : /************************************************************************/
1993 : /* FlushBlockBuf() */
1994 : /************************************************************************/
1995 :
1996 9602 : CPLErr GTiffDataset::FlushBlockBuf()
1997 :
1998 : {
1999 9602 : if (m_nLoadedBlock < 0 || !m_bLoadedBlockDirty)
2000 0 : return CE_None;
2001 :
2002 9602 : m_bLoadedBlockDirty = false;
2003 :
2004 : const CPLErr eErr =
2005 9602 : WriteEncodedTileOrStrip(m_nLoadedBlock, m_pabyBlockBuf, true);
2006 9602 : if (eErr != CE_None)
2007 : {
2008 0 : ReportError(CE_Failure, CPLE_AppDefined,
2009 : "WriteEncodedTile/Strip() failed.");
2010 0 : m_bWriteError = true;
2011 : }
2012 :
2013 9602 : return eErr;
2014 : }
2015 :
2016 : /************************************************************************/
2017 : /* GTiffFillStreamableOffsetAndCount() */
2018 : /************************************************************************/
2019 :
2020 8 : static void GTiffFillStreamableOffsetAndCount(TIFF *hTIFF, int nSize)
2021 : {
2022 8 : uint32_t nXSize = 0;
2023 8 : uint32_t nYSize = 0;
2024 8 : TIFFGetField(hTIFF, TIFFTAG_IMAGEWIDTH, &nXSize);
2025 8 : TIFFGetField(hTIFF, TIFFTAG_IMAGELENGTH, &nYSize);
2026 8 : const bool bIsTiled = CPL_TO_BOOL(TIFFIsTiled(hTIFF));
2027 : const int nBlockCount =
2028 8 : bIsTiled ? TIFFNumberOfTiles(hTIFF) : TIFFNumberOfStrips(hTIFF);
2029 :
2030 8 : toff_t *panOffset = nullptr;
2031 8 : TIFFGetField(hTIFF, bIsTiled ? TIFFTAG_TILEOFFSETS : TIFFTAG_STRIPOFFSETS,
2032 : &panOffset);
2033 8 : toff_t *panSize = nullptr;
2034 8 : TIFFGetField(hTIFF,
2035 : bIsTiled ? TIFFTAG_TILEBYTECOUNTS : TIFFTAG_STRIPBYTECOUNTS,
2036 : &panSize);
2037 8 : toff_t nOffset = nSize;
2038 : // Trick to avoid clang static analyzer raising false positive about
2039 : // divide by zero later.
2040 8 : int nBlocksPerBand = 1;
2041 8 : uint32_t nRowsPerStrip = 0;
2042 8 : if (!bIsTiled)
2043 : {
2044 6 : TIFFGetField(hTIFF, TIFFTAG_ROWSPERSTRIP, &nRowsPerStrip);
2045 6 : if (nRowsPerStrip > static_cast<uint32_t>(nYSize))
2046 0 : nRowsPerStrip = nYSize;
2047 6 : nBlocksPerBand = DIV_ROUND_UP(nYSize, nRowsPerStrip);
2048 : }
2049 2947 : for (int i = 0; i < nBlockCount; ++i)
2050 : {
2051 : GPtrDiff_t cc = bIsTiled
2052 2939 : ? static_cast<GPtrDiff_t>(TIFFTileSize(hTIFF))
2053 2907 : : static_cast<GPtrDiff_t>(TIFFStripSize(hTIFF));
2054 2939 : if (!bIsTiled)
2055 : {
2056 : /* --------------------------------------------------------------------
2057 : */
2058 : /* If this is the last strip in the image, and is partial, then
2059 : */
2060 : /* we need to trim the number of scanlines written to the */
2061 : /* amount of valid data we have. (#2748) */
2062 : /* --------------------------------------------------------------------
2063 : */
2064 2907 : int nStripWithinBand = i % nBlocksPerBand;
2065 2907 : if (nStripWithinBand * nRowsPerStrip > nYSize - nRowsPerStrip)
2066 : {
2067 1 : cc = (cc / nRowsPerStrip) *
2068 1 : (nYSize - nStripWithinBand * nRowsPerStrip);
2069 : }
2070 : }
2071 2939 : panOffset[i] = nOffset;
2072 2939 : panSize[i] = cc;
2073 2939 : nOffset += cc;
2074 : }
2075 8 : }
2076 :
2077 : /************************************************************************/
2078 : /* Crystalize() */
2079 : /* */
2080 : /* Make sure that the directory information is written out for */
2081 : /* a new file, require before writing any imagery data. */
2082 : /************************************************************************/
2083 :
2084 2652410 : void GTiffDataset::Crystalize()
2085 :
2086 : {
2087 2652410 : if (m_bCrystalized)
2088 2646720 : return;
2089 :
2090 : // TODO: libtiff writes extended tags in the order they are specified
2091 : // and not in increasing order.
2092 5687 : WriteMetadata(this, m_hTIFF, true, m_eProfile, m_osFilename.c_str(),
2093 5687 : m_papszCreationOptions);
2094 5687 : WriteGeoTIFFInfo();
2095 5687 : if (m_bNoDataSet)
2096 339 : WriteNoDataValue(m_hTIFF, m_dfNoDataValue);
2097 5348 : else if (m_bNoDataSetAsInt64)
2098 4 : WriteNoDataValue(m_hTIFF, m_nNoDataValueInt64);
2099 5344 : else if (m_bNoDataSetAsUInt64)
2100 4 : WriteNoDataValue(m_hTIFF, m_nNoDataValueUInt64);
2101 :
2102 5687 : m_bMetadataChanged = false;
2103 5687 : m_bGeoTIFFInfoChanged = false;
2104 5687 : m_bNoDataChanged = false;
2105 5687 : m_bNeedsRewrite = false;
2106 :
2107 5687 : m_bCrystalized = true;
2108 :
2109 5687 : TIFFWriteCheck(m_hTIFF, TIFFIsTiled(m_hTIFF), "GTiffDataset::Crystalize");
2110 :
2111 5687 : TIFFWriteDirectory(m_hTIFF);
2112 5687 : if (m_bStreamingOut)
2113 : {
2114 : // We need to write twice the directory to be sure that custom
2115 : // TIFF tags are correctly sorted and that padding bytes have been
2116 : // added.
2117 3 : TIFFSetDirectory(m_hTIFF, 0);
2118 3 : TIFFWriteDirectory(m_hTIFF);
2119 :
2120 3 : if (VSIFSeekL(m_fpL, 0, SEEK_END) != 0)
2121 : {
2122 0 : ReportError(CE_Failure, CPLE_FileIO, "Could not seek");
2123 : }
2124 3 : const int nSize = static_cast<int>(VSIFTellL(m_fpL));
2125 :
2126 3 : TIFFSetDirectory(m_hTIFF, 0);
2127 3 : GTiffFillStreamableOffsetAndCount(m_hTIFF, nSize);
2128 3 : TIFFWriteDirectory(m_hTIFF);
2129 :
2130 3 : vsi_l_offset nDataLength = 0;
2131 : void *pabyBuffer =
2132 3 : VSIGetMemFileBuffer(m_pszTmpFilename, &nDataLength, FALSE);
2133 3 : if (static_cast<int>(VSIFWriteL(
2134 3 : pabyBuffer, 1, static_cast<int>(nDataLength), m_fpToWrite)) !=
2135 : static_cast<int>(nDataLength))
2136 : {
2137 0 : ReportError(CE_Failure, CPLE_FileIO, "Could not write %d bytes",
2138 : static_cast<int>(nDataLength));
2139 : }
2140 : // In case of single strip file, there's a libtiff check that would
2141 : // issue a warning since the file hasn't the required size.
2142 3 : CPLPushErrorHandler(CPLQuietErrorHandler);
2143 3 : TIFFSetDirectory(m_hTIFF, 0);
2144 3 : CPLPopErrorHandler();
2145 : }
2146 : else
2147 : {
2148 5684 : const tdir_t nNumberOfDirs = TIFFNumberOfDirectories(m_hTIFF);
2149 5684 : if (nNumberOfDirs > 0)
2150 : {
2151 5684 : TIFFSetDirectory(m_hTIFF, static_cast<tdir_t>(nNumberOfDirs - 1));
2152 : }
2153 : }
2154 :
2155 5687 : RestoreVolatileParameters(m_hTIFF);
2156 :
2157 5687 : m_nDirOffset = TIFFCurrentDirOffset(m_hTIFF);
2158 : }
2159 :
2160 : /************************************************************************/
2161 : /* FlushCache() */
2162 : /* */
2163 : /* We override this so we can also flush out local tiff strip */
2164 : /* cache if need be. */
2165 : /************************************************************************/
2166 :
2167 4520 : CPLErr GTiffDataset::FlushCache(bool bAtClosing)
2168 :
2169 : {
2170 4520 : return FlushCacheInternal(bAtClosing, true);
2171 : }
2172 :
2173 46112 : CPLErr GTiffDataset::FlushCacheInternal(bool bAtClosing, bool bFlushDirectory)
2174 : {
2175 46112 : if (m_bIsFinalized)
2176 1 : return CE_None;
2177 :
2178 46111 : CPLErr eErr = GDALPamDataset::FlushCache(bAtClosing);
2179 :
2180 46111 : if (m_bLoadedBlockDirty && m_nLoadedBlock != -1)
2181 : {
2182 262 : if (FlushBlockBuf() != CE_None)
2183 0 : eErr = CE_Failure;
2184 : }
2185 :
2186 46111 : CPLFree(m_pabyBlockBuf);
2187 46111 : m_pabyBlockBuf = nullptr;
2188 46111 : m_nLoadedBlock = -1;
2189 46111 : m_bLoadedBlockDirty = false;
2190 :
2191 : // Finish compression
2192 46111 : auto poQueue = m_poBaseDS ? m_poBaseDS->m_poCompressQueue.get()
2193 43792 : : m_poCompressQueue.get();
2194 46111 : if (poQueue)
2195 : {
2196 161 : poQueue->WaitCompletion();
2197 :
2198 : // Flush remaining data
2199 : // cppcheck-suppress constVariableReference
2200 :
2201 161 : auto &oQueue =
2202 161 : m_poBaseDS ? m_poBaseDS->m_asQueueJobIdx : m_asQueueJobIdx;
2203 230 : while (!oQueue.empty())
2204 : {
2205 69 : WaitCompletionForJobIdx(oQueue.front());
2206 : }
2207 : }
2208 :
2209 46111 : if (bFlushDirectory && GetAccess() == GA_Update)
2210 : {
2211 13780 : if (FlushDirectory() != CE_None)
2212 12 : eErr = CE_Failure;
2213 : }
2214 46111 : return eErr;
2215 : }
2216 :
2217 : /************************************************************************/
2218 : /* FlushDirectory() */
2219 : /************************************************************************/
2220 :
2221 21595 : CPLErr GTiffDataset::FlushDirectory()
2222 :
2223 : {
2224 21595 : CPLErr eErr = CE_None;
2225 :
2226 680 : const auto ReloadAllOtherDirectories = [this]()
2227 : {
2228 335 : const auto poBaseDS = m_poBaseDS ? m_poBaseDS : this;
2229 338 : for (auto &poOvrDS : poBaseDS->m_apoOverviewDS)
2230 : {
2231 3 : if (poOvrDS->m_bCrystalized && poOvrDS.get() != this)
2232 : {
2233 3 : poOvrDS->ReloadDirectory(true);
2234 : }
2235 :
2236 3 : if (poOvrDS->m_poMaskDS && poOvrDS->m_poMaskDS.get() != this &&
2237 0 : poOvrDS->m_poMaskDS->m_bCrystalized)
2238 : {
2239 0 : poOvrDS->m_poMaskDS->ReloadDirectory(true);
2240 : }
2241 : }
2242 335 : if (poBaseDS->m_poMaskDS && poBaseDS->m_poMaskDS.get() != this &&
2243 0 : poBaseDS->m_poMaskDS->m_bCrystalized)
2244 : {
2245 0 : poBaseDS->m_poMaskDS->ReloadDirectory(true);
2246 : }
2247 335 : if (poBaseDS->m_bCrystalized && poBaseDS != this)
2248 : {
2249 7 : poBaseDS->ReloadDirectory(true);
2250 : }
2251 335 : };
2252 :
2253 21595 : if (eAccess == GA_Update)
2254 : {
2255 15445 : if (m_bMetadataChanged)
2256 : {
2257 199 : m_bNeedsRewrite =
2258 199 : WriteMetadata(this, m_hTIFF, true, m_eProfile,
2259 199 : m_osFilename.c_str(), m_papszCreationOptions);
2260 199 : m_bMetadataChanged = false;
2261 :
2262 199 : if (m_bForceUnsetRPC)
2263 : {
2264 5 : double *padfRPCTag = nullptr;
2265 : uint16_t nCount;
2266 5 : if (TIFFGetField(m_hTIFF, TIFFTAG_RPCCOEFFICIENT, &nCount,
2267 5 : &padfRPCTag))
2268 : {
2269 3 : std::vector<double> zeroes(92);
2270 3 : TIFFSetField(m_hTIFF, TIFFTAG_RPCCOEFFICIENT, 92,
2271 : zeroes.data());
2272 3 : TIFFUnsetField(m_hTIFF, TIFFTAG_RPCCOEFFICIENT);
2273 3 : m_bNeedsRewrite = true;
2274 : }
2275 :
2276 5 : if (m_poBaseDS == nullptr)
2277 : {
2278 5 : GDALWriteRPCTXTFile(m_osFilename.c_str(), nullptr);
2279 5 : GDALWriteRPBFile(m_osFilename.c_str(), nullptr);
2280 : }
2281 : }
2282 : }
2283 :
2284 15445 : if (m_bGeoTIFFInfoChanged)
2285 : {
2286 145 : WriteGeoTIFFInfo();
2287 145 : m_bGeoTIFFInfoChanged = false;
2288 : }
2289 :
2290 15445 : if (m_bNoDataChanged)
2291 : {
2292 52 : if (m_bNoDataSet)
2293 : {
2294 36 : WriteNoDataValue(m_hTIFF, m_dfNoDataValue);
2295 : }
2296 16 : else if (m_bNoDataSetAsInt64)
2297 : {
2298 0 : WriteNoDataValue(m_hTIFF, m_nNoDataValueInt64);
2299 : }
2300 16 : else if (m_bNoDataSetAsUInt64)
2301 : {
2302 0 : WriteNoDataValue(m_hTIFF, m_nNoDataValueUInt64);
2303 : }
2304 : else
2305 : {
2306 16 : UnsetNoDataValue(m_hTIFF);
2307 : }
2308 52 : m_bNeedsRewrite = true;
2309 52 : m_bNoDataChanged = false;
2310 : }
2311 :
2312 15445 : if (m_bNeedsRewrite)
2313 : {
2314 360 : if (!m_bCrystalized)
2315 : {
2316 28 : Crystalize();
2317 : }
2318 : else
2319 : {
2320 332 : const TIFFSizeProc pfnSizeProc = TIFFGetSizeProc(m_hTIFF);
2321 :
2322 332 : m_nDirOffset = pfnSizeProc(TIFFClientdata(m_hTIFF));
2323 332 : if ((m_nDirOffset % 2) == 1)
2324 69 : ++m_nDirOffset;
2325 :
2326 332 : if (TIFFRewriteDirectory(m_hTIFF) == 0)
2327 0 : eErr = CE_Failure;
2328 :
2329 332 : TIFFSetSubDirectory(m_hTIFF, m_nDirOffset);
2330 :
2331 332 : ReloadAllOtherDirectories();
2332 :
2333 332 : if (m_bLayoutIFDSBeforeData && m_bBlockOrderRowMajor &&
2334 1 : m_bLeaderSizeAsUInt4 &&
2335 1 : m_bTrailerRepeatedLast4BytesRepeated &&
2336 1 : !m_bKnownIncompatibleEdition &&
2337 1 : !m_bWriteKnownIncompatibleEdition)
2338 : {
2339 1 : ReportError(CE_Warning, CPLE_AppDefined,
2340 : "The IFD has been rewritten at the end of "
2341 : "the file, which breaks COG layout.");
2342 1 : m_bKnownIncompatibleEdition = true;
2343 1 : m_bWriteKnownIncompatibleEdition = true;
2344 : }
2345 : }
2346 :
2347 360 : m_bNeedsRewrite = false;
2348 : }
2349 : }
2350 :
2351 : // There are some circumstances in which we can reach this point
2352 : // without having made this our directory (SetDirectory()) in which
2353 : // case we should not risk a flush.
2354 37040 : if (GetAccess() == GA_Update &&
2355 15445 : TIFFCurrentDirOffset(m_hTIFF) == m_nDirOffset)
2356 : {
2357 15445 : const TIFFSizeProc pfnSizeProc = TIFFGetSizeProc(m_hTIFF);
2358 :
2359 15445 : toff_t nNewDirOffset = pfnSizeProc(TIFFClientdata(m_hTIFF));
2360 15445 : if ((nNewDirOffset % 2) == 1)
2361 3382 : ++nNewDirOffset;
2362 :
2363 15445 : if (TIFFFlush(m_hTIFF) == 0)
2364 12 : eErr = CE_Failure;
2365 :
2366 15445 : if (m_nDirOffset != TIFFCurrentDirOffset(m_hTIFF))
2367 : {
2368 3 : m_nDirOffset = nNewDirOffset;
2369 3 : ReloadAllOtherDirectories();
2370 3 : CPLDebug("GTiff",
2371 : "directory moved during flush in FlushDirectory()");
2372 : }
2373 : }
2374 :
2375 21595 : SetDirectory();
2376 21595 : return eErr;
2377 : }
2378 :
2379 : /************************************************************************/
2380 : /* CleanOverviews() */
2381 : /************************************************************************/
2382 :
2383 5 : CPLErr GTiffDataset::CleanOverviews()
2384 :
2385 : {
2386 5 : CPLAssert(!m_poBaseDS);
2387 :
2388 5 : ScanDirectories();
2389 :
2390 5 : FlushDirectory();
2391 :
2392 : /* -------------------------------------------------------------------- */
2393 : /* Cleanup overviews objects, and get offsets to all overview */
2394 : /* directories. */
2395 : /* -------------------------------------------------------------------- */
2396 10 : std::vector<toff_t> anOvDirOffsets;
2397 :
2398 10 : for (auto &poOvrDS : m_apoOverviewDS)
2399 : {
2400 5 : anOvDirOffsets.push_back(poOvrDS->m_nDirOffset);
2401 5 : if (poOvrDS->m_poMaskDS)
2402 1 : anOvDirOffsets.push_back(poOvrDS->m_poMaskDS->m_nDirOffset);
2403 : }
2404 5 : m_apoOverviewDS.clear();
2405 :
2406 : /* -------------------------------------------------------------------- */
2407 : /* Loop through all the directories, translating the offsets */
2408 : /* into indexes we can use with TIFFUnlinkDirectory(). */
2409 : /* -------------------------------------------------------------------- */
2410 10 : std::vector<uint16_t> anOvDirIndexes;
2411 5 : int iThisOffset = 1;
2412 :
2413 5 : TIFFSetDirectory(m_hTIFF, 0);
2414 :
2415 : while (true)
2416 : {
2417 28 : for (toff_t nOffset : anOvDirOffsets)
2418 : {
2419 16 : if (nOffset == TIFFCurrentDirOffset(m_hTIFF))
2420 : {
2421 6 : anOvDirIndexes.push_back(static_cast<uint16_t>(iThisOffset));
2422 : }
2423 : }
2424 :
2425 12 : if (TIFFLastDirectory(m_hTIFF))
2426 5 : break;
2427 :
2428 7 : TIFFReadDirectory(m_hTIFF);
2429 7 : ++iThisOffset;
2430 7 : }
2431 :
2432 : /* -------------------------------------------------------------------- */
2433 : /* Actually unlink the target directories. Note that we do */
2434 : /* this from last to first so as to avoid renumbering any of */
2435 : /* the earlier directories we need to remove. */
2436 : /* -------------------------------------------------------------------- */
2437 11 : while (!anOvDirIndexes.empty())
2438 : {
2439 6 : TIFFUnlinkDirectory(m_hTIFF, anOvDirIndexes.back());
2440 6 : anOvDirIndexes.pop_back();
2441 : }
2442 :
2443 5 : if (m_poMaskDS)
2444 : {
2445 1 : m_poMaskDS->m_apoOverviewDS.clear();
2446 : }
2447 :
2448 5 : if (!SetDirectory())
2449 0 : return CE_Failure;
2450 :
2451 5 : return CE_None;
2452 : }
2453 :
2454 : /************************************************************************/
2455 : /* RegisterNewOverviewDataset() */
2456 : /************************************************************************/
2457 :
2458 509 : CPLErr GTiffDataset::RegisterNewOverviewDataset(toff_t nOverviewOffset,
2459 : int l_nJpegQuality,
2460 : CSLConstList papszOptions)
2461 : {
2462 : const auto GetOptionValue =
2463 5599 : [papszOptions](const char *pszOptionKey, const char *pszConfigOptionKey,
2464 11197 : const char **ppszKeyUsed = nullptr)
2465 : {
2466 5599 : const char *pszVal = CSLFetchNameValue(papszOptions, pszOptionKey);
2467 5599 : if (pszVal)
2468 : {
2469 1 : if (ppszKeyUsed)
2470 1 : *ppszKeyUsed = pszOptionKey;
2471 1 : return pszVal;
2472 : }
2473 5598 : pszVal = CSLFetchNameValue(papszOptions, pszConfigOptionKey);
2474 5598 : if (pszVal)
2475 : {
2476 0 : if (ppszKeyUsed)
2477 0 : *ppszKeyUsed = pszConfigOptionKey;
2478 0 : return pszVal;
2479 : }
2480 5598 : if (pszConfigOptionKey)
2481 : {
2482 5598 : pszVal = CPLGetConfigOption(pszConfigOptionKey, nullptr);
2483 5598 : if (pszVal && ppszKeyUsed)
2484 13 : *ppszKeyUsed = pszConfigOptionKey;
2485 : }
2486 5598 : return pszVal;
2487 509 : };
2488 :
2489 509 : int nZLevel = m_nZLevel;
2490 509 : if (const char *opt = GetOptionValue("ZLEVEL", "ZLEVEL_OVERVIEW"))
2491 : {
2492 4 : nZLevel = atoi(opt);
2493 : }
2494 :
2495 509 : int nZSTDLevel = m_nZSTDLevel;
2496 509 : if (const char *opt = GetOptionValue("ZSTD_LEVEL", "ZSTD_LEVEL_OVERVIEW"))
2497 : {
2498 4 : nZSTDLevel = atoi(opt);
2499 : }
2500 :
2501 509 : bool bWebpLossless = m_bWebPLossless;
2502 : const char *pszWebPLosslessOverview =
2503 509 : GetOptionValue("WEBP_LOSSLESS", "WEBP_LOSSLESS_OVERVIEW");
2504 509 : if (pszWebPLosslessOverview)
2505 : {
2506 2 : bWebpLossless = CPLTestBool(pszWebPLosslessOverview);
2507 : }
2508 :
2509 509 : int nWebpLevel = m_nWebPLevel;
2510 509 : const char *pszKeyWebpLevel = "";
2511 509 : if (const char *opt = GetOptionValue("WEBP_LEVEL", "WEBP_LEVEL_OVERVIEW",
2512 : &pszKeyWebpLevel))
2513 : {
2514 14 : if (pszWebPLosslessOverview == nullptr && m_bWebPLossless)
2515 : {
2516 1 : CPLDebug("GTiff",
2517 : "%s specified, but not WEBP_LOSSLESS_OVERVIEW. "
2518 : "Assuming WEBP_LOSSLESS_OVERVIEW=NO",
2519 : pszKeyWebpLevel);
2520 1 : bWebpLossless = false;
2521 : }
2522 13 : else if (bWebpLossless)
2523 : {
2524 0 : CPLError(CE_Warning, CPLE_AppDefined,
2525 : "%s is specified, but WEBP_LOSSLESS_OVERVIEW=YES. "
2526 : "%s will be ignored.",
2527 : pszKeyWebpLevel, pszKeyWebpLevel);
2528 : }
2529 14 : nWebpLevel = atoi(opt);
2530 : }
2531 :
2532 509 : double dfMaxZError = m_dfMaxZErrorOverview;
2533 509 : if (const char *opt = GetOptionValue("MAX_Z_ERROR", "MAX_Z_ERROR_OVERVIEW"))
2534 : {
2535 20 : dfMaxZError = CPLAtof(opt);
2536 : }
2537 :
2538 509 : signed char nJpegTablesMode = m_nJpegTablesMode;
2539 509 : if (const char *opt =
2540 509 : GetOptionValue("JPEG_TABLESMODE", "JPEG_TABLESMODE_OVERVIEW"))
2541 : {
2542 0 : nJpegTablesMode = static_cast<signed char>(atoi(opt));
2543 : }
2544 :
2545 : #ifdef HAVE_JXL
2546 509 : bool bJXLLossless = m_bJXLLossless;
2547 509 : if (const char *opt =
2548 509 : GetOptionValue("JXL_LOSSLESS", "JXL_LOSSLESS_OVERVIEW"))
2549 : {
2550 0 : bJXLLossless = CPLTestBool(opt);
2551 : }
2552 :
2553 509 : float fJXLDistance = m_fJXLDistance;
2554 509 : if (const char *opt =
2555 509 : GetOptionValue("JXL_DISTANCE", "JXL_DISTANCE_OVERVIEW"))
2556 : {
2557 0 : fJXLDistance = static_cast<float>(CPLAtof(opt));
2558 : }
2559 :
2560 509 : float fJXLAlphaDistance = m_fJXLAlphaDistance;
2561 509 : if (const char *opt =
2562 509 : GetOptionValue("JXL_ALPHA_DISTANCE", "JXL_ALPHA_DISTANCE_OVERVIEW"))
2563 : {
2564 0 : fJXLAlphaDistance = static_cast<float>(CPLAtof(opt));
2565 : }
2566 :
2567 509 : int nJXLEffort = m_nJXLEffort;
2568 509 : if (const char *opt = GetOptionValue("JXL_EFFORT", "JXL_EFFORT_OVERVIEW"))
2569 : {
2570 0 : nJXLEffort = atoi(opt);
2571 : }
2572 : #endif
2573 :
2574 1018 : auto poODS = std::make_shared<GTiffDataset>();
2575 509 : poODS->ShareLockWithParentDataset(this);
2576 509 : poODS->eAccess = GA_Update;
2577 509 : poODS->m_osFilename = m_osFilename;
2578 509 : const char *pszSparseOK = GetOptionValue("SPARSE_OK", "SPARSE_OK_OVERVIEW");
2579 509 : if (pszSparseOK && CPLTestBool(pszSparseOK))
2580 : {
2581 1 : poODS->m_bWriteEmptyTiles = false;
2582 1 : poODS->m_bFillEmptyTilesAtClosing = false;
2583 : }
2584 : else
2585 : {
2586 508 : poODS->m_bWriteEmptyTiles = m_bWriteEmptyTiles;
2587 508 : poODS->m_bFillEmptyTilesAtClosing = m_bFillEmptyTilesAtClosing;
2588 : }
2589 509 : poODS->m_nJpegQuality = static_cast<signed char>(l_nJpegQuality);
2590 509 : poODS->m_nWebPLevel = static_cast<signed char>(nWebpLevel);
2591 509 : poODS->m_nZLevel = static_cast<signed char>(nZLevel);
2592 509 : poODS->m_nLZMAPreset = m_nLZMAPreset;
2593 509 : poODS->m_nZSTDLevel = static_cast<signed char>(nZSTDLevel);
2594 509 : poODS->m_bWebPLossless = bWebpLossless;
2595 509 : poODS->m_nJpegTablesMode = nJpegTablesMode;
2596 509 : poODS->m_dfMaxZError = dfMaxZError;
2597 509 : poODS->m_dfMaxZErrorOverview = dfMaxZError;
2598 1018 : memcpy(poODS->m_anLercAddCompressionAndVersion,
2599 509 : m_anLercAddCompressionAndVersion,
2600 : sizeof(m_anLercAddCompressionAndVersion));
2601 : #ifdef HAVE_JXL
2602 509 : poODS->m_bJXLLossless = bJXLLossless;
2603 509 : poODS->m_fJXLDistance = fJXLDistance;
2604 509 : poODS->m_fJXLAlphaDistance = fJXLAlphaDistance;
2605 509 : poODS->m_nJXLEffort = nJXLEffort;
2606 : #endif
2607 :
2608 509 : if (poODS->OpenOffset(VSI_TIFFOpenChild(m_hTIFF), nOverviewOffset,
2609 509 : GA_Update) != CE_None)
2610 : {
2611 0 : return CE_Failure;
2612 : }
2613 :
2614 : // Assign color interpretation from main dataset
2615 509 : const int l_nBands = GetRasterCount();
2616 1522 : for (int i = 1; i <= l_nBands; i++)
2617 : {
2618 1013 : auto poBand = dynamic_cast<GTiffRasterBand *>(poODS->GetRasterBand(i));
2619 1013 : if (poBand)
2620 1013 : poBand->m_eBandInterp = GetRasterBand(i)->GetColorInterpretation();
2621 : }
2622 :
2623 : // Do that now that m_nCompression is set
2624 509 : poODS->RestoreVolatileParameters(poODS->m_hTIFF);
2625 :
2626 509 : poODS->m_poBaseDS = this;
2627 509 : poODS->m_bIsOverview = true;
2628 :
2629 509 : m_apoOverviewDS.push_back(std::move(poODS));
2630 509 : return CE_None;
2631 : }
2632 :
2633 : /************************************************************************/
2634 : /* CreateTIFFColorTable() */
2635 : /************************************************************************/
2636 :
2637 12 : static void CreateTIFFColorTable(
2638 : GDALColorTable *poColorTable, int nBits, int nColorTableMultiplier,
2639 : std::vector<unsigned short> &anTRed, std::vector<unsigned short> &anTGreen,
2640 : std::vector<unsigned short> &anTBlue, unsigned short *&panRed,
2641 : unsigned short *&panGreen, unsigned short *&panBlue)
2642 : {
2643 : int nColors;
2644 :
2645 12 : if (nBits == 8)
2646 12 : nColors = 256;
2647 0 : else if (nBits < 8)
2648 0 : nColors = 1 << nBits;
2649 : else
2650 0 : nColors = 65536;
2651 :
2652 12 : anTRed.resize(nColors, 0);
2653 12 : anTGreen.resize(nColors, 0);
2654 12 : anTBlue.resize(nColors, 0);
2655 :
2656 3084 : for (int iColor = 0; iColor < nColors; ++iColor)
2657 : {
2658 3072 : if (iColor < poColorTable->GetColorEntryCount())
2659 : {
2660 : GDALColorEntry sRGB;
2661 :
2662 3072 : poColorTable->GetColorEntryAsRGB(iColor, &sRGB);
2663 :
2664 3072 : anTRed[iColor] = GTiffDataset::ClampCTEntry(iColor, 1, sRGB.c1,
2665 : nColorTableMultiplier);
2666 3072 : anTGreen[iColor] = GTiffDataset::ClampCTEntry(
2667 3072 : iColor, 2, sRGB.c2, nColorTableMultiplier);
2668 3072 : anTBlue[iColor] = GTiffDataset::ClampCTEntry(iColor, 3, sRGB.c3,
2669 : nColorTableMultiplier);
2670 : }
2671 : else
2672 : {
2673 0 : anTRed[iColor] = 0;
2674 0 : anTGreen[iColor] = 0;
2675 0 : anTBlue[iColor] = 0;
2676 : }
2677 : }
2678 :
2679 12 : panRed = &(anTRed[0]);
2680 12 : panGreen = &(anTGreen[0]);
2681 12 : panBlue = &(anTBlue[0]);
2682 12 : }
2683 :
2684 : /************************************************************************/
2685 : /* GetOverviewParameters() */
2686 : /************************************************************************/
2687 :
2688 326 : bool GTiffDataset::GetOverviewParameters(
2689 : int &nCompression, uint16_t &nPlanarConfig, uint16_t &nPredictor,
2690 : uint16_t &nPhotometric, int &nOvrJpegQuality, std::string &osNoData,
2691 : uint16_t *&panExtraSampleValues, uint16_t &nExtraSamples,
2692 : CSLConstList papszOptions) const
2693 : {
2694 : const auto GetOptionValue =
2695 1080 : [papszOptions](const char *pszOptionKey, const char *pszConfigOptionKey,
2696 2152 : const char **ppszKeyUsed = nullptr)
2697 : {
2698 1080 : const char *pszVal = CSLFetchNameValue(papszOptions, pszOptionKey);
2699 1080 : if (pszVal)
2700 : {
2701 8 : if (ppszKeyUsed)
2702 8 : *ppszKeyUsed = pszOptionKey;
2703 8 : return pszVal;
2704 : }
2705 1072 : pszVal = CSLFetchNameValue(papszOptions, pszConfigOptionKey);
2706 1072 : if (pszVal)
2707 : {
2708 0 : if (ppszKeyUsed)
2709 0 : *ppszKeyUsed = pszConfigOptionKey;
2710 0 : return pszVal;
2711 : }
2712 1072 : pszVal = CPLGetConfigOption(pszConfigOptionKey, nullptr);
2713 1072 : if (pszVal && ppszKeyUsed)
2714 58 : *ppszKeyUsed = pszConfigOptionKey;
2715 1072 : return pszVal;
2716 326 : };
2717 :
2718 : /* -------------------------------------------------------------------- */
2719 : /* Determine compression method. */
2720 : /* -------------------------------------------------------------------- */
2721 326 : nCompression = m_nCompression;
2722 326 : const char *pszOptionKey = "";
2723 : const char *pszCompressValue =
2724 326 : GetOptionValue("COMPRESS", "COMPRESS_OVERVIEW", &pszOptionKey);
2725 326 : if (pszCompressValue != nullptr)
2726 : {
2727 56 : nCompression =
2728 56 : GTIFFGetCompressionMethod(pszCompressValue, pszOptionKey);
2729 56 : if (nCompression < 0)
2730 : {
2731 0 : nCompression = m_nCompression;
2732 : }
2733 : }
2734 :
2735 : /* -------------------------------------------------------------------- */
2736 : /* Determine planar configuration. */
2737 : /* -------------------------------------------------------------------- */
2738 326 : nPlanarConfig = m_nPlanarConfig;
2739 326 : if (nCompression == COMPRESSION_WEBP)
2740 : {
2741 11 : nPlanarConfig = PLANARCONFIG_CONTIG;
2742 : }
2743 : const char *pszInterleave =
2744 326 : GetOptionValue("INTERLEAVE", "INTERLEAVE_OVERVIEW", &pszOptionKey);
2745 326 : if (pszInterleave != nullptr && pszInterleave[0] != '\0')
2746 : {
2747 2 : if (EQUAL(pszInterleave, "PIXEL"))
2748 1 : nPlanarConfig = PLANARCONFIG_CONTIG;
2749 1 : else if (EQUAL(pszInterleave, "BAND"))
2750 1 : nPlanarConfig = PLANARCONFIG_SEPARATE;
2751 : else
2752 : {
2753 0 : CPLError(CE_Warning, CPLE_AppDefined,
2754 : "%s=%s unsupported, "
2755 : "value must be PIXEL or BAND. ignoring",
2756 : pszOptionKey, pszInterleave);
2757 : }
2758 : }
2759 :
2760 : /* -------------------------------------------------------------------- */
2761 : /* Determine predictor tag */
2762 : /* -------------------------------------------------------------------- */
2763 326 : nPredictor = PREDICTOR_NONE;
2764 326 : if (GTIFFSupportsPredictor(nCompression))
2765 : {
2766 : const char *pszPredictor =
2767 75 : GetOptionValue("PREDICTOR", "PREDICTOR_OVERVIEW");
2768 75 : if (pszPredictor != nullptr)
2769 : {
2770 1 : nPredictor = static_cast<uint16_t>(atoi(pszPredictor));
2771 : }
2772 74 : else if (GTIFFSupportsPredictor(m_nCompression))
2773 73 : TIFFGetField(m_hTIFF, TIFFTAG_PREDICTOR, &nPredictor);
2774 : }
2775 :
2776 : /* -------------------------------------------------------------------- */
2777 : /* Determine photometric tag */
2778 : /* -------------------------------------------------------------------- */
2779 326 : if (m_nPhotometric == PHOTOMETRIC_YCBCR && nCompression != COMPRESSION_JPEG)
2780 1 : nPhotometric = PHOTOMETRIC_RGB;
2781 : else
2782 325 : nPhotometric = m_nPhotometric;
2783 : const char *pszPhotometric =
2784 326 : GetOptionValue("PHOTOMETRIC", "PHOTOMETRIC_OVERVIEW", &pszOptionKey);
2785 326 : if (!GTIFFUpdatePhotometric(pszPhotometric, pszOptionKey, nCompression,
2786 326 : pszInterleave, nBands, nPhotometric,
2787 : nPlanarConfig))
2788 : {
2789 0 : return false;
2790 : }
2791 :
2792 : /* -------------------------------------------------------------------- */
2793 : /* Determine JPEG quality */
2794 : /* -------------------------------------------------------------------- */
2795 326 : nOvrJpegQuality = m_nJpegQuality;
2796 326 : if (nCompression == COMPRESSION_JPEG)
2797 : {
2798 : const char *pszJPEGQuality =
2799 27 : GetOptionValue("JPEG_QUALITY", "JPEG_QUALITY_OVERVIEW");
2800 27 : if (pszJPEGQuality != nullptr)
2801 : {
2802 9 : nOvrJpegQuality = atoi(pszJPEGQuality);
2803 : }
2804 : }
2805 :
2806 : /* -------------------------------------------------------------------- */
2807 : /* Set nodata. */
2808 : /* -------------------------------------------------------------------- */
2809 326 : if (m_bNoDataSet)
2810 : {
2811 17 : osNoData = GTiffFormatGDALNoDataTagValue(m_dfNoDataValue);
2812 : }
2813 :
2814 : /* -------------------------------------------------------------------- */
2815 : /* Fetch extra sample tag */
2816 : /* -------------------------------------------------------------------- */
2817 326 : panExtraSampleValues = nullptr;
2818 326 : nExtraSamples = 0;
2819 326 : if (TIFFGetField(m_hTIFF, TIFFTAG_EXTRASAMPLES, &nExtraSamples,
2820 326 : &panExtraSampleValues))
2821 : {
2822 : uint16_t *panExtraSampleValuesNew = static_cast<uint16_t *>(
2823 40 : CPLMalloc(nExtraSamples * sizeof(uint16_t)));
2824 40 : memcpy(panExtraSampleValuesNew, panExtraSampleValues,
2825 40 : nExtraSamples * sizeof(uint16_t));
2826 40 : panExtraSampleValues = panExtraSampleValuesNew;
2827 : }
2828 : else
2829 : {
2830 286 : panExtraSampleValues = nullptr;
2831 286 : nExtraSamples = 0;
2832 : }
2833 :
2834 326 : return true;
2835 : }
2836 :
2837 : /************************************************************************/
2838 : /* CreateOverviewsFromSrcOverviews() */
2839 : /************************************************************************/
2840 :
2841 : // If poOvrDS is not null, it is used and poSrcDS is ignored.
2842 :
2843 69 : CPLErr GTiffDataset::CreateOverviewsFromSrcOverviews(GDALDataset *poSrcDS,
2844 : GDALDataset *poOvrDS,
2845 : int nOverviews)
2846 : {
2847 69 : CPLAssert(poSrcDS->GetRasterCount() != 0);
2848 69 : CPLAssert(m_apoOverviewDS.empty());
2849 :
2850 69 : ScanDirectories();
2851 :
2852 69 : FlushDirectory();
2853 :
2854 69 : int nOvBitsPerSample = m_nBitsPerSample;
2855 :
2856 : /* -------------------------------------------------------------------- */
2857 : /* Do we need some metadata for the overviews? */
2858 : /* -------------------------------------------------------------------- */
2859 138 : CPLString osMetadata;
2860 :
2861 69 : GTIFFBuildOverviewMetadata("NONE", this, false, osMetadata);
2862 :
2863 : int nCompression;
2864 : uint16_t nPlanarConfig;
2865 : uint16_t nPredictor;
2866 : uint16_t nPhotometric;
2867 : int nOvrJpegQuality;
2868 138 : std::string osNoData;
2869 69 : uint16_t *panExtraSampleValues = nullptr;
2870 69 : uint16_t nExtraSamples = 0;
2871 69 : if (!GetOverviewParameters(nCompression, nPlanarConfig, nPredictor,
2872 : nPhotometric, nOvrJpegQuality, osNoData,
2873 : panExtraSampleValues, nExtraSamples,
2874 : /*papszOptions=*/nullptr))
2875 : {
2876 0 : return CE_Failure;
2877 : }
2878 :
2879 : /* -------------------------------------------------------------------- */
2880 : /* Do we have a palette? If so, create a TIFF compatible version. */
2881 : /* -------------------------------------------------------------------- */
2882 138 : std::vector<unsigned short> anTRed;
2883 138 : std::vector<unsigned short> anTGreen;
2884 69 : std::vector<unsigned short> anTBlue;
2885 69 : unsigned short *panRed = nullptr;
2886 69 : unsigned short *panGreen = nullptr;
2887 69 : unsigned short *panBlue = nullptr;
2888 :
2889 69 : if (nPhotometric == PHOTOMETRIC_PALETTE && m_poColorTable != nullptr)
2890 : {
2891 0 : if (m_nColorTableMultiplier == 0)
2892 0 : m_nColorTableMultiplier = DEFAULT_COLOR_TABLE_MULTIPLIER_257;
2893 :
2894 0 : CreateTIFFColorTable(m_poColorTable.get(), nOvBitsPerSample,
2895 : m_nColorTableMultiplier, anTRed, anTGreen, anTBlue,
2896 : panRed, panGreen, panBlue);
2897 : }
2898 :
2899 69 : int nOvrBlockXSize = 0;
2900 69 : int nOvrBlockYSize = 0;
2901 69 : GTIFFGetOverviewBlockSize(GDALRasterBand::ToHandle(GetRasterBand(1)),
2902 : &nOvrBlockXSize, &nOvrBlockYSize, nullptr,
2903 : nullptr);
2904 :
2905 69 : CPLErr eErr = CE_None;
2906 :
2907 195 : for (int i = 0; i < nOverviews && eErr == CE_None; ++i)
2908 : {
2909 : GDALRasterBand *poOvrBand =
2910 163 : poOvrDS ? ((i == 0) ? poOvrDS->GetRasterBand(1)
2911 37 : : poOvrDS->GetRasterBand(1)->GetOverview(i - 1))
2912 55 : : poSrcDS->GetRasterBand(1)->GetOverview(i);
2913 :
2914 126 : int nOXSize = poOvrBand->GetXSize();
2915 126 : int nOYSize = poOvrBand->GetYSize();
2916 :
2917 252 : toff_t nOverviewOffset = GTIFFWriteDirectory(
2918 : m_hTIFF, FILETYPE_REDUCEDIMAGE, nOXSize, nOYSize, nOvBitsPerSample,
2919 126 : nPlanarConfig, m_nSamplesPerPixel, nOvrBlockXSize, nOvrBlockYSize,
2920 126 : TRUE, nCompression, nPhotometric, m_nSampleFormat, nPredictor,
2921 : panRed, panGreen, panBlue, nExtraSamples, panExtraSampleValues,
2922 : osMetadata,
2923 126 : nOvrJpegQuality >= 0 ? CPLSPrintf("%d", nOvrJpegQuality) : nullptr,
2924 126 : CPLSPrintf("%d", m_nJpegTablesMode),
2925 2 : osNoData.empty() ? nullptr : osNoData.c_str(),
2926 126 : m_anLercAddCompressionAndVersion, m_bWriteCOGLayout);
2927 :
2928 126 : if (nOverviewOffset == 0)
2929 0 : eErr = CE_Failure;
2930 : else
2931 126 : eErr = RegisterNewOverviewDataset(nOverviewOffset, nOvrJpegQuality,
2932 : nullptr);
2933 : }
2934 :
2935 : // For directory reloading, so that the chaining to the next directory is
2936 : // reloaded, as well as compression parameters.
2937 69 : ReloadDirectory();
2938 :
2939 69 : CPLFree(panExtraSampleValues);
2940 69 : panExtraSampleValues = nullptr;
2941 :
2942 69 : return eErr;
2943 : }
2944 :
2945 : /************************************************************************/
2946 : /* CreateInternalMaskOverviews() */
2947 : /************************************************************************/
2948 :
2949 272 : CPLErr GTiffDataset::CreateInternalMaskOverviews(int nOvrBlockXSize,
2950 : int nOvrBlockYSize)
2951 : {
2952 272 : ScanDirectories();
2953 :
2954 : /* -------------------------------------------------------------------- */
2955 : /* Create overviews for the mask. */
2956 : /* -------------------------------------------------------------------- */
2957 272 : CPLErr eErr = CE_None;
2958 :
2959 272 : if (m_poMaskDS != nullptr && m_poMaskDS->GetRasterCount() == 1)
2960 : {
2961 : int nMaskOvrCompression;
2962 43 : if (strstr(GDALGetMetadataItem(GDALGetDriverByName("GTiff"),
2963 : GDAL_DMD_CREATIONOPTIONLIST, nullptr),
2964 43 : "<Value>DEFLATE</Value>") != nullptr)
2965 43 : nMaskOvrCompression = COMPRESSION_ADOBE_DEFLATE;
2966 : else
2967 0 : nMaskOvrCompression = COMPRESSION_PACKBITS;
2968 :
2969 115 : for (auto &poOvrDS : m_apoOverviewDS)
2970 : {
2971 72 : if (poOvrDS->m_poMaskDS == nullptr)
2972 : {
2973 60 : const toff_t nOverviewOffset = GTIFFWriteDirectory(
2974 : m_hTIFF, FILETYPE_REDUCEDIMAGE | FILETYPE_MASK,
2975 60 : poOvrDS->nRasterXSize, poOvrDS->nRasterYSize, 1,
2976 : PLANARCONFIG_CONTIG, 1, nOvrBlockXSize, nOvrBlockYSize,
2977 : TRUE, nMaskOvrCompression, PHOTOMETRIC_MASK,
2978 : SAMPLEFORMAT_UINT, PREDICTOR_NONE, nullptr, nullptr,
2979 : nullptr, 0, nullptr, "", nullptr, nullptr, nullptr, nullptr,
2980 60 : m_bWriteCOGLayout);
2981 :
2982 60 : if (nOverviewOffset == 0)
2983 : {
2984 0 : eErr = CE_Failure;
2985 0 : continue;
2986 : }
2987 :
2988 120 : auto poMaskODS = std::make_shared<GTiffDataset>();
2989 60 : poMaskODS->eAccess = GA_Update;
2990 60 : poMaskODS->ShareLockWithParentDataset(this);
2991 60 : poMaskODS->m_osFilename = m_osFilename;
2992 60 : if (poMaskODS->OpenOffset(VSI_TIFFOpenChild(m_hTIFF),
2993 : nOverviewOffset,
2994 60 : GA_Update) != CE_None)
2995 : {
2996 0 : eErr = CE_Failure;
2997 : }
2998 : else
2999 : {
3000 120 : poMaskODS->m_bPromoteTo8Bits =
3001 60 : CPLTestBool(CPLGetConfigOption(
3002 : "GDAL_TIFF_INTERNAL_MASK_TO_8BIT", "YES"));
3003 60 : poMaskODS->m_poBaseDS = this;
3004 60 : poMaskODS->m_poImageryDS = poOvrDS.get();
3005 60 : poOvrDS->m_poMaskDS = poMaskODS;
3006 60 : m_poMaskDS->m_apoOverviewDS.push_back(std::move(poMaskODS));
3007 : }
3008 : }
3009 : }
3010 : }
3011 :
3012 272 : ReloadDirectory();
3013 :
3014 272 : return eErr;
3015 : }
3016 :
3017 : /************************************************************************/
3018 : /* AddOverviews() */
3019 : /************************************************************************/
3020 :
3021 : CPLErr
3022 13 : GTiffDataset::AddOverviews(const std::vector<GDALDataset *> &apoSrcOvrDSIn,
3023 : GDALProgressFunc pfnProgress, void *pProgressData,
3024 : CSLConstList papszOptions)
3025 : {
3026 : /* -------------------------------------------------------------------- */
3027 : /* If we don't have read access, then create the overviews */
3028 : /* externally. */
3029 : /* -------------------------------------------------------------------- */
3030 13 : if (GetAccess() != GA_Update)
3031 : {
3032 4 : CPLDebug("GTiff", "File open for read-only accessing, "
3033 : "creating overviews externally.");
3034 :
3035 4 : CPLErr eErr = GDALDataset::AddOverviews(apoSrcOvrDSIn, pfnProgress,
3036 : pProgressData, papszOptions);
3037 4 : if (eErr == CE_None && m_poMaskDS)
3038 : {
3039 0 : ReportError(
3040 : CE_Warning, CPLE_NotSupported,
3041 : "Building external overviews whereas there is an internal "
3042 : "mask is not fully supported. "
3043 : "The overviews of the non-mask bands will be created, "
3044 : "but not the overviews of the mask band.");
3045 : }
3046 4 : return eErr;
3047 : }
3048 :
3049 18 : std::vector<GDALDataset *> apoSrcOvrDS = apoSrcOvrDSIn;
3050 : // Sort overviews by descending size
3051 9 : std::sort(apoSrcOvrDS.begin(), apoSrcOvrDS.end(),
3052 0 : [](const GDALDataset *poDS1, const GDALDataset *poDS2)
3053 0 : { return poDS1->GetRasterXSize() > poDS2->GetRasterXSize(); });
3054 :
3055 9 : if (!GDALDefaultOverviews::CheckSrcOverviewsConsistencyWithBase(
3056 : this, apoSrcOvrDS))
3057 5 : return CE_Failure;
3058 :
3059 4 : ScanDirectories();
3060 :
3061 : // Make implicit JPEG overviews invisible, but do not destroy
3062 : // them in case they are already used (not sure that the client
3063 : // has the right to do that). Behavior maybe undefined in GDAL API.
3064 4 : std::swap(m_apoJPEGOverviewDSOld, m_apoJPEGOverviewDS);
3065 4 : m_apoJPEGOverviewDS.clear();
3066 :
3067 4 : FlushDirectory();
3068 :
3069 : /* -------------------------------------------------------------------- */
3070 : /* If we are averaging bit data to grayscale we need to create */
3071 : /* 8bit overviews. */
3072 : /* -------------------------------------------------------------------- */
3073 4 : int nOvBitsPerSample = m_nBitsPerSample;
3074 :
3075 : /* -------------------------------------------------------------------- */
3076 : /* Do we need some metadata for the overviews? */
3077 : /* -------------------------------------------------------------------- */
3078 8 : CPLString osMetadata;
3079 :
3080 4 : const bool bIsForMaskBand = nBands == 1 && GetRasterBand(1)->IsMaskBand();
3081 4 : GTIFFBuildOverviewMetadata(/* resampling = */ "", this, bIsForMaskBand,
3082 : osMetadata);
3083 :
3084 : int nCompression;
3085 : uint16_t nPlanarConfig;
3086 : uint16_t nPredictor;
3087 : uint16_t nPhotometric;
3088 : int nOvrJpegQuality;
3089 8 : std::string osNoData;
3090 4 : uint16_t *panExtraSampleValues = nullptr;
3091 4 : uint16_t nExtraSamples = 0;
3092 4 : if (!GetOverviewParameters(nCompression, nPlanarConfig, nPredictor,
3093 : nPhotometric, nOvrJpegQuality, osNoData,
3094 : panExtraSampleValues, nExtraSamples,
3095 : papszOptions))
3096 : {
3097 0 : return CE_Failure;
3098 : }
3099 :
3100 : /* -------------------------------------------------------------------- */
3101 : /* Do we have a palette? If so, create a TIFF compatible version. */
3102 : /* -------------------------------------------------------------------- */
3103 8 : std::vector<unsigned short> anTRed;
3104 8 : std::vector<unsigned short> anTGreen;
3105 4 : std::vector<unsigned short> anTBlue;
3106 4 : unsigned short *panRed = nullptr;
3107 4 : unsigned short *panGreen = nullptr;
3108 4 : unsigned short *panBlue = nullptr;
3109 :
3110 4 : if (nPhotometric == PHOTOMETRIC_PALETTE && m_poColorTable != nullptr)
3111 : {
3112 0 : if (m_nColorTableMultiplier == 0)
3113 0 : m_nColorTableMultiplier = DEFAULT_COLOR_TABLE_MULTIPLIER_257;
3114 :
3115 0 : CreateTIFFColorTable(m_poColorTable.get(), nOvBitsPerSample,
3116 : m_nColorTableMultiplier, anTRed, anTGreen, anTBlue,
3117 : panRed, panGreen, panBlue);
3118 : }
3119 :
3120 : /* -------------------------------------------------------------------- */
3121 : /* Establish which of the overview levels we already have, and */
3122 : /* which are new. We assume that band 1 of the file is */
3123 : /* representative. */
3124 : /* -------------------------------------------------------------------- */
3125 4 : int nOvrBlockXSize = 0;
3126 4 : int nOvrBlockYSize = 0;
3127 4 : GTIFFGetOverviewBlockSize(GDALRasterBand::ToHandle(GetRasterBand(1)),
3128 : &nOvrBlockXSize, &nOvrBlockYSize, papszOptions,
3129 : "BLOCKSIZE");
3130 :
3131 4 : CPLErr eErr = CE_None;
3132 8 : for (const auto *poSrcOvrDS : apoSrcOvrDS)
3133 : {
3134 4 : bool bFound = false;
3135 4 : for (auto &poOvrDS : m_apoOverviewDS)
3136 : {
3137 4 : if (poOvrDS->GetRasterXSize() == poSrcOvrDS->GetRasterXSize() &&
3138 2 : poOvrDS->GetRasterYSize() == poSrcOvrDS->GetRasterYSize())
3139 : {
3140 2 : bFound = true;
3141 2 : break;
3142 : }
3143 : }
3144 4 : if (!bFound && eErr == CE_None)
3145 : {
3146 2 : if (m_bLayoutIFDSBeforeData && !m_bKnownIncompatibleEdition &&
3147 0 : !m_bWriteKnownIncompatibleEdition)
3148 : {
3149 0 : ReportError(CE_Warning, CPLE_AppDefined,
3150 : "Adding new overviews invalidates the "
3151 : "LAYOUT=IFDS_BEFORE_DATA property");
3152 0 : m_bKnownIncompatibleEdition = true;
3153 0 : m_bWriteKnownIncompatibleEdition = true;
3154 : }
3155 :
3156 6 : const toff_t nOverviewOffset = GTIFFWriteDirectory(
3157 : m_hTIFF, FILETYPE_REDUCEDIMAGE, poSrcOvrDS->GetRasterXSize(),
3158 : poSrcOvrDS->GetRasterYSize(), nOvBitsPerSample, nPlanarConfig,
3159 2 : m_nSamplesPerPixel, nOvrBlockXSize, nOvrBlockYSize, TRUE,
3160 2 : nCompression, nPhotometric, m_nSampleFormat, nPredictor, panRed,
3161 : panGreen, panBlue, nExtraSamples, panExtraSampleValues,
3162 : osMetadata,
3163 2 : nOvrJpegQuality >= 0 ? CPLSPrintf("%d", nOvrJpegQuality)
3164 : : nullptr,
3165 2 : CPLSPrintf("%d", m_nJpegTablesMode),
3166 0 : osNoData.empty() ? nullptr : osNoData.c_str(),
3167 2 : m_anLercAddCompressionAndVersion, false);
3168 :
3169 2 : if (nOverviewOffset == 0)
3170 0 : eErr = CE_Failure;
3171 : else
3172 2 : eErr = RegisterNewOverviewDataset(
3173 : nOverviewOffset, nOvrJpegQuality, papszOptions);
3174 : }
3175 : }
3176 :
3177 4 : CPLFree(panExtraSampleValues);
3178 4 : panExtraSampleValues = nullptr;
3179 :
3180 4 : ReloadDirectory();
3181 :
3182 4 : if (!pfnProgress)
3183 2 : pfnProgress = GDALDummyProgress;
3184 :
3185 : // almost 0, but not 0 to please Coverity Scan
3186 4 : double dfTotalPixels = std::numeric_limits<double>::min();
3187 8 : for (const auto *poSrcOvrDS : apoSrcOvrDS)
3188 : {
3189 4 : dfTotalPixels += static_cast<double>(poSrcOvrDS->GetRasterXSize()) *
3190 4 : poSrcOvrDS->GetRasterYSize();
3191 : }
3192 :
3193 : // Copy source datasets into target overview datasets
3194 4 : double dfCurPixels = 0;
3195 8 : for (auto *poSrcOvrDS : apoSrcOvrDS)
3196 : {
3197 4 : GDALDataset *poDstOvrDS = nullptr;
3198 4 : for (auto &poOvrDS : m_apoOverviewDS)
3199 : {
3200 8 : if (poOvrDS->GetRasterXSize() == poSrcOvrDS->GetRasterXSize() &&
3201 4 : poOvrDS->GetRasterYSize() == poSrcOvrDS->GetRasterYSize())
3202 : {
3203 4 : poDstOvrDS = poOvrDS.get();
3204 4 : break;
3205 : }
3206 : }
3207 4 : if (eErr == CE_None && poDstOvrDS)
3208 : {
3209 : const double dfThisPixels =
3210 4 : static_cast<double>(poSrcOvrDS->GetRasterXSize()) *
3211 4 : poSrcOvrDS->GetRasterYSize();
3212 8 : void *pScaledProgressData = GDALCreateScaledProgress(
3213 : dfCurPixels / dfTotalPixels,
3214 4 : (dfCurPixels + dfThisPixels) / dfTotalPixels, pfnProgress,
3215 : pProgressData);
3216 4 : dfCurPixels += dfThisPixels;
3217 4 : eErr = GDALDatasetCopyWholeRaster(GDALDataset::ToHandle(poSrcOvrDS),
3218 : GDALDataset::ToHandle(poDstOvrDS),
3219 : nullptr, GDALScaledProgress,
3220 : pScaledProgressData);
3221 4 : GDALDestroyScaledProgress(pScaledProgressData);
3222 : }
3223 : }
3224 :
3225 4 : return eErr;
3226 : }
3227 :
3228 : /************************************************************************/
3229 : /* IBuildOverviews() */
3230 : /************************************************************************/
3231 :
3232 407 : CPLErr GTiffDataset::IBuildOverviews(const char *pszResampling, int nOverviews,
3233 : const int *panOverviewList, int nBandsIn,
3234 : const int *panBandList,
3235 : GDALProgressFunc pfnProgress,
3236 : void *pProgressData,
3237 : CSLConstList papszOptions)
3238 :
3239 : {
3240 407 : ScanDirectories();
3241 :
3242 : // Make implicit JPEG overviews invisible, but do not destroy
3243 : // them in case they are already used (not sure that the client
3244 : // has the right to do that. Behavior maybe undefined in GDAL API.
3245 407 : std::swap(m_apoJPEGOverviewDSOld, m_apoJPEGOverviewDS);
3246 407 : m_apoJPEGOverviewDS.clear();
3247 :
3248 : /* -------------------------------------------------------------------- */
3249 : /* If RRD or external OVR overviews requested, then invoke */
3250 : /* generic handling. */
3251 : /* -------------------------------------------------------------------- */
3252 407 : bool bUseGenericHandling = false;
3253 407 : bool bUseRRD = false;
3254 814 : CPLStringList aosOptions(papszOptions);
3255 :
3256 407 : const char *pszLocation = CSLFetchNameValue(papszOptions, "LOCATION");
3257 407 : if (pszLocation && EQUAL(pszLocation, "EXTERNAL"))
3258 : {
3259 1 : bUseGenericHandling = true;
3260 : }
3261 406 : else if (pszLocation && EQUAL(pszLocation, "INTERNAL"))
3262 : {
3263 0 : if (GetAccess() != GA_Update)
3264 : {
3265 0 : CPLError(CE_Failure, CPLE_AppDefined,
3266 : "Cannot create internal overviews on file opened in "
3267 : "read-only mode");
3268 0 : return CE_Failure;
3269 : }
3270 : }
3271 406 : else if (pszLocation && EQUAL(pszLocation, "RRD"))
3272 : {
3273 3 : bUseGenericHandling = true;
3274 3 : bUseRRD = true;
3275 3 : aosOptions.SetNameValue("USE_RRD", "YES");
3276 : }
3277 : // Legacy
3278 403 : else if ((bUseRRD = CPLTestBool(
3279 : CSLFetchNameValueDef(papszOptions, "USE_RRD",
3280 806 : CPLGetConfigOption("USE_RRD", "NO")))) ||
3281 403 : CPLTestBool(CSLFetchNameValueDef(
3282 : papszOptions, "TIFF_USE_OVR",
3283 : CPLGetConfigOption("TIFF_USE_OVR", "NO"))))
3284 : {
3285 0 : bUseGenericHandling = true;
3286 : }
3287 :
3288 : /* -------------------------------------------------------------------- */
3289 : /* If we don't have read access, then create the overviews */
3290 : /* externally. */
3291 : /* -------------------------------------------------------------------- */
3292 407 : if (GetAccess() != GA_Update)
3293 : {
3294 143 : CPLDebug("GTiff", "File open for read-only accessing, "
3295 : "creating overviews externally.");
3296 :
3297 143 : bUseGenericHandling = true;
3298 : }
3299 :
3300 407 : if (bUseGenericHandling)
3301 : {
3302 146 : if (!m_apoOverviewDS.empty())
3303 : {
3304 0 : ReportError(CE_Failure, CPLE_NotSupported,
3305 : "Cannot add external overviews when there are already "
3306 : "internal overviews");
3307 0 : return CE_Failure;
3308 : }
3309 :
3310 146 : if (!m_bWriteEmptyTiles && !bUseRRD)
3311 : {
3312 1 : aosOptions.SetNameValue("SPARSE_OK", "YES");
3313 : }
3314 :
3315 146 : CPLErr eErr = GDALDataset::IBuildOverviews(
3316 : pszResampling, nOverviews, panOverviewList, nBandsIn, panBandList,
3317 146 : pfnProgress, pProgressData, aosOptions);
3318 146 : if (eErr == CE_None && m_poMaskDS)
3319 : {
3320 1 : ReportError(
3321 : CE_Warning, CPLE_NotSupported,
3322 : "Building external overviews whereas there is an internal "
3323 : "mask is not fully supported. "
3324 : "The overviews of the non-mask bands will be created, "
3325 : "but not the overviews of the mask band.");
3326 : }
3327 146 : return eErr;
3328 : }
3329 :
3330 : /* -------------------------------------------------------------------- */
3331 : /* Our TIFF overview support currently only works safely if all */
3332 : /* bands are handled at the same time. */
3333 : /* -------------------------------------------------------------------- */
3334 261 : if (nBandsIn != GetRasterCount())
3335 : {
3336 0 : ReportError(CE_Failure, CPLE_NotSupported,
3337 : "Generation of overviews in TIFF currently only "
3338 : "supported when operating on all bands. "
3339 : "Operation failed.");
3340 0 : return CE_Failure;
3341 : }
3342 :
3343 : /* -------------------------------------------------------------------- */
3344 : /* If zero overviews were requested, we need to clear all */
3345 : /* existing overviews. */
3346 : /* -------------------------------------------------------------------- */
3347 261 : if (nOverviews == 0)
3348 : {
3349 8 : if (m_apoOverviewDS.empty())
3350 3 : return GDALDataset::IBuildOverviews(
3351 : pszResampling, nOverviews, panOverviewList, nBandsIn,
3352 3 : panBandList, pfnProgress, pProgressData, papszOptions);
3353 :
3354 5 : return CleanOverviews();
3355 : }
3356 :
3357 253 : CPLErr eErr = CE_None;
3358 :
3359 : /* -------------------------------------------------------------------- */
3360 : /* Initialize progress counter. */
3361 : /* -------------------------------------------------------------------- */
3362 253 : if (!pfnProgress(0.0, nullptr, pProgressData))
3363 : {
3364 0 : ReportError(CE_Failure, CPLE_UserInterrupt, "User terminated");
3365 0 : return CE_Failure;
3366 : }
3367 :
3368 253 : FlushDirectory();
3369 :
3370 : /* -------------------------------------------------------------------- */
3371 : /* If we are averaging bit data to grayscale we need to create */
3372 : /* 8bit overviews. */
3373 : /* -------------------------------------------------------------------- */
3374 253 : int nOvBitsPerSample = m_nBitsPerSample;
3375 :
3376 253 : if (STARTS_WITH_CI(pszResampling, "AVERAGE_BIT2"))
3377 2 : nOvBitsPerSample = 8;
3378 :
3379 : /* -------------------------------------------------------------------- */
3380 : /* Do we need some metadata for the overviews? */
3381 : /* -------------------------------------------------------------------- */
3382 506 : CPLString osMetadata;
3383 :
3384 253 : const bool bIsForMaskBand = nBands == 1 && GetRasterBand(1)->IsMaskBand();
3385 253 : GTIFFBuildOverviewMetadata(pszResampling, this, bIsForMaskBand, osMetadata);
3386 :
3387 : int nCompression;
3388 : uint16_t nPlanarConfig;
3389 : uint16_t nPredictor;
3390 : uint16_t nPhotometric;
3391 : int nOvrJpegQuality;
3392 506 : std::string osNoData;
3393 253 : uint16_t *panExtraSampleValues = nullptr;
3394 253 : uint16_t nExtraSamples = 0;
3395 253 : if (!GetOverviewParameters(nCompression, nPlanarConfig, nPredictor,
3396 : nPhotometric, nOvrJpegQuality, osNoData,
3397 : panExtraSampleValues, nExtraSamples,
3398 : papszOptions))
3399 : {
3400 0 : return CE_Failure;
3401 : }
3402 :
3403 : /* -------------------------------------------------------------------- */
3404 : /* Do we have a palette? If so, create a TIFF compatible version. */
3405 : /* -------------------------------------------------------------------- */
3406 506 : std::vector<unsigned short> anTRed;
3407 506 : std::vector<unsigned short> anTGreen;
3408 506 : std::vector<unsigned short> anTBlue;
3409 253 : unsigned short *panRed = nullptr;
3410 253 : unsigned short *panGreen = nullptr;
3411 253 : unsigned short *panBlue = nullptr;
3412 :
3413 253 : if (nPhotometric == PHOTOMETRIC_PALETTE && m_poColorTable != nullptr)
3414 : {
3415 12 : if (m_nColorTableMultiplier == 0)
3416 0 : m_nColorTableMultiplier = DEFAULT_COLOR_TABLE_MULTIPLIER_257;
3417 :
3418 12 : CreateTIFFColorTable(m_poColorTable.get(), nOvBitsPerSample,
3419 : m_nColorTableMultiplier, anTRed, anTGreen, anTBlue,
3420 : panRed, panGreen, panBlue);
3421 : }
3422 :
3423 : /* -------------------------------------------------------------------- */
3424 : /* Establish which of the overview levels we already have, and */
3425 : /* which are new. We assume that band 1 of the file is */
3426 : /* representative. */
3427 : /* -------------------------------------------------------------------- */
3428 253 : int nOvrBlockXSize = 0;
3429 253 : int nOvrBlockYSize = 0;
3430 253 : GTIFFGetOverviewBlockSize(GDALRasterBand::ToHandle(GetRasterBand(1)),
3431 : &nOvrBlockXSize, &nOvrBlockYSize, papszOptions,
3432 : "BLOCKSIZE");
3433 506 : std::vector<bool> abRequireNewOverview(nOverviews, true);
3434 690 : for (int i = 0; i < nOverviews && eErr == CE_None; ++i)
3435 : {
3436 770 : for (auto &poODS : m_apoOverviewDS)
3437 : {
3438 : const int nOvFactor =
3439 778 : GDALComputeOvFactor(poODS->GetRasterXSize(), GetRasterXSize(),
3440 389 : poODS->GetRasterYSize(), GetRasterYSize());
3441 :
3442 : // If we already have a 1x1 overview and this new one would result
3443 : // in it too, then don't create it.
3444 449 : if (poODS->GetRasterXSize() == 1 && poODS->GetRasterYSize() == 1 &&
3445 449 : DIV_ROUND_UP(GetRasterXSize(), panOverviewList[i]) == 1 &&
3446 21 : DIV_ROUND_UP(GetRasterYSize(), panOverviewList[i]) == 1)
3447 : {
3448 21 : abRequireNewOverview[i] = false;
3449 21 : break;
3450 : }
3451 :
3452 701 : if (nOvFactor == panOverviewList[i] ||
3453 333 : nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
3454 : GetRasterXSize(),
3455 : GetRasterYSize()))
3456 : {
3457 35 : abRequireNewOverview[i] = false;
3458 35 : break;
3459 : }
3460 : }
3461 :
3462 437 : if (abRequireNewOverview[i])
3463 : {
3464 381 : if (m_bLayoutIFDSBeforeData && !m_bKnownIncompatibleEdition &&
3465 2 : !m_bWriteKnownIncompatibleEdition)
3466 : {
3467 2 : ReportError(CE_Warning, CPLE_AppDefined,
3468 : "Adding new overviews invalidates the "
3469 : "LAYOUT=IFDS_BEFORE_DATA property");
3470 2 : m_bKnownIncompatibleEdition = true;
3471 2 : m_bWriteKnownIncompatibleEdition = true;
3472 : }
3473 :
3474 : const int nOXSize =
3475 381 : DIV_ROUND_UP(GetRasterXSize(), panOverviewList[i]);
3476 : const int nOYSize =
3477 381 : DIV_ROUND_UP(GetRasterYSize(), panOverviewList[i]);
3478 :
3479 762 : const toff_t nOverviewOffset = GTIFFWriteDirectory(
3480 : m_hTIFF, FILETYPE_REDUCEDIMAGE, nOXSize, nOYSize,
3481 381 : nOvBitsPerSample, nPlanarConfig, m_nSamplesPerPixel,
3482 : nOvrBlockXSize, nOvrBlockYSize, TRUE, nCompression,
3483 381 : nPhotometric, m_nSampleFormat, nPredictor, panRed, panGreen,
3484 : panBlue, nExtraSamples, panExtraSampleValues, osMetadata,
3485 381 : nOvrJpegQuality >= 0 ? CPLSPrintf("%d", nOvrJpegQuality)
3486 : : nullptr,
3487 381 : CPLSPrintf("%d", m_nJpegTablesMode),
3488 25 : osNoData.empty() ? nullptr : osNoData.c_str(),
3489 381 : m_anLercAddCompressionAndVersion, false);
3490 :
3491 381 : if (nOverviewOffset == 0)
3492 0 : eErr = CE_Failure;
3493 : else
3494 381 : eErr = RegisterNewOverviewDataset(
3495 : nOverviewOffset, nOvrJpegQuality, papszOptions);
3496 : }
3497 : }
3498 :
3499 253 : CPLFree(panExtraSampleValues);
3500 253 : panExtraSampleValues = nullptr;
3501 :
3502 253 : ReloadDirectory();
3503 :
3504 : /* -------------------------------------------------------------------- */
3505 : /* Create overviews for the mask. */
3506 : /* -------------------------------------------------------------------- */
3507 253 : if (eErr != CE_None)
3508 0 : return eErr;
3509 :
3510 253 : eErr = CreateInternalMaskOverviews(nOvrBlockXSize, nOvrBlockYSize);
3511 :
3512 : /* -------------------------------------------------------------------- */
3513 : /* Refresh overviews for the mask */
3514 : /* -------------------------------------------------------------------- */
3515 : const bool bHasInternalMask =
3516 253 : m_poMaskDS != nullptr && m_poMaskDS->GetRasterCount() == 1;
3517 : const bool bHasExternalMask =
3518 253 : !bHasInternalMask && oOvManager.HaveMaskFile();
3519 253 : const bool bHasMask = bHasInternalMask || bHasExternalMask;
3520 :
3521 253 : if (bHasInternalMask)
3522 : {
3523 48 : std::vector<GDALRasterBandH> ahOverviewBands;
3524 64 : for (auto &poOvrDS : m_apoOverviewDS)
3525 : {
3526 40 : if (poOvrDS->m_poMaskDS != nullptr)
3527 : {
3528 40 : ahOverviewBands.push_back(GDALRasterBand::ToHandle(
3529 40 : poOvrDS->m_poMaskDS->GetRasterBand(1)));
3530 : }
3531 : }
3532 :
3533 48 : void *pScaledProgressData = GDALCreateScaledProgress(
3534 24 : 0, 1.0 / (nBands + 1), pfnProgress, pProgressData);
3535 24 : eErr = GDALRegenerateOverviewsEx(
3536 24 : m_poMaskDS->GetRasterBand(1),
3537 24 : static_cast<int>(ahOverviewBands.size()), ahOverviewBands.data(),
3538 : pszResampling, GDALScaledProgress, pScaledProgressData,
3539 : papszOptions);
3540 24 : GDALDestroyScaledProgress(pScaledProgressData);
3541 : }
3542 229 : else if (bHasExternalMask)
3543 : {
3544 4 : void *pScaledProgressData = GDALCreateScaledProgress(
3545 2 : 0, 1.0 / (nBands + 1), pfnProgress, pProgressData);
3546 2 : eErr = oOvManager.BuildOverviewsMask(
3547 : pszResampling, nOverviews, panOverviewList, GDALScaledProgress,
3548 : pScaledProgressData, papszOptions);
3549 2 : GDALDestroyScaledProgress(pScaledProgressData);
3550 : }
3551 :
3552 : // If we have an alpha band, we want it to be generated before downsampling
3553 : // other bands
3554 253 : bool bHasAlphaBand = false;
3555 66253 : for (int iBand = 0; iBand < nBands; iBand++)
3556 : {
3557 66000 : if (papoBands[iBand]->GetColorInterpretation() == GCI_AlphaBand)
3558 18 : bHasAlphaBand = true;
3559 : }
3560 :
3561 : /* -------------------------------------------------------------------- */
3562 : /* Refresh old overviews that were listed. */
3563 : /* -------------------------------------------------------------------- */
3564 253 : const auto poColorTable = GetRasterBand(panBandList[0])->GetColorTable();
3565 21 : if ((m_nPlanarConfig == PLANARCONFIG_CONTIG || bHasAlphaBand) &&
3566 234 : GDALDataTypeIsComplex(
3567 234 : GetRasterBand(panBandList[0])->GetRasterDataType()) == FALSE &&
3568 12 : (poColorTable == nullptr || STARTS_WITH_CI(pszResampling, "NEAR") ||
3569 507 : poColorTable->IsIdentity()) &&
3570 226 : (STARTS_WITH_CI(pszResampling, "NEAR") ||
3571 117 : EQUAL(pszResampling, "AVERAGE") || EQUAL(pszResampling, "RMS") ||
3572 47 : EQUAL(pszResampling, "GAUSS") || EQUAL(pszResampling, "CUBIC") ||
3573 29 : EQUAL(pszResampling, "CUBICSPLINE") ||
3574 28 : EQUAL(pszResampling, "LANCZOS") || EQUAL(pszResampling, "BILINEAR") ||
3575 24 : EQUAL(pszResampling, "MODE")))
3576 : {
3577 : // In the case of pixel interleaved compressed overviews, we want to
3578 : // generate the overviews for all the bands block by block, and not
3579 : // band after band, in order to write the block once and not loose
3580 : // space in the TIFF file. We also use that logic for uncompressed
3581 : // overviews, since GDALRegenerateOverviewsMultiBand() will be able to
3582 : // trigger cascading overview regeneration even in the presence
3583 : // of an alpha band.
3584 :
3585 205 : int nNewOverviews = 0;
3586 :
3587 : GDALRasterBand ***papapoOverviewBands = static_cast<GDALRasterBand ***>(
3588 205 : CPLCalloc(sizeof(void *), nBandsIn));
3589 : GDALRasterBand **papoBandList =
3590 205 : static_cast<GDALRasterBand **>(CPLCalloc(sizeof(void *), nBandsIn));
3591 66109 : for (int iBand = 0; iBand < nBandsIn; ++iBand)
3592 : {
3593 65904 : GDALRasterBand *poBand = GetRasterBand(panBandList[iBand]);
3594 :
3595 65904 : papoBandList[iBand] = poBand;
3596 131808 : papapoOverviewBands[iBand] = static_cast<GDALRasterBand **>(
3597 65904 : CPLCalloc(sizeof(void *), poBand->GetOverviewCount()));
3598 :
3599 65904 : int iCurOverview = 0;
3600 : std::vector<bool> abAlreadyUsedOverviewBand(
3601 65904 : poBand->GetOverviewCount(), false);
3602 :
3603 132094 : for (int i = 0; i < nOverviews; ++i)
3604 : {
3605 66650 : for (int j = 0; j < poBand->GetOverviewCount(); ++j)
3606 : {
3607 66635 : if (abAlreadyUsedOverviewBand[j])
3608 459 : continue;
3609 :
3610 : int nOvFactor;
3611 66176 : GDALRasterBand *poOverview = poBand->GetOverview(j);
3612 :
3613 66176 : nOvFactor = GDALComputeOvFactor(
3614 : poOverview->GetXSize(), poBand->GetXSize(),
3615 : poOverview->GetYSize(), poBand->GetYSize());
3616 :
3617 66176 : GDALCopyNoDataValue(poOverview, poBand);
3618 :
3619 66177 : if (nOvFactor == panOverviewList[i] ||
3620 1 : nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
3621 : poBand->GetXSize(),
3622 : poBand->GetYSize()))
3623 : {
3624 66175 : if (iBand == 0)
3625 : {
3626 : const auto osNewResampling =
3627 664 : GDALGetNormalizedOvrResampling(pszResampling);
3628 : const char *pszExistingResampling =
3629 332 : poOverview->GetMetadataItem("RESAMPLING");
3630 664 : if (pszExistingResampling &&
3631 332 : pszExistingResampling != osNewResampling)
3632 : {
3633 2 : poOverview->SetMetadataItem(
3634 2 : "RESAMPLING", osNewResampling.c_str());
3635 : }
3636 : }
3637 :
3638 66175 : abAlreadyUsedOverviewBand[j] = true;
3639 66175 : CPLAssert(iCurOverview < poBand->GetOverviewCount());
3640 66175 : papapoOverviewBands[iBand][iCurOverview] = poOverview;
3641 66175 : ++iCurOverview;
3642 66175 : break;
3643 : }
3644 : }
3645 : }
3646 :
3647 65904 : if (nNewOverviews == 0)
3648 : {
3649 205 : nNewOverviews = iCurOverview;
3650 : }
3651 65699 : else if (nNewOverviews != iCurOverview)
3652 : {
3653 0 : CPLAssert(false);
3654 : return CE_Failure;
3655 : }
3656 : }
3657 :
3658 : void *pScaledProgressData =
3659 205 : bHasMask ? GDALCreateScaledProgress(1.0 / (nBands + 1), 1.0,
3660 : pfnProgress, pProgressData)
3661 179 : : GDALCreateScaledProgress(0.0, 1.0, pfnProgress,
3662 205 : pProgressData);
3663 205 : GDALRegenerateOverviewsMultiBand(nBandsIn, papoBandList, nNewOverviews,
3664 : papapoOverviewBands, pszResampling,
3665 : GDALScaledProgress,
3666 : pScaledProgressData, papszOptions);
3667 205 : GDALDestroyScaledProgress(pScaledProgressData);
3668 :
3669 66109 : for (int iBand = 0; iBand < nBandsIn; ++iBand)
3670 : {
3671 65904 : CPLFree(papapoOverviewBands[iBand]);
3672 : }
3673 205 : CPLFree(papapoOverviewBands);
3674 205 : CPLFree(papoBandList);
3675 : }
3676 : else
3677 : {
3678 : GDALRasterBand **papoOverviewBands = static_cast<GDALRasterBand **>(
3679 48 : CPLCalloc(sizeof(void *), nOverviews));
3680 :
3681 48 : const int iBandOffset = bHasMask ? 1 : 0;
3682 :
3683 144 : for (int iBand = 0; iBand < nBandsIn && eErr == CE_None; ++iBand)
3684 : {
3685 96 : GDALRasterBand *poBand = GetRasterBand(panBandList[iBand]);
3686 96 : if (poBand == nullptr)
3687 : {
3688 0 : eErr = CE_Failure;
3689 0 : break;
3690 : }
3691 :
3692 : std::vector<bool> abAlreadyUsedOverviewBand(
3693 192 : poBand->GetOverviewCount(), false);
3694 :
3695 96 : int nNewOverviews = 0;
3696 288 : for (int i = 0; i < nOverviews; ++i)
3697 : {
3698 450 : for (int j = 0; j < poBand->GetOverviewCount(); ++j)
3699 : {
3700 432 : if (abAlreadyUsedOverviewBand[j])
3701 257 : continue;
3702 :
3703 175 : GDALRasterBand *poOverview = poBand->GetOverview(j);
3704 :
3705 175 : GDALCopyNoDataValue(poOverview, poBand);
3706 :
3707 175 : const int nOvFactor = GDALComputeOvFactor(
3708 : poOverview->GetXSize(), poBand->GetXSize(),
3709 : poOverview->GetYSize(), poBand->GetYSize());
3710 :
3711 176 : if (nOvFactor == panOverviewList[i] ||
3712 1 : nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
3713 : poBand->GetXSize(),
3714 : poBand->GetYSize()))
3715 : {
3716 174 : if (iBand == 0)
3717 : {
3718 : const auto osNewResampling =
3719 168 : GDALGetNormalizedOvrResampling(pszResampling);
3720 : const char *pszExistingResampling =
3721 84 : poOverview->GetMetadataItem("RESAMPLING");
3722 136 : if (pszExistingResampling &&
3723 52 : pszExistingResampling != osNewResampling)
3724 : {
3725 1 : poOverview->SetMetadataItem(
3726 1 : "RESAMPLING", osNewResampling.c_str());
3727 : }
3728 : }
3729 :
3730 174 : abAlreadyUsedOverviewBand[j] = true;
3731 174 : CPLAssert(nNewOverviews < poBand->GetOverviewCount());
3732 174 : papoOverviewBands[nNewOverviews++] = poOverview;
3733 174 : break;
3734 : }
3735 : }
3736 : }
3737 :
3738 192 : void *pScaledProgressData = GDALCreateScaledProgress(
3739 96 : (iBand + iBandOffset) /
3740 96 : static_cast<double>(nBandsIn + iBandOffset),
3741 96 : (iBand + iBandOffset + 1) /
3742 96 : static_cast<double>(nBandsIn + iBandOffset),
3743 : pfnProgress, pProgressData);
3744 :
3745 96 : eErr = GDALRegenerateOverviewsEx(
3746 : poBand, nNewOverviews,
3747 : reinterpret_cast<GDALRasterBandH *>(papoOverviewBands),
3748 : pszResampling, GDALScaledProgress, pScaledProgressData,
3749 : papszOptions);
3750 :
3751 96 : GDALDestroyScaledProgress(pScaledProgressData);
3752 : }
3753 :
3754 : /* --------------------------------------------------------------------
3755 : */
3756 : /* Cleanup */
3757 : /* --------------------------------------------------------------------
3758 : */
3759 48 : CPLFree(papoOverviewBands);
3760 : }
3761 :
3762 253 : pfnProgress(1.0, nullptr, pProgressData);
3763 :
3764 253 : return eErr;
3765 : }
3766 :
3767 : /************************************************************************/
3768 : /* GTiffWriteDummyGeokeyDirectory() */
3769 : /************************************************************************/
3770 :
3771 1495 : static void GTiffWriteDummyGeokeyDirectory(TIFF *hTIFF)
3772 : {
3773 : // If we have existing geokeys, try to wipe them
3774 : // by writing a dummy geokey directory. (#2546)
3775 1495 : uint16_t *panVI = nullptr;
3776 1495 : uint16_t nKeyCount = 0;
3777 :
3778 1495 : if (TIFFGetField(hTIFF, TIFFTAG_GEOKEYDIRECTORY, &nKeyCount, &panVI))
3779 : {
3780 24 : GUInt16 anGKVersionInfo[4] = {1, 1, 0, 0};
3781 24 : double adfDummyDoubleParams[1] = {0.0};
3782 24 : TIFFSetField(hTIFF, TIFFTAG_GEOKEYDIRECTORY, 4, anGKVersionInfo);
3783 24 : TIFFSetField(hTIFF, TIFFTAG_GEODOUBLEPARAMS, 1, adfDummyDoubleParams);
3784 24 : TIFFSetField(hTIFF, TIFFTAG_GEOASCIIPARAMS, "");
3785 : }
3786 1495 : }
3787 :
3788 : /************************************************************************/
3789 : /* IsSRSCompatibleOfGeoTIFF() */
3790 : /************************************************************************/
3791 :
3792 3141 : static bool IsSRSCompatibleOfGeoTIFF(const OGRSpatialReference *poSRS,
3793 : GTIFFKeysFlavorEnum eGeoTIFFKeysFlavor)
3794 : {
3795 3141 : char *pszWKT = nullptr;
3796 3141 : if ((poSRS->IsGeographic() || poSRS->IsProjected()) && !poSRS->IsCompound())
3797 : {
3798 3123 : const char *pszAuthName = poSRS->GetAuthorityName(nullptr);
3799 3123 : const char *pszAuthCode = poSRS->GetAuthorityCode(nullptr);
3800 3123 : if (pszAuthName && pszAuthCode && EQUAL(pszAuthName, "EPSG"))
3801 2554 : return true;
3802 : }
3803 : OGRErr eErr;
3804 : {
3805 1174 : CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
3806 1174 : if (poSRS->IsDerivedGeographic() ||
3807 587 : (poSRS->IsProjected() && !poSRS->IsCompound() &&
3808 70 : poSRS->GetAxesCount() == 3))
3809 : {
3810 0 : eErr = OGRERR_FAILURE;
3811 : }
3812 : else
3813 : {
3814 : // Geographic3D CRS can't be exported to WKT1, but are
3815 : // valid GeoTIFF 1.1
3816 587 : const char *const apszOptions[] = {
3817 587 : poSRS->IsGeographic() ? nullptr : "FORMAT=WKT1", nullptr};
3818 587 : eErr = poSRS->exportToWkt(&pszWKT, apszOptions);
3819 587 : if (eErr == OGRERR_FAILURE && poSRS->IsProjected() &&
3820 : eGeoTIFFKeysFlavor == GEOTIFF_KEYS_ESRI_PE)
3821 : {
3822 0 : CPLFree(pszWKT);
3823 0 : const char *const apszOptionsESRIWKT[] = {"FORMAT=WKT1_ESRI",
3824 : nullptr};
3825 0 : eErr = poSRS->exportToWkt(&pszWKT, apszOptionsESRIWKT);
3826 : }
3827 : }
3828 : }
3829 587 : const bool bCompatibleOfGeoTIFF =
3830 1173 : (eErr == OGRERR_NONE && pszWKT != nullptr &&
3831 586 : strstr(pszWKT, "custom_proj4") == nullptr);
3832 587 : CPLFree(pszWKT);
3833 587 : return bCompatibleOfGeoTIFF;
3834 : }
3835 :
3836 : /************************************************************************/
3837 : /* WriteGeoTIFFInfo() */
3838 : /************************************************************************/
3839 :
3840 5832 : void GTiffDataset::WriteGeoTIFFInfo()
3841 :
3842 : {
3843 5832 : bool bPixelIsPoint = false;
3844 5832 : bool bPointGeoIgnore = false;
3845 :
3846 : const char *pszAreaOrPoint =
3847 5832 : GTiffDataset::GetMetadataItem(GDALMD_AREA_OR_POINT);
3848 5832 : if (pszAreaOrPoint && EQUAL(pszAreaOrPoint, GDALMD_AOP_POINT))
3849 : {
3850 19 : bPixelIsPoint = true;
3851 : bPointGeoIgnore =
3852 19 : CPLTestBool(CPLGetConfigOption("GTIFF_POINT_GEO_IGNORE", "FALSE"));
3853 : }
3854 :
3855 5832 : if (m_bForceUnsetGTOrGCPs)
3856 : {
3857 11 : m_bNeedsRewrite = true;
3858 11 : m_bForceUnsetGTOrGCPs = false;
3859 :
3860 11 : TIFFUnsetField(m_hTIFF, TIFFTAG_GEOPIXELSCALE);
3861 11 : TIFFUnsetField(m_hTIFF, TIFFTAG_GEOTIEPOINTS);
3862 11 : TIFFUnsetField(m_hTIFF, TIFFTAG_GEOTRANSMATRIX);
3863 : }
3864 :
3865 5832 : if (m_bForceUnsetProjection)
3866 : {
3867 8 : m_bNeedsRewrite = true;
3868 8 : m_bForceUnsetProjection = false;
3869 :
3870 8 : TIFFUnsetField(m_hTIFF, TIFFTAG_GEOKEYDIRECTORY);
3871 8 : TIFFUnsetField(m_hTIFF, TIFFTAG_GEODOUBLEPARAMS);
3872 8 : TIFFUnsetField(m_hTIFF, TIFFTAG_GEOASCIIPARAMS);
3873 : }
3874 :
3875 : /* -------------------------------------------------------------------- */
3876 : /* Write geotransform if valid. */
3877 : /* -------------------------------------------------------------------- */
3878 5832 : if (m_bGeoTransformValid)
3879 : {
3880 1800 : m_bNeedsRewrite = true;
3881 :
3882 : /* --------------------------------------------------------------------
3883 : */
3884 : /* Clear old tags to ensure we don't end up with conflicting */
3885 : /* information. (#2625) */
3886 : /* --------------------------------------------------------------------
3887 : */
3888 1800 : TIFFUnsetField(m_hTIFF, TIFFTAG_GEOPIXELSCALE);
3889 1800 : TIFFUnsetField(m_hTIFF, TIFFTAG_GEOTIEPOINTS);
3890 1800 : TIFFUnsetField(m_hTIFF, TIFFTAG_GEOTRANSMATRIX);
3891 :
3892 : /* --------------------------------------------------------------------
3893 : */
3894 : /* Write the transform. If we have a normal north-up image we */
3895 : /* use the tiepoint plus pixelscale otherwise we use a matrix. */
3896 : /* --------------------------------------------------------------------
3897 : */
3898 1800 : if (m_gt[2] == 0.0 && m_gt[4] == 0.0 && m_gt[5] < 0.0)
3899 : {
3900 1709 : double dfOffset = 0.0;
3901 1709 : if (m_eProfile != GTiffProfile::BASELINE)
3902 : {
3903 : // In the case the SRS has a vertical component and we have
3904 : // a single band, encode its scale/offset in the GeoTIFF tags
3905 1703 : int bHasScale = FALSE;
3906 1703 : double dfScale = GetRasterBand(1)->GetScale(&bHasScale);
3907 1703 : int bHasOffset = FALSE;
3908 1703 : dfOffset = GetRasterBand(1)->GetOffset(&bHasOffset);
3909 : const bool bApplyScaleOffset =
3910 1703 : m_oSRS.IsVertical() && GetRasterCount() == 1;
3911 1703 : if (bApplyScaleOffset && !bHasScale)
3912 0 : dfScale = 1.0;
3913 1703 : if (!bApplyScaleOffset || !bHasOffset)
3914 1700 : dfOffset = 0.0;
3915 : const double adfPixelScale[3] = {
3916 1703 : m_gt[1], fabs(m_gt[5]), bApplyScaleOffset ? dfScale : 0.0};
3917 1703 : TIFFSetField(m_hTIFF, TIFFTAG_GEOPIXELSCALE, 3, adfPixelScale);
3918 : }
3919 :
3920 1709 : double adfTiePoints[6] = {0.0, 0.0, 0.0,
3921 1709 : m_gt[0], m_gt[3], dfOffset};
3922 :
3923 1709 : if (bPixelIsPoint && !bPointGeoIgnore)
3924 : {
3925 15 : adfTiePoints[3] += m_gt[1] * 0.5 + m_gt[2] * 0.5;
3926 15 : adfTiePoints[4] += m_gt[4] * 0.5 + m_gt[5] * 0.5;
3927 : }
3928 :
3929 1709 : if (m_eProfile != GTiffProfile::BASELINE)
3930 1703 : TIFFSetField(m_hTIFF, TIFFTAG_GEOTIEPOINTS, 6, adfTiePoints);
3931 : }
3932 : else
3933 : {
3934 91 : double adfMatrix[16] = {};
3935 :
3936 91 : adfMatrix[0] = m_gt[1];
3937 91 : adfMatrix[1] = m_gt[2];
3938 91 : adfMatrix[3] = m_gt[0];
3939 91 : adfMatrix[4] = m_gt[4];
3940 91 : adfMatrix[5] = m_gt[5];
3941 91 : adfMatrix[7] = m_gt[3];
3942 91 : adfMatrix[15] = 1.0;
3943 :
3944 91 : if (bPixelIsPoint && !bPointGeoIgnore)
3945 : {
3946 0 : adfMatrix[3] += m_gt[1] * 0.5 + m_gt[2] * 0.5;
3947 0 : adfMatrix[7] += m_gt[4] * 0.5 + m_gt[5] * 0.5;
3948 : }
3949 :
3950 91 : if (m_eProfile != GTiffProfile::BASELINE)
3951 91 : TIFFSetField(m_hTIFF, TIFFTAG_GEOTRANSMATRIX, 16, adfMatrix);
3952 : }
3953 :
3954 1800 : if (m_poBaseDS == nullptr)
3955 : {
3956 : // Do we need a world file?
3957 1800 : if (CPLFetchBool(m_papszCreationOptions, "TFW", false))
3958 7 : GDALWriteWorldFile(m_osFilename.c_str(), "tfw", m_gt.data());
3959 1793 : else if (CPLFetchBool(m_papszCreationOptions, "WORLDFILE", false))
3960 2 : GDALWriteWorldFile(m_osFilename.c_str(), "wld", m_gt.data());
3961 : }
3962 : }
3963 4046 : else if (GetGCPCount() > 0 && GetGCPCount() <= knMAX_GCP_COUNT &&
3964 14 : m_eProfile != GTiffProfile::BASELINE)
3965 : {
3966 14 : m_bNeedsRewrite = true;
3967 :
3968 : double *padfTiePoints = static_cast<double *>(
3969 14 : CPLMalloc(6 * sizeof(double) * GetGCPCount()));
3970 :
3971 74 : for (size_t iGCP = 0; iGCP < m_aoGCPs.size(); ++iGCP)
3972 : {
3973 :
3974 60 : padfTiePoints[iGCP * 6 + 0] = m_aoGCPs[iGCP].Pixel();
3975 60 : padfTiePoints[iGCP * 6 + 1] = m_aoGCPs[iGCP].Line();
3976 60 : padfTiePoints[iGCP * 6 + 2] = 0;
3977 60 : padfTiePoints[iGCP * 6 + 3] = m_aoGCPs[iGCP].X();
3978 60 : padfTiePoints[iGCP * 6 + 4] = m_aoGCPs[iGCP].Y();
3979 60 : padfTiePoints[iGCP * 6 + 5] = m_aoGCPs[iGCP].Z();
3980 :
3981 60 : if (bPixelIsPoint && !bPointGeoIgnore)
3982 : {
3983 0 : padfTiePoints[iGCP * 6 + 0] += 0.5;
3984 0 : padfTiePoints[iGCP * 6 + 1] += 0.5;
3985 : }
3986 : }
3987 :
3988 14 : TIFFSetField(m_hTIFF, TIFFTAG_GEOTIEPOINTS, 6 * GetGCPCount(),
3989 : padfTiePoints);
3990 14 : CPLFree(padfTiePoints);
3991 : }
3992 :
3993 : /* -------------------------------------------------------------------- */
3994 : /* Write out projection definition. */
3995 : /* -------------------------------------------------------------------- */
3996 5832 : const bool bHasProjection = !m_oSRS.IsEmpty();
3997 5832 : if ((bHasProjection || bPixelIsPoint) &&
3998 1499 : m_eProfile != GTiffProfile::BASELINE)
3999 : {
4000 1495 : m_bNeedsRewrite = true;
4001 :
4002 : // If we have existing geokeys, try to wipe them
4003 : // by writing a dummy geokey directory. (#2546)
4004 1495 : GTiffWriteDummyGeokeyDirectory(m_hTIFF);
4005 :
4006 1495 : GTIF *psGTIF = GTiffDataset::GTIFNew(m_hTIFF);
4007 :
4008 : // Set according to coordinate system.
4009 1495 : if (bHasProjection)
4010 : {
4011 1494 : if (IsSRSCompatibleOfGeoTIFF(&m_oSRS, m_eGeoTIFFKeysFlavor))
4012 : {
4013 1492 : GTIFSetFromOGISDefnEx(psGTIF,
4014 : OGRSpatialReference::ToHandle(&m_oSRS),
4015 : m_eGeoTIFFKeysFlavor, m_eGeoTIFFVersion);
4016 : }
4017 : else
4018 : {
4019 2 : GDALPamDataset::SetSpatialRef(&m_oSRS);
4020 : }
4021 : }
4022 :
4023 1495 : if (bPixelIsPoint)
4024 : {
4025 19 : GTIFKeySet(psGTIF, GTRasterTypeGeoKey, TYPE_SHORT, 1,
4026 : RasterPixelIsPoint);
4027 : }
4028 :
4029 1495 : GTIFWriteKeys(psGTIF);
4030 1495 : GTIFFree(psGTIF);
4031 : }
4032 5832 : }
4033 :
4034 : /************************************************************************/
4035 : /* AppendMetadataItem() */
4036 : /************************************************************************/
4037 :
4038 3888 : static void AppendMetadataItem(CPLXMLNode **ppsRoot, CPLXMLNode **ppsTail,
4039 : const char *pszKey, const char *pszValue,
4040 : CPLXMLNode *psValueNode, int nBand,
4041 : const char *pszRole, const char *pszDomain)
4042 :
4043 : {
4044 3888 : CPLAssert(pszValue || psValueNode);
4045 3888 : CPLAssert(!(pszValue && psValueNode));
4046 :
4047 : /* -------------------------------------------------------------------- */
4048 : /* Create the Item element, and subcomponents. */
4049 : /* -------------------------------------------------------------------- */
4050 3888 : CPLXMLNode *psItem = CPLCreateXMLNode(nullptr, CXT_Element, "Item");
4051 3888 : CPLAddXMLAttributeAndValue(psItem, "name", pszKey);
4052 :
4053 3888 : if (nBand > 0)
4054 : {
4055 1164 : char szBandId[32] = {};
4056 1164 : snprintf(szBandId, sizeof(szBandId), "%d", nBand - 1);
4057 1164 : CPLAddXMLAttributeAndValue(psItem, "sample", szBandId);
4058 : }
4059 :
4060 3888 : if (pszRole != nullptr)
4061 382 : CPLAddXMLAttributeAndValue(psItem, "role", pszRole);
4062 :
4063 3888 : if (pszDomain != nullptr && strlen(pszDomain) > 0)
4064 1010 : CPLAddXMLAttributeAndValue(psItem, "domain", pszDomain);
4065 :
4066 3888 : if (pszValue)
4067 : {
4068 : // Note: this escaping should not normally be done, as the serialization
4069 : // of the tree to XML also does it, so we end up width double XML escaping,
4070 : // but keep it for backward compatibility.
4071 3868 : char *pszEscapedItemValue = CPLEscapeString(pszValue, -1, CPLES_XML);
4072 3868 : CPLCreateXMLNode(psItem, CXT_Text, pszEscapedItemValue);
4073 3868 : CPLFree(pszEscapedItemValue);
4074 : }
4075 : else
4076 : {
4077 20 : CPLAddXMLChild(psItem, psValueNode);
4078 : }
4079 :
4080 : /* -------------------------------------------------------------------- */
4081 : /* Create root, if missing. */
4082 : /* -------------------------------------------------------------------- */
4083 3888 : if (*ppsRoot == nullptr)
4084 760 : *ppsRoot = CPLCreateXMLNode(nullptr, CXT_Element, "GDALMetadata");
4085 :
4086 : /* -------------------------------------------------------------------- */
4087 : /* Append item to tail. We keep track of the tail to avoid */
4088 : /* O(nsquared) time as the list gets longer. */
4089 : /* -------------------------------------------------------------------- */
4090 3888 : if (*ppsTail == nullptr)
4091 760 : CPLAddXMLChild(*ppsRoot, psItem);
4092 : else
4093 3128 : CPLAddXMLSibling(*ppsTail, psItem);
4094 :
4095 3888 : *ppsTail = psItem;
4096 3888 : }
4097 :
4098 : /************************************************************************/
4099 : /* AppendMetadataItem() */
4100 : /************************************************************************/
4101 :
4102 3868 : static void AppendMetadataItem(CPLXMLNode **ppsRoot, CPLXMLNode **ppsTail,
4103 : const char *pszKey, const char *pszValue,
4104 : int nBand, const char *pszRole,
4105 : const char *pszDomain)
4106 :
4107 : {
4108 3868 : AppendMetadataItem(ppsRoot, ppsTail, pszKey, pszValue, nullptr, nBand,
4109 : pszRole, pszDomain);
4110 3868 : }
4111 :
4112 : /************************************************************************/
4113 : /* WriteMDMetadata() */
4114 : /************************************************************************/
4115 :
4116 310952 : static void WriteMDMetadata(GDALMultiDomainMetadata *poMDMD, TIFF *hTIFF,
4117 : CPLXMLNode **ppsRoot, CPLXMLNode **ppsTail,
4118 : int nBand, GTiffProfile eProfile)
4119 :
4120 : {
4121 :
4122 : /* ==================================================================== */
4123 : /* Process each domain. */
4124 : /* ==================================================================== */
4125 310952 : CSLConstList papszDomainList = poMDMD->GetDomainList();
4126 319378 : for (int iDomain = 0; papszDomainList && papszDomainList[iDomain];
4127 : ++iDomain)
4128 : {
4129 8426 : CSLConstList papszMD = poMDMD->GetMetadata(papszDomainList[iDomain]);
4130 8426 : bool bIsXMLOrJSON = false;
4131 :
4132 8426 : if (EQUAL(papszDomainList[iDomain], "IMAGE_STRUCTURE") ||
4133 2460 : EQUAL(papszDomainList[iDomain], "DERIVED_SUBDATASETS"))
4134 5969 : continue; // Ignored.
4135 2457 : if (EQUAL(papszDomainList[iDomain], "COLOR_PROFILE"))
4136 3 : continue; // Handled elsewhere.
4137 2454 : if (EQUAL(papszDomainList[iDomain], MD_DOMAIN_RPC))
4138 7 : continue; // Handled elsewhere.
4139 2448 : if (EQUAL(papszDomainList[iDomain], "xml:ESRI") &&
4140 1 : CPLTestBool(CPLGetConfigOption("ESRI_XML_PAM", "NO")))
4141 1 : continue; // Handled elsewhere.
4142 2446 : if (EQUAL(papszDomainList[iDomain], "xml:XMP"))
4143 2 : continue; // Handled in SetMetadata.
4144 :
4145 2444 : if (STARTS_WITH_CI(papszDomainList[iDomain], "xml:") ||
4146 2442 : STARTS_WITH_CI(papszDomainList[iDomain], "json:"))
4147 : {
4148 12 : bIsXMLOrJSON = true;
4149 : }
4150 :
4151 : /* --------------------------------------------------------------------
4152 : */
4153 : /* Process each item in this domain. */
4154 : /* --------------------------------------------------------------------
4155 : */
4156 7515 : for (int iItem = 0; papszMD && papszMD[iItem]; ++iItem)
4157 : {
4158 5071 : const char *pszItemValue = nullptr;
4159 5071 : char *pszItemName = nullptr;
4160 :
4161 5071 : if (bIsXMLOrJSON)
4162 : {
4163 11 : pszItemName = CPLStrdup("doc");
4164 11 : pszItemValue = papszMD[iItem];
4165 : }
4166 : else
4167 : {
4168 5060 : pszItemValue = CPLParseNameValue(papszMD[iItem], &pszItemName);
4169 5060 : if (pszItemName == nullptr)
4170 : {
4171 49 : CPLDebug("GTiff", "Invalid metadata item : %s",
4172 49 : papszMD[iItem]);
4173 49 : continue;
4174 : }
4175 : }
4176 :
4177 : /* --------------------------------------------------------------------
4178 : */
4179 : /* Convert into XML item or handle as a special TIFF tag. */
4180 : /* --------------------------------------------------------------------
4181 : */
4182 5022 : if (strlen(papszDomainList[iDomain]) == 0 && nBand == 0 &&
4183 3667 : (STARTS_WITH_CI(pszItemName, "TIFFTAG_") ||
4184 3606 : (EQUAL(pszItemName, "GEO_METADATA") &&
4185 3605 : eProfile == GTiffProfile::GDALGEOTIFF) ||
4186 3605 : (EQUAL(pszItemName, "TIFF_RSID") &&
4187 : eProfile == GTiffProfile::GDALGEOTIFF)))
4188 : {
4189 63 : if (EQUAL(pszItemName, "TIFFTAG_RESOLUTIONUNIT"))
4190 : {
4191 : // ResolutionUnit can't be 0, which is the default if
4192 : // atoi() fails. Set to 1=Unknown.
4193 9 : int v = atoi(pszItemValue);
4194 9 : if (!v)
4195 1 : v = RESUNIT_NONE;
4196 9 : TIFFSetField(hTIFF, TIFFTAG_RESOLUTIONUNIT, v);
4197 : }
4198 : else
4199 : {
4200 54 : bool bFoundTag = false;
4201 54 : size_t iTag = 0; // Used after for.
4202 54 : const auto *pasTIFFTags = GTiffDataset::GetTIFFTags();
4203 286 : for (; pasTIFFTags[iTag].pszTagName; ++iTag)
4204 : {
4205 286 : if (EQUAL(pszItemName, pasTIFFTags[iTag].pszTagName))
4206 : {
4207 54 : bFoundTag = true;
4208 54 : break;
4209 : }
4210 : }
4211 :
4212 54 : if (bFoundTag &&
4213 54 : pasTIFFTags[iTag].eType == GTIFFTAGTYPE_STRING)
4214 33 : TIFFSetField(hTIFF, pasTIFFTags[iTag].nTagVal,
4215 : pszItemValue);
4216 21 : else if (bFoundTag &&
4217 21 : pasTIFFTags[iTag].eType == GTIFFTAGTYPE_FLOAT)
4218 16 : TIFFSetField(hTIFF, pasTIFFTags[iTag].nTagVal,
4219 : CPLAtof(pszItemValue));
4220 5 : else if (bFoundTag &&
4221 5 : pasTIFFTags[iTag].eType == GTIFFTAGTYPE_SHORT)
4222 4 : TIFFSetField(hTIFF, pasTIFFTags[iTag].nTagVal,
4223 : atoi(pszItemValue));
4224 1 : else if (bFoundTag && pasTIFFTags[iTag].eType ==
4225 : GTIFFTAGTYPE_BYTE_STRING)
4226 : {
4227 1 : uint32_t nLen =
4228 1 : static_cast<uint32_t>(strlen(pszItemValue));
4229 1 : if (nLen)
4230 : {
4231 1 : TIFFSetField(hTIFF, pasTIFFTags[iTag].nTagVal, nLen,
4232 : pszItemValue);
4233 1 : }
4234 : }
4235 : else
4236 0 : CPLError(CE_Warning, CPLE_NotSupported,
4237 : "%s metadata item is unhandled and "
4238 : "will not be written",
4239 : pszItemName);
4240 63 : }
4241 : }
4242 4959 : else if (nBand == 0 && EQUAL(pszItemName, GDALMD_AREA_OR_POINT))
4243 : {
4244 : /* Do nothing, handled elsewhere. */;
4245 : }
4246 : else
4247 : {
4248 3075 : AppendMetadataItem(ppsRoot, ppsTail, pszItemName, pszItemValue,
4249 3075 : nBand, nullptr, papszDomainList[iDomain]);
4250 : }
4251 :
4252 5022 : CPLFree(pszItemName);
4253 : }
4254 :
4255 : /* --------------------------------------------------------------------
4256 : */
4257 : /* Remove TIFFTAG_xxxxxx that are already set but no longer in */
4258 : /* the metadata list (#5619) */
4259 : /* --------------------------------------------------------------------
4260 : */
4261 2444 : if (strlen(papszDomainList[iDomain]) == 0 && nBand == 0)
4262 : {
4263 2158 : const auto *pasTIFFTags = GTiffDataset::GetTIFFTags();
4264 32370 : for (size_t iTag = 0; pasTIFFTags[iTag].pszTagName; ++iTag)
4265 : {
4266 30212 : uint32_t nCount = 0;
4267 30212 : char *pszText = nullptr;
4268 30212 : int16_t nVal = 0;
4269 30212 : float fVal = 0.0f;
4270 : const char *pszVal =
4271 30212 : CSLFetchNameValue(papszMD, pasTIFFTags[iTag].pszTagName);
4272 60361 : if (pszVal == nullptr &&
4273 30149 : ((pasTIFFTags[iTag].eType == GTIFFTAGTYPE_STRING &&
4274 17231 : TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal,
4275 30141 : &pszText)) ||
4276 30141 : (pasTIFFTags[iTag].eType == GTIFFTAGTYPE_SHORT &&
4277 6461 : TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal, &nVal)) ||
4278 30138 : (pasTIFFTags[iTag].eType == GTIFFTAGTYPE_FLOAT &&
4279 4300 : TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal, &fVal)) ||
4280 30137 : (pasTIFFTags[iTag].eType == GTIFFTAGTYPE_BYTE_STRING &&
4281 2157 : TIFFGetField(hTIFF, pasTIFFTags[iTag].nTagVal, &nCount,
4282 : &pszText))))
4283 : {
4284 13 : TIFFUnsetField(hTIFF, pasTIFFTags[iTag].nTagVal);
4285 : }
4286 : }
4287 : }
4288 : }
4289 310952 : }
4290 :
4291 : /************************************************************************/
4292 : /* WriteRPC() */
4293 : /************************************************************************/
4294 :
4295 10089 : void GTiffDataset::WriteRPC(GDALDataset *poSrcDS, TIFF *l_hTIFF,
4296 : int bSrcIsGeoTIFF, GTiffProfile eProfile,
4297 : const char *pszTIFFFilename,
4298 : CSLConstList papszCreationOptions,
4299 : bool bWriteOnlyInPAMIfNeeded)
4300 : {
4301 : /* -------------------------------------------------------------------- */
4302 : /* Handle RPC data written to TIFF RPCCoefficient tag, RPB file, */
4303 : /* RPCTEXT file or PAM. */
4304 : /* -------------------------------------------------------------------- */
4305 10089 : CSLConstList papszRPCMD = poSrcDS->GetMetadata(MD_DOMAIN_RPC);
4306 10089 : if (papszRPCMD != nullptr)
4307 : {
4308 32 : bool bRPCSerializedOtherWay = false;
4309 :
4310 32 : if (eProfile == GTiffProfile::GDALGEOTIFF)
4311 : {
4312 20 : if (!bWriteOnlyInPAMIfNeeded)
4313 11 : GTiffDatasetWriteRPCTag(l_hTIFF, papszRPCMD);
4314 20 : bRPCSerializedOtherWay = true;
4315 : }
4316 :
4317 : // Write RPB file if explicitly asked, or if a non GDAL specific
4318 : // profile is selected and RPCTXT is not asked.
4319 : bool bRPBExplicitlyAsked =
4320 32 : CPLFetchBool(papszCreationOptions, "RPB", false);
4321 : bool bRPBExplicitlyDenied =
4322 32 : !CPLFetchBool(papszCreationOptions, "RPB", true);
4323 44 : if ((eProfile != GTiffProfile::GDALGEOTIFF &&
4324 12 : !CPLFetchBool(papszCreationOptions, "RPCTXT", false) &&
4325 44 : !bRPBExplicitlyDenied) ||
4326 : bRPBExplicitlyAsked)
4327 : {
4328 8 : if (!bWriteOnlyInPAMIfNeeded)
4329 4 : GDALWriteRPBFile(pszTIFFFilename, papszRPCMD);
4330 8 : bRPCSerializedOtherWay = true;
4331 : }
4332 :
4333 32 : if (CPLFetchBool(papszCreationOptions, "RPCTXT", false))
4334 : {
4335 2 : if (!bWriteOnlyInPAMIfNeeded)
4336 1 : GDALWriteRPCTXTFile(pszTIFFFilename, papszRPCMD);
4337 2 : bRPCSerializedOtherWay = true;
4338 : }
4339 :
4340 32 : if (!bRPCSerializedOtherWay && bWriteOnlyInPAMIfNeeded && bSrcIsGeoTIFF)
4341 1 : cpl::down_cast<GTiffDataset *>(poSrcDS)
4342 1 : ->GDALPamDataset::SetMetadata(papszRPCMD, MD_DOMAIN_RPC);
4343 : }
4344 10089 : }
4345 :
4346 : /************************************************************************/
4347 : /* WriteMetadata() */
4348 : /************************************************************************/
4349 :
4350 8004 : bool GTiffDataset::WriteMetadata(GDALDataset *poSrcDS, TIFF *l_hTIFF,
4351 : bool bSrcIsGeoTIFF, GTiffProfile eProfile,
4352 : const char *pszTIFFFilename,
4353 : CSLConstList papszCreationOptions,
4354 : bool bExcludeRPBandIMGFileWriting)
4355 :
4356 : {
4357 : /* -------------------------------------------------------------------- */
4358 : /* Convert all the remaining metadata into a simple XML */
4359 : /* format. */
4360 : /* -------------------------------------------------------------------- */
4361 8004 : CPLXMLNode *psRoot = nullptr;
4362 8004 : CPLXMLNode *psTail = nullptr;
4363 :
4364 : const char *pszCopySrcMDD =
4365 8004 : CSLFetchNameValueDef(papszCreationOptions, "COPY_SRC_MDD", "AUTO");
4366 : char **papszSrcMDD =
4367 8004 : CSLFetchNameValueMultiple(papszCreationOptions, "SRC_MDD");
4368 :
4369 : GTiffDataset *poSrcDSGTiff =
4370 8004 : bSrcIsGeoTIFF ? cpl::down_cast<GTiffDataset *>(poSrcDS) : nullptr;
4371 :
4372 8004 : if (poSrcDSGTiff)
4373 : {
4374 5892 : WriteMDMetadata(&poSrcDSGTiff->m_oGTiffMDMD, l_hTIFF, &psRoot, &psTail,
4375 : 0, eProfile);
4376 : }
4377 : else
4378 : {
4379 2112 : if (EQUAL(pszCopySrcMDD, "AUTO") || CPLTestBool(pszCopySrcMDD) ||
4380 : papszSrcMDD)
4381 : {
4382 4218 : GDALMultiDomainMetadata l_oMDMD;
4383 : {
4384 2109 : CSLConstList papszMD = poSrcDS->GetMetadata();
4385 2113 : if (CSLCount(papszMD) > 0 &&
4386 4 : (!papszSrcMDD || CSLFindString(papszSrcMDD, "") >= 0 ||
4387 2 : CSLFindString(papszSrcMDD, "_DEFAULT_") >= 0))
4388 : {
4389 1595 : l_oMDMD.SetMetadata(papszMD);
4390 : }
4391 : }
4392 :
4393 2109 : if (EQUAL(pszCopySrcMDD, "AUTO") && !papszSrcMDD)
4394 : {
4395 : // Propagate ISIS3 or VICAR metadata
4396 6300 : for (const char *pszMDD : {"json:ISIS3", "json:VICAR"})
4397 : {
4398 4200 : CSLConstList papszMD = poSrcDS->GetMetadata(pszMDD);
4399 4200 : if (papszMD)
4400 : {
4401 5 : l_oMDMD.SetMetadata(papszMD, pszMDD);
4402 : }
4403 : }
4404 : }
4405 :
4406 2109 : if ((!EQUAL(pszCopySrcMDD, "AUTO") && CPLTestBool(pszCopySrcMDD)) ||
4407 : papszSrcMDD)
4408 : {
4409 9 : char **papszDomainList = poSrcDS->GetMetadataDomainList();
4410 39 : for (CSLConstList papszIter = papszDomainList;
4411 39 : papszIter && *papszIter; ++papszIter)
4412 : {
4413 30 : const char *pszDomain = *papszIter;
4414 46 : if (pszDomain[0] != 0 &&
4415 16 : (!papszSrcMDD ||
4416 16 : CSLFindString(papszSrcMDD, pszDomain) >= 0))
4417 : {
4418 12 : l_oMDMD.SetMetadata(poSrcDS->GetMetadata(pszDomain),
4419 : pszDomain);
4420 : }
4421 : }
4422 9 : CSLDestroy(papszDomainList);
4423 : }
4424 :
4425 2109 : WriteMDMetadata(&l_oMDMD, l_hTIFF, &psRoot, &psTail, 0, eProfile);
4426 : }
4427 : }
4428 :
4429 8004 : if (!bExcludeRPBandIMGFileWriting &&
4430 5886 : (!poSrcDSGTiff || poSrcDSGTiff->m_poBaseDS == nullptr))
4431 : {
4432 7993 : WriteRPC(poSrcDS, l_hTIFF, bSrcIsGeoTIFF, eProfile, pszTIFFFilename,
4433 : papszCreationOptions);
4434 :
4435 : /* ------------------------------------------------------------------ */
4436 : /* Handle metadata data written to an IMD file. */
4437 : /* ------------------------------------------------------------------ */
4438 7993 : CSLConstList papszIMDMD = poSrcDS->GetMetadata(MD_DOMAIN_IMD);
4439 7993 : if (papszIMDMD != nullptr)
4440 : {
4441 20 : GDALWriteIMDFile(pszTIFFFilename, papszIMDMD);
4442 : }
4443 : }
4444 :
4445 8004 : uint16_t nPhotometric = 0;
4446 8004 : if (!TIFFGetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, &(nPhotometric)))
4447 1 : nPhotometric = PHOTOMETRIC_MINISBLACK;
4448 :
4449 8004 : const bool bStandardColorInterp = GTIFFIsStandardColorInterpretation(
4450 : GDALDataset::ToHandle(poSrcDS), nPhotometric, papszCreationOptions);
4451 :
4452 : /* -------------------------------------------------------------------- */
4453 : /* We also need to address band specific metadata, and special */
4454 : /* "role" metadata. */
4455 : /* -------------------------------------------------------------------- */
4456 315893 : for (int nBand = 1; nBand <= poSrcDS->GetRasterCount(); ++nBand)
4457 : {
4458 307889 : GDALRasterBand *poBand = poSrcDS->GetRasterBand(nBand);
4459 :
4460 307889 : if (bSrcIsGeoTIFF)
4461 : {
4462 : GTiffRasterBand *poSrcBandGTiff =
4463 302860 : cpl::down_cast<GTiffRasterBand *>(poBand);
4464 302860 : assert(poSrcBandGTiff);
4465 302860 : WriteMDMetadata(&poSrcBandGTiff->m_oGTiffMDMD, l_hTIFF, &psRoot,
4466 : &psTail, nBand, eProfile);
4467 : }
4468 : else
4469 : {
4470 10058 : GDALMultiDomainMetadata l_oMDMD;
4471 5029 : bool bOMDMDSet = false;
4472 :
4473 5029 : if (EQUAL(pszCopySrcMDD, "AUTO") && !papszSrcMDD)
4474 : {
4475 15051 : for (const char *pszDomain : {"", "IMAGERY"})
4476 : {
4477 10034 : if (CSLConstList papszMD = poBand->GetMetadata(pszDomain))
4478 : {
4479 89 : if (papszMD[0])
4480 : {
4481 89 : bOMDMDSet = true;
4482 89 : l_oMDMD.SetMetadata(papszMD, pszDomain);
4483 : }
4484 : }
4485 5017 : }
4486 : }
4487 12 : else if (CPLTestBool(pszCopySrcMDD) || papszSrcMDD)
4488 : {
4489 9 : char **papszDomainList = poBand->GetMetadataDomainList();
4490 3 : for (const char *pszDomain :
4491 15 : cpl::Iterate(CSLConstList(papszDomainList)))
4492 : {
4493 9 : if (pszDomain[0] != 0 &&
4494 5 : !EQUAL(pszDomain, "IMAGE_STRUCTURE") &&
4495 2 : (!papszSrcMDD ||
4496 2 : CSLFindString(papszSrcMDD, pszDomain) >= 0))
4497 : {
4498 2 : bOMDMDSet = true;
4499 2 : l_oMDMD.SetMetadata(poBand->GetMetadata(pszDomain),
4500 : pszDomain);
4501 : }
4502 : }
4503 9 : CSLDestroy(papszDomainList);
4504 : }
4505 :
4506 5029 : if (bOMDMDSet)
4507 : {
4508 91 : WriteMDMetadata(&l_oMDMD, l_hTIFF, &psRoot, &psTail, nBand,
4509 : eProfile);
4510 : }
4511 : }
4512 :
4513 307889 : const double dfOffset = poBand->GetOffset();
4514 307889 : const double dfScale = poBand->GetScale();
4515 307889 : bool bGeoTIFFScaleOffsetInZ = false;
4516 307889 : GDALGeoTransform gt;
4517 : // Check if we have already encoded scale/offset in the GeoTIFF tags
4518 313965 : if (poSrcDS->GetGeoTransform(gt) == CE_None && gt[2] == 0.0 &&
4519 6061 : gt[4] == 0.0 && gt[5] < 0.0 && poSrcDS->GetSpatialRef() &&
4520 313972 : poSrcDS->GetSpatialRef()->IsVertical() &&
4521 7 : poSrcDS->GetRasterCount() == 1)
4522 : {
4523 7 : bGeoTIFFScaleOffsetInZ = true;
4524 : }
4525 :
4526 307889 : if ((dfOffset != 0.0 || dfScale != 1.0) && !bGeoTIFFScaleOffsetInZ)
4527 : {
4528 25 : char szValue[128] = {};
4529 :
4530 25 : CPLsnprintf(szValue, sizeof(szValue), "%.17g", dfOffset);
4531 25 : AppendMetadataItem(&psRoot, &psTail, "OFFSET", szValue, nBand,
4532 : "offset", "");
4533 25 : CPLsnprintf(szValue, sizeof(szValue), "%.17g", dfScale);
4534 25 : AppendMetadataItem(&psRoot, &psTail, "SCALE", szValue, nBand,
4535 : "scale", "");
4536 : }
4537 :
4538 307889 : const char *pszUnitType = poBand->GetUnitType();
4539 307889 : if (pszUnitType != nullptr && pszUnitType[0] != '\0')
4540 : {
4541 40 : bool bWriteUnit = true;
4542 40 : auto poSRS = poSrcDS->GetSpatialRef();
4543 40 : if (poSRS && poSRS->IsCompound())
4544 : {
4545 2 : const char *pszVertUnit = nullptr;
4546 2 : poSRS->GetTargetLinearUnits("COMPD_CS|VERT_CS", &pszVertUnit);
4547 2 : if (pszVertUnit && EQUAL(pszVertUnit, pszUnitType))
4548 : {
4549 2 : bWriteUnit = false;
4550 : }
4551 : }
4552 40 : if (bWriteUnit)
4553 : {
4554 38 : AppendMetadataItem(&psRoot, &psTail, "UNITTYPE", pszUnitType,
4555 : nBand, "unittype", "");
4556 : }
4557 : }
4558 :
4559 307889 : if (strlen(poBand->GetDescription()) > 0)
4560 : {
4561 24 : AppendMetadataItem(&psRoot, &psTail, "DESCRIPTION",
4562 24 : poBand->GetDescription(), nBand, "description",
4563 : "");
4564 : }
4565 :
4566 308106 : if (!bStandardColorInterp &&
4567 217 : !(nBand <= 3 && EQUAL(CSLFetchNameValueDef(papszCreationOptions,
4568 : "PHOTOMETRIC", ""),
4569 : "RGB")))
4570 : {
4571 250 : AppendMetadataItem(&psRoot, &psTail, "COLORINTERP",
4572 : GDALGetColorInterpretationName(
4573 250 : poBand->GetColorInterpretation()),
4574 : nBand, "colorinterp", "");
4575 : }
4576 : }
4577 :
4578 8004 : CSLDestroy(papszSrcMDD);
4579 :
4580 : const char *pszTilingSchemeName =
4581 8004 : CSLFetchNameValue(papszCreationOptions, "@TILING_SCHEME_NAME");
4582 8004 : if (pszTilingSchemeName)
4583 : {
4584 23 : AppendMetadataItem(&psRoot, &psTail, "NAME", pszTilingSchemeName, 0,
4585 : nullptr, "TILING_SCHEME");
4586 :
4587 23 : const char *pszZoomLevel = CSLFetchNameValue(
4588 : papszCreationOptions, "@TILING_SCHEME_ZOOM_LEVEL");
4589 23 : if (pszZoomLevel)
4590 : {
4591 23 : AppendMetadataItem(&psRoot, &psTail, "ZOOM_LEVEL", pszZoomLevel, 0,
4592 : nullptr, "TILING_SCHEME");
4593 : }
4594 :
4595 23 : const char *pszAlignedLevels = CSLFetchNameValue(
4596 : papszCreationOptions, "@TILING_SCHEME_ALIGNED_LEVELS");
4597 23 : if (pszAlignedLevels)
4598 : {
4599 4 : AppendMetadataItem(&psRoot, &psTail, "ALIGNED_LEVELS",
4600 : pszAlignedLevels, 0, nullptr, "TILING_SCHEME");
4601 : }
4602 : }
4603 :
4604 8004 : if (const char *pszOverviewResampling =
4605 8004 : CSLFetchNameValue(papszCreationOptions, "@OVERVIEW_RESAMPLING"))
4606 : {
4607 39 : AppendMetadataItem(&psRoot, &psTail, "OVERVIEW_RESAMPLING",
4608 : pszOverviewResampling, 0, nullptr,
4609 : "IMAGE_STRUCTURE");
4610 : }
4611 :
4612 : /* -------------------------------------------------------------------- */
4613 : /* Write information about some codecs. */
4614 : /* -------------------------------------------------------------------- */
4615 8004 : if (CPLTestBool(
4616 : CPLGetConfigOption("GTIFF_WRITE_IMAGE_STRUCTURE_METADATA", "YES")))
4617 : {
4618 : const char *pszTileInterleave =
4619 7999 : CSLFetchNameValue(papszCreationOptions, "@TILE_INTERLEAVE");
4620 7999 : if (pszTileInterleave && CPLTestBool(pszTileInterleave))
4621 : {
4622 7 : AppendMetadataItem(&psRoot, &psTail, "INTERLEAVE", "TILE", 0,
4623 : nullptr, "IMAGE_STRUCTURE");
4624 : }
4625 :
4626 : const char *pszCompress =
4627 7999 : CSLFetchNameValue(papszCreationOptions, "COMPRESS");
4628 7999 : if (pszCompress && EQUAL(pszCompress, "WEBP"))
4629 : {
4630 31 : if (GTiffGetWebPLossless(papszCreationOptions))
4631 : {
4632 6 : AppendMetadataItem(&psRoot, &psTail,
4633 : "COMPRESSION_REVERSIBILITY", "LOSSLESS", 0,
4634 : nullptr, "IMAGE_STRUCTURE");
4635 : }
4636 : else
4637 : {
4638 25 : AppendMetadataItem(
4639 : &psRoot, &psTail, "WEBP_LEVEL",
4640 25 : CPLSPrintf("%d", GTiffGetWebPLevel(papszCreationOptions)),
4641 : 0, nullptr, "IMAGE_STRUCTURE");
4642 : }
4643 : }
4644 7968 : else if (pszCompress && STARTS_WITH_CI(pszCompress, "LERC"))
4645 : {
4646 : const double dfMaxZError =
4647 97 : GTiffGetLERCMaxZError(papszCreationOptions);
4648 : const double dfMaxZErrorOverview =
4649 97 : GTiffGetLERCMaxZErrorOverview(papszCreationOptions);
4650 97 : if (dfMaxZError == 0.0 && dfMaxZErrorOverview == 0.0)
4651 : {
4652 83 : AppendMetadataItem(&psRoot, &psTail,
4653 : "COMPRESSION_REVERSIBILITY", "LOSSLESS", 0,
4654 : nullptr, "IMAGE_STRUCTURE");
4655 : }
4656 : else
4657 : {
4658 14 : AppendMetadataItem(&psRoot, &psTail, "MAX_Z_ERROR",
4659 : CSLFetchNameValueDef(papszCreationOptions,
4660 : "MAX_Z_ERROR", ""),
4661 : 0, nullptr, "IMAGE_STRUCTURE");
4662 14 : if (dfMaxZError != dfMaxZErrorOverview)
4663 : {
4664 3 : AppendMetadataItem(
4665 : &psRoot, &psTail, "MAX_Z_ERROR_OVERVIEW",
4666 : CSLFetchNameValueDef(papszCreationOptions,
4667 : "MAX_Z_ERROR_OVERVIEW", ""),
4668 : 0, nullptr, "IMAGE_STRUCTURE");
4669 : }
4670 97 : }
4671 : }
4672 : #if HAVE_JXL
4673 7871 : else if (pszCompress && EQUAL(pszCompress, "JXL"))
4674 : {
4675 101 : float fDistance = 0.0f;
4676 101 : if (GTiffGetJXLLossless(papszCreationOptions))
4677 : {
4678 82 : AppendMetadataItem(&psRoot, &psTail,
4679 : "COMPRESSION_REVERSIBILITY", "LOSSLESS", 0,
4680 : nullptr, "IMAGE_STRUCTURE");
4681 : }
4682 : else
4683 : {
4684 19 : fDistance = GTiffGetJXLDistance(papszCreationOptions);
4685 19 : AppendMetadataItem(
4686 : &psRoot, &psTail, "JXL_DISTANCE",
4687 : CPLSPrintf("%f", static_cast<double>(fDistance)), 0,
4688 : nullptr, "IMAGE_STRUCTURE");
4689 : }
4690 : const float fAlphaDistance =
4691 101 : GTiffGetJXLAlphaDistance(papszCreationOptions);
4692 101 : if (fAlphaDistance >= 0.0f && fAlphaDistance != fDistance)
4693 : {
4694 2 : AppendMetadataItem(
4695 : &psRoot, &psTail, "JXL_ALPHA_DISTANCE",
4696 : CPLSPrintf("%f", static_cast<double>(fAlphaDistance)), 0,
4697 : nullptr, "IMAGE_STRUCTURE");
4698 : }
4699 101 : AppendMetadataItem(
4700 : &psRoot, &psTail, "JXL_EFFORT",
4701 : CPLSPrintf("%d", GTiffGetJXLEffort(papszCreationOptions)), 0,
4702 : nullptr, "IMAGE_STRUCTURE");
4703 : }
4704 : #endif
4705 : }
4706 :
4707 8004 : if (!CPLTestBool(CPLGetConfigOption("GTIFF_WRITE_RAT_TO_PAM", "NO")))
4708 : {
4709 315889 : for (int nBand = 1; nBand <= poSrcDS->GetRasterCount(); ++nBand)
4710 : {
4711 307887 : GDALRasterBand *poBand = poSrcDS->GetRasterBand(nBand);
4712 307887 : const auto poRAT = poBand->GetDefaultRAT();
4713 307887 : if (poRAT)
4714 : {
4715 21 : auto psSerializedRAT = poRAT->Serialize();
4716 21 : if (psSerializedRAT)
4717 : {
4718 20 : AppendMetadataItem(
4719 : &psRoot, &psTail, DEFAULT_RASTER_ATTRIBUTE_TABLE,
4720 : nullptr, psSerializedRAT, nBand, RAT_ROLE, nullptr);
4721 : }
4722 : }
4723 : }
4724 : }
4725 :
4726 : /* -------------------------------------------------------------------- */
4727 : /* Write out the generic XML metadata if there is any. */
4728 : /* -------------------------------------------------------------------- */
4729 8004 : if (psRoot != nullptr)
4730 : {
4731 760 : bool bRet = true;
4732 :
4733 760 : if (eProfile == GTiffProfile::GDALGEOTIFF)
4734 : {
4735 743 : char *pszXML_MD = CPLSerializeXMLTree(psRoot);
4736 743 : TIFFSetField(l_hTIFF, TIFFTAG_GDAL_METADATA, pszXML_MD);
4737 743 : CPLFree(pszXML_MD);
4738 : }
4739 : else
4740 : {
4741 17 : if (bSrcIsGeoTIFF)
4742 11 : cpl::down_cast<GTiffDataset *>(poSrcDS)->PushMetadataToPam();
4743 : else
4744 6 : bRet = false;
4745 : }
4746 :
4747 760 : CPLDestroyXMLNode(psRoot);
4748 :
4749 760 : return bRet;
4750 : }
4751 :
4752 : // If we have no more metadata but it existed before,
4753 : // remove the GDAL_METADATA tag.
4754 7244 : if (eProfile == GTiffProfile::GDALGEOTIFF)
4755 : {
4756 7220 : char *pszText = nullptr;
4757 7220 : if (TIFFGetField(l_hTIFF, TIFFTAG_GDAL_METADATA, &pszText))
4758 : {
4759 7 : TIFFUnsetField(l_hTIFF, TIFFTAG_GDAL_METADATA);
4760 : }
4761 : }
4762 :
4763 7244 : return true;
4764 : }
4765 :
4766 : /************************************************************************/
4767 : /* PushMetadataToPam() */
4768 : /* */
4769 : /* When producing a strict profile TIFF or if our aggregate */
4770 : /* metadata is too big for a single tiff tag we may end up */
4771 : /* needing to write it via the PAM mechanisms. This method */
4772 : /* copies all the appropriate metadata into the PAM level */
4773 : /* metadata object but with special care to avoid copying */
4774 : /* metadata handled in other ways in TIFF format. */
4775 : /************************************************************************/
4776 :
4777 17 : void GTiffDataset::PushMetadataToPam()
4778 :
4779 : {
4780 17 : if (GetPamFlags() & GPF_DISABLED)
4781 0 : return;
4782 :
4783 17 : const bool bStandardColorInterp = GTIFFIsStandardColorInterpretation(
4784 17 : GDALDataset::ToHandle(this), m_nPhotometric, m_papszCreationOptions);
4785 :
4786 55 : for (int nBand = 0; nBand <= GetRasterCount(); ++nBand)
4787 : {
4788 38 : GDALMultiDomainMetadata *poSrcMDMD = nullptr;
4789 38 : GTiffRasterBand *poBand = nullptr;
4790 :
4791 38 : if (nBand == 0)
4792 : {
4793 17 : poSrcMDMD = &(this->m_oGTiffMDMD);
4794 : }
4795 : else
4796 : {
4797 21 : poBand = cpl::down_cast<GTiffRasterBand *>(GetRasterBand(nBand));
4798 21 : poSrcMDMD = &(poBand->m_oGTiffMDMD);
4799 : }
4800 :
4801 : /* --------------------------------------------------------------------
4802 : */
4803 : /* Loop over the available domains. */
4804 : /* --------------------------------------------------------------------
4805 : */
4806 38 : CSLConstList papszDomainList = poSrcMDMD->GetDomainList();
4807 74 : for (int iDomain = 0; papszDomainList && papszDomainList[iDomain];
4808 : ++iDomain)
4809 : {
4810 36 : char **papszMD = poSrcMDMD->GetMetadata(papszDomainList[iDomain]);
4811 :
4812 36 : if (EQUAL(papszDomainList[iDomain], MD_DOMAIN_RPC) ||
4813 36 : EQUAL(papszDomainList[iDomain], MD_DOMAIN_IMD) ||
4814 36 : EQUAL(papszDomainList[iDomain], "_temporary_") ||
4815 36 : EQUAL(papszDomainList[iDomain], "IMAGE_STRUCTURE") ||
4816 19 : EQUAL(papszDomainList[iDomain], "COLOR_PROFILE"))
4817 17 : continue;
4818 :
4819 19 : papszMD = CSLDuplicate(papszMD);
4820 :
4821 69 : for (int i = CSLCount(papszMD) - 1; i >= 0; --i)
4822 : {
4823 50 : if (STARTS_WITH_CI(papszMD[i], "TIFFTAG_") ||
4824 50 : EQUALN(papszMD[i], GDALMD_AREA_OR_POINT,
4825 : strlen(GDALMD_AREA_OR_POINT)))
4826 4 : papszMD = CSLRemoveStrings(papszMD, i, 1, nullptr);
4827 : }
4828 :
4829 19 : if (nBand == 0)
4830 10 : GDALPamDataset::SetMetadata(papszMD, papszDomainList[iDomain]);
4831 : else
4832 9 : poBand->GDALPamRasterBand::SetMetadata(
4833 9 : papszMD, papszDomainList[iDomain]);
4834 :
4835 19 : CSLDestroy(papszMD);
4836 : }
4837 :
4838 : /* --------------------------------------------------------------------
4839 : */
4840 : /* Handle some "special domain" stuff. */
4841 : /* --------------------------------------------------------------------
4842 : */
4843 38 : if (poBand != nullptr)
4844 : {
4845 21 : poBand->GDALPamRasterBand::SetOffset(poBand->GetOffset());
4846 21 : poBand->GDALPamRasterBand::SetScale(poBand->GetScale());
4847 21 : poBand->GDALPamRasterBand::SetUnitType(poBand->GetUnitType());
4848 21 : poBand->GDALPamRasterBand::SetDescription(poBand->GetDescription());
4849 21 : if (!bStandardColorInterp)
4850 : {
4851 3 : poBand->GDALPamRasterBand::SetColorInterpretation(
4852 3 : poBand->GetColorInterpretation());
4853 : }
4854 : }
4855 : }
4856 17 : MarkPamDirty();
4857 : }
4858 :
4859 : /************************************************************************/
4860 : /* WriteNoDataValue() */
4861 : /************************************************************************/
4862 :
4863 520 : void GTiffDataset::WriteNoDataValue(TIFF *hTIFF, double dfNoData)
4864 :
4865 : {
4866 1040 : CPLString osVal(GTiffFormatGDALNoDataTagValue(dfNoData));
4867 520 : TIFFSetField(hTIFF, TIFFTAG_GDAL_NODATA, osVal.c_str());
4868 520 : }
4869 :
4870 5 : void GTiffDataset::WriteNoDataValue(TIFF *hTIFF, int64_t nNoData)
4871 :
4872 : {
4873 5 : TIFFSetField(hTIFF, TIFFTAG_GDAL_NODATA,
4874 : CPLSPrintf(CPL_FRMT_GIB, static_cast<GIntBig>(nNoData)));
4875 5 : }
4876 :
4877 5 : void GTiffDataset::WriteNoDataValue(TIFF *hTIFF, uint64_t nNoData)
4878 :
4879 : {
4880 5 : TIFFSetField(hTIFF, TIFFTAG_GDAL_NODATA,
4881 : CPLSPrintf(CPL_FRMT_GUIB, static_cast<GUIntBig>(nNoData)));
4882 5 : }
4883 :
4884 : /************************************************************************/
4885 : /* UnsetNoDataValue() */
4886 : /************************************************************************/
4887 :
4888 16 : void GTiffDataset::UnsetNoDataValue(TIFF *l_hTIFF)
4889 :
4890 : {
4891 16 : TIFFUnsetField(l_hTIFF, TIFFTAG_GDAL_NODATA);
4892 16 : }
4893 :
4894 : /************************************************************************/
4895 : /* SaveICCProfile() */
4896 : /* */
4897 : /* Save ICC Profile or colorimetric data into file */
4898 : /* pDS: */
4899 : /* Dataset that contains the metadata with the ICC or colorimetric */
4900 : /* data. If this argument is specified, all other arguments are */
4901 : /* ignored. Set them to NULL or 0. */
4902 : /* hTIFF: */
4903 : /* Pointer to TIFF handle. Only needed if pDS is NULL or */
4904 : /* pDS->m_hTIFF is NULL. */
4905 : /* papszParamList: */
4906 : /* Options containing the ICC profile or colorimetric metadata. */
4907 : /* Ignored if pDS is not NULL. */
4908 : /* nBitsPerSample: */
4909 : /* Bits per sample. Ignored if pDS is not NULL. */
4910 : /************************************************************************/
4911 :
4912 9751 : void GTiffDataset::SaveICCProfile(GTiffDataset *pDS, TIFF *l_hTIFF,
4913 : CSLConstList papszParamList,
4914 : uint32_t l_nBitsPerSample)
4915 : {
4916 9751 : if ((pDS != nullptr) && (pDS->eAccess != GA_Update))
4917 0 : return;
4918 :
4919 9751 : if (l_hTIFF == nullptr)
4920 : {
4921 2 : if (pDS == nullptr)
4922 0 : return;
4923 :
4924 2 : l_hTIFF = pDS->m_hTIFF;
4925 2 : if (l_hTIFF == nullptr)
4926 0 : return;
4927 : }
4928 :
4929 9751 : if ((papszParamList == nullptr) && (pDS == nullptr))
4930 4835 : return;
4931 :
4932 : const char *pszICCProfile =
4933 : (pDS != nullptr)
4934 4916 : ? pDS->GetMetadataItem("SOURCE_ICC_PROFILE", "COLOR_PROFILE")
4935 4914 : : CSLFetchNameValue(papszParamList, "SOURCE_ICC_PROFILE");
4936 4916 : if (pszICCProfile != nullptr)
4937 : {
4938 8 : char *pEmbedBuffer = CPLStrdup(pszICCProfile);
4939 : int32_t nEmbedLen =
4940 8 : CPLBase64DecodeInPlace(reinterpret_cast<GByte *>(pEmbedBuffer));
4941 :
4942 8 : TIFFSetField(l_hTIFF, TIFFTAG_ICCPROFILE, nEmbedLen, pEmbedBuffer);
4943 :
4944 8 : CPLFree(pEmbedBuffer);
4945 : }
4946 : else
4947 : {
4948 : // Output colorimetric data.
4949 4908 : float pCHR[6] = {}; // Primaries.
4950 4908 : uint16_t pTXR[6] = {}; // Transfer range.
4951 4908 : const char *pszCHRNames[] = {"SOURCE_PRIMARIES_RED",
4952 : "SOURCE_PRIMARIES_GREEN",
4953 : "SOURCE_PRIMARIES_BLUE"};
4954 4908 : const char *pszTXRNames[] = {"TIFFTAG_TRANSFERRANGE_BLACK",
4955 : "TIFFTAG_TRANSFERRANGE_WHITE"};
4956 :
4957 : // Output chromacities.
4958 4908 : bool bOutputCHR = true;
4959 4923 : for (int i = 0; i < 3 && bOutputCHR; ++i)
4960 : {
4961 : const char *pszColorProfile =
4962 : (pDS != nullptr)
4963 4918 : ? pDS->GetMetadataItem(pszCHRNames[i], "COLOR_PROFILE")
4964 4915 : : CSLFetchNameValue(papszParamList, pszCHRNames[i]);
4965 4918 : if (pszColorProfile == nullptr)
4966 : {
4967 4903 : bOutputCHR = false;
4968 4903 : break;
4969 : }
4970 :
4971 : const CPLStringList aosTokens(CSLTokenizeString2(
4972 : pszColorProfile, ",",
4973 : CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
4974 15 : CSLT_STRIPENDSPACES));
4975 :
4976 15 : if (aosTokens.size() != 3)
4977 : {
4978 0 : bOutputCHR = false;
4979 0 : break;
4980 : }
4981 :
4982 60 : for (int j = 0; j < 3; ++j)
4983 : {
4984 45 : float v = static_cast<float>(CPLAtof(aosTokens[j]));
4985 :
4986 45 : if (j == 2)
4987 : {
4988 : // Last term of xyY color must be 1.0.
4989 15 : if (v != 1.0f)
4990 : {
4991 0 : bOutputCHR = false;
4992 0 : break;
4993 : }
4994 : }
4995 : else
4996 : {
4997 30 : pCHR[i * 2 + j] = v;
4998 : }
4999 : }
5000 : }
5001 :
5002 4908 : if (bOutputCHR)
5003 : {
5004 5 : TIFFSetField(l_hTIFF, TIFFTAG_PRIMARYCHROMATICITIES, pCHR);
5005 : }
5006 :
5007 : // Output whitepoint.
5008 : const char *pszSourceWhitePoint =
5009 : (pDS != nullptr)
5010 4908 : ? pDS->GetMetadataItem("SOURCE_WHITEPOINT", "COLOR_PROFILE")
5011 4907 : : CSLFetchNameValue(papszParamList, "SOURCE_WHITEPOINT");
5012 4908 : if (pszSourceWhitePoint != nullptr)
5013 : {
5014 : const CPLStringList aosTokens(CSLTokenizeString2(
5015 : pszSourceWhitePoint, ",",
5016 : CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
5017 10 : CSLT_STRIPENDSPACES));
5018 :
5019 5 : bool bOutputWhitepoint = true;
5020 5 : float pWP[2] = {0.0f, 0.0f}; // Whitepoint
5021 5 : if (aosTokens.size() != 3)
5022 : {
5023 0 : bOutputWhitepoint = false;
5024 : }
5025 : else
5026 : {
5027 20 : for (int j = 0; j < 3; ++j)
5028 : {
5029 15 : const float v = static_cast<float>(CPLAtof(aosTokens[j]));
5030 :
5031 15 : if (j == 2)
5032 : {
5033 : // Last term of xyY color must be 1.0.
5034 5 : if (v != 1.0f)
5035 : {
5036 0 : bOutputWhitepoint = false;
5037 0 : break;
5038 : }
5039 : }
5040 : else
5041 : {
5042 10 : pWP[j] = v;
5043 : }
5044 : }
5045 : }
5046 :
5047 5 : if (bOutputWhitepoint)
5048 : {
5049 5 : TIFFSetField(l_hTIFF, TIFFTAG_WHITEPOINT, pWP);
5050 : }
5051 : }
5052 :
5053 : // Set transfer function metadata.
5054 : char const *pszTFRed =
5055 : (pDS != nullptr)
5056 4908 : ? pDS->GetMetadataItem("TIFFTAG_TRANSFERFUNCTION_RED",
5057 : "COLOR_PROFILE")
5058 4907 : : CSLFetchNameValue(papszParamList,
5059 4908 : "TIFFTAG_TRANSFERFUNCTION_RED");
5060 :
5061 : char const *pszTFGreen =
5062 : (pDS != nullptr)
5063 4908 : ? pDS->GetMetadataItem("TIFFTAG_TRANSFERFUNCTION_GREEN",
5064 : "COLOR_PROFILE")
5065 4907 : : CSLFetchNameValue(papszParamList,
5066 4908 : "TIFFTAG_TRANSFERFUNCTION_GREEN");
5067 :
5068 : char const *pszTFBlue =
5069 : (pDS != nullptr)
5070 4908 : ? pDS->GetMetadataItem("TIFFTAG_TRANSFERFUNCTION_BLUE",
5071 : "COLOR_PROFILE")
5072 4907 : : CSLFetchNameValue(papszParamList,
5073 4908 : "TIFFTAG_TRANSFERFUNCTION_BLUE");
5074 :
5075 4908 : if ((pszTFRed != nullptr) && (pszTFGreen != nullptr) &&
5076 : (pszTFBlue != nullptr))
5077 : {
5078 : // Get length of table.
5079 4 : const int nTransferFunctionLength =
5080 4 : 1 << ((pDS != nullptr) ? pDS->m_nBitsPerSample
5081 : : l_nBitsPerSample);
5082 :
5083 : const CPLStringList aosTokensRed(CSLTokenizeString2(
5084 : pszTFRed, ",",
5085 : CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
5086 8 : CSLT_STRIPENDSPACES));
5087 : const CPLStringList aosTokensGreen(CSLTokenizeString2(
5088 : pszTFGreen, ",",
5089 : CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
5090 8 : CSLT_STRIPENDSPACES));
5091 : const CPLStringList aosTokensBlue(CSLTokenizeString2(
5092 : pszTFBlue, ",",
5093 : CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
5094 8 : CSLT_STRIPENDSPACES));
5095 :
5096 4 : if ((aosTokensRed.size() == nTransferFunctionLength) &&
5097 8 : (aosTokensGreen.size() == nTransferFunctionLength) &&
5098 4 : (aosTokensBlue.size() == nTransferFunctionLength))
5099 : {
5100 : std::vector<uint16_t> anTransferFuncRed(
5101 8 : nTransferFunctionLength);
5102 : std::vector<uint16_t> anTransferFuncGreen(
5103 8 : nTransferFunctionLength);
5104 : std::vector<uint16_t> anTransferFuncBlue(
5105 8 : nTransferFunctionLength);
5106 :
5107 : // Convert our table in string format into int16_t format.
5108 1028 : for (int i = 0; i < nTransferFunctionLength; ++i)
5109 : {
5110 2048 : anTransferFuncRed[i] =
5111 1024 : static_cast<uint16_t>(atoi(aosTokensRed[i]));
5112 2048 : anTransferFuncGreen[i] =
5113 1024 : static_cast<uint16_t>(atoi(aosTokensGreen[i]));
5114 1024 : anTransferFuncBlue[i] =
5115 1024 : static_cast<uint16_t>(atoi(aosTokensBlue[i]));
5116 : }
5117 :
5118 4 : TIFFSetField(
5119 : l_hTIFF, TIFFTAG_TRANSFERFUNCTION, anTransferFuncRed.data(),
5120 : anTransferFuncGreen.data(), anTransferFuncBlue.data());
5121 : }
5122 : }
5123 :
5124 : // Output transfer range.
5125 4908 : bool bOutputTransferRange = true;
5126 4908 : for (int i = 0; (i < 2) && bOutputTransferRange; ++i)
5127 : {
5128 : const char *pszTXRVal =
5129 : (pDS != nullptr)
5130 4908 : ? pDS->GetMetadataItem(pszTXRNames[i], "COLOR_PROFILE")
5131 4907 : : CSLFetchNameValue(papszParamList, pszTXRNames[i]);
5132 4908 : if (pszTXRVal == nullptr)
5133 : {
5134 4908 : bOutputTransferRange = false;
5135 4908 : break;
5136 : }
5137 :
5138 : const CPLStringList aosTokens(CSLTokenizeString2(
5139 : pszTXRVal, ",",
5140 : CSLT_ALLOWEMPTYTOKENS | CSLT_STRIPLEADSPACES |
5141 0 : CSLT_STRIPENDSPACES));
5142 :
5143 0 : if (aosTokens.size() != 3)
5144 : {
5145 0 : bOutputTransferRange = false;
5146 0 : break;
5147 : }
5148 :
5149 0 : for (int j = 0; j < 3; ++j)
5150 : {
5151 0 : pTXR[i + j * 2] = static_cast<uint16_t>(atoi(aosTokens[j]));
5152 : }
5153 : }
5154 :
5155 4908 : if (bOutputTransferRange)
5156 : {
5157 0 : const int TIFFTAG_TRANSFERRANGE = 0x0156;
5158 0 : TIFFSetField(l_hTIFF, TIFFTAG_TRANSFERRANGE, pTXR);
5159 : }
5160 : }
5161 : }
5162 :
5163 17597 : static signed char GTiffGetLZMAPreset(CSLConstList papszOptions)
5164 : {
5165 17597 : int nLZMAPreset = -1;
5166 17597 : const char *pszValue = CSLFetchNameValue(papszOptions, "LZMA_PRESET");
5167 17597 : if (pszValue != nullptr)
5168 : {
5169 20 : nLZMAPreset = atoi(pszValue);
5170 20 : if (!(nLZMAPreset >= 0 && nLZMAPreset <= 9))
5171 : {
5172 0 : CPLError(CE_Warning, CPLE_IllegalArg,
5173 : "LZMA_PRESET=%s value not recognised, ignoring.",
5174 : pszValue);
5175 0 : nLZMAPreset = -1;
5176 : }
5177 : }
5178 17597 : return static_cast<signed char>(nLZMAPreset);
5179 : }
5180 :
5181 17597 : static signed char GTiffGetZSTDPreset(CSLConstList papszOptions)
5182 : {
5183 17597 : int nZSTDLevel = -1;
5184 17597 : const char *pszValue = CSLFetchNameValue(papszOptions, "ZSTD_LEVEL");
5185 17597 : if (pszValue != nullptr)
5186 : {
5187 24 : nZSTDLevel = atoi(pszValue);
5188 24 : if (!(nZSTDLevel >= 1 && nZSTDLevel <= 22))
5189 : {
5190 0 : CPLError(CE_Warning, CPLE_IllegalArg,
5191 : "ZSTD_LEVEL=%s value not recognised, ignoring.", pszValue);
5192 0 : nZSTDLevel = -1;
5193 : }
5194 : }
5195 17597 : return static_cast<signed char>(nZSTDLevel);
5196 : }
5197 :
5198 17597 : static signed char GTiffGetZLevel(CSLConstList papszOptions)
5199 : {
5200 17597 : int nZLevel = -1;
5201 17597 : const char *pszValue = CSLFetchNameValue(papszOptions, "ZLEVEL");
5202 17597 : if (pszValue != nullptr)
5203 : {
5204 44 : nZLevel = atoi(pszValue);
5205 : #ifdef TIFFTAG_DEFLATE_SUBCODEC
5206 44 : constexpr int nMaxLevel = 12;
5207 : #ifndef LIBDEFLATE_SUPPORT
5208 : if (nZLevel > 9 && nZLevel <= nMaxLevel)
5209 : {
5210 : CPLDebug("GTiff",
5211 : "ZLEVEL=%d not supported in a non-libdeflate enabled "
5212 : "libtiff build. Capping to 9",
5213 : nZLevel);
5214 : nZLevel = 9;
5215 : }
5216 : #endif
5217 : #else
5218 : constexpr int nMaxLevel = 9;
5219 : #endif
5220 44 : if (nZLevel < 1 || nZLevel > nMaxLevel)
5221 : {
5222 0 : CPLError(CE_Warning, CPLE_IllegalArg,
5223 : "ZLEVEL=%s value not recognised, ignoring.", pszValue);
5224 0 : nZLevel = -1;
5225 : }
5226 : }
5227 17597 : return static_cast<signed char>(nZLevel);
5228 : }
5229 :
5230 17597 : static signed char GTiffGetJpegQuality(CSLConstList papszOptions)
5231 : {
5232 17597 : int nJpegQuality = -1;
5233 17597 : const char *pszValue = CSLFetchNameValue(papszOptions, "JPEG_QUALITY");
5234 17597 : if (pszValue != nullptr)
5235 : {
5236 1939 : nJpegQuality = atoi(pszValue);
5237 1939 : if (nJpegQuality < 1 || nJpegQuality > 100)
5238 : {
5239 0 : CPLError(CE_Warning, CPLE_IllegalArg,
5240 : "JPEG_QUALITY=%s value not recognised, ignoring.",
5241 : pszValue);
5242 0 : nJpegQuality = -1;
5243 : }
5244 : }
5245 17597 : return static_cast<signed char>(nJpegQuality);
5246 : }
5247 :
5248 17597 : static signed char GTiffGetJpegTablesMode(CSLConstList papszOptions)
5249 : {
5250 17597 : return static_cast<signed char>(atoi(
5251 : CSLFetchNameValueDef(papszOptions, "JPEGTABLESMODE",
5252 17597 : CPLSPrintf("%d", knGTIFFJpegTablesModeDefault))));
5253 : }
5254 :
5255 : /************************************************************************/
5256 : /* GetDiscardLsbOption() */
5257 : /************************************************************************/
5258 :
5259 7787 : static GTiffDataset::MaskOffset *GetDiscardLsbOption(TIFF *hTIFF,
5260 : CSLConstList papszOptions)
5261 : {
5262 7787 : const char *pszBits = CSLFetchNameValue(papszOptions, "DISCARD_LSB");
5263 7787 : if (pszBits == nullptr)
5264 7665 : return nullptr;
5265 :
5266 122 : uint16_t nPhotometric = 0;
5267 122 : TIFFGetFieldDefaulted(hTIFF, TIFFTAG_PHOTOMETRIC, &nPhotometric);
5268 :
5269 122 : uint16_t nBitsPerSample = 0;
5270 122 : if (!TIFFGetField(hTIFF, TIFFTAG_BITSPERSAMPLE, &nBitsPerSample))
5271 0 : nBitsPerSample = 1;
5272 :
5273 122 : uint16_t nSamplesPerPixel = 0;
5274 122 : if (!TIFFGetField(hTIFF, TIFFTAG_SAMPLESPERPIXEL, &nSamplesPerPixel))
5275 0 : nSamplesPerPixel = 1;
5276 :
5277 122 : uint16_t nSampleFormat = 0;
5278 122 : if (!TIFFGetField(hTIFF, TIFFTAG_SAMPLEFORMAT, &nSampleFormat))
5279 0 : nSampleFormat = SAMPLEFORMAT_UINT;
5280 :
5281 122 : if (nPhotometric == PHOTOMETRIC_PALETTE)
5282 : {
5283 1 : CPLError(CE_Warning, CPLE_AppDefined,
5284 : "DISCARD_LSB ignored on a paletted image");
5285 1 : return nullptr;
5286 : }
5287 121 : if (!(nBitsPerSample == 8 || nBitsPerSample == 16 || nBitsPerSample == 32 ||
5288 13 : nBitsPerSample == 64))
5289 : {
5290 1 : CPLError(CE_Warning, CPLE_AppDefined,
5291 : "DISCARD_LSB ignored on non 8, 16, 32 or 64 bits images");
5292 1 : return nullptr;
5293 : }
5294 :
5295 240 : const CPLStringList aosTokens(CSLTokenizeString2(pszBits, ",", 0));
5296 120 : const int nTokens = aosTokens.size();
5297 120 : GTiffDataset::MaskOffset *panMaskOffsetLsb = nullptr;
5298 120 : if (nTokens == 1 || nTokens == nSamplesPerPixel)
5299 : {
5300 : panMaskOffsetLsb = static_cast<GTiffDataset::MaskOffset *>(
5301 119 : CPLCalloc(nSamplesPerPixel, sizeof(GTiffDataset::MaskOffset)));
5302 374 : for (int i = 0; i < nSamplesPerPixel; ++i)
5303 : {
5304 255 : const int nBits = atoi(aosTokens[nTokens == 1 ? 0 : i]);
5305 510 : const int nMaxBits = (nSampleFormat == SAMPLEFORMAT_IEEEFP)
5306 510 : ? ((nBitsPerSample == 16) ? 11 - 1
5307 78 : : (nBitsPerSample == 32) ? 23 - 1
5308 26 : : (nBitsPerSample == 64) ? 53 - 1
5309 : : 0)
5310 203 : : nSampleFormat == SAMPLEFORMAT_INT
5311 203 : ? nBitsPerSample - 2
5312 119 : : nBitsPerSample - 1;
5313 :
5314 255 : if (nBits < 0 || nBits > nMaxBits)
5315 : {
5316 0 : CPLError(
5317 : CE_Warning, CPLE_AppDefined,
5318 : "DISCARD_LSB ignored: values should be in [0,%d] range",
5319 : nMaxBits);
5320 0 : VSIFree(panMaskOffsetLsb);
5321 0 : return nullptr;
5322 : }
5323 255 : panMaskOffsetLsb[i].nMask =
5324 255 : ~((static_cast<uint64_t>(1) << nBits) - 1);
5325 255 : if (nBits > 1)
5326 : {
5327 249 : panMaskOffsetLsb[i].nRoundUpBitTest = static_cast<uint64_t>(1)
5328 249 : << (nBits - 1);
5329 : }
5330 119 : }
5331 : }
5332 : else
5333 : {
5334 1 : CPLError(CE_Warning, CPLE_AppDefined,
5335 : "DISCARD_LSB ignored: wrong number of components");
5336 : }
5337 120 : return panMaskOffsetLsb;
5338 : }
5339 :
5340 7787 : void GTiffDataset::GetDiscardLsbOption(CSLConstList papszOptions)
5341 : {
5342 7787 : m_panMaskOffsetLsb = ::GetDiscardLsbOption(m_hTIFF, papszOptions);
5343 7787 : }
5344 :
5345 : /************************************************************************/
5346 : /* GetProfile() */
5347 : /************************************************************************/
5348 :
5349 17648 : static GTiffProfile GetProfile(const char *pszProfile)
5350 : {
5351 17648 : GTiffProfile eProfile = GTiffProfile::GDALGEOTIFF;
5352 17648 : if (pszProfile != nullptr)
5353 : {
5354 70 : if (EQUAL(pszProfile, szPROFILE_BASELINE))
5355 50 : eProfile = GTiffProfile::BASELINE;
5356 20 : else if (EQUAL(pszProfile, szPROFILE_GeoTIFF))
5357 18 : eProfile = GTiffProfile::GEOTIFF;
5358 2 : else if (!EQUAL(pszProfile, szPROFILE_GDALGeoTIFF))
5359 : {
5360 0 : CPLError(CE_Warning, CPLE_NotSupported,
5361 : "Unsupported value for PROFILE: %s", pszProfile);
5362 : }
5363 : }
5364 17648 : return eProfile;
5365 : }
5366 :
5367 : /************************************************************************/
5368 : /* GTiffCreate() */
5369 : /* */
5370 : /* Shared functionality between GTiffDataset::Create() and */
5371 : /* GTiffCreateCopy() for creating TIFF file based on a set of */
5372 : /* options and a configuration. */
5373 : /************************************************************************/
5374 :
5375 9830 : TIFF *GTiffDataset::CreateLL(const char *pszFilename, int nXSize, int nYSize,
5376 : int l_nBands, GDALDataType eType,
5377 : double dfExtraSpaceForOverviews,
5378 : int nColorTableMultiplier,
5379 : CSLConstList papszParamList, VSILFILE **pfpL,
5380 : CPLString &l_osTmpFilename, bool bCreateCopy,
5381 : bool &bTileInterleavingOut)
5382 :
5383 : {
5384 9830 : bTileInterleavingOut = false;
5385 :
5386 9830 : GTiffOneTimeInit();
5387 :
5388 : /* -------------------------------------------------------------------- */
5389 : /* Blow on a few errors. */
5390 : /* -------------------------------------------------------------------- */
5391 9830 : if (nXSize < 1 || nYSize < 1 || l_nBands < 1)
5392 : {
5393 2 : ReportError(
5394 : pszFilename, CE_Failure, CPLE_AppDefined,
5395 : "Attempt to create %dx%dx%d TIFF file, but width, height and bands"
5396 : "must be positive.",
5397 : nXSize, nYSize, l_nBands);
5398 :
5399 2 : return nullptr;
5400 : }
5401 :
5402 9828 : if (l_nBands > 65535)
5403 : {
5404 1 : ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
5405 : "Attempt to create %dx%dx%d TIFF file, but bands "
5406 : "must be lesser or equal to 65535.",
5407 : nXSize, nYSize, l_nBands);
5408 :
5409 1 : return nullptr;
5410 : }
5411 :
5412 : /* -------------------------------------------------------------------- */
5413 : /* Setup values based on options. */
5414 : /* -------------------------------------------------------------------- */
5415 : const GTiffProfile eProfile =
5416 9827 : GetProfile(CSLFetchNameValue(papszParamList, "PROFILE"));
5417 :
5418 9827 : const bool bTiled = CPLFetchBool(papszParamList, "TILED", false);
5419 :
5420 9827 : int l_nBlockXSize = 0;
5421 9827 : if (const char *pszValue = CSLFetchNameValue(papszParamList, "BLOCKXSIZE"))
5422 : {
5423 460 : l_nBlockXSize = atoi(pszValue);
5424 460 : if (l_nBlockXSize < 0)
5425 : {
5426 0 : ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
5427 : "Invalid value for BLOCKXSIZE");
5428 0 : return nullptr;
5429 : }
5430 460 : if (!bTiled)
5431 : {
5432 10 : ReportError(pszFilename, CE_Warning, CPLE_IllegalArg,
5433 : "BLOCKXSIZE can only be used with TILED=YES");
5434 : }
5435 450 : else if (l_nBlockXSize % 16 != 0)
5436 : {
5437 1 : ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
5438 : "BLOCKXSIZE must be a multiple of 16");
5439 1 : return nullptr;
5440 : }
5441 : }
5442 :
5443 9826 : int l_nBlockYSize = 0;
5444 9826 : if (const char *pszValue = CSLFetchNameValue(papszParamList, "BLOCKYSIZE"))
5445 : {
5446 2571 : l_nBlockYSize = atoi(pszValue);
5447 2571 : if (l_nBlockYSize < 0)
5448 : {
5449 0 : ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
5450 : "Invalid value for BLOCKYSIZE");
5451 0 : return nullptr;
5452 : }
5453 2571 : if (bTiled && (l_nBlockYSize % 16 != 0))
5454 : {
5455 2 : ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
5456 : "BLOCKYSIZE must be a multiple of 16");
5457 2 : return nullptr;
5458 : }
5459 : }
5460 :
5461 9824 : if (bTiled)
5462 : {
5463 795 : if (l_nBlockXSize == 0)
5464 347 : l_nBlockXSize = 256;
5465 :
5466 795 : if (l_nBlockYSize == 0)
5467 347 : l_nBlockYSize = 256;
5468 : }
5469 :
5470 9824 : int nPlanar = 0;
5471 :
5472 : // Hidden @TILE_INTERLEAVE=YES parameter used by the COG driver
5473 9824 : if (bCreateCopy && CPLTestBool(CSLFetchNameValueDef(
5474 : papszParamList, "@TILE_INTERLEAVE", "NO")))
5475 : {
5476 7 : bTileInterleavingOut = true;
5477 7 : nPlanar = PLANARCONFIG_SEPARATE;
5478 : }
5479 : else
5480 : {
5481 9817 : if (const char *pszValue =
5482 9817 : CSLFetchNameValue(papszParamList, "INTERLEAVE"))
5483 : {
5484 1573 : if (EQUAL(pszValue, "PIXEL"))
5485 404 : nPlanar = PLANARCONFIG_CONTIG;
5486 1169 : else if (EQUAL(pszValue, "BAND"))
5487 : {
5488 1168 : nPlanar = PLANARCONFIG_SEPARATE;
5489 : }
5490 1 : else if (EQUAL(pszValue, "BAND"))
5491 : {
5492 0 : nPlanar = PLANARCONFIG_SEPARATE;
5493 : }
5494 : else
5495 : {
5496 1 : ReportError(
5497 : pszFilename, CE_Failure, CPLE_IllegalArg,
5498 : "INTERLEAVE=%s unsupported, value must be PIXEL or BAND.",
5499 : pszValue);
5500 1 : return nullptr;
5501 : }
5502 : }
5503 : else
5504 : {
5505 8244 : nPlanar = PLANARCONFIG_CONTIG;
5506 : }
5507 : }
5508 :
5509 9823 : int l_nCompression = COMPRESSION_NONE;
5510 9823 : if (const char *pszValue = CSLFetchNameValue(papszParamList, "COMPRESS"))
5511 : {
5512 3324 : l_nCompression = GTIFFGetCompressionMethod(pszValue, "COMPRESS");
5513 3324 : if (l_nCompression < 0)
5514 0 : return nullptr;
5515 : }
5516 :
5517 9823 : constexpr int JPEG_MAX_DIMENSION = 65500; // Defined in jpeglib.h
5518 9823 : constexpr int WEBP_MAX_DIMENSION = 16383;
5519 :
5520 : const struct
5521 : {
5522 : int nCodecID;
5523 : const char *pszCodecName;
5524 : int nMaxDim;
5525 9823 : } asLimitations[] = {
5526 : {COMPRESSION_JPEG, "JPEG", JPEG_MAX_DIMENSION},
5527 : {COMPRESSION_WEBP, "WEBP", WEBP_MAX_DIMENSION},
5528 : };
5529 :
5530 29457 : for (const auto &sLimitation : asLimitations)
5531 : {
5532 19642 : if (l_nCompression == sLimitation.nCodecID && !bTiled &&
5533 2074 : nXSize > sLimitation.nMaxDim)
5534 : {
5535 2 : ReportError(
5536 : pszFilename, CE_Failure, CPLE_IllegalArg,
5537 : "COMPRESS=%s is only compatible of un-tiled images whose "
5538 : "width is lesser or equal to %d pixels. "
5539 : "To overcome this limitation, set the TILED=YES creation "
5540 : "option.",
5541 2 : sLimitation.pszCodecName, sLimitation.nMaxDim);
5542 2 : return nullptr;
5543 : }
5544 19640 : else if (l_nCompression == sLimitation.nCodecID && bTiled &&
5545 52 : l_nBlockXSize > sLimitation.nMaxDim)
5546 : {
5547 2 : ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
5548 : "COMPRESS=%s is only compatible of tiled images whose "
5549 : "BLOCKXSIZE is lesser or equal to %d pixels.",
5550 2 : sLimitation.pszCodecName, sLimitation.nMaxDim);
5551 2 : return nullptr;
5552 : }
5553 19638 : else if (l_nCompression == sLimitation.nCodecID &&
5554 2122 : l_nBlockYSize > sLimitation.nMaxDim)
5555 : {
5556 4 : ReportError(pszFilename, CE_Failure, CPLE_IllegalArg,
5557 : "COMPRESS=%s is only compatible of images whose "
5558 : "BLOCKYSIZE is lesser or equal to %d pixels. "
5559 : "To overcome this limitation, set the TILED=YES "
5560 : "creation option",
5561 4 : sLimitation.pszCodecName, sLimitation.nMaxDim);
5562 4 : return nullptr;
5563 : }
5564 : }
5565 :
5566 : /* -------------------------------------------------------------------- */
5567 : /* How many bits per sample? We have a special case if NBITS */
5568 : /* specified for GDT_UInt8, GDT_UInt16, GDT_UInt32. */
5569 : /* -------------------------------------------------------------------- */
5570 9815 : int l_nBitsPerSample = GDALGetDataTypeSizeBits(eType);
5571 9815 : if (CSLFetchNameValue(papszParamList, "NBITS") != nullptr)
5572 : {
5573 1747 : int nMinBits = 0;
5574 1747 : int nMaxBits = 0;
5575 1747 : l_nBitsPerSample = atoi(CSLFetchNameValue(papszParamList, "NBITS"));
5576 1747 : if (eType == GDT_UInt8)
5577 : {
5578 527 : nMinBits = 1;
5579 527 : nMaxBits = 8;
5580 : }
5581 1220 : else if (eType == GDT_UInt16)
5582 : {
5583 1202 : nMinBits = 9;
5584 1202 : nMaxBits = 16;
5585 : }
5586 18 : else if (eType == GDT_UInt32)
5587 : {
5588 14 : nMinBits = 17;
5589 14 : nMaxBits = 32;
5590 : }
5591 4 : else if (eType == GDT_Float32)
5592 : {
5593 4 : if (l_nBitsPerSample != 16 && l_nBitsPerSample != 32)
5594 : {
5595 1 : ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
5596 : "Only NBITS=16 is supported for data type Float32");
5597 1 : l_nBitsPerSample = GDALGetDataTypeSizeBits(eType);
5598 : }
5599 : }
5600 : else
5601 : {
5602 0 : ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
5603 : "NBITS is not supported for data type %s",
5604 : GDALGetDataTypeName(eType));
5605 0 : l_nBitsPerSample = GDALGetDataTypeSizeBits(eType);
5606 : }
5607 :
5608 1747 : if (nMinBits != 0)
5609 : {
5610 1743 : if (l_nBitsPerSample < nMinBits)
5611 : {
5612 2 : ReportError(
5613 : pszFilename, CE_Warning, CPLE_AppDefined,
5614 : "NBITS=%d is invalid for data type %s. Using NBITS=%d",
5615 : l_nBitsPerSample, GDALGetDataTypeName(eType), nMinBits);
5616 2 : l_nBitsPerSample = nMinBits;
5617 : }
5618 1741 : else if (l_nBitsPerSample > nMaxBits)
5619 : {
5620 3 : ReportError(
5621 : pszFilename, CE_Warning, CPLE_AppDefined,
5622 : "NBITS=%d is invalid for data type %s. Using NBITS=%d",
5623 : l_nBitsPerSample, GDALGetDataTypeName(eType), nMaxBits);
5624 3 : l_nBitsPerSample = nMaxBits;
5625 : }
5626 : }
5627 : }
5628 :
5629 : #ifdef HAVE_JXL
5630 9815 : if ((l_nCompression == COMPRESSION_JXL ||
5631 106 : l_nCompression == COMPRESSION_JXL_DNG_1_7) &&
5632 105 : eType != GDT_Float16 && eType != GDT_Float32)
5633 : {
5634 : // Reflects tif_jxl's GetJXLDataType()
5635 85 : if (eType != GDT_UInt8 && eType != GDT_UInt16)
5636 : {
5637 1 : ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
5638 : "Data type %s not supported for JXL compression. Only "
5639 : "Byte, UInt16, Float16, Float32 are supported",
5640 : GDALGetDataTypeName(eType));
5641 2 : return nullptr;
5642 : }
5643 :
5644 : const struct
5645 : {
5646 : GDALDataType eDT;
5647 : int nBitsPerSample;
5648 84 : } asSupportedDTBitsPerSample[] = {
5649 : {GDT_UInt8, 8},
5650 : {GDT_UInt16, 16},
5651 : };
5652 :
5653 250 : for (const auto &sSupportedDTBitsPerSample : asSupportedDTBitsPerSample)
5654 : {
5655 167 : if (eType == sSupportedDTBitsPerSample.eDT &&
5656 84 : l_nBitsPerSample != sSupportedDTBitsPerSample.nBitsPerSample)
5657 : {
5658 1 : ReportError(
5659 : pszFilename, CE_Failure, CPLE_NotSupported,
5660 : "Bits per sample=%d not supported for JXL compression. "
5661 : "Only %d is supported for %s data type.",
5662 1 : l_nBitsPerSample, sSupportedDTBitsPerSample.nBitsPerSample,
5663 : GDALGetDataTypeName(eType));
5664 1 : return nullptr;
5665 : }
5666 : }
5667 : }
5668 : #endif
5669 :
5670 9813 : int nPredictor = PREDICTOR_NONE;
5671 9813 : const char *pszPredictor = CSLFetchNameValue(papszParamList, "PREDICTOR");
5672 9813 : if (pszPredictor)
5673 : {
5674 30 : nPredictor = atoi(pszPredictor);
5675 : }
5676 :
5677 9813 : if (nPredictor != PREDICTOR_NONE &&
5678 17 : l_nCompression != COMPRESSION_ADOBE_DEFLATE &&
5679 2 : l_nCompression != COMPRESSION_LZW &&
5680 2 : l_nCompression != COMPRESSION_LZMA &&
5681 : l_nCompression != COMPRESSION_ZSTD)
5682 : {
5683 1 : ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
5684 : "PREDICTOR option is ignored for COMPRESS=%s. "
5685 : "Only valid for DEFLATE, LZW, LZMA or ZSTD",
5686 : CSLFetchNameValueDef(papszParamList, "COMPRESS", "NONE"));
5687 : }
5688 :
5689 : // Do early checks as libtiff will only error out when starting to write.
5690 9841 : else if (nPredictor != PREDICTOR_NONE &&
5691 29 : CPLTestBool(
5692 : CPLGetConfigOption("GDAL_GTIFF_PREDICTOR_CHECKS", "YES")))
5693 : {
5694 : #if (TIFFLIB_VERSION > 20210416) || defined(INTERNAL_LIBTIFF)
5695 : #define HAVE_PREDICTOR_2_FOR_64BIT
5696 : #endif
5697 29 : if (nPredictor == 2)
5698 : {
5699 24 : if (l_nBitsPerSample != 8 && l_nBitsPerSample != 16 &&
5700 : l_nBitsPerSample != 32
5701 : #ifdef HAVE_PREDICTOR_2_FOR_64BIT
5702 2 : && l_nBitsPerSample != 64
5703 : #endif
5704 : )
5705 : {
5706 : #if !defined(HAVE_PREDICTOR_2_FOR_64BIT)
5707 : if (l_nBitsPerSample == 64)
5708 : {
5709 : ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
5710 : "PREDICTOR=2 is supported on 64 bit samples "
5711 : "starting with libtiff > 4.3.0.");
5712 : }
5713 : else
5714 : #endif
5715 : {
5716 2 : const int nBITSHint = (l_nBitsPerSample < 8) ? 8
5717 1 : : (l_nBitsPerSample < 16) ? 16
5718 0 : : (l_nBitsPerSample < 32) ? 32
5719 : : 64;
5720 1 : ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
5721 : #ifdef HAVE_PREDICTOR_2_FOR_64BIT
5722 : "PREDICTOR=2 is only supported with 8/16/32/64 "
5723 : "bit samples. You can specify the NBITS=%d "
5724 : "creation option to promote to the closest "
5725 : "supported bits per sample value.",
5726 : #else
5727 : "PREDICTOR=2 is only supported with 8/16/32 "
5728 : "bit samples. You can specify the NBITS=%d "
5729 : "creation option to promote to the closest "
5730 : "supported bits per sample value.",
5731 : #endif
5732 : nBITSHint);
5733 : }
5734 1 : return nullptr;
5735 : }
5736 : }
5737 5 : else if (nPredictor == 3)
5738 : {
5739 4 : if (eType != GDT_Float32 && eType != GDT_Float64)
5740 : {
5741 1 : ReportError(
5742 : pszFilename, CE_Failure, CPLE_AppDefined,
5743 : "PREDICTOR=3 is only supported with Float32 or Float64.");
5744 1 : return nullptr;
5745 : }
5746 : }
5747 : else
5748 : {
5749 1 : ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
5750 : "PREDICTOR=%s is not supported.", pszPredictor);
5751 1 : return nullptr;
5752 : }
5753 : }
5754 :
5755 9810 : const int l_nZLevel = GTiffGetZLevel(papszParamList);
5756 9810 : const int l_nLZMAPreset = GTiffGetLZMAPreset(papszParamList);
5757 9810 : const int l_nZSTDLevel = GTiffGetZSTDPreset(papszParamList);
5758 9810 : const int l_nWebPLevel = GTiffGetWebPLevel(papszParamList);
5759 9810 : const bool l_bWebPLossless = GTiffGetWebPLossless(papszParamList);
5760 9810 : const int l_nJpegQuality = GTiffGetJpegQuality(papszParamList);
5761 9810 : const int l_nJpegTablesMode = GTiffGetJpegTablesMode(papszParamList);
5762 9810 : const double l_dfMaxZError = GTiffGetLERCMaxZError(papszParamList);
5763 : #if HAVE_JXL
5764 9810 : bool bJXLLosslessSpecified = false;
5765 : const bool l_bJXLLossless =
5766 9810 : GTiffGetJXLLossless(papszParamList, &bJXLLosslessSpecified);
5767 9810 : const uint32_t l_nJXLEffort = GTiffGetJXLEffort(papszParamList);
5768 9810 : bool bJXLDistanceSpecified = false;
5769 : const float l_fJXLDistance =
5770 9810 : GTiffGetJXLDistance(papszParamList, &bJXLDistanceSpecified);
5771 9810 : if (bJXLDistanceSpecified && l_bJXLLossless)
5772 : {
5773 1 : ReportError(pszFilename, CE_Warning, CPLE_AppDefined,
5774 : "JXL_DISTANCE creation option is ignored, given %s "
5775 : "JXL_LOSSLESS=YES",
5776 : bJXLLosslessSpecified ? "(explicit)" : "(implicit)");
5777 : }
5778 9810 : bool bJXLAlphaDistanceSpecified = false;
5779 : const float l_fJXLAlphaDistance =
5780 9810 : GTiffGetJXLAlphaDistance(papszParamList, &bJXLAlphaDistanceSpecified);
5781 9810 : if (bJXLAlphaDistanceSpecified && l_bJXLLossless)
5782 : {
5783 1 : ReportError(pszFilename, CE_Warning, CPLE_AppDefined,
5784 : "JXL_ALPHA_DISTANCE creation option is ignored, given %s "
5785 : "JXL_LOSSLESS=YES",
5786 : bJXLLosslessSpecified ? "(explicit)" : "(implicit)");
5787 : }
5788 : #endif
5789 : /* -------------------------------------------------------------------- */
5790 : /* Streaming related code */
5791 : /* -------------------------------------------------------------------- */
5792 19620 : const CPLString osOriFilename(pszFilename);
5793 19620 : bool bStreaming = strcmp(pszFilename, "/vsistdout/") == 0 ||
5794 9810 : CPLFetchBool(papszParamList, "STREAMABLE_OUTPUT", false);
5795 : #ifdef S_ISFIFO
5796 9810 : if (!bStreaming)
5797 : {
5798 : VSIStatBufL sStat;
5799 9798 : if (VSIStatExL(pszFilename, &sStat,
5800 10678 : VSI_STAT_EXISTS_FLAG | VSI_STAT_NATURE_FLAG) == 0 &&
5801 880 : S_ISFIFO(sStat.st_mode))
5802 : {
5803 0 : bStreaming = true;
5804 : }
5805 : }
5806 : #endif
5807 9810 : if (bStreaming && !EQUAL("NONE", CSLFetchNameValueDef(papszParamList,
5808 : "COMPRESS", "NONE")))
5809 : {
5810 1 : ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
5811 : "Streaming only supported to uncompressed TIFF");
5812 1 : return nullptr;
5813 : }
5814 9809 : if (bStreaming && CPLFetchBool(papszParamList, "SPARSE_OK", false))
5815 : {
5816 1 : ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
5817 : "Streaming not supported with SPARSE_OK");
5818 1 : return nullptr;
5819 : }
5820 : const bool bCopySrcOverviews =
5821 9808 : CPLFetchBool(papszParamList, "COPY_SRC_OVERVIEWS", false);
5822 9808 : if (bStreaming && bCopySrcOverviews)
5823 : {
5824 1 : ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
5825 : "Streaming not supported with COPY_SRC_OVERVIEWS");
5826 1 : return nullptr;
5827 : }
5828 9807 : if (bStreaming)
5829 : {
5830 9 : l_osTmpFilename = VSIMemGenerateHiddenFilename("vsistdout.tif");
5831 9 : pszFilename = l_osTmpFilename.c_str();
5832 : }
5833 :
5834 : /* -------------------------------------------------------------------- */
5835 : /* Compute the uncompressed size. */
5836 : /* -------------------------------------------------------------------- */
5837 9807 : const unsigned nTileXCount =
5838 9807 : bTiled ? DIV_ROUND_UP(nXSize, l_nBlockXSize) : 0;
5839 9807 : const unsigned nTileYCount =
5840 9807 : bTiled ? DIV_ROUND_UP(nYSize, l_nBlockYSize) : 0;
5841 : const double dfUncompressedImageSize =
5842 9807 : (bTiled ? (static_cast<double>(nTileXCount) * nTileYCount *
5843 791 : l_nBlockXSize * l_nBlockYSize)
5844 9016 : : (nXSize * static_cast<double>(nYSize))) *
5845 9807 : l_nBands * GDALGetDataTypeSizeBytes(eType) +
5846 9807 : dfExtraSpaceForOverviews;
5847 :
5848 : /* -------------------------------------------------------------------- */
5849 : /* Should the file be created as a bigtiff file? */
5850 : /* -------------------------------------------------------------------- */
5851 9807 : const char *pszBIGTIFF = CSLFetchNameValue(papszParamList, "BIGTIFF");
5852 :
5853 9807 : if (pszBIGTIFF == nullptr)
5854 9378 : pszBIGTIFF = "IF_NEEDED";
5855 :
5856 9807 : bool bCreateBigTIFF = false;
5857 9807 : if (EQUAL(pszBIGTIFF, "IF_NEEDED"))
5858 : {
5859 9379 : if (l_nCompression == COMPRESSION_NONE &&
5860 : dfUncompressedImageSize > 4200000000.0)
5861 17 : bCreateBigTIFF = true;
5862 : }
5863 428 : else if (EQUAL(pszBIGTIFF, "IF_SAFER"))
5864 : {
5865 407 : if (dfUncompressedImageSize > 2000000000.0)
5866 1 : bCreateBigTIFF = true;
5867 : }
5868 : else
5869 : {
5870 21 : bCreateBigTIFF = CPLTestBool(pszBIGTIFF);
5871 21 : if (!bCreateBigTIFF && l_nCompression == COMPRESSION_NONE &&
5872 : dfUncompressedImageSize > 4200000000.0)
5873 : {
5874 2 : ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
5875 : "The TIFF file will be larger than 4GB, so BigTIFF is "
5876 : "necessary. Creation failed.");
5877 2 : return nullptr;
5878 : }
5879 : }
5880 :
5881 9805 : if (bCreateBigTIFF)
5882 35 : CPLDebug("GTiff", "File being created as a BigTIFF.");
5883 :
5884 : /* -------------------------------------------------------------------- */
5885 : /* Sanity check. */
5886 : /* -------------------------------------------------------------------- */
5887 9805 : if (bTiled)
5888 : {
5889 : // libtiff implementation limitation
5890 791 : if (nTileXCount > 0x80000000U / (bCreateBigTIFF ? 8 : 4) / nTileYCount)
5891 : {
5892 3 : ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
5893 : "File too large regarding tile size. This would result "
5894 : "in a file with tile arrays larger than 2GB");
5895 3 : return nullptr;
5896 : }
5897 : }
5898 :
5899 : /* -------------------------------------------------------------------- */
5900 : /* Check free space (only for big, non sparse) */
5901 : /* -------------------------------------------------------------------- */
5902 9802 : const double dfLikelyFloorOfFinalSize =
5903 : l_nCompression == COMPRESSION_NONE
5904 9802 : ? dfUncompressedImageSize
5905 : :
5906 : /* For compressed, we target 1% as the most optimistic reduction factor! */
5907 : 0.01 * dfUncompressedImageSize;
5908 9824 : if (dfLikelyFloorOfFinalSize >= 1e9 &&
5909 22 : !CPLFetchBool(papszParamList, "SPARSE_OK", false) &&
5910 5 : osOriFilename != "/vsistdout/" &&
5911 9829 : osOriFilename != "/vsistdout_redirect/" &&
5912 5 : CPLTestBool(CPLGetConfigOption("CHECK_DISK_FREE_SPACE", "TRUE")))
5913 : {
5914 : const GIntBig nFreeDiskSpace =
5915 4 : VSIGetDiskFreeSpace(CPLGetDirnameSafe(pszFilename).c_str());
5916 4 : if (nFreeDiskSpace >= 0 && nFreeDiskSpace < dfLikelyFloorOfFinalSize)
5917 : {
5918 6 : ReportError(
5919 : pszFilename, CE_Failure, CPLE_FileIO,
5920 : "Free disk space available is %s, "
5921 : "whereas %s are %s necessary. "
5922 : "You can disable this check by defining the "
5923 : "CHECK_DISK_FREE_SPACE configuration option to FALSE.",
5924 4 : CPLFormatReadableFileSize(static_cast<uint64_t>(nFreeDiskSpace))
5925 : .c_str(),
5926 4 : CPLFormatReadableFileSize(dfLikelyFloorOfFinalSize).c_str(),
5927 : l_nCompression == COMPRESSION_NONE
5928 : ? "at least"
5929 : : "likely at least (probably more)");
5930 2 : return nullptr;
5931 : }
5932 : }
5933 :
5934 : /* -------------------------------------------------------------------- */
5935 : /* Check if the user wishes a particular endianness */
5936 : /* -------------------------------------------------------------------- */
5937 :
5938 9800 : int eEndianness = ENDIANNESS_NATIVE;
5939 9800 : const char *pszEndianness = CSLFetchNameValue(papszParamList, "ENDIANNESS");
5940 9800 : if (pszEndianness == nullptr)
5941 9737 : pszEndianness = CPLGetConfigOption("GDAL_TIFF_ENDIANNESS", nullptr);
5942 9800 : if (pszEndianness != nullptr)
5943 : {
5944 123 : if (EQUAL(pszEndianness, "LITTLE"))
5945 : {
5946 36 : eEndianness = ENDIANNESS_LITTLE;
5947 : }
5948 87 : else if (EQUAL(pszEndianness, "BIG"))
5949 : {
5950 1 : eEndianness = ENDIANNESS_BIG;
5951 : }
5952 86 : else if (EQUAL(pszEndianness, "INVERTED"))
5953 : {
5954 : #ifdef CPL_LSB
5955 82 : eEndianness = ENDIANNESS_BIG;
5956 : #else
5957 : eEndianness = ENDIANNESS_LITTLE;
5958 : #endif
5959 : }
5960 4 : else if (!EQUAL(pszEndianness, "NATIVE"))
5961 : {
5962 1 : ReportError(pszFilename, CE_Warning, CPLE_NotSupported,
5963 : "ENDIANNESS=%s not supported. Defaulting to NATIVE",
5964 : pszEndianness);
5965 : }
5966 : }
5967 :
5968 : /* -------------------------------------------------------------------- */
5969 : /* Try opening the dataset. */
5970 : /* -------------------------------------------------------------------- */
5971 :
5972 : const bool bAppend =
5973 9800 : CPLFetchBool(papszParamList, "APPEND_SUBDATASET", false);
5974 :
5975 9800 : char szOpeningFlag[5] = {};
5976 9800 : strcpy(szOpeningFlag, bAppend ? "r+" : "w+");
5977 9800 : if (bCreateBigTIFF)
5978 32 : strcat(szOpeningFlag, "8");
5979 9800 : if (eEndianness == ENDIANNESS_BIG)
5980 83 : strcat(szOpeningFlag, "b");
5981 9717 : else if (eEndianness == ENDIANNESS_LITTLE)
5982 36 : strcat(szOpeningFlag, "l");
5983 :
5984 9800 : VSIErrorReset();
5985 9800 : const bool bOnlyVisibleAtCloseTime = CPLTestBool(CSLFetchNameValueDef(
5986 : papszParamList, "@CREATE_ONLY_VISIBLE_AT_CLOSE_TIME", "NO"));
5987 9800 : const bool bSuppressASAP = CPLTestBool(
5988 : CSLFetchNameValueDef(papszParamList, "@SUPPRESS_ASAP", "NO"));
5989 : auto l_fpL =
5990 9800 : (bOnlyVisibleAtCloseTime || bSuppressASAP) && !bAppend
5991 9868 : ? VSIFileManager::GetHandler(pszFilename)
5992 136 : ->CreateOnlyVisibleAtCloseTime(pszFilename, true, nullptr)
5993 68 : .release()
5994 19532 : : VSIFilesystemHandler::OpenStatic(pszFilename,
5995 : bAppend ? "r+b" : "w+b", true)
5996 9800 : .release();
5997 9800 : if (l_fpL == nullptr)
5998 : {
5999 21 : VSIToCPLErrorWithMsg(CE_Failure, CPLE_OpenFailed,
6000 42 : std::string("Attempt to create new tiff file `")
6001 21 : .append(pszFilename)
6002 21 : .append("' failed")
6003 : .c_str());
6004 21 : return nullptr;
6005 : }
6006 :
6007 9779 : if (bSuppressASAP)
6008 : {
6009 38 : l_fpL->CancelCreation();
6010 : }
6011 :
6012 9779 : TIFF *l_hTIFF = VSI_TIFFOpen(pszFilename, szOpeningFlag, l_fpL);
6013 9779 : if (l_hTIFF == nullptr)
6014 : {
6015 2 : if (CPLGetLastErrorNo() == 0)
6016 0 : CPLError(CE_Failure, CPLE_OpenFailed,
6017 : "Attempt to create new tiff file `%s' "
6018 : "failed in XTIFFOpen().",
6019 : pszFilename);
6020 2 : l_fpL->CancelCreation();
6021 2 : CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
6022 2 : return nullptr;
6023 : }
6024 :
6025 9777 : if (bAppend)
6026 : {
6027 : #if !(defined(INTERNAL_LIBTIFF) || TIFFLIB_VERSION > 20240911)
6028 : // This is a bit of a hack to cause (*tif->tif_cleanup)(tif); to be
6029 : // called. See https://trac.osgeo.org/gdal/ticket/2055
6030 : // Fixed in libtiff > 4.7.0
6031 : TIFFSetField(l_hTIFF, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
6032 : TIFFFreeDirectory(l_hTIFF);
6033 : #endif
6034 6 : TIFFCreateDirectory(l_hTIFF);
6035 : }
6036 :
6037 : /* -------------------------------------------------------------------- */
6038 : /* Do we have a custom pixel type (just used for signed byte now). */
6039 : /* -------------------------------------------------------------------- */
6040 9777 : const char *pszPixelType = CSLFetchNameValue(papszParamList, "PIXELTYPE");
6041 9777 : if (pszPixelType == nullptr)
6042 9769 : pszPixelType = "";
6043 9777 : if (eType == GDT_UInt8 && EQUAL(pszPixelType, "SIGNEDBYTE"))
6044 : {
6045 8 : CPLError(CE_Warning, CPLE_AppDefined,
6046 : "Using PIXELTYPE=SIGNEDBYTE with Byte data type is deprecated "
6047 : "(but still works). "
6048 : "Using Int8 data type instead is now recommended.");
6049 : }
6050 :
6051 : /* -------------------------------------------------------------------- */
6052 : /* Setup some standard flags. */
6053 : /* -------------------------------------------------------------------- */
6054 9777 : TIFFSetField(l_hTIFF, TIFFTAG_IMAGEWIDTH, nXSize);
6055 9777 : TIFFSetField(l_hTIFF, TIFFTAG_IMAGELENGTH, nYSize);
6056 9777 : TIFFSetField(l_hTIFF, TIFFTAG_BITSPERSAMPLE, l_nBitsPerSample);
6057 :
6058 9777 : uint16_t l_nSampleFormat = 0;
6059 9777 : if ((eType == GDT_UInt8 && EQUAL(pszPixelType, "SIGNEDBYTE")) ||
6060 9628 : eType == GDT_Int8 || eType == GDT_Int16 || eType == GDT_Int32 ||
6061 : eType == GDT_Int64)
6062 807 : l_nSampleFormat = SAMPLEFORMAT_INT;
6063 8970 : else if (eType == GDT_CInt16 || eType == GDT_CInt32)
6064 363 : l_nSampleFormat = SAMPLEFORMAT_COMPLEXINT;
6065 8607 : else if (eType == GDT_Float16 || eType == GDT_Float32 ||
6066 : eType == GDT_Float64)
6067 1146 : l_nSampleFormat = SAMPLEFORMAT_IEEEFP;
6068 7461 : else if (eType == GDT_CFloat16 || eType == GDT_CFloat32 ||
6069 : eType == GDT_CFloat64)
6070 471 : l_nSampleFormat = SAMPLEFORMAT_COMPLEXIEEEFP;
6071 : else
6072 6990 : l_nSampleFormat = SAMPLEFORMAT_UINT;
6073 :
6074 9777 : TIFFSetField(l_hTIFF, TIFFTAG_SAMPLEFORMAT, l_nSampleFormat);
6075 9777 : TIFFSetField(l_hTIFF, TIFFTAG_SAMPLESPERPIXEL, l_nBands);
6076 9777 : TIFFSetField(l_hTIFF, TIFFTAG_PLANARCONFIG, nPlanar);
6077 :
6078 : /* -------------------------------------------------------------------- */
6079 : /* Setup Photometric Interpretation. Take this value from the user */
6080 : /* passed option or guess correct value otherwise. */
6081 : /* -------------------------------------------------------------------- */
6082 9777 : int nSamplesAccountedFor = 1;
6083 9777 : bool bForceColorTable = false;
6084 :
6085 9777 : if (const char *pszValue = CSLFetchNameValue(papszParamList, "PHOTOMETRIC"))
6086 : {
6087 1906 : if (EQUAL(pszValue, "MINISBLACK"))
6088 14 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
6089 1892 : else if (EQUAL(pszValue, "MINISWHITE"))
6090 : {
6091 2 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
6092 : }
6093 1890 : else if (EQUAL(pszValue, "PALETTE"))
6094 : {
6095 5 : if (eType == GDT_UInt8 || eType == GDT_UInt16)
6096 : {
6097 4 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
6098 4 : nSamplesAccountedFor = 1;
6099 4 : bForceColorTable = true;
6100 : }
6101 : else
6102 : {
6103 1 : ReportError(
6104 : pszFilename, CE_Warning, CPLE_AppDefined,
6105 : "PHOTOMETRIC=PALETTE only compatible with Byte or UInt16");
6106 : }
6107 : }
6108 1885 : else if (EQUAL(pszValue, "RGB"))
6109 : {
6110 1145 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
6111 1145 : nSamplesAccountedFor = 3;
6112 : }
6113 740 : else if (EQUAL(pszValue, "CMYK"))
6114 : {
6115 10 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_SEPARATED);
6116 10 : nSamplesAccountedFor = 4;
6117 : }
6118 730 : else if (EQUAL(pszValue, "YCBCR"))
6119 : {
6120 : // Because of subsampling, setting YCBCR without JPEG compression
6121 : // leads to a crash currently. Would need to make
6122 : // GTiffRasterBand::IWriteBlock() aware of subsampling so that it
6123 : // doesn't overrun buffer size returned by libtiff.
6124 729 : if (l_nCompression != COMPRESSION_JPEG)
6125 : {
6126 1 : ReportError(
6127 : pszFilename, CE_Failure, CPLE_NotSupported,
6128 : "Currently, PHOTOMETRIC=YCBCR requires COMPRESS=JPEG");
6129 1 : XTIFFClose(l_hTIFF);
6130 1 : l_fpL->CancelCreation();
6131 1 : CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
6132 1 : return nullptr;
6133 : }
6134 :
6135 728 : if (nPlanar == PLANARCONFIG_SEPARATE)
6136 : {
6137 1 : ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
6138 : "PHOTOMETRIC=YCBCR requires INTERLEAVE=PIXEL");
6139 1 : XTIFFClose(l_hTIFF);
6140 1 : l_fpL->CancelCreation();
6141 1 : CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
6142 1 : return nullptr;
6143 : }
6144 :
6145 : // YCBCR strictly requires 3 bands. Not less, not more Issue an
6146 : // explicit error message as libtiff one is a bit cryptic:
6147 : // TIFFVStripSize64:Invalid td_samplesperpixel value.
6148 727 : if (l_nBands != 3)
6149 : {
6150 1 : ReportError(
6151 : pszFilename, CE_Failure, CPLE_NotSupported,
6152 : "PHOTOMETRIC=YCBCR not supported on a %d-band raster: "
6153 : "only compatible of a 3-band (RGB) raster",
6154 : l_nBands);
6155 1 : XTIFFClose(l_hTIFF);
6156 1 : l_fpL->CancelCreation();
6157 1 : CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
6158 1 : return nullptr;
6159 : }
6160 :
6161 726 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);
6162 726 : nSamplesAccountedFor = 3;
6163 :
6164 : // Explicitly register the subsampling so that JPEGFixupTags
6165 : // is a no-op (helps for cloud optimized geotiffs)
6166 726 : TIFFSetField(l_hTIFF, TIFFTAG_YCBCRSUBSAMPLING, 2, 2);
6167 : }
6168 1 : else if (EQUAL(pszValue, "CIELAB"))
6169 : {
6170 0 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_CIELAB);
6171 0 : nSamplesAccountedFor = 3;
6172 : }
6173 1 : else if (EQUAL(pszValue, "ICCLAB"))
6174 : {
6175 0 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_ICCLAB);
6176 0 : nSamplesAccountedFor = 3;
6177 : }
6178 1 : else if (EQUAL(pszValue, "ITULAB"))
6179 : {
6180 0 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_ITULAB);
6181 0 : nSamplesAccountedFor = 3;
6182 : }
6183 : else
6184 : {
6185 1 : ReportError(pszFilename, CE_Warning, CPLE_IllegalArg,
6186 : "PHOTOMETRIC=%s value not recognised, ignoring. "
6187 : "Set the Photometric Interpretation as MINISBLACK.",
6188 : pszValue);
6189 1 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
6190 : }
6191 :
6192 1903 : if (l_nBands < nSamplesAccountedFor)
6193 : {
6194 1 : ReportError(pszFilename, CE_Warning, CPLE_IllegalArg,
6195 : "PHOTOMETRIC=%s value does not correspond to number "
6196 : "of bands (%d), ignoring. "
6197 : "Set the Photometric Interpretation as MINISBLACK.",
6198 : pszValue, l_nBands);
6199 1 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
6200 : }
6201 : }
6202 : else
6203 : {
6204 : // If image contains 3 or 4 bands and datatype is Byte then we will
6205 : // assume it is RGB. In all other cases assume it is MINISBLACK.
6206 7871 : if (l_nBands == 3 && eType == GDT_UInt8)
6207 : {
6208 318 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
6209 318 : nSamplesAccountedFor = 3;
6210 : }
6211 7553 : else if (l_nBands == 4 && eType == GDT_UInt8)
6212 : {
6213 : uint16_t v[1] = {
6214 718 : GTiffGetAlphaValue(CSLFetchNameValue(papszParamList, "ALPHA"),
6215 718 : DEFAULT_ALPHA_TYPE)};
6216 :
6217 718 : TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, 1, v);
6218 718 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
6219 718 : nSamplesAccountedFor = 4;
6220 : }
6221 : else
6222 : {
6223 6835 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
6224 6835 : nSamplesAccountedFor = 1;
6225 : }
6226 : }
6227 :
6228 : /* -------------------------------------------------------------------- */
6229 : /* If there are extra samples, we need to mark them with an */
6230 : /* appropriate extrasamples definition here. */
6231 : /* -------------------------------------------------------------------- */
6232 9774 : if (l_nBands > nSamplesAccountedFor)
6233 : {
6234 1382 : const int nExtraSamples = l_nBands - nSamplesAccountedFor;
6235 :
6236 : uint16_t *v = static_cast<uint16_t *>(
6237 1382 : CPLMalloc(sizeof(uint16_t) * nExtraSamples));
6238 :
6239 1382 : v[0] = GTiffGetAlphaValue(CSLFetchNameValue(papszParamList, "ALPHA"),
6240 : EXTRASAMPLE_UNSPECIFIED);
6241 :
6242 297693 : for (int i = 1; i < nExtraSamples; ++i)
6243 296311 : v[i] = EXTRASAMPLE_UNSPECIFIED;
6244 :
6245 1382 : TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, nExtraSamples, v);
6246 :
6247 1382 : CPLFree(v);
6248 : }
6249 :
6250 : // Set the ICC color profile.
6251 9774 : if (eProfile != GTiffProfile::BASELINE)
6252 : {
6253 9749 : SaveICCProfile(nullptr, l_hTIFF, papszParamList, l_nBitsPerSample);
6254 : }
6255 :
6256 : // Set the compression method before asking the default strip size
6257 : // This is useful when translating to a JPEG-In-TIFF file where
6258 : // the default strip size is 8 or 16 depending on the photometric value.
6259 9774 : TIFFSetField(l_hTIFF, TIFFTAG_COMPRESSION, l_nCompression);
6260 :
6261 9774 : if (l_nCompression == COMPRESSION_LERC)
6262 : {
6263 : const char *pszCompress =
6264 97 : CSLFetchNameValueDef(papszParamList, "COMPRESS", "");
6265 97 : if (EQUAL(pszCompress, "LERC_DEFLATE"))
6266 : {
6267 16 : TIFFSetField(l_hTIFF, TIFFTAG_LERC_ADD_COMPRESSION,
6268 : LERC_ADD_COMPRESSION_DEFLATE);
6269 : }
6270 81 : else if (EQUAL(pszCompress, "LERC_ZSTD"))
6271 : {
6272 14 : if (TIFFSetField(l_hTIFF, TIFFTAG_LERC_ADD_COMPRESSION,
6273 14 : LERC_ADD_COMPRESSION_ZSTD) != 1)
6274 : {
6275 0 : XTIFFClose(l_hTIFF);
6276 0 : l_fpL->CancelCreation();
6277 0 : CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
6278 0 : return nullptr;
6279 : }
6280 : }
6281 : }
6282 : // TODO later: take into account LERC version
6283 :
6284 : /* -------------------------------------------------------------------- */
6285 : /* Setup tiling/stripping flags. */
6286 : /* -------------------------------------------------------------------- */
6287 9774 : if (bTiled)
6288 : {
6289 1562 : if (!TIFFSetField(l_hTIFF, TIFFTAG_TILEWIDTH, l_nBlockXSize) ||
6290 781 : !TIFFSetField(l_hTIFF, TIFFTAG_TILELENGTH, l_nBlockYSize))
6291 : {
6292 0 : XTIFFClose(l_hTIFF);
6293 0 : l_fpL->CancelCreation();
6294 0 : CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
6295 0 : return nullptr;
6296 : }
6297 : }
6298 : else
6299 : {
6300 8993 : const uint32_t l_nRowsPerStrip = std::min(
6301 : nYSize, l_nBlockYSize == 0
6302 8993 : ? static_cast<int>(TIFFDefaultStripSize(l_hTIFF, 0))
6303 8993 : : l_nBlockYSize);
6304 :
6305 8993 : TIFFSetField(l_hTIFF, TIFFTAG_ROWSPERSTRIP, l_nRowsPerStrip);
6306 : }
6307 :
6308 : /* -------------------------------------------------------------------- */
6309 : /* Set compression related tags. */
6310 : /* -------------------------------------------------------------------- */
6311 9774 : if (GTIFFSupportsPredictor(l_nCompression))
6312 943 : TIFFSetField(l_hTIFF, TIFFTAG_PREDICTOR, nPredictor);
6313 9774 : if (l_nCompression == COMPRESSION_ADOBE_DEFLATE ||
6314 : l_nCompression == COMPRESSION_LERC)
6315 : {
6316 280 : GTiffSetDeflateSubCodec(l_hTIFF);
6317 :
6318 280 : if (l_nZLevel != -1)
6319 22 : TIFFSetField(l_hTIFF, TIFFTAG_ZIPQUALITY, l_nZLevel);
6320 : }
6321 9774 : if (l_nCompression == COMPRESSION_JPEG && l_nJpegQuality != -1)
6322 1905 : TIFFSetField(l_hTIFF, TIFFTAG_JPEGQUALITY, l_nJpegQuality);
6323 9774 : if (l_nCompression == COMPRESSION_LZMA && l_nLZMAPreset != -1)
6324 10 : TIFFSetField(l_hTIFF, TIFFTAG_LZMAPRESET, l_nLZMAPreset);
6325 9774 : if ((l_nCompression == COMPRESSION_ZSTD ||
6326 190 : l_nCompression == COMPRESSION_LERC) &&
6327 : l_nZSTDLevel != -1)
6328 12 : TIFFSetField(l_hTIFF, TIFFTAG_ZSTD_LEVEL, l_nZSTDLevel);
6329 9774 : if (l_nCompression == COMPRESSION_LERC)
6330 : {
6331 97 : TIFFSetField(l_hTIFF, TIFFTAG_LERC_MAXZERROR, l_dfMaxZError);
6332 : }
6333 : #if HAVE_JXL
6334 9774 : if (l_nCompression == COMPRESSION_JXL ||
6335 : l_nCompression == COMPRESSION_JXL_DNG_1_7)
6336 : {
6337 104 : TIFFSetField(l_hTIFF, TIFFTAG_JXL_LOSSYNESS,
6338 : l_bJXLLossless ? JXL_LOSSLESS : JXL_LOSSY);
6339 104 : TIFFSetField(l_hTIFF, TIFFTAG_JXL_EFFORT, l_nJXLEffort);
6340 104 : TIFFSetField(l_hTIFF, TIFFTAG_JXL_DISTANCE,
6341 : static_cast<double>(l_fJXLDistance));
6342 104 : TIFFSetField(l_hTIFF, TIFFTAG_JXL_ALPHA_DISTANCE,
6343 : static_cast<double>(l_fJXLAlphaDistance));
6344 : }
6345 : #endif
6346 9774 : if (l_nCompression == COMPRESSION_WEBP)
6347 33 : TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LEVEL, l_nWebPLevel);
6348 9774 : if (l_nCompression == COMPRESSION_WEBP && l_bWebPLossless)
6349 7 : TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LOSSLESS, 1);
6350 :
6351 9774 : if (l_nCompression == COMPRESSION_JPEG)
6352 2083 : TIFFSetField(l_hTIFF, TIFFTAG_JPEGTABLESMODE, l_nJpegTablesMode);
6353 :
6354 : /* -------------------------------------------------------------------- */
6355 : /* If we forced production of a file with photometric=palette, */
6356 : /* we need to push out a default color table. */
6357 : /* -------------------------------------------------------------------- */
6358 9774 : if (bForceColorTable)
6359 : {
6360 4 : const int nColors = eType == GDT_UInt8 ? 256 : 65536;
6361 :
6362 : unsigned short *panTRed = static_cast<unsigned short *>(
6363 4 : CPLMalloc(sizeof(unsigned short) * nColors));
6364 : unsigned short *panTGreen = static_cast<unsigned short *>(
6365 4 : CPLMalloc(sizeof(unsigned short) * nColors));
6366 : unsigned short *panTBlue = static_cast<unsigned short *>(
6367 4 : CPLMalloc(sizeof(unsigned short) * nColors));
6368 :
6369 1028 : for (int iColor = 0; iColor < nColors; ++iColor)
6370 : {
6371 1024 : if (eType == GDT_UInt8)
6372 : {
6373 1024 : panTRed[iColor] = GTiffDataset::ClampCTEntry(
6374 : iColor, 1, iColor, nColorTableMultiplier);
6375 1024 : panTGreen[iColor] = GTiffDataset::ClampCTEntry(
6376 : iColor, 2, iColor, nColorTableMultiplier);
6377 1024 : panTBlue[iColor] = GTiffDataset::ClampCTEntry(
6378 : iColor, 3, iColor, nColorTableMultiplier);
6379 : }
6380 : else
6381 : {
6382 0 : panTRed[iColor] = static_cast<unsigned short>(iColor);
6383 0 : panTGreen[iColor] = static_cast<unsigned short>(iColor);
6384 0 : panTBlue[iColor] = static_cast<unsigned short>(iColor);
6385 : }
6386 : }
6387 :
6388 4 : TIFFSetField(l_hTIFF, TIFFTAG_COLORMAP, panTRed, panTGreen, panTBlue);
6389 :
6390 4 : CPLFree(panTRed);
6391 4 : CPLFree(panTGreen);
6392 4 : CPLFree(panTBlue);
6393 : }
6394 :
6395 : // This trick
6396 : // creates a temporary in-memory file and fetches its JPEG tables so that
6397 : // we can directly set them, before tif_jpeg.c compute them at the first
6398 : // strip/tile writing, which is too late, since we have already crystalized
6399 : // the directory. This way we avoid a directory rewriting.
6400 11857 : if (l_nCompression == COMPRESSION_JPEG &&
6401 2083 : CPLTestBool(
6402 : CSLFetchNameValueDef(papszParamList, "WRITE_JPEGTABLE_TAG", "YES")))
6403 : {
6404 1014 : GTiffWriteJPEGTables(
6405 : l_hTIFF, CSLFetchNameValue(papszParamList, "PHOTOMETRIC"),
6406 : CSLFetchNameValue(papszParamList, "JPEG_QUALITY"),
6407 : CSLFetchNameValue(papszParamList, "JPEGTABLESMODE"));
6408 : }
6409 :
6410 9774 : *pfpL = l_fpL;
6411 :
6412 9774 : return l_hTIFF;
6413 : }
6414 :
6415 : /************************************************************************/
6416 : /* GuessJPEGQuality() */
6417 : /* */
6418 : /* Guess JPEG quality from JPEGTABLES tag. */
6419 : /************************************************************************/
6420 :
6421 3850 : static const GByte *GTIFFFindNextTable(const GByte *paby, GByte byMarker,
6422 : int nLen, int *pnLenTable)
6423 : {
6424 7967 : for (int i = 0; i + 1 < nLen;)
6425 : {
6426 7967 : if (paby[i] != 0xFF)
6427 0 : return nullptr;
6428 7967 : ++i;
6429 7967 : if (paby[i] == 0xD8)
6430 : {
6431 3117 : ++i;
6432 3117 : continue;
6433 : }
6434 4850 : if (i + 2 >= nLen)
6435 833 : return nullptr;
6436 4017 : int nMarkerLen = paby[i + 1] * 256 + paby[i + 2];
6437 4017 : if (i + 1 + nMarkerLen >= nLen)
6438 0 : return nullptr;
6439 4017 : if (paby[i] == byMarker)
6440 : {
6441 3017 : if (pnLenTable)
6442 2473 : *pnLenTable = nMarkerLen;
6443 3017 : return paby + i + 1;
6444 : }
6445 1000 : i += 1 + nMarkerLen;
6446 : }
6447 0 : return nullptr;
6448 : }
6449 :
6450 : constexpr GByte MARKER_HUFFMAN_TABLE = 0xC4;
6451 : constexpr GByte MARKER_QUANT_TABLE = 0xDB;
6452 :
6453 : // We assume that if there are several quantization tables, they are
6454 : // in the same order. Which is a reasonable assumption for updating
6455 : // a file generated by ourselves.
6456 904 : static bool GTIFFQuantizationTablesEqual(const GByte *paby1, int nLen1,
6457 : const GByte *paby2, int nLen2)
6458 : {
6459 904 : bool bFound = false;
6460 : while (true)
6461 : {
6462 945 : int nLenTable1 = 0;
6463 945 : int nLenTable2 = 0;
6464 : const GByte *paby1New =
6465 945 : GTIFFFindNextTable(paby1, MARKER_QUANT_TABLE, nLen1, &nLenTable1);
6466 : const GByte *paby2New =
6467 945 : GTIFFFindNextTable(paby2, MARKER_QUANT_TABLE, nLen2, &nLenTable2);
6468 945 : if (paby1New == nullptr && paby2New == nullptr)
6469 904 : return bFound;
6470 911 : if (paby1New == nullptr || paby2New == nullptr)
6471 0 : return false;
6472 911 : if (nLenTable1 != nLenTable2)
6473 207 : return false;
6474 704 : if (memcmp(paby1New, paby2New, nLenTable1) != 0)
6475 663 : return false;
6476 41 : paby1New += nLenTable1;
6477 41 : paby2New += nLenTable2;
6478 41 : nLen1 -= static_cast<int>(paby1New - paby1);
6479 41 : nLen2 -= static_cast<int>(paby2New - paby2);
6480 41 : paby1 = paby1New;
6481 41 : paby2 = paby2New;
6482 41 : bFound = true;
6483 41 : }
6484 : }
6485 :
6486 : // Guess the JPEG quality by comparing against the MD5Sum of precomputed
6487 : // quantization tables
6488 409 : static int GuessJPEGQualityFromMD5(const uint8_t md5JPEGQuantTable[][16],
6489 : const GByte *const pabyJPEGTable,
6490 : int nJPEGTableSize)
6491 : {
6492 409 : int nRemainingLen = nJPEGTableSize;
6493 409 : const GByte *pabyCur = pabyJPEGTable;
6494 :
6495 : struct CPLMD5Context context;
6496 409 : CPLMD5Init(&context);
6497 :
6498 : while (true)
6499 : {
6500 1060 : int nLenTable = 0;
6501 1060 : const GByte *pabyNew = GTIFFFindNextTable(pabyCur, MARKER_QUANT_TABLE,
6502 : nRemainingLen, &nLenTable);
6503 1060 : if (pabyNew == nullptr)
6504 409 : break;
6505 651 : CPLMD5Update(&context, pabyNew, nLenTable);
6506 651 : pabyNew += nLenTable;
6507 651 : nRemainingLen -= static_cast<int>(pabyNew - pabyCur);
6508 651 : pabyCur = pabyNew;
6509 651 : }
6510 :
6511 : GByte digest[16];
6512 409 : CPLMD5Final(digest, &context);
6513 :
6514 28846 : for (int i = 0; i < 100; i++)
6515 : {
6516 28843 : if (memcmp(md5JPEGQuantTable[i], digest, 16) == 0)
6517 : {
6518 406 : return i + 1;
6519 : }
6520 : }
6521 3 : return -1;
6522 : }
6523 :
6524 464 : int GTiffDataset::GuessJPEGQuality(bool &bOutHasQuantizationTable,
6525 : bool &bOutHasHuffmanTable)
6526 : {
6527 464 : CPLAssert(m_nCompression == COMPRESSION_JPEG);
6528 464 : uint32_t nJPEGTableSize = 0;
6529 464 : void *pJPEGTable = nullptr;
6530 464 : if (!TIFFGetField(m_hTIFF, TIFFTAG_JPEGTABLES, &nJPEGTableSize,
6531 : &pJPEGTable))
6532 : {
6533 14 : bOutHasQuantizationTable = false;
6534 14 : bOutHasHuffmanTable = false;
6535 14 : return -1;
6536 : }
6537 :
6538 450 : bOutHasQuantizationTable =
6539 450 : GTIFFFindNextTable(static_cast<const GByte *>(pJPEGTable),
6540 : MARKER_QUANT_TABLE, nJPEGTableSize,
6541 450 : nullptr) != nullptr;
6542 450 : bOutHasHuffmanTable =
6543 450 : GTIFFFindNextTable(static_cast<const GByte *>(pJPEGTable),
6544 : MARKER_HUFFMAN_TABLE, nJPEGTableSize,
6545 450 : nullptr) != nullptr;
6546 450 : if (!bOutHasQuantizationTable)
6547 7 : return -1;
6548 :
6549 443 : if ((nBands == 1 && m_nBitsPerSample == 8) ||
6550 382 : (nBands == 3 && m_nBitsPerSample == 8 &&
6551 336 : m_nPhotometric == PHOTOMETRIC_RGB) ||
6552 288 : (nBands == 4 && m_nBitsPerSample == 8 &&
6553 27 : m_nPhotometric == PHOTOMETRIC_SEPARATED))
6554 : {
6555 167 : return GuessJPEGQualityFromMD5(md5JPEGQuantTable_generic_8bit,
6556 : static_cast<const GByte *>(pJPEGTable),
6557 167 : static_cast<int>(nJPEGTableSize));
6558 : }
6559 :
6560 276 : if (nBands == 3 && m_nBitsPerSample == 8 &&
6561 242 : m_nPhotometric == PHOTOMETRIC_YCBCR)
6562 : {
6563 : int nRet =
6564 242 : GuessJPEGQualityFromMD5(md5JPEGQuantTable_3_YCBCR_8bit,
6565 : static_cast<const GByte *>(pJPEGTable),
6566 : static_cast<int>(nJPEGTableSize));
6567 242 : if (nRet < 0)
6568 : {
6569 : // libjpeg 9e has modified the YCbCr quantization tables.
6570 : nRet =
6571 0 : GuessJPEGQualityFromMD5(md5JPEGQuantTable_3_YCBCR_8bit_jpeg9e,
6572 : static_cast<const GByte *>(pJPEGTable),
6573 : static_cast<int>(nJPEGTableSize));
6574 : }
6575 242 : return nRet;
6576 : }
6577 :
6578 34 : char **papszLocalParameters = nullptr;
6579 : papszLocalParameters =
6580 34 : CSLSetNameValue(papszLocalParameters, "COMPRESS", "JPEG");
6581 34 : if (m_nPhotometric == PHOTOMETRIC_YCBCR)
6582 : papszLocalParameters =
6583 7 : CSLSetNameValue(papszLocalParameters, "PHOTOMETRIC", "YCBCR");
6584 27 : else if (m_nPhotometric == PHOTOMETRIC_SEPARATED)
6585 : papszLocalParameters =
6586 0 : CSLSetNameValue(papszLocalParameters, "PHOTOMETRIC", "CMYK");
6587 : papszLocalParameters =
6588 34 : CSLSetNameValue(papszLocalParameters, "BLOCKYSIZE", "16");
6589 34 : if (m_nBitsPerSample == 12)
6590 : papszLocalParameters =
6591 16 : CSLSetNameValue(papszLocalParameters, "NBITS", "12");
6592 :
6593 : const CPLString osTmpFilenameIn(
6594 34 : VSIMemGenerateHiddenFilename("gtiffdataset_guess_jpeg_quality_tmp"));
6595 :
6596 34 : int nRet = -1;
6597 938 : for (int nQuality = 0; nQuality <= 100 && nRet < 0; ++nQuality)
6598 : {
6599 904 : VSILFILE *fpTmp = nullptr;
6600 904 : if (nQuality == 0)
6601 : papszLocalParameters =
6602 34 : CSLSetNameValue(papszLocalParameters, "JPEG_QUALITY", "75");
6603 : else
6604 : papszLocalParameters =
6605 870 : CSLSetNameValue(papszLocalParameters, "JPEG_QUALITY",
6606 : CPLSPrintf("%d", nQuality));
6607 :
6608 904 : CPLPushErrorHandler(CPLQuietErrorHandler);
6609 904 : CPLString osTmp;
6610 : bool bTileInterleaving;
6611 1808 : TIFF *hTIFFTmp = CreateLL(
6612 904 : osTmpFilenameIn, 16, 16, (nBands <= 4) ? nBands : 1,
6613 : GetRasterBand(1)->GetRasterDataType(), 0.0, 0, papszLocalParameters,
6614 : &fpTmp, osTmp, /* bCreateCopy=*/false, bTileInterleaving);
6615 904 : CPLPopErrorHandler();
6616 904 : if (!hTIFFTmp)
6617 : {
6618 0 : break;
6619 : }
6620 :
6621 904 : TIFFWriteCheck(hTIFFTmp, FALSE, "CreateLL");
6622 904 : TIFFWriteDirectory(hTIFFTmp);
6623 904 : TIFFSetDirectory(hTIFFTmp, 0);
6624 : // Now reset jpegcolormode.
6625 1196 : if (m_nPhotometric == PHOTOMETRIC_YCBCR &&
6626 292 : CPLTestBool(CPLGetConfigOption("CONVERT_YCBCR_TO_RGB", "YES")))
6627 : {
6628 292 : TIFFSetField(hTIFFTmp, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
6629 : }
6630 :
6631 904 : GByte abyZeroData[(16 * 16 * 4 * 3) / 2] = {};
6632 904 : const int nBlockSize =
6633 904 : (16 * 16 * ((nBands <= 4) ? nBands : 1) * m_nBitsPerSample) / 8;
6634 904 : TIFFWriteEncodedStrip(hTIFFTmp, 0, abyZeroData, nBlockSize);
6635 :
6636 904 : uint32_t nJPEGTableSizeTry = 0;
6637 904 : void *pJPEGTableTry = nullptr;
6638 904 : if (TIFFGetField(hTIFFTmp, TIFFTAG_JPEGTABLES, &nJPEGTableSizeTry,
6639 904 : &pJPEGTableTry))
6640 : {
6641 904 : if (GTIFFQuantizationTablesEqual(
6642 : static_cast<GByte *>(pJPEGTable), nJPEGTableSize,
6643 : static_cast<GByte *>(pJPEGTableTry), nJPEGTableSizeTry))
6644 : {
6645 34 : nRet = (nQuality == 0) ? 75 : nQuality;
6646 : }
6647 : }
6648 :
6649 904 : XTIFFClose(hTIFFTmp);
6650 904 : CPL_IGNORE_RET_VAL(VSIFCloseL(fpTmp));
6651 : }
6652 :
6653 34 : CSLDestroy(papszLocalParameters);
6654 34 : VSIUnlink(osTmpFilenameIn);
6655 :
6656 34 : return nRet;
6657 : }
6658 :
6659 : /************************************************************************/
6660 : /* SetJPEGQualityAndTablesModeFromFile() */
6661 : /************************************************************************/
6662 :
6663 161 : void GTiffDataset::SetJPEGQualityAndTablesModeFromFile(
6664 : int nQuality, bool bHasQuantizationTable, bool bHasHuffmanTable)
6665 : {
6666 161 : if (nQuality > 0)
6667 : {
6668 154 : CPLDebug("GTiff", "Guessed JPEG quality to be %d", nQuality);
6669 154 : m_nJpegQuality = static_cast<signed char>(nQuality);
6670 154 : TIFFSetField(m_hTIFF, TIFFTAG_JPEGQUALITY, nQuality);
6671 :
6672 : // This means we will use the quantization tables from the
6673 : // JpegTables tag.
6674 154 : m_nJpegTablesMode = JPEGTABLESMODE_QUANT;
6675 : }
6676 : else
6677 : {
6678 7 : uint32_t nJPEGTableSize = 0;
6679 7 : void *pJPEGTable = nullptr;
6680 7 : if (!TIFFGetField(m_hTIFF, TIFFTAG_JPEGTABLES, &nJPEGTableSize,
6681 : &pJPEGTable))
6682 : {
6683 4 : toff_t *panByteCounts = nullptr;
6684 8 : const int nBlockCount = m_nPlanarConfig == PLANARCONFIG_SEPARATE
6685 4 : ? m_nBlocksPerBand * nBands
6686 : : m_nBlocksPerBand;
6687 4 : if (TIFFIsTiled(m_hTIFF))
6688 1 : TIFFGetField(m_hTIFF, TIFFTAG_TILEBYTECOUNTS, &panByteCounts);
6689 : else
6690 3 : TIFFGetField(m_hTIFF, TIFFTAG_STRIPBYTECOUNTS, &panByteCounts);
6691 :
6692 4 : bool bFoundNonEmptyBlock = false;
6693 4 : if (panByteCounts != nullptr)
6694 : {
6695 56 : for (int iBlock = 0; iBlock < nBlockCount; ++iBlock)
6696 : {
6697 53 : if (panByteCounts[iBlock] != 0)
6698 : {
6699 1 : bFoundNonEmptyBlock = true;
6700 1 : break;
6701 : }
6702 : }
6703 : }
6704 4 : if (bFoundNonEmptyBlock)
6705 : {
6706 1 : CPLDebug("GTiff", "Could not guess JPEG quality. "
6707 : "JPEG tables are missing, so going in "
6708 : "TIFFTAG_JPEGTABLESMODE = 0/2 mode");
6709 : // Write quantization tables in each strile.
6710 1 : m_nJpegTablesMode = 0;
6711 : }
6712 : }
6713 : else
6714 : {
6715 3 : if (bHasQuantizationTable)
6716 : {
6717 : // FIXME in libtiff: this is likely going to cause issues
6718 : // since libtiff will reuse in each strile the number of
6719 : // the global quantization table, which is invalid.
6720 1 : CPLDebug("GTiff",
6721 : "Could not guess JPEG quality although JPEG "
6722 : "quantization tables are present, so going in "
6723 : "TIFFTAG_JPEGTABLESMODE = 0/2 mode");
6724 : }
6725 : else
6726 : {
6727 2 : CPLDebug("GTiff",
6728 : "Could not guess JPEG quality since JPEG "
6729 : "quantization tables are not present, so going in "
6730 : "TIFFTAG_JPEGTABLESMODE = 0/2 mode");
6731 : }
6732 :
6733 : // Write quantization tables in each strile.
6734 3 : m_nJpegTablesMode = 0;
6735 : }
6736 : }
6737 161 : if (bHasHuffmanTable)
6738 : {
6739 : // If there are Huffman tables in header use them, otherwise
6740 : // if we use optimized tables, libtiff will currently reuse
6741 : // the number of the Huffman tables of the header for the
6742 : // optimized version of each strile, which is illegal.
6743 23 : m_nJpegTablesMode |= JPEGTABLESMODE_HUFF;
6744 : }
6745 161 : if (m_nJpegTablesMode >= 0)
6746 159 : TIFFSetField(m_hTIFF, TIFFTAG_JPEGTABLESMODE, m_nJpegTablesMode);
6747 161 : }
6748 :
6749 : /************************************************************************/
6750 : /* Create() */
6751 : /* */
6752 : /* Create a new GeoTIFF or TIFF file. */
6753 : /************************************************************************/
6754 :
6755 5726 : GDALDataset *GTiffDataset::Create(const char *pszFilename, int nXSize,
6756 : int nYSize, int l_nBands, GDALDataType eType,
6757 : CSLConstList papszParamList)
6758 :
6759 : {
6760 5726 : VSILFILE *l_fpL = nullptr;
6761 11452 : CPLString l_osTmpFilename;
6762 :
6763 : const int nColorTableMultiplier = std::max(
6764 11452 : 1,
6765 11452 : std::min(257,
6766 5726 : atoi(CSLFetchNameValueDef(
6767 : papszParamList, "COLOR_TABLE_MULTIPLIER",
6768 5726 : CPLSPrintf("%d", DEFAULT_COLOR_TABLE_MULTIPLIER_257)))));
6769 :
6770 : /* -------------------------------------------------------------------- */
6771 : /* Create the underlying TIFF file. */
6772 : /* -------------------------------------------------------------------- */
6773 : bool bTileInterleaving;
6774 : TIFF *l_hTIFF =
6775 5726 : CreateLL(pszFilename, nXSize, nYSize, l_nBands, eType, 0,
6776 : nColorTableMultiplier, papszParamList, &l_fpL, l_osTmpFilename,
6777 : /* bCreateCopy=*/false, bTileInterleaving);
6778 5726 : const bool bStreaming = !l_osTmpFilename.empty();
6779 :
6780 5726 : if (l_hTIFF == nullptr)
6781 38 : return nullptr;
6782 :
6783 : /* -------------------------------------------------------------------- */
6784 : /* Create the new GTiffDataset object. */
6785 : /* -------------------------------------------------------------------- */
6786 11376 : auto poDS = std::make_unique<GTiffDataset>();
6787 5688 : poDS->m_hTIFF = l_hTIFF;
6788 5688 : poDS->m_fpL = l_fpL;
6789 5688 : const bool bSuppressASAP = CPLTestBool(
6790 : CSLFetchNameValueDef(papszParamList, "@SUPPRESS_ASAP", "NO"));
6791 5688 : if (bSuppressASAP)
6792 34 : poDS->MarkSuppressOnClose();
6793 5688 : if (bStreaming)
6794 : {
6795 4 : poDS->m_bStreamingOut = true;
6796 4 : poDS->m_pszTmpFilename = CPLStrdup(l_osTmpFilename);
6797 4 : poDS->m_fpToWrite = VSIFOpenL(pszFilename, "wb");
6798 4 : if (poDS->m_fpToWrite == nullptr)
6799 : {
6800 1 : VSIUnlink(l_osTmpFilename);
6801 1 : return nullptr;
6802 : }
6803 : }
6804 5687 : poDS->nRasterXSize = nXSize;
6805 5687 : poDS->nRasterYSize = nYSize;
6806 5687 : poDS->eAccess = GA_Update;
6807 :
6808 5687 : poDS->m_nColorTableMultiplier = nColorTableMultiplier;
6809 :
6810 5687 : poDS->m_bCrystalized = false;
6811 5687 : poDS->m_nSamplesPerPixel = static_cast<uint16_t>(l_nBands);
6812 5687 : poDS->m_osFilename = pszFilename;
6813 :
6814 : // Don't try to load external metadata files (#6597).
6815 5687 : poDS->m_bIMDRPCMetadataLoaded = true;
6816 :
6817 : // Avoid premature crystalization that will cause directory re-writing if
6818 : // GetProjectionRef() or GetGeoTransform() are called on the newly created
6819 : // GeoTIFF.
6820 5687 : poDS->m_bLookedForProjection = true;
6821 :
6822 5687 : TIFFGetField(l_hTIFF, TIFFTAG_SAMPLEFORMAT, &(poDS->m_nSampleFormat));
6823 5687 : TIFFGetField(l_hTIFF, TIFFTAG_PLANARCONFIG, &(poDS->m_nPlanarConfig));
6824 : // Weird that we need this, but otherwise we get a Valgrind warning on
6825 : // tiff_write_124.
6826 5687 : if (!TIFFGetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, &(poDS->m_nPhotometric)))
6827 1 : poDS->m_nPhotometric = PHOTOMETRIC_MINISBLACK;
6828 5687 : TIFFGetField(l_hTIFF, TIFFTAG_BITSPERSAMPLE, &(poDS->m_nBitsPerSample));
6829 5687 : TIFFGetField(l_hTIFF, TIFFTAG_COMPRESSION, &(poDS->m_nCompression));
6830 :
6831 5687 : if (TIFFIsTiled(l_hTIFF))
6832 : {
6833 395 : TIFFGetField(l_hTIFF, TIFFTAG_TILEWIDTH, &(poDS->m_nBlockXSize));
6834 395 : TIFFGetField(l_hTIFF, TIFFTAG_TILELENGTH, &(poDS->m_nBlockYSize));
6835 : }
6836 : else
6837 : {
6838 5292 : if (!TIFFGetField(l_hTIFF, TIFFTAG_ROWSPERSTRIP,
6839 5292 : &(poDS->m_nRowsPerStrip)))
6840 0 : poDS->m_nRowsPerStrip = 1; // Dummy value.
6841 :
6842 5292 : poDS->m_nBlockXSize = nXSize;
6843 10584 : poDS->m_nBlockYSize =
6844 5292 : std::min(static_cast<int>(poDS->m_nRowsPerStrip), nYSize);
6845 : }
6846 :
6847 5687 : if (!poDS->ComputeBlocksPerColRowAndBand(l_nBands))
6848 : {
6849 0 : poDS->m_fpL->CancelCreation();
6850 0 : return nullptr;
6851 : }
6852 :
6853 5687 : poDS->m_eProfile = GetProfile(CSLFetchNameValue(papszParamList, "PROFILE"));
6854 :
6855 : /* -------------------------------------------------------------------- */
6856 : /* YCbCr JPEG compressed images should be translated on the fly */
6857 : /* to RGB by libtiff/libjpeg unless specifically requested */
6858 : /* otherwise. */
6859 : /* -------------------------------------------------------------------- */
6860 5687 : if (poDS->m_nCompression == COMPRESSION_JPEG &&
6861 5708 : poDS->m_nPhotometric == PHOTOMETRIC_YCBCR &&
6862 21 : CPLTestBool(CPLGetConfigOption("CONVERT_YCBCR_TO_RGB", "YES")))
6863 : {
6864 21 : int nColorMode = 0;
6865 :
6866 21 : poDS->SetMetadataItem("SOURCE_COLOR_SPACE", "YCbCr", "IMAGE_STRUCTURE");
6867 42 : if (!TIFFGetField(l_hTIFF, TIFFTAG_JPEGCOLORMODE, &nColorMode) ||
6868 21 : nColorMode != JPEGCOLORMODE_RGB)
6869 21 : TIFFSetField(l_hTIFF, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
6870 : }
6871 :
6872 5687 : if (poDS->m_nCompression == COMPRESSION_LERC)
6873 : {
6874 26 : uint32_t nLercParamCount = 0;
6875 26 : uint32_t *panLercParams = nullptr;
6876 26 : if (TIFFGetField(l_hTIFF, TIFFTAG_LERC_PARAMETERS, &nLercParamCount,
6877 52 : &panLercParams) &&
6878 26 : nLercParamCount == 2)
6879 : {
6880 26 : memcpy(poDS->m_anLercAddCompressionAndVersion, panLercParams,
6881 : sizeof(poDS->m_anLercAddCompressionAndVersion));
6882 : }
6883 : }
6884 :
6885 : /* -------------------------------------------------------------------- */
6886 : /* Read palette back as a color table if it has one. */
6887 : /* -------------------------------------------------------------------- */
6888 5687 : unsigned short *panRed = nullptr;
6889 5687 : unsigned short *panGreen = nullptr;
6890 5687 : unsigned short *panBlue = nullptr;
6891 :
6892 5691 : if (poDS->m_nPhotometric == PHOTOMETRIC_PALETTE &&
6893 4 : TIFFGetField(l_hTIFF, TIFFTAG_COLORMAP, &panRed, &panGreen, &panBlue))
6894 : {
6895 :
6896 4 : poDS->m_poColorTable = std::make_unique<GDALColorTable>();
6897 :
6898 4 : const int nColorCount = 1 << poDS->m_nBitsPerSample;
6899 :
6900 1028 : for (int iColor = nColorCount - 1; iColor >= 0; iColor--)
6901 : {
6902 1024 : const GDALColorEntry oEntry = {
6903 1024 : static_cast<short>(panRed[iColor] / nColorTableMultiplier),
6904 1024 : static_cast<short>(panGreen[iColor] / nColorTableMultiplier),
6905 1024 : static_cast<short>(panBlue[iColor] / nColorTableMultiplier),
6906 1024 : static_cast<short>(255)};
6907 :
6908 1024 : poDS->m_poColorTable->SetColorEntry(iColor, &oEntry);
6909 : }
6910 : }
6911 :
6912 : /* -------------------------------------------------------------------- */
6913 : /* Do we want to ensure all blocks get written out on close to */
6914 : /* avoid sparse files? */
6915 : /* -------------------------------------------------------------------- */
6916 5687 : if (!CPLFetchBool(papszParamList, "SPARSE_OK", false))
6917 5577 : poDS->m_bFillEmptyTilesAtClosing = true;
6918 :
6919 5687 : poDS->m_bWriteEmptyTiles =
6920 6492 : bStreaming || (poDS->m_nCompression != COMPRESSION_NONE &&
6921 805 : poDS->m_bFillEmptyTilesAtClosing);
6922 : // Only required for people writing non-compressed striped files in the
6923 : // right order and wanting all tstrips to be written in the same order
6924 : // so that the end result can be memory mapped without knowledge of each
6925 : // strip offset.
6926 5687 : if (CPLTestBool(CSLFetchNameValueDef(
6927 11374 : papszParamList, "WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")) ||
6928 5687 : CPLTestBool(CSLFetchNameValueDef(
6929 : papszParamList, "@WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")))
6930 : {
6931 26 : poDS->m_bWriteEmptyTiles = true;
6932 : }
6933 :
6934 : /* -------------------------------------------------------------------- */
6935 : /* Preserve creation options for consulting later (for instance */
6936 : /* to decide if a TFW file should be written). */
6937 : /* -------------------------------------------------------------------- */
6938 5687 : poDS->m_papszCreationOptions = CSLDuplicate(papszParamList);
6939 :
6940 5687 : poDS->m_nZLevel = GTiffGetZLevel(papszParamList);
6941 5687 : poDS->m_nLZMAPreset = GTiffGetLZMAPreset(papszParamList);
6942 5687 : poDS->m_nZSTDLevel = GTiffGetZSTDPreset(papszParamList);
6943 5687 : poDS->m_nWebPLevel = GTiffGetWebPLevel(papszParamList);
6944 5687 : poDS->m_bWebPLossless = GTiffGetWebPLossless(papszParamList);
6945 5689 : if (poDS->m_nWebPLevel != 100 && poDS->m_bWebPLossless &&
6946 2 : CSLFetchNameValue(papszParamList, "WEBP_LEVEL"))
6947 : {
6948 0 : CPLError(CE_Warning, CPLE_AppDefined,
6949 : "WEBP_LEVEL is specified, but WEBP_LOSSLESS=YES. "
6950 : "WEBP_LEVEL will be ignored.");
6951 : }
6952 5687 : poDS->m_nJpegQuality = GTiffGetJpegQuality(papszParamList);
6953 5687 : poDS->m_nJpegTablesMode = GTiffGetJpegTablesMode(papszParamList);
6954 5687 : poDS->m_dfMaxZError = GTiffGetLERCMaxZError(papszParamList);
6955 5687 : poDS->m_dfMaxZErrorOverview = GTiffGetLERCMaxZErrorOverview(papszParamList);
6956 : #if HAVE_JXL
6957 5687 : poDS->m_bJXLLossless = GTiffGetJXLLossless(papszParamList);
6958 5687 : poDS->m_nJXLEffort = GTiffGetJXLEffort(papszParamList);
6959 5687 : poDS->m_fJXLDistance = GTiffGetJXLDistance(papszParamList);
6960 5687 : poDS->m_fJXLAlphaDistance = GTiffGetJXLAlphaDistance(papszParamList);
6961 : #endif
6962 5687 : poDS->InitCreationOrOpenOptions(true, papszParamList);
6963 :
6964 : /* -------------------------------------------------------------------- */
6965 : /* Create band information objects. */
6966 : /* -------------------------------------------------------------------- */
6967 308213 : for (int iBand = 0; iBand < l_nBands; ++iBand)
6968 : {
6969 371722 : if (poDS->m_nBitsPerSample == 8 || poDS->m_nBitsPerSample == 16 ||
6970 371963 : poDS->m_nBitsPerSample == 32 || poDS->m_nBitsPerSample == 64 ||
6971 241 : poDS->m_nBitsPerSample == 128)
6972 : {
6973 604922 : poDS->SetBand(iBand + 1, std::make_unique<GTiffRasterBand>(
6974 604922 : poDS.get(), iBand + 1));
6975 : }
6976 : else
6977 : {
6978 130 : poDS->SetBand(iBand + 1, std::make_unique<GTiffOddBitsBand>(
6979 65 : poDS.get(), iBand + 1));
6980 130 : poDS->GetRasterBand(iBand + 1)->SetMetadataItem(
6981 130 : "NBITS", CPLString().Printf("%d", poDS->m_nBitsPerSample),
6982 65 : "IMAGE_STRUCTURE");
6983 : }
6984 : }
6985 :
6986 5687 : poDS->GetDiscardLsbOption(papszParamList);
6987 :
6988 5687 : if (poDS->m_nPlanarConfig == PLANARCONFIG_CONTIG && l_nBands != 1)
6989 835 : poDS->SetMetadataItem("INTERLEAVE", "PIXEL", "IMAGE_STRUCTURE");
6990 : else
6991 4852 : poDS->SetMetadataItem("INTERLEAVE", "BAND", "IMAGE_STRUCTURE");
6992 :
6993 5687 : poDS->oOvManager.Initialize(poDS.get(), pszFilename);
6994 :
6995 5687 : return poDS.release();
6996 : }
6997 :
6998 : /************************************************************************/
6999 : /* CopyImageryAndMask() */
7000 : /************************************************************************/
7001 :
7002 344 : CPLErr GTiffDataset::CopyImageryAndMask(GTiffDataset *poDstDS,
7003 : GDALDataset *poSrcDS,
7004 : GDALRasterBand *poSrcMaskBand,
7005 : GDALProgressFunc pfnProgress,
7006 : void *pProgressData)
7007 : {
7008 344 : CPLErr eErr = CE_None;
7009 :
7010 344 : const auto eType = poDstDS->GetRasterBand(1)->GetRasterDataType();
7011 344 : const int nDataTypeSize = GDALGetDataTypeSizeBytes(eType);
7012 344 : const int l_nBands = poDstDS->GetRasterCount();
7013 : GByte *pBlockBuffer = static_cast<GByte *>(
7014 344 : VSI_MALLOC3_VERBOSE(poDstDS->m_nBlockXSize, poDstDS->m_nBlockYSize,
7015 : cpl::fits_on<int>(l_nBands * nDataTypeSize)));
7016 344 : if (pBlockBuffer == nullptr)
7017 : {
7018 0 : eErr = CE_Failure;
7019 : }
7020 344 : const int nYSize = poDstDS->nRasterYSize;
7021 344 : const int nXSize = poDstDS->nRasterXSize;
7022 : const bool bIsOddBand =
7023 344 : dynamic_cast<GTiffOddBitsBand *>(poDstDS->GetRasterBand(1)) != nullptr;
7024 :
7025 344 : if (poDstDS->m_poMaskDS)
7026 : {
7027 59 : CPLAssert(poDstDS->m_poMaskDS->m_nBlockXSize == poDstDS->m_nBlockXSize);
7028 59 : CPLAssert(poDstDS->m_poMaskDS->m_nBlockYSize == poDstDS->m_nBlockYSize);
7029 : }
7030 :
7031 344 : if (poDstDS->m_nPlanarConfig == PLANARCONFIG_SEPARATE &&
7032 58 : !poDstDS->m_bTileInterleave)
7033 : {
7034 45 : int iBlock = 0;
7035 45 : const int nBlocks = poDstDS->m_nBlocksPerBand *
7036 45 : (l_nBands + (poDstDS->m_poMaskDS ? 1 : 0));
7037 195 : for (int i = 0; eErr == CE_None && i < l_nBands; i++)
7038 : {
7039 345 : for (int iY = 0, nYBlock = 0; iY < nYSize && eErr == CE_None;
7040 195 : iY = ((nYSize - iY < poDstDS->m_nBlockYSize)
7041 195 : ? nYSize
7042 59 : : iY + poDstDS->m_nBlockYSize),
7043 : nYBlock++)
7044 : {
7045 : const int nReqYSize =
7046 195 : std::min(nYSize - iY, poDstDS->m_nBlockYSize);
7047 495 : for (int iX = 0, nXBlock = 0; iX < nXSize && eErr == CE_None;
7048 300 : iX = ((nXSize - iX < poDstDS->m_nBlockXSize)
7049 300 : ? nXSize
7050 155 : : iX + poDstDS->m_nBlockXSize),
7051 : nXBlock++)
7052 : {
7053 : const int nReqXSize =
7054 300 : std::min(nXSize - iX, poDstDS->m_nBlockXSize);
7055 300 : if (nReqXSize < poDstDS->m_nBlockXSize ||
7056 155 : nReqYSize < poDstDS->m_nBlockYSize)
7057 : {
7058 190 : memset(pBlockBuffer, 0,
7059 190 : static_cast<size_t>(poDstDS->m_nBlockXSize) *
7060 190 : poDstDS->m_nBlockYSize * nDataTypeSize);
7061 : }
7062 300 : eErr = poSrcDS->GetRasterBand(i + 1)->RasterIO(
7063 : GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
7064 : nReqXSize, nReqYSize, eType, nDataTypeSize,
7065 300 : static_cast<GSpacing>(nDataTypeSize) *
7066 300 : poDstDS->m_nBlockXSize,
7067 : nullptr);
7068 300 : if (eErr == CE_None)
7069 : {
7070 300 : eErr = poDstDS->WriteEncodedTileOrStrip(
7071 : iBlock, pBlockBuffer, false);
7072 : }
7073 :
7074 300 : iBlock++;
7075 600 : if (pfnProgress &&
7076 300 : !pfnProgress(static_cast<double>(iBlock) / nBlocks,
7077 : nullptr, pProgressData))
7078 : {
7079 0 : eErr = CE_Failure;
7080 : }
7081 :
7082 300 : if (poDstDS->m_bWriteError)
7083 0 : eErr = CE_Failure;
7084 : }
7085 : }
7086 : }
7087 45 : if (poDstDS->m_poMaskDS && eErr == CE_None)
7088 : {
7089 6 : int iBlockMask = 0;
7090 17 : for (int iY = 0, nYBlock = 0; iY < nYSize && eErr == CE_None;
7091 11 : iY = ((nYSize - iY < poDstDS->m_nBlockYSize)
7092 11 : ? nYSize
7093 5 : : iY + poDstDS->m_nBlockYSize),
7094 : nYBlock++)
7095 : {
7096 : const int nReqYSize =
7097 11 : std::min(nYSize - iY, poDstDS->m_nBlockYSize);
7098 49 : for (int iX = 0, nXBlock = 0; iX < nXSize && eErr == CE_None;
7099 38 : iX = ((nXSize - iX < poDstDS->m_nBlockXSize)
7100 38 : ? nXSize
7101 30 : : iX + poDstDS->m_nBlockXSize),
7102 : nXBlock++)
7103 : {
7104 : const int nReqXSize =
7105 38 : std::min(nXSize - iX, poDstDS->m_nBlockXSize);
7106 38 : if (nReqXSize < poDstDS->m_nBlockXSize ||
7107 30 : nReqYSize < poDstDS->m_nBlockYSize)
7108 : {
7109 16 : memset(pBlockBuffer, 0,
7110 16 : static_cast<size_t>(poDstDS->m_nBlockXSize) *
7111 16 : poDstDS->m_nBlockYSize);
7112 : }
7113 76 : eErr = poSrcMaskBand->RasterIO(
7114 : GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
7115 : nReqXSize, nReqYSize, GDT_UInt8, 1,
7116 38 : poDstDS->m_nBlockXSize, nullptr);
7117 38 : if (eErr == CE_None)
7118 : {
7119 : // Avoid any attempt to load from disk
7120 38 : poDstDS->m_poMaskDS->m_nLoadedBlock = iBlockMask;
7121 : eErr =
7122 38 : poDstDS->m_poMaskDS->GetRasterBand(1)->WriteBlock(
7123 : nXBlock, nYBlock, pBlockBuffer);
7124 38 : if (eErr == CE_None)
7125 38 : eErr = poDstDS->m_poMaskDS->FlushBlockBuf();
7126 : }
7127 :
7128 38 : iBlockMask++;
7129 76 : if (pfnProgress &&
7130 38 : !pfnProgress(static_cast<double>(iBlock + iBlockMask) /
7131 : nBlocks,
7132 : nullptr, pProgressData))
7133 : {
7134 0 : eErr = CE_Failure;
7135 : }
7136 :
7137 38 : if (poDstDS->m_poMaskDS->m_bWriteError)
7138 0 : eErr = CE_Failure;
7139 : }
7140 : }
7141 45 : }
7142 : }
7143 : else
7144 : {
7145 299 : int iBlock = 0;
7146 299 : const int nBlocks = poDstDS->m_nBlocksPerBand;
7147 7092 : for (int iY = 0, nYBlock = 0; iY < nYSize && eErr == CE_None;
7148 6793 : iY = ((nYSize - iY < poDstDS->m_nBlockYSize)
7149 6793 : ? nYSize
7150 6569 : : iY + poDstDS->m_nBlockYSize),
7151 : nYBlock++)
7152 : {
7153 6793 : const int nReqYSize = std::min(nYSize - iY, poDstDS->m_nBlockYSize);
7154 26509 : for (int iX = 0, nXBlock = 0; iX < nXSize && eErr == CE_None;
7155 19716 : iX = ((nXSize - iX < poDstDS->m_nBlockXSize)
7156 19716 : ? nXSize
7157 19420 : : iX + poDstDS->m_nBlockXSize),
7158 : nXBlock++)
7159 : {
7160 : const int nReqXSize =
7161 19716 : std::min(nXSize - iX, poDstDS->m_nBlockXSize);
7162 19716 : if (nReqXSize < poDstDS->m_nBlockXSize ||
7163 19420 : nReqYSize < poDstDS->m_nBlockYSize)
7164 : {
7165 465 : memset(pBlockBuffer, 0,
7166 465 : static_cast<size_t>(poDstDS->m_nBlockXSize) *
7167 465 : poDstDS->m_nBlockYSize * l_nBands *
7168 465 : nDataTypeSize);
7169 : }
7170 :
7171 19716 : if (poDstDS->m_bTileInterleave)
7172 : {
7173 114 : eErr = poSrcDS->RasterIO(
7174 : GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
7175 : nReqXSize, nReqYSize, eType, l_nBands, nullptr,
7176 : nDataTypeSize,
7177 57 : static_cast<GSpacing>(nDataTypeSize) *
7178 57 : poDstDS->m_nBlockXSize,
7179 57 : static_cast<GSpacing>(nDataTypeSize) *
7180 57 : poDstDS->m_nBlockXSize * poDstDS->m_nBlockYSize,
7181 : nullptr);
7182 57 : if (eErr == CE_None)
7183 : {
7184 228 : for (int i = 0; eErr == CE_None && i < l_nBands; i++)
7185 : {
7186 171 : eErr = poDstDS->WriteEncodedTileOrStrip(
7187 171 : iBlock + i * poDstDS->m_nBlocksPerBand,
7188 171 : pBlockBuffer + static_cast<size_t>(i) *
7189 171 : poDstDS->m_nBlockXSize *
7190 171 : poDstDS->m_nBlockYSize *
7191 171 : nDataTypeSize,
7192 : false);
7193 : }
7194 : }
7195 : }
7196 19659 : else if (!bIsOddBand)
7197 : {
7198 39196 : eErr = poSrcDS->RasterIO(
7199 : GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
7200 : nReqXSize, nReqYSize, eType, l_nBands, nullptr,
7201 19598 : static_cast<GSpacing>(nDataTypeSize) * l_nBands,
7202 19598 : static_cast<GSpacing>(nDataTypeSize) * l_nBands *
7203 19598 : poDstDS->m_nBlockXSize,
7204 : nDataTypeSize, nullptr);
7205 19598 : if (eErr == CE_None)
7206 : {
7207 19597 : eErr = poDstDS->WriteEncodedTileOrStrip(
7208 : iBlock, pBlockBuffer, false);
7209 : }
7210 : }
7211 : else
7212 : {
7213 : // In the odd bit case, this is a bit messy to ensure
7214 : // the strile gets written synchronously.
7215 : // We load the content of the n-1 bands in the cache,
7216 : // and for the last band we invoke WriteBlock() directly
7217 : // We also force FlushBlockBuf()
7218 122 : std::vector<GDALRasterBlock *> apoLockedBlocks;
7219 91 : for (int i = 0; eErr == CE_None && i < l_nBands - 1; i++)
7220 : {
7221 : auto poBlock =
7222 30 : poDstDS->GetRasterBand(i + 1)->GetLockedBlockRef(
7223 30 : nXBlock, nYBlock, TRUE);
7224 30 : if (poBlock)
7225 : {
7226 60 : eErr = poSrcDS->GetRasterBand(i + 1)->RasterIO(
7227 : GF_Read, iX, iY, nReqXSize, nReqYSize,
7228 : poBlock->GetDataRef(), nReqXSize, nReqYSize,
7229 : eType, nDataTypeSize,
7230 30 : static_cast<GSpacing>(nDataTypeSize) *
7231 30 : poDstDS->m_nBlockXSize,
7232 : nullptr);
7233 30 : poBlock->MarkDirty();
7234 30 : apoLockedBlocks.emplace_back(poBlock);
7235 : }
7236 : else
7237 : {
7238 0 : eErr = CE_Failure;
7239 : }
7240 : }
7241 61 : if (eErr == CE_None)
7242 : {
7243 122 : eErr = poSrcDS->GetRasterBand(l_nBands)->RasterIO(
7244 : GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
7245 : nReqXSize, nReqYSize, eType, nDataTypeSize,
7246 61 : static_cast<GSpacing>(nDataTypeSize) *
7247 61 : poDstDS->m_nBlockXSize,
7248 : nullptr);
7249 : }
7250 61 : if (eErr == CE_None)
7251 : {
7252 : // Avoid any attempt to load from disk
7253 61 : poDstDS->m_nLoadedBlock = iBlock;
7254 61 : eErr = poDstDS->GetRasterBand(l_nBands)->WriteBlock(
7255 : nXBlock, nYBlock, pBlockBuffer);
7256 61 : if (eErr == CE_None)
7257 61 : eErr = poDstDS->FlushBlockBuf();
7258 : }
7259 91 : for (auto poBlock : apoLockedBlocks)
7260 : {
7261 30 : poBlock->MarkClean();
7262 30 : poBlock->DropLock();
7263 : }
7264 : }
7265 :
7266 19716 : if (eErr == CE_None && poDstDS->m_poMaskDS)
7267 : {
7268 4664 : if (nReqXSize < poDstDS->m_nBlockXSize ||
7269 4621 : nReqYSize < poDstDS->m_nBlockYSize)
7270 : {
7271 81 : memset(pBlockBuffer, 0,
7272 81 : static_cast<size_t>(poDstDS->m_nBlockXSize) *
7273 81 : poDstDS->m_nBlockYSize);
7274 : }
7275 9328 : eErr = poSrcMaskBand->RasterIO(
7276 : GF_Read, iX, iY, nReqXSize, nReqYSize, pBlockBuffer,
7277 : nReqXSize, nReqYSize, GDT_UInt8, 1,
7278 4664 : poDstDS->m_nBlockXSize, nullptr);
7279 4664 : if (eErr == CE_None)
7280 : {
7281 : // Avoid any attempt to load from disk
7282 4664 : poDstDS->m_poMaskDS->m_nLoadedBlock = iBlock;
7283 : eErr =
7284 4664 : poDstDS->m_poMaskDS->GetRasterBand(1)->WriteBlock(
7285 : nXBlock, nYBlock, pBlockBuffer);
7286 4664 : if (eErr == CE_None)
7287 4664 : eErr = poDstDS->m_poMaskDS->FlushBlockBuf();
7288 : }
7289 : }
7290 19716 : if (poDstDS->m_bWriteError)
7291 6 : eErr = CE_Failure;
7292 :
7293 19716 : iBlock++;
7294 39432 : if (pfnProgress &&
7295 19716 : !pfnProgress(static_cast<double>(iBlock) / nBlocks, nullptr,
7296 : pProgressData))
7297 : {
7298 0 : eErr = CE_Failure;
7299 : }
7300 : }
7301 : }
7302 : }
7303 :
7304 344 : poDstDS->FlushCache(false); // mostly to wait for thread completion
7305 344 : VSIFree(pBlockBuffer);
7306 :
7307 344 : return eErr;
7308 : }
7309 :
7310 : /************************************************************************/
7311 : /* CreateCopy() */
7312 : /************************************************************************/
7313 :
7314 2136 : GDALDataset *GTiffDataset::CreateCopy(const char *pszFilename,
7315 : GDALDataset *poSrcDS, int bStrict,
7316 : CSLConstList papszOptions,
7317 : GDALProgressFunc pfnProgress,
7318 : void *pProgressData)
7319 :
7320 : {
7321 2136 : if (poSrcDS->GetRasterCount() == 0)
7322 : {
7323 2 : ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
7324 : "Unable to export GeoTIFF files with zero bands.");
7325 2 : return nullptr;
7326 : }
7327 :
7328 2134 : GDALRasterBand *const poPBand = poSrcDS->GetRasterBand(1);
7329 2134 : GDALDataType eType = poPBand->GetRasterDataType();
7330 :
7331 : /* -------------------------------------------------------------------- */
7332 : /* Check, whether all bands in input dataset has the same type. */
7333 : /* -------------------------------------------------------------------- */
7334 2134 : const int l_nBands = poSrcDS->GetRasterCount();
7335 5055 : for (int iBand = 2; iBand <= l_nBands; ++iBand)
7336 : {
7337 2921 : if (eType != poSrcDS->GetRasterBand(iBand)->GetRasterDataType())
7338 : {
7339 0 : if (bStrict)
7340 : {
7341 0 : ReportError(
7342 : pszFilename, CE_Failure, CPLE_AppDefined,
7343 : "Unable to export GeoTIFF file with different datatypes "
7344 : "per different bands. All bands should have the same "
7345 : "types in TIFF.");
7346 0 : return nullptr;
7347 : }
7348 : else
7349 : {
7350 0 : ReportError(
7351 : pszFilename, CE_Warning, CPLE_AppDefined,
7352 : "Unable to export GeoTIFF file with different datatypes "
7353 : "per different bands. All bands should have the same "
7354 : "types in TIFF.");
7355 : }
7356 : }
7357 : }
7358 :
7359 : /* -------------------------------------------------------------------- */
7360 : /* Capture the profile. */
7361 : /* -------------------------------------------------------------------- */
7362 : const GTiffProfile eProfile =
7363 2134 : GetProfile(CSLFetchNameValue(papszOptions, "PROFILE"));
7364 :
7365 2134 : const bool bGeoTIFF = eProfile != GTiffProfile::BASELINE;
7366 :
7367 : /* -------------------------------------------------------------------- */
7368 : /* Special handling for NBITS. Copy from band metadata if found. */
7369 : /* -------------------------------------------------------------------- */
7370 2134 : char **papszCreateOptions = CSLDuplicate(papszOptions);
7371 :
7372 2134 : if (poPBand->GetMetadataItem("NBITS", "IMAGE_STRUCTURE") != nullptr &&
7373 2151 : atoi(poPBand->GetMetadataItem("NBITS", "IMAGE_STRUCTURE")) > 0 &&
7374 17 : CSLFetchNameValue(papszCreateOptions, "NBITS") == nullptr)
7375 : {
7376 3 : papszCreateOptions = CSLSetNameValue(
7377 : papszCreateOptions, "NBITS",
7378 3 : poPBand->GetMetadataItem("NBITS", "IMAGE_STRUCTURE"));
7379 : }
7380 :
7381 2134 : if (CSLFetchNameValue(papszOptions, "PIXELTYPE") == nullptr &&
7382 : eType == GDT_UInt8)
7383 : {
7384 1773 : poPBand->EnablePixelTypeSignedByteWarning(false);
7385 : const char *pszPixelType =
7386 1773 : poPBand->GetMetadataItem("PIXELTYPE", "IMAGE_STRUCTURE");
7387 1773 : poPBand->EnablePixelTypeSignedByteWarning(true);
7388 1773 : if (pszPixelType)
7389 : {
7390 1 : papszCreateOptions =
7391 1 : CSLSetNameValue(papszCreateOptions, "PIXELTYPE", pszPixelType);
7392 : }
7393 : }
7394 :
7395 : /* -------------------------------------------------------------------- */
7396 : /* Color profile. Copy from band metadata if found. */
7397 : /* -------------------------------------------------------------------- */
7398 2134 : if (bGeoTIFF)
7399 : {
7400 2117 : const char *pszOptionsMD[] = {"SOURCE_ICC_PROFILE",
7401 : "SOURCE_PRIMARIES_RED",
7402 : "SOURCE_PRIMARIES_GREEN",
7403 : "SOURCE_PRIMARIES_BLUE",
7404 : "SOURCE_WHITEPOINT",
7405 : "TIFFTAG_TRANSFERFUNCTION_RED",
7406 : "TIFFTAG_TRANSFERFUNCTION_GREEN",
7407 : "TIFFTAG_TRANSFERFUNCTION_BLUE",
7408 : "TIFFTAG_TRANSFERRANGE_BLACK",
7409 : "TIFFTAG_TRANSFERRANGE_WHITE",
7410 : nullptr};
7411 :
7412 : // Copy all the tags. Options will override tags in the source.
7413 2117 : int i = 0;
7414 23267 : while (pszOptionsMD[i] != nullptr)
7415 : {
7416 : char const *pszMD =
7417 21152 : CSLFetchNameValue(papszOptions, pszOptionsMD[i]);
7418 21152 : if (pszMD == nullptr)
7419 : pszMD =
7420 21144 : poSrcDS->GetMetadataItem(pszOptionsMD[i], "COLOR_PROFILE");
7421 :
7422 21152 : if ((pszMD != nullptr) && !EQUAL(pszMD, ""))
7423 : {
7424 16 : papszCreateOptions =
7425 16 : CSLSetNameValue(papszCreateOptions, pszOptionsMD[i], pszMD);
7426 :
7427 : // If an ICC profile exists, other tags are not needed.
7428 16 : if (EQUAL(pszOptionsMD[i], "SOURCE_ICC_PROFILE"))
7429 2 : break;
7430 : }
7431 :
7432 21150 : ++i;
7433 : }
7434 : }
7435 :
7436 2134 : double dfExtraSpaceForOverviews = 0;
7437 : const bool bCopySrcOverviews =
7438 2134 : CPLFetchBool(papszCreateOptions, "COPY_SRC_OVERVIEWS", false);
7439 2134 : std::unique_ptr<GDALDataset> poOvrDS;
7440 2134 : int nSrcOverviews = 0;
7441 2134 : if (bCopySrcOverviews)
7442 : {
7443 : const char *pszOvrDS =
7444 228 : CSLFetchNameValue(papszCreateOptions, "@OVERVIEW_DATASET");
7445 228 : if (pszOvrDS)
7446 : {
7447 : // Empty string is used by COG driver to indicate that we want
7448 : // to ignore source overviews.
7449 37 : if (!EQUAL(pszOvrDS, ""))
7450 : {
7451 35 : poOvrDS.reset(GDALDataset::Open(pszOvrDS));
7452 35 : if (!poOvrDS)
7453 : {
7454 0 : CSLDestroy(papszCreateOptions);
7455 0 : return nullptr;
7456 : }
7457 35 : if (poOvrDS->GetRasterCount() != l_nBands)
7458 : {
7459 0 : CSLDestroy(papszCreateOptions);
7460 0 : return nullptr;
7461 : }
7462 35 : nSrcOverviews =
7463 35 : poOvrDS->GetRasterBand(1)->GetOverviewCount() + 1;
7464 : }
7465 : }
7466 : else
7467 : {
7468 191 : nSrcOverviews = poSrcDS->GetRasterBand(1)->GetOverviewCount();
7469 : }
7470 :
7471 : // Limit number of overviews if specified
7472 : const char *pszOverviewCount =
7473 228 : CSLFetchNameValue(papszCreateOptions, "@OVERVIEW_COUNT");
7474 228 : if (pszOverviewCount)
7475 8 : nSrcOverviews =
7476 8 : std::max(0, std::min(nSrcOverviews, atoi(pszOverviewCount)));
7477 :
7478 228 : if (nSrcOverviews)
7479 : {
7480 204 : for (int j = 1; j <= l_nBands; ++j)
7481 : {
7482 : const int nOtherBandOverviewCount =
7483 134 : poOvrDS ? poOvrDS->GetRasterBand(j)->GetOverviewCount() + 1
7484 198 : : poSrcDS->GetRasterBand(j)->GetOverviewCount();
7485 134 : if (nOtherBandOverviewCount < nSrcOverviews)
7486 : {
7487 1 : ReportError(
7488 : pszFilename, CE_Failure, CPLE_NotSupported,
7489 : "COPY_SRC_OVERVIEWS cannot be used when the bands have "
7490 : "not the same number of overview levels.");
7491 1 : CSLDestroy(papszCreateOptions);
7492 1 : return nullptr;
7493 : }
7494 388 : for (int i = 0; i < nSrcOverviews; ++i)
7495 : {
7496 : GDALRasterBand *poOvrBand =
7497 : poOvrDS
7498 353 : ? (i == 0 ? poOvrDS->GetRasterBand(j)
7499 192 : : poOvrDS->GetRasterBand(j)->GetOverview(
7500 96 : i - 1))
7501 348 : : poSrcDS->GetRasterBand(j)->GetOverview(i);
7502 257 : if (poOvrBand == nullptr)
7503 : {
7504 1 : ReportError(
7505 : pszFilename, CE_Failure, CPLE_NotSupported,
7506 : "COPY_SRC_OVERVIEWS cannot be used when one "
7507 : "overview band is NULL.");
7508 1 : CSLDestroy(papszCreateOptions);
7509 1 : return nullptr;
7510 : }
7511 : GDALRasterBand *poOvrFirstBand =
7512 : poOvrDS
7513 352 : ? (i == 0 ? poOvrDS->GetRasterBand(1)
7514 192 : : poOvrDS->GetRasterBand(1)->GetOverview(
7515 96 : i - 1))
7516 346 : : poSrcDS->GetRasterBand(1)->GetOverview(i);
7517 511 : if (poOvrBand->GetXSize() != poOvrFirstBand->GetXSize() ||
7518 255 : poOvrBand->GetYSize() != poOvrFirstBand->GetYSize())
7519 : {
7520 1 : ReportError(
7521 : pszFilename, CE_Failure, CPLE_NotSupported,
7522 : "COPY_SRC_OVERVIEWS cannot be used when the "
7523 : "overview bands have not the same dimensions "
7524 : "among bands.");
7525 1 : CSLDestroy(papszCreateOptions);
7526 1 : return nullptr;
7527 : }
7528 : }
7529 : }
7530 :
7531 198 : for (int i = 0; i < nSrcOverviews; ++i)
7532 : {
7533 : GDALRasterBand *poOvrFirstBand =
7534 : poOvrDS
7535 201 : ? (i == 0
7536 73 : ? poOvrDS->GetRasterBand(1)
7537 38 : : poOvrDS->GetRasterBand(1)->GetOverview(i - 1))
7538 183 : : poSrcDS->GetRasterBand(1)->GetOverview(i);
7539 128 : dfExtraSpaceForOverviews +=
7540 128 : static_cast<double>(poOvrFirstBand->GetXSize()) *
7541 128 : poOvrFirstBand->GetYSize();
7542 : }
7543 70 : dfExtraSpaceForOverviews *=
7544 70 : l_nBands * GDALGetDataTypeSizeBytes(eType);
7545 : }
7546 : else
7547 : {
7548 155 : CPLDebug("GTiff", "No source overviews to copy");
7549 : }
7550 : }
7551 :
7552 : /* -------------------------------------------------------------------- */
7553 : /* Should we use optimized way of copying from an input JPEG */
7554 : /* dataset? */
7555 : /* -------------------------------------------------------------------- */
7556 :
7557 : // TODO(schwehr): Refactor bDirectCopyFromJPEG to be a const.
7558 : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
7559 2131 : bool bDirectCopyFromJPEG = false;
7560 : #endif
7561 :
7562 : // Note: JPEG_DIRECT_COPY is not defined by default, because it is mainly
7563 : // useful for debugging purposes.
7564 : #ifdef JPEG_DIRECT_COPY
7565 : if (CPLFetchBool(papszCreateOptions, "JPEG_DIRECT_COPY", false) &&
7566 : GTIFF_CanDirectCopyFromJPEG(poSrcDS, papszCreateOptions))
7567 : {
7568 : CPLDebug("GTiff", "Using special direct copy mode from a JPEG dataset");
7569 :
7570 : bDirectCopyFromJPEG = true;
7571 : }
7572 : #endif
7573 :
7574 : #ifdef HAVE_LIBJPEG
7575 2131 : bool bCopyFromJPEG = false;
7576 :
7577 : // When CreateCopy'ing() from a JPEG dataset, and asking for COMPRESS=JPEG,
7578 : // use DCT coefficients (unless other options are incompatible, like
7579 : // strip/tile dimensions, specifying JPEG_QUALITY option, incompatible
7580 : // PHOTOMETRIC with the source colorspace, etc.) to avoid the lossy steps
7581 : // involved by decompression/recompression.
7582 4262 : if (!bDirectCopyFromJPEG &&
7583 2131 : GTIFF_CanCopyFromJPEG(poSrcDS, papszCreateOptions))
7584 : {
7585 12 : CPLDebug("GTiff", "Using special copy mode from a JPEG dataset");
7586 :
7587 12 : bCopyFromJPEG = true;
7588 : }
7589 : #endif
7590 :
7591 : /* -------------------------------------------------------------------- */
7592 : /* If the source is RGB, then set the PHOTOMETRIC=RGB value */
7593 : /* -------------------------------------------------------------------- */
7594 :
7595 : const bool bForcePhotometric =
7596 2131 : CSLFetchNameValue(papszOptions, "PHOTOMETRIC") != nullptr;
7597 :
7598 1225 : if (l_nBands >= 3 && !bForcePhotometric &&
7599 : #ifdef HAVE_LIBJPEG
7600 1187 : !bCopyFromJPEG &&
7601 : #endif
7602 1181 : poSrcDS->GetRasterBand(1)->GetColorInterpretation() == GCI_RedBand &&
7603 4425 : poSrcDS->GetRasterBand(2)->GetColorInterpretation() == GCI_GreenBand &&
7604 1069 : poSrcDS->GetRasterBand(3)->GetColorInterpretation() == GCI_BlueBand)
7605 : {
7606 1063 : papszCreateOptions =
7607 1063 : CSLSetNameValue(papszCreateOptions, "PHOTOMETRIC", "RGB");
7608 : }
7609 :
7610 : /* -------------------------------------------------------------------- */
7611 : /* Create the file. */
7612 : /* -------------------------------------------------------------------- */
7613 2131 : VSILFILE *l_fpL = nullptr;
7614 4262 : CPLString l_osTmpFilename;
7615 :
7616 2131 : const int nXSize = poSrcDS->GetRasterXSize();
7617 2131 : const int nYSize = poSrcDS->GetRasterYSize();
7618 :
7619 : const int nColorTableMultiplier = std::max(
7620 4262 : 1,
7621 4262 : std::min(257,
7622 2131 : atoi(CSLFetchNameValueDef(
7623 : papszOptions, "COLOR_TABLE_MULTIPLIER",
7624 2131 : CPLSPrintf("%d", DEFAULT_COLOR_TABLE_MULTIPLIER_257)))));
7625 :
7626 2131 : bool bTileInterleaving = false;
7627 2131 : TIFF *l_hTIFF = CreateLL(pszFilename, nXSize, nYSize, l_nBands, eType,
7628 : dfExtraSpaceForOverviews, nColorTableMultiplier,
7629 : papszCreateOptions, &l_fpL, l_osTmpFilename,
7630 : /* bCreateCopy = */ true, bTileInterleaving);
7631 2131 : const bool bStreaming = !l_osTmpFilename.empty();
7632 :
7633 2131 : CSLDestroy(papszCreateOptions);
7634 2131 : papszCreateOptions = nullptr;
7635 :
7636 2131 : if (l_hTIFF == nullptr)
7637 : {
7638 18 : if (bStreaming)
7639 0 : VSIUnlink(l_osTmpFilename);
7640 18 : return nullptr;
7641 : }
7642 :
7643 2113 : uint16_t l_nPlanarConfig = 0;
7644 2113 : TIFFGetField(l_hTIFF, TIFFTAG_PLANARCONFIG, &l_nPlanarConfig);
7645 :
7646 2113 : uint16_t l_nCompression = 0;
7647 :
7648 2113 : if (!TIFFGetField(l_hTIFF, TIFFTAG_COMPRESSION, &(l_nCompression)))
7649 0 : l_nCompression = COMPRESSION_NONE;
7650 :
7651 : /* -------------------------------------------------------------------- */
7652 : /* Set the alpha channel if we find one. */
7653 : /* -------------------------------------------------------------------- */
7654 2113 : uint16_t *extraSamples = nullptr;
7655 2113 : uint16_t nExtraSamples = 0;
7656 2113 : if (TIFFGetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, &nExtraSamples,
7657 2378 : &extraSamples) &&
7658 265 : nExtraSamples > 0)
7659 : {
7660 : // We need to allocate a new array as (current) libtiff
7661 : // versions will not like that we reuse the array we got from
7662 : // TIFFGetField().
7663 : uint16_t *pasNewExtraSamples = static_cast<uint16_t *>(
7664 265 : CPLMalloc(nExtraSamples * sizeof(uint16_t)));
7665 265 : memcpy(pasNewExtraSamples, extraSamples,
7666 265 : nExtraSamples * sizeof(uint16_t));
7667 265 : const char *pszAlpha = CPLGetConfigOption(
7668 : "GTIFF_ALPHA", CSLFetchNameValue(papszOptions, "ALPHA"));
7669 : const uint16_t nAlpha =
7670 265 : GTiffGetAlphaValue(pszAlpha, DEFAULT_ALPHA_TYPE);
7671 265 : const int nBaseSamples = l_nBands - nExtraSamples;
7672 895 : for (int iExtraBand = nBaseSamples + 1; iExtraBand <= l_nBands;
7673 : iExtraBand++)
7674 : {
7675 630 : if (poSrcDS->GetRasterBand(iExtraBand)->GetColorInterpretation() ==
7676 : GCI_AlphaBand)
7677 : {
7678 145 : pasNewExtraSamples[iExtraBand - nBaseSamples - 1] = nAlpha;
7679 145 : if (!pszAlpha)
7680 : {
7681 : // Use the ALPHA metadata item from the source band, when
7682 : // present, if no explicit ALPHA creation option
7683 286 : pasNewExtraSamples[iExtraBand - nBaseSamples - 1] =
7684 143 : GTiffGetAlphaValue(
7685 143 : poSrcDS->GetRasterBand(iExtraBand)
7686 143 : ->GetMetadataItem("ALPHA", "IMAGE_STRUCTURE"),
7687 : nAlpha);
7688 : }
7689 : }
7690 : }
7691 265 : TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, nExtraSamples,
7692 : pasNewExtraSamples);
7693 :
7694 265 : CPLFree(pasNewExtraSamples);
7695 : }
7696 :
7697 : /* -------------------------------------------------------------------- */
7698 : /* If the output is jpeg compressed, and the input is RGB make */
7699 : /* sure we note that. */
7700 : /* -------------------------------------------------------------------- */
7701 :
7702 2113 : if (l_nCompression == COMPRESSION_JPEG)
7703 : {
7704 134 : if (l_nBands >= 3 &&
7705 58 : (poSrcDS->GetRasterBand(1)->GetColorInterpretation() ==
7706 0 : GCI_YCbCr_YBand) &&
7707 0 : (poSrcDS->GetRasterBand(2)->GetColorInterpretation() ==
7708 134 : GCI_YCbCr_CbBand) &&
7709 0 : (poSrcDS->GetRasterBand(3)->GetColorInterpretation() ==
7710 : GCI_YCbCr_CrBand))
7711 : {
7712 : // Do nothing.
7713 : }
7714 : else
7715 : {
7716 : // Assume RGB if it is not explicitly YCbCr.
7717 76 : CPLDebug("GTiff", "Setting JPEGCOLORMODE_RGB");
7718 76 : TIFFSetField(l_hTIFF, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
7719 : }
7720 : }
7721 :
7722 : /* -------------------------------------------------------------------- */
7723 : /* Does the source image consist of one band, with a palette? */
7724 : /* If so, copy over. */
7725 : /* -------------------------------------------------------------------- */
7726 1311 : if ((l_nBands == 1 || l_nBands == 2) &&
7727 3424 : poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr &&
7728 : eType == GDT_UInt8)
7729 : {
7730 19 : unsigned short anTRed[256] = {0};
7731 19 : unsigned short anTGreen[256] = {0};
7732 19 : unsigned short anTBlue[256] = {0};
7733 19 : GDALColorTable *poCT = poSrcDS->GetRasterBand(1)->GetColorTable();
7734 :
7735 4883 : for (int iColor = 0; iColor < 256; ++iColor)
7736 : {
7737 4864 : if (iColor < poCT->GetColorEntryCount())
7738 : {
7739 3809 : GDALColorEntry sRGB = {0, 0, 0, 0};
7740 :
7741 3809 : poCT->GetColorEntryAsRGB(iColor, &sRGB);
7742 :
7743 7618 : anTRed[iColor] = GTiffDataset::ClampCTEntry(
7744 3809 : iColor, 1, sRGB.c1, nColorTableMultiplier);
7745 7618 : anTGreen[iColor] = GTiffDataset::ClampCTEntry(
7746 3809 : iColor, 2, sRGB.c2, nColorTableMultiplier);
7747 3809 : anTBlue[iColor] = GTiffDataset::ClampCTEntry(
7748 3809 : iColor, 3, sRGB.c3, nColorTableMultiplier);
7749 : }
7750 : else
7751 : {
7752 1055 : anTRed[iColor] = 0;
7753 1055 : anTGreen[iColor] = 0;
7754 1055 : anTBlue[iColor] = 0;
7755 : }
7756 : }
7757 :
7758 19 : if (!bForcePhotometric)
7759 19 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
7760 19 : TIFFSetField(l_hTIFF, TIFFTAG_COLORMAP, anTRed, anTGreen, anTBlue);
7761 : }
7762 1310 : else if ((l_nBands == 1 || l_nBands == 2) &&
7763 3404 : poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr &&
7764 : eType == GDT_UInt16)
7765 : {
7766 : unsigned short *panTRed = static_cast<unsigned short *>(
7767 1 : CPLMalloc(65536 * sizeof(unsigned short)));
7768 : unsigned short *panTGreen = static_cast<unsigned short *>(
7769 1 : CPLMalloc(65536 * sizeof(unsigned short)));
7770 : unsigned short *panTBlue = static_cast<unsigned short *>(
7771 1 : CPLMalloc(65536 * sizeof(unsigned short)));
7772 :
7773 1 : GDALColorTable *poCT = poSrcDS->GetRasterBand(1)->GetColorTable();
7774 :
7775 65537 : for (int iColor = 0; iColor < 65536; ++iColor)
7776 : {
7777 65536 : if (iColor < poCT->GetColorEntryCount())
7778 : {
7779 65536 : GDALColorEntry sRGB = {0, 0, 0, 0};
7780 :
7781 65536 : poCT->GetColorEntryAsRGB(iColor, &sRGB);
7782 :
7783 131072 : panTRed[iColor] = GTiffDataset::ClampCTEntry(
7784 65536 : iColor, 1, sRGB.c1, nColorTableMultiplier);
7785 131072 : panTGreen[iColor] = GTiffDataset::ClampCTEntry(
7786 65536 : iColor, 2, sRGB.c2, nColorTableMultiplier);
7787 65536 : panTBlue[iColor] = GTiffDataset::ClampCTEntry(
7788 65536 : iColor, 3, sRGB.c3, nColorTableMultiplier);
7789 : }
7790 : else
7791 : {
7792 0 : panTRed[iColor] = 0;
7793 0 : panTGreen[iColor] = 0;
7794 0 : panTBlue[iColor] = 0;
7795 : }
7796 : }
7797 :
7798 1 : if (!bForcePhotometric)
7799 1 : TIFFSetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
7800 1 : TIFFSetField(l_hTIFF, TIFFTAG_COLORMAP, panTRed, panTGreen, panTBlue);
7801 :
7802 1 : CPLFree(panTRed);
7803 1 : CPLFree(panTGreen);
7804 1 : CPLFree(panTBlue);
7805 : }
7806 2093 : else if (poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr)
7807 1 : ReportError(
7808 : pszFilename, CE_Failure, CPLE_AppDefined,
7809 : "Unable to export color table to GeoTIFF file. Color tables "
7810 : "can only be written to 1 band or 2 bands Byte or "
7811 : "UInt16 GeoTIFF files.");
7812 :
7813 2113 : if (l_nCompression == COMPRESSION_JPEG)
7814 : {
7815 76 : uint16_t l_nPhotometric = 0;
7816 76 : TIFFGetField(l_hTIFF, TIFFTAG_PHOTOMETRIC, &l_nPhotometric);
7817 : // Check done in tif_jpeg.c later, but not with a very clear error
7818 : // message
7819 76 : if (l_nPhotometric == PHOTOMETRIC_PALETTE)
7820 : {
7821 1 : ReportError(pszFilename, CE_Failure, CPLE_NotSupported,
7822 : "JPEG compression not supported with paletted image");
7823 1 : XTIFFClose(l_hTIFF);
7824 1 : VSIUnlink(l_osTmpFilename);
7825 1 : CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
7826 1 : return nullptr;
7827 : }
7828 : }
7829 :
7830 2199 : if (l_nBands == 2 &&
7831 2112 : poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr &&
7832 0 : (eType == GDT_UInt8 || eType == GDT_UInt16))
7833 : {
7834 1 : uint16_t v[1] = {EXTRASAMPLE_UNASSALPHA};
7835 :
7836 1 : TIFFSetField(l_hTIFF, TIFFTAG_EXTRASAMPLES, 1, v);
7837 : }
7838 :
7839 2112 : const int nMaskFlags = poSrcDS->GetRasterBand(1)->GetMaskFlags();
7840 2112 : bool bCreateMask = false;
7841 4224 : CPLString osHiddenStructuralMD;
7842 : const char *pszInterleave =
7843 2112 : CSLFetchNameValueDef(papszOptions, "INTERLEAVE", "PIXEL");
7844 2334 : if (bCopySrcOverviews &&
7845 222 : CPLTestBool(CSLFetchNameValueDef(papszOptions, "TILED", "NO")))
7846 : {
7847 210 : osHiddenStructuralMD += "LAYOUT=IFDS_BEFORE_DATA\n";
7848 210 : osHiddenStructuralMD += "BLOCK_ORDER=ROW_MAJOR\n";
7849 210 : osHiddenStructuralMD += "BLOCK_LEADER=SIZE_AS_UINT4\n";
7850 210 : osHiddenStructuralMD += "BLOCK_TRAILER=LAST_4_BYTES_REPEATED\n";
7851 210 : if (l_nBands > 1 && !EQUAL(pszInterleave, "PIXEL"))
7852 : {
7853 21 : osHiddenStructuralMD += "INTERLEAVE=";
7854 21 : osHiddenStructuralMD += CPLString(pszInterleave).toupper();
7855 21 : osHiddenStructuralMD += "\n";
7856 : }
7857 : osHiddenStructuralMD +=
7858 210 : "KNOWN_INCOMPATIBLE_EDITION=NO\n "; // Final space intended, so
7859 : // this can be replaced by YES
7860 : }
7861 2112 : if (!(nMaskFlags & (GMF_ALL_VALID | GMF_ALPHA | GMF_NODATA)) &&
7862 42 : (nMaskFlags & GMF_PER_DATASET) && !bStreaming)
7863 : {
7864 38 : bCreateMask = true;
7865 38 : if (GTiffDataset::MustCreateInternalMask() &&
7866 38 : !osHiddenStructuralMD.empty() && EQUAL(pszInterleave, "PIXEL"))
7867 : {
7868 21 : osHiddenStructuralMD += "MASK_INTERLEAVED_WITH_IMAGERY=YES\n";
7869 : }
7870 : }
7871 2322 : if (!osHiddenStructuralMD.empty() &&
7872 210 : CPLTestBool(CPLGetConfigOption("GTIFF_WRITE_COG_GHOST_AREA", "YES")))
7873 : {
7874 209 : const int nHiddenMDSize = static_cast<int>(osHiddenStructuralMD.size());
7875 : osHiddenStructuralMD =
7876 209 : CPLOPrintf("GDAL_STRUCTURAL_METADATA_SIZE=%06d bytes\n",
7877 418 : nHiddenMDSize) +
7878 209 : osHiddenStructuralMD;
7879 209 : VSI_TIFFWrite(l_hTIFF, osHiddenStructuralMD.c_str(),
7880 : osHiddenStructuralMD.size());
7881 : }
7882 :
7883 : // FIXME? libtiff writes extended tags in the order they are specified
7884 : // and not in increasing order.
7885 :
7886 : /* -------------------------------------------------------------------- */
7887 : /* Transfer some TIFF specific metadata, if available. */
7888 : /* The return value will tell us if we need to try again later with*/
7889 : /* PAM because the profile doesn't allow to write some metadata */
7890 : /* as TIFF tag */
7891 : /* -------------------------------------------------------------------- */
7892 2112 : const bool bHasWrittenMDInGeotiffTAG = GTiffDataset::WriteMetadata(
7893 : poSrcDS, l_hTIFF, false, eProfile, pszFilename, papszOptions);
7894 :
7895 : /* -------------------------------------------------------------------- */
7896 : /* Write NoData value, if exist. */
7897 : /* -------------------------------------------------------------------- */
7898 2112 : if (eProfile == GTiffProfile::GDALGEOTIFF)
7899 : {
7900 2091 : int bSuccess = FALSE;
7901 2091 : GDALRasterBand *poFirstBand = poSrcDS->GetRasterBand(1);
7902 2091 : if (poFirstBand->GetRasterDataType() == GDT_Int64)
7903 : {
7904 4 : const auto nNoData = poFirstBand->GetNoDataValueAsInt64(&bSuccess);
7905 4 : if (bSuccess)
7906 1 : GTiffDataset::WriteNoDataValue(l_hTIFF, nNoData);
7907 : }
7908 2087 : else if (poFirstBand->GetRasterDataType() == GDT_UInt64)
7909 : {
7910 4 : const auto nNoData = poFirstBand->GetNoDataValueAsUInt64(&bSuccess);
7911 4 : if (bSuccess)
7912 1 : GTiffDataset::WriteNoDataValue(l_hTIFF, nNoData);
7913 : }
7914 : else
7915 : {
7916 2083 : const auto dfNoData = poFirstBand->GetNoDataValue(&bSuccess);
7917 2083 : if (bSuccess)
7918 145 : GTiffDataset::WriteNoDataValue(l_hTIFF, dfNoData);
7919 : }
7920 : }
7921 :
7922 : /* -------------------------------------------------------------------- */
7923 : /* Are we addressing PixelIsPoint mode? */
7924 : /* -------------------------------------------------------------------- */
7925 2112 : bool bPixelIsPoint = false;
7926 2112 : bool bPointGeoIgnore = false;
7927 :
7928 3540 : if (poSrcDS->GetMetadataItem(GDALMD_AREA_OR_POINT) &&
7929 1428 : EQUAL(poSrcDS->GetMetadataItem(GDALMD_AREA_OR_POINT), GDALMD_AOP_POINT))
7930 : {
7931 10 : bPixelIsPoint = true;
7932 : bPointGeoIgnore =
7933 10 : CPLTestBool(CPLGetConfigOption("GTIFF_POINT_GEO_IGNORE", "FALSE"));
7934 : }
7935 :
7936 : /* -------------------------------------------------------------------- */
7937 : /* Write affine transform if it is meaningful. */
7938 : /* -------------------------------------------------------------------- */
7939 2112 : const OGRSpatialReference *l_poSRS = nullptr;
7940 2112 : GDALGeoTransform l_gt;
7941 2112 : if (poSrcDS->GetGeoTransform(l_gt) == CE_None)
7942 : {
7943 1682 : if (bGeoTIFF)
7944 : {
7945 1677 : l_poSRS = poSrcDS->GetSpatialRef();
7946 :
7947 1677 : if (l_gt[2] == 0.0 && l_gt[4] == 0.0 && l_gt[5] < 0.0)
7948 : {
7949 1661 : double dfOffset = 0.0;
7950 : {
7951 : // In the case the SRS has a vertical component and we have
7952 : // a single band, encode its scale/offset in the GeoTIFF
7953 : // tags
7954 1661 : int bHasScale = FALSE;
7955 : double dfScale =
7956 1661 : poSrcDS->GetRasterBand(1)->GetScale(&bHasScale);
7957 1661 : int bHasOffset = FALSE;
7958 : dfOffset =
7959 1661 : poSrcDS->GetRasterBand(1)->GetOffset(&bHasOffset);
7960 : const bool bApplyScaleOffset =
7961 1665 : l_poSRS && l_poSRS->IsVertical() &&
7962 4 : poSrcDS->GetRasterCount() == 1;
7963 1661 : if (bApplyScaleOffset && !bHasScale)
7964 0 : dfScale = 1.0;
7965 1661 : if (!bApplyScaleOffset || !bHasOffset)
7966 1657 : dfOffset = 0.0;
7967 1661 : const double adfPixelScale[3] = {l_gt[1], fabs(l_gt[5]),
7968 1661 : bApplyScaleOffset ? dfScale
7969 1661 : : 0.0};
7970 :
7971 1661 : TIFFSetField(l_hTIFF, TIFFTAG_GEOPIXELSCALE, 3,
7972 : adfPixelScale);
7973 : }
7974 :
7975 1661 : double adfTiePoints[6] = {0.0, 0.0, 0.0,
7976 1661 : l_gt[0], l_gt[3], dfOffset};
7977 :
7978 1661 : if (bPixelIsPoint && !bPointGeoIgnore)
7979 : {
7980 6 : adfTiePoints[3] += l_gt[1] * 0.5 + l_gt[2] * 0.5;
7981 6 : adfTiePoints[4] += l_gt[4] * 0.5 + l_gt[5] * 0.5;
7982 : }
7983 :
7984 1661 : TIFFSetField(l_hTIFF, TIFFTAG_GEOTIEPOINTS, 6, adfTiePoints);
7985 : }
7986 : else
7987 : {
7988 16 : double adfMatrix[16] = {0.0};
7989 :
7990 16 : adfMatrix[0] = l_gt[1];
7991 16 : adfMatrix[1] = l_gt[2];
7992 16 : adfMatrix[3] = l_gt[0];
7993 16 : adfMatrix[4] = l_gt[4];
7994 16 : adfMatrix[5] = l_gt[5];
7995 16 : adfMatrix[7] = l_gt[3];
7996 16 : adfMatrix[15] = 1.0;
7997 :
7998 16 : if (bPixelIsPoint && !bPointGeoIgnore)
7999 : {
8000 0 : adfMatrix[3] += l_gt[1] * 0.5 + l_gt[2] * 0.5;
8001 0 : adfMatrix[7] += l_gt[4] * 0.5 + l_gt[5] * 0.5;
8002 : }
8003 :
8004 16 : TIFFSetField(l_hTIFF, TIFFTAG_GEOTRANSMATRIX, 16, adfMatrix);
8005 : }
8006 : }
8007 :
8008 : /* --------------------------------------------------------------------
8009 : */
8010 : /* Do we need a TFW file? */
8011 : /* --------------------------------------------------------------------
8012 : */
8013 1682 : if (CPLFetchBool(papszOptions, "TFW", false))
8014 2 : GDALWriteWorldFile(pszFilename, "tfw", l_gt.data());
8015 1680 : else if (CPLFetchBool(papszOptions, "WORLDFILE", false))
8016 1 : GDALWriteWorldFile(pszFilename, "wld", l_gt.data());
8017 : }
8018 :
8019 : /* -------------------------------------------------------------------- */
8020 : /* Otherwise write tiepoints if they are available. */
8021 : /* -------------------------------------------------------------------- */
8022 430 : else if (poSrcDS->GetGCPCount() > 0 && bGeoTIFF)
8023 : {
8024 12 : const GDAL_GCP *pasGCPs = poSrcDS->GetGCPs();
8025 : double *padfTiePoints = static_cast<double *>(
8026 12 : CPLMalloc(6 * sizeof(double) * poSrcDS->GetGCPCount()));
8027 :
8028 60 : for (int iGCP = 0; iGCP < poSrcDS->GetGCPCount(); ++iGCP)
8029 : {
8030 :
8031 48 : padfTiePoints[iGCP * 6 + 0] = pasGCPs[iGCP].dfGCPPixel;
8032 48 : padfTiePoints[iGCP * 6 + 1] = pasGCPs[iGCP].dfGCPLine;
8033 48 : padfTiePoints[iGCP * 6 + 2] = 0;
8034 48 : padfTiePoints[iGCP * 6 + 3] = pasGCPs[iGCP].dfGCPX;
8035 48 : padfTiePoints[iGCP * 6 + 4] = pasGCPs[iGCP].dfGCPY;
8036 48 : padfTiePoints[iGCP * 6 + 5] = pasGCPs[iGCP].dfGCPZ;
8037 :
8038 48 : if (bPixelIsPoint && !bPointGeoIgnore)
8039 : {
8040 4 : padfTiePoints[iGCP * 6 + 0] -= 0.5;
8041 4 : padfTiePoints[iGCP * 6 + 1] -= 0.5;
8042 : }
8043 : }
8044 :
8045 12 : TIFFSetField(l_hTIFF, TIFFTAG_GEOTIEPOINTS, 6 * poSrcDS->GetGCPCount(),
8046 : padfTiePoints);
8047 12 : CPLFree(padfTiePoints);
8048 :
8049 12 : l_poSRS = poSrcDS->GetGCPSpatialRef();
8050 :
8051 24 : if (CPLFetchBool(papszOptions, "TFW", false) ||
8052 12 : CPLFetchBool(papszOptions, "WORLDFILE", false))
8053 : {
8054 0 : ReportError(
8055 : pszFilename, CE_Warning, CPLE_AppDefined,
8056 : "TFW=ON or WORLDFILE=ON creation options are ignored when "
8057 : "GCPs are available");
8058 : }
8059 : }
8060 : else
8061 : {
8062 418 : l_poSRS = poSrcDS->GetSpatialRef();
8063 : }
8064 :
8065 : /* -------------------------------------------------------------------- */
8066 : /* Copy xml:XMP data */
8067 : /* -------------------------------------------------------------------- */
8068 2112 : CSLConstList papszXMP = poSrcDS->GetMetadata("xml:XMP");
8069 2112 : if (papszXMP != nullptr && *papszXMP != nullptr)
8070 : {
8071 9 : int nTagSize = static_cast<int>(strlen(*papszXMP));
8072 9 : TIFFSetField(l_hTIFF, TIFFTAG_XMLPACKET, nTagSize, *papszXMP);
8073 : }
8074 :
8075 : /* -------------------------------------------------------------------- */
8076 : /* Write the projection information, if possible. */
8077 : /* -------------------------------------------------------------------- */
8078 2112 : const bool bHasProjection = l_poSRS != nullptr;
8079 2112 : bool bExportSRSToPAM = false;
8080 2112 : if ((bHasProjection || bPixelIsPoint) && bGeoTIFF)
8081 : {
8082 1647 : GTIF *psGTIF = GTiffDataset::GTIFNew(l_hTIFF);
8083 :
8084 1647 : if (bHasProjection)
8085 : {
8086 1647 : const auto eGeoTIFFKeysFlavor = GetGTIFFKeysFlavor(papszOptions);
8087 1647 : if (IsSRSCompatibleOfGeoTIFF(l_poSRS, eGeoTIFFKeysFlavor))
8088 : {
8089 1647 : GTIFSetFromOGISDefnEx(
8090 : psGTIF,
8091 : OGRSpatialReference::ToHandle(
8092 : const_cast<OGRSpatialReference *>(l_poSRS)),
8093 : eGeoTIFFKeysFlavor, GetGeoTIFFVersion(papszOptions));
8094 : }
8095 : else
8096 : {
8097 0 : bExportSRSToPAM = true;
8098 : }
8099 : }
8100 :
8101 1647 : if (bPixelIsPoint)
8102 : {
8103 10 : GTIFKeySet(psGTIF, GTRasterTypeGeoKey, TYPE_SHORT, 1,
8104 : RasterPixelIsPoint);
8105 : }
8106 :
8107 1647 : GTIFWriteKeys(psGTIF);
8108 1647 : GTIFFree(psGTIF);
8109 : }
8110 :
8111 2112 : bool l_bDontReloadFirstBlock = false;
8112 :
8113 : #ifdef HAVE_LIBJPEG
8114 2112 : if (bCopyFromJPEG)
8115 : {
8116 12 : GTIFF_CopyFromJPEG_WriteAdditionalTags(l_hTIFF, poSrcDS);
8117 : }
8118 : #endif
8119 :
8120 : /* -------------------------------------------------------------------- */
8121 : /* Cleanup */
8122 : /* -------------------------------------------------------------------- */
8123 2112 : if (bCopySrcOverviews)
8124 : {
8125 222 : TIFFDeferStrileArrayWriting(l_hTIFF);
8126 : }
8127 2112 : TIFFWriteCheck(l_hTIFF, TIFFIsTiled(l_hTIFF), "GTiffCreateCopy()");
8128 2112 : TIFFWriteDirectory(l_hTIFF);
8129 2112 : if (bStreaming)
8130 : {
8131 : // We need to write twice the directory to be sure that custom
8132 : // TIFF tags are correctly sorted and that padding bytes have been
8133 : // added.
8134 5 : TIFFSetDirectory(l_hTIFF, 0);
8135 5 : TIFFWriteDirectory(l_hTIFF);
8136 :
8137 5 : if (VSIFSeekL(l_fpL, 0, SEEK_END) != 0)
8138 0 : ReportError(pszFilename, CE_Failure, CPLE_FileIO, "Cannot seek");
8139 5 : const int nSize = static_cast<int>(VSIFTellL(l_fpL));
8140 :
8141 5 : vsi_l_offset nDataLength = 0;
8142 5 : VSIGetMemFileBuffer(l_osTmpFilename, &nDataLength, FALSE);
8143 5 : TIFFSetDirectory(l_hTIFF, 0);
8144 5 : GTiffFillStreamableOffsetAndCount(l_hTIFF, nSize);
8145 5 : TIFFWriteDirectory(l_hTIFF);
8146 : }
8147 2112 : const auto nDirCount = TIFFNumberOfDirectories(l_hTIFF);
8148 2112 : if (nDirCount >= 1)
8149 : {
8150 2105 : TIFFSetDirectory(l_hTIFF, static_cast<tdir_t>(nDirCount - 1));
8151 : }
8152 2112 : const toff_t l_nDirOffset = TIFFCurrentDirOffset(l_hTIFF);
8153 2112 : TIFFFlush(l_hTIFF);
8154 2112 : XTIFFClose(l_hTIFF);
8155 :
8156 2112 : VSIFSeekL(l_fpL, 0, SEEK_SET);
8157 :
8158 : // fpStreaming will assigned to the instance and not closed here.
8159 2112 : VSILFILE *fpStreaming = nullptr;
8160 2112 : if (bStreaming)
8161 : {
8162 5 : vsi_l_offset nDataLength = 0;
8163 : void *pabyBuffer =
8164 5 : VSIGetMemFileBuffer(l_osTmpFilename, &nDataLength, FALSE);
8165 5 : fpStreaming = VSIFOpenL(pszFilename, "wb");
8166 5 : if (fpStreaming == nullptr)
8167 : {
8168 1 : VSIUnlink(l_osTmpFilename);
8169 1 : CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
8170 1 : return nullptr;
8171 : }
8172 4 : if (static_cast<vsi_l_offset>(VSIFWriteL(pabyBuffer, 1,
8173 : static_cast<int>(nDataLength),
8174 4 : fpStreaming)) != nDataLength)
8175 : {
8176 0 : ReportError(pszFilename, CE_Failure, CPLE_FileIO,
8177 : "Could not write %d bytes",
8178 : static_cast<int>(nDataLength));
8179 0 : CPL_IGNORE_RET_VAL(VSIFCloseL(fpStreaming));
8180 0 : VSIUnlink(l_osTmpFilename);
8181 0 : CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
8182 0 : return nullptr;
8183 : }
8184 : }
8185 :
8186 : /* -------------------------------------------------------------------- */
8187 : /* Re-open as a dataset and copy over missing metadata using */
8188 : /* PAM facilities. */
8189 : /* -------------------------------------------------------------------- */
8190 2111 : l_hTIFF = VSI_TIFFOpen(bStreaming ? l_osTmpFilename.c_str() : pszFilename,
8191 : "r+", l_fpL);
8192 2111 : if (l_hTIFF == nullptr)
8193 : {
8194 11 : if (bStreaming)
8195 0 : VSIUnlink(l_osTmpFilename);
8196 11 : l_fpL->CancelCreation();
8197 11 : CPL_IGNORE_RET_VAL(VSIFCloseL(l_fpL));
8198 11 : return nullptr;
8199 : }
8200 :
8201 : /* -------------------------------------------------------------------- */
8202 : /* Create a corresponding GDALDataset. */
8203 : /* -------------------------------------------------------------------- */
8204 4200 : auto poDS = std::make_unique<GTiffDataset>();
8205 : const bool bSuppressASAP =
8206 2100 : CPLTestBool(CSLFetchNameValueDef(papszOptions, "@SUPPRESS_ASAP", "NO"));
8207 2100 : if (bSuppressASAP)
8208 4 : poDS->MarkSuppressOnClose();
8209 2100 : poDS->SetDescription(pszFilename);
8210 2100 : poDS->eAccess = GA_Update;
8211 2100 : poDS->m_osFilename = pszFilename;
8212 2100 : poDS->m_fpL = l_fpL;
8213 2100 : poDS->m_bIMDRPCMetadataLoaded = true;
8214 2100 : poDS->m_nColorTableMultiplier = nColorTableMultiplier;
8215 2100 : poDS->m_bTileInterleave = bTileInterleaving;
8216 :
8217 2100 : if (bTileInterleaving)
8218 : {
8219 7 : poDS->m_oGTiffMDMD.SetMetadataItem("INTERLEAVE", "TILE",
8220 : "IMAGE_STRUCTURE");
8221 : }
8222 :
8223 2100 : const bool bAppend = CPLFetchBool(papszOptions, "APPEND_SUBDATASET", false);
8224 4199 : if (poDS->OpenOffset(l_hTIFF,
8225 2099 : bAppend ? l_nDirOffset : TIFFCurrentDirOffset(l_hTIFF),
8226 : GA_Update,
8227 : false, // bAllowRGBAInterface
8228 : true // bReadGeoTransform
8229 2100 : ) != CE_None)
8230 : {
8231 0 : l_fpL->CancelCreation();
8232 0 : poDS.reset();
8233 0 : if (bStreaming)
8234 0 : VSIUnlink(l_osTmpFilename);
8235 0 : return nullptr;
8236 : }
8237 :
8238 : // Legacy... Patch back GDT_Int8 type to GDT_UInt8 if the user used
8239 : // PIXELTYPE=SIGNEDBYTE
8240 2100 : const char *pszPixelType = CSLFetchNameValue(papszOptions, "PIXELTYPE");
8241 2100 : if (pszPixelType == nullptr)
8242 2095 : pszPixelType = "";
8243 2100 : if (eType == GDT_UInt8 && EQUAL(pszPixelType, "SIGNEDBYTE"))
8244 : {
8245 10 : for (int i = 0; i < poDS->nBands; ++i)
8246 : {
8247 5 : auto poBand = static_cast<GTiffRasterBand *>(poDS->papoBands[i]);
8248 5 : poBand->eDataType = GDT_UInt8;
8249 5 : poBand->EnablePixelTypeSignedByteWarning(false);
8250 5 : poBand->SetMetadataItem("PIXELTYPE", "SIGNEDBYTE",
8251 : "IMAGE_STRUCTURE");
8252 5 : poBand->EnablePixelTypeSignedByteWarning(true);
8253 : }
8254 : }
8255 :
8256 2100 : poDS->oOvManager.Initialize(poDS.get(), pszFilename);
8257 :
8258 2100 : if (bStreaming)
8259 : {
8260 4 : VSIUnlink(l_osTmpFilename);
8261 4 : poDS->m_fpToWrite = fpStreaming;
8262 : }
8263 2100 : poDS->m_eProfile = eProfile;
8264 :
8265 2100 : int nCloneInfoFlags = GCIF_PAM_DEFAULT & ~GCIF_MASK;
8266 :
8267 : // If we explicitly asked not to tag the alpha band as such, do not
8268 : // reintroduce this alpha color interpretation in PAM.
8269 2100 : if (poSrcDS->GetRasterBand(l_nBands)->GetColorInterpretation() ==
8270 2228 : GCI_AlphaBand &&
8271 128 : GTiffGetAlphaValue(
8272 : CPLGetConfigOption("GTIFF_ALPHA",
8273 : CSLFetchNameValue(papszOptions, "ALPHA")),
8274 : DEFAULT_ALPHA_TYPE) == EXTRASAMPLE_UNSPECIFIED)
8275 : {
8276 1 : nCloneInfoFlags &= ~GCIF_COLORINTERP;
8277 : }
8278 : // Ignore source band color interpretation if requesting PHOTOMETRIC=RGB
8279 3321 : else if (l_nBands >= 3 &&
8280 1222 : EQUAL(CSLFetchNameValueDef(papszOptions, "PHOTOMETRIC", ""),
8281 : "RGB"))
8282 : {
8283 28 : for (int i = 1; i <= 3; i++)
8284 : {
8285 21 : poDS->GetRasterBand(i)->SetColorInterpretation(
8286 21 : static_cast<GDALColorInterp>(GCI_RedBand + (i - 1)));
8287 : }
8288 7 : nCloneInfoFlags &= ~GCIF_COLORINTERP;
8289 9 : if (!(l_nBands == 4 &&
8290 2 : CSLFetchNameValue(papszOptions, "ALPHA") != nullptr))
8291 : {
8292 15 : for (int i = 4; i <= l_nBands; i++)
8293 : {
8294 18 : poDS->GetRasterBand(i)->SetColorInterpretation(
8295 9 : poSrcDS->GetRasterBand(i)->GetColorInterpretation());
8296 : }
8297 : }
8298 : }
8299 :
8300 : CPLString osOldGTIFF_REPORT_COMPD_CSVal(
8301 4200 : CPLGetConfigOption("GTIFF_REPORT_COMPD_CS", ""));
8302 2100 : CPLSetThreadLocalConfigOption("GTIFF_REPORT_COMPD_CS", "YES");
8303 2100 : poDS->CloneInfo(poSrcDS, nCloneInfoFlags);
8304 2100 : CPLSetThreadLocalConfigOption("GTIFF_REPORT_COMPD_CS",
8305 2100 : osOldGTIFF_REPORT_COMPD_CSVal.empty()
8306 : ? nullptr
8307 0 : : osOldGTIFF_REPORT_COMPD_CSVal.c_str());
8308 :
8309 2117 : if ((!bGeoTIFF || bExportSRSToPAM) &&
8310 17 : (poDS->GetPamFlags() & GPF_DISABLED) == 0)
8311 : {
8312 : // Copy georeferencing info to PAM if the profile is not GeoTIFF
8313 16 : poDS->GDALPamDataset::SetSpatialRef(poDS->GetSpatialRef());
8314 16 : GDALGeoTransform gt;
8315 16 : if (poDS->GetGeoTransform(gt) == CE_None)
8316 : {
8317 5 : poDS->GDALPamDataset::SetGeoTransform(gt);
8318 : }
8319 16 : poDS->GDALPamDataset::SetGCPs(poDS->GetGCPCount(), poDS->GetGCPs(),
8320 : poDS->GetGCPSpatialRef());
8321 : }
8322 :
8323 2100 : poDS->m_papszCreationOptions = CSLDuplicate(papszOptions);
8324 2100 : poDS->m_bDontReloadFirstBlock = l_bDontReloadFirstBlock;
8325 :
8326 : /* -------------------------------------------------------------------- */
8327 : /* CloneInfo() does not merge metadata, it just replaces it */
8328 : /* totally. So we have to merge it. */
8329 : /* -------------------------------------------------------------------- */
8330 :
8331 2100 : CSLConstList papszSRC_MD = poSrcDS->GetMetadata();
8332 2100 : char **papszDST_MD = CSLDuplicate(poDS->GetMetadata());
8333 :
8334 2100 : papszDST_MD = CSLMerge(papszDST_MD, papszSRC_MD);
8335 :
8336 2100 : poDS->SetMetadata(papszDST_MD);
8337 2100 : CSLDestroy(papszDST_MD);
8338 :
8339 : // Depending on the PHOTOMETRIC tag, the TIFF file may not have the same
8340 : // band count as the source. Will fail later in GDALDatasetCopyWholeRaster
8341 : // anyway.
8342 7115 : for (int nBand = 1;
8343 7115 : nBand <= std::min(poDS->GetRasterCount(), poSrcDS->GetRasterCount());
8344 : ++nBand)
8345 : {
8346 5015 : GDALRasterBand *poSrcBand = poSrcDS->GetRasterBand(nBand);
8347 5015 : GDALRasterBand *poDstBand = poDS->GetRasterBand(nBand);
8348 5015 : papszSRC_MD = poSrcBand->GetMetadata();
8349 5015 : papszDST_MD = CSLDuplicate(poDstBand->GetMetadata());
8350 :
8351 5015 : papszDST_MD = CSLMerge(papszDST_MD, papszSRC_MD);
8352 :
8353 5015 : poDstBand->SetMetadata(papszDST_MD);
8354 5015 : CSLDestroy(papszDST_MD);
8355 :
8356 5015 : char **papszCatNames = poSrcBand->GetCategoryNames();
8357 5015 : if (nullptr != papszCatNames)
8358 0 : poDstBand->SetCategoryNames(papszCatNames);
8359 : }
8360 :
8361 2100 : l_hTIFF = static_cast<TIFF *>(poDS->GetInternalHandle("TIFF_HANDLE"));
8362 :
8363 : /* -------------------------------------------------------------------- */
8364 : /* Handle forcing xml:ESRI data to be written to PAM. */
8365 : /* -------------------------------------------------------------------- */
8366 2100 : if (CPLTestBool(CPLGetConfigOption("ESRI_XML_PAM", "NO")))
8367 : {
8368 1 : CSLConstList papszESRIMD = poSrcDS->GetMetadata("xml:ESRI");
8369 1 : if (papszESRIMD)
8370 : {
8371 1 : poDS->SetMetadata(papszESRIMD, "xml:ESRI");
8372 : }
8373 : }
8374 :
8375 : /* -------------------------------------------------------------------- */
8376 : /* Second chance: now that we have a PAM dataset, it is possible */
8377 : /* to write metadata that we could not write as a TIFF tag. */
8378 : /* -------------------------------------------------------------------- */
8379 2100 : if (!bHasWrittenMDInGeotiffTAG && !bStreaming)
8380 : {
8381 6 : GTiffDataset::WriteMetadata(
8382 6 : poDS.get(), l_hTIFF, true, eProfile, pszFilename, papszOptions,
8383 : true /* don't write RPC and IMD file again */);
8384 : }
8385 :
8386 2100 : if (!bStreaming)
8387 2096 : GTiffDataset::WriteRPC(poDS.get(), l_hTIFF, true, eProfile, pszFilename,
8388 : papszOptions,
8389 : true /* write only in PAM AND if needed */);
8390 :
8391 2100 : poDS->m_bWriteCOGLayout = bCopySrcOverviews;
8392 :
8393 : // To avoid unnecessary directory rewriting.
8394 2100 : poDS->m_bMetadataChanged = false;
8395 2100 : poDS->m_bGeoTIFFInfoChanged = false;
8396 2100 : poDS->m_bNoDataChanged = false;
8397 2100 : poDS->m_bForceUnsetGTOrGCPs = false;
8398 2100 : poDS->m_bForceUnsetProjection = false;
8399 2100 : poDS->m_bStreamingOut = bStreaming;
8400 :
8401 : // Don't try to load external metadata files (#6597).
8402 2100 : poDS->m_bIMDRPCMetadataLoaded = true;
8403 :
8404 : // We must re-set the compression level at this point, since it has been
8405 : // lost a few lines above when closing the newly create TIFF file The
8406 : // TIFFTAG_ZIPQUALITY & TIFFTAG_JPEGQUALITY are not store in the TIFF file.
8407 : // They are just TIFF session parameters.
8408 :
8409 2100 : poDS->m_nZLevel = GTiffGetZLevel(papszOptions);
8410 2100 : poDS->m_nLZMAPreset = GTiffGetLZMAPreset(papszOptions);
8411 2100 : poDS->m_nZSTDLevel = GTiffGetZSTDPreset(papszOptions);
8412 2100 : poDS->m_nWebPLevel = GTiffGetWebPLevel(papszOptions);
8413 2100 : poDS->m_bWebPLossless = GTiffGetWebPLossless(papszOptions);
8414 2103 : if (poDS->m_nWebPLevel != 100 && poDS->m_bWebPLossless &&
8415 3 : CSLFetchNameValue(papszOptions, "WEBP_LEVEL"))
8416 : {
8417 0 : CPLError(CE_Warning, CPLE_AppDefined,
8418 : "WEBP_LEVEL is specified, but WEBP_LOSSLESS=YES. "
8419 : "WEBP_LEVEL will be ignored.");
8420 : }
8421 2100 : poDS->m_nJpegQuality = GTiffGetJpegQuality(papszOptions);
8422 2100 : poDS->m_nJpegTablesMode = GTiffGetJpegTablesMode(papszOptions);
8423 2100 : poDS->GetDiscardLsbOption(papszOptions);
8424 2100 : poDS->m_dfMaxZError = GTiffGetLERCMaxZError(papszOptions);
8425 2100 : poDS->m_dfMaxZErrorOverview = GTiffGetLERCMaxZErrorOverview(papszOptions);
8426 : #if HAVE_JXL
8427 2100 : poDS->m_bJXLLossless = GTiffGetJXLLossless(papszOptions);
8428 2100 : poDS->m_nJXLEffort = GTiffGetJXLEffort(papszOptions);
8429 2100 : poDS->m_fJXLDistance = GTiffGetJXLDistance(papszOptions);
8430 2100 : poDS->m_fJXLAlphaDistance = GTiffGetJXLAlphaDistance(papszOptions);
8431 : #endif
8432 2100 : poDS->InitCreationOrOpenOptions(true, papszOptions);
8433 :
8434 2100 : if (l_nCompression == COMPRESSION_ADOBE_DEFLATE ||
8435 2072 : l_nCompression == COMPRESSION_LERC)
8436 : {
8437 99 : GTiffSetDeflateSubCodec(l_hTIFF);
8438 :
8439 99 : if (poDS->m_nZLevel != -1)
8440 : {
8441 12 : TIFFSetField(l_hTIFF, TIFFTAG_ZIPQUALITY, poDS->m_nZLevel);
8442 : }
8443 : }
8444 2100 : if (l_nCompression == COMPRESSION_JPEG)
8445 : {
8446 75 : if (poDS->m_nJpegQuality != -1)
8447 : {
8448 9 : TIFFSetField(l_hTIFF, TIFFTAG_JPEGQUALITY, poDS->m_nJpegQuality);
8449 : }
8450 75 : TIFFSetField(l_hTIFF, TIFFTAG_JPEGTABLESMODE, poDS->m_nJpegTablesMode);
8451 : }
8452 2100 : if (l_nCompression == COMPRESSION_LZMA)
8453 : {
8454 7 : if (poDS->m_nLZMAPreset != -1)
8455 : {
8456 6 : TIFFSetField(l_hTIFF, TIFFTAG_LZMAPRESET, poDS->m_nLZMAPreset);
8457 : }
8458 : }
8459 2100 : if (l_nCompression == COMPRESSION_ZSTD ||
8460 2089 : l_nCompression == COMPRESSION_LERC)
8461 : {
8462 82 : if (poDS->m_nZSTDLevel != -1)
8463 : {
8464 8 : TIFFSetField(l_hTIFF, TIFFTAG_ZSTD_LEVEL, poDS->m_nZSTDLevel);
8465 : }
8466 : }
8467 2100 : if (l_nCompression == COMPRESSION_LERC)
8468 : {
8469 71 : TIFFSetField(l_hTIFF, TIFFTAG_LERC_MAXZERROR, poDS->m_dfMaxZError);
8470 : }
8471 : #if HAVE_JXL
8472 2100 : if (l_nCompression == COMPRESSION_JXL ||
8473 2100 : l_nCompression == COMPRESSION_JXL_DNG_1_7)
8474 : {
8475 91 : TIFFSetField(l_hTIFF, TIFFTAG_JXL_LOSSYNESS,
8476 91 : poDS->m_bJXLLossless ? JXL_LOSSLESS : JXL_LOSSY);
8477 91 : TIFFSetField(l_hTIFF, TIFFTAG_JXL_EFFORT, poDS->m_nJXLEffort);
8478 91 : TIFFSetField(l_hTIFF, TIFFTAG_JXL_DISTANCE,
8479 91 : static_cast<double>(poDS->m_fJXLDistance));
8480 91 : TIFFSetField(l_hTIFF, TIFFTAG_JXL_ALPHA_DISTANCE,
8481 91 : static_cast<double>(poDS->m_fJXLAlphaDistance));
8482 : }
8483 : #endif
8484 2100 : if (l_nCompression == COMPRESSION_WEBP)
8485 : {
8486 14 : if (poDS->m_nWebPLevel != -1)
8487 : {
8488 14 : TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LEVEL, poDS->m_nWebPLevel);
8489 : }
8490 :
8491 14 : if (poDS->m_bWebPLossless)
8492 : {
8493 5 : TIFFSetField(l_hTIFF, TIFFTAG_WEBP_LOSSLESS, poDS->m_bWebPLossless);
8494 : }
8495 : }
8496 :
8497 : /* -------------------------------------------------------------------- */
8498 : /* Do we want to ensure all blocks get written out on close to */
8499 : /* avoid sparse files? */
8500 : /* -------------------------------------------------------------------- */
8501 2100 : if (!CPLFetchBool(papszOptions, "SPARSE_OK", false))
8502 2072 : poDS->m_bFillEmptyTilesAtClosing = true;
8503 :
8504 2100 : poDS->m_bWriteEmptyTiles =
8505 3982 : (bCopySrcOverviews && poDS->m_bFillEmptyTilesAtClosing) || bStreaming ||
8506 1882 : (poDS->m_nCompression != COMPRESSION_NONE &&
8507 306 : poDS->m_bFillEmptyTilesAtClosing);
8508 : // Only required for people writing non-compressed striped files in the
8509 : // rightorder and wanting all tstrips to be written in the same order
8510 : // so that the end result can be memory mapped without knowledge of each
8511 : // strip offset
8512 2100 : if (CPLTestBool(CSLFetchNameValueDef(
8513 4200 : papszOptions, "WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")) ||
8514 2100 : CPLTestBool(CSLFetchNameValueDef(
8515 : papszOptions, "@WRITE_EMPTY_TILES_SYNCHRONOUSLY", "FALSE")))
8516 : {
8517 0 : poDS->m_bWriteEmptyTiles = true;
8518 : }
8519 :
8520 : // Precreate (internal) mask, so that the IBuildOverviews() below
8521 : // has a chance to create also the overviews of the mask.
8522 2100 : CPLErr eErr = CE_None;
8523 :
8524 2100 : if (bCreateMask)
8525 : {
8526 38 : eErr = poDS->CreateMaskBand(nMaskFlags);
8527 38 : if (poDS->m_poMaskDS)
8528 : {
8529 37 : poDS->m_poMaskDS->m_bFillEmptyTilesAtClosing =
8530 37 : poDS->m_bFillEmptyTilesAtClosing;
8531 37 : poDS->m_poMaskDS->m_bWriteEmptyTiles = poDS->m_bWriteEmptyTiles;
8532 : }
8533 : }
8534 :
8535 : /* -------------------------------------------------------------------- */
8536 : /* Create and then copy existing overviews if requested */
8537 : /* We do it such that all the IFDs are at the beginning of the file, */
8538 : /* and that the imagery data for the smallest overview is written */
8539 : /* first, that way the file is more usable when embedded in a */
8540 : /* compressed stream. */
8541 : /* -------------------------------------------------------------------- */
8542 :
8543 : // For scaled progress due to overview copying.
8544 2100 : const int nBandsWidthMask = l_nBands + (bCreateMask ? 1 : 0);
8545 2100 : double dfTotalPixels =
8546 2100 : static_cast<double>(nXSize) * nYSize * nBandsWidthMask;
8547 2100 : double dfCurPixels = 0;
8548 :
8549 2100 : if (eErr == CE_None && bCopySrcOverviews)
8550 : {
8551 0 : std::unique_ptr<GDALDataset> poMaskOvrDS;
8552 : const char *pszMaskOvrDS =
8553 219 : CSLFetchNameValue(papszOptions, "@MASK_OVERVIEW_DATASET");
8554 219 : if (pszMaskOvrDS)
8555 : {
8556 6 : poMaskOvrDS.reset(GDALDataset::Open(pszMaskOvrDS));
8557 6 : if (!poMaskOvrDS)
8558 : {
8559 0 : l_fpL->CancelCreation();
8560 0 : return nullptr;
8561 : }
8562 6 : if (poMaskOvrDS->GetRasterCount() != 1)
8563 : {
8564 0 : l_fpL->CancelCreation();
8565 0 : return nullptr;
8566 : }
8567 : }
8568 219 : if (nSrcOverviews)
8569 : {
8570 69 : eErr = poDS->CreateOverviewsFromSrcOverviews(poSrcDS, poOvrDS.get(),
8571 : nSrcOverviews);
8572 :
8573 201 : if (eErr == CE_None &&
8574 69 : (poMaskOvrDS != nullptr ||
8575 63 : (poSrcDS->GetRasterBand(1)->GetOverview(0) &&
8576 35 : poSrcDS->GetRasterBand(1)->GetOverview(0)->GetMaskFlags() ==
8577 : GMF_PER_DATASET)))
8578 : {
8579 19 : int nOvrBlockXSize = 0;
8580 19 : int nOvrBlockYSize = 0;
8581 19 : GTIFFGetOverviewBlockSize(
8582 19 : GDALRasterBand::ToHandle(poDS->GetRasterBand(1)),
8583 : &nOvrBlockXSize, &nOvrBlockYSize, nullptr, nullptr);
8584 19 : eErr = poDS->CreateInternalMaskOverviews(nOvrBlockXSize,
8585 : nOvrBlockYSize);
8586 : }
8587 : }
8588 :
8589 219 : TIFFForceStrileArrayWriting(poDS->m_hTIFF);
8590 :
8591 219 : if (poDS->m_poMaskDS)
8592 : {
8593 27 : TIFFForceStrileArrayWriting(poDS->m_poMaskDS->m_hTIFF);
8594 : }
8595 :
8596 345 : for (auto &poIterOvrDS : poDS->m_apoOverviewDS)
8597 : {
8598 126 : TIFFForceStrileArrayWriting(poIterOvrDS->m_hTIFF);
8599 :
8600 126 : if (poIterOvrDS->m_poMaskDS)
8601 : {
8602 32 : TIFFForceStrileArrayWriting(poIterOvrDS->m_poMaskDS->m_hTIFF);
8603 : }
8604 : }
8605 :
8606 219 : if (eErr == CE_None && nSrcOverviews)
8607 : {
8608 69 : if (poDS->m_apoOverviewDS.size() !=
8609 69 : static_cast<size_t>(nSrcOverviews))
8610 : {
8611 0 : ReportError(
8612 : pszFilename, CE_Failure, CPLE_AppDefined,
8613 : "Did only manage to instantiate %d overview levels, "
8614 : "whereas source contains %d",
8615 0 : static_cast<int>(poDS->m_apoOverviewDS.size()),
8616 : nSrcOverviews);
8617 0 : eErr = CE_Failure;
8618 : }
8619 :
8620 195 : for (int i = 0; eErr == CE_None && i < nSrcOverviews; ++i)
8621 : {
8622 : GDALRasterBand *poOvrBand =
8623 : poOvrDS
8624 197 : ? (i == 0
8625 71 : ? poOvrDS->GetRasterBand(1)
8626 37 : : poOvrDS->GetRasterBand(1)->GetOverview(i - 1))
8627 181 : : poSrcDS->GetRasterBand(1)->GetOverview(i);
8628 : const double dfOvrPixels =
8629 126 : static_cast<double>(poOvrBand->GetXSize()) *
8630 126 : poOvrBand->GetYSize();
8631 126 : dfTotalPixels += dfOvrPixels * l_nBands;
8632 234 : if (poOvrBand->GetMaskFlags() == GMF_PER_DATASET ||
8633 108 : poMaskOvrDS != nullptr)
8634 : {
8635 32 : dfTotalPixels += dfOvrPixels;
8636 : }
8637 94 : else if (i == 0 && poDS->GetRasterBand(1)->GetMaskFlags() ==
8638 : GMF_PER_DATASET)
8639 : {
8640 1 : ReportError(pszFilename, CE_Warning, CPLE_AppDefined,
8641 : "Source dataset has a mask band on full "
8642 : "resolution, overviews on the regular bands, "
8643 : "but lacks overviews on the mask band.");
8644 : }
8645 : }
8646 :
8647 : // Now copy the imagery.
8648 : // Begin with the smallest overview.
8649 69 : for (int iOvrLevel = nSrcOverviews - 1;
8650 194 : eErr == CE_None && iOvrLevel >= 0; --iOvrLevel)
8651 : {
8652 125 : auto poDstDS = poDS->m_apoOverviewDS[iOvrLevel].get();
8653 :
8654 : // Create a fake dataset with the source overview level so that
8655 : // GDALDatasetCopyWholeRaster can cope with it.
8656 : GDALDataset *poSrcOvrDS =
8657 : poOvrDS
8658 162 : ? (iOvrLevel == 0 ? poOvrDS.get()
8659 37 : : GDALCreateOverviewDataset(
8660 : poOvrDS.get(), iOvrLevel - 1,
8661 : /* bThisLevelOnly = */ true))
8662 54 : : GDALCreateOverviewDataset(
8663 : poSrcDS, iOvrLevel,
8664 125 : /* bThisLevelOnly = */ true);
8665 : GDALRasterBand *poSrcOvrBand =
8666 196 : poOvrDS ? (iOvrLevel == 0
8667 71 : ? poOvrDS->GetRasterBand(1)
8668 74 : : poOvrDS->GetRasterBand(1)->GetOverview(
8669 37 : iOvrLevel - 1))
8670 179 : : poSrcDS->GetRasterBand(1)->GetOverview(iOvrLevel);
8671 : double dfNextCurPixels =
8672 : dfCurPixels +
8673 125 : static_cast<double>(poSrcOvrBand->GetXSize()) *
8674 125 : poSrcOvrBand->GetYSize() * l_nBands;
8675 :
8676 125 : poDstDS->m_bBlockOrderRowMajor = true;
8677 125 : poDstDS->m_bLeaderSizeAsUInt4 = true;
8678 125 : poDstDS->m_bTrailerRepeatedLast4BytesRepeated = true;
8679 125 : poDstDS->m_bFillEmptyTilesAtClosing =
8680 125 : poDS->m_bFillEmptyTilesAtClosing;
8681 125 : poDstDS->m_bWriteEmptyTiles = poDS->m_bWriteEmptyTiles;
8682 125 : poDstDS->m_bTileInterleave = poDS->m_bTileInterleave;
8683 125 : GDALRasterBand *poSrcMaskBand = nullptr;
8684 125 : if (poDstDS->m_poMaskDS)
8685 : {
8686 32 : poDstDS->m_poMaskDS->m_bBlockOrderRowMajor = true;
8687 32 : poDstDS->m_poMaskDS->m_bLeaderSizeAsUInt4 = true;
8688 32 : poDstDS->m_poMaskDS->m_bTrailerRepeatedLast4BytesRepeated =
8689 : true;
8690 64 : poDstDS->m_poMaskDS->m_bFillEmptyTilesAtClosing =
8691 32 : poDS->m_bFillEmptyTilesAtClosing;
8692 64 : poDstDS->m_poMaskDS->m_bWriteEmptyTiles =
8693 32 : poDS->m_bWriteEmptyTiles;
8694 :
8695 32 : poSrcMaskBand =
8696 : poMaskOvrDS
8697 46 : ? (iOvrLevel == 0
8698 14 : ? poMaskOvrDS->GetRasterBand(1)
8699 16 : : poMaskOvrDS->GetRasterBand(1)->GetOverview(
8700 8 : iOvrLevel - 1))
8701 50 : : poSrcOvrBand->GetMaskBand();
8702 : }
8703 :
8704 125 : if (poDstDS->m_poMaskDS)
8705 : {
8706 32 : dfNextCurPixels +=
8707 32 : static_cast<double>(poSrcOvrBand->GetXSize()) *
8708 32 : poSrcOvrBand->GetYSize();
8709 : }
8710 : void *pScaledData =
8711 125 : GDALCreateScaledProgress(dfCurPixels / dfTotalPixels,
8712 : dfNextCurPixels / dfTotalPixels,
8713 : pfnProgress, pProgressData);
8714 :
8715 125 : eErr = CopyImageryAndMask(poDstDS, poSrcOvrDS, poSrcMaskBand,
8716 : GDALScaledProgress, pScaledData);
8717 :
8718 125 : dfCurPixels = dfNextCurPixels;
8719 125 : GDALDestroyScaledProgress(pScaledData);
8720 :
8721 125 : if (poSrcOvrDS != poOvrDS.get())
8722 91 : delete poSrcOvrDS;
8723 125 : poSrcOvrDS = nullptr;
8724 : }
8725 : }
8726 : }
8727 :
8728 : /* -------------------------------------------------------------------- */
8729 : /* Copy actual imagery. */
8730 : /* -------------------------------------------------------------------- */
8731 2100 : double dfNextCurPixels =
8732 2100 : dfCurPixels + static_cast<double>(nXSize) * nYSize * l_nBands;
8733 2100 : void *pScaledData = GDALCreateScaledProgress(
8734 : dfCurPixels / dfTotalPixels, dfNextCurPixels / dfTotalPixels,
8735 : pfnProgress, pProgressData);
8736 :
8737 : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
8738 2100 : bool bTryCopy = true;
8739 : #endif
8740 :
8741 : #ifdef HAVE_LIBJPEG
8742 2100 : if (bCopyFromJPEG)
8743 : {
8744 12 : eErr = GTIFF_CopyFromJPEG(poDS.get(), poSrcDS, pfnProgress,
8745 : pProgressData, bTryCopy);
8746 :
8747 : // In case of failure in the decompression step, try normal copy.
8748 12 : if (bTryCopy)
8749 0 : eErr = CE_None;
8750 : }
8751 : #endif
8752 :
8753 : #ifdef JPEG_DIRECT_COPY
8754 : if (bDirectCopyFromJPEG)
8755 : {
8756 : eErr = GTIFF_DirectCopyFromJPEG(poDS.get(), poSrcDS, pfnProgress,
8757 : pProgressData, bTryCopy);
8758 :
8759 : // In case of failure in the reading step, try normal copy.
8760 : if (bTryCopy)
8761 : eErr = CE_None;
8762 : }
8763 : #endif
8764 :
8765 2100 : bool bWriteMask = true;
8766 2100 : if (
8767 : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
8768 4188 : bTryCopy &&
8769 : #endif
8770 2088 : (poDS->m_bTreatAsSplit || poDS->m_bTreatAsSplitBitmap))
8771 : {
8772 : // For split bands, we use TIFFWriteScanline() interface.
8773 9 : CPLAssert(poDS->m_nBitsPerSample == 8 || poDS->m_nBitsPerSample == 1);
8774 :
8775 9 : if (poDS->m_nPlanarConfig == PLANARCONFIG_CONTIG && poDS->nBands > 1)
8776 : {
8777 : GByte *pabyScanline = static_cast<GByte *>(
8778 3 : VSI_MALLOC_VERBOSE(TIFFScanlineSize(l_hTIFF)));
8779 3 : if (pabyScanline == nullptr)
8780 0 : eErr = CE_Failure;
8781 9052 : for (int j = 0; j < nYSize && eErr == CE_None; ++j)
8782 : {
8783 18098 : eErr = poSrcDS->RasterIO(GF_Read, 0, j, nXSize, 1, pabyScanline,
8784 : nXSize, 1, GDT_UInt8, l_nBands,
8785 9049 : nullptr, poDS->nBands, 0, 1, nullptr);
8786 18098 : if (eErr == CE_None &&
8787 9049 : TIFFWriteScanline(l_hTIFF, pabyScanline, j, 0) == -1)
8788 : {
8789 0 : ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
8790 : "TIFFWriteScanline() failed.");
8791 0 : eErr = CE_Failure;
8792 : }
8793 9049 : if (!GDALScaledProgress((j + 1) * 1.0 / nYSize, nullptr,
8794 : pScaledData))
8795 0 : eErr = CE_Failure;
8796 : }
8797 3 : CPLFree(pabyScanline);
8798 : }
8799 : else
8800 : {
8801 : GByte *pabyScanline =
8802 6 : static_cast<GByte *>(VSI_MALLOC_VERBOSE(nXSize));
8803 6 : if (pabyScanline == nullptr)
8804 0 : eErr = CE_Failure;
8805 : else
8806 6 : eErr = CE_None;
8807 14 : for (int iBand = 1; iBand <= l_nBands && eErr == CE_None; ++iBand)
8808 : {
8809 48211 : for (int j = 0; j < nYSize && eErr == CE_None; ++j)
8810 : {
8811 48203 : eErr = poSrcDS->GetRasterBand(iBand)->RasterIO(
8812 : GF_Read, 0, j, nXSize, 1, pabyScanline, nXSize, 1,
8813 : GDT_UInt8, 0, 0, nullptr);
8814 48203 : if (poDS->m_bTreatAsSplitBitmap)
8815 : {
8816 7225210 : for (int i = 0; i < nXSize; ++i)
8817 : {
8818 7216010 : const GByte byVal = pabyScanline[i];
8819 7216010 : if ((i & 0x7) == 0)
8820 902001 : pabyScanline[i >> 3] = 0;
8821 7216010 : if (byVal)
8822 7097220 : pabyScanline[i >> 3] |= 0x80 >> (i & 0x7);
8823 : }
8824 : }
8825 96406 : if (eErr == CE_None &&
8826 48203 : TIFFWriteScanline(l_hTIFF, pabyScanline, j,
8827 48203 : static_cast<uint16_t>(iBand - 1)) ==
8828 : -1)
8829 : {
8830 0 : ReportError(pszFilename, CE_Failure, CPLE_AppDefined,
8831 : "TIFFWriteScanline() failed.");
8832 0 : eErr = CE_Failure;
8833 : }
8834 48203 : if (!GDALScaledProgress((j + 1 + (iBand - 1) * nYSize) *
8835 48203 : 1.0 / (l_nBands * nYSize),
8836 : nullptr, pScaledData))
8837 0 : eErr = CE_Failure;
8838 : }
8839 : }
8840 6 : CPLFree(pabyScanline);
8841 : }
8842 :
8843 : // Necessary to be able to read the file without re-opening.
8844 9 : TIFFSizeProc pfnSizeProc = TIFFGetSizeProc(l_hTIFF);
8845 :
8846 9 : TIFFFlushData(l_hTIFF);
8847 :
8848 9 : toff_t nNewDirOffset = pfnSizeProc(TIFFClientdata(l_hTIFF));
8849 9 : if ((nNewDirOffset % 2) == 1)
8850 5 : ++nNewDirOffset;
8851 :
8852 9 : TIFFFlush(l_hTIFF);
8853 :
8854 9 : if (poDS->m_nDirOffset != TIFFCurrentDirOffset(l_hTIFF))
8855 : {
8856 0 : poDS->m_nDirOffset = nNewDirOffset;
8857 0 : CPLDebug("GTiff", "directory moved during flush.");
8858 : }
8859 : }
8860 2091 : else if (
8861 : #if defined(HAVE_LIBJPEG) || defined(JPEG_DIRECT_COPY)
8862 2079 : bTryCopy &&
8863 : #endif
8864 : eErr == CE_None)
8865 : {
8866 2078 : const char *papszCopyWholeRasterOptions[3] = {nullptr, nullptr,
8867 : nullptr};
8868 2078 : int iNextOption = 0;
8869 2078 : papszCopyWholeRasterOptions[iNextOption++] = "SKIP_HOLES=YES";
8870 2078 : if (l_nCompression != COMPRESSION_NONE)
8871 : {
8872 490 : papszCopyWholeRasterOptions[iNextOption++] = "COMPRESSED=YES";
8873 : }
8874 :
8875 : // For streaming with separate, we really want that bands are written
8876 : // after each other, even if the source is pixel interleaved.
8877 1588 : else if (bStreaming && poDS->m_nPlanarConfig == PLANARCONFIG_SEPARATE)
8878 : {
8879 1 : papszCopyWholeRasterOptions[iNextOption++] = "INTERLEAVE=BAND";
8880 : }
8881 :
8882 2078 : if (bCopySrcOverviews || bTileInterleaving)
8883 : {
8884 219 : poDS->m_bBlockOrderRowMajor = true;
8885 219 : poDS->m_bLeaderSizeAsUInt4 = bCopySrcOverviews;
8886 219 : poDS->m_bTrailerRepeatedLast4BytesRepeated = bCopySrcOverviews;
8887 219 : if (poDS->m_poMaskDS)
8888 : {
8889 27 : poDS->m_poMaskDS->m_bBlockOrderRowMajor = true;
8890 27 : poDS->m_poMaskDS->m_bLeaderSizeAsUInt4 = bCopySrcOverviews;
8891 27 : poDS->m_poMaskDS->m_bTrailerRepeatedLast4BytesRepeated =
8892 : bCopySrcOverviews;
8893 27 : GDALDestroyScaledProgress(pScaledData);
8894 : pScaledData =
8895 27 : GDALCreateScaledProgress(dfCurPixels / dfTotalPixels, 1.0,
8896 : pfnProgress, pProgressData);
8897 : }
8898 :
8899 219 : eErr = CopyImageryAndMask(poDS.get(), poSrcDS,
8900 219 : poSrcDS->GetRasterBand(1)->GetMaskBand(),
8901 : GDALScaledProgress, pScaledData);
8902 219 : if (poDS->m_poMaskDS)
8903 : {
8904 27 : bWriteMask = false;
8905 : }
8906 : }
8907 : else
8908 : {
8909 1859 : eErr = GDALDatasetCopyWholeRaster(GDALDataset::ToHandle(poSrcDS),
8910 1859 : GDALDataset::ToHandle(poDS.get()),
8911 : papszCopyWholeRasterOptions,
8912 : GDALScaledProgress, pScaledData);
8913 : }
8914 : }
8915 :
8916 2100 : GDALDestroyScaledProgress(pScaledData);
8917 :
8918 2100 : if (eErr == CE_None && !bStreaming && bWriteMask)
8919 : {
8920 2051 : pScaledData = GDALCreateScaledProgress(dfNextCurPixels / dfTotalPixels,
8921 : 1.0, pfnProgress, pProgressData);
8922 2051 : if (poDS->m_poMaskDS)
8923 : {
8924 10 : const char *l_papszOptions[2] = {"COMPRESSED=YES", nullptr};
8925 10 : eErr = GDALRasterBandCopyWholeRaster(
8926 10 : poSrcDS->GetRasterBand(1)->GetMaskBand(),
8927 10 : poDS->GetRasterBand(1)->GetMaskBand(),
8928 : const_cast<char **>(l_papszOptions), GDALScaledProgress,
8929 : pScaledData);
8930 : }
8931 : else
8932 : {
8933 2041 : eErr = GDALDriver::DefaultCopyMasks(poSrcDS, poDS.get(), bStrict,
8934 : nullptr, GDALScaledProgress,
8935 : pScaledData);
8936 : }
8937 2051 : GDALDestroyScaledProgress(pScaledData);
8938 : }
8939 :
8940 2100 : poDS->m_bWriteCOGLayout = false;
8941 :
8942 4182 : if (eErr == CE_None &&
8943 2082 : CPLTestBool(CSLFetchNameValueDef(poDS->m_papszCreationOptions,
8944 : "@FLUSHCACHE", "NO")))
8945 : {
8946 172 : if (poDS->FlushCache(false) != CE_None)
8947 : {
8948 0 : eErr = CE_Failure;
8949 : }
8950 : }
8951 :
8952 2100 : if (eErr == CE_Failure)
8953 : {
8954 18 : if (CPLTestBool(CPLGetConfigOption("GTIFF_DELETE_ON_ERROR", "YES")))
8955 : {
8956 17 : l_fpL->CancelCreation();
8957 17 : poDS.reset();
8958 :
8959 17 : if (!bStreaming)
8960 : {
8961 : // Should really delete more carefully.
8962 17 : VSIUnlink(pszFilename);
8963 : }
8964 : }
8965 : else
8966 : {
8967 1 : poDS.reset();
8968 : }
8969 : }
8970 :
8971 2100 : return poDS.release();
8972 : }
8973 :
8974 : /************************************************************************/
8975 : /* SetSpatialRef() */
8976 : /************************************************************************/
8977 :
8978 1498 : CPLErr GTiffDataset::SetSpatialRef(const OGRSpatialReference *poSRS)
8979 :
8980 : {
8981 1498 : if (m_bStreamingOut && m_bCrystalized)
8982 : {
8983 1 : ReportError(CE_Failure, CPLE_NotSupported,
8984 : "Cannot modify projection at that point in "
8985 : "a streamed output file");
8986 1 : return CE_Failure;
8987 : }
8988 :
8989 1497 : LoadGeoreferencingAndPamIfNeeded();
8990 1497 : LookForProjection();
8991 :
8992 1497 : CPLErr eErr = CE_None;
8993 1497 : if (eAccess == GA_Update)
8994 : {
8995 1499 : if ((m_eProfile == GTiffProfile::BASELINE) &&
8996 7 : (GetPamFlags() & GPF_DISABLED) == 0)
8997 : {
8998 7 : eErr = GDALPamDataset::SetSpatialRef(poSRS);
8999 : }
9000 : else
9001 : {
9002 1485 : if (GDALPamDataset::GetSpatialRef() != nullptr)
9003 : {
9004 : // Cancel any existing SRS from PAM file.
9005 1 : GDALPamDataset::SetSpatialRef(nullptr);
9006 : }
9007 1485 : m_bGeoTIFFInfoChanged = true;
9008 : }
9009 : }
9010 : else
9011 : {
9012 5 : CPLDebug("GTIFF", "SetSpatialRef() goes to PAM instead of TIFF tags");
9013 5 : eErr = GDALPamDataset::SetSpatialRef(poSRS);
9014 : }
9015 :
9016 1497 : if (eErr == CE_None)
9017 : {
9018 1497 : if (poSRS == nullptr || poSRS->IsEmpty())
9019 : {
9020 14 : if (!m_oSRS.IsEmpty())
9021 : {
9022 4 : m_bForceUnsetProjection = true;
9023 : }
9024 14 : m_oSRS.Clear();
9025 : }
9026 : else
9027 : {
9028 1483 : m_oSRS = *poSRS;
9029 1483 : m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
9030 : }
9031 : }
9032 :
9033 1497 : return eErr;
9034 : }
9035 :
9036 : /************************************************************************/
9037 : /* SetGeoTransform() */
9038 : /************************************************************************/
9039 :
9040 1805 : CPLErr GTiffDataset::SetGeoTransform(const GDALGeoTransform >)
9041 :
9042 : {
9043 1805 : if (m_bStreamingOut && m_bCrystalized)
9044 : {
9045 1 : ReportError(CE_Failure, CPLE_NotSupported,
9046 : "Cannot modify geotransform at that point in a "
9047 : "streamed output file");
9048 1 : return CE_Failure;
9049 : }
9050 :
9051 1804 : LoadGeoreferencingAndPamIfNeeded();
9052 :
9053 1804 : CPLErr eErr = CE_None;
9054 1804 : if (eAccess == GA_Update)
9055 : {
9056 1798 : if (!m_aoGCPs.empty())
9057 : {
9058 1 : ReportError(CE_Warning, CPLE_AppDefined,
9059 : "GCPs previously set are going to be cleared "
9060 : "due to the setting of a geotransform.");
9061 1 : m_bForceUnsetGTOrGCPs = true;
9062 1 : m_aoGCPs.clear();
9063 : }
9064 2006 : else if (gt[0] == 0.0 && gt[1] == 0.0 && gt[2] == 0.0 && gt[3] == 0.0 &&
9065 2006 : gt[4] == 0.0 && gt[5] == 0.0)
9066 : {
9067 2 : if (m_bGeoTransformValid)
9068 : {
9069 2 : m_bForceUnsetGTOrGCPs = true;
9070 2 : m_bGeoTIFFInfoChanged = true;
9071 : }
9072 2 : m_bGeoTransformValid = false;
9073 2 : m_gt = gt;
9074 2 : return CE_None;
9075 : }
9076 :
9077 3601 : if ((m_eProfile == GTiffProfile::BASELINE) &&
9078 9 : !CPLFetchBool(m_papszCreationOptions, "TFW", false) &&
9079 1810 : !CPLFetchBool(m_papszCreationOptions, "WORLDFILE", false) &&
9080 5 : (GetPamFlags() & GPF_DISABLED) == 0)
9081 : {
9082 5 : eErr = GDALPamDataset::SetGeoTransform(gt);
9083 : }
9084 : else
9085 : {
9086 : // Cancel any existing geotransform from PAM file.
9087 1791 : GDALPamDataset::DeleteGeoTransform();
9088 1791 : m_bGeoTIFFInfoChanged = true;
9089 : }
9090 : }
9091 : else
9092 : {
9093 6 : CPLDebug("GTIFF", "SetGeoTransform() goes to PAM instead of TIFF tags");
9094 6 : eErr = GDALPamDataset::SetGeoTransform(gt);
9095 : }
9096 :
9097 1802 : if (eErr == CE_None)
9098 : {
9099 1802 : m_gt = gt;
9100 1802 : m_bGeoTransformValid = true;
9101 : }
9102 :
9103 1802 : return eErr;
9104 : }
9105 :
9106 : /************************************************************************/
9107 : /* SetGCPs() */
9108 : /************************************************************************/
9109 :
9110 23 : CPLErr GTiffDataset::SetGCPs(int nGCPCountIn, const GDAL_GCP *pasGCPListIn,
9111 : const OGRSpatialReference *poGCPSRS)
9112 : {
9113 23 : CPLErr eErr = CE_None;
9114 23 : LoadGeoreferencingAndPamIfNeeded();
9115 23 : LookForProjection();
9116 :
9117 23 : if (eAccess == GA_Update)
9118 : {
9119 21 : if (!m_aoGCPs.empty() && nGCPCountIn == 0)
9120 : {
9121 3 : m_bForceUnsetGTOrGCPs = true;
9122 : }
9123 18 : else if (nGCPCountIn > 0 && m_bGeoTransformValid)
9124 : {
9125 5 : ReportError(CE_Warning, CPLE_AppDefined,
9126 : "A geotransform previously set is going to be cleared "
9127 : "due to the setting of GCPs.");
9128 5 : m_gt = GDALGeoTransform();
9129 5 : m_bGeoTransformValid = false;
9130 5 : m_bForceUnsetGTOrGCPs = true;
9131 : }
9132 21 : if ((m_eProfile == GTiffProfile::BASELINE) &&
9133 0 : (GetPamFlags() & GPF_DISABLED) == 0)
9134 : {
9135 0 : eErr = GDALPamDataset::SetGCPs(nGCPCountIn, pasGCPListIn, poGCPSRS);
9136 : }
9137 : else
9138 : {
9139 21 : if (nGCPCountIn > knMAX_GCP_COUNT)
9140 : {
9141 2 : if (GDALPamDataset::GetGCPCount() == 0 && !m_aoGCPs.empty())
9142 : {
9143 1 : m_bForceUnsetGTOrGCPs = true;
9144 : }
9145 2 : ReportError(CE_Warning, CPLE_AppDefined,
9146 : "Trying to write %d GCPs, whereas the maximum "
9147 : "supported in GeoTIFF tag is %d. "
9148 : "Falling back to writing them to PAM",
9149 : nGCPCountIn, knMAX_GCP_COUNT);
9150 2 : eErr = GDALPamDataset::SetGCPs(nGCPCountIn, pasGCPListIn,
9151 : poGCPSRS);
9152 : }
9153 19 : else if (GDALPamDataset::GetGCPCount() > 0)
9154 : {
9155 : // Cancel any existing GCPs from PAM file.
9156 1 : GDALPamDataset::SetGCPs(
9157 : 0, nullptr,
9158 : static_cast<const OGRSpatialReference *>(nullptr));
9159 : }
9160 21 : m_bGeoTIFFInfoChanged = true;
9161 : }
9162 : }
9163 : else
9164 : {
9165 2 : CPLDebug("GTIFF", "SetGCPs() goes to PAM instead of TIFF tags");
9166 2 : eErr = GDALPamDataset::SetGCPs(nGCPCountIn, pasGCPListIn, poGCPSRS);
9167 : }
9168 :
9169 23 : if (eErr == CE_None)
9170 : {
9171 23 : if (poGCPSRS == nullptr || poGCPSRS->IsEmpty())
9172 : {
9173 12 : if (!m_oSRS.IsEmpty())
9174 : {
9175 5 : m_bForceUnsetProjection = true;
9176 : }
9177 12 : m_oSRS.Clear();
9178 : }
9179 : else
9180 : {
9181 11 : m_oSRS = *poGCPSRS;
9182 11 : m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
9183 : }
9184 :
9185 23 : m_aoGCPs = gdal::GCP::fromC(pasGCPListIn, nGCPCountIn);
9186 : }
9187 :
9188 23 : return eErr;
9189 : }
9190 :
9191 : /************************************************************************/
9192 : /* SetMetadata() */
9193 : /************************************************************************/
9194 2689 : CPLErr GTiffDataset::SetMetadata(CSLConstList papszMD, const char *pszDomain)
9195 :
9196 : {
9197 2689 : LoadGeoreferencingAndPamIfNeeded();
9198 :
9199 2689 : if (m_bStreamingOut && m_bCrystalized)
9200 : {
9201 1 : ReportError(
9202 : CE_Failure, CPLE_NotSupported,
9203 : "Cannot modify metadata at that point in a streamed output file");
9204 1 : return CE_Failure;
9205 : }
9206 :
9207 2688 : if (pszDomain && EQUAL(pszDomain, "json:ISIS3"))
9208 : {
9209 5 : m_oISIS3Metadata.Deinit();
9210 5 : m_oMapISIS3MetadataItems.clear();
9211 : }
9212 :
9213 2688 : CPLErr eErr = CE_None;
9214 2688 : if (eAccess == GA_Update)
9215 : {
9216 2685 : if (pszDomain != nullptr && EQUAL(pszDomain, MD_DOMAIN_RPC))
9217 : {
9218 : // So that a subsequent GetMetadata() wouldn't override our new
9219 : // values
9220 22 : LoadMetadata();
9221 22 : m_bForceUnsetRPC = (CSLCount(papszMD) == 0);
9222 : }
9223 :
9224 2685 : if ((papszMD != nullptr) && (pszDomain != nullptr) &&
9225 1851 : EQUAL(pszDomain, "COLOR_PROFILE"))
9226 : {
9227 0 : m_bColorProfileMetadataChanged = true;
9228 : }
9229 2685 : else if (pszDomain == nullptr || !EQUAL(pszDomain, "_temporary_"))
9230 : {
9231 2685 : m_bMetadataChanged = true;
9232 : // Cancel any existing metadata from PAM file.
9233 2685 : if (GDALPamDataset::GetMetadata(pszDomain) != nullptr)
9234 1 : GDALPamDataset::SetMetadata(nullptr, pszDomain);
9235 : }
9236 :
9237 5334 : if ((pszDomain == nullptr || EQUAL(pszDomain, "")) &&
9238 2649 : CSLFetchNameValue(papszMD, GDALMD_AREA_OR_POINT) != nullptr)
9239 : {
9240 2015 : const char *pszPrevValue = GetMetadataItem(GDALMD_AREA_OR_POINT);
9241 : const char *pszNewValue =
9242 2015 : CSLFetchNameValue(papszMD, GDALMD_AREA_OR_POINT);
9243 2015 : if (pszPrevValue == nullptr || pszNewValue == nullptr ||
9244 1593 : !EQUAL(pszPrevValue, pszNewValue))
9245 : {
9246 426 : LookForProjection();
9247 426 : m_bGeoTIFFInfoChanged = true;
9248 : }
9249 : }
9250 :
9251 2685 : if (pszDomain != nullptr && EQUAL(pszDomain, "xml:XMP"))
9252 : {
9253 2 : if (papszMD != nullptr && *papszMD != nullptr)
9254 : {
9255 1 : int nTagSize = static_cast<int>(strlen(*papszMD));
9256 1 : TIFFSetField(m_hTIFF, TIFFTAG_XMLPACKET, nTagSize, *papszMD);
9257 : }
9258 : else
9259 : {
9260 1 : TIFFUnsetField(m_hTIFF, TIFFTAG_XMLPACKET);
9261 : }
9262 : }
9263 : }
9264 : else
9265 : {
9266 3 : CPLDebug(
9267 : "GTIFF",
9268 : "GTiffDataset::SetMetadata() goes to PAM instead of TIFF tags");
9269 3 : eErr = GDALPamDataset::SetMetadata(papszMD, pszDomain);
9270 : }
9271 :
9272 2688 : if (eErr == CE_None)
9273 : {
9274 2688 : eErr = m_oGTiffMDMD.SetMetadata(papszMD, pszDomain);
9275 : }
9276 2688 : return eErr;
9277 : }
9278 :
9279 : /************************************************************************/
9280 : /* SetMetadataItem() */
9281 : /************************************************************************/
9282 :
9283 5815 : CPLErr GTiffDataset::SetMetadataItem(const char *pszName, const char *pszValue,
9284 : const char *pszDomain)
9285 :
9286 : {
9287 5815 : LoadGeoreferencingAndPamIfNeeded();
9288 :
9289 5815 : if (m_bStreamingOut && m_bCrystalized)
9290 : {
9291 1 : ReportError(
9292 : CE_Failure, CPLE_NotSupported,
9293 : "Cannot modify metadata at that point in a streamed output file");
9294 1 : return CE_Failure;
9295 : }
9296 :
9297 5814 : if (pszDomain && EQUAL(pszDomain, "json:ISIS3"))
9298 : {
9299 1 : ReportError(CE_Failure, CPLE_NotSupported,
9300 : "Updating part of json:ISIS3 is not supported. "
9301 : "Use SetMetadata() instead");
9302 1 : return CE_Failure;
9303 : }
9304 :
9305 5813 : CPLErr eErr = CE_None;
9306 5813 : if (eAccess == GA_Update)
9307 : {
9308 5806 : if ((pszDomain != nullptr) && EQUAL(pszDomain, "COLOR_PROFILE"))
9309 : {
9310 8 : m_bColorProfileMetadataChanged = true;
9311 : }
9312 5798 : else if (pszDomain == nullptr || !EQUAL(pszDomain, "_temporary_"))
9313 : {
9314 5798 : m_bMetadataChanged = true;
9315 : // Cancel any existing metadata from PAM file.
9316 5798 : if (GDALPamDataset::GetMetadataItem(pszName, pszDomain) != nullptr)
9317 1 : GDALPamDataset::SetMetadataItem(pszName, nullptr, pszDomain);
9318 : }
9319 :
9320 5806 : if ((pszDomain == nullptr || EQUAL(pszDomain, "")) &&
9321 82 : pszName != nullptr && EQUAL(pszName, GDALMD_AREA_OR_POINT))
9322 : {
9323 7 : LookForProjection();
9324 7 : m_bGeoTIFFInfoChanged = true;
9325 : }
9326 : }
9327 : else
9328 : {
9329 7 : CPLDebug(
9330 : "GTIFF",
9331 : "GTiffDataset::SetMetadataItem() goes to PAM instead of TIFF tags");
9332 7 : eErr = GDALPamDataset::SetMetadataItem(pszName, pszValue, pszDomain);
9333 : }
9334 :
9335 5813 : if (eErr == CE_None)
9336 : {
9337 5813 : eErr = m_oGTiffMDMD.SetMetadataItem(pszName, pszValue, pszDomain);
9338 : }
9339 :
9340 5813 : return eErr;
9341 : }
9342 :
9343 : /************************************************************************/
9344 : /* CreateMaskBand() */
9345 : /************************************************************************/
9346 :
9347 98 : CPLErr GTiffDataset::CreateMaskBand(int nFlagsIn)
9348 : {
9349 98 : ScanDirectories();
9350 :
9351 98 : if (m_poMaskDS != nullptr)
9352 : {
9353 1 : ReportError(CE_Failure, CPLE_AppDefined,
9354 : "This TIFF dataset has already an internal mask band");
9355 1 : return CE_Failure;
9356 : }
9357 97 : else if (MustCreateInternalMask())
9358 : {
9359 84 : if (nFlagsIn != GMF_PER_DATASET)
9360 : {
9361 1 : ReportError(CE_Failure, CPLE_AppDefined,
9362 : "The only flag value supported for internal mask is "
9363 : "GMF_PER_DATASET");
9364 1 : return CE_Failure;
9365 : }
9366 :
9367 83 : int l_nCompression = COMPRESSION_PACKBITS;
9368 83 : if (strstr(GDALGetMetadataItem(GDALGetDriverByName("GTiff"),
9369 : GDAL_DMD_CREATIONOPTIONLIST, nullptr),
9370 83 : "<Value>DEFLATE</Value>") != nullptr)
9371 83 : l_nCompression = COMPRESSION_ADOBE_DEFLATE;
9372 :
9373 : /* --------------------------------------------------------------------
9374 : */
9375 : /* If we don't have read access, then create the mask externally.
9376 : */
9377 : /* --------------------------------------------------------------------
9378 : */
9379 83 : if (GetAccess() != GA_Update)
9380 : {
9381 1 : ReportError(CE_Warning, CPLE_AppDefined,
9382 : "File open for read-only accessing, "
9383 : "creating mask externally.");
9384 :
9385 1 : return GDALPamDataset::CreateMaskBand(nFlagsIn);
9386 : }
9387 :
9388 82 : if (m_bLayoutIFDSBeforeData && !m_bKnownIncompatibleEdition &&
9389 0 : !m_bWriteKnownIncompatibleEdition)
9390 : {
9391 0 : ReportError(CE_Warning, CPLE_AppDefined,
9392 : "Adding a mask invalidates the "
9393 : "LAYOUT=IFDS_BEFORE_DATA property");
9394 0 : m_bKnownIncompatibleEdition = true;
9395 0 : m_bWriteKnownIncompatibleEdition = true;
9396 : }
9397 :
9398 82 : bool bIsOverview = false;
9399 82 : uint32_t nSubType = 0;
9400 82 : if (TIFFGetField(m_hTIFF, TIFFTAG_SUBFILETYPE, &nSubType))
9401 : {
9402 8 : bIsOverview = (nSubType & FILETYPE_REDUCEDIMAGE) != 0;
9403 :
9404 8 : if ((nSubType & FILETYPE_MASK) != 0)
9405 : {
9406 0 : ReportError(CE_Failure, CPLE_AppDefined,
9407 : "Cannot create a mask on a TIFF mask IFD !");
9408 0 : return CE_Failure;
9409 : }
9410 : }
9411 :
9412 82 : const int bIsTiled = TIFFIsTiled(m_hTIFF);
9413 :
9414 82 : FlushDirectory();
9415 :
9416 82 : const toff_t nOffset = GTIFFWriteDirectory(
9417 : m_hTIFF,
9418 : bIsOverview ? FILETYPE_REDUCEDIMAGE | FILETYPE_MASK : FILETYPE_MASK,
9419 : nRasterXSize, nRasterYSize, 1, PLANARCONFIG_CONTIG, 1,
9420 : m_nBlockXSize, m_nBlockYSize, bIsTiled, l_nCompression,
9421 : PHOTOMETRIC_MASK, PREDICTOR_NONE, SAMPLEFORMAT_UINT, nullptr,
9422 : nullptr, nullptr, 0, nullptr, "", nullptr, nullptr, nullptr,
9423 82 : nullptr, m_bWriteCOGLayout);
9424 :
9425 82 : ReloadDirectory();
9426 :
9427 82 : if (nOffset == 0)
9428 0 : return CE_Failure;
9429 :
9430 82 : m_poMaskDS = std::make_shared<GTiffDataset>();
9431 82 : m_poMaskDS->eAccess = GA_Update;
9432 82 : m_poMaskDS->m_poBaseDS = this;
9433 82 : m_poMaskDS->m_poImageryDS = this;
9434 82 : m_poMaskDS->ShareLockWithParentDataset(this);
9435 82 : m_poMaskDS->m_osFilename = m_osFilename;
9436 82 : m_poMaskDS->m_bPromoteTo8Bits = CPLTestBool(
9437 : CPLGetConfigOption("GDAL_TIFF_INTERNAL_MASK_TO_8BIT", "YES"));
9438 82 : return m_poMaskDS->OpenOffset(VSI_TIFFOpenChild(m_hTIFF), nOffset,
9439 82 : GA_Update);
9440 : }
9441 :
9442 13 : return GDALPamDataset::CreateMaskBand(nFlagsIn);
9443 : }
9444 :
9445 : /************************************************************************/
9446 : /* MustCreateInternalMask() */
9447 : /************************************************************************/
9448 :
9449 135 : bool GTiffDataset::MustCreateInternalMask()
9450 : {
9451 135 : return CPLTestBool(CPLGetConfigOption("GDAL_TIFF_INTERNAL_MASK", "YES"));
9452 : }
9453 :
9454 : /************************************************************************/
9455 : /* CreateMaskBand() */
9456 : /************************************************************************/
9457 :
9458 29 : CPLErr GTiffRasterBand::CreateMaskBand(int nFlagsIn)
9459 : {
9460 29 : m_poGDS->ScanDirectories();
9461 :
9462 29 : if (m_poGDS->m_poMaskDS != nullptr)
9463 : {
9464 5 : ReportError(CE_Failure, CPLE_AppDefined,
9465 : "This TIFF dataset has already an internal mask band");
9466 5 : return CE_Failure;
9467 : }
9468 :
9469 : const char *pszGDAL_TIFF_INTERNAL_MASK =
9470 24 : CPLGetConfigOption("GDAL_TIFF_INTERNAL_MASK", nullptr);
9471 27 : if ((pszGDAL_TIFF_INTERNAL_MASK &&
9472 24 : CPLTestBool(pszGDAL_TIFF_INTERNAL_MASK)) ||
9473 : nFlagsIn == GMF_PER_DATASET)
9474 : {
9475 16 : return m_poGDS->CreateMaskBand(nFlagsIn);
9476 : }
9477 :
9478 8 : return GDALPamRasterBand::CreateMaskBand(nFlagsIn);
9479 : }
9480 :
9481 : /************************************************************************/
9482 : /* ClampCTEntry() */
9483 : /************************************************************************/
9484 :
9485 235119 : /* static */ unsigned short GTiffDataset::ClampCTEntry(int iColor, int iComp,
9486 : int nCTEntryVal,
9487 : int nMultFactor)
9488 : {
9489 235119 : const int nVal = nCTEntryVal * nMultFactor;
9490 235119 : if (nVal < 0)
9491 : {
9492 0 : CPLError(CE_Warning, CPLE_AppDefined,
9493 : "Color table entry [%d][%d] = %d, clamped to 0", iColor, iComp,
9494 : nCTEntryVal);
9495 0 : return 0;
9496 : }
9497 235119 : if (nVal > 65535)
9498 : {
9499 2 : CPLError(CE_Warning, CPLE_AppDefined,
9500 : "Color table entry [%d][%d] = %d, clamped to 65535", iColor,
9501 : iComp, nCTEntryVal);
9502 2 : return 65535;
9503 : }
9504 235117 : return static_cast<unsigned short>(nVal);
9505 : }
|