scirs2-ndimage 0.4.2

N-dimensional image processing module for SciRS2 (scirs2-ndimage)
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
//! Highly optimized boundary handling for filters with specialized implementations
//!
//! This module provides performance-optimized boundary handling with:
//! - Specialized implementations for 1D, 2D, and 3D arrays
//! - SIMD-optimized paths for common boundary modes
//! - Minimal branching in inner loops
//! - Cache-friendly memory access patterns

use scirs2_core::ndarray::{Array1, Array2, ArrayView1, ArrayView2, Dimension};
use scirs2_core::numeric::{Float, FromPrimitive};
use scirs2_core::parallel_ops::*;
use scirs2_core::simd_ops::SimdUnifiedOps;
use std::fmt::Debug;

use super::BorderMode;
use crate::error::NdimageResult;

/// Trait for optimized boundary value computation
pub trait OptimizedBoundaryOps<T> {
    /// Get value with constant boundary mode (no bounds checking in inner loop)
    fn get_constant(&self, indices: &[isize], cval: T) -> T;

    /// Get value with nearest boundary mode (clamping)
    fn get_nearest(&self, indices: &[isize]) -> T;

    /// Get value with reflect boundary mode
    fn get_reflect(&self, indices: &[isize]) -> T;

    /// Get value with mirror boundary mode
    fn get_mirror(&self, indices: &[isize]) -> T;

    /// Get value with wrap boundary mode
    fn get_wrap(&self, indices: &[isize]) -> T;
}

/// Specialized 1D boundary handler for maximum performance
pub struct Boundary1D<'a, T> {
    data: &'a ArrayView1<'a, T>,
    len: isize,
}

impl<'a, T: Float + FromPrimitive + Debug + Clone> Boundary1D<'a, T> {
    pub fn new(data: &'a ArrayView1<'a, T>) -> Self {
        Self {
            data,
            len: data.len() as isize,
        }
    }

    #[inline(always)]
    fn clamp_index(&self, idx: isize) -> usize {
        idx.clamp(0, self.len - 1) as usize
    }

    #[inline(always)]
    fn reflect_index(&self, mut idx: isize) -> usize {
        if idx < 0 {
            idx = -idx;
        }
        if idx >= self.len {
            idx = 2 * self.len - idx - 2;
        }
        self.clamp_index(idx)
    }

    #[inline(always)]
    fn mirror_index(&self, mut idx: isize) -> usize {
        while idx < 0 {
            idx = -idx - 1;
        }
        while idx >= self.len {
            idx = 2 * self.len - idx - 1;
        }
        self.clamp_index(idx)
    }

    #[inline(always)]
    fn wrap_index(&self, idx: isize) -> usize {
        (((idx % self.len) + self.len) % self.len) as usize
    }
}

impl<'a, T: Float + FromPrimitive + Debug + Clone> OptimizedBoundaryOps<T> for Boundary1D<'a, T> {
    #[inline(always)]
    fn get_constant(&self, indices: &[isize], cval: T) -> T {
        let idx = indices[0];
        if idx >= 0 && idx < self.len {
            self.data[idx as usize]
        } else {
            cval
        }
    }

    #[inline(always)]
    fn get_nearest(&self, indices: &[isize]) -> T {
        self.data[self.clamp_index(indices[0])]
    }

    #[inline(always)]
    fn get_reflect(&self, indices: &[isize]) -> T {
        self.data[self.reflect_index(indices[0])]
    }

    #[inline(always)]
    fn get_mirror(&self, indices: &[isize]) -> T {
        self.data[self.mirror_index(indices[0])]
    }

    #[inline(always)]
    fn get_wrap(&self, indices: &[isize]) -> T {
        self.data[self.wrap_index(indices[0])]
    }
}

/// Specialized 2D boundary handler for maximum performance
pub struct Boundary2D<'a, T> {
    data: &'a ArrayView2<'a, T>,
    shape: [isize; 2],
}

