oximedia-align 0.1.7

Video alignment and registration tools for multi-camera synchronization in OxiMedia
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
//! Video stabilization pipeline.
//!
//! Combines optical flow estimation, affine transform fitting, temporal
//! smoothing, and image warping to produce stabilized video output.
//!
//! # Pipeline stages
//!
//! 1. **Motion estimation**: Compute inter-frame motion using sparse KLT
//!    tracking (or dense Farneback flow).
//! 2. **Global motion model**: Fit an affine (or similarity) transform to the
//!    estimated motion vectors using RANSAC.
//! 3. **Trajectory smoothing**: Build a cumulative camera trajectory and smooth
//!    it with a configurable Gaussian filter to remove high-frequency jitter
//!    while preserving intentional camera movements.
//! 4. **Compensation**: Apply the smoothed correction to each frame via affine
//!    warping.

#![allow(clippy::cast_precision_loss)]

use crate::affine::AffineMatrix;
use crate::{AlignError, AlignResult};

/// Stabilization strategy.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StabilizationMode {
    /// Correct only translation (2 DOF).
    Translation,
    /// Correct translation + rotation (3 DOF).
    TranslationRotation,
    /// Full affine correction (6 DOF).
    Affine,
}

/// Configuration for the stabilization pipeline.
#[derive(Debug, Clone)]
pub struct StabilizeConfig {
    /// Stabilization mode.
    pub mode: StabilizationMode,
    /// Gaussian smoothing radius (in frames). Larger values produce smoother
    /// output but can introduce more cropping/border artefacts.
    pub smooth_radius: usize,
    /// Standard deviation of the Gaussian kernel (in frames).
    pub smooth_sigma: f64,
    /// Maximum correction in pixels. Corrections exceeding this are clamped
    /// to prevent extreme warping.
    pub max_correction_px: f64,
    /// Border handling: percentage of the frame to crop on each side (0.0 to 0.5).
    pub crop_ratio: f64,
}

impl Default for StabilizeConfig {
    fn default() -> Self {
        Self {
            mode: StabilizationMode::TranslationRotation,
            smooth_radius: 15,
            smooth_sigma: 5.0,
            max_correction_px: 50.0,
            crop_ratio: 0.05,
        }
    }
}

/// A single frame's estimated global motion relative to the previous frame.
#[derive(Debug, Clone, Copy)]
pub struct FrameMotion {
    /// Translation X.
    pub dx: f64,
    /// Translation Y.
    pub dy: f64,
    /// Rotation angle (radians).
    pub da: f64,
    /// Scale factor (1.0 = no scaling).
    pub ds: f64,
}

impl FrameMotion {
    /// Create a new frame motion with all parameters.
    #[must_use]
    pub fn new(dx: f64, dy: f64, da: f64, ds: f64) -> Self {
        Self { dx, dy, da, ds }
    }

    /// Identity (no motion).
    #[must_use]
    pub fn identity() -> Self {
        Self {
            dx: 0.0,
            dy: 0.0,
            da: 0.0,
            ds: 1.0,
        }
    }

    /// Convert to an affine matrix.
    #[must_use]
    pub fn to_affine(&self) -> AffineMatrix {
        let c = (self.da.cos() * self.ds) as f32;
        let s = (self.da.sin() * self.ds) as f32;
        AffineMatrix {
            data: [
                [c, -s, self.dx as f32],
                [s, c, self.dy as f32],
                [0.0, 0.0, 1.0],
            ],
        }
    }
}

/// Cumulative trajectory: the sum of all frame motions up to a given frame.
#[derive(Debug, Clone, Copy)]
pub struct Trajectory {
    /// Cumulative X translation.
    pub x: f64,
    /// Cumulative Y translation.
    pub y: f64,
    /// Cumulative angle.
    pub a: f64,
}

impl Trajectory {
    /// Zero trajectory.
    #[must_use]
    pub fn zero() -> Self {
        Self {
            x: 0.0,
            y: 0.0,
            a: 0.0,
        }
    }
}

