Blender V5.3
node_composite_inpaint.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"
6#include "BLI_math_vector.hh"
8
11#include "COM_node_operation.hh"
12#include "COM_utilities.hh"
13
15
17
19{
20 b.use_custom_socket_order();
21 b.allow_any_socket_order();
22 b.add_input<decl::Color>("Image"_ustr)
23 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
24 .hide_value()
25 .structure_type(StructureType::Dynamic);
26 b.add_output<decl::Color>("Image"_ustr)
27 .structure_type(StructureType::Dynamic)
28 .align_with_previous();
29
30 b.add_input<decl::Int>("Size"_ustr)
31 .default_value(0)
33 .min(0)
34 .description("The size of the inpaint in pixels");
35}
36
37using namespace blender::compositor;
38
40 public:
42
43 void execute() override
44 {
45 const Result &input = this->get_input("Image");
46 if (input.is_single_value() || this->get_max_distance() == 0) {
47 Result &output = this->get_result("Image");
48 output.share_data(input);
49 return;
50 }
51
52 Result inpainting_boundary = compute_inpainting_boundary();
53
54 /* Compute a jump flooding table to get the closest boundary pixel to each pixel. */
56 jump_flooding(context(), inpainting_boundary, flooded_boundary);
57 inpainting_boundary.release();
58
60 Result distance_to_boundary = context().create_result(ResultType::Float);
61 Result smoothing_radius = context().create_result(ResultType::Float);
63 flooded_boundary, filled_region, distance_to_boundary, smoothing_radius);
64 flooded_boundary.release();
65
66 Result smoothed_region = context().create_result(ResultType::Color);
68 context(), filled_region, smoothing_radius, smoothed_region, get_max_distance());
69 filled_region.release();
70 smoothing_radius.release();
71
72 compute_inpainting_region(smoothed_region, distance_to_boundary);
73 distance_to_boundary.release();
74 smoothed_region.release();
75 }
76
77 /* Compute an image that marks the boundary pixels of the inpainting region as seed pixels for
78 * the jump flooding algorithm. The inpainting region is the region composed of pixels that are
79 * not opaque. */
81 {
82 if (this->context().use_gpu()) {
84 }
85
87 }
88
90 {
91 gpu::Shader *shader = context().get_shader("compositor_inpaint_compute_boundary",
94
95 const Result &input = get_input("Image");
96 input.bind_as_texture(shader, "input_tx");
97
99 const Domain domain = compute_domain();
100 inpainting_boundary.allocate_texture(domain);
101 inpainting_boundary.bind_as_image(shader, "boundary_img");
102
104
105 input.unbind_as_texture();
106 inpainting_boundary.unbind_as_image();
108
109 return inpainting_boundary;
110 }
111
113 {
114 const Result &input = this->get_input("Image");
115
117 const Domain domain = this->compute_domain();
118 boundary.allocate_texture(domain);
119
120 /* The in-paint operation uses a jump flood algorithm to flood the region to be in-painted with
121 * the pixels at its boundary. The algorithms expects an input image whose values are those
122 * returned by the initialize_jump_flooding_value function, given the texel location and a
123 * boolean specifying if the pixel is a boundary one.
124 *
125 * Technically, we needn't restrict the output to just the boundary pixels, since the algorithm
126 * can still operate if the interior of the region was also included. However, the algorithm
127 * operates more accurately when the number of pixels to be flooded is minimum. */
128 parallel_for(domain.data_size, [&](const int2 texel) {
129 /* Identify if any of the 8 neighbors around the center pixel are transparent. */
130 bool has_transparent_neighbors = false;
131 constexpr float alpha_threshold = 1.0f - 1e-3f;
132 for (int j = -1; j <= 1; j++) {
133 for (int i = -1; i <= 1; i++) {
134 int2 offset = int2(i, j);
135
136 /* Exempt the center pixel. */
137 if (offset != int2(0)) {
138 if (input.load_pixel_extended<Color>(texel + offset).a < alpha_threshold) {
139 has_transparent_neighbors = true;
140 break;
141 }
142 }
143 }
144 }
145
146 /* The pixels at the boundary are those that are opaque and have transparent neighbors. */
147 bool is_opaque = input.load_pixel<Color>(texel).a >= alpha_threshold;
148 bool is_boundary_pixel = is_opaque && has_transparent_neighbors;
149
150 /* Encode the boundary information in the format expected by the jump flooding algorithm. */
151 int2 jump_flooding_value = initialize_jump_flooding_value(texel, is_boundary_pixel);
152
153 boundary.store_pixel(texel, jump_flooding_value);
154 });
155
156 return boundary;
157 }
158
159 /* Fill the inpainting region based on the jump flooding table and write the distance to the
160 * closest boundary pixel to an intermediate buffer. */
161 void fill_inpainting_region(const Result &flooded_boundary,
162 Result &filled_region,
163 Result &distance_to_boundary,
164 Result &smoothing_radius)
165 {
166 if (this->context().use_gpu()) {
168 flooded_boundary, filled_region, distance_to_boundary, smoothing_radius);
169 }
170 else {
172 flooded_boundary, filled_region, distance_to_boundary, smoothing_radius);
173 }
174 }
175
176 void fill_inpainting_region_gpu(const Result &flooded_boundary,
177 Result &filled_region,
178 Result &distance_to_boundary,
179 Result &smoothing_radius)
180 {
181 gpu::Shader *shader = context().get_shader("compositor_inpaint_fill_region");
183
185
186 const Result &input = get_input("Image");
187 input.bind_as_texture(shader, "input_tx");
188
189 flooded_boundary.bind_as_texture(shader, "flooded_boundary_tx");
190
191 const Domain domain = compute_domain();
192 filled_region.allocate_texture(domain);
193 filled_region.bind_as_image(shader, "filled_region_img");
194
195 distance_to_boundary.allocate_texture(domain);
196 distance_to_boundary.bind_as_image(shader, "distance_to_boundary_img");
197
198 smoothing_radius.allocate_texture(domain);
199 smoothing_radius.bind_as_image(shader, "smoothing_radius_img");
200
202
203 input.unbind_as_texture();
204 flooded_boundary.unbind_as_texture();
205 filled_region.unbind_as_image();
206 distance_to_boundary.unbind_as_image();
207 smoothing_radius.unbind_as_image();
209 }
210
211 void fill_inpainting_region_cpu(const Result &flooded_boundary,
212 Result &filled_region,
213 Result &distance_to_boundary_image,
214 Result &smoothing_radius_image)
215 {
216 const int max_distance = this->get_max_distance();
217
218 const Result &input = this->get_input("Image");
219
220 const Domain domain = this->compute_domain();
221 filled_region.allocate_texture(domain);
222 distance_to_boundary_image.allocate_texture(domain);
223 smoothing_radius_image.allocate_texture(domain);
224
225 /* Fill the inpainting region by sampling the color of the nearest boundary pixel.
226 * Additionally, compute some information about the inpainting region, like the distance to the
227 * boundary, as well as the blur radius to use to smooth out that region. */
228 parallel_for(domain.data_size, [&](const int2 texel) {
229 float4 color = float4(input.load_pixel<Color>(texel));
230
231 /* An opaque pixel, not part of the inpainting region. */
232 constexpr float alpha_threshold = 1.0f - 1e-3f;
233 if (color.w >= alpha_threshold) {
234 filled_region.store_pixel(texel, Color(color));
235 smoothing_radius_image.store_pixel(texel, 0.0f);
236 distance_to_boundary_image.store_pixel(texel, 0.0f);
237 return;
238 }
239
240 int2 closest_boundary_texel = flooded_boundary.load_pixel<int2>(texel);
241 float distance_to_boundary = math::distance(float2(texel), float2(closest_boundary_texel));
242 distance_to_boundary_image.store_pixel(texel, distance_to_boundary);
243
244 /* We follow this shader by a blur shader that smooths out the inpainting region, where the
245 * blur radius is the radius of the circle that touches the boundary. We can imagine the blur
246 * window to be inscribed in that circle and thus the blur radius is the distance to the
247 * boundary divided by square root two. As a performance optimization, we limit the blurring
248 * to areas that will affect the inpainting region, that is, whose distance to boundary is
249 * less than double the inpainting distance. Additionally, we clamp to the distance to the
250 * inpainting distance since areas outside of the clamp range only indirectly affect the
251 * inpainting region due to blurring and thus needn't use higher blur radii. */
252 float blur_window_size = math::min(float(max_distance), distance_to_boundary) /
253 std::numbers::sqrt2;
254 bool skip_smoothing = distance_to_boundary > (max_distance * 2.0f);
255 float smoothing_radius = skip_smoothing ? 0.0f : blur_window_size;
256 smoothing_radius_image.store_pixel(texel, smoothing_radius);
257
258 /* Mix the boundary color with the original color using its alpha because semi-transparent
259 * areas are considered to be partially inpainted. */
260 float4 boundary_color = float4(input.load_pixel_extended<Color>(closest_boundary_texel));
261 filled_region.store_pixel(texel, Color(math::interpolate(boundary_color, color, color.w)));
262 });
263 }
264
265 /* Compute the inpainting region by mixing the smoothed inpainted region with the original input
266 * up to the inpainting distance. */
267 void compute_inpainting_region(const Result &inpainted_region,
268 const Result &distance_to_boundary)
269 {
270 if (this->context().use_gpu()) {
271 this->compute_inpainting_region_gpu(inpainted_region, distance_to_boundary);
272 }
273 else {
274 this->compute_inpainting_region_cpu(inpainted_region, distance_to_boundary);
275 }
276 }
277
278 void compute_inpainting_region_gpu(const Result &inpainted_region,
279 const Result &distance_to_boundary)
280 {
281 gpu::Shader *shader = context().get_shader("compositor_inpaint_compute_region");
283
285
286 const Result &input = get_input("Image");
287 input.bind_as_texture(shader, "input_tx");
288
289 inpainted_region.bind_as_texture(shader, "inpainted_region_tx");
290 distance_to_boundary.bind_as_texture(shader, "distance_to_boundary_tx");
291
292 const Domain domain = compute_domain();
293 Result &output = get_result("Image");
294 output.allocate_texture(domain);
295 output.bind_as_image(shader, "output_img");
296
298
299 input.unbind_as_texture();
300 inpainted_region.unbind_as_texture();
301 distance_to_boundary.unbind_as_texture();
302 output.unbind_as_image();
304 }
305
306 void compute_inpainting_region_cpu(const Result &inpainted_region,
307 const Result &distance_to_boundary_image)
308 {
309 const int max_distance = this->get_max_distance();
310
311 const Result &input = this->get_input("Image");
312
313 const Domain domain = this->compute_domain();
314 Result &output = this->get_result("Image");
315 output.allocate_texture(domain);
316
317 parallel_for(domain.data_size, [&](const int2 texel) {
318 float4 color = float4(input.load_pixel<Color>(texel));
319
320 /* An opaque pixel, not part of the inpainting region, write the original color. */
321 constexpr float alpha_threshold = 1.0f - 1e-3f;
322 if (color.w >= alpha_threshold) {
323 output.store_pixel(texel, Color(color));
324 return;
325 }
326
327 float distance_to_boundary = distance_to_boundary_image.load_pixel<float>(texel);
328
329 /* Further than the inpainting distance, not part of the inpainting region, write the
330 * original color. */
331 if (distance_to_boundary > max_distance) {
332 output.store_pixel(texel, Color(color));
333 return;
334 }
335
336 /* Mix the inpainted color with the original color using its alpha because semi-transparent
337 * areas are considered to be partially inpainted. */
338 float4 inpainted_color = float4(inpainted_region.load_pixel<Color>(texel));
339 output.store_pixel(
340 texel,
341 Color(float4(math::interpolate(inpainted_color.xyz(), color.xyz(), color.w), 1.0f)));
342 });
343 }
344
346 {
347 return math::max(0, this->get_input("Size").get_single_value_default<int>());
348 }
349};
350
352{
353 return new InpaintOperation(context, node);
354}
355
356static void node_register()
357{
358 static bke::bNodeType ntype;
359
360 cmp_node_type_base(&ntype, "CompositorNodeInpaint"_ustr, CMP_NODE_INPAINT);
361 ntype.ui_name = "Inpaint";
362 ntype.ui_description = "Extend borders of an image into transparent or masked regions";
363 ntype.enum_name_legacy = "INPAINT";
365 ntype.declare = node_declare;
367
369}
370NOD_REGISTER_NODE(node_register)
371
372} // namespace blender::nodes::node_composite_inpaint_cc
#define NODE_CLASS_OP_FILTER
Definition BKE_node.hh:482
#define CMP_NODE_INPAINT
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define output
ChannelStorageType a
Result create_result(ResultType type, ResultPrecision precision)
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 store_pixel(const int2 &texel, const T &pixel_value)
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_extended(const int2 &texel) const
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
void fill_inpainting_region(const Result &flooded_boundary, Result &filled_region, Result &distance_to_boundary, Result &smoothing_radius)
void compute_inpainting_region_gpu(const Result &inpainted_region, const Result &distance_to_boundary)
void compute_inpainting_region_cpu(const Result &inpainted_region, const Result &distance_to_boundary_image)
void fill_inpainting_region_cpu(const Result &flooded_boundary, Result &filled_region, Result &distance_to_boundary_image, Result &smoothing_radius_image)
void fill_inpainting_region_gpu(const Result &flooded_boundary, Result &filled_region, Result &distance_to_boundary, Result &smoothing_radius)
void compute_inpainting_region(const Result &inpainted_region, const Result &distance_to_boundary)
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
int2 initialize_jump_flooding_value(const int2 &texel, const bool is_seed)
void symmetric_separable_blur_variable_size(Context &context, const Result &input, const Result &radius, Result &output, const int weights_resolution=128, const math::FilterKernel filter_type=math::FilterKernel::Gauss)
void jump_flooding(Context &context, Result &input, Result &output)
void parallel_for(const int2 range, const Function &function)
T distance(const T &a, const T &b)
T min(const T &a, const T &b)
T interpolate(const T &a, const T &b, const FactorT &t)
T max(const T &a, const T &b)
static void node_declare(NodeDeclarationBuilder &b)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
void GPU_shader_unbind()
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1i(gpu::Shader *sh, const char *name, int value)
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