Blender V5.3
effect_gaussian_blur.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 "BLI_math_base.hh"
10#include "BLI_task.hh"
11
12#include "DNA_sequence_types.h"
13
14#include "IMB_imbuf.hh"
15
16#include "PRF_profile.hh"
17
18#include "SEQ_render.hh"
19
20#include "effects.hh"
21
22namespace blender::seq {
23
25{
26 GaussianBlurVars *data = MEM_new<GaussianBlurVars>("gaussianblurvars");
27 strip->effectdata = data;
28 data->size_x = 9.0f;
29 data->size_y = 9.0f;
30}
31
32static void free_gaussian_blur_effect(Strip *strip, const bool /*do_id_user*/)
33{
34 if (strip->effectdata) {
35 GaussianBlurVars *data = static_cast<GaussianBlurVars *>(strip->effectdata);
36 MEM_delete(data);
37 strip->effectdata = nullptr;
38 }
39}
40
41static StripEarlyOut early_out_gaussian_blur(const Strip *strip, float /*fac*/)
42{
43 GaussianBlurVars *data = static_cast<GaussianBlurVars *>(strip->effectdata);
44 if (data->size_x == 0.0f && data->size_y == 0) {
46 }
48}
49
50template<typename T>
51static void gaussian_blur_x(const Span<float> gaussian,
52 int half_size,
53 int start_line,
54 int width,
55 int height,
56 int /*frame_height*/,
57 const T *rect,
58 T *dst)
59{
60 dst += int64_t(start_line) * width * 4;
61 for (int y = start_line; y < start_line + height; y++) {
62 for (int x = 0; x < width; x++) {
63 float4 accum(0.0f);
64 float accum_weight = 0.0f;
65
66 int xmin = math::max(x - half_size, 0);
67 int xmax = math::min(x + half_size, width - 1);
68 for (int nx = xmin, index = (xmin - x) + half_size; nx <= xmax; nx++, index++) {
69 float weight = gaussian[index];
70 int offset = (y * width + nx) * 4;
71 accum += float4(rect + offset) * weight;
72 accum_weight += weight;
73 }
74 accum *= (1.0f / accum_weight);
75 if constexpr (math::is_math_float_type<T>) {
76 dst[0] = accum[0];
77 dst[1] = accum[1];
78 dst[2] = accum[2];
79 dst[3] = accum[3];
80 }
81 else {
82 dst[0] = accum[0] + 0.5f;
83 dst[1] = accum[1] + 0.5f;
84 dst[2] = accum[2] + 0.5f;
85 dst[3] = accum[3] + 0.5f;
86 }
87 dst += 4;
88 }
89 }
90}
91
92template<typename T>
93static void gaussian_blur_y(const Span<float> gaussian,
94 int half_size,
95 int start_line,
96 int width,
97 int height,
98 int frame_height,
99 const T *rect,
100 T *dst)
101{
102 dst += int64_t(start_line) * width * 4;
103 for (int y = start_line; y < start_line + height; y++) {
104 for (int x = 0; x < width; x++) {
105 float4 accum(0.0f);
106 float accum_weight = 0.0f;
107 int ymin = math::max(y - half_size, 0);
108 int ymax = math::min(y + half_size, frame_height - 1);
109 for (int ny = ymin, index = (ymin - y) + half_size; ny <= ymax; ny++, index++) {
110 float weight = gaussian[index];
111 int offset = (ny * width + x) * 4;
112 accum += float4(rect + offset) * weight;
113 accum_weight += weight;
114 }
115 accum *= (1.0f / accum_weight);
116 if constexpr (math::is_math_float_type<T>) {
117 dst[0] = accum[0];
118 dst[1] = accum[1];
119 dst[2] = accum[2];
120 dst[3] = accum[3];
121 }
122 else {
123 dst[0] = accum[0] + 0.5f;
124 dst[1] = accum[1] + 0.5f;
125 dst[2] = accum[2] + 0.5f;
126 dst[3] = accum[3] + 0.5f;
127 }
128 dst += 4;
129 }
130 }
131}
132
134 SeqRenderState * /*state*/,
135 Strip *strip,
136 float /*timeline_frame*/,
137 float /*fac*/,
138 const SeqResult &ibuf1,
139 const SeqResult & /*ibuf2*/)
140{
142 /* Create blur kernel weights. */
143 const GaussianBlurVars *data = static_cast<const GaussianBlurVars *>(strip->effectdata);
144
145 const float size_scale = seq::get_render_scale_factor(*context);
146 const float size_x = data->size_x * size_scale;
147 const float size_y = data->size_y * size_scale;
148
149 const int half_size_x = int(size_x + 0.5f);
150 const int half_size_y = int(size_y + 0.5f);
151 Array<float> gaussian_x = make_gaussian_blur_kernel(size_x, half_size_x);
152 Array<float> gaussian_y = make_gaussian_blur_kernel(size_y, half_size_y);
153
154 const int width = context->rectx;
155 const int height = context->recty;
156
157 /* Horizontal blur: create output, blur ibuf1 into it. */
158 SeqResult out = prepare_effect_imbufs(context, ibuf1, {});
159
160 {
161 const bool is_float = out.image->float_data();
162 float *out_float = out.image->float_data_for_write();
163 uint8_t *out_byte = out.image->byte_data_for_write();
164 threading::parallel_for(IndexRange(context->recty), 32, [&](const IndexRange y_range) {
165 const int y_first = y_range.first();
166 const int y_size = y_range.size();
167 if (is_float) {
168 gaussian_blur_x(gaussian_x,
169 half_size_x,
170 y_first,
171 width,
172 y_size,
173 height,
174 ibuf1.image->float_data(),
175 out_float);
176 }
177 else {
178 gaussian_blur_x(gaussian_x,
179 half_size_x,
180 y_first,
181 width,
182 y_size,
183 height,
184 ibuf1.image->byte_data(),
185 out_byte);
186 }
187 });
188 }
189 /* Vertical blur: create output, blur previous output into it. */
190 SeqResult vin = out;
191 out = prepare_effect_imbufs(context, vin, {});
192 {
193 const bool is_float = out.image->float_data();
194 float *out_float = out.image->float_data_for_write();
195 uint8_t *out_byte = out.image->byte_data_for_write();
196 threading::parallel_for(IndexRange(context->recty), 32, [&](const IndexRange y_range) {
197 const int y_first = y_range.first();
198 const int y_size = y_range.size();
199 if (is_float) {
200 gaussian_blur_y(gaussian_y,
201 half_size_y,
202 y_first,
203 width,
204 y_size,
205 height,
206 vin.image->float_data(),
207 out_float);
208 }
209 else {
210 gaussian_blur_y(gaussian_y,
211 half_size_y,
212 y_first,
213 width,
214 y_size,
215 height,
216 vin.image->byte_data(),
217 out_byte);
218 }
219 });
220 }
221 /* Free the first output. */
222 IMB_freeImBuf(vin.image);
223
224 return out;
225}
226
234
235} // namespace blender::seq
#define PRF_scope_with_name(ui_name, category)
constexpr IndexRange()=default
#define T
constexpr bool is_math_float_type
T min(const T &a, const T &b)
T max(const T &a, const T &b)
static void init_gaussian_blur_effect(Strip *strip)
void gaussian_blur_effect_get_handle(EffectHandle &rval)
static void free_gaussian_blur_effect(Strip *strip, const bool)
float get_render_scale_factor(eSpaceSeq_Proxy_RenderSize render_size, short scene_render_scale)
Definition render.cc:2249
Array< float > make_gaussian_blur_kernel(float rad, int size)
Definition effects.cc:77
SeqResult prepare_effect_imbufs(const RenderData *context, const SeqResult &ibuf1, const SeqResult &ibuf2, bool uninitialized_pixels)
Definition effects.cc:34
static void gaussian_blur_x(const Span< float > gaussian, int half_size, int start_line, int width, int height, int, const T *rect, T *dst)
static SeqResult do_gaussian_blur_effect(const RenderData *context, SeqRenderState *, Strip *strip, float, float, const SeqResult &ibuf1, const SeqResult &)
static StripEarlyOut early_out_gaussian_blur(const Strip *strip, float)
static void gaussian_blur_y(const Span< float > gaussian, int half_size, int start_line, int width, int height, int frame_height, const T *rect, T *dst)
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
__int64 int64_t
Definition BKE_text.h:90
VecBase< float, 4 > float4
void IMB_freeImBuf(ImBuf *ibuf)
BMesh const char void * data
unsigned char uint8_t
Definition BKE_text.h:79
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