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
//! Translate an [`crate::op::ConvertPlan`] into an
//! [`oxideav_pipeline::Job`].
//!
//! The generated job has exactly one output keyed by the plan's
//! output path. That output owns one or two tracks — an `all`
//! selector that pulls every stream through the filter chain in order.
//! The pipeline executor figures out which tracks actually exist in
//! the input (just a video stream for a PNG, both audio + video for
//! an MP4) and dispatches filters to the matching `FilterKind` at
//! DAG-build time.
use crate::op::{ConvertPlan, Dither, Op};
use indexmap::IndexMap;
use oxideav_core::{Error, RuntimeContext};
use oxideav_pipeline::{FilterNode, Job, OutputSpec, SourceRef, TrackInput, TrackSpec};
use serde_json::{json, Value};
/// Build a [`Job`] from a [`ConvertPlan`].
///
/// `ctx` is consulted via its [`ContainerRegistry`](oxideav_core::ContainerRegistry)
/// to map the output extension to a codec id — sibling crates that
/// register a container (e.g. `oxideav-png`, `oxideav-qoi`,
/// `oxideav-dds`) automatically extend the supported output set without
/// needing a hard-coded entry here.
pub fn plan_to_job(plan: &ConvertPlan, ctx: &RuntimeContext) -> Result<Job, Error> {
// Walk ops, distinguishing track-side filters from sink-side
// metadata (format / quality / strip). Filters are flattened
// into the recursive `TrackInput` chain; the rest are deferred
// to the sink.
let mut codec_params = json!({});
let mut strip_metadata = false;
let mut format_override: Option<String> = None;
// Starting input: the file itself.
let mut chain: TrackInput = TrackInput::Source(SourceRef {
from: plan.input.clone(),
});
// A filter-chain step — wrap the current chain in a FilterNode.
let wrap = |prev: TrackInput, filter: &str, params: Value| {
TrackInput::Filter(FilterNode {
filter: filter.to_string(),
params,
input: Box::new(prev),
})
};
for op in &plan.ops {
match op {
Op::Resize {
width,
height,
mode,
} => {
// The pipeline's resize factory takes literal target
// dims today. We forward `width`/`height` along with
// the geometry mode tag so a future executor pass can
// resolve the source-aware variants (Fill/Shrink/Grow/
// Percent/Area) against the actual frame size at
// DAG-build time. Until that lands, only `Default` and
// `Force` are pixel-accurate on the pipeline path —
// the source-aware modes degrade to `Default` semantics
// when the executor sees a mode it doesn't understand.
// The PDF side-channel (which already knows the source
// dims) honours every mode today.
chain = wrap(
chain,
"video.resize",
json!({
"width": width,
"height": height,
"interpolation": "bilinear",
"mode": mode.as_tag(),
}),
);
}
Op::Thumbnail {
width,
height,
mode,
} => {
// `-thumbnail` is sugar for `Resize + Strip` (and, on a
// future pass, EXIF auto-orient). Emit both downstream
// ops so the executor sees the same shape as a
// hand-written `-resize ... -strip`.
chain = wrap(
chain,
"video.resize",
json!({
"width": width,
"height": height,
"interpolation": "bilinear",
"mode": mode.as_tag(),
}),
);
strip_metadata = true;
}
Op::Define { key, value } => {
// Forward the literal key (preserving any `:` namespace
// separator) onto the codec params bag. Values that
// parse as integers / floats / booleans still come
// through as JSON strings — codecs that care about
// type can re-parse from the string. Bare `-define KEY`
// (no `=VALUE`) becomes `{"KEY": true}`.
match value {
Some(v) => codec_params[key.clone()] = json!(v),
None => codec_params[key.clone()] = json!(true),
}
}
Op::Blur { radius, sigma } => {
chain = wrap(
chain,
"video.blur",
json!({
"radius": radius,
"sigma": sigma,
"planes": "all"
}),
);
}
Op::Edge { radius } => {
chain = wrap(chain, "video.edge", json!({ "radius": radius }));
}
Op::Colors { count, dither } => {
// Palette quantisation: rely on the pipeline's
// ConvertNode (pixfmt) which already supports
// `pal8` + dither via `oxideav-pixfmt`. We nest
// the palette step inline as a filter-shaped node
// that the executor recognises by name.
let dither_str = match dither {
Dither::None => "none",
Dither::Bayer => "bayer",
Dither::FloydSteinberg => "floyd_steinberg",
};
chain = wrap(
chain,
"video.pixfmt",
json!({
"format": "pal8",
"dither": dither_str,
"colors": count,
}),
);
}
Op::Format(fmt) => format_override = Some(fmt.clone()),
Op::Quality(q) => {
// Codec-specific key; the encoder drops it when
// unsupported. Use `quality` for JPEG/WebP, `crf`
// could be added by a later pass for h264/h265.
codec_params["quality"] = json!(q);
}
Op::Strip => strip_metadata = true,
// Vector-input ops; silently dropped on the raster
// pipeline path (raster inputs have no DPI, no
// composite-over-bg semantics that wouldn't conflict
// with `-resize`/etc., and no alpha-channel grammar to
// honour beyond what the encoder already does). The
// pdf_runner side-channel applies them when reading PDFs.
Op::Density(_) | Op::Background(_) | Op::Alpha(_) => {}
// Round-1 inline geometry / negate ops. The PDF
// side-channel applies these via crate::pixel_xform; the
// generic pipeline path now also wires them through to
// the matching `oxideav-image-filter` factory so non-PDF
// input paths (PNG/JPG/MP4/…) honour them too.
Op::Rotate { degrees } => {
chain = wrap(chain, "video.rotate", json!({ "degrees": degrees }));
}
Op::Flip => {
chain = wrap(chain, "video.flip", json!({}));
}
Op::Flop => {
chain = wrap(chain, "video.flop", json!({}));
}
Op::Crop { x, y, w, h } => {
chain = wrap(
chain,
"video.crop",
json!({ "x": x, "y": y, "width": w, "height": h }),
);
}
Op::Extent {
width,
height,
x,
y,
bg,
} => {
// The `oxideav-image-filter` `Extent` factory accepts
// `width`/`height` plus optional signed `offset_x` /
// `offset_y` and an RGBA `background` array. We forward
// everything verbatim — the background was already
// resolved against the source-order `-background` ops
// at args-parse time so the plan walker stays
// stateless.
chain = wrap(
chain,
"video.extent",
json!({
"width": width,
"height": height,
"offset_x": x,
"offset_y": y,
"background": [bg[0], bg[1], bg[2], bg[3]],
}),
);
}
Op::Negate => {
chain = wrap(chain, "video.negate", json!({}));
}
// ---- Round-next: tonal / colour-grading ops wired to
// the matching `oxideav-image-filter` factory. ----
Op::Sharpen { radius, sigma } => {
chain = wrap(
chain,
"video.sharpen",
json!({ "radius": radius, "sigma": sigma }),
);
}
Op::Unsharp {
radius,
sigma,
amount,
threshold,
} => {
chain = wrap(
chain,
"video.unsharp",
json!({
"radius": radius,
"sigma": sigma,
"amount": amount,
"threshold": threshold,
}),
);
}
Op::Gamma { value } => {
chain = wrap(chain, "video.gamma", json!({ "value": value }));
}
Op::BrightnessContrast {
brightness,
contrast,
} => {
chain = wrap(
chain,
"video.brightness-contrast",
json!({ "brightness": brightness, "contrast": contrast }),
);
}
Op::Contrast { delta } => {
// IM's bare `-contrast` flag bumps contrast by a single
// 5%-of-range step. Multiple `-contrast` accumulate
// linearly. Map to the brightness-contrast factory's
// contrast-only knob so the executor can fold this into
// a single LUT pass alongside any explicit
// `-brightness-contrast` already in the chain.
let pct = (*delta as f32) * 5.0;
chain = wrap(chain, "video.contrast", json!({ "value": pct }));
}
Op::Sepia { threshold } => {
chain = wrap(chain, "video.sepia", json!({ "threshold": threshold }));
}
Op::Modulate {
brightness,
saturation,
hue,
} => {
// IM's hue is "percent-of-base around 100" — 0 is
// -180°, 100 is identity, 200 is +180°. The
// image-filter factory accepts degrees directly via
// `hue_degrees`, so translate.
let hue_degrees = (hue - 100.0) * 1.8;
chain = wrap(
chain,
"video.modulate",
json!({
"brightness": brightness,
"saturation": saturation,
"hue_degrees": hue_degrees,
}),
);
}
Op::Level {
black,
gamma,
white,
} => {
chain = wrap(
chain,
"video.level",
json!({ "black": black, "gamma": gamma, "white": white }),
);
}
Op::Normalize => {
chain = wrap(chain, "video.normalize", json!({}));
}
Op::Threshold { value } => {
chain = wrap(chain, "video.threshold", json!({ "value": value }));
}
Op::Posterize { levels } => {
chain = wrap(chain, "video.posterize", json!({ "levels": levels }));
}
Op::Solarize { value } => {
chain = wrap(chain, "video.solarize", json!({ "value": value }));
}
Op::Colorspace(cs) => {
let lower = cs.to_ascii_lowercase();
if lower == "gray" || lower == "grey" {
chain = wrap(chain, "video.grayscale", json!({ "preserve_alpha": true }));
}
// `rgb` / `srgb` are recorded no-ops — input keeps its
// colourspace, downstream encoder converts as needed.
}
// ---- Round-after-next: vignette / colorize / equalize /
// auto-gamma. JSON shape mirrors the image-filter factories
// (resolves to identity defaults when the value is absent). ----
Op::Vignette {
radius,
sigma,
x,
y,
} => {
chain = wrap(
chain,
"video.vignette",
json!({ "x": x, "y": y, "radius": radius, "sigma": sigma }),
);
}
Op::Colorize { color, amount } => {
// Factory wants a 4-element JSON array for `color`.
chain = wrap(
chain,
"video.colorize",
json!({
"color": [color[0], color[1], color[2], color[3]],
"amount": amount,
}),
);
}
Op::Equalize => {
chain = wrap(chain, "video.equalize", json!({}));
}
Op::AutoGamma => {
chain = wrap(chain, "video.auto-gamma", json!({}));
}
Op::Trim { fuzz } => {
// The image-filter `Trim` factory accepts `fuzz` as a
// 0..=255 byte plus an optional `background` array. We
// forward only `fuzz` here — the args parser doesn't
// expose a per-`-trim` colour override yet, so the
// factory falls back to its corner-pixel auto-detection
// (matching IM's behaviour when neither `-bordercolor`
// nor `-background` is set).
chain = wrap(chain, "video.trim", json!({ "fuzz": fuzz }));
}
Op::Roll { dx, dy } => {
// The `oxideav-image-filter` `Roll` factory accepts
// signed `dx`/`dy` (also as `x`/`y` aliases); pixels
// that fall off one edge wrap around to the opposite
// edge so the visible image translates as a rigid
// block. Width/height stay unchanged, so no shape
// recovery is needed downstream.
chain = wrap(chain, "video.roll", json!({ "dx": dx, "dy": dy }));
}
}
}
// Sink-side metadata handling.
if strip_metadata {
codec_params["strip_metadata"] = json!(true);
}
if let Some(ref f) = format_override {
codec_params["format"] = json!(f);
}
// The pipeline insists every filter-terminated track carry an
// output codec — frames can't be stream-copied. Infer from the
// output extension (or `-format` override) so the IM-style
// `convert in.png -resize 64x64 out.jpg` works without the user
// stating the obvious.
let codec = codec_for_output(format_override.as_deref(), &plan.output, ctx);
let track = TrackSpec {
input: chain,
codec,
params: codec_params,
stream_selector: None,
};
let mut outputs = IndexMap::new();
outputs.insert(
plan.output.clone(),
OutputSpec {
audio: vec![],
video: vec![],
subtitle: vec![],
all: vec![track],
},
);
Ok(Job {
outputs,
aliases: IndexMap::new(),
threads: None,
})
}
/// Guess the output codec from the extension, honouring `-format`
/// override.
///
/// Resolution order:
///
/// 1. Consult [`ContainerRegistry::container_for_extension`]
/// (`oxideav_core::ContainerRegistry`). For image-format crates the
/// container name doubles as the codec id (e.g. `png` → `png`,
/// `qoi` → `qoi`, `dds` → `dds`), so a registered extension Just
/// Works without any hard-coded knowledge here. New sibling crates
/// extend the supported set the moment the umbrella registers them.
/// 2. Otherwise return `None` and let the pipeline infer per-track
/// (the right answer for video containers like MP4/MKV that don't
/// pin a single codec, and the prompt to add `-format CODEC` for
/// extensions nobody has registered yet).
fn codec_for_output(
format_override: Option<&str>,
output: &str,
ctx: &RuntimeContext,
) -> Option<String> {
let ext = format_override
.map(|s| s.to_ascii_lowercase())
.or_else(|| ext_of(output).map(|s| s.to_ascii_lowercase()))?;
ctx.containers
.container_for_extension(&ext)
.map(|s| s.to_string())
}
fn ext_of(path: &str) -> Option<&str> {
let last = path.rsplit('/').next().unwrap_or(path);
let last = last.split('?').next().unwrap_or(last);
let dot = last.rfind('.')?;
Some(&last[dot + 1..])
}
#[cfg(test)]
mod tests {
use super::*;
use crate::op::ResizeMode;
fn plan_with(ops: Vec<Op>) -> ConvertPlan {
ConvertPlan {
input: "in.png".into(),
input_pages: None,
ops,
output: "out.jpg".into(),
output_template: None,
ping: false,
probe: false,
probe_json: false,
probe_watch: false,
mesh3d_options: Default::default(),
}
}
/// Empty context — every extension resolves to `None` since no
/// crates have registered any extension/codec pairs yet.
fn empty_ctx() -> RuntimeContext {
RuntimeContext::new()
}
#[test]
fn empty_plan_is_passthrough() {
let job = plan_to_job(&plan_with(vec![]), &empty_ctx()).unwrap();
assert_eq!(job.outputs.len(), 1);
let (key, out) = job.outputs.iter().next().unwrap();
assert_eq!(key, "out.jpg");
assert_eq!(out.all.len(), 1);
match &out.all[0].input {
TrackInput::Source(s) => assert_eq!(s.from, "in.png"),
other => panic!("expected direct source, got {other:?}"),
}
}
#[test]
fn resize_produces_filter_wrapper() {
let job = plan_to_job(
&plan_with(vec![Op::Resize {
width: 64,
height: 32,
mode: ResizeMode::Default,
}]),
&empty_ctx(),
)
.unwrap();
let out = job.outputs.values().next().unwrap();
match &out.all[0].input {
TrackInput::Filter(f) => {
// `video.` prefix — the pipeline's dag uses it to
// route to FilterKind::Video.
assert_eq!(f.filter, "video.resize");
assert_eq!(f.params["width"], 64);
assert_eq!(f.params["height"], 32);
}
other => panic!("expected filter, got {other:?}"),
}
}
#[test]
fn chain_order_preserved() {
let job = plan_to_job(
&plan_with(vec![
Op::Resize {
width: 64,
height: 64,
mode: ResizeMode::Default,
},
Op::Blur {
radius: 2,
sigma: 1.0,
},
]),
&empty_ctx(),
)
.unwrap();
let out = job.outputs.values().next().unwrap();
// Ops apply outside-in on the recursive TrackInput: the
// innermost node is the source; the outermost filter is the
// last op in source order (blur).
let outer = match &out.all[0].input {
TrackInput::Filter(f) => f,
other => panic!("expected outer filter, got {other:?}"),
};
assert_eq!(outer.filter, "video.blur");
let inner = match outer.input.as_ref() {
TrackInput::Filter(f) => f,
other => panic!("expected inner filter, got {other:?}"),
};
assert_eq!(inner.filter, "video.resize");
}
#[test]
fn quality_and_strip_land_in_codec_params() {
let job = plan_to_job(&plan_with(vec![Op::Quality(85), Op::Strip]), &empty_ctx()).unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
assert_eq!(track.params["quality"], 85);
assert_eq!(track.params["strip_metadata"], true);
}
#[test]
fn resize_mode_tag_emitted_in_filter_params() {
// Each ResizeMode round-trips through the JSON params as a
// stable lowercase tag. Future executor passes can branch off
// these without us re-shaping the IR.
for (mode, tag) in [
(ResizeMode::Default, "default"),
(ResizeMode::Force, "force"),
(ResizeMode::Fill, "fill"),
(ResizeMode::Shrink, "shrink"),
(ResizeMode::Grow, "grow"),
(ResizeMode::Percent, "percent"),
(ResizeMode::Area, "area"),
] {
let job = plan_to_job(
&plan_with(vec![Op::Resize {
width: 100,
height: 100,
mode,
}]),
&empty_ctx(),
)
.unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = match &track.input {
TrackInput::Filter(f) => f,
other => panic!("expected resize filter, got {other:?}"),
};
assert_eq!(f.params["mode"], tag, "mode={mode:?}");
}
}
#[test]
fn thumbnail_unrolls_into_resize_plus_strip() {
let job = plan_to_job(
&plan_with(vec![Op::Thumbnail {
width: 200,
height: 200,
mode: ResizeMode::Fill,
}]),
&empty_ctx(),
)
.unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
// Resize node present with mode=fill.
let f = match &track.input {
TrackInput::Filter(f) => f,
other => panic!("expected outer resize filter, got {other:?}"),
};
assert_eq!(f.filter, "video.resize");
assert_eq!(f.params["mode"], "fill");
// And strip_metadata also set on the codec params side.
assert_eq!(track.params["strip_metadata"], true);
}
#[test]
fn define_with_value_lands_in_codec_params() {
let job = plan_to_job(
&plan_with(vec![Op::Define {
key: "jpeg:dct-method".into(),
value: Some("float".into()),
}]),
&empty_ctx(),
)
.unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
assert_eq!(track.params["jpeg:dct-method"], "float");
}
#[test]
fn define_bare_key_becomes_json_true() {
let job = plan_to_job(
&plan_with(vec![Op::Define {
key: "png:strip-comments".into(),
value: None,
}]),
&empty_ctx(),
)
.unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
assert_eq!(track.params["png:strip-comments"], true);
}
#[test]
fn multiple_defines_all_preserved() {
// Ensures sequential `-define` flags don't shadow each other.
let job = plan_to_job(
&plan_with(vec![
Op::Define {
key: "jpeg:dct-method".into(),
value: Some("float".into()),
},
Op::Define {
key: "jpeg:optimize-coding".into(),
value: Some("true".into()),
},
Op::Define {
key: "webp:lossless".into(),
value: None,
},
]),
&empty_ctx(),
)
.unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
assert_eq!(track.params["jpeg:dct-method"], "float");
assert_eq!(track.params["jpeg:optimize-coding"], "true");
assert_eq!(track.params["webp:lossless"], true);
}
// ---- codec_for_output coverage ----
#[test]
fn unknown_extension_returns_none() {
// Empty context — no registrations, so every lookup is None.
let ctx = empty_ctx();
assert!(codec_for_output(None, "out.jpg", &ctx).is_none());
assert!(codec_for_output(None, "out.mkv", &ctx).is_none());
assert!(codec_for_output(None, "out.unknown_ext_xyz", &ctx).is_none());
}
#[test]
fn registry_hit_returns_container_name() {
// Register a synthetic container under an extension and
// confirm the resolver picks it up. This is the path every
// image-format crate rides (qoi, dds, exr, icer, pict, jxs,
// jxl, jp2, png, qoi, …) once their `register_containers`
// call lands and the umbrella wires them in.
let mut ctx = RuntimeContext::new();
ctx.containers.register_extension("foo", "foo_codec");
assert_eq!(
codec_for_output(None, "out.foo", &ctx).as_deref(),
Some("foo_codec")
);
// Case-insensitive on the input filename.
assert_eq!(
codec_for_output(None, "OUT.FOO", &ctx).as_deref(),
Some("foo_codec")
);
}
/// Walk the recursive TrackInput chain and collect every filter
/// node's name in source order (innermost first → applied first).
fn collect_filter_names(input: &TrackInput) -> Vec<String> {
let mut names = Vec::new();
let mut cur = input;
while let TrackInput::Filter(f) = cur {
names.push(f.filter.clone());
cur = f.input.as_ref();
}
names.reverse();
names
}
/// Pull out the filter node matching `name` (first match found).
fn find_filter<'a>(input: &'a TrackInput, name: &str) -> Option<&'a FilterNode> {
let mut cur = input;
while let TrackInput::Filter(f) = cur {
if f.filter == name {
return Some(f);
}
cur = f.input.as_ref();
}
None
}
#[test]
fn sharpen_wires_to_video_sharpen_factory() {
let job = plan_to_job(
&plan_with(vec![Op::Sharpen {
radius: 2,
sigma: 1.0,
}]),
&empty_ctx(),
)
.unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.sharpen").expect("video.sharpen node");
assert_eq!(f.params["radius"], 2);
assert!((f.params["sigma"].as_f64().unwrap() - 1.0).abs() < 1e-6);
}
#[test]
fn unsharp_wires_full_params() {
let job = plan_to_job(
&plan_with(vec![Op::Unsharp {
radius: 3,
sigma: 1.5,
amount: 0.8,
threshold: 7,
}]),
&empty_ctx(),
)
.unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.unsharp").expect("video.unsharp node");
assert_eq!(f.params["radius"], 3);
assert!((f.params["sigma"].as_f64().unwrap() - 1.5).abs() < 1e-6);
assert!((f.params["amount"].as_f64().unwrap() - 0.8).abs() < 1e-6);
assert_eq!(f.params["threshold"], 7);
}
#[test]
fn gamma_wires_value_key() {
let job = plan_to_job(&plan_with(vec![Op::Gamma { value: 2.2 }]), &empty_ctx()).unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.gamma").expect("video.gamma node");
assert!((f.params["value"].as_f64().unwrap() - 2.2).abs() < 1e-6);
}
#[test]
fn brightness_contrast_wires_both_keys() {
let job = plan_to_job(
&plan_with(vec![Op::BrightnessContrast {
brightness: 10.0,
contrast: -5.0,
}]),
&empty_ctx(),
)
.unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.brightness-contrast")
.expect("video.brightness-contrast node");
assert!((f.params["brightness"].as_f64().unwrap() - 10.0).abs() < 1e-6);
assert!((f.params["contrast"].as_f64().unwrap() - (-5.0)).abs() < 1e-6);
}
#[test]
fn contrast_step_translates_to_factor() {
let job = plan_to_job(&plan_with(vec![Op::Contrast { delta: 1 }]), &empty_ctx()).unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.contrast").expect("video.contrast node");
// delta=1 → 5% contrast bump.
assert!((f.params["value"].as_f64().unwrap() - 5.0).abs() < 1e-6);
}
#[test]
fn modulate_translates_hue_to_degrees() {
// IM hue=200 (max) → +180°; hue=0 → -180°; hue=100 → 0°.
let job = plan_to_job(
&plan_with(vec![Op::Modulate {
brightness: 100.0,
saturation: 100.0,
hue: 200.0,
}]),
&empty_ctx(),
)
.unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.modulate").expect("video.modulate node");
assert!((f.params["brightness"].as_f64().unwrap() - 100.0).abs() < 1e-6);
assert!((f.params["saturation"].as_f64().unwrap() - 100.0).abs() < 1e-6);
assert!((f.params["hue_degrees"].as_f64().unwrap() - 180.0).abs() < 1e-6);
}
#[test]
fn level_wires_all_three_endpoints() {
let job = plan_to_job(
&plan_with(vec![Op::Level {
black: 16,
gamma: 1.2,
white: 235,
}]),
&empty_ctx(),
)
.unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.level").expect("video.level node");
assert_eq!(f.params["black"], 16);
assert!((f.params["gamma"].as_f64().unwrap() - 1.2).abs() < 1e-6);
assert_eq!(f.params["white"], 235);
}
#[test]
fn normalize_wires_empty_params() {
let job = plan_to_job(&plan_with(vec![Op::Normalize]), &empty_ctx()).unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.normalize").expect("video.normalize node");
// Defaults — empty object.
assert!(f.params.as_object().unwrap().is_empty());
}
#[test]
fn threshold_wires_value() {
let job =
plan_to_job(&plan_with(vec![Op::Threshold { value: 200 }]), &empty_ctx()).unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.threshold").expect("video.threshold node");
assert_eq!(f.params["value"], 200);
}
#[test]
fn posterize_wires_levels() {
let job = plan_to_job(&plan_with(vec![Op::Posterize { levels: 6 }]), &empty_ctx()).unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.posterize").expect("video.posterize node");
assert_eq!(f.params["levels"], 6);
}
#[test]
fn solarize_wires_value() {
let job = plan_to_job(&plan_with(vec![Op::Solarize { value: 100 }]), &empty_ctx()).unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.solarize").expect("video.solarize node");
assert_eq!(f.params["value"], 100);
}
#[test]
fn sepia_wires_threshold() {
let job =
plan_to_job(&plan_with(vec![Op::Sepia { threshold: 0.8 }]), &empty_ctx()).unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.sepia").expect("video.sepia node");
assert!((f.params["threshold"].as_f64().unwrap() - 0.8).abs() < 1e-6);
}
#[test]
fn colorspace_gray_wires_grayscale_factory() {
let job = plan_to_job(
&plan_with(vec![Op::Colorspace("Gray".into())]),
&empty_ctx(),
)
.unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.grayscale").expect("video.grayscale node");
assert_eq!(f.params["preserve_alpha"], true);
}
#[test]
fn colorspace_rgb_is_pure_passthrough() {
let job =
plan_to_job(&plan_with(vec![Op::Colorspace("RGB".into())]), &empty_ctx()).unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
// No filter wrapping the source — `Op::Colorspace("rgb")` is
// a recorded no-op.
match &track.input {
TrackInput::Source(_) => {}
other => panic!("expected raw source, got {other:?}"),
}
}
#[test]
fn rotate_wires_video_rotate_factory() {
let job = plan_to_job(&plan_with(vec![Op::Rotate { degrees: 90 }]), &empty_ctx()).unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.rotate").expect("video.rotate node");
assert_eq!(f.params["degrees"], 90);
}
#[test]
fn flip_flop_wire_independent_factories() {
let job = plan_to_job(&plan_with(vec![Op::Flip, Op::Flop]), &empty_ctx()).unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let names = collect_filter_names(&track.input);
assert_eq!(
names,
vec!["video.flip".to_string(), "video.flop".to_string()]
);
}
#[test]
fn crop_wires_xywh() {
let job = plan_to_job(
&plan_with(vec![Op::Crop {
x: 1,
y: 2,
w: 10,
h: 20,
}]),
&empty_ctx(),
)
.unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.crop").expect("video.crop node");
assert_eq!(f.params["x"], 1);
assert_eq!(f.params["y"], 2);
assert_eq!(f.params["width"], 10);
assert_eq!(f.params["height"], 20);
}
#[test]
fn negate_wires_video_negate_factory() {
let job = plan_to_job(&plan_with(vec![Op::Negate]), &empty_ctx()).unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
find_filter(&track.input, "video.negate").expect("video.negate node");
}
/// `Op::Extent` lowers to a `video.extent` FilterNode carrying
/// width / height / signed offsets / RGBA background — the four
/// keys the `oxideav-image-filter` `Extent` factory consumes.
#[test]
fn extent_wires_width_height_offsets_background() {
let job = plan_to_job(
&plan_with(vec![Op::Extent {
width: 200,
height: 150,
x: -10,
y: 20,
bg: [12, 34, 56, 200],
}]),
&empty_ctx(),
)
.unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.extent").expect("video.extent node");
assert_eq!(f.params["width"], 200);
assert_eq!(f.params["height"], 150);
assert_eq!(f.params["offset_x"], -10);
assert_eq!(f.params["offset_y"], 20);
let bg = f.params["background"].as_array().expect("background array");
assert_eq!(bg.len(), 4);
assert_eq!(bg[0], 12);
assert_eq!(bg[1], 34);
assert_eq!(bg[2], 56);
assert_eq!(bg[3], 200);
}
/// End-to-end shape check: build a plan with a sharpen op,
/// translate to a Job, then take the JSON params we emitted and
/// hand them to the **real** image-filter factory (registered in
/// the test ctx). The factory must accept them — confirming the
/// CLI side speaks the same JSON dialect the registry expects.
///
/// NOTE: this verifies the JSON-dialect contract once the new
/// image-filter factories land in the published registry. The
/// published 0.1.1 image-filter only registers `blur`, `edge`,
/// `resize`; the round-next factories (sharpen / gamma / etc.)
/// are on master and pending publish. We skip the assertion in
/// the standalone-published path to avoid flapping; inside the
/// workspace (where image-filter is path-patched to in-tree),
/// the assertion runs and proves the contract.
#[test]
fn sharpen_emitted_params_are_accepted_by_image_filter_registry() {
use oxideav_core::{PixelFormat, PortSpec, TimeBase};
let job = plan_to_job(
&plan_with(vec![Op::Sharpen {
radius: 1,
sigma: 0.5,
}]),
&empty_ctx(),
)
.unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.sharpen").expect("video.sharpen node");
// Stand up a real ctx with the image-filter factories wired.
let mut filter_ctx = RuntimeContext::new();
oxideav_image_filter::register(&mut filter_ctx);
// Skip the registry assertion when the build saw an older
// image-filter that hasn't published the round-next factories
// yet. The `wrap()`-produced JSON is the same in either case.
if !filter_ctx.filters.contains("sharpen") {
return;
}
// Pretend we're the executor: ask the registry to build a
// StreamFilter from the JSON params we just emitted.
let inputs = [PortSpec::video(
"in",
4,
4,
PixelFormat::Rgba,
TimeBase::new(1, 30),
)];
filter_ctx
.filters
.make("video.sharpen", &f.params, &inputs)
.expect("registry must accept the JSON params we emit");
}
/// Same shape check for every newly-wired round-next factory.
/// Driving them all through one test keeps the test count modest
/// while still catching the "we emitted a key the factory doesn't
/// recognise" class of bugs.
///
/// Same caveat as `sharpen_emitted_params_…`: skipped on
/// standalone-published until image-filter publishes the new
/// factories.
#[test]
fn every_round_next_op_round_trips_through_image_filter_registry() {
use oxideav_core::{PixelFormat, PortSpec, TimeBase};
// A representative input port; most factories ignore the dim
// values but Crop reads `width`/`height` so we use 16x16.
let make_inputs = |w: u32, h: u32, fmt: PixelFormat| {
[PortSpec::video("in", w, h, fmt, TimeBase::new(1, 30))]
};
let mut filter_ctx = RuntimeContext::new();
oxideav_image_filter::register(&mut filter_ctx);
// Skip the registry-build assertion when the linked image-filter
// pre-dates the round-next factories. JSON-shape coverage (the
// other tests) still runs.
if !filter_ctx.filters.contains("sharpen") {
return;
}
// (op, expected filter-id, inputs)
let cases: Vec<(Op, &str, [PortSpec; 1])> = vec![
(
Op::Sharpen {
radius: 1,
sigma: 0.5,
},
"video.sharpen",
make_inputs(4, 4, PixelFormat::Rgba),
),
(
Op::Unsharp {
radius: 1,
sigma: 0.5,
amount: 1.0,
threshold: 0,
},
"video.unsharp",
make_inputs(4, 4, PixelFormat::Rgba),
),
(
Op::Gamma { value: 1.8 },
"video.gamma",
make_inputs(4, 4, PixelFormat::Rgba),
),
(
Op::BrightnessContrast {
brightness: 10.0,
contrast: -5.0,
},
"video.brightness-contrast",
make_inputs(4, 4, PixelFormat::Rgba),
),
(
Op::Contrast { delta: 1 },
"video.contrast",
make_inputs(4, 4, PixelFormat::Rgba),
),
(
Op::Sepia { threshold: 0.8 },
"video.sepia",
make_inputs(4, 4, PixelFormat::Rgb24),
),
(
Op::Modulate {
brightness: 100.0,
saturation: 100.0,
hue: 100.0,
},
"video.modulate",
make_inputs(4, 4, PixelFormat::Rgba),
),
(
Op::Level {
black: 16,
gamma: 1.2,
white: 235,
},
"video.level",
make_inputs(4, 4, PixelFormat::Gray8),
),
(
Op::Normalize,
"video.normalize",
make_inputs(4, 4, PixelFormat::Gray8),
),
(
Op::Threshold { value: 128 },
"video.threshold",
make_inputs(4, 4, PixelFormat::Gray8),
),
(
Op::Posterize { levels: 4 },
"video.posterize",
make_inputs(4, 4, PixelFormat::Gray8),
),
(
Op::Solarize { value: 100 },
"video.solarize",
make_inputs(4, 4, PixelFormat::Gray8),
),
(
Op::Colorspace("Gray".into()),
"video.grayscale",
make_inputs(4, 4, PixelFormat::Rgba),
),
(
Op::Rotate { degrees: 90 },
"video.rotate",
make_inputs(4, 4, PixelFormat::Rgba),
),
(Op::Flip, "video.flip", make_inputs(4, 4, PixelFormat::Rgba)),
(Op::Flop, "video.flop", make_inputs(4, 4, PixelFormat::Rgba)),
(
Op::Crop {
x: 1,
y: 1,
w: 2,
h: 2,
},
"video.crop",
make_inputs(16, 16, PixelFormat::Rgba),
),
(
Op::Negate,
"video.negate",
make_inputs(4, 4, PixelFormat::Rgba),
),
];
// Round-after-next factories. Skip them as a group when the
// linked image-filter pre-dates the vignette/colorize/equalize/
// auto-gamma additions (published 0.1.1 only registers up to the
// round-prev set). We piggy-back on the same skip pattern as
// above — if `vignette` isn't in the registry, none of the new
// four are.
let mut later_cases: Vec<(Op, &str, [PortSpec; 1])> = Vec::new();
if filter_ctx.filters.contains("vignette") {
later_cases.push((
Op::Vignette {
radius: 50.0,
sigma: 25.0,
x: 0.5,
y: 0.5,
},
"video.vignette",
make_inputs(4, 4, PixelFormat::Rgba),
));
later_cases.push((
Op::Colorize {
color: [200, 100, 50, 255],
amount: 0.4,
},
"video.colorize",
make_inputs(4, 4, PixelFormat::Rgba),
));
later_cases.push((
Op::Equalize,
"video.equalize",
make_inputs(4, 4, PixelFormat::Gray8),
));
later_cases.push((
Op::AutoGamma,
"video.auto-gamma",
make_inputs(4, 4, PixelFormat::Gray8),
));
}
let cases: Vec<(Op, &str, [PortSpec; 1])> = cases.into_iter().chain(later_cases).collect();
for (op, expected_name, inputs) in cases {
let job = plan_to_job(&plan_with(vec![op.clone()]), &empty_ctx()).unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, expected_name)
.unwrap_or_else(|| panic!("expected node {expected_name} for {op:?}"));
filter_ctx
.filters
.make(expected_name, &f.params, &inputs)
.unwrap_or_else(|e| {
panic!(
"registry rejected emitted params for {op:?}: {e:?} \
(json: {})",
f.params
)
});
}
}
/// True end-to-end on a 4x4 RGBA fixture: synthesise a frame, run
/// it through `image_filter::Sharpen` directly to get the expected
/// output bytes, then walk the same JSON params through the
/// registry-built filter and compare. This locks down the
/// "factory takes the same params we emit" contract at a pixel
/// level, not just at a build-success level.
///
/// Skipped on standalone-published until the image-filter sharpen
/// factory publishes (today's published 0.1.1 doesn't have it).
#[test]
fn sharpen_4x4_pixel_match_through_registry() {
use oxideav_core::{
filter::FilterContext, Frame, PixelFormat, PortSpec, Result as CoreResult, TimeBase,
VideoFrame, VideoPlane,
};
use oxideav_image_filter::{ImageFilter, Sharpen, VideoStreamParams};
// 4x4 RGBA fixture with an obvious horizontal edge so sharpen
// produces a visible delta.
let make_frame = || {
let mut data = Vec::with_capacity(4 * 4 * 4);
for _y in 0..4 {
for x in 0..4 {
let v = if x < 2 { 60 } else { 200 };
data.extend_from_slice(&[v, v, v, 255]);
}
}
VideoFrame {
pts: None,
planes: vec![VideoPlane { stride: 16, data }],
}
};
// Reference: call the library directly with the same params
// the CLI translates `-sharpen 1x0.5` to.
let reference = Sharpen::new(1, 0.5)
.apply(
&make_frame(),
VideoStreamParams {
format: PixelFormat::Rgba,
width: 4,
height: 4,
},
)
.expect("library Sharpen apply");
// Through the registry: build the plan, translate to a Job,
// grab the JSON, build the filter via the registry, run.
let plan = ConvertPlan {
input: "in.png".into(),
input_pages: None,
ops: vec![Op::Sharpen {
radius: 1,
sigma: 0.5,
}],
output: "out.png".into(),
output_template: None,
ping: false,
probe: false,
probe_json: false,
probe_watch: false,
mesh3d_options: Default::default(),
};
let job = plan_to_job(&plan, &empty_ctx()).unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.sharpen").expect("video.sharpen node");
let mut filter_ctx = RuntimeContext::new();
oxideav_image_filter::register(&mut filter_ctx);
// Skip when the linked image-filter pre-dates the sharpen
// factory (published 0.1.1 only registers blur/edge/resize).
if !filter_ctx.filters.contains("sharpen") {
return;
}
let inputs = [PortSpec::video(
"in",
4,
4,
PixelFormat::Rgba,
TimeBase::new(1, 30),
)];
let mut built = filter_ctx
.filters
.make("video.sharpen", &f.params, &inputs)
.expect("factory build");
struct Collect {
out: Vec<Frame>,
}
impl FilterContext for Collect {
fn emit(&mut self, _port: usize, frame: Frame) -> CoreResult<()> {
self.out.push(frame);
Ok(())
}
}
let mut col = Collect { out: Vec::new() };
built
.push(&mut col, 0, &Frame::Video(make_frame()))
.expect("filter push");
let registry_out = match col.out.into_iter().next().unwrap() {
Frame::Video(v) => v,
other => panic!("expected video frame, got {other:?}"),
};
// Pixel-exact match: same params, same input → same output.
assert_eq!(
registry_out.planes[0].data, reference.planes[0].data,
"registry-built sharpen must match library Sharpen byte-for-byte"
);
}
#[test]
fn vignette_wires_centre_radius_sigma() {
let job = plan_to_job(
&plan_with(vec![Op::Vignette {
radius: 50.0,
sigma: 25.0,
x: 0.5,
y: 0.5,
}]),
&empty_ctx(),
)
.unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.vignette").expect("video.vignette node");
assert!((f.params["x"].as_f64().unwrap() - 0.5).abs() < 1e-6);
assert!((f.params["y"].as_f64().unwrap() - 0.5).abs() < 1e-6);
assert!((f.params["radius"].as_f64().unwrap() - 50.0).abs() < 1e-6);
assert!((f.params["sigma"].as_f64().unwrap() - 25.0).abs() < 1e-6);
}
#[test]
fn colorize_wires_color_array_and_amount() {
let job = plan_to_job(
&plan_with(vec![Op::Colorize {
color: [200, 100, 50, 255],
amount: 0.4,
}]),
&empty_ctx(),
)
.unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.colorize").expect("video.colorize node");
let arr = f.params["color"].as_array().expect("color is array");
assert_eq!(arr.len(), 4);
assert_eq!(arr[0], 200);
assert_eq!(arr[1], 100);
assert_eq!(arr[2], 50);
assert_eq!(arr[3], 255);
assert!((f.params["amount"].as_f64().unwrap() - 0.4).abs() < 1e-6);
}
#[test]
fn equalize_wires_empty_params() {
let job = plan_to_job(&plan_with(vec![Op::Equalize]), &empty_ctx()).unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.equalize").expect("video.equalize node");
assert!(f.params.as_object().unwrap().is_empty());
}
#[test]
fn auto_gamma_wires_empty_params() {
let job = plan_to_job(&plan_with(vec![Op::AutoGamma]), &empty_ctx()).unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.auto-gamma").expect("video.auto-gamma node");
assert!(f.params.as_object().unwrap().is_empty());
}
#[test]
fn long_chain_preserves_source_order() {
// Sanity that all the new filters interleave with existing
// ones in source order (innermost = first parsed).
let job = plan_to_job(
&plan_with(vec![
Op::Resize {
width: 64,
height: 64,
mode: ResizeMode::Default,
},
Op::Sharpen {
radius: 1,
sigma: 0.5,
},
Op::Gamma { value: 1.8 },
Op::Negate,
]),
&empty_ctx(),
)
.unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let names = collect_filter_names(&track.input);
assert_eq!(
names,
vec![
"video.resize".to_string(),
"video.sharpen".to_string(),
"video.gamma".to_string(),
"video.negate".to_string(),
]
);
}
/// `Op::Trim` lowers to a `video.trim` FilterNode carrying the
/// captured fuzz tolerance. The factory infers the reference
/// background from the input's `(0, 0)` pixel when no explicit
/// `background` array is forwarded (IM's default behaviour).
#[test]
fn trim_wires_fuzz_value() {
let job = plan_to_job(&plan_with(vec![Op::Trim { fuzz: 17 }]), &empty_ctx()).unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.trim").expect("video.trim node");
assert_eq!(f.params["fuzz"], 17);
// No background override → factory falls back to corner pixel.
assert!(f.params.get("background").is_none());
}
/// Default `Op::Trim { fuzz: 0 }` is the canonical no-fuzz exact-
/// match shape — pin it explicitly so a future refactor that
/// switches the default representation surfaces here, not on the
/// pipeline executor.
#[test]
fn trim_default_fuzz_lowers_to_zero() {
let job = plan_to_job(&plan_with(vec![Op::Trim { fuzz: 0 }]), &empty_ctx()).unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.trim").expect("video.trim node");
assert_eq!(f.params["fuzz"], 0);
}
/// `Op::Roll` lowers to a `video.roll` FilterNode carrying the
/// signed `dx`/`dy` shift values. The image-filter factory
/// recognises both `dx`/`dy` and the `x`/`y` aliases; we forward
/// the canonical pair.
#[test]
fn roll_lowers_to_video_roll() {
let job = plan_to_job(&plan_with(vec![Op::Roll { dx: 5, dy: -10 }]), &empty_ctx()).unwrap();
let track = &job.outputs.values().next().unwrap().all[0];
let f = find_filter(&track.input, "video.roll").expect("video.roll node");
assert_eq!(f.params["dx"], 5);
assert_eq!(f.params["dy"], -10);
}
#[test]
fn format_override_uses_registry() {
let mut ctx = RuntimeContext::new();
ctx.containers.register_extension("bar", "bar_codec");
// -format BAR with output that ends in something else still
// lands on bar_codec.
assert_eq!(
codec_for_output(Some("BAR"), "out.unrelated", &ctx).as_deref(),
Some("bar_codec")
);
// -format with no matching registration → None (caller can
// surface a "use -format CODEC" hint).
assert!(codec_for_output(Some("nothing"), "out.unrelated", &ctx).is_none());
}
}