fovea 0.1.1

A high-precision, type-safe computer vision library guaranteeing absolute image correctness at compile time
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
//! 2D convolution and correlation wrappers built on [`fold_neighborhood`].
//!
//! Convolution is closely related to correlation — the only difference is
//! that convolution flips (rotates 180°) the kernel before sliding it
//! across the image. For symmetric kernels the two operations are
//! identical.
//!
//! This module provides:
//!
//! - [`convolve_into`] / [`convolve`] — true convolution (kernel is flipped)
//! - [`correlate_into`] / [`correlate`] — cross-correlation (kernel as-is)
//!
//! All functions require:
//! - `P: LinearPixel<f32>` — source pixels support `scale(f32)`
//! - `P::Accumulator: Default` — zero-initialisation of the running sum
//! - `Out: FromLinear<P::Accumulator>` — convert accumulated value to output pixel

use crate::border::BorderPolicy;
use crate::image::Kernel;
use crate::image::{Image, RasterImage, RasterImageMut};
use crate::pixel::{FromLinear, LinearPixel, ZeroablePixel};
use crate::transform::fold::{FoldItem, FoldOp, fold_neighborhood, fold_neighborhood_into};

// ─── ConvolveFold ────────────────────────────────────────────────────────────

/// A [`FoldOp`] that computes the weighted sum used by convolution and
/// correlation.
///
/// For each neighbor, it scales the source pixel by the kernel weight and
/// accumulates: `Out::from_linear( Σ pixel_i.scale(weight_i) )`.
///
/// This struct replaces the old `convolve_fold` closure. Because it
/// implements `FoldOp` with a generic `fold` method, both the interior
/// (hot) and boundary (cold) paths are fully monomorphized — no `dyn
/// Iterator` vtable dispatch.
pub(crate) struct ConvolveFold<P, Out> {
    _marker: core::marker::PhantomData<(P, Out)>,
}

impl<P, Out> ConvolveFold<P, Out> {
    #[inline(always)]
    pub(crate) fn new() -> Self {
        Self {
            _marker: core::marker::PhantomData,
        }
    }
}

impl<P, Out> FoldOp<P, f32> for ConvolveFold<P, Out>
where
    P: Copy + LinearPixel<f32>,
    <P as LinearPixel<f32>>::Accumulator: Default,
    Out: FromLinear<<P as LinearPixel<f32>>::Accumulator>,
{
    type Accumulator = <P as LinearPixel<f32>>::Accumulator;
    type Output = Out;

    #[inline(always)]
    fn init(&self) -> Self::Accumulator {
        <P as LinearPixel<f32>>::Accumulator::default()
    }

    #[inline(always)]
    fn accumulate(&self, acc: &mut Self::Accumulator, item: FoldItem<P, f32>) {
        *acc = item.pixel.scale_add(item.weight, *acc);
    }

    #[inline(always)]
    fn finalize(&mut self, acc: Self::Accumulator) -> Out {
        Out::from_linear(acc)
    }
}

