outline-core 0.2.0

Image background removal with flexible mask processing options
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
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
use std::borrow::Cow;
use std::collections::VecDeque;
use std::path::Path;
use std::sync::Arc;

use image::{GrayImage, Luma, Rgb, RgbImage, Rgba, RgbaImage};
use imageproc::contrast::{ThresholdType, threshold as ip_threshold};
use imageproc::distance_transform::euclidean_squared_distance_transform;
use imageproc::filter::gaussian_blur_f32;
use ndarray::Array2;

use crate::MaskVectorizer;
use crate::OutlineResult;
use crate::config::{ErosionBorderMode, MaskProcessingDefaults};
use crate::foreground::{ForegroundHandle, compose_foreground};
use crate::geometry::{
    BoundingBox, Padding, crop_bounds_fit_image, crop_gray_image, crop_rgb_image,
    mask_bounding_box, pad_gray_image, pad_rgb_image,
};

#[cfg(feature = "vectorizer-vtracer")]
use vtracer::ColorImage;

/// A single transformation step applied to a grayscale mask image.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum MaskOperation {
    /// Apply Gaussian blur to the mask.
    Blur {
        /// Gaussian sigma. Must be greater than zero.
        sigma: f32,
    },
    /// Threshold the mask into a binary mask.
    Threshold {
        /// Threshold value in the 0-255 range.
        value: u8,
    },
    /// Expand white mask regions.
    Dilate {
        /// Dilation radius in pixels. Must be non-negative and not NaN.
        radius: f32,
    },
    /// Shrink white mask regions.
    Erode {
        /// Erosion radius in pixels. Must be non-negative and not NaN.
        radius: f32,
        /// How pixels outside the image bounds are treated.
        border_mode: ErosionBorderMode,
    },
    /// Fill enclosed background regions.
    FillHoles {
        /// Threshold used to distinguish foreground from background.
        threshold: u8,
    },
}

impl MaskOperation {
    /// Apply this operation to a grayscale mask image.
    ///
    /// # Panics
    ///
    /// Panics if this is a blur operation whose `sigma` is not greater than zero, or a dilation or
    /// erosion operation whose radius is negative or NaN.
    pub fn apply(&self, input: &GrayImage) -> GrayImage {
        match self {
            MaskOperation::Blur { sigma } => gaussian_blur_f32(input, *sigma),
            MaskOperation::Threshold { value } => threshold_mask(input, *value),
            MaskOperation::Dilate { radius } => dilate_euclidean(input, *radius),
            MaskOperation::Erode {
                radius,
                border_mode,
            } => erode_euclidean_with_border_mode(input, *radius, *border_mode),
            MaskOperation::FillHoles { threshold } => fill_mask_holes(input, *threshold),
        }
    }
}

/// Run a list of operations against the provided source image, returning the transformed mask.
///
/// # Panics
///
/// Panics if `operations` contains a blur operation whose `sigma` is not greater than zero, or a
/// dilation or erosion operation whose radius is negative or NaN.
pub fn apply_operations(source: &GrayImage, operations: &[MaskOperation]) -> GrayImage {
    let mut current = source.clone();
    for op in operations {
        current = op.apply(&current);
    }
    current
}

/// An ordered mask processing pipeline.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct MaskPipeline {
    operations: Vec<MaskOperation>,
}

impl MaskPipeline {
    /// Create an empty mask pipeline.
    pub fn new() -> Self {
        Self::default()
    }

    /// Return whether the pipeline has no operations.
    pub fn is_empty(&self) -> bool {
        self.operations.is_empty()
    }

    /// Return the ordered operations in this pipeline.
    pub fn operations(&self) -> &[MaskOperation] {
        &self.operations
    }

    /// Add a blur operation with a custom sigma.
    ///
    /// # Panics
    ///
    /// Panics if `sigma` is not greater than zero.
    pub fn blur_with(mut self, sigma: f32) -> Self {
        assert!(sigma > 0.0, "sigma must be > 0.0");
        self.operations.push(MaskOperation::Blur { sigma });
        self
    }

    /// Add a threshold operation with a custom value.
    pub fn threshold_with(mut self, value: u8) -> Self {
        self.operations.push(MaskOperation::Threshold { value });
        self
    }

    /// Add a dilation operation with a custom radius.
    ///
    /// A radius of zero leaves the mask unchanged.
    ///
    /// # Panics
    ///
    /// Panics if `radius` is negative or NaN.
    pub fn dilate_with(mut self, radius: f32) -> Self {
        assert_nonnegative_radius(radius);
        self.operations.push(MaskOperation::Dilate { radius });
        self
    }

    /// Add an erosion operation with the default boundary behavior.
    ///
    /// A radius of zero leaves the mask unchanged.
    ///
    /// # Panics
    ///
    /// Panics if `radius` is negative or NaN.
    pub fn erode_with(self, radius: f32) -> Self {
        self.erode_with_border_mode(radius, ErosionBorderMode::default())
    }

    /// Add an erosion operation with a custom radius and boundary behavior.
    ///
    /// A radius of zero leaves the mask unchanged.
    ///
    /// # Panics
    ///
    /// Panics if `radius` is negative or NaN.
    pub fn erode_with_border_mode(mut self, radius: f32, border_mode: ErosionBorderMode) -> Self {
        assert_nonnegative_radius(radius);
        self.operations.push(MaskOperation::Erode {
            radius,
            border_mode,
        });
        self
    }

    /// Add a hole-filling operation with a custom threshold.
    pub fn fill_holes_with(mut self, threshold: u8) -> Self {
        self.operations.push(MaskOperation::FillHoles { threshold });
        self
    }
}

/// Convert a 2D array of f32 values in [0.0, 1.0] to a grayscale image.
pub fn array_to_gray_image(array: &Array2<f32>) -> GrayImage {
    let (h, w) = array.dim();
    GrayImage::from_fn(w as u32, h as u32, |x, y| {
        let value = array[[y as usize, x as usize]].clamp(0.0, 1.0);
        let byte = (value * 255.0 + 0.5) as u8;
        Luma([byte])
    })
}

/// Convert a grayscale image to an RGBA color image.
#[cfg(feature = "vectorizer-vtracer")]
pub fn gray_to_color_image_rgba(
    gray: &GrayImage,
    threshold: Option<u8>,
    invert: bool,
) -> ColorImage {
    let (w, h) = gray.dimensions();
    let (w_usize, h_usize) = (w as usize, h as usize);
    let mut rgba = vec![0u8; 4 * w_usize * h_usize];

    for (i, gray_pixel) in gray.pixels().enumerate() {
        let Luma([g]) = gray_pixel;
        let base = if let Some(t) = threshold {
            if *g >= t { 255 } else { 0 }
        } else {
            *g
        };
        let v = if invert {
            255u8.saturating_sub(base)
        } else {
            base
        };
        let idx = i * 4;
        rgba[idx] = v;
        rgba[idx + 1] = v;
        rgba[idx + 2] = v;
        rgba[idx + 3] = 255;
    }

    ColorImage {
        pixels: rgba,
        width: w_usize,
        height: h_usize,
    }
}

/// Threshold the grayscale image to produce a binary mask.
pub fn threshold_mask(gray: &GrayImage, thr: u8) -> GrayImage {
    ip_threshold(gray, thr, ThresholdType::Binary)
}

fn assert_nonnegative_radius(radius: f32) {
    assert!(radius >= 0.0, "radius must be >= 0.0");
}

/// Dilate a binary mask by a Euclidean radius.
///
/// A radius of zero leaves the mask unchanged.
///
/// # Panics
///
/// Panics if `r` is negative or NaN.
pub fn dilate_euclidean(mask_bin: &GrayImage, r: f32) -> GrayImage {
    assert_nonnegative_radius(r);

    if r == 0.0 {
        return mask_bin.clone();
    }

    let d2 = euclidean_squared_distance_transform(mask_bin);
    let r2: f64 = (r as f64) * (r as f64);
    let (w, h) = mask_bin.dimensions();
    let mut out = GrayImage::new(w, h);
    for (o_pixel, d2pixel) in out.pixels_mut().zip(d2.pixels()) {
        let d2xy: f64 = d2pixel[0];
        let v: u8 = if d2xy <= r2 { 255 } else { 0 };
        *o_pixel = Luma([v]);
    }
    out
}

/// Erode a binary mask by the provided radius using the requested boundary behavior.
///
/// A radius of zero leaves the mask unchanged.
///
/// # Panics
///
/// Panics if `r` is negative or NaN.
pub fn erode_euclidean_with_border_mode(
    mask_bin: &GrayImage,
    r: f32,
    border_mode: ErosionBorderMode,
) -> GrayImage {
    assert_nonnegative_radius(r);

    if r == 0.0 {
        return mask_bin.clone();
    }

    let inverted = invert_mask(mask_bin);
    match border_mode {
        ErosionBorderMode::OutsideIsUnknown => invert_mask(&dilate_euclidean(&inverted, r)),
        ErosionBorderMode::OutsideIsBackground => {
            let padding = Padding::uniform(1);
            let padded = pad_gray_image(&inverted, padding, 255);
            let dilated = dilate_euclidean(&padded, r);
            let cropped = crop_gray_image(
                &dilated,
                BoundingBox::new(
                    padding.left,
                    padding.top,
                    mask_bin.width(),
                    mask_bin.height(),
                ),
            );
            invert_mask(&cropped)
        }
    }
}

