MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ImageFilter.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file ImageFilter.hpp
5 * @brief 2D spatial filtering on normalised float image spans.
6 *
7 * Pure functions. No MayaFlux type dependencies.
8 *
9 * ## Conventions
10 * - All inputs are single-channel normalised float in [0, 1] unless noted
11 * - w and h are pixel dimensions; caller ensures span size == w * h
12 * - Border handling is clamp-to-edge throughout
13 * - Separable filters decompose into two 1D passes for O(w*h*k) cost
14 * rather than O(w*h*k^2)
15 * - Parallelism handled internally via Parallel::par_unseq
16 */
17
19
20// ============================================================================
21// Multi-plane separable convolution
22// ============================================================================
23
24/**
25 * @brief Fused horizontal separable filter over N planes.
26 *
27 * Applies @p kernel horizontally to all source planes simultaneously.
28 * One row scan reads all N sources and writes all N destinations.
29 * N is src.size(); src.size() must equal dst.size().
30 *
31 * No aliasing between any src pointer and any dst pointer.
32 *
33 * @param src Source plane pointers, each pointing to w * h floats.
34 * @param dst Destination plane pointers, each pointing to w * h floats.
35 * @param w Image width in pixels.
36 * @param h Image height in pixels.
37 * @param kernel 1D kernel. Length must be odd.
38 */
39MAYAFLUX_API void filter_horizontal_planes(
40 std::span<const float* const> src,
41 std::span<float* const> dst,
42 uint32_t w, uint32_t h,
43 std::span<const float> kernel);
44
45/**
46 * @brief Fused vertical separable filter over N planes.
47 *
48 * Same contract as filter_horizontal_planes but operates vertically.
49 *
50 * @param src Source plane pointers, each pointing to w * h floats.
51 * @param dst Destination plane pointers, each pointing to w * h floats.
52 * @param w Image width in pixels.
53 * @param h Image height in pixels.
54 * @param kernel 1D kernel. Length must be odd.
55 */
56MAYAFLUX_API void filter_vertical_planes(
57 std::span<const float* const> src,
58 std::span<float* const> dst,
59 uint32_t w, uint32_t h,
60 std::span<const float> kernel);
61// ============================================================================
62// Separable convolution
63// ============================================================================
64
65/**
66 * @brief Apply a separable 2D filter via two 1D passes.
67 *
68 * Applies kernel_x horizontally then kernel_y vertically.
69 * Both kernels are applied with clamp-to-edge border handling.
70 *
71 * @param src Single-channel float span, size must be w * h.
72 * @param w Image width in pixels.
73 * @param h Image height in pixels.
74 * @param kernel_x Horizontal 1D kernel. Length must be odd.
75 * @param kernel_y Vertical 1D kernel. Length must be odd.
76 * @return Filtered float vector of length w * h.
77 */
78[[nodiscard]] MAYAFLUX_API std::vector<float> filter_separable(
79 std::span<const float> src, uint32_t w, uint32_t h,
80 std::span<const float> kernel_x, std::span<const float> kernel_y);
81
82/**
83 * @brief Separable 2D filter writing into caller-supplied buffers.
84 *
85 * Horizontal pass writes into @p tmp, vertical pass writes into @p dst.
86 * All three spans must be size >= w * h and must not alias each other.
87 *
88 * @param src Input span, size w * h.
89 * @param tmp Scratch span for horizontal pass, size >= w * h.
90 * @param dst Output span, size >= w * h.
91 * @param w Image width in pixels.
92 * @param h Image height in pixels.
93 * @param kernel_x Horizontal 1D kernel, odd length.
94 * @param kernel_y Vertical 1D kernel, odd length.
95 */
96MAYAFLUX_API void filter_separable(
97 std::span<const float> src,
98 std::span<float> tmp,
99 std::span<float> dst,
100 uint32_t w, uint32_t h,
101 std::span<const float> kernel_x,
102 std::span<const float> kernel_y);
103
104// ============================================================================
105// Gaussian blur
106// ============================================================================
107
108/**
109 * @brief Separable Gaussian blur.
110 *
111 * Kernel radius is ceil(3 * sigma). Kernel is normalised to unit sum.
112 * Decomposes into horizontal and vertical 1D passes via filter_separable.
113 *
114 * @param src Single-channel float span, size must be w * h.
115 * @param w Image width in pixels.
116 * @param h Image height in pixels.
117 * @param sigma Standard deviation in pixels. Must be > 0.
118 * @return Blurred float vector of length w * h.
119 */
120[[nodiscard]] MAYAFLUX_API std::vector<float> gaussian_blur(
121 std::span<const float> src, uint32_t w, uint32_t h, float sigma);
122
123/**
124 * @brief Gaussian blur writing into caller-supplied buffers.
125 *
126 * @param src Input span, size w * h.
127 * @param tmp Scratch span for horizontal pass, size >= w * h.
128 * @param dst Output span, size >= w * h.
129 * @param w Image width in pixels.
130 * @param h Image height in pixels.
131 * @param sigma Standard deviation in pixels. Must be > 0.
132 */
133MAYAFLUX_API void gaussian_blur(
134 std::span<const float> src,
135 std::span<float> tmp,
136 std::span<float> dst,
137 uint32_t w, uint32_t h,
138 float sigma);
139
140} // namespace MayaFlux::Kinesis::Vision
uint32_t h
Definition InkPress.cpp:28
float sigma
std::vector< float > filter_separable(std::span< const float > src, uint32_t w, uint32_t h, std::span< const float > kernel_x, std::span< const float > kernel_y)
Apply a separable 2D filter via two 1D passes.
void filter_horizontal_planes(std::span< const float *const > src, std::span< float *const > dst, uint32_t w, uint32_t h, std::span< const float > kernel)
Fused horizontal separable filter over N planes.
std::vector< float > gaussian_blur(std::span< const float > src, uint32_t w, uint32_t h, float sigma)
Separable Gaussian blur.
void filter_vertical_planes(std::span< const float *const > src, std::span< float *const > dst, uint32_t w, uint32_t h, std::span< const float > kernel)
Fused vertical separable filter over N planes.