temporal-vision 0.2.1

Deterministic temporal visual analysis: storyboards, difference maps, motion history images, and region filmstrips from frame sequences
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
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
use std::{
    collections::BTreeMap,
    fmt::Display,
    num::{NonZeroU8, NonZeroU32, NonZeroUsize},
};

use serde::{Deserialize, Deserializer, Serialize};
use sha2::{Digest, Sha256};

use crate::{
    AlgorithmDescriptor, ArtifactKind, ArtifactManifest, BinaryMask, DeclaredGap, EncodedImage,
    ErrorCode, EvidenceClass, FrameRegion, FrameSequence, GeneratedArtifact, IntegerScale, Marker,
    NormalizationKind, NormalizationParameters, NormalizationStep, ParameterValue, Parameters,
    PixelDimensions, PixelRect, ProcessingLimits, Result, Rgb8, Timestamp, VisionError,
    generator_descriptor,
    normalize::make_parameters,
    normalize_sequence,
    render::{
        canvas::{
            BLACK, Canvas, MUTED, PANEL, WARNING, WHITE, canvas_limit_error,
            canvas_output_limit_error,
        },
        font::{CELL_WIDTH, draw_text, ellipsize},
    },
};

stable_registry! {
    /// Coordinate space in which a fixed filmstrip region was declared.
    pub enum RegionCoordinateSpace {
        SourceImage => "source_image",
        Viewport => "viewport",
    }
}

/// A non-empty half-open rectangle whose origin may lie outside the source image.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, schemars::JsonSchema)]
pub struct SignedPixelRect {
    x: i64,
    y: i64,
    width: NonZeroU32,
    height: NonZeroU32,
}

impl SignedPixelRect {
    pub fn new(x: i64, y: i64, width: NonZeroU32, height: NonZeroU32) -> Result<Self> {
        let rect = Self {
            x,
            y,
            width,
            height,
        };
        rect.right_exclusive()?;
        rect.bottom_exclusive()?;
        Ok(rect)
    }

    /// Converts finite fractional bounds to the smallest containing pixel rect.
    /// Left/top round down and right/bottom round up, including below zero.
    pub fn from_outward_f64_bounds(left: f64, top: f64, right: f64, bottom: f64) -> Result<Self> {
        if ![left, top, right, bottom]
            .iter()
            .all(|value| value.is_finite())
            || right <= left
            || bottom <= top
        {
            return Err(VisionError::new(
                ErrorCode::InvalidRegion,
                "fractional region bounds must be finite and non-empty",
            ));
        }
        const I64_UPPER_EXCLUSIVE: f64 = 9_223_372_036_854_775_808.0;
        let convert = |value: f64, round: fn(f64) -> f64| {
            let value = round(value);
            if value < i64::MIN as f64 || value >= I64_UPPER_EXCLUSIVE {
                Err(VisionError::new(
                    ErrorCode::InvalidRegion,
                    "fractional region exceeds the supported coordinate space",
                ))
            } else {
                Ok(value as i64)
            }
        };
        let left = convert(left, f64::floor)?;
        let top = convert(top, f64::floor)?;
        let right = convert(right, f64::ceil)?;
        let bottom = convert(bottom, f64::ceil)?;
        let width = u32::try_from(i128::from(right) - i128::from(left))
            .ok()
            .and_then(NonZeroU32::new)
            .ok_or_else(|| {
                VisionError::new(
                    ErrorCode::InvalidRegion,
                    "fractional region width exceeds the supported coordinate space",
                )
            })?;
        let height = u32::try_from(i128::from(bottom) - i128::from(top))
            .ok()
            .and_then(NonZeroU32::new)
            .ok_or_else(|| {
                VisionError::new(
                    ErrorCode::InvalidRegion,
                    "fractional region height exceeds the supported coordinate space",
                )
            })?;
        Self::new(left, top, width, height)
    }

    pub const fn x(self) -> i64 {
        self.x
    }

    pub const fn y(self) -> i64 {
        self.y
    }

    pub const fn width(self) -> u32 {
        self.width.get()
    }

    pub const fn height(self) -> u32 {
        self.height.get()
    }

    pub fn right_exclusive(self) -> Result<i64> {
        self.x
            .checked_add(i64::from(self.width.get()))
            .ok_or_else(|| {
                VisionError::new(
                    ErrorCode::InvalidRegion,
                    "signed region exceeds the coordinate space",
                )
            })
    }

    pub fn bottom_exclusive(self) -> Result<i64> {
        self.y
            .checked_add(i64::from(self.height.get()))
            .ok_or_else(|| {
                VisionError::new(
                    ErrorCode::InvalidRegion,
                    "signed region exceeds the coordinate space",
                )
            })
    }
}

impl<'de> Deserialize<'de> for SignedPixelRect {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct Wire {
            x: i64,
            y: i64,
            width: NonZeroU32,
            height: NonZeroU32,
        }
        let wire = Wire::deserialize(deserializer)?;
        Self::new(wire.x, wire.y, wire.width, wire.height).map_err(serde::de::Error::custom)
    }
}

/// A positive exact rational scale in source pixels per declared coordinate unit.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct RationalScale {
    numerator: NonZeroU32,
    denominator: NonZeroU32,
}

impl RationalScale {
    pub const fn new(numerator: NonZeroU32, denominator: NonZeroU32) -> Self {
        Self {
            numerator,
            denominator,
        }
    }

    pub const fn numerator(self) -> u32 {
        self.numerator.get()
    }

    pub const fn denominator(self) -> u32 {
        self.denominator.get()
    }
}

/// Caller-declared mapping from viewport units to source-image pixels.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct ViewportMapping {
    viewport_dimensions: PixelDimensions,
    scale_x: RationalScale,
    scale_y: RationalScale,
}

impl ViewportMapping {
    pub const fn new(
        viewport_dimensions: PixelDimensions,
        scale_x: RationalScale,
        scale_y: RationalScale,
    ) -> Self {
        Self {
            viewport_dimensions,
            scale_x,
            scale_y,
        }
    }

    /// Builds the canonical exact viewport-to-source rational mapping.
    pub fn for_source(
        viewport_dimensions: PixelDimensions,
        source_dimensions: PixelDimensions,
    ) -> Self {
        fn reduced(numerator: u32, denominator: u32) -> RationalScale {
            fn gcd(mut left: u32, mut right: u32) -> u32 {
                while right != 0 {
                    let remainder = left % right;
                    left = right;
                    right = remainder;
                }
                left
            }
            let divisor = gcd(numerator, denominator);
            RationalScale::new(
                NonZeroU32::new(numerator / divisor).expect("source dimension is non-zero"),
                NonZeroU32::new(denominator / divisor).expect("viewport dimension is non-zero"),
            )
        }
        Self::new(
            viewport_dimensions,
            reduced(source_dimensions.width(), viewport_dimensions.width()),
            reduced(source_dimensions.height(), viewport_dimensions.height()),
        )
    }

    pub const fn viewport_dimensions(self) -> PixelDimensions {
        self.viewport_dimensions
    }

    pub const fn scale_x(self) -> RationalScale {
        self.scale_x
    }

    pub const fn scale_y(self) -> RationalScale {
        self.scale_y
    }
}

/// One fixed region declaration. Neither variant implies logical-element tracking.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(tag = "coordinate_space", rename_all = "snake_case")]
pub enum RegionDefinition {
    FixedSourceImage {
        rect: SignedPixelRect,
    },
    FixedViewport {
        rect: SignedPixelRect,
        mapping: ViewportMapping,
    },
}

impl RegionDefinition {
    pub const fn coordinate_space(self) -> RegionCoordinateSpace {
        match self {
            Self::FixedSourceImage { .. } => RegionCoordinateSpace::SourceImage,
            Self::FixedViewport { .. } => RegionCoordinateSpace::Viewport,
        }
    }

    pub const fn rect(self) -> SignedPixelRect {
        match self {
            Self::FixedSourceImage { rect } | Self::FixedViewport { rect, .. } => rect,
        }
    }
}

/// Missing source-image edges retained as explicit padding in unscaled pixels.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
pub struct PaddingInsets {
    left: u32,
    top: u32,
    right: u32,
    bottom: u32,
}

impl PaddingInsets {
    pub const fn left(self) -> u32 {
        self.left
    }

    pub const fn top(self) -> u32 {
        self.top
    }

    pub const fn right(self) -> u32 {
        self.right
    }

    pub const fn bottom(self) -> u32 {
        self.bottom
    }

    pub const fn is_empty(self) -> bool {
        self.left == 0 && self.top == 0 && self.right == 0 && self.bottom == 0
    }
}

/// Everything needed to render one chronological fixed-region tile.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct FilmstripTilePlan<FrameId> {
    frame_id: FrameId,
    frame_index: usize,
    timestamp: Timestamp,
    anchor_offset_nanos: i128,
    source_rect: Option<PixelRect>,
    padding: PaddingInsets,
    gap_after: bool,
}

impl<F> FilmstripTilePlan<F> {
    pub fn frame_id(&self) -> &F {
        &self.frame_id
    }

    pub const fn frame_index(&self) -> usize {
        self.frame_index
    }

    pub const fn timestamp(&self) -> Timestamp {
        self.timestamp
    }

    pub const fn anchor_offset_nanos(&self) -> i128 {
        self.anchor_offset_nanos
    }

    pub const fn source_rect(&self) -> Option<PixelRect> {
        self.source_rect
    }

    pub const fn padding(&self) -> PaddingInsets {
        self.padding
    }

    pub const fn gap_after(&self) -> bool {
        self.gap_after
    }
}

/// Reusable, deterministic fixed-region selection and crop plan.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct RegionFilmstripPlan<FrameId> {
    tiles: Box<[FilmstripTilePlan<FrameId>]>,
    locator_frame_index: usize,
    coordinate_space: RegionCoordinateSpace,
    declared_region: SignedPixelRect,
    resolved_source_region: SignedPixelRect,
    tile_source_dimensions: PixelDimensions,
    omitted_frame_count: u64,
}

impl<F> RegionFilmstripPlan<F> {
    pub fn tiles(&self) -> &[FilmstripTilePlan<F>] {
        &self.tiles
    }

    pub const fn locator_frame_index(&self) -> usize {
        self.locator_frame_index
    }

    pub const fn coordinate_space(&self) -> RegionCoordinateSpace {
        self.coordinate_space
    }

    pub const fn declared_region(&self) -> SignedPixelRect {
        self.declared_region
    }

    pub const fn resolved_source_region(&self) -> SignedPixelRect {
        self.resolved_source_region
    }

    pub const fn tile_source_dimensions(&self) -> PixelDimensions {
        self.tile_source_dimensions
    }

    pub const fn omitted_frame_count(&self) -> u64 {
        self.omitted_frame_count
    }
}

/// Maximum number of crops shown in one filmstrip.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(transparent)]
pub struct FilmstripTileLimit(NonZeroU8);

impl FilmstripTileLimit {
    pub const DEFAULT: Self = Self(NonZeroU8::new(12).expect("default is nonzero"));

    pub fn new(value: u8) -> Result<Self> {
        let value = NonZeroU8::new(value).ok_or_else(|| {
            VisionError::new(
                ErrorCode::InvalidParameter,
                "filmstrip tile limit must be between one and twenty-four",
            )
        })?;
        if value.get() > 24 {
            return Err(VisionError::new(
                ErrorCode::InvalidParameter,
                "filmstrip tile limit must be between one and twenty-four",
            ));
        }
        Ok(Self(value))
    }

