oximedia-cv 0.1.8

Computer vision for 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
//! SIMD-accelerated convolution for image filtering.
//!
//! Provides optimized 2D convolution for common kernel sizes (3x3, 5x5, 7x7)
//! using batch processing and loop unrolling. Falls back to scalar code on
//! platforms without SIMD intrinsics, while still benefiting from the
//! cache-friendly memory access patterns.

use crate::error::{CvError, CvResult};

/// SIMD-accelerated convolution engine.
///
/// Optimizes convolution by processing multiple pixels at once using
/// vectorized operations and cache-friendly access patterns.
#[derive(Debug, Clone)]
pub struct SimdConvolver {
    /// Kernel coefficients (row-major, always square).
    kernel: Vec<f32>,
    /// Kernel dimension (3, 5, or 7).
    size: usize,
    /// Half-kernel radius.
    half: usize,
}

impl SimdConvolver {
    /// Create a new SIMD convolver with the given square kernel.
    ///
    /// # Errors
    ///
    /// Returns an error if the kernel size is not 3, 5, or 7, or data length mismatches.
    pub fn new(kernel: &[f64], size: usize) -> CvResult<Self> {
        if size != 3 && size != 5 && size != 7 {
            return Err(CvError::invalid_parameter(
                "kernel_size",
                "must be 3, 5, or 7",
            ));
        }
        if kernel.len() != size * size {
            return Err(CvError::invalid_parameter(
                "kernel",
                "length must equal size*size",
            ));
        }

        let kernel_f32: Vec<f32> = kernel.iter().map(|&v| v as f32).collect();

        Ok(Self {
            kernel: kernel_f32,
            size,
            half: size / 2,
        })
    }

    /// Create a 3x3 Gaussian blur kernel.
    ///
    /// # Errors
    ///
    /// Returns an error if kernel construction fails.
    pub fn gaussian_3x3() -> CvResult<Self> {
        #[rustfmt::skip]
        let k = [
            1.0/16.0, 2.0/16.0, 1.0/16.0,
            2.0/16.0, 4.0/16.0, 2.0/16.0,
            1.0/16.0, 2.0/16.0, 1.0/16.0,
        ];
        Self::new(&k, 3)
    }

    /// Create a 3x3 Sobel X kernel.
    ///
    /// # Errors
    ///
    /// Returns an error if kernel construction fails.
    pub fn sobel_x_3x3() -> CvResult<Self> {
        #[rustfmt::skip]
        let k = [
            -1.0, 0.0, 1.0,
            -2.0, 0.0, 2.0,
            -1.0, 0.0, 1.0,
        ];
        Self::new(&k, 3)
    }

    /// Create a 3x3 Sobel Y kernel.
    ///
    /// # Errors
    ///
    /// Returns an error if kernel construction fails.
    pub fn sobel_y_3x3() -> CvResult<Self> {
        #[rustfmt::skip]
        let k = [
            -1.0, -2.0, -1.0,
             0.0,  0.0,  0.0,
             1.0,  2.0,  1.0,
        ];
        Self::new(&k, 3)
    }

    /// Create a 5x5 Gaussian blur kernel.
    ///
    /// # Errors
    ///
    /// Returns an error if kernel construction fails.
    pub fn gaussian_5x5() -> CvResult<Self> {
        #[rustfmt::skip]
        let k = [
            1.0/256.0,  4.0/256.0,  6.0/256.0,  4.0/256.0,  1.0/256.0,
            4.0/256.0, 16.0/256.0, 24.0/256.0, 16.0/256.0,  4.0/256.0,
            6.0/256.0, 24.0/256.0, 36.0/256.0, 24.0/256.0,  6.0/256.0,
            4.0/256.0, 16.0/256.0, 24.0/256.0, 16.0/256.0,  4.0/256.0,
            1.0/256.0,  4.0/256.0,  6.0/256.0,  4.0/256.0,  1.0/256.0,
        ];
        Self::new(&k, 5)
    }