/// Write the result of convolving `image` with the given kernel into `output`.
///
/// This is the **base method** — [`convolve`] is a convenience wrapper
/// that allocates the output for you.
///
/// # Convolution vs. correlation
///
/// Convolution rotates the kernel 180° before sliding. This function
/// uses [`Kernel::flipped`] to obtain the rotated kernel, then delegates
/// to [`fold_neighborhood_into`].
///
/// For symmetric kernels (box blur, Gaussian, Laplacian, etc.) the flip
/// has no effect. It only matters for asymmetric kernels like Sobel or
/// Prewitt.
///
/// # Panics
///
/// Panics if `output` is smaller than the region returned by
/// `border.output_region()`.
///
/// # Example
///
/// ```
/// use fovea::image::{Image, ImageView, ImageViewMut, Neighborhood};
/// use fovea::Size;
/// use fovea::border::Clamp;
/// use fovea::pixel::MonoF32;
/// use fovea::transform::convolve_into;
///
/// let src = Image::fill(5, 5, MonoF32(1.0));
/// let kernel = Neighborhood::<f32, 3, 3>::identity_3x3();
///
/// let border = Clamp;
/// let out_region = fovea::border::BorderPolicy::<Image<MonoF32>>::output_region(
///     &border, src.size(), kernel.weights().size(), kernel.anchor(),
/// );
/// let mut out = Image::<MonoF32>::zero(out_region.size.width, out_region.size.height);
///
/// convolve_into(&src, &kernel, &border, &mut out);
///
/// for y in 0..out.height() {
///     for x in 0..out.width() {
///         assert!((out.pixel_at(x, y).0 - 1.0).abs() < 1e-6);
///     }
/// }
/// ```
///
/// **Note:** Convolution flips the kernel before applying it. For the
/// unflipped variant (cross-correlation), see [`correlate_into`]. The
/// [module docs](crate::transform) explain the mathematical distinction.
pub fn convolve_into<I, K, B, O, P, Out>(image: &I, kernel: &K, border: &B, output: &mut O)
where
    I: RasterImage<Pixel = P>,
    P: Copy + LinearPixel<f32>,
    <P as LinearPixel<f32>>::Accumulator: Default,
    K: Kernel<Weight = f32>,
    B: BorderPolicy<I>,
    O: RasterImageMut<Pixel = Out>,
    Out: FromLinear<<P as LinearPixel<f32>>::Accumulator>,
{
    // True convolution = correlation with a 180°-rotated kernel.
    // Kernel::flipped() preserves the concrete type, so stack-backed
    // kernels stay on the stack (zero heap allocation).
    let flipped = kernel.flipped();

    fold_neighborhood_into(
        image,
        flipped.weights(),
        flipped.anchor(),
        border,
        output,
        ConvolveFold::<P, Out>::new(),
    );
}

/// Convolve `image` with the given kernel and return a newly allocated
/// output [`Image`].
///
/// This is a convenience wrapper around [`convolve_into`]. The output
/// size is determined by `border.output_region(…)`.
///
/// # Example
///
/// ```
/// use fovea::image::{Image, ImageView, Neighborhood};
/// use fovea::border::Clamp;
/// use fovea::pixel::Mono8;
/// use fovea::transform::convolve;
///
/// let src = Image::fill(5, 5, Mono8::new(10));
/// let kernel = Neighborhood::<f32, 3, 3>::box_blur_3x3();
///
/// let result: Image<Mono8> = convolve(&src, &kernel, &Clamp);
///
/// // Uniform image convolved with box blur stays the same
/// for y in 0..result.height() {
///     for x in 0..result.width() {
///         assert_eq!(result.pixel_at(x, y), Mono8::new(10));
///     }
/// }
/// ```
///
/// **Note:** Convolution flips the kernel before applying it. For the
/// unflipped variant (cross-correlation), see [`correlate`]. The
/// [module docs](crate::transform) explain the mathematical distinction.
#[must_use]
pub fn convolve<I, K, B, P, Out>(image: &I, kernel: &K, border: &B) -> Image<Out>
where
    I: RasterImage<Pixel = P>,
    P: Copy + LinearPixel<f32>,
    <P as LinearPixel<f32>>::Accumulator: Default,
    K: Kernel<Weight = f32>,
    B: BorderPolicy<I>,
    Out: ZeroablePixel + FromLinear<<P as LinearPixel<f32>>::Accumulator>,
{
    let flipped = kernel.flipped();

    fold_neighborhood(
        image,
        flipped.weights(),
        flipped.anchor(),
        border,
        ConvolveFold::<P, Out>::new(),
    )
}

