Blender V5.3
effect_wipe.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
9#include <algorithm>
10
11#include "BLI_math_vector.hh"
12#include "BLI_task.hh"
13
14#include "DNA_sequence_types.h"
15
16#include "IMB_imbuf.hh"
17
18#include "PRF_profile.hh"
19
20#include "SEQ_render.hh"
21
22#include "effects.hh"
23
24namespace blender::seq {
25
26struct WipeData {
27 WipeData(const WipeVars *wipe, int width, int height, float fac)
28 {
29 this->type = wipe->wipetype;
30 this->forward = wipe->forward != 0;
31 this->size = float2(width, height);
32
33 if (this->type == SEQ_WIPE_SINGLE) {
34 /* Position that the wipe line goes through: moves along
35 * the image diagonal. The other diagonal when angle is negative. */
36 this->pos = this->size * (this->forward ? fac : (1.0f - fac));
37 if (wipe->angle < 0.0f) {
38 this->pos.x = this->size.x - this->pos.x;
39 }
40 }
41 if (this->type == SEQ_WIPE_DOUBLE) {
42 /* For double blend, position goes from center of screen
43 * along the diagonal. The other blend line position will be
44 * a mirror of it. */
45 float2 offset = this->size * (this->forward ? (1.0f - fac) : fac) * 0.5f;
46 if (wipe->angle < 0.0f) {
47 offset.x = -offset.x;
48 }
49 this->pos = this->size * 0.5f + offset;
50 }
51
52 /* Line direction: (cos(a), sin(a)). Perpendicular: (-sin(a), cos(a)).
53 * Angle is negative to match previous behavior. */
54 this->normal.x = -sinf(-wipe->angle);
55 this->normal.y = cosf(-wipe->angle);
56
57 /* Blend zone width. */
58 float blend_width = wipe->edgeWidth * ((width + height) / 2.0f);
60 blend_width *= 0.5f;
61 }
62 /* For single/double wipes, make sure the blend zone goes to zero at start & end
63 * of transition. */
65 blend_width = std::min(blend_width, fac * this->size.y);
66 blend_width = std::min(blend_width, this->size.y - fac * this->size.y);
67 }
68 this->blend_width_inv = math::safe_rcp(blend_width);
69
70 if (this->type == SEQ_WIPE_IRIS) {
71 /* Distance to Iris circle at current factor. */
72 float2 iris = this->size * 0.5f * (this->forward ? (1.0f - fac) : fac);
73 this->iris_dist = math::length(iris);
74 }
75
76 if (this->type == SEQ_WIPE_CLOCK) {
77 float angle_cur = 2.0f * float(M_PI) * (this->forward ? (1.0f - fac) : fac);
78 float angle_width = wipe->edgeWidth * float(M_PI);
79 float delta_neg = angle_width * (this->forward ? fac : (1.0f - fac));
80 float delta_pos = angle_width * (this->forward ? (1.0f - fac) : fac);
81 this->clock_angles.x = std::max(angle_cur - delta_neg, 0.0f);
82 this->clock_angles.y = std::min(angle_cur + delta_pos, 2.0f * float(M_PI));
83 this->clock_angle_inv_dif = math::safe_rcp(this->clock_angles.y - this->clock_angles.x);
84 }
85 }
86
87 float2 size; /* Image size. */
88 float2 pos; /* Position that wipe line goes through. */
89 float2 normal; /* Normal vector to single/double wipe line. */
90 float blend_width_inv = 0.0f;
91 float iris_dist = 0.0f;
92 float2 clock_angles; /* Min, max clock angles at current factor. */
93 float clock_angle_inv_dif = 0.0f;
95 bool forward = false;
96};
97
98static float calc_wipe_band(float dist, float inv_width)
99{
100 if (inv_width == 0.0f) {
101 return dist < 0.0f ? 0.0f : 1.0f;
102 }
103 return dist * inv_width + 0.5f;
104}
105
106static float calc_wipe_blend(const WipeData *data, int x, int y)
107{
108 float output = 0.0f;
109 switch (data->type) {
110 case SEQ_WIPE_SINGLE: {
111 /* Distance to line: dot(pixel_pos - line_pos, line_normal). */
112 float dist = math::dot(float2(x, y) - data->pos, data->normal);
113 output = calc_wipe_band(dist, data->blend_width_inv);
114 } break;
115
116 case SEQ_WIPE_DOUBLE: {
117 /* Distance to line: dot(pixel_pos - line_pos, line_normal).
118 * For double wipe, we have two lines to calculate the distance to. */
119 float2 pos1 = data->pos;
120 float2 pos2 = data->size - data->pos;
121 float dist1 = math::dot(float2(x, y) - pos1, -data->normal);
122 float dist2 = math::dot(float2(x, y) - pos2, data->normal);
123 float dist = std::min(dist1, dist2);
124 output = calc_wipe_band(dist, data->blend_width_inv);
125 } break;
126
127 case SEQ_WIPE_CLOCK: {
128 float2 offset = float2(x, y) - data->size * 0.5f;
129 if (math::length_squared(offset) < 1.0e-3f) {
130 output = 0.0f;
131 }
132 else {
133 float angle;
134 angle = atan2f(offset.y, offset.x);
135 if (angle < 0.0f) {
136 angle += 2.0f * float(M_PI);
137 }
138 if (angle < data->clock_angles.x) {
139 output = 1;
140 }
141 else if (angle > data->clock_angles.y) {
142 output = 0;
143 }
144 else {
145 output = (data->clock_angles.y - angle) * data->clock_angle_inv_dif;
146 }
147 }
148 } break;
149
150 case SEQ_WIPE_IRIS: {
151 float dist = math::distance(float2(x, y), data->size * 0.5f);
152 output = calc_wipe_band(data->iris_dist - dist, data->blend_width_inv);
153 } break;
154 }
155 if (!data->forward) {
156 output = 1.0f - output;
157 }
158 return output;
159}
160
161static void init_wipe_effect(Strip *strip)
162{
163 strip->effectdata = MEM_new<WipeVars>("wipevars");
164}
165
166static void free_wipe_effect(Strip *strip, const bool /*do_id_user*/)
167{
168 if (strip->effectdata) {
169 MEM_delete(static_cast<WipeVars *>(strip->effectdata));
170 strip->effectdata = nullptr;
171 }
172}
173
174template<typename T>
175static void do_wipe_effect(
176 const Strip *strip, float fac, int width, int height, const T *rect1, const T *rect2, T *out)
177{
178 const WipeVars *wipe = static_cast<const WipeVars *>(strip->effectdata);
179
180 const WipeData data(wipe, width, height, fac);
181
182 threading::parallel_for(IndexRange(height), 64, [&](const IndexRange y_range) {
183 const T *cp1 = rect1 + y_range.first() * width * 4;
184 const T *cp2 = rect2 + y_range.first() * width * 4;
185 T *rt = out + y_range.first() * width * 4;
186 for (const int y : y_range) {
187 for (int x = 0; x < width; x++) {
188 float blend = calc_wipe_blend(&data, x, y);
189 if (blend <= 0.0f) {
190 memcpy(rt, cp2, sizeof(T) * 4);
191 }
192 else if (blend >= 1.0f) {
193 memcpy(rt, cp1, sizeof(T) * 4);
194 }
195 else {
196 float4 col1 = load_premul_pixel(cp1);
197 float4 col2 = load_premul_pixel(cp2);
198 float4 col = col1 * blend + col2 * (1.0f - blend);
199 store_premul_pixel(col, rt);
200 }
201
202 rt += 4;
203 cp1 += 4;
204 cp2 += 4;
205 }
206 }
207 });
208}
209
210static SeqResult do_wipe_effect(const RenderData *context,
211 SeqRenderState * /*state*/,
212 Strip *strip,
213 float /*timeline_frame*/,
214 float fac,
215 const SeqResult &ibuf1,
216 const SeqResult &ibuf2)
217{
219 SeqResult out = prepare_effect_imbufs(context, ibuf1, ibuf2);
220
221 if (out.image->float_data()) {
222 do_wipe_effect(strip,
223 fac,
224 context->rectx,
225 context->recty,
226 ibuf1.image->float_data(),
227 ibuf2.image->float_data(),
228 out.image->float_data_for_write());
229 }
230 else {
231 do_wipe_effect(strip,
232 fac,
233 context->rectx,
234 context->recty,
235 ibuf1.image->byte_data(),
236 ibuf2.image->byte_data(),
237 out.image->byte_data_for_write());
238 }
239
240 out.is_opaque_before_transform = !ibuf1.image->can_contain_alpha() &&
241 !ibuf2.image->can_contain_alpha();
242 return out;
243}
244
252
253} // namespace blender::seq
#define M_PI
#define PRF_scope_with_name(ui_name, category)
#define ELEM(...)
#define output
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
constexpr int64_t first() const
constexpr IndexRange()=default
#define T
T length_squared(const VecBase< T, Size > &a)
T safe_rcp(const T &a)
T distance(const T &a, const T &b)
T length(const VecBase< T, Size > &a)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
static void free_wipe_effect(Strip *strip, const bool)
void wipe_effect_get_handle(EffectHandle &rval)
float4 load_premul_pixel(const uchar *ptr)
Definition effects.hh:88
static void init_wipe_effect(Strip *strip)
SeqResult prepare_effect_imbufs(const RenderData *context, const SeqResult &ibuf1, const SeqResult &ibuf2, bool uninitialized_pixels)
Definition effects.cc:34
static void do_wipe_effect(const Strip *strip, float fac, int width, int height, const T *rect1, const T *rect2, T *out)
static float calc_wipe_blend(const WipeData *data, int x, int y)
static float calc_wipe_band(float dist, float inv_width)
void store_premul_pixel(const float4 &pix, uchar *dst)
Definition effects.hh:100
StripEarlyOut early_out_fade(const Strip *, float fac)
Definition effects.cc:110
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
VecBase< float, 4 > float4
VecBase< float, 2 > float2
BMesh const char void * data
#define sinf
#define cosf
#define atan2f
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const uint8_t * byte_data() const
const float * float_data() const
bool can_contain_alpha() const
eEffectWipeType wipetype
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
WipeData(const WipeVars *wipe, int width, int height, float fac)
eEffectWipeType type