/* * SPDX-FileCopyrightText: 2017 Dmitry Kazakov * SPDX-FileCopyrightText: 2021 Deif Lou * * SPDX-License-Identifier: GPL-2.0-or-later */ #ifndef KISMASKINGBRUSHCOMPOSITEOP_H #define KISMASKINGBRUSHCOMPOSITEOP_H #include #ifdef HAVE_OPENEXR #include "half.h" #endif #include #include #include #include #include #include "KisMaskingBrushCompositeOpBase.h" enum KisMaskingBrushCompositeFuncTypes { KIS_MASKING_BRUSH_COMPOSITE_MULT, KIS_MASKING_BRUSH_COMPOSITE_DARKEN, KIS_MASKING_BRUSH_COMPOSITE_OVERLAY, KIS_MASKING_BRUSH_COMPOSITE_DODGE, KIS_MASKING_BRUSH_COMPOSITE_BURN, KIS_MASKING_BRUSH_COMPOSITE_LINEAR_BURN, KIS_MASKING_BRUSH_COMPOSITE_LINEAR_DODGE, KIS_MASKING_BRUSH_COMPOSITE_HARD_MIX_PHOTOSHOP, KIS_MASKING_BRUSH_COMPOSITE_HARD_MIX_SOFTER_PHOTOSHOP, KIS_MASKING_BRUSH_COMPOSITE_SUBTRACT, KIS_MASKING_BRUSH_COMPOSITE_HEIGHT, KIS_MASKING_BRUSH_COMPOSITE_LINEAR_HEIGHT, KIS_MASKING_BRUSH_COMPOSITE_HEIGHT_PHOTOSHOP, KIS_MASKING_BRUSH_COMPOSITE_LINEAR_HEIGHT_PHOTOSHOP }; namespace KisMaskingBrushCompositeDetail { template struct StrengthCompositeFunctionBase { const channels_type strength; StrengthCompositeFunctionBase(qreal strength) : strength(KoColorSpaceMaths::scaleToA(strength)) {} }; template struct CompositeFunction; template struct CompositeFunction { channels_type apply(channels_type src, channels_type dst) { return cfMultiply(src, dst); } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(strength) {} channels_type apply(channels_type src, channels_type dst) { return Arithmetic::mul(src, dst, StrengthCompositeFunctionBase::strength); } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { const channels_type invertedStrength; CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(strength) , invertedStrength(Arithmetic::inv(StrengthCompositeFunctionBase::strength)) {} channels_type apply(channels_type src, channels_type dst) { return Arithmetic::mul(Arithmetic::unionShapeOpacity(src, invertedStrength), dst); } }; template struct CompositeFunction { channels_type apply(channels_type src, channels_type dst) { return cfDarkenOnly(src, dst); } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(strength) {} channels_type apply(channels_type src, channels_type dst) { return cfDarkenOnly(src, Arithmetic::mul(dst, StrengthCompositeFunctionBase::strength)); } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { const channels_type invertedStrength; CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(strength) , invertedStrength(Arithmetic::inv(StrengthCompositeFunctionBase::strength)) {} channels_type apply(channels_type src, channels_type dst) { return cfDarkenOnly(Arithmetic::unionShapeOpacity(src, invertedStrength), dst); } }; template struct CompositeFunction { channels_type apply(channels_type src, channels_type dst) { return CFOverlay::composeChannel(src, dst); } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(strength) {} channels_type apply(channels_type src, channels_type dst) { return CFOverlay::composeChannel(src, Arithmetic::mul(dst, StrengthCompositeFunctionBase::strength)); } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { const channels_type invertedStrength; CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(strength) , invertedStrength(Arithmetic::inv(StrengthCompositeFunctionBase::strength)) {} channels_type apply(channels_type src, channels_type dst) { return CFOverlay::composeChannel(Arithmetic::unionShapeOpacity(src, invertedStrength), dst); } }; /** * A special Color Dodge variant for alpha channel. * * The meaning of alpha channel is a bit different from the one in color. * Color dodge can quickly make the values higher than 1 or less than 0 so, * contrary to the color values case, we should clamp to the unit range */ template inline T colorDodgeAlphaHelper(T src, T dst) { using composite_type = typename KoColorSpaceMathsTraits::compositetype; using namespace Arithmetic; // 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. // For alpha values, the result should be clamped to the unit range, // contrary to the color version, where the values should be clamped to // the min/max range. // 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 (isUnitValueFuzzy(src)) { return isZeroValueFuzzy(dst) ? zeroValue() : unitValue(); } return kisBoundFast(composite_type(KoColorSpaceMathsTraits::zeroValue), div(dst, inv(src)), composite_type(KoColorSpaceMathsTraits::unitValue)); } // Integer version of color dodge alpha template inline typename std::enable_if::is_integer, T>::type colorDodgeAlpha(T src, T dst) { return colorDodgeAlphaHelper(src, dst); } // Floating point version of color dodge alpha template inline typename std::enable_if::is_integer, T>::type colorDodgeAlpha(T src, T dst) { const T result = colorDodgeAlphaHelper(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::unitValue; } template struct CompositeFunction { channels_type apply(channels_type src, channels_type dst) { return colorDodgeAlpha(src, dst); } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(strength) {} channels_type apply(channels_type src, channels_type dst) { if constexpr (use_soft_texturing) { return colorDodgeAlpha(Arithmetic::mul(src, StrengthCompositeFunctionBase::strength), dst); } else { return colorDodgeAlpha(src, Arithmetic::mul(dst, StrengthCompositeFunctionBase::strength)); } } }; /** * A special Color Burn variant for alpha channel. * * The meaning of alpha channel is a bit different from the one in color. * Color burn can quickly make the values less than 0 so, * contrary to the color values case, we should clamp to the unit range */ template inline T colorBurnAlphaHelper(T src, T dst) { using composite_type = typename KoColorSpaceMathsTraits::compositetype; using namespace Arithmetic; // Handle the case where the denominator is 0. See color dodge for a // detailed explanation if(isZeroValueFuzzy(src)) { return isUnitValueFuzzy(dst) ? zeroValue() : unitValue(); } return kisBoundFast(composite_type(KoColorSpaceMathsTraits::zeroValue), div(inv(dst), src), composite_type(KoColorSpaceMathsTraits::unitValue)); } template inline T colorBurnAlpha(T src, T dst) { using namespace Arithmetic; using namespace KoCompositeOpClampPolicy; return CFColorBurn::composeChannel(src, dst); } template struct CompositeFunction { channels_type apply(channels_type src, channels_type dst) { return colorBurnAlpha(src, dst); } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(strength) {} channels_type apply(channels_type src, channels_type dst) { return colorBurnAlpha(src, Arithmetic::mul(dst, StrengthCompositeFunctionBase::strength)); } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { const channels_type invertedStrength; CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(strength) , invertedStrength(Arithmetic::inv(StrengthCompositeFunctionBase::strength)) {} channels_type apply(channels_type src, channels_type dst) { return colorBurnAlpha(Arithmetic::unionShapeOpacity(src, invertedStrength), dst); } }; /** * A special Linear Dodge variant for alpha channel. * * The meaning of alpha channel is a bit different from the one in color. If * alpha channel of the destination is totally null, we should not try * to resurrect its contents from ashes :) */ template struct CompositeFunction { channels_type apply(channels_type src, channels_type dst) { using composite_type = typename KoColorSpaceMathsTraits::compositetype; using namespace Arithmetic; if (isZeroValueFuzzy(dst)) { return zeroValue(); } return qMin(composite_type(src) + dst, composite_type(unitValue())); } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(strength) {} channels_type apply(channels_type src, channels_type dst) { using composite_type = typename KoColorSpaceMathsTraits::compositetype; using namespace Arithmetic; if (isZeroValueFuzzy(dst)) { return zeroValue(); } if constexpr (use_soft_texturing) { return qMin(composite_type(mul(src, StrengthCompositeFunctionBase::strength) + dst), composite_type(unitValue())); } else { return qMin(composite_type(src) + mul(dst, StrengthCompositeFunctionBase::strength), composite_type(unitValue())); } } }; /** * A special Linear Burn variant for alpha channel * * The meaning of alpha channel is a bit different from the one in color. We should * clamp the values around [zero, max] only to avoid the brush to **erase** the content * of the layer below */ template struct CompositeFunction { channels_type apply(channels_type src, channels_type dst) { using namespace Arithmetic; using composite_type = typename KoColorSpaceMathsTraits::compositetype; return qMax(composite_type(KoColorSpaceMathsTraits::zeroValue), composite_type(src) + dst - unitValue()); } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(strength) {} channels_type apply(channels_type src, channels_type dst) { using namespace Arithmetic; using composite_type = typename KoColorSpaceMathsTraits::compositetype; return qMax(composite_type(zeroValue()), composite_type(src) + mul(dst, StrengthCompositeFunctionBase::strength) - unitValue()); } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { const channels_type invertedStrength; CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(strength) , invertedStrength(Arithmetic::inv(StrengthCompositeFunctionBase::strength)) {} channels_type apply(channels_type src, channels_type dst) { using namespace Arithmetic; using composite_type = typename KoColorSpaceMathsTraits::compositetype; return qMax(composite_type(zeroValue()), composite_type(unionShapeOpacity(src, invertedStrength) + dst - unitValue())); } }; template struct CompositeFunction { channels_type apply(channels_type src, channels_type dst) { return CFHardMixPhotoshop::composeChannel(src, dst); } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(strength) {} channels_type apply(channels_type src, channels_type dst) { return CFHardMixPhotoshop::composeChannel(src, Arithmetic::mul(dst, StrengthCompositeFunctionBase::strength)); } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { const channels_type invertedStrength; CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(strength) , invertedStrength(Arithmetic::inv(StrengthCompositeFunctionBase::strength)) {} channels_type apply(channels_type src, channels_type dst) { using namespace Arithmetic; return mul(CFHardMixPhotoshop::composeChannel(unionShapeOpacity(src, invertedStrength), dst), unionShapeOpacity(dst, StrengthCompositeFunctionBase::strength)); } }; /** * A special Hard Mix Softer variant for alpha channel * * The meaning of alpha channel is a bit different from the one in color. * We have to clamp the values to the unit range */ template inline T hardMixSofterPhotoshopAlpha(T src, T dst) { using namespace Arithmetic; typedef typename KoColorSpaceMathsTraits::compositetype composite_type; const composite_type srcScaleFactor = static_cast(2); const composite_type dstScaleFactor = static_cast(3); return kisBoundFast(composite_type(KoColorSpaceMathsTraits::zeroValue), dstScaleFactor * dst - srcScaleFactor * inv(src), composite_type(KoColorSpaceMathsTraits::unitValue)); } template struct CompositeFunction { channels_type apply(channels_type src, channels_type dst) { return hardMixSofterPhotoshopAlpha(src, dst); } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(strength) {} channels_type apply(channels_type src, channels_type dst) { if constexpr (use_soft_texturing) { return hardMixSofterPhotoshopAlpha(Arithmetic::mul(src, StrengthCompositeFunctionBase::strength), dst); } else { return hardMixSofterPhotoshopAlpha(src, Arithmetic::mul(dst, StrengthCompositeFunctionBase::strength)); } } }; /** * A special Subtract variant for alpha channel. * * The meaning of alpha channel is a bit different from the one in color. * If the result of the subtraction becomes negative, we should clamp it * to the unit range. Otherwise, the layer may have negative alpha channel, * which generates funny artifacts :) See bug 424210. */ template struct CompositeFunction { channels_type apply(channels_type src, channels_type dst) { using composite_type = typename KoColorSpaceMathsTraits::compositetype; using namespace Arithmetic; return qMax(composite_type(KoColorSpaceMathsTraits::zeroValue), composite_type(dst) - src); } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { const channels_type invertedStrength; CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(strength) , invertedStrength(Arithmetic::inv(StrengthCompositeFunctionBase::strength)) {} channels_type apply(channels_type src, channels_type dst) { using composite_type = typename KoColorSpaceMathsTraits::compositetype; using namespace Arithmetic; return qMax(composite_type(zeroValue()), composite_type(dst) - (composite_type(src) + invertedStrength)); } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(strength) {} channels_type apply(channels_type src, channels_type dst) { using composite_type = typename KoColorSpaceMathsTraits::compositetype; using namespace Arithmetic; return qMax(composite_type(KoColorSpaceMathsTraits::zeroValue), composite_type(dst) - mul(src, StrengthCompositeFunctionBase::strength)); } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { const channels_type invertedStrength; CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(0.99 * strength) , invertedStrength(Arithmetic::inv(StrengthCompositeFunctionBase::strength)) {} channels_type apply(channels_type src, channels_type dst) { using composite_type = typename KoColorSpaceMathsTraits::compositetype; using namespace Arithmetic; if constexpr (use_soft_texturing) { return kisBoundFast(composite_type(zeroValue()), div(dst, invertedStrength) - mul(src, StrengthCompositeFunctionBase::strength), composite_type(unitValue())); } else { return kisBoundFast(composite_type(zeroValue()), div(dst, invertedStrength) - (composite_type(src) + invertedStrength), composite_type(unitValue())); } } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { const channels_type invertedStrength; CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(0.99 * strength) , invertedStrength(Arithmetic::inv(StrengthCompositeFunctionBase::strength)) {} channels_type apply(channels_type src, channels_type dst) { using composite_type = typename KoColorSpaceMathsTraits::compositetype; using namespace Arithmetic; if constexpr (use_soft_texturing) { const composite_type modifiedDst = div(dst, invertedStrength); const channels_type srcTimesStrength = mul(src, StrengthCompositeFunctionBase::strength); const composite_type multiply = modifiedDst * inv(srcTimesStrength) / unitValue(); const composite_type height = modifiedDst - srcTimesStrength; return kisBoundFast(composite_type(zeroValue()), qMax(multiply, height), composite_type(unitValue())); } else { const composite_type modifiedDst = div(dst, invertedStrength) - invertedStrength; const composite_type multiply = modifiedDst * inv(src) / unitValue(); const composite_type height = modifiedDst - src; return kisBoundFast(composite_type(zeroValue()), qMax(multiply, height), composite_type(unitValue())); } } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { using composite_type = typename KoColorSpaceMathsTraits::compositetype; const composite_type weight; CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(strength) , weight(composite_type(10) * StrengthCompositeFunctionBase::strength) {} channels_type apply(channels_type src, channels_type dst) { using namespace Arithmetic; return kisBoundFast(composite_type(zeroValue()), dst * weight / unitValue() - src, composite_type(unitValue())); } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { using composite_type = typename KoColorSpaceMathsTraits::compositetype; const composite_type weight; CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(strength) , weight(composite_type(9) * StrengthCompositeFunctionBase::strength) {} channels_type apply(channels_type src, channels_type dst) { using namespace Arithmetic; return kisBoundFast(composite_type(zeroValue()), composite_type(dst) + dst * weight / unitValue() - mul(src, StrengthCompositeFunctionBase::strength), composite_type(unitValue())); } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { using composite_type = typename KoColorSpaceMathsTraits::compositetype; const composite_type weight; CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(strength) , weight(composite_type(10) * StrengthCompositeFunctionBase::strength) {} channels_type apply(channels_type src, channels_type dst) { using namespace Arithmetic; const composite_type modifiedDst = dst * weight / unitValue(); const composite_type multiply = inv(src) * modifiedDst / unitValue(); const composite_type height = modifiedDst - src; return kisBoundFast(composite_type(zeroValue()), qMax(multiply, height), composite_type(unitValue())); } }; template struct CompositeFunction : public StrengthCompositeFunctionBase { using composite_type = typename KoColorSpaceMathsTraits::compositetype; const composite_type weight; CompositeFunction(qreal strength) : StrengthCompositeFunctionBase(strength) , weight(composite_type(9) * StrengthCompositeFunctionBase::strength) {} channels_type apply(channels_type src, channels_type dst) { using namespace Arithmetic; const composite_type modifiedDst = dst + dst * weight / unitValue(); const channels_type srcTimesStrength = mul(src, StrengthCompositeFunctionBase::strength); const composite_type multiply = modifiedDst * inv(srcTimesStrength) / unitValue(); const composite_type height = modifiedDst - srcTimesStrength; return kisBoundFast(composite_type(zeroValue()), qMax(multiply, height), composite_type(unitValue())); } }; } template class KisMaskingBrushCompositeOp : public KisMaskingBrushCompositeOpBase { public: using MaskPixel = typename std::conditional::type; template ::type> KisMaskingBrushCompositeOp(int dstPixelSize, int dstAlphaOffset) : m_dstPixelSize(dstPixelSize) , m_dstAlphaOffset(dstAlphaOffset) {} template ::type> KisMaskingBrushCompositeOp(int dstPixelSize, int dstAlphaOffset, qreal strength) : m_dstPixelSize(dstPixelSize) , m_dstAlphaOffset(dstAlphaOffset) , m_compositeFunction(strength) {} void composite(const quint8 *srcRowStart, int srcRowStride, quint8 *dstRowStart, int dstRowStride, int columns, int rows) override { dstRowStart += m_dstAlphaOffset; for (int y = 0; y < rows; y++) { const quint8 *srcPtr = srcRowStart; quint8 *dstPtr = dstRowStart; for (int x = 0; x < columns; x++) { const MaskPixel *srcDataPtr = reinterpret_cast(srcPtr); const quint8 mask = preprocessMask(srcDataPtr); const channels_type maskScaled = KoColorSpaceMaths::scaleToA(mask); channels_type *dstDataPtr = reinterpret_cast(dstPtr); *dstDataPtr = m_compositeFunction.apply(maskScaled, *dstDataPtr); srcPtr += sizeof(MaskPixel); dstPtr += m_dstPixelSize; } srcRowStart += srcRowStride; dstRowStart += dstRowStride; } } private: inline quint8 preprocessMask(const quint8 *pixel) { return *pixel; } inline quint8 preprocessMask(const KoGrayU8Traits::Pixel *pixel) { return KoColorSpaceMaths::multiply(pixel->gray, pixel->alpha); } private: int m_dstPixelSize; int m_dstAlphaOffset; KisMaskingBrushCompositeDetail::CompositeFunction m_compositeFunction; }; #endif // KISMASKINGBRUSHCOMPOSITEOP_H