leptonica 0.1.0

Rust port of Leptonica image processing library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
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
//! Image scaling operations
//!
//! Provides various scaling algorithms including:
//! - Linear interpolation (for upscaling)
//! - Sampling (nearest neighbor)
//! - Area mapping (for downscaling with anti-aliasing)

use crate::core::{Pix, PixelDepth, pixel};
use crate::transform::{TransformError, TransformResult};

/// Scaling method to use
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScaleMethod {
    /// Nearest-neighbor sampling (fastest, pixelated results)
    Sampling,
    /// Bilinear interpolation (good for upscaling)
    Linear,
    /// Area mapping (best for downscaling, anti-aliased)
    AreaMap,
    /// Automatic selection based on scale factor
    Auto,
}

/// Mode for grayscale min/max downscaling
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GrayMinMaxMode {
    /// Select minimum value in block (darkest)
    Min,
    /// Select maximum value in block (lightest)
    Max,
    /// Select max minus min value in block (contrast)
    MaxDiff,
}

/// Scale an image by the given factors
///
/// # Arguments
/// * `pix` - Input image
/// * `scale_x` - Horizontal scale factor (e.g., 2.0 = double width)
/// * `scale_y` - Vertical scale factor
/// * `method` - Scaling algorithm to use
pub fn scale(pix: &Pix, scale_x: f32, scale_y: f32, method: ScaleMethod) -> TransformResult<Pix> {
    if scale_x <= 0.0 || scale_y <= 0.0 {
        return Err(TransformError::InvalidScaleFactor(format!(
            "scale factors must be positive: ({}, {})",
            scale_x, scale_y
        )));
    }

    let w = pix.width();
    let h = pix.height();
    let new_w = ((w as f32) * scale_x).round() as u32;
    let new_h = ((h as f32) * scale_y).round() as u32;

    if new_w == 0 || new_h == 0 {
        return Err(TransformError::InvalidScaleFactor(
            "resulting dimensions would be zero".to_string(),
        ));
    }

    let method = match method {
        ScaleMethod::Auto => {
            // Choose method based on scale factors
            let min_scale = scale_x.min(scale_y);
            if min_scale < 0.7 {
                ScaleMethod::AreaMap
            } else {
                ScaleMethod::Linear
            }
        }
        m => m,
    };

    match method {
        ScaleMethod::Sampling => scale_by_sampling_impl(pix, new_w, new_h),
        ScaleMethod::Linear => scale_linear(pix, new_w, new_h),
        ScaleMethod::AreaMap => scale_area_map(pix, scale_x, scale_y, new_w, new_h),
        ScaleMethod::Auto => unreachable!(),
    }
}

/// Scale an image to a specific size
///
/// # Arguments
/// * `pix` - Input image
/// * `width` - Target width (0 to maintain aspect ratio)
/// * `height` - Target height (0 to maintain aspect ratio)
pub fn scale_to_size(pix: &Pix, width: u32, height: u32) -> TransformResult<Pix> {
    let w = pix.width();
    let h = pix.height();

    let (target_w, target_h) = match (width, height) {
        (0, 0) => return Ok(pix.deep_clone()),
        (0, h_target) => {
            let scale = h_target as f32 / h as f32;
            ((w as f32 * scale).round() as u32, h_target)
        }
        (w_target, 0) => {
            let scale = w_target as f32 / w as f32;
            (w_target, (h as f32 * scale).round() as u32)
        }
        (w_target, h_target) => (w_target, h_target),
    };

    let scale_x = target_w as f32 / w as f32;
    let scale_y = target_h as f32 / h as f32;

    scale(pix, scale_x, scale_y, ScaleMethod::Auto)
}

/// Scale an image using nearest-neighbor sampling
pub fn scale_by_sampling(pix: &Pix, scale_x: f32, scale_y: f32) -> TransformResult<Pix> {
    scale(pix, scale_x, scale_y, ScaleMethod::Sampling)
}

/// Scale using bilinear (linear) interpolation.
///
/// For 8bpp (grayscale) and 32bpp (color) images.
/// If scale factors are both < 0.7, redirects to [`scale_general`] with area mapping.
pub fn scale_li(pix: &Pix, scale_x: f32, scale_y: f32) -> TransformResult<Pix> {
    if scale_x <= 0.0 || scale_y <= 0.0 {
        return Err(TransformError::InvalidScaleFactor(format!(
            "scale factors must be positive: ({}, {})",
            scale_x, scale_y
        )));
    }
    let max_scale = scale_x.max(scale_y);
    if max_scale < 0.7 {
        return scale_general(pix, scale_x, scale_y, 0.0, 0);
    }
    let depth = pix.depth();
    match depth {
        PixelDepth::Bit32 => scale_color_li(pix, scale_x, scale_y),
        PixelDepth::Bit8 => scale_gray_li(pix, scale_x, scale_y),
        _ => {
            let new_w = ((pix.width() as f32) * scale_x).round() as u32;
            let new_h = ((pix.height() as f32) * scale_y).round() as u32;
            scale_by_sampling_impl(pix, new_w.max(1), new_h.max(1))
        }
    }
}

/// Scale a 32bpp color image using bilinear interpolation.
///
/// If scale factors are both < 0.7, redirects to area mapping via [`scale_general`].
pub fn scale_color_li(pix: &Pix, scale_x: f32, scale_y: f32) -> TransformResult<Pix> {
    if scale_x <= 0.0 || scale_y <= 0.0 {
        return Err(TransformError::InvalidScaleFactor(format!(
            "scale factors must be positive: ({}, {})",
            scale_x, scale_y
        )));
    }
    if pix.depth() != PixelDepth::Bit32 {
        return Err(TransformError::InvalidParameters(
            "scale_color_li requires 32bpp input".to_string(),
        ));
    }
    let max_scale = scale_x.max(scale_y);
    if max_scale < 0.7 {
        return scale_general(pix, scale_x, scale_y, 0.0, 0);
    }
    let new_w = ((pix.width() as f32) * scale_x).round() as u32;
    let new_h = ((pix.height() as f32) * scale_y).round() as u32;
    scale_linear_color(pix, new_w.max(1), new_h.max(1))
}

/// Scale an 8bpp grayscale image using bilinear interpolation.
///
/// If scale factors are both < 0.7, redirects to area mapping via [`scale_general`].
pub fn scale_gray_li(pix: &Pix, scale_x: f32, scale_y: f32) -> TransformResult<Pix> {
    if scale_x <= 0.0 || scale_y <= 0.0 {
        return Err(TransformError::InvalidScaleFactor(format!(
            "scale factors must be positive: ({}, {})",
            scale_x, scale_y
        )));
    }
    if pix.depth() != PixelDepth::Bit8 {
        return Err(TransformError::InvalidParameters(
            "scale_gray_li requires 8bpp input".to_string(),
        ));
    }
    let max_scale = scale_x.max(scale_y);
    if max_scale < 0.7 {
        return scale_general(pix, scale_x, scale_y, 0.0, 0);
    }
    let new_w = ((pix.width() as f32) * scale_x).round() as u32;
    let new_h = ((pix.height() as f32) * scale_y).round() as u32;
    scale_linear_gray(pix, new_w.max(1), new_h.max(1))
}

/// General-purpose scaling with optional sharpening.
///
/// Dispatches to the appropriate method based on scale factors:
/// - For 1bpp: nearest-neighbor sampling
/// - For `max_scale < 0.7`: smooth (box-filter) or area mapping
/// - Otherwise: linear interpolation (LI)
///
/// **Note**: The `sharpfract` and `sharpwidth` parameters are accepted for API compatibility
/// but sharpening is not applied. Use an external unsharp-masking step if needed.
pub fn scale_general(
    pix: &Pix,
    scale_x: f32,
    scale_y: f32,
    _sharpfract: f32,
    _sharpwidth: i32,
) -> TransformResult<Pix> {
    if scale_x <= 0.0 || scale_y <= 0.0 {
        return Err(TransformError::InvalidScaleFactor(format!(
            "scale factors must be positive: ({}, {})",
            scale_x, scale_y
        )));
    }
    if scale_x == 1.0 && scale_y == 1.0 {
        return Ok(pix.deep_clone());
    }

    let depth = pix.depth();
    // 1bpp: fall through to sampling
    if depth == PixelDepth::Bit1 {
        let new_w = ((pix.width() as f32) * scale_x).round() as u32;
        let new_h = ((pix.height() as f32) * scale_y).round() as u32;
        return scale_by_sampling_impl(pix, new_w.max(1), new_h.max(1));
    }

    let max_scale = scale_x.max(scale_y);
    let min_scale = scale_x.min(scale_y);

    if max_scale < 0.7 {
        if min_scale < 0.02 {
            scale_smooth(pix, scale_x, scale_y)
        } else {
            let new_w = ((pix.width() as f32) * scale_x).round() as u32;
            let new_h = ((pix.height() as f32) * scale_y).round() as u32;
            scale_area_map(pix, scale_x, scale_y, new_w.max(1), new_h.max(1))
        }
    } else {
        // Linear interpolation
        let new_w = ((pix.width() as f32) * scale_x).round() as u32;
        let new_h = ((pix.height() as f32) * scale_y).round() as u32;
        scale_linear(pix, new_w.max(1), new_h.max(1))
    }
}

/// Scale to a target resolution.
///
/// If the image has a known resolution (`xres > 0`), the scale factor is computed as
/// `target / xres`. If the resolution is unknown (0), `assumed` is used instead;
/// passing `assumed == 0.0` returns a copy of the input unchanged.
pub fn scale_to_resolution(pix: &Pix, target: f32, assumed: f32) -> TransformResult<Pix> {
    if target <= 0.0 {
        return Err(TransformError::InvalidParameters(
            "target resolution must be > 0".to_string(),
        ));
    }
    let xres = pix.xres();
    let effective_res = if xres > 0 {
        xres as f32
    } else if assumed <= 0.0 {
        return Ok(pix.deep_clone());
    } else {
        assumed
    };
    let factor = target / effective_res;
    scale(pix, factor, factor, ScaleMethod::Auto)
}

