75 if (!image || width == 0 || height == 0 || !std::isfinite(sigma_pixels) || sigma_pixels <= 0)
80 constexpr size_t kMaxDim = size_t(1) << 30;
81 if (width > kMaxDim || height > kMaxDim)
86 const double pad_d = std::ceil(3.0 * sigma_pixels);
87 if (pad_d >
static_cast<double>(kMaxDim))
89 const size_t pad =
static_cast<size_t>(pad_d);
92 if (pad > (kMaxDim - width) / 2 || pad > (kMaxDim - height) / 2)
94 const size_t padded_width = width + 2 * pad;
95 const size_t padded_height = height + 2 * pad;
98 const size_t W = fft::next_power_of_two(padded_width);
99 const size_t H = fft::next_power_of_two(padded_height);
103 constexpr size_t MAX_FFT_ELEMENTS = 64 * 1024 * 1024;
104 if (W > MAX_FFT_ELEMENTS / H)
106 const size_t Npix_padded = W * H;
109 auto freq_coord = [](
int k,
int dim) ->
double {
110 return (k <= dim / 2) ? double(k) / dim : double(k - dim) / dim;
114 const double two_pi2_sigma2 = 2.0 * M_PI * M_PI * sigma_pixels * sigma_pixels;
116 for (
int channel = 0; channel < 3; channel++) {
118 std::vector<fft::cd> data(Npix_padded, {0.0, 0.0});
121 for (
size_t y = 0; y < H; y++) {
122 int py =
static_cast<int>(y) -
static_cast<int>(pad);
123 size_t src_y = reflect_index(py,
static_cast<int>(height));
124 for (
size_t x = 0; x < W; x++) {
125 int px =
static_cast<int>(x) -
static_cast<int>(pad);
126 size_t src_x = reflect_index(px,
static_cast<int>(width));
127 data[y * W + x] = fft::cd(image[(src_y * width + src_x) * 4 + channel], 0.0);
132 fft::iterative_fft_2d(data, W, H,
false);
135 for (
size_t y = 0; y < H; y++) {
136 double fy2 = freq_coord(y, H) * freq_coord(y, H);
137 for (
size_t x = 0; x < W; x++) {
138 double fx2 = freq_coord(x, W) * freq_coord(x, W);
139 double gain = std::exp(-two_pi2_sigma2 * (fx2 + fy2));
140 data[y * W + x] *= gain;
145 fft::iterative_fft_2d(data, W, H,
true);
148 for (
size_t y = 0; y < height; y++) {
149 for (
size_t x = 0; x < width; x++) {
152 double v = data[py * W + px].real();
157 image[(y * width + x) * 4 + channel] =
static_cast<uint8_t
>(std::lrint(v));
180void threshold_image(uint8_t* image,
const int width,
const int height,
const int num_thresholds) {
181 if (num_thresholds <= 0) {
184 const uint8_t REGION_SIZE(255 / num_thresholds);
187 img.loadFromBuffer(image, width, height, ImageLib::RGBA_CONVERTER<uint8_t>);
189 const auto imgWidth {img.getWidth()}, imgHeight {img.getHeight()};
191 p.red = quantize(p.red, REGION_SIZE);
192 p.green = quantize(p.green, REGION_SIZE);
193 p.blue = quantize(p.blue, REGION_SIZE);
196 const auto& modified = img.getData();
204 uint8_t* image,
const int width,
const int height,
const int num_thresholds
206 if (num_thresholds <= 0) {
210 img.loadFromBuffer(image, width, height, ImageLib::RGBA_CONVERTER<uint8_t>);
212 const auto imgWidth {img.getWidth()}, imgHeight {img.getHeight()};
214 const bool R {p.red < num_thresholds};
215 const bool G {p.green < num_thresholds};
216 const bool B {p.blue < num_thresholds};
222 const auto& modified = img.getData();
void black_threshold_image(uint8_t *image, const int width, const int height, const int num_thresholds)
Apply black-thresholding to an image.
void gaussian_blur_fft(uint8_t *image, size_t width, size_t height, double sigma)
Apply a Gaussian blur to an image using FFT.
void threshold_image(uint8_t *image, const int width, const int height, const int num_thresholds)
Apply a thresholding operation to an image.