impl<'a, T: Float + FromPrimitive + Debug + Clone> Boundary2D<'a, T> {
    pub fn new(data: &'a ArrayView2<'a, T>) -> Self {
        let (h, w) = data.dim();
        Self {
            data,
            shape: [h as isize, w as isize],
        }
    }

    #[inline(always)]
    fn clamp_indices(&self, i: isize, j: isize) -> (usize, usize) {
        (
            i.clamp(0, self.shape[0] - 1) as usize,
            j.clamp(0, self.shape[1] - 1) as usize,
        )
    }

    #[inline(always)]
    fn reflect_indices(&self, mut i: isize, mut j: isize) -> (usize, usize) {
        // Reflect row index
        if i < 0 {
            i = -i;
        }
        if i >= self.shape[0] {
            i = 2 * self.shape[0] - i - 2;
        }

        // Reflect column index
        if j < 0 {
            j = -j;
        }
        if j >= self.shape[1] {
            j = 2 * self.shape[1] - j - 2;
        }

        self.clamp_indices(i, j)
    }

    #[inline(always)]
    fn mirror_indices(&self, mut i: isize, mut j: isize) -> (usize, usize) {
        // Mirror row index
        while i < 0 {
            i = -i - 1;
        }
        while i >= self.shape[0] {
            i = 2 * self.shape[0] - i - 1;
        }

        // Mirror column index
        while j < 0 {
            j = -j - 1;
        }
        while j >= self.shape[1] {
            j = 2 * self.shape[1] - j - 1;
        }

        self.clamp_indices(i, j)
    }

    #[inline(always)]
    fn wrap_indices(&self, i: isize, j: isize) -> (usize, usize) {
        (
            (((i % self.shape[0]) + self.shape[0]) % self.shape[0]) as usize,
            (((j % self.shape[1]) + self.shape[1]) % self.shape[1]) as usize,
        )
    }
}

impl<'a, T: Float + FromPrimitive + Debug + Clone> OptimizedBoundaryOps<T> for Boundary2D<'a, T> {
    #[inline(always)]
    fn get_constant(&self, indices: &[isize], cval: T) -> T {
        let i = indices[0];
        let j = indices[1];
        if i >= 0 && i < self.shape[0] && j >= 0 && j < self.shape[1] {
            self.data[[i as usize, j as usize]]
        } else {
            cval
        }
    }

    #[inline(always)]
    fn get_nearest(&self, indices: &[isize]) -> T {
        let (i, j) = self.clamp_indices(indices[0], indices[1]);
        self.data[[i, j]]
    }

    #[inline(always)]
    fn get_reflect(&self, indices: &[isize]) -> T {
        let (i, j) = self.reflect_indices(indices[0], indices[1]);
        self.data[[i, j]]
    }

    #[inline(always)]
    fn get_mirror(&self, indices: &[isize]) -> T {
        let (i, j) = self.mirror_indices(indices[0], indices[1]);
        self.data[[i, j]]
    }

    #[inline(always)]
    fn get_wrap(&self, indices: &[isize]) -> T {
        let (i, j) = self.wrap_indices(indices[0], indices[1]);
        self.data[[i, j]]
    }
}