/// Scale using nearest-neighbor sampling with a configurable half-pixel shift.
///
/// `shift_x` / `shift_y` must be either `0.0` or `0.5`.
/// The default (used by [`scale_by_sampling`]) is `0.5`.
/// Using `0.0` minimizes edge effects near image borders.
pub fn scale_by_sampling_with_shift(
    pix: &Pix,
    scale_x: f32,
    scale_y: f32,
    shift_x: f32,
    shift_y: f32,
) -> TransformResult<Pix> {
    if scale_x <= 0.0 || scale_y <= 0.0 {
        return Err(TransformError::InvalidScaleFactor(format!(
            "scale factors must be positive: ({}, {})",
            scale_x, scale_y
        )));
    }
    if (shift_x != 0.0 && shift_x != 0.5) || (shift_y != 0.0 && shift_y != 0.5) {
        return Err(TransformError::InvalidParameters(
            "shift must be 0.0 or 0.5".to_string(),
        ));
    }
    let w = pix.width();
    let h = pix.height();
    let new_w = ((w as f32) * scale_x).round() as u32;
    let new_h = ((h as f32) * scale_y).round() as u32;
    if new_w == 0 || new_h == 0 {
        return Err(TransformError::InvalidScaleFactor(
            "resulting dimensions would be zero".to_string(),
        ));
    }

    let depth = pix.depth();
    let out_pix = Pix::new(new_w, new_h, depth)?;
    let mut out_mut = out_pix.try_into_mut().unwrap();
    if let Some(cmap) = pix.colormap() {
        let _ = out_mut.set_colormap(Some(cmap.clone()));
    }

    let inv_x = w as f32 / new_w as f32;
    let inv_y = h as f32 / new_h as f32;

    for y in 0..new_h {
        let src_y = ((y as f32 + shift_y) * inv_y) as u32;
        let src_y = src_y.min(h - 1);
        for x in 0..new_w {
            let src_x = ((x as f32 + shift_x) * inv_x) as u32;
            let src_x = src_x.min(w - 1);
            out_mut.set_pixel_unchecked(x, y, pix.get_pixel_unchecked(src_x, src_y));
        }
    }

    Ok(out_mut.into())
}

/// Scale by an integer subsampling factor (isotropic downsampling).
///
/// A `factor` of 1 returns a copy. For `factor >= 2`, every `factor`-th pixel
/// is sampled (equivalent to `scale_by_sampling(pix, 1/factor, 1/factor)`).
pub fn scale_by_int_sampling(pix: &Pix, factor: u32) -> TransformResult<Pix> {
    if factor == 0 {
        return Err(TransformError::InvalidParameters(
            "factor must be >= 1".to_string(),
        ));
    }
    if factor == 1 {
        return Ok(pix.deep_clone());
    }
    let scale = 1.0 / factor as f32;
    scale_by_sampling(pix, scale, scale)
}

/// Smooth downscaling using a box filter followed by subsampling.
///
/// Should only be used when both scale factors are < 0.7. For larger factors,
/// this redirects to [`scale_general`] with LI.
///
/// The box filter width is `max(2, round(1 / min_scale))`, ensuring adequate
/// anti-aliasing.
pub fn scale_smooth(pix: &Pix, scale_x: f32, scale_y: f32) -> TransformResult<Pix> {
    if scale_x <= 0.0 || scale_y <= 0.0 {
        return Err(TransformError::InvalidScaleFactor(format!(
            "scale factors must be positive: ({}, {})",
            scale_x, scale_y
        )));
    }
    if scale_x >= 0.7 || scale_y >= 0.7 {
        return scale_general(pix, scale_x, scale_y, 0.0, 0);
    }

    let depth = pix.depth();
    let w = pix.width();
    let h = pix.height();
    let new_w = ((w as f32) * scale_x).round() as u32;
    let new_h = ((h as f32) * scale_y).round() as u32;
    if new_w == 0 || new_h == 0 {
        return Err(TransformError::InvalidScaleFactor(
            "resulting dimensions would be zero".to_string(),
        ));
    }

    let min_scale = scale_x.min(scale_y);
    let isize = (1.0 / min_scale + 0.5).floor() as u32;
    let isize = isize.clamp(2, 10000);

    match depth {
        PixelDepth::Bit8 if pix.colormap().is_none() => {
            scale_smooth_gray(pix, scale_x, scale_y, new_w, new_h, isize)
        }
        PixelDepth::Bit32 => scale_smooth_color(pix, scale_x, scale_y, new_w, new_h, isize),
        _ => scale_area_map(pix, scale_x, scale_y, new_w, new_h),
    }
}

/// Box-filter smooth downscale for 8bpp grayscale
fn scale_smooth_gray(
    pix: &Pix,
    scale_x: f32,
    scale_y: f32,
    new_w: u32,
    new_h: u32,
    isize: u32,
) -> TransformResult<Pix> {
    let w = pix.width();
    let h = pix.height();
    let out_pix = Pix::new(new_w, new_h, PixelDepth::Bit8)?;
    let mut out_mut = out_pix.try_into_mut().unwrap();

    let half = isize / 2;
    for yd in 0..new_h {
        let ys_center = (yd as f32 / scale_y + 0.5) as i32;
        for xd in 0..new_w {
            let xs_center = (xd as f32 / scale_x + 0.5) as i32;
            let mut sum: u32 = 0;
            let mut count: u32 = 0;
            for dy in 0..isize {
                let ys = ys_center - half as i32 + dy as i32;
                if ys < 0 || ys >= h as i32 {
                    continue;
                }
                for dx in 0..isize {
                    let xs = xs_center - half as i32 + dx as i32;
                    if xs < 0 || xs >= w as i32 {
                        continue;
                    }
                    sum += pix.get_pixel_unchecked(xs as u32, ys as u32);
                    count += 1;
                }
            }
            let val = if count > 0 {
                ((sum as f32 / count as f32) + 0.5) as u32
            } else {
                0
            };
            out_mut.set_pixel_unchecked(xd, yd, val.min(255));
        }
    }
    Ok(out_mut.into())
}

/// Box-filter smooth downscale for 32bpp color
fn scale_smooth_color(
    pix: &Pix,
    scale_x: f32,
    scale_y: f32,
    new_w: u32,
    new_h: u32,
    isize: u32,
) -> TransformResult<Pix> {
    let w = pix.width();
    let h = pix.height();
    let out_pix = Pix::new(new_w, new_h, PixelDepth::Bit32)?;
    let mut out_mut = out_pix.try_into_mut().unwrap();
    out_mut.set_spp(pix.spp());

    let half = isize / 2;
    for yd in 0..new_h {
        let ys_center = (yd as f32 / scale_y + 0.5) as i32;
        for xd in 0..new_w {
            let xs_center = (xd as f32 / scale_x + 0.5) as i32;
            let mut sum_r: u32 = 0;
            let mut sum_g: u32 = 0;
            let mut sum_b: u32 = 0;
            let mut sum_a: u32 = 0;
            let mut count: u32 = 0;
            for dy in 0..isize {
                let ys = ys_center - half as i32 + dy as i32;
                if ys < 0 || ys >= h as i32 {
                    continue;
                }
                for dx in 0..isize {
                    let xs = xs_center - half as i32 + dx as i32;
                    if xs < 0 || xs >= w as i32 {
                        continue;
                    }
                    let (r, g, b, a) =
                        pixel::extract_rgba(pix.get_pixel_unchecked(xs as u32, ys as u32));
                    sum_r += r as u32;
                    sum_g += g as u32;
                    sum_b += b as u32;
                    sum_a += a as u32;
                    count += 1;
                }
            }
            let pixel = if count > 0 {
                let r = ((sum_r as f32 / count as f32) + 0.5) as u8;
                let g = ((sum_g as f32 / count as f32) + 0.5) as u8;
                let b = ((sum_b as f32 / count as f32) + 0.5) as u8;
                let a = ((sum_a as f32 / count as f32) + 0.5) as u8;
                pixel::compose_rgba(r, g, b, a)
            } else {
                0
            };
            out_mut.set_pixel_unchecked(xd, yd, pixel);
        }
    }
    Ok(out_mut.into())
}

// --- Phase 4: 1bpp→8bpp scale-to-gray functions ---

/// Scale a 1bpp binary image to 8bpp grayscale using optimal integer reduction.
///
/// Dispatches to the most appropriate `scale_to_gray_N` function based on `scale_factor`.
/// For scale factors that are not exact powers-of-two-reciprocals, binary pre-scaling
/// is applied before the nearest integer `scale_to_gray_N` step.
///
/// `scale_factor` must be in (0.0, 1.0).
pub fn scale_to_gray(pix: &Pix, scale_factor: f32) -> TransformResult<Pix> {
    if pix.depth() != PixelDepth::Bit1 {
        return Err(TransformError::InvalidParameters(
            "scale_to_gray requires 1bpp input".to_string(),
        ));
    }
    if scale_factor <= 0.0 || scale_factor >= 1.0 {
        return Err(TransformError::InvalidParameters(
            "scale_factor must be in (0.0, 1.0)".to_string(),
        ));
    }

    if scale_factor > 0.5 {
        let mag = 2.0 * scale_factor;
        let pre = scale_binary(pix, mag, mag)?;
        scale_to_gray_2(&pre)
    } else if scale_factor == 0.5 {
        scale_to_gray_2(pix)
    } else if scale_factor > 1.0 / 3.0 {
        let mag = 3.0 * scale_factor;
        let pre = scale_binary(pix, mag, mag)?;
        scale_to_gray_3(&pre)
    } else if scale_factor > 0.25 {
        let mag = 4.0 * scale_factor;
        let pre = scale_binary(pix, mag, mag)?;
        scale_to_gray_4(&pre)
    } else if scale_factor == 0.25 {
        scale_to_gray_4(pix)
    } else if scale_factor > 1.0 / 6.0 {
        let mag = 6.0 * scale_factor;
        let pre = scale_binary(pix, mag, mag)?;
        scale_to_gray_6(&pre)
    } else if scale_factor > 0.125 {
        let mag = 8.0 * scale_factor;
        let pre = scale_binary(pix, mag, mag)?;
        scale_to_gray_8(&pre)
    } else if scale_factor == 0.125 {
        scale_to_gray_8(pix)
    } else if scale_factor > 0.0625 {
        let red = 8.0 * scale_factor;
        let pre = scale_binary(pix, red, red)?;
        scale_to_gray_8(&pre)
    } else if scale_factor == 0.0625 {
        scale_to_gray_16(pix)
    } else {
        let red = 16.0 * scale_factor;
        let pre = scale_to_gray_16(pix)?;
        if red < 0.7 {
            scale_smooth(&pre, red, red)
        } else {
            scale_gray_li(&pre, red, red)
        }
    }
}

