Krita Source Code Documentation
Loading...
Searching...
No Matches
KoCompositeOpAlphaDarken.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006 Cyrille Berger <cberger@cberger.net>
3 * SPDX-FileCopyrightText: 2011 Silvio Heinrich <plassy@web.de>
4 * SPDX-FileCopyrightText: 2026 Carsten Hartenfels <carsten.hartenfels@pm.me>
5 *
6 * SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8
9#ifndef KOCOMPOSITEOPALPHADARKEN_H_
10#define KOCOMPOSITEOPALPHADARKEN_H_
11
12#include "KoColorSpaceMaths.h"
13#include "KoCompositeOpBase.h"
15
19template<class Traits, class ParamsWrapper, bool wash = true>
21{
22 typedef typename Traits::channels_type channels_type;
23
24 static const qint32 channels_nb = Traits::channels_nb;
25 static const qint32 alpha_pos = Traits::alpha_pos;
26
27public:
30
32
33 void composite(const KoCompositeOp::ParameterInfo& params) const override
34 {
35 if(params.maskRowStart != 0)
36 genericComposite<true>(params);
37 else
38 genericComposite<false>(params);
39 }
40
41 template<bool useMask>
43 {
44 using namespace Arithmetic;
45
46 ParamsWrapper paramsWrapper(params);
47
48 qint32 srcInc = (params.srcRowStride == 0) ? 0 : channels_nb;
49 channels_type flow = scale<channels_type>(paramsWrapper.flow);
50 channels_type opacity = scale<channels_type>(paramsWrapper.opacity);
51 quint8* dstRowStart = params.dstRowStart;
52 const quint8* srcRowStart = params.srcRowStart;
53 const quint8* maskRowStart = params.maskRowStart;
54
55 for (qint32 r = params.rows; r > 0; --r) {
56 const channels_type* src = reinterpret_cast<const channels_type*>(srcRowStart);
57 channels_type* dst = reinterpret_cast<channels_type*>(dstRowStart);
58 const quint8* mask = maskRowStart;
59
60 for(qint32 c=params.cols; c>0; --c) {
61 channels_type srcAlpha = (alpha_pos == -1) ? unitValue<channels_type>() : src[alpha_pos];
62 channels_type dstAlpha = (alpha_pos == -1) ? unitValue<channels_type>() : dst[alpha_pos];
63 channels_type mskAlpha = useMask ? mul(scale<channels_type>(*mask), srcAlpha) : srcAlpha;
64
65 srcAlpha = mul(mskAlpha, opacity);
66
67 if(dstAlpha != zeroValue<channels_type>()) {
68 for(qint32 i=0; i <channels_nb; i++) {
69 if(i != alpha_pos)
70 dst[i] = lerp(dst[i], src[i], srcAlpha);
71 }
72 }
73 else {
74 for(qint32 i=0; i <channels_nb; i++) {
75 if(i != alpha_pos)
76 dst[i] = src[i];
77 }
78 }
79
80 if(alpha_pos != -1) {
81 if constexpr (wash) {
82 dst[alpha_pos] = calculateAlpha(params, paramsWrapper, flow, opacity, srcAlpha, dstAlpha, mskAlpha);
83 } else {
84 // This is for the Clamp paint mode. Instead of meddling
85 // with opacity and averageOpacity, we just look at the
86 // alpha of each pixel. If it's higher, that's our full
87 // flow alpha, otherwise leave it be. There is a prior
88 // commit that implements averageOpacity instead that
89 // gets a different effect on brush tails, but it's
90 // doubtful whether that's the right thing to do.
91 if (srcAlpha > dstAlpha) {
92 if (params.flow == 1.0f) {
93 dst[alpha_pos] = srcAlpha;
94 } else {
95 dst[alpha_pos] = lerp(dstAlpha, srcAlpha, flow);
96 }
97 }
98 }
99 }
100
101 src += srcInc;
102 dst += channels_nb;
103
104 if(useMask)
105 ++mask;
106 }
107
108 srcRowStart += params.srcRowStride;
109 dstRowStart += params.dstRowStride;
110 maskRowStart += params.maskRowStride;
111 }
112 }
113
115 const ParamsWrapper &paramsWrapper,
116 channels_type flow,
117 channels_type opacity,
118 channels_type srcAlpha,
119 channels_type dstAlpha,
120 channels_type mskAlpha)
121 {
122 using namespace Arithmetic;
123
124 channels_type fullFlowAlpha;
125 channels_type averageOpacity = scale<channels_type>(paramsWrapper.averageOpacity);
126
132 if (averageOpacity > opacity) {
145 channels_type reverseBlend = KoColorSpaceMaths<channels_type>::divide(dstAlpha, averageOpacity);
146 fullFlowAlpha = averageOpacity > dstAlpha ? lerp(srcAlpha, averageOpacity, reverseBlend) : dstAlpha;
147 } else {
148 fullFlowAlpha = opacity > dstAlpha ? lerp(dstAlpha, opacity, mskAlpha) : dstAlpha;
149 }
150
151 if (params.flow == 1.0f) {
152 return fullFlowAlpha;
153 } else {
154 channels_type zeroFlowAlpha = ParamsWrapper::calculateZeroFlowAlphaLegacy(srcAlpha, dstAlpha);
155 return lerp(zeroFlowAlpha, fullFlowAlpha, flow);
156 }
157 }
158};
159
160#endif // KOCOMPOSITEOPALPHADARKEN_H_
const QString COMPOSITE_ALPHA_DARKEN_CLAMP
const QString COMPOSITE_ALPHA_DARKEN
QPointF lerp(const QPointF &p1, const QPointF &p2, qreal t)
static dst_compositetype divide(_T a, _Tdst b)
void composite(const KoCompositeOp::ParameterInfo &params) const override
KoCompositeOpAlphaDarken(const KoColorSpace *cs)
void genericComposite(const KoCompositeOp::ParameterInfo &params) const
static channels_type calculateAlpha(const KoCompositeOp::ParameterInfo &params, const ParamsWrapper &paramsWrapper, channels_type flow, channels_type opacity, channels_type srcAlpha, channels_type dstAlpha, channels_type mskAlpha)
Traits::channels_type channels_type
void composite(quint8 *dstRowStart, qint32 dstRowStride, const quint8 *srcRowStart, qint32 srcRowStride, const quint8 *maskRowStart, qint32 maskRowStride, qint32 rows, qint32 numColumns, float opacity, const QBitArray &channelFlags=QBitArray()) const
static QString categoryMix()