Blender V5.3
MOD_brightness_contrast.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 <cfloat>
10
11#include "BLI_math_base_c.hh"
12#include "BLI_math_vector.hh"
13
14#include "BLT_translation.hh"
15
16#include "DNA_sequence_types.h"
17
18#include "PRF_profile.hh"
19
20#include "SEQ_modifier.hh"
21#include "SEQ_render.hh"
22
23#include "UI_interface.hh"
25
26#include "modifier.hh"
27#include "render.hh"
28
29namespace blender::seq {
30
32 float mul;
33 float add;
34
35 template<typename ImageT, typename MaskSampler>
36 void apply(ImageT *image, MaskSampler &mask, int image_x, IndexRange y_range)
37 {
38 image += y_range.first() * image_x * 4;
39 for (int64_t y : y_range) {
40 mask.begin_row(y);
41 for ([[maybe_unused]] int64_t x : IndexRange(image_x)) {
42 /* NOTE: arguably incorrect usage of "raw" values, should be un-premultiplied.
43 * Not changing behavior for now, but would be good to fix someday. */
45
47 result = input * this->mul + this->add;
48 result.w = input.w;
49
50 mask.apply_mask(input, result);
52 image += 4;
53 }
54 }
55 }
56};
57
59{
60 PRF_scope_with_name("SeqModBrightContrast", ProfileCategory::Draw);
61 ensure_ibuf_is_sequencer_space(context.render_data.scene, context.result.image, false);
62 ImBuf *mask = modifier_render_mask_input(context, *smd);
63
64 const BrightContrastModifierData *bcmd = reinterpret_cast<BrightContrastModifierData *>(smd);
65
67
68 /* The algorithm is by Werner D. Streidt
69 * (http://visca.com/ffactory/archives/5-99/msg00021.html)
70 * Extracted from OpenCV `demhist.cpp`. */
71 const float brightness = bcmd->bright / 100.0f;
72 const float contrast = bcmd->contrast;
73 float delta = contrast / 200.0f;
74
75 if (contrast > 0) {
76 op.mul = 1.0f - delta * 2.0f;
77 op.mul = 1.0f / max_ff(op.mul, FLT_EPSILON);
78 op.add = op.mul * (brightness - delta);
79 }
80 else {
81 delta *= -1;
82 op.mul = max_ff(1.0f - delta * 2.0f, 0.0f);
83 op.add = op.mul * brightness + delta;
84 }
85
86 apply_modifier_op(op, context.result.image, mask, context.transform);
87 if (mask != nullptr) {
89 }
90}
91
92static void brightcontrast_panel_draw(const bContext *C, Panel *panel)
93{
94 ui::Layout &layout = *panel->layout;
96
97 layout.use_property_split_set(true);
98
99 layout.prop(ptr, "bright", UI_ITEM_NONE, std::nullopt, ICON_NONE);
100 layout.prop(ptr, "contrast", UI_ITEM_NONE, std::nullopt, ICON_NONE);
101
102 if (ui::Layout *mask_input_layout = layout.panel_prop(
103 C, ptr, "open_mask_input_panel", IFACE_("Mask Input")))
104 {
105 draw_mask_input_type_settings(C, *mask_input_layout, ptr);
106 }
107}
108
113
115 /*idname*/ "BrightContrast",
116 /*name*/ CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "Brightness/Contrast"),
117 /*struct_name*/ "BrightContrastModifierData",
118 /*struct_size*/ sizeof(BrightContrastModifierData),
119 /*init_data*/ nullptr,
120 /*free_data*/ nullptr,
121 /*copy_data*/ nullptr,
122 /*apply*/ brightcontrast_apply,
123 /*panel_register*/ brightcontrast_register,
124 /*blend_write*/ nullptr,
125 /*blend_read*/ nullptr,
126};
127
128}; // 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
constexpr int64_t first() const
constexpr IndexRange()=default
#define FLT_EPSILON
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
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
StripModifierTypeInfo seqModifierType_BrightContrast
static void brightcontrast_register(ARegionType *region_type)
float4 load_pixel_raw(const uchar *ptr)
static void brightcontrast_panel_draw(const bContext *C, Panel *panel)
static void brightcontrast_apply(ModifierApplyContext &context, StripModifierData *smd)
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)
PointerRNA * panel_custom_data_get(const Panel *panel)
__int64 int64_t
Definition BKE_text.h:90
VecBase< float, 4 > float4
void IMB_freeImBuf(ImBuf *ibuf)
MINLINE float max_ff(float a, float b)
@ eSeqModifierType_BrightContrast
ui::Layout * layout
void apply(ImageT *image, MaskSampler &mask, int image_x, IndexRange y_range)
void use_property_split_set(bool is_sep)
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)
PanelLayout panel_prop(const bContext *C, PointerRNA *open_prop_owner, StringRefNull open_prop_name)