Blender V5.3
node_composite_color_correction.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2006 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_math_base.hh"
6#include "BLI_math_vector.hh"
8
10
11#include "NOD_multi_function.hh"
12
14
15#include "GPU_material.hh"
16
17#include "COM_result.hh"
18
20
22
24{
25 b.is_function_node();
26 b.use_custom_socket_order();
27 b.allow_any_socket_order();
28
29 b.add_input<decl::Color>("Image"_ustr).default_value({1.0f, 1.0f, 1.0f, 1.0f}).hide_value();
30 b.add_output<decl::Color>("Image"_ustr).align_with_previous();
31
32 b.add_input<decl::Float>("Mask"_ustr).default_value(1.0f).min(0.0f).max(1.0f);
33
34 PanelDeclarationBuilder &master_panel = b.add_panel("Master"_ustr).default_closed(true);
35 master_panel.add_input<decl::Float>("Saturation"_ustr, "Master Saturation"_ustr)
36 .default_value(1.0f)
38 .min(0.0f)
39 .max(4.0f)
40 .description("Controls the saturation of the entire image");
41 master_panel.add_input<decl::Float>("Contrast"_ustr, "Master Contrast"_ustr)
42 .default_value(1.0f)
44 .min(0.0f)
45 .max(4.0f)
46 .description("Controls the contrast of the entire image");
47 master_panel.add_input<decl::Float>("Gamma"_ustr, "Master Gamma"_ustr)
48 .default_value(1.0f)
50 .min(0.0f)
51 .max(4.0f)
52 .description("Controls the gamma of the entire image");
53 master_panel.add_input<decl::Float>("Gain"_ustr, "Master Gain"_ustr)
54 .default_value(1.0f)
56 .min(0.0f)
57 .max(4.0f)
58 .description("Controls the gain of the entire image");
59 master_panel.add_input<decl::Float>("Offset"_ustr, "Master Offset"_ustr)
60 .default_value(0.0f)
62 .min(-1.0f)
63 .max(1.0f)
64 .description("Controls the offset of the entire image");
65
66 PanelDeclarationBuilder &highlights_panel = b.add_panel("Highlights"_ustr).default_closed(true);
67 highlights_panel.add_input<decl::Float>("Saturation"_ustr, "Highlights Saturation"_ustr)
68 .default_value(1.0f)
70 .min(0.0f)
71 .max(4.0f)
72 .description("Controls the saturation of the highlights");
73 highlights_panel.add_input<decl::Float>("Contrast"_ustr, "Highlights Contrast"_ustr)
74 .default_value(1.0f)
76 .min(0.0f)
77 .max(4.0f)
78 .description("Controls the contrast of the highlights");
79 highlights_panel.add_input<decl::Float>("Gamma"_ustr, "Highlights Gamma"_ustr)
80 .default_value(1.0f)
82 .min(0.0f)
83 .max(4.0f)
84 .description("Controls the gamma of the highlights");
85 highlights_panel.add_input<decl::Float>("Gain"_ustr, "Highlights Gain"_ustr)
86 .default_value(1.0f)
88 .min(0.0f)
89 .max(4.0f)
90 .description("Controls the gain of the highlights");
91 highlights_panel.add_input<decl::Float>("Offset"_ustr, "Highlights Offset"_ustr)
92 .default_value(0.0f)
94 .min(-1.0f)
95 .max(1.0f)
96 .description("Controls the offset of the highlights");
97
98 PanelDeclarationBuilder &midtones_panel = b.add_panel("Midtones"_ustr).default_closed(true);
99 midtones_panel.add_input<decl::Float>("Saturation"_ustr, "Midtones Saturation"_ustr)
100 .default_value(1.0f)
102 .min(0.0f)
103 .max(4.0f)
104 .description("Controls the saturation of the midtones");
105 midtones_panel.add_input<decl::Float>("Contrast"_ustr, "Midtones Contrast"_ustr)
106 .default_value(1.0f)
108 .min(0.0f)
109 .max(4.0f)
110 .description("Controls the contrast of the midtones");
111 midtones_panel.add_input<decl::Float>("Gamma"_ustr, "Midtones Gamma"_ustr)
112 .default_value(1.0f)
114 .min(0.0f)
115 .max(4.0f)
116 .description("Controls the gamma of the midtones");
117 midtones_panel.add_input<decl::Float>("Gain"_ustr, "Midtones Gain"_ustr)
118 .default_value(1.0f)
120 .min(0.0f)
121 .max(4.0f)
122 .description("Controls the gain of the midtones");
123 midtones_panel.add_input<decl::Float>("Offset"_ustr, "Midtones Offset"_ustr)
124 .default_value(0.0f)
126 .min(-1.0f)
127 .max(1.0f)
128 .description("Controls the offset of the midtones");
129
130 PanelDeclarationBuilder &shadows_panel = b.add_panel("Shadows"_ustr).default_closed(true);
131 shadows_panel.add_input<decl::Float>("Saturation"_ustr, "Shadows Saturation"_ustr)
132 .default_value(1.0f)
134 .min(0.0f)
135 .max(4.0f)
136 .description("Controls the saturation of the shadows");
137 shadows_panel.add_input<decl::Float>("Contrast"_ustr, "Shadows Contrast"_ustr)
138 .default_value(1.0f)
140 .min(0.0f)
141 .max(4.0f)
142 .description("Controls the contrast of the shadows");
143 shadows_panel.add_input<decl::Float>("Gamma"_ustr, "Shadows Gamma"_ustr)
144 .default_value(1.0f)
146 .min(0.0f)
147 .max(4.0f)
148 .description("Controls the gamma of the shadows");
149 shadows_panel.add_input<decl::Float>("Gain"_ustr, "Shadows Gain"_ustr)
150 .default_value(1.0f)
152 .min(0.0f)
153 .max(4.0f)
154 .description("Controls the gain of the shadows");
155 shadows_panel.add_input<decl::Float>("Offset"_ustr, "Shadows Offset"_ustr)
156 .default_value(0.0f)
158 .min(-1.0f)
159 .max(1.0f)
160 .description("Controls the offset of the shadows");
161
162 PanelDeclarationBuilder &tonal_range_panel =
163 b.add_panel("Tonal Range"_ustr).default_closed(true);
164 tonal_range_panel.add_input<decl::Float>("Midtones Start"_ustr)
165 .default_value(0.2f)
167 .min(0.0f)
168 .max(1.0f)
169 .description(
170 "Specifies the luminance at which the midtones of the image start and the shadows end");
171 tonal_range_panel.add_input<decl::Float>("Midtones End"_ustr)
172 .default_value(0.7f)
174 .min(0.0f)
175 .max(1.0f)
176 .description(
177 "Specifies the luminance at which the midtones of the image end and the highlights "
178 "start");
179
180 PanelDeclarationBuilder &tone_range_panel = b.add_panel("Channels"_ustr).default_closed(true);
181 tone_range_panel.add_input<decl::Bool>("Red"_ustr, "Apply On Red"_ustr)
182 .default_value(true)
183 .description("If true, the correction will be applied on the red channel");
184 tone_range_panel.add_input<decl::Bool>("Green"_ustr, "Apply On Green"_ustr)
185 .default_value(true)
186 .description("If true, the correction will be applied on the green channel");
187 tone_range_panel.add_input<decl::Bool>("Blue"_ustr, "Apply On Blue"_ustr)
188 .default_value(true)
189 .description("If true, the correction will be applied on the blue channel");
190}
191
192using namespace blender::compositor;
193
194static int node_gpu_material(GPUMaterial *material,
195 bNode *node,
196 bNodeExecData * /*execdata*/,
197 GPUNodeStack *inputs,
198 GPUNodeStack *outputs)
199{
200 float luminance_coefficients[3];
202
203 return GPU_stack_link(material,
204 node,
205 "node_composite_color_correction",
206 inputs,
207 outputs,
208 GPU_constant(luminance_coefficients));
209}
210
212 const float &mask,
213 const float &master_saturation,
214 const float &master_contrast,
215 const float &master_gamma,
216 const float &master_gain,
217 const float &master_offset,
218 const float &highlights_saturation,
219 const float &highlights_contrast,
220 const float &highlights_gamma,
221 const float &highlights_gain,
222 const float &highlights_offset,
223 const float &midtones_saturation,
224 const float &midtones_contrast,
225 const float &midtones_gamma,
226 const float &midtones_gain,
227 const float &midtones_offset,
228 const float &shadows_saturation,
229 const float &shadows_contrast,
230 const float &shadows_gamma,
231 const float &shadows_gain,
232 const float &shadows_offset,
233 const float &start_midtones,
234 const float &end_midtones,
235 const bool &apply_on_red,
236 const bool &apply_on_green,
237 const bool &apply_on_blue,
238 const float3 &luminance_coefficients)
239{
240 const float margin = 0.10f;
241 const float margin_divider = 0.5f / margin;
242 float level = (color.x + color.y + color.z) / 3.0f;
243 float level_shadows = 0.0f;
244 float level_midtones = 0.0f;
245 float level_highlights = 0.0f;
246 if (level < (start_midtones - margin)) {
247 level_shadows = 1.0f;
248 }
249 else if (level < (start_midtones + margin)) {
250 level_midtones = ((level - start_midtones) * margin_divider) + 0.5f;
251 level_shadows = 1.0f - level_midtones;
252 }
253 else if (level < (end_midtones - margin)) {
254 level_midtones = 1.0f;
255 }
256 else if (level < (end_midtones + margin)) {
257 level_highlights = ((level - end_midtones) * margin_divider) + 0.5f;
258 level_midtones = 1.0f - level_highlights;
259 }
260 else {
261 level_highlights = 1.0f;
262 }
263
264 float contrast = level_shadows * shadows_contrast;
265 contrast += level_midtones * midtones_contrast;
266 contrast += level_highlights * highlights_contrast;
267 contrast *= master_contrast;
268 float saturation = level_shadows * shadows_saturation;
269 saturation += level_midtones * midtones_saturation;
270 saturation += level_highlights * highlights_saturation;
271 saturation *= master_saturation;
272 float gamma = level_shadows * shadows_gamma;
273 gamma += level_midtones * midtones_gamma;
274 gamma += level_highlights * highlights_gamma;
275 gamma *= master_gamma;
276 float gain = level_shadows * shadows_gain;
277 gain += level_midtones * midtones_gain;
278 gain += level_highlights * highlights_gain;
279 gain *= master_gain;
280 float offset = level_shadows * shadows_offset;
281 offset += level_midtones * midtones_offset;
282 offset += level_highlights * highlights_offset;
283 offset += master_offset;
284
285 float inverse_gamma = 1.0f / gamma;
286 float luma = math::dot(color.xyz(), luminance_coefficients);
287
288 float3 corrected = luma + saturation * (color.xyz() - luma);
289 corrected = 0.5f + (corrected - 0.5f) * contrast;
290 corrected = corrected * gain + offset;
291
292 /* Don't allow colors to go negative (or more negative than before) to keep them in gamut. */
293 corrected = math::max(math::min(color.xyz(), float3(0.0f)), corrected);
294
295 corrected = math::fallback_pow(corrected, inverse_gamma, corrected);
296 corrected = math::interpolate(color.xyz(), corrected, math::min(mask, 1.0f));
297
298 return float4(apply_on_red ? corrected.x : color.x,
299 apply_on_green ? corrected.y : color.y,
300 apply_on_blue ? corrected.z : color.z,
301 color.w);
302}
303
305
307{
308 float3 luminance_coefficients;
310
312 return mf::build::detail::build_multi_function_with_n_inputs_one_output<Color>(
313 "Color Correction",
314 [=](const Color &color,
315 const float &mask,
316 const float &master_saturation,
317 const float &master_contrast,
318 const float &master_gamma,
319 const float &master_gain,
320 const float &master_offset,
321 const float &highlights_saturation,
322 const float &highlights_contrast,
323 const float &highlights_gamma,
324 const float &highlights_gain,
325 const float &highlights_offset,
326 const float &midtones_saturation,
327 const float &midtones_contrast,
328 const float &midtones_gamma,
329 const float &midtones_gain,
330 const float &midtones_offset,
331 const float &shadows_saturation,
332 const float &shadows_contrast,
333 const float &shadows_gamma,
334 const float &shadows_gain,
335 const float &shadows_offset,
336 const float &start_midtones,
337 const float &end_midtones,
338 const bool &apply_on_red,
339 const bool &apply_on_green,
340 const bool &apply_on_blue) -> Color {
342 mask,
343 master_saturation,
344 master_contrast,
345 master_gamma,
346 master_gain,
347 master_offset,
348 highlights_saturation,
349 highlights_contrast,
350 highlights_gamma,
351 highlights_gain,
352 highlights_offset,
353 midtones_saturation,
354 midtones_contrast,
355 midtones_gamma,
356 midtones_gain,
357 midtones_offset,
358 shadows_saturation,
359 shadows_contrast,
360 shadows_gamma,
361 shadows_gain,
362 shadows_offset,
363 start_midtones,
364 end_midtones,
365 apply_on_red,
366 apply_on_green,
367 apply_on_blue,
368 luminance_coefficients));
369 },
370 mf::build::exec_presets::SomeSpanOrSingle<0>(),
372 float,
373 float,
374 float,
375 float,
376 float,
377 float,
378 float,
379 float,
380 float,
381 float,
382 float,
383 float,
384 float,
385 float,
386 float,
387 float,
388 float,
389 float,
390 float,
391 float,
392 float,
393 float,
394 float,
395 bool,
396 bool,
397 bool>());
398 });
399}
400
401static void node_register()
402{
403 static bke::bNodeType ntype;
404
405 cmp_node_type_base(&ntype, "CompositorNodeColorCorrection"_ustr, CMP_NODE_COLORCORRECTION);
406 ntype.ui_name = "Color Correction";
407 ntype.ui_description =
408 "Adjust the color of an image, separately in several tonal ranges (highlights, midtones and "
409 "shadows)";
410 ntype.enum_name_legacy = "COLORCORRECTION";
412 ntype.declare = node_declare;
415
417}
419
420} // namespace blender::nodes::node_composite_color_correction_cc
#define NODE_CLASS_OP_COLOR
Definition BKE_node.hh:480
#define CMP_NODE_COLORCORRECTION
#define NOD_REGISTER_NODE(REGISTER_FUNC)
DeclType::Builder & add_input(UString name, UString identifier=""_ustr)
void construct_and_set_matching_fn_cb(Fn &&create_multi_function)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
T min(const T &a, const T &b)
T interpolate(const T &a, const T &b, const FactorT &t)
T fallback_pow(const T &x, const T &power, const T &fallback)
T max(const T &a, const T &b)
static float4 color_correction(const float4 &color, const float &mask, const float &master_saturation, const float &master_contrast, const float &master_gamma, const float &master_gain, const float &master_offset, const float &highlights_saturation, const float &highlights_contrast, const float &highlights_gamma, const float &highlights_gain, const float &highlights_offset, const float &midtones_saturation, const float &midtones_contrast, const float &midtones_gamma, const float &midtones_gain, const float &midtones_offset, const float &shadows_saturation, const float &shadows_contrast, const float &shadows_gamma, const float &shadows_gain, const float &shadows_offset, const float &start_midtones, const float &end_midtones, const bool &apply_on_red, const bool &apply_on_green, const bool &apply_on_blue, const float3 &luminance_coefficients)
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
static void node_build_multi_function(nodes::NodeMultiFunctionBuilder &builder)
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
BLI_INLINE void IMB_colormanagement_get_luminance_coefficients(float r_rgb[3])
GPUNodeLink * GPU_constant(const float *num)
@ PROP_FACTOR
Definition RNA_types.hh:308
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393