Blender  V3.3
COM_ColorCorrectionOperation.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2011 Blender Foundation. */
3 
5 
6 #include "IMB_colormanagement.h"
7 
8 namespace blender::compositor {
9 
11 {
15  input_image_ = nullptr;
16  input_mask_ = nullptr;
17  red_channel_enabled_ = true;
18  green_channel_enabled_ = true;
19  blue_channel_enabled_ = true;
20  flags_.can_be_constant = true;
21 }
23 {
24  input_image_ = this->get_input_socket_reader(0);
25  input_mask_ = this->get_input_socket_reader(1);
26 }
27 
28 /* Calculate x^y if the function is defined. Otherwise return the given fallback value. */
29 BLI_INLINE float color_correct_powf_safe(const float x, const float y, const float fallback_value)
30 {
31  if (x < 0) {
32  return fallback_value;
33  }
34  return powf(x, y);
35 }
36 
38  float x,
39  float y,
41 {
42  float input_image_color[4];
43  float input_mask[4];
44  input_image_->read_sampled(input_image_color, x, y, sampler);
45  input_mask_->read_sampled(input_mask, x, y, sampler);
46 
47  float level = (input_image_color[0] + input_image_color[1] + input_image_color[2]) / 3.0f;
48  float contrast = data_->master.contrast;
49  float saturation = data_->master.saturation;
50  float gamma = data_->master.gamma;
51  float gain = data_->master.gain;
52  float lift = data_->master.lift;
53  float r, g, b;
54 
55  float value = input_mask[0];
56  value = MIN2(1.0f, value);
57  const float mvalue = 1.0f - value;
58 
59  float level_shadows = 0.0;
60  float level_midtones = 0.0;
61  float level_highlights = 0.0;
62 #define MARGIN 0.10f
63 #define MARGIN_DIV (0.5f / MARGIN)
64  if (level < data_->startmidtones - MARGIN) {
65  level_shadows = 1.0f;
66  }
67  else if (level < data_->startmidtones + MARGIN) {
68  level_midtones = ((level - data_->startmidtones) * MARGIN_DIV) + 0.5f;
69  level_shadows = 1.0f - level_midtones;
70  }
71  else if (level < data_->endmidtones - MARGIN) {
72  level_midtones = 1.0f;
73  }
74  else if (level < data_->endmidtones + MARGIN) {
75  level_highlights = ((level - data_->endmidtones) * MARGIN_DIV) + 0.5f;
76  level_midtones = 1.0f - level_highlights;
77  }
78  else {
79  level_highlights = 1.0f;
80  }
81 #undef MARGIN
82 #undef MARGIN_DIV
83  contrast *= (level_shadows * data_->shadows.contrast) +
84  (level_midtones * data_->midtones.contrast) +
85  (level_highlights * data_->highlights.contrast);
86  saturation *= (level_shadows * data_->shadows.saturation) +
87  (level_midtones * data_->midtones.saturation) +
88  (level_highlights * data_->highlights.saturation);
89  gamma *= (level_shadows * data_->shadows.gamma) + (level_midtones * data_->midtones.gamma) +
90  (level_highlights * data_->highlights.gamma);
91  gain *= (level_shadows * data_->shadows.gain) + (level_midtones * data_->midtones.gain) +
92  (level_highlights * data_->highlights.gain);
93  lift += (level_shadows * data_->shadows.lift) + (level_midtones * data_->midtones.lift) +
94  (level_highlights * data_->highlights.lift);
95 
96  float invgamma = 1.0f / gamma;
97  float luma = IMB_colormanagement_get_luminance(input_image_color);
98 
99  r = input_image_color[0];
100  g = input_image_color[1];
101  b = input_image_color[2];
102 
103  r = (luma + saturation * (r - luma));
104  g = (luma + saturation * (g - luma));
105  b = (luma + saturation * (b - luma));
106 
107  r = 0.5f + ((r - 0.5f) * contrast);
108  g = 0.5f + ((g - 0.5f) * contrast);
109  b = 0.5f + ((b - 0.5f) * contrast);
110 
111  /* Check for negative values to avoid nan. */
112  r = color_correct_powf_safe(r * gain + lift, invgamma, r);
113  g = color_correct_powf_safe(g * gain + lift, invgamma, g);
114  b = color_correct_powf_safe(b * gain + lift, invgamma, b);
115 
116  /* Mix with mask. */
117  r = mvalue * input_image_color[0] + value * r;
118  g = mvalue * input_image_color[1] + value * g;
119  b = mvalue * input_image_color[2] + value * b;
120 
121  if (red_channel_enabled_) {
122  output[0] = r;
123  }
124  else {
125  output[0] = input_image_color[0];
126  }
127  if (green_channel_enabled_) {
128  output[1] = g;
129  }
130  else {
131  output[1] = input_image_color[1];
132  }
133  if (blue_channel_enabled_) {
134  output[2] = b;
135  }
136  else {
137  output[2] = input_image_color[2];
138  }
139  output[3] = input_image_color[3];
140 }
141 
143 {
144  for (; p.out < p.row_end; p.next()) {
145  const float *in_color = p.ins[0];
146  const float *in_mask = p.ins[1];
147 
148  const float level = (in_color[0] + in_color[1] + in_color[2]) / 3.0f;
149  float level_shadows = 0.0f;
150  float level_midtones = 0.0f;
151  float level_highlights = 0.0f;
152  constexpr float MARGIN = 0.10f;
153  constexpr float MARGIN_DIV = 0.5f / MARGIN;
154  if (level < data_->startmidtones - MARGIN) {
155  level_shadows = 1.0f;
156  }
157  else if (level < data_->startmidtones + MARGIN) {
158  level_midtones = ((level - data_->startmidtones) * MARGIN_DIV) + 0.5f;
159  level_shadows = 1.0f - level_midtones;
160  }
161  else if (level < data_->endmidtones - MARGIN) {
162  level_midtones = 1.0f;
163  }
164  else if (level < data_->endmidtones + MARGIN) {
165  level_highlights = ((level - data_->endmidtones) * MARGIN_DIV) + 0.5f;
166  level_midtones = 1.0f - level_highlights;
167  }
168  else {
169  level_highlights = 1.0f;
170  }
171  float contrast = data_->master.contrast;
172  float saturation = data_->master.saturation;
173  float gamma = data_->master.gamma;
174  float gain = data_->master.gain;
175  float lift = data_->master.lift;
176  contrast *= level_shadows * data_->shadows.contrast +
177  level_midtones * data_->midtones.contrast +
178  level_highlights * data_->highlights.contrast;
179  saturation *= level_shadows * data_->shadows.saturation +
180  level_midtones * data_->midtones.saturation +
181  level_highlights * data_->highlights.saturation;
182  gamma *= level_shadows * data_->shadows.gamma + level_midtones * data_->midtones.gamma +
183  level_highlights * data_->highlights.gamma;
184  gain *= level_shadows * data_->shadows.gain + level_midtones * data_->midtones.gain +
185  level_highlights * data_->highlights.gain;
186  lift += level_shadows * data_->shadows.lift + level_midtones * data_->midtones.lift +
187  level_highlights * data_->highlights.lift;
188 
189  const float inv_gamma = 1.0f / gamma;
190  const float luma = IMB_colormanagement_get_luminance(in_color);
191 
192  float r = luma + saturation * (in_color[0] - luma);
193  float g = luma + saturation * (in_color[1] - luma);
194  float b = luma + saturation * (in_color[2] - luma);
195 
196  r = 0.5f + (r - 0.5f) * contrast;
197  g = 0.5f + (g - 0.5f) * contrast;
198  b = 0.5f + (b - 0.5f) * contrast;
199 
200  /* Check for negative values to avoid nan. */
201  r = color_correct_powf_safe(r * gain + lift, inv_gamma, r);
202  g = color_correct_powf_safe(g * gain + lift, inv_gamma, g);
203  b = color_correct_powf_safe(b * gain + lift, inv_gamma, b);
204 
205  /* Mix with mask. */
206  const float value = MIN2(1.0f, in_mask[0]);
207  const float value_ = 1.0f - value;
208  r = value_ * in_color[0] + value * r;
209  g = value_ * in_color[1] + value * g;
210  b = value_ * in_color[2] + value * b;
211 
212  p.out[0] = red_channel_enabled_ ? r : in_color[0];
213  p.out[1] = green_channel_enabled_ ? g : in_color[1];
214  p.out[2] = blue_channel_enabled_ ? b : in_color[2];
215  p.out[3] = in_color[3];
216  }
217 }
218 
220 {
221  input_image_ = nullptr;
222  input_mask_ = nullptr;
223 }
224 
225 } // namespace blender::compositor
#define BLI_INLINE
#define MIN2(a, b)
#define MARGIN
#define MARGIN_DIV
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble GLdouble r _GL_VOID_RET _GL_VOID GLfloat GLfloat r _GL_VOID_RET _GL_VOID GLint GLint r _GL_VOID_RET _GL_VOID GLshort GLshort r _GL_VOID_RET _GL_VOID GLdouble GLdouble r
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
BLI_INLINE float IMB_colormanagement_get_luminance(const float rgb[3])
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Sky Generate a procedural sky texture Noise Generate fractal Perlin noise Wave Generate procedural bands or rings with noise Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks Texture Retrieve multiple types of texture coordinates nTypically used as inputs for texture nodes Vector Convert a or normal between and object coordinate space Combine Create a color from its saturation
void execute_pixel_sampled(float output[4], float x, float y, PixelSampler sampler) override
void add_output_socket(DataType datatype)
SocketReader * get_input_socket_reader(unsigned int index)
void read_sampled(float result[4], float x, float y, PixelSampler sampler)
void add_input_socket(DataType datatype, ResizeMode resize_mode=ResizeMode::Center)
#define powf(x, y)
Definition: cuda/compat.h:103
depth_tx sampler(1, ImageType::FLOAT_2D, "combined_tx") .sampler(2
ccl_global KernelShaderEvalInput ccl_global float * output
BLI_INLINE float color_correct_powf_safe(const float x, const float y, const float fallback_value)
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
static const pxr::TfToken g("g", pxr::TfToken::Immortal)
ColorCorrectionData shadows
ColorCorrectionData midtones
ColorCorrectionData master
ColorCorrectionData highlights