Blender V5.3
modifier.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
10
11#include "BLI_math_color_c.hh"
12#include "BLI_math_interp.hh"
13#include "BLI_math_matrix.hh"
14#include "BLI_math_vector.hh"
15#include "BLI_task.hh"
16
17#include "DNA_sequence_types.h"
18
19#include "IMB_imbuf.hh"
20
21namespace blender {
22
23struct bContext;
24struct ARegionType;
25struct ImBuf;
26struct Strip;
27struct Panel;
28struct PanelType;
29struct PointerRNA;
30
31namespace ui {
32struct Layout;
33} // namespace ui
34
35namespace seq {
36
37struct RenderData;
38struct SeqRenderState;
39struct SeqResult;
40
60 const Strip &strip;
61
62 /* Transformation from strip image local pixel coordinates to the
63 * full render area pixel coordinates.This is used to sample
64 * modifier masks (since masks are in full render area space). */
66 /* Transformation to apply when sampling masks in compositor modifier. */
68 /* Timeline frame at which the modifiers are being applied at. */
69 const float timeline_frame;
71};
72
74
76 const StripModifierData &smd);
77
79
81
82bool modifier_ui_poll(const bContext *C, PanelType *pt);
83
84using PanelDrawFn = void (*)(const bContext *, Panel *);
85
87 const eStripModifierType type,
89
91float4 load_pixel_premul(const float *ptr);
92void store_pixel_premul(const float4 pix, uchar *ptr);
93void store_pixel_premul(const float4 pix, float *ptr);
94float4 load_pixel_raw(const uchar *ptr);
95float4 load_pixel_raw(const float *ptr);
96void store_pixel_raw(const float4 pix, uchar *ptr);
97void store_pixel_raw(const float4 pix, float *ptr);
98
99/* Mask sampler for #apply_modifier_op: no mask is present. */
101 void begin_row(int64_t /*y*/) {}
102 void apply_mask(const float4 /*input*/, float4 & /*result*/) {}
104 {
105 return 0.0f;
106 }
107};
108
109/* Mask sampler for #apply_modifier_op: floating point mask,
110 * same size as input, no transform. */
113 {
114 BLI_assert(mask && mask->float_data());
115 }
117 {
118 BLI_assert(y >= 0 && y < mask->y);
119 ptr = mask->float_data() + y * mask->x * 4;
120 }
121 void apply_mask(const float4 input, float4 &result)
122 {
123 float3 m(this->ptr);
124 result.x = math::interpolate(input.x, result.x, m.x);
125 result.y = math::interpolate(input.y, result.y, m.y);
126 result.z = math::interpolate(input.z, result.z, m.z);
127 this->ptr += 4;
128 }
130 {
131 float r = std::min({this->ptr[0], this->ptr[1], this->ptr[2]});
132 this->ptr += 4;
133 return r;
134 }
135 const float *ptr = nullptr;
136 const ImBuf *mask;
137};
138
139/* Mask sampler for #apply_modifier_op: byte mask,
140 * same size as input, no transform. */
143 {
144 BLI_assert(mask && mask->byte_data());
145 }
147 {
148 BLI_assert(y >= 0 && y < mask->y);
149 ptr = mask->byte_data() + y * mask->x * 4;
150 }
151 void apply_mask(const float4 input, float4 &result)
152 {
153 float3 m;
154 rgb_uchar_to_float(m, this->ptr);
155 result.x = math::interpolate(input.x, result.x, m.x);
156 result.y = math::interpolate(input.y, result.y, m.y);
157 result.z = math::interpolate(input.z, result.z, m.z);
158 this->ptr += 4;
159 }
161 {
162 float r = float(std::min({this->ptr[0], this->ptr[1], this->ptr[2]})) * (1.0f / 255.0f);
163 this->ptr += 4;
164 return r;
165 }
166 const uchar *ptr = nullptr;
167 const ImBuf *mask;
168};
169
170/* Mask sampler for #apply_modifier_op: floating point mask,
171 * sample mask with a transform. */
175 {
176 BLI_assert(mask && mask->float_data());
177 start_uv = transform.location().xy();
178 add_x = transform.x_axis().xy();
179 add_y = transform.y_axis().xy();
180 }
182 {
183 this->cur_y = y;
184 this->cur_x = 0;
185 /* Sample at pixel centers. */
186 this->cur_uv_row = this->start_uv + (y + 0.5f) * this->add_y + 0.5f * this->add_x;
187 }
188 void apply_mask(const float4 input, float4 &result)
189 {
190 float2 uv = this->cur_uv_row + this->cur_x * this->add_x - 0.5f;
191 float4 m;
193 this->mask->float_data(), m, this->mask->x, this->mask->y, 4, uv.x, uv.y);
194 result.x = math::interpolate(input.x, result.x, m.x);
195 result.y = math::interpolate(input.y, result.y, m.y);
196 result.z = math::interpolate(input.z, result.z, m.z);
197 this->cur_x++;
198 }
200 {
201 float2 uv = this->cur_uv_row + this->cur_x * this->add_x - 0.5f;
202 float4 m;
204 this->mask->float_data(), m, this->mask->x, this->mask->y, 4, uv.x, uv.y);
205 float r = std::min({m.x, m.y, m.z});
206 this->cur_x++;
207 return r;
208 }
210 const ImBuf *mask;
214};
215
216/* Mask sampler for #apply_modifier_op: byte mask,
217 * sample mask with a transform. */
221 {
222 BLI_assert(mask && mask->byte_data());
223 start_uv = transform.location().xy();
224 add_x = transform.x_axis().xy();
225 add_y = transform.y_axis().xy();
226 }
228 {
229 this->cur_y = y;
230 this->cur_x = 0;
231 /* Sample at pixel centers. */
232 this->cur_uv_row = this->start_uv + (y + 0.5f) * this->add_y + 0.5f * this->add_x;
233 }
234 void apply_mask(const float4 input, float4 &result)
235 {
236 float2 uv = this->cur_uv_row + this->cur_x * this->add_x - 0.5f;
238 this->mask->byte_data(), this->mask->x, this->mask->y, uv.x, uv.y);
239 float3 m;
240 rgb_uchar_to_float(m, mb);
241 result.x = math::interpolate(input.x, result.x, m.x);
242 result.y = math::interpolate(input.y, result.y, m.y);
243 result.z = math::interpolate(input.z, result.z, m.z);
244 this->cur_x++;
245 }
247 {
248 float2 uv = this->cur_uv_row + this->cur_x * this->add_x - 0.5f;
250 this->mask->byte_data(), this->mask->x, this->mask->y, uv.x, uv.y);
251 float r = float(std::min({m.x, m.y, m.z})) * (1.0f / 255.0f);
252 this->cur_x++;
253 return r;
254 }
256 const ImBuf *mask;
260};
261
262/* Given `T` that implements an `apply` function:
263 *
264 * template <typename ImageT, typename MaskSampler>
265 * void apply(ImageT* image, MaskSampler &mask, int image_x, IndexRange y_range);
266 *
267 * this function calls the apply() function in parallel
268 * chunks of the image to process, and with needed
269 * uchar or float ImageT types, and with appropriate MaskSampler
270 * instantiated, depending on whether the mask exists, data type
271 * of the mask, and whether it needs a transformation or can be
272 * sampled directly.
273 *
274 * Both input and mask images are expected to have
275 * 4 (RGBA) color channels. Input is modified. */
276template<typename T>
277void apply_modifier_op(T &op, ImBuf *ibuf, const ImBuf *mask, const float3x3 &mask_transform)
278{
279 if (ibuf == nullptr) {
280 return;
281 }
282 BLI_assert_msg(ibuf->channels == 0 || ibuf->channels == 4,
283 "Sequencer only supports 4 channel images");
284 BLI_assert_msg(mask == nullptr || mask->channels == 0 || mask->channels == 4,
285 "Sequencer only supports 4 channel images");
286 const bool direct_mask_sampling = mask == nullptr || (mask->x == ibuf->x && mask->y == ibuf->y &&
287 math::is_identity(mask_transform));
288 const int image_x = ibuf->x;
289 uchar *image_byte = ibuf->byte_data_for_write();
290 float *image_float = ibuf->float_data_for_write();
291 threading::parallel_for(IndexRange(ibuf->y), 16, [&](IndexRange y_range) {
292 const uchar *mask_byte = mask ? mask->byte_data() : nullptr;
293 const float *mask_float = mask ? mask->float_data() : nullptr;
294
295 /* Instantiate the needed processing function based on image/mask
296 * data types. */
297 if (image_byte) {
298 if (mask_byte) {
299 if (direct_mask_sampling) {
300 MaskSamplerDirectByte sampler(mask);
301 op.apply(image_byte, sampler, image_x, y_range);
302 }
303 else {
304 MaskSamplerTransformedByte sampler(mask, mask_transform);
305 op.apply(image_byte, sampler, image_x, y_range);
306 }
307 }
308 else if (mask_float) {
309 if (direct_mask_sampling) {
310 MaskSamplerDirectFloat sampler(mask);
311 op.apply(image_byte, sampler, image_x, y_range);
312 }
313 else {
314 MaskSamplerTransformedFloat sampler(mask, mask_transform);
315 op.apply(image_byte, sampler, image_x, y_range);
316 }
317 }
318 else {
319 MaskSamplerNone sampler;
320 op.apply(image_byte, sampler, image_x, y_range);
321 }
322 }
323 else if (image_float) {
324 if (mask_byte) {
325 if (direct_mask_sampling) {
327 op.apply(image_float, sampler, image_x, y_range);
328 }
329 else {
330 MaskSamplerTransformedByte sampler(mask, mask_transform);
331 op.apply(image_float, sampler, image_x, y_range);
332 }
333 }
334 else if (mask_float) {
335 if (direct_mask_sampling) {
337 op.apply(image_float, sampler, image_x, y_range);
338 }
339 else {
341 op.apply(image_float, sampler, image_x, y_range);
342 }
343 }
344 else {
346 op.apply(image_float, sampler, image_x, y_range);
347 }
348 }
349 });
350}
351
352} // namespace seq
353} // namespace blender
#define C
Definition RandGen.cpp:29
#define BLI_assert_msg(a, msg)
constexpr IndexRange()=default
#define sampler(slot)
#define T
float4 interpolate_bilinear_border_fl(const float *buffer, int width, int height, float u, float v)
T interpolate(const T &a, const T &b, const FactorT &t)
bool is_identity(const MatBase< T, NumCol, NumRow > &mat)
uchar4 interpolate_bilinear_border_byte(const uchar *buffer, int width, int height, float u, float v)
ImBuf * modifier_render_mask_input(const ModifierApplyContext &context, const StripModifierData &smd)
void store_pixel_raw(float4 pix, uchar *ptr)
void apply_modifier_op(T &op, ImBuf *ibuf, const ImBuf *mask, const float3x3 &mask_transform)
Definition modifier.hh:277
void store_pixel_premul(float4 pix, uchar *ptr)
bool modifier_persistent_uids_are_valid(const Strip &strip)
float4 load_pixel_raw(const uchar *ptr)
float4 load_pixel_premul(const uchar *ptr)
void modifier_apply_stack(ModifierApplyContext &context)
bool modifier_ui_poll(const bContext *C, PanelType *)
void(*)(const bContext *, Panel *) PanelDrawFn
Definition modifier.hh:84
void draw_mask_input_type_settings(const bContext *C, ui::Layout &layout, PointerRNA *ptr)
PanelType * modifier_panel_register(ARegionType *region_type, const eStripModifierType type, PanelDrawFn draw)
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
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
VecBase< uint8_t, 4 > uchar4
__int64 int64_t
Definition BKE_text.h:90
VecBase< float, 4 > float4
VecBase< float, 2 > float2
unsigned char uchar
Definition BKE_text.h:58
MatBase< float, 3, 3 > float3x3
MINLINE void rgb_uchar_to_float(float r_col[3], const unsigned char col_ub[3])
VecBase< float, 3 > float3
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const uint8_t * byte_data() const
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
const float * float_data() const
float * float_data_for_write()
Definition allocimbuf.cc:79
MaskSamplerDirectByte(const ImBuf *mask)
Definition modifier.hh:142
void apply_mask(const float4 input, float4 &result)
Definition modifier.hh:151
void apply_mask(const float4 input, float4 &result)
Definition modifier.hh:121
MaskSamplerDirectFloat(const ImBuf *mask)
Definition modifier.hh:112
void apply_mask(const float4, float4 &)
Definition modifier.hh:102
void apply_mask(const float4 input, float4 &result)
Definition modifier.hh:234
MaskSamplerTransformedByte(const ImBuf *mask, const float3x3 &transform)
Definition modifier.hh:219
void apply_mask(const float4 input, float4 &result)
Definition modifier.hh:188
MaskSamplerTransformedFloat(const ImBuf *mask, const float3x3 &transform)
Definition modifier.hh:173
ModifierApplyContext(const RenderData &render_data, SeqRenderState &render_state, const Strip &strip, const float3x3 &transform, const float3x3 &transform_comp_result, const float timeline_frame, SeqResult &result)
Definition modifier.hh:42
const RenderData & render_data
Definition modifier.hh:58