leptonica 0.4.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
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
//! Structuring Element (SEL) for morphological operations
//!
//! A structuring element defines the neighborhood used in morphological operations.

use crate::core::{Pix, Pta};
use crate::morph::{MorphError, MorphResult};
use std::io::{BufRead, Write};
use std::path::Path;

/// Element type in a structuring element
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
#[derive(Default)]
pub enum SelElement {
    /// Don't care - this position is ignored
    #[default]
    DontCare = 0,
    /// Hit - must match foreground (set pixels)
    Hit = 1,
    /// Miss - must match background (unset pixels)
    Miss = 2,
}

/// Structuring Element (SEL)
///
/// Defines the neighborhood pattern for morphological operations.
/// The origin (cx, cy) is the reference point for the operation.
#[derive(Debug, Clone)]
pub struct Sel {
    /// Width of the SEL
    width: u32,
    /// Height of the SEL
    height: u32,
    /// X coordinate of the origin
    cx: u32,
    /// Y coordinate of the origin
    cy: u32,
    /// Element data (row-major order)
    data: Vec<SelElement>,
    /// Optional name for identification
    name: Option<String>,
}

impl Sel {
    /// Create a new empty structuring element
    pub fn new(width: u32, height: u32) -> MorphResult<Self> {
        if width == 0 || height == 0 {
            return Err(MorphError::InvalidSel(
                "width and height must be > 0".to_string(),
            ));
        }

        let size = (width * height) as usize;
        Ok(Sel {
            width,
            height,
            cx: width / 2,
            cy: height / 2,
            data: vec![SelElement::DontCare; size],
            name: None,
        })
    }

    /// Create a rectangular "brick" structuring element with all hits
    ///
    /// # Arguments
    /// * `width` - Width of the brick
    /// * `height` - Height of the brick
    pub fn create_brick(width: u32, height: u32) -> MorphResult<Self> {
        if width == 0 || height == 0 {
            return Err(MorphError::InvalidSel(
                "width and height must be > 0".to_string(),
            ));
        }

        let size = (width * height) as usize;
        Ok(Sel {
            width,
            height,
            cx: width / 2,
            cy: height / 2,
            data: vec![SelElement::Hit; size],
            name: Some(format!("brick_{}x{}", width, height)),
        })
    }

    /// Create a square structuring element with all hits
    pub fn create_square(size: u32) -> MorphResult<Self> {
        Self::create_brick(size, size)
    }

    /// Create a horizontal line structuring element
    pub fn create_horizontal(length: u32) -> MorphResult<Self> {
        Self::create_brick(length, 1)
    }

    /// Create a vertical line structuring element
    pub fn create_vertical(length: u32) -> MorphResult<Self> {
        Self::create_brick(1, length)
    }

    /// Create a horizontal comb structuring element
    ///
    /// A comb SEL has `factor2` hits spaced `factor1` apart in a row
    /// of width `factor1 * factor2`. Used for composite decomposition:
    /// `dilate(brick(f1)) then dilate(comb(f1, f2))` == `dilate(brick(f1*f2))`.
    ///
    /// C version: `sel1.c:452-487` `selCreateComb`
    pub fn create_comb_horizontal(factor1: u32, factor2: u32) -> MorphResult<Self> {
        if factor1 == 0 || factor2 == 0 {
            return Err(MorphError::InvalidSel("factors must be > 0".to_string()));
        }
        let size = factor1 * factor2;
        let mut sel = Self::new(size, 1)?;
        for i in 0..factor2 {
            let z = factor1 / 2 + i * factor1;
            sel.set_element(z, 0, SelElement::Hit);
        }
        sel.name = Some(format!("comb_{}h", size));
        Ok(sel)
    }

    /// Create a vertical comb structuring element
    ///
    /// Same as horizontal comb but oriented vertically.
    pub fn create_comb_vertical(factor1: u32, factor2: u32) -> MorphResult<Self> {
        if factor1 == 0 || factor2 == 0 {
            return Err(MorphError::InvalidSel("factors must be > 0".to_string()));
        }
        let size = factor1 * factor2;
        let mut sel = Self::new(1, size)?;
        for i in 0..factor2 {
            let z = factor1 / 2 + i * factor1;
            sel.set_element(0, z, SelElement::Hit);
        }
        sel.name = Some(format!("comb_{}v", size));
        Ok(sel)
    }

    /// Create a cross (+) structuring element
    pub fn create_cross(size: u32) -> MorphResult<Self> {
        if size == 0 {
            return Err(MorphError::InvalidSel("size must be > 0".to_string()));
        }

        let mut sel = Self::new(size, size)?;
        let center = size / 2;

        // Horizontal line
        for x in 0..size {
            sel.set_element(x, center, SelElement::Hit);
        }

        // Vertical line
        for y in 0..size {
            sel.set_element(center, y, SelElement::Hit);
        }

        sel.name = Some(format!("cross_{}", size));
        Ok(sel)
    }

    /// Create a diamond structuring element
    pub fn create_diamond(radius: u32) -> MorphResult<Self> {
        if radius == 0 {
            return Err(MorphError::InvalidSel("radius must be > 0".to_string()));
        }

        let size = 2 * radius + 1;
        let mut sel = Self::new(size, size)?;
        let center = radius as i32;

        for y in 0..size {
            for x in 0..size {
                let dx = (x as i32 - center).abs();
                let dy = (y as i32 - center).abs();
                if dx + dy <= radius as i32 {
                    sel.set_element(x, y, SelElement::Hit);
                }
            }
        }

        sel.name = Some(format!("diamond_{}", radius));
        Ok(sel)
    }

    /// Create a disk (approximate circle) structuring element
    pub fn create_disk(radius: u32) -> MorphResult<Self> {
        if radius == 0 {
            return Err(MorphError::InvalidSel("radius must be > 0".to_string()));
        }

        let size = 2 * radius + 1;
        let mut sel = Self::new(size, size)?;
        let center = radius as f32;
        let r_sq = (radius as f32 + 0.5).powi(2);

        for y in 0..size {
            for x in 0..size {
                let dx = x as f32 - center;
                let dy = y as f32 - center;
                if dx * dx + dy * dy <= r_sq {
                    sel.set_element(x, y, SelElement::Hit);
                }
            }
        }

        sel.name = Some(format!("disk_{}", radius));
        Ok(sel)
    }

    /// Create a structuring element from a string pattern
    ///
    /// # Arguments
    /// * `pattern` - String with 'x' for hit, 'o' for miss, '.' for don't care
    /// * `origin_x` - X coordinate of origin
    /// * `origin_y` - Y coordinate of origin
    ///
    /// # Example
    /// ```
    /// use leptonica::morph::Sel;
    ///
    /// let sel = Sel::from_string(
    ///     "xxx\n\
    ///      xox\n\
    ///      xxx",
    ///     1, 1
    /// ).unwrap();
    /// ```
    pub fn from_string(pattern: &str, origin_x: u32, origin_y: u32) -> MorphResult<Self> {
        let lines: Vec<&str> = pattern.lines().collect();
        if lines.is_empty() {
            return Err(MorphError::InvalidSel("empty pattern".to_string()));
        }

        let height = lines.len() as u32;
        let width = lines.iter().map(|l| l.chars().count()).max().unwrap_or(0) as u32;

        if width == 0 {
            return Err(MorphError::InvalidSel("empty pattern".to_string()));
        }

        let mut sel = Self::new(width, height)?;

        // Validate that the origin lies within the SEL bounds
        if origin_x >= width || origin_y >= height {
            return Err(MorphError::InvalidSel(format!(
                "origin ({}, {}) out of bounds for {}x{} SEL",
                origin_x, origin_y, width, height
            )));
        }

        sel.cx = origin_x;
        sel.cy = origin_y;

        for (y, line) in lines.iter().enumerate() {
            for (x, ch) in line.chars().enumerate() {
                let elem = match ch {
                    'x' | 'X' | '1' => SelElement::Hit,
                    'o' | 'O' | '0' => SelElement::Miss,
                    '.' | ' ' | '-' => SelElement::DontCare,
                    _ => continue,
                };
                sel.set_element(x as u32, y as u32, elem);
            }
        }

        Ok(sel)
    }

    /// Get the width
    #[inline]
    pub fn width(&self) -> u32 {
        self.width
    }

    /// Get the height
    #[inline]
    pub fn height(&self) -> u32 {
        self.height
    }

    /// Get the origin x coordinate
    #[inline]
    pub fn origin_x(&self) -> u32 {
        self.cx
    }

    /// Get the origin y coordinate
    #[inline]
    pub fn origin_y(&self) -> u32 {
        self.cy
    }

    /// Set the origin
    pub fn set_origin(&mut self, cx: u32, cy: u32) -> MorphResult<()> {
        if cx >= self.width || cy >= self.height {
            return Err(MorphError::InvalidSel(
                "origin must be within SEL bounds".to_string(),
            ));
        }
        self.cx = cx;
        self.cy = cy;
        Ok(())
    }

    /// Get the name
    pub fn name(&self) -> Option<&str> {
        self.name.as_deref()
    }

    /// Set the name
    pub fn set_name(&mut self, name: impl Into<String>) {
        self.name = Some(name.into());
    }

    /// Get an element at (x, y)
    #[inline]
    pub fn get_element(&self, x: u32, y: u32) -> Option<SelElement> {
        if x < self.width && y < self.height {
            Some(self.data[(y * self.width + x) as usize])
        } else {
            None
        }
    }

    /// Set an element at (x, y)
    #[inline]
    pub fn set_element(&mut self, x: u32, y: u32, elem: SelElement) {
        if x < self.width && y < self.height {
            self.data[(y * self.width + x) as usize] = elem;
        }
    }

