| 721 | } |
| 722 | |
| 723 | image_channels_t imvignette(const image_channels_t &A, double min_radius, |
| 724 | double exponent) { |
| 725 | auto image = A; |
| 726 | if (image.empty()) { |
| 727 | return A; |
| 728 | } |
| 729 | if (image.size() < 2) { |
| 730 | image = gray2rgb(image); |
| 731 | } |
| 732 | if (image.size() < 3) { |
| 733 | image.emplace_back(image[0]); |
| 734 | } |
| 735 | auto [h, w] = size(image[0]); |
| 736 | if (image.size() < 4) { |
| 737 | image.emplace_back(image_channel_t(h, image_row_t(w, 255))); |
| 738 | } |
| 739 | double center_x = static_cast<double>(w) / 2; |
| 740 | double center_y = static_cast<double>(h) / 2; |
| 741 | double radius_sq = pow(min(h / 2, w / 2) * min_radius, 2); |
| 742 | double max_t1 = pow(static_cast<double>(h) - center_y, 2); |
| 743 | double max_t2 = pow(static_cast<double>(w) - center_x, 2); |
| 744 | double max_t_minus_radius = (max_t1 + max_t2) - radius_sq; |
| 745 | auto &alpha_channel = image[3]; |
| 746 | for (size_t i = 0; i < h; ++i) { |
| 747 | for (size_t j = 0; j < w; ++j) { |
| 748 | double t1 = pow(static_cast<double>(i) - center_y, 2); |
| 749 | double t2 = pow(static_cast<double>(j) - center_x, 2); |
| 750 | if (t1 + t2 > radius_sq) { |
| 751 | double norm_dist_from_r = |
| 752 | ((t1 + t2) - radius_sq) / max_t_minus_radius; |
| 753 | norm_dist_from_r = pow(norm_dist_from_r, exponent); |
| 754 | alpha_channel[i][j] = static_cast< |
| 755 | std::decay_t<decltype(alpha_channel[i][j])>>( |
| 756 | 255. * (1. - norm_dist_from_r)); |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | return image; |
| 761 | } |
| 762 | |
| 763 | vector_2d transpose(const vector_2d &z) { |
| 764 | vector_2d z2(z[0].size(), vector_1d(z.size())); |