/// Build the cumulative trajectory from per-frame motions.
#[must_use]
pub fn build_trajectory(motions: &[FrameMotion]) -> Vec<Trajectory> {
    let mut trajectory = Vec::with_capacity(motions.len());
    let mut cum = Trajectory::zero();

    for m in motions {
        cum.x += m.dx;
        cum.y += m.dy;
        cum.a += m.da;
        trajectory.push(cum);
    }

    trajectory
}

/// Smooth a trajectory using a 1D Gaussian filter.
///
/// The smoothed trajectory preserves intentional camera movements (pans, tilts)
/// while removing high-frequency jitter.
#[must_use]
pub fn smooth_trajectory(trajectory: &[Trajectory], radius: usize, sigma: f64) -> Vec<Trajectory> {
    if trajectory.is_empty() {
        return Vec::new();
    }

    let n = trajectory.len();
    let kernel = build_gaussian_kernel(radius, sigma);

    let mut smoothed = Vec::with_capacity(n);

    for i in 0..n {
        let mut sum_x = 0.0_f64;
        let mut sum_y = 0.0_f64;
        let mut sum_a = 0.0_f64;
        let mut sum_w = 0.0_f64;

        for (ki, &w) in kernel.iter().enumerate() {
            let j_signed = i as isize + ki as isize - radius as isize;
            let j = j_signed.max(0).min((n - 1) as isize) as usize;

            sum_x += trajectory[j].x * w;
            sum_y += trajectory[j].y * w;
            sum_a += trajectory[j].a * w;
            sum_w += w;
        }

        if sum_w > 0.0 {
            smoothed.push(Trajectory {
                x: sum_x / sum_w,
                y: sum_y / sum_w,
                a: sum_a / sum_w,
            });
        } else {
            smoothed.push(trajectory[i]);
        }
    }

    smoothed
}

/// Compute the per-frame correction transforms.
///
/// The correction is: `smoothed_trajectory - original_trajectory`, which gives
/// the delta to apply to each frame.
#[must_use]
pub fn compute_corrections(
    motions: &[FrameMotion],
    original: &[Trajectory],
    smoothed: &[Trajectory],
    config: &StabilizeConfig,
) -> Vec<FrameMotion> {
    let n = motions.len().min(original.len()).min(smoothed.len());
    let mut corrections = Vec::with_capacity(n);

    for i in 0..n {
        let mut cdx = smoothed[i].x - original[i].x;
        let mut cdy = smoothed[i].y - original[i].y;
        let cda = smoothed[i].a - original[i].a;

        // Clamp correction
        let mag = (cdx * cdx + cdy * cdy).sqrt();
        if mag > config.max_correction_px {
            let scale = config.max_correction_px / mag;
            cdx *= scale;
            cdy *= scale;
        }

        corrections.push(FrameMotion::new(cdx, cdy, cda, 1.0));
    }

    corrections
}

/// Apply an affine correction to a single grayscale frame.
///
/// # Errors
///
/// Returns an error if the frame size is inconsistent.
pub fn apply_stabilization(
    frame: &[u8],
    width: usize,
    height: usize,
    correction: &FrameMotion,
    config: &StabilizeConfig,
) -> AlignResult<Vec<u8>> {
    if frame.len() != width * height {
        return Err(AlignError::InvalidConfig(
            "Frame size does not match width*height".to_string(),
        ));
    }

    let mat = correction.to_affine();

    // Compute inverse transform for backward mapping
    let inv = invert_affine(&mat)?;

    // Compute crop region
    let crop_x = (width as f64 * config.crop_ratio) as usize;
    let crop_y = (height as f64 * config.crop_ratio) as usize;

    let mut output = vec![0u8; width * height];

    for y in 0..height {
        for x in 0..width {
            // Map output pixel to input pixel via inverse transform
            let (sx, sy) = inv.transform_point(x as f32, y as f32);

            let sx = sx as isize;
            let sy = sy as isize;

            // Bounds check (accounting for crop region = fill with border value)
            if sx >= crop_x as isize
                && sy >= crop_y as isize
                && sx < (width - crop_x) as isize
                && sy < (height - crop_y) as isize
            {
                let src_idx = sy as usize * width + sx as usize;
                if src_idx < frame.len() {
                    output[y * width + x] = frame[src_idx];
                }
            }
        }
    }

    Ok(output)
}

