From ef68e3de5f45939bd3cbcd04d6188fd8499fbfb2 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Wed, 18 Dec 2024 01:54:11 +0100 Subject: [PATCH] GTiff creation: emit warning when PREDICTOR option is used with an incompatible codec --- autotest/gcore/tiff_write.py | 14 ++++++++++++++ frmts/gtiff/gtiffdataset_write.cpp | 17 +++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/autotest/gcore/tiff_write.py b/autotest/gcore/tiff_write.py index f75199fc0e04..25f58a827f9c 100755 --- a/autotest/gcore/tiff_write.py +++ b/autotest/gcore/tiff_write.py @@ -11974,3 +11974,17 @@ def test_tiff_write_float32_predictor_3_endianness(tmp_path): ) with gdal.Open(out_filename) as ds: assert ds.GetRasterBand(1).Checksum() == 4672 + + +############################################################################### +# + + +def test_tiff_write_warn_ignore_predictor_option(tmp_vsimem): + out_filename = str(tmp_vsimem / "out.tif") + gdal.ErrorReset() + with gdal.quiet_errors(): + gdal.GetDriverByName("GTiff").Create( + out_filename, 1, 1, options=["PREDICTOR=2"] + ) + assert "PREDICTOR option is ignored" in gdal.GetLastErrorMsg() diff --git a/frmts/gtiff/gtiffdataset_write.cpp b/frmts/gtiff/gtiffdataset_write.cpp index 23ead97bca5f..095b9d19a9e3 100644 --- a/frmts/gtiff/gtiffdataset_write.cpp +++ b/frmts/gtiff/gtiffdataset_write.cpp @@ -5297,9 +5297,22 @@ TIFF *GTiffDataset::CreateLL(const char *pszFilename, int nXSize, int nYSize, nPredictor = atoi(pszValue); } - // Do early checks as libtiff will only error out when starting to write. if (nPredictor != PREDICTOR_NONE && - CPLTestBool(CPLGetConfigOption("GDAL_GTIFF_PREDICTOR_CHECKS", "YES"))) + l_nCompression != COMPRESSION_ADOBE_DEFLATE && + l_nCompression != COMPRESSION_LZW && + l_nCompression != COMPRESSION_LZMA && + l_nCompression != COMPRESSION_ZSTD) + { + ReportError(pszFilename, CE_Warning, CPLE_NotSupported, + "PREDICTOR option is ignored for COMPRESS=%s. " + "Only valid for DEFLATE, LZW, LZMA or ZSTD", + CSLFetchNameValueDef(papszParamList, "COMPRESS", "NONE")); + } + + // Do early checks as libtiff will only error out when starting to write. + else if (nPredictor != PREDICTOR_NONE && + CPLTestBool( + CPLGetConfigOption("GDAL_GTIFF_PREDICTOR_CHECKS", "YES"))) { #if (TIFFLIB_VERSION > 20210416) || defined(INTERNAL_LIBTIFF) #define HAVE_PREDICTOR_2_FOR_64BIT