Blender V5.3
node_composite_kuwahara.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <limits>
6
7#include "BLI_math_base.hh"
9#include "BLI_math_vector.hh"
11
12#include "RNA_types.hh"
13
14#include "COM_node_operation.hh"
15#include "COM_utilities.hh"
16
19
21
23
24static const EnumPropertyItem type_items[] = {
26 "CLASSIC",
27 0,
28 N_("Classic"),
29 N_("Fast but less accurate variation")},
31 "ANISOTROPIC",
32 0,
33 N_("Anisotropic"),
34 N_("Accurate but slower variation")},
35 {0, nullptr, 0, nullptr, nullptr},
36};
37
39{
40 b.use_custom_socket_order();
41 b.allow_any_socket_order();
42 b.add_input<decl::Color>("Image"_ustr)
43 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
44 .hide_value()
45 .structure_type(StructureType::Dynamic);
46 b.add_output<decl::Color>("Image"_ustr)
47 .structure_type(StructureType::Dynamic)
48 .align_with_previous();
49
50 b.add_input<decl::Float>("Size"_ustr)
51 .default_value(6.0f)
53 .min(0.0f)
54 .description("The size of the filter in pixels")
55 .structure_type(StructureType::Dynamic);
56 b.add_input<decl::Menu>("Type"_ustr)
57 .default_value(CMP_NODE_KUWAHARA_ANISOTROPIC)
58 .static_items(type_items)
60
61 b.add_input<decl::Int>("Uniformity"_ustr)
62 .default_value(4)
63 .min(0)
64 .usage_by_single_menu(CMP_NODE_KUWAHARA_ANISOTROPIC)
66 "Controls the uniformity of the direction of the filter. Higher values produces more "
67 "uniform directions");
68 b.add_input<decl::Float>("Sharpness"_ustr)
69 .default_value(1.0f)
71 .min(0.0f)
72 .max(1.0f)
73 .usage_by_single_menu(CMP_NODE_KUWAHARA_ANISOTROPIC)
74 .description(
75 "Controls the sharpness of the filter. 0 means completely smooth while 1 means "
76 "completely sharp");
77 b.add_input<decl::Float>("Eccentricity"_ustr)
78 .default_value(1.0f)
80 .min(0.0f)
81 .max(2.0f)
82 .usage_by_single_menu(CMP_NODE_KUWAHARA_ANISOTROPIC)
83 .description(
84 "Controls how directional the filter is. 0 means the filter is completely "
85 "omnidirectional while 2 means it is maximally directed along the edges of the image");
86 b.add_input<decl::Bool>("High Precision"_ustr)
87 .default_value(false)
88 .usage_by_single_menu(CMP_NODE_KUWAHARA_CLASSIC)
90 "Uses a more precise but slower method. Use if the output contains undesirable noise.");
91}
92
93static void node_init(bNodeTree * /*ntree*/, bNode *node)
94{
95 /* Unused, kept for forward compatibility. */
96 NodeKuwaharaData *data = MEM_new<NodeKuwaharaData>(__func__);
97 node->storage = data;
98}
99
100using namespace blender::compositor;
101
103 public:
105
106 void execute() override
107 {
108 const Result &input = this->get_input("Image");
109 if (input.is_single_value()) {
110 Result &output = this->get_result("Image");
111 output.share_data(input);
112 return;
113 }
114
117 }
118 else {
120 }
121 }
122
124 {
125 /* For high radii, we accelerate the filter using a summed area table, making the filter
126 * execute in constant time as opposed to having quadratic complexity. Except if high precision
127 * is enabled, since summed area tables are less precise. */
128 Result &size_input = get_input("Size");
129 if (!this->get_high_precision() &&
130 (!size_input.is_single_value() || size_input.get_single_value<float>() > 5.0f))
131 {
133 }
134 else {
136 }
137 }
138
140 {
141 if (this->context().use_gpu()) {
143 }
144 else {
146 }
147 }
148
150 {
153
154 const Result &input_image = get_input("Image");
155 input_image.bind_as_texture(shader, "input_tx");
156
157 Result &size_input = get_input("Size");
158 if (size_input.is_single_value()) {
159 GPU_shader_uniform_1i(shader, "size", int(size_input.get_single_value<float>()));
160 }
161 else {
162 size_input.bind_as_texture(shader, "size_tx");
163 }
164
165 const Domain domain = compute_domain();
166 Result &output_image = get_result("Image");
167 output_image.allocate_texture(domain);
168 output_image.bind_as_image(shader, "output_img");
169
171
172 input_image.unbind_as_texture();
173 output_image.unbind_as_image();
175 }
176
178 {
179 if (is_constant_size()) {
180 return "compositor_kuwahara_classic_convolution_constant_size";
181 }
182 return "compositor_kuwahara_classic_convolution_variable_size";
183 }
184
186 {
187 const Domain domain = this->compute_domain();
188 Result &output = this->get_result("Image");
189 output.allocate_texture(domain);
190
191 this->compute_classic<false>(&this->get_input("Image"),
192 nullptr,
193 nullptr,
194 this->get_input("Size"),
195 output,
196 domain.data_size);
197 }
198
200 {
202 summed_area_table(this->context(), this->get_input("Image"), table);
203
206 this->get_input("Image"),
207 squared_table,
209
210 if (this->context().use_gpu()) {
211 this->execute_classic_summed_area_table_gpu(table, squared_table);
212 }
213 else {
214 this->execute_classic_summed_area_table_cpu(table, squared_table);
215 }
216
217 table.release();
218 squared_table.release();
219 }
220
221 void execute_classic_summed_area_table_gpu(const Result &table, const Result &squared_table)
222 {
225
226 Result &size_input = get_input("Size");
227 if (size_input.is_single_value()) {
228 GPU_shader_uniform_1i(shader, "size", int(size_input.get_single_value<float>()));
229 }
230 else {
231 size_input.bind_as_texture(shader, "size_tx");
232 }
233
234 table.bind_as_texture(shader, "table_tx");
235 squared_table.bind_as_texture(shader, "squared_table_tx");
236
237 const Domain domain = compute_domain();
238 Result &output_image = get_result("Image");
239 output_image.allocate_texture(domain);
240 output_image.bind_as_image(shader, "output_img");
241
243
244 table.unbind_as_texture();
245 squared_table.unbind_as_texture();
246 output_image.unbind_as_image();
248 }
249
251 {
252 if (is_constant_size()) {
253 return "compositor_kuwahara_classic_summed_area_table_constant_size";
254 }
255 return "compositor_kuwahara_classic_summed_area_table_variable_size";
256 }
257
258 void execute_classic_summed_area_table_cpu(const Result &table, const Result &squared_table)
259 {
260 const Domain domain = this->compute_domain();
261 Result &output = this->get_result("Image");
262 output.allocate_texture(domain);
263
265 nullptr, &table, &squared_table, this->get_input("Size"), output, domain.data_size);
266 }
267
268 /* If UseSummedAreaTable is true, then `table` and `squared_table` should be provided while
269 * `input` should be nullptr, otherwise, `input` should be provided while `table` and
270 * `squared_table` should be nullptr. */
271 template<bool UseSummedAreaTable>
272 void compute_classic(const Result *input,
273 const Result *table,
274 const Result *squared_table,
275 const Result &size_input,
276 Result &output,
277 const int2 size)
278 {
279 parallel_for(size, [&](const int2 texel) {
280 int radius = math::max(0, int(size_input.load_pixel<float, true>(texel)));
281
282 float4 mean_of_squared_color_of_quadrants[4] = {
283 float4(0.0f), float4(0.0f), float4(0.0f), float4(0.0f)};
284 float4 mean_of_color_of_quadrants[4] = {
285 float4(0.0f), float4(0.0f), float4(0.0f), float4(0.0f)};
286
287 /* Compute the above statistics for each of the quadrants around the current pixel. */
288 for (int q = 0; q < 4; q++) {
289 /* A fancy expression to compute the sign of the quadrant q. */
290 int2 sign = int2((q % 2) * 2 - 1, ((q / 2) * 2 - 1));
291
292 int2 lower_bound = texel - int2(sign.x > 0 ? 0 : radius, sign.y > 0 ? 0 : radius);
293 int2 upper_bound = texel + int2(sign.x < 0 ? 0 : radius, sign.y < 0 ? 0 : radius);
294
295 /* Limit the quadrants to the image bounds. */
296 int2 image_bound = size - int2(1);
297 int2 corrected_lower_bound = math::min(image_bound, math::max(int2(0), lower_bound));
298 int2 corrected_upper_bound = math::min(image_bound, math::max(int2(0), upper_bound));
299 int2 region_size = corrected_upper_bound - corrected_lower_bound + int2(1);
300 int quadrant_pixel_count = region_size.x * region_size.y;
301
302 if constexpr (UseSummedAreaTable) {
303 mean_of_color_of_quadrants[q] = summed_area_table_sum(*table, lower_bound, upper_bound);
304 mean_of_squared_color_of_quadrants[q] = summed_area_table_sum(
305 *squared_table, lower_bound, upper_bound);
306 }
307 else {
308 for (int j = 0; j <= radius; j++) {
309 for (int i = 0; i <= radius; i++) {
310 float4 color = float4(input->load_pixel_zero<Color>(texel + int2(i, j) * sign));
311 mean_of_color_of_quadrants[q] += color;
312 mean_of_squared_color_of_quadrants[q] += color * color;
313 }
314 }
315 }
316
317 mean_of_color_of_quadrants[q] /= quadrant_pixel_count;
318 mean_of_squared_color_of_quadrants[q] /= quadrant_pixel_count;
319 }
320
321 /* Find the quadrant which has the minimum variance. */
322 float minimum_variance = std::numeric_limits<float>::max();
323 float4 mean_color_of_chosen_quadrant = mean_of_color_of_quadrants[0];
324 for (int q = 0; q < 4; q++) {
325 float4 color_mean = mean_of_color_of_quadrants[q];
326 float4 squared_color_mean = mean_of_squared_color_of_quadrants[q];
327 float4 color_variance = squared_color_mean - color_mean * color_mean;
328
329 float variance = math::dot(color_variance.xyz(), float3(1.0f));
330 if (variance < minimum_variance) {
331 minimum_variance = variance;
332 mean_color_of_chosen_quadrant = color_mean;
333 }
334 }
335
336 output.store_pixel(texel, Color(mean_color_of_chosen_quadrant));
337 });
338 }
339
340 /* An implementation of the Anisotropic Kuwahara filter described in the paper:
341 *
342 * Kyprianidis, Jan Eric, Henry Kang, and Jurgen Dollner. "Image and video abstraction by
343 * anisotropic Kuwahara filtering." 2009.
344 */
346 {
347 Result structure_tensor = compute_structure_tensor();
348 Result smoothed_structure_tensor = context().create_result(ResultType::Float4);
350 context(), structure_tensor, smoothed_structure_tensor, float2(this->get_uniformity()));
351 structure_tensor.release();
352
353 if (this->context().use_gpu()) {
354 this->execute_anisotropic_gpu(smoothed_structure_tensor);
355 }
356 else {
357 this->execute_anisotropic_cpu(smoothed_structure_tensor);
358 }
359
360 smoothed_structure_tensor.release();
361 }
362
363 void execute_anisotropic_gpu(const Result &structure_tensor)
364 {
367
368 GPU_shader_uniform_1f(shader, "eccentricity", this->compute_eccentricity());
369 GPU_shader_uniform_1f(shader, "sharpness", this->compute_sharpness());
370
371 Result &input = get_input("Image");
372 input.bind_as_texture(shader, "input_tx");
373
374 Result &size_input = get_input("Size");
375 if (size_input.is_single_value()) {
376 GPU_shader_uniform_1f(shader, "size", size_input.get_single_value<float>());
377 }
378 else {
379 size_input.bind_as_texture(shader, "size_tx");
380 }
381
382 structure_tensor.bind_as_texture(shader, "structure_tensor_tx");
383
384 const Domain domain = compute_domain();
385 Result &output_image = get_result("Image");
386 output_image.allocate_texture(domain);
387 output_image.bind_as_image(shader, "output_img");
388
390
391 input.unbind_as_texture();
392 structure_tensor.unbind_as_texture();
393 output_image.unbind_as_image();
395 }
396
398 {
399 if (is_constant_size()) {
400 return "compositor_kuwahara_anisotropic_constant_size";
401 }
402 return "compositor_kuwahara_anisotropic_variable_size";
403 }
404
405 static float radial_falloff(float x)
406 {
407 /* We know x is in [0..1] range, and the Gaussian falloff
408 * function we want is `exp(-PI * x)`. This approximates
409 * it with a polynomial. */
410 return 0.99963078f +
411 x * (-3.12538948f +
412 x * (4.76244166f + x * (-4.41057815f + x * (2.39937989f + x * (-0.58256130f)))));
413 }
414
415 void execute_anisotropic_cpu(const Result &structure_tensor)
416 {
417 const float eccentricity = this->compute_eccentricity();
418 const float sharpness = this->compute_sharpness();
419
420 Result &input = this->get_input("Image");
421 Result &size = this->get_input("Size");
422
423 const Domain domain = this->compute_domain();
424 Result &output = this->get_result("Image");
425 output.allocate_texture(domain);
426
427 /* An implementation of the Anisotropic Kuwahara filter described in the paper:
428 *
429 * Kyprianidis, Jan Eric, Henry Kang, and Jurgen Dollner. "Image and video abstraction by
430 * anisotropic Kuwahara filtering." 2009.
431 *
432 * But with the polynomial weighting functions described in the paper:
433 *
434 * Kyprianidis, Jan Eric, et al. "Anisotropic Kuwahara Filtering with Polynomial Weighting
435 * Functions." 2010.
436 *
437 * And the sector weight function described in the paper:
438 *
439 * Kyprianidis, Jan Eric. "Image and video abstraction by multi-scale anisotropic Kuwahara
440 * filtering." 2011.
441 */
442
443 parallel_for(domain.data_size, [&](const int2 texel) {
444 /* The structure tensor is encoded in a float4 using a column major storage order, as can be
445 * seen in the compute_structure_tensor_cpu method. */
446 float4 encoded_structure_tensor = structure_tensor.load_pixel<float4>(texel);
447 float dxdx = encoded_structure_tensor.x;
448 float dxdy = encoded_structure_tensor.y;
449 float dydy = encoded_structure_tensor.w;
450
451 /* Compute the first and second eigenvalues of the structure tensor using the equations in
452 * section "3.1 Orientation and Anisotropy Estimation" of the paper. */
453 float eigenvalue_first_term = (dxdx + dydy) / 2.0f;
454 float eigenvalue_square_root_term = math::sqrt(math::square(dxdx - dydy) +
455 4.0f * math::square(dxdy)) /
456 2.0f;
457 float first_eigenvalue = eigenvalue_first_term + eigenvalue_square_root_term;
458 float second_eigenvalue = eigenvalue_first_term - eigenvalue_square_root_term;
459
460 /* Compute the normalized eigenvector of the structure tensor oriented in direction of the
461 * minimum rate of change using the equations in section "3.1 Orientation and Anisotropy
462 * Estimation" of the paper. */
463 float2 eigenvector = float2(first_eigenvalue - dxdx, -dxdy);
464 float eigenvector_length = math::length(eigenvector);
465 float2 unit_eigenvector = eigenvector_length != 0.0f ? eigenvector / eigenvector_length :
466 float2(1.0f);
467
468 /* Compute the amount of anisotropy using equations in section "3.1 Orientation and
469 * Anisotropy Estimation" of the paper. The anisotropy ranges from 0 to 1, where 0
470 * corresponds to isotropic and 1 corresponds to entirely anisotropic regions. */
471 float eigenvalue_sum = first_eigenvalue + second_eigenvalue;
472 float eigenvalue_difference = first_eigenvalue - second_eigenvalue;
473 float anisotropy = eigenvalue_sum > 0.0f ? eigenvalue_difference / eigenvalue_sum : 0.0f;
474
475 float radius = math::max(0.0f, size.load_pixel<float, true>(texel));
476 if (radius == 0.0f) {
477 output.store_pixel(texel, input.load_pixel<Color>(texel));
478 return;
479 }
480
481 /* Compute the width and height of an ellipse that is more width-elongated for high
482 * anisotropy and more circular for low anisotropy, controlled using the eccentricity factor.
483 * Since the anisotropy is in the [0, 1] range, the width factor tends to 1 as the
484 * eccentricity tends to infinity and tends to infinity when the eccentricity tends to zero.
485 * This is based on the equations in section "3.2. Anisotropic Kuwahara Filtering" of the
486 * paper. */
487 float ellipse_width_factor = (eccentricity + anisotropy) / eccentricity;
488 float ellipse_width = ellipse_width_factor * radius;
489 float ellipse_height = radius / ellipse_width_factor;
490
491 /* Compute the cosine and sine of the angle that the eigenvector makes with the x axis. Since
492 * the eigenvector is normalized, its x and y components are the cosine and sine of the angle
493 * it makes with the x axis. */
494 float cosine = unit_eigenvector.x;
495 float sine = unit_eigenvector.y;
496
497 /* Compute an inverse transformation matrix that represents an ellipse of the given width and
498 * height and makes and an angle with the x axis of the given cosine and sine. This is an
499 * inverse matrix, so it transforms the ellipse into a disk of unit radius. */
500 float2x2 inverse_ellipse_matrix = float2x2(
501 float2(cosine / ellipse_width, -sine / ellipse_height),
502 float2(sine / ellipse_width, cosine / ellipse_height));
503
504 /* Compute the bounding box of a zero centered ellipse whose major axis is aligned with the
505 * eigenvector and has the given width and height. This is based on the equations described
506 * in:
507 *
508 * https://iquilezles.org/articles/ellipses/
509 *
510 * Notice that we only compute the upper bound, the lower bound is just negative that since
511 * the ellipse is zero centered. Also notice that we take the ceiling of the bounding box,
512 * just to ensure the filter window is at least 1x1. */
513 float2 ellipse_major_axis = ellipse_width * unit_eigenvector;
514 float2 ellipse_minor_axis = ellipse_height * float2(unit_eigenvector.y, unit_eigenvector.x) *
515 float2(-1, 1);
516 int2 ellipse_bounds = int2(math::ceil(
517 math::sqrt(math::square(ellipse_major_axis) + math::square(ellipse_minor_axis))));
518
519 /* Compute the overlap polynomial parameters for 8-sector ellipse based on the equations in
520 * section "3 Alternative Weighting Functions" of the polynomial weights paper. More on this
521 * later in the code. */
522 const int number_of_sectors = 8;
523 float sector_center_overlap_parameter = 2.0f / radius;
524 float sector_envelope_angle = ((3.0f / 2.0f) * std::numbers::pi_v<float>) /
525 number_of_sectors;
526 float cross_sector_overlap_parameter = (sector_center_overlap_parameter +
527 math::cos(sector_envelope_angle)) /
528 math::square(math::sin(sector_envelope_angle));
529
530 /* We need to compute the weighted mean of color and squared color of each of the 8 sectors
531 * of the ellipse, so we declare arrays for accumulating those and initialize them in the
532 * next code section. */
533 float4 weighted_mean_of_squared_color_of_sectors[8];
534 float4 weighted_mean_of_color_of_sectors[8];
535 /* Opposite sectors have equal total weights because each sample is paired.
536 * Share the weight sum between sectors [i] and [i+4]. */
537 float sum_of_weights_of_sectors[4];
538
539 /* The center pixel (0, 0) is exempt from the main loop below for reasons that are explained
540 * in the first row's lower bound, so we need to accumulate its color, squared color,
541 * and weight separately first. Luckily, the zero coordinates of the center pixel zeros out
542 * most of the complex computations below, and it can easily be shown that the weight for the
543 * center pixel in all sectors is simply (1 / number_of_sectors). */
544 float4 center_color = float4(input.load_pixel<Color>(texel));
545 float4 center_color_squared = center_color * center_color;
546 float center_weight = 1.0f / number_of_sectors;
547 float4 weighted_center_color = center_color * center_weight;
548 float4 weighted_center_color_squared = center_color_squared * center_weight;
549 for (int i = 0; i < number_of_sectors; i++) {
550 weighted_mean_of_squared_color_of_sectors[i] = weighted_center_color_squared;
551 weighted_mean_of_color_of_sectors[i] = weighted_center_color;
552 }
553 for (int i = 0; i < number_of_sectors / 2; i++) {
554 sum_of_weights_of_sectors[i] = center_weight;
555 }
556
557 /* Loop over the window of pixels inside the bounding box of the ellipse. However, we utilize
558 * the fact that ellipses are mirror symmetric along the horizontal axis, so we reduce the
559 * window to only the upper two quadrants, and compute each two mirrored pixels at the same
560 * time using the same weight as an optimization. */
561 for (int j = 0; j <= ellipse_bounds.y; j++) {
562 /* On the first row, negative offsets are covered by the mirrored samples, and the center
563 * pixel was accumulated separately above. */
564 int lower_bound = j == 0 ? 1 : -ellipse_bounds.x;
565 for (int i = lower_bound; i <= ellipse_bounds.x; i++) {
566 /* Map the pixels of the ellipse into a unit disk, exempting any points that are not part
567 * of the ellipse or disk. */
568 float2 disk_point = inverse_ellipse_matrix * float2(i, j);
569 float disk_point_length_squared = math::dot(disk_point, disk_point);
570 if (disk_point_length_squared > 1.0f) {
571 continue;
572 }
573
574 /* While each pixel belongs to a single sector in the ellipse, we expand the definition
575 * of a sector a bit to also overlap with other sectors as illustrated in Figure 8 of the
576 * polynomial weights paper. So each pixel may contribute to multiple sectors, and thus
577 * we compute its weight in each of the 8 sectors. */
578 float sector_weights[8];
579
580 /* We evaluate the weighting polynomial at each of the 8 sectors by rotating the disk
581 * point by 45 degrees and evaluating the weighting polynomial at each incremental
582 * rotation. To avoid potentially expensive rotations, we utilize the fact that rotations
583 * by 90 degrees are simply swapping of the coordinates and negating the x component. We
584 * also note that since the y term of the weighting polynomial is squared, it is not
585 * affected by the sign and can be computed once for the x and once for the y
586 * coordinates. So we compute every other even-indexed 4 weights by successive 90 degree
587 * rotations as discussed. */
588 float2 polynomial = sector_center_overlap_parameter -
589 cross_sector_overlap_parameter * math::square(disk_point);
590 sector_weights[0] = math::square(math::max(0.0f, disk_point.y + polynomial.x));
591 sector_weights[2] = math::square(math::max(0.0f, -disk_point.x + polynomial.y));
592 sector_weights[4] = math::square(math::max(0.0f, -disk_point.y + polynomial.x));
593 sector_weights[6] = math::square(math::max(0.0f, disk_point.x + polynomial.y));
594
595 /* Then we rotate the disk point by 45 degrees, which is a simple expression involving a
596 * constant as can be demonstrated by applying a 45 degree rotation matrix. */
597 float2 rotated_disk_point = (1.0f / std::numbers::sqrt2) *
598 float2(disk_point.x - disk_point.y,
599 disk_point.x + disk_point.y);
600
601 /* Finally, we compute every other odd-index 4 weights starting from the 45 degrees
602 * rotated disk point. */
603 float2 rotated_polynomial = sector_center_overlap_parameter -
604 cross_sector_overlap_parameter *
605 math::square(rotated_disk_point);
606 sector_weights[1] = math::square(
607 math::max(0.0f, rotated_disk_point.y + rotated_polynomial.x));
608 sector_weights[3] = math::square(
609 math::max(0.0f, -rotated_disk_point.x + rotated_polynomial.y));
610 sector_weights[5] = math::square(
611 math::max(0.0f, -rotated_disk_point.y + rotated_polynomial.x));
612 sector_weights[7] = math::square(
613 math::max(0.0f, rotated_disk_point.x + rotated_polynomial.y));
614
615 /* We compute a radial Gaussian weighting component such that pixels further away from
616 * the sector center gets attenuated, and we also divide by the sum of sector weights to
617 * normalize them, since the radial weight will eventually be multiplied to the sector
618 * weight below. */
619 float sector_weights_sum = sector_weights[0] + sector_weights[1] + sector_weights[2] +
620 sector_weights[3] + sector_weights[4] + sector_weights[5] +
621 sector_weights[6] + sector_weights[7];
622 float radial_gaussian_weight = radial_falloff(disk_point_length_squared) /
623 sector_weights_sum;
624
625 /* Load the color of the pixel and its mirrored pixel and compute their square. */
626 float4 upper_color = float4(input.load_pixel_extended<Color>(texel + int2(i, j)));
627 float4 lower_color = float4(input.load_pixel_extended<Color>(texel - int2(i, j)));
628 float4 upper_color_squared = upper_color * upper_color;
629 float4 lower_color_squared = lower_color * lower_color;
630
631 for (int k = 0; k < number_of_sectors; k++) {
632 float weight = sector_weights[k] * radial_gaussian_weight;
633 /* Skip zero weight sectors. */
634 if (weight == 0.0f) {
635 continue;
636 }
637 sum_of_weights_of_sectors[k % (number_of_sectors / 2)] += weight;
638
639 /* Accumulate the pixel to each of the sectors multiplied by the sector weight. */
640 int upper_index = k;
641 weighted_mean_of_color_of_sectors[upper_index] += upper_color * weight;
642 weighted_mean_of_squared_color_of_sectors[upper_index] += upper_color_squared * weight;
643
644 /* Accumulate the mirrored pixel to each of the sectors multiplied by the sector
645 * weight. */
646 int lower_index = (k + number_of_sectors / 2) % number_of_sectors;
647 weighted_mean_of_color_of_sectors[lower_index] += lower_color * weight;
648 weighted_mean_of_squared_color_of_sectors[lower_index] += lower_color_squared * weight;
649 }
650 }
651 }
652
653 /* Compute the weighted sum of mean of sectors, such that sectors with lower standard
654 * deviation gets more significant weight than sectors with higher standard deviation. */
655 float sum_of_weights = 0.0f;
656 float4 weighted_sum = float4(0.0f);
657 for (int i = 0; i < number_of_sectors; i++) {
658 float sector_weight_sum = sum_of_weights_of_sectors[i % (number_of_sectors / 2)];
659 weighted_mean_of_color_of_sectors[i] /= sector_weight_sum;
660 weighted_mean_of_squared_color_of_sectors[i] /= sector_weight_sum;
661
662 float4 color_mean = weighted_mean_of_color_of_sectors[i];
663 float4 squared_color_mean = weighted_mean_of_squared_color_of_sectors[i];
664 float4 color_variance = math::abs(squared_color_mean - color_mean * color_mean);
665
666 float standard_deviation = math::dot(math::sqrt(color_variance.xyz()), float3(1.0f));
667
668 /* Compute the sector weight based on the weight function introduced in section "3.3.1
669 * Single-scale Filtering" of the multi-scale paper. Use a threshold of 0.02 to avoid zero
670 * division and avoid artifacts in homogeneous regions as demonstrated in the paper. */
671 float weight = 1.0f / math::pow(math::max(0.02f, standard_deviation), sharpness);
672
673 sum_of_weights += weight;
674 weighted_sum += color_mean * weight;
675 }
676
677 /* Fall back to the original color if all sector weights are zero due to very high standard
678 * deviation and sharpness. */
679 if (sum_of_weights == 0.0f) {
680 weighted_sum = center_color;
681 }
682 else {
683 weighted_sum /= sum_of_weights;
684 }
685
686 output.store_pixel(texel, Color(weighted_sum));
687 });
688 }
689
691 {
692 if (this->context().use_gpu()) {
693 return this->compute_structure_tensor_gpu();
694 }
695 return this->compute_structure_tensor_cpu();
696 }
697
699 {
700 gpu::Shader *shader = context().get_shader(
701 "compositor_kuwahara_anisotropic_compute_structure_tensor");
703
704 Result &input = get_input("Image");
705 input.bind_as_texture(shader, "input_tx");
706
707 const Domain domain = compute_domain();
708 Result structure_tensor = context().create_result(ResultType::Float4);
709 structure_tensor.allocate_texture(domain);
710 structure_tensor.bind_as_image(shader, "structure_tensor_img");
711
713
714 input.unbind_as_texture();
715 structure_tensor.unbind_as_image();
717
718 return structure_tensor;
719 }
720
722 {
723 const Result &input = this->get_input("Image");
724
725 const Domain domain = this->compute_domain();
726 Result structure_tensor_image = context().create_result(ResultType::Float4);
727 structure_tensor_image.allocate_texture(domain);
728
729 /* Computes the structure tensor of the image using a Dirac delta window function as described
730 * in section "3.2 Local Structure Estimation" of the paper:
731 *
732 * Kyprianidis, Jan Eric. "Image and video abstraction by multi-scale anisotropic Kuwahara
733 * filtering." 2011.
734 *
735 * The structure tensor should then be smoothed using a Gaussian function to eliminate high
736 * frequency details. */
737 parallel_for(domain.data_size, [&](const int2 texel) {
738 /* The weight kernels of the filter optimized for rotational symmetry described in section
739 * "3.2.1 Gradient Calculation". */
740 const float corner_weight = 0.182f;
741 const float center_weight = 1.0f - 2.0f * corner_weight;
742
743 float3 x_partial_derivative =
744 float4(input.load_pixel_extended<Color>(texel + int2(-1, 1))).xyz() * -corner_weight +
745 float4(input.load_pixel_extended<Color>(texel + int2(-1, 0))).xyz() * -center_weight +
746 float4(input.load_pixel_extended<Color>(texel + int2(-1, -1))).xyz() * -corner_weight +
747 float4(input.load_pixel_extended<Color>(texel + int2(1, 1))).xyz() * corner_weight +
748 float4(input.load_pixel_extended<Color>(texel + int2(1, 0))).xyz() * center_weight +
749 float4(input.load_pixel_extended<Color>(texel + int2(1, -1))).xyz() * corner_weight;
750
751 float3 y_partial_derivative =
752 float4(input.load_pixel_extended<Color>(texel + int2(-1, 1))).xyz() * corner_weight +
753 float4(input.load_pixel_extended<Color>(texel + int2(0, 1))).xyz() * center_weight +
754 float4(input.load_pixel_extended<Color>(texel + int2(1, 1))).xyz() * corner_weight +
755 float4(input.load_pixel_extended<Color>(texel + int2(-1, -1))).xyz() * -corner_weight +
756 float4(input.load_pixel_extended<Color>(texel + int2(0, -1))).xyz() * -center_weight +
757 float4(input.load_pixel_extended<Color>(texel + int2(1, -1))).xyz() * -corner_weight;
758
759 float dxdx = math::dot(x_partial_derivative, x_partial_derivative);
760 float dxdy = math::dot(x_partial_derivative, y_partial_derivative);
761 float dydy = math::dot(y_partial_derivative, y_partial_derivative);
762
763 /* We encode the structure tensor in a float4 using a column major storage order. */
764 float4 structure_tensor = float4(dxdx, dxdy, dxdy, dydy);
765
766 structure_tensor_image.store_pixel(texel, structure_tensor);
767 });
768
769 return structure_tensor_image;
770 }
771
773 {
774 return get_input("Size").is_single_value();
775 }
776
777 /* The sharpness controls the sharpness of the transitions between the kuwahara sectors, which
778 * is controlled by the weighting function pow(standard_deviation, -sharpness) as can be seen
779 * in the shader. The transition is completely smooth when the sharpness is zero and completely
780 * sharp when it is infinity. But realistically, the sharpness doesn't change much beyond the
781 * value of 16 due to its exponential nature, so we just assume a maximum sharpness of 16.
782 *
783 * The stored sharpness is in the range [0, 1], so we multiply by 16 to get it in the range
784 * [0, 16], however, we also square it before multiplication to slow down the rate of change
785 * near zero to counter its exponential nature for more intuitive user control. */
787 {
788 const float sharpness_factor = this->get_sharpness();
789 return sharpness_factor * sharpness_factor * 16.0f;
790 }
791
792 /* The eccentricity controls how much the image anisotropy affects the eccentricity of the
793 * kuwahara sectors, which is controlled by the following factor that gets multiplied to the
794 * radius to get the ellipse width and divides the radius to get the ellipse height:
795 *
796 * (eccentricity + anisotropy) / eccentricity
797 *
798 * Since the anisotropy is in the [0, 1] range, the factor tends to 1 as the eccentricity tends
799 * to infinity and tends to infinity when the eccentricity tends to zero. The stored
800 * eccentricity is in the range [0, 2], we map that to the range [infinity, 0.5] by taking the
801 * reciprocal, satisfying the aforementioned limits. The upper limit doubles the computed
802 * default eccentricity, which users can use to enhance the directionality of the filter.
803 * Instead of actual infinity, we just use an eccentricity of 1 / 0.01 since the result is very
804 * similar to that of infinity. */
806 {
807 return 1.0f / math::max(0.01f, this->get_eccentricity());
808 }
809
811 {
812 return this->get_input("High Precision").get_single_value_default<bool>();
813 }
814
816 {
817 return math::max(0, this->get_input("Uniformity").get_single_value_default<int>());
818 }
819
821 {
822 return math::clamp(this->get_input("Sharpness").get_single_value_default<float>(), 0.0f, 1.0f);
823 }
824
826 {
827 return math::clamp(
828 this->get_input("Eccentricity").get_single_value_default<float>(), 0.0f, 2.0f);
829 }
830
832 {
833 return CMPNodeKuwahara(this->get_input("Type").get_single_value_default<MenuValue>().value);
834 }
835};
836
838{
839 return new ConvertKuwaharaOperation(context, node);
840}
841
842static void node_register()
843{
844 static bke::bNodeType ntype;
845
846 cmp_node_type_base(&ntype, "CompositorNodeKuwahara"_ustr, CMP_NODE_KUWAHARA);
847 ntype.ui_name = "Kuwahara";
848 ntype.ui_description =
849 "Apply smoothing filter that preserves edges, for stylized and painterly effects";
850 ntype.enum_name_legacy = "KUWAHARA";
852 ntype.declare = node_declare;
853 ntype.initfunc = node_init;
855 ntype, "NodeKuwaharaData", node_free_standard_storage, node_copy_standard_storage);
858
860}
861NOD_REGISTER_NODE(node_register)
862
863} // namespace blender::nodes::node_composite_kuwahara_cc
#define NODE_CLASS_OP_FILTER
Definition BKE_node.hh:482
#define CMP_NODE_KUWAHARA
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define output
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 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
T load_pixel_zero(const int2 &texel) const
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
const T & get_single_value() const
void execute_classic_summed_area_table_cpu(const Result &table, const Result &squared_table)
void compute_classic(const Result *input, const Result *table, const Result *squared_table, const Result &size_input, Result &output, const int2 size)
void execute_classic_summed_area_table_gpu(const Result &table, const Result &squared_table)
constexpr T sign(T) RET
#define weighted_sum(val0, val1, val2, val3, weights)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:235
void symmetric_separable_blur(Context &context, const Result &input, Result &output, const float2 &radius, const math::FilterKernel filter_type=math::FilterKernel::Gauss, const bool extend_bounds=false)
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
void summed_area_table(Context &context, Result &input, Result &output, SummedAreaTableOperation operation=SummedAreaTableOperation::Identity)
float4 summed_area_table_sum(const Result &table, const int2 &lower_bound, const int2 &upper_bound)
void parallel_for(const int2 range, const Function &function)
T cos(const AngleRadianBase< T > &a)
T pow(const T &x, const T &power)
T clamp(const T &a, const T &min, const T &max)
T sqrt(const T &a)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
T min(const T &a, const T &b)
T square(const T &a)
T ceil(const T &a)
T sin(const AngleRadianBase< T > &a)
T max(const T &a, const T &b)
T abs(const T &a)
static void node_init(bNodeTree *, bNode *node)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_declare(NodeDeclarationBuilder &b)
@ CMP_NODE_KUWAHARA_ANISOTROPIC
@ CMP_NODE_KUWAHARA_CLASSIC
MatBase< float, 2, 2 > float2x2
BMesh const char void * data
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 GPU_shader_uniform_1f(gpu::Shader *sh, const char *name, float value)
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
@ PROP_FACTOR
Definition RNA_types.hh:308
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
static constexpr int _160
Definition BKE_node.hh:246
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
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
#define N_(msgid)