/// Invert a grayscale mask so each pixel becomes `255 - value`.
pub fn invert_mask(mask: &GrayImage) -> GrayImage {
    let (w, h) = mask.dimensions();
    let mut out = GrayImage::new(w, h);
    for (src, dst) in mask.pixels().zip(out.pixels_mut()) {
        *dst = Luma([255u8.saturating_sub(src[0])]);
    }
    out
}

/// Fill holes in a binary mask using a flood-fill algorithm from the borders.
pub fn fill_mask_holes(mask: &GrayImage, threshold: u8) -> GrayImage {
    let (w, h) = mask.dimensions();
    if w == 0 || h == 0 {
        return mask.clone();
    }

    let (w_usize, h_usize) = (w as usize, h as usize);
    let mut visited = vec![false; w_usize * h_usize];
    let mut queue = VecDeque::new();

    let idx = |x: u32, y: u32| -> usize { (y as usize) * w_usize + x as usize };
    let mask_raw = mask.as_raw();

    // Start flood-fill from all dark pixels at the image borders
    for x in 0..w {
        if mask_raw[idx(x, 0)] < threshold {
            queue.push_back((x, 0));
        }
        if mask_raw[idx(x, h - 1)] < threshold {
            queue.push_back((x, h - 1));
        }
    }

    for y in 0..h {
        if mask_raw[idx(0, y)] < threshold {
            queue.push_back((0, y));
        }
        if mask_raw[idx(w - 1, y)] < threshold {
            queue.push_back((w - 1, y));
        }
    }

    // Use BFS to find all dark pixels connected to the borders
    while let Some((x, y)) = queue.pop_front() {
        let id = idx(x, y);
        if visited[id] {
            continue;
        }
        visited[id] = true;

        // Check neighbors (left, right, up, down) and enqueue if dark and unvisited
        if x > 0 {
            let nx = x - 1;
            let nid = idx(nx, y);
            if !visited[nid] && mask_raw[nid] < threshold {
                queue.push_back((nx, y));
            }
        }
        if x + 1 < w {
            let nx = x + 1;
            let nid = idx(nx, y);
            if !visited[nid] && mask_raw[nid] < threshold {
                queue.push_back((nx, y));
            }
        }
        if y > 0 {
            let ny = y - 1;
            let nid = idx(x, ny);
            if !visited[nid] && mask_raw[nid] < threshold {
                queue.push_back((x, ny));
            }
        }
        if y + 1 < h {
            let ny = y + 1;
            let nid = idx(x, ny);
            if !visited[nid] && mask_raw[nid] < threshold {
                queue.push_back((x, ny));
            }
        }
    }

    let mut out = GrayImage::new(w, h);
    for ((x, y, out_pixel), mask_pixel) in out.enumerate_pixels_mut().zip(mask.pixels()) {
        let id = idx(x, y);
        let value = mask_pixel[0];
        // A pixel is part of a hole if it's dark but was not visited
        let filled = if value >= threshold || !visited[id] {
            255
        } else {
            0
        };
        *out_pixel = Luma([filled]);
    }

    out
}

/// How mask values are converted into output alpha.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum MaskAlphaMode {
    /// Use the mask value directly as alpha.
    #[default]
    UseMask,
    /// Scale the mask alpha by a factor.
    Scale(f32),
    /// Treat any non-zero mask value as a solid alpha.
    Solid(u8),
}

/// Options for colorizing a mask into an RGBA image.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MaskColor {
    /// Flat RGBA color applied to mask-covered pixels; A acts as a global opacity multiplier.
    color: [u8; 4],
    /// How mask values influence the output alpha.
    alpha_mode: MaskAlphaMode,
}

impl MaskColor {
    /// Create a mask color from an RGBA array.
    pub const fn new(color: [u8; 4]) -> Self {
        Self::from_rgba(color)
    }

    /// Create an opaque mask color from RGB channels.
    pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
        Self::from_rgb([r, g, b])
    }

    /// Create a mask color from RGBA channels.
    pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
        Self::from_rgba([r, g, b, a])
    }

    /// Create an opaque mask color from an RGB array.
    pub const fn from_rgb([r, g, b]: [u8; 3]) -> Self {
        Self::from_rgba([r, g, b, 255])
    }

    /// Create a mask color from an RGBA array.
    pub const fn from_rgba(color: [u8; 4]) -> Self {
        Self {
            color,
            alpha_mode: MaskAlphaMode::UseMask,
        }
    }

    /// Return the flat RGBA color.
    pub const fn to_rgba8(self) -> [u8; 4] {
        self.color
    }

    /// Return how mask values influence the output alpha.
    pub const fn alpha_mode(self) -> MaskAlphaMode {
        self.alpha_mode
    }

    /// Override the alpha mode while keeping the color.
    pub const fn with_alpha_mode(mut self, alpha_mode: MaskAlphaMode) -> Self {
        self.alpha_mode = alpha_mode;
        self
    }
}

impl Default for MaskColor {
    fn default() -> Self {
        Self::new([255, 255, 255, 255])
    }
}

impl From<[u8; 4]> for MaskColor {
    fn from(color: [u8; 4]) -> Self {
        Self::from_rgba(color)
    }
}

impl From<[u8; 3]> for MaskColor {
    fn from(color: [u8; 3]) -> Self {
        Self::from_rgb(color)
    }
}

impl From<Rgb<u8>> for MaskColor {
    fn from(color: Rgb<u8>) -> Self {
        Self::from_rgb(color.0)
    }
}

impl From<Rgba<u8>> for MaskColor {
    fn from(color: Rgba<u8>) -> Self {
        Self::from_rgba(color.0)
    }
}

fn resolve_mask_alpha(mask_value: u8, mode: MaskAlphaMode) -> u8 {
    match mode {
        MaskAlphaMode::UseMask => mask_value,
        MaskAlphaMode::Scale(scale) => {
            let scaled = (mask_value as f32) * scale.max(0.0);
            scaled.round().clamp(0.0, 255.0) as u8
        }
        MaskAlphaMode::Solid(alpha) => {
            if mask_value > 0 {
                alpha
            } else {
                0
            }
        }
    }
}

/// Convert a grayscale mask into a flat-color RGBA image.
///
/// The final alpha is the mask-derived alpha, as controlled by [`MaskAlphaMode`],
/// multiplied by `color[3]`.
pub fn colorize_mask(mask: &GrayImage, color: impl Into<MaskColor>) -> RgbaImage {
    let color = color.into();
    let (w, h) = mask.dimensions();
    let [r, g, b, base_alpha] = color.to_rgba8();

    let mut out = RgbaImage::new(w, h);
    for (mask_px, out_px) in mask.pixels().zip(out.pixels_mut()) {
        let mask_alpha = resolve_mask_alpha(mask_px[0], color.alpha_mode());
        let alpha = ((mask_alpha as u16 * base_alpha as u16) / 255) as u8;
        *out_px = Rgba([r, g, b, alpha]);
    }

    out
}

/// Processed mask image with optional further refinement and output generation.
///
/// Represents a concrete mask image (typically binary after thresholding) produced by executing
/// operations from a [`crate::MatteHandle`].
///
/// # Example
/// ```no_run
/// use outline::Outline;
///
/// let outline = Outline::new("model.onnx");
/// let session = outline.for_image("input.jpg")?;
/// let mask = session.matte().blur().threshold().processed()?;
///
/// // Generate multiple outputs from the mask
/// mask.save("mask.png")?;
/// mask.foreground()?.save("subject.png")?;
/// # Ok::<_, outline::OutlineError>(())
/// ```
///
/// To trace the mask into SVG, pass a [`MaskVectorizer`] implementation to [`MaskHandle::trace`].
/// The crate re-exports `VtracerSvgVectorizer` when the `vectorizer-vtracer` feature is enabled.
#[derive(Debug, Clone)]
pub struct MaskHandle {
    rgb_image: Arc<RgbImage>,
    mask: GrayImage,
    mask_processing_defaults: MaskProcessingDefaults,
    operations: Vec<MaskOperation>,
}

impl MaskHandle {
    pub(crate) fn new(
        rgb_image: Arc<RgbImage>,
        mask: GrayImage,
        mask_processing_defaults: MaskProcessingDefaults,
    ) -> Self {
        Self {
            rgb_image,
            mask,
            mask_processing_defaults,
            operations: Vec::new(),
        }
    }

    fn resolve_pending_operations(mut self) -> Self {
        if self.operations.is_empty() {
            return self;
        }

        let operations = std::mem::take(&mut self.operations);
        let mask = apply_operations(&self.mask, &operations);
        Self::new(self.rgb_image, mask, self.mask_processing_defaults)
    }