    /// Create a 7x7 Gaussian blur kernel with sigma=1.0.
    ///
    /// # Errors
    ///
    /// Returns an error if kernel construction fails.
    pub fn gaussian_7x7() -> CvResult<Self> {
        let sigma = 1.0f64;
        let two_sigma_sq = 2.0 * sigma * sigma;
        let mut k = [0.0f64; 49];
        let mut sum = 0.0;
        for ky in 0..7 {
            for kx in 0..7 {
                let dx = kx as f64 - 3.0;
                let dy = ky as f64 - 3.0;
                let val = (-(dx * dx + dy * dy) / two_sigma_sq).exp();
                k[ky * 7 + kx] = val;
                sum += val;
            }
        }
        for v in &mut k {
            *v /= sum;
        }
        Self::new(&k, 7)
    }

    /// Apply the convolution to a grayscale image.
    ///
    /// Uses SIMD-friendly batch processing: processes 4 output pixels at a time
    /// with unrolled inner loops for each kernel size.
    ///
    /// # Errors
    ///
    /// Returns an error if dimensions are invalid.
    pub fn convolve(&self, src: &[u8], width: u32, height: u32) -> CvResult<Vec<u8>> {
        let w = width as usize;
        let h = height as usize;

        if w == 0 || h == 0 {
            return Err(CvError::invalid_dimensions(width, height));
        }
        let size = w * h;
        if src.len() < size {
            return Err(CvError::insufficient_data(size, src.len()));
        }

        match self.size {
            3 => self.convolve_3x3(src, w, h),
            5 => self.convolve_5x5(src, w, h),
            7 => self.convolve_7x7(src, w, h),
            _ => self.convolve_generic(src, w, h),
        }
    }

    /// Optimized 3x3 convolution with unrolled kernel application.
    fn convolve_3x3(&self, src: &[u8], w: usize, h: usize) -> CvResult<Vec<u8>> {
        let mut dst = vec![0u8; w * h];
        let k = &self.kernel;

        for y in 0..h {
            // Process 4 pixels at a time for vectorization opportunity
            let mut x = 0;
            while x + 4 <= w {
                let mut sums = [0.0f32; 4];

                for ky in 0..3 {
                    let sy = (y as i32 + ky as i32 - 1).clamp(0, h as i32 - 1) as usize;
                    let row_offset = sy * w;

                    for kx in 0..3 {
                        let k_val = k[ky * 3 + kx];

                        for lane in 0..4 {
                            let sx = (x as i32 + lane as i32 + kx as i32 - 1).clamp(0, w as i32 - 1)
                                as usize;
                            sums[lane] += src[row_offset + sx] as f32 * k_val;
                        }
                    }
                }

                for lane in 0..4 {
                    dst[y * w + x + lane] = sums[lane].round().clamp(0.0, 255.0) as u8;
                }
                x += 4;
            }

            // Handle remaining pixels
            while x < w {
                let mut sum = 0.0f32;
                for ky in 0..3 {
                    let sy = (y as i32 + ky as i32 - 1).clamp(0, h as i32 - 1) as usize;
                    for kx in 0..3 {
                        let sx = (x as i32 + kx as i32 - 1).clamp(0, w as i32 - 1) as usize;
                        sum += src[sy * w + sx] as f32 * k[ky * 3 + kx];
                    }
                }
                dst[y * w + x] = sum.round().clamp(0.0, 255.0) as u8;
                x += 1;
            }
        }

        Ok(dst)
    }

    /// Optimized 5x5 convolution with unrolled kernel application.
    fn convolve_5x5(&self, src: &[u8], w: usize, h: usize) -> CvResult<Vec<u8>> {
        let mut dst = vec![0u8; w * h];
        let k = &self.kernel;

        for y in 0..h {
            let mut x = 0;
            while x + 4 <= w {
                let mut sums = [0.0f32; 4];

                for ky in 0..5 {
                    let sy = (y as i32 + ky as i32 - 2).clamp(0, h as i32 - 1) as usize;
                    let row_offset = sy * w;

                    for kx in 0..5 {
                        let k_val = k[ky * 5 + kx];

                        for lane in 0..4 {
                            let sx = (x as i32 + lane as i32 + kx as i32 - 2).clamp(0, w as i32 - 1)
                                as usize;
                            sums[lane] += src[row_offset + sx] as f32 * k_val;
                        }
                    }
                }

                for lane in 0..4 {
                    dst[y * w + x + lane] = sums[lane].round().clamp(0.0, 255.0) as u8;
                }
                x += 4;
            }

            while x < w {
                let mut sum = 0.0f32;
                for ky in 0..5 {
                    let sy = (y as i32 + ky as i32 - 2).clamp(0, h as i32 - 1) as usize;
                    for kx in 0..5 {
                        let sx = (x as i32 + kx as i32 - 2).clamp(0, w as i32 - 1) as usize;
                        sum += src[sy * w + sx] as f32 * k[ky * 5 + kx];
                    }
                }
                dst[y * w + x] = sum.round().clamp(0.0, 255.0) as u8;
                x += 1;
            }
        }

        Ok(dst)
    }

