274 const Result *squared_table,
282 float4 mean_of_squared_color_of_quadrants[4] = {
284 float4 mean_of_color_of_quadrants[4] = {
288 for (
int q = 0; q < 4; q++) {
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);
299 int2 region_size = corrected_upper_bound - corrected_lower_bound +
int2(1);
300 int quadrant_pixel_count = region_size.x * region_size.y;
302 if constexpr (UseSummedAreaTable) {
305 *squared_table, lower_bound, upper_bound);
308 for (
int j = 0; j <= radius; j++) {
309 for (
int i = 0; i <= radius; i++) {
311 mean_of_color_of_quadrants[q] +=
color;
312 mean_of_squared_color_of_quadrants[q] +=
color *
color;
317 mean_of_color_of_quadrants[q] /= quadrant_pixel_count;
318 mean_of_squared_color_of_quadrants[q] /= quadrant_pixel_count;
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;
330 if (variance < minimum_variance) {
331 minimum_variance = variance;
332 mean_color_of_chosen_quadrant = color_mean;
336 output.store_pixel(texel,
Color(mean_color_of_chosen_quadrant));
425 output.allocate_texture(domain);
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;
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)) /
457 float first_eigenvalue = eigenvalue_first_term + eigenvalue_square_root_term;
458 float second_eigenvalue = eigenvalue_first_term - eigenvalue_square_root_term;
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 :
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;
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));
487 float ellipse_width_factor = (eccentricity + anisotropy) / eccentricity;
488 float ellipse_width = ellipse_width_factor * radius;
489 float ellipse_height = radius / ellipse_width_factor;
494 float cosine = unit_eigenvector.x;
495 float sine = unit_eigenvector.y;
501 float2(cosine / ellipse_width, -sine / ellipse_height),
502 float2(sine / ellipse_width, cosine / ellipse_height));
513 float2 ellipse_major_axis = ellipse_width * unit_eigenvector;
514 float2 ellipse_minor_axis = ellipse_height *
float2(unit_eigenvector.y, unit_eigenvector.x) *
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>) /
526 float cross_sector_overlap_parameter = (sector_center_overlap_parameter +
533 float4 weighted_mean_of_squared_color_of_sectors[8];
534 float4 weighted_mean_of_color_of_sectors[8];
537 float sum_of_weights_of_sectors[4];
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;
553 for (
int i = 0; i < number_of_sectors / 2; i++) {
554 sum_of_weights_of_sectors[i] = center_weight;
561 for (
int j = 0; j <= ellipse_bounds.y; j++) {
564 int lower_bound = j == 0 ? 1 : -ellipse_bounds.x;
565 for (
int i = lower_bound; i <= ellipse_bounds.x; i++) {
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) {
578 float sector_weights[8];
588 float2 polynomial = sector_center_overlap_parameter -
589 cross_sector_overlap_parameter *
math::square(disk_point);
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);
603 float2 rotated_polynomial = sector_center_overlap_parameter -
604 cross_sector_overlap_parameter *
607 math::max(0.0f, rotated_disk_point.y + rotated_polynomial.x));
609 math::max(0.0f, -rotated_disk_point.x + rotated_polynomial.y));
611 math::max(0.0f, -rotated_disk_point.y + rotated_polynomial.x));
613 math::max(0.0f, rotated_disk_point.x + rotated_polynomial.y));
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) /
628 float4 upper_color_squared = upper_color * upper_color;
629 float4 lower_color_squared = lower_color * lower_color;
631 for (
int k = 0; k < number_of_sectors; k++) {
632 float weight = sector_weights[k] * radial_gaussian_weight;
634 if (weight == 0.0f) {
637 sum_of_weights_of_sectors[k % (number_of_sectors / 2)] += weight;
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;
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;
655 float sum_of_weights = 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;
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);
673 sum_of_weights += weight;
679 if (sum_of_weights == 0.0f) {
740 const float corner_weight = 0.182f;
741 const float center_weight = 1.0f - 2.0f * corner_weight;
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;
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;
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);
764 float4 structure_tensor = float4(dxdx, dxdy, dxdy, dydy);
766 structure_tensor_image.store_pixel(texel, structure_tensor);
769 return structure_tensor_image;