Blender V5.3
effect_glow.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_vector.hh"
10#include "BLI_task.hh"
11
12#include "DNA_scene_types.h"
13#include "DNA_sequence_types.h"
14
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
26static void glow_blur_bitmap(
27 const float4 *src, float4 *map, int width, int height, float blur, int quality)
28{
29 /* If we're not really blurring, bail out */
30 if (blur <= 0) {
31 return;
32 }
33
34 /* If result would be no blurring, early out. */
35 const int halfWidth = ((quality + 1) * blur);
36 if (halfWidth == 0) {
37 return;
38 }
39
40 Array<float4> temp(width * height);
41
42 /* Initialize the gaussian filter.
43 * TODO: use code from #filter_kernel_value. */
44 Array<float> filter(halfWidth * 2);
45 const float k = -1.0f / (2.0f * float(M_PI) * blur * blur);
46 float weight = 0;
47 for (int ix = 0; ix < halfWidth; ix++) {
48 weight = exp(k * (ix * ix));
49 filter[halfWidth - ix] = weight;
50 filter[halfWidth + ix] = weight;
51 }
52 filter[0] = weight;
53 /* Normalize the array */
54 float fval = 0;
55 for (int ix = 0; ix < halfWidth * 2; ix++) {
56 fval += filter[ix];
57 }
58 for (int ix = 0; ix < halfWidth * 2; ix++) {
59 filter[ix] /= fval;
60 }
61
62 /* Blur the rows: read map, write temp */
63 threading::parallel_for(IndexRange(height), 32, [&](const IndexRange y_range) {
64 for (const int y : y_range) {
65 for (int x = 0; x < width; x++) {
66 float4 curColor = float4(0.0f);
67 int xmin = math::max(x - halfWidth, 0);
68 int xmax = math::min(x + halfWidth, width);
69 for (int nx = xmin, index = (xmin - x) + halfWidth; nx < xmax; nx++, index++) {
70 curColor += map[nx + y * width] * filter[index];
71 }
72 temp[x + y * width] = curColor;
73 }
74 }
75 });
76
77 /* Blur the columns: read temp, write map */
78 threading::parallel_for(IndexRange(width), 32, [&](const IndexRange x_range) {
79 const float4 one = float4(1.0f);
80 for (const int x : x_range) {
81 for (int y = 0; y < height; y++) {
82 float4 curColor = float4(0.0f);
83 int ymin = math::max(y - halfWidth, 0);
84 int ymax = math::min(y + halfWidth, height);
85 for (int ny = ymin, index = (ymin - y) + halfWidth; ny < ymax; ny++, index++) {
86 curColor += temp[x + ny * width] * filter[index];
87 }
88 if (src != nullptr) {
89 curColor = math::min(one, src[x + y * width] + curColor);
90 }
91 map[x + y * width] = curColor;
92 }
93 }
94 });
95}
96
98 float4 *out,
99 int width,
100 int height,
101 float threshold,
102 float boost,
103 float clamp)
104{
105 threading::parallel_for(IndexRange(height), 64, [&](const IndexRange y_range) {
106 const float4 clampv = float4(clamp);
107 for (const int y : y_range) {
108 int index = y * width;
109 for (int x = 0; x < width; x++, index++) {
110
111 /* Isolate the intensity */
112 float intensity = (in[index].x + in[index].y + in[index].z - threshold);
113 float4 val;
114 if (intensity > 0) {
115 val = math::min(clampv, in[index] * (boost * intensity));
116 }
117 else {
118 val = float4(0.0f);
119 }
120 out[index] = val;
121 }
122 }
123 });
124}
125
126static void init_glow_effect(Strip *strip)
127{
128 GlowVars *data = MEM_new<GlowVars>("glowvars");
129 strip->effectdata = data;
130 data->fMini = 0.25f;
131 data->fClamp = 1.0f;
132 data->fBoost = 0.5f;
133 data->dDist = 3.0f;
134 data->dQuality = 3;
135 data->bNoComp = 0;
136}
137
138static void free_glow_effect(Strip *strip, const bool /*do_id_user*/)
139{
140 if (strip->effectdata) {
141 GlowVars *data = static_cast<GlowVars *>(strip->effectdata);
142 MEM_delete(data);
143 strip->effectdata = nullptr;
144 }
145}
146
147static void do_glow_effect_byte(Strip *strip,
148 int render_size,
149 float fac,
150 int x,
151 int y,
152 const uchar *rect1,
153 const uchar * /*rect2*/,
154 uchar *out)
155{
156 GlowVars *glow = static_cast<GlowVars *>(strip->effectdata);
157
158 Array<float4> inbuf(x * y);
159 Array<float4> outbuf(x * y);
160
161 IMB_colormanagement_transform_byte_to_float(*inbuf.data(), rect1, x, y, 4, "sRGB", "sRGB");
162
164 inbuf.data(), outbuf.data(), x, y, glow->fMini * 3.0f, glow->fBoost * fac, glow->fClamp);
165 glow_blur_bitmap(glow->bNoComp ? nullptr : inbuf.data(),
166 outbuf.data(),
167 x,
168 y,
169 glow->dDist * (render_size / 100.0f),
170 glow->dQuality);
171
172 threading::parallel_for(IndexRange(y), 64, [&](const IndexRange y_range) {
173 size_t offset = y_range.first() * x;
175 out + offset * 4, *(outbuf.data() + offset), 4, 0.0f, true, x, y_range.size(), x);
176 });
177}
178
179static void do_glow_effect_float(Strip *strip,
180 int render_size,
181 float fac,
182 int x,
183 int y,
184 const float *rect1,
185 const float * /*rect2*/,
186 float *out)
187{
188 float4 *outbuf = reinterpret_cast<float4 *>(out);
189 const float4 *inbuf = reinterpret_cast<const float4 *>(rect1);
190 GlowVars *glow = static_cast<GlowVars *>(strip->effectdata);
191
193 inbuf, outbuf, x, y, glow->fMini * 3.0f, glow->fBoost * fac, glow->fClamp);
194 glow_blur_bitmap(glow->bNoComp ? nullptr : inbuf,
195 outbuf,
196 x,
197 y,
198 glow->dDist * (render_size / 100.0f),
199 glow->dQuality);
200}
201
202static SeqResult do_glow_effect(const RenderData *context,
203 SeqRenderState * /*state*/,
204 Strip *strip,
205 float /*timeline_frame*/,
206 float fac,
207 const SeqResult &ibuf1,
208 const SeqResult & /*ibuf2*/)
209{
211 SeqResult out = prepare_effect_imbufs(context, ibuf1, {});
212
213 int render_size = 100 * context->rectx / context->scene->r.xsch;
214
215 if (out.image->float_data()) {
217 render_size,
218 fac,
219 context->rectx,
220 context->recty,
221 ibuf1.image->float_data(),
222 nullptr,
223 out.image->float_data_for_write());
224 }
225 else {
227 render_size,
228 fac,
229 context->rectx,
230 context->recty,
231 ibuf1.image->byte_data(),
232 nullptr,
233 out.image->byte_data_for_write());
234 }
235
236 return out;
237}
238
240{
241 rval.init = init_glow_effect;
242 rval.free = free_glow_effect;
243 rval.execute = do_glow_effect;
244}
245
246} // namespace blender::seq
#define M_PI
#define PRF_scope_with_name(ui_name, category)
const T * data() const
Definition BLI_array.hh:323
constexpr int64_t first() const
constexpr int64_t size() const
constexpr IndexRange()=default
#define exp
constexpr T clamp(T, U, U) RET
T min(const T &a, const T &b)
T max(const T &a, const T &b)
static void do_glow_effect_float(Strip *strip, int render_size, float fac, int x, int y, const float *rect1, const float *, float *out)
static void init_glow_effect(Strip *strip)
static SeqResult do_glow_effect(const RenderData *context, SeqRenderState *, Strip *strip, float, float fac, const SeqResult &ibuf1, const SeqResult &)
SeqResult prepare_effect_imbufs(const RenderData *context, const SeqResult &ibuf1, const SeqResult &ibuf2, bool uninitialized_pixels)
Definition effects.cc:34
static void glow_blur_bitmap(const float4 *src, float4 *map, int width, int height, float blur, int quality)
static void free_glow_effect(Strip *strip, const bool)
static void do_glow_effect_byte(Strip *strip, int render_size, float fac, int x, int y, const uchar *rect1, const uchar *, uchar *out)
static void blur_isolate_highlights(const float4 *in, float4 *out, int width, int height, float threshold, float boost, float clamp)
void glow_effect_get_handle(EffectHandle &rval)
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
void IMB_colormanagement_transform_byte_to_float(float *float_buffer, const unsigned char *byte_buffer, int width, int height, int channels, const char *from_colorspace, const char *to_colorspace)
void IMB_buffer_byte_from_float(unsigned char *dest, const float *src, int src_channels, float dither, bool predivide, int width, int height, int stride, int start_y=0)
Definition conversion.cc:50
VecBase< float, 4 > float4
unsigned char uchar
Definition BKE_text.h:58
BMesh const char void * data
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const uint8_t * byte_data() const
const float * float_data() const
void(*) free(Strip *strip, bool do_id_user)
Definition effects.hh:47
void(*) init(Strip *strip)
Definition effects.hh:41
SeqResult(*) execute(const RenderData *context, SeqRenderState *state, Strip *strip, float timeline_frame, float fac, const SeqResult &input1, const SeqResult &input2)
Definition effects.hh:52