/// Faster variant of [`scale_to_gray`] for scale factors in [0.0625, 0.5].
///
/// Binary downscaling is applied first, followed by `scale_to_gray_2`.
/// Quality is slightly lower than [`scale_to_gray`] but computation is faster.
pub fn scale_to_gray_fast(pix: &Pix, scale_factor: f32) -> TransformResult<Pix> {
    if pix.depth() != PixelDepth::Bit1 {
        return Err(TransformError::InvalidParameters(
            "scale_to_gray_fast requires 1bpp input".to_string(),
        ));
    }
    if scale_factor <= 0.0 || scale_factor >= 1.0 {
        return Err(TransformError::InvalidParameters(
            "scale_factor must be in (0.0, 1.0)".to_string(),
        ));
    }

    let eps = 0.0001;
    // Exact special cases
    if (scale_factor - 0.5).abs() < eps {
        return scale_to_gray_2(pix);
    } else if (scale_factor - 1.0 / 3.0).abs() < eps {
        return scale_to_gray_3(pix);
    } else if (scale_factor - 0.25).abs() < eps {
        return scale_to_gray_4(pix);
    } else if (scale_factor - 1.0 / 6.0).abs() < eps {
        return scale_to_gray_6(pix);
    } else if (scale_factor - 0.125).abs() < eps {
        return scale_to_gray_8(pix);
    } else if (scale_factor - 0.0625).abs() < eps {
        return scale_to_gray_16(pix);
    }

    if scale_factor > 0.0625 {
        let factor = 2.0 * scale_factor;
        let pre = scale_binary(pix, factor, factor)?;
        scale_to_gray_2(&pre)
    } else {
        let factor = 16.0 * scale_factor;
        let pre = scale_to_gray_16(pix)?;
        if factor < 0.7 {
            scale_smooth(&pre, factor, factor)
        } else {
            scale_gray_li(&pre, factor, factor)
        }
    }
}

/// Scale a 1bpp image to 8bpp by averaging 2×2 pixel blocks.
///
/// Each output pixel represents the fraction of white pixels in a 2×2 source block
/// (0 = all black, 255 = all white).
/// Output dimensions: `(w/2, h/2)`.
pub fn scale_to_gray_2(pix: &Pix) -> TransformResult<Pix> {
    scale_to_gray_n(pix, 2)
}

/// Scale a 1bpp image to 8bpp by averaging 3×3 pixel blocks.
/// Output dimensions: `(w/3, h/3)`.
pub fn scale_to_gray_3(pix: &Pix) -> TransformResult<Pix> {
    scale_to_gray_n(pix, 3)
}

/// Scale a 1bpp image to 8bpp by averaging 4×4 pixel blocks.
/// Output dimensions: `(w/4, h/4)`.
pub fn scale_to_gray_4(pix: &Pix) -> TransformResult<Pix> {
    scale_to_gray_n(pix, 4)
}

/// Scale a 1bpp image to 8bpp by averaging 6×6 pixel blocks.
/// Output dimensions: `(w/6, h/6)`.
pub fn scale_to_gray_6(pix: &Pix) -> TransformResult<Pix> {
    scale_to_gray_n(pix, 6)
}

/// Scale a 1bpp image to 8bpp by averaging 8×8 pixel blocks.
/// Output dimensions: `(w/8, h/8)`.
pub fn scale_to_gray_8(pix: &Pix) -> TransformResult<Pix> {
    scale_to_gray_n(pix, 8)
}

/// Scale a 1bpp image to 8bpp by averaging 16×16 pixel blocks.
/// Output dimensions: `(w/16, h/16)`.
pub fn scale_to_gray_16(pix: &Pix) -> TransformResult<Pix> {
    scale_to_gray_n(pix, 16)
}

/// Generic N×N scale-to-gray for 1bpp input.
/// For each N×N block, counts white (0) pixels and maps to 0-255.
fn scale_to_gray_n(pix: &Pix, n: u32) -> TransformResult<Pix> {
    if pix.depth() != PixelDepth::Bit1 {
        return Err(TransformError::InvalidParameters(format!(
            "scale_to_gray_{n} requires 1bpp input"
        )));
    }
    let ws = pix.width();
    let hs = pix.height();
    let wd = ws / n;
    let hd = hs / n;
    if wd == 0 || hd == 0 {
        return Err(TransformError::InvalidParameters(format!(
            "image too small for scale_to_gray_{n}: {ws}x{hs}"
        )));
    }

    let out = Pix::new(wd, hd, PixelDepth::Bit8)?;
    let mut out_mut = out.try_into_mut().unwrap();
    let total = n * n;

    for yd in 0..hd {
        for xd in 0..wd {
            let mut white_count: u32 = 0;
            for dy in 0..n {
                let ys = yd * n + dy;
                for dx in 0..n {
                    let xs = xd * n + dx;
                    if xs < ws && ys < hs {
                        // 1bpp: 0 = white, 1 = black
                        if pix.get_pixel_unchecked(xs, ys) == 0 {
                            white_count += 1;
                        }
                    }
                }
            }
            let gray = ((white_count * 255 + total / 2) / total).min(255);
            out_mut.set_pixel_unchecked(xd, yd, gray);
        }
    }
    Ok(out_mut.into())
}

/// Replicate each pixel `factor` times in each direction.
///
/// All pixel depths (1, 2, 4, 8, 16, 32 bpp) are supported.
/// A factor of 1 returns a copy.
pub fn expand_replicate(pix: &Pix, factor: u32) -> TransformResult<Pix> {
    if factor == 0 {
        return Err(TransformError::InvalidParameters(
            "factor must be >= 1".to_string(),
        ));
    }
    if factor == 1 {
        return Ok(pix.deep_clone());
    }
    let w = pix.width();
    let h = pix.height();
    let new_w = w.checked_mul(factor).ok_or_else(|| {
        TransformError::InvalidParameters("scaled width would overflow u32".to_string())
    })?;
    let new_h = h.checked_mul(factor).ok_or_else(|| {
        TransformError::InvalidParameters("scaled height would overflow u32".to_string())
    })?;
    let depth = pix.depth();

    let out = Pix::new(new_w, new_h, depth)?;
    let mut out_mut = out.try_into_mut().unwrap();
    if let Some(cmap) = pix.colormap() {
        let _ = out_mut.set_colormap(Some(cmap.clone()));
    }
    out_mut.set_spp(pix.spp());

    for ys in 0..h {
        for xs in 0..w {
            let val = pix.get_pixel_unchecked(xs, ys);
            for dy in 0..factor {
                for dx in 0..factor {
                    out_mut.set_pixel_unchecked(xs * factor + dx, ys * factor + dy, val);
                }
            }
        }
    }
    Ok(out_mut.into())
}

/// Scale a 1bpp binary image using nearest-neighbor sampling.
///
/// This is equivalent to [`scale_by_sampling_with_shift`] with `shift = 0.5`
/// applied to a 1bpp image.
pub fn scale_binary(pix: &Pix, scale_x: f32, scale_y: f32) -> TransformResult<Pix> {
    if pix.depth() != PixelDepth::Bit1 {
        return Err(TransformError::InvalidParameters(
            "scale_binary requires 1bpp input".to_string(),
        ));
    }
    scale_by_sampling_with_shift(pix, scale_x, scale_y, 0.5, 0.5)
}

// ────────────────────────────────────────────────────────────────────────────
// Phase 5: Special scaling operations
// ────────────────────────────────────────────────────────────────────────────

/// 2× upscale of a 32bpp color image using linear interpolation.
///
/// Equivalent to `scale_color_li(pix, 2.0, 2.0)`.
pub fn scale_color_2x_li(pix: &Pix) -> TransformResult<Pix> {
    if pix.depth() != PixelDepth::Bit32 {
        return Err(TransformError::UnsupportedDepth(format!(
            "{:?}",
            pix.depth()
        )));
    }
    scale_color_li(pix, 2.0, 2.0)
}

/// 4× upscale of a 32bpp color image using linear interpolation.
///
/// Equivalent to `scale_color_li(pix, 4.0, 4.0)`.
pub fn scale_color_4x_li(pix: &Pix) -> TransformResult<Pix> {
    if pix.depth() != PixelDepth::Bit32 {
        return Err(TransformError::UnsupportedDepth(format!(
            "{:?}",
            pix.depth()
        )));
    }
    scale_color_li(pix, 4.0, 4.0)
}

/// 2× upscale of an 8bpp grayscale image using linear interpolation.
///
/// Equivalent to `scale_gray_li(pix, 2.0, 2.0)`.
pub fn scale_gray_2x_li(pix: &Pix) -> TransformResult<Pix> {
    if pix.depth() != PixelDepth::Bit8 {
        return Err(TransformError::UnsupportedDepth(format!(
            "{:?}",
            pix.depth()
        )));
    }
    if pix.colormap().is_some() {
        return Err(TransformError::InvalidParameters(
            "cmapped input not supported".to_string(),
        ));
    }
    scale_gray_li(pix, 2.0, 2.0)
}

/// 4× upscale of an 8bpp grayscale image using linear interpolation.
///
/// Equivalent to `scale_gray_li(pix, 4.0, 4.0)`.
pub fn scale_gray_4x_li(pix: &Pix) -> TransformResult<Pix> {
    if pix.depth() != PixelDepth::Bit8 {
        return Err(TransformError::UnsupportedDepth(format!(
            "{:?}",
            pix.depth()
        )));
    }
    if pix.colormap().is_some() {
        return Err(TransformError::InvalidParameters(
            "cmapped input not supported".to_string(),
        ));
    }
    scale_gray_li(pix, 4.0, 4.0)
}

/// 2× upscale of an 8bpp image with LI, then threshold to 1bpp.
///
/// `thresh` must be in `[0, 256]`.  Pixels with value `< thresh` become
/// black (1) and pixels `>= thresh` become white (0).
pub fn scale_gray_2x_li_thresh(pix: &Pix, thresh: i32) -> TransformResult<Pix> {
    if pix.depth() != PixelDepth::Bit8 {
        return Err(TransformError::UnsupportedDepth(format!(
            "{:?}",
            pix.depth()
        )));
    }
    if pix.colormap().is_some() {
        return Err(TransformError::InvalidParameters(
            "cmapped input not supported".to_string(),
        ));
    }
    if !(0..=256).contains(&thresh) {
        return Err(TransformError::InvalidParameters(format!(
            "thresh must be in [0, 256], got {thresh}"
        )));
    }
    let gray = scale_gray_2x_li(pix)?;
    gray_threshold_to_binary(&gray, thresh)
}

/// 4× upscale of an 8bpp image with LI, then threshold to 1bpp.
///
/// `thresh` must be in `[0, 256]`.  Pixels with value `< thresh` become
/// black (1) and pixels `>= thresh` become white (0).
pub fn scale_gray_4x_li_thresh(pix: &Pix, thresh: i32) -> TransformResult<Pix> {
    if pix.depth() != PixelDepth::Bit8 {
        return Err(TransformError::UnsupportedDepth(format!(
            "{:?}",
            pix.depth()
        )));
    }
    if pix.colormap().is_some() {
        return Err(TransformError::InvalidParameters(
            "cmapped input not supported".to_string(),
        ));
    }
    if !(0..=256).contains(&thresh) {
        return Err(TransformError::InvalidParameters(format!(
            "thresh must be in [0, 256], got {thresh}"
        )));
    }
    let gray = scale_gray_4x_li(pix)?;
    gray_threshold_to_binary(&gray, thresh)
}