    /// Get raw element data
    pub fn data(&self) -> &[SelElement] {
        &self.data
    }

    /// Count the number of hit elements
    pub fn hit_count(&self) -> usize {
        self.data.iter().filter(|&&e| e == SelElement::Hit).count()
    }

    /// Count the number of miss elements
    pub fn miss_count(&self) -> usize {
        self.data.iter().filter(|&&e| e == SelElement::Miss).count()
    }

    /// Create the reflected (180-degree rotated) SEL
    ///
    /// Used for correlation-based operations.
    pub fn reflect(&self) -> Self {
        let mut reflected = Sel {
            width: self.width,
            height: self.height,
            cx: self.width - 1 - self.cx,
            cy: self.height - 1 - self.cy,
            data: vec![SelElement::DontCare; self.data.len()],
            name: self.name.as_ref().map(|n| format!("{}_reflected", n)),
        };

        for y in 0..self.height {
            for x in 0..self.width {
                let src = self.data[(y * self.width + x) as usize];
                let dst_x = self.width - 1 - x;
                let dst_y = self.height - 1 - y;
                reflected.data[(dst_y * self.width + dst_x) as usize] = src;
            }
        }

        reflected
    }

    /// Rotate the SEL by 90 degrees orthogonally
    ///
    /// # Arguments
    /// * `rotation` - Number of 90-degree rotations (0-3)
    ///   - 0: no rotation
    ///   - 1: 90 degrees clockwise
    ///   - 2: 180 degrees
    ///   - 3: 270 degrees clockwise (90 degrees counter-clockwise)
    pub fn rotate_orth(&self, rotation: u32) -> Self {
        let rotation = rotation % 4;

        if rotation == 0 {
            return self.clone();
        }

        if rotation == 2 {
            return self.reflect();
        }

        // For 90 and 270 degree rotations, width and height swap
        let (new_width, new_height) = (self.height, self.width);

        let (new_cx, new_cy) = match rotation {
            1 => {
                // 90 degrees clockwise: (x, y) -> (height-1-y, x)
                // Origin: (cx, cy) -> (height-1-cy, cx)
                (self.height - 1 - self.cy, self.cx)
            }
            3 => {
                // 270 degrees clockwise: (x, y) -> (y, width-1-x)
                // Origin: (cx, cy) -> (cy, width-1-cx)
                (self.cy, self.width - 1 - self.cx)
            }
            _ => unreachable!(),
        };

        let mut rotated = Sel {
            width: new_width,
            height: new_height,
            cx: new_cx,
            cy: new_cy,
            data: vec![SelElement::DontCare; self.data.len()],
            name: self.name.as_ref().map(|n| format!("{}_rot{}", n, rotation)),
        };

        for y in 0..self.height {
            for x in 0..self.width {
                let src = self.data[(y * self.width + x) as usize];
                let (dst_x, dst_y) = match rotation {
                    1 => {
                        // 90 degrees clockwise
                        (self.height - 1 - y, x)
                    }
                    3 => {
                        // 270 degrees clockwise
                        (y, self.width - 1 - x)
                    }
                    _ => unreachable!(),
                };
                rotated.data[(dst_y * new_width + dst_x) as usize] = src;
            }
        }

        rotated
    }

    /// Iterate over hit positions relative to origin
    pub fn hit_offsets(&self) -> impl Iterator<Item = (i32, i32)> + '_ {
        let cx = self.cx as i32;
        let cy = self.cy as i32;
        let width = self.width;

