Repository: opencv/opencv
File: modules/dnn/src/layers/resize2_layer.cpp (C++)

Code before:

enum class CoordTransMode {
    HALF_PIXEL,
    PYTORCH_HALF_PIXEL,
    TF_HALF_PIXEL_FOR_NN,
    TF_CROP_AND_RESIZE,
    ASYMMETRIC
};

static inline CoordTransMode parseCoordTransMode(const String& s)
{
    if (s == "half_pixel") return CoordTransMode::HALF_PIXEL;
    if (s == "pytorch_half_pixel") return CoordTransMode::PYTORCH_HALF_PIXEL;
    if (s == "tf_half_pixel_for_nn") return CoordTransMode::TF_HALF_PIXEL_FOR_NN;
    if (s == "tf_crop_and_resize") return CoordTransMode::TF_CROP_AND_RESIZE;
    return CoordTransMode::ASYMMETRIC;
}

enum class NearestMode {
    FLOOR,
    CEIL,
    ROUND_PREFER_CEIL,
    ROUND_PREFER_FLOOR
};

static inline NearestMode parseNearestMode(const String& s)
{
    if (s == "floor") return NearestMode::FLOOR;
    if (s == "ceil") return NearestMode::CEIL;
    if (s == "round_prefer_ceil") return NearestMode::ROUND_PREFER_CEIL;
    return NearestMode::ROUND_PREFER_FLOOR;
}

static constexpr int kResizeNumStripes = 16;

inline float computeSrcGeneric(int dst, float scale, int limit, int len,
                               CoordTransMode coordTransMode, bool /*halfPixelCenters*/,
                               float start_coord = 0.0f, float end_coord = 1.0f)
{
    if (coordTransMode == CoordTransMode::TF_CROP_AND_RESIZE)
    {
        if (len > 1)
            return start_coord * (limit - 1) + dst * (end_coord - start_coord) * (limit - 1) / float(len - 1);
        else
            return 0.5f * (start_coord + end_coord) * (limit - 1);
    }
    if (coordTransMode == CoordTransMode::PYTORCH_HALF_PIXEL)
        return (len > 1) ? (dst + 0.5f)*scale - 0.5f : 0.f;
    if (coordTransMode == CoordTransMode::HALF_PIXEL)
        return (dst + 0.5f)*scale - 0.5f;
    if (coordTransMode == CoordTransMode::TF_HALF_PIXEL_FOR_NN)

Review comment (suggestion):
I used to think C++17 is already a minimal supported version for 5.x: https://github.com/opencv/opencv/pull/26287

Code after:

enum class CoordTransMode {
    HALF_PIXEL,
    PYTORCH_HALF_PIXEL,
    TF_HALF_PIXEL_FOR_NN,
    TF_CROP_AND_RESIZE,
    ASYMMETRIC
};

static inline CoordTransMode parseCoordTransMode(const String& s)
{
    if (s == "half_pixel") return CoordTransMode::HALF_PIXEL;
    if (s == "pytorch_half_pixel") return CoordTransMode::PYTORCH_HALF_PIXEL;
    if (s == "tf_half_pixel_for_nn") return CoordTransMode::TF_HALF_PIXEL_FOR_NN;
    if (s == "tf_crop_and_resize") return CoordTransMode::TF_CROP_AND_RESIZE;
    return CoordTransMode::ASYMMETRIC;
}

enum class NearestMode {
    FLOOR,
    CEIL,
    ROUND_PREFER_CEIL,
    ROUND_PREFER_FLOOR
};

#if __cplusplus < 201703L
template<typename T>
static T clamp(T d, T min, T max)
{
    return std::min(std::max(d, min), max);
}
#else
#define clamp std::clamp
#endif

static inline NearestMode parseNearestMode(const String& s)
{
    if (s == "floor") return NearestMode::FLOOR;
    if (s == "ceil") return NearestMode::CEIL;
    if (s == "round_prefer_ceil") return NearestMode::ROUND_PREFER_CEIL;
    return NearestMode::ROUND_PREFER_FLOOR;
}

static constexpr int kResizeNumStripes = 16;

inline float computeSrcGeneric(int dst, float scale, int limit, int len,
                               CoordTransMode coordTransMode, bool /*halfPixelCenters*/,
                               float start_coord = 0.0f, float end_coord = 1.0f)
{
    if (coordTransMode == CoordTransMode::TF_CROP_AND_RESIZE)
    {