Skip to content

Commit

Permalink
GDALWarpResolveWorkingDataType(): ignore srcNoDataValue if it doesn't…
Browse files Browse the repository at this point in the history
… fit into the data type of the source band
  • Loading branch information
rouault committed Sep 1, 2024
1 parent ddfdd08 commit 2ac41d7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
26 changes: 26 additions & 0 deletions alg/gdalwarper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,32 @@ void CPL_STDCALL GDALWarpResolveWorkingDataType(GDALWarpOptions *psOptions)

psOptions->eWorkingDataType = GDT_Byte;

// If none of the provided input nodata values can be represented in the
// data type of the corresponding source band, ignore them.
if (psOptions->hSrcDS && psOptions->padfSrcNoDataReal)
{
int nCountInvalidSrcNoDataReal = 0;
for (int iBand = 0; iBand < psOptions->nBandCount; iBand++)
{
GDALRasterBandH hSrcBand = GDALGetRasterBand(
psOptions->hSrcDS, psOptions->panSrcBands[iBand]);

if (hSrcBand &&
!GDALIsValueExactAs(psOptions->padfSrcNoDataReal[iBand],
GDALGetRasterDataType(hSrcBand)))
{
nCountInvalidSrcNoDataReal++;
}
}
if (nCountInvalidSrcNoDataReal == psOptions->nBandCount)
{
CPLFree(psOptions->padfSrcNoDataReal);
psOptions->padfSrcNoDataReal = nullptr;
CPLFree(psOptions->padfSrcNoDataImag);
psOptions->padfSrcNoDataImag = nullptr;
}
}

for (int iBand = 0; iBand < psOptions->nBandCount; iBand++)
{
if (psOptions->hDstDS != nullptr)
Expand Down
29 changes: 29 additions & 0 deletions autotest/cpp/test_alg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,35 @@ TEST_F(test_alg, GDALWarpResolveWorkingDataType_padfSrcNoDataReal)
GDALDestroyWarpOptions(psOptions);
}

// GDALWarpResolveWorkingDataType: effect of padfSrcNoDataReal
TEST_F(test_alg, GDALWarpResolveWorkingDataType_padfSrcNoDataReal_with_band)
{
GDALDatasetUniquePtr poDS(GDALDriver::FromHandle(GDALGetDriverByName("MEM"))
->Create("", 1, 1, 1, GDT_Byte, nullptr));
GDALWarpOptions *psOptions = GDALCreateWarpOptions();
psOptions->hSrcDS = GDALDataset::ToHandle(poDS.get());
psOptions->nBandCount = 1;
psOptions->panSrcBands =
static_cast<int *>(CPLMalloc(psOptions->nBandCount * sizeof(int)));
psOptions->panSrcBands[0] = 1;
psOptions->padfSrcNoDataReal =
static_cast<double *>(CPLMalloc(sizeof(double)));
psOptions->padfSrcNoDataReal[0] = 0.0;
GDALWarpResolveWorkingDataType(psOptions);
EXPECT_EQ(psOptions->eWorkingDataType, GDT_Byte);

psOptions->padfSrcNoDataReal[0] = -1.0;
GDALWarpResolveWorkingDataType(psOptions);
EXPECT_EQ(psOptions->eWorkingDataType, GDT_Byte);

psOptions->eWorkingDataType = GDT_Unknown;
psOptions->padfSrcNoDataReal[0] = 2.0;
GDALWarpResolveWorkingDataType(psOptions);
EXPECT_EQ(psOptions->eWorkingDataType, GDT_Byte);

GDALDestroyWarpOptions(psOptions);
}

// GDALWarpResolveWorkingDataType: effect of padfSrcNoDataImag
TEST_F(test_alg, GDALWarpResolveWorkingDataType_padfSrcNoDataImag)
{
Expand Down

0 comments on commit 2ac41d7

Please sign in to comment.