Skip to content

Commit 64116d8

Browse files
authored
Merge pull request #12778 from rouault/tiff_rgbnir
GTIFF: fix creating a R,G,B,Nir file without explicit PHOTOMETRIC creation option
2 parents e8f2181 + 99e851a commit 64116d8

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

autotest/gcore/tiff_write.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5350,6 +5350,41 @@ def test_tiff_write_123():
53505350
gdaltest.tiff_drv.Delete("/vsimem/tiff_write_123_rgba_to_undefined.tif")
53515351

53525352

5353+
###############################################################################
5354+
# Test implicit photometric interpretation
5355+
5356+
5357+
def test_tiff_write_RGBNir(tmp_vsimem):
5358+
5359+
out_filename = tmp_vsimem / "test_tiff_write_RGBNir.tif"
5360+
ds = gdaltest.tiff_drv.Create(out_filename, 1, 1, 4, gdal.GDT_Int16)
5361+
ds.GetRasterBand(1).SetColorInterpretation(gdal.GCI_RedBand)
5362+
ds.GetRasterBand(2).SetColorInterpretation(gdal.GCI_GreenBand)
5363+
ds.GetRasterBand(3).SetColorInterpretation(gdal.GCI_BlueBand)
5364+
ds.GetRasterBand(4).SetColorInterpretation(gdal.GCI_NIRBand)
5365+
5366+
errors = []
5367+
5368+
def handler(lvl, no, msg):
5369+
errors.append({"level": lvl, "number": no, "message": msg})
5370+
5371+
with gdaltest.error_handler(handler):
5372+
ds.Close()
5373+
5374+
assert len(errors) == 0
5375+
5376+
statBuf = gdal.VSIStatL(
5377+
str(out_filename) + ".aux.xml",
5378+
gdal.VSI_STAT_EXISTS_FLAG | gdal.VSI_STAT_NATURE_FLAG | gdal.VSI_STAT_SIZE_FLAG,
5379+
)
5380+
assert statBuf is None, "did not expect PAM file"
5381+
src_ds = gdal.Open(out_filename)
5382+
assert src_ds.GetRasterBand(1).GetColorInterpretation() == gdal.GCI_RedBand
5383+
assert src_ds.GetRasterBand(2).GetColorInterpretation() == gdal.GCI_GreenBand
5384+
assert src_ds.GetRasterBand(3).GetColorInterpretation() == gdal.GCI_BlueBand
5385+
assert src_ds.GetRasterBand(4).GetColorInterpretation() == gdal.GCI_NIRBand
5386+
5387+
53535388
###############################################################################
53545389
# Test error cases with palette creation
53555390

frmts/gtiff/gtiffrasterband_write.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,12 +465,14 @@ CPLErr GTiffRasterBand::SetColorInterpretation(GDALColorInterp eInterp)
465465
}
466466
}
467467

468-
// Mark alpha band / undefined in extrasamples.
469-
if (eInterp == GCI_AlphaBand || eInterp == GCI_Undefined)
468+
// Mark non-RGB in extrasamples.
469+
if (eInterp != GCI_RedBand && eInterp != GCI_GreenBand &&
470+
eInterp != GCI_BlueBand)
470471
{
471472
uint16_t *v = nullptr;
472473
uint16_t count = 0;
473-
if (TIFFGetField(m_poGDS->m_hTIFF, TIFFTAG_EXTRASAMPLES, &count, &v))
474+
if (TIFFGetField(m_poGDS->m_hTIFF, TIFFTAG_EXTRASAMPLES, &count, &v) &&
475+
count > 0)
474476
{
475477
const int nBaseSamples = m_poGDS->m_nSamplesPerPixel - count;
476478

0 commit comments

Comments
 (0)