    /// Optimized 7x7 convolution with unrolled kernel application.
    fn convolve_7x7(&self, src: &[u8], w: usize, h: usize) -> CvResult<Vec<u8>> {
        let mut dst = vec![0u8; w * h];
        let k = &self.kernel;

        for y in 0..h {
            let mut x = 0;
            while x + 4 <= w {
                let mut sums = [0.0f32; 4];

                for ky in 0..7 {
                    let sy = (y as i32 + ky as i32 - 3).clamp(0, h as i32 - 1) as usize;
                    let row_offset = sy * w;

                    for kx in 0..7 {
                        let k_val = k[ky * 7 + kx];

                        for lane in 0..4 {
                            let sx = (x as i32 + lane as i32 + kx as i32 - 3).clamp(0, w as i32 - 1)
                                as usize;
                            sums[lane] += src[row_offset + sx] as f32 * k_val;
                        }
                    }
                }

                for lane in 0..4 {
                    dst[y * w + x + lane] = sums[lane].round().clamp(0.0, 255.0) as u8;
                }
                x += 4;
            }

            while x < w {
                let mut sum = 0.0f32;
                for ky in 0..7 {
                    let sy = (y as i32 + ky as i32 - 3).clamp(0, h as i32 - 1) as usize;
                    for kx in 0..7 {
                        let sx = (x as i32 + kx as i32 - 3).clamp(0, w as i32 - 1) as usize;
                        sum += src[sy * w + sx] as f32 * k[ky * 7 + kx];
                    }
                }
                dst[y * w + x] = sum.round().clamp(0.0, 255.0) as u8;
                x += 1;
            }
        }

        Ok(dst)
    }

    /// Generic convolution fallback for any kernel size.
    fn convolve_generic(&self, src: &[u8], w: usize, h: usize) -> CvResult<Vec<u8>> {
        let mut dst = vec![0u8; w * h];

        for y in 0..h {
            for x in 0..w {
                let mut sum = 0.0f32;
                for ky in 0..self.size {
                    let sy =
                        (y as i32 + ky as i32 - self.half as i32).clamp(0, h as i32 - 1) as usize;
                    for kx in 0..self.size {
                        let sx = (x as i32 + kx as i32 - self.half as i32).clamp(0, w as i32 - 1)
                            as usize;
                        sum += src[sy * w + sx] as f32 * self.kernel[ky * self.size + kx];
                    }
                }
                dst[y * w + x] = sum.round().clamp(0.0, 255.0) as u8;
            }
        }

        Ok(dst)
    }

