Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Port new utitility functions back to KoColorSpaceMaths
Dmitry Kazakov9cd5f6d7c07d
diff --git a/libs/pigment/KoColorSpaceMaths.h b/libs/pigment/KoColorSpaceMaths.hindex 8d10da1e29..1ad564fdba 100644--- a/libs/pigment/KoColorSpaceMaths.h+++ b/libs/pigment/KoColorSpaceMaths.h@@ -274,8 +274,20 @@ public:}inline static _T clampChannelToSDR(_T val) {- // integer version, float versions are specialized separately- return val;+ if constexpr (std::numeric_limits<_T>::is_integer) {+ return val;+ } else {+ return qBound<_T>(KoColorSpaceMathsTraits<_T>::zeroValue, val, KoColorSpaceMathsTraits<_T>::unitValue);+ }+ }++ inline static _T clampChannelToSDRBottom(_T val)+ {+ if constexpr (std::numeric_limits<_T>::is_integer) {+ return val;+ } else {+ return qMax<_T>(KoColorSpaceMathsTraits<_T>::zeroValue, val);+ }}inline static dst_compositetype clampToSDR(dst_compositetype val) {@@ -290,6 +302,22 @@ public:return qMax<dst_compositetype>(KoColorSpaceMathsTraits<_Tdst>::zeroValue, val);}+ inline static dst_compositetype divideInCompositeSpace(dst_compositetype a, dst_compositetype b) {+ if constexpr (std::numeric_limits<_Tdst>::is_integer) {+ return a * KoColorSpaceMathsTraits<_Tdst>::unitValue / b;+ } else {+ return a / b;+ }+ }++ inline static dst_compositetype multiplyInCompositeSpace(dst_compositetype a, dst_compositetype b) {+ if constexpr (std::numeric_limits<_Tdst>::is_integer) {+ return a * b / KoColorSpaceMathsTraits<_Tdst>::unitValue;+ } else {+ return a * b;+ }+ }+/*** Clamps the composite type on higher border only. That is a fast path* for scale-only transformations@@ -299,16 +327,35 @@ public:}inline static _T isUnsafeAsDivisor(_T value) {+ static_assert(std::numeric_limits<_Tdst>::is_integer);return value == KoColorSpaceMathsTraits<_T>::zeroValue;}inline static bool isUnitValue(_T value) {+ static_assert(std::numeric_limits<_Tdst>::is_integer);return value == KoColorSpaceMathsTraits<_T>::unitValue;}inline static bool isZeroValue(_T value) {+ static_assert(std::numeric_limits<_Tdst>::is_integer);return value == KoColorSpaceMathsTraits<_T>::zeroValue;}++ inline static bool isZeroValueClamped(_T v) {+ static_assert(std::numeric_limits<_Tdst>::is_integer);+ return v <= 0;+ }++ static inline bool isUnitValueClamped(_T v) {+ static_assert(std::numeric_limits<_Tdst>::is_integer);+ return v >= KoColorSpaceMathsTraits<_T>::unitValue;+ }++ static inline bool isHalfValue(_T v)+ {+ static_assert(std::numeric_limits<_Tdst>::is_integer);+ return v == KoColorSpaceMathsTraits<_T>::halfValue;+ }};//------------------------------ double specialization ------------------------------//@@ -349,6 +396,35 @@ inline double KoColorSpaceMaths<double>::isUnsafeAsDivisor(double value) {return value < 1e-6; // negative values are also unsafe!}+template<>+inline bool KoColorSpaceMaths<double>::isUnitValue(double value) {+ return qFuzzyCompare(value, KoColorSpaceMathsTraits<double>::unitValue);+}++template<>+inline bool KoColorSpaceMaths<double>::isZeroValue(double value) {+ return qFuzzyIsNull(value);+}++template<>+inline bool KoColorSpaceMaths<double>::isZeroValueClamped(double d)+{+ // constant is from qFuzzyIsNull()+ return d <= 0.000000000001;+}++template<>+inline bool KoColorSpaceMaths<double>::isUnitValueClamped(double d)+{+ // constant is from qFuzzyIsNull()+ return d > 1.0 - 0.000000000001;+}++template<>+inline bool KoColorSpaceMaths<double>::isHalfValue(double value) {+ return qFuzzyCompare(value, KoColorSpaceMathsTraits<double>::halfValue);+}+//------------------------------ float specialization ------------------------------//template<>@@ -417,8 +493,22 @@ inline bool KoColorSpaceMaths<float>::isZeroValue(float value) {}template<>-inline float KoColorSpaceMaths<float>::clampChannelToSDR(float val) {- return qBound<float>(KoColorSpaceMathsTraits<float>::zeroValue, val, KoColorSpaceMathsTraits<float>::unitValue);+inline bool KoColorSpaceMaths<float>::isZeroValueClamped(float f)+{+ // constant is from qFuzzyIsNull()+ return f <= 0.00001f;+}++template<>+inline bool KoColorSpaceMaths<float>::isUnitValueClamped(float f)+{+ // constant is from qFuzzyIsNull()+ return f > 1.0 - 0.00001f;+}++template<>+inline bool KoColorSpaceMaths<float>::isHalfValue(float value) {+ return qFuzzyCompare(value, KoColorSpaceMathsTraits<float>::halfValue);}//------------------------------ half specialization ------------------------------//@@ -498,10 +588,36 @@ inline half KoColorSpaceMaths<half>::isUnsafeAsDivisor(half value) {}template<>-inline half KoColorSpaceMaths<half>::clampChannelToSDR(half val) {- return qBound<half>(KoColorSpaceMathsTraits<half>::zeroValue, val, KoColorSpaceMathsTraits<half>::unitValue);+inline bool KoColorSpaceMaths<half>::isUnitValue(half value) {+ // TODO: check actual constant+ return qAbs(value - KoColorSpaceMathsTraits<half>::unitValue) < 0.0001f;}+template<>+inline bool KoColorSpaceMaths<half>::isZeroValue(half value) {+ // TODO: check actual constant+ return qAbs(value) < 0.0001f;+}++template<>+inline bool KoColorSpaceMaths<half>::isZeroValueClamped(half f)+{+ // TODO: check actual constant+ return f <= 0.0001f;+}++template<>+inline bool KoColorSpaceMaths<half>::isUnitValueClamped(half f)+{+ // TODO: check actual constant+ return f > 1.0 - 0.0001f;+}++template<>+inline bool KoColorSpaceMaths<half>::isHalfValue(half value) {+ // TODO: check actual constant+ return qAbs(value - 0.5f) < 0.0001f;+}#endif@@ -652,13 +768,18 @@ namespace Arithmetic}template<class T>- inline T clampToSDR(typename KoColorSpaceMathsTraits<T>::compositetype a) {- return KoColorSpaceMaths<T>::clampToSDR(a);+ inline T clampChannelToSDR(T a) {+ return KoColorSpaceMaths<T>::clampChannelToSDR(a);}template<class T>- inline T clampChannelToSDR(T a) {- return KoColorSpaceMaths<T>::clampChannelToSDR(a);+ inline T clampChannelToSDRBottom(T a) {+ return KoColorSpaceMaths<T>::clampChannelToSDRBottom(a);+ }++ template<class T>+ inline T clampToSDR(typename KoColorSpaceMathsTraits<T>::compositetype a) {+ return KoColorSpaceMaths<T>::clampToSDR(a);}template<class T>@@ -670,7 +791,47 @@ namespace Arithmeticinline T clampToSDRBottom(typename KoColorSpaceMathsTraits<T>::compositetype a) {return KoColorSpaceMaths<T>::clampToSDRBottom(a);}++ template<class T, typename composite_type = typename KoColorSpaceMathsTraits<T>::compositetype>+ inline composite_type divideInCompositeSpace(composite_type a, composite_type b) {+ return KoColorSpaceMaths<T>::divideInCompositeSpace(a, b);+ }++ template<class T, typename composite_type = typename KoColorSpaceMathsTraits<T>::compositetype>+ inline composite_type multiplyInCompositeSpace(composite_type a, composite_type b) {+ return KoColorSpaceMaths<T>::multiplyInCompositeSpace(a, b);+ }+ template <typename T>+ static inline bool isZeroValue(T v)+ {+ return KoColorSpaceMaths<T>::isZeroValue(v);+ }++ template <typename T>+ static inline bool isUnitValue(T v)+ {+ return KoColorSpaceMaths<T>::isUnitValue(v);+ }++ template <typename T>+ static inline bool isZeroValueClamped(T v)+ {+ return KoColorSpaceMaths<T>::isZeroValueClamped(v);+ }++ template <typename T>+ static inline bool isUnitValueClamped(T v)+ {+ return KoColorSpaceMaths<T>::isUnitValueClamped(v);+ }++ template <typename T>+ static inline bool isHalfValue(T v)+ {+ return KoColorSpaceMaths<T>::isHalfValue(v);+ }+template<class T>inline T min(T a, T b, T c) {b = (a < b) ? a : b;diff --git a/libs/pigment/compositeops/KoCompositeOpGreater.h b/libs/pigment/compositeops/KoCompositeOpGreater.hindex cc0f1c4bc1..553cc5e22c 100644--- a/libs/pigment/compositeops/KoCompositeOpGreater.h+++ b/libs/pigment/compositeops/KoCompositeOpGreater.h@@ -23,18 +23,7 @@#include "KoCompositeOpBase.h"namespace tmp3 {-template <typename T>-Q_REQUIRED_RESULT static inline Q_DECL_UNUSED bool isZeroValue(T v)-{- return KoColorSpaceMaths<T>::isZeroValue(v);-}--template <typename T>-Q_REQUIRED_RESULT static inline Q_DECL_UNUSED bool isUnitValue(T v)-{- return KoColorSpaceMaths<T>::isUnitValue(v);-}-+using namespace Arithmetic;}/**diff --git a/libs/pigment/tests/KoCompositeOpFunctions2.h b/libs/pigment/tests/KoCompositeOpFunctions2.hindex 3e7a4ea9ff..5b640f3689 100644--- a/libs/pigment/tests/KoCompositeOpFunctions2.h+++ b/libs/pigment/tests/KoCompositeOpFunctions2.h@@ -99,156 +99,7 @@ struct ClampPolicyFloatHDR {};namespace tmp {-Q_REQUIRED_RESULT Q_DECL_CONSTEXPR static inline Q_DECL_UNUSED bool isNullValueClamped(double d)-{- return d <= 0.000000000001;-}--Q_REQUIRED_RESULT Q_DECL_CONSTEXPR static inline Q_DECL_UNUSED bool isNullValueClamped(float f)-{- return f <= 0.00001f;-}--#ifdef HAVE_OPENEXR--Q_REQUIRED_RESULT static inline Q_DECL_UNUSED bool isNullValueClamped(half h)-{- return h.isZero() || h.isNegative();-}--#endif /* HAVE_OPENEXR */--template <typename T>-Q_REQUIRED_RESULT Q_DECL_CONSTEXPR static inline Q_DECL_UNUSED bool isNullValueClamped(T v)-{- return v <= 0;-}--Q_REQUIRED_RESULT Q_DECL_CONSTEXPR static inline Q_DECL_UNUSED bool isUnitValueClamped(double d)-{- return d > 1.0 - 0.000000000001;-}--Q_REQUIRED_RESULT Q_DECL_CONSTEXPR static inline Q_DECL_UNUSED bool isUnitValueClamped(float f)-{- return f > 1.0 - 0.00001f;-}--#ifdef HAVE_OPENEXR--Q_REQUIRED_RESULT static inline Q_DECL_UNUSED bool isUnitValueClamped(half h)-{- return h > 1.0f - 0.0001f;-}--#endif /* HAVE_OPENEXR */--template <typename T>-Q_REQUIRED_RESULT Q_DECL_CONSTEXPR static inline Q_DECL_UNUSED bool isUnitValueClamped(T v)-{- return v >= KoColorSpaceMathsTraits<T>::unitValue;-}---template <typename T>-Q_REQUIRED_RESULT static inline Q_DECL_UNUSED bool isZeroValue(T v)-{- return KoColorSpaceMaths<T>::isZeroValue(v);-}--template <typename T>-Q_REQUIRED_RESULT static inline Q_DECL_UNUSED bool isUnitValue(T v)-{- return KoColorSpaceMaths<T>::isUnitValue(v);-}--template <typename T>-Q_REQUIRED_RESULT static inline Q_DECL_UNUSED T clampChannelToSDRBottom(T v)-{- return v;-}--template <>-Q_REQUIRED_RESULT inline Q_DECL_UNUSED float clampChannelToSDRBottom<float>(float v)-{- return qMax<float>(KoColorSpaceMathsTraits<float>::zeroValue, v);-}--template <typename T, typename composite_type = typename KoColorSpaceMathsTraits<T>::compositetype>-Q_REQUIRED_RESULT static inline Q_DECL_UNUSED composite_type divideInCompositeSpace(composite_type a, composite_type b)-{- return a / b;-}--template <>-Q_REQUIRED_RESULT inline Q_DECL_UNUSED- KoColorSpaceMathsTraits<quint8>::compositetype- divideInCompositeSpace<quint8>(KoColorSpaceMathsTraits<quint8>::compositetype a,- KoColorSpaceMathsTraits<quint8>::compositetype b)--{- return a * KoColorSpaceMathsTraits<quint8>::unitValue / b;-}--template <>-Q_REQUIRED_RESULT inline Q_DECL_UNUSED- KoColorSpaceMathsTraits<quint16>::compositetype- divideInCompositeSpace<quint16>(KoColorSpaceMathsTraits<quint16>::compositetype a,- KoColorSpaceMathsTraits<quint16>::compositetype b)--{- return a * KoColorSpaceMathsTraits<quint16>::unitValue / b;-}--template <typename T, typename composite_type = typename KoColorSpaceMathsTraits<T>::compositetype>-Q_REQUIRED_RESULT static inline Q_DECL_UNUSED composite_type multiplyInCompositeSpace(composite_type a, composite_type b)-{- return a * b;-}---template <>-Q_REQUIRED_RESULT inline Q_DECL_UNUSED- KoColorSpaceMathsTraits<quint8>::compositetype- multiplyInCompositeSpace<quint8>(KoColorSpaceMathsTraits<quint8>::compositetype a,- KoColorSpaceMathsTraits<quint8>::compositetype b)--{- return a * b / KoColorSpaceMathsTraits<quint8>::unitValue;-}--template <>-Q_REQUIRED_RESULT inline Q_DECL_UNUSED- KoColorSpaceMathsTraits<quint16>::compositetype- multiplyInCompositeSpace<quint16>(KoColorSpaceMathsTraits<quint16>::compositetype a,- KoColorSpaceMathsTraits<quint16>::compositetype b)--{- return a * b / KoColorSpaceMathsTraits<quint16>::unitValue;-}--template <typename T>-inline bool isHalfValue(T v)-{- return v == KoColorSpaceMathsTraits<T>::halfValue;-}--template <>-inline bool isHalfValue<float>(float v)-{- return qFuzzyCompare(v, KoColorSpaceMathsTraits<float>::halfValue);-}--#ifdef HAVE_OPENEXR--template <>-inline bool isHalfValue<half>(half v)-{- return qAbs(v - KoColorSpaceMathsTraits<half>::halfValue) < 0.0001f;-}--#endif /* HAVE_OPENEXR */-+using namespace Arithmetic;}template<class HSXType, class TReal>@@ -497,7 +348,7 @@ struct CFColorDodge : ClampedSourceCompositeFunctorBase<T> {// and the numerator can remain as 0, so dividing 0 over a number (no matter// how small it is) gives 0.if (KoColorSpaceMaths<T>::isUnitValue(src)) {- return tmp::isNullValueClamped(dst) ? zeroValue<T>() : KoColorSpaceMathsTraits<T>::unitValue;+ return tmp::isZeroValueClamped(dst) ? zeroValue<T>() : KoColorSpaceMathsTraits<T>::unitValue;}return clamp_policy::clampResultAllowNegative(@@ -650,7 +501,7 @@ struct CFVividLight : ClampedSourceCompositeFunctorBase<T> {}if (tmp::isUnitValue(src)) {- return tmp::isNullValueClamped(dst) ? zeroValue<T>() : unitValue<T>();+ return tmp::isZeroValueClamped(dst) ? zeroValue<T>() : unitValue<T>();}// min(1,max(0, dst / (2*(1-src)))diff --git a/libs/pigment/tests/KoCompositeOpGeneric2.h b/libs/pigment/tests/KoCompositeOpGeneric2.hindex 889c2d0bfe..38cee663ef 100644--- a/libs/pigment/tests/KoCompositeOpGeneric2.h+++ b/libs/pigment/tests/KoCompositeOpGeneric2.h@@ -15,30 +15,7 @@#include "KoCompositeOpBase.h"namespace tmp2 {-template <typename T>-Q_REQUIRED_RESULT static inline Q_DECL_UNUSED bool isZeroValue(T v)-{- return KoColorSpaceMaths<T>::isZeroValue(v);-}--template <typename T>-Q_REQUIRED_RESULT static inline Q_DECL_UNUSED T clampChannelToSDRBottom(T v)-{- return v;-}--template <>-Q_REQUIRED_RESULT inline Q_DECL_UNUSED float clampChannelToSDRBottom<float>(float v)-{- return qMax<float>(KoColorSpaceMathsTraits<float>::zeroValue, v);-}--template <typename T>-Q_REQUIRED_RESULT static inline Q_DECL_UNUSED bool isUnitValue(T v)-{- return KoColorSpaceMaths<T>::isUnitValue(v);-}-+using namespace Arithmetic;}template <class Traits,