/// 2× upscale of an 8bpp image with LI, then Floyd-Steinberg dither to 1bpp.
pub fn scale_gray_2x_li_dither(pix: &Pix) -> TransformResult<Pix> {
    if pix.depth() != PixelDepth::Bit8 {
        return Err(TransformError::UnsupportedDepth(format!(
            "{:?}",
            pix.depth()
        )));
    }
    if pix.colormap().is_some() {
        return Err(TransformError::InvalidParameters(
            "cmapped input not supported".to_string(),
        ));
    }
    let gray = scale_gray_2x_li(pix)?;
    gray_floyd_steinberg_dither(&gray)
}

/// 4× upscale of an 8bpp image with LI, then Floyd-Steinberg dither to 1bpp.
pub fn scale_gray_4x_li_dither(pix: &Pix) -> TransformResult<Pix> {
    if pix.depth() != PixelDepth::Bit8 {
        return Err(TransformError::UnsupportedDepth(format!(
            "{:?}",
            pix.depth()
        )));
    }
    if pix.colormap().is_some() {
        return Err(TransformError::InvalidParameters(
            "cmapped input not supported".to_string(),
        ));
    }
    let gray = scale_gray_4x_li(pix)?;
    gray_floyd_steinberg_dither(&gray)
}

/// Downscale an 8bpp image by selecting the min, max, or max-minus-min value
/// from each `xfact × yfact` block.
///
/// The output dimensions are `(w / xfact, h / yfact)`.
/// Use [`GrayMinMaxMode`] to select which aggregate to compute.
pub fn scale_gray_min_max(
    pix: &Pix,
    xfact: u32,
    yfact: u32,
    mode: GrayMinMaxMode,
) -> TransformResult<Pix> {
    if pix.depth() != PixelDepth::Bit8 {
        return Err(TransformError::UnsupportedDepth(format!(
            "{:?}",
            pix.depth()
        )));
    }
    if pix.colormap().is_some() {
        return Err(TransformError::InvalidParameters(
            "cmapped input not supported".to_string(),
        ));
    }
    if xfact == 0 || yfact == 0 {
        return Err(TransformError::InvalidParameters(
            "xfact and yfact must be >= 1".to_string(),
        ));
    }

    let ws = pix.width();
    let hs = pix.height();
    let wd = (ws / xfact).max(1);
    let hd = (hs / yfact).max(1);
    let xfact = if ws / xfact == 0 { ws } else { xfact };
    let yfact = if hs / yfact == 0 { hs } else { yfact };

    let out = Pix::new(wd, hd, PixelDepth::Bit8)?;
    let mut out_mut = out.try_into_mut().unwrap();

    for i in 0..hd {
        for j in 0..wd {
            let mut minval: u32 = 255;
            let mut maxval: u32 = 0;
            let compute_min = mode == GrayMinMaxMode::Min || mode == GrayMinMaxMode::MaxDiff;
            let compute_max = mode == GrayMinMaxMode::Max || mode == GrayMinMaxMode::MaxDiff;

            for k in 0..yfact {
                let ys = (yfact * i + k).min(hs - 1);
                for m in 0..xfact {
                    let xs = (xfact * j + m).min(ws - 1);
                    let val = pix.get_pixel_unchecked(xs, ys);
                    if compute_min && val < minval {
                        minval = val;
                    }
                    if compute_max && val > maxval {
                        maxval = val;
                    }
                }
            }

            let out_val = match mode {
                GrayMinMaxMode::Min => minval,
                GrayMinMaxMode::Max => maxval,
                GrayMinMaxMode::MaxDiff => maxval.saturating_sub(minval),
            };
            out_mut.set_pixel_unchecked(j, i, out_val);
        }
    }
    Ok(out_mut.into())
}

/// Apply up to four cascaded 2× rank downscales to an 8bpp image.
///
/// Each level selects the rank-th value (1=darkest, 4=lightest) from a 2×2
/// block.  Use `level = 0` to stop the cascade early.  `level1` must be > 0.
///
/// The maximum reduction is 16× (four stages of 2×).
pub fn scale_gray_rank_cascade(
    pix: &Pix,
    level1: i32,
    level2: i32,
    level3: i32,
    level4: i32,
) -> TransformResult<Pix> {
    if pix.depth() != PixelDepth::Bit8 {
        return Err(TransformError::UnsupportedDepth(format!(
            "{:?}",
            pix.depth()
        )));
    }
    if pix.colormap().is_some() {
        return Err(TransformError::InvalidParameters(
            "cmapped input not supported".to_string(),
        ));
    }
    if level1 > 4 {
        return Err(TransformError::InvalidParameters(format!(
            "level1 must be in [1, 4], got {level1}"
        )));
    }
    for &lvl in &[level2, level3, level4] {
        if !(0..=4).contains(&lvl) {
            return Err(TransformError::InvalidParameters(format!(
                "levels must be in [0, 4], got {lvl}"
            )));
        }
    }
    if level1 <= 0 {
        return Ok(pix.deep_clone());
    }

    let t1 = scale_gray_rank2(pix, level1)?;
    if level2 <= 0 {
        return Ok(t1);
    }
    let t2 = scale_gray_rank2(&t1, level2)?;
    if level3 <= 0 {
        return Ok(t2);
    }
    let t3 = scale_gray_rank2(&t2, level3)?;
    if level4 <= 0 {
        return Ok(t3);
    }
    scale_gray_rank2(&t3, level4)
}

/// Scale a 1bpp image to 8bpp grayscale using mipmap-based interpolation.
///
/// `scale_factor` must be in `(0, 1)`.  Two nearby scale-to-gray reductions
/// are computed and linearly interpolated to produce the output.
///
/// Note: this method can produce noticeable aliasing; prefer [`scale_to_gray`]
/// for production use.
pub fn scale_to_gray_mipmap(pix: &Pix, scale_factor: f32) -> TransformResult<Pix> {
    if pix.depth() != PixelDepth::Bit1 {
        return Err(TransformError::UnsupportedDepth(format!(
            "{:?}",
            pix.depth()
        )));
    }
    if scale_factor <= 0.0 || scale_factor >= 1.0 {
        return Err(TransformError::InvalidScaleFactor(format!(
            "scale_factor must be in (0, 1), got {scale_factor}"
        )));
    }
    let (w, h) = (pix.width(), pix.height());
    let min_src = w.min(h);
    let min_dst = (min_src as f32 * scale_factor) as u32;
    if min_dst < 2 {
        return Err(TransformError::InvalidScaleFactor(
            "scale_factor too small: destination would be < 2 pixels".to_string(),
        ));
    }

    // Use range comparisons with a small epsilon to avoid floating-point
    // equality fragility while still correctly identifying the exact power-of-2
    // boundaries that correspond to an available pyramid level.
    const EPS: f32 = 1e-6;
    if scale_factor > 0.5 + EPS {
        // Interpolate between full 1→8 and 2× reduced
        let s1 = pix.convert_1_to_8(255, 0)?;
        let s2 = scale_to_gray_2(pix)?;
        scale_mipmap(&s1, &s2, scale_factor)
    } else if (scale_factor - 0.5).abs() < EPS {
        scale_to_gray_2(pix)
    } else if scale_factor > 0.25 + EPS {
        let s1 = scale_to_gray_2(pix)?;
        let s2 = scale_to_gray_4(pix)?;
        scale_mipmap(&s1, &s2, 2.0 * scale_factor)
    } else if (scale_factor - 0.25).abs() < EPS {
        scale_to_gray_4(pix)
    } else if scale_factor > 0.125 + EPS {
        let s1 = scale_to_gray_4(pix)?;
        let s2 = scale_to_gray_8(pix)?;
        scale_mipmap(&s1, &s2, 4.0 * scale_factor)
    } else if (scale_factor - 0.125).abs() < EPS {
        scale_to_gray_8(pix)
    } else if scale_factor > 0.0625 + EPS {
        let s1 = scale_to_gray_8(pix)?;
        let s2 = scale_to_gray_16(pix)?;
        scale_mipmap(&s1, &s2, 8.0 * scale_factor)
    } else if (scale_factor - 0.0625).abs() < EPS {
        scale_to_gray_16(pix)
    } else {
        // Beyond the pyramid: scale from 16× reduced
        let red = 16.0 * scale_factor;
        let t = scale_to_gray_16(pix)?;
        if red < 0.7 {
            scale_smooth(&t, red, red)
        } else {
            scale_gray_li(&t, red, red)
        }
    }
}

// ────────────────────────────────────────────────────────────────────────────
// Private helpers for Phase 5
// ────────────────────────────────────────────────────────────────────────────

/// Threshold an 8bpp image to 1bpp.
/// Pixels with value `< thresh` → black (1); pixels `>= thresh` → white (0).
/// `thresh = 256` means all pixels become black.
fn gray_threshold_to_binary(pix: &Pix, thresh: i32) -> TransformResult<Pix> {
    let w = pix.width();
    let h = pix.height();
    let out = Pix::new(w, h, PixelDepth::Bit1)?;
    let mut out_mut = out.try_into_mut().unwrap();
    for y in 0..h {
        for x in 0..w {
            let val = pix.get_pixel_unchecked(x, y) as i32;
            let binary: u32 = if val < thresh { 1 } else { 0 };
            out_mut.set_pixel_unchecked(x, y, binary);
        }
    }
    Ok(out_mut.into())
}

/// Floyd-Steinberg error-diffusion dithering from 8bpp to 1bpp.
///
/// In the output 1bpp image: 0 = white, 1 = black.
fn gray_floyd_steinberg_dither(pix: &Pix) -> TransformResult<Pix> {
    let w = pix.width();
    let h = pix.height();

    // Use an f32 buffer to accumulate diffused errors.
    let mut buf: Vec<f32> = Vec::with_capacity((w * h) as usize);
    for y in 0..h {
        for x in 0..w {
            buf.push(pix.get_pixel_unchecked(x, y) as f32);
        }
    }

    let out = Pix::new(w, h, PixelDepth::Bit1)?;
    let mut out_mut = out.try_into_mut().unwrap();

    for y in 0..h {
        for x in 0..w {
            let idx = (y * w + x) as usize;
            let old_val = buf[idx].clamp(0.0, 255.0);
            // Quantize: >= 128 → white (0 in 1bpp), < 128 → black (1 in 1bpp)
            let (new_val, binary) = if old_val >= 128.0 {
                (255.0_f32, 0_u32)
            } else {
                (0.0_f32, 1_u32)
            };
            out_mut.set_pixel_unchecked(x, y, binary);
            let error = old_val - new_val;

            // Distribute error to neighbours (Floyd-Steinberg weights)
            if x + 1 < w {
                buf[idx + 1] += error * 7.0 / 16.0;
            }
            if y + 1 < h {
                if x > 0 {
                    buf[((y + 1) * w + x - 1) as usize] += error * 3.0 / 16.0;
                }
                buf[((y + 1) * w + x) as usize] += error * 5.0 / 16.0;
                if x + 1 < w {
                    buf[((y + 1) * w + x + 1) as usize] += error * 1.0 / 16.0;
                }
            }
        }
    }
    Ok(out_mut.into())
}