        self.data
            .iter()
            .enumerate()
            .filter_map(move |(idx, &elem)| {
                if elem == SelElement::Hit {
                    let x = (idx as u32 % width) as i32;
                    let y = (idx as u32 / width) as i32;
                    Some((x - cx, y - cy))
                } else {
                    None
                }
            })
    }

    /// Iterate over miss positions relative to origin
    pub fn miss_offsets(&self) -> impl Iterator<Item = (i32, i32)> + '_ {
        let cx = self.cx as i32;
        let cy = self.cy as i32;
        let width = self.width;

        self.data
            .iter()
            .enumerate()
            .filter_map(move |(idx, &elem)| {
                if elem == SelElement::Miss {
                    let x = (idx as u32 % width) as i32;
                    let y = (idx as u32 / width) as i32;
                    Some((x - cx, y - cy))
                } else {
                    None
                }
            })
    }

    /// Find the maximum translations of hit elements relative to the origin.
    ///
    /// Returns `(xp, yp, xn, yn)` where each value is a non-negative
    /// translation *magnitude*:
    /// - `xp`: maximum positive x-translation (+x) required due to hits located
    ///   to the left of the origin (`cx - x`)
    /// - `yp`: maximum positive y-translation (+y) required due to hits located
    ///   above the origin (`cy - y`)
    /// - `xn`: maximum negative x-translation (−x) required due to hits located
    ///   to the right of the origin (`x - cx`)
    /// - `yn`: maximum negative y-translation (−y) required due to hits located
    ///   below the origin (`y - cy`)
    ///
    /// Used to determine the safe erosion/HMT application region.
    ///
    /// # See also
    ///
    /// C Leptonica: `selFindMaxTranslations()` in `sel1.c`
    pub fn find_max_translations(&self) -> (u32, u32, u32, u32) {
        let cx = self.cx as i32;
        let cy = self.cy as i32;
        let mut xp: i32 = 0;
        let mut yp: i32 = 0;
        let mut xn: i32 = 0;
        let mut yn: i32 = 0;

        for (idx, &elem) in self.data.iter().enumerate() {
            if elem == SelElement::Hit {
                let x = (idx as u32 % self.width) as i32;
                let y = (idx as u32 / self.width) as i32;
                xp = xp.max(cx - x);
                yp = yp.max(cy - y);
                xn = xn.max(x - cx);
                yn = yn.max(y - cy);
            }
        }

        (xp as u32, yp as u32, xn as u32, yn as u32)
    }

    /// Create a structuring element from a 1-bpp image.
    ///
    /// Each foreground pixel in the image becomes a Hit element.
    /// All other pixels become DontCare.
    ///
    /// # See also
    ///
    /// C Leptonica: `selCreateFromPix()` in `sel1.c`
    pub fn from_pix(pix: &Pix, cx: u32, cy: u32) -> MorphResult<Self> {
        if pix.depth() != crate::core::PixelDepth::Bit1 {
            return Err(MorphError::UnsupportedDepth {
                expected: "1 bpp",
                actual: pix.depth() as u32,
            });
        }

        let w = pix.width();
        let h = pix.height();
        let mut sel = Sel::new(w, h)?;
        sel.set_origin(cx, cy)?;

        for y in 0..h {
            for x in 0..w {
                if pix.get_pixel_unchecked(x, y) != 0 {
                    sel.set_element(x, y, SelElement::Hit);
                }
            }
        }

        Ok(sel)
    }

    /// Return SEL dimensions and origin as a tuple: (height, width, cy, cx).
    ///
    /// Based on C leptonica `selGetParameters`.
    pub fn get_parameters(&self) -> (u32, u32, u32, u32) {
        (self.height, self.width, self.cy, self.cx)
    }

    /// Return a human-readable string representation of the SEL.
    ///
    /// Each element is encoded as:
    /// - `x` / `X`: Hit (uppercase if at origin)
    /// - `o` / `O`: Miss (uppercase if at origin)
    /// - ` ` / `C`: DontCare (uppercase `C` if at origin)
    ///
    /// Based on C leptonica `selPrintToString`.
    pub fn print_to_string(&self) -> String {
        let mut s = String::with_capacity((self.height as usize) * (self.width as usize + 1));
        for y in 0..self.height {
            for x in 0..self.width {
                let is_center = x == self.cx && y == self.cy;
                let ch = match self.data[(y * self.width + x) as usize] {
                    SelElement::Hit => {
                        if is_center {
                            'X'
                        } else {
                            'x'
                        }
                    }
                    SelElement::Miss => {
                        if is_center {
                            'O'
                        } else {
                            'o'
                        }
                    }
                    SelElement::DontCare => {
                        if is_center {
                            'C'
                        } else {
                            ' '
                        }
                    }
                };
                s.push(ch);
            }
            s.push('\n');
        }
        s
    }

    /// Serialize the SEL to the Leptonica binary SEL file format (Version 1).
    ///
    /// Format:
    /// ```text
    ///   Sel Version 1
    ///   ------  <name>  ------
    ///   sy = <h>, sx = <w>, cy = <cy>, cx = <cx>
    ///     <row data: 0=dont_care, 1=hit, 2=miss>
    ///     ...
    /// ```
    ///
    /// Based on C leptonica `selWriteStream`.
    pub fn write_to_writer<W: Write>(&self, writer: &mut W) -> MorphResult<()> {
        let name = self.name.as_deref().unwrap_or("");
        let map_io = |e: std::io::Error| MorphError::InvalidParameters(e.to_string());
        writeln!(writer, "  Sel Version 1").map_err(map_io)?;
        writeln!(writer, "  ------  {}  ------", name).map_err(map_io)?;
        writeln!(
            writer,
            "  sy = {}, sx = {}, cy = {}, cx = {}",
            self.height, self.width, self.cy, self.cx
        )
        .map_err(map_io)?;
        for y in 0..self.height {
            write!(writer, "    ").map_err(map_io)?;
            for x in 0..self.width {
                let val = self.data[(y * self.width + x) as usize] as u8;
                write!(writer, "{}", val).map_err(map_io)?;
            }
            writeln!(writer).map_err(map_io)?;
        }
        writeln!(writer).map_err(map_io)?;
        Ok(())
    }

    /// Deserialize a SEL from the Leptonica binary SEL file format (Version 1).
    ///
    /// Leading blank lines before "Sel Version" are silently skipped, which
    /// allows reading individual SELs from a Sela file stream.
    ///
    /// Based on C leptonica `selReadStream`.
    pub fn read_from_reader<R: BufRead>(mut reader: R) -> MorphResult<Self> {
        let map_io = |e: std::io::Error| MorphError::InvalidParameters(e.to_string());

        // Line 1: "  Sel Version 1" (skip any preceding blank lines)
        let mut line = String::new();
        loop {
            line.clear();
            let bytes_read = reader.read_line(&mut line).map_err(map_io)?;
            if bytes_read == 0 {
                return Err(MorphError::InvalidParameters("unexpected EOF".into()));
            }
            if !line.trim().is_empty() {
                break;
            }
        }
        let version: u32 = line
            .trim()
            .strip_prefix("Sel Version")
            .ok_or_else(|| MorphError::InvalidParameters("not a sel file".into()))?
            .trim()
            .parse()
            .map_err(|_| MorphError::InvalidParameters("invalid version number".into()))?;
        if version != 1 {
            return Err(MorphError::InvalidParameters(format!(
                "invalid sel version: {}",
                version
            )));
        }

        // Line 2: "  ------  <name>  ------"
        line.clear();
        reader.read_line(&mut line).map_err(map_io)?;
        let name = {
            let trimmed = line.trim();
            let inner = trimmed
                .strip_prefix("------")
                .ok_or_else(|| MorphError::InvalidParameters("bad sel name line".into()))?
                .trim();
            inner
                .strip_suffix("------")
                .ok_or_else(|| MorphError::InvalidParameters("bad sel name line".into()))?
                .trim()
                .to_string()
        };

        // Line 3: "  sy = <h>, sx = <w>, cy = <cy>, cx = <cx>"
        line.clear();
        reader.read_line(&mut line).map_err(map_io)?;
        let (sy, sx, cy, cx) = Self::parse_dimensions(line.trim())?;

        let mut sel = Sel::new(sx, sy)?;
        sel.set_origin(cx, cy)?;
        if !name.is_empty() {
            sel.set_name(&name);
        }

        // Read sy rows of pixel data
        for y in 0..sy {
            line.clear();
            reader.read_line(&mut line).map_err(map_io)?;
            let row_data = line.trim();
            if row_data.len() != sx as usize {
                return Err(MorphError::InvalidParameters(format!(
                    "row {} length {} != expected {}",
                    y,
                    row_data.len(),
                    sx
                )));
            }
            for (x, ch) in row_data.chars().enumerate() {
                let elem = match ch {
                    '0' => SelElement::DontCare,
                    '1' => SelElement::Hit,
                    '2' => SelElement::Miss,
                    other => {
                        return Err(MorphError::InvalidParameters(format!(
                            "invalid sel element: {}",
                            other
                        )));
                    }
                };
                sel.set_element(x as u32, y, elem);
            }
        }

        Ok(sel)
    }

    fn parse_dimensions(line: &str) -> MorphResult<(u32, u32, u32, u32)> {
        let parse_kv = |s: &str, key: &str| -> MorphResult<u32> {
            s.trim()
                .strip_prefix(key)
                .ok_or_else(|| MorphError::InvalidParameters(format!("expected key '{}'", key)))?
                .trim()
                .parse()
                .map_err(|_| MorphError::InvalidParameters("invalid dimension value".into()))
        };
        let parts: Vec<&str> = line.splitn(4, ',').collect();
        if parts.len() != 4 {
            return Err(MorphError::InvalidParameters("bad dimensions line".into()));
        }
        let sy = parse_kv(parts[0], "sy =")?;
        let sx = parse_kv(parts[1], "sx =")?;
        let cy = parse_kv(parts[2], "cy =")?;
        let cx = parse_kv(parts[3], "cx =")?;
        Ok((sy, sx, cy, cx))
    }

    /// Create a SEL from a 32-bpp color image.
    ///
    /// Pixel color encoding:
    /// - Pure green (R=0, G>0, B=0): Hit
    /// - Pure red (R>0, G=0, B=0): Miss
    /// - White (R>0, G>0, B>0): DontCare
    /// - Any non-white, non-primary pixel: error
    ///
    /// The origin is set to the first non-white pixel found.
    /// Based on C leptonica `selCreateFromColorPix`.
    pub fn from_color_image(pix: &Pix, name: Option<&str>) -> MorphResult<Self> {
        use crate::core::PixelDepth;
        if pix.depth() != PixelDepth::Bit32 {
            return Err(MorphError::UnsupportedDepth {
                expected: "32-bpp color",
                actual: pix.depth().bits(),
            });
        }

        let w = pix.width();
        let h = pix.height();
        let mut sel = Sel::new(w, h)?;
        sel.set_origin(w / 2, h / 2)?;
        if let Some(n) = name {
            sel.set_name(n);
        }

        let mut num_origins = 0u32;
        let mut has_hits = false;

        for y in 0..h {
            for x in 0..w {
                let pixel = pix.get_pixel_unchecked(x, y);
                let (r, g, b, _) = crate::core::pixel::extract_rgba(pixel);

                // Non-white pixel (white is exactly 255,255,255) = first one sets the origin
                if !(r == 255 && g == 255 && b == 255) {
                    num_origins += 1;
                    if num_origins == 1 {
                        sel.set_origin(x, y)?;
                    }
                }

                if r == 0 && g > 0 && b == 0 {
                    has_hits = true;
                    sel.set_element(x, y, SelElement::Hit);
                } else if r > 0 && g == 0 && b == 0 {
                    sel.set_element(x, y, SelElement::Miss);
                } else if r > 0 && g > 0 && b > 0 {
                    sel.set_element(x, y, SelElement::DontCare);
                } else {
                    return Err(MorphError::InvalidParameters(format!(
                        "invalid pixel color at ({}, {}): r={}, g={}, b={}",
                        x, y, r, g, b
                    )));
                }
            }
        }

        if !has_hits {
            return Err(MorphError::InvalidParameters(
                "no hits found in color image".into(),
            ));
        }

        Ok(sel)
    }

    /// Create a SEL from a point array (PTA).
    ///
    /// Each point in the PTA becomes a Hit element.
    /// The SEL is sized to enclose all points (using their bounding box).
    ///
    /// # Arguments
    /// * `pta` - point array; all points must have non-negative coordinates
    /// * `cy` - y coordinate of the origin
    /// * `cx` - x coordinate of the origin
    /// * `name` - optional name for the SEL
    ///
    /// Based on C leptonica `selCreateFromPta`.
    pub fn from_pta(pta: &Pta, cy: u32, cx: u32, name: Option<&str>) -> MorphResult<Self> {
        if pta.is_empty() {
            return Err(MorphError::InvalidParameters("PTA is empty".into()));
        }

        // First pass: determine SEL width/height from the integer coordinates
        // returned by get_i_pt (which rounds, not truncates), so size and element
        // placement are consistent.
        let mut max_x: i32 = 0;
        let mut max_y: i32 = 0;
        for i in 0..pta.len() {
            let (x, y) = pta
                .get_i_pt(i)
                .ok_or_else(|| MorphError::InvalidParameters("PTA index out of bounds".into()))?;
            if x < 0 || y < 0 {
                return Err(MorphError::InvalidParameters(
                    "PTA point coordinates must be non-negative".into(),
                ));
            }
            if x > max_x {
                max_x = x;
            }
            if y > max_y {
                max_y = y;
            }
        }

        let w = max_x as u32 + 1;
        let h = max_y as u32 + 1;
        let mut sel = Sel::new(w, h)?;
        sel.set_origin(cx, cy)?;
        if let Some(n) = name {
            sel.set_name(n);
        }

        // Second pass: set hit elements using the same integer coordinates.
        for i in 0..pta.len() {
            let (x, y) = pta
                .get_i_pt(i)
                .ok_or_else(|| MorphError::InvalidParameters("PTA index out of bounds".into()))?;
            sel.set_element(x as u32, y as u32, SelElement::Hit);
        }

        Ok(sel)
    }
}

// ── Phase 6: Sela – ordered collection of Sel instances ────────────────────

/// An ordered, named collection of [`Sel`] structuring elements.
///
/// `Sela` mirrors the C leptonica `SELA` type and supports:
/// - Building up a set incrementally with [`add`](Sela::add)
/// - Indexed and name-based lookup
/// - Serialization/deserialization using the Leptonica text format
#[derive(Debug, Clone, Default)]
pub struct Sela {
    sels: Vec<Sel>,
}

impl Sela {
    /// Create an empty Sela.
    pub fn new() -> Self {
        Self { sels: Vec::new() }
    }

    /// Return the number of SELs in the collection.
    ///
    /// Based on C leptonica `selaGetCount`.
    pub fn count(&self) -> usize {
        self.sels.len()
    }

    /// Add a [`Sel`] to the collection.
    ///
    /// The SEL must have a name (see [`Sel::set_name`]).
    ///
    /// Based on C leptonica `selaAddSel`.
    pub fn add(&mut self, sel: Sel) -> MorphResult<()> {
        if sel.name().is_none() {
            return Err(MorphError::InvalidParameters(
                "SEL must have a name to be added to Sela".into(),
            ));
        }
        self.sels.push(sel);
        Ok(())
    }

    /// Return a reference to the SEL at the given index.
    ///
    /// Returns `None` if `index >= self.count()`.
    ///
    /// Based on C leptonica `selaGetSel`.
    pub fn get(&self, index: usize) -> Option<&Sel> {
        self.sels.get(index)
    }

