Blender V5.3
effect_blend.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "DNA_sequence_types.h"
12
13#include "IMB_imbuf.hh"
14
15#include "PRF_profile.hh"
16
17#include "SEQ_render.hh"
18
19#include "effects.hh"
20
21namespace blender::seq {
22
23/* -------------------------------------------------------------------- */
24/* Alpha Over Effect */
25
27{
28 Strip *input1 = strip->input1;
29 Strip *input2 = strip->input2;
30
31 strip->input2 = input1;
32 strip->input1 = input2;
33}
34
35static bool alpha_opaque(uchar alpha)
36{
37 return alpha == 255;
38}
39
40static bool alpha_opaque(float alpha)
41{
42 return alpha >= 1.0f;
43}
44
45/* dst = src1 over src2 (alpha from src1) */
47 template<typename T> void apply(const T *src1, const T *src2, T *dst, int64_t size) const
48 {
49 const float fac = this->factor;
50 if (fac <= 0.0f) {
51 memcpy(dst, src2, sizeof(T) * 4 * size);
52 return;
53 }
54
55 for (int64_t idx = 0; idx < size; idx++) {
56 if (std::is_same_v<T, uchar> && src1[3] == 0) {
57 /* Optimization for fully transparent pixels: copy src2. Only do this for byte images;
58 * in floats alpha=0 can still have pure emissive color. */
59 memcpy(dst, src2, sizeof(T) * 4);
60 }
61 else if (fac == 1.0f && alpha_opaque(src1[3])) {
62 /* No change to `src1` as `fac == 1` and fully opaque. */
63 memcpy(dst, src1, sizeof(T) * 4);
64 }
65 else {
66 float4 col1 = load_premul_pixel(src1);
67 float mfac = 1.0f - fac * col1.w;
68 float4 col2 = load_premul_pixel(src2);
69 float4 col = fac * col1 + mfac * col2;
70 store_premul_pixel(col, dst);
71 }
72 src1 += 4;
73 src2 += 4;
74 dst += 4;
75 }
76 }
77
78 float factor;
79};
80
82 SeqRenderState * /*state*/,
83 Strip * /*strip*/,
84 float /*timeline_frame*/,
85 float fac,
86 const SeqResult &src1,
87 const SeqResult &src2)
88{
90 SeqResult dst = prepare_effect_imbufs(context, src1, src2);
92 op.factor = fac;
93 apply_effect_op(op, src1.image, src2.image, dst.image);
94 return dst;
95}
96
97/* -------------------------------------------------------------------- */
98/* Alpha Under Effect */
99
100/* dst = src1 under src2 (alpha from src2) */
102 template<typename T> void apply(const T *src1, const T *src2, T *dst, int64_t size) const
103 {
104 const float fac = this->factor;
105 if (fac <= 0.0f) {
106 memcpy(dst, src2, sizeof(T) * 4 * size);
107 return;
108 }
109
110 for (int64_t idx = 0; idx < size; idx++) {
111 if (src2[3] <= 0.0f && fac >= 1.0f) {
112 memcpy(dst, src1, sizeof(T) * 4);
113 }
114 else if (alpha_opaque(src2[3])) {
115 memcpy(dst, src2, sizeof(T) * 4);
116 }
117 else {
118 float4 col2 = load_premul_pixel(src2);
119 float mfac = fac * (1.0f - col2.w);
120 float4 col1 = load_premul_pixel(src1);
121 float4 col = mfac * col1 + col2;
122 store_premul_pixel(col, dst);
123 }
124 src1 += 4;
125 src2 += 4;
126 dst += 4;
127 }
128 }
129 float factor;
130};
131
133 SeqRenderState * /*state*/,
134 Strip * /*strip*/,
135 float /*timeline_frame*/,
136 float fac,
137 const SeqResult &src1,
138 const SeqResult &src2)
139{
141 SeqResult dst = prepare_effect_imbufs(context, src1, src2);
143 op.factor = fac;
144 apply_effect_op(op, src1.image, src2.image, dst.image);
145 return dst;
146}
147
148/* -------------------------------------------------------------------- */
149/* Blend Mode Effect */
150
151/* blend_function has to be: void (T* dst, const T *src1, const T *src2) */
152template<typename T, typename Func>
154 float fac, int64_t size, const T *src1, const T *src2, T *dst, Func blend_function)
155{
156 for (int64_t i = 0; i < size; i++) {
157 T achannel = src2[3];
158 (static_cast<T *>(const_cast<T *>(src2)))[3] = T(achannel * fac);
159 blend_function(dst, src1, src2);
160 (static_cast<T *>(const_cast<T *>(src2)))[3] = achannel;
161 dst[3] = src1[3];
162 src1 += 4;
163 src2 += 4;
164 dst += 4;
165 }
166}
167
168static void do_blend_effect_float(float fac,
169 int64_t size,
170 const float *rect1,
171 const float *rect2,
172 StripBlendMode btype,
173 float *out)
174{
175 switch (btype) {
176 case STRIP_BLEND_ADD:
177 apply_blend_function(fac, size, rect1, rect2, out, blend_color_add_float);
178 break;
179 case STRIP_BLEND_SUB:
180 apply_blend_function(fac, size, rect1, rect2, out, blend_color_sub_float);
181 break;
182 case STRIP_BLEND_MUL:
183 apply_blend_function(fac, size, rect1, rect2, out, blend_color_mul_float);
184 break;
186 apply_blend_function(fac, size, rect1, rect2, out, blend_color_darken_float);
187 break;
189 apply_blend_function(fac, size, rect1, rect2, out, blend_color_burn_float);
190 break;
192 apply_blend_function(fac, size, rect1, rect2, out, blend_color_linearburn_float);
193 break;
195 apply_blend_function(fac, size, rect1, rect2, out, blend_color_screen_float);
196 break;
198 apply_blend_function(fac, size, rect1, rect2, out, blend_color_lighten_float);
199 break;
201 apply_blend_function(fac, size, rect1, rect2, out, blend_color_dodge_float);
202 break;
204 apply_blend_function(fac, size, rect1, rect2, out, blend_color_overlay_float);
205 break;
207 apply_blend_function(fac, size, rect1, rect2, out, blend_color_softlight_float);
208 break;
210 apply_blend_function(fac, size, rect1, rect2, out, blend_color_hardlight_float);
211 break;
213 apply_blend_function(fac, size, rect1, rect2, out, blend_color_pinlight_float);
214 break;
216 apply_blend_function(fac, size, rect1, rect2, out, blend_color_linearlight_float);
217 break;
219 apply_blend_function(fac, size, rect1, rect2, out, blend_color_vividlight_float);
220 break;
222 apply_blend_function(fac, size, rect1, rect2, out, blend_color_color_float);
223 break;
224 case STRIP_BLEND_HUE:
225 apply_blend_function(fac, size, rect1, rect2, out, blend_color_hue_float);
226 break;
228 apply_blend_function(fac, size, rect1, rect2, out, blend_color_saturation_float);
229 break;
231 apply_blend_function(fac, size, rect1, rect2, out, blend_color_luminosity_float);
232 break;
234 apply_blend_function(fac, size, rect1, rect2, out, blend_color_difference_float);
235 break;
237 apply_blend_function(fac, size, rect1, rect2, out, blend_color_exclusion_float);
238 break;
239 default:
240 break;
241 }
242}
243
244static void do_blend_effect_byte(float fac,
245 int64_t size,
246 const uchar *rect1,
247 const uchar *rect2,
248 StripBlendMode btype,
249 uchar *out)
250{
251 switch (btype) {
252 case STRIP_BLEND_ADD:
253 apply_blend_function(fac, size, rect1, rect2, out, blend_color_add_byte);
254 break;
255 case STRIP_BLEND_SUB:
256 apply_blend_function(fac, size, rect1, rect2, out, blend_color_sub_byte);
257 break;
258 case STRIP_BLEND_MUL:
259 apply_blend_function(fac, size, rect1, rect2, out, blend_color_mul_byte);
260 break;
262 apply_blend_function(fac, size, rect1, rect2, out, blend_color_darken_byte);
263 break;
265 apply_blend_function(fac, size, rect1, rect2, out, blend_color_burn_byte);
266 break;
268 apply_blend_function(fac, size, rect1, rect2, out, blend_color_linearburn_byte);
269 break;
271 apply_blend_function(fac, size, rect1, rect2, out, blend_color_screen_byte);
272 break;
274 apply_blend_function(fac, size, rect1, rect2, out, blend_color_lighten_byte);
275 break;
277 apply_blend_function(fac, size, rect1, rect2, out, blend_color_dodge_byte);
278 break;
280 apply_blend_function(fac, size, rect1, rect2, out, blend_color_overlay_byte);
281 break;
283 apply_blend_function(fac, size, rect1, rect2, out, blend_color_softlight_byte);
284 break;
286 apply_blend_function(fac, size, rect1, rect2, out, blend_color_hardlight_byte);
287 break;
289 apply_blend_function(fac, size, rect1, rect2, out, blend_color_pinlight_byte);
290 break;
292 apply_blend_function(fac, size, rect1, rect2, out, blend_color_linearlight_byte);
293 break;
295 apply_blend_function(fac, size, rect1, rect2, out, blend_color_vividlight_byte);
296 break;
298 apply_blend_function(fac, size, rect1, rect2, out, blend_color_color_byte);
299 break;
300 case STRIP_BLEND_HUE:
301 apply_blend_function(fac, size, rect1, rect2, out, blend_color_hue_byte);
302 break;
304 apply_blend_function(fac, size, rect1, rect2, out, blend_color_saturation_byte);
305 break;
307 apply_blend_function(fac, size, rect1, rect2, out, blend_color_luminosity_byte);
308 break;
310 apply_blend_function(fac, size, rect1, rect2, out, blend_color_difference_byte);
311 break;
313 apply_blend_function(fac, size, rect1, rect2, out, blend_color_exclusion_byte);
314 break;
315 default:
316 break;
317 }
318}
319
321 template<typename T> void apply(const T *src1, const T *src2, T *dst, int64_t size) const
322 {
323 if constexpr (std::is_same_v<T, float>) {
324 do_blend_effect_float(this->factor, size, src1, src2, this->blend_mode, dst);
325 }
326 else {
327 do_blend_effect_byte(this->factor, size, src1, src2, this->blend_mode, dst);
328 }
329 }
331 float factor;
332};
333
335 SeqRenderState * /*state*/,
336 Strip *strip,
337 float /*timeline_frame*/,
338 float fac,
339 const SeqResult &src1,
340 const SeqResult &src2)
341{
343 SeqResult dst = prepare_effect_imbufs(context, src1, src2);
345 op.factor = fac;
346 op.blend_mode = strip->blend_mode;
347 apply_effect_op(op, src1.image, src2.image, dst.image);
348 return dst;
349}
350
351/* -------------------------------------------------------------------- */
352/* Color Mix Effect */
353
354static void init_colormix_effect(Strip *strip)
355{
356 ColorMixVars *data = MEM_new<ColorMixVars>("colormixvars");
357 strip->effectdata = data;
358 data->blend_effect = STRIP_BLEND_OVERLAY;
359 data->factor = 1.0f;
360}
361
362static void free_colormix_effect(Strip *strip, const bool /*do_id_user*/)
363{
364 if (strip->effectdata) {
365 MEM_delete(static_cast<ColorMixVars *>(strip->effectdata));
366 strip->effectdata = nullptr;
367 }
368}
369
371 SeqRenderState * /*state*/,
372 Strip *strip,
373 float /*timeline_frame*/,
374 float /*fac*/,
375 const SeqResult &src1,
376 const SeqResult &src2)
377{
379 SeqResult dst = prepare_effect_imbufs(context, src1, src2);
380 const ColorMixVars *data = static_cast<const ColorMixVars *>(strip->effectdata);
382 op.blend_mode = data->blend_effect;
383 op.factor = data->factor;
384 apply_effect_op(op, src1.image, src2.image, dst.image);
385 return dst;
386}
387
393
401
408
414
415} // namespace blender::seq
#define PRF_scope_with_name(ui_name, category)
#define T
static SeqResult do_blend_mode_effect(const RenderData *context, SeqRenderState *, Strip *strip, float, float fac, const SeqResult &src1, const SeqResult &src2)
static void free_colormix_effect(Strip *strip, const bool)
static SeqResult do_alphaover_effect(const RenderData *context, SeqRenderState *, Strip *, float, float fac, const SeqResult &src1, const SeqResult &src2)
void alpha_under_effect_get_handle(EffectHandle &rval)
float4 load_premul_pixel(const uchar *ptr)
Definition effects.hh:88
StripEarlyOut early_out_mul_input2(const Strip *, float fac)
Definition effects.cc:121
static void do_blend_effect_float(float fac, int64_t size, const float *rect1, const float *rect2, StripBlendMode btype, float *out)
static bool alpha_opaque(uchar alpha)
void blend_mode_effect_get_handle(EffectHandle &rval)
SeqResult prepare_effect_imbufs(const RenderData *context, const SeqResult &ibuf1, const SeqResult &ibuf2, bool uninitialized_pixels)
Definition effects.cc:34
static void init_colormix_effect(Strip *strip)
static SeqResult do_alphaunder_effect(const RenderData *context, SeqRenderState *, Strip *, float, float fac, const SeqResult &src1, const SeqResult &src2)
static void do_blend_effect_byte(float fac, int64_t size, const uchar *rect1, const uchar *rect2, StripBlendMode btype, uchar *out)
StripEarlyOut early_out_mul_input1(const Strip *, float fac)
Definition effects.cc:129
static void init_alpha_over_or_under(Strip *strip)
static SeqResult do_colormix_effect(const RenderData *context, SeqRenderState *, Strip *strip, float, float, const SeqResult &src1, const SeqResult &src2)
void store_premul_pixel(const float4 &pix, uchar *dst)
Definition effects.hh:100
void color_mix_effect_get_handle(EffectHandle &rval)
static void apply_blend_function(float fac, int64_t size, const T *src1, const T *src2, T *dst, Func blend_function)
void alpha_over_effect_get_handle(EffectHandle &rval)
MINLINE void blend_color_vividlight_float(float dst[4], const float src1[4], const float src2[4])
__int64 int64_t
Definition BKE_text.h:90
MINLINE void blend_color_luminosity_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_linearburn_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_linearlight_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_add_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4])
MINLINE void blend_color_saturation_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_sub_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4])
MINLINE void blend_color_darken_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4])
MINLINE void blend_color_pinlight_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_overlay_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_hardlight_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_hue_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_darken_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_exclusion_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_sub_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_difference_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_luminosity_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_color_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
VecBase< float, 4 > float4
MINLINE void blend_color_color_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_difference_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_add_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_overlay_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
unsigned char uchar
Definition BKE_text.h:58
MINLINE void blend_color_hardlight_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
BMesh const char void * data
MINLINE void blend_color_exclusion_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_screen_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_saturation_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_dodge_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_hue_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_screen_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_mul_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_burn_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_lighten_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_linearburn_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_vividlight_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_lighten_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4])
MINLINE void blend_color_burn_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_softlight_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_pinlight_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_mul_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4])
MINLINE void blend_color_softlight_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_linearlight_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_dodge_float(float dst[4], const float src1[4], const float src2[4])
void apply(const T *src1, const T *src2, T *dst, int64_t size) const
void apply(const T *src1, const T *src2, T *dst, int64_t size) const
void apply(const T *src1, const T *src2, T *dst, int64_t size) const
void(*) free(Strip *strip, bool do_id_user)
Definition effects.hh:47
void(*) init(Strip *strip)
Definition effects.hh:41
StripEarlyOut(*) early_out(const Strip *strip, float fac)
Definition effects.hh:49
SeqResult(*) execute(const RenderData *context, SeqRenderState *state, Strip *strip, float timeline_frame, float fac, const SeqResult &input1, const SeqResult &input2)
Definition effects.hh:52