Skip to content

Commit

Permalink
Merge pull request #9403 from rouault/fix_9402
Browse files Browse the repository at this point in the history
gdal_translate -expand rgb[a]: automatically select Byte output data …
  • Loading branch information
rouault authored Mar 11, 2024
2 parents ccad35a + a8134aa commit 48227ca
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
37 changes: 35 additions & 2 deletions apps/gdal_translate_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1917,6 +1917,8 @@ GDALDatasetH GDALTranslate(const char *pszDest, GDALDatasetH hSrcDataset,
/* ==================================================================== */
/* Process all bands. */
/* ==================================================================== */
GDALDataType eOutputType = psOptions->eOutputType;

for (int i = 0; i < psOptions->nBandCount; i++)
{
int nComponent = 0;
Expand Down Expand Up @@ -1947,13 +1949,44 @@ GDALDatasetH GDALTranslate(const char *pszDest, GDALDatasetH hSrcDataset,
GDALRasterBand *poRealSrcBand =
(nSrcBand < 0) ? poSrcBand->GetMaskBand() : poSrcBand;
GDALDataType eBandType;
if (psOptions->eOutputType == GDT_Unknown)
if (eOutputType == GDT_Unknown)
{
eBandType = poRealSrcBand->GetRasterDataType();
if (eBandType != GDT_Byte && psOptions->nRGBExpand != 0)
{
// Use case of https://github.com/OSGeo/gdal/issues/9402
if (const auto poColorTable = poRealSrcBand->GetColorTable())
{
bool bIn0To255Range = true;
const int nColorCount = poColorTable->GetColorEntryCount();
for (int nColor = 0; nColor < nColorCount; nColor++)
{
const GDALColorEntry *poEntry =
poColorTable->GetColorEntry(nColor);
if (poEntry->c1 > 255 || poEntry->c2 > 255 ||
poEntry->c3 > 255 || poEntry->c4 > 255)
{
bIn0To255Range = false;
break;
}
}
if (bIn0To255Range)
{
if (!psOptions->bQuiet)
{
CPLError(CE_Warning, CPLE_AppDefined,
"Using Byte output data type due to range "
"of values in color table");
}
eBandType = GDT_Byte;
}
}
eOutputType = eBandType;
}
}
else
{
eBandType = psOptions->eOutputType;
eBandType = eOutputType;

// Check that we can copy existing statistics
GDALDataType eSrcBandType = poRealSrcBand->GetRasterDataType();
Expand Down
31 changes: 31 additions & 0 deletions autotest/utilities/test_gdal_translate_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1249,3 +1249,34 @@ def test_gdal_translate_ovr_rpc():
assert float(ovr_rpc["SAMP_SCALE"]) == pytest.approx(
0.5 * float(src_rpc["SAMP_SCALE"])
)


###############################################################################
# Test scenario of https://github.com/OSGeo/gdal/issues/9402


def test_gdal_translate_lib_raster_uint16_ct_0_255_range():

for (r, g, b, a) in [
(255 + 1, 255, 255, 255),
(255, 255 + 1, 255, 255),
(255, 255, 255 + 1, 255),
(255, 255, 255, 255 + 1),
]:
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1, 1, gdal.GDT_UInt16)
ct = gdal.ColorTable()
ct.SetColorEntry(0, (r, g, b, a))
src_ds.GetRasterBand(1).SetRasterColorTable(ct)
out_ds = gdal.Translate("", src_ds, format="MEM", rgbExpand="rgb")
assert out_ds.GetRasterBand(1).DataType == gdal.GDT_UInt16
assert out_ds.GetRasterBand(2).DataType == gdal.GDT_UInt16
assert out_ds.GetRasterBand(3).DataType == gdal.GDT_UInt16

src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1, 1, gdal.GDT_UInt16)
ct = gdal.ColorTable()
ct.SetColorEntry(0, (255, 255, 255, 255))
src_ds.GetRasterBand(1).SetRasterColorTable(ct)
out_ds = gdal.Translate("", src_ds, format="MEM", rgbExpand="rgb")
assert out_ds.GetRasterBand(1).DataType == gdal.GDT_Byte
assert out_ds.GetRasterBand(2).DataType == gdal.GDT_Byte
assert out_ds.GetRasterBand(3).DataType == gdal.GDT_Byte

0 comments on commit 48227ca

Please sign in to comment.