    /// Find a SEL by its name.
    ///
    /// Returns `None` if no SEL with the given name exists.
    ///
    /// Based on C leptonica `selaFindSelByName`.
    pub fn find_by_name(&self, name: &str) -> Option<&Sel> {
        self.sels.iter().find(|s| s.name() == Some(name))
    }

    /// Deserialize a `Sela` from the Leptonica text format.
    ///
    /// File format (produced by `selaWrite` / [`write`](Sela::write)):
    /// ```text
    /// \nSela Version 1\nNumber of Sels = N\n\n
    /// <SEL 0>
    /// ...
    /// <SEL N-1>
    /// ```
    ///
    /// Based on C leptonica `selaRead` / `selaReadStream`.
    pub fn read<P: AsRef<Path>>(path: P) -> MorphResult<Self> {
        let file = std::fs::File::open(path.as_ref())
            .map_err(|e| MorphError::InvalidParameters(format!("cannot open sela file: {}", e)))?;
        Self::read_from_reader(std::io::BufReader::new(file))
    }

    /// Serialize this `Sela` to the Leptonica text format.
    ///
    /// Based on C leptonica `selaWrite` / `selaWriteStream`.
    pub fn write<P: AsRef<Path>>(&self, path: P) -> MorphResult<()> {
        let file = std::fs::File::create(path.as_ref()).map_err(|e| {
            MorphError::InvalidParameters(format!("cannot create sela file: {}", e))
        })?;
        self.write_to_writer(std::io::BufWriter::new(file))
    }

    /// Serialize to any [`Write`] sink.
    pub fn write_to_writer<W: Write>(&self, mut writer: W) -> MorphResult<()> {
        let map_io = |e: std::io::Error| MorphError::InvalidParameters(e.to_string());
        writeln!(writer, "\nSela Version 1").map_err(map_io)?;
        writeln!(writer, "Number of Sels = {}", self.sels.len()).map_err(map_io)?;
        writeln!(writer).map_err(map_io)?;
        for sel in &self.sels {
            sel.write_to_writer(&mut writer)?;
        }
        Ok(())
    }

    /// Deserialize from any [`BufRead`] source.
    pub fn read_from_reader<R: BufRead>(mut reader: R) -> MorphResult<Self> {
        let map_io = |e: std::io::Error| MorphError::InvalidParameters(e.to_string());

        // Skip optional leading blank line then read header
        let mut line = String::new();
        loop {
            line.clear();
            let bytes_read = reader.read_line(&mut line).map_err(map_io)?;
            if bytes_read == 0 {
                return Err(MorphError::InvalidParameters(
                    "unexpected EOF before Sela header".into(),
                ));
            }
            if !line.trim().is_empty() {
                break;
            }
        }
        // line now contains "Sela Version N"
        let version: u32 = line
            .trim()
            .strip_prefix("Sela Version")
            .ok_or_else(|| MorphError::InvalidParameters("not a sela file".into()))?
            .trim()
            .parse()
            .map_err(|_| MorphError::InvalidParameters("invalid sela version".into()))?;
        if version != 1 {
            return Err(MorphError::InvalidParameters(format!(
                "unsupported sela version: {}",
                version
            )));
        }

        line.clear();
        reader.read_line(&mut line).map_err(map_io)?;
        let n: usize = line
            .trim()
            .strip_prefix("Number of Sels =")
            .ok_or_else(|| MorphError::InvalidParameters("bad sela header".into()))?
            .trim()
            .parse()
            .map_err(|_| MorphError::InvalidParameters("invalid sel count".into()))?;

        let mut sela = Sela::new();
        for _ in 0..n {
            let sel = Sel::read_from_reader(&mut reader)?;
            sela.add(sel)?;
        }
        Ok(sela)
    }
}

/// Create a Sela from a Pixa of color-coded images.
///
/// Each Pix in the Pixa should be a 32-bpp color image representing a SEL
/// using the color encoding from [`Sel::from_color_image`].
/// The SArray provides names for each SEL.
///
/// # Arguments
/// * `pixa` - Collection of 32-bpp color images
/// * `sa` - Array of names, one per image
///
/// Based on C leptonica `selaCreateFromColorPixa`.
pub fn sela_create_from_color_pixa(
    pixa: &crate::core::Pixa,
    sa: &crate::core::Sarray,
) -> MorphResult<Sela> {
    if pixa.len() != sa.len() {
        return Err(MorphError::InvalidParameters(format!(
            "pixa length ({}) must match sarray length ({})",
            pixa.len(),
            sa.len()
        )));
    }

    let mut sela = Sela::new();
    for i in 0..pixa.len() {
        let pix = pixa.get(i).ok_or_else(|| {
            MorphError::InvalidParameters(format!("pixa index {} out of bounds", i))
        })?;
        let name = sa.get(i).ok_or_else(|| {
            MorphError::InvalidParameters(format!("sarray index {} out of bounds", i))
        })?;
        let sel = Sel::from_color_image(pix, Some(name))?;
        sela.add(sel)?;
    }

    Ok(sela)
}

// ── Phase 4: SEL-set generation free functions ─────────────────────────────

const BASIC_LINEAR: &[u32] = &[
    2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 20, 21, 25, 30, 31, 35, 40, 41, 45, 50, 51,
];

/// Build the "basic" SEL library (horizontal/vertical bricks, square bricks,
/// diagonal SELs).
///
/// Returns the same set as C leptonica `selaAddBasic`.
pub fn sela_add_basic() -> Vec<Sel> {
    let mut sels = Vec::new();

    // Linear horizontal bricks: 1×N with origin at (N/2, 0)
    for &size in BASIC_LINEAR {
        if let Ok(mut sel) = Sel::create_brick(size, 1) {
            sel.set_origin(size / 2, 0)
                .expect("horizontal SEL origin is within bounds");
            sel.set_name(format!("sel_{}h", size));
            sels.push(sel);
        }
    }

    // Linear vertical bricks: N×1 with origin at (0, N/2)
    for &size in BASIC_LINEAR {
        if let Ok(mut sel) = Sel::create_brick(1, size) {
            sel.set_origin(0, size / 2)
                .expect("vertical SEL origin is within bounds");
            sel.set_name(format!("sel_{}v", size));
            sels.push(sel);
        }
    }

    // Square 2-D bricks 2×2 – 5×5
    for i in 2u32..=5 {
        if let Ok(mut sel) = Sel::create_brick(i, i) {
            sel.set_origin(i / 2, i / 2)
                .expect("square SEL origin is within bounds");
            sel.set_name(format!("sel_{}", i));
            sels.push(sel);
        }
    }

    // Diagonal sel_2dp: 2×2, hits at (0,1) and (1,0), origin (0,0)
    // Pattern: . x / x .  (DontCare on diagonal, Hit off-diagonal)
    if let Ok(mut sel) = Sel::create_brick(2, 2) {
        sel.set_origin(0, 0)
            .expect("sel_2dp origin is within bounds");
        sel.set_element(0, 0, SelElement::DontCare);
        sel.set_element(1, 1, SelElement::DontCare);
        sel.set_name("sel_2dp");
        sels.push(sel);
    }

    // Diagonal sel_2dm: 2×2, hits at (0,0) and (1,1), origin (0,0)
    // Pattern: x . / . x
    if let Ok(mut sel) = Sel::create_brick(2, 2) {
        sel.set_origin(0, 0)
            .expect("sel_2dm origin is within bounds");
        sel.set_element(0, 1, SelElement::DontCare);
        sel.set_element(1, 0, SelElement::DontCare);
        sel.set_name("sel_2dm");
        sels.push(sel);
    }

    // sel_5dp: 5×5, positive-slope diagonal (top-right to bottom-left)
    // Hit positions: (0,4),(1,3),(2,2),(3,1),(4,0) with origin (2,2)
    if let Ok(mut sel) = Sel::new(5, 5) {
        sel.set_origin(2, 2)
            .expect("diagonal SEL origin is within bounds");
        for i in 0u32..5 {
            sel.set_element(4 - i, i, SelElement::Hit);
        }
        sel.set_name("sel_5dp");
        sels.push(sel);
    }

    // sel_5dm: 5×5, negative-slope diagonal (top-left to bottom-right)
    // Hit positions: (0,0),(1,1),(2,2),(3,3),(4,4) with origin (2,2)
    if let Ok(mut sel) = Sel::new(5, 5) {
        sel.set_origin(2, 2)
            .expect("diagonal SEL origin is within bounds");
        for i in 0u32..5 {
            sel.set_element(i, i, SelElement::Hit);
        }
        sel.set_name("sel_5dm");
        sels.push(sel);
    }

    sels
}