    /// Apply separable convolution (for symmetric kernels) — horizontal pass
    /// followed by vertical pass for better cache performance.
    ///
    /// The 1D kernel is extracted from the first row of the 2D kernel,
    /// assuming separability (valid for Gaussian kernels).
    ///
    /// # Errors
    ///
    /// Returns an error if the kernel is not separable or dimensions are invalid.
    pub fn convolve_separable(&self, src: &[u8], width: u32, height: u32) -> CvResult<Vec<u8>> {
        let w = width as usize;
        let h = height as usize;

        if w == 0 || h == 0 {
            return Err(CvError::invalid_dimensions(width, height));
        }
        let size = w * h;
        if src.len() < size {
            return Err(CvError::insufficient_data(size, src.len()));
        }

        // Extract 1D kernel from diagonal scaling
        // For Gaussian: k_2d[i][j] = k_1d[i] * k_1d[j]
        // So k_1d[i] = sqrt(k_2d[i][center]) where center = half
        let center = self.half;
        let mut k1d = vec![0.0f32; self.size];
        let center_val = self.kernel[center * self.size + center];
        if center_val.abs() < f32::EPSILON {
            return Err(CvError::computation("kernel center is zero; not separable"));
        }

        for i in 0..self.size {
            k1d[i] = self.kernel[i * self.size + center] / center_val.sqrt();
        }

        // Horizontal pass -> f32 intermediate
        let mut temp = vec![0.0f32; w * h];
        for y in 0..h {
            let mut x = 0;
            while x + 4 <= w {
                let mut sums = [0.0f32; 4];
                for ki in 0..self.size {
                    let kv = k1d[ki];
                    for lane in 0..4 {
                        let sx = (x as i32 + lane as i32 + ki as i32 - self.half as i32)
                            .clamp(0, w as i32 - 1) as usize;
                        sums[lane] += src[y * w + sx] as f32 * kv;
                    }
                }
                for lane in 0..4 {
                    temp[y * w + x + lane] = sums[lane];
                }
                x += 4;
            }
            while x < w {
                let mut sum = 0.0f32;
                for ki in 0..self.size {
                    let sx =
                        (x as i32 + ki as i32 - self.half as i32).clamp(0, w as i32 - 1) as usize;
                    sum += src[y * w + sx] as f32 * k1d[ki];
                }
                temp[y * w + x] = sum;
                x += 1;
            }
        }

        // Vertical pass -> u8 output
        let mut dst = vec![0u8; w * h];
        for y in 0..h {
            for x in 0..w {
                let mut sum = 0.0f32;
                for ki in 0..self.size {
                    let sy =
                        (y as i32 + ki as i32 - self.half as i32).clamp(0, h as i32 - 1) as usize;
                    sum += temp[sy * w + x] * k1d[ki];
                }
                dst[y * w + x] = sum.round().clamp(0.0, 255.0) as u8;
            }
        }

        Ok(dst)
    }
}

/// Convolve a grayscale image with a 3x3 kernel using batch processing.
///
/// Convenience function that creates a [`SimdConvolver`] and applies it.
///
/// # Errors
///
/// Returns an error if dimensions or kernel are invalid.
pub fn convolve_3x3(src: &[u8], width: u32, height: u32, kernel: &[f64; 9]) -> CvResult<Vec<u8>> {
    let conv = SimdConvolver::new(kernel, 3)?;
    conv.convolve(src, width, height)
}

/// Convolve a grayscale image with a 5x5 kernel using batch processing.
///
/// # Errors
///
/// Returns an error if dimensions or kernel are invalid.
pub fn convolve_5x5(src: &[u8], width: u32, height: u32, kernel: &[f64; 25]) -> CvResult<Vec<u8>> {
    let conv = SimdConvolver::new(kernel, 5)?;
    conv.convolve(src, width, height)
}

