std1.0.0-beta-7 SafeC docs

Source dsp/imaging.h

View in the repository

// SafeC Standard Library — 2D DSP (imaging)
//
// The 2D analogues of std/dsp's 1D building blocks: conv2d is 2D
// convolution with zero-padded ("same" size) boundaries; fft2d/ifft2d
// are the row-column-decomposition 2D FFT (apply the existing 1D fft.h
// transform to every row, then to every column — the standard way to get
// a 2D FFT from a 1D one, exact because the 2D DFT is separable); the
// kernel generators are the handful of convolution kernels almost every
// image pipeline needs (Gaussian blur, Sobel edge gradients).
#pragma once
#include <std/dsp/complex_dsp.h>

namespace std {

// 'img' is h*w row-major; 'kernel' is kh*kw row-major (kh, kw should be
// odd so the kernel has a well-defined center tap); 'out' must have room
// for h*w doubles (same size as 'img' — out-of-bounds kernel taps read as
// zero, i.e. zero-padded boundary handling).
int conv2d([]const double img, unsigned long h, unsigned long w,
            []const double kernel, unsigned long kh, unsigned long kw, []double out);

// In-place 2D FFT/IFFT via row-column decomposition. Both h and w must
// be powers of two (see fft.h).
int fft2d([]Complex img, unsigned long h, unsigned long w);
int ifft2d([]Complex img, unsigned long h, unsigned long w);

// Fills a size*size buffer (size should be odd) with a normalized
// (sums to 1) 2D Gaussian kernel of standard deviation 'sigma'.
int gaussian_kernel([]double kernel, unsigned long size, double sigma);

// Fixed 3x3 Sobel gradient kernels (horizontal/vertical edge response);
// 'kernel' must have room for 9 doubles, row-major.
int sobel_x_kernel([]double kernel);
int sobel_y_kernel([]double kernel);

// Single precision: conv2d / fft2d / ifft2d on float and Complex32, same
// contracts (kernels can come from the double helpers above, rounded).
int conv2d_f32([]const float img, unsigned long h, unsigned long w,
            []const float kernel, unsigned long kh, unsigned long kw, []float out);
int fft2d_f32([]Complex32 img, unsigned long h, unsigned long w);
int ifft2d_f32([]Complex32 img, unsigned long h, unsigned long w);

// Internal: the (reference, length) forms the slice API above wraps
// (std/dsp's own code calls these; not API).
void conv2d_n_(const &double img, unsigned long h, unsigned long w,
            const &double kernel, unsigned long kh, unsigned long kw, &double out);
void conv2d_f32_n_(const &float img, unsigned long h, unsigned long w,
                const &float kernel, unsigned long kh, unsigned long kw, &float out);
void fft2d_n_(&Complex img, unsigned long h, unsigned long w);
void ifft2d_n_(&Complex img, unsigned long h, unsigned long w);
void fft2d_f32_n_(&Complex32 img, unsigned long h, unsigned long w);
void ifft2d_f32_n_(&Complex32 img, unsigned long h, unsigned long w);
void gaussian_kernel_n_(&double kernel, unsigned long size, double sigma);
void sobel_x_kernel_n_(&double kernel);
void sobel_y_kernel_n_(&double kernel);

} // namespace std