/// Build the hit-miss SEL library (isolated pixel, edge, corner, slanted-edge).
///
/// Returns the same set as C leptonica `selaAddHitMiss`.
pub fn sela_add_hit_miss() -> Vec<Sel> {
    let mut sels = Vec::new();

    // sel_3hm: isolated foreground pixel (3×3 all miss, center hit)
    if let Ok(mut sel) = Sel::create_brick(3, 3) {
        for y in 0u32..3 {
            for x in 0u32..3 {
                sel.set_element(x, y, SelElement::Miss);
            }
        }
        sel.set_element(1, 1, SelElement::Hit);
        sel.set_name("sel_3hm");
        sels.push(sel);
    }

    // sel_3de: top edge detector (3×2, origin cx=1 cy=0)
    // C: selCreateBrick(sy=2, sx=3, cy=0, cx=1, SEL_HIT)
    // Row 0: all Hit; Row 1: all Miss
    if let Ok(mut sel) = Sel::create_brick(3, 2) {
        sel.set_origin(1, 0)
            .expect("sel_3de origin is within bounds");
        sel.set_element(0, 1, SelElement::Miss);
        sel.set_element(1, 1, SelElement::Miss);
        sel.set_element(2, 1, SelElement::Miss);
        sel.set_name("sel_3de");
        sels.push(sel);
    }

    // sel_3ue: bottom edge detector (3×2, origin cx=1 cy=1)
    // C: selCreateBrick(sy=2, sx=3, cy=1, cx=1, SEL_HIT)
    // Row 0: all Miss; Row 1: all Hit
    if let Ok(mut sel) = Sel::create_brick(3, 2) {
        sel.set_origin(1, 1)
            .expect("sel_3ue origin is within bounds");
        sel.set_element(0, 0, SelElement::Miss);
        sel.set_element(1, 0, SelElement::Miss);
        sel.set_element(2, 0, SelElement::Miss);
        sel.set_name("sel_3ue");
        sels.push(sel);
    }

    // sel_3re: right edge detector (2×3, origin cx=0 cy=1)
    // C: selCreateBrick(sy=3, sx=2, cy=1, cx=0, SEL_HIT)
    // Col 0: all Hit; Col 1: all Miss
    if let Ok(mut sel) = Sel::create_brick(2, 3) {
        sel.set_origin(0, 1)
            .expect("sel_3re origin is within bounds");
        sel.set_element(1, 0, SelElement::Miss);
        sel.set_element(1, 1, SelElement::Miss);
        sel.set_element(1, 2, SelElement::Miss);
        sel.set_name("sel_3re");
        sels.push(sel);
    }

    // sel_3le: left edge detector (2×3, origin cx=1 cy=1)
    // C: selCreateBrick(sy=3, sx=2, cy=1, cx=1, SEL_HIT)
    // Col 0: all Miss; Col 1: all Hit
    if let Ok(mut sel) = Sel::create_brick(2, 3) {
        sel.set_origin(1, 1)
            .expect("sel_3le origin is within bounds");
        sel.set_element(0, 0, SelElement::Miss);
        sel.set_element(0, 1, SelElement::Miss);
        sel.set_element(0, 2, SelElement::Miss);
        sel.set_name("sel_3le");
        sels.push(sel);
    }

    // sel_sl1: slanted edge (width=6, height=13, origin cx=2 cy=6)
    // C: selCreateBrick(sy=13, sx=6, cy=6, cx=2, SEL_DONT_CARE)
    if let Ok(mut sel) = Sel::new(6, 13) {
        sel.set_origin(2, 6)
            .expect("sel_sl1 origin is within bounds");
        sel.set_element(3, 0, SelElement::Miss);
        sel.set_element(5, 0, SelElement::Hit);
        sel.set_element(2, 4, SelElement::Miss);
        sel.set_element(4, 4, SelElement::Hit);
        sel.set_element(1, 8, SelElement::Miss);
        sel.set_element(3, 8, SelElement::Hit);
        sel.set_element(0, 12, SelElement::Miss);
        sel.set_element(2, 12, SelElement::Hit);
        sel.set_name("sel_sl1");
        sels.push(sel);
    }

    // Corner detectors: 4×4 with mix of Hit/Miss/DontCare

    // sel_ulc: upper-left corner
    if let Ok(mut sel) = Sel::create_brick(4, 4) {
        sel.set_origin(1, 1)
            .expect("sel_ulc origin is within bounds");
        for y in 0u32..4 {
            for x in 0u32..4 {
                sel.set_element(x, y, SelElement::Miss);
            }
        }
        sel.set_element(1, 1, SelElement::DontCare);
        sel.set_element(2, 1, SelElement::DontCare);
        sel.set_element(1, 2, SelElement::DontCare);
        sel.set_element(3, 1, SelElement::Hit);
        sel.set_element(2, 2, SelElement::Hit);
        sel.set_element(3, 2, SelElement::Hit);
        sel.set_element(1, 3, SelElement::Hit);
        sel.set_element(2, 3, SelElement::Hit);
        sel.set_element(3, 3, SelElement::Hit);
        sel.set_name("sel_ulc");
        sels.push(sel);
    }

    // sel_urc: upper-right corner
    // C: selCreateBrick(sy=4, sx=4, cy=1, cx=2, SEL_MISS)
    if let Ok(mut sel) = Sel::create_brick(4, 4) {
        sel.set_origin(2, 1)
            .expect("sel_urc origin is within bounds");
        for y in 0u32..4 {
            for x in 0u32..4 {
                sel.set_element(x, y, SelElement::Miss);
            }
        }
        sel.set_element(1, 1, SelElement::DontCare);
        sel.set_element(2, 1, SelElement::DontCare);
        sel.set_element(2, 2, SelElement::DontCare);
        sel.set_element(0, 1, SelElement::Hit);
        sel.set_element(0, 2, SelElement::Hit);
        sel.set_element(1, 2, SelElement::Hit);
        sel.set_element(0, 3, SelElement::Hit);
        sel.set_element(1, 3, SelElement::Hit);
        sel.set_element(2, 3, SelElement::Hit);
        sel.set_name("sel_urc");
        sels.push(sel);
    }

    // sel_llc: lower-left corner
    // C: selCreateBrick(sy=4, sx=4, cy=2, cx=1, SEL_MISS)
    // Hits at (row,col): (0,1)(0,2)(0,3)(1,2)(1,3)(2,3) [C notation]
    // → set_element(col, row, val) in Rust API
    if let Ok(mut sel) = Sel::create_brick(4, 4) {
        sel.set_origin(1, 2)
            .expect("sel_llc origin is within bounds");
        for y in 0u32..4 {
            for x in 0u32..4 {
                sel.set_element(x, y, SelElement::Miss);
            }
        }
        sel.set_element(1, 1, SelElement::DontCare);
        sel.set_element(1, 2, SelElement::DontCare);
        sel.set_element(2, 2, SelElement::DontCare);
        sel.set_element(1, 0, SelElement::Hit);
        sel.set_element(2, 0, SelElement::Hit);
        sel.set_element(3, 0, SelElement::Hit);
        sel.set_element(2, 1, SelElement::Hit);
        sel.set_element(3, 1, SelElement::Hit);
        sel.set_element(3, 2, SelElement::Hit);
        sel.set_name("sel_llc");
        sels.push(sel);
    }

    // sel_lrc: lower-right corner
    if let Ok(mut sel) = Sel::create_brick(4, 4) {
        sel.set_origin(2, 2)
            .expect("sel_lrc origin is within bounds");
        for y in 0u32..4 {
            for x in 0u32..4 {
                sel.set_element(x, y, SelElement::Miss);
            }
        }
        sel.set_element(2, 1, SelElement::DontCare);
        sel.set_element(1, 2, SelElement::DontCare);
        sel.set_element(2, 2, SelElement::DontCare);
        sel.set_element(0, 0, SelElement::Hit);
        sel.set_element(1, 0, SelElement::Hit);
        sel.set_element(2, 0, SelElement::Hit);
        sel.set_element(0, 1, SelElement::Hit);
        sel.set_element(1, 1, SelElement::Hit);
        sel.set_element(0, 2, SelElement::Hit);
        sel.set_name("sel_lrc");
        sels.push(sel);
    }

    sels
}

/// Build linear SELs for sizes 2–63 (horizontal and vertical).
///
/// Returns the same set as C leptonica `selaAddDwaLinear`.
pub fn sela_add_dwa_linear() -> Vec<Sel> {
    let mut sels = Vec::new();

    for i in 2u32..64 {
        if let Ok(mut sel) = Sel::create_brick(i, 1) {
            sel.set_origin(i / 2, 0)
                .expect("horizontal SEL origin is within bounds");
            sel.set_name(format!("sel_{}h", i));
            sels.push(sel);
        }
    }
    for i in 2u32..64 {
        if let Ok(mut sel) = Sel::create_brick(1, i) {
            sel.set_origin(0, i / 2)
                .expect("vertical SEL origin is within bounds");
            sel.set_name(format!("sel_{}v", i));
            sels.push(sel);
        }
    }

    sels
}

/// Build comb SELs for DWA composite operations (sizes 4–63).
///
/// For each composable size that can be factored as `f1 * f2`,
/// generates a horizontal comb and a vertical comb SEL.
///
/// Returns the same set as C leptonica `selaAddDwaCombs`.
pub fn sela_add_dwa_combs() -> Vec<Sel> {
    use crate::morph::binary::select_composable_sizes;

    let mut sels = Vec::new();
    let mut prev_size = 0u32;

    for i in 4u32..64 {
        let (f1, f2) = select_composable_sizes(i);
        let size = f1 * f2;
        if size == prev_size {
            continue;
        }
        if let Ok(mut sel) = Sel::create_comb_horizontal(f1, f2) {
            sel.set_name(format!("sel_comb_{}h", size));
            sels.push(sel);
        }
        if let Ok(mut sel) = Sel::create_comb_vertical(f1, f2) {
            sel.set_name(format!("sel_comb_{}v", size));
            sels.push(sel);
        }
        prev_size = size;
    }

    sels
}

/// Generate pixel positions along a line from a center point.
///
/// Starting from `(xc, yc)`, extends `length` pixels in direction `radang` (radians).
fn line_points_from_pt(xc: i32, yc: i32, length: f64, radang: f64) -> Vec<(i32, i32)> {
    let n = length.ceil() as i32;
    (1..=n)
        .map(|k| {
            let x = xc + (k as f64 * radang.cos()).round() as i32;
            let y = yc + (k as f64 * radang.sin()).round() as i32;
            (x, y)
        })
        .collect()
}