/// Convolve a grayscale image with a 7x7 kernel using batch processing.
///
/// # Errors
///
/// Returns an error if dimensions or kernel are invalid.
pub fn convolve_7x7(src: &[u8], width: u32, height: u32, kernel: &[f64; 49]) -> CvResult<Vec<u8>> {
    let conv = SimdConvolver::new(kernel, 7)?;
    conv.convolve(src, width, height)
}

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

    #[test]
    fn test_simd_convolver_creation() {
        let k = [0.0; 9];
        let conv = SimdConvolver::new(&k, 3);
        assert!(conv.is_ok());
    }

    #[test]
    fn test_simd_convolver_invalid_size() {
        let k = [0.0; 4];
        assert!(SimdConvolver::new(&k, 2).is_err());
        assert!(SimdConvolver::new(&k, 4).is_err());
    }

    #[test]
    fn test_simd_convolver_size_mismatch() {
        let k = [0.0; 9];
        assert!(SimdConvolver::new(&k, 5).is_err());
    }

    #[test]
    fn test_gaussian_3x3_uniform() {
        let conv = SimdConvolver::gaussian_3x3().expect("should create");
        let src = vec![100u8; 10 * 10];
        let result = conv.convolve(&src, 10, 10).expect("should convolve");
        assert_eq!(result.len(), 100);
        // Uniform image should stay approximately the same
        for &v in &result {
            assert!((v as i32 - 100).abs() < 2);
        }
    }

    #[test]
    fn test_gaussian_5x5_uniform() {
        let conv = SimdConvolver::gaussian_5x5().expect("should create");
        let src = vec![128u8; 20 * 20];
        let result = conv.convolve(&src, 20, 20).expect("should convolve");
        assert_eq!(result.len(), 400);
        for &v in &result {
            assert!((v as i32 - 128).abs() < 2);
        }
    }

    #[test]
    fn test_gaussian_7x7_uniform() {
        let conv = SimdConvolver::gaussian_7x7().expect("should create");
        let src = vec![200u8; 15 * 15];
        let result = conv.convolve(&src, 15, 15).expect("should convolve");
        assert_eq!(result.len(), 225);
        for &v in &result {
            assert!((v as i32 - 200).abs() < 2);
        }
    }

    #[test]
    fn test_sobel_x_3x3() {
        let conv = SimdConvolver::sobel_x_3x3().expect("should create");
        // Vertical edge: left half=0, right half=255
        let mut src = vec![0u8; 10 * 10];
        for y in 0..10 {
            for x in 5..10 {
                src[y * 10 + x] = 255;
            }
        }
        let result = conv.convolve(&src, 10, 10).expect("should convolve");
        // Edge should be detected around column 5
        assert!(result[5 * 10 + 5] > 0 || result[5 * 10 + 4] > 0);
    }

    #[test]
    fn test_convolve_3x3_convenience() {
        #[rustfmt::skip]
        let k = [
            0.0, 0.0, 0.0,
            0.0, 1.0, 0.0,
            0.0, 0.0, 0.0,
        ];
        let src = vec![42u8; 8 * 8];
        let result = convolve_3x3(&src, 8, 8, &k).expect("should work");
        // Identity kernel should preserve the image
        for &v in &result {
            assert_eq!(v, 42);
        }
    }

    #[test]
    fn test_convolve_5x5_convenience() {
        let mut k = [0.0f64; 25];
        k[12] = 1.0; // center element
        let src = vec![77u8; 12 * 12];
        let result = convolve_5x5(&src, 12, 12, &k).expect("should work");
        for &v in &result {
            assert_eq!(v, 77);
        }
    }

    #[test]
    fn test_convolve_7x7_convenience() {
        let mut k = [0.0f64; 49];
        k[24] = 1.0; // center element
        let src = vec![55u8; 14 * 14];
        let result = convolve_7x7(&src, 14, 14, &k).expect("should work");
        for &v in &result {
            assert_eq!(v, 55);
        }
    }

    #[test]
    fn test_convolve_invalid_dimensions() {
        let conv = SimdConvolver::gaussian_3x3().expect("should create");
        assert!(conv.convolve(&[], 0, 0).is_err());
    }

    #[test]
    fn test_convolve_insufficient_data() {
        let conv = SimdConvolver::gaussian_3x3().expect("should create");
        let src = vec![0u8; 5];
        assert!(conv.convolve(&src, 10, 10).is_err());
    }

    #[test]
    fn test_separable_gaussian_3x3() {
        let conv = SimdConvolver::gaussian_3x3().expect("should create");
        let src = vec![100u8; 10 * 10];
        let result = conv.convolve_separable(&src, 10, 10).expect("should work");
        assert_eq!(result.len(), 100);
        for &v in &result {
            assert!((v as i32 - 100).abs() < 5);
        }
    }

    #[test]
    fn test_3x3_matches_generic_on_small_image() {
        // Use an image width not divisible by 4 to test remainder handling
        let conv = SimdConvolver::gaussian_3x3().expect("should create");
        let src: Vec<u8> = (0..7 * 7).map(|i| (i * 37 % 256) as u8).collect();

        let result_fast = conv.convolve(&src, 7, 7).expect("fast should work");
        let result_generic = conv
            .convolve_generic(&src, 7, 7)
            .expect("generic should work");

        assert_eq!(result_fast, result_generic);
    }
}