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

Add basic unittest that verifies blendmode properties in HDR mode

Dmitry Kazakovedcf50d5f53e
diff --git a/libs/pigment/KoColorSpaceMaths.h b/libs/pigment/KoColorSpaceMaths.h
index e78edde9dd..9891c22c94 100644
--- a/libs/pigment/KoColorSpaceMaths.h
+++ b/libs/pigment/KoColorSpaceMaths.h
@@ -273,6 +273,23 @@ public:
return qBound<dst_compositetype>(KoColorSpaceMathsTraits<_Tdst>::min, val, KoColorSpaceMathsTraits<_Tdst>::max);
}
+ inline static _T clampChannelToSDR(_T val) {
+ // integer version, float versions are specialized separately
+ return val;
+ }
+
+ inline static dst_compositetype clampToSDR(dst_compositetype val) {
+ return qBound<dst_compositetype>(KoColorSpaceMathsTraits<_Tdst>::zeroValue, val, KoColorSpaceMathsTraits<_Tdst>::unitValue);
+ }
+
+ inline static dst_compositetype clampToSDRTop(dst_compositetype val) {
+ return qMin<dst_compositetype>(val, KoColorSpaceMathsTraits<_Tdst>::unitValue);
+ }
+
+ inline static dst_compositetype clampToSDRBottom(dst_compositetype val) {
+ return qMax<dst_compositetype>(KoColorSpaceMathsTraits<_Tdst>::zeroValue, val);
+ }
+
/**
* Clamps the composite type on higher border only. That is a fast path
* for scale-only transformations
@@ -284,6 +301,14 @@ public:
inline static _T isUnsafeAsDivisor(_T value) {
return value == KoColorSpaceMathsTraits<_T>::zeroValue;
}
+
+ inline static bool isUnitValue(_T value) {
+ return value == KoColorSpaceMathsTraits<_T>::unitValue;
+ }
+
+ inline static bool isZeroValue(_T value) {
+ return value == KoColorSpaceMathsTraits<_T>::zeroValue;
+ }
};
//------------------------------ double specialization ------------------------------//
@@ -381,6 +406,21 @@ inline float KoColorSpaceMaths<float>::isUnsafeAsDivisor(float value) {
return value < 1e-6; // negative values are also unsafe!
}
+template<>
+inline bool KoColorSpaceMaths<float>::isUnitValue(float value) {
+ return qFuzzyCompare(value, KoColorSpaceMathsTraits<float>::unitValue);
+}
+
+template<>
+inline bool KoColorSpaceMaths<float>::isZeroValue(float value) {
+ return qFuzzyIsNull(value);
+}
+
+template<>
+inline float KoColorSpaceMaths<float>::clampChannelToSDR(float val) {
+ return qBound<float>(KoColorSpaceMathsTraits<float>::zeroValue, val, KoColorSpaceMathsTraits<float>::unitValue);
+}
+
//------------------------------ half specialization ------------------------------//
#ifdef HAVE_OPENEXR
@@ -457,6 +497,12 @@ inline half KoColorSpaceMaths<half>::isUnsafeAsDivisor(half value) {
return value < 1e-6; // negative values are also unsafe!
}
+template<>
+inline half KoColorSpaceMaths<half>::clampChannelToSDR(half val) {
+ return qBound<half>(KoColorSpaceMathsTraits<half>::zeroValue, val, KoColorSpaceMathsTraits<half>::unitValue);
+}
+
+
#endif
//------------------------------ quint8 specialization ------------------------------//
@@ -604,6 +650,26 @@ namespace Arithmetic
inline T clamp(typename KoColorSpaceMathsTraits<T>::compositetype a) {
return KoColorSpaceMaths<T>::clamp(a);
}
+
+ template<class T>
+ inline T clampToSDR(typename KoColorSpaceMathsTraits<T>::compositetype a) {
+ return KoColorSpaceMaths<T>::clampToSDR(a);
+ }
+
+ template<class T>
+ inline T clampChannelToSDR(T a) {
+ return KoColorSpaceMaths<T>::clampChannelToSDR(a);
+ }
+
+ template<class T>
+ inline T clampToSDRTop(typename KoColorSpaceMathsTraits<T>::compositetype a) {
+ return KoColorSpaceMaths<T>::clampToSDRTop(a);
+ }
+
+ template<class T>
+ inline T clampToSDRBottom(typename KoColorSpaceMathsTraits<T>::compositetype a) {
+ return KoColorSpaceMaths<T>::clampToSDRBottom(a);
+ }
template<class T>
inline T min(T a, T b, T c) {
diff --git a/libs/pigment/compositeops/KoCompositeOpBase.h b/libs/pigment/compositeops/KoCompositeOpBase.h
index fb2a31237f..4e1f9061b9 100644
--- a/libs/pigment/compositeops/KoCompositeOpBase.h
+++ b/libs/pigment/compositeops/KoCompositeOpBase.h
@@ -8,7 +8,7 @@
#define KOCOMPOSITEOP_BASE_H_
#include <KoCompositeOp.h>
-#include "KoCompositeOpFunctions.h"
+#include <KoColorSpaceMaths.h>
/**
* A template base class that can be used for most composite modes/ops
diff --git a/libs/pigment/compositeops/KoCompositeOpFunctions.h b/libs/pigment/compositeops/KoCompositeOpFunctions.h
index 56e67cbf34..2245901118 100644
--- a/libs/pigment/compositeops/KoCompositeOpFunctions.h
+++ b/libs/pigment/compositeops/KoCompositeOpFunctions.h
@@ -213,9 +213,10 @@ inline T colorBurnHelper(T src, T dst) {
// Handle the case where the denominator is 0. See color dodge for a
// detailed explanation
if(src == zeroValue<T>()) {
- return dst == unitValue<T>() ? zeroValue<T>() : KoColorSpaceMathsTraits<T>::max;
+ return dst == unitValue<T>() ? inv(dst) : unitValue<T>();
}
- return clamp<T>(div(inv(dst), src));
+
+ return clampToSDR<T>(div(inv(dst), src));
}
// Integer version of color burn
@@ -234,14 +235,14 @@ cfColorBurn(T src, T dst) {
const T result = colorBurnHelper(src, dst);
// Constantly dividing by small numbers can quickly make the result
// become infinity or NaN, so we check that and correct (kind of clamping)
- return inv(std::isfinite(result) ? result : KoColorSpaceMathsTraits<T>::max);
+ return inv(std::isfinite(result) ? result : KoColorSpaceMathsTraits<T>::unitValue);
}
template<class T>
inline T cfLinearBurn(T src, T dst) {
using namespace Arithmetic;
typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;
- return clamp<T>(composite_type(src) + dst - unitValue<T>());
+ return clampToSDR<T>(composite_type(src) + dst - unitValue<T>());
}
template<class T>
@@ -257,10 +258,10 @@ inline T colorDodgeHelper(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 (src == unitValue<T>()) {
- return dst == zeroValue<T>() ? zeroValue<T>() : KoColorSpaceMathsTraits<T>::max;
+ if (KoColorSpaceMaths<T>::isUnitValue(src)) {
+ return KoColorSpaceMaths<T>::isZeroValue(dst) ? zeroValue<T>() : KoColorSpaceMathsTraits<T>::unitValue;
}
- return Arithmetic::clamp<T>(div(dst, inv(src)));
+ return Arithmetic::clampToSDR<T>(div(dst, inv(src)));
}
// Integer version of color dodge
@@ -277,7 +278,7 @@ cfColorDodge(T src, T dst) {
const T result = colorDodgeHelper(src, dst);
// Constantly dividing by small numbers can quickly make the result
// become infinity or NaN, so we check that and correct (kind of clamping)
- return std::isfinite(result) ? result : KoColorSpaceMathsTraits<T>::max;
+ return std::isfinite(result) ? result : KoColorSpaceMathsTraits<T>::unitValue;
}
template<class T>
diff --git a/libs/pigment/compositeops/KoCompositeOpGeneric.h b/libs/pigment/compositeops/KoCompositeOpGeneric.h
index 0dbb10cf9c..00fcd5aec8 100644
--- a/libs/pigment/compositeops/KoCompositeOpGeneric.h
+++ b/libs/pigment/compositeops/KoCompositeOpGeneric.h
@@ -7,7 +7,7 @@
#ifndef _KOCOMPOSITEOP_GENERIC_H_
#define _KOCOMPOSITEOP_GENERIC_H_
-#include "KoCompositeOpFunctions.h"
+//#include "KoCompositeOpFunctions.h"
#include "KoCompositeOpBase.h"
/**
@@ -74,7 +74,12 @@ public:
blend(srcInBlendSpace, srcAlpha,
dstInBlendSpace, dstAlpha,
compositeFunc(srcInBlendSpace, dstInBlendSpace));
+
+
+
dst[i] = BlendingPolicy::fromAdditiveSpace(div(result, newDstAlpha));
+
+ //qDebug() << i << ppVar(result) << ppVar(compositeFunc(srcInBlendSpace, dstInBlendSpace)) << div(result, newDstAlpha);
}
}
}
diff --git a/libs/pigment/compositeops/KoCompositeOpGreater.h b/libs/pigment/compositeops/KoCompositeOpGreater.h
index c711fe0a48..cc0f1c4bc1 100644
--- a/libs/pigment/compositeops/KoCompositeOpGreater.h
+++ b/libs/pigment/compositeops/KoCompositeOpGreater.h
@@ -22,6 +22,21 @@
#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);
+}
+
+}
+
/**
* Greater-than compositor - uses the greater of two alpha values to determine the color
*/
@@ -47,10 +62,10 @@ public:
const QBitArray& channelFlags ) {
using namespace Arithmetic;
- if (dstAlpha == unitValue<channels_type>()) return dstAlpha;
+ if (tmp3::isUnitValue(dstAlpha)) return dstAlpha;
channels_type appliedAlpha = mul(maskAlpha, srcAlpha, opacity);
- if (appliedAlpha == zeroValue<channels_type>()) return dstAlpha;
+ if (tmp3::isZeroValue(appliedAlpha)) return dstAlpha;
channels_type newDstAlpha;
float dA = scale<float>(dstAlpha);
@@ -70,8 +85,10 @@ public:
if (a<dA) a = dA;
float fakeOpacity = 1.0f - (1.0f - a)/(1.0f - dA + 1e-16f);
newDstAlpha=scale<channels_type>(a);
-
- if (dstAlpha != zeroValue<channels_type>()) {
+
+ if (tmp3::isZeroValue(newDstAlpha)) {
+ // just do nothing with color channels and return null opacity
+ } else if (!tmp3::isZeroValue(dstAlpha)) {
for (qint8 channel = 0; channel < channels_nb; ++channel)
if(channel != alpha_pos && (allChannelFlags || channelFlags.testBit(channel)))
{
@@ -80,9 +97,7 @@ public:
channels_type dstMult = mul(BlendingPolicy::toAdditiveSpace(dst[channel]), dstAlpha);
channels_type srcMult = mul(BlendingPolicy::toAdditiveSpace(src[channel]), unitValue<channels_type>());
channels_type blendedValue = lerp(dstMult, srcMult, scale<channels_type>(fakeOpacity));
- // CID 249016 (#1 of 15):
- // Division or modulo by zero (DIVIDE_BY_ZERO)12. divide_by_zero: In function call divide, division by expression newDstAlpha which may be zero has undefined behavior.
- if (newDstAlpha == 0) newDstAlpha = 1;
+
composite_type normedValue = KoColorSpaceMaths<channels_type>::divide(blendedValue, newDstAlpha);
dst[channel] = BlendingPolicy::fromAdditiveSpace(KoColorSpaceMaths<channels_type>::clampAfterScale(normedValue));
diff --git a/libs/pigment/compositeops/KoCompositeOps.h b/libs/pigment/compositeops/KoCompositeOps.h
index 5204fe2104..11bd934cab 100644
--- a/libs/pigment/compositeops/KoCompositeOps.h
+++ b/libs/pigment/compositeops/KoCompositeOps.h
@@ -13,6 +13,7 @@
#include <KoColorSpace.h>
#include <KoColorSpaceTraits.h>
#include <KoColorSpaceMaths.h>
+#include <KoCompositeOpFunctions.h>
#include "compositeops/KoCompositeOpGeneric.h"
#include "compositeops/KoCompositeOpOver.h"
diff --git a/libs/pigment/tests/KoCompositeOpFunctions2.h b/libs/pigment/tests/KoCompositeOpFunctions2.h
new file mode 100644
index 0000000000..5d85575e32
--- /dev/null
+++ b/libs/pigment/tests/KoCompositeOpFunctions2.h
@@ -0,0 +1,1371 @@
+/*
+ * SPDX-FileCopyrightText: 2011 Silvio Heinrich <[email protected]>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+*/
+
+#ifdef KOCOMPOSITEOP_FUNCTIONS_H_
+#error "ssksjdkjs"
+#endif
+
+#ifndef KOCOMPOSITEOP_FUNCTIONS_H_
+#define KOCOMPOSITEOP_FUNCTIONS_H_
+
+#include <KoColorSpaceMaths.h>
+
+#include <type_traits>
+#include <cmath>
+
+#ifdef HAVE_OPENEXR
+#include "half.h"
+#endif
+
+template <typename T>
+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);
+ }
+
+ static inline channels_type clampResult(compositetype value) {
+ using namespace Arithmetic;
+ return clampToSDR<channels_type>(value);
+ }
+
+ static inline channels_type clampInvertedResult(compositetype value) {
+ using namespace Arithmetic;
+ return clampToSDR<channels_type>(value);
+ }
+
+ static inline compositetype fixInfiniteAfterDivision(compositetype value) {
+ return value;
+ }
+};
+
+template <typename T>
+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);
+ }
+
+ static inline channels_type clampResult(compositetype value) {
+ using namespace Arithmetic;
+ return clampToSDR<channels_type>(value);
+ }
+
+ static channels_type clampInvertedResult(compositetype value) {
+ using namespace Arithmetic;
+ return clampToSDR<channels_type>(value);
+ }
+
+ static compositetype fixInfiniteAfterDivision(compositetype value) {
+ // Constantly dividing by small numbers can quickly make the result
+ // become infinity or NaN, so we check that and correct (kind of clamping)
+ return std::isfinite(value) ? value : KoColorSpaceMathsTraits<T>::unitValue;
+ }
+};
+
+template <typename T>
+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;
+ }
+
+ static inline channels_type clampResult(compositetype value) {
+ using namespace Arithmetic;
+ return clampToSDRBottom<channels_type>(value);
+ }
+
+ static channels_type clampInvertedResult(compositetype value) {
+ using namespace Arithmetic;
+ return clampToSDRTop<channels_type>(value);
+ }
+
+ static compositetype fixInfiniteAfterDivision(compositetype value) {
+ // Constantly dividing by small numbers can quickly make the result
+ // become infinity or NaN, so we check that and correct (kind of clamping)
+ return std::isfinite(value) ? value : KoColorSpaceMathsTraits<T>::max;
+ }
+};
+
+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<class HSXType, class TReal>
+inline void cfReorientedNormalMapCombine(TReal srcR, TReal srcG, TReal srcB, TReal& dstR, TReal& dstG, TReal& dstB)
+{
+ // see https://blog.selfshadow.com/publications/blending-in-detail/ by Barre-Brisebois and Hill
+ TReal tx = 2*srcR-1;
+ TReal ty = 2*srcG-1;
+ TReal tz = 2*srcB;
+ TReal ux = -2*dstR+1;
+ TReal uy = -2*dstG+1;
+ TReal uz = 2*dstB-1;
+ TReal k = (tx*ux+ty*uy+tz*uz)/tz; // dot(t,u)/t.z
+ TReal rx = tx*k-ux;
+ TReal ry = ty*k-uy;
+ TReal rz = tz*k-uz;
+ k = 1/sqrt(rx*rx+ry*ry+rz*rz); // normalize result
+ rx *= k;
+ ry *= k;
+ rz *= k;
+ dstR = rx*0.5+0.5;
+ dstG = ry*0.5+0.5;
+ dstB = rz*0.5+0.5;
+}
+
+template<class HSXType, class TReal>
+inline void cfColor(TReal sr, TReal sg, TReal sb, TReal& dr, TReal& dg, TReal& db) {
+ TReal lum = getLightness<HSXType>(dr, dg, db);
+ dr = sr;
+ dg = sg;
+ db = sb;
+ setLightness<HSXType>(dr, dg, db, lum);
+}
+
+template<class HSXType, class TReal>
+inline void cfLambertLighting(TReal sr, TReal sg, TReal sb, TReal& dr, TReal& dg, TReal& db)
+{
+ TReal tr = sr * dr * (1.0 / 0.215686);
+ TReal tg = sg * dg * (1.0 / 0.215686);
+ TReal tb = sb * db * (1.0 / 0.215686);
+
+ if (tr > 1.0) {
+ dr = 1.0 + (tr - 1.0) * (tr - 1.0) * 0.01925;
+ }
+ else {
+ dr = tr;
+ }
+
+ if (tg > 1.0) {
+ dg = 1.0 + (tg - 1.0) * (tg - 1.0) * 0.01925;
+ }
+ else {
+ dg = tg;
+ }
+
+ if (tb > 1.0) {
+ db = 1.0 + (tb - 1.0) * (tb - 1.0) * 0.01925;
+ }
+ else {
+ db = tb;
+ }
+
+
+ ToneMapping<HSXType, TReal>(dr, dg, db);
+}
+
+template<class HSXType, class TReal>
+inline void cfLambertLightingGamma2_2(TReal sr, TReal sg, TReal sb, TReal& dr, TReal& dg, TReal& db) {
+ TReal tr = sr * dr * 2.0;
+ TReal tg = sg * dg * 2.0;
+ TReal tb = sb * db * 2.0;
+
+ if (tr > 1.0) {
+ dr = 1.0 + (tr - 1.0) * (tr - 1.0) * 0.4;
+ }
+ else {
+ dr = tr;
+ }
+
+ if (tg > 1.0) {
+ dg = 1.0 + (tg - 1.0) * (tg - 1.0) * 0.4;
+ }
+ else {
+ dg = tg;
+ }
+
+ if (tb > 1.0) {
+ db = 1.0 + (tb - 1.0) * (tb - 1.0) * 0.4;
+ }
+ else {
+ db = tb;
+ }
+
+ ToneMapping<HSXType, TReal>(dr, dg, db);
+}
+
+template<class HSXType, class TReal>
+inline void cfLightness(TReal sr, TReal sg, TReal sb, TReal& dr, TReal& dg, TReal& db) {
+ setLightness<HSXType>(dr, dg, db, getLightness<HSXType>(sr, sg, sb));
+}
+
+template<class HSXType, class TReal>
+inline void cfIncreaseLightness(TReal sr, TReal sg, TReal sb, TReal& dr, TReal& dg, TReal& db) {
+ addLightness<HSXType>(dr, dg, db, getLightness<HSXType>(sr, sg, sb));
+}
+
+template<class HSXType, class TReal>
+inline void cfDecreaseLightness(TReal sr, TReal sg, TReal sb, TReal& dr, TReal& dg, TReal& db) {
+ addLightness<HSXType>(dr, dg, db, getLightness<HSXType>(sr, sg, sb) - TReal(1.0));
+}
+
+template<class HSXType, class TReal>
+inline void cfSaturation(TReal sr, TReal sg, TReal sb, TReal& dr, TReal& dg, TReal& db) {
+ TReal sat = getSaturation<HSXType>(sr, sg, sb);
+ TReal light = getLightness<HSXType>(dr, dg, db);
+ setSaturation<HSXType>(dr, dg, db, sat);
+ setLightness<HSXType>(dr, dg, db, light);
+}
+
+template<class HSXType, class TReal>
+inline void cfIncreaseSaturation(TReal sr, TReal sg, TReal sb, TReal& dr, TReal& dg, TReal& db) {
+ using namespace Arithmetic;
+ TReal sat = lerp(getSaturation<HSXType>(dr,dg,db), unitValue<TReal>(), getSaturation<HSXType>(sr,sg,sb));
+ TReal light = getLightness<HSXType>(dr, dg, db);
+ setSaturation<HSXType>(dr, dg, db, sat);
+ setLightness<HSXType>(dr, dg, db, light);
+}
+
+template<class HSXType, class TReal>
+inline void cfDecreaseSaturation(TReal sr, TReal sg, TReal sb, TReal& dr, TReal& dg, TReal& db) {
+ using namespace Arithmetic;
+ TReal sat = lerp(zeroValue<TReal>(), getSaturation<HSXType>(dr,dg,db), getSaturation<HSXType>(sr,sg,sb));
+ TReal light = getLightness<HSXType>(dr, dg, db);
+ setSaturation<HSXType>(dr, dg, db, sat);
+ setLightness<HSXType>(dr, dg, db, light);
+}
+
+template<class HSXType, class TReal>
+inline void cfHue(TReal sr, TReal sg, TReal sb, TReal& dr, TReal& dg, TReal& db) {
+ TReal sat = getSaturation<HSXType>(dr, dg, db);
+ TReal lum = getLightness<HSXType>(dr, dg, db);
+ dr = sr;
+ dg = sg;
+ db = sb;
+ setSaturation<HSXType>(dr, dg, db, sat);
+ setLightness<HSXType>(dr, dg, db, lum);
+}
+
+template<class HSXType, class TReal>
+inline void cfTangentNormalmap(TReal sr, TReal sg, TReal sb, TReal& dr, TReal& dg, TReal& db) {
+ using namespace Arithmetic;
+ TReal half=halfValue<TReal>();
+
+ dr = sr+(dr-half);
+ dg = sg+(dg-half);
+ db = sb+(db-unitValue<TReal>());
+}
+
+template<class HSXType, class TReal>
+inline void cfDarkerColor(TReal sr, TReal sg, TReal sb, TReal& dr, TReal& dg, TReal& db) {
+
+ TReal lum = getLightness<HSXType>(dr, dg, db);
+ TReal lum2 = getLightness<HSXType>(sr, sg, sb);
+ if (lum<lum2) {
+ sr = dr;
+ sg = dg;
+ sb = db;
+ }
+ else {
+ dr = sr;
+ dg = sg;
+ db = sb;
+ }
+
+}
+
+template<class HSXType, class TReal>
+inline void cfLighterColor(TReal sr, TReal sg, TReal sb, TReal& dr, TReal& dg, TReal& db) {
+
+ TReal lum = getLightness<HSXType>(dr, dg, db);
+ TReal lum2 = getLightness<HSXType>(sr, sg, sb);
+ if (lum>lum2) {
+ sr = dr;
+ sg = dg;
+ sb = db;
+ }
+ else {
+ dr = sr;
+ dg = sg;
+ db = sb;
+ }
+}
+
+template<class T,
+ template <typename> typename ClampPolicy>
+struct CFColorBurn : ClampedSourceCompositeFunctorBase<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(tmp::isZeroValue(src)) {
+ return !tmp::isUnitValueClamped(dst) ? zeroValue<T>() : clamp_policy::clampResultAllowNegative(dst);
+ }
+ return inv<T>(
+ clamp_policy::clampInvertedResult(
+ clamp_policy::fixInfiniteAfterDivision(
+ div(inv(dst), src)
+ )
+ )
+ );
+ }
+};
+
+
+template<class T,
+ template <typename> typename ClampPolicy>
+struct CFLinearBurn : ClampedSourceCompositeFunctorBase<T> {
+ static inline T composeChannel(T src, T dst) {
+ using namespace Arithmetic;
+ using clamp_policy = ClampPolicy<T>;
+ using composite_type = typename KoColorSpaceMathsTraits<T>::compositetype;
+
+ return clamp_policy::clampResult(composite_type(src) + dst - unitValue<T>());
+ }
+};
+
+template<typename T,
+ template <typename> typename ClampPolicy>
+struct CFColorDodge : ClampedSourceCompositeFunctorBase<T> {
+ static inline T composeChannel(T src, T dst) {
+ using namespace Arithmetic;
+ using clamp_policy = ClampPolicy<T>;
+
+ // Handle the case where the denominator is 0.
+ // When src is 1 then the denominator (1 - src) becomes 0, and to avoid
+ // dividing by 0 we treat the denominator as an infinitely small number,
+ // so the result of the formula would approach infinity. As in the generic
+ // case, that result is clamped to the maximum value (which for integer
+ // types is the same as the unit value).
+ // Another special case is when both numerator and denominator are 0. In
+ // 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 tmp::isNullValueClamped(dst) ? zeroValue<T>() : KoColorSpaceMathsTraits<T>::unitValue;
+ }
+
+ return clamp_policy::clampResultAllowNegative(
+ clamp_policy::fixInfiniteAfterDivision(
+ div(dst, inv(src))
+ )
+ );
+ }
+};
+
+template<class T>
+inline T cfAddition(T src, T dst) {
+ typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;
+ return Arithmetic::clamp<T>(composite_type(src) + dst);
+}
+
+template<class T>
+inline T cfSubtract(T src, T dst) {
+ using namespace Arithmetic;
+ typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;
+ return clamp<T>(composite_type(dst) - src);
+}
+
+template<class T>
+inline T cfInverseSubtract(T src, T dst) {
+ using namespace Arithmetic;
+ typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;
+ return clamp<T>(composite_type(dst) - inv(src));
+}
+
+template<class T>
+inline T cfExclusion(T src, T dst) {
+ using namespace Arithmetic;
+ typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;
+
+ composite_type x = mul(src, dst);
+ return clamp<T>(composite_type(dst) + src - (x + x));
+}
+
+template<class T>
+inline T cfDivide(T src, T dst) {
+ using namespace Arithmetic;
+ //typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;
+
+ if(tmp::isZeroValue(src))
+ return tmp::isZeroValue(dst) ? zeroValue<T>() : unitValue<T>();
+
+ return clamp<T>(div(dst, src));
+}
+
+template<class T>
+struct CFHardLight : ClampedSourceCompositeFunctorBase<T> {
+ static inline T composeChannel(T src, T dst) {
+ using namespace Arithmetic;
+ using composite_type = typename KoColorSpaceMathsTraits<T>::compositetype;
+
+ composite_type src2 = composite_type(src) + src;
+
+ if(src > halfValue<T>()) {
+ // screen(src*2.0 - 1.0, dst)
+ src2 -= unitValue<T>();
+
+ // src2 is guaranteed to be smaller than unitValue<T>() now
+ return Arithmetic::unionShapeOpacity(T(src2), dst);
+ }
+
+ // src2 is guaranteed to be smaller than unitValue<T>() due to 'if'
+ return Arithmetic::mul(T(src2), dst);
+ }
+};
+
+template<class T>
+struct CFSoftLightSvg : ClampedSourceCompositeFunctorBase<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);
+
+ if(fsrc > 0.5) {
+ qreal D = (fdst > 0.25) ? sqrt(fdst) : ((16.0*fdst - 12.0)*fdst + 4.0)*fdst;
+ return scale<T>(fdst + (2.0*fsrc - 1.0) * (D - fdst));
+ }
+
+ return scale<T>(fdst - (1.0 - 2.0 * fsrc) * fdst * (1.0 - fdst));
+ }
+};
+
+
+template<class T>
+struct CFSoftLight : ClampedSourceCompositeFunctorBase<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);
+
+ if(fsrc > 0.5) {
+ // lerp(dst, sqrt(dst), (2.0 * fsrc - 1.0))
+ return scale<T>(fdst + (2.0 * fsrc - 1.0) * (sqrt(fdst) - fdst));
+ }
+
+ // lerp(dst, dst^2, (1.0 - 2.0 * fsrc))
+ return scale<T>(fdst - (1.0 - 2.0*fsrc) * fdst * (1.0 - fdst));
+ }
+};
+
+template<class T,
+ template <typename> typename ClampPolicy>
+struct CFVividLight : ClampedSourceCompositeFunctorBase<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 (src < halfValue<T>()) {
+ if (tmp::isZeroValue(src)) {
+ return tmp::isUnitValueClamped(dst) ? clamp_policy::clampResultAllowNegative(dst) : zeroValue<T>();
+ }
+
+ // min(1,max(0,1-(1-dst) / (2*src)))
+ composite_type src2 = composite_type(src) + src;
+ composite_type dsti = inv(dst);
+ return clamp_policy::clampResult(
+ unitValue<T>() -
+ clamp_policy::fixInfiniteAfterDivision(
+ tmp::divideInCompositeSpace<T>(dsti, src2)
+ )
+ );
+ }
+
+ if (tmp::isUnitValue(src)) {
+ return tmp::isNullValueClamped(dst) ? zeroValue<T>() : unitValue<T>();
+ }
+
+ // min(1,max(0, dst / (2*(1-src)))
+ composite_type srci2 = inv(src);
+ srci2 += srci2;
+ return clamp_policy::clampResultAllowNegative(
+ clamp_policy::fixInfiniteAfterDivision(
+ tmp::divideInCompositeSpace<T>(composite_type(dst), srci2)
+ )
+ );
+ }
+};
+
+template<class T,
+ template <typename> typename ClampPolicy>
+struct CFPinLight : ClampedSourceCompositeFunctorBase<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)
+ // max(0, max(2*src-1, min(dst, 2*src)))
+ 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);
+ }
+};
+
+template<class T>
+inline T cfArcTangent(T src, T dst) {
+ using namespace Arithmetic;
+
+ if (tmp::isZeroValue(dst))
+ return tmp::isZeroValue(src) ? zeroValue<T>() : unitValue<T>();
+
+ return scale<T>(2.0 * atan(scale<qreal>(src) / scale<qreal>(dst)) / Arithmetic::pi);
+}
+
+template<class T>
+inline T cfAllanon(T src, T dst) {
+ using namespace Arithmetic;
+ typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;
+ // (dst + src) / 2 [or (dst + src) * 0.5]
+ return T((composite_type(src) + dst) * halfValue<T>() / unitValue<T>());
+}
+
+template<class T>
+inline T cfLinearLight(T src, T dst) {
+ using namespace Arithmetic;
+ typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;
+
+ // min(1,max(0,(dst + 2*src)-1))
+ return clamp<T>((composite_type(src) + src + dst) - unitValue<T>());
+}
+
+template<class T>
+struct CFParallel : ClampedSourceAndDestinationCompositeFunctorBase<T> {
+ static inline T composeChannel(T src, T dst) {
+ using namespace Arithmetic;
+ typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;
+
+ const bool srcIsSafe = !isUnsafeAsDivisor(src);
+ const bool dstIsSafe = !isUnsafeAsDivisor(dst);
+
+ if (!srcIsSafe || !dstIsSafe) {
+ return zeroValue<T>();
+ }
+
+ // min(max(2 / (1/dst + 1/src), 0), 1)
+ composite_type unit = unitValue<T>();
+ composite_type s = div<T>(unit, src);
+ composite_type d = div<T>(unit, dst);
+
+ return clamp<T>((unit+unit) * unit / (d+s));
+ }
+};
+
+template<class T>
+struct CFEquivalence : ClampedSourceAndDestinationBottomCompositeFunctorBase<T> {
+ static inline T composeChannel(T src, T dst) {
+ using namespace Arithmetic;
+ typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;
+ // TODO: is the formula correct?
+ // 1 - abs(dst - src)
+ composite_type x = qAbs(composite_type(dst) - src);
+ return clamp<T>(x);
+ }
+};
+
+template<class T>
+inline T cfGrainMerge(T src, T dst) {
+ using namespace Arithmetic;
+ typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;
+ return clamp<T>(composite_type(dst) + src - halfValue<T>());
+}
+
+template<class T>
+inline T cfGrainExtract(T src, T dst) {
+ using namespace Arithmetic;
+ typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;
+ return clamp<T>(composite_type(dst) - src + halfValue<T>());
+}
+
+template<class T,
+ template <typename> typename ClampPolicy>
+struct CFHardMix : ClampedSourceCompositeFunctorBase<T> {
+ static inline T composeChannel(T src, T dst) {
+ using namespace Arithmetic;
+
+ /**
+ * We do not clamp dst here, because it is supposed to be modified by
+ * cfColorDodge if it is too bright. The result will be clamped in the
+ * subordinate function.
+ */
+ return dst > halfValue<T>() ?
+ CFColorDodge<T, ClampPolicy>::composeChannel(src,dst) :
+ CFColorBurn<T, ClampPolicy>::composeChannel(src,dst);
+ }
+};
+
+template<class T>
+inline T cfHardMixPhotoshop(T src, T dst) {
+ using namespace Arithmetic;
+ typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;
+
+ const composite_type sum = composite_type(src) + dst;
+
+ return sum > unitValue<T>() ? unitValue<T>() : zeroValue<T>();
+}
+
+// Approximation of the hard mix mode used by photoshop in the brush texturing
+// In contrast to the normal hard mix, this produces antialiased edges, better
+// for texturing the brush dab, at least visually
+template<class T>
+inline T cfHardMixSofterPhotoshop(T src, T dst) {
+ using namespace Arithmetic;
+ typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;
+
+ const composite_type srcScaleFactor = static_cast<composite_type>(2);
+ const composite_type dstScaleFactor = static_cast<composite_type>(3);
+
+ return clamp<T>(dstScaleFactor * dst - srcScaleFactor * inv(src));
+}
+
+template<class T>
+struct CFAdditiveSubtractive : ClampedSourceAndDestinationBottomCompositeFunctorBase<T> {
+ static inline T composeChannel(T src, T dst) {
+ using namespace Arithmetic;
+
+ // min(1,max(0,abs(sqr(CB)-sqr(CT))))
+ qreal x = sqrt(scale<qreal>(dst)) - sqrt(scale<qreal>(src));
+ return scale<T>(qAbs(x));
+ }
+};
+
+template<class T>
+struct CFGammaDark : ClampedSourceAndDestinationBottomCompositeFunctorBase<T> {
+ static inline T composeChannel(T src, T dst) {
+ using namespace Arithmetic;
+
+ if(tmp::isZeroValue(src)) {
+ return zeroValue<T>();
+ }
+
+ // power(dst, 1/src)
+ return scale<T>(pow(scale<qreal>(dst), 1.0/scale<qreal>(src)));
+ }
+};
+
+template<class T>
+struct CFGammaLight : ClampedSourceAndDestinationBottomCompositeFunctorBase<T> {
+ static inline T composeChannel(T src, T dst) {
+ using namespace Arithmetic;
+
+ if (tmp::isZeroValue(dst)) {
+ return tmp::isZeroValue(src) ? unitValue<T>() : zeroValue<T>();
+ }
+
+ return scale<T>(pow(scale<qreal>(dst), scale<qreal>(src)));
+ }
+};
+
+template<class T>
+struct CFGammaIllumination : ClampedSourceAndDestinationCompositeFunctorBase<T> {
+ static inline T composeChannel(T src, T dst) {
+ using namespace Arithmetic;
+ return inv(CFGammaDark<T>::composeChannel(inv(src),inv(dst)));
+ }
+};
+
+template<class T>
+struct CFGeometricMean : ClampedSourceAndDestinationBottomCompositeFunctorBase<T> {
+ static inline T composeChannel(T src, T dst) {
+ using namespace Arithmetic;
+ return scale<T>(sqrt(scale<qreal>(dst) * scale<qreal>(src)));
+ }
+};
+
+template<class T>
+inline T cfOver(T src, T dst) { Q_UNUSED(dst); return src; }
+
+template<class T>
+inline T cfOverlay(T src, T dst) { return cfHardLight(dst, src); }
+
+template<class T>
+inline T cfMultiply(T src, T dst) { return Arithmetic::mul(src, dst); }
+
+template<class T,
+ template <typename> typename ClampPolicy>
+struct CFHardOverlay : ClampedSourceCompositeFunctorBase<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 (tmp::isUnitValue(src)) {
+ return unitValue<T>();
+ }
+
+ if(src >= halfValue<T>()) {
+ return clamp_policy::clampResultAllowNegative(
+ clamp_policy::fixInfiniteAfterDivision(
+ tmp::divideInCompositeSpace<T>(composite_type(dst),
+ 2 * composite_type(inv(src)))
+ )
+ );
+ }
+
+ return clamp_policy::clampResultAllowNegative(
+ tmp::multiplyInCompositeSpace<T>(composite_type(dst),
+ 2 * composite_type(src))
+ );
+ }
+};
+
+template<class T>
+inline T cfDifference(T src, T dst) { return qMax(src,dst) - qMin(src,dst); }
+
+template<class T>
+inline T cfScreen(T src, T dst) { return Arithmetic::unionShapeOpacity(src, dst); }
+
+template<class T>
+inline T cfDarkenOnly(T src, T dst) { return qMin(src, dst); }
+
+template<class T>
+inline T cfLightenOnly(T src, T dst) { return qMax(src, dst); }
+
+template<class T>
+inline T cfGlow(T src, T dst) {
+ 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 (dst == unitValue<T>()) {
+ return unitValue<T>();
+ }
+
+ return clamp<T>(div(mul(src, src), inv(dst)));
+}
+
+template<class T>
+inline T cfReflect(T src, T dst) {
+ using namespace Arithmetic;
+
+
+ return clamp<T>(cfGlow(dst,src));
+}
+
+template<class T>
+inline T cfHeat(T src, T dst) {
+ using namespace Arithmetic;
+
+ if(src == unitValue<T>()) {
+ return unitValue<T>();
+ }
+
+ if(dst == zeroValue<T>()) {
+ return zeroValue<T>();
+ }
+
+ return inv(clamp<T>(div(mul(inv(src), inv(src)),dst)));
+}
+
+template<class T>
+inline T cfFreeze(T src, T dst) {
+ using namespace Arithmetic;
+
+ return (cfHeat(dst,src));
+}
+
+template<class T>
+inline T cfHelow(T src, T dst) {
+ 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 (cfHardMixPhotoshop(src,dst) == unitValue<T>()) {
+ return cfHeat(src,dst);
+ }
+
+ if (src == zeroValue<T>()) {
+ return zeroValue<T>();
+ }
+
+ return (cfGlow(src,dst));
+}
+
+template<class T>
+inline T cfFrect(T src, T dst) {
+ using namespace Arithmetic;
+
+ if (cfHardMixPhotoshop(src,dst) == unitValue<T>()) {
+ return cfFreeze(src,dst);
+ }
+
+ if (dst == zeroValue<T>()) {
+ return zeroValue<T>();
+ }
+
+ return (cfReflect(src,dst));
+}
+
+template<class T>
+inline T cfGleat(T src, T dst) {
+ 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(dst == unitValue<T>()) {
+ return unitValue<T>();
+ }
+
+ if(cfHardMixPhotoshop(src,dst) == unitValue<T>()) {
+ return cfGlow(src,dst);
+ }
+
+ return (cfHeat(src,dst));
+}
+
+template<class T>
+inline T cfReeze(T src, T dst) {
+ using namespace Arithmetic;
+
+ return (cfGleat(dst,src));
+}
+template<class T>
+inline T cfFhyrd(T src, T dst) {
+ using namespace Arithmetic;
+
+ return (cfAllanon(cfFrect(src,dst),cfHelow(src,dst)));
+}
+
+template<class T>
+inline T cfInterpolation(T src, T dst) {
+ using namespace Arithmetic;
+
+ qreal fsrc = scale<qreal>(src);
+ qreal fdst = scale<qreal>(dst);
+
+ if(dst == zeroValue<T>() && src == zeroValue<T>()) {
+ return zeroValue<T>();
+ }
+
+ return scale<T>(.5f-.25f*cos(pi*(fsrc))-.25f*cos(pi*(fdst)));
+}
+
+template<class T>
+inline T cfInterpolationB(T src, T dst) {
+ using namespace Arithmetic;
+
+ return cfInterpolation(cfInterpolation(src,dst),cfInterpolation(src,dst));
+}
+
+
+template<class T>
+inline T cfPenumbraB(T src, T dst) {
+ using namespace Arithmetic;
+
+ if (dst == unitValue<T>()) {
+ return unitValue<T>();
+ }
+ if (dst + src < unitValue<T>()) {
+ return (cfColorDodge(dst,src)/2);
+ }
+ if (src == zeroValue<T>()) {
+ return zeroValue<T>();
+ }
+
+ return inv(clamp<T>(div(inv(dst),src)/2));
+}
+
+template<class T>
+inline T cfPenumbraD(T src, T dst) {
+ using namespace Arithmetic;
+
+ if (dst == unitValue<T>()) {
+ return unitValue<T>();
+ }
+
+ return cfArcTangent(src,inv(dst));
+}
+
+template<class T>
+inline T cfPenumbraC(T src, T dst) {
+ using namespace Arithmetic;
+
+ return cfPenumbraD(dst,src);
+}
+
+template<class T>
+inline T cfPenumbraA(T src, T dst) {
+ using namespace Arithmetic;
+
+ return (cfPenumbraB(dst,src));
+}
+
+template<class T>
+inline T cfSoftLightIFSIllusions(T src, T dst) {
+ using namespace Arithmetic;
+
+ qreal fsrc = scale<qreal>(src);
+ qreal fdst = scale<qreal>(dst);
+
+ return scale<T>(pow(fdst,pow(2.0,(mul(2.0,.5f-fsrc)))));
+}
+
+template<class T>
+inline T cfSoftLightPegtopDelphi(T src, T dst) {
+ using namespace Arithmetic;
+
+ return clamp<T>(cfAddition(mul(dst,cfScreen(src,dst)),mul(mul(src,dst),inv(dst))));
+}
+
+template<class T>
+inline T cfNegation(T src, T dst) {
+ using namespace Arithmetic;
+ typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;
+
+ composite_type unit = unitValue<T>();
+ composite_type a = unit - src - dst;
+ composite_type s = std::abs(a);
+ composite_type d = unit - s;
+
+ return T(d);
+}
+
+template<class T>
+inline T cfNor(T src, T dst) {
+ using namespace Arithmetic;
+
+ return and(src,dst);
+}
+
+template<class T>
+inline T cfNand(T src, T dst) {
+ using namespace Arithmetic;
+
+ return or(src,dst);
+}
+
+template<class T>
+inline T cfXor(T src, T dst) {
+ using namespace Arithmetic;
+
+ return xor(src,dst);
+}
+
+template<class T>
+inline T cfXnor(T src, T dst) {
+ using namespace Arithmetic;
+
+ return cfXor(src,inv(dst));
+}
+
+template<class T>
+inline T cfAnd(T src, T dst) {
+ using namespace Arithmetic;
+
+ return cfNor(inv(src),inv(dst));
+}
+
+template<class T>
+inline T cfOr(T src, T dst) {
+ using namespace Arithmetic;
+
+ return cfNand(inv(src),inv(dst));
+}
+
+template<class T>
+inline T cfConverse(T src, T dst) {
+ using namespace Arithmetic;
+
+ return cfOr(inv(src),dst);
+}
+
+template<class T>
+inline T cfNotConverse(T src, T dst) {
+ using namespace Arithmetic;
+
+ return cfAnd(src,inv(dst));
+}
+
+template<class T>
+inline T cfImplies(T src, T dst) {
+ using namespace Arithmetic;
+
+ return cfOr(src,inv(dst));
+}
+
+template<class T>
+inline T cfNotImplies(T src, T dst) {
+ using namespace Arithmetic;
+
+ return cfAnd(inv(src),dst);
+}
+
+template<class T>
+inline T cfPNormA(T src, T dst) {
+ using namespace Arithmetic;
+ //This is also known as P-Norm mode with factor of 2.3333 See IMBLEND image blending mode samples, and please see imblend.m file found on Additional Blending Mode thread at Phabricator. 1/2.3333 is .42875...
+
+ return clamp<T>(pow(pow((float)dst, 2.3333333333333333) + pow((float)src, 2.3333333333333333), 0.428571428571434));
+}
+
+template<class T>
+inline T cfPNormB(T src, T dst) {
+ using namespace Arithmetic;
+ //This is also known as P-Norm mode with factor of 2.3333 See IMBLEND image blending mode samples, and please see imblend.m file found on Additional Blending Mode thread at Phabricator. 1/2.3333 is .42875...
+
+ return clamp<T>(pow(pow(dst,4)+pow(src,4),0.25));
+}
+
+template<class T>
+inline T cfSuperLight(T src, T dst) {
+ using namespace Arithmetic;
+ //4.0 can be adjusted to taste. 4.0 is picked for being the best in terms of contrast and details. See imblend.m file.
+
+ qreal fsrc = scale<qreal>(src);
+ qreal fdst = scale<qreal>(dst);
+
+ if (fsrc < .5) {
+ return scale<T>(inv(pow(pow(inv(fdst),2.875)+pow(inv(2.0*fsrc),2.875),1.0/2.875)));
+ }
+
+ return scale<T>(pow(pow(fdst,2.875)+pow(2.0*fsrc-1.0,2.875),1.0/2.875));
+}
+
+template<class T>
+inline T cfTintIFSIllusions(T src, T dst) {
+ using namespace Arithmetic;
+ //Known as Light Blending mode found in IFS Illusions. Picked this name because it results into a very strong tint, and has better naming convention.
+
+ qreal fsrc = scale<qreal>(src);
+ qreal fdst = scale<qreal>(dst);
+
+ return scale<T>(fsrc*inv(fdst)+sqrt(fdst));
+}
+
+template<class T>
+inline T cfShadeIFSIllusions(T src, T dst) {
+ using namespace Arithmetic;
+ //Known as Shadow Blending mode found in IFS Illusions. Picked this name because it is the opposite of Tint (IFS Illusion Blending mode).
+
+ qreal fsrc = scale<qreal>(src);
+ qreal fdst = scale<qreal>(dst);
+
+ return scale<T>(inv((inv(fdst)*fsrc)+sqrt(inv(fsrc))));
+}
+
+template<class T>
+inline T cfFogLightenIFSIllusions(T src, T dst) {
+ using namespace Arithmetic;
+ //Known as Bright Blending mode found in IFS Illusions. Picked this name because the shading reminds me of fog when overlaying with a gradient.
+
+ qreal fsrc = scale<qreal>(src);
+ qreal fdst = scale<qreal>(dst);
+
+ if (fsrc < .5) {
+ return scale<T>(inv(inv(fsrc)*fsrc)-inv(fdst)*inv(fsrc));
+ }
+
+ return scale<T>(fsrc-inv(fdst)*inv(fsrc)+pow(inv(fsrc),2));
+}
+
+template<class T>
+inline T cfFogDarkenIFSIllusions(T src, T dst) {
+ using namespace Arithmetic;
+ //Known as Dark Blending mode found in IFS Illusions. Picked this name because the shading reminds me of fog when overlaying with a gradient.
+
+ qreal fsrc = scale<qreal>(src);
+ qreal fdst = scale<qreal>(dst);
+
+ if (fsrc < .5) {
+ return scale<T>(inv(fsrc)*fsrc+fsrc*fdst);
+ }
+
+ return scale<T>(fsrc*fdst+fsrc-pow(fsrc,2));
+}
+
+template<class T>
+inline T cfModulo(T src, T dst) {
+ using namespace Arithmetic;
+
+ return mod(dst,src);
+}
+
+template<class T>
+inline T cfModuloShift(T src, T dst) {
+ using namespace Arithmetic;
+ qreal fsrc = scale<qreal>(src);
+ qreal fdst = scale<qreal>(dst);
+
+ if (fsrc == 1.0 && fdst == 0.0) {
+ return scale<T>(0.0);
+ }
+
+
+ return scale<T>(mod((fdst+fsrc),1.0000000000));
+}
+
+template<class T>
+inline T cfModuloShiftContinuous(T src, T dst) {
+ using namespace Arithmetic;
+ //This blending mode do not behave like difference/equivalent with destination layer inverted if you use group layer on addition while the content of group layer contains several addition-mode layers, it works as expected on float images. So, no need to change this.
+ qreal fsrc = scale<qreal>(src);
+ qreal fdst = scale<qreal>(dst);
+
+ if (fsrc == 1.0 && fdst == 0.0) {
+ return scale<T>(1.0);
+ }
+
+ return scale<T>((int(ceil(fdst+fsrc)) % 2 != 0) || (fdst == zeroValue<T>()) ? cfModuloShift(fsrc,fdst) : inv(cfModuloShift(fsrc,fdst)));
+}
+
+template<class T>
+inline T cfDivisiveModulo(T src, T dst) {
+ using namespace Arithmetic;
+ //I have to use 1.00000 as unitValue failed to work for those area.
+
+ qreal fsrc = scale<qreal>(src);
+ qreal fdst = scale<qreal>(dst);
+
+ if (fsrc == zeroValue<T>()) {
+ return scale<T>(mod(((1.0000000000/epsilon<T>()) * fdst),1.0000000000));
+ }
+
+ return scale<T>(mod(((1.0000000000/fsrc) * fdst),1.0000000000));
+}
+
+template<class T>
+inline T cfDivisiveModuloContinuous(T src, T dst) {
+ using namespace Arithmetic;
+
+ qreal fsrc = scale<qreal>(src);
+ qreal fdst = scale<qreal>(dst);
+
+ if (fdst == zeroValue<T>()) {
+ return zeroValue<T>();
+ }
+
+ if (fsrc == zeroValue<T>()) {
+ return cfDivisiveModulo(fsrc,fdst);
+ }
+
+
+ return scale<T>( int(ceil(fdst/fsrc)) % 2 != 0 ? cfDivisiveModulo(fsrc,fdst) : inv(cfDivisiveModulo(fsrc,fdst)));
+}
+
+template<class T>
+inline T cfModuloContinuous(T src, T dst) {
+ using namespace Arithmetic;
+
+ return cfMultiply(cfDivisiveModuloContinuous(src,dst),src);
+}
+
+template<class T>
+inline T cfEasyDodge(T src, T dst) {
+ using namespace Arithmetic;
+ // The 13 divided by 15 can be adjusted to taste. See imgblend.m
+
+ qreal fsrc = scale<qreal>(src);
+ qreal fdst = scale<qreal>(dst);
+
+ if (fsrc == 1.0) {
+ return scale<T>(1.0);}
+
+
+ return scale<T>(pow(fdst,mul(inv(fsrc != 1.0 ? fsrc : .999999999999),1.039999999)));
+}
+
+template<class T>
+inline T cfEasyBurn(T src, T dst) {
+ using namespace Arithmetic;
+ // The 13 divided by 15 can be adjusted to taste. See imgblend.m
+
+ qreal fsrc = scale<qreal>(src);
+ qreal fdst = scale<qreal>(dst);
+
+
+ return scale<T>(inv(pow(inv(fsrc != 1.0 ? fsrc : .999999999999),mul(fdst,1.039999999))));
+}
+
+template<class T>
+inline T cfFlatLight(T src, T dst) {
+ using namespace Arithmetic;
+
+ if (src == zeroValue<T>()) {
+ return zeroValue<T>();
+ }
+
+ return clamp<T>(cfHardMixPhotoshop(inv(src),dst)==unitValue<T>() ? cfPenumbraB(src,dst) : cfPenumbraA(src,dst));
+}
+
+
+template<class HSXType, class TReal>
+inline void cfAdditionSAI(TReal src, TReal sa, TReal& dst, TReal& da)
+{
+ using namespace Arithmetic;
+ typedef typename KoColorSpaceMathsTraits<TReal>::compositetype composite_type;
+
+ Q_UNUSED(da);
+ composite_type newsrc;
+ newsrc = mul(src, sa);
+ dst = clamp<TReal>(newsrc + dst);
+}
+
+
+
+#endif // KOCOMPOSITEOP_FUNCTIONS_H_
diff --git a/libs/pigment/tests/KoCompositeOpGeneric2.h b/libs/pigment/tests/KoCompositeOpGeneric2.h
new file mode 100644
index 0000000000..81180e2f45
--- /dev/null
+++ b/libs/pigment/tests/KoCompositeOpGeneric2.h
@@ -0,0 +1,373 @@
+/*
+ * SPDX-FileCopyrightText: 2011 Silvio Heinrich <[email protected]>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+*/
+
+#ifdef _KOCOMPOSITEOP_GENERIC_H_
+#error "ssksjdkjs"
+#endif
+
+
+#ifndef _KOCOMPOSITEOP_GENERIC_H_
+#define _KOCOMPOSITEOP_GENERIC_H_
+
+#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)
+{
+ 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 <class Traits,
+ typename Traits::channels_type compositeFunc(typename Traits::channels_type, typename Traits::channels_type)>
+struct CompositeFunctionWrapper
+{
+ using channels_type = typename Traits::channels_type;
+
+ static inline channels_type composeChannel(channels_type src, channels_type dst) {
+ return compositeFunc(src, dst);
+ }
+
+ static inline channels_type clampSourceChannelValue(channels_type value) {
+ return value;
+ }
+
+ static inline channels_type clampDestinationChannelValue(channels_type value) {
+ return value;
+ }
+};
+
+template <typename T>
+struct ClampedSourceCompositeFunctorBase
+{
+ static inline T clampSourceChannelValue(T value) {
+ using namespace Arithmetic;
+ return clampChannelToSDR(value);
+ }
+
+ static inline T clampDestinationChannelValue(T value) {
+ return value;
+ }
+};
+
+template <typename T>
+struct ClampedSourceAndDestinationBottomCompositeFunctorBase
+{
+ static inline T clampSourceChannelValue(T value) {
+ using namespace Arithmetic;
+ return tmp2::clampChannelToSDRBottom(value);
+ }
+
+ static inline T clampDestinationChannelValue(T value) {
+ using namespace Arithmetic;
+ return tmp2::clampChannelToSDRBottom(value);
+ }
+};
+
+template <typename T>
+struct ClampedSourceAndDestinationCompositeFunctorBase
+{
+ static inline T clampSourceChannelValue(T value) {
+ using namespace Arithmetic;
+ return clampChannelToSDR(value);
+ }
+
+ static inline T clampDestinationChannelValue(T value) {
+ using namespace Arithmetic;
+ return clampChannelToSDR(value);
+ }
+};
+
+/**
+ * Generic CompositeOp for separable channel compositing functions
+ *
+ * A template to generate a KoCompositeOp class by just specifying a
+ * blending/compositing function. This template works with compositing functions
+ * for separable channels (means each channel of a pixel can be processed separately)
+ */
+template<
+ class Traits,
+ typename CompositeOpFunctor,
+ typename BlendingPolicy
+>
+class KoCompositeOpGenericSCFunctor: public KoCompositeOpBase< Traits, KoCompositeOpGenericSCFunctor<Traits,CompositeOpFunctor,BlendingPolicy> >
+{
+ typedef KoCompositeOpBase< Traits, KoCompositeOpGenericSCFunctor<Traits,CompositeOpFunctor,BlendingPolicy> > base_class;
+ typedef typename Traits::channels_type channels_type;
+
+ static const qint32 channels_nb = Traits::channels_nb;
+ static const qint32 alpha_pos = Traits::alpha_pos;
+
+public:
+ KoCompositeOpGenericSCFunctor(const KoColorSpace* cs, const QString& id, const QString& category)
+ : base_class(cs, id, category) { }
+
+public:
+ template<bool alphaLocked, bool allChannelFlags>
+ inline static channels_type composeColorChannels(const channels_type* src, channels_type srcAlpha,
+ channels_type* dst, channels_type dstAlpha, channels_type maskAlpha,
+ channels_type opacity, const QBitArray& channelFlags) {
+ using namespace Arithmetic;
+
+ srcAlpha = mul(srcAlpha, maskAlpha, opacity);
+
+ 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]));
+ const channels_type dstInBlendSpace =
+ BlendingPolicy::toAdditiveSpace(
+ CompositeOpFunctor::clampDestinationChannelValue(dst[i]));
+
+ dst[i] = BlendingPolicy::fromAdditiveSpace(
+ lerp(dstInBlendSpace,
+ CompositeOpFunctor::composeChannel(srcInBlendSpace, dstInBlendSpace),
+ srcAlpha));
+ }
+ }
+ }
+
+ return dstAlpha;
+ }
+ else {
+ channels_type newDstAlpha = unionShapeOpacity(srcAlpha, dstAlpha);
+
+ if (!tmp2::isZeroValue(newDstAlpha)) {
+
+ 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]));
+ const channels_type dstInBlendSpace =
+ BlendingPolicy::toAdditiveSpace(
+ CompositeOpFunctor::clampDestinationChannelValue(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);
+ }
+ }
+ }
+
+ return newDstAlpha;
+ }
+ }
+};
+
+
+template<
+ class Traits,
+ typename Traits::channels_type compositeFunc(typename Traits::channels_type, typename Traits::channels_type),
+ typename BlendingPolicy
+>
+class KoCompositeOpGenericSC : public KoCompositeOpGenericSCFunctor<Traits, CompositeFunctionWrapper<Traits, compositeFunc>, BlendingPolicy>
+{
+protected:
+ using base_class = KoCompositeOpGenericSCFunctor<Traits, CompositeFunctionWrapper<Traits, compositeFunc>, BlendingPolicy>;
+public:
+ using base_class::base_class;
+};
+
+/**
+ * Generic CompositeOp for nonseparable/HSL channel compositing functions
+ *
+ * A template to generate a KoCompositeOp class by just specifying a
+ * blending/compositing function. This template works with compositing functions
+ * for RGB channels only (the channels can not be processed separately)
+ */
+template<class Traits, void compositeFunc(float, float, float, float&, float&, float&)>
+class KoCompositeOpGenericHSL: public KoCompositeOpBase< Traits, KoCompositeOpGenericHSL<Traits,compositeFunc> >
+{
+ typedef KoCompositeOpBase< Traits, KoCompositeOpGenericHSL<Traits,compositeFunc> > base_class;
+ typedef typename Traits::channels_type channels_type;
+
+ static const qint32 red_pos = Traits::red_pos;
+ static const qint32 green_pos = Traits::green_pos;
+ static const qint32 blue_pos = Traits::blue_pos;
+
+public:
+ KoCompositeOpGenericHSL(const KoColorSpace* cs, const QString& id, const QString& category)
+ : base_class(cs, id, category) { }
+
+public:
+ template<bool alphaLocked, bool allChannelFlags>
+ inline static channels_type composeColorChannels(const channels_type* src, channels_type srcAlpha,
+ channels_type* dst, channels_type dstAlpha, channels_type maskAlpha,
+ channels_type opacity, const QBitArray& channelFlags) {
+ using namespace Arithmetic;
+
+ srcAlpha = mul(srcAlpha, maskAlpha, opacity);
+
+ if(alphaLocked) {
+ if(dstAlpha != zeroValue<channels_type>()) {
+ float srcR = scale<float>(src[red_pos]);
+ float srcG = scale<float>(src[green_pos]);
+ float srcB = scale<float>(src[blue_pos]);
+
+ float dstR = scale<float>(dst[red_pos]);
+ float dstG = scale<float>(dst[green_pos]);
+ float dstB = scale<float>(dst[blue_pos]);
+
+ compositeFunc(srcR, srcG, srcB, dstR, dstG, dstB);
+
+ if(allChannelFlags || channelFlags.testBit(red_pos))
+ dst[red_pos] = lerp(dst[red_pos], scale<channels_type>(dstR), srcAlpha);
+
+ if(allChannelFlags || channelFlags.testBit(green_pos))
+ dst[green_pos] = lerp(dst[green_pos], scale<channels_type>(dstG), srcAlpha);
+
+ if(allChannelFlags || channelFlags.testBit(blue_pos))
+ dst[blue_pos] = lerp(dst[blue_pos], scale<channels_type>(dstB), srcAlpha);
+ }
+
+ return dstAlpha;
+ }
+ else {
+ channels_type newDstAlpha = unionShapeOpacity(srcAlpha, dstAlpha);
+
+ if(newDstAlpha != zeroValue<channels_type>()) {
+ float srcR = scale<float>(src[red_pos]);
+ float srcG = scale<float>(src[green_pos]);
+ float srcB = scale<float>(src[blue_pos]);
+
+ float dstR = scale<float>(dst[red_pos]);
+ float dstG = scale<float>(dst[green_pos]);
+ float dstB = scale<float>(dst[blue_pos]);
+
+ compositeFunc(srcR, srcG, srcB, dstR, dstG, dstB);
+
+ if(allChannelFlags || channelFlags.testBit(red_pos))
+ dst[red_pos] = div(blend(src[red_pos], srcAlpha, dst[red_pos], dstAlpha, scale<channels_type>(dstR)), newDstAlpha);
+
+ if(allChannelFlags || channelFlags.testBit(green_pos))
+ dst[green_pos] = div(blend(src[green_pos], srcAlpha, dst[green_pos], dstAlpha, scale<channels_type>(dstG)), newDstAlpha);
+
+ if(allChannelFlags || channelFlags.testBit(blue_pos))
+ dst[blue_pos] = div(blend(src[blue_pos], srcAlpha, dst[blue_pos], dstAlpha, scale<channels_type>(dstB)), newDstAlpha);
+ }
+
+ return newDstAlpha;
+ }
+ }
+};
+
+
+
+/**
+ * Generic CompositeOp for separable channel + alpha compositing functions
+ *
+ * A template to generate a KoCompositeOp class by just specifying a
+ * blending/compositing function. This template works with compositing functions
+ * for separable channels (means each channel of a pixel can be processed separately)
+ * with taking alpha into consideration.
+ * Note that because of special treating of alpha, any composite op function
+ * needs to make alpha blending itself - the value of color that is written onto the projection
+ * is the same that the composite function gives (compare with KoCompositeOpGenericHSL and KoCompositeOpGenericSC).
+ */
+template<class Traits, void compositeFunc(float, float, float&, float&), typename BlendingPolicy>
+class KoCompositeOpGenericSCAlpha: public KoCompositeOpBase< Traits, KoCompositeOpGenericSCAlpha<Traits,compositeFunc,BlendingPolicy> >
+{
+ typedef KoCompositeOpBase< Traits, KoCompositeOpGenericSCAlpha<Traits,compositeFunc,BlendingPolicy> > base_class;
+ typedef typename Traits::channels_type channels_type;
+
+ static const qint32 channels_nb = Traits::channels_nb;
+ static const qint32 alpha_pos = Traits::alpha_pos;
+
+public:
+ KoCompositeOpGenericSCAlpha(const KoColorSpace* cs, const QString& id, const QString& category)
+ : base_class(cs, id, category) { }
+
+public:
+ template<bool alphaLocked, bool allChannelFlags>
+ inline static channels_type composeColorChannels(const channels_type* src, channels_type srcAlpha,
+ channels_type* dst, channels_type dstAlpha, channels_type maskAlpha,
+ channels_type opacity, const QBitArray& channelFlags)
+ {
+ using namespace Arithmetic;
+
+ srcAlpha = mul(srcAlpha, maskAlpha, opacity);
+
+ if(alphaLocked) {
+ channels_type oldAlpha = dstAlpha;
+ if(dstAlpha != zeroValue<channels_type>()) {
+ for(qint32 i=0; i <channels_nb; i++) {
+ if(i != alpha_pos && (allChannelFlags || channelFlags.testBit(i))) {
+ float dstValueFloat = scale<float>(BlendingPolicy::toAdditiveSpace(dst[i]));
+ float dstAlphaFloat = scale<float>(oldAlpha);
+ compositeFunc(scale<float>(BlendingPolicy::toAdditiveSpace(src[i])), scale<float>(srcAlpha), dstValueFloat, dstAlphaFloat);
+ dst[i] = BlendingPolicy::fromAdditiveSpace(scale<channels_type>(dstValueFloat));
+ }
+ }
+ }
+
+ return dstAlpha;
+ }
+ else {
+ channels_type oldAlpha = dstAlpha;
+ channels_type newDstAlpha = unionShapeOpacity(srcAlpha, dstAlpha);
+
+ if(newDstAlpha != zeroValue<channels_type>()) {
+ for(qint32 i=0; i <channels_nb; i++) {
+ if(i != alpha_pos && (allChannelFlags || channelFlags.testBit(i))) {
+ float dstFloat = scale<float>(BlendingPolicy::toAdditiveSpace(dst[i]));
+ float dstAlphaFloat = scale<float>(oldAlpha);
+ compositeFunc(scale<float>(BlendingPolicy::toAdditiveSpace(src[i])), scale<float>(srcAlpha), dstFloat, dstAlphaFloat);
+ dst[i] = BlendingPolicy::fromAdditiveSpace(scale<channels_type>(dstFloat));
+ }
+ }
+ }
+
+ return newDstAlpha;
+ }
+ }
+};
+
+
+
+#endif // _KOCOMPOSITEOP_GENERIC_H_
diff --git a/libs/pigment/tests/TestCompositeOpInversion.cpp b/libs/pigment/tests/TestCompositeOpInversion.cpp
index b6d7fbee15..e4edd090c8 100644
--- a/libs/pigment/tests/TestCompositeOpInversion.cpp
+++ b/libs/pigment/tests/TestCompositeOpInversion.cpp
@@ -208,4 +208,1004 @@ void TestCompositeOpInversion::test_data()
}
}
+#include <KoCompositeOpGeneric2.h>
+#include <KoCompositeOpFunctions2.h>
+#include "compositeops/KoColorSpaceBlendingPolicy.h"
+
+#include <KoColorModelStandardIds.h>
+void TestCompositeOpInversion::testBurnInF32Mode()
+{
+ const KoColorSpace* cs = KoColorSpaceRegistry::instance()->colorSpace(RGBAColorModelID.id(), Float32BitsColorDepthID.id(), 0);
+ const KoCompositeOp *op = cs->compositeOp(COMPOSITE_BURN);
+
+ const qreal opacity = 0.25;
+ KoColor srcColor(QColor(10,0,20,255), cs);
+ KoColor dstColor(QColor(0,1,2,10), cs);
+
+ op->composite(dstColor.data(), 0, srcColor.data(), 0,
+ 0, 0,
+ 1, 1,
+ opacity);
+
+ ENTER_FUNCTION() << ppVar(srcColor) << ppVar(dstColor);
+
+}
+
+namespace {
+struct Wrapper {
+ KoColor color;
+ int index = 0;
+};
+
+QDebug operator<<(QDebug debug, const Wrapper &w) {
+ const KoColor &c = w.color;
+
+ if (c.colorSpace()->colorDepthId() == Float32BitsColorDepthID) {
+ const float *ptr = reinterpret_cast<const float*>(c.data());
+ debug.nospace() << "(" << ptr[w.index] << ", " << ptr[3] << ")";
+ } else if (c.colorSpace()->colorDepthId() == Integer16BitsColorDepthID) {
+ using namespace Arithmetic;
+ const quint16 *ptr = reinterpret_cast<const quint16*>(c.data());
+ debug.nospace() << "(" << qreal(ptr[2 - w.index]) / unitValue<quint16>() << ", " << qreal(ptr[3]) / unitValue<quint16>() << ")";
+ }
+
+ return debug.space();
+}
+
+Wrapper dumpPixel(const KoColor &color) {
+ return {color, 0};
+}
+
+float getColorValue(const KoColor &c) {
+ float result = 777.0;
+
+ if (c.colorSpace()->colorDepthId() == Float32BitsColorDepthID) {
+ const float *ptr = reinterpret_cast<const float*>(c.data());
+ result = ptr[0];
+ } else if (c.colorSpace()->colorDepthId() == Integer16BitsColorDepthID) {
+ using namespace Arithmetic;
+ const quint16 *ptr = reinterpret_cast<const quint16*>(c.data());
+ result = qreal(ptr[2]) / unitValue<quint16>();
+ }
+
+ return result;
+}
+
+quint16 getColorValueU(const KoColor &c) {
+ KIS_ASSERT(c.colorSpace()->colorDepthId() == Integer16BitsColorDepthID);
+
+ using namespace Arithmetic;
+ const quint16 *ptr = reinterpret_cast<const quint16*>(c.data());
+ return ptr[2];
+}
+
+
+
+const KoCompositeOp* createOp(const KoColorSpace *cs, const QString &id, bool isHDR)
+{
+ const KoCompositeOp *op = nullptr;
+
+ if (id == COMPOSITE_DODGE) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ if (isHDR) {
+ using func = CFColorDodge<float, ClampPolicyFloatHDR>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryLight());
+ } else {
+ using func = CFColorDodge<float, ClampPolicyFloatSDR>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryLight());
+ }
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ using func = CFColorDodge<quint16, ClampPolicyInteger>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryLight());
+ }
+#if 1
+ } else if (id == COMPOSITE_BURN) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ if (isHDR) {
+ using func = CFColorBurn<float, ClampPolicyFloatHDR>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func,
+ KoAdditiveBlendingPolicy<Traits>>
+ (cs, id, KoCompositeOp::categoryDark());
+ } else {
+ using func = CFColorBurn<float, ClampPolicyFloatSDR>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func,
+ KoAdditiveBlendingPolicy<Traits>>
+ (cs, id, KoCompositeOp::categoryDark());
+ }
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ using func = CFColorBurn<quint16, ClampPolicyInteger>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryDark());
+ }
+ } else if (id == COMPOSITE_LINEAR_BURN) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ if (isHDR) {
+ using func = CFLinearBurn<float, ClampPolicyFloatHDR>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryDark());
+ } else {
+ using func = CFLinearBurn<float, ClampPolicyFloatSDR>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryDark());
+ }
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ using func = CFLinearBurn<quint16, ClampPolicyInteger>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryDark());
+ }
+
+ } else if (id == COMPOSITE_HARD_LIGHT) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ using func = CFHardLight<float>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryDark());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ using func = CFHardLight<quint16>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_SOFT_LIGHT_PHOTOSHOP) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ using func = CFSoftLight<float>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryDark());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ using func = CFSoftLight<quint16>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_SOFT_LIGHT_SVG) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ using func = CFSoftLightSvg<float>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryDark());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ using func = CFSoftLightSvg<quint16>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_VIVID_LIGHT) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ if (isHDR) {
+ using func = CFVividLight<float, ClampPolicyFloatHDR>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryDark());
+ } else {
+ using func = CFVividLight<float, ClampPolicyFloatSDR>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryDark());
+ }
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ using func = CFVividLight<quint16, ClampPolicyInteger>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_HARD_OVERLAY) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ if (isHDR) {
+ using func = CFHardOverlay<float, ClampPolicyFloatHDR>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryDark());
+ } else {
+ using func = CFHardOverlay<float, ClampPolicyFloatSDR>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryDark());
+ }
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ using func = CFHardOverlay<quint16, ClampPolicyInteger>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ // } else if (id == COMPOSITE_FLAT_LIGHT) {
+ // if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ // using Traits = KoRgbF32Traits;
+ // constexpr auto func = &cfFlatLight<float>;
+ // op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryDark());
+ // } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ // using Traits = KoRgbU16Traits;
+ // constexpr auto func = &cfFlatLight<quint16>;
+ // op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ // }
+ } else if (id == COMPOSITE_PIN_LIGHT) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ if (isHDR) {
+ using func = CFPinLight<float, ClampPolicyFloatHDR>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryDark());
+ } else {
+ using func = CFPinLight<float, ClampPolicyFloatSDR>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryDark());
+ }
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ using func = CFPinLight<quint16, ClampPolicyInteger>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_ADD) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ constexpr auto func = &cfAddition<float>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ constexpr auto func = &cfAddition<quint16>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_SUBTRACT) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ constexpr auto func = &cfSubtract<float>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ constexpr auto func = &cfSubtract<quint16>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_INVERSE_SUBTRACT) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ constexpr auto func = &cfInverseSubtract<float>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ constexpr auto func = &cfInverseSubtract<quint16>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_MULT) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ constexpr auto func = &cfMultiply<float>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ constexpr auto func = &cfMultiply<quint16>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_DIVIDE) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ constexpr auto func = &cfDivide<float>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ constexpr auto func = &cfDivide<quint16>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_EXCLUSION) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ constexpr auto func = &cfExclusion<float>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ constexpr auto func = &cfExclusion<quint16>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_DIFF) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ constexpr auto func = &cfDifference<float>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ constexpr auto func = &cfDifference<quint16>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_SCREEN) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ constexpr auto func = &cfScreen<float>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ constexpr auto func = &cfScreen<quint16>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_DARKEN) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ constexpr auto func = &cfDarkenOnly<float>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ constexpr auto func = &cfDarkenOnly<quint16>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_LIGHTEN) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ constexpr auto func = &cfLightenOnly<float>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ constexpr auto func = &cfLightenOnly<quint16>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_NEGATION) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ constexpr auto func = &cfNegation<float>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ constexpr auto func = &cfNegation<quint16>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_HARD_MIX) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ if (isHDR) {
+ using func = CFHardMix<float, ClampPolicyFloatHDR>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryDark());
+ } else {
+ using func = CFHardMix<float, ClampPolicyFloatSDR>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryDark());
+ }
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ using func = CFHardMix<quint16, ClampPolicyInteger>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_HARD_MIX_PHOTOSHOP) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ constexpr auto func = &cfHardMixPhotoshop<float>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ constexpr auto func = &cfHardMixPhotoshop<quint16>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_HARD_MIX_PHOTOSHOP) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ constexpr auto func = &cfHardMixSofterPhotoshop<float>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ constexpr auto func = &cfHardMixSofterPhotoshop<quint16>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_LUMINOSITY_SAI) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ constexpr auto func = &cfAdditionSAI<HSVType, float>;
+ op = new KoCompositeOpGenericSCAlpha<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ constexpr auto func = &cfAdditionSAI<HSVType, float>;
+ op = new KoCompositeOpGenericSCAlpha<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_ARC_TANGENT) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ constexpr auto func = &cfArcTangent<float>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ constexpr auto func = &cfArcTangent<quint16>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_ALLANON) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ constexpr auto func = &cfAllanon<float>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ constexpr auto func = &cfAllanon<quint16>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_PARALLEL) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ using func = CFParallel<float>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ using func = CFParallel<quint16>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_EQUIVALENCE) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ using func = CFEquivalence<float>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ using func = CFEquivalence<quint16>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_GRAIN_MERGE) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ constexpr auto func = &cfGrainMerge<float>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ constexpr auto func = &cfGrainMerge<quint16>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_GRAIN_EXTRACT) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ constexpr auto func = &cfGrainExtract<float>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ constexpr auto func = &cfGrainExtract<quint16>;
+ op = new KoCompositeOpGenericSC<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_GEOMETRIC_MEAN) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ using func = CFGeometricMean<float>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ using func = CFGeometricMean<quint16>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_ADDITIVE_SUBTRACTIVE) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ using func = CFAdditiveSubtractive<float>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ using func = CFAdditiveSubtractive<quint16>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_GAMMA_DARK) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ using func = CFGammaDark<float>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ using func = CFGammaDark<quint16>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_GAMMA_LIGHT) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ using func = CFGammaLight<float>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ using func = CFGammaLight<quint16>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+ } else if (id == COMPOSITE_GAMMA_ILLUMINATION) {
+ if (cs->colorDepthId() == Float32BitsColorDepthID) {
+ using Traits = KoRgbF32Traits;
+ using func = CFGammaIllumination<float>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ } else if (cs->colorDepthId() == Integer16BitsColorDepthID) {
+ using Traits = KoRgbU16Traits;
+ using func = CFGammaIllumination<quint16>;
+ op = new KoCompositeOpGenericSCFunctor<Traits, func, KoAdditiveBlendingPolicy<Traits>>(cs, id, KoCompositeOp::categoryArithmetic());
+ }
+#endif
+ } else if (id == COMPOSITE_DISSOLVE ||
+ id == COMPOSITE_GREATER ||
+ id == COMPOSITE_OVER ||
+ id == COMPOSITE_ALPHA_DARKEN ||
+ id == COMPOSITE_DESTINATION_ATOP ||
+ id == COMPOSITE_DESTINATION_IN ||
+ id == COMPOSITE_ERASE) {
+ op = cs->compositeOp(id);
+ }
+
+ KIS_ASSERT(op);
+
+ return op;
+}
+
+}
+
+enum TestFlag {
+ None = 0x0,
+ HDR = 0x1,
+ UndefinedIfSrcInHDRRange = 0x2,
+ AllowClampedComparisonInSDR = 0x4,
+ SrcCannotMakeNegative = 0x8,
+ ForceClippedResult = 0x10,
+ UseStrictSdrRange = 0x20,
+ PreservesSdrRange = 0x40,
+ PreservesStrictSdrRange = 0x80
+};
+Q_DECLARE_FLAGS(TestFlags, TestFlag)
+Q_DECLARE_OPERATORS_FOR_FLAGS(TestFlags)
+Q_DECLARE_METATYPE(TestFlags)
+
+void TestCompositeOpInversion::testFloatModes()
+{
+ QFETCH(QString, id);
+ QFETCH(TestFlags, flags);
+
+ const KoColorSpace* csU = KoColorSpaceRegistry::instance()->colorSpace(RGBAColorModelID.id(), Integer16BitsColorDepthID.id(), 0);
+ const KoCompositeOp *opU = createOp(csU, id, flags.testFlag(HDR));
+ const KoCompositeOp *opU2 = csU->compositeOp(id);
+
+ const KoColorSpace* csF = KoColorSpaceRegistry::instance()->colorSpace(RGBAColorModelID.id(), Float32BitsColorDepthID.id(), 0);
+ const KoCompositeOp *opF = createOp(csF, id, flags.testFlag(HDR));
+
+ auto createColorU = [&] (qreal colorF, qreal alphaF) {
+ using namespace Arithmetic;
+
+ /**
+ * Some composite ops reply on the halfValue to decide about the
+ * processing they do, so we should make sure that float and uint16
+ * modes use the same half-point in the test
+ */
+ const quint16 color = qFuzzyCompare(colorF, 0.5) ?
+ KoColorSpaceMathsTraits<quint16>::halfValue :
+ qRound(qBound(0.0, colorF, 1.0) * unitValue<quint16>());
+ const quint16 alpha = qRound(qBound(0.0, alphaF, 1.0) * unitValue<quint16>());
+
+ KoColor c(csU);
+ quint16 *ptr = reinterpret_cast<quint16*>(c.data());
+ ptr[2] = color;
+ ptr[3] = alpha;
+ return c;
+ };
+
+ auto createColorF = [&] (qreal color, qreal alpha) {
+ KoColor c(csF);
+ float *ptr = reinterpret_cast<float*>(c.data());
+ ptr[0] = color;
+ ptr[3] = alpha;
+ return c;
+ };
+
+ using namespace boost::accumulators;
+ accumulator_set<qreal, stats<tag::max, tag::mean> > error;
+
+ std::vector<qreal> opacityValues({0.0,
+ 0 + 2 * std::numeric_limits<float>::epsilon(),
+ 1.0f / 255.0f,
+ 0.1, 0.2, 0.5, 0.8, 0.9,
+ 1.0 - 1.0f / 255.0f,
+ 1.0 - 2 * std::numeric_limits<float>::epsilon(),
+ 1.0});
+ //std::vector<qreal> opacityValues({1.0});
+ //std::vector<qreal> alphaValues({1.0});
+ std::vector<qreal> alphaValues = opacityValues;
+ std::vector<qreal> values({-0.1,
+ 0 - std::numeric_limits<float>::epsilon(),
+ 0,
+ 0 + std::numeric_limits<float>::epsilon(),
+ 0.1, 0.2, 0.5, 0.8, 0.9,
+ 1.0 - std::numeric_limits<float>::epsilon(),
+ 1.0,
+ 1.0 + std::numeric_limits<float>::epsilon(),
+ 1.1,
+ 1.5});
+ std::vector<qreal> valuesSrc = values;
+ std::vector<qreal> valuesDst = values;
+
+// std::vector<qreal> valuesSrc({0.0});
+// std::vector<qreal> valuesDst({-0.1});
+
+ Q_FOREACH (qreal opacity, opacityValues) {
+ Q_FOREACH (qreal srcAlphaValue, alphaValues) {
+ Q_FOREACH (qreal dstAlphaValue, alphaValues) {
+ Q_FOREACH (qreal srcColorValue, valuesSrc) {
+ Q_FOREACH (qreal dstColorValue, valuesDst) {
+
+ const qreal minOpacity = std::min({opacity, srcAlphaValue, dstAlphaValue});
+
+
+ float tolerance = minOpacity > 0.999 ? 0.001 :
+ minOpacity > 0.5 ? 10 :
+ minOpacity > 0.2 ? 100 :
+ 100;
+// {
+// {
+// {
+// {
+// {
+// qreal srcColorValue = 1.0 - std::numeric_limits<float>::epsilon();
+// qreal dstColorValue = 0.2;
+
+// qreal opacity = 0.5;
+// qreal srcAlphaValue = 0.5;
+// qreal dstAlphaValue = 0.5;
+
+ KoColor srcColorF = createColorF(srcColorValue,srcAlphaValue);
+ KoColor dstColorF = createColorF(dstColorValue,dstAlphaValue);
+ KoColor resultColorF = dstColorF;
+
+ opF->composite(resultColorF.data(), 0, srcColorF.data(), 0,
+ 0, 0,
+ 1, 1,
+ opacity);
+
+ KoColor srcColorU = createColorU(srcColorValue,srcAlphaValue);
+ KoColor dstColorU = createColorU(dstColorValue,dstAlphaValue);
+ KoColor resultColorU = dstColorU;
+
+ opU->composite(resultColorU.data(), 0, srcColorU.data(), 0,
+ 0, 0,
+ 1, 1,
+ opacity);
+
+ float resultColorValueU = getColorValue(resultColorU);
+ float resultColorValueF = getColorValue(resultColorF);
+
+
+ if (!flags.testFlag(HDR) &&
+ id != COMPOSITE_DISSOLVE &&
+ id != COMPOSITE_GREATER &&
+ id != COMPOSITE_OVER &&
+ id != COMPOSITE_ALPHA_DARKEN &&
+ id != COMPOSITE_DESTINATION_ATOP &&
+ id != COMPOSITE_DESTINATION_IN &&
+ id != COMPOSITE_ERASE) {
+ KoColor resultColorU2 = dstColorU;
+
+ opU2->composite(resultColorU2.data(), 0, srcColorU.data(), 0,
+ 0, 0,
+ 1, 1,
+ opacity);
+
+ const quint16 referenceColor = getColorValueU(resultColorU2);
+ const quint16 resultColor = getColorValueU(resultColorU);
+
+ /**
+ * 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 (difference > maxDifference) {
+
+ qDebug() << "--- integer implementation is inconsistent to the original mode! ---";
+ qDebug() << ppVar(opacity) << ppVar(resultColor) << ppVar(referenceColor);
+ qDebug() << "U16 result: " << fixed << qSetRealNumberPrecision(8)
+ << "s:" << dumpPixel(srcColorU) << "+" << "d:" << dumpPixel(dstColorU) << "->" << dumpPixel(resultColorU);
+ qDebug() << "U16 reference:" << fixed << qSetRealNumberPrecision(8)
+ << "s:" << dumpPixel(srcColorU) << "+" << "d:" << dumpPixel(dstColorU) << "->" << dumpPixel(resultColorU2);
+ QFAIL("integer implementation is inconsistent to the original mode!");
+ }
+ }
+
+ if (std::isnan(resultColorValueF)) {
+ qDebug() << "--- NaN value is found! ---";
+ qDebug() << ppVar(opacity);
+ qDebug() << "U16:" << fixed << qSetRealNumberPrecision(8)
+ << "s:" << dumpPixel(srcColorU) << "+" << "d:" << dumpPixel(dstColorU) << "->" << dumpPixel(resultColorU);
+ qDebug() << "F32:" << fixed << qSetRealNumberPrecision(8)
+ << "s:" << dumpPixel(srcColorF) << "+" << "d:" << dumpPixel(dstColorF) << "->" << dumpPixel(resultColorF);
+ QFAIL("NaN value is found!");
+ }
+
+ QVERIFY(!std::isinf(resultColorValueF));
+
+ auto checkInSdrRangeImpl = [=] (qreal value, bool useStrictRange) {
+ return useStrictRange ?
+ (value >= 0.0 && value <= 1.0) :
+ (value >= -std::numeric_limits<float>::epsilon() &&
+ value <= 1.0 + std::numeric_limits<float>::epsilon());
+ };
+
+ auto checkInSdrRange = [=] (qreal value) {
+ return checkInSdrRangeImpl(value, flags.testFlag(UseStrictSdrRange));
+ };
+
+ const bool isInSdrRange =
+ checkInSdrRange(dstColorValue) &&
+ checkInSdrRange(srcColorValue);
+
+ if (isInSdrRange &&
+ qAbs(resultColorValueF - resultColorValueU) > tolerance) {
+
+ if (qFuzzyIsNull(resultColorValueU) &&
+ qAbs(resultColorValueU - qMax(0.0f, resultColorValueF)) <= 0.001) {
+ // noop, should be fine result
+ } else if (qFuzzyCompare(resultColorValueU, 1.0f) &&
+ qAbs(resultColorValueU - qMin(1.0f, resultColorValueF)) <= 0.001) {
+ // noop, should be fine result as well
+ } else if (resultColorValueF < 0.0) {
+ qDebug() << "--- resulting value in SDR range is negative! ---";
+ qDebug() << ppVar(opacity);
+ qDebug() << "U16:" << fixed << qSetRealNumberPrecision(8)
+ << "s:" << dumpPixel(srcColorU) << "+" << "d:" << dumpPixel(dstColorU) << "->" << dumpPixel(resultColorU);
+ qDebug() << "F32:" << fixed << qSetRealNumberPrecision(8)
+ << "s:" << dumpPixel(srcColorF) << "+" << "d:" << dumpPixel(dstColorF) << "->" << dumpPixel(resultColorF);
+ QFAIL("resulting value in SDR range is negative!");
+ } else {
+
+ // in non-HDR mode both blending modes should behave in
+ // exactly the same way (including zero and unit values)
+
+ qDebug() << "--- inconsistent result in SDR range ---";
+ qDebug() << ppVar(opacity);
+ qDebug() << "U16:" << fixed << qSetRealNumberPrecision(8)
+ << "s:" << dumpPixel(srcColorU) << "+" << "d:" << dumpPixel(dstColorU) << "->" << dumpPixel(resultColorU);
+ qDebug() << "F32:" << fixed << qSetRealNumberPrecision(8)
+ << "s:" << dumpPixel(srcColorF) << "+" << "d:" << dumpPixel(dstColorF) << "->" << dumpPixel(resultColorF);
+ QFAIL("inconsistent result in SDR range!");
+ }
+ }
+
+ if ((flags.testFlag(PreservesSdrRange) ||
+ flags.testFlag(PreservesStrictSdrRange)) &&
+ checkInSdrRangeImpl(srcColorValue, flags.testFlag(PreservesStrictSdrRange)) &&
+ checkInSdrRangeImpl(dstColorValue, flags.testFlag(PreservesStrictSdrRange)) &&
+ !checkInSdrRangeImpl(resultColorValueF, flags.testFlag(PreservesStrictSdrRange))) {
+
+
+ bool resultConvergedToAPoint = false;
+
+ std::vector<float> transitionalValues;
+ transitionalValues.reserve(20 + 1);
+
+ for (int i = 0; i < 30; i++) {
+ transitionalValues.push_back(resultColorValueF);
+
+ opF->composite(resultColorF.data(), 0, srcColorF.data(), 0,
+ 0, 0,
+ 1, 1,
+ opacity);
+ resultColorValueF = getColorValue(resultColorF);
+
+ QVERIFY(!std::isnan(resultColorValueF));
+ QVERIFY(!std::isinf(resultColorValueF));
+
+ /**
+ * We are using strict comparison here explicitly, not
+ * qFuzzyCompare(), because we need the algorithm to converge
+ * strictly to a single value.
+ */
+ if (resultColorValueF == transitionalValues.back()) {
+ resultConvergedToAPoint = true;
+ transitionalValues.push_back(resultColorValueF);
+ break;
+ }
+ }
+
+ const float originalError = qAbs(transitionalValues.front() - resultColorValueU);
+ const float finalError = qAbs(transitionalValues.back() - resultColorValueU);
+
+ if (checkInSdrRangeImpl(transitionalValues.back(), true) ||
+ ((resultConvergedToAPoint || finalError < originalError) &&
+ qAbs(transitionalValues.back() - transitionalValues.front())
+ < 64 * std::numeric_limits<float>::epsilon())) {
+
+ // noop, everything is fine
+ } else {
+ qDebug() << "--- op does not preserve SDR range! ---";
+ qDebug() << fixed << qSetRealNumberPrecision(8)
+ << ppVar(opacity) << ppVar(srcColorValue) << ppVar(dstColorValue) << ppVar(resultColorValueF)
+ << fixed << qSetRealNumberPrecision(14);
+
+ qDebug() << "stabilization track:";
+ for (size_t i = 0; i < transitionalValues.size(); i++) {
+ qDebug() << fixed << qSetRealNumberPrecision(14)
+ << " " << i << ":" << transitionalValues[i];
+ }
+
+ qDebug() << "U16:" << fixed << qSetRealNumberPrecision(8)
+ << "s:" << dumpPixel(srcColorU) << "+" << "d:" << dumpPixel(dstColorU) << "->" << dumpPixel(resultColorU);
+ qDebug() << "F32:" << fixed << qSetRealNumberPrecision(8)
+ << "s:" << dumpPixel(srcColorF) << "+" << "d:" << dumpPixel(dstColorF) << "->" << dumpPixel(resultColorF);
+
+ QFAIL("op does not preserve SDR range!");
+ }
+ }
+
+ if (flags.testFlag(SrcCannotMakeNegative) &&
+ dstColorValue >= 0.0f &&
+ dstColorValue <= 1.0f &&
+ resultColorValueF < -std::numeric_limits<float>::epsilon()) {
+
+ qDebug() << "--- resulting value in SDR range is negative for SRC-clipped op! ---";
+ qDebug() << ppVar(opacity);
+ qDebug() << "U16:" << fixed << qSetRealNumberPrecision(8)
+ << "s:" << dumpPixel(srcColorU) << "+" << "d:" << dumpPixel(dstColorU) << "->" << dumpPixel(resultColorU);
+ qDebug() << "F32:" << fixed << qSetRealNumberPrecision(8)
+ << "s:" << dumpPixel(srcColorF) << "+" << "d:" << dumpPixel(dstColorF) << "->" << dumpPixel(resultColorF);
+ QFAIL("resulting value in SDR range is negative for SRC-clipped op!");
+ }
+
+ // TODO; it doesn't work for some reason
+ if (opacity > 0.01 &&
+ flags.testFlag(ForceClippedResult) &&
+ resultColorValueF > 1.0f) {
+
+ qDebug() << "--- resulting value is outside SDR range for clipped op! ---";
+ qDebug() << ppVar(opacity);
+ qDebug() << "U16:" << fixed << qSetRealNumberPrecision(8)
+ << "s:" << dumpPixel(srcColorU) << "+" << "d:" << dumpPixel(dstColorU) << "->" << dumpPixel(resultColorU);
+ qDebug() << "F32:" << fixed << qSetRealNumberPrecision(8)
+ << "s:" << dumpPixel(srcColorF) << "+" << "d:" << dumpPixel(dstColorF) << "->" << dumpPixel(resultColorF);
+ QFAIL("resulting value in outside SDR range for clipped op!");
+ }
+ }
+ }
+ }
+ }
+ }
+}
+
+void TestCompositeOpInversion::testFloatModes_data()
+{
+ QStringList ids;
+// ids << COMPOSITE_OVER;
+// ids << COMPOSITE_ALPHA_DARKEN;
+// ids << COMPOSITE_COPY;
+// ids << COMPOSITE_ERASE;
+// ids << COMPOSITE_BEHIND;
+// ids << COMPOSITE_DESTINATION_IN;
+// ids << COMPOSITE_DESTINATION_ATOP;
+
+
+ ids << COMPOSITE_HARD_MIX;
+
+// ids << COMPOSITE_OVERLAY; // equals to hard-light
+// ids << COMPOSITE_GRAIN_MERGE;
+// ids << COMPOSITE_GRAIN_EXTRACT;
+// ids << COMPOSITE_GEOMETRIC_MEAN;
+// ids << COMPOSITE_ALLANON;
+ ids << COMPOSITE_HARD_OVERLAY;
+// ids << COMPOSITE_INTERPOLATION;
+// ids << COMPOSITE_INTERPOLATIONB;
+// ids << COMPOSITE_PENUMBRAA;
+// ids << COMPOSITE_PENUMBRAB;
+// ids << COMPOSITE_PENUMBRAC;
+// ids << COMPOSITE_PENUMBRAD;
+// ids << COMPOSITE_SCREEN;
+ ids << COMPOSITE_DODGE;
+// ids << COMPOSITE_LINEAR_DODGE;
+// ids << COMPOSITE_LIGHTEN;
+// ids << COMPOSITE_SOFT_LIGHT_IFS_ILLUSIONS;
+// ids << COMPOSITE_SOFT_LIGHT_PEGTOP_DELPHI;
+// ids << COMPOSITE_GAMMA_LIGHT;
+// ids << COMPOSITE_GAMMA_ILLUMINATION;
+ ids << COMPOSITE_VIVID_LIGHT;
+ ids << COMPOSITE_PIN_LIGHT;
+// ids << COMPOSITE_LINEAR_LIGHT;
+// ids << COMPOSITE_PNORM_A;
+// ids << COMPOSITE_PNORM_B;
+// ids << COMPOSITE_SUPER_LIGHT;
+// ids << COMPOSITE_TINT_IFS_ILLUSIONS;
+// ids << COMPOSITE_FOG_LIGHTEN_IFS_ILLUSIONS;
+// ids << COMPOSITE_EASY_DODGE;
+ ids << COMPOSITE_BURN;
+ ids << COMPOSITE_LINEAR_BURN;
+// ids << COMPOSITE_DARKEN;
+// ids << COMPOSITE_GAMMA_DARK;
+// ids << COMPOSITE_SHADE_IFS_ILLUSIONS;
+// ids << COMPOSITE_FOG_DARKEN_IFS_ILLUSIONS;
+// ids << COMPOSITE_EASY_BURN;
+// ids << COMPOSITE_MOD;
+// ids << COMPOSITE_MOD_CON;
+// ids << COMPOSITE_DIVISIVE_MOD;
+// ids << COMPOSITE_DIVISIVE_MOD_CON;
+// ids << COMPOSITE_MODULO_SHIFT;
+// ids << COMPOSITE_MODULO_SHIFT_CON;
+// ids << COMPOSITE_DIFF;
+// ids << COMPOSITE_EXCLUSION;
+// ids << COMPOSITE_EQUIVALENCE;
+// ids << COMPOSITE_ADDITIVE_SUBTRACTIVE;
+// ids << COMPOSITE_NEGATION;
+
+// ids << COMPOSITE_XOR;
+// ids << COMPOSITE_OR;
+// ids << COMPOSITE_AND;
+// ids << COMPOSITE_NAND;
+// ids << COMPOSITE_NOR;
+// ids << COMPOSITE_XNOR;
+// ids << COMPOSITE_IMPLICATION;
+// ids << COMPOSITE_NOT_IMPLICATION;
+// ids << COMPOSITE_CONVERSE;
+// ids << COMPOSITE_NOT_CONVERSE;
+
+// ids << COMPOSITE_REFLECT;
+// ids << COMPOSITE_GLOW;
+// ids << COMPOSITE_FREEZE;
+// ids << COMPOSITE_HEAT;
+// ids << COMPOSITE_GLEAT;
+// ids << COMPOSITE_HELOW;
+// ids << COMPOSITE_REEZE;
+// ids << COMPOSITE_FRECT;
+// ids << COMPOSITE_FHYRD;
+
+
+
+
+ QStringList sdrPreservingIds;
+
+ sdrPreservingIds << COMPOSITE_DARKEN;
+ sdrPreservingIds << COMPOSITE_LIGHTEN;
+ sdrPreservingIds << COMPOSITE_ALLANON;
+ sdrPreservingIds << COMPOSITE_DISSOLVE;
+ sdrPreservingIds << COMPOSITE_OVER;
+ sdrPreservingIds << COMPOSITE_ALPHA_DARKEN;
+ sdrPreservingIds << COMPOSITE_DESTINATION_IN;
+ sdrPreservingIds << COMPOSITE_DESTINATION_ATOP;
+ sdrPreservingIds << COMPOSITE_ERASE;
+
+ QStringList strictSdrPreservingIds;
+ strictSdrPreservingIds << COMPOSITE_MULT;
+ strictSdrPreservingIds << COMPOSITE_DIFF;
+ strictSdrPreservingIds << COMPOSITE_SCREEN;
+ strictSdrPreservingIds << COMPOSITE_GREATER;
+
+
+ QStringList specialIds;
+ // unclamped non-preserving blendmodes
+ specialIds << COMPOSITE_ADD;
+ specialIds << COMPOSITE_SUBTRACT;
+ specialIds << COMPOSITE_INVERSE_SUBTRACT;
+ specialIds << COMPOSITE_DIVIDE;
+ specialIds << COMPOSITE_LUMINOSITY_SAI;
+ specialIds << COMPOSITE_EXCLUSION;
+ specialIds << COMPOSITE_NEGATION;
+ specialIds << COMPOSITE_GRAIN_MERGE;
+ specialIds << COMPOSITE_GRAIN_EXTRACT;
+
+ QTest::addColumn<QString>("id");
+ QTest::addColumn<TestFlags>("flags");
+
+ auto fixId = [] (QString id) {
+ id.replace(' ', '_');
+ return id;
+ };
+
+ std::vector<bool> isHDRValues({false, true});
+
+ Q_FOREACH (const QString &id, ids) {
+ Q_FOREACH (const bool isHDR, isHDRValues) {
+ TestFlags flags = isHDR ? HDR : None;
+ flags |= SrcCannotMakeNegative;
+
+ if (!isHDR) {
+ flags |= PreservesSdrRange;
+ } else {
+ // TODO: not all blendmodes currently preserve SDR range in HDR mode
+ //flags |= PreservesStrictSdrRange;
+ }
+ QTest::addRow("%s_%s", fixId(id).toLatin1().data(), isHDR ? "hdr" : "sdr") << id << flags;
+ }
+ }
+
+ Q_FOREACH (const QString &id, specialIds) {
+ TestFlags flags = None;
+ QTest::addRow("%s_%s", fixId(id).toLatin1().data(), "sdr") << id << flags;
+ }
+
+ Q_FOREACH (const QString &id, sdrPreservingIds) {
+ TestFlags flags = PreservesSdrRange;
+ QTest::addRow("%s_%s", fixId(id).toLatin1().data(), "sdr") << id << flags;
+ }
+
+ Q_FOREACH (const QString &id, strictSdrPreservingIds) {
+ TestFlags flags = PreservesStrictSdrRange;
+ QTest::addRow("%s_%s", fixId(id).toLatin1().data(), "sdr") << id << flags;
+ }
+
+ // both channels are clamped, sdr only!
+ QTest::addRow("%s_%s", COMPOSITE_SOFT_LIGHT_SVG.toLatin1().data(), "sdr") << COMPOSITE_SOFT_LIGHT_SVG << TestFlags(SrcCannotMakeNegative | PreservesSdrRange);
+ QTest::addRow("%s_%s", COMPOSITE_SOFT_LIGHT_PHOTOSHOP.toLatin1().data(), "sdr") << COMPOSITE_SOFT_LIGHT_PHOTOSHOP << TestFlags(SrcCannotMakeNegative | PreservesSdrRange);
+
+ QTest::addRow("%s_%s", COMPOSITE_HARD_MIX_PHOTOSHOP.toLatin1().data(), "sdr") << COMPOSITE_SOFT_LIGHT_SVG << TestFlags(SrcCannotMakeNegative | UseStrictSdrRange | PreservesStrictSdrRange);
+ QTest::addRow("%s_%s", COMPOSITE_HARD_MIX_SOFTER_PHOTOSHOP.toLatin1().data(), "sdr") << COMPOSITE_SOFT_LIGHT_SVG << TestFlags(SrcCannotMakeNegative | UseStrictSdrRange | PreservesStrictSdrRange);
+ QTest::addRow("%s_%s", COMPOSITE_ARC_TANGENT.toLatin1().data(), "sdr") << COMPOSITE_ARC_TANGENT << TestFlags(UseStrictSdrRange | PreservesStrictSdrRange);
+
+
+ // 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_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);
+ QTest::addRow("%s_%s", COMPOSITE_ADDITIVE_SUBTRACTIVE.toLatin1().data(), "sdr") << COMPOSITE_ADDITIVE_SUBTRACTIVE << TestFlags(SrcCannotMakeNegative | PreservesSdrRange);
+ QTest::addRow("%s_%s", COMPOSITE_GAMMA_DARK.toLatin1().data(), "sdr") << COMPOSITE_GAMMA_DARK << TestFlags(SrcCannotMakeNegative | PreservesStrictSdrRange);
+ QTest::addRow("%s_%s", COMPOSITE_GAMMA_LIGHT.toLatin1().data(), "sdr") << COMPOSITE_GAMMA_LIGHT << TestFlags(SrcCannotMakeNegative | PreservesSdrRange);
+ QTest::addRow("%s_%s", COMPOSITE_GAMMA_ILLUMINATION.toLatin1().data(), "sdr") << COMPOSITE_GAMMA_ILLUMINATION << TestFlags(SrcCannotMakeNegative | PreservesStrictSdrRange);
+}
+
SIMPLE_TEST_MAIN(TestCompositeOpInversion)
diff --git a/libs/pigment/tests/TestCompositeOpInversion.h b/libs/pigment/tests/TestCompositeOpInversion.h
index 162212cbd9..9e4bfffd5c 100644
--- a/libs/pigment/tests/TestCompositeOpInversion.h
+++ b/libs/pigment/tests/TestCompositeOpInversion.h
@@ -15,6 +15,11 @@ public:
private Q_SLOTS:
void test();
void test_data();
+
+ void testBurnInF32Mode();
+
+ void testFloatModes();
+ void testFloatModes_data();
};
#endif // TESTCOMPOSITEOPINVERSION_H
diff --git a/libs/ui/tool/strokes/KisMaskingBrushCompositeOpFactory.cpp b/libs/ui/tool/strokes/KisMaskingBrushCompositeOpFactory.cpp
index 255b350db7..967f510bd3 100644
--- a/libs/ui/tool/strokes/KisMaskingBrushCompositeOpFactory.cpp
+++ b/libs/ui/tool/strokes/KisMaskingBrushCompositeOpFactory.cpp
@@ -10,7 +10,6 @@
#include "kis_assert.h"
#include <KoCompositeOpRegistry.h>
-#include <KoCompositeOpFunctions.h>
#include "KisMaskingBrushCompositeOp.h"