| 4570 | /************************************************************************/ |
| 4571 | |
| 4572 | int GDALBandGetBestOverviewLevel(GDALRasterBand *poBand, |
| 4573 | double dfTargetDownsamplingRatio, |
| 4574 | double dfOversamplingThreshold) |
| 4575 | { |
| 4576 | int iBestOvr = -1; |
| 4577 | double dfBestRatio = 0; |
| 4578 | const int nOvCount = poBand->GetOverviewCount(); |
| 4579 | constexpr double EPSILON = 1e-1; |
| 4580 | for (int iOvr = -1; iOvr < nOvCount; iOvr++) |
| 4581 | { |
| 4582 | double dfOvrRatio = 1.0; |
| 4583 | GDALRasterBand *poOvrBand = nullptr; |
| 4584 | if (iOvr >= 0) |
| 4585 | { |
| 4586 | poOvrBand = poBand->GetOverview(iOvr); |
| 4587 | if (poOvrBand == nullptr || |
| 4588 | poOvrBand->GetXSize() > poBand->GetXSize() || |
| 4589 | poOvrBand->GetYSize() > poBand->GetYSize()) |
| 4590 | { |
| 4591 | continue; |
| 4592 | } |
| 4593 | dfOvrRatio = std::min(static_cast<double>(poBand->GetXSize()) / |
| 4594 | poOvrBand->GetXSize(), |
| 4595 | static_cast<double>(poBand->GetYSize()) / |
| 4596 | poOvrBand->GetYSize()); |
| 4597 | } |
| 4598 | |
| 4599 | // Is it nearly the requested factor and better (lower) than |
| 4600 | // the current best factor? |
| 4601 | // Use an epsilon because of numerical instability. |
| 4602 | if (dfOvrRatio >= |
| 4603 | dfTargetDownsamplingRatio * dfOversamplingThreshold + EPSILON || |
| 4604 | dfOvrRatio <= dfBestRatio) |
| 4605 | { |
| 4606 | continue; |
| 4607 | } |
| 4608 | |
| 4609 | if (poOvrBand) |
| 4610 | { |
| 4611 | // Ignore AVERAGE_BIT2GRAYSCALE overviews. |
| 4612 | const char *pszResampling = |
| 4613 | poOvrBand->GetMetadataItem("RESAMPLING"); |
| 4614 | if (pszResampling != nullptr && |
| 4615 | STARTS_WITH_CI(pszResampling, "AVERAGE_BIT2")) |
| 4616 | { |
| 4617 | continue; |
| 4618 | } |
| 4619 | } |
| 4620 | |
| 4621 | iBestOvr = iOvr; |
| 4622 | dfBestRatio = dfOvrRatio; |
| 4623 | if (std::abs(dfTargetDownsamplingRatio - dfOvrRatio) < EPSILON) |
| 4624 | { |
| 4625 | break; |
| 4626 | } |
| 4627 | } |
| 4628 | return iBestOvr; |
| 4629 | } |
no test coverage detected