/// Single-stage 2× grayscale rank reduction.
///
/// `rank` in `[1, 4]`: 1 = darkest, 4 = lightest.
fn scale_gray_rank2(pix: &Pix, rank: i32) -> TransformResult<Pix> {
    if !(1..=4).contains(&rank) {
        return Err(TransformError::InvalidParameters(format!(
            "rank must be in [1, 4], got {rank}"
        )));
    }

    let ws = pix.width();
    let hs = pix.height();
    if ws < 2 || hs < 2 {
        return Err(TransformError::InvalidParameters(
            "image too small for 2x rank reduction".to_string(),
        ));
    }
    let wd = ws / 2;
    let hd = hs / 2;

    let out = Pix::new(wd, hd, PixelDepth::Bit8)?;
    let mut out_mut = out.try_into_mut().unwrap();

    for i in 0..hd {
        for j in 0..wd {
            let mut vals = [
                pix.get_pixel_unchecked(2 * j, 2 * i),
                pix.get_pixel_unchecked(2 * j + 1, 2 * i),
                pix.get_pixel_unchecked(2 * j, 2 * i + 1),
                pix.get_pixel_unchecked(2 * j + 1, 2 * i + 1),
            ];
            vals.sort_unstable();
            // rank 1 = darkest (index 0), rank 4 = lightest (index 3)
            let out_val = vals[(rank - 1) as usize];
            out_mut.set_pixel_unchecked(j, i, out_val);
        }
    }
    Ok(out_mut.into())
}

/// Mipmap interpolation between two 8bpp images at adjacent pyramid levels.
///
/// `pixs1` is the higher-resolution image; `pixs2` is 2× lower resolution.
/// `scale` (in `[0.5, 1.0]`) is the reduction factor relative to `pixs1`.
fn scale_mipmap(pixs1: &Pix, pixs2: &Pix, scale: f32) -> TransformResult<Pix> {
    if pixs1.depth() != PixelDepth::Bit8 || pixs2.depth() != PixelDepth::Bit8 {
        return Err(TransformError::UnsupportedDepth(format!(
            "{:?}",
            pixs1.depth()
        )));
    }
    let ws2 = pixs2.width();
    let hs2 = pixs2.height();
    let wd = (2.0 * scale * ws2 as f32) as u32;
    let hd = (2.0 * scale * hs2 as f32) as u32;
    if wd == 0 || hd == 0 {
        return Err(TransformError::InvalidScaleFactor(
            "mipmap output dimensions would be zero".to_string(),
        ));
    }

    let out = Pix::new(wd, hd, PixelDepth::Bit8)?;
    let mut out_mut = out.try_into_mut().unwrap();

    // For each output pixel, linearly interpolate between the two source levels.
    // A weight of `scale - 0.5` blends from the high-res to the low-res image.
    let frac = (scale - 0.5) * 2.0; // 0.0 at scale=0.5, 1.0 at scale=1.0

    let ws1 = pixs1.width();
    let hs1 = pixs1.height();

    for y in 0..hd {
        let src_y1 = ((y as f32 / hd as f32) * hs1 as f32) as u32;
        let src_y2 = ((y as f32 / hd as f32) * hs2 as f32) as u32;
        let src_y1 = src_y1.min(hs1 - 1);
        let src_y2 = src_y2.min(hs2 - 1);
        for x in 0..wd {
            let src_x1 = ((x as f32 / wd as f32) * ws1 as f32) as u32;
            let src_x2 = ((x as f32 / wd as f32) * ws2 as f32) as u32;
            let src_x1 = src_x1.min(ws1 - 1);
            let src_x2 = src_x2.min(ws2 - 1);
            let v1 = pixs1.get_pixel_unchecked(src_x1, src_y1) as f32;
            let v2 = pixs2.get_pixel_unchecked(src_x2, src_y2) as f32;
            let val = (v1 * (1.0 - frac) + v2 * frac).round() as u32;
            out_mut.set_pixel_unchecked(x, y, val.min(255));
        }
    }
    Ok(out_mut.into())
}

/// Internal sampling implementation
fn scale_by_sampling_impl(pix: &Pix, new_w: u32, new_h: u32) -> TransformResult<Pix> {
    let w = pix.width();
    let h = pix.height();
    let depth = pix.depth();

    let out_pix = Pix::new(new_w, new_h, depth)?;
    let mut out_mut = out_pix.try_into_mut().unwrap();

    if let Some(cmap) = pix.colormap() {
        let _ = out_mut.set_colormap(Some(cmap.clone()));
    }

    let scale_x = w as f32 / new_w as f32;
    let scale_y = h as f32 / new_h as f32;

    for y in 0..new_h {
        let src_y = ((y as f32 + 0.5) * scale_y) as u32;
        let src_y = src_y.min(h - 1);

        for x in 0..new_w {
            let src_x = ((x as f32 + 0.5) * scale_x) as u32;
            let src_x = src_x.min(w - 1);

            let val = pix.get_pixel_unchecked(src_x, src_y);
            out_mut.set_pixel_unchecked(x, y, val);
        }
    }

    Ok(out_mut.into())
}

/// Scale using bilinear interpolation
fn scale_linear(pix: &Pix, new_w: u32, new_h: u32) -> TransformResult<Pix> {
    let depth = pix.depth();

    match depth {
        PixelDepth::Bit32 => scale_linear_color(pix, new_w, new_h),
        PixelDepth::Bit8 => scale_linear_gray(pix, new_w, new_h),
        _ => {
            // For other depths, fall back to sampling
            scale_by_sampling_impl(pix, new_w, new_h)
        }
    }
}

/// Bilinear interpolation for 32bpp color images
fn scale_linear_color(pix: &Pix, new_w: u32, new_h: u32) -> TransformResult<Pix> {
    let w = pix.width();
    let h = pix.height();

    let out_pix = Pix::new(new_w, new_h, PixelDepth::Bit32)?;
    let mut out_mut = out_pix.try_into_mut().unwrap();
    out_mut.set_spp(pix.spp());

    let scale_x = (w as f32 - 1.0) / (new_w as f32 - 1.0).max(1.0);
    let scale_y = (h as f32 - 1.0) / (new_h as f32 - 1.0).max(1.0);

    for y in 0..new_h {
        let src_y = y as f32 * scale_y;
        let y0 = src_y.floor() as u32;
        let y1 = (y0 + 1).min(h - 1);
        let fy = src_y - y0 as f32;

        for x in 0..new_w {
            let src_x = x as f32 * scale_x;
            let x0 = src_x.floor() as u32;
            let x1 = (x0 + 1).min(w - 1);
            let fx = src_x - x0 as f32;

            // Get 4 corner pixels
            let p00 = pix.get_pixel_unchecked(x0, y0);
            let p10 = pix.get_pixel_unchecked(x1, y0);
            let p01 = pix.get_pixel_unchecked(x0, y1);
            let p11 = pix.get_pixel_unchecked(x1, y1);

            // Interpolate each channel
            let result = interpolate_color(p00, p10, p01, p11, fx, fy);
            out_mut.set_pixel_unchecked(x, y, result);
        }
    }

    Ok(out_mut.into())
}

/// Bilinear interpolation for 8bpp grayscale images
fn scale_linear_gray(pix: &Pix, new_w: u32, new_h: u32) -> TransformResult<Pix> {
    let w = pix.width();
    let h = pix.height();

    // If has colormap, convert values
    if pix.colormap().is_some() {
        return scale_by_sampling_impl(pix, new_w, new_h);
    }

    let out_pix = Pix::new(new_w, new_h, PixelDepth::Bit8)?;
    let mut out_mut = out_pix.try_into_mut().unwrap();

    let scale_x = (w as f32 - 1.0) / (new_w as f32 - 1.0).max(1.0);
    let scale_y = (h as f32 - 1.0) / (new_h as f32 - 1.0).max(1.0);

    for y in 0..new_h {
        let src_y = y as f32 * scale_y;
        let y0 = src_y.floor() as u32;
        let y1 = (y0 + 1).min(h - 1);
        let fy = src_y - y0 as f32;

        for x in 0..new_w {
            let src_x = x as f32 * scale_x;
            let x0 = src_x.floor() as u32;
            let x1 = (x0 + 1).min(w - 1);
            let fx = src_x - x0 as f32;

            // Get 4 corner pixels
            let p00 = pix.get_pixel_unchecked(x0, y0) as f32;
            let p10 = pix.get_pixel_unchecked(x1, y0) as f32;
            let p01 = pix.get_pixel_unchecked(x0, y1) as f32;
            let p11 = pix.get_pixel_unchecked(x1, y1) as f32;

            // Bilinear interpolation
            let top = p00 * (1.0 - fx) + p10 * fx;
            let bottom = p01 * (1.0 - fx) + p11 * fx;
            let result = (top * (1.0 - fy) + bottom * fy).round() as u32;

            out_mut.set_pixel_unchecked(x, y, result);
        }
    }

    Ok(out_mut.into())
}

/// Interpolate between 4 color pixels using bilinear weights
fn interpolate_color(p00: u32, p10: u32, p01: u32, p11: u32, fx: f32, fy: f32) -> u32 {
    let (r00, g00, b00, a00) = pixel::extract_rgba(p00);
    let (r10, g10, b10, a10) = pixel::extract_rgba(p10);
    let (r01, g01, b01, a01) = pixel::extract_rgba(p01);
    let (r11, g11, b11, a11) = pixel::extract_rgba(p11);

    let r = interpolate_channel(r00, r10, r01, r11, fx, fy);
    let g = interpolate_channel(g00, g10, g01, g11, fx, fy);
    let b = interpolate_channel(b00, b10, b01, b11, fx, fy);
    let a = interpolate_channel(a00, a10, a01, a11, fx, fy);

    pixel::compose_rgba(r, g, b, a)
}

/// Bilinear interpolation for a single channel
fn interpolate_channel(c00: u8, c10: u8, c01: u8, c11: u8, fx: f32, fy: f32) -> u8 {
    let c00 = c00 as f32;
    let c10 = c10 as f32;
    let c01 = c01 as f32;
    let c11 = c11 as f32;

    let top = c00 * (1.0 - fx) + c10 * fx;
    let bottom = c01 * (1.0 - fx) + c11 * fx;
    let result = top * (1.0 - fy) + bottom * fy;

    result.round().clamp(0.0, 255.0) as u8
}