    pub const fn get(self) -> u8 {
        self.0.get()
    }
}

impl Default for FilmstripTileLimit {
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl<'de> Deserialize<'de> for FilmstripTileLimit {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        Self::new(u8::deserialize(deserializer)?).map_err(serde::de::Error::custom)
    }
}

/// Resolve one fixed region and select a bounded chronological set of source frames.
pub fn plan_region_filmstrip<F: Clone + Eq, M: Eq, G: Eq, P: AsRef<[u8]>>(
    source: &FrameSequence<F, M, G, P>,
    region: RegionDefinition,
    anchor: Timestamp,
    tile_limit: FilmstripTileLimit,
    locator_frame_index: Option<usize>,
) -> Result<RegionFilmstripPlan<F>> {
    if !source.range().contains(anchor) {
        return Err(VisionError::new(
            ErrorCode::InvalidParameter,
            "filmstrip anchor lies outside the source range",
        ));
    }
    if locator_frame_index.is_some_and(|index| index >= source.frames().len()) {
        return Err(VisionError::new(
            ErrorCode::InvalidParameter,
            "filmstrip locator frame index is outside the source sequence",
        ));
    }

    let resolved = resolve_region(region, source.dimensions())?;
    let tile_source_dimensions = PixelDimensions::new(resolved.width(), resolved.height())
        .map_err(|_| {
            VisionError::new(
                ErrorCode::ResourceLimitExceeded,
                "resolved filmstrip region exceeds supported dimensions",
            )
        })?;
    let selected_indices = select_indices(source.frames().len(), usize::from(tile_limit.get()));
    let locator_frame_index = locator_frame_index.unwrap_or_else(|| {
        selected_indices
            .iter()
            .copied()
            .find(|index| source.frames()[*index].timestamp() >= anchor)
            .unwrap_or(selected_indices[0])
    });

    let mut tiles = selected_indices
        .iter()
        .map(|index| {
            let frame = &source.frames()[*index];
            let (source_rect, padding) = intersect_region(resolved, source.dimensions())?;
            Ok(FilmstripTilePlan {
                frame_id: frame.id().clone(),
                frame_index: *index,
                timestamp: frame.timestamp(),
                anchor_offset_nanos: i128::from(frame.timestamp().as_nanos())
                    - i128::from(anchor.as_nanos()),
                source_rect,
                padding,
                gap_after: false,
            })
        })
        .collect::<Result<Vec<_>>>()?;
    for tile in 0..tiles.len().saturating_sub(1) {
        tiles[tile].gap_after = source.gaps().iter().any(|gap| {
            gap.range().start() <= tiles[tile + 1].timestamp
                && gap.range().end() >= tiles[tile].timestamp
        });
    }

    let omitted_frame_count =
        u64::try_from(source.source_frame_count() - tiles.len()).map_err(|_| {
            VisionError::new(
                ErrorCode::ResourceLimitExceeded,
                "filmstrip frame count exceeds the manifest representation",
            )
        })?;
    Ok(RegionFilmstripPlan {
        tiles: tiles.into_boxed_slice(),
        locator_frame_index,
        coordinate_space: region.coordinate_space(),
        declared_region: region.rect(),
        resolved_source_region: resolved,
        tile_source_dimensions,
        omitted_frame_count,
    })
}

fn resolve_region(region: RegionDefinition, source: PixelDimensions) -> Result<SignedPixelRect> {
    match region {
        RegionDefinition::FixedSourceImage { rect } => Ok(rect),
        RegionDefinition::FixedViewport { rect, mapping } => {
            validate_viewport_mapping(mapping, source)?;
            let left = scale_floor(rect.x(), mapping.scale_x())?;
            let top = scale_floor(rect.y(), mapping.scale_y())?;
            let right = scale_ceil(rect.right_exclusive()?, mapping.scale_x())?;
            let bottom = scale_ceil(rect.bottom_exclusive()?, mapping.scale_y())?;
            let width = u32::try_from(right.checked_sub(left).ok_or_else(invalid_scale_error)?)
                .ok()
                .and_then(NonZeroU32::new)
                .ok_or_else(invalid_scale_error)?;
            let height = u32::try_from(bottom.checked_sub(top).ok_or_else(invalid_scale_error)?)
                .ok()
                .and_then(NonZeroU32::new)
                .ok_or_else(invalid_scale_error)?;
            SignedPixelRect::new(left, top, width, height)
        }
    }
}

fn validate_viewport_mapping(mapping: ViewportMapping, source: PixelDimensions) -> Result<()> {
    let mapped_width = scale_ceil(
        i64::from(mapping.viewport_dimensions().width()),
        mapping.scale_x(),
    )?;
    let mapped_height = scale_ceil(
        i64::from(mapping.viewport_dimensions().height()),
        mapping.scale_y(),
    )?;
    if mapped_width != i64::from(source.width()) || mapped_height != i64::from(source.height()) {
        return Err(VisionError::new(
            ErrorCode::InvalidScale,
            "viewport mapping dimensions contradict the source-frame dimensions",
        ));
    }
    Ok(())
}

fn scale_floor(value: i64, scale: RationalScale) -> Result<i64> {
    let numerator = i128::from(value)
        .checked_mul(i128::from(scale.numerator()))
        .ok_or_else(invalid_scale_error)?;
    let result = numerator.div_euclid(i128::from(scale.denominator()));
    i64::try_from(result).map_err(|_| invalid_scale_error())
}

fn scale_ceil(value: i64, scale: RationalScale) -> Result<i64> {
    let numerator = i128::from(value)
        .checked_mul(i128::from(scale.numerator()))
        .ok_or_else(invalid_scale_error)?;
    let denominator = i128::from(scale.denominator());
    let result = -(-numerator).div_euclid(denominator);
    i64::try_from(result).map_err(|_| invalid_scale_error())
}

fn invalid_scale_error() -> VisionError {
    VisionError::new(
        ErrorCode::InvalidScale,
        "viewport scale conversion exceeds the supported coordinate space",
    )
}

/// Select a deterministic, evenly spaced chronological subset including both endpoints.
pub fn select_indices(frame_count: usize, limit: usize) -> Vec<usize> {
    if frame_count <= limit {
        return (0..frame_count).collect();
    }
    if limit == 1 {
        return vec![0];
    }
    let span = frame_count - 1;
    let denominator = limit - 1;
    (0..limit)
        .map(|slot| {
            let numerator = (slot as u128) * (span as u128);
            round_ratio_ties_down(numerator, denominator as u128) as usize
        })
        .collect()
}

fn round_ratio_ties_down(numerator: u128, denominator: u128) -> u128 {
    let quotient = numerator / denominator;
    let remainder = numerator % denominator;
    quotient + u128::from(remainder > denominator - remainder)
}

fn intersect_region(
    region: SignedPixelRect,
    dimensions: PixelDimensions,
) -> Result<(Option<PixelRect>, PaddingInsets)> {
    let right = region.right_exclusive()?;
    let bottom = region.bottom_exclusive()?;
    let source_right = i64::from(dimensions.width());
    let source_bottom = i64::from(dimensions.height());
    let left = region.x().clamp(0, source_right);
    let top = region.y().clamp(0, source_bottom);
    let clipped_right = right.clamp(0, source_right);
    let clipped_bottom = bottom.clamp(0, source_bottom);
    let visible_width = u32::try_from(clipped_right.saturating_sub(left)).map_err(|_| {
        VisionError::new(
            ErrorCode::InvalidRegion,
            "filmstrip intersection exceeds limits",
        )
    })?;
    let visible_height = u32::try_from(clipped_bottom.saturating_sub(top)).map_err(|_| {
        VisionError::new(
            ErrorCode::InvalidRegion,
            "filmstrip intersection exceeds limits",
        )
    })?;
    let padding = PaddingInsets {
        left: u32::try_from((left - region.x()).clamp(0, i64::from(region.width())))
            .map_err(|_| invalid_region_error())?,
        top: u32::try_from((top - region.y()).clamp(0, i64::from(region.height())))
            .map_err(|_| invalid_region_error())?,
        right: region
            .width()
            .checked_sub(
                u32::try_from((clipped_right - region.x()).clamp(0, i64::from(region.width())))
                    .map_err(|_| invalid_region_error())?,
            )
            .ok_or_else(invalid_region_error)?,
        bottom: region
            .height()
            .checked_sub(
                u32::try_from((clipped_bottom - region.y()).clamp(0, i64::from(region.height())))
                    .map_err(|_| invalid_region_error())?,
            )
            .ok_or_else(invalid_region_error)?,
    };
    let source_rect = if visible_width == 0 || visible_height == 0 {
        None
    } else {
        Some(PixelRect::new(
            u32::try_from(left).map_err(|_| invalid_region_error())?,
            u32::try_from(top).map_err(|_| invalid_region_error())?,
            visible_width,
            visible_height,
        )?)
    };
    Ok((source_rect, padding))
}

fn invalid_region_error() -> VisionError {
    VisionError::new(
        ErrorCode::InvalidRegion,
        "filmstrip region intersection exceeds supported limits",
    )
}

const DEFAULT_MAX_DIMENSION: u32 = 4_096;
const DEFAULT_MAX_BYTES: usize = 64 * 1024 * 1024;
const DEFAULT_MAX_SOURCE_FRAMES: usize = 4_096;
const MARGIN: u32 = 12;
const HEADER_HEIGHT: u32 = 64;
const LOCATOR_WIDTH: u32 = 200;
const LOCATOR_HEIGHT: u32 = 200;
const LOCATOR_ANNOTATION_HEIGHT: u32 = 44;
const TILE_ANNOTATION_HEIGHT: u32 = 56;
const MINIMUM_TILE_SLOT_WIDTH: u32 = 160;
const SECTION_GAP: u32 = 16;
const TILE_GAP: u32 = 12;
const TIMELINE_HEIGHT: u32 = 24;

/// Required title and source context drawn onto a region filmstrip.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RegionFilmstripLabels {
    title: String,
    source: String,
}

impl RegionFilmstripLabels {
    pub fn new(title: impl Into<String>, source: impl Into<String>) -> Result<Self> {
        let title = title.into();
        let source = source.into();
        if title.trim().is_empty() || source.trim().is_empty() {
            return Err(VisionError::new(
                ErrorCode::InvalidParameter,
                "filmstrip title and source context must not be empty",
            ));
        }
        Ok(Self { title, source })
    }

    pub fn title(&self) -> &str {
        &self.title
    }

    pub fn source(&self) -> &str {
        &self.source
    }
}

/// Processing and output ceilings for one region-filmstrip generation request.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RegionFilmstripRenderLimits {
    max_width: NonZeroU32,
    max_height: NonZeroU32,
    max_canvas_bytes: NonZeroUsize,
    max_encoded_bytes: NonZeroUsize,
    max_source_frames: NonZeroUsize,
}

impl RegionFilmstripRenderLimits {
    pub const fn new(
        max_width: NonZeroU32,
        max_height: NonZeroU32,
        max_canvas_bytes: NonZeroUsize,
        max_encoded_bytes: NonZeroUsize,
    ) -> Self {
        Self {
            max_width,
            max_height,
            max_canvas_bytes,
            max_encoded_bytes,
            max_source_frames: NonZeroUsize::new(DEFAULT_MAX_SOURCE_FRAMES)
                .expect("default is nonzero"),
        }
    }