/// Build hit-miss SELs for detecting cross (X) junctions of two lines.
///
/// # Arguments
/// * `hlsize` - Length of each hit arm from the origin
/// * `mdist`  - Distance of miss elements from the origin
/// * `norient` - Number of orientations (1–8); generates `norient` SELs
///
/// Based on C leptonica `selaAddCrossJunctions`.
pub fn sela_add_cross_junctions(hlsize: f32, mdist: f32, norient: u32) -> MorphResult<Vec<Sel>> {
    if hlsize <= 0.0 {
        return Err(MorphError::InvalidParameters(
            "hlsize must be > 0".to_string(),
        ));
    }
    if mdist <= 0.0 {
        return Err(MorphError::InvalidParameters(
            "mdist must be > 0".to_string(),
        ));
    }
    if norient == 0 || norient > 8 {
        return Err(MorphError::InvalidParameters(
            "norient must be in [1, 8]".to_string(),
        ));
    }

    let half_pi = std::f64::consts::FRAC_PI_2;
    let rad_incr = half_pi / norient as f64;
    let w_f = 2.2 * (hlsize.max(mdist) as f64 + 0.5);
    let w = if (w_f as u32).is_multiple_of(2) {
        w_f as u32 + 1
    } else {
        w_f as u32
    };
    let xc = (w / 2) as i32;
    let yc = (w / 2) as i32;

    let mut sels = Vec::with_capacity(norient as usize);
    for i in 0..norient {
        let mut sel = Sel::new(w, w)?;
        sel.set_origin(xc as u32, yc as u32)?;
        let rad = i as f64 * rad_incr;

        // Four arms of hits (cross shape)
        for &angle_off in &[0.0, half_pi, std::f64::consts::PI, 3.0 * half_pi] {
            for (px, py) in line_points_from_pt(xc, yc, (hlsize + 1.0) as f64, rad + angle_off) {
                if px >= 0 && py >= 0 && (px as u32) < w && (py as u32) < w {
                    sel.set_element(px as u32, py as u32, SelElement::Hit);
                }
            }
        }
        sel.set_element(xc as u32, yc as u32, SelElement::Hit); // origin itself

        // Four miss elements between arms
        for j in 0..4i32 {
            let angle = rad + (j as f64 - 0.5) * half_pi;
            let mx = xc + (mdist as f64 * angle.cos()).round() as i32;
            let my = yc + (mdist as f64 * angle.sin()).round() as i32;
            if mx >= 0 && my >= 0 && (mx as u32) < w && (my as u32) < w {
                // Only set to Miss if not already Hit
                if sel.get_element(mx as u32, my as u32) != Some(SelElement::Hit) {
                    sel.set_element(mx as u32, my as u32, SelElement::Miss);
                }
            }
        }

        sel.set_name(format!("sel_cross_{}", i));
        sels.push(sel);
    }

    Ok(sels)
}

/// Build hit-miss SELs for detecting T-junctions of two lines.
///
/// # Arguments
/// * `hlsize` - Length of each hit arm from the origin
/// * `mdist`  - Distance of miss elements from the origin
/// * `norient` - Number of orientations (1–8); generates `4 * norient` SELs
///
/// Based on C leptonica `selaAddTJunctions`.
pub fn sela_add_t_junctions(hlsize: f32, mdist: f32, norient: u32) -> MorphResult<Vec<Sel>> {
    if hlsize <= 2.0 {
        return Err(MorphError::InvalidParameters(
            "hlsize must be > 2".to_string(),
        ));
    }
    if mdist <= 0.0 {
        return Err(MorphError::InvalidParameters(
            "mdist must be > 0".to_string(),
        ));
    }
    if norient == 0 || norient > 8 {
        return Err(MorphError::InvalidParameters(
            "norient must be in [1, 8]".to_string(),
        ));
    }

    let half_pi = std::f64::consts::FRAC_PI_2;
    let rad_incr = half_pi / norient as f64;
    let w_f = 2.4 * (hlsize.max(mdist) as f64 + 0.5);
    let w = if (w_f as u32).is_multiple_of(2) {
        w_f as u32 + 1
    } else {
        w_f as u32
    };
    let xc = (w / 2) as i32;
    let yc = (w / 2) as i32;

    let mut sels = Vec::with_capacity(4 * norient as usize);
    for i in 0..norient {
        let rad = i as f64 * rad_incr;
        for j in 0..4u32 {
            let j_ang = j as f64 * half_pi;
            let mut sel = Sel::new(w, w)?;
            sel.set_origin(xc as u32, yc as u32)?;

            // Three arms of hits (T shape): straight + two half-perpendiculars
            for (arm_rad, len_mult) in [
                (j_ang + rad, hlsize as f64 + 1.0),
                (j_ang + rad + half_pi, hlsize as f64 / 2.0 + 1.0),
                (j_ang + rad - half_pi, hlsize as f64 / 2.0 + 1.0),
            ] {
                for (px, py) in line_points_from_pt(xc, yc, len_mult, arm_rad) {
                    if px >= 0 && py >= 0 && (px as u32) < w && (py as u32) < w {
                        sel.set_element(px as u32, py as u32, SelElement::Hit);
                    }
                }
            }
            sel.set_element(xc as u32, yc as u32, SelElement::Hit); // origin

            // Three miss elements (opposite to arms)
            for (k, &dist) in [mdist as f64, mdist as f64 / 2.0, mdist as f64 / 2.0]
                .iter()
                .enumerate()
            {
                let angle = j_ang + rad + std::f64::consts::PI + (k as f64 - 1.0) * half_pi;
                let mx = xc + (dist * angle.cos()).round() as i32;
                let my = yc + (dist * angle.sin()).round() as i32;
                if mx >= 0
                    && my >= 0
                    && (mx as u32) < w
                    && (my as u32) < w
                    && sel.get_element(mx as u32, my as u32) != Some(SelElement::Hit)
                {
                    sel.set_element(mx as u32, my as u32, SelElement::Miss);
                }
            }

            sel.set_name(format!("sel_t_{}_{}", i, j));
            sels.push(sel);
        }
    }

    Ok(sels)
}