/// Scale using area mapping (for downscaling with anti-aliasing)
fn scale_area_map(
    pix: &Pix,
    scale_x: f32,
    scale_y: f32,
    new_w: u32,
    new_h: u32,
) -> TransformResult<Pix> {
    let depth = pix.depth();

    match depth {
        PixelDepth::Bit32 => scale_area_map_color(pix, scale_x, scale_y, new_w, new_h),
        PixelDepth::Bit8 if pix.colormap().is_none() => {
            scale_area_map_gray(pix, scale_x, scale_y, new_w, new_h)
        }
        _ => {
            // For other depths, fall back to sampling
            scale_by_sampling_impl(pix, new_w, new_h)
        }
    }
}

/// Area mapping for color images
fn scale_area_map_color(
    pix: &Pix,
    scale_x: f32,
    scale_y: f32,
    new_w: u32,
    new_h: u32,
) -> TransformResult<Pix> {
    let w = pix.width();
    let h = pix.height();

    let out_pix = Pix::new(new_w, new_h, PixelDepth::Bit32)?;
    let mut out_mut = out_pix.try_into_mut().unwrap();
    out_mut.set_spp(pix.spp());

    let inv_scale_x = 1.0 / scale_x;
    let inv_scale_y = 1.0 / scale_y;

    for y in 0..new_h {
        let src_y_start = y as f32 * inv_scale_y;
        let src_y_end = ((y + 1) as f32 * inv_scale_y).min(h as f32);

        for x in 0..new_w {
            let src_x_start = x as f32 * inv_scale_x;
            let src_x_end = ((x + 1) as f32 * inv_scale_x).min(w as f32);

            let pixel = area_average_color(pix, src_x_start, src_y_start, src_x_end, src_y_end);
            out_mut.set_pixel_unchecked(x, y, pixel);
        }
    }

    Ok(out_mut.into())
}

/// Area mapping for grayscale images
fn scale_area_map_gray(
    pix: &Pix,
    scale_x: f32,
    scale_y: f32,
    new_w: u32,
    new_h: u32,
) -> TransformResult<Pix> {
    let w = pix.width();
    let h = pix.height();

    let out_pix = Pix::new(new_w, new_h, PixelDepth::Bit8)?;
    let mut out_mut = out_pix.try_into_mut().unwrap();

    let inv_scale_x = 1.0 / scale_x;
    let inv_scale_y = 1.0 / scale_y;

    for y in 0..new_h {
        let src_y_start = y as f32 * inv_scale_y;
        let src_y_end = ((y + 1) as f32 * inv_scale_y).min(h as f32);

        for x in 0..new_w {
            let src_x_start = x as f32 * inv_scale_x;
            let src_x_end = ((x + 1) as f32 * inv_scale_x).min(w as f32);

            let val = area_average_gray(pix, src_x_start, src_y_start, src_x_end, src_y_end);
            out_mut.set_pixel_unchecked(x, y, val as u32);
        }
    }

    Ok(out_mut.into())
}

/// Calculate area-weighted average for color pixels
fn area_average_color(pix: &Pix, x0: f32, y0: f32, x1: f32, y1: f32) -> u32 {
    let w = pix.width();
    let h = pix.height();

    let ix0 = x0.floor() as u32;
    let iy0 = y0.floor() as u32;
    let ix1 = (x1.ceil() as u32).min(w);
    let iy1 = (y1.ceil() as u32).min(h);

    let mut sum_r = 0.0f32;
    let mut sum_g = 0.0f32;
    let mut sum_b = 0.0f32;
    let mut sum_a = 0.0f32;
    let mut total_weight = 0.0f32;

    for sy in iy0..iy1 {
        // Calculate vertical weight
        let y_weight = if sy == iy0 {
            1.0 - (y0 - sy as f32)
        } else if sy + 1 >= iy1 {
            y1 - sy as f32
        } else {
            1.0
        };

        for sx in ix0..ix1 {
            // Calculate horizontal weight
            let x_weight = if sx == ix0 {
                1.0 - (x0 - sx as f32)
            } else if sx + 1 >= ix1 {
                x1 - sx as f32
            } else {
                1.0
            };

            let weight = x_weight * y_weight;
            let pixel = pix.get_pixel_unchecked(sx, sy);
            let (r, g, b, a) = pixel::extract_rgba(pixel);

            sum_r += r as f32 * weight;
            sum_g += g as f32 * weight;
            sum_b += b as f32 * weight;
            sum_a += a as f32 * weight;
            total_weight += weight;
        }
    }

    if total_weight > 0.0 {
        let r = (sum_r / total_weight).round().clamp(0.0, 255.0) as u8;
        let g = (sum_g / total_weight).round().clamp(0.0, 255.0) as u8;
        let b = (sum_b / total_weight).round().clamp(0.0, 255.0) as u8;
        let a = (sum_a / total_weight).round().clamp(0.0, 255.0) as u8;
        pixel::compose_rgba(r, g, b, a)
    } else {
        0
    }
}

