Krita Source Code Documentation
Loading...
Searching...
No Matches
AlphaDarkenCompositor32< channels_type, pixel_type, ParamsWrapperT, wash > Struct Template Reference

#include <KoOptimizedCompositeOpAlphaDarken32.h>

Public Types

using ParamsWrapper = ParamsWrapperT
 

Static Public Member Functions

template<bool haveMask, typename _impl >
static ALWAYS_INLINE void compositeOnePixelScalar (const channels_type *src, channels_type *dst, const quint8 *mask, float opacity, const ParamsWrapper &oparams)
 
template<bool haveMask, bool src_aligned, typename _impl >
static ALWAYS_INLINE void compositeVector (const quint8 *src, quint8 *dst, const quint8 *mask, float opacity, const ParamsWrapper &oparams)
 

Detailed Description

template<typename channels_type, typename pixel_type, typename ParamsWrapperT, bool wash = true>
struct AlphaDarkenCompositor32< channels_type, pixel_type, ParamsWrapperT, wash >

Definition at line 19 of file KoOptimizedCompositeOpAlphaDarken32.h.

Member Typedef Documentation

◆ ParamsWrapper

template<typename channels_type , typename pixel_type , typename ParamsWrapperT , bool wash = true>
using AlphaDarkenCompositor32< channels_type, pixel_type, ParamsWrapperT, wash >::ParamsWrapper = ParamsWrapperT

Definition at line 20 of file KoOptimizedCompositeOpAlphaDarken32.h.

Member Function Documentation

◆ compositeOnePixelScalar()

template<typename channels_type , typename pixel_type , typename ParamsWrapperT , bool wash = true>
template<bool haveMask, typename _impl >
static ALWAYS_INLINE void AlphaDarkenCompositor32< channels_type, pixel_type, ParamsWrapperT, wash >::compositeOnePixelScalar ( const channels_type * src,
channels_type * dst,
const quint8 * mask,
float opacity,
const ParamsWrapper & oparams )
inlinestatic

Composes one pixel of the source into the destination

Definition at line 170 of file KoOptimizedCompositeOpAlphaDarken32.h.

171 {
172 using namespace Arithmetic;
173 const qint32 alpha_pos = 3;
174
175 const float uint8Rec1 = 1.0f / 255.0f;
176 const float uint8Rec2 = 1.0f / (255.0f * 255.0f);
177 const float uint8Max = 255.0;
178
179 // we don't use directly passed value
180 Q_UNUSED(opacity);
181
182 // instead we use value calculated by ParamsWrapper
183 opacity = oparams.opacity;
184
185 const quint8 dstAlphaInt = dst[alpha_pos];
186 const float dstAlphaNorm = dstAlphaInt ? dstAlphaInt * uint8Rec1 : 0.0;
187 const float mskAlphaNorm = [&]() {
188 if (haveMask) {
189 return float(*mask) * uint8Rec2 * src[alpha_pos];
190 } else {
191 return src[alpha_pos] * uint8Rec1;
192 }
193 }();
194 const float srcAlphaNorm = mskAlphaNorm * opacity;
195
196 if (dstAlphaInt != 0) {
197 dst[0] = KoStreamedMath<_impl>::lerp_mixed_u8_float(dst[0], src[0], srcAlphaNorm);
198 dst[1] = KoStreamedMath<_impl>::lerp_mixed_u8_float(dst[1], src[1], srcAlphaNorm);
199 dst[2] = KoStreamedMath<_impl>::lerp_mixed_u8_float(dst[2], src[2], srcAlphaNorm);
200 } else {
201 if constexpr (wash) {
202 const auto *s = reinterpret_cast<const pixel_type*>(src);
203 auto *d = reinterpret_cast<pixel_type*>(dst);
204 *d = *s;
205 } else {
206 dst[0] = src[0];
207 dst[1] = src[1];
208 dst[2] = src[2];
209 }
210 }
211
212 if constexpr (wash) {
213 const float flow = oparams.flow;
214 const float averageOpacity = oparams.averageOpacity;
215
216 const float fullFlowAlpha = [&]() {
217 if (averageOpacity > opacity) {
218 return averageOpacity > dstAlphaNorm ? lerp(srcAlphaNorm, averageOpacity, dstAlphaNorm / averageOpacity) : dstAlphaNorm;
219 } else {
220 return opacity > dstAlphaNorm ? lerp(dstAlphaNorm, opacity, mskAlphaNorm) : dstAlphaNorm;
221 }
222 }();
223
224 const float dstAlpha = [&](){
225 if (flow == 1.0f) {
226 return fullFlowAlpha * uint8Max;
227 } else {
228 const float zeroFlowAlpha = ParamsWrapper::calculateZeroFlowAlpha(srcAlphaNorm, dstAlphaNorm);
229 return lerp(zeroFlowAlpha, fullFlowAlpha, flow) * uint8Max;
230 }
231 }();
232
233 dst[alpha_pos] = KoStreamedMath<_impl>::round_float_to_u8(dstAlpha);
234 } else {
235 // Clamp paint mode.
236 if (srcAlphaNorm > dstAlphaNorm) {
237 const float flow = oparams.flow;
238
239 const float dstAlpha = [&](){
240 if (flow == 1.0f) {
241 return srcAlphaNorm * uint8Max;
242 } else {
243 return lerp(dstAlphaNorm, srcAlphaNorm, flow) * uint8Max;
244 }
245 }();
246
247 dst[alpha_pos] = KoStreamedMath<_impl>::round_float_to_u8(dstAlpha);
248 }
249 }
250
251 }
QPointF lerp(const QPointF &p1, const QPointF &p2, qreal t)
static quint8 lerp_mixed_u8_float(quint8 a, quint8 b, float alpha)
static quint8 round_float_to_u8(float x)

