Blender  V3.3
COM_DespeckleOperation.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2011 Blender Foundation. */
3 
4 #include "MEM_guardedalloc.h"
5 
7 
8 namespace blender::compositor {
9 
11 {
15  this->set_canvas_input_index(0);
16  input_operation_ = nullptr;
17  flags_.complex = true;
18 }
20 {
23 }
24 
26 {
27  input_operation_ = nullptr;
28  input_value_operation_ = nullptr;
29 }
30 
31 BLI_INLINE int color_diff(const float a[3], const float b[3], const float threshold)
32 {
33  return ((fabsf(a[0] - b[0]) > threshold) || (fabsf(a[1] - b[1]) > threshold) ||
34  (fabsf(a[2] - b[2]) > threshold));
35 }
36 
37 void DespeckleOperation::execute_pixel(float output[4], int x, int y, void * /*data*/)
38 {
39  float w = 0.0f;
40  float color_org[4];
41  float color_mid[4];
42  float color_mid_ok[4];
43  float in1[4];
44  int x1 = x - 1;
45  int x2 = x;
46  int x3 = x + 1;
47  int y1 = y - 1;
48  int y2 = y;
49  int y3 = y + 1;
50  CLAMP(x1, 0, get_width() - 1);
51  CLAMP(x2, 0, get_width() - 1);
52  CLAMP(x3, 0, get_width() - 1);
53  CLAMP(y1, 0, get_height() - 1);
54  CLAMP(y2, 0, get_height() - 1);
55  CLAMP(y3, 0, get_height() - 1);
56  float value[4];
57  input_value_operation_->read(value, x2, y2, nullptr);
58  // const float mval = 1.0f - value[0];
59 
60  input_operation_->read(color_org, x2, y2, nullptr);
61 
62 #define TOT_DIV_ONE 1.0f
63 #define TOT_DIV_CNR (float)M_SQRT1_2
64 
65 #define WTOT (TOT_DIV_ONE * 4 + TOT_DIV_CNR * 4)
66 
67 #define COLOR_ADD(fac) \
68  { \
69  madd_v4_v4fl(color_mid, in1, fac); \
70  if (color_diff(in1, color_org, threshold_)) { \
71  w += fac; \
72  madd_v4_v4fl(color_mid_ok, in1, fac); \
73  } \
74  }
75 
76  zero_v4(color_mid);
77  zero_v4(color_mid_ok);
78 
79  input_operation_->read(in1, x1, y1, nullptr);
81  input_operation_->read(in1, x2, y1, nullptr);
83  input_operation_->read(in1, x3, y1, nullptr);
85  input_operation_->read(in1, x1, y2, nullptr);
87 
88 #if 0
89  input_operation_->read(in2, x2, y2, nullptr);
90  madd_v4_v4fl(color_mid, in2, filter_[4]);
91 #endif
92 
93  input_operation_->read(in1, x3, y2, nullptr);
95  input_operation_->read(in1, x1, y3, nullptr);
97  input_operation_->read(in1, x2, y3, nullptr);
99  input_operation_->read(in1, x3, y3, nullptr);
101 
102  mul_v4_fl(color_mid, 1.0f / (4.0f + (4.0f * (float)M_SQRT1_2)));
103  // mul_v4_fl(color_mid, 1.0f / w);
104 
105  if ((w != 0.0f) && ((w / WTOT) > (threshold_neighbor_)) &&
106  color_diff(color_mid, color_org, threshold_)) {
107  mul_v4_fl(color_mid_ok, 1.0f / w);
108  interp_v4_v4v4(output, color_org, color_mid_ok, value[0]);
109  }
110  else {
111  copy_v4_v4(output, color_org);
112  }
113 
114 #undef TOT_DIV_ONE
115 #undef TOT_DIV_CNR
116 #undef WTOT
117 #undef COLOR_ADD
118 }
119 
121  ReadBufferOperation *read_operation,
122  rcti *output)
123 {
124  rcti new_input;
125  int addx = 2; //(filter_width_ - 1) / 2 + 1;
126  int addy = 2; //(filter_height_ - 1) / 2 + 1;
127  new_input.xmax = input->xmax + addx;
128  new_input.xmin = input->xmin - addx;
129  new_input.ymax = input->ymax + addy;
130  new_input.ymin = input->ymin - addy;
131 
132  return NodeOperation::determine_depending_area_of_interest(&new_input, read_operation, output);
133 }
134 
136  const rcti &output_area,
137  rcti &r_input_area)
138 {
139  switch (input_idx) {
140  case IMAGE_INPUT_INDEX: {
141  const int add_x = 2; //(filter_width_ - 1) / 2 + 1;
142  const int add_y = 2; //(filter_height_ - 1) / 2 + 1;
143  r_input_area.xmin = output_area.xmin - add_x;
144  r_input_area.xmax = output_area.xmax + add_x;
145  r_input_area.ymin = output_area.ymin - add_y;
146  r_input_area.ymax = output_area.ymax + add_y;
147  break;
148  }
149  case FACTOR_INPUT_INDEX: {
150  r_input_area = output_area;
151  break;
152  }
153  }
154 }
155 
157  const rcti &area,
159 {
160  const MemoryBuffer *image = inputs[IMAGE_INPUT_INDEX];
161  const int last_x = get_width() - 1;
162  const int last_y = get_height() - 1;
163  for (BuffersIterator<float> it = output->iterate_with(inputs, area); !it.is_end(); ++it) {
164  const int x1 = MAX2(it.x - 1, 0);
165  const int x2 = it.x;
166  const int x3 = MIN2(it.x + 1, last_x);
167  const int y1 = MAX2(it.y - 1, 0);
168  const int y2 = it.y;
169  const int y3 = MIN2(it.y + 1, last_y);
170 
171  float w = 0.0f;
172  const float *color_org = it.in(IMAGE_INPUT_INDEX);
173  float color_mid[4];
174  float color_mid_ok[4];
175  const float *in1 = nullptr;
176 
177 #define TOT_DIV_ONE 1.0f
178 #define TOT_DIV_CNR (float)M_SQRT1_2
179 
180 #define WTOT (TOT_DIV_ONE * 4 + TOT_DIV_CNR * 4)
181 
182 #define COLOR_ADD(fac) \
183  { \
184  madd_v4_v4fl(color_mid, in1, fac); \
185  if (color_diff(in1, color_org, threshold_)) { \
186  w += fac; \
187  madd_v4_v4fl(color_mid_ok, in1, fac); \
188  } \
189  }
190 
191  zero_v4(color_mid);
192  zero_v4(color_mid_ok);
193 
194  in1 = image->get_elem(x1, y1);
196  in1 = image->get_elem(x2, y1);
198  in1 = image->get_elem(x3, y1);
200  in1 = image->get_elem(x1, y2);
202 
203 #if 0
204  const float* in2 = image->get_elem(x2, y2);
205  madd_v4_v4fl(color_mid, in2, filter_[4]);
206 #endif
207 
208  in1 = image->get_elem(x3, y2);
210  in1 = image->get_elem(x1, y3);
212  in1 = image->get_elem(x2, y3);
214  in1 = image->get_elem(x3, y3);
216 
217  mul_v4_fl(color_mid, 1.0f / (4.0f + (4.0f * (float)M_SQRT1_2)));
218  // mul_v4_fl(color_mid, 1.0f / w);
219 
220  if ((w != 0.0f) && ((w / WTOT) > (threshold_neighbor_)) &&
221  color_diff(color_mid, color_org, threshold_)) {
222  const float factor = *it.in(FACTOR_INPUT_INDEX);
223  mul_v4_fl(color_mid_ok, 1.0f / w);
224  interp_v4_v4v4(it.out, color_org, color_mid_ok, factor);
225  }
226  else {
227  copy_v4_v4(it.out, color_org);
228  }
229 
230 #undef TOT_DIV_ONE
231 #undef TOT_DIV_CNR
232 #undef WTOT
233 #undef COLOR_ADD
234  }
235 }
236 
237 } // namespace blender::compositor
#define BLI_INLINE
#define M_SQRT1_2
Definition: BLI_math_base.h:32
MINLINE void mul_v4_fl(float r[4], float f)
MINLINE void copy_v4_v4(float r[4], const float a[4])
void interp_v4_v4v4(float r[4], const float a[4], const float b[4], float t)
Definition: math_vector.c:38
MINLINE void zero_v4(float r[4])
MINLINE void madd_v4_v4fl(float r[4], const float a[4], float f)
#define MAX2(a, b)
#define MIN2(a, b)
#define COLOR_ADD(fac)
#define TOT_DIV_CNR
#define TOT_DIV_ONE
#define WTOT
_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 y1
_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
_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 x2
Read Guarded memory(de)allocation.
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position CLAMP
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition: btQuadWord.h:119
void update_memory_buffer_partial(MemoryBuffer *output, const rcti &area, Span< MemoryBuffer * > inputs) override
void execute_pixel(float output[4], int x, int y, void *data) override
calculate a single pixel
void get_area_of_interest(int input_idx, const rcti &output_area, rcti &r_input_area) override
Get input operation area being read by this operation on rendering given output area.
bool determine_depending_area_of_interest(rcti *input, ReadBufferOperation *read_operation, rcti *output) override
a MemoryBuffer contains access to the data of a chunk
void add_output_socket(DataType datatype)
SocketReader * get_input_socket_reader(unsigned int index)
void read(float result[4], int x, int y, void *chunk_data)
virtual bool determine_depending_area_of_interest(rcti *input, ReadBufferOperation *read_operation, rcti *output)
void add_input_socket(DataType datatype, ResizeMode resize_mode=ResizeMode::Center)
void set_canvas_input_index(unsigned int index)
set the index of the input socket that will determine the canvas of this operation
depth_tx normal_tx diffuse_light_tx specular_light_tx volume_light_tx environment_tx ambient_occlusion_tx aov_value_tx in_weight_img image(1, GPU_R32F, Qualifier::WRITE, ImageType::FLOAT_2D_ARRAY, "out_weight_img") .image(3
ccl_gpu_kernel_postfix ccl_global float int int int int float threshold
ccl_global KernelShaderEvalInput ccl_global float * output
ccl_global KernelShaderEvalInput * input
#define fabsf(x)
Definition: metal/compat.h:219
static unsigned a[3]
Definition: RandGen.cpp:78
static void area(int d1, int d2, int e1, int e2, float weights[2])
BLI_INLINE int color_diff(const float a[3], const float b[3], const float threshold)
typename BuffersIteratorBuilder< T >::Iterator BuffersIterator
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
static bNodeSocketTemplate inputs[]
DepsgraphFromIDsFilter filter_
int ymin
Definition: DNA_vec_types.h:64
int ymax
Definition: DNA_vec_types.h:64
int xmin
Definition: DNA_vec_types.h:63
int xmax
Definition: DNA_vec_types.h:63