    pub const fn with_max_source_frames(mut self, max_source_frames: NonZeroUsize) -> Self {
        self.max_source_frames = max_source_frames;
        self
    }

    pub const fn max_width(self) -> u32 {
        self.max_width.get()
    }

    pub const fn max_height(self) -> u32 {
        self.max_height.get()
    }

    pub const fn max_canvas_bytes(self) -> usize {
        self.max_canvas_bytes.get()
    }

    pub const fn max_encoded_bytes(self) -> usize {
        self.max_encoded_bytes.get()
    }

    pub const fn max_source_frames(self) -> usize {
        self.max_source_frames.get()
    }

    fn processing_limits(self) -> ProcessingLimits {
        let max_pixels = (self.max_canvas_bytes() / 6).max(1);
        ProcessingLimits::new(
            self.max_source_frames,
            NonZeroUsize::new(max_pixels).expect("maximum pixels is nonzero"),
            self.max_canvas_bytes,
        )
    }
}

impl Default for RegionFilmstripRenderLimits {
    fn default() -> Self {
        Self::new(
            NonZeroU32::new(DEFAULT_MAX_DIMENSION).expect("default is nonzero"),
            NonZeroU32::new(DEFAULT_MAX_DIMENSION).expect("default is nonzero"),
            NonZeroUsize::new(DEFAULT_MAX_BYTES).expect("default is nonzero"),
            NonZeroUsize::new(DEFAULT_MAX_BYTES).expect("default is nonzero"),
        )
    }
}

/// Complete deterministic request for one fixed-region filmstrip.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RegionFilmstripParameters {
    region: RegionDefinition,
    tracking_label: Option<String>,
    mask: Option<BinaryMask>,
    anchor: Timestamp,
    tile_limit: FilmstripTileLimit,
    locator_frame_index: Option<usize>,
    background: Rgb8,
    padding_color: Rgb8,
    display_scale: IntegerScale,
    labels: RegionFilmstripLabels,
    limits: RegionFilmstripRenderLimits,
}

impl RegionFilmstripParameters {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        region: RegionDefinition,
        anchor: Timestamp,
        tile_limit: FilmstripTileLimit,
        background: Rgb8,
        padding_color: Rgb8,
        display_scale: IntegerScale,
        labels: RegionFilmstripLabels,
        limits: RegionFilmstripRenderLimits,
    ) -> Self {
        Self {
            region,
            tracking_label: None,
            mask: None,
            anchor,
            tile_limit,
            locator_frame_index: None,
            background,
            padding_color,
            display_scale,
            labels,
            limits,
        }
    }

    pub const fn with_locator_frame_index(mut self, index: usize) -> Self {
        self.locator_frame_index = Some(index);
        self
    }

    /// Applies one immutable full-frame mask at identical source coordinates.
    pub fn with_mask(mut self, mask: BinaryMask) -> Result<Self> {
        if mask.bounds()?.is_none() {
            return Err(VisionError::new(
                ErrorCode::InvalidMask,
                "filmstrip mask must select at least one source pixel",
            ));
        }
        self.mask = Some(mask);
        Ok(self)
    }

    pub fn with_tracking_label(mut self, label: impl Into<String>) -> Self {
        self.tracking_label = Some(label.into());
        self
    }

    pub const fn region(&self) -> RegionDefinition {
        self.region
    }

    pub const fn mask(&self) -> Option<&BinaryMask> {
        self.mask.as_ref()
    }

    pub const fn anchor(&self) -> Timestamp {
        self.anchor
    }

    pub const fn tile_limit(&self) -> FilmstripTileLimit {
        self.tile_limit
    }

    pub const fn locator_frame_index(&self) -> Option<usize> {
        self.locator_frame_index
    }

    pub const fn background(&self) -> Rgb8 {
        self.background
    }

    pub const fn padding_color(&self) -> Rgb8 {
        self.padding_color
    }

    pub const fn display_scale(&self) -> IntegerScale {
        self.display_scale
    }

    pub const fn labels(&self) -> &RegionFilmstripLabels {
        &self.labels
    }

    pub const fn limits(&self) -> RegionFilmstripRenderLimits {
        self.limits
    }
}

/// Encoded filmstrip evidence, provenance, and its reusable fixed-region plan.
#[derive(Clone, Debug, PartialEq)]
pub struct RegionFilmstripArtifact<A, F, M, G> {
    artifact: GeneratedArtifact<A, F, M, G>,
    plan: RegionFilmstripPlan<F>,
}

impl<A, F, M, G> RegionFilmstripArtifact<A, F, M, G> {
    pub const fn image(&self) -> &EncodedImage {
        self.artifact.image()
    }

    pub const fn manifest(&self) -> &ArtifactManifest<A, F, M, G> {
        self.artifact.manifest()
    }

    pub const fn plan(&self) -> &RegionFilmstripPlan<F> {
        &self.plan
    }
}

/// Generate one deterministic source-derived fixed-region filmstrip.
pub fn generate_region_filmstrip<A, F, M, G, P>(
    artifact_id: A,
    source: &FrameSequence<F, M, G, P>,
    parameters: RegionFilmstripParameters,
) -> Result<RegionFilmstripArtifact<A, F, M, G>>
where
    F: Clone + Eq + Display,
    M: Clone + Eq,
    G: Clone + Eq,
    P: AsRef<[u8]>,
{
    if source.frames().len() > parameters.limits.max_source_frames() {
        return Err(render_limit_error());
    }
    let effective_region = if let Some(mask) = parameters.mask.as_ref() {
        if mask.dimensions() != source.dimensions() {
            return Err(VisionError::new(
                ErrorCode::InvalidMask,
                "filmstrip mask dimensions must match every source frame",
            ));
        }
        let bounds = mask.bounds()?.ok_or_else(|| {
            VisionError::new(
                ErrorCode::InvalidMask,
                "filmstrip mask must select at least one source pixel",
            )
        })?;
        RegionDefinition::FixedSourceImage {
            rect: SignedPixelRect::new(
                i64::from(bounds.x()),
                i64::from(bounds.y()),
                NonZeroU32::new(bounds.width()).expect("mask bounds are non-empty"),
                NonZeroU32::new(bounds.height()).expect("mask bounds are non-empty"),
            )?,
        }
    } else {
        parameters.region
    };
    let plan = plan_region_filmstrip(
        source,
        effective_region,
        parameters.anchor,
        parameters.tile_limit,
        parameters.locator_frame_index,
    )?;
    let tile_dimensions =
        scaled_tile_dimensions(plan.tile_source_dimensions(), parameters.display_scale)?;
    let (crop, _) = intersect_region(effective_region.rect(), source.dimensions())?;
    // A fully outside region is rendered entirely as padding. Keep normalization bounded to a
    // single source pixel because draw_tile never reads normalized data for that tile.
    let crop = crop.unwrap_or(PixelRect::new(0, 0, 1, 1)?);
    let tile_source = FrameSequence::new(
        plan.tiles()
            .iter()
            .map(|tile| source.frames()[tile.frame_index()].to_owned())
            .collect(),
        Vec::<Marker<M>>::new(),
        Vec::<DeclaredGap<G>>::new(),
        source.region(),
        source.mask().cloned(),
    )?;
    let normalized = normalize_sequence(
        &tile_source,
        NormalizationParameters::new(
            parameters.background,
            Some(crop),
            IntegerScale::IDENTITY,
            parameters.limits.processing_limits(),
        ),
    )?;
    let locator_source = FrameSequence::new(
        vec![source.frames()[plan.locator_frame_index()].to_owned()],
        Vec::<Marker<M>>::new(),
        Vec::<DeclaredGap<G>>::new(),
        None,
        None,
    )?;
    let locator_normalized = normalize_sequence(
        &locator_source,
        NormalizationParameters::new(
            parameters.background,
            None,
            IntegerScale::IDENTITY,
            parameters.limits.processing_limits(),
        ),
    )?;
    let layout = FilmstripLayout::new(tile_dimensions, plan.tiles().len(), parameters.limits)?;
    let mut canvas = Canvas::new(
        layout.dimensions,
        BLACK,
        parameters.limits.max_canvas_bytes(),
    )?;
    render_filmstrip(
        &mut canvas,
        layout,
        source,
        &normalized,
        &locator_normalized,
        &plan,
        &parameters,
    )?;
    let (bytes, hash) = crate::encode::encode_png(
        layout.dimensions,
        canvas.pixels(),
        parameters.limits.max_encoded_bytes(),
    )?;

    let mut normalization = normalized.normalization_steps().to_vec();
    normalization.push(display_conversion_step()?);
    normalization.push(region_padding_step(&plan, parameters.padding_color)?);
    if let Some(mask) = parameters.mask.as_ref() {
        normalization.push(mask_application_step(mask, &plan)?);
    }
    if !parameters.display_scale.is_identity() {
        normalization.push(display_scale_step(
            parameters.display_scale,
            tile_dimensions,
        )?);
    }
    let mut artifact_source_indices = plan
        .tiles()
        .iter()
        .map(FilmstripTilePlan::frame_index)
        .collect::<Vec<_>>();
    if !artifact_source_indices.contains(&plan.locator_frame_index()) {
        artifact_source_indices.push(plan.locator_frame_index());
        artifact_source_indices.sort_unstable();
    }
    let selected_ids = artifact_source_indices
        .iter()
        .map(|index| source.frames()[*index].id().clone())
        .collect();
    let manifest_region = manifest_region(effective_region, source.dimensions())?;
    let manifest = ArtifactManifest::from_sequence_with_domain(
        artifact_id,
        ArtifactKind::RegionFilmstrip,
        EvidenceClass::SourceDerived,
        {
            let descriptor = generator_descriptor(ArtifactKind::RegionFilmstrip);
            AlgorithmDescriptor::new(descriptor.name, descriptor.version)?
        },
        source,
        manifest_region,
        parameters.mask.clone(),
        selected_ids,
        // A filmstrip looks at the frames it renders. Frames between tiles are
        // decoded by the plan and then never examined, so counting them as
        // analyzed would overstate what this artifact is evidence of.
        crate::provenance::SequenceConsumption::SelectedFramesOnly,
        normalization,
        filmstrip_parameters(
            &plan,
            &artifact_source_indices,
            source.source_frame_count(),
            &parameters,
            layout,
            tile_dimensions,
        )?,
        layout.dimensions,
        hash,
    )?;
    Ok(RegionFilmstripArtifact {
        artifact: GeneratedArtifact::new(EncodedImage::new(layout.dimensions, bytes), manifest),
        plan,
    })
}

/// A fixed-size crop location for one source frame.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, schemars::JsonSchema)]
pub struct TrackedRegion {
    pub frame_index: usize,
    pub rect: SignedPixelRect,
}

/// Parameters for a filmstrip whose crop follows a recorded region per frame.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TrackedFilmstripParameters {
    pub regions: Vec<TrackedRegion>,
    pub anchor: Timestamp,
    pub tile_limit: FilmstripTileLimit,
    pub background: Rgb8,
    pub padding_color: Rgb8,
    pub display_scale: IntegerScale,
    pub labels: RegionFilmstripLabels,
    pub limits: RegionFilmstripRenderLimits,
}

impl TrackedFilmstripParameters {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        regions: Vec<TrackedRegion>,
        anchor: Timestamp,
        tile_limit: FilmstripTileLimit,
        background: Rgb8,
        padding_color: Rgb8,
        display_scale: IntegerScale,
        labels: RegionFilmstripLabels,
        limits: RegionFilmstripRenderLimits,
    ) -> Self {
        Self {
            regions,
            anchor,
            tile_limit,
            background,
            padding_color,
            display_scale,
            labels,
            limits,
        }
    }
}

