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++
Implement other Paint Tool SAI blend modes
Carsten Hartenfelsbcf957b20d0a
diff --git a/libs/pigment/KoCompositeOpRegistry.cpp b/libs/pigment/KoCompositeOpRegistry.cppindex e667bfadc0..8d0025041a 100644--- a/libs/pigment/KoCompositeOpRegistry.cpp+++ b/libs/pigment/KoCompositeOpRegistry.cpp@@ -66,6 +66,8 @@ KoCompositeOpRegistry::KoCompositeOpRegistry()m_map.insert(m_categories[2], KoID(COMPOSITE_SHADE_IFS_ILLUSIONS, i18nc("Blending mode - Shade (IFS Illusions)", "Shade (IFS Illusions)")));m_map.insert(m_categories[2], KoID(COMPOSITE_FOG_DARKEN_IFS_ILLUSIONS, i18nc("Blending mode - Fog Darken (IFS Illusions)", "Fog Darken (IFS Illusions)")));m_map.insert(m_categories[2], KoID(COMPOSITE_EASY_BURN , i18nc("Blending mode - Easy Burn", "Easy Burn")));+ m_map.insert(m_categories[2], KoID(COMPOSITE_BURN_SAI , i18nc("Blending mode - Burn (SAI)", "Burn (SAI)")));+ m_map.insert(m_categories[2], KoID(COMPOSITE_SHADE_SAI , i18nc("Blending mode - Shade (SAI)", "Shade (SAI)")));m_map.insert(m_categories[3], KoID(COMPOSITE_DODGE , i18nc("Blending mode - Color Dodge", "Color Dodge")));m_map.insert(m_categories[3], KoID(COMPOSITE_DODGE_HDR , i18nc("Blending mode - Color Dodge HDR", "Color Dodge HDR")));@@ -92,6 +94,7 @@ KoCompositeOpRegistry::KoCompositeOpRegistry()m_map.insert(m_categories[3], KoID(COMPOSITE_FOG_LIGHTEN_IFS_ILLUSIONS, i18nc("Blending mode - Fog Lighten (IFS Illusions)", "Fog Lighten (IFS Illusions)")));m_map.insert(m_categories[3], KoID(COMPOSITE_EASY_DODGE , i18nc("Blending mode - Easy Dodge", "Easy Dodge")));m_map.insert(m_categories[3], KoID(COMPOSITE_LUMINOSITY_SAI , i18nc("Blending mode - Luminosity/Shine (SAI)", "Luminosity/Shine (SAI)")));+ m_map.insert(m_categories[3], KoID(COMPOSITE_DODGE_SAI , i18nc("Blending mode - Dodge (SAI)", "Dodge (SAI)")));m_map.insert(m_categories[4], KoID(COMPOSITE_MOD , i18nc("Blending mode - Modulo", "Modulo")));m_map.insert(m_categories[4], KoID(COMPOSITE_MOD_CON , i18nc("Blending mode - Modulo - Continuous", "Modulo - Continuous")));@@ -101,6 +104,7 @@ KoCompositeOpRegistry::KoCompositeOpRegistry()m_map.insert(m_categories[4], KoID(COMPOSITE_MODULO_SHIFT_CON , i18nc("Blending mode - Modulo Shift - Continuous", "Modulo Shift - Continuous")));m_map.insert(m_categories[5], KoID(COMPOSITE_DIFF , i18nc("Blending mode - Difference", "Difference")));+ m_map.insert(m_categories[5], KoID(COMPOSITE_DIFFERENCE_SAI , i18nc("Blending mode - Difference (SAI)", "Difference (SAI)")));m_map.insert(m_categories[5], KoID(COMPOSITE_EQUIVALENCE , i18nc("Blending mode - Equivalence", "Equivalence")));m_map.insert(m_categories[5], KoID(COMPOSITE_ADDITIVE_SUBTRACTIVE , i18nc("Blending mode - Additive Subtractive", "Additive Subtractive")));m_map.insert(m_categories[5], KoID(COMPOSITE_EXCLUSION , i18nc("Blending mode - Exclusion", "Exclusion")));@@ -121,6 +125,7 @@ KoCompositeOpRegistry::KoCompositeOpRegistry()m_map.insert(m_categories[6], KoID(COMPOSITE_HARD_MIX_HDR , i18nc("Blending mode - Hard Mix HDR", "Hard Mix HDR")));m_map.insert(m_categories[6], KoID(COMPOSITE_HARD_MIX_PHOTOSHOP, i18nc("Blending mode - Hard Mix (Photoshop)", "Hard Mix (Photoshop)")));m_map.insert(m_categories[6], KoID(COMPOSITE_HARD_MIX_SOFTER_PHOTOSHOP, i18nc("Blending mode - Hard Mix Softer (Photoshop)", "Hard Mix Softer (Photoshop)")));+ m_map.insert(m_categories[6], KoID(COMPOSITE_HARD_MIX_SAI , i18nc("Blending mode - Hard Mix (SAI)", "Hard Mix (SAI)")));m_map.insert(m_categories[6], KoID(COMPOSITE_GRAIN_MERGE , i18nc("Blending mode - Grain Merge", "Grain Merge")));m_map.insert(m_categories[6], KoID(COMPOSITE_GRAIN_EXTRACT , i18nc("Blending mode - Grain Extract", "Grain Extract")));m_map.insert(m_categories[6], KoID(COMPOSITE_PARALLEL , i18nc("Blending mode - Parallel", "Parallel")));@@ -136,6 +141,8 @@ KoCompositeOpRegistry::KoCompositeOpRegistry()m_map.insert(m_categories[6], KoID(COMPOSITE_PENUMBRAB , i18nc("Blending mode - Penumbra B", "Penumbra B")));m_map.insert(m_categories[6], KoID(COMPOSITE_PENUMBRAC , i18nc("Blending mode - Penumbra C", "Penumbra C")));m_map.insert(m_categories[6], KoID(COMPOSITE_PENUMBRAD , i18nc("Blending mode - Penumbra D", "Penumbra D")));+ m_map.insert(m_categories[6], KoID(COMPOSITE_BURN_DODGE_SAI , i18nc("Blending mode - Burn/Dodge (SAI)", "Burn/Dodge (SAI)")));+ m_map.insert(m_categories[6], KoID(COMPOSITE_SHADE_SHINE_SAI , i18nc("Blending mode - Shade/Shine (SAI)", "Shade/Shine (SAI)")));m_map.insert(m_categories[7], KoID(COMPOSITE_BUMPMAP , i18nc("Blending mode - Bumpmap", "Bumpmap")));m_map.insert(m_categories[7], KoID(COMPOSITE_COMBINE_NORMAL, i18nc("Blending mode - Combine Normal Map", "Combine Normal Map")));diff --git a/libs/pigment/KoCompositeOpRegistry.h b/libs/pigment/KoCompositeOpRegistry.hindex f312d9ca55..88f044a189 100644--- a/libs/pigment/KoCompositeOpRegistry.h+++ b/libs/pigment/KoCompositeOpRegistry.h@@ -114,6 +114,13 @@ const QString COMPOSITE_TINT_IFS_ILLUSIONS = "tint_ifs_illusions";const QString COMPOSITE_FOG_LIGHTEN_IFS_ILLUSIONS = "fog_lighten_ifs_illusions";const QString COMPOSITE_EASY_DODGE = "easy dodge";const QString COMPOSITE_LUMINOSITY_SAI = "luminosity_sai";+const QString COMPOSITE_DODGE_SAI = "dodge_sai";+const QString COMPOSITE_BURN_SAI = "burn_sai";+const QString COMPOSITE_SHADE_SAI = "shade_sai";+const QString COMPOSITE_BURN_DODGE_SAI = "burn_dodge_sai";+const QString COMPOSITE_SHADE_SHINE_SAI = "shade_shine_sai";+const QString COMPOSITE_HARD_MIX_SAI = "hard_mix_sai";+const QString COMPOSITE_DIFFERENCE_SAI = "difference_sai";const QString COMPOSITE_HUE = "hue";diff --git a/libs/pigment/compositeops/KoColorSpaceBlendingPolicy.cpp b/libs/pigment/compositeops/KoColorSpaceBlendingPolicy.cppindex e152718474..48b936a51c 100644--- a/libs/pigment/compositeops/KoColorSpaceBlendingPolicy.cpp+++ b/libs/pigment/compositeops/KoColorSpaceBlendingPolicy.cpp@@ -127,6 +127,13 @@ QStringList subtractiveBlendingModesInCmyk()ids << COMPOSITE_FHYRD;ids << COMPOSITE_LUMINOSITY_SAI;+ ids << COMPOSITE_DODGE_SAI;+ ids << COMPOSITE_BURN_SAI;+ ids << COMPOSITE_SHADE_SAI;+ ids << COMPOSITE_BURN_DODGE_SAI;+ ids << COMPOSITE_SHADE_SHINE_SAI;+ ids << COMPOSITE_HARD_MIX_SAI;+ ids << COMPOSITE_DIFFERENCE_SAI;return ids;}diff --git a/libs/pigment/compositeops/KoCompositeOpFunctions.h b/libs/pigment/compositeops/KoCompositeOpFunctions.hindex 095efc052c..417a998a1b 100644--- a/libs/pigment/compositeops/KoCompositeOpFunctions.h+++ b/libs/pigment/compositeops/KoCompositeOpFunctions.h@@ -1341,6 +1341,109 @@ inline T cfAdditionSAI(T src, T dst, T srcAlpha)Q_UNUSED(srcAlpha);return cfAddition(src, dst);}++template<class T>+inline T cfColorDodgeSAI(T src, T dst, T srcAlpha)+{+ using namespace Arithmetic;+ Q_UNUSED(srcAlpha);++ if (dst <= zeroValue<T>()) {+ return zeroValue<T>();+ }++ T denom = inv(src);+ if (denom <= zeroValue<T>()) {+ return unitValue<T>();+ }++ return clampToSDR<T>(div(dst, denom));+}++template<class T>+inline T cfColorBurnSAI(T src, T dst, T srcAlpha)+{+ using namespace Arithmetic;+ typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;++ if (dst >= unitValue<T>()) {+ return unitValue<T>();+ }++ if (dst <= zeroValue<T>()) {+ return zeroValue<T>();+ }++ composite_type denom = composite_type(unitValue<T>()) - srcAlpha + src;+ if (denom <= composite_type(0)) {+ return zeroValue<T>();+ }++ composite_type burned = div(inv(dst), T(denom));+ return inv(clampToSDR<T>(burned));+}++template<class T>+inline T cfLinearBurnSAI(T src, T dst, T srcAlpha)+{+ using namespace Arithmetic;+ typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;++ return clampToSDR<T>(composite_type(src) + dst - srcAlpha);+}++template<class T>+inline T cfVividLightSAI(T src, T dst, T srcAlpha)+{+ typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;++ composite_type src2 = composite_type(src) + src;++ if (src2 <= composite_type(srcAlpha)) {+ return cfColorBurnSAI<T>(T(src2), dst, srcAlpha);+ } else {+ return cfColorDodgeSAI<T>(T(src2 - srcAlpha), dst, srcAlpha);+ }+}++template<class T>+inline T cfLinearLightSAI(T src, T dst, T srcAlpha)+{+ using namespace Arithmetic;+ typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;++ return clampToSDR<T>(composite_type(src) + src + dst - srcAlpha);+}++template<class T>+inline T cfHardMixSAI(T src, T dst, T srcAlpha)+{+ using namespace Arithmetic;+ typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;++ composite_type c = composite_type(src) + dst - srcAlpha;+ if (c < composite_type(0)) {+ return zeroValue<T>();+ }++ composite_type denom = composite_type(unitValue<T>()) - srcAlpha;+ if (denom <= composite_type(0)) {+ return unitValue<T>();+ }++ return clampToSDR<T>(div(clampToSDR<T>(c), T(denom)));+}++template<class T>+inline T cfDifferenceSAI(T src, T dst, T srcAlpha)+{+ using namespace Arithmetic;+ typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;+ Q_UNUSED(srcAlpha);++ composite_type d = src > dst ? composite_type(src) - dst : composite_type(dst) - src;+ return clampToSDR<T>(d);+}// SPDX-SnippetEnddiff --git a/libs/pigment/compositeops/KoCompositeOpGeneric.h b/libs/pigment/compositeops/KoCompositeOpGeneric.hindex bb43e14b35..864d21e6c5 100644--- a/libs/pigment/compositeops/KoCompositeOpGeneric.h+++ b/libs/pigment/compositeops/KoCompositeOpGeneric.h@@ -338,6 +338,11 @@ public:// SPDX-License-Identifier: MIT// SPDX-SnippetName: SAI blending logic based on psd-export by cromachinachannels_type srcInBlendSpace = BlendingPolicy::toAdditiveSpace(src[i]);+ if constexpr (!std::numeric_limits<channels_type>::is_integer) {+ if (srcInBlendSpace < zeroValue<channels_type>()) {+ srcInBlendSpace = zeroValue<channels_type>();+ }+ }channels_type dstInBlendSpace = BlendingPolicy::toAdditiveSpace(dst[i]);channels_type srcPremultiplied = mul(srcInBlendSpace, srcAlpha);channels_type resultInBlendSpace = compositeFunc(srcPremultiplied, dstInBlendSpace, srcAlpha);@@ -358,6 +363,11 @@ public:// SPDX-License-Identifier: MIT// SPDX-SnippetName: SAI blending logic based on psd-export by cromachinachannels_type srcInBlendSpace = BlendingPolicy::toAdditiveSpace(src[i]);+ if constexpr (!std::numeric_limits<channels_type>::is_integer) {+ if (srcInBlendSpace < zeroValue<channels_type>()) {+ srcInBlendSpace = zeroValue<channels_type>();+ }+ }channels_type dstInBlendSpace = BlendingPolicy::toAdditiveSpace(dst[i]);channels_type srcPremultiplied = mul(srcInBlendSpace, srcAlpha);channels_type resultPremultiplied =diff --git a/libs/pigment/compositeops/KoCompositeOps.h b/libs/pigment/compositeops/KoCompositeOps.hindex c1f089bf5a..2ad288a506 100644--- a/libs/pigment/compositeops/KoCompositeOps.h+++ b/libs/pigment/compositeops/KoCompositeOps.h@@ -429,6 +429,13 @@ struct AddSAIOps<Traits, true>static void add(KoColorSpace* cs){add<&cfAdditionSAI<Arg>>(cs, COMPOSITE_LUMINOSITY_SAI, KoCompositeOp::categoryLight());+ add<&cfColorDodgeSAI<Arg>>(cs, COMPOSITE_DODGE_SAI, KoCompositeOp::categoryLight());+ add<&cfColorBurnSAI<Arg>>(cs, COMPOSITE_BURN_SAI, KoCompositeOp::categoryDark());+ add<&cfLinearBurnSAI<Arg>>(cs, COMPOSITE_SHADE_SAI, KoCompositeOp::categoryDark());+ add<&cfVividLightSAI<Arg>>(cs, COMPOSITE_BURN_DODGE_SAI, KoCompositeOp::categoryMix());+ add<&cfLinearLightSAI<Arg>>(cs, COMPOSITE_SHADE_SHINE_SAI, KoCompositeOp::categoryMix());+ add<&cfHardMixSAI<Arg>>(cs, COMPOSITE_HARD_MIX_SAI, KoCompositeOp::categoryMix());+ add<&cfDifferenceSAI<Arg>>(cs, COMPOSITE_DIFFERENCE_SAI, KoCompositeOp::categoryNegative());}diff --git a/libs/pigment/tests/TestCompositeOpInversion.cpp b/libs/pigment/tests/TestCompositeOpInversion.cppindex 557aa00dd7..ea15dd6028 100644--- a/libs/pigment/tests/TestCompositeOpInversion.cpp+++ b/libs/pigment/tests/TestCompositeOpInversion.cpp@@ -219,6 +219,13 @@ void TestCompositeOpInversion::test_data()ids << COMPOSITE_DISSOLVE;ids << COMPOSITE_LUMINOSITY_SAI;+ ids << COMPOSITE_DODGE_SAI;+ ids << COMPOSITE_BURN_SAI;+ ids << COMPOSITE_SHADE_SAI;+ ids << COMPOSITE_BURN_DODGE_SAI;+ ids << COMPOSITE_SHADE_SHINE_SAI;+ ids << COMPOSITE_HARD_MIX_SAI;+ ids << COMPOSITE_DIFFERENCE_SAI;QTest::addColumn<QString>("id");@@ -525,6 +532,23 @@ std::vector<std::pair<QString, TestFlags>> generateCompositeOpIdSet()addUnboundedRangeOp(COMPOSITE_LUMINOSITY_SAI);+ result.emplace_back(COMPOSITE_DODGE_SAI, HDR | SrcCannotMakeNegative | PositivePreserveUnstable);+ result.emplace_back(COMPOSITE_DODGE_SAI, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);++ addSdrPreservingOp(COMPOSITE_BURN_SAI);+ addSdrPreservingHdrOp(COMPOSITE_SHADE_SAI);++ result.emplace_back(COMPOSITE_BURN_DODGE_SAI, HDR | SrcCannotMakeNegative | PositivePreserveUnstable);+ result.emplace_back(COMPOSITE_BURN_DODGE_SAI, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);++ addSdrPreservingOp(COMPOSITE_SHADE_SHINE_SAI);++ // Hard-mix is the only stable positive-preserving mode+ result.emplace_back(COMPOSITE_HARD_MIX_SAI, HDR | SrcCannotMakeNegative | PositivePreserveStable);+ result.emplace_back(COMPOSITE_HARD_MIX_SAI, SrcCannotMakeNegative | SdrRangePreserveStable | PositivePreserveStable);++ addSdrPreservingOp(COMPOSITE_DIFFERENCE_SAI);+result.emplace_back(COMPOSITE_DIVIDE, PositivePreserveUnstable);@@ -879,10 +903,10 @@ void TestCompositeOpInversion::testF32vsU16ConsistencyInSDR()qAbs(resultColorValueF - resultColorValueU) > tolerance) {/**- * HARD_MIX_PHOTOSHOP is not contiguous near the unit sum point,- * so we should just skip this test case+ * HARD_MIX_PHOTOSHOP and _SAI are not contiguous near the+ * unit sum point, so we should just skip this test case*/- if (id == COMPOSITE_HARD_MIX_PHOTOSHOP &&+ if ((id == COMPOSITE_HARD_MIX_PHOTOSHOP || id == COMPOSITE_HARD_MIX_SAI) &&Arithmetic::isUnitValueFuzzy<float>(float(srcColorValueF) + float(dstColorValueF))) {continue;}@@ -901,27 +925,27 @@ void TestCompositeOpInversion::testF32vsU16ConsistencyInSDR()}}- if (id == COMPOSITE_DODGE) {+ if (id == COMPOSITE_DODGE || id == COMPOSITE_DODGE_SAI) {if (Arithmetic::isUnitValueClampedFuzzy<float>(srcColorValueF) &&Arithmetic::isZeroValueClampedFuzzy<float>(dstColorValueF)) {continue;}}- if (id == COMPOSITE_DODGE && flags.testFlag(HDR)) {+ if ((id == COMPOSITE_DODGE || id == COMPOSITE_DODGE_SAI) && flags.testFlag(HDR)) {if (Arithmetic::isUnitValueClampedFuzzy<float>(srcColorValueF)) {continue;}}- if (id == COMPOSITE_BURN) {+ if (id == COMPOSITE_BURN || id == COMPOSITE_BURN_SAI) {if (Arithmetic::isZeroValueClampedFuzzy<float>(srcColorValueF) &&Arithmetic::isUnitValueClampedFuzzy<float>(dstColorValueF)) {continue;}}- if (id == COMPOSITE_VIVID_LIGHT) {+ if (id == COMPOSITE_VIVID_LIGHT || id == COMPOSITE_BURN_DODGE_SAI) {if (Arithmetic::isUnitValueClampedFuzzy<float>(srcColorValueF) ||Arithmetic::isZeroValueClampedFuzzy<float>(srcColorValueF)) {continue;