Blender V5.3
MOD_color_balance.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_math_base_c.hh"
10
11#include "BLT_translation.hh"
12
13#include "DNA_sequence_types.h"
14
15#include "PRF_profile.hh"
16
17#include "SEQ_modifier.hh"
18#include "SEQ_render.hh"
19
20#include "UI_interface.hh"
22
23#include "RNA_access.hh"
24
25#include "modifier.hh"
26#include "render.hh"
27
28namespace blender::seq {
29
30/* Lift-Gamma-Gain math. NOTE: lift is actually (2-lift). */
31static float color_balance_lgg(
32 float in, const float lift, const float gain, const float gamma, const float mul)
33{
34 float x = (((in - 1.0f) * lift) + 1.0f) * gain;
35
36 /* prevent NaN */
37 x = std::max(x, 0.0f);
38
39 x = powf(x, gamma) * mul;
41 return x;
42}
43
44/* Slope-Offset-Power (ASC CDL) math, see https://en.wikipedia.org/wiki/ASC_CDL */
45static float color_balance_sop(
46 float in, const float slope, const float offset, const float power, float mul)
47{
48 float x = in * slope + offset;
49
50 /* prevent NaN */
51 x = std::max(x, 0.0f);
52
53 x = powf(x, power);
54 x *= mul;
56 return x;
57}
58
63static constexpr int CB_TABLE_SIZE = 1024;
64
66 float lift, float gain, float gamma, float mul, float r_table[CB_TABLE_SIZE])
67{
68 for (int i = 0; i < CB_TABLE_SIZE; i++) {
69 float x = float(i) * (1.0f / (CB_TABLE_SIZE - 1.0f));
70 r_table[i] = color_balance_lgg(x, lift, gain, gamma, mul);
71 }
72}
73
75 float slope, float offset, float power, float mul, float r_table[CB_TABLE_SIZE])
76{
77 for (int i = 0; i < CB_TABLE_SIZE; i++) {
78 float x = float(i) * (1.0f / (CB_TABLE_SIZE - 1.0f));
79 r_table[i] = color_balance_sop(x, slope, offset, power, mul);
80 }
81}
82
84 int method;
88 float lut[3][CB_TABLE_SIZE];
89
90 /* Apply on a byte image via a table lookup. */
91 template<typename MaskSampler>
92 void apply(uchar *image, MaskSampler &mask, int image_x, IndexRange y_range)
93 {
94 image += y_range.first() * image_x * 4;
95 for (int64_t y : y_range) {
96 mask.begin_row(y);
97 for ([[maybe_unused]] int64_t x : IndexRange(image_x)) {
99
101 int p0 = int(input.x * (CB_TABLE_SIZE - 1.0f) + 0.5f);
102 int p1 = int(input.y * (CB_TABLE_SIZE - 1.0f) + 0.5f);
103 int p2 = int(input.z * (CB_TABLE_SIZE - 1.0f) + 0.5f);
104 result.x = this->lut[0][p0];
105 result.y = this->lut[1][p1];
106 result.z = this->lut[2][p2];
107 result.w = input.w;
108
109 mask.apply_mask(input, result);
111 image += 4;
112 }
113 }
114 }
115
116 /* Apply on a float image by doing full math. */
117 template<typename MaskSampler>
118 void apply(float *image, MaskSampler &mask, int image_x, IndexRange y_range)
119 {
120 image += y_range.first() * image_x * 4;
121 for (int64_t y : y_range) {
122 mask.begin_row(y);
123 if (this->method == SEQ_COLOR_BALANCE_METHOD_LIFTGAMMAGAIN) {
124 /* Lift/Gamma/Gain */
125 for ([[maybe_unused]] int64_t x : IndexRange(image_x)) {
127
130 input.x, this->lift.x, this->gain.x, this->gamma.x, this->multiplier);
132 input.y, this->lift.y, this->gain.y, this->gamma.y, this->multiplier);
134 input.z, this->lift.z, this->gain.z, this->gamma.z, this->multiplier);
135 result.w = input.w;
136
137 mask.apply_mask(input, result);
139 image += 4;
140 }
141 }
142 else if (this->method == SEQ_COLOR_BALANCE_METHOD_SLOPEOFFSETPOWER) {
143 /* Slope/Offset/Power */
144 for ([[maybe_unused]] int64_t x : IndexRange(image_x)) {
146
149 input.x, this->slope.x, this->offset.x, this->power.x, this->multiplier);
151 input.y, this->slope.y, this->offset.y, this->power.y, this->multiplier);
153 input.z, this->slope.z, this->offset.z, this->power.z, this->multiplier);
154 result.w = input.w;
155
156 mask.apply_mask(input, result);
158 image += 4;
159 }
160 }
161 else {
163 }
164 }
165 }
166
168 {
170
171 this->lift = 2.0f - float3(data.lift);
173 for (int c = 0; c < 3; c++) {
174 /* tweak to give more subtle results
175 * values above 1.0 are scaled */
176 if (this->lift[c] > 1.0f) {
177 this->lift[c] = powf(this->lift[c] - 1.0f, 2.0f) + 1.0f;
178 }
179 this->lift[c] = 2.0f - this->lift[c];
180 }
181 }
182
183 this->gain = float3(data.gain);
185 this->gain = math::rcp(math::max(this->gain, float3(1.0e-6f)));
186 }
187
188 this->gamma = float3(data.gamma);
190 this->gamma = math::rcp(math::max(this->gamma, float3(1.0e-6f)));
191 }
192 }
193
195 {
197
198 this->slope = float3(data.slope);
200 this->slope = math::rcp(math::max(this->slope, float3(1.0e-6f)));
201 }
202
203 this->offset = float3(data.offset) - 1.0f;
205 this->offset = -this->offset;
206 }
207
208 this->power = float3(data.power);
210 this->power = math::rcp(math::max(this->power, float3(1.0e-6f)));
211 }
212 }
213
214 void init(const ColorBalanceModifierData &data, bool byte_image)
215 {
216 this->multiplier = data.color_multiply;
217 this->method = data.color_balance.method;
218
219 if (this->method == SEQ_COLOR_BALANCE_METHOD_LIFTGAMMAGAIN) {
220 init_lgg(data.color_balance);
221 if (byte_image) {
222 for (int c = 0; c < 3; c++) {
224 this->lift[c], this->gain[c], this->gamma[c], this->multiplier, this->lut[c]);
225 }
226 }
227 }
228 else if (this->method == SEQ_COLOR_BALANCE_METHOD_SLOPEOFFSETPOWER) {
229 init_sop(data.color_balance);
230 if (byte_image) {
231 for (int c = 0; c < 3; c++) {
233 this->slope[c], this->offset[c], this->power[c], this->multiplier, this->lut[c]);
234 }
235 }
236 }
237 else {
239 }
240 }
241};
242
244{
245 ColorBalanceModifierData *cbmd = reinterpret_cast<ColorBalanceModifierData *>(smd);
246
247 cbmd->color_multiply = 1.0f;
249
250 for (int c = 0; c < 3; c++) {
251 cbmd->color_balance.lift[c] = 1.0f;
252 cbmd->color_balance.gamma[c] = 1.0f;
253 cbmd->color_balance.gain[c] = 1.0f;
254 cbmd->color_balance.slope[c] = 1.0f;
255 cbmd->color_balance.offset[c] = 1.0f;
256 cbmd->color_balance.power[c] = 1.0f;
257 }
258}
259
261{
262 PRF_scope_with_name("SeqModColorBalance", ProfileCategory::Draw);
263 ensure_ibuf_is_sequencer_space(context.render_data.scene, context.result.image, false);
264 ImBuf *mask = modifier_render_mask_input(context, *smd);
265
266 const ColorBalanceModifierData *cbmd = reinterpret_cast<const ColorBalanceModifierData *>(smd);
267
269 op.init(*cbmd, context.result.image->byte_data() != nullptr);
270 apply_modifier_op(op, context.result.image, mask, context.transform);
271 if (mask != nullptr) {
273 }
274}
275
276static void colorBalance_panel_draw(const bContext *C, Panel *panel)
277{
278 ui::Layout &layout = *panel->layout;
280
281 PointerRNA color_balance = RNA_pointer_get(ptr, "color_balance");
282 const int correction_method = RNA_enum_get(&color_balance, "correction_method");
283
284 layout.use_property_split_set(true);
285
286 layout.prop(ptr, "color_multiply", UI_ITEM_NONE, std::nullopt, ICON_NONE);
287 layout.prop(&color_balance, "correction_method", UI_ITEM_NONE, std::nullopt, ICON_NONE);
288
289 ui::Layout &flow = layout.grid_flow(true, 0, true, false, false);
290 flow.use_property_split_set(false);
291 if (correction_method == SEQ_COLOR_BALANCE_METHOD_LIFTGAMMAGAIN) {
292 /* Split into separate scopes to be able to reuse "split" and "col" variable names. */
293 {
294 ui::Layout &split = flow.column(false).split(0.35f, false);
295 ui::Layout &col = split.column(true);
296 col.label(IFACE_("Lift"), ICON_NONE);
297 col.separator();
298 col.separator();
299 col.prop(&color_balance, "lift", UI_ITEM_NONE, "", ICON_NONE);
300 col.prop(
301 &color_balance, "invert_lift", UI_ITEM_NONE, IFACE_("Invert"), ICON_ARROW_LEFTRIGHT);
302 template_color_picker(&split, &color_balance, "lift", true, false, false, true);
303 col.separator();
304 }
305 {
306 ui::Layout &split = flow.column(false).split(0.35f, false);
307 ui::Layout &col = split.column(true);
308 col.label(IFACE_("Gamma"), ICON_NONE);
309 col.separator();
310 col.separator();
311 col.prop(&color_balance, "gamma", UI_ITEM_NONE, "", ICON_NONE);
312 col.prop(
313 &color_balance, "invert_gamma", UI_ITEM_NONE, IFACE_("Invert"), ICON_ARROW_LEFTRIGHT);
314 template_color_picker(&split, &color_balance, "gamma", true, false, true, true);
315 col.separator();
316 }
317 {
318 ui::Layout &split = flow.column(false).split(0.35f, false);
319 ui::Layout &col = split.column(true);
320 col.label(IFACE_("Gain"), ICON_NONE);
321 col.separator();
322 col.separator();
323 col.prop(&color_balance, "gain", UI_ITEM_NONE, "", ICON_NONE);
324 col.prop(
325 &color_balance, "invert_gain", UI_ITEM_NONE, IFACE_("Invert"), ICON_ARROW_LEFTRIGHT);
326 template_color_picker(&split, &color_balance, "gain", true, false, true, true);
327 }
328 }
329 else if (correction_method == SEQ_COLOR_BALANCE_METHOD_SLOPEOFFSETPOWER) {
330 {
331 ui::Layout &split = flow.column(false).split(0.35f, false);
332 ui::Layout &col = split.column(true);
333 col.label(IFACE_("Offset"), ICON_NONE);
334 col.separator();
335 col.separator();
336 col.prop(&color_balance, "offset", UI_ITEM_NONE, "", ICON_NONE);
337 col.prop(
338 &color_balance, "invert_offset", UI_ITEM_NONE, IFACE_("Invert"), ICON_ARROW_LEFTRIGHT);
339 template_color_picker(&split, &color_balance, "offset", true, false, false, true);
340 col.separator();
341 }
342 {
343 ui::Layout &split = flow.column(false).split(0.35f, false);
344 ui::Layout &col = split.column(true);
345 col.label(IFACE_("Power"), ICON_NONE);
346 col.separator();
347 col.separator();
348 col.prop(&color_balance, "power", UI_ITEM_NONE, "", ICON_NONE);
349 col.prop(
350 &color_balance, "invert_power", UI_ITEM_NONE, IFACE_("Invert"), ICON_ARROW_LEFTRIGHT);
351 template_color_picker(&split, &color_balance, "power", true, false, false, true);
352 col.separator();
353 }
354 {
355 ui::Layout &split = flow.column(false).split(0.35f, false);
356 ui::Layout &col = split.column(true);
357 col.label(IFACE_("Slope"), ICON_NONE);
358 col.separator();
359 col.separator();
360 col.prop(&color_balance, "slope", UI_ITEM_NONE, "", ICON_NONE);
361 col.prop(
362 &color_balance, "invert_slope", UI_ITEM_NONE, IFACE_("Invert"), ICON_ARROW_LEFTRIGHT);
363 template_color_picker(&split, &color_balance, "slope", true, false, false, true);
364 }
365 }
366 else {
368 }
369
370 if (ui::Layout *mask_input_layout = layout.panel_prop(
371 C, ptr, "open_mask_input_panel", IFACE_("Mask Input")))
372 {
373 draw_mask_input_type_settings(C, *mask_input_layout, ptr);
374 }
375}
376
381
383 /*idname*/ "ColorBalance",
384 /*name*/ CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "Color Balance"),
385 /*struct_name*/ "ColorBalanceModifierData",
386 /*struct_size*/ sizeof(ColorBalanceModifierData),
387 /*init_data*/ colorBalance_init_data,
388 /*free_data*/ nullptr,
389 /*copy_data*/ nullptr,
390 /*apply*/ colorBalance_apply,
391 /*panel_register*/ colorBalance_register,
392 /*blend_write*/ nullptr,
393 /*blend_read*/ nullptr,
394};
395
396}; // namespace blender::seq
#define CTX_N_(context, msgid)
#define BLT_I18NCONTEXT_ID_SEQUENCE
#define IFACE_(msgid)
#define PRF_scope_with_name(ui_name, category)
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define CLAMP(a, b, c)
#define BLI_assert_unreachable()
constexpr int64_t first() const
constexpr IndexRange()=default
#define powf(x, y)
#define FLT_MAX
#define FLT_MIN
ccl_device_inline float2 power(const float2 v, const float e)
void split(const std::string &s, const char delim, std::vector< std::string > &tokens)
Definition abc_util.cc:68
T rcp(const T &a)
T max(const T &a, const T &b)
ImBuf * modifier_render_mask_input(const ModifierApplyContext &context, const StripModifierData &smd)
void ensure_ibuf_is_sequencer_space(const Scene *scene, ImBuf *ibuf, bool make_float)
Definition render.cc:170
static void colorBalance_init_data(StripModifierData *smd)
static constexpr int CB_TABLE_SIZE
static float color_balance_lgg(float in, const float lift, const float gain, const float gamma, const float mul)
void apply_modifier_op(T &op, ImBuf *ibuf, const ImBuf *mask, const float3x3 &mask_transform)
Definition modifier.hh:277
static float color_balance_sop(float in, const float slope, const float offset, const float power, float mul)
void store_pixel_premul(float4 pix, uchar *ptr)
static void make_cb_table_sop(float slope, float offset, float power, float mul, float r_table[CB_TABLE_SIZE])
static void make_cb_table_lgg(float lift, float gain, float gamma, float mul, float r_table[CB_TABLE_SIZE])
static void colorBalance_panel_draw(const bContext *C, Panel *panel)
static void colorBalance_apply(ModifierApplyContext &context, StripModifierData *smd)
StripModifierTypeInfo seqModifierType_ColorBalance
float4 load_pixel_premul(const uchar *ptr)
void draw_mask_input_type_settings(const bContext *C, ui::Layout &layout, PointerRNA *ptr)
static void colorBalance_register(ARegionType *region_type)
PanelType * modifier_panel_register(ARegionType *region_type, const eStripModifierType type, PanelDrawFn draw)
PointerRNA * panel_custom_data_get(const Panel *panel)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
int RNA_enum_get(PointerRNA *ptr, const char *name)
@ SEQ_COLOR_BALANCE_INVERSE_POWER
@ SEQ_COLOR_BALANCE_INVERSE_LIFT
@ SEQ_COLOR_BALANCE_INVERSE_SLOPE
@ SEQ_COLOR_BALANCE_INVERSE_GAIN
@ SEQ_COLOR_BALANCE_INVERSE_GAMMA
@ SEQ_COLOR_BALANCE_INVERSE_OFFSET
@ SEQ_COLOR_BALANCE_METHOD_LIFTGAMMAGAIN
@ SEQ_COLOR_BALANCE_METHOD_SLOPEOFFSETPOWER
VecBase< float, 4 > float4
void IMB_freeImBuf(ImBuf *ibuf)
unsigned char uchar
Definition BKE_text.h:58
BMesh const char void * data
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
VecBase< float, 3 > float3
@ eSeqModifierType_ColorBalance
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ui::Layout * layout
eModColorBalanceMethod method
void init_sop(const StripColorBalance &data)
void apply(uchar *image, MaskSampler &mask, int image_x, IndexRange y_range)
void init_lgg(const StripColorBalance &data)
void apply(float *image, MaskSampler &mask, int image_x, IndexRange y_range)
void init(const ColorBalanceModifierData &data, bool byte_image)
void use_property_split_set(bool is_sep)
void label(StringRef name, int icon)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
Layout & split(float percentage, bool align)
Layout & column(bool align)
void separator(float factor=1.0f, LayoutSeparatorType type=LayoutSeparatorType::Auto)
Layout & grid_flow(bool row_major, int columns_len, bool even_columns, bool even_rows, bool align)
PanelLayout panel_prop(const bContext *C, PointerRNA *open_prop_owner, StringRefNull open_prop_name)