Welcome to mirror list, hosted at ThFree Co, Russian Federation.

COM_ColorCorrectionOperation.cc « operations « compositor « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2e7ce6b84979a41c127395684cc6991b8c39d1f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Copyright 2011, Blender Foundation.
 */

#include "COM_ColorCorrectionOperation.h"

#include "IMB_colormanagement.h"

namespace blender::compositor {

ColorCorrectionOperation::ColorCorrectionOperation()
{
  this->add_input_socket(DataType::Color);
  this->add_input_socket(DataType::Value);
  this->add_output_socket(DataType::Color);
  input_image_ = nullptr;
  input_mask_ = nullptr;
  red_channel_enabled_ = true;
  green_channel_enabled_ = true;
  blue_channel_enabled_ = true;
  flags_.can_be_constant = true;
}
void ColorCorrectionOperation::init_execution()
{
  input_image_ = this->get_input_socket_reader(0);
  input_mask_ = this->get_input_socket_reader(1);
}

/* Calculate x^y if the function is defined. Otherwise return the given fallback value. */
BLI_INLINE float color_correct_powf_safe(const float x, const float y, const float fallback_value)
{
  if (x < 0) {
    return fallback_value;
  }
  return powf(x, y);
}

void ColorCorrectionOperation::execute_pixel_sampled(float output[4],
                                                     float x,
                                                     float y,
                                                     PixelSampler sampler)
{
  float input_image_color[4];
  float input_mask[4];
  input_image_->read_sampled(input_image_color, x, y, sampler);
  input_mask_->read_sampled(input_mask, x, y, sampler);

  float level = (input_image_color[0] + input_image_color[1] + input_image_color[2]) / 3.0f;
  float contrast = data_->master.contrast;
  float saturation = data_->master.saturation;
  float gamma = data_->master.gamma;
  float gain = data_->master.gain;
  float lift = data_->master.lift;
  float r, g, b;

  float value = input_mask[0];
  value = MIN2(1.0f, value);
  const float mvalue = 1.0f - value;

  float level_shadows = 0.0;
  float level_midtones = 0.0;
  float level_highlights = 0.0;
#define MARGIN 0.10f
#define MARGIN_DIV (0.5f / MARGIN)
  if (level < data_->startmidtones - MARGIN) {
    level_shadows = 1.0f;
  }
  else if (level < data_->startmidtones + MARGIN) {
    level_midtones = ((level - data_->startmidtones) * MARGIN_DIV) + 0.5f;
    level_shadows = 1.0f - level_midtones;
  }
  else if (level < data_->endmidtones - MARGIN) {
    level_midtones = 1.0f;
  }
  else if (level < data_->endmidtones + MARGIN) {
    level_highlights = ((level - data_->endmidtones) * MARGIN_DIV) + 0.5f;
    level_midtones = 1.0f - level_highlights;
  }
  else {
    level_highlights = 1.0f;
  }
#undef MARGIN
#undef MARGIN_DIV
  contrast *= (level_shadows * data_->shadows.contrast) +
              (level_midtones * data_->midtones.contrast) +
              (level_highlights * data_->highlights.contrast);
  saturation *= (level_shadows * data_->shadows.saturation) +
                (level_midtones * data_->midtones.saturation) +
                (level_highlights * data_->highlights.saturation);
  gamma *= (level_shadows * data_->shadows.gamma) + (level_midtones * data_->midtones.gamma) +
           (level_highlights * data_->highlights.gamma);
  gain *= (level_shadows * data_->shadows.gain) + (level_midtones * data_->midtones.gain) +
          (level_highlights * data_->highlights.gain);
  lift += (level_shadows * data_->shadows.lift) + (level_midtones * data_->midtones.lift) +
          (level_highlights * data_->highlights.lift);

  float invgamma = 1.0f / gamma;
  float luma = IMB_colormanagement_get_luminance(input_image_color);

  r = input_image_color[0];
  g = input_image_color[1];
  b = input_image_color[2];

  r = (luma + saturation * (r - luma));
  g = (luma + saturation * (g - luma));
  b = (luma + saturation * (b - luma));

  r = 0.5f + ((r - 0.5f) * contrast);
  g = 0.5f + ((g - 0.5f) * contrast);
  b = 0.5f + ((b - 0.5f) * contrast);

  /* Check for negative values to avoid nan. */
  r = color_correct_powf_safe(r * gain + lift, invgamma, r);
  g = color_correct_powf_safe(g * gain + lift, invgamma, g);
  b = color_correct_powf_safe(b * gain + lift, invgamma, b);

  /* Mix with mask. */
  r = mvalue * input_image_color[0] + value * r;
  g = mvalue * input_image_color[1] + value * g;
  b = mvalue * input_image_color[2] + value * b;

  if (red_channel_enabled_) {
    output[0] = r;
  }
  else {
    output[0] = input_image_color[0];
  }
  if (green_channel_enabled_) {
    output[1] = g;
  }
  else {
    output[1] = input_image_color[1];
  }
  if (blue_channel_enabled_) {
    output[2] = b;
  }
  else {
    output[2] = input_image_color[2];
  }
  output[3] = input_image_color[3];
}

void ColorCorrectionOperation::update_memory_buffer_row(PixelCursor &p)
{
  for (; p.out < p.row_end; p.next()) {
    const float *in_color = p.ins[0];
    const float *in_mask = p.ins[1];

    const float level = (in_color[0] + in_color[1] + in_color[2]) / 3.0f;
    float level_shadows = 0.0f;
    float level_midtones = 0.0f;
    float level_highlights = 0.0f;
    constexpr float MARGIN = 0.10f;
    constexpr float MARGIN_DIV = 0.5f / MARGIN;
    if (level < data_->startmidtones - MARGIN) {
      level_shadows = 1.0f;
    }
    else if (level < data_->startmidtones + MARGIN) {
      level_midtones = ((level - data_->startmidtones) * MARGIN_DIV) + 0.5f;
      level_shadows = 1.0f - level_midtones;
    }
    else if (level < data_->endmidtones - MARGIN) {
      level_midtones = 1.0f;
    }
    else if (level < data_->endmidtones + MARGIN) {
      level_highlights = ((level - data_->endmidtones) * MARGIN_DIV) + 0.5f;
      level_midtones = 1.0f - level_highlights;
    }
    else {
      level_highlights = 1.0f;
    }
    float contrast = data_->master.contrast;
    float saturation = data_->master.saturation;
    float gamma = data_->master.gamma;
    float gain = data_->master.gain;
    float lift = data_->master.lift;
    contrast *= level_shadows * data_->shadows.contrast +
                level_midtones * data_->midtones.contrast +
                level_highlights * data_->highlights.contrast;
    saturation *= level_shadows * data_->shadows.saturation +
                  level_midtones * data_->midtones.saturation +
                  level_highlights * data_->highlights.saturation;
    gamma *= level_shadows * data_->shadows.gamma + level_midtones * data_->midtones.gamma +
             level_highlights * data_->highlights.gamma;
    gain *= level_shadows * data_->shadows.gain + level_midtones * data_->midtones.gain +
            level_highlights * data_->highlights.gain;
    lift += level_shadows * data_->shadows.lift + level_midtones * data_->midtones.lift +
            level_highlights * data_->highlights.lift;

    const float inv_gamma = 1.0f / gamma;
    const float luma = IMB_colormanagement_get_luminance(in_color);

    float r = luma + saturation * (in_color[0] - luma);
    float g = luma + saturation * (in_color[1] - luma);
    float b = luma + saturation * (in_color[2] - luma);

    r = 0.5f + (r - 0.5f) * contrast;
    g = 0.5f + (g - 0.5f) * contrast;
    b = 0.5f + (b - 0.5f) * contrast;

    /* Check for negative values to avoid nan. */
    r = color_correct_powf_safe(r * gain + lift, inv_gamma, r);
    g = color_correct_powf_safe(g * gain + lift, inv_gamma, g);
    b = color_correct_powf_safe(b * gain + lift, inv_gamma, b);

    /* Mix with mask. */
    const float value = MIN2(1.0f, in_mask[0]);
    const float value_ = 1.0f - value;
    r = value_ * in_color[0] + value * r;
    g = value_ * in_color[1] + value * g;
    b = value_ * in_color[2] + value * b;

    p.out[0] = red_channel_enabled_ ? r : in_color[0];
    p.out[1] = green_channel_enabled_ ? g : in_color[1];
    p.out[2] = blue_channel_enabled_ ? b : in_color[2];
    p.out[3] = in_color[3];
  }
}

void ColorCorrectionOperation::deinit_execution()
{
  input_image_ = nullptr;
  input_mask_ = nullptr;
}

}  // namespace blender::compositor