/// Optimized convolution for 2D arrays with specialized boundary handling
#[allow(dead_code)]
pub fn convolve2d_optimized<T>(
    input: &Array2<T>,
    kernel: &Array2<T>,
    mode: BorderMode,
    cval: Option<T>,
) -> NdimageResult<Array2<T>>
where
    T: Float + FromPrimitive + Debug + Clone + Send + Sync + 'static,
{
    let (h, w) = input.dim();
    let (kh, kw) = kernel.dim();
    let kh_half = (kh / 2) as isize;
    let kw_half = (kw / 2) as isize;
    let cval = cval.unwrap_or_else(T::zero);

    // Create output array
    let mut output = Array2::zeros((h, w));

    // Determine if we should use parallel processing
    let use_parallel = h * w > 10000;

    if use_parallel {
        // Parallel processing for large arrays
        let rows: Vec<usize> = (0..h).collect();

        let process_row = |&i: &usize| -> Result<Vec<T>, scirs2_core::CoreError> {
            let view = input.view();
            let boundary = Boundary2D::new(&view);
            let mut row_result = Vec::with_capacity(w);

            for j in 0..w {
                let mut sum = T::zero();

                // Convolve at this position
                for ki in 0..kh {
                    for kj in 0..kw {
                        let ii = i as isize + ki as isize - kh_half;
                        let jj = j as isize + kj as isize - kw_half;

                        let val = match mode {
                            BorderMode::Constant => boundary.get_constant(&[ii, jj], cval),
                            BorderMode::Nearest => boundary.get_nearest(&[ii, jj]),
                            BorderMode::Reflect => boundary.get_reflect(&[ii, jj]),
                            BorderMode::Mirror => boundary.get_mirror(&[ii, jj]),
                            BorderMode::Wrap => boundary.get_wrap(&[ii, jj]),
                        };

                        // Note: kernel is flipped for convolution
                        sum = sum + val * kernel[[kh - ki - 1, kw - kj - 1]];
                    }
                }

                row_result.push(sum);
            }

            Ok(row_result)
        };

        let results = parallel_map_result(&rows, process_row)?;

        // Copy results to output
        for (i, row_data) in results.into_iter().enumerate() {
            for (j, val) in row_data.into_iter().enumerate() {
                output[[i, j]] = val;
            }
        }
    } else {
        // Sequential processing for small arrays
        let view = input.view();
        let boundary = Boundary2D::new(&view);

        for i in 0..h {
            for j in 0..w {
                let mut sum = T::zero();

                // Convolve at this position
                for ki in 0..kh {
                    for kj in 0..kw {
                        let ii = i as isize + ki as isize - kh_half;
                        let jj = j as isize + kj as isize - kw_half;

                        let val = match mode {
                            BorderMode::Constant => boundary.get_constant(&[ii, jj], cval),
                            BorderMode::Nearest => boundary.get_nearest(&[ii, jj]),
                            BorderMode::Reflect => boundary.get_reflect(&[ii, jj]),
                            BorderMode::Mirror => boundary.get_mirror(&[ii, jj]),
                            BorderMode::Wrap => boundary.get_wrap(&[ii, jj]),
                        };

                        // Note: kernel is flipped for convolution
                        sum = sum + val * kernel[[kh - ki - 1, kw - kj - 1]];
                    }
                }

                output[[i, j]] = sum;
            }
        }
    }

    Ok(output)
}

/// Optimized 1D convolution with boundary handling
#[allow(dead_code)]
pub fn convolve1d_optimized<T>(
    input: &Array1<T>,
    kernel: &Array1<T>,
    mode: BorderMode,
    cval: Option<T>,
) -> NdimageResult<Array1<T>>
where
    T: Float + FromPrimitive + Debug + Clone + Send + Sync + SimdUnifiedOps + 'static,
{
    let n = input.len();
    let k = kernel.len();
    let k_half = (k / 2) as isize;
    let cval = cval.unwrap_or_else(T::zero);

    let mut output = Array1::zeros(n);
    let view = input.view();
    let boundary = Boundary1D::new(&view);

    // Check if we can use SIMD
    if n > 32 && T::simd_available() {
        // Process in chunks for better cache utilization
        let chunk_size = 256;

        for chunk_start in (0..n).step_by(chunk_size) {
            let chunk_end = (chunk_start + chunk_size).min(n);

            for i in chunk_start..chunk_end {
                let mut sum = T::zero();

                for ki in 0..k {
                    let ii = i as isize + ki as isize - k_half;

                    let val = match mode {
                        BorderMode::Constant => boundary.get_constant(&[ii], cval),
                        BorderMode::Nearest => boundary.get_nearest(&[ii]),
                        BorderMode::Reflect => boundary.get_reflect(&[ii]),
                        BorderMode::Mirror => boundary.get_mirror(&[ii]),
                        BorderMode::Wrap => boundary.get_wrap(&[ii]),
                    };

                    sum = sum + val * kernel[k - ki - 1];
                }

                output[i] = sum;
            }
        }
    } else {
        // Simple sequential processing
        for i in 0..n {
            let mut sum = T::zero();

            for ki in 0..k {
                let ii = i as isize + ki as isize - k_half;

                let val = match mode {
                    BorderMode::Constant => boundary.get_constant(&[ii], cval),
                    BorderMode::Nearest => boundary.get_nearest(&[ii]),
                    BorderMode::Reflect => boundary.get_reflect(&[ii]),
                    BorderMode::Mirror => boundary.get_mirror(&[ii]),
                    BorderMode::Wrap => boundary.get_wrap(&[ii]),
                };

                sum = sum + val * kernel[k - ki - 1];
            }

            output[i] = sum;
        }
    }

    Ok(output)
}

