Blender V5.3
node_composite_despeckle.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"
7#include "BLI_math_vector.hh"
9
10#include "GPU_shader.hh"
11
12#include "COM_node_operation.hh"
13#include "COM_utilities.hh"
14
16
18
20{
21 b.use_custom_socket_order();
22 b.allow_any_socket_order();
23 b.add_input<decl::Color>("Image"_ustr)
24 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
25 .hide_value()
26 .structure_type(StructureType::Dynamic);
27 b.add_output<decl::Color>("Image"_ustr)
28 .structure_type(StructureType::Dynamic)
29 .align_with_previous();
30
31 b.add_input<decl::Float>("Factor"_ustr, "Fac"_ustr)
32 .default_value(1.0f)
33 .min(0.0f)
34 .max(1.0f)
36 .structure_type(StructureType::Dynamic);
37 b.add_input<decl::Float>("Color Threshold"_ustr)
38 .default_value(0.5f)
39 .min(0.0f)
41 "Pixels are despeckled only if their color difference from the average color of their "
42 "neighbors exceeds this threshold");
43 b.add_input<decl::Float>("Neighbor Threshold"_ustr)
44 .default_value(0.5f)
46 .min(0.0f)
47 .max(1.0f)
48 .description(
49 "Pixels are despeckled only if the number of pixels in their neighborhood that are "
50 "different exceed this ratio threshold relative to the total number of neighbors. "
51 "Neighbors are considered different if they exceed the color threshold input");
52}
53
54using namespace blender::compositor;
55
57 public:
59
60 void execute() override
61 {
62 const Result &input = this->get_input("Image");
63 if (input.is_single_value()) {
64 Result &output = this->get_result("Image");
65 output.share_data(input);
66 return;
67 }
68
69 if (this->context().use_gpu()) {
70 this->execute_gpu();
71 }
72 else {
73 this->execute_cpu();
74 }
75 }
76
78 {
79 gpu::Shader *shader = context().get_shader("compositor_despeckle");
81
83 GPU_shader_uniform_1f(shader, "neighbor_threshold", get_neighbor_threshold());
84
85 const Result &input_image = get_input("Image");
86 input_image.bind_as_texture(shader, "input_tx");
87
88 const Result &factor_image = get_input("Fac");
89 gpu::Texture *factor_texture = factor_image.bind_as_texture_or_single_value(shader,
90 "factor_tx");
91
92 const Domain domain = compute_domain();
93 Result &output_image = get_result("Image");
94 output_image.allocate_texture(domain);
95 output_image.bind_as_image(shader, "output_img");
96
98
100 output_image.unbind_as_image();
101 input_image.unbind_as_texture();
102 factor_image.unbind_as_texture_or_single_value(factor_texture);
103 }
104
106 {
107 const float color_threshold = this->get_color_threshold();
108 const float neighbor_threshold = this->get_neighbor_threshold();
109
110 const Result &input = get_input("Image");
111 const Result &factor_image = get_input("Fac");
112
113 const Domain domain = compute_domain();
114 Result &output = get_result("Image");
115 output.allocate_texture(domain);
116
117 /* A 3x3 weights kernel whose weights are the inverse of the distance to the center of the
118 * kernel. So the center weight is zero, the corners weights are (1 / sqrt(2)), and the rest
119 * of the weights are 1. The total sum of weights is 4 plus quadruple the corner weight. */
120 float corner_weight = 1.0f / math::sqrt(2.0f);
121 float sum_of_weights = 4.0f + corner_weight * 4.0f;
122 float3x3 weights = float3x3(float3(corner_weight, 1.0f, corner_weight),
123 float3(1.0f, 0.0f, 1.0f),
124 float3(corner_weight, 1.0f, corner_weight));
125
126 parallel_for(domain.data_size, [&](const int2 texel) {
127 float4 center_color = float4(input.load_pixel<Color>(texel));
128
129 /* Go over the pixels in the 3x3 window around the center pixel and compute the total sum of
130 * their colors multiplied by their weights. Additionally, for pixels whose colors are not
131 * close enough to the color of the center pixel, accumulate their color as well as their
132 * weights. */
133 float4 sum_of_colors = float4(0.0f);
134 float accumulated_weight = 0.0f;
135 float4 accumulated_color = float4(0.0f);
136 for (int j = 0; j < 3; j++) {
137 for (int i = 0; i < 3; i++) {
138 float weight = weights[j][i];
139 float4 color = float4(input.load_pixel_extended<Color>(texel + int2(i - 1, j - 1))) *
140 weight;
141 sum_of_colors += color;
142 if (!math::is_equal(center_color.xyz(), color.xyz(), color_threshold)) {
143 accumulated_color += color;
144 accumulated_weight += weight;
145 }
146 }
147 }
148
149 /* If the accumulated weight is zero, that means all pixels in the 3x3 window are similar and
150 * no need to despeckle anything, so write the original center color and return. */
151 if (accumulated_weight == 0.0f) {
152 output.store_pixel(texel, Color(center_color));
153 return;
154 }
155
156 /* If the ratio between the accumulated weights and the total sum of weights is not larger
157 * than the user specified neighbor threshold, then the number of pixels in the neighborhood
158 * that are not close enough to the center pixel is low, and no need to despeckle anything,
159 * so write the original center color and return. */
160 if (accumulated_weight / sum_of_weights < neighbor_threshold) {
161 output.store_pixel(texel, Color(center_color));
162 return;
163 }
164
165 /* If the weighted average color of the neighborhood is close enough to the center pixel,
166 * then no need to despeckle anything, so write the original center color and return. */
167 if (math::is_equal(
168 center_color.xyz(), (sum_of_colors / sum_of_weights).xyz(), color_threshold))
169 {
170 output.store_pixel(texel, Color(center_color));
171 return;
172 }
173
174 /* We need to despeckle, so write the mean accumulated color. */
175 float factor = factor_image.load_pixel<float, true>(texel);
176 float4 mean_color = accumulated_color / accumulated_weight;
177 output.store_pixel(texel, Color(math::interpolate(center_color, mean_color, factor)));
178 });
179 }
180
182 {
183 return math::max(0.0f, this->get_input("Color Threshold").get_single_value_default<float>());
184 }
185
187 {
188 return math::clamp(
189 this->get_input("Neighbor Threshold").get_single_value_default<float>(), 0.0f, 1.0f);
190 }
191};
192
194{
195 return new DespeckleOperation(context, node);
196}
197
198static void node_register()
199{
200 static bke::bNodeType ntype;
201
202 cmp_node_type_base(&ntype, "CompositorNodeDespeckle"_ustr, CMP_NODE_DESPECKLE);
203 ntype.ui_name = "Despeckle";
204 ntype.ui_description =
205 "Smooth areas of an image in which noise is noticeable, while leaving complex areas "
206 "untouched";
207 ntype.enum_name_legacy = "DESPECKLE";
209 ntype.declare = node_declare;
210 ntype.flag |= NODE_PREVIEW;
212
214}
215NOD_REGISTER_NODE(node_register)
216
217} // namespace blender::nodes::node_composite_despeckle_cc
#define NODE_CLASS_OP_FILTER
Definition BKE_node.hh:482
#define CMP_NODE_DESPECKLE
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define output
gpu::Shader * get_shader(const char *info_name, ResultPrecision precision)
NodeOperation(Context &context, const bNode &node)
Result & get_result(StringRef identifier)
Definition operation.cc:40
Context & context() const
Definition operation.cc:57
Result & get_input(StringRef identifier) const
Definition operation.cc:35
virtual Domain compute_domain()
Definition operation.cc:62
void unbind_as_texture_or_single_value(gpu::Texture *single_value_texture) const
Definition result.cc:970
void allocate_texture(const Domain domain, const bool from_pool=true, const std::optional< ResultStorageType > storage_type=std::nullopt)
Definition result.cc:554
void unbind_as_texture() const
Definition result.cc:964
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:854
T load_pixel(const int2 &texel) const
void unbind_as_image() const
Definition result.cc:990
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
Definition result.cc:951
gpu::Texture * bind_as_texture_or_single_value(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:861
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:235
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
void parallel_for(const int2 range, const Function &function)
T clamp(const T &a, const T &min, const T &max)
T sqrt(const T &a)
T interpolate(const T &a, const T &b, const FactorT &t)
bool is_equal(const MatBase< T, NumCol, NumRow > &a, const MatBase< T, NumCol, NumRow > &b, const T epsilon=T(0))
T max(const T &a, const T &b)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_declare(NodeDeclarationBuilder &b)
void GPU_shader_unbind()
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1f(gpu::Shader *sh, const char *name, float value)
@ PROP_FACTOR
Definition RNA_types.hh:308
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:379
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393