/* * SPDX-FileCopyrightText: 2024 Dmitry Kazakov * * SPDX-License-Identifier: GPL-2.0-or-later */ #ifndef KOCOMPOSITEOPCLAMPPOLICY_H #define KOCOMPOSITEOPCLAMPPOLICY_H #include namespace KoCompositeOpClampPolicy { template struct ClampAsInteger { using channels_type = T; using compositetype = typename KoColorSpaceMathsTraits::compositetype; static inline channels_type clampResultAllowNegative(compositetype value) { using namespace Arithmetic; return clampToSDR(value); } static inline channels_type clampResult(compositetype value) { using namespace Arithmetic; return clampToSDR(value); } static inline channels_type clampInvertedResult(compositetype value) { using namespace Arithmetic; return clampToSDR(value); } static inline compositetype fixInfiniteAfterDivision(compositetype value) { return value; } static inline T clippedMaxValue() { using namespace Arithmetic; return unitValue(); } }; template struct ClampAsFloatSDR { using channels_type = T; using compositetype = typename KoColorSpaceMathsTraits::compositetype; static channels_type clampResultAllowNegative(compositetype value) { using namespace Arithmetic; return clampToSDR(value); } static inline channels_type clampResult(compositetype value) { using namespace Arithmetic; return clampToSDR(value); } static channels_type clampInvertedResult(compositetype value) { using namespace Arithmetic; return clampToSDR(value); } static compositetype fixInfiniteAfterDivision(compositetype value) { // Constantly dividing by small numbers can quickly make the result // become infinity or NaN, so we check that and correct (kind of clamping) return std::isfinite(value) ? value : compositetype(KoColorSpaceMathsTraits::unitValue); } static inline T clippedMaxValue() { using namespace Arithmetic; return unitValue(); } }; template struct ClampAsFloatHDR { using channels_type = T; using compositetype = typename KoColorSpaceMathsTraits::compositetype; static channels_type clampResultAllowNegative(compositetype value) { return value; } static inline channels_type clampResult(compositetype value) { using namespace Arithmetic; return clampToSDRBottom(value); } static channels_type clampInvertedResult(compositetype value) { using namespace Arithmetic; return clampToSDRTop(value); } static compositetype fixInfiniteAfterDivision(compositetype value) { // Constantly dividing by small numbers can quickly make the result // become infinity or NaN, so we check that and correct (kind of clamping) return std::isfinite(value) ? value : compositetype(KoColorSpaceMathsTraits::max); } static inline T clippedMaxValue() { return KoColorSpaceMathsTraits::max; } }; template