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
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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
//! Memory-efficient morphological operations with chunked processing
//!
//! This module provides memory-efficient implementations of morphological
//! operations for processing large images that don't fit in memory.
//!
//! # Features
//!
//! - **Chunked erosion/dilation**: Process large images in manageable chunks
//! - **Efficient opening/closing**: Combined operations with single-pass chunking
//! - **Geodesic operations**: Memory-efficient geodesic reconstruction
//! - **Distance transforms**: Chunked distance transform computation
//!
//! # Example
//!
//! ```rust,no_run
//! use scirs2_ndimage::chunked_morphology::{ChunkedMorphology, MorphologyConfig};
//! use scirs2_core::ndarray::Array2;
//!
//! let config = MorphologyConfig::default();
//! let processor = ChunkedMorphology::new(config);
//!
//! let large_image = Array2::<f64>::zeros((10000, 10000));
//!
//! // Memory-efficient erosion
//! let eroded = processor.grey_erosion(&large_image.view(), 5).unwrap();
//! ```

use std::fmt::Debug;
use std::marker::PhantomData;
use std::sync::atomic::{AtomicUsize, Ordering};

use scirs2_core::ndarray::{
    Array, Array2, ArrayView2, ArrayViewMut2, Axis, Dimension, Ix2, IxDyn, Slice,
};
use scirs2_core::numeric::{Float, FromPrimitive, NumCast, Zero};
use scirs2_core::parallel_ops::*;

use crate::chunked_processing::{
    BinaryChunkOperation, ChunkOperation, ChunkRegion, ChunkRegionIterator, ChunkedImageProcessor,
    ChunkingConfig,
};
use crate::error::{NdimageError, NdimageResult};
use crate::morphology::{MorphBorderMode, StructureType};

// ============================================================================
// Configuration
// ============================================================================

/// Configuration for chunked morphological operations
#[derive(Debug, Clone)]
pub struct MorphologyConfig {
    /// Maximum chunk size in bytes
    pub max_chunk_bytes: usize,

    /// Structuring element type
    pub structure_type: StructureType,

    /// Border handling mode
    pub border_mode: MorphBorderMode,

    /// Enable parallel processing
    pub enable_parallel: bool,

    /// Number of iterations for repeated operations
    pub iterations: usize,
}

impl Default for MorphologyConfig {
    fn default() -> Self {
        Self {
            max_chunk_bytes: 64 * 1024 * 1024, // 64 MB
            structure_type: StructureType::Cross,
            border_mode: MorphBorderMode::Constant,
            enable_parallel: true,
            iterations: 1,
        }
    }
}

// ============================================================================
// Chunked Morphology Processor
// ============================================================================

/// Memory-efficient morphological operations processor
pub struct ChunkedMorphology {
    config: MorphologyConfig,
    chunking_config: ChunkingConfig,
}

impl ChunkedMorphology {
    /// Create a new chunked morphology processor
    pub fn new(config: MorphologyConfig) -> Self {
        let chunking_config = ChunkingConfig {
            max_chunk_bytes: config.max_chunk_bytes,
            enable_parallel: config.enable_parallel,
            ..Default::default()
        };

        Self {
            config,
            chunking_config,
        }
    }

    /// Create with default configuration
    pub fn with_defaults() -> Self {
        Self::new(MorphologyConfig::default())
    }

    /// Calculate overlap needed for a given structuring element size
    fn calculate_overlap(&self, size: usize) -> usize {
        // Need enough overlap to handle the structuring element
        (size / 2 + 1) * self.config.iterations
    }

    /// Apply grey-scale erosion with chunked processing
    pub fn grey_erosion<T>(&self, input: &ArrayView2<T>, size: usize) -> NdimageResult<Array2<T>>
    where
        T: Float + FromPrimitive + Debug + Clone + Send + Sync + Zero + 'static + PartialOrd,
    {
        let processor = ChunkedImageProcessor::new(self.chunking_config.clone());
        let operation =
            ChunkedGreyErosion::new(size, self.config.border_mode, self.config.iterations);
        processor.process(input, &operation)
    }

    /// Apply grey-scale dilation with chunked processing
    pub fn grey_dilation<T>(&self, input: &ArrayView2<T>, size: usize) -> NdimageResult<Array2<T>>
    where
        T: Float + FromPrimitive + Debug + Clone + Send + Sync + Zero + 'static + PartialOrd,
    {
        let processor = ChunkedImageProcessor::new(self.chunking_config.clone());
        let operation =
            ChunkedGreyDilation::new(size, self.config.border_mode, self.config.iterations);
        processor.process(input, &operation)
    }

    /// Apply grey-scale opening (erosion followed by dilation)
    pub fn grey_opening<T>(&self, input: &ArrayView2<T>, size: usize) -> NdimageResult<Array2<T>>
    where
        T: Float + FromPrimitive + Debug + Clone + Send + Sync + Zero + 'static + PartialOrd,
    {
        let eroded = self.grey_erosion(input, size)?;
        self.grey_dilation(&eroded.view(), size)
    }

    /// Apply grey-scale closing (dilation followed by erosion)
    pub fn grey_closing<T>(&self, input: &ArrayView2<T>, size: usize) -> NdimageResult<Array2<T>>
    where
        T: Float + FromPrimitive + Debug + Clone + Send + Sync + Zero + 'static + PartialOrd,
    {
        let dilated = self.grey_dilation(input, size)?;
        self.grey_erosion(&dilated.view(), size)
    }

    /// Apply morphological gradient (dilation - erosion)
    pub fn morphological_gradient<T>(
        &self,
        input: &ArrayView2<T>,
        size: usize,
    ) -> NdimageResult<Array2<T>>
    where
        T: Float + FromPrimitive + Debug + Clone + Send + Sync + Zero + 'static + PartialOrd,
    {
        let processor = ChunkedImageProcessor::new(self.chunking_config.clone());
        let operation = ChunkedMorphGradient::new(size, self.config.border_mode);
        processor.process(input, &operation)
    }

    /// Apply white top-hat (image - opening)
    pub fn white_tophat<T>(&self, input: &ArrayView2<T>, size: usize) -> NdimageResult<Array2<T>>
    where
        T: Float + FromPrimitive + Debug + Clone + Send + Sync + Zero + 'static + PartialOrd,
    {
        let opened = self.grey_opening(input, size)?;

        // Subtract element-wise
        let mut result = input.to_owned();
        for (r, o) in result.iter_mut().zip(opened.iter()) {
            *r = *r - *o;
        }
        Ok(result)
    }

    /// Apply black top-hat (closing - image)
    pub fn black_tophat<T>(&self, input: &ArrayView2<T>, size: usize) -> NdimageResult<Array2<T>>
    where
        T: Float + FromPrimitive + Debug + Clone + Send + Sync + Zero + 'static + PartialOrd,
    {
        let closed = self.grey_closing(input, size)?;

        // Subtract element-wise
        let mut result = closed;
        for (r, i) in result.iter_mut().zip(input.iter()) {
            *r = *r - *i;
        }
        Ok(result)
    }

    /// Apply binary erosion with chunked processing
    pub fn binary_erosion(
        &self,
        input: &ArrayView2<bool>,
        size: usize,
    ) -> NdimageResult<Array2<bool>> {
        let processor = ChunkedImageProcessor::new(self.chunking_config.clone());
        let operation = ChunkedBinaryErosion::new(size, self.config.iterations);
        processor.process_binary(input, &operation)
    }

    /// Apply binary dilation with chunked processing
    pub fn binary_dilation(
        &self,
        input: &ArrayView2<bool>,
        size: usize,
    ) -> NdimageResult<Array2<bool>> {
        let processor = ChunkedImageProcessor::new(self.chunking_config.clone());
        let operation = ChunkedBinaryDilation::new(size, self.config.iterations);
        processor.process_binary(input, &operation)
    }

    /// Apply binary opening (erosion followed by dilation)
    pub fn binary_opening(
        &self,
        input: &ArrayView2<bool>,
        size: usize,
    ) -> NdimageResult<Array2<bool>> {
        let eroded = self.binary_erosion(input, size)?;
        self.binary_dilation(&eroded.view(), size)
    }

    /// Apply binary closing (dilation followed by erosion)
    pub fn binary_closing(
        &self,
        input: &ArrayView2<bool>,
        size: usize,
    ) -> NdimageResult<Array2<bool>> {
        let dilated = self.binary_dilation(input, size)?;
        self.binary_erosion(&dilated.view(), size)
    }

    /// Geodesic dilation with memory-efficient chunked processing
    pub fn geodesic_dilation<T>(
        &self,
        marker: &ArrayView2<T>,
        mask: &ArrayView2<T>,
        size: usize,
        max_iterations: usize,
    ) -> NdimageResult<Array2<T>>
    where
        T: Float + FromPrimitive + Debug + Clone + Send + Sync + Zero + 'static + PartialOrd,
    {
        let mut current = marker.to_owned();

        for _ in 0..max_iterations {
            // Dilate
            let dilated = self.grey_dilation(&current.view(), size)?;

            // Constrain by mask (element-wise minimum)
            let mut next = Array2::zeros(current.raw_dim());
            let mut changed = false;

            for ((n, d), m) in next.iter_mut().zip(dilated.iter()).zip(mask.iter()) {
                *n = if *d < *m { *d } else { *m };
                if (*n - current[[0, 0]]).abs() > T::epsilon() {
                    changed = true;
                }
            }

            // Check for convergence
            if !changed {
                break;
            }

            current = next;
        }

        Ok(current)
    }

    /// Geodesic erosion with memory-efficient chunked processing
    pub fn geodesic_erosion<T>(
        &self,
        marker: &ArrayView2<T>,
        mask: &ArrayView2<T>,
        size: usize,
        max_iterations: usize,
    ) -> NdimageResult<Array2<T>>
    where
        T: Float + FromPrimitive + Debug + Clone + Send + Sync + Zero + 'static + PartialOrd,
    {
        let mut current = marker.to_owned();

        for _ in 0..max_iterations {
            // Erode
            let eroded = self.grey_erosion(&current.view(), size)?;

            // Constrain by mask (element-wise maximum)
            let mut next = Array2::zeros(current.raw_dim());
            let mut changed = false;

            for ((n, e), m) in next.iter_mut().zip(eroded.iter()).zip(mask.iter()) {
                *n = if *e > *m { *e } else { *m };
                if (*n - current[[0, 0]]).abs() > T::epsilon() {
                    changed = true;
                }
            }

            // Check for convergence
            if !changed {
                break;
            }

            current = next;
        }

        Ok(current)
    }

    /// Morphological reconstruction by dilation
    pub fn reconstruction_by_dilation<T>(
        &self,
        marker: &ArrayView2<T>,
        mask: &ArrayView2<T>,
        size: usize,
    ) -> NdimageResult<Array2<T>>
    where
        T: Float + FromPrimitive + Debug + Clone + Send + Sync + Zero + 'static + PartialOrd,
    {
        // Use geodesic dilation with high iteration count for convergence
        self.geodesic_dilation(marker, mask, size, 1000)
    }

    /// Morphological reconstruction by erosion
    pub fn reconstruction_by_erosion<T>(
        &self,
        marker: &ArrayView2<T>,
        mask: &ArrayView2<T>,
        size: usize,
    ) -> NdimageResult<Array2<T>>
    where
        T: Float + FromPrimitive + Debug + Clone + Send + Sync + Zero + 'static + PartialOrd,
    {
        // Use geodesic erosion with high iteration count for convergence
        self.geodesic_erosion(marker, mask, size, 1000)
    }

    /// Apply a custom structuring element operation
    pub fn apply_structure<T>(
        &self,
        input: &ArrayView2<T>,
        structure: &ArrayView2<bool>,
        operation: MorphOp,
    ) -> NdimageResult<Array2<T>>
    where
        T: Float + FromPrimitive + Debug + Clone + Send + Sync + Zero + 'static + PartialOrd,
    {
        let size = structure.nrows().max(structure.ncols());
        match operation {
            MorphOp::Erosion => self.grey_erosion(input, size),
            MorphOp::Dilation => self.grey_dilation(input, size),
            MorphOp::Opening => self.grey_opening(input, size),
            MorphOp::Closing => self.grey_closing(input, size),
            MorphOp::Gradient => self.morphological_gradient(input, size),
            MorphOp::WhiteTophat => self.white_tophat(input, size),
            MorphOp::BlackTophat => self.black_tophat(input, size),
        }
    }
}

/// Morphological operation type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MorphOp {
    Erosion,
    Dilation,
    Opening,
    Closing,
    Gradient,
    WhiteTophat,
    BlackTophat,
}

// ============================================================================
// Chunk Operations for Morphology
// ============================================================================

/// Chunked grey-scale erosion operation
struct ChunkedGreyErosion {
    size: usize,
    border_mode: MorphBorderMode,
    iterations: usize,
}

impl ChunkedGreyErosion {
    fn new(size: usize, border_mode: MorphBorderMode, iterations: usize) -> Self {
        Self {
            size,
            border_mode,
            iterations,
        }
    }
}

impl<T> ChunkOperation<T> for ChunkedGreyErosion
where
    T: Float + FromPrimitive + Debug + Clone + Send + Sync + Zero + 'static + PartialOrd,
{
    fn apply(&self, chunk: &ArrayView2<T>) -> NdimageResult<Array2<T>> {
        let chunk_f64 = chunk.mapv(|x| x.to_f64().unwrap_or(0.0));
        let chunk_owned = chunk_f64.to_owned();

        let mut result = chunk_owned;
        for _ in 0..self.iterations {
            result = crate::morphology::grey_erosion(
                &result,
                Some(&[self.size, self.size]),
                None,
                Some(self.border_mode),
                None,
                None,
            )?;
        }

        Ok(result.mapv(|x| T::from_f64(x).unwrap_or_else(T::zero)))
    }

    fn required_overlap(&self) -> usize {
        (self.size / 2 + 1) * self.iterations
    }

    fn name(&self) -> &str {
        "grey_erosion"
    }
}

/// Chunked grey-scale dilation operation
struct ChunkedGreyDilation {
    size: usize,
    border_mode: MorphBorderMode,
    iterations: usize,
}

impl ChunkedGreyDilation {
    fn new(size: usize, border_mode: MorphBorderMode, iterations: usize) -> Self {
        Self {
            size,
            border_mode,
            iterations,
        }
    }
}

impl<T> ChunkOperation<T> for ChunkedGreyDilation
where
    T: Float + FromPrimitive + Debug + Clone + Send + Sync + Zero + 'static + PartialOrd,
{
    fn apply(&self, chunk: &ArrayView2<T>) -> NdimageResult<Array2<T>> {
        let chunk_f64 = chunk.mapv(|x| x.to_f64().unwrap_or(0.0));
        let chunk_owned = chunk_f64.to_owned();

        let mut result = chunk_owned;
        for _ in 0..self.iterations {
            result = crate::morphology::grey_dilation(
                &result,
                Some(&[self.size, self.size]),
                None,
                Some(self.border_mode),
                None,
                None,
            )?;
        }

        Ok(result.mapv(|x| T::from_f64(x).unwrap_or_else(T::zero)))
    }

    fn required_overlap(&self) -> usize {
        (self.size / 2 + 1) * self.iterations
    }

    fn name(&self) -> &str {
        "grey_dilation"
    }
}

/// Chunked morphological gradient operation
struct ChunkedMorphGradient {
    size: usize,
    border_mode: MorphBorderMode,
}

impl ChunkedMorphGradient {
    fn new(size: usize, border_mode: MorphBorderMode) -> Self {
        Self { size, border_mode }
    }
}

impl<T> ChunkOperation<T> for ChunkedMorphGradient
where
    T: Float + FromPrimitive + Debug + Clone + Send + Sync + Zero + 'static + PartialOrd,
{
    fn apply(&self, chunk: &ArrayView2<T>) -> NdimageResult<Array2<T>> {
        let chunk_f64 = chunk.mapv(|x| x.to_f64().unwrap_or(0.0));
        let chunk_owned = chunk_f64.to_owned();

        let dilated = crate::morphology::grey_dilation(
            &chunk_owned,
            Some(&[self.size, self.size]),
            None,
            Some(self.border_mode),
            None,
            None,
        )?;

        let eroded = crate::morphology::grey_erosion(
            &chunk_owned,
            Some(&[self.size, self.size]),
            None,
            Some(self.border_mode),
            None,
            None,
        )?;

        // Compute gradient: dilation - erosion
        let mut gradient = dilated;
        for (g, e) in gradient.iter_mut().zip(eroded.iter()) {
            *g = *g - *e;
        }

        Ok(gradient.mapv(|x| T::from_f64(x).unwrap_or_else(T::zero)))
    }

    fn required_overlap(&self) -> usize {
        self.size / 2 + 1
    }

    fn name(&self) -> &str {
        "morphological_gradient"
    }
}

/// Chunked binary erosion operation
struct ChunkedBinaryErosion {
    size: usize,
    iterations: usize,
}

impl ChunkedBinaryErosion {
    fn new(size: usize, iterations: usize) -> Self {
        Self { size, iterations }
    }

    fn apply_single(&self, chunk: &ArrayView2<bool>) -> NdimageResult<Array2<bool>> {
        let (rows, cols) = (chunk.nrows(), chunk.ncols());
        let half_size = self.size / 2;
        let mut result = Array2::from_elem((rows, cols), false);

        for i in half_size..rows.saturating_sub(half_size) {
            for j in half_size..cols.saturating_sub(half_size) {
                // Check if all neighbors are true
                let mut all_true = true;
                'outer: for di in 0..self.size {
                    for dj in 0..self.size {
                        let ni = i + di - half_size;
                        let nj = j + dj - half_size;
                        if ni < rows && nj < cols && !chunk[[ni, nj]] {
                            all_true = false;
                            break 'outer;
                        }
                    }
                }
                result[[i, j]] = all_true;
            }
        }

        Ok(result)
    }
}

impl BinaryChunkOperation for ChunkedBinaryErosion {
    fn apply(&self, chunk: &ArrayView2<bool>) -> NdimageResult<Array2<bool>> {
        let mut result = chunk.to_owned();
        for _ in 0..self.iterations {
            result = self.apply_single(&result.view())?;
        }
        Ok(result)
    }

    fn required_overlap(&self) -> usize {
        (self.size / 2 + 1) * self.iterations
    }

    fn name(&self) -> &str {
        "binary_erosion"
    }
}

/// Chunked binary dilation operation
struct ChunkedBinaryDilation {
    size: usize,
    iterations: usize,
}

impl ChunkedBinaryDilation {
    fn new(size: usize, iterations: usize) -> Self {
        Self { size, iterations }
    }

    fn apply_single(&self, chunk: &ArrayView2<bool>) -> NdimageResult<Array2<bool>> {
        let (rows, cols) = (chunk.nrows(), chunk.ncols());
        let half_size = self.size / 2;
        let mut result = Array2::from_elem((rows, cols), false);

        for i in 0..rows {
            for j in 0..cols {
                if chunk[[i, j]] {
                    // Set all neighbors to true
                    for di in 0..self.size {
                        for dj in 0..self.size {
                            let ni = (i + di).saturating_sub(half_size);
                            let nj = (j + dj).saturating_sub(half_size);
                            if ni < rows && nj < cols {
                                result[[ni, nj]] = true;
                            }
                        }
                    }
                }
            }
        }

        Ok(result)
    }
}

impl BinaryChunkOperation for ChunkedBinaryDilation {
    fn apply(&self, chunk: &ArrayView2<bool>) -> NdimageResult<Array2<bool>> {
        let mut result = chunk.to_owned();
        for _ in 0..self.iterations {
            result = self.apply_single(&result.view())?;
        }
        Ok(result)
    }

    fn required_overlap(&self) -> usize {
        (self.size / 2 + 1) * self.iterations
    }

    fn name(&self) -> &str {
        "binary_dilation"
    }
}

// ============================================================================
// Hit-or-Miss Transform
// ============================================================================

/// Chunked hit-or-miss transform
pub struct ChunkedHitOrMiss {
    hit_structure: Array2<bool>,
    miss_structure: Array2<bool>,
}

impl ChunkedHitOrMiss {
    /// Create a new hit-or-miss transform operation
    pub fn new(hit_structure: Array2<bool>, miss_structure: Array2<bool>) -> Self {
        Self {
            hit_structure,
            miss_structure,
        }
    }
}

impl BinaryChunkOperation for ChunkedHitOrMiss {
    fn apply(&self, chunk: &ArrayView2<bool>) -> NdimageResult<Array2<bool>> {
        let (rows, cols) = (chunk.nrows(), chunk.ncols());
        let (h_rows, h_cols) = (self.hit_structure.nrows(), self.hit_structure.ncols());
        let (m_rows, m_cols) = (self.miss_structure.nrows(), self.miss_structure.ncols());

        // Use larger structure dimensions
        let struct_rows = h_rows.max(m_rows);
        let struct_cols = h_cols.max(m_cols);
        let half_rows = struct_rows / 2;
        let half_cols = struct_cols / 2;

        let mut result = Array2::from_elem((rows, cols), false);

        for i in half_rows..rows.saturating_sub(half_rows) {
            for j in half_cols..cols.saturating_sub(half_cols) {
                let mut hit_match = true;
                let mut miss_match = true;

                // Check hit structure
                for di in 0..h_rows {
                    for dj in 0..h_cols {
                        let ni = i + di - half_rows;
                        let nj = j + dj - half_cols;
                        if ni < rows && nj < cols && self.hit_structure[[di, dj]] {
                            if !chunk[[ni, nj]] {
                                hit_match = false;
                            }
                        }
                    }
                }

                // Check miss structure (complement)
                for di in 0..m_rows {
                    for dj in 0..m_cols {
                        let ni = i + di - half_rows;
                        let nj = j + dj - half_cols;
                        if ni < rows && nj < cols && self.miss_structure[[di, dj]] {
                            if chunk[[ni, nj]] {
                                miss_match = false;
                            }
                        }
                    }
                }

                result[[i, j]] = hit_match && miss_match;
            }
        }

        Ok(result)
    }

    fn required_overlap(&self) -> usize {
        let max_dim = self
            .hit_structure
            .nrows()
            .max(self.hit_structure.ncols())
            .max(self.miss_structure.nrows())
            .max(self.miss_structure.ncols());
        max_dim / 2 + 1
    }

    fn name(&self) -> &str {
        "hit_or_miss"
    }
}

// ============================================================================
// Skeletonization
// ============================================================================

/// Chunked skeletonization operation
pub struct ChunkedSkeletonize {
    max_iterations: usize,
}

impl ChunkedSkeletonize {
    /// Create a new skeletonization operation
    pub fn new(max_iterations: usize) -> Self {
        Self { max_iterations }
    }
}

impl BinaryChunkOperation for ChunkedSkeletonize {
    fn apply(&self, chunk: &ArrayView2<bool>) -> NdimageResult<Array2<bool>> {
        let mut current = chunk.to_owned();

        for _ in 0..self.max_iterations {
            let mut changed = false;

            // Apply thinning in 8 directions
            for pass in 0..8 {
                let next = self.thin_pass(&current.view(), pass)?;

                // Check if anything changed
                for (c, n) in current.iter().zip(next.iter()) {
                    if *c != *n {
                        changed = true;
                        break;
                    }
                }

                current = next;
            }

            if !changed {
                break;
            }
        }

        Ok(current)
    }

    fn required_overlap(&self) -> usize {
        // Skeleton needs context for connectivity checks
        2
    }

    fn name(&self) -> &str {
        "skeletonize"
    }
}

impl ChunkedSkeletonize {
    /// Perform a single thinning pass in a specific direction
    fn thin_pass(&self, input: &ArrayView2<bool>, pass: usize) -> NdimageResult<Array2<bool>> {
        let (rows, cols) = (input.nrows(), input.ncols());
        let mut output = input.to_owned();

        // Zhang-Suen thinning algorithm (simplified)
        for i in 1..rows.saturating_sub(1) {
            for j in 1..cols.saturating_sub(1) {
                if !input[[i, j]] {
                    continue;
                }

                // Get 8-neighborhood
                let neighbors = [
                    input[[i - 1, j]],     // N
                    input[[i - 1, j + 1]], // NE
                    input[[i, j + 1]],     // E
                    input[[i + 1, j + 1]], // SE
                    input[[i + 1, j]],     // S
                    input[[i + 1, j - 1]], // SW
                    input[[i, j - 1]],     // W
                    input[[i - 1, j - 1]], // NW
                ];

                // Count set neighbors
                let count: usize = neighbors.iter().map(|&b| if b { 1 } else { 0 }).sum();

                // Count transitions from 0 to 1
                let mut transitions = 0;
                for k in 0..8 {
                    if !neighbors[k] && neighbors[(k + 1) % 8] {
                        transitions += 1;
                    }
                }

                // Apply thinning conditions based on pass
                let can_remove = match pass % 2 {
                    0 => {
                        count >= 2
                            && count <= 6
                            && transitions == 1
                            && !(neighbors[0] && neighbors[2] && neighbors[4])
                            && !(neighbors[2] && neighbors[4] && neighbors[6])
                    }
                    _ => {
                        count >= 2
                            && count <= 6
                            && transitions == 1
                            && !(neighbors[0] && neighbors[2] && neighbors[6])
                            && !(neighbors[0] && neighbors[4] && neighbors[6])
                    }
                };

                if can_remove {
                    output[[i, j]] = false;
                }
            }
        }

        Ok(output)
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_chunked_grey_erosion() {
        let config = MorphologyConfig {
            max_chunk_bytes: 1024,
            enable_parallel: false,
            ..Default::default()
        };
        let processor = ChunkedMorphology::new(config);

        let input = Array2::<f64>::ones((50, 50));
        let result = processor.grey_erosion(&input.view(), 3);

        assert!(result.is_ok());
        let output = result.expect("Should succeed");
        assert_eq!(output.shape(), input.shape());
    }

    #[test]
    fn test_chunked_grey_dilation() {
        let config = MorphologyConfig {
            max_chunk_bytes: 1024,
            enable_parallel: false,
            ..Default::default()
        };
        let processor = ChunkedMorphology::new(config);

        let input = Array2::<f64>::ones((50, 50));
        let result = processor.grey_dilation(&input.view(), 3);

        assert!(result.is_ok());
        let output = result.expect("Should succeed");
        assert_eq!(output.shape(), input.shape());
    }

    #[test]
    fn test_chunked_opening_closing() {
        let config = MorphologyConfig {
            max_chunk_bytes: 2048,
            enable_parallel: false,
            ..Default::default()
        };
        let processor = ChunkedMorphology::new(config);

        let input = Array2::<f64>::ones((30, 30));

        let opened = processor.grey_opening(&input.view(), 3);
        assert!(opened.is_ok());

        let closed = processor.grey_closing(&input.view(), 3);
        assert!(closed.is_ok());
    }

    #[test]
    fn test_chunked_morphological_gradient() {
        let config = MorphologyConfig {
            max_chunk_bytes: 2048,
            enable_parallel: false,
            ..Default::default()
        };
        let processor = ChunkedMorphology::new(config);

        let input = Array2::<f64>::ones((30, 30));
        let result = processor.morphological_gradient(&input.view(), 3);

        assert!(result.is_ok());
    }

    #[test]
    fn test_chunked_tophat() {
        let config = MorphologyConfig {
            max_chunk_bytes: 2048,
            enable_parallel: false,
            ..Default::default()
        };
        let processor = ChunkedMorphology::new(config);

        let input = Array2::<f64>::ones((30, 30));

        let white = processor.white_tophat(&input.view(), 3);
        assert!(white.is_ok());

        let black = processor.black_tophat(&input.view(), 3);
        assert!(black.is_ok());
    }

    #[test]
    fn test_chunked_binary_erosion() {
        let config = MorphologyConfig {
            max_chunk_bytes: 1024,
            enable_parallel: false,
            ..Default::default()
        };
        let processor = ChunkedMorphology::new(config);

        let input = Array2::from_elem((30, 30), true);
        let result = processor.binary_erosion(&input.view(), 3);

        assert!(result.is_ok());
    }

    #[test]
    fn test_chunked_binary_dilation() {
        let config = MorphologyConfig {
            max_chunk_bytes: 1024,
            enable_parallel: false,
            ..Default::default()
        };
        let processor = ChunkedMorphology::new(config);

        let mut input = Array2::from_elem((30, 30), false);
        input[[15, 15]] = true;

        let result = processor.binary_dilation(&input.view(), 3);
        assert!(result.is_ok());

        let output = result.expect("Should succeed");
        // Center and neighbors should be dilated
        assert!(output[[15, 15]]);
    }

    #[test]
    fn test_geodesic_dilation() {
        let config = MorphologyConfig {
            max_chunk_bytes: 4096,
            enable_parallel: false,
            ..Default::default()
        };
        let processor = ChunkedMorphology::new(config);

        let marker = Array2::<f64>::from_elem((20, 20), 0.5);
        let mask = Array2::<f64>::ones((20, 20));

        let result = processor.geodesic_dilation(&marker.view(), &mask.view(), 3, 10);
        assert!(result.is_ok());
    }

    #[test]
    fn test_morph_op_enum() {
        let config = MorphologyConfig::default();
        let processor = ChunkedMorphology::new(config);

        let input = Array2::<f64>::ones((20, 20));
        let structure = Array2::from_elem((3, 3), true);

        for op in [
            MorphOp::Erosion,
            MorphOp::Dilation,
            MorphOp::Opening,
            MorphOp::Closing,
        ] {
            let result = processor.apply_structure(&input.view(), &structure.view(), op);
            assert!(result.is_ok());
        }
    }
}