/// Perform correlation (cross-correlation) — identical to convolution but
/// **without** flipping the kernel.
///
/// This is useful when you already have the kernel in the orientation you
/// want (e.g., for template matching or when the kernel is symmetric).
///
/// # Panics
///
/// Panics if `output` is smaller than the region returned by
/// `border.output_region()`.
///
/// # Example
///
/// ```
/// use fovea::image::{Image, ImageView, ImageViewMut, Neighborhood};
/// use fovea::Size;
/// use fovea::border::Clamp;
/// use fovea::pixel::MonoF32;
/// use fovea::transform::correlate_into;
///
/// let src = Image::fill(5, 5, MonoF32(2.0));
/// let kernel = Neighborhood::<f32, 3, 3>::identity_3x3();
///
/// let border = Clamp;
/// let out_region = fovea::border::BorderPolicy::<Image<MonoF32>>::output_region(
///     &border, src.size(), kernel.weights().size(), kernel.anchor(),
/// );
/// let mut out = Image::<MonoF32>::zero(out_region.size.width, out_region.size.height);
///
/// correlate_into(&src, &kernel, &border, &mut out);
///
/// for y in 0..out.height() {
///     for x in 0..out.width() {
///         assert!((out.pixel_at(x, y).0 - 2.0).abs() < 1e-6);
///     }
/// }
/// ```
///
/// **Note:** Correlation applies the kernel without flipping. For the
/// flipped variant (true convolution), see [`convolve_into`]. The
/// [module docs](crate::transform) explain the mathematical distinction.
pub fn correlate_into<I, K, B, O, P, Out>(image: &I, kernel: &K, border: &B, output: &mut O)
where
    I: RasterImage<Pixel = P>,
    P: Copy + LinearPixel<f32>,
    <P as LinearPixel<f32>>::Accumulator: Default,
    K: Kernel<Weight = f32>,
    B: BorderPolicy<I>,
    O: RasterImageMut<Pixel = Out>,
    Out: FromLinear<<P as LinearPixel<f32>>::Accumulator>,
{
    fold_neighborhood_into(
        image,
        kernel.weights(),
        kernel.anchor(),
        border,
        output,
        ConvolveFold::<P, Out>::new(),
    );
}

