Skip to main content

qualia_vision/biosense/magnification/
laplacian_pyramid_build.rs

1//! Laplacian pyramid from a packed Gaussian pyramid (caller-buffered).
2
3use super::gaussian_pyramid_build::{PyramidLevelMeta, MAX_PYRAMID_LEVELS};
4use crate::cv::error::CvError;
5
6/// Expand a coarser level to parent resolution by nearest-neighbor upsample + light blur.
7fn expand_to(
8    coarse: &[f32],
9    cw: usize,
10    ch: usize,
11    parent_w: usize,
12    parent_h: usize,
13    dest: &mut [f32],
14) {
15    debug_assert!(dest.len() >= parent_w * parent_h);
16    for y in 0..parent_h {
17        let cy = (y / 2).min(ch - 1);
18        for x in 0..parent_w {
19            let cx = (x / 2).min(cw - 1);
20            dest[y * parent_w + x] = coarse[cy * cw + cx];
21        }
22    }
23}
24
25/// Build Laplacian levels into `lap_packed` (same layout as Gaussian: L_i = G_i − expand(G_{i+1}),
26/// top residual L_{n-1} = G_{n-1}).
27///
28/// `gauss_packed` / `gauss_meta` from [`super::gaussian_pyramid_build::gaussian_pyramid_build`].
29/// `n_levels` must match the Gaussian pyramid depth.
30///
31/// Scratch: `expand_scratch` ≥ max parent level size (level 0 size is safe).
32pub fn laplacian_pyramid_build(
33    gauss_packed: &[f32],
34    gauss_meta: &[PyramidLevelMeta; MAX_PYRAMID_LEVELS],
35    n_levels: usize,
36    lap_packed: &mut [f32],
37    expand_scratch: &mut [f32],
38) -> Result<(), CvError> {
39    if n_levels == 0 || n_levels > MAX_PYRAMID_LEVELS {
40        return Err(CvError::InvalidParameter);
41    }
42    let total: usize = (0..n_levels).map(|i| gauss_meta[i].len).sum();
43    if gauss_packed.len() < total || lap_packed.len() < total {
44        return Err(CvError::BufferTooSmall);
45    }
46
47    // Top residual
48    let top = n_levels - 1;
49    let to = gauss_meta[top].offset;
50    let tl = gauss_meta[top].len;
51    lap_packed[to..to + tl].copy_from_slice(&gauss_packed[to..to + tl]);
52
53    if n_levels == 1 {
54        return Ok(());
55    }
56
57    for i in (0..n_levels - 1).rev() {
58        let parent = &gauss_meta[i];
59        let child = &gauss_meta[i + 1];
60        let pw = parent.width as usize;
61        let ph = parent.height as usize;
62        if expand_scratch.len() < parent.len {
63            return Err(CvError::BufferTooSmall);
64        }
65        let coarse = &gauss_packed[child.offset..child.offset + child.len];
66        expand_to(
67            coarse,
68            child.width as usize,
69            child.height as usize,
70            pw,
71            ph,
72            expand_scratch,
73        );
74        let g = &gauss_packed[parent.offset..parent.offset + parent.len];
75        let dest = &mut lap_packed[parent.offset..parent.offset + parent.len];
76        for k in 0..parent.len {
77            dest[k] = g[k] - expand_scratch[k];
78        }
79    }
80    Ok(())
81}
82
83#[cfg(test)]
84mod tests {
85    use super::*;
86    use crate::biosense::magnification::gaussian_pyramid_build::{
87        gaussian_pyramid_build, gaussian_pyramid_scratch_elems,
88    };
89
90    #[test]
91    fn top_equals_gaussian_top() {
92        let w = 8u32;
93        let h = 8u32;
94        let src: Vec<f32> = (0..64).map(|i| (i % 7) as f32).collect();
95        let need = gaussian_pyramid_scratch_elems(w, h, 3);
96        let mut g = vec![0.0f32; need];
97        let mut meta = [PyramidLevelMeta::default(); MAX_PYRAMID_LEVELS];
98        let n = gaussian_pyramid_build(&src, w, h, 3, &mut g, &mut meta).unwrap();
99        let mut lap = vec![0.0f32; need];
100        let mut scratch = vec![0.0f32; (w * h) as usize];
101        laplacian_pyramid_build(&g, &meta, n, &mut lap, &mut scratch).unwrap();
102        let top = n - 1;
103        let o = meta[top].offset;
104        let l = meta[top].len;
105        for k in 0..l {
106            assert!((lap[o + k] - g[o + k]).abs() < 1e-5);
107        }
108    }
109}