Skip to content

armgit / krita

Public Imported from GitHub
0

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++

Fix numerical instabilities in blendmodes

Dmitry Kazakovc2cb48f34a6a
diff --git a/libs/pigment/tests/KoCompositeOpFunctions2.h b/libs/pigment/tests/KoCompositeOpFunctions2.h
index 5d85575e32..3e7a4ea9ff 100644
--- a/libs/pigment/tests/KoCompositeOpFunctions2.h
+++ b/libs/pigment/tests/KoCompositeOpFunctions2.h
@@ -25,10 +25,6 @@ struct ClampPolicyInteger {
using channels_type = T;
using compositetype = typename KoColorSpaceMathsTraits<T>::compositetype;
- static inline channels_type clampInputSrc(channels_type value) {
- return value;
- }
-
static inline channels_type clampResultAllowNegative(compositetype value) {
using namespace Arithmetic;
return clampToSDR<channels_type>(value);
@@ -54,11 +50,6 @@ struct ClampPolicyFloatSDR {
using channels_type = T;
using compositetype = typename KoColorSpaceMathsTraits<T>::compositetype;
- static channels_type clampInputSrc(channels_type value) {
- using namespace Arithmetic;
- return clampToSDR<channels_type>(value);
- }
-
static channels_type clampResultAllowNegative(compositetype value) {
using namespace Arithmetic;
return clampToSDR<channels_type>(value);
@@ -86,11 +77,6 @@ struct ClampPolicyFloatHDR {
using channels_type = T;
using compositetype = typename KoColorSpaceMathsTraits<T>::compositetype;
- static channels_type clampInputSrc(channels_type value) {
- using namespace Arithmetic;
- return clampToSDR<channels_type>(value);
- }
-
static channels_type clampResultAllowNegative(compositetype value) {
return value;
}
@@ -241,7 +227,27 @@ Q_REQUIRED_RESULT inline Q_DECL_UNUSED
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 */
}
@@ -447,7 +453,7 @@ struct CFColorBurn : ClampedSourceCompositeFunctorBase<T>
// Handle the case where the denominator is 0. See color dodge for a
// detailed explanation
- if(tmp::isZeroValue(src)) {
+ if (tmp::isZeroValue(src)) {
return !tmp::isUnitValueClamped(dst) ? zeroValue<T>() : clamp_policy::clampResultAllowNegative(dst);
}
return inv<T>(
@@ -536,7 +542,7 @@ inline T cfDivide(T src, T dst) {
using namespace Arithmetic;
//typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;
- if(tmp::isZeroValue(src))
+ if (tmp::isZeroValue(src))
return tmp::isZeroValue(dst) ? zeroValue<T>() : unitValue<T>();
return clamp<T>(div(dst, src));
@@ -548,6 +554,10 @@ struct CFHardLight : ClampedSourceCompositeFunctorBase<T> {
using namespace Arithmetic;
using composite_type = typename KoColorSpaceMathsTraits<T>::compositetype;
+ if (tmp::isHalfValue(src)) {
+ return dst;
+ }
+
composite_type src2 = composite_type(src) + src;
if(src > halfValue<T>()) {
@@ -803,12 +813,16 @@ struct CFGammaDark : ClampedSourceAndDestinationBottomCompositeFunctorBase<T> {
static inline T composeChannel(T src, T dst) {
using namespace Arithmetic;
- if(tmp::isZeroValue(src)) {
+ if (tmp::isZeroValue(src)) {
return zeroValue<T>();
}
+ if (tmp::isUnitValue(dst)) {
+ return unitValue<T>();
+ }
+
// power(dst, 1/src)
- return scale<T>(pow(scale<qreal>(dst), 1.0/scale<qreal>(src)));
+ return scale<T>(pow(scale<qreal>(dst), 1.0 / scale<qreal>(src)));
}
};
@@ -821,6 +835,10 @@ struct CFGammaLight : ClampedSourceAndDestinationBottomCompositeFunctorBase<T> {
return tmp::isZeroValue(src) ? unitValue<T>() : zeroValue<T>();
}
+ if (tmp::isUnitValue(dst)) {
+ return unitValue<T>();
+ }
+
return scale<T>(pow(scale<qreal>(dst), scale<qreal>(src)));
}
};
@@ -837,6 +855,11 @@ template<class T>
struct CFGeometricMean : ClampedSourceAndDestinationBottomCompositeFunctorBase<T> {
static inline T composeChannel(T src, T dst) {
using namespace Arithmetic;
+
+ if (tmp::isUnitValue(src) && tmp::isUnitValue(dst)) {
+ return unitValue<T>();
+ }
+
return scale<T>(sqrt(scale<qreal>(dst) * scale<qreal>(src)));
}
};
@@ -1347,7 +1370,7 @@ inline T cfFlatLight(T src, T dst) {
using namespace Arithmetic;
if (src == zeroValue<T>()) {
- return zeroValue<T>();
+ return zeroValue<T>();
}
return clamp<T>(cfHardMixPhotoshop(inv(src),dst)==unitValue<T>() ? cfPenumbraB(src,dst) : cfPenumbraA(src,dst));
diff --git a/libs/pigment/tests/KoCompositeOpGeneric2.h b/libs/pigment/tests/KoCompositeOpGeneric2.h
index 81180e2f45..889c2d0bfe 100644
--- a/libs/pigment/tests/KoCompositeOpGeneric2.h
+++ b/libs/pigment/tests/KoCompositeOpGeneric2.h
@@ -14,14 +14,6 @@
#include "KoCompositeOpBase.h"
-bool qFuzzyCompare(quint8 a, quint8 b) {
- return a == b;
-}
-
-bool qFuzzyCompare(quint16 a, quint16 b) {
- return a == b;
-}
-
namespace tmp2 {
template <typename T>
Q_REQUIRED_RESULT static inline Q_DECL_UNUSED bool isZeroValue(T v)
@@ -41,6 +33,12 @@ Q_REQUIRED_RESULT inline Q_DECL_UNUSED float clampChannelToSDRBottom<float>(fl
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);
+}
+
}
template <class Traits,
@@ -136,16 +134,22 @@ public:
srcAlpha = mul(srcAlpha, maskAlpha, opacity);
+ if (tmp2::isZeroValue(srcAlpha)) {
+ return dstAlpha;
+ }
+
if(alphaLocked) {
if(!tmp2::isZeroValue(dstAlpha)) {
for(qint32 i=0; i <channels_nb; i++) {
if(i != alpha_pos && (allChannelFlags || channelFlags.testBit(i))) {
const channels_type srcInBlendSpace =
- BlendingPolicy::toAdditiveSpace(
- CompositeOpFunctor::clampSourceChannelValue(src[i]));
+ CompositeOpFunctor::clampSourceChannelValue(
+ BlendingPolicy::toAdditiveSpace(
+ src[i]));
const channels_type dstInBlendSpace =
- BlendingPolicy::toAdditiveSpace(
- CompositeOpFunctor::clampDestinationChannelValue(dst[i]));
+ CompositeOpFunctor::clampDestinationChannelValue(
+ BlendingPolicy::toAdditiveSpace(
+ dst[i]));
dst[i] = BlendingPolicy::fromAdditiveSpace(
lerp(dstInBlendSpace,
@@ -156,8 +160,54 @@ public:
}
return dstAlpha;
- }
- else {
+ } else if (tmp2::isZeroValue(dstAlpha)) {
+ for(qint32 i=0; i <channels_nb; i++) {
+ if(i != alpha_pos && (allChannelFlags || channelFlags.testBit(i))) {
+ dst[i] = BlendingPolicy::fromAdditiveSpace(
+ CompositeOpFunctor::clampSourceChannelValue(
+ BlendingPolicy::toAdditiveSpace(src[i])));
+ }
+ }
+ return srcAlpha;
+ } else if (tmp2::isUnitValue(dstAlpha)) {
+ for(qint32 i=0; i <channels_nb; i++) {
+ if(i != alpha_pos && (allChannelFlags || channelFlags.testBit(i))) {
+ const channels_type srcInBlendSpace =
+ CompositeOpFunctor::clampSourceChannelValue(
+ BlendingPolicy::toAdditiveSpace(
+ src[i]));
+ const channels_type dstInBlendSpace =
+ CompositeOpFunctor::clampDestinationChannelValue(
+ BlendingPolicy::toAdditiveSpace(
+ dst[i]));
+
+ dst[i] = BlendingPolicy::fromAdditiveSpace(
+ lerp(dstInBlendSpace,
+ CompositeOpFunctor::composeChannel(srcInBlendSpace, dstInBlendSpace),
+ srcAlpha));
+ }
+ }
+ return unitValue<channels_type>();
+ } else if (tmp2::isUnitValue(srcAlpha)) {
+ for(qint32 i=0; i <channels_nb; i++) {
+ if(i != alpha_pos && (allChannelFlags || channelFlags.testBit(i))) {
+ const channels_type srcInBlendSpace =
+ CompositeOpFunctor::clampSourceChannelValue(
+ BlendingPolicy::toAdditiveSpace(
+ src[i]));
+ const channels_type dstInBlendSpace =
+ CompositeOpFunctor::clampDestinationChannelValue(
+ BlendingPolicy::toAdditiveSpace(
+ dst[i]));
+
+ dst[i] = BlendingPolicy::fromAdditiveSpace(
+ lerp(srcInBlendSpace,
+ CompositeOpFunctor::composeChannel(srcInBlendSpace, dstInBlendSpace),
+ dstAlpha));
+ }
+ }
+ return unitValue<channels_type>();
+ } else {
channels_type newDstAlpha = unionShapeOpacity(srcAlpha, dstAlpha);
if (!tmp2::isZeroValue(newDstAlpha)) {
@@ -165,32 +215,34 @@ public:
for(qint32 i=0; i <channels_nb; i++) {
if(i != alpha_pos && (allChannelFlags || channelFlags.testBit(i))) {
const channels_type srcInBlendSpace =
- BlendingPolicy::toAdditiveSpace(
- CompositeOpFunctor::clampSourceChannelValue(src[i]));
+ CompositeOpFunctor::clampSourceChannelValue(
+ BlendingPolicy::toAdditiveSpace(
+ src[i]));
const channels_type dstInBlendSpace =
- BlendingPolicy::toAdditiveSpace(
- CompositeOpFunctor::clampDestinationChannelValue(dst[i]));
+ CompositeOpFunctor::clampDestinationChannelValue(
+ BlendingPolicy::toAdditiveSpace(
+ dst[i]));
channels_type result =
blend(srcInBlendSpace, srcAlpha,
dstInBlendSpace, dstAlpha,
CompositeOpFunctor::composeChannel(srcInBlendSpace, dstInBlendSpace));
-
-
dst[i] = BlendingPolicy::fromAdditiveSpace(div(result, newDstAlpha));
- // qDebug() << i
- // << fixed << qSetRealNumberPrecision(10)
- // << ppVar(src[i])
- // << ppVar(dst[i])
- // << ppVar(compositeFunc(srcInBlendSpace, dstInBlendSpace))
- // << ppVar(result)
- // << div(result, newDstAlpha)
- // << reset
- // << ppVar(newDstAlpha)
- // << ppVar(srcAlpha)
- // << ppVar(dstAlpha);
+// qDebug() << i
+// << fixed << qSetRealNumberPrecision(10)
+// << ppVar(src[i])
+// << ppVar(dst[i])
+// << ppVar(CompositeOpFunctor::composeChannel(srcInBlendSpace, dstInBlendSpace))
+// << ppVar(result)
+// << div(result, newDstAlpha)
+// << reset
+// << ppVar(newDstAlpha)
+// << ppVar(srcAlpha)
+// << ppVar(dstAlpha)
+// << ppVar(tmp2::isUnitValue(result))
+// << ppVar(tmp2::isUnitValue(newDstAlpha));
}
}
}
diff --git a/libs/pigment/tests/TestCompositeOpInversion.cpp b/libs/pigment/tests/TestCompositeOpInversion.cpp
index f50be99976..81888f281f 100644
--- a/libs/pigment/tests/TestCompositeOpInversion.cpp
+++ b/libs/pigment/tests/TestCompositeOpInversion.cpp
@@ -844,7 +844,8 @@ void TestCompositeOpInversion::testFloatModes()
id != COMPOSITE_DESTINATION_ATOP &&
id != COMPOSITE_DESTINATION_IN &&
id != COMPOSITE_ERASE &&
- id != COMPOSITE_COPY) {
+ id != COMPOSITE_COPY &&
+ !qFuzzyIsNull(float(minOpacity))) { // we have added a fast-path for opacity == 0.0 case
KoColor resultColorU2 = dstColorU;
opU2->composite(resultColorU2.data(), 0, srcColorU.data(), 0,
@@ -855,14 +856,34 @@ void TestCompositeOpInversion::testFloatModes()
const quint16 referenceColor = getColorValueU(resultColorU2);
const quint16 resultColor = getColorValueU(resultColorU);
+ const int difference = qAbs(referenceColor - resultColor);
+ int maxDifference = 0;
+
/**
* Hard overlay was changed from float to int16 composition
*/
- const int difference = qAbs(referenceColor - resultColor);
- const int maxDifference = id != COMPOSITE_HARD_OVERLAY ? 0 :
- minOpacity > 0.99 ? 1 : 10;
+ if (id == COMPOSITE_HARD_OVERLAY) {
+ maxDifference = minOpacity > 0.99 ? 1 : 10;
+ }
+
+ /**
+ * We have changed CompositeGeneric so now there is a precise
+ * case for alpha-unity values
+ */
+ if (qFuzzyCompare(float(dstAlphaValue), 1.0f) ||
+ qFuzzyCompare(float(srcAlphaValue), 1.0f)) {
+ maxDifference = 3;
+ }
- if (difference > maxDifference) {
+ /**
+ * We have added a static point that resolves numerical
+ * instability.
+ */
+ const bool shouldSkipCheck =
+ (id == COMPOSITE_HARD_LIGHT &&
+ qFuzzyCompare(srcColorValue, 0.5));
+
+ if (!shouldSkipCheck && difference > maxDifference) {
qDebug() << "--- integer implementation is inconsistent to the original mode! ---";
qDebug() << ppVar(opacity) << ppVar(resultColor) << ppVar(referenceColor);
@@ -954,6 +975,12 @@ void TestCompositeOpInversion::testFloatModes()
opacity);
resultColorValueF = getColorValue(resultColorF);
+ if (std::isinf(resultColorValueF)) {
+ qDebug() << ppVar(i);
+ qDebug() << fixed << qSetRealNumberPrecision(16) << ppVar(srcColorValue) << ppVar(dstColorValue);
+ qDebug() << fixed << qSetRealNumberPrecision(16) << ppVar(srcAlphaValue) << ppVar(dstAlphaValue);
+ }
+
QVERIFY(!std::isnan(resultColorValueF));
QVERIFY(!std::isinf(resultColorValueF));
@@ -972,8 +999,23 @@ void TestCompositeOpInversion::testFloatModes()
const float originalError = qAbs(transitionalValues.front() - resultColorValueU);
const float finalError = qAbs(transitionalValues.back() - resultColorValueU);
+ bool skipConvergencyCheck = false;
+
+ if ((id == COMPOSITE_DARKEN ||
+ id == COMPOSITE_LIGHTEN ||
+ id == COMPOSITE_ALLANON) &&
+ qFuzzyCompare(float(srcColorValue), 1.0f) &&
+ qFuzzyCompare(float(srcColorValue), 1.0f)) {
+
+ skipConvergencyCheck = true;
+ }
+
if (checkInSdrRangeImpl(transitionalValues.back(), true) ||
- ((resultConvergedToAPoint || finalError < originalError) &&
+
+ ((resultConvergedToAPoint ||
+ finalError < originalError ||
+ skipConvergencyCheck) &&
+
qAbs(transitionalValues.back() - transitionalValues.front())
< 64 * std::numeric_limits<float>::epsilon())) {
@@ -1201,7 +1243,7 @@ void TestCompositeOpInversion::testFloatModes_data()
// doesn't clamp result, always preserves SDR state
- QTest::addRow("%s_%s", COMPOSITE_HARD_LIGHT.toLatin1().data(), "sdr") << COMPOSITE_HARD_LIGHT << TestFlags(SrcCannotMakeNegative | PreservesSdrRange);
+ QTest::addRow("%s_%s", COMPOSITE_HARD_LIGHT.toLatin1().data(), "sdr") << COMPOSITE_HARD_LIGHT << TestFlags(SrcCannotMakeNegative | PreservesStrictSdrRange);
QTest::addRow("%s_%s", COMPOSITE_PARALLEL.toLatin1().data(), "sdr") << COMPOSITE_PARALLEL << TestFlags(SrcCannotMakeNegative | PreservesSdrRange);
QTest::addRow("%s_%s", COMPOSITE_EQUIVALENCE.toLatin1().data(), "sdr") << COMPOSITE_EQUIVALENCE << TestFlags(SrcCannotMakeNegative | PreservesStrictSdrRange);
QTest::addRow("%s_%s", COMPOSITE_GEOMETRIC_MEAN.toLatin1().data(), "sdr") << COMPOSITE_GEOMETRIC_MEAN << TestFlags(SrcCannotMakeNegative | PreservesSdrRange);