/// Create a plus-sign (+) SEL of the given size and line width.
///
/// # Arguments
/// * `size`      - Side of the bounding square (must be >= 3)
/// * `linewidth` - Width of the horizontal/vertical arms
///
/// Based on C leptonica `selMakePlusSign`.
pub fn sel_make_plus_sign(size: u32, linewidth: u32) -> MorphResult<Sel> {
    if size < 3 {
        return Err(MorphError::InvalidParameters(
            "size must be >= 3".to_string(),
        ));
    }
    if linewidth == 0 || linewidth > size {
        return Err(MorphError::InvalidParameters(
            "linewidth must be between 1 and size".to_string(),
        ));
    }

    let mut sel = Sel::new(size, size)?;
    sel.set_origin(size / 2, size / 2)?;
    sel.set_name("plus_sign");

    let cx = size / 2;
    let cy = size / 2;
    let half = linewidth / 2;

    // Horizontal arm
    for x in 0..size {
        for dy in 0..linewidth {
            let y = cy.saturating_sub(half) + dy;
            if y < size {
                sel.set_element(x, y, SelElement::Hit);
            }
        }
    }
    // Vertical arm
    for y in 0..size {
        for dx in 0..linewidth {
            let x = cx.saturating_sub(half) + dx;
            if x < size {
                sel.set_element(x, y, SelElement::Hit);
            }
        }
    }

    Ok(sel)
}

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

    #[test]
    fn test_create_brick() {
        let sel = Sel::create_brick(3, 3).unwrap();
        assert_eq!(sel.width(), 3);
        assert_eq!(sel.height(), 3);
        assert_eq!(sel.origin_x(), 1);
        assert_eq!(sel.origin_y(), 1);
        assert_eq!(sel.hit_count(), 9);
    }

    #[test]
    fn test_create_cross() {
        let sel = Sel::create_cross(3).unwrap();
        assert_eq!(sel.hit_count(), 5); // 3 horizontal + 3 vertical - 1 center
    }

    #[test]
    fn test_create_diamond() {
        let sel = Sel::create_diamond(1).unwrap();
        // Diamond with radius 1: 3x3 with 5 hits
        //  .x.
        //  xxx
        //  .x.
        assert_eq!(sel.hit_count(), 5);
    }

    #[test]
    fn test_from_string() {
        let sel = Sel::from_string(
            "xxx\n\
             xox\n\
             xxx",
            1,
            1,
        )
        .unwrap();

        assert_eq!(sel.width(), 3);
        assert_eq!(sel.height(), 3);
        assert_eq!(sel.hit_count(), 8);
        assert_eq!(sel.miss_count(), 1);
        assert_eq!(sel.get_element(1, 1), Some(SelElement::Miss));
    }

    #[test]
    fn test_reflect() {
        let sel = Sel::from_string("xx.\n...", 0, 0).unwrap();

        let reflected = sel.reflect();
        assert_eq!(reflected.get_element(0, 0), Some(SelElement::DontCare));
        assert_eq!(reflected.get_element(2, 1), Some(SelElement::Hit));
        assert_eq!(reflected.get_element(1, 1), Some(SelElement::Hit));
    }

    #[test]
    fn test_hit_offsets() {
        let sel = Sel::create_brick(3, 3).unwrap();
        let offsets: Vec<_> = sel.hit_offsets().collect();

        assert_eq!(offsets.len(), 9);
        assert!(offsets.contains(&(-1, -1)));
        assert!(offsets.contains(&(0, 0)));
        assert!(offsets.contains(&(1, 1)));
    }

    #[test]
    fn test_rotate_orth_identity() {
        let sel = Sel::from_string("xx.\n...", 0, 0).unwrap();
        let rotated = sel.rotate_orth(0);

        assert_eq!(rotated.width(), sel.width());
        assert_eq!(rotated.height(), sel.height());
        assert_eq!(rotated.origin_x(), sel.origin_x());
        assert_eq!(rotated.origin_y(), sel.origin_y());
    }

    #[test]
    fn test_rotate_orth_90() {
        // Original (3x2):
        // xx.   (y=0)
        // ...   (y=1)
        // Origin at (0, 0)
        let sel = Sel::from_string("xx.\n...", 0, 0).unwrap();
        let rotated = sel.rotate_orth(1);

        // After 90 degree rotation (2x3):
        // Width and height swap
        assert_eq!(rotated.width(), 2);
        assert_eq!(rotated.height(), 3);

        // Check the pattern:
        // 90 clockwise: (x, y) -> (height-1-y, x)
        // (0,0) -> (1, 0) - hit
        // (1,0) -> (1, 1) - hit
        // (2,0) -> (1, 2) - don't care
        // (0,1) -> (0, 0) - don't care
        // (1,1) -> (0, 1) - don't care
        // (2,1) -> (0, 2) - don't care
        assert_eq!(rotated.get_element(1, 0), Some(SelElement::Hit));
        assert_eq!(rotated.get_element(1, 1), Some(SelElement::Hit));
        assert_eq!(rotated.get_element(1, 2), Some(SelElement::DontCare));
        assert_eq!(rotated.get_element(0, 0), Some(SelElement::DontCare));
    }

    #[test]
    fn test_rotate_orth_180() {
        let sel = Sel::from_string("xx.\n...", 0, 0).unwrap();
        let rotated = sel.rotate_orth(2);

        // 180 degrees is same as reflect
        let reflected = sel.reflect();
        assert_eq!(rotated.width(), reflected.width());
        assert_eq!(rotated.height(), reflected.height());
        for y in 0..rotated.height() {
            for x in 0..rotated.width() {
                assert_eq!(rotated.get_element(x, y), reflected.get_element(x, y));
            }
        }
    }

    #[test]
    fn test_rotate_orth_360() {
        let sel = Sel::from_string("xx.\n...", 0, 0).unwrap();

        // Four rotations should return to original
        let rotated = sel.rotate_orth(4);
        assert_eq!(rotated.width(), sel.width());
        assert_eq!(rotated.height(), sel.height());
        for y in 0..sel.height() {
            for x in 0..sel.width() {
                assert_eq!(rotated.get_element(x, y), sel.get_element(x, y));
            }
        }
    }

    #[test]
    fn test_create_comb_horizontal_3x3() {
        // comb(f1=3, f2=3): size=9, hits at columns 1, 4, 7
        let sel = Sel::create_comb_horizontal(3, 3).unwrap();
        assert_eq!(sel.width(), 9);
        assert_eq!(sel.height(), 1);
        assert_eq!(sel.origin_x(), 4); // 9 / 2
        assert_eq!(sel.origin_y(), 0);
        assert_eq!(sel.hit_count(), 3);
        // Verify hit positions: f1/2 + i*f1 for i in 0..f2
        assert_eq!(sel.get_element(1, 0), Some(SelElement::Hit));
        assert_eq!(sel.get_element(4, 0), Some(SelElement::Hit));
        assert_eq!(sel.get_element(7, 0), Some(SelElement::Hit));
        // Non-hit positions should be DontCare
        assert_eq!(sel.get_element(0, 0), Some(SelElement::DontCare));
        assert_eq!(sel.get_element(2, 0), Some(SelElement::DontCare));
        assert_eq!(sel.get_element(3, 0), Some(SelElement::DontCare));
    }

    #[test]
    fn test_create_comb_horizontal_2x2() {
        // comb(f1=2, f2=2): size=4, hits at columns 1, 3
        let sel = Sel::create_comb_horizontal(2, 2).unwrap();
        assert_eq!(sel.width(), 4);
        assert_eq!(sel.height(), 1);
        assert_eq!(sel.hit_count(), 2);
        assert_eq!(sel.get_element(1, 0), Some(SelElement::Hit));
        assert_eq!(sel.get_element(3, 0), Some(SelElement::Hit));
    }

    #[test]
    fn test_create_comb_vertical_3x4() {
        // comb(f1=3, f2=4): size=12, hits at rows 1, 4, 7, 10
        let sel = Sel::create_comb_vertical(3, 4).unwrap();
        assert_eq!(sel.width(), 1);
        assert_eq!(sel.height(), 12);
        assert_eq!(sel.origin_x(), 0);
        assert_eq!(sel.origin_y(), 6); // 12 / 2
        assert_eq!(sel.hit_count(), 4);
        assert_eq!(sel.get_element(0, 1), Some(SelElement::Hit));
        assert_eq!(sel.get_element(0, 4), Some(SelElement::Hit));
        assert_eq!(sel.get_element(0, 7), Some(SelElement::Hit));
        assert_eq!(sel.get_element(0, 10), Some(SelElement::Hit));
    }

    // --- Phase 3: SEL I/O and extended creation ---

    fn make_hit_miss_sel() -> Sel {
        // 3x3: center=Hit, corners=Miss, edges=DontCare
        let mut sel = Sel::new(3, 3).unwrap();
        sel.set_origin(1, 1).unwrap();
        sel.set_element(1, 1, SelElement::Hit);
        sel.set_element(0, 0, SelElement::Miss);
        sel.set_element(2, 0, SelElement::Miss);
        sel.set_element(0, 2, SelElement::Miss);
        sel.set_element(2, 2, SelElement::Miss);
        sel.set_name("test_sel");
        sel
    }

    #[test]
    fn test_get_parameters_returns_height_width_cy_cx() {
        let sel = make_hit_miss_sel();
        let (sy, sx, cy, cx) = sel.get_parameters();
        assert_eq!(sy, 3);
        assert_eq!(sx, 3);
        assert_eq!(cy, 1);
        assert_eq!(cx, 1);
    }

    #[test]
    fn test_get_parameters_brick() {
        let sel = Sel::create_brick(5, 3).unwrap();
        let (sy, sx, cy, cx) = sel.get_parameters();
        assert_eq!(sy, 3); // height
        assert_eq!(sx, 5); // width
        assert_eq!(cy, 1); // height/2
        assert_eq!(cx, 2); // width/2
    }

    #[test]
    fn test_print_to_string_encodes_elements() {
        let sel = make_hit_miss_sel();
        let s = sel.print_to_string();
        // Should have height rows each ending with '\n'
        let lines: Vec<&str> = s.lines().collect();
        assert_eq!(lines.len(), 3);
        // Center (1,1) is the origin, element is Hit → 'X'
        assert!(lines[1].contains('X'));
        // Corners are Miss → 'o'
        assert!(lines[0].starts_with('o'));
    }

    #[test]
    fn test_print_to_string_dont_care_is_space() {
        let sel = Sel::create_brick(1, 1).unwrap();
        // 1x1 with Hit at origin
        let s = sel.print_to_string();
        // Single cell, hit at origin → 'X'
        assert_eq!(s.trim(), "X");
    }

    #[test]
    fn test_write_read_roundtrip() {
        let original = make_hit_miss_sel();
        let mut buf = Vec::new();
        original.write_to_writer(&mut buf).unwrap();
        let text = std::str::from_utf8(&buf).unwrap();
        assert!(text.contains("Sel Version 1"));
        assert!(text.contains("test_sel"));

        let restored = Sel::read_from_reader(std::io::BufReader::new(buf.as_slice())).unwrap();
        assert_eq!(restored.width(), original.width());
        assert_eq!(restored.height(), original.height());
        assert_eq!(restored.origin_x(), original.origin_x());
        assert_eq!(restored.origin_y(), original.origin_y());
        assert_eq!(restored.name(), original.name());
        for y in 0..original.height() {
            for x in 0..original.width() {
                assert_eq!(restored.get_element(x, y), original.get_element(x, y));
            }
        }
    }

    #[test]
    fn test_write_format_matches_leptonica() {
        let sel = Sel::create_brick(3, 3).unwrap();
        let mut buf = Vec::new();
        sel.write_to_writer(&mut buf).unwrap();
        let text = std::str::from_utf8(&buf).unwrap();
        // Check format header
        assert!(text.contains("  Sel Version 1\n"));
        assert!(text.contains("  sy = 3, sx = 3, cy = 1, cx = 1\n"));
        // Brick: all 1 (Hit), each row "    111\n"
        assert!(text.contains("    111\n"));
    }

    #[test]
    fn test_read_from_reader_invalid_version_errors() {
        let data =
            b"  Sel Version 99\n  ------  foo  ------\n  sy = 1, sx = 1, cy = 0, cx = 0\n    1\n\n";
        let result = Sel::read_from_reader(std::io::BufReader::new(data.as_slice()));
        assert!(result.is_err());
    }

    #[test]
    fn test_from_color_image_green_is_hit_red_is_miss() {
        use crate::core::{Pix, PixelDepth};
        // 3x1 image: green, red, white
        let pix = Pix::new(3, 1, PixelDepth::Bit32).unwrap();
        let mut pm = pix.try_into_mut().unwrap();
        // Green pixel at (0,0) → Hit
        pm.set_pixel_unchecked(0, 0, crate::core::pixel::compose_rgba(0, 255, 0, 0));
        // Red pixel at (1,0) → Miss
        pm.set_pixel_unchecked(1, 0, crate::core::pixel::compose_rgba(255, 0, 0, 0));
        // White pixel at (2,0) → DontCare
        pm.set_pixel_unchecked(2, 0, crate::core::pixel::compose_rgba(255, 255, 255, 0));
        let pix: Pix = pm.into();

        let sel = Sel::from_color_image(&pix, Some("test")).unwrap();
        assert_eq!(sel.get_element(0, 0), Some(SelElement::Hit));
        assert_eq!(sel.get_element(1, 0), Some(SelElement::Miss));
        assert_eq!(sel.get_element(2, 0), Some(SelElement::DontCare));
    }

    #[test]
    fn test_from_color_image_requires_32bpp() {
        use crate::core::{Pix, PixelDepth};
        let pix = Pix::new(3, 3, PixelDepth::Bit8).unwrap();
        assert!(Sel::from_color_image(&pix, None).is_err());
    }

    #[test]
    fn test_from_pta_creates_hit_at_each_point() {
        use crate::core::Pta;
        let mut pta = Pta::new();
        pta.push(2.0, 1.0); // (x=2, y=1)
        pta.push(3.0, 2.0); // (x=3, y=2)
        let sel = Sel::from_pta(&pta, 0, 0, Some("pta_test")).unwrap();
        assert_eq!(sel.get_element(2, 1), Some(SelElement::Hit));
        assert_eq!(sel.get_element(3, 2), Some(SelElement::Hit));
        assert_eq!(sel.name(), Some("pta_test"));
    }

    #[test]
    fn test_from_pta_origin_and_dimensions() {
        use crate::core::Pta;
        let mut pta = Pta::new();
        pta.push(1.0, 0.0);
        pta.push(2.0, 1.0);
        // bounding box: x in [1,2], y in [0,1] → w=2, h=2 → sel is (x+w=4) x (y+h=2)
        let sel = Sel::from_pta(&pta, 1, 2, None).unwrap();
        assert_eq!(sel.origin_y(), 1);
        assert_eq!(sel.origin_x(), 2);
    }

    // ── Phase 4: SEL-set generation tests ────────────────────────────────

    #[test]
    fn test_sela_add_basic_count() {
        // 25 horizontal + 25 vertical + 4 squares (2..=5) + 4 diagonals = 58
        let sels = sela_add_basic();
        assert_eq!(sels.len(), 58);
    }

    #[test]
    fn test_sela_add_basic_has_expected_names() {
        let sels = sela_add_basic();
        let names: Vec<_> = sels.iter().filter_map(|s| s.name()).collect();
        assert!(names.contains(&"sel_2h"), "missing sel_2h");
        assert!(names.contains(&"sel_51v"), "missing sel_51v");
        assert!(names.contains(&"sel_4"), "missing sel_4 (2D square)");
        assert!(names.contains(&"sel_2dp"), "missing sel_2dp");
        assert!(names.contains(&"sel_5dm"), "missing sel_5dm");
    }

    #[test]
    fn test_sela_add_basic_linear_sel_dimensions() {
        let sels = sela_add_basic();
        // sel_5h: 1 row, 5 columns, origin (0, 2)
        let sel_5h = sels.iter().find(|s| s.name() == Some("sel_5h")).unwrap();
        assert_eq!(sel_5h.height(), 1);
        assert_eq!(sel_5h.width(), 5);
        assert_eq!(sel_5h.origin_y(), 0);
        assert_eq!(sel_5h.origin_x(), 2);
    }

    #[test]
    fn test_sela_add_hit_miss_count() {
        // 1 isolated + 4 edge + 1 slanted + 4 corner = 10
        let sels = sela_add_hit_miss();
        assert_eq!(sels.len(), 10);
    }

    #[test]
    fn test_sela_add_hit_miss_isolated_pixel() {
        let sels = sela_add_hit_miss();
        let sel = sels.iter().find(|s| s.name() == Some("sel_3hm")).unwrap();
        // 3x3, all miss except center hit
        assert_eq!(sel.width(), 3);
        assert_eq!(sel.height(), 3);
        assert_eq!(sel.hit_count(), 1);
        assert_eq!(sel.get_element(1, 1), Some(SelElement::Hit));
        assert_eq!(sel.get_element(0, 0), Some(SelElement::Miss));
    }

    #[test]
    fn test_sela_add_dwa_linear_count() {
        // sizes 2..=63 horizontal + vertical = 62 * 2 = 124
        let sels = sela_add_dwa_linear();
        assert_eq!(sels.len(), 124);
    }

    #[test]
    fn test_sela_add_dwa_linear_sizes() {
        let sels = sela_add_dwa_linear();
        // first SEL should be sel_2h: 1×2
        let first = &sels[0];
        assert_eq!(first.name(), Some("sel_2h"));
        assert_eq!(first.width(), 2);
        assert_eq!(first.height(), 1);
        // last should be sel_63v: 63×1
        let last = &sels[sels.len() - 1];
        assert_eq!(last.name(), Some("sel_63v"));
        assert_eq!(last.height(), 63);
        assert_eq!(last.width(), 1);
    }

    #[test]
    fn test_sela_add_dwa_combs_nonempty() {
        let sels = sela_add_dwa_combs();
        assert!(!sels.is_empty());
        // Each comb SEL should be either 1×N (horizontal) or N×1 (vertical)
        for s in &sels {
            let is_h = s.height() == 1 && s.width() > 1;
            let is_v = s.width() == 1 && s.height() > 1;
            assert!(is_h || is_v, "comb sel {:?} has unexpected shape", s.name());
        }
    }

    #[test]
    fn test_sela_add_dwa_combs_hit_spacing() {
        let sels = sela_add_dwa_combs();
        // sel_comb_4h should have factor1=2, factor2=2, width=4, 2 hits
        let sel = sels
            .iter()
            .find(|s| s.name() == Some("sel_comb_4h"))
            .unwrap();
        assert_eq!(sel.width(), 4);
        assert_eq!(sel.height(), 1);
        assert_eq!(sel.hit_count(), 2);
    }

    #[test]
    fn test_sela_add_cross_junctions_count() {
        let sels = sela_add_cross_junctions(6.0, 5.0, 2).unwrap();
        assert_eq!(sels.len(), 2); // norient SELs
    }

    #[test]
    fn test_sela_add_cross_junctions_has_hits_and_misses() {
        let sels = sela_add_cross_junctions(6.0, 5.0, 1).unwrap();
        let sel = &sels[0];
        assert!(sel.hit_count() > 0, "cross junction sel should have hits");
        assert!(
            sel.miss_count() > 0,
            "cross junction sel should have misses"
        );
    }

    #[test]
    fn test_sela_add_t_junctions_count() {
        let sels = sela_add_t_junctions(6.0, 5.0, 2).unwrap();
        assert_eq!(sels.len(), 8); // 4 * norient SELs
    }

    #[test]
    fn test_sel_make_plus_sign_dimensions() {
        let sel = sel_make_plus_sign(5, 1).unwrap();
        assert_eq!(sel.width(), 5);
        assert_eq!(sel.height(), 5);
        assert_eq!(sel.origin_x(), 2);
        assert_eq!(sel.origin_y(), 2);
    }

    #[test]
    fn test_sel_make_plus_sign_hit_count() {
        // size=5, linewidth=1: horizontal 5 hits + vertical 5 hits - center 1 = 9 hits
        let sel = sel_make_plus_sign(5, 1).unwrap();
        assert_eq!(sel.hit_count(), 9);
    }

    #[test]
    fn test_sel_make_plus_sign_invalid() {
        assert!(sel_make_plus_sign(2, 1).is_err(), "size < 3 should fail");
        assert!(
            sel_make_plus_sign(5, 6).is_err(),
            "linewidth > size should fail"
        );
    }

    // ── Phase 6: Sela tests ─────────────────────────────────────────────────

    #[test]
    fn test_sela_new_is_empty() {
        let sela = Sela::new();
        assert_eq!(sela.count(), 0);
    }

    #[test]
    fn test_sela_add_and_count() {
        let mut sela = Sela::new();
        let mut sel = Sel::new(3, 3).unwrap();
        sel.set_name("test_sel");
        sela.add(sel).unwrap();
        assert_eq!(sela.count(), 1);
    }

    #[test]
    fn test_sela_add_unnamed_fails() {
        let mut sela = Sela::new();
        let sel = Sel::new(3, 3).unwrap(); // no name
        assert!(sela.add(sel).is_err());
    }

    #[test]
    fn test_sela_get_by_index() {
        let mut sela = Sela::new();
        let mut sel = Sel::new(3, 3).unwrap();
        sel.set_name("my_sel");
        sela.add(sel).unwrap();
        let retrieved = sela.get(0).unwrap();
        assert_eq!(retrieved.name(), Some("my_sel"));
    }

    #[test]
    fn test_sela_get_out_of_bounds_returns_none() {
        let sela = Sela::new();
        assert!(sela.get(0).is_none());
    }

    #[test]
    fn test_sela_find_by_name() {
        let mut sela = Sela::new();
        let mut sel = Sel::new(3, 3).unwrap();
        sel.set_name("target");
        sela.add(sel).unwrap();
        assert!(sela.find_by_name("target").is_some());
        assert!(sela.find_by_name("missing").is_none());
    }

    #[test]
    fn test_sela_write_and_read_roundtrip() {
        let mut sela = Sela::new();
        let mut sel1 = Sel::new(3, 3).unwrap();
        sel1.set_name("sel_a");
        sel1.set_element(1, 1, SelElement::Hit);
        sela.add(sel1).unwrap();

        let mut sel2 = Sel::new(5, 1).unwrap();
        sel2.set_name("sel_b");
        sel2.set_origin(2, 0).unwrap();
        for x in 0..5 {
            sel2.set_element(x, 0, SelElement::Hit);
        }
        sela.add(sel2).unwrap();

        let tmp = std::env::temp_dir().join("test_sela_roundtrip.sel");
        sela.write(&tmp).unwrap();

        let loaded = Sela::read(&tmp).unwrap();
        assert_eq!(loaded.count(), 2);
        assert!(loaded.find_by_name("sel_a").is_some());
        assert!(loaded.find_by_name("sel_b").is_some());

        let _ = std::fs::remove_file(&tmp);
    }
}