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++
The second round of fixing blendmodes in floating-point spaces
Dmitry Kazakov491b271437ed
diff --git a/libs/pigment/KoColorSpaceMaths.h b/libs/pigment/KoColorSpaceMaths.hindex 05573d2562..9b2636f485 100644--- a/libs/pigment/KoColorSpaceMaths.h+++ b/libs/pigment/KoColorSpaceMaths.h@@ -331,27 +331,27 @@ public:return value == KoColorSpaceMathsTraits<_T>::zeroValue;}- inline static bool isUnitValue(_T value) {+ inline static bool isUnitValueFuzzy(_T value) {static_assert(std::numeric_limits<_Tdst>::is_integer);return value == KoColorSpaceMathsTraits<_T>::unitValue;}- inline static bool isZeroValue(_T value) {+ inline static bool isZeroValueFuzzy(_T value) {static_assert(std::numeric_limits<_Tdst>::is_integer);return value == KoColorSpaceMathsTraits<_T>::zeroValue;}- inline static bool isZeroValueClamped(_T v) {+ inline static bool isZeroValueClampedFuzzy(_T v) {static_assert(std::numeric_limits<_Tdst>::is_integer);return v <= 0;}- static inline bool isUnitValueClamped(_T v) {+ static inline bool isUnitValueClampedFuzzy(_T v) {static_assert(std::numeric_limits<_Tdst>::is_integer);return v >= KoColorSpaceMathsTraits<_T>::unitValue;}- static inline bool isHalfValue(_T v)+ static inline bool isHalfValueFuzzy(_T v){static_assert(std::numeric_limits<_Tdst>::is_integer);return v == KoColorSpaceMathsTraits<_T>::halfValue;@@ -397,31 +397,31 @@ inline double KoColorSpaceMaths<double>::isUnsafeAsDivisor(double value) {}template<>-inline bool KoColorSpaceMaths<double>::isUnitValue(double value) {+inline bool KoColorSpaceMaths<double>::isUnitValueFuzzy(double value) {return qFuzzyCompare(value, KoColorSpaceMathsTraits<double>::unitValue);}template<>-inline bool KoColorSpaceMaths<double>::isZeroValue(double value) {+inline bool KoColorSpaceMaths<double>::isZeroValueFuzzy(double value) {return qFuzzyIsNull(value);}template<>-inline bool KoColorSpaceMaths<double>::isZeroValueClamped(double d)+inline bool KoColorSpaceMaths<double>::isZeroValueClampedFuzzy(double d){// constant is from qFuzzyIsNull()return d <= 0.000000000001;}template<>-inline bool KoColorSpaceMaths<double>::isUnitValueClamped(double d)+inline bool KoColorSpaceMaths<double>::isUnitValueClampedFuzzy(double d){// constant is from qFuzzyIsNull()return d > 1.0 - 0.000000000001;}template<>-inline bool KoColorSpaceMaths<double>::isHalfValue(double value) {+inline bool KoColorSpaceMaths<double>::isHalfValueFuzzy(double value) {return qFuzzyCompare(value, KoColorSpaceMathsTraits<double>::halfValue);}@@ -483,31 +483,31 @@ inline float KoColorSpaceMaths<float>::isUnsafeAsDivisor(float value) {}template<>-inline bool KoColorSpaceMaths<float>::isUnitValue(float value) {+inline bool KoColorSpaceMaths<float>::isUnitValueFuzzy(float value) {return qFuzzyCompare(value, KoColorSpaceMathsTraits<float>::unitValue);}template<>-inline bool KoColorSpaceMaths<float>::isZeroValue(float value) {+inline bool KoColorSpaceMaths<float>::isZeroValueFuzzy(float value) {return qFuzzyIsNull(value);}template<>-inline bool KoColorSpaceMaths<float>::isZeroValueClamped(float f)+inline bool KoColorSpaceMaths<float>::isZeroValueClampedFuzzy(float f){// constant is from qFuzzyIsNull()return f <= 0.00001f;}template<>-inline bool KoColorSpaceMaths<float>::isUnitValueClamped(float f)+inline bool KoColorSpaceMaths<float>::isUnitValueClampedFuzzy(float f){// constant is from qFuzzyIsNull()return f > 1.0 - 0.00001f;}template<>-inline bool KoColorSpaceMaths<float>::isHalfValue(float value) {+inline bool KoColorSpaceMaths<float>::isHalfValueFuzzy(float value) {return qFuzzyCompare(value, KoColorSpaceMathsTraits<float>::halfValue);}@@ -588,33 +588,33 @@ inline half KoColorSpaceMaths<half>::isUnsafeAsDivisor(half value) {}template<>-inline bool KoColorSpaceMaths<half>::isUnitValue(half value) {+inline bool KoColorSpaceMaths<half>::isUnitValueFuzzy(half value) {// ~ 2 * HALF_ESPILONreturn qAbs(value - KoColorSpaceMathsTraits<half>::unitValue) < 0.002f;}template<>-inline bool KoColorSpaceMaths<half>::isZeroValue(half value) {+inline bool KoColorSpaceMaths<half>::isZeroValueFuzzy(half value) {// ~ 2 * HALF_ESPILONreturn qAbs(value) < 0.002f;}template<>-inline bool KoColorSpaceMaths<half>::isZeroValueClamped(half f)+inline bool KoColorSpaceMaths<half>::isZeroValueClampedFuzzy(half f){// ~ 2 * HALF_ESPILONreturn f <= 0.002f;}template<>-inline bool KoColorSpaceMaths<half>::isUnitValueClamped(half f)+inline bool KoColorSpaceMaths<half>::isUnitValueClampedFuzzy(half f){// ~ 2 * HALF_ESPILONreturn f > 1.0 - 0.002f;}template<>-inline bool KoColorSpaceMaths<half>::isHalfValue(half value) {+inline bool KoColorSpaceMaths<half>::isHalfValueFuzzy(half value) {// TODO: check actual constantreturn qAbs(value - 0.5f) < 0.001f;}@@ -803,35 +803,62 @@ namespace Arithmetic}template <typename T>- static inline bool isZeroValue(T v)+ static inline bool isZeroValueFuzzy(T v){- return KoColorSpaceMaths<T>::isZeroValue(v);+ return KoColorSpaceMaths<T>::isZeroValueFuzzy(v);}template <typename T>- static inline bool isUnitValue(T v)+ static inline bool isUnitValueFuzzy(T v){- return KoColorSpaceMaths<T>::isUnitValue(v);+ return KoColorSpaceMaths<T>::isUnitValueFuzzy(v);}template <typename T>- static inline bool isZeroValueClamped(T v)+ static inline bool isZeroValueClampedFuzzy(T v){- return KoColorSpaceMaths<T>::isZeroValueClamped(v);+ return KoColorSpaceMaths<T>::isZeroValueClampedFuzzy(v);}template <typename T>- static inline bool isUnitValueClamped(T v)+ static inline bool isUnitValueClampedFuzzy(T v){- return KoColorSpaceMaths<T>::isUnitValueClamped(v);+ return KoColorSpaceMaths<T>::isUnitValueClampedFuzzy(v);}template <typename T>- static inline bool isHalfValue(T v)+ static inline bool isHalfValueFuzzy(T v){- return KoColorSpaceMaths<T>::isHalfValue(v);+ return KoColorSpaceMaths<T>::isHalfValueFuzzy(v);}+ template <typename T>+ inline bool isUnitValueStrict(T value) {+ return value == KoColorSpaceMathsTraits<T>::unitValue;+ }+ template <typename T>+ inline bool isZeroValueStrict(T value) {+ return value == KoColorSpaceMathsTraits<T>::zeroValue;+ }++ template <typename T>+ inline bool isUnitValueClampedStrict(T value) {+ if constexpr (std::numeric_limits<T>::is_integer) {+ return value == KoColorSpaceMathsTraits<T>::unitValue;+ } else {+ return value >= KoColorSpaceMathsTraits<T>::unitValue;+ }+ }+ template <typename T>+ inline bool isZeroValueClampedStrict(T value) {+ if constexpr (std::numeric_limits<T>::is_integer) {+ return value == KoColorSpaceMathsTraits<T>::zeroValue;+ } else {+ return value <= KoColorSpaceMathsTraits<T>::zeroValue;+ }+ }++template<class T>inline T min(T a, T b, T c) {b = (a < b) ? a : b;diff --git a/libs/pigment/compositeops/KoCompositeOpClampPolicy.h b/libs/pigment/compositeops/KoCompositeOpClampPolicy.hindex 163720e3ae..34a0125a1b 100644--- a/libs/pigment/compositeops/KoCompositeOpClampPolicy.h+++ b/libs/pigment/compositeops/KoCompositeOpClampPolicy.h@@ -33,6 +33,11 @@ struct ClampAsInteger {static inline compositetype fixInfiniteAfterDivision(compositetype value) {return value;}++ static inline T clippedMaxValue() {+ using namespace Arithmetic;+ return unitValue<T>();+ }};template <typename T>@@ -60,6 +65,11 @@ struct ClampAsFloatSDR {// become infinity or NaN, so we check that and correct (kind of clamping)return std::isfinite(value) ? value : compositetype(KoColorSpaceMathsTraits<T>::unitValue);}++ static inline T clippedMaxValue() {+ using namespace Arithmetic;+ return unitValue<T>();+ }};template <typename T>@@ -86,6 +96,10 @@ struct ClampAsFloatHDR {// become infinity or NaN, so we check that and correct (kind of clamping)return std::isfinite(value) ? value : compositetype(KoColorSpaceMathsTraits<T>::max);}++ static inline T clippedMaxValue() {+ return KoColorSpaceMathsTraits<T>::max;+ }};diff --git a/libs/pigment/compositeops/KoCompositeOpFunctions.h b/libs/pigment/compositeops/KoCompositeOpFunctions.hindex 1d306b5c21..58ef32b574 100644--- a/libs/pigment/compositeops/KoCompositeOpFunctions.h+++ b/libs/pigment/compositeops/KoCompositeOpFunctions.h@@ -209,27 +209,38 @@ inline void cfLighterColor(TReal sr, TReal sg, TReal sb, TReal& dr, TReal& dg, T}}-template<class T,- template <typename> typename ClampPolicy>-struct CFColorBurn : KoClampedSourceCompositeOpGenericFunctorBase<T>+template<class T>+struct CFColorBurn : KoClampedSourceAndDestinationCompositeOpGenericFunctorBase<T>{- using clamp_policy = ClampPolicy<T>;-static T composeChannel(T src, T dst) {using namespace Arithmetic;- // Handle the case where the denominator is 0. See color dodge for a- // detailed explanation- if (isZeroValue(src)) {- return !isUnitValueClamped(dst) ? zeroValue<T>() : clamp_policy::clampResultAllowNegative(dst);+ /**+ * dst-unit is a stable point of color-burn belndmode,+ * it never changes, whatever the value of src channel+ */+ if (isUnitValueStrict(dst)) {+ return unitValue<T>();}- return inv<T>(- clamp_policy::clampInvertedResult(- clamp_policy::fixInfiniteAfterDivision(- div(inv(dst), src)- )- )- );++ if constexpr (std::numeric_limits<T>::is_integer) {+ // Handle the case where the denominator is 0. See color dodge for a+ // detailed explanation+ if (isZeroValueStrict(src)) {+ return zeroValue<T>();+ }+ }++ using composite_type = typename KoColorSpaceMathsTraits<T>::compositetype;+ composite_type divisionResult = div(inv(dst), src);++ if constexpr (!std::numeric_limits<T>::is_integer) {+ if (!std::isfinite(divisionResult)) {+ return zeroValue<T>();+ }+ }++ return inv<T>(clampToSDR<T>(divisionResult));}};@@ -263,8 +274,8 @@ struct CFColorDodge : KoClampedSourceCompositeOpGenericFunctorBase<T> {// this case we also treat the denominator as an infinitely small number,// 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 isZeroValueClamped(dst) ? zeroValue<T>() : KoColorSpaceMathsTraits<T>::unitValue;+ if (isUnitValueStrict(src)) {+ return isZeroValueClampedStrict(dst) ? zeroValue<T>() : KoColorSpaceMathsTraits<T>::unitValue;}return clamp_policy::clampResultAllowNegative(@@ -308,16 +319,32 @@ struct CFExclusion : KoClampedSourceAndDestinationCompositeOpGenericFunctorBase<}};++template<class T>-inline T cfDivide(T src, T dst) {- using namespace Arithmetic;- //typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;-- if (isZeroValue(src))- return isZeroValue(dst) ? zeroValue<T>() : unitValue<T>();-- return clamp<T>(div(dst, src));-}+struct CFDivide : KoClampedSourceAndDestinationBottomCompositeOpGenericFunctorBase<T> {+ static inline T composeChannel(T src, T dst) {+ using namespace Arithmetic;+ using composite_type = typename KoColorSpaceMathsTraits<T>::compositetype;++ if (isZeroValueStrict(dst)) {+ return zeroValue<T>();+ }++ if (isZeroValueStrict(src)) {+ return unitValue<T>();+ }++ composite_type result = div(dst, src);++ /**+ * Since input values are clamped, we can never get a negative+ * infinity as a result of an operation. Hence we can just clamp the result+ * to SDR safely.+ */+ return clampToSDRTop<T>(result);+ }+};template<class T>struct CFHardLight : KoClampedSourceCompositeOpGenericFunctorBase<T> {@@ -325,7 +352,7 @@ struct CFHardLight : KoClampedSourceCompositeOpGenericFunctorBase<T> {using namespace Arithmetic;using composite_type = typename KoColorSpaceMathsTraits<T>::compositetype;- if (isHalfValue(src)) {+ if (isHalfValueFuzzy(src)) {return dst;}@@ -345,17 +372,14 @@ struct CFHardLight : KoClampedSourceCompositeOpGenericFunctorBase<T> {};template<class T>-struct CFSoftLightSvg : KoClampedSourceCompositeOpGenericFunctorBase<T> {+struct CFSoftLightSvg : KoClampedSourceAndDestinationCompositeOpGenericFunctorBase<T> {static inline T composeChannel(T src, T dst) {using namespace Arithmetic;- // TODO: check if dst-clamping should happen at higher level-/*** soft-light scales the color using sqrt and pow2* functions, hence we cannot support HDR values*/- dst = clampChannelToSDR<T>(dst);qreal fsrc = scale<qreal>(src);qreal fdst = scale<qreal>(dst);@@ -371,18 +395,10 @@ struct CFSoftLightSvg : KoClampedSourceCompositeOpGenericFunctorBase<T> {template<class T>-struct CFSoftLight : KoClampedSourceCompositeOpGenericFunctorBase<T> {+struct CFSoftLight : KoClampedSourceAndDestinationCompositeOpGenericFunctorBase<T> {static inline T composeChannel(T src, T dst) {using namespace Arithmetic;- // TODO: check if dst-clamping should happen at higher level-- /**- * soft-light scales the color using sqrt and pow2- * functions, hence we cannot support HDR values- */- dst = clampChannelToSDR<T>(dst);-qreal fsrc = scale<qreal>(src);qreal fdst = scale<qreal>(dst);@@ -405,8 +421,8 @@ struct CFVividLight : KoClampedSourceCompositeOpGenericFunctorBase<T> {using composite_type = typename KoColorSpaceMathsTraits<T>::compositetype;if (src < halfValue<T>()) {- if (isZeroValue(src)) {- return isUnitValueClamped(dst) ? clamp_policy::clampResultAllowNegative(dst) : zeroValue<T>();+ if (isZeroValueStrict(src)) {+ return isUnitValueClampedStrict(dst) ? clamp_policy::clampResultAllowNegative(dst) : zeroValue<T>();}// min(1,max(0,1-(1-dst) / (2*src)))@@ -420,8 +436,8 @@ struct CFVividLight : KoClampedSourceCompositeOpGenericFunctorBase<T> {);}- if (isUnitValue(src)) {- return isZeroValueClamped(dst) ? zeroValue<T>() : unitValue<T>();+ if (isUnitValueStrict(src)) {+ return isZeroValueClampedStrict(dst) ? zeroValue<T>() : unitValue<T>();}// min(1,max(0, dst / (2*(1-src)))@@ -435,12 +451,10 @@ struct CFVividLight : KoClampedSourceCompositeOpGenericFunctorBase<T> {}};-template<class T,- template <typename> typename ClampPolicy>-struct CFPinLight : KoClampedSourceCompositeOpGenericFunctorBase<T> {+template<class T>+struct CFPinLight : KoClampedSourceAndDestinationCompositeOpGenericFunctorBase<T> {static inline T composeChannel(T src, T dst) {using namespace Arithmetic;- using clamp_policy = ClampPolicy<T>;using composite_type = typename KoColorSpaceMathsTraits<T>::compositetype;// TODO: verify that the formula is correct (the first max would be useless here)@@ -448,18 +462,19 @@ struct CFPinLight : KoClampedSourceCompositeOpGenericFunctorBase<T> {composite_type src2 = composite_type(src) + src;composite_type a = qMin<composite_type>(dst, src2);composite_type b = qMax<composite_type>(src2-Arithmetic::unitValue<T>(), a);- return clamp_policy::clampResult(b);++ /**+ * `b` is guaranteed to be in range 0...1 since the inputs are clamped,+ * so no additional clamping is needed+ */+ return b;}};template<class T>inline T cfArcTangent(T src, T dst) {using namespace Arithmetic;-- if (isZeroValue(dst))- return isZeroValue(src) ? zeroValue<T>() : unitValue<T>();-- return scale<T>(2.0 * atan(scale<qreal>(src) / scale<qreal>(dst)) / Arithmetic::pi);+ return scale<T>(2.0 * atan2(scale<qreal>(src), scale<qreal>(dst)) / Arithmetic::pi);}template<class T>@@ -544,7 +559,7 @@ struct CFHardMix : KoClampedSourceCompositeOpGenericFunctorBase<T> {*/return dst > halfValue<T>() ?CFColorDodge<T, ClampPolicy>::composeChannel(src,dst) :- CFColorBurn<T, ClampPolicy>::composeChannel(src,dst);+ CFColorBurn<T>::composeChannel(src,dst);}};@@ -589,37 +604,53 @@ struct CFAdditiveSubtractive : KoClampedSourceAndDestinationBottomCompositeOpGen};template<class T>-struct CFGammaDark : KoClampedSourceAndDestinationBottomCompositeOpGenericFunctorBase<T> {+struct CFGammaDark : KoClampedSourceAndDestinationCompositeOpGenericFunctorBase<T> {static inline T composeChannel(T src, T dst) {using namespace Arithmetic;- if (isZeroValue(src)) {+ const qreal dstReal = scale<qreal>(dst);++ if (isZeroValueFuzzy(dstReal)) {return zeroValue<T>();}- if (isUnitValue(dst)) {+ if (isUnitValueFuzzy(dstReal)) {return unitValue<T>();}// power(dst, 1/src)- return scale<T>(pow(scale<qreal>(dst), 1.0 / scale<qreal>(src)));++ const qreal srcReal = scale<qreal>(src);+ const qreal exponent = 1.0 / srcReal;++ if (std::isfinite(exponent)) {+ return scale<T>(+ std::min<qreal>(pow(dstReal, exponent),+ KoColorSpaceMathsTraits<T>::max));+ } else {+ return zeroValue<T>();+ }}};template<class T>-struct CFGammaLight : KoClampedSourceAndDestinationBottomCompositeOpGenericFunctorBase<T> {+struct CFGammaLight : KoClampedSourceAndDestinationCompositeOpGenericFunctorBase<T> {static inline T composeChannel(T src, T dst) {using namespace Arithmetic;- if (isZeroValue(dst)) {- return isZeroValue(src) ? unitValue<T>() : zeroValue<T>();+ const qreal dstReal = scale<qreal>(dst);++ if (isZeroValueFuzzy(dstReal)) {+ return zeroValue<T>();}- if (isUnitValue(dst)) {+ if (isUnitValueFuzzy(dstReal)) {return unitValue<T>();}- return scale<T>(pow(scale<qreal>(dst), scale<qreal>(src)));+ const qreal srcReal = scale<qreal>(src);++ return scale<T>(pow(dstReal, srcReal));}};@@ -636,7 +667,7 @@ struct CFGeometricMean : KoClampedSourceAndDestinationBottomCompositeOpGenericFustatic inline T composeChannel(T src, T dst) {using namespace Arithmetic;- if (isUnitValue(src) && isUnitValue(dst)) {+ if (isUnitValueFuzzy(src) && isUnitValueFuzzy(dst)) {return unitValue<T>();}@@ -659,14 +690,18 @@ inline T cfMultiply(T src, T dst) { return Arithmetic::mul(src, dst); }template<class T,template <typename> typename ClampPolicy>-struct CFHardOverlay : KoClampedSourceCompositeOpGenericFunctorBase<T> {+struct CFHardOverlay : KoClampedSourceFullAndDestinationBottomCompositeOpGenericFunctorBase<T> {static inline T composeChannel(T src, T dst) {using namespace Arithmetic;using clamp_policy = ClampPolicy<T>;using composite_type = typename KoColorSpaceMathsTraits<T>::compositetype;- if (isUnitValue(src)) {- return unitValue<T>();+ if (isZeroValueStrict(dst)) {+ return zeroValue<T>();+ }++ if (isUnitValueStrict(src)) {+ return clamp_policy::clippedMaxValue();}if(src >= halfValue<T>()) {@@ -745,11 +780,11 @@ struct CFHelow : KoClampedSourceAndDestinationCompositeOpGenericFunctorBase<T> {using namespace Arithmetic;// see http://www.pegtop.net/delphi/articles/blendmodes/quadratic.htm for formulas of Quadratic Blending Modes like Glow, Reflect, Freeze, and Heat- if (isUnitValue<T>(CFHardMixPhotoshop<T>::composeChannel(src,dst))) {+ if (isUnitValueFuzzy<T>(CFHardMixPhotoshop<T>::composeChannel(src,dst))) {return cfHeat(src,dst);}- if (isZeroValue<T>(src)) {+ if (isZeroValueFuzzy<T>(src)) {return zeroValue<T>();}@@ -762,11 +797,11 @@ struct CFFrect : KoClampedSourceAndDestinationCompositeOpGenericFunctorBase<T> {static inline T composeChannel(T src, T dst) {using namespace Arithmetic;- if (isUnitValue<T>(CFHardMixPhotoshop<T>::composeChannel(src,dst))) {+ if (isUnitValueFuzzy<T>(CFHardMixPhotoshop<T>::composeChannel(src,dst))) {return cfFreeze(src,dst);}- if (isZeroValue<T>(dst)) {+ if (isZeroValueFuzzy<T>(dst)) {return zeroValue<T>();}@@ -780,11 +815,11 @@ struct CFGleat : KoClampedSourceAndDestinationCompositeOpGenericFunctorBase<T> {using namespace Arithmetic;// see http://www.pegtop.net/delphi/articles/blendmodes/quadratic.htm for formulas of Quadratic Blending Modes like Glow, Reflect, Freeze, and Heat- if(isUnitValue<T>(dst)) {+ if(isUnitValueFuzzy<T>(dst)) {return unitValue<T>();}- if(isUnitValue<T>(CFHardMixPhotoshop<T>::composeChannel(src, dst))) {+ if(isUnitValueFuzzy<T>(CFHardMixPhotoshop<T>::composeChannel(src, dst))) {return cfGlow(src,dst);}@@ -1170,11 +1205,11 @@ struct CFFlatLight : KoClampedSourceAndDestinationCompositeOpGenericFunctorBase<static inline T composeChannel(T src, T dst) {using namespace Arithmetic;- if (isZeroValue<T>(src)) {+ if (isZeroValueFuzzy<T>(src)) {return zeroValue<T>();}- return clamp<T>(isUnitValue<T>(CFHardMixPhotoshop<T>::composeChannel(inv(src),dst)) ?+ return clamp<T>(isUnitValueFuzzy<T>(CFHardMixPhotoshop<T>::composeChannel(inv(src),dst)) ?CFPenumbraB<T>::composeChannel(src,dst) :CFPenumbraA<T>::composeChannel(src,dst));}diff --git a/libs/pigment/compositeops/KoCompositeOpGeneric.h b/libs/pigment/compositeops/KoCompositeOpGeneric.hindex c8ae9cb3a5..40fab42701 100644--- a/libs/pigment/compositeops/KoCompositeOpGeneric.h+++ b/libs/pigment/compositeops/KoCompositeOpGeneric.h@@ -62,12 +62,12 @@ public:srcAlpha = mul(srcAlpha, maskAlpha, opacity);- if (isZeroValue(srcAlpha)) {+ if (isZeroValueFuzzy(srcAlpha)) {return dstAlpha;}if(alphaLocked) {- if(!isZeroValue(dstAlpha)) {+ if(!isZeroValueFuzzy(dstAlpha)) {for(qint32 i=0; i <channels_nb; i++) {if(i != alpha_pos && (allChannelFlags || channelFlags.testBit(i))) {const channels_type srcInBlendSpace =@@ -88,7 +88,7 @@ public:}return dstAlpha;- } else if (isZeroValue(dstAlpha)) {+ } else if (isZeroValueFuzzy(dstAlpha)) {for(qint32 i=0; i <channels_nb; i++) {if(i != alpha_pos && (allChannelFlags || channelFlags.testBit(i))) {dst[i] = BlendingPolicy::fromAdditiveSpace(@@ -97,7 +97,7 @@ public:}}return srcAlpha;- } else if (isUnitValue(dstAlpha)) {+ } else if (isUnitValueFuzzy(dstAlpha)) {for(qint32 i=0; i <channels_nb; i++) {if(i != alpha_pos && (allChannelFlags || channelFlags.testBit(i))) {const channels_type srcInBlendSpace =@@ -116,7 +116,7 @@ public:}}return unitValue<channels_type>();- } else if (isUnitValue(srcAlpha)) {+ } else if (isUnitValueFuzzy(srcAlpha)) {for(qint32 i=0; i <channels_nb; i++) {if(i != alpha_pos && (allChannelFlags || channelFlags.testBit(i))) {const channels_type srcInBlendSpace =@@ -138,7 +138,7 @@ public:} else {channels_type newDstAlpha = unionShapeOpacity(srcAlpha, dstAlpha);- if (!isZeroValue(newDstAlpha)) {+ if (!isZeroValueFuzzy(newDstAlpha)) {for(qint32 i=0; i <channels_nb; i++) {if(i != alpha_pos && (allChannelFlags || channelFlags.testBit(i))) {diff --git a/libs/pigment/compositeops/KoCompositeOpGenericFunctorBase.h b/libs/pigment/compositeops/KoCompositeOpGenericFunctorBase.hindex 4c51352029..9d318172b1 100644--- a/libs/pigment/compositeops/KoCompositeOpGenericFunctorBase.h+++ b/libs/pigment/compositeops/KoCompositeOpGenericFunctorBase.h@@ -80,4 +80,20 @@ struct KoClampedSourceAndDestinationCompositeOpGenericFunctorBase}};+// clamp source on the both sides and clamp destination on+// the negative side only+template <typename T>+struct KoClampedSourceFullAndDestinationBottomCompositeOpGenericFunctorBase+{+ static inline T clampSourceChannelValue(T value) {+ using namespace Arithmetic;+ return clampChannelToSDR(value);+ }++ static inline T clampDestinationChannelValue(T value) {+ using namespace Arithmetic;+ return clampChannelToSDRBottom(value);+ }+};+#endif // KOCOMPOSITEOPGENERICFUNCTORBASE_Hdiff --git a/libs/pigment/compositeops/KoCompositeOpGreater.h b/libs/pigment/compositeops/KoCompositeOpGreater.hindex dc89fd7286..c01fec3a60 100644--- a/libs/pigment/compositeops/KoCompositeOpGreater.h+++ b/libs/pigment/compositeops/KoCompositeOpGreater.h@@ -47,10 +47,10 @@ public:const QBitArray& channelFlags ) {using namespace Arithmetic;- if (isUnitValue(dstAlpha)) return dstAlpha;+ if (isUnitValueFuzzy(dstAlpha)) return dstAlpha;channels_type appliedAlpha = mul(maskAlpha, srcAlpha, opacity);- if (isZeroValue(appliedAlpha)) return dstAlpha;+ if (isZeroValueFuzzy(appliedAlpha)) return dstAlpha;channels_type newDstAlpha;float dA = scale<float>(dstAlpha);@@ -71,9 +71,9 @@ public:float fakeOpacity = 1.0f - (1.0f - a)/(1.0f - dA + 1e-16f);newDstAlpha=scale<channels_type>(a);- if (isZeroValue(newDstAlpha)) {+ if (isZeroValueFuzzy(newDstAlpha)) {// just do nothing with color channels and return null opacity- } else if (!isZeroValue(dstAlpha)) {+ } else if (!isZeroValueFuzzy(dstAlpha)) {for (qint8 channel = 0; channel < channels_nb; ++channel)if(channel != alpha_pos && (allChannelFlags || channelFlags.testBit(channel))){diff --git a/libs/pigment/compositeops/KoCompositeOps.h b/libs/pigment/compositeops/KoCompositeOps.hindex f4f13c2b21..daa7efad3c 100644--- a/libs/pigment/compositeops/KoCompositeOps.h+++ b/libs/pigment/compositeops/KoCompositeOps.h@@ -259,7 +259,7 @@ struct AddGeneralOps<Traits, true>add<&cfSubtract<Arg> >(cs, COMPOSITE_SUBTRACT , KoCompositeOp::categoryArithmetic());add<CFInverseSubtract<Arg> >(cs, COMPOSITE_INVERSE_SUBTRACT, KoCompositeOp::categoryArithmetic());add<&cfMultiply<Arg> >(cs, COMPOSITE_MULT , KoCompositeOp::categoryArithmetic());- add<&cfDivide<Arg> >(cs, COMPOSITE_DIVIDE , KoCompositeOp::categoryArithmetic());+ add<CFDivide<Arg> >(cs, COMPOSITE_DIVIDE , KoCompositeOp::categoryArithmetic());add<&cfModulo<Arg> >(cs, COMPOSITE_MOD , KoCompositeOp::categoryModulo());add<&cfModuloContinuous<Arg> >(cs, COMPOSITE_MOD_CON , KoCompositeOp::categoryModulo());diff --git a/libs/pigment/tests/TestCompositeOpInversion.cpp b/libs/pigment/tests/TestCompositeOpInversion.cppindex 9421bf8dde..371adf1a2c 100644--- a/libs/pigment/tests/TestCompositeOpInversion.cpp+++ b/libs/pigment/tests/TestCompositeOpInversion.cpp@@ -364,8 +364,6 @@ std::vector<std::pair<QString, TestFlags>> generateCompositeOpIdSet()result.emplace_back(id, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);};- addSdrPreservingHdrOp(COMPOSITE_PIN_LIGHT);- addSdrPreservingHdrOp(COMPOSITE_BURN);addSdrPreservingHdrOp(COMPOSITE_LINEAR_BURN);// Hard-mix is the only stable positive-preserving mode@@ -385,6 +383,8 @@ std::vector<std::pair<QString, TestFlags>> generateCompositeOpIdSet()result.emplace_back(id, SdrRangePreserveStable | PositivePreserveStable);};+ addSdrPreservingOp(COMPOSITE_PIN_LIGHT);+ addSdrPreservingOp(COMPOSITE_BURN);addSdrPreservingOp(COMPOSITE_DARKEN);addSdrPreservingOp(COMPOSITE_LIGHTEN);addSdrPreservingOp(COMPOSITE_ALLANON);@@ -438,10 +438,50 @@ std::vector<std::pair<QString, TestFlags>> generateCompositeOpIdSet()result.emplace_back(COMPOSITE_EQUIVALENCE, SrcCannotMakeNegative | SdrRangePreserveUnstable | PositivePreserveStable);result.emplace_back(COMPOSITE_GEOMETRIC_MEAN, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);result.emplace_back(COMPOSITE_ADDITIVE_SUBTRACTIVE, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);- result.emplace_back(COMPOSITE_GAMMA_DARK, SrcCannotMakeNegative | SdrRangePreserveUnstable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_GAMMA_DARK, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);result.emplace_back(COMPOSITE_GAMMA_LIGHT, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);result.emplace_back(COMPOSITE_GAMMA_ILLUMINATION, SrcCannotMakeNegative | SdrRangePreserveUnstable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_TANGENT_NORMALMAP, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_COMBINE_NORMAL, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_COLOR, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_HUE, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_SATURATION, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_INC_SATURATION, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_DEC_SATURATION, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_LUMINIZE, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_INC_LUMINOSITY, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_DEC_LUMINOSITY, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_DARKER_COLOR, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_LIGHTER_COLOR, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_LAMBERT_LIGHTING, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_LAMBERT_LIGHTING_GAMMA_2_2, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_LAMBERT_LIGHTING_GAMMA_2_2, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_COLOR_HSI, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_HUE_HSI, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_SATURATION_HSI, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_INC_SATURATION_HSI, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_DEC_SATURATION_HSI, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_INTENSITY, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_INC_INTENSITY, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_DEC_INTENSITY, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_COLOR_HSL, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_HUE_HSL, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_SATURATION_HSL, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_INC_SATURATION_HSL, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_DEC_SATURATION_HSL, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_LIGHTNESS, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_INC_LIGHTNESS, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_DEC_LIGHTNESS, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_COLOR_HSV, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_HUE_HSV, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_SATURATION_HSV, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_INC_SATURATION_HSV, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_DEC_SATURATION_HSV, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_VALUE, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_INC_VALUE, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+ result.emplace_back(COMPOSITE_DEC_VALUE, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);+return result;}@@ -552,7 +592,7 @@ void TestCompositeOpInversion::testU16ModesConsistent()const qreal minOpacity = std::min({it.opacity(), it.srcAlpha(), it.dstAlpha()});// we have added a fast-path for opacity == 0.0 case- if (Arithmetic::isZeroValue<float>(minOpacity)) continue;+ if (Arithmetic::isZeroValueFuzzy<float>(minOpacity)) continue;KoColor srcColorU = it.srcColor(csU);KoColor dstColorU = it.dstColor(csU);@@ -688,15 +728,98 @@ void TestCompositeOpInversion::testF32vsU16ConsistencyInSDR()* so we should just skip this test case*/if (id == COMPOSITE_HARD_MIX_PHOTOSHOP &&- Arithmetic::isUnitValue<float>(float(it.srcColor()) + float(it.dstColor()))) {+ Arithmetic::isUnitValueFuzzy<float>(float(it.srcColor()) + float(it.dstColor()))) {continue;}- if (Arithmetic::isZeroValue<float>(resultColorValueU) &&+ if (id == COMPOSITE_HARD_OVERLAY) {+ if (Arithmetic::isUnitValueClampedFuzzy<float>(it.srcColor()) &&+ Arithmetic::isZeroValueClampedFuzzy<float>(it.dstColor())) {++ continue;+ }+ }++ if (id == COMPOSITE_HARD_OVERLAY && flags.testFlag(HDR)) {+ if (Arithmetic::isUnitValueClampedFuzzy<float>(it.srcColor())) {+ continue;+ }+ }++ if (id == COMPOSITE_DODGE) {+ if (Arithmetic::isUnitValueClampedFuzzy<float>(it.srcColor()) &&+ Arithmetic::isZeroValueClampedFuzzy<float>(it.dstColor())) {+ continue;+ }+ }++ if (id == COMPOSITE_DODGE && flags.testFlag(HDR)) {+ if (Arithmetic::isUnitValueClampedFuzzy<float>(it.srcColor())) {+ continue;+ }+ }++ if (id == COMPOSITE_BURN) {+ if (Arithmetic::isZeroValueClampedFuzzy<float>(it.srcColor()) &&+ Arithmetic::isUnitValueClampedFuzzy<float>(it.dstColor())) {+ continue;+ }+ }++ if (id == COMPOSITE_VIVID_LIGHT) {+ if (Arithmetic::isUnitValueClampedFuzzy<float>(it.srcColor()) ||+ Arithmetic::isZeroValueClampedFuzzy<float>(it.srcColor())) {+ continue;+ }+ }++ if (id == COMPOSITE_DIVIDE) {+ if (Arithmetic::isZeroValueClampedFuzzy<float>(it.srcColor()) &&+ Arithmetic::isZeroValueClampedFuzzy<float>(it.dstColor())) {+ continue;+ }+ }++ if (id == COMPOSITE_ARC_TANGENT) {+ if (Arithmetic::isZeroValueClampedFuzzy<float>(it.srcColor()) &&+ Arithmetic::isZeroValueClampedFuzzy<float>(it.dstColor())) {+ continue;+ }+ }++ if (id == COMPOSITE_GAMMA_DARK) {+ if (Arithmetic::isZeroValueClampedFuzzy<float>(it.srcColor()) &&+ Arithmetic::isUnitValueClampedFuzzy<float>(it.dstColor())) {+ continue;+ }+ }++ if (id == COMPOSITE_GAMMA_LIGHT) {+ if (Arithmetic::isZeroValueClampedFuzzy<float>(it.dstColor())) {+ continue;+ }+ }++ if (id == COMPOSITE_GAMMA_ILLUMINATION) {+ if (Arithmetic::isUnitValueClampedFuzzy<float>(it.srcColor()) &&+ Arithmetic::isZeroValueClampedFuzzy<float>(it.dstColor())) {+ continue;+ }+ }++ // normal extension into HDR+ bool isStraightUnitExtension =+ Arithmetic::isUnitValueFuzzy<float>(resultColorValueU) &&+ qAbs(resultColorValueU - qMin(1.0f, resultColorValueF)) <= 0.001;++ // clipped extension into HDR (when opacity < 1.0)+ isStraightUnitExtension |=+ resultColorValueU > minOpacity && resultColorValueF > 1.0;++ if (Arithmetic::isZeroValueFuzzy<float>(resultColorValueU) &&qAbs(resultColorValueU - qMax(0.0f, resultColorValueF)) <= 0.001) {// noop, should be fine result- } else if (Arithmetic::isUnitValue<float>(resultColorValueU) &&- qAbs(resultColorValueU - qMin(1.0f, resultColorValueF)) <= 0.001) {+ } else if (isStraightUnitExtension) {// noop, should be fine result as well} else if (resultColorValueF < 0.0) {qDebug() << "--- resulting value in SDR range is negative! ---";@@ -1104,4 +1227,427 @@ void TestCompositeOpInversion::testF16Modes()}}+QString csShortName(const KoID &depthId)+{+ if (depthId == Integer16BitsColorDepthID) {+ return "u16";+ } else if (depthId == Float16BitsColorDepthID) {+ return "f16";+ } else if (depthId == Float32BitsColorDepthID) {+ return "f32";+ } else {+ qFatal("not implemented");+ }+ return "error";+}+++void TestCompositeOpInversion::generateSampleSheets_data()+{+ auto ids = generateCompositeOpIdSet();++ QTest::addColumn<QString>("id");+ QTest::addColumn<TestFlags>("flags");+ QTest::addColumn<KoID>("depthId");++ auto fixId = [] (QString id) {+ id.replace(' ', '_');+ return id;+ };++ std::vector<KoID> depthIds = {Integer16BitsColorDepthID, Float32BitsColorDepthID, Float16BitsColorDepthID};+++ for (auto it = ids.begin(); it != ids.end(); ++it) {+ for (auto depthIt = depthIds.begin(); depthIt != depthIds.end(); ++depthIt) {+ const QString id = it->first;+ const TestFlags flags = it->second;+ const bool isHDR = flags.testFlag(HDR);++ if (id == COMPOSITE_DISSOLVE) continue;++ if (*depthIt == Integer16BitsColorDepthID && isHDR) continue;++ if (id == COMPOSITE_BURN && !isHDR && *depthIt != Integer16BitsColorDepthID) {+ continue;+ } else if (id == COMPOSITE_LINEAR_BURN && !isHDR && *depthIt != Integer16BitsColorDepthID) {+ continue;+ } else if (id == COMPOSITE_PIN_LIGHT && !isHDR && *depthIt != Integer16BitsColorDepthID) {+ continue;+ }++ QTest::addRow("%s_%s_%s", fixId(id).toLatin1().data(), isHDR ? "hdr" : "sdr", csShortName(*depthIt).toLatin1().data()) << id << flags << *depthIt;+ }+ }+}++#include <QPainter>+void TestCompositeOpInversion::generateSampleSheets()+{+ QFETCH(QString, id);+ QFETCH(TestFlags, flags);+ QFETCH(KoID, depthId);++ const KoColorSpace* csF32 = KoColorSpaceRegistry::instance()->colorSpace(RGBAColorModelID.id(), depthId.id(), 0);+ const KoCompositeOp *opF32 = createOp(csF32, id, flags.testFlag(HDR));+++ QImage image(QSize(768, 1024), QImage::Format_ARGB32);++++ auto createColor = [] (const KoColorSpace *cs, QRgb value) {+ KoColor c(cs);++ if (cs->colorDepthId() == Float32BitsColorDepthID) {+ float *ptr = reinterpret_cast<float*>(c.data());+ ptr[0] = KoColorSpaceMaths<quint8, float>::scaleToA(qRed(value));+ ptr[1] = KoColorSpaceMaths<quint8, float>::scaleToA(qGreen(value));+ ptr[2] = KoColorSpaceMaths<quint8, float>::scaleToA(qBlue(value));+ ptr[3] = KoColorSpaceMaths<quint8, float>::scaleToA(qAlpha(value));+ } else if (cs->colorDepthId() == Float16BitsColorDepthID) {+ half *ptr = reinterpret_cast<half*>(c.data());+ ptr[0] = KoColorSpaceMaths<quint8, half>::scaleToA(qRed(value));+ ptr[1] = KoColorSpaceMaths<quint8, half>::scaleToA(qGreen(value));+ ptr[2] = KoColorSpaceMaths<quint8, half>::scaleToA(qBlue(value));+ ptr[3] = KoColorSpaceMaths<quint8, half>::scaleToA(qAlpha(value));+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {+ quint16 *ptr = reinterpret_cast<quint16*>(c.data());+ ptr[2] = KoColorSpaceMaths<quint8, quint16>::scaleToA(qRed(value));+ ptr[1] = KoColorSpaceMaths<quint8, quint16>::scaleToA(qGreen(value));+ ptr[0] = KoColorSpaceMaths<quint8, quint16>::scaleToA(qBlue(value));+ ptr[3] = KoColorSpaceMaths<quint8, quint16>::scaleToA(qAlpha(value));+ } else {+ qFatal("bit depth is not implemented");+ }++ return c;+ };++ auto createQRgb = [] (const KoColorSpace *cs, const KoColor &c) {+ QRgb result = 0;++ if (cs->colorDepthId() == Float32BitsColorDepthID) {+ const float *ptr = reinterpret_cast<const float*>(c.data());+ result = qRgba(+ KoColorSpaceMaths<float, quint8>::scaleToA(ptr[0]),+ KoColorSpaceMaths<float, quint8>::scaleToA(ptr[1]),+ KoColorSpaceMaths<float, quint8>::scaleToA(ptr[2]),+ KoColorSpaceMaths<float, quint8>::scaleToA(ptr[3]));+ } else if (cs->colorDepthId() == Float16BitsColorDepthID) {+ const half *ptr = reinterpret_cast<const half*>(c.data());+ result = qRgba(+ KoColorSpaceMaths<half, quint8>::scaleToA(ptr[0]),+ KoColorSpaceMaths<half, quint8>::scaleToA(ptr[1]),+ KoColorSpaceMaths<half, quint8>::scaleToA(ptr[2]),+ KoColorSpaceMaths<half, quint8>::scaleToA(ptr[3]));+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {+ const quint16 *ptr = reinterpret_cast<const quint16*>(c.data());+ result = qRgba(+ KoColorSpaceMaths<quint16, quint8>::scaleToA(ptr[2]),+ KoColorSpaceMaths<quint16, quint8>::scaleToA(ptr[1]),+ KoColorSpaceMaths<quint16, quint8>::scaleToA(ptr[0]),+ KoColorSpaceMaths<quint16, quint8>::scaleToA(ptr[3]));+ } else {+ qFatal("bit depth is not implemented");+ }++ return result;+ };+++ auto drawVerticalGradient =+ [&image, createColor, createQRgb, opF32, csF32] (const QPoint &offset,+ auto dstGradient,+ auto srcGradient,+ const QString &dstText,+ const QString &srcText) {+ for (int y = 0; y < 256; y++) {+ for (int x = 0; x < 256; x++) {+ KoColor dst = createColor(csF32, dstGradient(x));+ KoColor src = createColor(csF32, srcGradient(y));++ opF32->composite(dst.data(), 0, src.data(), 0,+ 0, 0,+ 1, 1,+ OPACITY_OPAQUE_F);++ image.setPixel(x + offset.x(), y + offset.y(), createQRgb(csF32, dst));+ }+ }++ QPainter gc(&image);++ QFont font;+ font.setPointSize(10);+ gc.setFont(font);+ gc.setRenderHints(QPainter::TextAntialiasing);+ gc.setPen(QPen(Qt::black, 1));+ gc.drawText(offset + QPoint(100,100) + QPoint(1,1), QString("s: %1").arg(srcText));+ gc.drawText(offset + QPoint(100,115) + QPoint(1,1), QString("d: %1").arg(dstText));+ };++ auto r2w = [] (int i) { return qRgba(255, i, i, 255);};+ auto g2w = [] (int i) { return qRgba(i, 255, i, 255);};+ auto b2w = [] (int i) { return qRgba(i, i, 255, 255);};++ auto r2k = [] (int i) { return qRgba(255 - i, 0, 0, 255);};+ auto g2k = [] (int i) { return qRgba(0, 255 - i, 0, 255);};+ auto b2k = [] (int i) { return qRgba(0, 0, 255 - i, 255);};++ auto r2a = [] (int i) { return qRgba(255, 0, 0, 255 - i);};+ auto g2a = [] (int i) { return qRgba(0, 255, 0, 255 - i);};+ auto b2a = [] (int i) { return qRgba(0, 0, 255, 255 - i);};++ auto w2k = [] (int i) { return qRgba(255 - i, 255 - i, 255 - i, 255);};+ auto w2a = [] (int i) { return qRgba(255, 255, 255, 255 - i);};+ auto k2a = [] (int i) { return qRgba(0, 0, 0, 255 - i);};++ auto generateSheet = [&] (const QString &baseName, auto dstGradient) {+ drawVerticalGradient(QPoint(0 * 256, 0 * 256), dstGradient, r2w, baseName, "r2w");+ drawVerticalGradient(QPoint(1 * 256, 0 * 256), dstGradient, r2k, baseName, "r2k");+ drawVerticalGradient(QPoint(2 * 256, 0 * 256), dstGradient, r2a, baseName, "r2a");++ drawVerticalGradient(QPoint(0 * 256, 1 * 256), dstGradient, g2w, baseName, "g2w");+ drawVerticalGradient(QPoint(1 * 256, 1 * 256), dstGradient, g2k, baseName, "g2k");+ drawVerticalGradient(QPoint(2 * 256, 1 * 256), dstGradient, g2a, baseName, "g2a");++ drawVerticalGradient(QPoint(0 * 256, 2 * 256), dstGradient, b2w, baseName, "b2w");+ drawVerticalGradient(QPoint(1 * 256, 2 * 256), dstGradient, b2k, baseName, "b2k");+ drawVerticalGradient(QPoint(2 * 256, 2 * 256), dstGradient, b2a, baseName, "b2a");++ drawVerticalGradient(QPoint(0 * 256, 3 * 256), dstGradient, w2k, baseName, "w2k");+ drawVerticalGradient(QPoint(1 * 256, 3 * 256), dstGradient, w2a, baseName, "w2a");+ drawVerticalGradient(QPoint(2 * 256, 3 * 256), dstGradient, k2a, baseName, "k2a");+++ QString hdrString = flags.testFlag(HDR) ? "_hdr" : "_sdr";++ if (id == COMPOSITE_BURN || id == COMPOSITE_LINEAR_BURN || id == COMPOSITE_PIN_LIGHT) {+ hdrString = "";+ }++ image.save(QString("sample_sheet_%1%2_%3_%4.png")+ .arg(id,+ hdrString,+ baseName,+ csShortName(csF32->colorDepthId())));+ };++ generateSheet("r2w", r2w);+ generateSheet("r2k", r2k);+ generateSheet("w2k", w2k);+ generateSheet("w2a", w2a);+ generateSheet("k2a", k2a);++}++void TestCompositeOpInversion::generateSampleSheetsLong_data()+{+ generateSampleSheets_data();+}++void TestCompositeOpInversion::generateSampleSheetsLong()+{+ QFETCH(QString, id);+ QFETCH(TestFlags, flags);+ QFETCH(KoID, depthId);++ const KoColorSpace* csF32 = KoColorSpaceRegistry::instance()->colorSpace(RGBAColorModelID.id(), depthId.id(), 0);+ const KoCompositeOp *opF32 = createOp(csF32, id, flags.testFlag(HDR));+++ auto createColor = [] (const KoColorSpace *cs, QRgba64 value) {+ KoColor c(cs);++ if (cs->colorDepthId() == Float32BitsColorDepthID) {+ float *ptr = reinterpret_cast<float*>(c.data());+ ptr[0] = KoColorSpaceMaths<quint16, float>::scaleToA(value.red());+ ptr[1] = KoColorSpaceMaths<quint16, float>::scaleToA(value.green());+ ptr[2] = KoColorSpaceMaths<quint16, float>::scaleToA(value.blue());+ ptr[3] = KoColorSpaceMaths<quint16, float>::scaleToA(value.alpha());+ } else if (cs->colorDepthId() == Float16BitsColorDepthID) {+ half *ptr = reinterpret_cast<half*>(c.data());+ ptr[0] = KoColorSpaceMaths<quint16, half>::scaleToA(value.red());+ ptr[1] = KoColorSpaceMaths<quint16, half>::scaleToA(value.green());+ ptr[2] = KoColorSpaceMaths<quint16, half>::scaleToA(value.blue());+ ptr[3] = KoColorSpaceMaths<quint16, half>::scaleToA(value.alpha());+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {+ quint16 *ptr = reinterpret_cast<quint16*>(c.data());+ ptr[2] = value.red();+ ptr[1] = value.green();+ ptr[0] = value.blue();+ ptr[3] = value.alpha();+ } else {+ qFatal("bit depth is not implemented");+ }++ return c;+ };++ auto createQRgb = [] (const KoColorSpace *cs, const KoColor &c) -> QRgb {+ QRgba64 result;++ if (cs->colorDepthId() == Float32BitsColorDepthID) {+ const float *ptr = reinterpret_cast<const float*>(c.data());+ result = qRgba64(+ KoColorSpaceMaths<float, quint16>::scaleToA(ptr[0]),+ KoColorSpaceMaths<float, quint16>::scaleToA(ptr[1]),+ KoColorSpaceMaths<float, quint16>::scaleToA(ptr[2]),+ KoColorSpaceMaths<float, quint16>::scaleToA(ptr[3]));+ } else if (cs->colorDepthId() == Float16BitsColorDepthID) {+ const half *ptr = reinterpret_cast<const half*>(c.data());+ result = qRgba64(+ KoColorSpaceMaths<half, quint16>::scaleToA(ptr[0]),+ KoColorSpaceMaths<half, quint16>::scaleToA(ptr[1]),+ KoColorSpaceMaths<half, quint16>::scaleToA(ptr[2]),+ KoColorSpaceMaths<half, quint16>::scaleToA(ptr[3]));+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {+ const quint16 *ptr = reinterpret_cast<const quint16*>(c.data());+ result = qRgba64(+ KoColorSpaceMaths<quint16, quint16>::scaleToA(ptr[2]),+ KoColorSpaceMaths<quint16, quint16>::scaleToA(ptr[1]),+ KoColorSpaceMaths<quint16, quint16>::scaleToA(ptr[0]),+ KoColorSpaceMaths<quint16, quint16>::scaleToA(ptr[3]));+ } else {+ qFatal("bit depth is not implemented");+ }++ return result.toArgb32();+ };++ const int height = 256;+ const int width = 1536;++ auto drawVerticalGradient =+ [&] (const QPoint &offset,+ auto dstGradient,+ auto srcGradient,+ const QString &dstText,+ const QString &srcText,+ bool flipSrcDst,+ QImage &image) {++ const int &patchWidth = !flipSrcDst ? width : height;+ const int &patchHeight = !flipSrcDst ? height : width;++ for (int y = 0; y < patchHeight; y++) {+ for (int x = 0; x < patchWidth; x++) {+ KoColor dst = createColor(csF32, dstGradient(qreal(y) / patchHeight));+ KoColor src = createColor(csF32, srcGradient(qreal(x) / patchWidth));++ opF32->composite(dst.data(), 0, src.data(), 0,+ 0, 0,+ 1, 1,+ OPACITY_OPAQUE_F);++ image.setPixel(x + offset.x(), y + offset.y(), createQRgb(csF32, dst));+ }+ }++ QPainter gc(&image);++ QFont font;+ font.setPointSize(10);+ gc.setFont(font);+ gc.setRenderHints(QPainter::TextAntialiasing);+ gc.setPen(QPen(Qt::black, 1));+ gc.drawText(offset + QPoint(100,100) + QPoint(1,1), QString("s: %1 (x)").arg(srcText));+ gc.drawText(offset + QPoint(100,115) + QPoint(1,1), QString("d: %1 (y)").arg(dstText));+ };++ auto makeQRgba64F = [] (qreal r, qreal g, qreal b, qreal a) {+ return qRgba64(qRound(r * 0xffffu),+ qRound(g * 0xffffu),+ qRound(b * 0xffffu),+ qRound(a * 0xffffu));+ };++ auto r2w = [&] (qreal i) { return makeQRgba64F(1.0, i, i, 1.0);};+ auto g2w = [&] (qreal i) { return makeQRgba64F(i, 1.0, i, 1.0);};+ auto b2w = [&] (qreal i) { return makeQRgba64F(i, i, 1.0, 1.0);};++ auto r2k = [&] (qreal i) { return makeQRgba64F(1.0 - i, 0, 0, 1.0);};+ auto g2k = [&] (qreal i) { return makeQRgba64F(0, 1.0 - i, 0, 1.0);};+ auto b2k = [&] (qreal i) { return makeQRgba64F(0, 0, 1.0 - i, 1.0);};++ auto r2a = [&] (qreal i) { return makeQRgba64F(1.0, 0, 0, 1.0 - i);};+ auto g2a = [&] (qreal i) { return makeQRgba64F(0, 1.0, 0, 1.0 - i);};+ auto b2a = [&] (qreal i) { return makeQRgba64F(0, 0, 1.0, 1.0 - i);};++ auto w2k = [&] (qreal i) { return makeQRgba64F(1.0 - i, 1.0 - i, 1.0 - i, 1.0);};+ auto w2a = [&] (qreal i) { return makeQRgba64F(1.0, 1.0, 1.0, 1.0 - i);};+ auto k2a = [&] (qreal i) { return makeQRgba64F(0, 0, 0, 1.0 - i);};++ auto generateSheet = [&] (const QString &baseName, auto dstGradient, bool flipSrcDst) {++ const int stepX = !flipSrcDst ? 0 : height;+ const int stepY = !flipSrcDst ? height : 0;++ const int imageWidth = !flipSrcDst ? width : 9 * height;+ const int imageHeight = !flipSrcDst ? 9 * height : width;++ QImage image(QSize(imageWidth, imageHeight), QImage::Format_ARGB32);++ drawVerticalGradient(QPoint(0 * stepX, 0 * stepY), dstGradient, r2w, baseName, "r2w", flipSrcDst, image);+ drawVerticalGradient(QPoint(1 * stepX, 1 * stepY), dstGradient, r2k, baseName, "r2k", flipSrcDst, image);+ drawVerticalGradient(QPoint(2 * stepX, 2 * stepY), dstGradient, r2a, baseName, "r2a", flipSrcDst, image);++ drawVerticalGradient(QPoint(3 * stepX, 3 * stepY), dstGradient, b2w, baseName, "b2w", flipSrcDst, image);+ drawVerticalGradient(QPoint(4 * stepX, 4 * stepY), dstGradient, b2k, baseName, "b2k", flipSrcDst, image);+ drawVerticalGradient(QPoint(5 * stepX, 5 * stepY), dstGradient, b2a, baseName, "b2a", flipSrcDst, image);++ drawVerticalGradient(QPoint(6 * stepX, 6 * stepY), dstGradient, w2k, baseName, "w2k", flipSrcDst, image);+ drawVerticalGradient(QPoint(7 * stepX, 7 * stepY), dstGradient, w2a, baseName, "w2a", flipSrcDst, image);+ drawVerticalGradient(QPoint(8 * stepX, 8 * stepY), dstGradient, k2a, baseName, "k2a", flipSrcDst, image);+++ QString hdrString = flags.testFlag(HDR) ? "_hdr" : "_sdr";++ if (id == COMPOSITE_BURN || id == COMPOSITE_LINEAR_BURN || id == COMPOSITE_PIN_LIGHT) {+ hdrString = "";+ }++ image.save(QString("sample_sheet_%1%2_%3_%4_%5.png")+ .arg(id,+ hdrString,+ baseName,+ flipSrcDst ? "flip" : "nor",+ csShortName(csF32->colorDepthId())));+ };++ generateSheet("r2w", r2w, false);+ generateSheet("r2k", r2k, false);+ generateSheet("w2k", w2k, false);+ generateSheet("w2a", w2a, false);+ generateSheet("k2a", k2a, false);++ generateSheet("r2w", r2w, true);+ generateSheet("r2k", r2k, true);+ generateSheet("w2k", w2k, true);+ generateSheet("w2a", w2a, true);+ generateSheet("k2a", k2a, true);++}++void TestCompositeOpInversion::testColor()+{+ const KoColorSpace* csF32 = KoColorSpaceRegistry::instance()->colorSpace(RGBAColorModelID.id(), Float32BitsColorDepthID.id(), 0);+ const KoCompositeOp *opF32 = createOp(csF32, COMPOSITE_DIVIDE, false);++ KoColor src(QColor(255, 0, 0), csF32);+ KoColor dst(QColor(120, 255, 255), csF32);++ qDebug() << ppVar(src) << ppVar(dst);++ opF32->composite(dst.data(), 0, src.data(), 0,+ 0, 0,+ 1, 1,+ OPACITY_OPAQUE_F);++ qDebug() << ppVar(dst);+++}+SIMPLE_TEST_MAIN(TestCompositeOpInversion)diff --git a/libs/pigment/tests/TestCompositeOpInversion.h b/libs/pigment/tests/TestCompositeOpInversion.hindex cce5c65861..4c2ff7f66a 100644--- a/libs/pigment/tests/TestCompositeOpInversion.h+++ b/libs/pigment/tests/TestCompositeOpInversion.h@@ -44,6 +44,14 @@ private Q_SLOTS:void dumpOpCategories();+ void generateSampleSheets();+ void generateSampleSheets_data();++ void generateSampleSheetsLong_data();+ void generateSampleSheetsLong();++ void testColor();+private:// TODO: disabled for nowvoid testF16Modes();diff --git a/libs/pigment/tests/TestCompositeOpInversion_p.cpp b/libs/pigment/tests/TestCompositeOpInversion_p.cppindex 2883768634..7d28575360 100644--- a/libs/pigment/tests/TestCompositeOpInversion_p.cpp+++ b/libs/pigment/tests/TestCompositeOpInversion_p.cpp@@ -11,36 +11,358 @@#include <KoCompositeOpRegistry.h>#include <KoColorModelStandardIds.h>+//#define USE_LOCAL_BUILD_OF_OPS+//#define BUILD_ONLY_BASIC_OPS++#ifdef USE_LOCAL_BUILD_OF_OPS+#include <KoCompositeOpGeneric2.h>+#include <KoCompositeOpFunctions2.h>+#include <compositeops/KoCompositeOpGreater.h>+#else#include <KoCompositeOpGeneric.h>#include <KoCompositeOpFunctions.h>+#endif+#include "compositeops/KoColorSpaceBlendingPolicy.h"#include "compositeops/KoCompositeOpClampPolicy.h"+#ifdef USE_LOCAL_BUILD_OF_OPS++namespace detail {++template<class Traits>+struct OpsStorage {+ typedef typename Traits::channels_type Arg;+ typedef Arg (*CompositeFunc)(Arg, Arg);+ static const qint32 alpha_pos = Traits::alpha_pos;+ static constexpr bool IsIntegerSpace = std::numeric_limits<Arg>::is_integer;++ template<CompositeFunc func>+ void add(const KoColorSpace* cs, const QString& id, const QString& category) {+ if constexpr (std::is_base_of_v<KoCmykTraits<typename Traits::channels_type>, Traits>) {+ if (useSubtractiveBlendingForCmykColorSpaces()) {+ m_ops.insert(id, new KoCompositeOpGenericSC<Traits, func, KoSubtractiveBlendingPolicy<Traits>>(cs, id, category));+ } else {+ m_ops.insert(id,new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, category));+ }+ } else {+ m_ops.insert(id,new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, category));+ }+ }++ template<typename Functor>+ void add(const KoColorSpace* cs, const QString& id, const QString& category) {+ if constexpr (std::is_base_of_v<KoCmykTraits<typename Traits::channels_type>, Traits>) {+ if (useSubtractiveBlendingForCmykColorSpaces()) {+ m_ops.insert(id, new KoCompositeOpGenericSCFunctor<Traits, Functor, KoSubtractiveBlendingPolicy<Traits>>(cs, id, category));+ } else {+ m_ops.insert(id, new KoCompositeOpGenericSCFunctor<Traits, Functor, KoAdditiveBlendingPolicy<Traits>>(cs, id, category));+ }+ } else {+ m_ops.insert(id, new KoCompositeOpGenericSCFunctor<Traits, Functor, KoAdditiveBlendingPolicy<Traits>>(cs, id, category));+ }+ }++ void add(const KoColorSpace* cs) {+ using namespace KoCompositeOpClampPolicy;++ if constexpr (std::is_base_of_v<KoCmykTraits<typename Traits::channels_type>, Traits>) {+ if (useSubtractiveBlendingForCmykColorSpaces()) {+ m_ops.insert(COMPOSITE_GREATER, new KoCompositeOpGreater<Traits, KoSubtractiveBlendingPolicy<Traits>>(cs));+ } else {+ m_ops.insert(COMPOSITE_GREATER, new KoCompositeOpGreater<Traits, KoAdditiveBlendingPolicy<Traits>>(cs));+ }+ } else {+ m_ops.insert(COMPOSITE_GREATER, new KoCompositeOpGreater<Traits, KoAdditiveBlendingPolicy<Traits>>(cs));+ }++ add<CFOverlay<Arg> >(cs, COMPOSITE_OVERLAY , KoCompositeOp::categoryMix());+ add<CFGrainMerge<Arg> >(cs, COMPOSITE_GRAIN_MERGE , KoCompositeOp::categoryMix());+ add<CFGrainExtract<Arg> >(cs, COMPOSITE_GRAIN_EXTRACT , KoCompositeOp::categoryMix());+ add<FunctorWithSDRClampPolicy<CFHardMix, Arg>>(cs, COMPOSITE_HARD_MIX, KoCompositeOp::categoryMix());+ if constexpr (!IsIntegerSpace) {+ add<CFHardMix<Arg, ClampAsFloatHDR>>(cs, COMPOSITE_HARD_MIX_HDR, KoCompositeOp::categoryMix());+ }++ add<CFHardMixPhotoshop<Arg>>(cs, COMPOSITE_HARD_MIX_PHOTOSHOP, KoCompositeOp::categoryMix());+ add<CFHardMixSofterPhotoshop<Arg>>(cs, COMPOSITE_HARD_MIX_SOFTER_PHOTOSHOP, KoCompositeOp::categoryMix());+ add<CFGeometricMean<Arg> >(cs, COMPOSITE_GEOMETRIC_MEAN, KoCompositeOp::categoryMix());+ add<CFParallel<Arg> >(cs, COMPOSITE_PARALLEL , KoCompositeOp::categoryMix());+ add<&cfAllanon<Arg> >(cs, COMPOSITE_ALLANON , KoCompositeOp::categoryMix());+ add<FunctorWithSDRClampPolicy<CFHardOverlay, Arg>>(cs, COMPOSITE_HARD_OVERLAY, KoCompositeOp::categoryMix());+ if constexpr (!IsIntegerSpace) {+ add<CFHardOverlay<Arg, ClampAsFloatHDR>>(cs, COMPOSITE_HARD_OVERLAY_HDR, KoCompositeOp::categoryMix());+ }++#ifndef BUILD_ONLY_BASIC_OPS+ add<&cfInterpolation<Arg> >(cs, COMPOSITE_INTERPOLATION , KoCompositeOp::categoryMix());+ add<&cfInterpolationB<Arg>>(cs, COMPOSITE_INTERPOLATIONB, KoCompositeOp::categoryMix());+ add<CFPenumbraA<Arg> >(cs, COMPOSITE_PENUMBRAA , KoCompositeOp::categoryMix());+ add<CFPenumbraB<Arg> >(cs, COMPOSITE_PENUMBRAB , KoCompositeOp::categoryMix());+ add<&cfPenumbraC<Arg> >(cs, COMPOSITE_PENUMBRAC , KoCompositeOp::categoryMix());+ add<&cfPenumbraD<Arg> >(cs, COMPOSITE_PENUMBRAD , KoCompositeOp::categoryMix());+#endif+ add<&cfScreen<Arg> >(cs, COMPOSITE_SCREEN , KoCompositeOp::categoryLight());++ add<FunctorWithSDRClampPolicy<CFColorDodge, Arg>>(cs, COMPOSITE_DODGE, KoCompositeOp::categoryLight());+ if constexpr (!IsIntegerSpace) {+ add<CFColorDodge<Arg, ClampAsFloatHDR>>(cs, COMPOSITE_DODGE_HDR, KoCompositeOp::categoryLight());+ }++ add<&cfAddition<Arg> >(cs, COMPOSITE_LINEAR_DODGE, KoCompositeOp::categoryLight());+ add<&cfLightenOnly<Arg> >(cs, COMPOSITE_LIGHTEN , KoCompositeOp::categoryLight());+ add<CFHardLight<Arg> >(cs, COMPOSITE_HARD_LIGHT , KoCompositeOp::categoryLight());+ add<&cfSoftLightIFSIllusions<Arg>>(cs, COMPOSITE_SOFT_LIGHT_IFS_ILLUSIONS, KoCompositeOp::categoryLight());+ add<&cfSoftLightPegtopDelphi<Arg>>(cs, COMPOSITE_SOFT_LIGHT_PEGTOP_DELPHI, KoCompositeOp::categoryLight());+ add<CFSoftLightSvg<Arg> >(cs, COMPOSITE_SOFT_LIGHT_SVG, KoCompositeOp::categoryLight());+ add<CFSoftLight<Arg> >(cs, COMPOSITE_SOFT_LIGHT_PHOTOSHOP, KoCompositeOp::categoryLight());+ add<CFGammaLight<Arg> >(cs, COMPOSITE_GAMMA_LIGHT , KoCompositeOp::categoryLight());+ add<CFGammaIllumination<Arg>>(cs, COMPOSITE_GAMMA_ILLUMINATION, KoCompositeOp::categoryLight());++ add<FunctorWithSDRClampPolicy<CFVividLight, Arg>>(cs, COMPOSITE_VIVID_LIGHT, KoCompositeOp::categoryLight());+ if constexpr (!IsIntegerSpace) {+ add<CFVividLight<Arg, ClampAsFloatHDR>>(cs, COMPOSITE_VIVID_LIGHT_HDR, KoCompositeOp::categoryLight());+ }+ add<CFFlatLight<Arg> >(cs, COMPOSITE_FLAT_LIGHT , KoCompositeOp::categoryLight());+ add<CFPinLight<Arg>>(cs, COMPOSITE_PIN_LIGHT, KoCompositeOp::categoryLight()); // using HDR mode as default+ add<&cfLinearLight<Arg> >(cs, COMPOSITE_LINEAR_LIGHT, KoCompositeOp::categoryLight());+ add<&cfPNormA<Arg> >(cs, COMPOSITE_PNORM_A , KoCompositeOp::categoryLight());+ add<&cfPNormB<Arg> >(cs, COMPOSITE_PNORM_B , KoCompositeOp::categoryLight());+ add<&cfSuperLight<Arg> >(cs, COMPOSITE_SUPER_LIGHT , KoCompositeOp::categoryLight());+ add<&cfTintIFSIllusions<Arg>>(cs, COMPOSITE_TINT_IFS_ILLUSIONS, KoCompositeOp::categoryLight());+ add<&cfFogLightenIFSIllusions<Arg>>(cs, COMPOSITE_FOG_LIGHTEN_IFS_ILLUSIONS, KoCompositeOp::categoryLight());+ add<&cfEasyDodge<Arg> >(cs, COMPOSITE_EASY_DODGE , KoCompositeOp::categoryLight());++ add<CFColorBurn<Arg>>(cs, COMPOSITE_BURN, KoCompositeOp::categoryDark()); // using HDR mode as default+ add<CFLinearBurn<Arg, ClampAsFloatHDR>>(cs, COMPOSITE_LINEAR_BURN , KoCompositeOp::categoryDark()); // using HDR mode as default+ add<&cfDarkenOnly<Arg> >(cs, COMPOSITE_DARKEN , KoCompositeOp::categoryDark());+ add<CFGammaDark<Arg> >(cs, COMPOSITE_GAMMA_DARK , KoCompositeOp::categoryDark());+ add<&cfShadeIFSIllusions<Arg>>(cs, COMPOSITE_SHADE_IFS_ILLUSIONS, KoCompositeOp::categoryDark());+ add<&cfFogDarkenIFSIllusions<Arg>>(cs, COMPOSITE_FOG_DARKEN_IFS_ILLUSIONS, KoCompositeOp::categoryDark());+ add<&cfEasyBurn<Arg> >(cs, COMPOSITE_EASY_BURN , KoCompositeOp::categoryDark());++ add<&cfAddition<Arg> >(cs, COMPOSITE_ADD , KoCompositeOp::categoryArithmetic());+ add<&cfSubtract<Arg> >(cs, COMPOSITE_SUBTRACT , KoCompositeOp::categoryArithmetic());+ add<CFInverseSubtract<Arg> >(cs, COMPOSITE_INVERSE_SUBTRACT, KoCompositeOp::categoryArithmetic());+ add<&cfMultiply<Arg> >(cs, COMPOSITE_MULT , KoCompositeOp::categoryArithmetic());+ add<CFDivide<Arg> >(cs, COMPOSITE_DIVIDE , KoCompositeOp::categoryArithmetic());++#ifndef BUILD_ONLY_BASIC_OPS+ add<&cfModulo<Arg> >(cs, COMPOSITE_MOD , KoCompositeOp::categoryModulo());+ add<&cfModuloContinuous<Arg> >(cs, COMPOSITE_MOD_CON , KoCompositeOp::categoryModulo());+ add<&cfDivisiveModulo<Arg> >(cs, COMPOSITE_DIVISIVE_MOD , KoCompositeOp::categoryModulo());+ add<&cfDivisiveModuloContinuous<Arg>>(cs, COMPOSITE_DIVISIVE_MOD_CON, KoCompositeOp::categoryModulo());+ add<&cfModuloShift<Arg> >(cs, COMPOSITE_MODULO_SHIFT , KoCompositeOp::categoryModulo());+ add<&cfModuloShiftContinuous<Arg>>(cs, COMPOSITE_MODULO_SHIFT_CON , KoCompositeOp::categoryModulo());+#endif+ add<&cfArcTangent<Arg> >(cs, COMPOSITE_ARC_TANGENT , KoCompositeOp::categoryNegative());+ add<&cfDifference<Arg> >(cs, COMPOSITE_DIFF , KoCompositeOp::categoryNegative());+ add<CFExclusion<Arg> >(cs, COMPOSITE_EXCLUSION , KoCompositeOp::categoryNegative());+ add<CFEquivalence<Arg> >(cs, COMPOSITE_EQUIVALENCE , KoCompositeOp::categoryNegative());+ add<CFAdditiveSubtractive<Arg> >(cs, COMPOSITE_ADDITIVE_SUBTRACTIVE, KoCompositeOp::categoryNegative());+ add<CFNegation<Arg> >(cs, COMPOSITE_NEGATION , KoCompositeOp::categoryNegative());++#ifndef BUILD_ONLY_BASIC_OPS+ if constexpr (IsIntegerSpace) {+ add<&cfXor<Arg> >(cs, COMPOSITE_XOR , KoCompositeOp::categoryBinary());+ add<&cfOr<Arg> >(cs, COMPOSITE_OR , KoCompositeOp::categoryBinary());+ add<&cfAnd<Arg> >(cs, COMPOSITE_AND , KoCompositeOp::categoryBinary());+ add<&cfNand<Arg> >(cs, COMPOSITE_NAND , KoCompositeOp::categoryBinary());+ add<&cfNor<Arg> >(cs, COMPOSITE_NOR , KoCompositeOp::categoryBinary());+ add<&cfXnor<Arg> >(cs, COMPOSITE_XNOR , KoCompositeOp::categoryBinary());+ add<&cfImplies<Arg> >(cs, COMPOSITE_IMPLICATION , KoCompositeOp::categoryBinary());+ add<&cfNotImplies<Arg> >(cs, COMPOSITE_NOT_IMPLICATION, KoCompositeOp::categoryBinary());+ add<&cfConverse<Arg> >(cs, COMPOSITE_CONVERSE , KoCompositeOp::categoryBinary());+ add<&cfNotConverse<Arg>>(cs, COMPOSITE_NOT_CONVERSE , KoCompositeOp::categoryBinary());+ }++ add<&cfReflect<Arg>>(cs, COMPOSITE_REFLECT, KoCompositeOp::categoryQuadratic());+ add<&cfGlow<Arg> >(cs, COMPOSITE_GLOW , KoCompositeOp::categoryQuadratic());+ add<&cfFreeze<Arg> >(cs, COMPOSITE_FREEZE , KoCompositeOp::categoryQuadratic());+ add<&cfHeat<Arg> >(cs, COMPOSITE_HEAT , KoCompositeOp::categoryQuadratic());+ add<CFGleat<Arg> >(cs, COMPOSITE_GLEAT , KoCompositeOp::categoryQuadratic());+ add<CFHelow<Arg> >(cs, COMPOSITE_HELOW , KoCompositeOp::categoryQuadratic());+ add<CFReeze<Arg> >(cs, COMPOSITE_REEZE , KoCompositeOp::categoryQuadratic());+ add<CFFrect<Arg> >(cs, COMPOSITE_FRECT , KoCompositeOp::categoryQuadratic());+ add<&cfFhyrd<Arg> >(cs, COMPOSITE_FHYRD , KoCompositeOp::categoryQuadratic());+#endif+ }++ bool isInitialized() const {+ return !m_ops.isEmpty();+ }++ const KoCompositeOp* compositeOp(const QString &id) {+ return m_ops.value(id);+ }++private:++ QMap<QString, KoCompositeOp*> m_ops;+};++template<class Traits>+struct RgbOpsStorage {+ typedef float Arg;++ static const qint32 red_pos = Traits::red_pos;+ static const qint32 green_pos = Traits::green_pos;+ static const qint32 blue_pos = Traits::blue_pos;++ template<void compositeFunc(Arg, Arg, Arg, Arg&, Arg&, Arg&)>++ void add(const KoColorSpace* cs, const QString& id, const QString& category) {+ m_ops.insert(id, new KoCompositeOpGenericHSL<Traits, compositeFunc>(cs, id, category));+ }++ void add(const KoColorSpace* cs) {++ add<&cfTangentNormalmap <HSYType,Arg> >(cs, COMPOSITE_TANGENT_NORMALMAP , KoCompositeOp::categoryMisc());+ add<&cfReorientedNormalMapCombine <HSYType, Arg> >(cs, COMPOSITE_COMBINE_NORMAL, KoCompositeOp::categoryMisc());++ add<&cfColor <HSYType,Arg> >(cs, COMPOSITE_COLOR , KoCompositeOp::categoryHSY());+ add<&cfHue <HSYType,Arg> >(cs, COMPOSITE_HUE , KoCompositeOp::categoryHSY());+ add<&cfSaturation <HSYType,Arg> >(cs, COMPOSITE_SATURATION , KoCompositeOp::categoryHSY());+ add<&cfIncreaseSaturation<HSYType,Arg> >(cs, COMPOSITE_INC_SATURATION, KoCompositeOp::categoryHSY());+ add<&cfDecreaseSaturation<HSYType,Arg> >(cs, COMPOSITE_DEC_SATURATION, KoCompositeOp::categoryHSY());+ add<&cfLightness <HSYType,Arg> >(cs, COMPOSITE_LUMINIZE , KoCompositeOp::categoryHSY());+ add<&cfIncreaseLightness <HSYType,Arg> >(cs, COMPOSITE_INC_LUMINOSITY, KoCompositeOp::categoryHSY());+ add<&cfDecreaseLightness <HSYType,Arg> >(cs, COMPOSITE_DEC_LUMINOSITY, KoCompositeOp::categoryHSY());+ add<&cfDarkerColor <HSYType,Arg> >(cs, COMPOSITE_DARKER_COLOR , KoCompositeOp::categoryDark());//darker color as PSD does it//+ add<&cfLighterColor <HSYType,Arg> >(cs, COMPOSITE_LIGHTER_COLOR , KoCompositeOp::categoryLight());//lighter color as PSD does it//++ add<&cfLambertLighting <HSIType,Arg> >(cs, COMPOSITE_LAMBERT_LIGHTING, KoCompositeOp::categoryMix());+ add<&cfLambertLightingGamma2_2 <HSIType, Arg> >(cs, COMPOSITE_LAMBERT_LIGHTING_GAMMA_2_2, KoCompositeOp::categoryMix());++ add<&cfColor <HSIType,Arg> >(cs, COMPOSITE_COLOR_HSI , KoCompositeOp::categoryHSI());+ add<&cfHue <HSIType,Arg> >(cs, COMPOSITE_HUE_HSI , KoCompositeOp::categoryHSI());+ add<&cfSaturation <HSIType,Arg> >(cs, COMPOSITE_SATURATION_HSI , KoCompositeOp::categoryHSI());+ add<&cfIncreaseSaturation<HSIType,Arg> >(cs, COMPOSITE_INC_SATURATION_HSI, KoCompositeOp::categoryHSI());+ add<&cfDecreaseSaturation<HSIType,Arg> >(cs, COMPOSITE_DEC_SATURATION_HSI, KoCompositeOp::categoryHSI());+ add<&cfLightness <HSIType,Arg> >(cs, COMPOSITE_INTENSITY , KoCompositeOp::categoryHSI());+ add<&cfIncreaseLightness <HSIType,Arg> >(cs, COMPOSITE_INC_INTENSITY , KoCompositeOp::categoryHSI());+ add<&cfDecreaseLightness <HSIType,Arg> >(cs, COMPOSITE_DEC_INTENSITY , KoCompositeOp::categoryHSI());++ add<&cfColor <HSLType,Arg> >(cs, COMPOSITE_COLOR_HSL , KoCompositeOp::categoryHSL());+ add<&cfHue <HSLType,Arg> >(cs, COMPOSITE_HUE_HSL , KoCompositeOp::categoryHSL());+ add<&cfSaturation <HSLType,Arg> >(cs, COMPOSITE_SATURATION_HSL , KoCompositeOp::categoryHSL());+ add<&cfIncreaseSaturation<HSLType,Arg> >(cs, COMPOSITE_INC_SATURATION_HSL, KoCompositeOp::categoryHSL());+ add<&cfDecreaseSaturation<HSLType,Arg> >(cs, COMPOSITE_DEC_SATURATION_HSL, KoCompositeOp::categoryHSL());+ add<&cfLightness <HSLType,Arg> >(cs, COMPOSITE_LIGHTNESS , KoCompositeOp::categoryHSL());+ add<&cfIncreaseLightness <HSLType,Arg> >(cs, COMPOSITE_INC_LIGHTNESS , KoCompositeOp::categoryHSL());+ add<&cfDecreaseLightness <HSLType,Arg> >(cs, COMPOSITE_DEC_LIGHTNESS , KoCompositeOp::categoryHSL());++ add<&cfColor <HSVType,Arg> >(cs, COMPOSITE_COLOR_HSV , KoCompositeOp::categoryHSV());+ add<&cfHue <HSVType,Arg> >(cs, COMPOSITE_HUE_HSV , KoCompositeOp::categoryHSV());+ add<&cfSaturation <HSVType,Arg> >(cs, COMPOSITE_SATURATION_HSV , KoCompositeOp::categoryHSV());+ add<&cfIncreaseSaturation<HSVType,Arg> >(cs, COMPOSITE_INC_SATURATION_HSV, KoCompositeOp::categoryHSV());+ add<&cfDecreaseSaturation<HSVType,Arg> >(cs, COMPOSITE_DEC_SATURATION_HSV, KoCompositeOp::categoryHSV());+ add<&cfLightness <HSVType,Arg> >(cs, COMPOSITE_VALUE , KoCompositeOp::categoryHSV());+ add<&cfIncreaseLightness <HSVType,Arg> >(cs, COMPOSITE_INC_VALUE , KoCompositeOp::categoryHSV());+ add<&cfDecreaseLightness <HSVType,Arg> >(cs, COMPOSITE_DEC_VALUE , KoCompositeOp::categoryHSV());+ }++ bool isInitialized() const {+ return !m_ops.isEmpty();+ }++ const KoCompositeOp* compositeOp(const QString &id) {+ return m_ops.value(id);+ }++private:++ QMap<QString, KoCompositeOp*> m_ops;+};++OpsStorage<KoRgbU16Traits> sU16Ops;+OpsStorage<KoRgbF16Traits> sF16Ops;+OpsStorage<KoRgbF32Traits> sF32Ops;++RgbOpsStorage<KoRgbU16Traits> sU16RgbOps;+RgbOpsStorage<KoRgbF16Traits> sF16RgbOps;+RgbOpsStorage<KoRgbF32Traits> sF32RgbOps;++}++#endif /* USE_LOCAL_BUILD_OF_OPS */+const KoCompositeOp* createOp(const KoColorSpace *cs, const QString &id, bool isHDR){using namespace KoCompositeOpClampPolicy;const KoCompositeOp *op = nullptr;+#ifdef USE_LOCAL_BUILD_OF_OPS+ const bool isFloat = cs->colorDepthId() == Float32BitsColorDepthID || cs->colorDepthId() == Float16BitsColorDepthID;+ QString effectiveId = id;++ if (isHDR && isFloat) {+ if (id == COMPOSITE_DODGE) {+ effectiveId = COMPOSITE_DODGE_HDR;+ } else if (id == COMPOSITE_VIVID_LIGHT) {+ effectiveId = COMPOSITE_VIVID_LIGHT_HDR;+ } else if (id == COMPOSITE_HARD_MIX) {+ effectiveId = COMPOSITE_HARD_MIX_HDR;+ } else if (id == COMPOSITE_HARD_OVERLAY) {+ effectiveId = COMPOSITE_HARD_OVERLAY_HDR;+ }+ }++ if (cs->colorDepthId() == Integer16BitsColorDepthID) {+ if (!detail::sU16Ops.isInitialized()) {+ detail::sU16Ops.add(cs);+ }++ op = detail::sU16Ops.compositeOp(effectiveId);++ if (!op) {+ if (!detail::sU16RgbOps.isInitialized()) {+ detail::sU16RgbOps.add(cs);+ }++ op = detail::sU16RgbOps.compositeOp(effectiveId);+ }++ } else if (cs->colorDepthId() == Float16BitsColorDepthID) {+ if (!detail::sF16Ops.isInitialized()) {+ detail::sF16Ops.add(cs);+ }++ op = detail::sF16Ops.compositeOp(effectiveId);++ if (!op) {+ if (!detail::sF16RgbOps.isInitialized()) {+ detail::sF16RgbOps.add(cs);+ }++ op = detail::sF16RgbOps.compositeOp(effectiveId);+ }++ } else if (cs->colorDepthId() == Float32BitsColorDepthID) {+ if (!detail::sF32Ops.isInitialized()) {+ detail::sF32Ops.add(cs);+ }++ op = detail::sF32Ops.compositeOp(effectiveId);++ if (!op) {+ if (!detail::sF32RgbOps.isInitialized()) {+ detail::sF32RgbOps.add(cs);+ }++ op = detail::sF32RgbOps.compositeOp(effectiveId);+ }+ }+#else+/*** Some of the blendmodes exist only "in theory". We test them only to make sure* they are not actually needed in real life (exactly the same in SDR range and* SDR/negative-preserving).*/- if (id == COMPOSITE_BURN && !isHDR) { // SDR-only, HDR is the default- if (cs->colorDepthId() == Float32BitsColorDepthID) {- using Traits = KoRgbF32Traits;- using func = CFColorBurn<float, ClampAsFloatSDR>;- op = new KoCompositeOpGenericSCFunctor<Traits, func,- KoAdditiveBlendingPolicy<Traits>>- (cs, id, KoCompositeOp::categoryDark());- } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {- using Traits = KoRgbU16Traits;- using func = CFColorBurn<quint16, ClampAsInteger>;- op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryDark());- }- } else if (id == COMPOSITE_LINEAR_BURN && !isHDR) { // SDR-only, HDR is the default+ if (id == COMPOSITE_LINEAR_BURN && !isHDR) { // SDR-only, HDR is the defaultif (cs->colorDepthId() == Float32BitsColorDepthID) {using Traits = KoRgbF32Traits;using func = CFLinearBurn<float, ClampAsFloatSDR>;@@ -58,16 +380,6 @@ const KoCompositeOp* createOp(const KoColorSpace *cs, const QString &id, bool isconst bool isFloat = cs->colorDepthId() == Float32BitsColorDepthID || cs->colorDepthId() == Float16BitsColorDepthID;const QString newId = isHDR && isFloat ? COMPOSITE_VIVID_LIGHT_HDR : COMPOSITE_VIVID_LIGHT;op = cs->compositeOp(newId);- } else if (id == COMPOSITE_PIN_LIGHT && !isHDR) { // SDR-only, HDR is the default- if (cs->colorDepthId() == Float32BitsColorDepthID) {- using Traits = KoRgbF32Traits;- using func = CFPinLight<float, ClampAsFloatSDR>;- op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryDark());- } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {- using Traits = KoRgbU16Traits;- using func = CFPinLight<quint16, ClampAsInteger>;- op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());- }} else if (id == COMPOSITE_HARD_MIX) {const bool isFloat = cs->colorDepthId() == Float32BitsColorDepthID || cs->colorDepthId() == Float16BitsColorDepthID;const QString newId = isHDR && isFloat ? COMPOSITE_HARD_MIX_HDR : COMPOSITE_HARD_MIX;@@ -80,7 +392,12 @@ const KoCompositeOp* createOp(const KoColorSpace *cs, const QString &id, bool isop = cs->compositeOp(id);}- KIS_ASSERT(op);+#endif++ if (!op) {+ qWarning() << "WARNING: fall back to the upstream blendmode" << id;+ op = cs->compositeOp(id);+ }return op;}diff --git a/libs/ui/tool/strokes/KisMaskingBrushCompositeOp.h b/libs/ui/tool/strokes/KisMaskingBrushCompositeOp.hindex 66f788e119..e42c31b952 100644--- a/libs/ui/tool/strokes/KisMaskingBrushCompositeOp.h+++ b/libs/ui/tool/strokes/KisMaskingBrushCompositeOp.h@@ -193,8 +193,8 @@ inline T colorDodgeAlphaHelper(T src, T dst)// this case we also treat the denominator as an infinitely small number,// and the numerator can remain as 0, so dividing 0 over a number (no matter// how small it is) gives 0.- if (isUnitValue<T>(src)) {- return isZeroValue<T>(dst) ? zeroValue<T>() : unitValue<T>();+ if (isUnitValueFuzzy<T>(src)) {+ return isZeroValueFuzzy<T>(dst) ? zeroValue<T>() : unitValue<T>();}return qBound(composite_type(KoColorSpaceMathsTraits<T>::zeroValue),div(dst, inv(src)),@@ -260,8 +260,8 @@ inline T colorBurnAlphaHelper(T src, T dst)using namespace Arithmetic;// Handle the case where the denominator is 0. See color dodge for a// detailed explanation- if(isZeroValue<T>(src)) {- return isUnitValue<T>(dst) ? zeroValue<T>() : unitValue<T>();+ if(isZeroValueFuzzy<T>(src)) {+ return isUnitValueFuzzy<T>(dst) ? zeroValue<T>() : unitValue<T>();}return qBound(composite_type(KoColorSpaceMathsTraits<T>::zeroValue),div(inv(dst), src),@@ -328,7 +328,7 @@ struct CompositeFunction<channels_type, KIS_MASKING_BRUSH_COMPOSITE_LINEAR_DODGE{using composite_type = typename KoColorSpaceMathsTraits<channels_type>::compositetype;using namespace Arithmetic;- if (isZeroValue<channels_type>(dst)) {+ if (isZeroValueFuzzy<channels_type>(dst)) {return zeroValue<channels_type>();}return qMin(composite_type(src) + dst, composite_type(unitValue<channels_type>()));@@ -345,7 +345,7 @@ struct CompositeFunction<channels_type, KIS_MASKING_BRUSH_COMPOSITE_LINEAR_DODGE{using composite_type = typename KoColorSpaceMathsTraits<channels_type>::compositetype;using namespace Arithmetic;- if (isZeroValue<channels_type>(dst)) {+ if (isZeroValueFuzzy<channels_type>(dst)) {return zeroValue<channels_type>();}if constexpr (use_soft_texturing) {