Skip to content

Commit

Permalink
Merge pull request #9100 from rouault/GDAL_OVERVIEW_OVERSAMPLING_THRE…
Browse files Browse the repository at this point in the history
…SHOLD_tune

GDALBandGetBestOverviewLevel2(): avoid numerical instability with GDAL_OVERVIEW_OVERSAMPLING_THRESHOLD = 1.0
  • Loading branch information
rouault authored Jan 25, 2024
2 parents 862b44d + 9a0494a commit da8d811
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions gcore/rasterio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3602,11 +3602,20 @@ int GDALBandGetBestOverviewLevel2(GDALRasterBand *poBand, int &nXOff,

const char *pszOversampligThreshold =
CPLGetConfigOption("GDAL_OVERVIEW_OVERSAMPLING_THRESHOLD", nullptr);
const double dfOversamplingThreshold =

// Cf https://github.com/OSGeo/gdal/pull/9040#issuecomment-1898524693
// Do not exactly use a oversampling threshold of 1.0 because of numerical
// instability.
const auto AdjustThreshold = [](double x)
{
constexpr double EPS = 1e-2;
return x == 1.0 ? x + EPS : x;
};
const double dfOversamplingThreshold = AdjustThreshold(
pszOversampligThreshold ? CPLAtof(pszOversampligThreshold)
: psExtraArg && psExtraArg->eResampleAlg != GRIORA_NearestNeighbour
? 1.0
: 1.2;
: 1.2);
for (int iOverview = 0; iOverview < nOverviewCount; iOverview++)
{
GDALRasterBand *poOverview = poBand->GetOverview(iOverview);
Expand All @@ -3624,11 +3633,8 @@ int GDALBandGetBestOverviewLevel2(GDALRasterBand *poBand, int &nXOff,

// Is it nearly the requested factor and better (lower) than
// the current best factor?
if ((dfOversamplingThreshold == 1.0 &&
dfDownsamplingFactor > dfDesiredDownsamplingFactor) ||
(dfOversamplingThreshold > 1.0 &&
dfDownsamplingFactor >=
dfDesiredDownsamplingFactor * dfOversamplingThreshold) ||
if (dfDownsamplingFactor >=
dfDesiredDownsamplingFactor * dfOversamplingThreshold ||
dfDownsamplingFactor <= dfBestDownsamplingFactor)
{
continue;
Expand Down

0 comments on commit da8d811

Please sign in to comment.