/// Calculate area-weighted average for grayscale pixels
fn area_average_gray(pix: &Pix, x0: f32, y0: f32, x1: f32, y1: f32) -> u8 {
    let w = pix.width();
    let h = pix.height();

    let ix0 = x0.floor() as u32;
    let iy0 = y0.floor() as u32;
    let ix1 = (x1.ceil() as u32).min(w);
    let iy1 = (y1.ceil() as u32).min(h);

    let mut sum = 0.0f32;
    let mut total_weight = 0.0f32;

    for sy in iy0..iy1 {
        let y_weight = if sy == iy0 {
            1.0 - (y0 - sy as f32)
        } else if sy + 1 >= iy1 {
            y1 - sy as f32
        } else {
            1.0
        };

        for sx in ix0..ix1 {
            let x_weight = if sx == ix0 {
                1.0 - (x0 - sx as f32)
            } else if sx + 1 >= ix1 {
                x1 - sx as f32
            } else {
                1.0
            };

            let weight = x_weight * y_weight;
            let val = pix.get_pixel_unchecked(sx, sy);
            sum += val as f32 * weight;
            total_weight += weight;
        }
    }

    if total_weight > 0.0 {
        (sum / total_weight).round().clamp(0.0, 255.0) as u8
    } else {
        0
    }
}

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

    #[test]
    fn test_scale_sampling_up() {
        let pix = Pix::new(2, 2, PixelDepth::Bit8).unwrap();
        let mut pix_mut = pix.try_into_mut().unwrap();

        // [10, 20]
        // [30, 40]
        pix_mut.set_pixel_unchecked(0, 0, 10);
        pix_mut.set_pixel_unchecked(1, 0, 20);
        pix_mut.set_pixel_unchecked(0, 1, 30);
        pix_mut.set_pixel_unchecked(1, 1, 40);

        let pix: Pix = pix_mut.into();
        let scaled = scale(&pix, 2.0, 2.0, ScaleMethod::Sampling).unwrap();

        assert_eq!((scaled.width(), scaled.height()), (4, 4));
    }

    #[test]
    fn test_scale_sampling_down() {
        let pix = Pix::new(4, 4, PixelDepth::Bit8).unwrap();
        let mut pix_mut = pix.try_into_mut().unwrap();

        for y in 0..4 {
            for x in 0..4 {
                pix_mut.set_pixel_unchecked(x, y, x + y * 4);
            }
        }

        let pix: Pix = pix_mut.into();
        let scaled = scale(&pix, 0.5, 0.5, ScaleMethod::Sampling).unwrap();

        assert_eq!((scaled.width(), scaled.height()), (2, 2));
    }

    #[test]
    fn test_scale_to_size_aspect_ratio() {
        let pix = Pix::new(100, 200, PixelDepth::Bit8).unwrap();

        // Scale to width 50, maintaining aspect ratio
        let scaled = scale_to_size(&pix, 50, 0).unwrap();
        assert_eq!((scaled.width(), scaled.height()), (50, 100));

        // Scale to height 100, maintaining aspect ratio
        let scaled = scale_to_size(&pix, 0, 100).unwrap();
        assert_eq!((scaled.width(), scaled.height()), (50, 100));
    }

    #[test]
    fn test_scale_linear_color() {
        let pix = Pix::new(2, 2, PixelDepth::Bit32).unwrap();
        let mut pix_mut = pix.try_into_mut().unwrap();

        let black = pixel::compose_rgb(0, 0, 0);
        let white = pixel::compose_rgb(255, 255, 255);

        pix_mut.set_pixel_unchecked(0, 0, black);
        pix_mut.set_pixel_unchecked(1, 0, white);
        pix_mut.set_pixel_unchecked(0, 1, white);
        pix_mut.set_pixel_unchecked(1, 1, black);

        let pix: Pix = pix_mut.into();
        let scaled = scale(&pix, 2.0, 2.0, ScaleMethod::Linear).unwrap();

        assert_eq!((scaled.width(), scaled.height()), (4, 4));
        // Center pixels should be interpolated (grayish)
    }

    #[test]
    fn test_scale_area_map_gray() {
        let pix = Pix::new(4, 4, PixelDepth::Bit8).unwrap();
        let mut pix_mut = pix.try_into_mut().unwrap();

        // Fill with values 0-255
        for y in 0..4 {
            for x in 0..4 {
                let val = (x + y * 4) * 16;
                pix_mut.set_pixel_unchecked(x, y, val);
            }
        }

        let pix: Pix = pix_mut.into();
        let scaled = scale(&pix, 0.5, 0.5, ScaleMethod::AreaMap).unwrap();

        assert_eq!((scaled.width(), scaled.height()), (2, 2));
        // Values should be averaged from 2x2 blocks
    }

    #[test]
    fn test_scale_invalid_factor() {
        let pix = Pix::new(10, 10, PixelDepth::Bit8).unwrap();

        let result = scale(&pix, 0.0, 1.0, ScaleMethod::Sampling);
        assert!(result.is_err());

        let result = scale(&pix, -1.0, 1.0, ScaleMethod::Sampling);
        assert!(result.is_err());
    }

    #[test]
    fn test_scale_auto_method() {
        let pix = Pix::new(100, 100, PixelDepth::Bit8).unwrap();

        // Upscaling should use linear
        let scaled = scale(&pix, 2.0, 2.0, ScaleMethod::Auto).unwrap();
        assert_eq!((scaled.width(), scaled.height()), (200, 200));

        // Downscaling should use area map (but 8bpp without colormap works)
        let scaled = scale(&pix, 0.5, 0.5, ScaleMethod::Auto).unwrap();
        assert_eq!((scaled.width(), scaled.height()), (50, 50));
    }

    #[test]
    fn test_scale_1bpp_sampling() {
        let pix = Pix::new(4, 4, PixelDepth::Bit1).unwrap();
        let mut pix_mut = pix.try_into_mut().unwrap();

        // Checkerboard pattern
        for y in 0..4 {
            for x in 0..4 {
                let val = (x + y) % 2;
                pix_mut.set_pixel_unchecked(x, y, val);
            }
        }

        let pix: Pix = pix_mut.into();
        let scaled = scale(&pix, 2.0, 2.0, ScaleMethod::Sampling).unwrap();

        assert_eq!((scaled.width(), scaled.height()), (8, 8));
        assert_eq!(scaled.depth(), PixelDepth::Bit1);
    }

    // --- Phase 3: Scale拡張 - 基本 ---

    #[test]
    fn test_scale_li_gray_upscale() {
        let pix = Pix::new(4, 4, PixelDepth::Bit8).unwrap();
        let mut pix_mut = pix.try_into_mut().unwrap();
        for y in 0..4u32 {
            for x in 0..4u32 {
                pix_mut.set_pixel_unchecked(x, y, x * 30 + y * 30);
            }
        }
        let pix: Pix = pix_mut.into();
        let scaled = scale_li(&pix, 2.0, 2.0).unwrap();
        assert_eq!((scaled.width(), scaled.height()), (8, 8));
        assert_eq!(scaled.depth(), PixelDepth::Bit8);
        // top-left corner must stay the same
        assert_eq!(scaled.get_pixel(0, 0).unwrap(), 0);
        // bottom-right output corner (7,7) maps exactly to source (3,3) = 3*30+3*30 = 180
        assert_eq!(scaled.get_pixel(7, 7).unwrap(), 180);
    }

    #[test]
    fn test_scale_li_color_upscale() {
        let pix = Pix::new(4, 4, PixelDepth::Bit32).unwrap();
        let mut pix_mut = pix.try_into_mut().unwrap();
        pix_mut.set_pixel_unchecked(0, 0, pixel::compose_rgb(0, 0, 0));
        pix_mut.set_pixel_unchecked(3, 3, pixel::compose_rgb(255, 255, 255));
        let pix: Pix = pix_mut.into();
        let scaled = scale_li(&pix, 2.0, 2.0).unwrap();
        assert_eq!((scaled.width(), scaled.height()), (8, 8));
        assert_eq!(scaled.depth(), PixelDepth::Bit32);
    }

    #[test]
    fn test_scale_color_li_basic() {
        let pix = Pix::new(3, 3, PixelDepth::Bit32).unwrap();
        let mut pix_mut = pix.try_into_mut().unwrap();
        pix_mut.set_pixel_unchecked(0, 0, pixel::compose_rgb(100, 0, 0));
        pix_mut.set_pixel_unchecked(2, 0, pixel::compose_rgb(0, 100, 0));
        let pix: Pix = pix_mut.into();
        let scaled = scale_color_li(&pix, 2.0, 2.0).unwrap();
        assert_eq!((scaled.width(), scaled.height()), (6, 6));
        assert_eq!(scaled.depth(), PixelDepth::Bit32);
    }

    #[test]
    fn test_scale_gray_li_basic() {
        let pix = Pix::new(4, 4, PixelDepth::Bit8).unwrap();
        let mut pix_mut = pix.try_into_mut().unwrap();
        pix_mut.set_pixel_unchecked(0, 0, 50);
        pix_mut.set_pixel_unchecked(3, 3, 200);
        let pix: Pix = pix_mut.into();
        let scaled = scale_gray_li(&pix, 1.5, 1.5).unwrap();
        assert_eq!((scaled.width(), scaled.height()), (6, 6));
        assert_eq!(scaled.depth(), PixelDepth::Bit8);
    }

    #[test]
    fn test_scale_general_no_sharpening_gray() {
        let pix = Pix::new(100, 100, PixelDepth::Bit8).unwrap();
        let scaled = scale_general(&pix, 0.5, 0.5, 0.0, 1).unwrap();
        assert_eq!((scaled.width(), scaled.height()), (50, 50));
        assert_eq!(scaled.depth(), PixelDepth::Bit8);
    }

    #[test]
    fn test_scale_general_upscale_color() {
        let pix = Pix::new(4, 4, PixelDepth::Bit32).unwrap();
        let scaled = scale_general(&pix, 2.0, 2.0, 0.0, 1).unwrap();
        assert_eq!((scaled.width(), scaled.height()), (8, 8));
        assert_eq!(scaled.depth(), PixelDepth::Bit32);
    }

    #[test]
    fn test_scale_to_resolution_known_res() {
        let mut pix = Pix::new(100, 100, PixelDepth::Bit8)
            .unwrap()
            .try_into_mut()
            .unwrap();
        pix.set_xres(300);
        pix.set_yres(300);
        let pix: Pix = pix.into();
        // Scale from 300 DPI to 150 DPI → factor = 0.5
        let scaled = scale_to_resolution(&pix, 150.0, 300.0).unwrap();
        assert_eq!((scaled.width(), scaled.height()), (50, 50));
    }

    #[test]
    fn test_scale_to_resolution_unknown_res() {
        // No xres set → uses assumed value
        let pix = Pix::new(100, 100, PixelDepth::Bit8).unwrap();
        let scaled = scale_to_resolution(&pix, 150.0, 300.0).unwrap();
        // assumed=300 DPI → target/assumed = 150/300 = 0.5 → 50x50
        assert_eq!((scaled.width(), scaled.height()), (50, 50));
    }

    #[test]
    fn test_scale_by_sampling_with_shift_zero() {
        let pix = Pix::new(8, 8, PixelDepth::Bit8).unwrap();
        let mut pix_mut = pix.try_into_mut().unwrap();
        for y in 0..8u32 {
            for x in 0..8u32 {
                pix_mut.set_pixel_unchecked(x, y, x * 10);
            }
        }
        let pix: Pix = pix_mut.into();
        let scaled = scale_by_sampling_with_shift(&pix, 0.5, 0.5, 0.0, 0.0).unwrap();
        assert_eq!((scaled.width(), scaled.height()), (4, 4));
    }

    #[test]
    fn test_scale_by_sampling_with_shift_half() {
        let pix = Pix::new(8, 8, PixelDepth::Bit8).unwrap();
        let scaled = scale_by_sampling_with_shift(&pix, 0.5, 0.5, 0.5, 0.5).unwrap();
        assert_eq!((scaled.width(), scaled.height()), (4, 4));
    }

    #[test]
    fn test_scale_by_int_sampling_factor2() {
        let pix = Pix::new(8, 8, PixelDepth::Bit8).unwrap();
        let mut pix_mut = pix.try_into_mut().unwrap();
        for y in 0..8u32 {
            for x in 0..8u32 {
                pix_mut.set_pixel_unchecked(x, y, x * 10);
            }
        }
        let pix: Pix = pix_mut.into();
        let scaled = scale_by_int_sampling(&pix, 2).unwrap();
        assert_eq!((scaled.width(), scaled.height()), (4, 4));
        // With factor=2 and shift=0.5: src_x = (0 + 0.5) * 2 = 1 → pixel[1,0] = 10
        assert_eq!(scaled.get_pixel(0, 0).unwrap(), 10);
    }

    #[test]
    fn test_scale_by_int_sampling_factor1() {
        let pix = Pix::new(5, 5, PixelDepth::Bit8).unwrap();
        let scaled = scale_by_int_sampling(&pix, 1).unwrap();
        assert_eq!((scaled.width(), scaled.height()), (5, 5));
    }

    #[test]
    fn test_scale_smooth_gray() {
        let pix = Pix::new(20, 20, PixelDepth::Bit8).unwrap();
        let mut pix_mut = pix.try_into_mut().unwrap();
        // Alternating checkerboard
        for y in 0..20u32 {
            for x in 0..20u32 {
                pix_mut.set_pixel_unchecked(x, y, ((x + y) % 2) * 255);
            }
        }
        let pix: Pix = pix_mut.into();
        let scaled = scale_smooth(&pix, 0.5, 0.5).unwrap();
        assert_eq!((scaled.width(), scaled.height()), (10, 10));
        assert_eq!(scaled.depth(), PixelDepth::Bit8);
        // The smoothed result should be approximately 127 (average of 0 and 255)
        let center_val = scaled.get_pixel(5, 5).unwrap();
        assert!(
            center_val > 100 && center_val < 160,
            "expected ~127 but got {center_val}"
        );
    }

    #[test]
    fn test_scale_smooth_color() {
        let pix = Pix::new(20, 20, PixelDepth::Bit32).unwrap();
        let mut pix_mut = pix.try_into_mut().unwrap();
        for y in 0..20u32 {
            for x in 0..20u32 {
                let v = ((x + y) % 2) as u8 * 255;
                pix_mut.set_pixel_unchecked(x, y, pixel::compose_rgb(v, v, v));
            }
        }
        let pix: Pix = pix_mut.into();
        let scaled = scale_smooth(&pix, 0.5, 0.5).unwrap();
        assert_eq!((scaled.width(), scaled.height()), (10, 10));
        assert_eq!(scaled.depth(), PixelDepth::Bit32);
    }

    // --- Phase 4: Scale拡張 - 1bpp→8bpp変換 ---

    fn make_1bpp(w: u32, h: u32, vals: &[(u32, u32, u32)]) -> Pix {
        let pix = Pix::new(w, h, PixelDepth::Bit1).unwrap();
        let mut pix_mut = pix.try_into_mut().unwrap();
        for &(x, y, v) in vals {
            pix_mut.set_pixel_unchecked(x, y, v);
        }
        pix_mut.into()
    }

    #[test]
    fn test_scale_to_gray_2_dims() {
        let pix = make_1bpp(8, 8, &[]);
        let out = scale_to_gray_2(&pix).unwrap();
        assert_eq!((out.width(), out.height()), (4, 4));
        assert_eq!(out.depth(), PixelDepth::Bit8);
    }

    #[test]
    fn test_scale_to_gray_2_all_white() {
        // 1bpp: all 0 (white) → 8bpp: all 255
        let pix = make_1bpp(8, 8, &[]);
        let out = scale_to_gray_2(&pix).unwrap();
        assert_eq!(out.get_pixel(0, 0).unwrap(), 255);
        assert_eq!(out.get_pixel(3, 3).unwrap(), 255);
    }

    #[test]
    fn test_scale_to_gray_2_all_black() {
        // 1bpp: all 1 (black) → 8bpp: all 0
        let vals: Vec<(u32, u32, u32)> = (0..8)
            .flat_map(|y| (0..8u32).map(move |x| (x, y, 1)))
            .collect();
        let pix = make_1bpp(8, 8, &vals);
        let out = scale_to_gray_2(&pix).unwrap();
        assert_eq!(out.get_pixel(0, 0).unwrap(), 0);
    }

    #[test]
    fn test_scale_to_gray_2_half_black() {
        // Top row of each 2x2 block is black → half pixels black → gray ≈ 127-128
        let vals: Vec<(u32, u32, u32)> = (0..8u32)
            .flat_map(|y| (0..8u32).map(move |x| (x, y, if y % 2 == 0 { 1 } else { 0 })))
            .collect();
        let pix = make_1bpp(8, 8, &vals);
        let out = scale_to_gray_2(&pix).unwrap();
        let v = out.get_pixel(0, 0).unwrap();
        assert!(v == 127 || v == 128, "expected ~128 but got {v}");
    }

    #[test]
    fn test_scale_to_gray_4_dims() {
        let pix = make_1bpp(16, 16, &[]);
        let out = scale_to_gray_4(&pix).unwrap();
        assert_eq!((out.width(), out.height()), (4, 4));
        assert_eq!(out.depth(), PixelDepth::Bit8);
    }

    #[test]
    fn test_scale_to_gray_8_dims() {
        let pix = make_1bpp(32, 32, &[]);
        let out = scale_to_gray_8(&pix).unwrap();
        assert_eq!((out.width(), out.height()), (4, 4));
        assert_eq!(out.depth(), PixelDepth::Bit8);
    }

    #[test]
    fn test_scale_to_gray_3_dims() {
        let pix = make_1bpp(12, 12, &[]);
        let out = scale_to_gray_3(&pix).unwrap();
        assert_eq!((out.width(), out.height()), (4, 4));
        assert_eq!(out.depth(), PixelDepth::Bit8);
    }

    #[test]
    fn test_scale_to_gray_6_dims() {
        let pix = make_1bpp(24, 24, &[]);
        let out = scale_to_gray_6(&pix).unwrap();
        assert_eq!((out.width(), out.height()), (4, 4));
        assert_eq!(out.depth(), PixelDepth::Bit8);
    }

    #[test]
    fn test_scale_to_gray_16_dims() {
        let pix = make_1bpp(64, 64, &[]);
        let out = scale_to_gray_16(&pix).unwrap();
        assert_eq!((out.width(), out.height()), (4, 4));
        assert_eq!(out.depth(), PixelDepth::Bit8);
    }

    #[test]
    fn test_scale_to_gray_general_half() {
        // scale=0.5 should give same as scale_to_gray_2
        let pix = make_1bpp(8, 8, &[]);
        let out = scale_to_gray(&pix, 0.5).unwrap();
        assert_eq!((out.width(), out.height()), (4, 4));
        assert_eq!(out.depth(), PixelDepth::Bit8);
    }

    #[test]
    fn test_scale_to_gray_general_quarter() {
        let pix = make_1bpp(16, 16, &[]);
        let out = scale_to_gray(&pix, 0.25).unwrap();
        assert_eq!((out.width(), out.height()), (4, 4));
        assert_eq!(out.depth(), PixelDepth::Bit8);
    }

    #[test]
    fn test_scale_to_gray_fast_half() {
        let pix = make_1bpp(8, 8, &[]);
        let out = scale_to_gray_fast(&pix, 0.5).unwrap();
        assert_eq!((out.width(), out.height()), (4, 4));
        assert_eq!(out.depth(), PixelDepth::Bit8);
    }

    #[test]
    fn test_expand_replicate_8bpp() {
        let pix = Pix::new(2, 2, PixelDepth::Bit8).unwrap();
        let mut pix_mut = pix.try_into_mut().unwrap();
        pix_mut.set_pixel_unchecked(0, 0, 100);
        pix_mut.set_pixel_unchecked(1, 0, 200);
        pix_mut.set_pixel_unchecked(0, 1, 50);
        pix_mut.set_pixel_unchecked(1, 1, 150);
        let pix: Pix = pix_mut.into();
        let out = expand_replicate(&pix, 3).unwrap();
        assert_eq!((out.width(), out.height()), (6, 6));
        // Each 3x3 block must have the same value
        assert_eq!(out.get_pixel(0, 0).unwrap(), 100);
        assert_eq!(out.get_pixel(2, 2).unwrap(), 100);
        assert_eq!(out.get_pixel(3, 0).unwrap(), 200);
        assert_eq!(out.get_pixel(5, 0).unwrap(), 200);
        assert_eq!(out.get_pixel(0, 3).unwrap(), 50);
        assert_eq!(out.get_pixel(3, 3).unwrap(), 150);
    }

    #[test]
    fn test_expand_replicate_factor1() {
        let pix = Pix::new(4, 4, PixelDepth::Bit8).unwrap();
        let out = expand_replicate(&pix, 1).unwrap();
        assert_eq!((out.width(), out.height()), (4, 4));
    }

    #[test]
    fn test_scale_binary_upscale() {
        let pix = make_1bpp(4, 4, &[(1, 1, 1)]);
        let out = scale_binary(&pix, 2.0, 2.0).unwrap();
        assert_eq!((out.width(), out.height()), (8, 8));
        assert_eq!(out.depth(), PixelDepth::Bit1);
    }

    #[test]
    fn test_scale_binary_downscale() {
        let pix = make_1bpp(8, 8, &[(1, 1, 1), (4, 4, 1)]);
        let out = scale_binary(&pix, 0.5, 0.5).unwrap();
        assert_eq!((out.width(), out.height()), (4, 4));
        assert_eq!(out.depth(), PixelDepth::Bit1);
    }

    // --- Phase 5: Scale拡張 - 特殊 ---

    #[test]
    fn test_scale_color_2x_li_dims() {
        let pix = Pix::new(4, 3, PixelDepth::Bit32).unwrap();
        let out = scale_color_2x_li(&pix).unwrap();
        assert_eq!((out.width(), out.height()), (8, 6));
        assert_eq!(out.depth(), PixelDepth::Bit32);
    }

    #[test]
    fn test_scale_color_4x_li_dims() {
        let pix = Pix::new(4, 3, PixelDepth::Bit32).unwrap();
        let out = scale_color_4x_li(&pix).unwrap();
        assert_eq!((out.width(), out.height()), (16, 12));
        assert_eq!(out.depth(), PixelDepth::Bit32);
    }

    #[test]
    fn test_scale_gray_2x_li_dims() {
        let pix = Pix::new(4, 3, PixelDepth::Bit8).unwrap();
        let out = scale_gray_2x_li(&pix).unwrap();
        assert_eq!((out.width(), out.height()), (8, 6));
        assert_eq!(out.depth(), PixelDepth::Bit8);
    }

    #[test]
    fn test_scale_gray_4x_li_dims() {
        let pix = Pix::new(4, 3, PixelDepth::Bit8).unwrap();
        let out = scale_gray_4x_li(&pix).unwrap();
        assert_eq!((out.width(), out.height()), (16, 12));
        assert_eq!(out.depth(), PixelDepth::Bit8);
    }

    #[test]
    fn test_scale_gray_2x_li_thresh_dims() {
        let pix = Pix::new(4, 3, PixelDepth::Bit8).unwrap();
        let out = scale_gray_2x_li_thresh(&pix, 128).unwrap();
        assert_eq!((out.width(), out.height()), (8, 6));
        assert_eq!(out.depth(), PixelDepth::Bit1);
    }

    #[test]
    fn test_scale_gray_4x_li_thresh_dims() {
        let pix = Pix::new(4, 3, PixelDepth::Bit8).unwrap();
        let out = scale_gray_4x_li_thresh(&pix, 128).unwrap();
        assert_eq!((out.width(), out.height()), (16, 12));
        assert_eq!(out.depth(), PixelDepth::Bit1);
    }

    #[test]
    fn test_scale_gray_2x_li_dither_dims() {
        let pix = Pix::new(4, 3, PixelDepth::Bit8).unwrap();
        let out = scale_gray_2x_li_dither(&pix).unwrap();
        assert_eq!((out.width(), out.height()), (8, 6));
        assert_eq!(out.depth(), PixelDepth::Bit1);
    }

    #[test]
    fn test_scale_gray_4x_li_dither_dims() {
        let pix = Pix::new(4, 3, PixelDepth::Bit8).unwrap();
        let out = scale_gray_4x_li_dither(&pix).unwrap();
        assert_eq!((out.width(), out.height()), (16, 12));
        assert_eq!(out.depth(), PixelDepth::Bit1);
    }

    #[test]
    fn test_scale_gray_min_max_2x_min() {
        let pix = Pix::new(4, 4, PixelDepth::Bit8).unwrap();
        let mut pm = pix.try_into_mut().unwrap();
        pm.set_pixel_unchecked(0, 0, 50);
        pm.set_pixel_unchecked(1, 0, 100);
        pm.set_pixel_unchecked(0, 1, 200);
        pm.set_pixel_unchecked(1, 1, 150);
        let pix: Pix = pm.into();
        let out = scale_gray_min_max(&pix, 2, 2, GrayMinMaxMode::Min).unwrap();
        assert_eq!((out.width(), out.height()), (2, 2));
        assert_eq!(out.get_pixel(0, 0).unwrap(), 50);
    }

    #[test]
    fn test_scale_gray_min_max_2x_max() {
        let pix = Pix::new(4, 4, PixelDepth::Bit8).unwrap();
        let mut pm = pix.try_into_mut().unwrap();
        pm.set_pixel_unchecked(0, 0, 50);
        pm.set_pixel_unchecked(1, 0, 100);
        pm.set_pixel_unchecked(0, 1, 200);
        pm.set_pixel_unchecked(1, 1, 150);
        let pix: Pix = pm.into();
        let out = scale_gray_min_max(&pix, 2, 2, GrayMinMaxMode::Max).unwrap();
        assert_eq!((out.width(), out.height()), (2, 2));
        assert_eq!(out.get_pixel(0, 0).unwrap(), 200);
    }

    #[test]
    fn test_scale_gray_rank_cascade_single() {
        let pix = Pix::new(8, 8, PixelDepth::Bit8).unwrap();
        let out = scale_gray_rank_cascade(&pix, 1, 0, 0, 0).unwrap();
        assert_eq!((out.width(), out.height()), (4, 4));
    }

    #[test]
    fn test_scale_gray_rank_cascade_double() {
        let pix = Pix::new(8, 8, PixelDepth::Bit8).unwrap();
        let out = scale_gray_rank_cascade(&pix, 1, 1, 0, 0).unwrap();
        assert_eq!((out.width(), out.height()), (2, 2));
    }

    #[test]
    fn test_scale_to_gray_mipmap_half() {
        let pix = make_1bpp(16, 16, &[]);
        let out = scale_to_gray_mipmap(&pix, 0.5).unwrap();
        assert_eq!((out.width(), out.height()), (8, 8));
        assert_eq!(out.depth(), PixelDepth::Bit8);
    }

    #[test]
    fn test_scale_to_gray_mipmap_quarter() {
        let pix = make_1bpp(16, 16, &[]);
        let out = scale_to_gray_mipmap(&pix, 0.25).unwrap();
        assert_eq!((out.width(), out.height()), (4, 4));
        assert_eq!(out.depth(), PixelDepth::Bit8);
    }
}