27 for (const int64_t y : sub_y_range) {
28 for (const int64_t x : IndexRange(size.x)) {
29 float4 color = float4(input->get_elem(x, y));
31 if (color.w == 1.0f) {
32 copy_v4_v4(output->get_elem(x, y), color);
36 float distance_to_boundary = *distance_to_boundary_buffer.get_elem(x, y);
38 if (distance_to_boundary > max_distance_) {
39 copy_v4_v4(output->get_elem(x, y), color);
43 float4 inpainted_color = float4(inpainted_region.get_elem(x, y));
44 float4 final_color = float4(math::interpolate(inpainted_color, color, color.w).xyz(),
46 copy_v4_v4(output->get_elem(x, y), final_color);
52void InpaintSimpleOperation::fill_inpainting_region(
const MemoryBuffer *input,
58 const int2 size =
int2(this->get_width(), this->get_height());
60 for (const int64_t y : sub_y_range) {
61 for (const int64_t x : IndexRange(size.x)) {
62 int2 texel = int2(x, y);
63 const size_t index = size_t(y) * size.x + x;
65 float4 color = float4(input->get_elem(x, y));
67 if (color.w == 1.0f) {
68 copy_v4_v4(filled_region.get_elem(x, y), color);
69 *smoothing_radius_buffer.get_elem(x, y) = 0.0f;
70 *distance_to_boundary_buffer.get_elem(x, y) = 0.0f;
74 int2 closest_boundary_texel = flooded_boundary[index];
75 float distance_to_boundary = math::distance(float2(texel), float2(closest_boundary_texel));
76 *distance_to_boundary_buffer.get_elem(x, y) = distance_to_boundary;
78 float blur_window_size = math::min(float(max_distance_), distance_to_boundary) /
80 bool skip_smoothing = distance_to_boundary > (max_distance_ * 2.0f);
81 float smoothing_radius = skip_smoothing ? 0.0f : blur_window_size;
82 *smoothing_radius_buffer.get_elem(x, y) = smoothing_radius;
84 float4 boundary_color = float4(
85 input->get_elem_clamped(closest_boundary_texel.x, closest_boundary_texel.y));
86 float4 final_color = math::interpolate(boundary_color, color, color.w);
87 copy_v4_v4(filled_region.get_elem(x, y), final_color);
95 const int2 size =
int2(this->get_width(), this->get_height());
99 for (const int64_t y : sub_y_range) {
100 for (const int64_t x : IndexRange(size.x)) {
101 int2 texel = int2(x, y);
103 bool has_transparent_neighbors = false;
104 for (int j = -1; j <= 1; j++) {
105 for (int i = -1; i <= 1; i++) {
106 int2 offset = int2(i, j);
108 if (offset != int2(0)) {
109 if (float4(input->get_elem_clamped(x + i, y + j)).w < 1.0f) {
110 has_transparent_neighbors = true;
117 bool is_opaque = float4(input->get_elem(x, y)).w == 1.0f;
118 bool is_boundary_pixel = is_opaque && has_transparent_neighbors;
120 int2 jump_flooding_value = initialize_jump_flooding_value(texel, is_boundary_pixel);
122 const size_t index = size_t(y) * size.x + x;
123 boundary[index] = jump_flooding_value;
135 const int2 size =
int2(this->get_width(), this->get_height());
136 Array<int2> inpainting_boundary = compute_inpainting_boundary(input);
139 MemoryBuffer filled_region(DataType::Color, input->get_rect());
140 MemoryBuffer distance_to_boundary(DataType::Value, input->get_rect());
141 MemoryBuffer smoothing_radius(DataType::Value, input->get_rect());
142 fill_inpainting_region(
143 input, flooded_boundary, filled_region, distance_to_boundary, smoothing_radius);
145 MemoryBuffer smoothed_region(DataType::Color, input->get_rect());
147 filled_region, smoothed_region, smoothing_radius,
R_FILTER_GAUSS, max_distance_);
149 compute_inpainting_region(input, smoothed_region, distance_to_boundary, output);