Blender V4.3
COM_ColorCorrectionOperation.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
8
9namespace blender::compositor {
10
12{
16 red_channel_enabled_ = true;
17 green_channel_enabled_ = true;
18 blue_channel_enabled_ = true;
20}
21
22/* Calculate x^y if the function is defined. Otherwise return the given fallback value. */
23BLI_INLINE float color_correct_powf_safe(const float x, const float y, const float fallback_value)
24{
25 if (x < 0) {
26 return fallback_value;
27 }
28 return powf(x, y);
29}
30
32{
33 for (; p.out < p.row_end; p.next()) {
34 const float *in_color = p.ins[0];
35 const float *in_mask = p.ins[1];
36
37 const float level = (in_color[0] + in_color[1] + in_color[2]) / 3.0f;
38 float level_shadows = 0.0f;
39 float level_midtones = 0.0f;
40 float level_highlights = 0.0f;
41 constexpr float MARGIN = 0.10f;
42 constexpr float MARGIN_DIV = 0.5f / MARGIN;
43 if (level < data_->startmidtones - MARGIN) {
44 level_shadows = 1.0f;
45 }
46 else if (level < data_->startmidtones + MARGIN) {
47 level_midtones = ((level - data_->startmidtones) * MARGIN_DIV) + 0.5f;
48 level_shadows = 1.0f - level_midtones;
49 }
50 else if (level < data_->endmidtones - MARGIN) {
51 level_midtones = 1.0f;
52 }
53 else if (level < data_->endmidtones + MARGIN) {
54 level_highlights = ((level - data_->endmidtones) * MARGIN_DIV) + 0.5f;
55 level_midtones = 1.0f - level_highlights;
56 }
57 else {
58 level_highlights = 1.0f;
59 }
60 float contrast = data_->master.contrast;
61 float saturation = data_->master.saturation;
62 float gamma = data_->master.gamma;
63 float gain = data_->master.gain;
64 float lift = data_->master.lift;
65 contrast *= level_shadows * data_->shadows.contrast +
66 level_midtones * data_->midtones.contrast +
67 level_highlights * data_->highlights.contrast;
68 saturation *= level_shadows * data_->shadows.saturation +
69 level_midtones * data_->midtones.saturation +
70 level_highlights * data_->highlights.saturation;
71 gamma *= level_shadows * data_->shadows.gamma + level_midtones * data_->midtones.gamma +
72 level_highlights * data_->highlights.gamma;
73 gain *= level_shadows * data_->shadows.gain + level_midtones * data_->midtones.gain +
74 level_highlights * data_->highlights.gain;
75 lift += level_shadows * data_->shadows.lift + level_midtones * data_->midtones.lift +
76 level_highlights * data_->highlights.lift;
77
78 const float inv_gamma = 1.0f / gamma;
79 const float luma = IMB_colormanagement_get_luminance(in_color);
80
81 float r = luma + saturation * (in_color[0] - luma);
82 float g = luma + saturation * (in_color[1] - luma);
83 float b = luma + saturation * (in_color[2] - luma);
84
85 r = 0.5f + (r - 0.5f) * contrast;
86 g = 0.5f + (g - 0.5f) * contrast;
87 b = 0.5f + (b - 0.5f) * contrast;
88
89 /* Check for negative values to avoid nan. */
90 r = color_correct_powf_safe(r * gain + lift, inv_gamma, r);
91 g = color_correct_powf_safe(g * gain + lift, inv_gamma, g);
92 b = color_correct_powf_safe(b * gain + lift, inv_gamma, b);
93
94 /* Mix with mask. */
95 const float value = std::min(1.0f, in_mask[0]);
96 const float value_ = 1.0f - value;
97 r = value_ * in_color[0] + value * r;
98 g = value_ * in_color[1] + value * g;
99 b = value_ * in_color[2] + value * b;
100
101 p.out[0] = red_channel_enabled_ ? r : in_color[0];
102 p.out[1] = green_channel_enabled_ ? g : in_color[1];
103 p.out[2] = blue_channel_enabled_ ? b : in_color[2];
104 p.out[3] = in_color[3];
105 }
106}
107
108} // namespace blender::compositor
#define BLI_INLINE
BLI_INLINE float IMB_colormanagement_get_luminance(const float rgb[3])
void add_output_socket(DataType datatype)
void add_input_socket(DataType datatype, ResizeMode resize_mode=ResizeMode::Center)
local_group_size(16, 16) .push_constant(Type b
#define powf(x, y)
BLI_INLINE float color_correct_powf_safe(const float x, const float y, const float fallback_value)
ColorCorrectionData shadows
ColorCorrectionData midtones
ColorCorrectionData master
ColorCorrectionData highlights