    fn resolved_mask(&self) -> Cow<'_, GrayImage> {
        if self.operations.is_empty() {
            Cow::Borrowed(&self.mask)
        } else {
            Cow::Owned(apply_operations(&self.mask, &self.operations))
        }
    }

    /// Clone and return the raw mask.
    pub fn to_raw_mask(&self) -> GrayImage {
        self.mask.clone()
    }

    /// Get a reference to the raw mask.
    pub fn as_raw_mask(&self) -> &GrayImage {
        &self.mask
    }

    /// Return the current mask canvas dimensions.
    pub fn dimensions(&self) -> (u32, u32) {
        self.mask.dimensions()
    }

    /// Get the raw mask.
    #[deprecated(note = "use to_raw_mask()")]
    pub fn raw(&self) -> GrayImage {
        self.to_raw_mask()
    }

    /// Get a reference to the raw mask.
    #[deprecated(note = "use as_raw_mask()")]
    pub fn image(&self) -> &GrayImage {
        self.as_raw_mask()
    }

    /// Consume the handle and return the current mask.
    pub fn into_image(self) -> GrayImage {
        self.resolve_pending_operations().mask
    }

    /// Save the current mask to the specified path.
    pub fn save(&self, path: impl AsRef<Path>) -> OutlineResult<()> {
        self.resolved_mask().save(path)?;
        Ok(())
    }

    /// Compute the bounding box of the current mask using a non-zero threshold.
    pub fn bounding_box(&self) -> Option<BoundingBox> {
        self.bounding_box_with(1)
    }

    /// Compute the bounding box of the current mask at or above `threshold`.
    pub fn bounding_box_with(&self, threshold: u8) -> Option<BoundingBox> {
        let mask = self.resolved_mask();
        mask_bounding_box(&mask, threshold)
    }

    /// Add a blur operation using the default sigma.
    ///
    /// # Panics
    ///
    /// Panics if the configured default blur sigma is not greater than zero.
    pub fn blur(mut self) -> Self {
        let sigma = self.mask_processing_defaults.blur_sigma;
        assert!(sigma > 0.0, "sigma must be > 0.0");
        self.operations.push(MaskOperation::Blur { sigma });
        self
    }

    /// Add a blur operation with a custom sigma.
    ///
    /// # Panics
    ///
    /// Panics if `sigma` is not greater than zero.
    pub fn blur_with(mut self, sigma: f32) -> Self {
        assert!(sigma > 0.0, "sigma must be > 0.0");
        self.operations.push(MaskOperation::Blur { sigma });
        self
    }

    /// Add a threshold operation using the default mask threshold.
    pub fn threshold(mut self) -> Self {
        let value = self.mask_processing_defaults.mask_threshold;
        self.operations.push(MaskOperation::Threshold { value });
        self
    }

    /// Add a threshold operation with a custom value.
    pub fn threshold_with(mut self, value: u8) -> Self {
        self.operations.push(MaskOperation::Threshold { value });
        self
    }

    /// Add a dilation operation using the default radius.
    ///
    /// **Note**: Dilation typically works best on binary masks. If this mask is still grayscale,
    /// consider calling [`threshold`](MaskHandle::threshold) first.
    ///
    /// A radius of zero leaves the mask unchanged.
    ///
    /// # Panics
    ///
    /// Panics if the configured default dilation radius is negative or NaN.
    pub fn dilate(mut self) -> Self {
        let radius = self.mask_processing_defaults.dilation_radius;
        assert_nonnegative_radius(radius);
        self.operations.push(MaskOperation::Dilate { radius });
        self
    }

    /// Add a dilation operation with a custom radius.
    ///
    /// **Note**: Dilation typically works best on binary masks. If this mask is still grayscale,
    /// consider calling [`threshold`](MaskHandle::threshold) first.
    ///
    /// A radius of zero leaves the mask unchanged.
    ///
    /// # Panics
    ///
    /// Panics if `radius` is negative or NaN.
    pub fn dilate_with(mut self, radius: f32) -> Self {
        assert_nonnegative_radius(radius);
        self.operations.push(MaskOperation::Dilate { radius });
        self
    }

    /// Add an erosion operation using the default radius.
    ///
    /// **Note**: Erosion typically works best on binary masks. If this mask is still grayscale,
    /// consider calling [`threshold`](MaskHandle::threshold) first.
    ///
    /// A radius of zero leaves the mask unchanged.
    ///
    /// # Panics
    ///
    /// Panics if the configured default erosion radius is negative or NaN.
    pub fn erode(mut self) -> Self {
        let radius = self.mask_processing_defaults.erosion_radius;
        assert_nonnegative_radius(radius);
        let border_mode = self.mask_processing_defaults.erosion_border_mode;
        self.operations.push(MaskOperation::Erode {
            radius,
            border_mode,
        });
        self
    }

    /// Add an erosion operation with a custom radius.
    ///
    /// **Note**: Erosion typically works best on binary masks. If this mask is still grayscale,
    /// consider calling [`threshold`](MaskHandle::threshold) first.
    ///
    /// A radius of zero leaves the mask unchanged.
    ///
    /// # Panics
    ///
    /// Panics if `radius` is negative or NaN.
    pub fn erode_with(mut self, radius: f32) -> Self {
        let border_mode = self.mask_processing_defaults.erosion_border_mode;
        assert_nonnegative_radius(radius);
        self.operations.push(MaskOperation::Erode {
            radius,
            border_mode,
        });
        self
    }

    /// Add an erosion operation with a custom radius and boundary behavior.
    ///
    /// **Note**: Erosion typically works best on binary masks. If this mask is still grayscale,
    /// consider calling [`threshold`](MaskHandle::threshold) first.
    ///
    /// A radius of zero leaves the mask unchanged.
    ///
    /// # Panics
    ///
    /// Panics if `radius` is negative or NaN.
    pub fn erode_with_border_mode(mut self, radius: f32, border_mode: ErosionBorderMode) -> Self {
        assert_nonnegative_radius(radius);
        self.operations.push(MaskOperation::Erode {
            radius,
            border_mode,
        });
        self
    }

    /// Add a hole-filling operation to the processing pipeline.
    ///
    /// **Note**: Hole-filling typically works best on binary masks. If this mask is still grayscale,
    /// consider calling [`threshold`](MaskHandle::threshold) first.
    pub fn fill_holes(self) -> Self {
        let threshold = self.mask_processing_defaults.mask_threshold;
        self.fill_holes_with(threshold)
    }

    /// Add a hole-filling operation with a custom threshold.
    ///
    /// **Note**: Hole-filling typically works best on binary masks. If this mask is still grayscale,
    /// consider calling [`threshold`](MaskHandle::threshold) first.
    pub fn fill_holes_with(mut self, threshold: u8) -> Self {
        self.operations.push(MaskOperation::FillHoles { threshold });
        self
    }

    /// Process the mask with the accumulated operations.
    pub fn processed(self) -> OutlineResult<MaskHandle> {
        self.process_with_pipeline(None)
    }

    /// Process the mask with the accumulated operations and a custom pipeline.
    pub fn processed_with(self, pipeline: &MaskPipeline) -> OutlineResult<MaskHandle> {
        self.process_with_pipeline(Some(pipeline))
    }

    fn process_with_pipeline(
        mut self,
        pipeline: Option<&MaskPipeline>,
    ) -> OutlineResult<MaskHandle> {
        let mut ops = std::mem::take(&mut self.operations);
        if let Some(custom) = pipeline {
            ops.extend_from_slice(custom.operations());
        }

        let mask = apply_operations(&self.mask, &ops);
        Ok(MaskHandle::new(
            self.rgb_image,
            mask,
            self.mask_processing_defaults,
        ))
    }

    /// Compose the RGBA foreground image from the RGB image and the current mask.
    pub fn foreground(&self) -> OutlineResult<ForegroundHandle> {
        let mask = self.resolved_mask();
        let rgba = compose_foreground(self.rgb_image.as_ref(), &mask)?;
        Ok(ForegroundHandle::new(rgba))
    }

    /// Colorize the current mask into a flat-color RGBA image.
    pub fn colorize(&self, color: impl Into<MaskColor>) -> RgbaImage {
        let mask = self.resolved_mask();
        colorize_mask(&mask, color)
    }

    /// Trace the current mask using the specified vectorizer and options.
    pub fn trace<V>(&self, vectorizer: &V, options: &V::Options) -> OutlineResult<V::Output>
    where
        V: MaskVectorizer,
    {
        let mask = self.resolved_mask();
        vectorizer.vectorize(&mask, options)
    }

    /// Expand the mask canvas by the given padding while keeping content at the same offset.
    ///
    /// Any pending mask operations are applied before padding so method call order is preserved.
    /// The underlying RGB canvas is padded with black to keep [`foreground`](MaskHandle::foreground)
    /// aligned with the mask.
    pub fn pad(self, padding: impl Into<Padding>) -> Self {
        let this = self.resolve_pending_operations();
        let padding = padding.into();
        let mask = pad_gray_image(&this.mask, padding, 0);
        let rgb = Arc::new(pad_rgb_image(this.rgb_image.as_ref(), padding, [0, 0, 0]));
        Self::new(rgb, mask, this.mask_processing_defaults)
    }

    /// Crop the mask and source RGB image to `bounds`.
    ///
    /// Returns `None` when `bounds` is empty or outside the current mask canvas.
    pub fn crop(self, bounds: BoundingBox) -> Option<Self> {
        let this = self.resolve_pending_operations();
        if !crop_bounds_fit_image(bounds, this.mask.width(), this.mask.height()) {
            return None;
        }

        let mask = crop_gray_image(&this.mask, bounds);
        let rgb = Arc::new(crop_rgb_image(this.rgb_image.as_ref(), bounds));
        Some(Self::new(rgb, mask, this.mask_processing_defaults))
    }

    /// Crop the mask and source RGB image to the smallest non-zero content box.
    ///
    /// Returns `None` when the current mask has no non-zero pixels.
    pub fn crop_to_content(self) -> Option<Self> {
        self.crop_to_content_with(1)
    }

    /// Crop the mask and source RGB image to the smallest content box at or above `threshold`.
    ///
    /// Returns `None` when the current mask has no pixels at or above `threshold`.
    pub fn crop_to_content_with(self, threshold: u8) -> Option<Self> {
        let this = self.resolve_pending_operations();
        let bounds = mask_bounding_box(&this.mask, threshold)?;
        this.crop(bounds)
    }
}

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

    fn gray_image(w: u32, h: u32, value: u8) -> GrayImage {
        GrayImage::from_pixel(w, h, Luma([value]))
    }

    mod threshold_mask {
        use super::*;

        mod unit {
            use super::*;

            #[test]
            fn all_below_threshold_become_black() {
                let input = gray_image(2, 2, 100);
                let result = threshold_mask(&input, 128);
                for px in result.pixels() {
                    assert_eq!(px.0[0], 0);
                }
            }

            #[test]
            fn all_above_threshold_become_white() {
                let input = gray_image(2, 2, 200);
                let result = threshold_mask(&input, 128);
                for px in result.pixels() {
                    assert_eq!(px.0[0], 255);
                }
            }

            #[test]
            fn exact_threshold_becomes_black() {
                // imageproc threshold: > threshold -> white, <= threshold -> black
                let input = gray_image(2, 2, 128);
                let result = threshold_mask(&input, 128);
                for px in result.pixels() {
                    assert_eq!(px.0[0], 0);
                }
            }

            #[test]
            fn one_below_threshold_becomes_black() {
                let input = gray_image(2, 2, 127);
                let result = threshold_mask(&input, 128);
                for px in result.pixels() {
                    assert_eq!(px.0[0], 0);
                }
            }

            #[test]
            fn dimensions_preserved() {
                let input = gray_image(5, 3, 100);
                let result = threshold_mask(&input, 128);
                assert_eq!(result.dimensions(), (5, 3));
            }

            #[test]
            fn mixed_values_per_pixel() {
                // 2x2 image with values: 127, 128, 129, 255
                let mut input = GrayImage::new(2, 2);
                input.put_pixel(0, 0, Luma([127])); // thr-1
                input.put_pixel(1, 0, Luma([128])); // thr
                input.put_pixel(0, 1, Luma([129])); // thr+1
                input.put_pixel(1, 1, Luma([255])); // max

                let result = threshold_mask(&input, 128);

                assert_eq!(result.get_pixel(0, 0).0[0], 0); // 127 <= 128
                assert_eq!(result.get_pixel(1, 0).0[0], 0); // 128 <= 128
                assert_eq!(result.get_pixel(0, 1).0[0], 255); // 129 > 128
                assert_eq!(result.get_pixel(1, 1).0[0], 255); // 255 > 128
            }

            #[test]
            fn threshold_zero() {
                // thr=0: only value=0 becomes black, everything else white
                let mut input = GrayImage::new(2, 1);
                input.put_pixel(0, 0, Luma([0]));
                input.put_pixel(1, 0, Luma([1]));

                let result = threshold_mask(&input, 0);

                assert_eq!(result.get_pixel(0, 0).0[0], 0); // 0 <= 0
                assert_eq!(result.get_pixel(1, 0).0[0], 255); // 1 > 0
            }

            #[test]
            fn threshold_255() {
                // thr=255: everything becomes black (nothing > 255)
                let mut input = GrayImage::new(2, 1);
                input.put_pixel(0, 0, Luma([254]));
                input.put_pixel(1, 0, Luma([255]));

                let result = threshold_mask(&input, 255);

                assert_eq!(result.get_pixel(0, 0).0[0], 0); // 254 <= 255
                assert_eq!(result.get_pixel(1, 0).0[0], 0); // 255 <= 255
            }
        }

        mod prop {
            use super::*;
            use proptest::prelude::*;

            proptest! {
                /// threshold_mask: output is always binary (0 or 255)
                #[test]
                fn output_is_binary(
                    w in 1u32..20,
                    h in 1u32..20,
                    fill_value in proptest::num::u8::ANY,
                    threshold in proptest::num::u8::ANY
                ) {
                    let input = GrayImage::from_pixel(w, h, Luma([fill_value]));
                    let result = threshold_mask(&input, threshold);

                    prop_assert_eq!(result.dimensions(), (w, h));
                    for px in result.pixels() {
                        prop_assert!(px.0[0] == 0 || px.0[0] == 255);
                    }
                }

                /// threshold_mask: values > threshold become 255, values <= threshold become 0
                #[test]
                fn respects_threshold(
                    value in proptest::num::u8::ANY,
                    threshold in proptest::num::u8::ANY
                ) {
                    let input = GrayImage::from_pixel(1, 1, Luma([value]));
                    let result = threshold_mask(&input, threshold);
                    let out = result.get_pixel(0, 0).0[0];

                    if value > threshold {
                        prop_assert_eq!(out, 255);
                    } else {
                        prop_assert_eq!(out, 0);
                    }
                }
            }
        }
    }

    mod array_to_gray_image {
        use super::*;

        mod unit {
            use super::*;

            #[test]
            fn all_zeros_black() {
                let arr = arr2(&[[0.0, 0.0], [0.0, 0.0]]);
                let result = array_to_gray_image(&arr);
                for px in result.pixels() {
                    assert_eq!(px.0[0], 0);
                }
            }

            #[test]
            fn all_ones_white() {
                let arr = arr2(&[[1.0, 1.0], [1.0, 1.0]]);
                let result = array_to_gray_image(&arr);
                for px in result.pixels() {
                    assert_eq!(px.0[0], 255);
                }
            }

            #[test]
            fn half_value_gray() {
                let arr = arr2(&[[0.5]]);
                let result = array_to_gray_image(&arr);
                // 0.5 * 255 + 0.5 = 128
                assert_eq!(result.get_pixel(0, 0).0[0], 128);
            }

            #[test]
            fn clamps_above_one() {
                let arr = arr2(&[[2.0]]);
                let result = array_to_gray_image(&arr);
                assert_eq!(result.get_pixel(0, 0).0[0], 255);
            }

            #[test]
            fn clamps_below_zero() {
                let arr = arr2(&[[-1.0]]);
                let result = array_to_gray_image(&arr);
                assert_eq!(result.get_pixel(0, 0).0[0], 0);
            }

            #[test]
            fn dimensions_preserved() {
                let arr = arr2(&[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]);
                let result = array_to_gray_image(&arr);
                // ndarray is (rows, cols) = (h, w), image is (w, h)
                assert_eq!(result.dimensions(), (3, 2));
            }

            #[test]
            fn rounding_behavior() {
                let arr = arr2(&[[0.4]]);
                let result = array_to_gray_image(&arr);
                assert_eq!(result.get_pixel(0, 0).0[0], 102); // 0.4 * 255 + 0.5 = 102.5, truncated
            }
        }

        mod prop {
            use super::*;
            use proptest::prelude::*;

            proptest! {
                /// array_to_gray_image: any f32 input clamps to [0, 255] range
                #[test]
                fn clamps_out_of_range_values(
                    values in proptest::collection::vec(-10.0f32..10.0f32, 1..100)
                ) {
                    // Create a 1D array that we'll reshape - use length as width, height=1
                    let len = values.len();
                    let arr = ndarray::Array2::from_shape_vec((1, len), values.clone()).unwrap();
                    let result = array_to_gray_image(&arr);

                    // Verify clamping: negative values -> 0, values > 1 -> 255
                    for (i, px) in result.pixels().enumerate() {
                        let input = values[i];
                        if input <= 0.0 {
                            prop_assert_eq!(px.0[0], 0);
                        } else if input >= 1.0 {
                            prop_assert_eq!(px.0[0], 255);
                        }
                    }
                    // Dimensions preserved
                    prop_assert_eq!(result.dimensions(), (len as u32, 1));
                }

                /// array_to_gray_image: values in [0, 1] map to proportional bytes
                #[test]
                fn valid_range_maps_proportionally(value in 0.0f32..=1.0f32) {
                    let arr = ndarray::arr2(&[[value]]);
                    let result = array_to_gray_image(&arr);
                    let byte = result.get_pixel(0, 0).0[0];

                    // Expected: (value * 255.0 + 0.5) as u8
                    let expected = (value * 255.0 + 0.5) as u8;
                    prop_assert_eq!(byte, expected);
                }
            }
        }
    }

    mod fill_mask_holes {
        use super::*;

        mod unit {
            use super::*;

            #[test]
            fn solid_white_unchanged() {
                let input = gray_image(4, 4, 255);
                let result = fill_mask_holes(&input, 128);
                for px in result.pixels() {
                    assert_eq!(px.0[0], 255);
                }
            }

            #[test]
            fn solid_black_unchanged() {
                let input = gray_image(4, 4, 0); // all black, connected to border
                let result = fill_mask_holes(&input, 128);
                for px in result.pixels() {
                    assert_eq!(px.0[0], 0); // stays black
                }
            }

            #[test]
            fn interior_hole_filled() {
                // Create a 5x5 image: white border, black interior
                // W W W W W
                // W B B B W
                // W B B B W
                // W B B B W
                // W W W W W
                let mut input = gray_image(5, 5, 255);
                for y in 1..4 {
                    for x in 1..4 {
                        input.put_pixel(x, y, Luma([0]));
                    }
                }

                let result = fill_mask_holes(&input, 128);

                // The interior black region is NOT connected to border, so it gets filled
                for px in result.pixels() {
                    assert_eq!(px.0[0], 255);
                }
            }

            #[test]
            fn border_connected_black_not_filled() {
                // Create a 4x4 image with black on left edge
                // B W W W
                // B W W W
                // B W W W
                // B W W W
                let mut input = gray_image(4, 4, 255);
                for y in 0..4 {
                    input.put_pixel(0, y, Luma([0]));
                }

                let result = fill_mask_holes(&input, 128);

                // Left column is connected to border, stays black (=0 in output)
                for y in 0..4 {
                    assert_eq!(result.get_pixel(0, y).0[0], 0);
                }
                // Rest stays white
                for y in 0..4 {
                    for x in 1..4 {
                        assert_eq!(result.get_pixel(x, y).0[0], 255);
                    }
                }
            }

            #[test]
            fn dimensions_preserved() {
                let input = gray_image(7, 5, 128);
                let result = fill_mask_holes(&input, 128);
                assert_eq!(result.dimensions(), (7, 5));
            }

            #[test]
            fn empty_image_is_unchanged() {
                let input = GrayImage::new(0, 0);
                let result = fill_mask_holes(&input, 128);
                assert_eq!(result.dimensions(), (0, 0));
            }

            #[test]
            fn diagonal_only_connection_not_traversed() {
                // 4-connectivity: diagonal doesn't count as connected
                // W W W
                // W B W
                // B W W  (bottom-left black only diagonally connected to center)
                let mut input = GrayImage::new(3, 3);
                for y in 0..3 {
                    for x in 0..3 {
                        input.put_pixel(x, y, Luma([255]));
                    }
                }
                input.put_pixel(1, 1, Luma([0])); // center black
                input.put_pixel(0, 2, Luma([0])); // corner black (touches border)

                let result = fill_mask_holes(&input, 128);

                // corner (0,2) is on border, stays black
                assert_eq!(result.get_pixel(0, 2).0[0], 0);
                // center (1,1) is NOT 4-connected to border, gets filled
                assert_eq!(result.get_pixel(1, 1).0[0], 255);
            }

            #[test]
            fn threshold_changes_border_connectivity() {
                // pixel value 110 on border:
                // thr=128: 110 < 128, dark, BFS marks visited, output 0
                // thr=100: 110 >= 100, light, not visited by BFS, output 255
                let mut input = gray_image(3, 3, 255);
                input.put_pixel(0, 1, Luma([110])); // left border

                let r128 = fill_mask_holes(&input, 128);
                assert_eq!(r128.get_pixel(0, 1).0[0], 0);

                let r100 = fill_mask_holes(&input, 100);
                assert_eq!(r100.get_pixel(0, 1).0[0], 255);
            }

            #[test]
            fn output_is_always_binary() {
                // fill_mask_holes should always produce binary output (0 or 255)
                let mut input = gray_image(5, 5, 200); // non-binary input
                input.put_pixel(2, 2, Luma([50])); // interior dark pixel

                let result = fill_mask_holes(&input, 128);

                let is_binary = result.pixels().all(|p| p.0[0] == 0 || p.0[0] == 255);
                assert!(is_binary);
            }
        }

        mod prop {
            use super::*;
            use proptest::prelude::*;

            proptest! {
                /// fill_mask_holes: output is always binary and dimensions preserved
                #[test]
                fn output_is_binary(
                    w in 1u32..15,
                    h in 1u32..15,
                    fill_value in proptest::num::u8::ANY,
                    threshold in proptest::num::u8::ANY
                ) {
                    let input = GrayImage::from_pixel(w, h, Luma([fill_value]));
                    let result = fill_mask_holes(&input, threshold);

                    prop_assert_eq!(result.dimensions(), (w, h));
                    for px in result.pixels() {
                        prop_assert!(px.0[0] == 0 || px.0[0] == 255);
                    }
                }
            }
        }
    }

    mod dilate_euclidean {
        use super::*;

        mod unit {
            use super::*;

            #[test]
            fn solid_white_stays_white() {
                let input = gray_image(4, 4, 255);
                let result = dilate_euclidean(&input, 2.0);
                for px in result.pixels() {
                    assert_eq!(px.0[0], 255);
                }
            }

            #[test]
            fn solid_black_stays_black() {
                let input = gray_image(4, 4, 0);
                let result = dilate_euclidean(&input, 2.0);
                for px in result.pixels() {
                    assert_eq!(px.0[0], 0);
                }
            }

            #[test]
            fn single_white_pixel_dilates() {
                // 5x5 black image with center pixel white
                let mut input = gray_image(5, 5, 0);
                input.put_pixel(2, 2, Luma([255]));

                let result = dilate_euclidean(&input, 1.5);

                // Center should be white
                assert_eq!(result.get_pixel(2, 2).0[0], 255);
                // Neighbors within radius should also be white
                assert_eq!(result.get_pixel(2, 1).0[0], 255);
                assert_eq!(result.get_pixel(2, 3).0[0], 255);
                assert_eq!(result.get_pixel(1, 2).0[0], 255);
                assert_eq!(result.get_pixel(3, 2).0[0], 255);
            }

            #[test]
            fn dimensions_preserved() {
                let input = gray_image(6, 4, 128);
                let result = dilate_euclidean(&input, 1.0);
                assert_eq!(result.dimensions(), (6, 4));
            }

            #[test]
            fn diagonal_within_radius() {
                // r=1.5, diagonal distance = sqrt(2) ~ 1.414 < 1.5
                let mut input = gray_image(5, 5, 0);
                input.put_pixel(2, 2, Luma([255]));

                let result = dilate_euclidean(&input, 1.5);

                // diagonals should be white (euclidean distance ~1.414)
                assert_eq!(result.get_pixel(1, 1).0[0], 255);
                assert_eq!(result.get_pixel(3, 1).0[0], 255);
                assert_eq!(result.get_pixel(1, 3).0[0], 255);
                assert_eq!(result.get_pixel(3, 3).0[0], 255);
            }

            #[test]
            fn radius_zero_only_original_pixels() {
                // r=0: only original white pixels stay white
                let mut input = gray_image(3, 3, 0);
                input.put_pixel(1, 1, Luma([255]));

                let result = dilate_euclidean(&input, 0.0);

                assert_eq!(result.get_pixel(1, 1).0[0], 255);
                // neighbors should stay black
                assert_eq!(result.get_pixel(0, 1).0[0], 0);
                assert_eq!(result.get_pixel(2, 1).0[0], 0);
                assert_eq!(result.get_pixel(1, 0).0[0], 0);
                assert_eq!(result.get_pixel(1, 2).0[0], 0);
            }

            #[test]
            #[should_panic(expected = "radius must be >= 0.0")]
            fn negative_radius_panics() {
                let input = gray_image(3, 3, 0);
                let _ = dilate_euclidean(&input, -1.0);
            }
        }

        mod prop {
            use super::*;
            use proptest::prelude::*;

            proptest! {
                /// dilate_euclidean: output is always binary and dimensions preserved
                #[test]
                fn output_is_binary(
                    w in 1u32..15,
                    h in 1u32..15,
                    fill_value in proptest::num::u8::ANY,
                    radius in 0.0f32..5.0f32
                ) {
                    let input = GrayImage::from_pixel(w, h, Luma([fill_value]));
                    let result = dilate_euclidean(&input, radius);

                    prop_assert_eq!(result.dimensions(), (w, h));
                    for px in result.pixels() {
                        prop_assert!(px.0[0] == 0 || px.0[0] == 255);
                    }
                }
            }
        }
    }

    mod erode_euclidean_tests {
        use super::*;
        use crate::config::ErosionBorderMode;

        #[test]
        fn erosion_shrinks_binary_mask() {
            let mut input = gray_image(5, 5, 0);
            for y in 1..4 {
                for x in 1..4 {
                    input.put_pixel(x, y, Luma([255]));
                }
            }

            let result =
                erode_euclidean_with_border_mode(&input, 1.0, ErosionBorderMode::default());

            assert_eq!(result.get_pixel(2, 2).0[0], 255);
            assert_eq!(result.get_pixel(1, 1).0[0], 0);
            assert_eq!(result.get_pixel(1, 2).0[0], 0);
            assert_eq!(result.get_pixel(2, 1).0[0], 0);
            assert_eq!(result.get_pixel(3, 3).0[0], 0);
        }

        #[test]
        fn erosion_treats_image_exterior_as_background() {
            let input = gray_image(5, 5, 255);

            let result =
                erode_euclidean_with_border_mode(&input, 1.0, ErosionBorderMode::default());

            for y in 0..5 {
                for x in 0..5 {
                    let expected = if x == 0 || y == 0 || x == 4 || y == 4 {
                        0
                    } else {
                        255
                    };
                    assert_eq!(result.get_pixel(x, y).0[0], expected, "pixel ({x}, {y})");
                }
            }
        }

        #[test]
        fn erosion_can_treat_image_exterior_as_unknown() {
            let input = gray_image(5, 5, 255);

            let result =
                erode_euclidean_with_border_mode(&input, 1.0, ErosionBorderMode::OutsideIsUnknown);

            assert_eq!(result.as_raw(), input.as_raw());
        }

        #[test]
        fn erosion_radius_zero_preserves_mask() {
            let mut input = gray_image(3, 3, 0);
            input.put_pixel(0, 0, Luma([255]));
            input.put_pixel(1, 1, Luma([255]));
            input.put_pixel(2, 2, Luma([255]));

            let result =
                erode_euclidean_with_border_mode(&input, 0.0, ErosionBorderMode::default());

            assert_eq!(result.as_raw(), input.as_raw());
        }

        #[test]
        #[should_panic(expected = "radius must be >= 0.0")]
        fn erosion_negative_radius_panics() {
            let input = gray_image(3, 3, 0);
            let _ = erode_euclidean_with_border_mode(&input, -1.0, ErosionBorderMode::default());
        }

        #[test]
        fn erosion_shrinks_foreground_touching_one_edge() {
            let mut input = gray_image(5, 5, 0);
            for y in 1..4 {
                for x in 0..3 {
                    input.put_pixel(x, y, Luma([255]));
                }
            }

            let result =
                erode_euclidean_with_border_mode(&input, 1.0, ErosionBorderMode::default());

            for y in 0..5 {
                for x in 0..5 {
                    let expected = if x == 1 && y == 2 { 255 } else { 0 };
                    assert_eq!(result.get_pixel(x, y).0[0], expected, "pixel ({x}, {y})");
                }
            }
        }
    }

    mod apply_operations {
        use super::*;

        mod unit {
            use super::*;

            #[test]
            fn empty_operations_returns_clone() {
                let input = gray_image(3, 3, 100);
                let result = apply_operations(&input, &[]);
                assert_eq!(result.as_raw(), input.as_raw());
            }

            #[test]
            fn single_threshold_operation() {
                let input = gray_image(2, 2, 200);
                let ops = vec![MaskOperation::Threshold { value: 128 }];
                let result = apply_operations(&input, &ops);
                for px in result.pixels() {
                    assert_eq!(px.0[0], 255); // 200 > 128
                }
            }

            #[test]
            fn threshold_then_dilate() {
                let mut input = gray_image(5, 5, 0);
                input.put_pixel(2, 2, Luma([200]));

                // threshold (200 > 128 = white), then dilate expands it
                let ops = vec![
                    MaskOperation::Threshold { value: 128 },
                    MaskOperation::Dilate { radius: 1.0 },
                ];
                let result = apply_operations(&input, &ops);

                // center and neighbors should be white
                assert_eq!(result.get_pixel(2, 2).0[0], 255);
                assert_eq!(result.get_pixel(2, 1).0[0], 255);
            }

            #[test]
            fn threshold_then_erode() {
                let mut input = gray_image(5, 5, 0);
                for y in 1..4 {
                    for x in 1..4 {
                        input.put_pixel(x, y, Luma([200]));
                    }
                }

                let ops = vec![
                    MaskOperation::Threshold { value: 128 },
                    MaskOperation::Erode {
                        radius: 1.0,
                        border_mode: ErosionBorderMode::default(),
                    },
                ];
                let result = apply_operations(&input, &ops);

                assert_eq!(result.get_pixel(2, 2).0[0], 255);
                assert_eq!(result.get_pixel(1, 1).0[0], 0);
            }

            #[test]
            fn order_matters_blur_vs_threshold() {
                // blur then threshold vs threshold then blur produce different results
                let mut input = gray_image(5, 5, 0);
                input.put_pixel(2, 2, Luma([255])); // single white pixel

                // blur first spreads the white, then threshold produces binary output
                let ops_blur_first = vec![
                    MaskOperation::Blur { sigma: 1.0 },
                    MaskOperation::Threshold { value: 50 },
                ];
                let result_blur_first = apply_operations(&input, &ops_blur_first);

                // threshold first (255 > 50 = white), then blur produces soft edges
                let ops_threshold_first = vec![
                    MaskOperation::Threshold { value: 50 },
                    MaskOperation::Blur { sigma: 1.0 },
                ];
                let result_threshold_first = apply_operations(&input, &ops_threshold_first);

                // blur->threshold is binary (all pixels are 0 or 255)
                let is_binary = result_blur_first
                    .pixels()
                    .all(|p| p.0[0] == 0 || p.0[0] == 255);
                assert!(is_binary);

                // threshold->blur has intermediate grays (some 0 < value < 255)
                let has_intermediate = result_threshold_first
                    .pixels()
                    .any(|p| p.0[0] > 0 && p.0[0] < 255);
                assert!(has_intermediate);

                // fallback: overall results must differ
                assert_ne!(result_blur_first.as_raw(), result_threshold_first.as_raw());
            }

            #[test]
            fn order_matters_dilate_vs_blur() {
                let mut input = gray_image(5, 5, 0);
                input.put_pixel(2, 2, Luma([255]));

                let ops_dilate_first = vec![
                    MaskOperation::Dilate { radius: 1.0 },
                    MaskOperation::Blur { sigma: 1.0 },
                ];
                let result_dilate_first = apply_operations(&input, &ops_dilate_first);

                let ops_blur_first = vec![
                    MaskOperation::Blur { sigma: 1.0 },
                    MaskOperation::Dilate { radius: 1.0 },
                ];
                let result_blur_first = apply_operations(&input, &ops_blur_first);

                assert_ne!(result_dilate_first.as_raw(), result_blur_first.as_raw());
            }
        }

        mod prop {
            use super::*;
            use proptest::prelude::*;

            proptest! {
                /// apply_operations: dimensions always preserved regardless of operations
                #[test]
                fn preserves_dimensions(
                    w in 1u32..10,
                    h in 1u32..10,
                    fill_value in proptest::num::u8::ANY
                ) {
                    let input = GrayImage::from_pixel(w, h, Luma([fill_value]));

                    // Test with various operation combinations
                    let ops_threshold = vec![MaskOperation::Threshold { value: 128 }];
                    let result = apply_operations(&input, &ops_threshold);
                    prop_assert_eq!(result.dimensions(), (w, h));

                    let ops_dilate = vec![MaskOperation::Dilate { radius: 1.0 }];
                    let result = apply_operations(&input, &ops_dilate);
                    prop_assert_eq!(result.dimensions(), (w, h));

                    let ops_erode = vec![MaskOperation::Erode {
                        radius: 1.0,
                        border_mode: ErosionBorderMode::default(),
                    }];
                    let result = apply_operations(&input, &ops_erode);
                    prop_assert_eq!(result.dimensions(), (w, h));

                    let ops_fill = vec![MaskOperation::FillHoles { threshold: 128 }];
                    let result = apply_operations(&input, &ops_fill);
                    prop_assert_eq!(result.dimensions(), (w, h));

                    let ops_blur = vec![MaskOperation::Blur { sigma: 1.0 }];
                    let result = apply_operations(&input, &ops_blur);
                    prop_assert_eq!(result.dimensions(), (w, h));
                }
            }
        }
    }

    mod mask_pipeline {
        use super::*;

        mod unit {
            use super::*;

            #[test]
            fn new_returns_empty() {
                let pipeline = MaskPipeline::new();

                assert!(pipeline.is_empty());
                assert!(pipeline.operations().is_empty());
            }

            #[test]
            fn blur_with_adds_blur_operation() {
                let pipeline = MaskPipeline::new().blur_with(3.0);
                let ops = pipeline.operations();

                assert_eq!(ops.len(), 1);
                assert!(
                    matches!(ops[0], MaskOperation::Blur { sigma } if (sigma - 3.0).abs() < 0.001)
                );
            }

            #[test]
            fn full_pipeline_order_and_values() {
                let pipeline = MaskPipeline::new()
                    .blur_with(2.0)
                    .threshold_with(128)
                    .dilate_with(5.0)
                    .erode_with_border_mode(3.0, ErosionBorderMode::OutsideIsUnknown)
                    .fill_holes_with(128);
                let ops = pipeline.operations();

                assert_eq!(ops.len(), 5);
                assert!(
                    matches!(ops[0], MaskOperation::Blur { sigma } if (sigma - 2.0).abs() < 1e-6)
                );
                assert!(matches!(ops[1], MaskOperation::Threshold { value: 128 }));
                assert!(
                    matches!(ops[2], MaskOperation::Dilate { radius } if (radius - 5.0).abs() < 1e-6)
                );
                assert!(matches!(
                    ops[3],
                    MaskOperation::Erode { radius, border_mode }
                        if (radius - 3.0).abs() < 1e-6
                            && border_mode == ErosionBorderMode::OutsideIsUnknown
                ));
                assert!(matches!(
                    ops[4],
                    MaskOperation::FillHoles { threshold: 128 }
                ));
            }

            #[test]
            fn partial_pipeline_keeps_inserted_order() {
                let pipeline = MaskPipeline::new().threshold_with(100).fill_holes_with(100);
                let ops = pipeline.operations();

                assert_eq!(ops.len(), 2);
                assert!(matches!(ops[0], MaskOperation::Threshold { value: 100 }));
                assert!(matches!(
                    ops[1],
                    MaskOperation::FillHoles { threshold: 100 }
                ));
            }
        }
    }

    mod mask_handle_api {
        use super::*;
        use image::Rgb;

        fn mask_handle() -> MaskHandle {
            MaskHandle {
                rgb_image: Arc::new(RgbImage::from_pixel(1, 1, Rgb([255, 255, 255]))),
                mask: GrayImage::from_pixel(1, 1, Luma([255])),
                mask_processing_defaults: MaskProcessingDefaults::default(),
                operations: Vec::new(),
            }
        }

        fn mask_handle_with_images(rgb: RgbImage, mask: GrayImage) -> MaskHandle {
            MaskHandle {
                rgb_image: Arc::new(rgb),
                mask,
                mask_processing_defaults: MaskProcessingDefaults::default(),
                operations: Vec::new(),
            }
        }

        fn single_pixel_mask_handle() -> MaskHandle {
            mask_handle_with_images(
                RgbImage::from_pixel(5, 5, Rgb([10, 20, 30])),
                GrayImage::from_fn(5, 5, |x, y| {
                    if x == 2 && y == 2 {
                        Luma([255])
                    } else {
                        Luma([0])
                    }
                }),
            )
        }

        struct BoundingBoxVectorizer;

        impl MaskVectorizer for BoundingBoxVectorizer {
            type Options = ();
            type Output = Option<BoundingBox>;

            fn vectorize(
                &self,
                mask: &GrayImage,
                _options: &Self::Options,
            ) -> OutlineResult<Self::Output> {
                Ok(mask_bounding_box(mask, 1))
            }
        }

        mod colorize {
            use super::*;

            #[test]
            fn default_color_is_white_with_mask_alpha() {
                let color = MaskColor::default();
                assert_eq!(color.to_rgba8(), [255, 255, 255, 255]);
                assert!(matches!(color.alpha_mode(), MaskAlphaMode::UseMask));
            }

            #[test]
            fn rgb_constructor_defaults_to_opaque_alpha() {
                let color = MaskColor::rgb(10, 20, 30);

                assert_eq!(color.to_rgba8(), [10, 20, 30, 255]);
                assert!(matches!(color.alpha_mode(), MaskAlphaMode::UseMask));
            }

            #[test]
            fn rgba_constructor_keeps_all_channels() {
                let color = MaskColor::rgba(10, 20, 30, 40);

                assert_eq!(color.to_rgba8(), [10, 20, 30, 40]);
                assert!(matches!(color.alpha_mode(), MaskAlphaMode::UseMask));
            }

            #[test]
            fn rgb_array_color_defaults_to_opaque_alpha() {
                let color = MaskColor::from([10, 20, 30]);

                assert_eq!(color.to_rgba8(), [10, 20, 30, 255]);
                assert!(matches!(color.alpha_mode(), MaskAlphaMode::UseMask));
            }

            #[test]
            fn rgb_pixel_color_defaults_to_opaque_alpha() {
                let color = MaskColor::from(Rgb([10, 20, 30]));

                assert_eq!(color.to_rgba8(), [10, 20, 30, 255]);
                assert!(matches!(color.alpha_mode(), MaskAlphaMode::UseMask));
            }

            #[test]
            fn rgba_pixel_color_keeps_all_channels() {
                let color = MaskColor::from(Rgba([10, 20, 30, 40]));

                assert_eq!(color.to_rgba8(), [10, 20, 30, 40]);
                assert!(matches!(color.alpha_mode(), MaskAlphaMode::UseMask));
            }

            #[test]
            fn use_mask_mode_sets_alpha_from_mask() {
                let mask = gray_image(2, 2, 128);
                let result = colorize_mask(&mask, MaskColor::new([0, 180, 255, 255]));

                for px in result.pixels() {
                    assert_eq!(px.0, [0, 180, 255, 128]);
                }
            }

            #[test]
            fn base_alpha_is_multiplied_with_mask_alpha() {
                let mask = gray_image(1, 1, 128);
                let result = colorize_mask(&mask, MaskColor::new([255, 255, 255, 128]));

                assert_eq!(result.get_pixel(0, 0).0, [255, 255, 255, 64]);
            }

            #[test]
            fn scale_mode_scales_mask_alpha() {
                let mask = gray_image(1, 1, 200);
                let color =
                    MaskColor::new([255, 0, 0, 255]).with_alpha_mode(MaskAlphaMode::Scale(0.5));
                let result = colorize_mask(&mask, color);

                assert_eq!(result.get_pixel(0, 0).0, [255, 0, 0, 100]);
            }

            #[test]
            fn solid_mode_uses_alpha_for_nonzero_mask() {
                let mask =
                    GrayImage::from_fn(2, 1, |x, _| if x == 0 { Luma([0]) } else { Luma([25]) });
                let color =
                    MaskColor::new([0, 255, 0, 255]).with_alpha_mode(MaskAlphaMode::Solid(200));
                let result = colorize_mask(&mask, color);

                assert_eq!(result.get_pixel(0, 0).0, [0, 255, 0, 0]);
                assert_eq!(result.get_pixel(1, 0).0, [0, 255, 0, 200]);
            }
        }

        mod erode_builder {
            use super::*;

            #[test]
            fn mask_handle_erode_uses_default_radius() {
                let handle = mask_handle().erode();
                assert!(matches!(
                    handle.operations.as_slice(),
                    [MaskOperation::Erode { radius, border_mode }]
                        if (*radius - MaskProcessingDefaults::default().erosion_radius).abs() < f32::EPSILON
                            && *border_mode == MaskProcessingDefaults::default().erosion_border_mode
                ));
            }

            #[test]
            fn mask_handle_erode_with_uses_custom_radius() {
                let handle = mask_handle().erode_with(3.0);
                assert!(matches!(
                    handle.operations.as_slice(),
                    [MaskOperation::Erode { radius, border_mode }]
                        if (*radius - 3.0).abs() < f32::EPSILON
                            && *border_mode == MaskProcessingDefaults::default().erosion_border_mode
                ));
            }

            #[test]
            fn mask_handle_erode_with_border_mode_uses_custom_mode() {
                let handle =
                    mask_handle().erode_with_border_mode(3.0, ErosionBorderMode::OutsideIsUnknown);
                assert!(matches!(
                    handle.operations.as_slice(),
                    [MaskOperation::Erode { radius, border_mode }]
                        if (*radius - 3.0).abs() < f32::EPSILON
                            && *border_mode == ErosionBorderMode::OutsideIsUnknown
                ));
            }
        }

        mod fill_holes_builder {
            use super::*;

            #[test]
            fn mask_handle_fill_holes_with_uses_custom_threshold() {
                let handle = mask_handle().fill_holes_with(180);
                assert!(matches!(
                    handle.operations.as_slice(),
                    [MaskOperation::FillHoles { threshold: 180 }]
                ));
            }
        }

        mod processing_pipeline {
            use super::*;

            #[test]
            fn chain_and_pipeline_are_equivalent() {
                let pipeline = MaskPipeline::new()
                    .threshold_with(128)
                    .dilate_with(1.0)
                    .erode_with(1.0);

                let chained = single_pixel_mask_handle()
                    .threshold_with(128)
                    .dilate_with(1.0)
                    .erode_with(1.0)
                    .processed()
                    .expect("chain should process")
                    .into_image();
                let piped = single_pixel_mask_handle()
                    .processed_with(&pipeline)
                    .expect("pipeline should process")
                    .into_image();

                assert_eq!(chained.as_raw(), piped.as_raw());
            }

            #[test]
            fn processed_with_appends_after_chained_operations() {
                let pipeline = MaskPipeline::new().dilate_with(1.0);

                let appended = single_pixel_mask_handle()
                    .threshold_with(128)
                    .processed_with(&pipeline)
                    .expect("processing should succeed")
                    .into_image();
                let chained = single_pixel_mask_handle()
                    .threshold_with(128)
                    .dilate_with(1.0)
                    .processed()
                    .expect("processing should succeed")
                    .into_image();

                assert_eq!(appended.as_raw(), chained.as_raw());
            }

            #[test]
            fn processed_without_chained_operations_is_identity() {
                let source = single_pixel_mask_handle().into_image();
                let handle = MaskHandle {
                    rgb_image: Arc::new(RgbImage::from_pixel(5, 5, Rgb([10, 20, 30]))),
                    mask: source.clone(),
                    mask_processing_defaults: MaskProcessingDefaults::default(),
                    operations: Vec::new(),
                };

                let mask = handle
                    .processed()
                    .expect("empty processing should succeed")
                    .into_image();

                assert_eq!(mask.as_raw(), source.as_raw());
            }

            #[test]
            fn no_arg_methods_use_custom_defaults() {
                let defaults = MaskProcessingDefaults {
                    blur_sigma: 2.0,
                    mask_threshold: 180,
                    dilation_radius: 3.0,
                    erosion_radius: 4.0,
                    erosion_border_mode: ErosionBorderMode::OutsideIsUnknown,
                };
                let handle = MaskHandle {
                    rgb_image: Arc::new(RgbImage::from_pixel(1, 1, Rgb([255, 255, 255]))),
                    mask: GrayImage::from_pixel(1, 1, Luma([255])),
                    mask_processing_defaults: defaults,
                    operations: Vec::new(),
                }
                .blur()
                .threshold()
                .dilate()
                .erode()
                .fill_holes();

                assert!(matches!(
                    handle.operations.as_slice(),
                    [
                        MaskOperation::Blur { sigma },
                        MaskOperation::Threshold { value: 180 },
                        MaskOperation::Dilate { radius: dilation_radius },
                        MaskOperation::Erode {
                            radius: erosion_radius,
                            border_mode: ErosionBorderMode::OutsideIsUnknown
                        },
                        MaskOperation::FillHoles { threshold: 180 }
                    ] if (*sigma - 2.0).abs() < f32::EPSILON
                        && (*dilation_radius - 3.0).abs() < f32::EPSILON
                        && (*erosion_radius - 4.0).abs() < f32::EPSILON
                ));
            }
        }

        mod geometry {
            use super::*;

            #[test]
            fn mask_handle_dimensions_reports_current_canvas() {
                let cropped = single_pixel_mask_handle()
                    .crop(BoundingBox::new(1, 1, 2, 3))
                    .expect("bounds are inside the image");
                assert_eq!(cropped.dimensions(), (2, 3));

                let padded = cropped.pad(Padding::new(1, 2, 3, 4));
                assert_eq!(padded.dimensions(), (6, 9));
            }

            #[test]
            fn mask_handle_pad_updates_mask_and_foreground_canvas() {
                let rgb = RgbImage::from_pixel(2, 2, Rgb([10, 20, 30]));
                let mut mask = GrayImage::from_pixel(2, 2, Luma([0]));
                mask.put_pixel(1, 1, Luma([255]));

                let padded = mask_handle_with_images(rgb, mask).pad(Padding::new(1, 2, 3, 4));

                assert_eq!(padded.dimensions(), (6, 8));
                let foreground = padded.foreground().expect("foreground should compose");
                assert_eq!(foreground.dimensions(), (6, 8));
                assert_eq!(foreground.image().get_pixel(2, 3)[3], 255);
            }

            #[test]
            fn mask_handle_crop_to_content_crops_mask_and_rgb_together() {
                let rgb = RgbImage::from_fn(4, 4, |x, y| Rgb([x as u8, y as u8, 0]));
                let mut mask = GrayImage::from_pixel(4, 4, Luma([0]));
                mask.put_pixel(2, 1, Luma([255]));
                mask.put_pixel(2, 2, Luma([255]));

                let cropped = mask_handle_with_images(rgb, mask)
                    .crop_to_content()
                    .expect("mask has content");

                assert_eq!(cropped.dimensions(), (1, 2));
                let foreground = cropped.foreground().expect("foreground should compose");
                assert_eq!(foreground.dimensions(), (1, 2));
                assert_eq!(foreground.image().get_pixel(0, 0)[0], 2);
                assert_eq!(foreground.image().get_pixel(0, 0)[1], 1);
                assert_eq!(foreground.image().get_pixel(0, 0)[3], 255);
            }

            #[test]
            fn mask_handle_crop_uses_requested_bounds() {
                let rgb = RgbImage::from_fn(4, 4, |x, y| Rgb([x as u8, y as u8, 0]));
                let mut mask = GrayImage::from_pixel(4, 4, Luma([0]));
                mask.put_pixel(2, 1, Luma([255]));

                let cropped = mask_handle_with_images(rgb, mask)
                    .crop(BoundingBox::new(1, 1, 2, 2))
                    .expect("bounds are inside the image");

                assert_eq!(cropped.dimensions(), (2, 2));
                assert_eq!(cropped.as_raw_mask().get_pixel(1, 0)[0], 255);
                let foreground = cropped.foreground().expect("foreground should compose");
                assert_eq!(foreground.image().get_pixel(1, 0)[0], 2);
                assert_eq!(foreground.image().get_pixel(1, 0)[1], 1);
                assert_eq!(foreground.image().get_pixel(1, 0)[3], 255);
            }

            #[test]
            fn mask_handle_crop_rejects_invalid_bounds() {
                assert!(mask_handle().crop(BoundingBox::new(4, 4, 2, 2)).is_none());
                assert!(mask_handle().crop(BoundingBox::new(0, 0, 0, 1)).is_none());
            }

            #[test]
            fn mask_handle_crop_to_content_with_ignores_low_values() {
                let rgb = RgbImage::from_fn(5, 3, |x, y| Rgb([x as u8, y as u8, 0]));
                let mut mask = GrayImage::from_pixel(5, 3, Luma([0]));
                mask.put_pixel(1, 1, Luma([32]));
                mask.put_pixel(3, 1, Luma([200]));

                let cropped = mask_handle_with_images(rgb, mask)
                    .crop_to_content_with(128)
                    .expect("mask has content above threshold");

                assert_eq!(cropped.dimensions(), (1, 1));
                assert_eq!(cropped.as_raw_mask().get_pixel(0, 0)[0], 200);
                let foreground = cropped.foreground().expect("foreground should compose");
                assert_eq!(foreground.image().get_pixel(0, 0)[0], 3);
                assert_eq!(foreground.image().get_pixel(0, 0)[1], 1);
                assert_eq!(foreground.image().get_pixel(0, 0)[3], 200);
            }
        }

        mod pending_operations {
            use super::*;

            #[test]
            fn mask_handle_pad_applies_pending_operations_first() {
                let padded = single_pixel_mask_handle()
                    .dilate_with(1.0)
                    .pad(Padding::new(1, 2, 0, 0));

                assert_eq!(
                    mask_bounding_box(padded.as_raw_mask(), 1),
                    Some(BoundingBox::new(2, 3, 3, 3))
                );
            }

            #[test]
            fn mask_handle_into_image_applies_pending_operations() {
                let mask = single_pixel_mask_handle().dilate_with(1.0).into_image();

                assert_eq!(
                    mask_bounding_box(&mask, 1),
                    Some(BoundingBox::new(1, 1, 3, 3))
                );
            }

            #[test]
            fn mask_handle_save_applies_pending_operations() {
                let temp_dir = tempfile::tempdir().expect("temp dir should be created");
                let path = temp_dir.path().join("mask.png");

                single_pixel_mask_handle()
                    .dilate_with(1.0)
                    .save(&path)
                    .expect("mask should save");

                let saved = image::open(&path)
                    .expect("saved mask should load")
                    .to_luma8();

                assert_eq!(
                    mask_bounding_box(&saved, 1),
                    Some(BoundingBox::new(1, 1, 3, 3))
                );
            }

            #[test]
            fn mask_handle_foreground_applies_pending_operations() {
                let foreground = single_pixel_mask_handle()
                    .dilate_with(1.0)
                    .foreground()
                    .expect("foreground should compose");

                assert_eq!(foreground.image().get_pixel(1, 2)[3], 255);
                assert_eq!(foreground.image().get_pixel(0, 0)[3], 0);
            }

            #[test]
            fn mask_handle_colorize_applies_pending_operations() {
                let colorized = single_pixel_mask_handle()
                    .dilate_with(1.0)
                    .colorize([0, 180, 255, 255]);

                assert_eq!(colorized.get_pixel(1, 2).0, [0, 180, 255, 255]);
                assert_eq!(colorized.get_pixel(0, 0).0, [0, 180, 255, 0]);
            }

            #[test]
            fn mask_handle_trace_applies_pending_operations() {
                let bounds = single_pixel_mask_handle()
                    .dilate_with(1.0)
                    .trace(&BoundingBoxVectorizer, &())
                    .expect("trace should run");

                assert_eq!(bounds, Some(BoundingBox::new(1, 1, 3, 3)));
            }
        }
    }

    #[cfg(feature = "vectorizer-vtracer")]
    mod gray_to_color_image_rgba {
        use super::*;

        mod unit {
            use super::*;

            #[test]
            fn passthrough_no_threshold_no_invert() {
                let mut input = GrayImage::new(2, 1);
                input.put_pixel(0, 0, Luma([0]));
                input.put_pixel(1, 0, Luma([128]));

                let result = gray_to_color_image_rgba(&input, None, false);

                assert_eq!(result.width, 2);
                assert_eq!(result.height, 1);
                // pixel 0: gray=0
                assert_eq!(result.pixels[0..4], [0, 0, 0, 255]);
                // pixel 1: gray=128
                assert_eq!(result.pixels[4..8], [128, 128, 128, 255]);
            }

            #[test]
            fn threshold_boundary() {
                // threshold=128: >= 128 becomes 255, < 128 becomes 0
                let mut input = GrayImage::new(3, 1);
                input.put_pixel(0, 0, Luma([127])); // below
                input.put_pixel(1, 0, Luma([128])); // exact
                input.put_pixel(2, 0, Luma([129])); // above

                let result = gray_to_color_image_rgba(&input, Some(128), false);

                assert_eq!(result.pixels[0..4], [0, 0, 0, 255]); // 127 < 128
                assert_eq!(result.pixels[4..8], [255, 255, 255, 255]); // 128 >= 128
                assert_eq!(result.pixels[8..12], [255, 255, 255, 255]); // 129 >= 128
            }

            #[test]
            fn invert_without_threshold() {
                let mut input = GrayImage::new(2, 1);
                input.put_pixel(0, 0, Luma([0]));
                input.put_pixel(1, 0, Luma([255]));

                let result = gray_to_color_image_rgba(&input, None, true);

                assert_eq!(result.pixels[0..4], [255, 255, 255, 255]); // 255 - 0
                assert_eq!(result.pixels[4..8], [0, 0, 0, 255]); // 255 - 255
            }

            #[test]
            fn invert_with_threshold() {
                let mut input = GrayImage::new(2, 1);
                input.put_pixel(0, 0, Luma([100])); // < 128, becomes 0, then inverted to 255
                input.put_pixel(1, 0, Luma([200])); // >= 128, becomes 255, then inverted to 0

                let result = gray_to_color_image_rgba(&input, Some(128), true);

                assert_eq!(result.pixels[0..4], [255, 255, 255, 255]);
                assert_eq!(result.pixels[4..8], [0, 0, 0, 255]);
            }

            #[test]
            fn alpha_always_255() {
                let input = gray_image(3, 3, 100);
                let result = gray_to_color_image_rgba(&input, None, false);

                // check every 4th byte (alpha channel)
                for i in 0..9 {
                    assert_eq!(result.pixels[i * 4 + 3], 255);
                }
            }

            #[test]
            fn dimensions_correct() {
                let input = gray_image(7, 5, 128);
                let result = gray_to_color_image_rgba(&input, None, false);

                assert_eq!(result.width, 7);
                assert_eq!(result.height, 5);
                assert_eq!(result.pixels.len(), 7 * 5 * 4);
            }
        }
    }
}