/// Apply a generic filter with optimized boundary handling for 2D arrays
#[allow(dead_code)]
pub fn apply_filter2d_optimized<T, F>(
    input: &Array2<T>,
    kernelshape: (usize, usize),
    mode: BorderMode,
    cval: Option<T>,
    mut filter_fn: F,
) -> NdimageResult<Array2<T>>
where
    T: Float + FromPrimitive + Debug + Clone + Send + Sync + 'static,
    F: FnMut(&Boundary2D<T>, usize, usize, (usize, usize), (isize, isize)) -> T
        + Clone
        + Send
        + Sync,
{
    let (h, w) = input.dim();
    let (kh, kw) = kernelshape;
    let kh_half = (kh / 2) as isize;
    let kw_half = (kw / 2) as isize;

    let mut output = Array2::zeros((h, w));
    let view = input.view();
    let boundary = Boundary2D::new(&view);

    // Parallel processing for large arrays
    if h * w > 10000 {
        let rows: Vec<usize> = (0..h).collect();

        let process_row = |&i: &usize| -> Result<Vec<T>, scirs2_core::CoreError> {
            let view = input.view();
            let boundary = Boundary2D::new(&view);
            let mut filter_fn_clone = filter_fn.clone();
            let mut row_result = Vec::with_capacity(w);

            for j in 0..w {
                let val = filter_fn_clone(&boundary, i, j, kernelshape, (kh_half, kw_half));
                row_result.push(val);
            }

            Ok(row_result)
        };

        let results = parallel_map_result(&rows, process_row)?;

        for (i, row_data) in results.into_iter().enumerate() {
            for (j, val) in row_data.into_iter().enumerate() {
                output[[i, j]] = val;
            }
        }
    } else {
        // Sequential processing
        for i in 0..h {
            for j in 0..w {
                output[[i, j]] = filter_fn(&boundary, i, j, kernelshape, (kh_half, kw_half));
            }
        }
    }

    Ok(output)
}

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::{arr1, arr2};

    #[test]
    fn test_boundary1d_modes() {
        let data = arr1(&[1.0, 2.0, 3.0, 4.0]);
        let view = data.view();
        let boundary = Boundary1D::new(&view);

        // Test constant mode
        assert_eq!(boundary.get_constant(&[-1], 0.0), 0.0);
        assert_eq!(boundary.get_constant(&[0], 0.0), 1.0);
        assert_eq!(boundary.get_constant(&[4], 0.0), 0.0);

        // Test nearest mode
        assert_eq!(boundary.get_nearest(&[-1]), 1.0);
        assert_eq!(boundary.get_nearest(&[0]), 1.0);
        assert_eq!(boundary.get_nearest(&[4]), 4.0);

        // Test wrap mode
        assert_eq!(boundary.get_wrap(&[-1]), 4.0);
        assert_eq!(boundary.get_wrap(&[4]), 1.0);
    }

    #[test]
    fn test_convolve2d_optimized() {
        let input = arr2(&[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]);
        let kernel = arr2(&[[1.0, 0.0], [0.0, 1.0]]);

        let result = convolve2d_optimized(&input, &kernel, BorderMode::Constant, Some(0.0))
            .expect("Operation failed");

        assert_eq!(result.shape(), input.shape());
        assert_eq!(result[[0, 0]], 1.0);
        assert_eq!(result[[1, 1]], 6.0);
    }
}