/// Generate a per-frame moving crop. All tiles share the first union crop for
/// normalization, while their plans retain their own source rectangle/padding.
pub fn generate_tracked_region_filmstrip<A, F, M, G, P>(
    artifact_id: A,
    source: &FrameSequence<F, M, G, P>,
    parameters: TrackedFilmstripParameters,
) -> Result<RegionFilmstripArtifact<A, F, M, G>>
where
    F: Clone + Eq + Display,
    M: Clone + Eq,
    G: Clone + Eq,
    P: AsRef<[u8]>,
{
    if parameters.regions.is_empty() {
        return Err(VisionError::new(
            ErrorCode::InvalidParameter,
            "tracked filmstrip requires regions",
        ));
    }
    let selected = select_indices(
        source.frames().len(),
        usize::from(parameters.tile_limit.get()),
    );
    let mut by_frame = std::collections::BTreeMap::new();
    for region in parameters.regions {
        by_frame.insert(region.frame_index, region.rect);
    }
    let first = by_frame.values().next().copied().ok_or_else(|| {
        VisionError::new(
            ErrorCode::InvalidParameter,
            "tracked filmstrip requires regions",
        )
    })?;
    let mut union = first;
    for rect in by_frame.values().copied() {
        if rect.width() != first.width() || rect.height() != first.height() {
            return Err(VisionError::new(
                ErrorCode::InvalidRegion,
                "tracked crop dimensions must remain fixed",
            ));
        }
        let left = union.x().min(rect.x());
        let top = union.y().min(rect.y());
        let right = union.right_exclusive()?.max(rect.right_exclusive()?);
        let bottom = union.bottom_exclusive()?.max(rect.bottom_exclusive()?);
        union = SignedPixelRect::from_outward_f64_bounds(
            left as f64,
            top as f64,
            right as f64,
            bottom as f64,
        )?;
    }
    let tile_dims = PixelDimensions::new(first.width(), first.height())?;
    let mut tiles = Vec::with_capacity(selected.len());
    for index in &selected {
        let frame = &source.frames()[*index];
        let Some(rect) = by_frame.get(index).copied() else {
            tiles.push(FilmstripTilePlan {
                frame_id: frame.id().clone(),
                frame_index: *index,
                timestamp: frame.timestamp(),
                anchor_offset_nanos: i128::from(frame.timestamp().as_nanos())
                    - i128::from(parameters.anchor.as_nanos()),
                source_rect: None,
                padding: PaddingInsets {
                    left: first.width(),
                    top: first.height(),
                    right: first.width(),
                    bottom: first.height(),
                },
                gap_after: false,
            });
            continue;
        };
        let (source_rect, padding) = intersect_region(rect, source.dimensions())?;
        tiles.push(FilmstripTilePlan {
            frame_id: frame.id().clone(),
            frame_index: *index,
            timestamp: frame.timestamp(),
            anchor_offset_nanos: i128::from(frame.timestamp().as_nanos())
                - i128::from(parameters.anchor.as_nanos()),
            source_rect,
            padding,
            gap_after: false,
        });
    }
    let plan = RegionFilmstripPlan {
        tiles: tiles.into_boxed_slice(),
        locator_frame_index: selected[0],
        coordinate_space: RegionCoordinateSpace::SourceImage,
        declared_region: union,
        resolved_source_region: union,
        tile_source_dimensions: tile_dims,
        omitted_frame_count: (source.frames().len() - selected.len()) as u64,
    };
    let tile_source = FrameSequence::new(
        selected
            .iter()
            .map(|i| source.frames()[*i].to_owned())
            .collect(),
        Vec::<Marker<M>>::new(),
        Vec::<DeclaredGap<G>>::new(),
        source.region(),
        source.mask().cloned(),
    )?;
    let normalized = normalize_sequence(
        &tile_source,
        NormalizationParameters::new(
            parameters.background,
            Some(
                intersect_region(union, source.dimensions())?
                    .0
                    .unwrap_or(PixelRect::new(0, 0, 1, 1)?),
            ),
            IntegerScale::IDENTITY,
            parameters.limits.processing_limits(),
        ),
    )?;
    let locator_source = FrameSequence::new(
        vec![source.frames()[selected[0]].to_owned()],
        Vec::<Marker<M>>::new(),
        Vec::<DeclaredGap<G>>::new(),
        None,
        None,
    )?;
    let locator = normalize_sequence(
        &locator_source,
        NormalizationParameters::new(
            parameters.background,
            None,
            IntegerScale::IDENTITY,
            parameters.limits.processing_limits(),
        ),
    )?;
    let display_dims = scaled_tile_dimensions(tile_dims, parameters.display_scale)?;
    let layout = FilmstripLayout::new(display_dims, selected.len(), parameters.limits)?;
    let mut canvas = Canvas::new(
        layout.dimensions,
        BLACK,
        parameters.limits.max_canvas_bytes(),
    )?;
    let fixed = RegionFilmstripParameters::new(
        RegionDefinition::FixedSourceImage { rect: union },
        parameters.anchor,
        parameters.tile_limit,
        parameters.background,
        parameters.padding_color,
        parameters.display_scale,
        parameters.labels,
        parameters.limits,
    )
    .with_tracking_label("TRACKING NODE | PER-FRAME REGION");
    render_filmstrip(
        &mut canvas,
        layout,
        source,
        &normalized,
        &locator,
        &plan,
        &fixed,
    )?;
    let (bytes, hash) = crate::encode::encode_png(
        layout.dimensions,
        canvas.pixels(),
        parameters.limits.max_encoded_bytes(),
    )?;
    let manifest = ArtifactManifest::from_sequence_with_domain(
        artifact_id,
        ArtifactKind::RegionFilmstrip,
        EvidenceClass::SourceDerived,
        {
            let d = generator_descriptor(ArtifactKind::RegionFilmstrip);
            AlgorithmDescriptor::new(d.name, d.version)?
        },
        source,
        manifest_region(
            RegionDefinition::FixedSourceImage { rect: union },
            source.dimensions(),
        )?,
        None,
        selected
            .iter()
            .map(|i| source.frames()[*i].id().clone())
            .collect(),
        crate::provenance::SequenceConsumption::SelectedFramesOnly,
        Vec::new(),
        Parameters::new(BTreeMap::new())?,
        layout.dimensions,
        hash,
    )?;
    Ok(RegionFilmstripArtifact {
        artifact: GeneratedArtifact::new(EncodedImage::new(layout.dimensions, bytes), manifest),
        plan,
    })
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct FilmstripLayout {
    dimensions: PixelDimensions,
    locator_panel: PixelRect,
    locator_annotation: PixelRect,
    strip_x: u32,
    strip_y: u32,
    tile_slot_width: u32,
    tile_width: u32,
    tile_height: u32,
    columns: usize,
    rows: usize,
    timeline_y: u32,
}

impl FilmstripLayout {
    fn new(
        tile: PixelDimensions,
        tile_count: usize,
        limits: RegionFilmstripRenderLimits,
    ) -> Result<Self> {
        let tile_slot_width = tile.width().max(MINIMUM_TILE_SLOT_WIDTH);
        let fixed_width = MARGIN
            .checked_mul(2)
            .and_then(|value| value.checked_add(LOCATOR_WIDTH))
            .and_then(|value| value.checked_add(SECTION_GAP))
            .ok_or_else(canvas_limit_error)?;
        let available = limits
            .max_width()
            .checked_sub(fixed_width)
            .ok_or_else(canvas_limit_error)?;
        let columns = usize::try_from(
            available
                .checked_add(TILE_GAP)
                .ok_or_else(canvas_limit_error)?
                / tile_slot_width
                    .checked_add(TILE_GAP)
                    .ok_or_else(canvas_limit_error)?,
        )
        .map_err(|_| canvas_limit_error())?
        .min(tile_count);
        if columns == 0 {
            return Err(canvas_limit_error());
        }
        let rows = tile_count.div_ceil(columns);
        let columns_u32 = u32::try_from(columns).map_err(|_| canvas_limit_error())?;
        let rows_u32 = u32::try_from(rows).map_err(|_| canvas_limit_error())?;
        let strip_width = columns_u32
            .checked_mul(tile_slot_width)
            .and_then(|value| {
                value.checked_add(TILE_GAP.checked_mul(columns_u32.saturating_sub(1))?)
            })
            .ok_or_else(canvas_limit_error)?;
        let row_height = tile
            .height()
            .checked_add(TILE_ANNOTATION_HEIGHT)
            .ok_or_else(canvas_limit_error)?;
        let strip_height = rows_u32
            .checked_mul(row_height)
            .and_then(|value| value.checked_add(TILE_GAP.checked_mul(rows_u32 - 1)?))
            .ok_or_else(canvas_limit_error)?;
        let locator_height = LOCATOR_HEIGHT
            .checked_add(LOCATOR_ANNOTATION_HEIGHT)
            .ok_or_else(canvas_limit_error)?;
        let content_height = strip_height.max(locator_height);
        let width = fixed_width
            .checked_add(strip_width)
            .ok_or_else(canvas_limit_error)?;
        let timeline_y = HEADER_HEIGHT
            .checked_add(MARGIN)
            .and_then(|value| value.checked_add(content_height))
            .and_then(|value| value.checked_add(MARGIN))
            .ok_or_else(canvas_limit_error)?;
        let height = timeline_y
            .checked_add(TIMELINE_HEIGHT)
            .ok_or_else(canvas_limit_error)?;
        if width > limits.max_width() || height > limits.max_height() {
            return Err(canvas_output_limit_error(
                width,
                height,
                limits.max_width(),
                limits.max_height(),
            ));
        }
        let dimensions = PixelDimensions::new(width, height).map_err(|_| canvas_limit_error())?;
        let bytes = dimensions
            .pixel_count()?
            .checked_mul(3)
            .ok_or_else(canvas_limit_error)?;
        if bytes > limits.max_canvas_bytes() {
            return Err(canvas_limit_error());
        }
        let locator_y = HEADER_HEIGHT + MARGIN;
        Ok(Self {
            dimensions,
            locator_panel: PixelRect::new(MARGIN, locator_y, LOCATOR_WIDTH, LOCATOR_HEIGHT)?,
            locator_annotation: PixelRect::new(
                MARGIN,
                locator_y + LOCATOR_HEIGHT,
                LOCATOR_WIDTH,
                LOCATOR_ANNOTATION_HEIGHT,
            )?,
            strip_x: MARGIN + LOCATOR_WIDTH + SECTION_GAP,
            strip_y: locator_y,
            tile_slot_width,
            tile_width: tile.width(),
            tile_height: tile.height(),
            columns,
            rows,
            timeline_y,
        })
    }

    fn tile_slot(self, index: usize) -> Result<PixelRect> {
        let column = u32::try_from(index % self.columns).map_err(|_| canvas_limit_error())?;
        let row = u32::try_from(index / self.columns).map_err(|_| canvas_limit_error())?;
        let x = self
            .strip_x
            .checked_add(
                column
                    .checked_mul(self.tile_slot_width + TILE_GAP)
                    .ok_or_else(canvas_limit_error)?,
            )
            .ok_or_else(canvas_limit_error)?;
        let y = self
            .strip_y
            .checked_add(
                row.checked_mul(self.tile_height + TILE_ANNOTATION_HEIGHT + TILE_GAP)
                    .ok_or_else(canvas_limit_error)?,
            )
            .ok_or_else(canvas_limit_error)?;
        PixelRect::new(
            x,
            y,
            self.tile_slot_width,
            self.tile_height + TILE_ANNOTATION_HEIGHT,
        )
    }
}

fn scaled_tile_dimensions(source: PixelDimensions, scale: IntegerScale) -> Result<PixelDimensions> {
    let factor = u32::from(scale.factor());
    let (width, height) = match scale.direction_name() {
        "identity" => (source.width(), source.height()),
        "up" => (
            source
                .width()
                .checked_mul(factor)
                .ok_or_else(invalid_scale_error)?,
            source
                .height()
                .checked_mul(factor)
                .ok_or_else(invalid_scale_error)?,
        ),
        "down" => {
            if !source.width().is_multiple_of(factor) || !source.height().is_multiple_of(factor) {
                return Err(VisionError::new(
                    ErrorCode::InvalidScale,
                    "filmstrip downscale factor must exactly divide declared region dimensions",
                ));
            }
            (source.width() / factor, source.height() / factor)
        }
        _ => unreachable!("integer scale direction is a closed internal registry"),
    };
    PixelDimensions::new(width, height).map_err(|_| invalid_scale_error())
}

fn render_filmstrip<F: Display + Eq, M: Eq, G: Eq, P: AsRef<[u8]>>(
    canvas: &mut Canvas,
    layout: FilmstripLayout,
    source: &FrameSequence<F, M, G, P>,
    normalized: &crate::NormalizedSequence<F>,
    locator_normalized: &crate::NormalizedSequence<F>,
    plan: &RegionFilmstripPlan<F>,
    parameters: &RegionFilmstripParameters,
) -> Result<()> {
    draw_header(canvas, source, plan, parameters)?;
    draw_locator(
        canvas,
        layout,
        &locator_normalized.frames()[0],
        source.dimensions(),
        plan,
    )?;
    for (index, tile) in plan.tiles().iter().enumerate() {
        draw_tile(
            canvas,
            layout,
            index,
            tile,
            &normalized.frames()[index],
            normalized.source_crop(),
            parameters,
        )?;
    }
    draw_timeline(canvas, layout, source)
}

fn draw_header<F: Eq, M: Eq, G: Eq, P: AsRef<[u8]>>(
    canvas: &mut Canvas,
    source: &FrameSequence<F, M, G, P>,
    plan: &RegionFilmstripPlan<F>,
    parameters: &RegionFilmstripParameters,
) -> Result<()> {
    canvas.fill_rect(0, 0, canvas.dimensions().width(), HEADER_HEIGHT, BLACK)?;
    let width = canvas.dimensions().width().saturating_sub(8);
    draw_clipped_text(canvas, 4, 2, width, parameters.labels.title(), WHITE)?;
    draw_clipped_text(canvas, 4, 14, width, parameters.labels.source(), MUTED)?;
    draw_clipped_text(
        canvas,
        4,
        26,
        width,
        &format!(
            "RANGE {} - {} | ANCHOR {}",
            format_time(source.range().start()),
            format_time(source.range().end()),
            format_time(parameters.anchor)
        ),
        MUTED,
    )?;
    let status = if source.gaps().is_empty() {
        if let Some(label) = parameters.tracking_label.as_deref() {
            format!(
                "SOURCE-DERIVED | PER-FRAME REGION | {label} | STRIP OMITTED {}",
                plan.omitted_frame_count()
            )
        } else {
            format!(
                "SOURCE-DERIVED | FIXED {} | TRACKING NONE | STRIP OMITTED {}",
                plan.coordinate_space().as_str(),
                plan.omitted_frame_count()
            )
        }
    } else if let Some(label) = parameters.tracking_label.as_deref() {
        format!("GAP - UNSEEN BEHAVIOR MAY HAVE OCCURRED | PER-FRAME REGION | {label}")
    } else {
        format!(
            "GAP - UNSEEN BEHAVIOR MAY HAVE OCCURRED | FIXED {} | TRACKING NONE",
            plan.coordinate_space().as_str()
        )
    };
    draw_clipped_text(
        canvas,
        4,
        38,
        width,
        &status,
        if source.gaps().is_empty() {
            MUTED
        } else {
            WARNING
        },
    )?;
    draw_clipped_text(
        canvas,
        4,
        50,
        width,
        if parameters.mask.is_some() {
            "MASK APPLIED | SELECTED PIXELS SHOWN | EXCLUDED PIXELS HATCHED"
        } else {
            parameters
                .tracking_label
                .as_deref()
                .unwrap_or("FIXED VISUAL REGION; NO LOGICAL ELEMENT FOLLOWING")
        },
        if parameters.mask.is_some() {
            WARNING
        } else {
            MUTED
        },
    )
}

fn draw_locator<F>(
    canvas: &mut Canvas,
    layout: FilmstripLayout,
    frame: &crate::NormalizedFrame<F>,
    source_dimensions: PixelDimensions,
    plan: &RegionFilmstripPlan<F>,
) -> Result<()> {
    canvas.fill_rect(
        layout.locator_panel.x(),
        layout.locator_panel.y(),
        layout.locator_panel.width(),
        layout.locator_panel.height(),
        PANEL,
    )?;
    let (draw_x, draw_y, draw_width, draw_height) = canvas.draw_linear_frame(
        frame.dimensions(),
        frame.linear_rgb16(),
        layout.locator_panel.x(),
        layout.locator_panel.y(),
        layout.locator_panel.width(),
        layout.locator_panel.height(),
    )?;
    let (intersection, _) = intersect_region(plan.resolved_source_region(), source_dimensions)?;
    if let Some(rect) = intersection {
        let x0 = draw_x + map_floor(rect.x(), draw_width, source_dimensions.width())?;
        let y0 = draw_y + map_floor(rect.y(), draw_height, source_dimensions.height())?;
        let x1 = draw_x
            + map_ceil(
                rect.right_exclusive()?,
                draw_width,
                source_dimensions.width(),
            )?;
        let y1 = draw_y
            + map_ceil(
                rect.bottom_exclusive()?,
                draw_height,
                source_dimensions.height(),
            )?;
        draw_outline(
            canvas,
            x0,
            y0,
            x1.saturating_sub(x0),
            y1.saturating_sub(y0),
            WARNING,
        )?;
    }
    let resolved = plan.resolved_source_region();
    let right = resolved.right_exclusive()?;
    let bottom = resolved.bottom_exclusive()?;
    let mut outside = Vec::new();
    if resolved.x() < 0 {
        draw_edge_chevrons(canvas, draw_x, draw_y, draw_width, draw_height, Edge::Left)?;
        outside.push("LEFT");
    }
    if right > i64::from(source_dimensions.width()) {
        draw_edge_chevrons(canvas, draw_x, draw_y, draw_width, draw_height, Edge::Right)?;
        outside.push("RIGHT");
    }
    if resolved.y() < 0 {
        draw_edge_chevrons(canvas, draw_x, draw_y, draw_width, draw_height, Edge::Top)?;
        outside.push("TOP");
    }
    if bottom > i64::from(source_dimensions.height()) {
        draw_edge_chevrons(
            canvas,
            draw_x,
            draw_y,
            draw_width,
            draw_height,
            Edge::Bottom,
        )?;
        outside.push("BOTTOM");
    }
    canvas.fill_rect(
        layout.locator_annotation.x(),
        layout.locator_annotation.y(),
        layout.locator_annotation.width(),
        layout.locator_annotation.height(),
        PANEL,
    )?;
    draw_clipped_text(
        canvas,
        layout.locator_annotation.x() + 3,
        layout.locator_annotation.y() + 3,
        layout.locator_annotation.width().saturating_sub(6),
        "REGION IN CONTEXT",
        WHITE,
    )?;
    draw_clipped_text(
        canvas,
        layout.locator_annotation.x() + 3,
        layout.locator_annotation.y() + 15,
        layout.locator_annotation.width().saturating_sub(6),
        &format!("LOCATOR FRAME {}", plan.locator_frame_index()),
        MUTED,
    )?;
    let outside_label = if outside.is_empty() {
        "REGION INSIDE SOURCE".to_owned()
    } else {
        format!("OUTSIDE: {}", outside.join(" + "))
    };
    draw_clipped_text(
        canvas,
        layout.locator_annotation.x() + 3,
        layout.locator_annotation.y() + 27,
        layout.locator_annotation.width().saturating_sub(6),
        &outside_label,
        if outside.is_empty() { MUTED } else { WARNING },
    )
}

fn draw_tile<F: Display>(
    canvas: &mut Canvas,
    layout: FilmstripLayout,
    index: usize,
    tile: &FilmstripTilePlan<F>,
    frame: &crate::NormalizedFrame<F>,
    crop: PixelRect,
    parameters: &RegionFilmstripParameters,
) -> Result<()> {
    let slot = layout.tile_slot(index)?;
    let image_x = slot.x() + (slot.width() - layout.tile_width) / 2;
    canvas.fill_rect(slot.x(), slot.y(), slot.width(), layout.tile_height, PANEL)?;
    for y in 0..layout.tile_height {
        for x in 0..layout.tile_width {
            let (color, padding) = scaled_region_pixel(
                frame,
                tile,
                parameters.mask.as_ref(),
                parameters.padding_color.channels(),
                parameters.display_scale,
                crop,
                x,
                y,
            )?;
            let color = if padding && (x + y) % 8 < 2 {
                WARNING
            } else {
                color
            };
            canvas.set_pixel(image_x + x, slot.y() + y, color)?;
        }
    }
    let annotation_y = slot.y() + layout.tile_height;
    canvas.fill_rect(
        slot.x(),
        annotation_y,
        slot.width(),
        TILE_ANNOTATION_HEIGHT,
        PANEL,
    )?;
    let text_width = slot.width().saturating_sub(6);
    draw_clipped_text(
        canvas,
        slot.x() + 3,
        annotation_y + 2,
        text_width,
        &format!(
            "T {} | {}",
            format_time(tile.timestamp()),
            format_offset(tile.anchor_offset_nanos())
        ),
        WHITE,
    )?;
    draw_clipped_text(
        canvas,
        slot.x() + 3,
        annotation_y + 14,
        text_width,
        &format!(
            "FRAME {} | SOURCE INDEX {}",
            tile.frame_id(),
            tile.frame_index()
        ),
        MUTED,
    )?;
    let padding = tile.padding();
    let padding_label = if tile.source_rect().is_none() {
        "OUTSIDE SOURCE | ALL PADDING".to_owned()
    } else if padding.is_empty() {
        "SOURCE CROP | NO PADDING".to_owned()
    } else {
        format!(
            "PADDING L{} T{} R{} B{}",
            padding.left(),
            padding.top(),
            padding.right(),
            padding.bottom()
        )
    };
    draw_clipped_text(
        canvas,
        slot.x() + 3,
        annotation_y + 26,
        text_width,
        &padding_label,
        if padding.is_empty() { MUTED } else { WARNING },
    )?;
    draw_clipped_text(
        canvas,
        slot.x() + 3,
        annotation_y + 38,
        text_width,
        if parameters.mask.is_some() {
            "FIXED MASK | TRACKING NONE"
        } else if parameters.tracking_label.is_some() {
            "TRACKED REGION | PER-FRAME CROP"
        } else {
            "FIXED REGION | TRACKING NONE"
        },
        if parameters.mask.is_some() {
            WARNING
        } else {
            MUTED
        },
    )?;
    if tile.gap_after() {
        let hatch_x = slot.right_exclusive()?.saturating_sub(6);
        canvas.draw_hatch(hatch_x, slot.y(), 6, slot.height(), WARNING)?;
        draw_clipped_text(
            canvas,
            slot.x() + 3,
            annotation_y + 44,
            text_width,
            "GAP ->",
            WARNING,
        )?;
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn scaled_region_pixel<F>(
    frame: &crate::NormalizedFrame<F>,
    tile: &FilmstripTilePlan<F>,
    mask: Option<&BinaryMask>,
    padding_color: [u8; 3],
    scale: IntegerScale,
    crop: PixelRect,
    output_x: u32,
    output_y: u32,
) -> Result<([u8; 3], bool)> {
    let factor = u32::from(scale.factor());
    if scale.direction_name() != "down" {
        let source_x = if scale.direction_name() == "up" {
            output_x / factor
        } else {
            output_x
        };
        let source_y = if scale.direction_name() == "up" {
            output_y / factor
        } else {
            output_y
        };
        return region_pixel(frame, tile, mask, padding_color, crop, source_x, source_y);
    }

    let mut sums = [0_u64; 3];
    let mut padding = false;
    for dy in 0..factor {
        for dx in 0..factor {
            let (pixel, generated) = region_pixel(
                frame,
                tile,
                mask,
                padding_color,
                crop,
                output_x * factor + dx,
                output_y * factor + dy,
            )?;
            padding |= generated;
            for channel in 0..3 {
                sums[channel] += u64::from(pixel[channel]);
            }
        }
    }
    let count = u64::from(factor) * u64::from(factor);
    Ok((
        std::array::from_fn(|channel| ((sums[channel] + count / 2) / count) as u8),
        padding,
    ))
}

fn region_pixel<F>(
    frame: &crate::NormalizedFrame<F>,
    tile: &FilmstripTilePlan<F>,
    mask: Option<&BinaryMask>,
    padding_color: [u8; 3],
    crop: PixelRect,
    x: u32,
    y: u32,
) -> Result<([u8; 3], bool)> {
    let padding = tile.padding();
    let Some(source_rect) = tile.source_rect() else {
        return Ok((padding_color, true));
    };
    let visible_x = x.checked_sub(padding.left());
    let visible_y = y.checked_sub(padding.top());
    if visible_x.is_none_or(|value| value >= source_rect.width())
        || visible_y.is_none_or(|value| value >= source_rect.height())
    {
        return Ok((padding_color, true));
    }
    let source_x = source_rect.x() + visible_x.expect("checked above");
    let source_y = source_rect.y() + visible_y.expect("checked above");
    if mask.is_some_and(|mask| mask.includes(source_x, source_y) != Some(true)) {
        return Ok((padding_color, true));
    }
    let index = usize::try_from(source_y.saturating_sub(crop.y()))
        .ok()
        .and_then(|row| row.checked_mul(usize::try_from(frame.dimensions().width()).ok()?))
        .and_then(|row| row.checked_add(usize::try_from(source_x.saturating_sub(crop.x())).ok()?))
        .and_then(|pixel| pixel.checked_mul(3))
        .ok_or_else(canvas_limit_error)?;
    let linear = &frame.linear_rgb16()[index..index + 3];
    Ok((
        [linear[0], linear[1], linear[2]].map(crate::normalize::linear16_to_srgb8),
        false,
    ))
}

fn draw_timeline<F: Eq, M: Eq, G: Eq, P: AsRef<[u8]>>(
    canvas: &mut Canvas,
    layout: FilmstripLayout,
    source: &FrameSequence<F, M, G, P>,
) -> Result<()> {
    canvas.fill_rect(
        0,
        layout.timeline_y,
        layout.dimensions.width(),
        TIMELINE_HEIGHT,
        BLACK,
    )?;
    draw_clipped_text(
        canvas,
        4,
        layout.timeline_y + 6,
        layout.dimensions.width().saturating_sub(8),
        &format!(
            "TIME -> {} -> {} | CHRONOLOGICAL SOURCE ORDER | ROWS {}",
            format_time(source.range().start()),
            format_time(source.range().end()),
            layout.rows
        ),
        WHITE,
    )
}

#[derive(Clone, Copy)]
enum Edge {
    Left,
    Right,
    Top,
    Bottom,
}

fn draw_edge_chevrons(
    canvas: &mut Canvas,
    x: u32,
    y: u32,
    width: u32,
    height: u32,
    edge: Edge,
) -> Result<()> {
    for offset in (4..match edge {
        Edge::Left | Edge::Right => height,
        _ => width,
    })
        .step_by(12)
    {
        for arm in 0..4 {
            let (px, py) = match edge {
                Edge::Left => (x + arm, y + offset.saturating_sub(2) + arm),
                Edge::Right => (x + width - 1 - arm, y + offset.saturating_sub(2) + arm),
                Edge::Top => (x + offset.saturating_sub(2) + arm, y + arm),
                Edge::Bottom => (x + offset.saturating_sub(2) + arm, y + height - 1 - arm),
            };
            if px < x + width && py < y + height {
                canvas.set_pixel(px, py, WARNING)?;
            }
        }
    }
    Ok(())
}

fn draw_outline(
    canvas: &mut Canvas,
    x: u32,
    y: u32,
    width: u32,
    height: u32,
    color: [u8; 3],
) -> Result<()> {
    if width == 0 || height == 0 {
        return Ok(());
    }
    canvas.fill_rect(x, y, width, height.min(2), color)?;
    canvas.fill_rect(x, y + height.saturating_sub(2), width, height.min(2), color)?;
    canvas.fill_rect(x, y, width.min(2), height, color)?;
    canvas.fill_rect(x + width.saturating_sub(2), y, width.min(2), height, color)
}

fn map_floor(value: u32, target: u32, source: u32) -> Result<u32> {
    u32::try_from(u64::from(value) * u64::from(target) / u64::from(source))
        .map_err(|_| canvas_limit_error())
}

fn map_ceil(value: u32, target: u32, source: u32) -> Result<u32> {
    let numerator = u64::from(value)
        .checked_mul(u64::from(target))
        .and_then(|value| value.checked_add(u64::from(source) - 1))
        .ok_or_else(canvas_limit_error)?;
    u32::try_from(numerator / u64::from(source)).map_err(|_| canvas_limit_error())
}

fn draw_clipped_text(
    canvas: &mut Canvas,
    x: u32,
    y: u32,
    width: u32,
    text: &str,
    color: [u8; 3],
) -> Result<()> {
    let cells = usize::try_from(width / CELL_WIDTH).unwrap_or(0);
    if cells == 0 {
        return Ok(());
    }
    draw_text(canvas, x, y, &ellipsize(text, cells), color)
}

fn format_time(timestamp: Timestamp) -> String {
    let milliseconds = timestamp.as_nanos() / 1_000_000;
    let micros = timestamp.as_nanos() % 1_000_000 / 1_000;
    format!("{milliseconds}.{micros:03} MS")
}

fn format_offset(offset: i128) -> String {
    let sign = if offset < 0 { '-' } else { '+' };
    let magnitude = offset.unsigned_abs();
    let milliseconds = magnitude / 1_000_000;
    let micros = magnitude % 1_000_000 / 1_000;
    format!("{sign}{milliseconds}.{micros:03} MS")
}

fn display_conversion_step() -> Result<NormalizationStep> {
    NormalizationStep::new(
        NormalizationKind::ColorSpaceConversion,
        "linear16-to-srgb8-v1",
        make_parameters([
            ("input", ParameterValue::Text("rgb16_linear_opaque".into())),
            ("output", ParameterValue::Text("rgb8_srgb_opaque".into())),
            (
                "mapping",
                ParameterValue::Text("nearest_checked_lut_entry".into()),
            ),
        ])?,
    )
}

fn region_padding_step<F>(
    plan: &RegionFilmstripPlan<F>,
    padding_color: Rgb8,
) -> Result<NormalizationStep> {
    NormalizationStep::new(
        NormalizationKind::FixedCrop,
        "fixed-region-padding-v1",
        make_parameters([
            (
                "resolved_source_region",
                signed_rect_value(plan.resolved_source_region())?,
            ),
            ("padding_rgb8", rgb_value(padding_color)),
            (
                "missing_pixels",
                ParameterValue::Text("explicit_padding_with_warning_hatch".into()),
            ),
        ])?,
    )
}

fn mask_application_step<F>(
    mask: &BinaryMask,
    plan: &RegionFilmstripPlan<F>,
) -> Result<NormalizationStep> {
    NormalizationStep::new(
        NormalizationKind::FixedCrop,
        "fixed-binary-mask-v1",
        make_parameters([
            ("mask_dimensions", dimensions_value(mask.dimensions())?),
            (
                "mask_bounds",
                signed_rect_value(plan.resolved_source_region())?,
            ),
            (
                "excluded_pixels",
                ParameterValue::Text("padding_color_with_warning_hatch".into()),
            ),
            (
                "mask_sha256",
                ParameterValue::Text(mask_sha256(mask).into()),
            ),
        ])?,
    )
}

fn display_scale_step(
    scale: IntegerScale,
    dimensions: PixelDimensions,
) -> Result<NormalizationStep> {
    NormalizationStep::new(
        NormalizationKind::IntegerScaling,
        "filmstrip-display-scale-v1",
        make_parameters([
            (
                "direction",
                ParameterValue::Text(scale.direction_name().into()),
            ),
            (
                "factor",
                ParameterValue::Unsigned(u64::from(scale.factor())),
            ),
            (
                "kernel",
                ParameterValue::Text(
                    if scale.direction_name() == "down" {
                        "non_overlapping_srgb8_box_average"
                    } else {
                        "nearest_neighbor"
                    }
                    .into(),
                ),
            ),
            ("output_dimensions", dimensions_value(dimensions)?),
        ])?,
    )
}

fn manifest_region(
    region: RegionDefinition,
    dimensions: PixelDimensions,
) -> Result<Option<FrameRegion>> {
    let RegionDefinition::FixedSourceImage { rect } = region else {
        return Ok(None);
    };
    if rect.x() < 0 || rect.y() < 0 {
        return Ok(None);
    }
    let pixel_rect = PixelRect::new(
        u32::try_from(rect.x()).map_err(|_| invalid_region_error())?,
        u32::try_from(rect.y()).map_err(|_| invalid_region_error())?,
        rect.width(),
        rect.height(),
    )?;
    if !pixel_rect.fits_within(dimensions) {
        return Ok(None);
    }
    Ok(Some(FrameRegion::new(pixel_rect, dimensions)?))
}

fn filmstrip_parameters<F: Display>(
    plan: &RegionFilmstripPlan<F>,
    artifact_source_indices: &[usize],
    source_frame_count: usize,
    request: &RegionFilmstripParameters,
    layout: FilmstripLayout,
    tile_dimensions: PixelDimensions,
) -> Result<Parameters> {
    let selected = plan
        .tiles()
        .iter()
        .map(|tile| {
            object([
                ("frame_index", unsigned_usize(tile.frame_index())?),
                (
                    "frame_label",
                    ParameterValue::Text(tile.frame_id().to_string().into()),
                ),
                (
                    "timestamp_nanos",
                    ParameterValue::Unsigned(tile.timestamp().as_nanos()),
                ),
                (
                    "anchor_offset",
                    signed_offset_value(tile.anchor_offset_nanos())?,
                ),
                (
                    "selection_reason",
                    ParameterValue::Text(
                        if plan.omitted_frame_count() == 0 {
                            "all_source_frames"
                        } else {
                            "uniform_source_order_coverage"
                        }
                        .into(),
                    ),
                ),
                ("source_rect", optional_rect_value(tile.source_rect())?),
                ("padding", padding_value(tile.padding())?),
                ("gap_after", ParameterValue::Bool(tile.gap_after())),
            ])
        })
        .collect::<Result<Vec<_>>>()?;
    let gap_warning_count = plan.tiles().iter().filter(|tile| tile.gap_after()).count();
    let mapping = match (request.mask.as_ref(), request.region) {
        (None, RegionDefinition::FixedViewport { mapping, .. }) => object([
            (
                "viewport_dimensions",
                dimensions_value(mapping.viewport_dimensions())?,
            ),
            ("scale_x", rational_value(mapping.scale_x())?),
            ("scale_y", rational_value(mapping.scale_y())?),
        ])?,
        _ => ParameterValue::Text("not_applicable".into()),
    };
    let effective_region = if request.mask.is_some() {
        RegionDefinition::FixedSourceImage {
            rect: plan.resolved_source_region(),
        }
    } else {
        request.region
    };
    let mask = request.mask.as_ref().map_or_else(
        || Ok(ParameterValue::Text("none".into())),
        |mask| {
            object([
                ("dimensions", dimensions_value(mask.dimensions())?),
                ("bounds", signed_rect_value(plan.resolved_source_region())?),
                ("sha256", ParameterValue::Text(mask_sha256(mask).into())),
                (
                    "encoding",
                    ParameterValue::Text("row_major_msb_first_one_bit".into()),
                ),
            ])
        },
    )?;
    Parameters::new(
        [
            (
                "algorithm_version".into(),
                ParameterValue::Text(
                    generator_descriptor(ArtifactKind::RegionFilmstrip)
                        .version
                        .into(),
                ),
            ),
            ("region_definition".into(), region_value(effective_region)?),
            ("mask".into(), mask),
            (
                "coordinate_space".into(),
                ParameterValue::Text(plan.coordinate_space().as_str().into()),
            ),
            ("viewport_mapping".into(), mapping),
            (
                "resolved_source_region".into(),
                signed_rect_value(plan.resolved_source_region())?,
            ),
            ("selected".into(), ParameterValue::List(selected)),
            (
                "artifact_source_indices".into(),
                ParameterValue::List(
                    artifact_source_indices
                        .iter()
                        .map(|index| unsigned_usize(*index))
                        .collect::<Result<Vec<_>>>()?,
                ),
            ),
            (
                "omitted_frame_count".into(),
                unsigned_usize(
                    source_frame_count
                        .checked_sub(artifact_source_indices.len())
                        .ok_or_else(render_limit_error)?,
                )?,
            ),
            (
                "strip_omitted_frame_count".into(),
                ParameterValue::Unsigned(plan.omitted_frame_count()),
            ),
            (
                "locator_frame_index".into(),
                unsigned_usize(plan.locator_frame_index())?,
            ),
            ("background_rgb8".into(), rgb_value(request.background)),
            ("padding_rgb8".into(), rgb_value(request.padding_color)),
            (
                "display_scale".into(),
                object([
                    (
                        "direction",
                        ParameterValue::Text(request.display_scale.direction_name().into()),
                    ),
                    (
                        "factor",
                        ParameterValue::Unsigned(u64::from(request.display_scale.factor())),
                    ),
                ])?,
            ),
            (
                "tile_source_dimensions".into(),
                dimensions_value(plan.tile_source_dimensions())?,
            ),
            (
                "tile_output_dimensions".into(),
                dimensions_value(tile_dimensions)?,
            ),
            (
                "gap_warning_count".into(),
                unsigned_usize(gap_warning_count)?,
            ),
            (
                "title".into(),
                ParameterValue::Text(request.labels.title().into()),
            ),
            (
                "source_context".into(),
                ParameterValue::Text(request.labels.source().into()),
            ),
            (
                "tracking_method".into(),
                ParameterValue::Text("none".into()),
            ),
            (
                "fixed_region_semantics".into(),
                ParameterValue::Text("no_logical_element_following".into()),
            ),
            (
                "label_truncation".into(),
                ParameterValue::Text("embedded_ascii_ellipsis_exact_text_in_manifest".into()),
            ),
            (
                "output_layout".into(),
                object([
                    (
                        "name",
                        ParameterValue::Text("locator_and_wrapped_strip_v1".into()),
                    ),
                    ("columns", unsigned_usize(layout.columns)?),
                    ("rows", unsigned_usize(layout.rows)?),
                    (
                        "tile_slot_width",
                        ParameterValue::Unsigned(u64::from(layout.tile_slot_width)),
                    ),
                    (
                        "time_direction",
                        ParameterValue::Text("left_to_right_then_next_row".into()),
                    ),
                ])?,
            ),
            (
                "png".into(),
                ParameterValue::Text("png-0.17.16-rgb8-best-no_filter-no_chunks".into()),
            ),
            (
                "max_output_width".into(),
                ParameterValue::Unsigned(u64::from(request.limits.max_width())),
            ),
            (
                "max_output_height".into(),
                ParameterValue::Unsigned(u64::from(request.limits.max_height())),
            ),
            (
                "max_canvas_bytes".into(),
                unsigned_usize(request.limits.max_canvas_bytes())?,
            ),
            (
                "max_encoded_bytes".into(),
                unsigned_usize(request.limits.max_encoded_bytes())?,
            ),
            (
                "max_source_frames".into(),
                unsigned_usize(request.limits.max_source_frames())?,
            ),
        ]
        .into_iter()
        .collect::<BTreeMap<_, _>>(),
    )
}

fn region_value(region: RegionDefinition) -> Result<ParameterValue> {
    match region {
        RegionDefinition::FixedSourceImage { rect } => object([
            ("kind", ParameterValue::Text("fixed_source_image".into())),
            ("rect", signed_rect_value(rect)?),
        ]),
        RegionDefinition::FixedViewport { rect, mapping } => object([
            ("kind", ParameterValue::Text("fixed_viewport".into())),
            ("rect", signed_rect_value(rect)?),
            (
                "mapping",
                object([
                    (
                        "viewport_dimensions",
                        dimensions_value(mapping.viewport_dimensions())?,
                    ),
                    ("scale_x", rational_value(mapping.scale_x())?),
                    ("scale_y", rational_value(mapping.scale_y())?),
                ])?,
            ),
        ]),
    }
}

fn signed_rect_value(rect: SignedPixelRect) -> Result<ParameterValue> {
    object([
        ("x", ParameterValue::Signed(rect.x())),
        ("y", ParameterValue::Signed(rect.y())),
        ("width", ParameterValue::Unsigned(u64::from(rect.width()))),
        ("height", ParameterValue::Unsigned(u64::from(rect.height()))),
    ])
}

fn optional_rect_value(rect: Option<PixelRect>) -> Result<ParameterValue> {
    rect.map_or_else(
        || Ok(ParameterValue::Text("none".into())),
        |rect| {
            object([
                ("x", ParameterValue::Unsigned(u64::from(rect.x()))),
                ("y", ParameterValue::Unsigned(u64::from(rect.y()))),
                ("width", ParameterValue::Unsigned(u64::from(rect.width()))),
                ("height", ParameterValue::Unsigned(u64::from(rect.height()))),
            ])
        },
    )
}

fn padding_value(padding: PaddingInsets) -> Result<ParameterValue> {
    object([
        ("left", ParameterValue::Unsigned(u64::from(padding.left()))),
        ("top", ParameterValue::Unsigned(u64::from(padding.top()))),
        (
            "right",
            ParameterValue::Unsigned(u64::from(padding.right())),
        ),
        (
            "bottom",
            ParameterValue::Unsigned(u64::from(padding.bottom())),
        ),
    ])
}

fn rational_value(scale: RationalScale) -> Result<ParameterValue> {
    object([
        (
            "numerator",
            ParameterValue::Unsigned(u64::from(scale.numerator())),
        ),
        (
            "denominator",
            ParameterValue::Unsigned(u64::from(scale.denominator())),
        ),
    ])
}

fn dimensions_value(dimensions: PixelDimensions) -> Result<ParameterValue> {
    object([
        (
            "width",
            ParameterValue::Unsigned(u64::from(dimensions.width())),
        ),
        (
            "height",
            ParameterValue::Unsigned(u64::from(dimensions.height())),
        ),
    ])
}

fn mask_sha256(mask: &BinaryMask) -> String {
    let mut digest = Sha256::new();
    digest.update(mask.dimensions().width().to_be_bytes());
    digest.update(mask.dimensions().height().to_be_bytes());
    digest.update(mask.bits());
    digest
        .finalize()
        .iter()
        .map(|byte| format!("{byte:02x}"))
        .collect()
}

fn rgb_value(color: Rgb8) -> ParameterValue {
    ParameterValue::List(
        color
            .channels()
            .into_iter()
            .map(|channel| ParameterValue::Unsigned(u64::from(channel)))
            .collect(),
    )
}

fn signed_offset_value(value: i128) -> Result<ParameterValue> {
    let sign = if value < 0 { "negative" } else { "nonnegative" };
    let magnitude = u64::try_from(value.unsigned_abs()).map_err(|_| render_limit_error())?;
    object([
        ("sign", ParameterValue::Text(sign.into())),
        ("magnitude_nanos", ParameterValue::Unsigned(magnitude)),
    ])
}

fn unsigned_usize(value: usize) -> Result<ParameterValue> {
    u64::try_from(value)
        .map(ParameterValue::Unsigned)
        .map_err(|_| render_limit_error())
}

fn object<const N: usize>(entries: [(&'static str, ParameterValue); N]) -> Result<ParameterValue> {
    let values = entries
        .into_iter()
        .map(|(key, value)| (Box::<str>::from(key), value))
        .collect::<BTreeMap<_, _>>();
    Parameters::new(values.clone())?;
    Ok(ParameterValue::Object(values))
}

fn render_limit_error() -> VisionError {
    VisionError::new(
        ErrorCode::ResourceLimitExceeded,
        "region filmstrip exceeds configured processing or rendering limits",
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{DeclaredGap, Frame, Marker, PixelFormat};

    fn source(frame_count: u8) -> FrameSequence<u8, u8, u8, Box<[u8]>> {
        let dimensions = PixelDimensions::new(8, 6).unwrap();
        FrameSequence::new(
            (0..frame_count)
                .map(|id| {
                    Frame::new(
                        id,
                        Timestamp::from_nanos(u64::from(id) * 10),
                        dimensions,
                        PixelFormat::Rgba8SrgbStraight,
                        vec![0; dimensions.rgba8_byte_len().unwrap()].into_boxed_slice(),
                    )
                    .unwrap()
                })
                .collect(),
            Vec::<Marker<u8>>::new(),
            Vec::<DeclaredGap<u8>>::new(),
            None,
            None,
        )
        .unwrap()
    }

    #[test]
    fn resolves_outward_viewport_mapping_and_rejects_contradictions() {
        let mapping = ViewportMapping::new(
            PixelDimensions::new(4, 3).unwrap(),
            RationalScale::new(NonZeroU32::new(2).unwrap(), NonZeroU32::new(1).unwrap()),
            RationalScale::new(NonZeroU32::new(2).unwrap(), NonZeroU32::new(1).unwrap()),
        );
        let rect = SignedPixelRect::new(
            -1,
            1,
            NonZeroU32::new(3).unwrap(),
            NonZeroU32::new(2).unwrap(),
        )
        .unwrap();
        let plan = plan_region_filmstrip(
            &source(2),
            RegionDefinition::FixedViewport { rect, mapping },
            Timestamp::ZERO,
            FilmstripTileLimit::DEFAULT,
            None,
        )
        .unwrap();
        assert_eq!(plan.resolved_source_region().x(), -2);
        assert_eq!(plan.resolved_source_region().width(), 6);
        assert_eq!(plan.tiles()[0].padding().left(), 2);
        assert_eq!(plan.tiles()[0].source_rect().unwrap().width(), 4);

        let wrong = ViewportMapping::new(
            PixelDimensions::new(5, 3).unwrap(),
            mapping.scale_x(),
            mapping.scale_y(),
        );
        assert_eq!(
            plan_region_filmstrip(
                &source(2),
                RegionDefinition::FixedViewport {
                    rect,
                    mapping: wrong,
                },
                Timestamp::ZERO,
                FilmstripTileLimit::DEFAULT,
                None,
            )
            .unwrap_err()
            .code,
            ErrorCode::InvalidScale
        );
    }

    #[test]
    fn exact_padding_and_selection_cover_partial_and_fully_outside_regions() {
        let partial = SignedPixelRect::new(
            -2,
            -1,
            NonZeroU32::new(12).unwrap(),
            NonZeroU32::new(9).unwrap(),
        )
        .unwrap();
        let plan = plan_region_filmstrip(
            &source(10),
            RegionDefinition::FixedSourceImage { rect: partial },
            Timestamp::from_nanos(50),
            FilmstripTileLimit::new(4).unwrap(),
            None,
        )
        .unwrap();
        assert_eq!(
            plan.tiles()
                .iter()
                .map(FilmstripTilePlan::frame_index)
                .collect::<Vec<_>>(),
            [0, 3, 6, 9]
        );
        assert_eq!(plan.locator_frame_index(), 6);
        assert_eq!(plan.omitted_frame_count(), 6);
        let tile = &plan.tiles()[0];
        assert_eq!(
            tile.source_rect(),
            Some(PixelRect::new(0, 0, 8, 6).unwrap())
        );
        assert_eq!(
            tile.padding(),
            PaddingInsets {
                left: 2,
                top: 1,
                right: 2,
                bottom: 2
            }
        );

        let outside = SignedPixelRect::new(
            20,
            20,
            NonZeroU32::new(3).unwrap(),
            NonZeroU32::new(2).unwrap(),
        )
        .unwrap();
        let plan = plan_region_filmstrip(
            &source(1),
            RegionDefinition::FixedSourceImage { rect: outside },
            Timestamp::ZERO,
            FilmstripTileLimit::DEFAULT,
            None,
        )
        .unwrap();
        assert_eq!(plan.tiles()[0].source_rect(), None);
        assert_eq!(
            plan.tiles()[0].padding(),
            PaddingInsets {
                left: 0,
                top: 0,
                right: 3,
                bottom: 2
            }
        );
    }

    #[test]
    fn selected_tile_subsequence_matches_full_normalization() {
        let source = source(10);
        let region = SignedPixelRect::new(
            0,
            0,
            NonZeroU32::new(8).unwrap(),
            NonZeroU32::new(6).unwrap(),
        )
        .unwrap();
        let plan = plan_region_filmstrip(
            &source,
            RegionDefinition::FixedSourceImage { rect: region },
            Timestamp::from_nanos(50),
            FilmstripTileLimit::new(4).unwrap(),
            None,
        )
        .unwrap();
        let tile_source = FrameSequence::new(
            plan.tiles()
                .iter()
                .map(|tile| source.frames()[tile.frame_index()].clone())
                .collect(),
            Vec::<Marker<u8>>::new(),
            Vec::<DeclaredGap<u8>>::new(),
            None,
            None,
        )
        .unwrap();
        let limits = ProcessingLimits::default();
        let parameters = |sequence: &FrameSequence<u8, u8, u8, Box<[u8]>>| {
            normalize_sequence(
                sequence,
                NormalizationParameters::new(
                    Rgb8::new(0, 0, 0),
                    Some(PixelRect::new(0, 0, 8, 6).unwrap()),
                    IntegerScale::IDENTITY,
                    limits,
                ),
            )
            .unwrap()
        };
        let full = parameters(&source);
        let selected = parameters(&tile_source);
        for (position, tile) in plan.tiles().iter().enumerate() {
            assert_eq!(
                selected.frames()[position],
                full.frames()[tile.frame_index()]
            );
        }
    }

    #[test]
    fn filmstrip_120_frames_fit_when_only_selected_tiles_are_retained() {
        let dimensions = PixelDimensions::new(128, 128).unwrap();
        let source = sized_source(120, dimensions);
        let region = SignedPixelRect::new(
            0,
            0,
            NonZeroU32::new(128).unwrap(),
            NonZeroU32::new(128).unwrap(),
        )
        .unwrap();
        let limits = RegionFilmstripRenderLimits::new(
            NonZeroU32::new(4_096).unwrap(),
            NonZeroU32::new(4_096).unwrap(),
            NonZeroUsize::new(1_000_000).unwrap(),
            NonZeroUsize::new(1_000_000).unwrap(),
        )
        .with_max_source_frames(NonZeroUsize::new(120).unwrap());
        let artifact = generate_region_filmstrip(
            1_u32,
            &source,
            RegionFilmstripParameters::new(
                RegionDefinition::FixedSourceImage { rect: region },
                Timestamp::from_nanos(400),
                FilmstripTileLimit::new(3).unwrap(),
                Rgb8::new(0, 0, 0),
                Rgb8::new(255, 0, 255),
                IntegerScale::IDENTITY,
                RegionFilmstripLabels::new("FILMSTRIP", "SOURCE").unwrap(),
                limits,
            ),
        )
        .unwrap();
        assert_eq!(artifact.plan().tiles().len(), 3);
        assert!(!artifact.image().bytes().is_empty());
    }

    fn sized_source(
        frame_count: usize,
        dimensions: PixelDimensions,
    ) -> FrameSequence<u32, u32, u32, Box<[u8]>> {
        FrameSequence::new(
            (0..frame_count)
                .map(|id| {
                    Frame::new(
                        u32::try_from(id).unwrap(),
                        Timestamp::from_nanos(u64::try_from(id).unwrap() * 10),
                        dimensions,
                        PixelFormat::Rgba8SrgbStraight,
                        vec![0; dimensions.rgba8_byte_len().unwrap()].into_boxed_slice(),
                    )
                    .unwrap()
                })
                .collect(),
            Vec::<Marker<u32>>::new(),
            Vec::<DeclaredGap<u32>>::new(),
            None,
            None,
        )
        .unwrap()
    }

    fn tracked_params(regions: Vec<TrackedRegion>, tile_limit: u8) -> TrackedFilmstripParameters {
        TrackedFilmstripParameters::new(
            regions,
            Timestamp::ZERO,
            FilmstripTileLimit::new(tile_limit).unwrap(),
            Rgb8::new(0, 0, 0),
            Rgb8::new(32, 32, 32),
            IntegerScale::IDENTITY,
            RegionFilmstripLabels::new("TRACKED".to_owned(), "TEST".to_owned()).unwrap(),
            RegionFilmstripRenderLimits::default(),
        )
    }

    fn rect(x: i64, y: i64, w: u32, h: u32) -> SignedPixelRect {
        SignedPixelRect::new(
            x,
            y,
            NonZeroU32::new(w).unwrap(),
            NonZeroU32::new(h).unwrap(),
        )
        .unwrap()
    }

    #[test]
    fn tracked_filmstrip_crops_each_tile_from_its_own_region() {
        // 4 frames at 8x6; two 4x3 regions at opposite corners. Evenly-spaced
        // selection over 4 frames with limit 4 selects all frames, but only
        // frames 1 and 3 have regions.
        let seq = sized_source(4, PixelDimensions::new(8, 6).unwrap());
        let artifact = generate_tracked_region_filmstrip(
            "tracked",
            &seq,
            tracked_params(
                vec![
                    TrackedRegion {
                        frame_index: 1,
                        rect: rect(0, 0, 4, 3),
                    },
                    TrackedRegion {
                        frame_index: 3,
                        rect: rect(4, 3, 4, 3),
                    },
                ],
                4,
            ),
        )
        .unwrap();
        let tiles = artifact.plan().tiles();
        assert_eq!(tiles.len(), 4);
        // Frame 1: top-left region, fully inside the source.
        let tile1 = &tiles[1];
        assert_eq!(tile1.source_rect().unwrap().x(), 0);
        assert_eq!(tile1.source_rect().unwrap().y(), 0);
        assert!(tile1.padding().is_empty());
        // Frame 3: bottom-right region, fully inside the source.
        let tile3 = &tiles[3];
        assert_eq!(tile3.source_rect().unwrap().x(), 4);
        assert_eq!(tile3.source_rect().unwrap().y(), 3);
        assert!(tile3.padding().is_empty());
        assert!(!artifact.image().bytes().is_empty());
    }

    #[test]
    fn tracked_filmstrip_missing_selected_frame_renders_padded_tile() {
        // Regions only cover frames 0 and 3; the selected middle frames get
        // fully-padded tiles instead of an error.
        let seq = sized_source(4, PixelDimensions::new(8, 6).unwrap());
        let artifact = generate_tracked_region_filmstrip(
            "tracked",
            &seq,
            tracked_params(
                vec![
                    TrackedRegion {
                        frame_index: 0,
                        rect: rect(0, 0, 4, 3),
                    },
                    TrackedRegion {
                        frame_index: 3,
                        rect: rect(0, 0, 4, 3),
                    },
                ],
                4,
            ),
        )
        .unwrap();
        let tiles = artifact.plan().tiles();
        assert_eq!(tiles.len(), 4);
        for index in [1usize, 2] {
            let tile = &tiles[index];
            assert!(
                tile.source_rect().is_none(),
                "frame {index} without a region must be fully padded"
            );
            assert_eq!(tile.padding().left(), 4);
            assert_eq!(tile.padding().top(), 3);
        }
        assert!(!artifact.image().bytes().is_empty());
    }

    #[test]
    fn tracked_filmstrip_is_deterministic() {
        let seq = sized_source(3, PixelDimensions::new(8, 6).unwrap());
        let make = || {
            generate_tracked_region_filmstrip(
                "tracked",
                &seq,
                tracked_params(
                    vec![
                        TrackedRegion {
                            frame_index: 0,
                            rect: rect(0, 0, 4, 3),
                        },
                        TrackedRegion {
                            frame_index: 2,
                            rect: rect(2, 1, 4, 3),
                        },
                    ],
                    3,
                ),
            )
            .unwrap()
        };
        assert_eq!(make().image().bytes(), make().image().bytes());
    }

    #[test]
    fn tracked_filmstrip_rejects_inconsistent_crop_dimensions() {
        let seq = sized_source(3, PixelDimensions::new(8, 6).unwrap());
        let result = generate_tracked_region_filmstrip(
            "tracked",
            &seq,
            tracked_params(
                vec![
                    TrackedRegion {
                        frame_index: 0,
                        rect: rect(0, 0, 4, 3),
                    },
                    TrackedRegion {
                        frame_index: 1,
                        rect: rect(0, 0, 2, 3),
                    },
                ],
                3,
            ),
        );
        assert!(result.is_err());
    }
}