References lerp(), KoStreamedMath< _impl >::lerp_mixed_u8_float(), and KoStreamedMath< _impl >::round_float_to_u8().

◆ compositeVector()

template<typename channels_type , typename pixel_type , typename ParamsWrapperT , bool wash = true>
template<bool haveMask, bool src_aligned, typename _impl >
static ALWAYS_INLINE void AlphaDarkenCompositor32< channels_type, pixel_type, ParamsWrapperT, wash >::compositeVector ( const quint8 * src,
quint8 * dst,
const quint8 * mask,
float opacity,
const ParamsWrapper & oparams )
inlinestatic

This is a vector equivalent of compositeOnePixelScalar(). It is considered to process float_v::size pixels in a single pass.

o the haveMask parameter points whether the real (non-null) mask pointer is passed to the function. o the src pointer may be aligned to vector boundary or may be not. In case not, it must be pointed with a special parameter src_aligned. o the dst pointer must always(!) be aligned to the boundary of a streaming vector. Unaligned writes are really expensive.

Definition at line 35 of file KoOptimizedCompositeOpAlphaDarken32.h.

36 {
37 using float_v = typename KoStreamedMath<_impl>::float_v;
38 using float_m = typename float_v::batch_bool_type;
39
40 // we don't use directly passed value
41 Q_UNUSED(opacity);
42
43 // instead we use value calculated by ParamsWrapper
44 opacity = oparams.opacity;
45 const float_v opacity_vec(255.0f * opacity);
46
47 const float_v average_opacity_vec(255.0 * oparams.averageOpacity);
48 const float_v flow_norm_vec(oparams.flow);
49
50 const float_v uint8MaxRec2(1.0f / (255.0f * 255.0f));
51 const float_v uint8MaxRec1(1.0f / 255.0f);
52 const float_v uint8Max(255.0f);
53 const float_v zeroValue(0);
54
55 float_v src_alpha = KoStreamedMath<_impl>::template fetch_alpha_32<src_aligned>(src);
56
57 const float_v msk_norm_alpha = [&]() {
58 if (haveMask) {
59 const float_v mask_vec = KoStreamedMath<_impl>::fetch_mask_8(mask);
60 return src_alpha * mask_vec * uint8MaxRec2;
61 } else {
62 return src_alpha * uint8MaxRec1;
63 }
64 }();
65
66 float_v dst_alpha = KoStreamedMath<_impl>::template fetch_alpha_32<true>(dst);
67 src_alpha = msk_norm_alpha * opacity_vec;
68
69 const float_m empty_dst_pixels_mask = dst_alpha == zeroValue;
70
71 float_v src_c1;
72 float_v src_c2;
73 float_v src_c3;
74
75 float_v dst_c1;
76 float_v dst_c2;
77 float_v dst_c3;
78
79 KoStreamedMath<_impl>::template fetch_colors_32<src_aligned>(src, src_c1, src_c2, src_c3);
80
81 const bool srcAlphaIsZero = xsimd::all(src_alpha == zeroValue);
82 if (srcAlphaIsZero) return;
83
84 const bool dstAlphaIsZero = xsimd::all(empty_dst_pixels_mask);
85
86 const float_v dst_blend = src_alpha * uint8MaxRec1;
87
88 const bool srcAlphaIsUnit = xsimd::all(src_alpha == uint8Max);
89
90 if (dstAlphaIsZero) {
91 dst_c1 = src_c1;
92 dst_c2 = src_c2;
93 dst_c3 = src_c3;
94 } else if (srcAlphaIsUnit) {
95 const bool dstAlphaIsUnit = xsimd::all(dst_alpha == uint8Max);
96 if (dstAlphaIsUnit) {
97 memcpy(dst, src, 4 * float_v::size);
98 return;
99 } else {
100 dst_c1 = src_c1;
101 dst_c2 = src_c2;
102 dst_c3 = src_c3;
103 }
104 } else if (xsimd::none(empty_dst_pixels_mask)) {
105 KoStreamedMath<_impl>::template fetch_colors_32<true>(dst, dst_c1, dst_c2, dst_c3);
106 dst_c1 = dst_blend * (src_c1 - dst_c1) + dst_c1;
107 dst_c2 = dst_blend * (src_c2 - dst_c2) + dst_c2;
108 dst_c3 = dst_blend * (src_c3 - dst_c3) + dst_c3;
109 } else {
110 KoStreamedMath<_impl>::template fetch_colors_32<true>(dst, dst_c1, dst_c2, dst_c3);
111 dst_c1 = xsimd::select(empty_dst_pixels_mask, src_c1, dst_blend * (src_c1 - dst_c1) + dst_c1);
112 dst_c2 = xsimd::select(empty_dst_pixels_mask, src_c2, dst_blend * (src_c2 - dst_c2) + dst_c2);
113 dst_c3 = xsimd::select(empty_dst_pixels_mask, src_c3, dst_blend * (src_c3 - dst_c3) + dst_c3);
114 }
115
116 if constexpr (wash) {
117 const float_v fullFlowAlpha = [&]() {
118 if (oparams.averageOpacity > opacity) {
119 const float_m fullFlowAlpha_mask = average_opacity_vec > dst_alpha;
120
121 if (xsimd::none(fullFlowAlpha_mask)) {
122 return dst_alpha;
123 } else {
124 const float_v reverse_blend = dst_alpha / average_opacity_vec;
125 const float_v opt1 = (average_opacity_vec - src_alpha) * reverse_blend + src_alpha;
126 return xsimd::select(fullFlowAlpha_mask, opt1, dst_alpha);
127 }
128 } else {
129 const float_m fullFlowAlpha_mask = opacity_vec > dst_alpha;
130
131 if (xsimd::none(fullFlowAlpha_mask)) {
132 return dst_alpha;
133 } else {
134 const float_v opt1 = (opacity_vec - dst_alpha) * msk_norm_alpha + dst_alpha;
135 return xsimd::select(fullFlowAlpha_mask, opt1, dst_alpha);
136 }
137 }
138 }();
139
140 dst_alpha = [&]() {
141 if (oparams.flow == 1.0) {
142 return fullFlowAlpha;
143 } else {
144 const float_v zeroFlowAlpha = ParamsWrapper::calculateZeroFlowAlpha(src_alpha, dst_alpha, uint8MaxRec1);
145 return (fullFlowAlpha - zeroFlowAlpha) * flow_norm_vec + zeroFlowAlpha;
146 }
147 }();
148 } else {
149 // Clamp paint mode.
150 const float_m fullFlowAlpha_mask = src_alpha > dst_alpha;
151 if (!xsimd::none(fullFlowAlpha_mask)) {
152 dst_alpha = [&]() {
153 if (oparams.flow == 1.0f) {
154 return xsimd::select(fullFlowAlpha_mask, src_alpha, dst_alpha);
155 } else {
156 const float_v lerped = (src_alpha - dst_alpha) * flow_norm_vec + dst_alpha;
157 return xsimd::select(fullFlowAlpha_mask, lerped, dst_alpha);
158 }
159 }();
160 }
161 }
162
163 KoStreamedMath<_impl>::write_channels_32(dst, dst_alpha, dst_c1, dst_c2, dst_c3);
164 }
static void write_channels_32(void *data, const float_v alpha, const float_v c1, const float_v c2, const float_v c3)
xsimd::batch< float, _impl > float_v
static float_v fetch_mask_8(const quint8 *data)

References KoStreamedMath< _impl >::fetch_mask_8(), and KoStreamedMath< _impl >::write_channels_32().


The documentation for this struct was generated from the following file: