leptonica 0.1.0

Rust port of Leptonica image processing library
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
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
//! Connected component analysis
//!
//! This module provides functions for finding and labeling connected components
//! in binary images. It uses Union-Find (disjoint set) data structure for
//! efficient labeling.

use crate::core::{Box, Boxa, Pix, PixMut, Pixa, PixelDepth};
use crate::region::error::{RegionError, RegionResult};

/// Connectivity type for component analysis
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ConnectivityType {
    /// 4-way connectivity (up, down, left, right)
    #[default]
    FourWay,
    /// 8-way connectivity (includes diagonals)
    EightWay,
}

/// A connected component in an image
#[derive(Debug, Clone)]
pub struct ConnectedComponent {
    /// Unique label for this component
    pub label: u32,
    /// Number of pixels in this component
    pub pixel_count: u32,
    /// Bounding box of this component
    pub bounds: Box,
}

impl ConnectedComponent {
    /// Create a new connected component
    pub fn new(label: u32, pixel_count: u32, bounds: Box) -> Self {
        Self {
            label,
            pixel_count,
            bounds,
        }
    }
}

/// Union-Find data structure for efficient connected component labeling
struct UnionFind {
    parent: Vec<u32>,
    rank: Vec<u32>,
}

impl UnionFind {
    fn new(size: usize) -> Self {
        Self {
            parent: (0..size as u32).collect(),
            rank: vec![0; size],
        }
    }

    fn find(&mut self, x: u32) -> u32 {
        if self.parent[x as usize] != x {
            self.parent[x as usize] = self.find(self.parent[x as usize]);
        }
        self.parent[x as usize]
    }

    fn union(&mut self, x: u32, y: u32) {
        let root_x = self.find(x);
        let root_y = self.find(y);

        if root_x != root_y {
            let rank_x = self.rank[root_x as usize];
            let rank_y = self.rank[root_y as usize];

            if rank_x < rank_y {
                self.parent[root_x as usize] = root_y;
            } else if rank_x > rank_y {
                self.parent[root_y as usize] = root_x;
            } else {
                self.parent[root_y as usize] = root_x;
                self.rank[root_x as usize] += 1;
            }
        }
    }
}

/// Find connected components in a binary image
///
/// Returns a vector of connected components found in the foreground (1 pixels).
///
/// # Arguments
///
/// * `pix` - Input binary image (1-bit depth)
/// * `connectivity` - Type of connectivity (4-way or 8-way)
///
/// # Returns
///
/// A vector of `ConnectedComponent` structures describing each component.
///
/// # Errors
///
/// Returns an error if the image is not 1-bit depth.
pub fn find_connected_components(
    pix: &Pix,
    connectivity: ConnectivityType,
) -> RegionResult<Vec<ConnectedComponent>> {
    if pix.depth() != PixelDepth::Bit1 {
        return Err(RegionError::UnsupportedDepth {
            expected: "1-bit",
            actual: pix.depth().bits(),
        });
    }

    let width = pix.width();
    let height = pix.height();

    if width == 0 || height == 0 {
        return Ok(Vec::new());
    }

    // Label the image first
    let labeled = label_connected_components(pix, connectivity)?;

    // Extract component information from labeled image
    extract_components_from_labels(&labeled)
}

/// Label connected components in a binary image
///
/// Creates a labeled image where each connected component has a unique label.
/// Background pixels have label 0.
///
/// # Arguments
///
/// * `pix` - Input binary image (1-bit depth)
/// * `connectivity` - Type of connectivity (4-way or 8-way)
///
/// # Returns
///
/// A 32-bit image where each pixel contains its component label (0 for background).
///
/// # Errors
///
/// Returns an error if the image is not 1-bit depth.
pub fn label_connected_components(pix: &Pix, connectivity: ConnectivityType) -> RegionResult<Pix> {
    if pix.depth() != PixelDepth::Bit1 {
        return Err(RegionError::UnsupportedDepth {
            expected: "1-bit",
            actual: pix.depth().bits(),
        });
    }

    let width = pix.width();
    let height = pix.height();

    // Create output image with 32-bit depth for labels
    let mut output = Pix::new(width, height, PixelDepth::Bit32)
        .map_err(RegionError::Core)?
        .try_into_mut()
        .unwrap_or_else(|p| p.to_mut());

    if width == 0 || height == 0 {
        return Ok(output.into());
    }

    // Maximum possible labels (worst case: every other pixel is a separate component)
    let max_labels = ((width as usize) * (height as usize) / 2) + 1;
    let mut uf = UnionFind::new(max_labels);
    let mut next_label: u32 = 1;

    // First pass: assign provisional labels and record equivalences
    for y in 0..height {
        for x in 0..width {
            // Skip background pixels
            if pix.get_pixel(x, y).unwrap_or(0) == 0 {
                continue;
            }

            let mut neighbors = Vec::with_capacity(4);

            // Check neighbors that have already been processed (above and left)
            // For 4-way: check left and top
            // For 8-way: check left, top-left, top, top-right

            // Left neighbor
            if x > 0
                && let Some(label) = output.get_pixel(x - 1, y)
                && label > 0
            {
                neighbors.push(label);
            }

            // Top neighbor
            if y > 0
                && let Some(label) = output.get_pixel(x, y - 1)
                && label > 0
            {
                neighbors.push(label);
            }

            if connectivity == ConnectivityType::EightWay {
                // Top-left neighbor
                if x > 0
                    && y > 0
                    && let Some(label) = output.get_pixel(x - 1, y - 1)
                    && label > 0
                {
                    neighbors.push(label);
                }

                // Top-right neighbor
                if x + 1 < width
                    && y > 0
                    && let Some(label) = output.get_pixel(x + 1, y - 1)
                    && label > 0
                {
                    neighbors.push(label);
                }
            }

            if neighbors.is_empty() {
                // New component
                let _ = output.set_pixel(x, y, next_label);
                next_label += 1;
            } else {
                // Find minimum label among neighbors
                let min_label = *neighbors.iter().min().unwrap();
                let _ = output.set_pixel(x, y, min_label);

                // Union all neighbor labels
                for &label in &neighbors {
                    uf.union(min_label, label);
                }
            }
        }
    }

    // Second pass: resolve labels using union-find
    // Create a mapping from root labels to sequential labels
    let mut label_map = std::collections::HashMap::new();
    let mut final_label: u32 = 1;

    for y in 0..height {
        for x in 0..width {
            if let Some(label) = output.get_pixel(x, y)
                && label > 0
            {
                let root = uf.find(label);
                let mapped = *label_map.entry(root).or_insert_with(|| {
                    let l = final_label;
                    final_label += 1;
                    l
                });
                let _ = output.set_pixel(x, y, mapped);
            }
        }
    }

    Ok(output.into())
}

/// Extract component information from a labeled image
fn extract_components_from_labels(labeled: &Pix) -> RegionResult<Vec<ConnectedComponent>> {
    let width = labeled.width();
    let height = labeled.height();

    // Collect statistics for each label
    let mut stats: std::collections::HashMap<u32, (u32, i32, i32, i32, i32)> =
        std::collections::HashMap::new();

    for y in 0..height {
        for x in 0..width {
            if let Some(label) = labeled.get_pixel(x, y)
                && label > 0
            {
                let entry = stats
                    .entry(label)
                    .or_insert((0, x as i32, y as i32, x as i32, y as i32));
                entry.0 += 1; // pixel count
                entry.1 = entry.1.min(x as i32); // min_x
                entry.2 = entry.2.min(y as i32); // min_y
                entry.3 = entry.3.max(x as i32); // max_x
                entry.4 = entry.4.max(y as i32); // max_y
            }
        }
    }

    // Convert to ConnectedComponent structures
    let mut components: Vec<ConnectedComponent> = stats
        .into_iter()
        .map(|(label, (count, min_x, min_y, max_x, max_y))| {
            let bounds = Box::new_unchecked(min_x, min_y, max_x - min_x + 1, max_y - min_y + 1);
            ConnectedComponent::new(label, count, bounds)
        })
        .collect();

    // Sort by label for consistent ordering
    components.sort_by_key(|c| c.label);

    Ok(components)
}

/// Extract a single component from a labeled image
///
/// Creates a binary image containing only the specified component.
///
/// # Arguments
///
/// * `labeled` - Labeled image (from `label_connected_components`)
/// * `label` - Label of the component to extract
///
/// # Returns
///
/// A 1-bit binary image containing only the specified component.
pub fn extract_component(labeled: &Pix, label: u32) -> RegionResult<Pix> {
    if labeled.depth() != PixelDepth::Bit32 {
        return Err(RegionError::UnsupportedDepth {
            expected: "32-bit (labeled image)",
            actual: labeled.depth().bits(),
        });
    }

    let width = labeled.width();
    let height = labeled.height();

    let mut output = Pix::new(width, height, PixelDepth::Bit1)
        .map_err(RegionError::Core)?
        .try_into_mut()
        .unwrap_or_else(|p| p.to_mut());

    for y in 0..height {
        for x in 0..width {
            if let Some(pixel_label) = labeled.get_pixel(x, y)
                && pixel_label == label
            {
                let _ = output.set_pixel(x, y, 1);
            }
        }
    }

    Ok(output.into())
}

/// Filter components by size
///
/// Removes components that don't meet the size criteria.
///
/// # Arguments
///
/// * `labeled` - Labeled image
/// * `min_size` - Minimum pixel count (components smaller than this are removed)
/// * `max_size` - Maximum pixel count (components larger than this are removed), 0 means no limit
///
/// # Returns
///
/// A new labeled image with filtered components.
pub fn filter_components_by_size(labeled: &Pix, min_size: u32, max_size: u32) -> RegionResult<Pix> {
    if labeled.depth() != PixelDepth::Bit32 {
        return Err(RegionError::UnsupportedDepth {
            expected: "32-bit (labeled image)",
            actual: labeled.depth().bits(),
        });
    }

    let width = labeled.width();
    let height = labeled.height();

    // Count pixels for each label
    let mut label_counts: std::collections::HashMap<u32, u32> = std::collections::HashMap::new();

    for y in 0..height {
        for x in 0..width {
            if let Some(label) = labeled.get_pixel(x, y)
                && label > 0
            {
                *label_counts.entry(label).or_insert(0) += 1;
            }
        }
    }

    // Determine which labels to keep
    let valid_labels: std::collections::HashSet<u32> = label_counts
        .into_iter()
        .filter(|&(_, count)| count >= min_size && (max_size == 0 || count <= max_size))
        .map(|(label, _)| label)
        .collect();

    // Create output with filtered labels
    let mut output = Pix::new(width, height, PixelDepth::Bit32)
        .map_err(RegionError::Core)?
        .try_into_mut()
        .unwrap_or_else(|p| p.to_mut());

    // Remap labels to sequential values
    let mut label_remap: std::collections::HashMap<u32, u32> = std::collections::HashMap::new();
    let mut next_label = 1u32;

    for y in 0..height {
        for x in 0..width {
            if let Some(label) = labeled.get_pixel(x, y)
                && label > 0
                && valid_labels.contains(&label)
            {
                let new_label = *label_remap.entry(label).or_insert_with(|| {
                    let l = next_label;
                    next_label += 1;
                    l
                });
                let _ = output.set_pixel(x, y, new_label);
            }
        }
    }

    Ok(output.into())
}

/// Transform labeled image to area values
///
/// Creates an image where each pixel contains the area (pixel count) of its component.
///
/// # Arguments
///
/// * `labeled` - Labeled image (from `label_connected_components`)
///
/// # Returns
///
/// A 32-bit image where each pixel contains its component's pixel count.
pub fn component_area_transform(labeled: &Pix) -> RegionResult<Pix> {
    if labeled.depth() != PixelDepth::Bit32 {
        return Err(RegionError::UnsupportedDepth {
            expected: "32-bit (labeled image)",
            actual: labeled.depth().bits(),
        });
    }

    let width = labeled.width();
    let height = labeled.height();

    // Count pixels for each label
    let mut label_counts: std::collections::HashMap<u32, u32> = std::collections::HashMap::new();

    for y in 0..height {
        for x in 0..width {
            if let Some(label) = labeled.get_pixel(x, y)
                && label > 0
            {
                *label_counts.entry(label).or_insert(0) += 1;
            }
        }
    }

    // Create output with area values
    let mut output = Pix::new(width, height, PixelDepth::Bit32)
        .map_err(RegionError::Core)?
        .try_into_mut()
        .unwrap_or_else(|p| p.to_mut());

    for y in 0..height {
        for x in 0..width {
            if let Some(label) = labeled.get_pixel(x, y)
                && label > 0
            {
                let area = label_counts.get(&label).copied().unwrap_or(0);
                let _ = output.set_pixel(x, y, area);
            }
        }
    }

    Ok(output.into())
}

/// Find connected components and return as Pixa with bounding boxes
///
/// Finds all foreground connected components in a binary image and returns
/// them as a `Pixa` (array of component images clipped to their bounding boxes)
/// along with a `Boxa` of the bounding boxes in the original image.
///
/// This is the Rust equivalent of C Leptonica's `pixConnCompPixa`.
///
/// # Arguments
///
/// * `pix` - Input binary image (1-bit depth)
/// * `connectivity` - Type of connectivity (4-way or 8-way)
///
/// # Returns
///
/// A tuple of `(Boxa, Pixa)` where each entry corresponds to one connected
/// component. The Pixa images are clipped to the bounding box of each component.
///
/// # Errors
///
/// Returns an error if the image is not 1-bit depth.
pub fn conncomp_pixa(pix: &Pix, connectivity: ConnectivityType) -> RegionResult<(Boxa, Pixa)> {
    if pix.depth() != PixelDepth::Bit1 {
        return Err(RegionError::UnsupportedDepth {
            expected: "1-bit",
            actual: pix.depth().bits(),
        });
    }

    let width = pix.width();
    let height = pix.height();

    if width == 0 || height == 0 {
        return Ok((Boxa::new(), Pixa::new()));
    }

    // Label the image and extract component metadata
    let labeled = label_connected_components(pix, connectivity)?;
    let components = extract_components_from_labels(&labeled)?;

    let mut boxa = Boxa::with_capacity(components.len());
    let mut pixa = Pixa::with_capacity(components.len());

    for comp in &components {
        let b = comp.bounds;
        let bx = b.x as u32;
        let by = b.y as u32;
        let bw = b.w as u32;
        let bh = b.h as u32;

        // Create a clipped binary image for this component
        let clip = Pix::new(bw, bh, PixelDepth::Bit1).map_err(RegionError::Core)?;
        let mut clip_mut = clip.try_into_mut().unwrap_or_else(|p| p.to_mut());

        for y in 0..bh {
            for x in 0..bw {
                if labeled.get_pixel(bx + x, by + y).unwrap_or(0) == comp.label {
                    let _ = clip_mut.set_pixel(x, y, 1);
                }
            }
        }

        pixa.push_with_box(clip_mut.into(), b);
        boxa.push(b);
    }

    Ok((boxa, pixa))
}

/// Get unique sorted neighbor label values at a pixel location
///
/// For a labeled image (8, 16, or 32 bpp), returns the unique non-zero
/// label values of the neighbors of the pixel at (x, y), sorted in
/// ascending order.
///
/// This is the Rust equivalent of C Leptonica's `pixGetSortedNeighborValues`.
///
/// # Arguments
///
/// * `pix` - Labeled image (8, 16, or 32 bpp)
/// * `x` - X coordinate
/// * `y` - Y coordinate
/// * `connectivity` - 4-way or 8-way connectivity
///
/// # Returns
///
/// A sorted `Vec<u32>` of unique non-zero neighbor values. Empty if no
/// non-zero neighbors exist.
///
/// # Errors
///
/// Returns an error if depth is less than 8 bpp, or if (x, y) is out of bounds.
pub fn get_sorted_neighbor_values(
    pix: &Pix,
    x: u32,
    y: u32,
    connectivity: ConnectivityType,
) -> RegionResult<Vec<u32>> {
    let depth = pix.depth().bits();
    if depth < 8 {
        return Err(RegionError::UnsupportedDepth {
            expected: "8, 16, or 32 bpp",
            actual: depth,
        });
    }

    let w = pix.width();
    let h = pix.height();

    if x >= w || y >= h {
        return Err(RegionError::InvalidParameters(format!(
            "coordinates ({x}, {y}) out of bounds for {}x{} image",
            w, h
        )));
    }

    // Collect neighbor coordinates based on connectivity
    let mut neighbors: Vec<(u32, u32)> = Vec::with_capacity(8);

    // 4-way neighbors
    if x > 0 {
        neighbors.push((x - 1, y));
    }
    if x + 1 < w {
        neighbors.push((x + 1, y));
    }
    if y > 0 {
        neighbors.push((x, y - 1));
    }
    if y + 1 < h {
        neighbors.push((x, y + 1));
    }

    // Additional diagonal neighbors for 8-way
    if connectivity == ConnectivityType::EightWay {
        if x > 0 && y > 0 {
            neighbors.push((x - 1, y - 1));
        }
        if x + 1 < w && y > 0 {
            neighbors.push((x + 1, y - 1));
        }
        if x > 0 && y + 1 < h {
            neighbors.push((x - 1, y + 1));
        }
        if x + 1 < w && y + 1 < h {
            neighbors.push((x + 1, y + 1));
        }
    }

    // Collect unique non-zero values using Vec + sort/dedup for efficiency
    // (at most 8 neighbors, so this is cheaper than BTreeSet)
    let mut values: Vec<u32> = Vec::with_capacity(8);
    for (nx, ny) in neighbors {
        let val = pix.get_pixel(nx, ny).unwrap_or(0);
        if val > 0 {
            values.push(val);
        }
    }
    values.sort_unstable();
    values.dedup();

    Ok(values)
}

/// Count connected components without full labeling
///
/// Returns the number of connected components (4-way or 8-way) in a binary image.
/// More efficient than `find_connected_components()` when only the count is needed.
///
/// # Arguments
///
/// * `pix` - 1-bpp binary image
/// * `connectivity` - 4-way or 8-way connectivity
///
/// # Returns
///
/// Number of connected components
///
/// # Errors
///
/// Returns an error if the image is not 1-bit depth, or if the component count
/// exceeds `u32::MAX`.
pub fn count_conn_comp(pix: &Pix, connectivity: ConnectivityType) -> RegionResult<u32> {
    if pix.depth() != PixelDepth::Bit1 {
        return Err(RegionError::UnsupportedDepth {
            expected: "1-bit",
            actual: pix.depth().bits(),
        });
    }

    // For now, use find_connected_components as implementation
    // TODO: Optimize to avoid storing all component info
    let components = find_connected_components(pix, connectivity)?;
    u32::try_from(components.len())
        .map_err(|_| RegionError::InvalidParameters("component count exceeds u32::MAX".into()))
}

/// Find the next ON pixel in raster scan order starting from a given position
///
/// Scans the image from left to right, top to bottom, starting at the pixel
/// **after** `(start_x, start_y)` in raster order. The start pixel itself is
/// not checked.
///
/// # Arguments
///
/// * `pix` - 1-bpp binary image
/// * `start_x` - Starting x coordinate (exclusive: scan begins after this position)
/// * `start_y` - Starting y coordinate (exclusive: scan begins after this position)
///
/// # Returns
///
/// Coordinates of the next ON pixel, or None if none found
pub fn next_on_pixel_in_raster(pix: &Pix, start_x: u32, start_y: u32) -> Option<(u32, u32)> {
    if pix.depth() != PixelDepth::Bit1 {
        return None;
    }

    let w = pix.width();
    let h = pix.height();

    if start_x >= w || start_y >= h {
        return None;
    }

    // Scan from the pixel AFTER (start_x, start_y) in raster order
    let mut y = start_y;
    let mut x = start_x + 1;

    // Move to next row if past end of current row
    if x >= w {
        x = 0;
        y += 1;
    }

    // Scan remaining rows
    for sy in y..h {
        let x_start = if sy == y { x } else { 0 };
        for sx in x_start..w {
            if let Some(val) = pix.get_pixel(sx, sy)
                && val != 0
            {
                return Some((sx, sy));
            }
        }
    }

    None
}

/// Seedfill starting at a point with bounding box tracking
///
/// Performs a flood fill starting from the given point. Clears (sets to 0) all
/// ON pixels reachable from `(x, y)` via the specified connectivity, modifying
/// the image in-place. Returns the bounding box of all pixels that were cleared.
///
/// # Arguments
///
/// * `pix` - Mutable 1-bit binary image to fill
/// * `x` - Starting x coordinate
/// * `y` - Starting y coordinate
/// * `connectivity` - 4-way or 8-way connectivity
///
/// # Returns
///
/// Bounding box of the filled (cleared) region
///
/// # Errors
///
/// Returns an error if:
/// - The image is not 1-bit depth
/// - The coordinates are out of bounds
/// - The starting pixel is OFF
pub fn seedfill_bb(
    pix: &mut PixMut,
    x: u32,
    y: u32,
    connectivity: ConnectivityType,
) -> RegionResult<Box> {
    if pix.depth() != PixelDepth::Bit1 {
        return Err(RegionError::UnsupportedDepth {
            expected: "1-bit",
            actual: pix.depth().bits(),
        });
    }

    let w = pix.width();
    let h = pix.height();

    if x >= w || y >= h {
        return Err(RegionError::InvalidParameters(format!(
            "coordinates ({x}, {y}) out of bounds for {w}x{h} image"
        )));
    }

    // Check if starting pixel is ON
    if pix.get_pixel(x, y).unwrap_or(0) == 0 {
        return Err(RegionError::InvalidParameters(
            "starting pixel must be ON (non-zero)".into(),
        ));
    }

    // BFS flood fill while tracking bounding box
    let total = (w as usize).saturating_mul(h as usize);
    let mut filled = vec![false; total];
    let mut queue = std::collections::VecDeque::new();
    let mut min_x = x;
    let mut max_x = x;
    let mut min_y = y;
    let mut max_y = y;

    // Start from seed point
    let sidx = (y as usize) * (w as usize) + (x as usize);
    filled[sidx] = true;
    queue.push_back((x, y));

    while let Some((cx, cy)) = queue.pop_front() {
        // Clear this pixel in-place (flood fill removes pixels from image)
        let _ = pix.set_pixel(cx, cy, 0);

        // Update bounding box
        if cx < min_x {
            min_x = cx;
        }
        if cx > max_x {
            max_x = cx;
        }
        if cy < min_y {
            min_y = cy;
        }
        if cy > max_y {
            max_y = cy;
        }

        // Get neighbors based on connectivity
        let neighbors = match connectivity {
            ConnectivityType::FourWay => {
                let mut n = Vec::with_capacity(4);
                if cx > 0 {
                    n.push((cx - 1, cy));
                }
                if cx + 1 < w {
                    n.push((cx + 1, cy));
                }
                if cy > 0 {
                    n.push((cx, cy - 1));
                }
                if cy + 1 < h {
                    n.push((cx, cy + 1));
                }
                n
            }
            ConnectivityType::EightWay => {
                let mut n = Vec::with_capacity(8);
                for dy in -1i32..=1 {
                    for dx in -1i32..=1 {
                        if dx == 0 && dy == 0 {
                            continue;
                        }
                        let nx = cx as i32 + dx;
                        let ny = cy as i32 + dy;
                        if nx >= 0 && nx < w as i32 && ny >= 0 && ny < h as i32 {
                            n.push((nx as u32, ny as u32));
                        }
                    }
                }
                n
            }
        };

        // Add ON neighbors to queue
        for (nx, ny) in neighbors {
            let nidx = (ny as usize) * (w as usize) + (nx as usize);
            if !filled[nidx] && pix.get_pixel(nx, ny).unwrap_or(0) != 0 {
                filled[nidx] = true;
                queue.push_back((nx, ny));
            }
        }
    }

    Ok(Box {
        x: min_x as i32,
        y: min_y as i32,
        w: (max_x - min_x + 1) as i32,
        h: (max_y - min_y + 1) as i32,
    })
}

/// 4-connected seedfill with bounding box tracking
pub fn seedfill_4_bb(pix: &mut PixMut, x: u32, y: u32) -> RegionResult<Box> {
    seedfill_bb(pix, x, y, ConnectivityType::FourWay)
}

/// 8-connected seedfill with bounding box tracking
pub fn seedfill_8_bb(pix: &mut PixMut, x: u32, y: u32) -> RegionResult<Box> {
    seedfill_bb(pix, x, y, ConnectivityType::EightWay)
}

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

    fn create_test_image(width: u32, height: u32, pixels: &[(u32, u32)]) -> Pix {
        let pix = Pix::new(width, height, PixelDepth::Bit1).unwrap();
        let mut pix_mut = pix.try_into_mut().unwrap();

        for &(x, y) in pixels {
            let _ = pix_mut.set_pixel(x, y, 1);
        }

        pix_mut.into()
    }

    #[test]
    fn test_single_component_4way() {
        // Create a 2x2 square
        let pix = create_test_image(10, 10, &[(1, 1), (2, 1), (1, 2), (2, 2)]);

        let components = find_connected_components(&pix, ConnectivityType::FourWay).unwrap();

        assert_eq!(components.len(), 1);
        assert_eq!(components[0].pixel_count, 4);
        assert_eq!(components[0].bounds.x, 1);
        assert_eq!(components[0].bounds.y, 1);
        assert_eq!(components[0].bounds.w, 2);
        assert_eq!(components[0].bounds.h, 2);
    }

    #[test]
    fn test_two_separate_components() {
        // Create two separate 2x2 squares
        let pix = create_test_image(
            10,
            10,
            &[
                (0, 0),
                (1, 0),
                (0, 1),
                (1, 1), // First square
                (5, 5),
                (6, 5),
                (5, 6),
                (6, 6), // Second square
            ],
        );

        let components = find_connected_components(&pix, ConnectivityType::FourWay).unwrap();

        assert_eq!(components.len(), 2);
        assert!(components.iter().all(|c| c.pixel_count == 4));
    }

    #[test]
    fn test_diagonal_4way_vs_8way() {
        // Create two diagonally adjacent pixels
        let pix = create_test_image(10, 10, &[(0, 0), (1, 1)]);

        // 4-way should see them as separate
        let components_4 = find_connected_components(&pix, ConnectivityType::FourWay).unwrap();
        assert_eq!(components_4.len(), 2);

        // 8-way should see them as one component
        let components_8 = find_connected_components(&pix, ConnectivityType::EightWay).unwrap();
        assert_eq!(components_8.len(), 1);
        assert_eq!(components_8[0].pixel_count, 2);
    }

    #[test]
    fn test_empty_image() {
        let pix = create_test_image(10, 10, &[]);
        let components = find_connected_components(&pix, ConnectivityType::FourWay).unwrap();
        assert!(components.is_empty());
    }

    #[test]
    fn test_label_connected_components() {
        let pix = create_test_image(10, 10, &[(0, 0), (1, 0), (5, 5)]);

        let labeled = label_connected_components(&pix, ConnectivityType::FourWay).unwrap();

        assert_eq!(labeled.depth(), PixelDepth::Bit32);
        // (0,0) and (1,0) should have the same label
        let label_0_0 = labeled.get_pixel(0, 0).unwrap();
        let label_1_0 = labeled.get_pixel(1, 0).unwrap();
        let label_5_5 = labeled.get_pixel(5, 5).unwrap();

        assert!(label_0_0 > 0);
        assert_eq!(label_0_0, label_1_0);
        assert_ne!(label_0_0, label_5_5);
    }

    #[test]
    fn test_extract_component() {
        let pix = create_test_image(10, 10, &[(0, 0), (1, 0), (5, 5)]);
        let labeled = label_connected_components(&pix, ConnectivityType::FourWay).unwrap();

        let label = labeled.get_pixel(0, 0).unwrap();
        let extracted = extract_component(&labeled, label).unwrap();

        assert_eq!(extracted.depth(), PixelDepth::Bit1);
        assert_eq!(extracted.get_pixel(0, 0), Some(1));
        assert_eq!(extracted.get_pixel(1, 0), Some(1));
        assert_eq!(extracted.get_pixel(5, 5), Some(0));
    }

    #[test]
    fn test_filter_by_size() {
        // Create components of different sizes
        let pix = create_test_image(
            20,
            10,
            &[
                (0, 0), // 1 pixel
                (5, 0),
                (6, 0),
                (5, 1),
                (6, 1), // 4 pixels
                (10, 0),
                (11, 0),
                (12, 0),
                (10, 1),
                (11, 1),
                (12, 1),
                (10, 2),
                (11, 2),
                (12, 2), // 9 pixels
            ],
        );

        let labeled = label_connected_components(&pix, ConnectivityType::FourWay).unwrap();

        // Filter to keep only components with 2-5 pixels
        let filtered = filter_components_by_size(&labeled, 2, 5).unwrap();

        // Count non-zero pixels (should only be the 4-pixel component)
        let mut count = 0;
        for y in 0..10 {
            for x in 0..20 {
                if filtered.get_pixel(x, y).unwrap_or(0) > 0 {
                    count += 1;
                }
            }
        }
        assert_eq!(count, 4);
    }

    #[test]
    fn test_component_area_transform() {
        let pix = create_test_image(10, 10, &[(0, 0), (1, 0), (5, 5)]);
        let labeled = label_connected_components(&pix, ConnectivityType::FourWay).unwrap();
        let area_image = component_area_transform(&labeled).unwrap();

        // (0,0) and (1,0) form a 2-pixel component
        assert_eq!(area_image.get_pixel(0, 0), Some(2));
        assert_eq!(area_image.get_pixel(1, 0), Some(2));

        // (5,5) is a 1-pixel component
        assert_eq!(area_image.get_pixel(5, 5), Some(1));

        // Background should be 0
        assert_eq!(area_image.get_pixel(3, 3), Some(0));
    }

    #[test]
    fn test_unsupported_depth() {
        let pix = Pix::new(10, 10, PixelDepth::Bit8).unwrap();
        let result = find_connected_components(&pix, ConnectivityType::FourWay);
        assert!(result.is_err());
    }

    #[test]
    fn test_l_shaped_component() {
        // Create an L-shaped component
        let pix = create_test_image(10, 10, &[(0, 0), (0, 1), (0, 2), (1, 2), (2, 2)]);

        let components = find_connected_components(&pix, ConnectivityType::FourWay).unwrap();

        assert_eq!(components.len(), 1);
        assert_eq!(components[0].pixel_count, 5);
        assert_eq!(components[0].bounds.x, 0);
        assert_eq!(components[0].bounds.y, 0);
        assert_eq!(components[0].bounds.w, 3);
        assert_eq!(components[0].bounds.h, 3);
    }

    // -- Phase 3: ConnCompæ‹¡å¼µ tests --

    #[test]
    fn test_count_conn_comp_basic() {
        // Create a 10x10 image with two separate components
        let pix = create_test_image(
            10,
            10,
            &[
                (1, 1),
                (2, 1),
                (1, 2), // Component 1: 3 pixels
                (6, 6),
                (7, 6),
                (6, 7),
                (7, 7), // Component 2: 4 pixels
            ],
        );

        let count = count_conn_comp(&pix, ConnectivityType::FourWay).unwrap();
        assert_eq!(count, 2);
    }

    #[test]
    fn test_next_on_pixel_in_raster_basic() {
        // 5x5 image: pixels at (1,0), (3,2), (4,4)
        let pix = create_test_image(5, 5, &[(1, 0), (3, 2), (4, 4)]);

        // From origin, should find (1,0)
        let next = next_on_pixel_in_raster(&pix, 0, 0).unwrap();
        assert_eq!(next, (1, 0));

        // From (1,0), should find (3,2)
        let next = next_on_pixel_in_raster(&pix, 1, 0).unwrap();
        assert_eq!(next, (3, 2));

        // From (3,2), should find (4,4)
        let next = next_on_pixel_in_raster(&pix, 3, 2).unwrap();
        assert_eq!(next, (4, 4));

        // From (4,4), should find nothing
        let next = next_on_pixel_in_raster(&pix, 4, 4);
        assert!(next.is_none());
    }

    #[test]
    fn test_next_on_pixel_raster_order() {
        // Verify raster scan order (left-to-right, top-to-bottom)
        let pix = create_test_image(5, 5, &[(2, 1), (1, 2), (3, 1)]);

        // Should traverse in raster order: (1,2) comes after (3,1) in raster scan
        let next1 = next_on_pixel_in_raster(&pix, 0, 0).unwrap();
        assert_eq!(next1, (2, 1)); // First in raster order

        let next2 = next_on_pixel_in_raster(&pix, next1.0, next1.1).unwrap();
        assert_eq!(next2, (3, 1)); // Second in raster order (same row)

        let next3 = next_on_pixel_in_raster(&pix, next2.0, next2.1).unwrap();
        assert_eq!(next3, (1, 2)); // Third in raster order (next row)
    }

    #[test]
    fn test_seedfill_bb_basic() {
        // Create a 5x5 image with a square component at (1,1) to (3,3)
        let pix = create_test_image(
            5,
            5,
            &[
                (1, 1),
                (2, 1),
                (3, 1),
                (1, 2),
                (2, 2),
                (3, 2),
                (1, 3),
                (2, 3),
                (3, 3),
            ],
        );
        let mut pix_mut = pix.try_into_mut().unwrap();

        let bbox = seedfill_4_bb(&mut pix_mut, 2, 2).unwrap();

        // Bounding box should be (1,1) to (3,3)
        assert_eq!(bbox.x, 1);
        assert_eq!(bbox.y, 1);
        assert_eq!(bbox.w, 3);
        assert_eq!(bbox.h, 3);

        // All filled pixels should now be OFF (in-place modification)
        for y in 1..=3u32 {
            for x in 1..=3u32 {
                assert_eq!(
                    pix_mut.get_pixel(x, y),
                    Some(0),
                    "pixel ({x},{y}) should be OFF after seedfill"
                );
            }
        }
        // Pixels outside the component should remain OFF (were never ON)
        assert_eq!(pix_mut.get_pixel(0, 0), Some(0));
        assert_eq!(pix_mut.get_pixel(4, 4), Some(0));
    }

    #[test]
    fn test_seedfill_8_bb() {
        // Diagonal pixels should be connected in 8-way mode
        let pix = create_test_image(5, 5, &[(1, 1), (2, 2), (3, 3)]);
        let mut pix_mut = pix.try_into_mut().unwrap();

        let bbox = seedfill_8_bb(&mut pix_mut, 1, 1).unwrap();

        // Should cover all three pixels
        assert_eq!(bbox.x, 1);
        assert_eq!(bbox.y, 1);
        assert_eq!(bbox.w, 3);
        assert_eq!(bbox.h, 3);

        // All filled pixels should now be OFF (in-place modification)
        assert_eq!(pix_mut.get_pixel(1, 1), Some(0));
        assert_eq!(pix_mut.get_pixel(2, 2), Some(0));
        assert_eq!(pix_mut.get_pixel(3, 3), Some(0));
    }
}