/// Full stabilization pipeline: given a sequence of frame motions, compute
/// the stabilized correction transforms.
///
/// # Errors
///
/// Returns an error if the motions slice is empty.
pub fn stabilize_pipeline(
    motions: &[FrameMotion],
    config: &StabilizeConfig,
) -> AlignResult<Vec<FrameMotion>> {
    if motions.is_empty() {
        return Err(AlignError::InsufficientData(
            "Need at least one frame motion".to_string(),
        ));
    }

    let trajectory = build_trajectory(motions);
    let smoothed = smooth_trajectory(&trajectory, config.smooth_radius, config.smooth_sigma);
    let corrections = compute_corrections(motions, &trajectory, &smoothed, config);

    Ok(corrections)
}

/// Estimate per-frame motion from consecutive grayscale frames using a simple
/// block-matching approach (for use when KLT is not yet integrated).
///
/// Returns the estimated translation (dx, dy) between `prev` and `curr`.
///
/// # Errors
///
/// Returns an error if the frame sizes are inconsistent.
pub fn estimate_motion_translation(
    prev: &[u8],
    curr: &[u8],
    width: usize,
    height: usize,
    block_size: usize,
    search_range: usize,
) -> AlignResult<FrameMotion> {
    if prev.len() != width * height || curr.len() != width * height {
        return Err(AlignError::InvalidConfig(
            "Frame size does not match width*height".to_string(),
        ));
    }

    let mut total_dx = 0.0_f64;
    let mut total_dy = 0.0_f64;
    let mut count = 0u32;

    // Sample blocks from the interior
    let margin = search_range + block_size;
    if margin >= width / 2 || margin >= height / 2 {
        return Ok(FrameMotion::identity());
    }

    for by in (margin..height - margin).step_by(block_size) {
        for bx in (margin..width - margin).step_by(block_size) {
            let (best_dx, best_dy) =
                match_block(prev, curr, width, height, bx, by, block_size, search_range);
            total_dx += best_dx as f64;
            total_dy += best_dy as f64;
            count += 1;
        }
    }

    if count == 0 {
        return Ok(FrameMotion::identity());
    }

    Ok(FrameMotion::new(
        total_dx / f64::from(count),
        total_dy / f64::from(count),
        0.0,
        1.0,
    ))
}

// -- Internal helpers ---------------------------------------------------------

fn match_block(
    prev: &[u8],
    curr: &[u8],
    width: usize,
    _height: usize,
    bx: usize,
    by: usize,
    block_size: usize,
    search_range: usize,
) -> (i32, i32) {
    let mut best_sad = u64::MAX;
    let mut best_dx = 0i32;
    let mut best_dy = 0i32;
    let sr = search_range as i32;

    for dy in -sr..=sr {
        for dx in -sr..=sr {
            let mut sad = 0u64;
            for row in 0..block_size {
                for col in 0..block_size {
                    let px = bx + col;
                    let py = by + row;
                    let cx = (px as i32 + dx) as usize;
                    let cy = (py as i32 + dy) as usize;

                    let prev_val = i32::from(prev[py * width + px]);
                    let curr_val = i32::from(curr[cy * width + cx]);
                    sad += (prev_val - curr_val).unsigned_abs() as u64;
                }
            }

            if sad < best_sad
                || (sad == best_sad
                    && (dx.unsigned_abs() + dy.unsigned_abs())
                        < (best_dx.unsigned_abs() + best_dy.unsigned_abs()))
            {
                best_sad = sad;
                best_dx = dx;
                best_dy = dy;
            }
        }
    }

    (best_dx, best_dy)
}