/// Perform correlation and return a newly allocated output [`Image`].
///
/// Correlation is identical to convolution but without flipping the kernel.
/// See [`correlate_into`] for the base method.
///
/// # Example
///
/// ```
/// use fovea::image::{Image, ImageView, Neighborhood};
/// use fovea::border::Clamp;
/// use fovea::pixel::MonoF32;
/// use fovea::transform::correlate;
///
/// let src = Image::fill(5, 5, MonoF32(2.0));
/// let kernel = Neighborhood::<f32, 3, 3>::identity_3x3();
///
/// let result: Image<MonoF32> = correlate(&src, &kernel, &Clamp);
///
/// for y in 0..result.height() {
///     for x in 0..result.width() {
///         assert!((result.pixel_at(x, y).0 - 2.0).abs() < 1e-6);
///     }
/// }
/// ```
///
/// **Note:** Correlation applies the kernel without flipping. For the
/// flipped variant (true convolution), see [`convolve`]. The
/// [module docs](crate::transform) explain the mathematical distinction.
#[must_use]
pub fn correlate<I, K, B, P, Out>(image: &I, kernel: &K, border: &B) -> Image<Out>
where
    I: RasterImage<Pixel = P>,
    P: Copy + LinearPixel<f32>,
    <P as LinearPixel<f32>>::Accumulator: Default,
    K: Kernel<Weight = f32>,
    B: BorderPolicy<I>,
    Out: ZeroablePixel + FromLinear<<P as LinearPixel<f32>>::Accumulator>,
{
    fold_neighborhood(
        image,
        kernel.weights(),
        kernel.anchor(),
        border,
        ConvolveFold::<P, Out>::new(),
    )
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::border::{Clamp, Constant, Skip};
    use crate::image::{ImageView, Neighborhood};
    use crate::pixel::{Mono8, MonoF32};

    // ── helpers ──────────────────────────────────────────────────────────

    fn make_4x4_monof32() -> Image<MonoF32> {
        Image::generate(4, 4, |x, y| MonoF32((x + y * 4) as f32))
    }

    // ── identity kernel ─────────────────────────────────────────────────

    #[test]
    fn convolve_identity_preserves_image() {
        let src = make_4x4_monof32();
        let kernel = Neighborhood::<f32, 3, 3>::identity_3x3();
        let result: Image<MonoF32> = convolve(&src, &kernel, &Clamp);

        assert_eq!(result.width(), 4);
        assert_eq!(result.height(), 4);
        for y in 0..4 {
            for x in 0..4 {
                assert!(
                    (result.pixel_at(x, y).0 - src.pixel_at(x, y).0).abs() < 1e-6,
                    "mismatch at ({x}, {y}): got {}, expected {}",
                    result.pixel_at(x, y).0,
                    src.pixel_at(x, y).0,
                );
            }
        }
    }

    #[test]
    fn correlate_identity_preserves_image() {
        let src = make_4x4_monof32();
        let kernel = Neighborhood::<f32, 3, 3>::identity_3x3();
        let result: Image<MonoF32> = correlate(&src, &kernel, &Clamp);

        for y in 0..4 {
            for x in 0..4 {
                assert!((result.pixel_at(x, y).0 - src.pixel_at(x, y).0).abs() < 1e-6,);
            }
        }
    }

    // ── symmetric kernel: convolve == correlate ─────────────────────────

    #[test]
    fn symmetric_kernel_convolve_equals_correlate() {
        let src = make_4x4_monof32();
        let kernel = Neighborhood::<f32, 3, 3>::box_blur_3x3();

        let conv: Image<MonoF32> = convolve(&src, &kernel, &Clamp);
        let corr: Image<MonoF32> = correlate(&src, &kernel, &Clamp);

        assert_eq!(conv.width(), corr.width());
        assert_eq!(conv.height(), corr.height());
        for y in 0..conv.height() {
            for x in 0..conv.width() {
                assert!(
                    (conv.pixel_at(x, y).0 - corr.pixel_at(x, y).0).abs() < 1e-6,
                    "mismatch at ({x}, {y})",
                );
            }
        }
    }

    // ── box blur on uniform image ───────────────────────────────────────

    #[test]
    fn box_blur_uniform_image() {
        let src = Image::fill(6, 6, MonoF32(5.0));
        let kernel = Neighborhood::<f32, 3, 3>::box_blur_3x3();

        let result: Image<MonoF32> = convolve(&src, &kernel, &Clamp);

        for y in 0..result.height() {
            for x in 0..result.width() {
                assert!(
                    (result.pixel_at(x, y).0 - 5.0).abs() < 1e-5,
                    "at ({x}, {y}): {}",
                    result.pixel_at(x, y).0,
                );
            }
        }
    }

    // ── asymmetric kernel: convolve ≠ correlate ─────────────────────────

    #[test]
    fn asymmetric_kernel_convolve_differs_from_correlate() {
        // Sobel-Y kernel: [-1 0 1; -2 0 2; -1 0 1]
        // Flipped (180°):  [1 0 -1;  2 0 -2;  1 0 -1] = -sobel_y
        // So convolve(sobel_y) = correlate(-sobel_y) = -correlate(sobel_y).
        //
        // Use an image with a clear horizontal gradient so the response
        // is non-zero.
        let src = Image::generate(6, 6, |x, _y| MonoF32(x as f32));
        let kernel = Neighborhood::<f32, 3, 3>::sobel_y();

        let conv: Image<MonoF32> = convolve(&src, &kernel, &Clamp);
        let corr: Image<MonoF32> = correlate(&src, &kernel, &Clamp);

        // For this anti-symmetric kernel, convolve = -correlate
        let mut found_nonzero = false;
        for y in 0..conv.height() {
            for x in 0..conv.width() {
                let sum = conv.pixel_at(x, y).0 + corr.pixel_at(x, y).0;
                assert!(
                    sum.abs() < 1e-4,
                    "conv + corr should be ~0 at ({x}, {y}): conv={}, corr={}, sum={}",
                    conv.pixel_at(x, y).0,
                    corr.pixel_at(x, y).0,
                    sum,
                );
                if conv.pixel_at(x, y).0.abs() > 0.1 {
                    found_nonzero = true;
                }
            }
        }
        assert!(
            found_nonzero,
            "expected non-zero response on gradient image"
        );
    }

    // ── convolve_into writes correct output ──────────────────────────────

    #[test]
    fn convolve_into_writes_correct_output() {
        let src = Image::fill(4, 4, MonoF32(3.0));
        let kernel = Neighborhood::<f32, 3, 3>::identity_3x3();
        let border = Clamp;
        let out_region = BorderPolicy::<Image<MonoF32>>::output_region(
            &border,
            src.size(),
            kernel.weights().size(),
            kernel.anchor(),
        );
        let mut out = Image::<MonoF32>::zero(out_region.size.width, out_region.size.height);

        convolve_into(&src, &kernel, &border, &mut out);

        for y in 0..out.height() {
            for x in 0..out.width() {
                assert!((out.pixel_at(x, y).0 - 3.0).abs() < 1e-6);
            }
        }
    }

    // ── Skip border policy ──────────────────────────────────────────────

    #[test]
    fn convolve_with_skip_shrinks_output() {
        let src = Image::generate(6, 6, |x, y| MonoF32((x + y) as f32));
        let kernel = Neighborhood::<f32, 3, 3>::identity_3x3();

        let result: Image<MonoF32> = convolve(&src, &kernel, &Skip);

        // Skip with 3×3 kernel on 6×6 image → 4×4 output
        assert_eq!(result.width(), 4);
        assert_eq!(result.height(), 4);

        // Identity kernel should reproduce interior pixels
        for y in 0..4 {
            for x in 0..4 {
                assert!((result.pixel_at(x, y).0 - src.pixel_at(x + 1, y + 1).0).abs() < 1e-6,);
            }
        }
    }

    // ── Constant border policy ──────────────────────────────────────────

    #[test]
    fn convolve_constant_border_zero_padding() {
        let src = Image::fill(3, 3, MonoF32(1.0));
        let kernel = Neighborhood::<f32, 3, 3>::box_blur_3x3();
        let border = Constant(MonoF32(0.0));

        let result: Image<MonoF32> = convolve(&src, &kernel, &border);

        assert_eq!(result.width(), 3);
        assert_eq!(result.height(), 3);

        // Center pixel: all 9 neighbors are 1.0 → average = 1.0
        assert!((result.pixel_at(1, 1).0 - 1.0).abs() < 1e-5);

        // Corner pixel (0,0): 4 neighbors are 1.0, 5 are 0.0 → average = 4/9
        assert!((result.pixel_at(0, 0).0 - 4.0 / 9.0).abs() < 1e-5);

        // Edge pixel (1,0): 6 neighbors are 1.0, 3 are 0.0 → average = 6/9
        assert!((result.pixel_at(1, 0).0 - 6.0 / 9.0).abs() < 1e-5);
    }

    // ── u8 pixel type ───────────────────────────────────────────────────

    #[test]
    fn convolve_u8_identity() {
        let src = Image::generate(5, 5, |x, y| Mono8::new(((x + y * 5) % 256) as u8));
        let kernel = Neighborhood::<f32, 3, 3>::identity_3x3();

        let result: Image<Mono8> = convolve(&src, &kernel, &Clamp);

        for y in 0..5 {
            for x in 0..5 {
                assert_eq!(result.pixel_at(x, y), src.pixel_at(x, y));
            }
        }
    }

    #[test]
    fn convolve_u8_box_blur_uniform() {
        let src = Image::fill(4, 4, Mono8::new(100));
        let kernel = Neighborhood::<f32, 3, 3>::box_blur_3x3();

        let result: Image<Mono8> = convolve(&src, &kernel, &Clamp);

        for y in 0..result.height() {
            for x in 0..result.width() {
                assert_eq!(result.pixel_at(x, y), Mono8::new(100));
            }
        }
    }

    // ── 5×5 kernel ──────────────────────────────────────────────────────

    #[test]
    fn convolve_5x5_box_blur_uniform() {
        let src = Image::fill(8, 8, MonoF32(7.0));
        let kernel = Neighborhood::<f32, 5, 5>::box_blur_5x5();

        let result: Image<MonoF32> = convolve(&src, &kernel, &Clamp);

        for y in 0..result.height() {
            for x in 0..result.width() {
                assert!(
                    (result.pixel_at(x, y).0 - 7.0).abs() < 1e-4,
                    "at ({x}, {y}): {}",
                    result.pixel_at(x, y).0,
                );
            }
        }
    }

    // ── convolve and convolve_into produce same result ──────────────────

    #[test]
    fn convolve_and_convolve_into_match() {
        let src = make_4x4_monof32();
        let kernel = Neighborhood::<f32, 3, 3>::gaussian_3x3();

        let result_alloc: Image<MonoF32> = convolve(&src, &kernel, &Clamp);

        let border = Clamp;
        let out_region = BorderPolicy::<Image<MonoF32>>::output_region(
            &border,
            src.size(),
            kernel.weights().size(),
            kernel.anchor(),
        );
        let mut result_into = Image::<MonoF32>::zero(out_region.size.width, out_region.size.height);
        convolve_into(&src, &kernel, &border, &mut result_into);

        for y in 0..result_alloc.height() {
            for x in 0..result_alloc.width() {
                assert!(
                    (result_alloc.pixel_at(x, y).0 - result_into.pixel_at(x, y).0).abs() < 1e-6,
                );
            }
        }
    }

    // ── correlate and correlate_into produce same result ─────────────────

    #[test]
    fn correlate_and_correlate_into_match() {
        let src = make_4x4_monof32();
        let kernel = Neighborhood::<f32, 3, 3>::sobel_x();

        let result_alloc: Image<MonoF32> = correlate(&src, &kernel, &Clamp);

        let border = Clamp;
        let out_region = BorderPolicy::<Image<MonoF32>>::output_region(
            &border,
            src.size(),
            kernel.weights().size(),
            kernel.anchor(),
        );
        let mut result_into = Image::<MonoF32>::zero(out_region.size.width, out_region.size.height);
        correlate_into(&src, &kernel, &border, &mut result_into);

        for y in 0..result_alloc.height() {
            for x in 0..result_alloc.width() {
                assert!(
                    (result_alloc.pixel_at(x, y).0 - result_into.pixel_at(x, y).0).abs() < 1e-6,
                );
            }
        }
    }

    // ── Sobel on known gradient ─────────────────────────────────────────

    #[test]
    fn sobel_y_on_horizontal_gradient() {
        // Image where each column has constant value = x
        let src = Image::generate(5, 5, |x, _y| MonoF32(x as f32));
        let kernel = Neighborhood::<f32, 3, 3>::sobel_y();

        let result: Image<MonoF32> = convolve(&src, &kernel, &Skip);

        // Skip → 3×3 output (interior only)
        assert_eq!(result.width(), 3);
        assert_eq!(result.height(), 3);

        // All interior pixels should have the same magnitude
        let expected = result.pixel_at(0, 0).0;
        for y in 0..3 {
            for x in 0..3 {
                assert!(
                    (result.pixel_at(x, y).0 - expected).abs() < 1e-4,
                    "at ({x}, {y}): got {}, expected {expected}",
                    result.pixel_at(x, y).0,
                );
            }
        }
    }

    // ── Gaussian 3×3 un-normalised ──────────────────────────────────────

    #[test]
    fn gaussian_3x3_unnormalised_on_uniform() {
        // gaussian_3x3 weights sum to 16, so convolving a uniform image
        // of value v produces v * 16.
        let src = Image::fill(5, 5, MonoF32(1.0));
        let kernel = Neighborhood::<f32, 3, 3>::gaussian_3x3();

        let result: Image<MonoF32> = convolve(&src, &kernel, &Clamp);

        for y in 0..result.height() {
            for x in 0..result.width() {
                assert!(
                    (result.pixel_at(x, y).0 - 16.0).abs() < 1e-4,
                    "at ({x}, {y}): {}",
                    result.pixel_at(x, y).0,
                );
            }
        }
    }

    // ── Single-pixel image ──────────────────────────────────────────────

    #[test]
    fn convolve_single_pixel_clamp() {
        let src = Image::fill(1, 1, MonoF32(42.0));
        let kernel = Neighborhood::<f32, 3, 3>::identity_3x3();

        let result: Image<MonoF32> = convolve(&src, &kernel, &Clamp);

        assert_eq!(result.width(), 1);
        assert_eq!(result.height(), 1);
        assert!((result.pixel_at(0, 0).0 - 42.0).abs() < 1e-6);
    }

    // ── Laplacian on uniform → zero ─────────────────────────────────────

    #[test]
    fn laplacian_on_uniform_is_zero() {
        let src = Image::fill(6, 6, MonoF32(10.0));
        let kernel = Neighborhood::<f32, 3, 3>::laplacian();

        let result: Image<MonoF32> = convolve(&src, &kernel, &Clamp);

        for y in 0..result.height() {
            for x in 0..result.width() {
                assert!(
                    result.pixel_at(x, y).0.abs() < 1e-4,
                    "at ({x}, {y}): {}",
                    result.pixel_at(x, y).0,
                );
            }
        }
    }

    #[test]
    fn laplacian_8_on_uniform_is_zero() {
        let src = Image::fill(6, 6, MonoF32(7.0));
        let kernel = Neighborhood::<f32, 3, 3>::laplacian_8();

        let result: Image<MonoF32> = convolve(&src, &kernel, &Clamp);

        for y in 0..result.height() {
            for x in 0..result.width() {
                assert!(
                    result.pixel_at(x, y).0.abs() < 1e-4,
                    "at ({x}, {y}): {}",
                    result.pixel_at(x, y).0,
                );
            }
        }
    }

    // ── Prewitt on uniform → zero ───────────────────────────────────────

    #[test]
    fn prewitt_on_uniform_is_zero() {
        let src = Image::fill(5, 5, MonoF32(3.0));
        let kx = Neighborhood::<f32, 3, 3>::prewitt_x();
        let ky = Neighborhood::<f32, 3, 3>::prewitt_y();

        let rx: Image<MonoF32> = convolve(&src, &kx, &Clamp);
        let ry: Image<MonoF32> = convolve(&src, &ky, &Clamp);

        for y in 0..rx.height() {
            for x in 0..rx.width() {
                assert!(rx.pixel_at(x, y).0.abs() < 1e-4);
                assert!(ry.pixel_at(x, y).0.abs() < 1e-4);
            }
        }
    }

    // ── Scharr on uniform → zero ────────────────────────────────────────

    #[test]
    fn scharr_on_uniform_is_zero() {
        let src = Image::fill(5, 5, MonoF32(3.0));
        let kx = Neighborhood::<f32, 3, 3>::scharr_x();
        let ky = Neighborhood::<f32, 3, 3>::scharr_y();

        let rx: Image<MonoF32> = convolve(&src, &kx, &Clamp);
        let ry: Image<MonoF32> = convolve(&src, &ky, &Clamp);

        for y in 0..rx.height() {
            for x in 0..rx.width() {
                assert!(rx.pixel_at(x, y).0.abs() < 1e-4);
                assert!(ry.pixel_at(x, y).0.abs() < 1e-4);
            }
        }
    }
}