fn build_gaussian_kernel(radius: usize, sigma: f64) -> Vec<f64> {
    let size = 2 * radius + 1;
    let mut kernel = Vec::with_capacity(size);
    let sigma2 = sigma * sigma;

    for i in 0..size {
        let x = i as f64 - radius as f64;
        kernel.push((-0.5 * x * x / sigma2).exp());
    }

    kernel
}

fn invert_affine(mat: &AffineMatrix) -> AlignResult<AffineMatrix> {
    let a = f64::from(mat.data[0][0]);
    let b = f64::from(mat.data[0][1]);
    let tx = f64::from(mat.data[0][2]);
    let c = f64::from(mat.data[1][0]);
    let d = f64::from(mat.data[1][1]);
    let ty = f64::from(mat.data[1][2]);

    let det = a * d - b * c;
    if det.abs() < 1e-12 {
        return Err(AlignError::NumericalError(
            "Singular affine matrix cannot be inverted".to_string(),
        ));
    }

    let inv_det = 1.0 / det;
    let ia = (d * inv_det) as f32;
    let ib = (-b * inv_det) as f32;
    let ic = (-c * inv_det) as f32;
    let id = (a * inv_det) as f32;
    let itx = ((-d * tx + b * ty) * inv_det) as f32;
    let ity = ((c * tx - a * ty) * inv_det) as f32;

    Ok(AffineMatrix {
        data: [[ia, ib, itx], [ic, id, ity], [0.0, 0.0, 1.0]],
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    // -- FrameMotion ----------------------------------------------------------

    #[test]
    fn test_frame_motion_identity() {
        let m = FrameMotion::identity();
        assert_eq!(m.dx, 0.0);
        assert_eq!(m.dy, 0.0);
        assert_eq!(m.da, 0.0);
        assert_eq!(m.ds, 1.0);
    }

    #[test]
    fn test_frame_motion_to_affine_identity() {
        let m = FrameMotion::identity();
        let mat = m.to_affine();
        assert!(mat.is_identity());
    }

    #[test]
    fn test_frame_motion_to_affine_translation() {
        let m = FrameMotion::new(10.0, 20.0, 0.0, 1.0);
        let mat = m.to_affine();
        let (x, y) = mat.transform_point(0.0, 0.0);
        assert!((x - 10.0).abs() < 1e-4);
        assert!((y - 20.0).abs() < 1e-4);
    }

    #[test]
    fn test_frame_motion_to_affine_rotation() {
        let m = FrameMotion::new(0.0, 0.0, std::f64::consts::PI / 2.0, 1.0);
        let mat = m.to_affine();
        let (x, y) = mat.transform_point(1.0, 0.0);
        assert!((x - 0.0).abs() < 1e-4, "x={x}");
        assert!((y - 1.0).abs() < 1e-4, "y={y}");
    }

    // -- Trajectory -----------------------------------------------------------

    #[test]
    fn test_build_trajectory() {
        let motions = vec![
            FrameMotion::new(1.0, 0.0, 0.0, 1.0),
            FrameMotion::new(2.0, 0.0, 0.0, 1.0),
            FrameMotion::new(-1.0, 0.0, 0.0, 1.0),
        ];
        let traj = build_trajectory(&motions);
        assert_eq!(traj.len(), 3);
        assert!((traj[0].x - 1.0).abs() < 1e-10);
        assert!((traj[1].x - 3.0).abs() < 1e-10);
        assert!((traj[2].x - 2.0).abs() < 1e-10);
    }

    #[test]
    fn test_build_trajectory_empty() {
        let traj = build_trajectory(&[]);
        assert!(traj.is_empty());
    }

    // -- Smoothing ------------------------------------------------------------

    #[test]
    fn test_smooth_trajectory_constant() {
        let traj = vec![
            Trajectory {
                x: 5.0,
                y: 3.0,
                a: 0.0,
            },
            Trajectory {
                x: 5.0,
                y: 3.0,
                a: 0.0,
            },
            Trajectory {
                x: 5.0,
                y: 3.0,
                a: 0.0,
            },
            Trajectory {
                x: 5.0,
                y: 3.0,
                a: 0.0,
            },
            Trajectory {
                x: 5.0,
                y: 3.0,
                a: 0.0,
            },
        ];
        let smoothed = smooth_trajectory(&traj, 2, 1.0);
        for s in &smoothed {
            assert!((s.x - 5.0).abs() < 1e-6);
            assert!((s.y - 3.0).abs() < 1e-6);
        }
    }

    #[test]
    fn test_smooth_trajectory_reduces_spike() {
        let traj = vec![
            Trajectory {
                x: 0.0,
                y: 0.0,
                a: 0.0,
            },
            Trajectory {
                x: 0.0,
                y: 0.0,
                a: 0.0,
            },
            Trajectory {
                x: 100.0,
                y: 0.0,
                a: 0.0,
            }, // spike
            Trajectory {
                x: 0.0,
                y: 0.0,
                a: 0.0,
            },
            Trajectory {
                x: 0.0,
                y: 0.0,
                a: 0.0,
            },
        ];
        let smoothed = smooth_trajectory(&traj, 2, 1.0);
        // The spike at index 2 should be attenuated
        assert!(
            smoothed[2].x < 100.0,
            "spike should be smoothed: {}",
            smoothed[2].x
        );
        assert!(smoothed[2].x > 0.0, "spike should still have some value");
    }

    #[test]
    fn test_smooth_trajectory_empty() {
        let smoothed = smooth_trajectory(&[], 5, 1.0);
        assert!(smoothed.is_empty());
    }

    // -- Corrections ----------------------------------------------------------

    #[test]
    fn test_corrections_zero_when_no_smoothing() {
        let motions = vec![FrameMotion::new(1.0, 0.0, 0.0, 1.0); 5];
        let traj = build_trajectory(&motions);
        // If smoothed == original, corrections should be zero
        let corrections = compute_corrections(&motions, &traj, &traj, &StabilizeConfig::default());
        for c in &corrections {
            assert!((c.dx).abs() < 1e-10);
            assert!((c.dy).abs() < 1e-10);
        }
    }

    #[test]
    fn test_corrections_clamped() {
        let config = StabilizeConfig {
            max_correction_px: 10.0,
            ..StabilizeConfig::default()
        };

        let original = vec![Trajectory {
            x: 0.0,
            y: 0.0,
            a: 0.0,
        }];
        let smoothed = vec![Trajectory {
            x: 100.0,
            y: 0.0,
            a: 0.0,
        }];
        let motions = vec![FrameMotion::identity()];

        let corrections = compute_corrections(&motions, &original, &smoothed, &config);
        let mag =
            (corrections[0].dx * corrections[0].dx + corrections[0].dy * corrections[0].dy).sqrt();
        assert!(
            mag <= config.max_correction_px + 1e-6,
            "correction should be clamped: mag={mag}"
        );
    }

    // -- Pipeline -------------------------------------------------------------

    #[test]
    fn test_stabilize_pipeline() {
        let motions: Vec<FrameMotion> = (0..30)
            .map(|i| FrameMotion::new((i as f64 * 0.5).sin() * 5.0, 0.0, 0.0, 1.0))
            .collect();

        let config = StabilizeConfig {
            smooth_radius: 5,
            smooth_sigma: 2.0,
            max_correction_px: 50.0,
            ..StabilizeConfig::default()
        };

        let corrections = stabilize_pipeline(&motions, &config).expect("should succeed");
        assert_eq!(corrections.len(), motions.len());
    }

    #[test]
    fn test_stabilize_pipeline_empty() {
        let result = stabilize_pipeline(&[], &StabilizeConfig::default());
        assert!(result.is_err());
    }

    // -- Apply stabilization --------------------------------------------------

    #[test]
    fn test_apply_stabilization_identity() {
        let w = 32usize;
        let h = 32usize;
        let frame: Vec<u8> = (0..w * h).map(|i| (i % 256) as u8).collect();

        let correction = FrameMotion::identity();
        let config = StabilizeConfig {
            crop_ratio: 0.0,
            ..StabilizeConfig::default()
        };

        let output =
            apply_stabilization(&frame, w, h, &correction, &config).expect("should succeed");
        // With identity correction and no crop, output should match input
        assert_eq!(output.len(), frame.len());
        // Interior pixels should match
        for y in 1..h - 1 {
            for x in 1..w - 1 {
                assert_eq!(
                    output[y * w + x],
                    frame[y * w + x],
                    "mismatch at ({x}, {y})"
                );
            }
        }
    }

    #[test]
    fn test_apply_stabilization_size_mismatch() {
        let result = apply_stabilization(
            &[0u8; 100],
            20,
            20,
            &FrameMotion::identity(),
            &StabilizeConfig::default(),
        );
        assert!(result.is_err());
    }

    // -- Motion estimation ----------------------------------------------------

    #[test]
    fn test_estimate_motion_identical() {
        let w = 64usize;
        let h = 64usize;
        let frame = vec![128u8; w * h];

        let motion =
            estimate_motion_translation(&frame, &frame, w, h, 8, 4).expect("should succeed");
        assert!((motion.dx).abs() < 1.0);
        assert!((motion.dy).abs() < 1.0);
    }

    #[test]
    fn test_estimate_motion_shifted() {
        let w = 128usize;
        let h = 128usize;
        let mut prev = vec![64u8; w * h];
        let mut curr = vec![64u8; w * h];

        // Create a strong vertical stripe pattern
        for y in 0..h {
            for x in 0..w {
                prev[y * w + x] = if (x / 16) % 2 == 0 { 200 } else { 50 };
                // Shift right by 3
                let sx = if x >= 3 { x - 3 } else { 0 };
                curr[y * w + x] = if (sx / 16) % 2 == 0 { 200 } else { 50 };
            }
        }

        let motion =
            estimate_motion_translation(&prev, &curr, w, h, 16, 8).expect("should succeed");
        // Should detect roughly 3px rightward shift
        assert!(
            (motion.dx - 3.0).abs() < 3.0,
            "expected ~3px shift, got dx={:.2}",
            motion.dx
        );
    }

    #[test]
    fn test_estimate_motion_size_mismatch() {
        let result = estimate_motion_translation(&[0u8; 100], &[0u8; 200], 10, 10, 4, 2);
        assert!(result.is_err());
    }

    // -- Internal helpers -----------------------------------------------------

    #[test]
    fn test_gaussian_kernel_is_symmetric() {
        let kernel = build_gaussian_kernel(5, 2.0);
        assert_eq!(kernel.len(), 11);
        for i in 0..5 {
            assert!((kernel[i] - kernel[10 - i]).abs() < 1e-10);
        }
    }

    #[test]
    fn test_gaussian_kernel_peak_at_center() {
        let kernel = build_gaussian_kernel(3, 1.0);
        let center = kernel[3];
        for (i, &v) in kernel.iter().enumerate() {
            if i != 3 {
                assert!(v <= center + 1e-10);
            }
        }
    }

    #[test]
    fn test_invert_affine_identity() {
        let id = AffineMatrix::identity();
        let inv = invert_affine(&id).expect("should succeed");
        assert!(inv.is_identity());
    }

    #[test]
    fn test_invert_affine_translation() {
        let t = AffineMatrix::translation(5.0, -3.0);
        let inv = invert_affine(&t).expect("should succeed");
        let (x, y) = inv.transform_point(5.0, -3.0);
        assert!((x - 0.0).abs() < 1e-4);
        assert!((y - 0.0).abs() < 1e-4);
    }

    #[test]
    fn test_invert_affine_singular() {
        let singular = AffineMatrix {
            data: [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 1.0]],
        };
        assert!(invert_affine(&singular).is_err());
    }

    #[test]
    fn test_stabilize_config_default() {
        let config = StabilizeConfig::default();
        assert_eq!(config.smooth_radius, 15);
        assert_eq!(config.mode, StabilizationMode::TranslationRotation);
    }
}