1//! GPU unit tests for the 14 OCIO / Layer / Metadata / Generator / Utility built-ins.
2
3use super::*;
4
5fn comp(tools: &str) -> Comp {
6 let src = format!(
7 "Composition {{ CurrentTime = 0, RenderRange = {{ 0, 10 }}, GlobalRange = {{ 0, 10 }}, Tools = ordered() {{ {tools} }} }}"
8 );
9 Comp::from_fusion_str(&src).expect("parse comp")
10}
11
12/// Float32 solid-colour `Background` named `name`.
13fn solid(name: &str, w: u32, h: u32, [r, g, b, a]: [f32; 4]) -> String {
14 format!(
15 "{name} = Background {{ Inputs = {{ Width = Input {{ Value = {w}, }}, Height = Input {{ Value = {h}, }}, Depth = Input {{ Value = 4, }}, \
16 TopLeftRed = Input {{ Value = {r}, }}, TopLeftGreen = Input {{ Value = {g}, }}, TopLeftBlue = Input {{ Value = {b}, }}, TopLeftAlpha = Input {{ Value = {a}, }}, }}, }},"
17 )
18}
19
20/// `name = <kind> { Inputs = { Input = <src>, <extra> } }`.
21fn tool(name: &str, kind: &str, src: &str, extra: &str) -> String {
22 format!("{name} = {kind} {{ Inputs = {{ Input = Input {{ SourceOp = \"{src}\", Source = \"Output\", }}, {extra} }}, }},")
23}
24
25fn with_z(name: &str, src: &str) -> String {
26 tool(name, "Dimension.CopyAux", src, "Channel = Input { Value = FuID { \"Z\" }, }, Mode = Input { Value = 1, },")
27}
28
29fn render(c: &Comp, name: &str, time: f64) -> Option<(CachedTexture, Vec<f32>)> {
30 let mut eng = Engine::new_headless().ok()?;
31 let (tex, stats) = eng.evaluate(c, c.find_node(name).expect("find node"), time);
32 assert!(stats.unsupported_nodes.is_empty(), "unsupported: {:?}", stats.unsupported_nodes);
33 let px = eng.readback_rgba_f32(&tex).unwrap();
34 Some((tex, px))
35}
36
37fn px(buf: &[f32], w: u32, x: u32, y: u32) -> [f32; 4] {
38 let i = ((y * w + x) * 4) as usize;
39 buf[i..i + 4].try_into().unwrap()
40}
41
42fn near(got: f32, want: f32, tol: f32, what: &str) {
43 assert!((got - want).abs() <= tol, "{what}: got {got}, want {want} (±{tol})");
44}
45
46fn srgb_oetf(x: f32) -> f32 {
47 if x <= 0.0031308 { x * 12.92 } else { 1.055 * x.powf(1.0 / 2.4) - 0.055 }
48}
49
50fn left_half_mask() -> &'static str {
51 "M = RectangleMask { Inputs = { MaskWidth = Input { Value = 32, }, MaskHeight = Input { Value = 16, }, \
52 Center = Input { Value = { 0.25, 0.5 }, }, Width = Input { Value = 0.5, }, Height = Input { Value = 1.0, }, }, },"
53}
54
55#[test]
56fn ocio_color_space_converts_transfer_gamut_and_preserves_hdr() {
57 let c = comp(&[
58 solid("Mid", 16, 16, [0.5, 0.5, 0.5, 1.0]),
59 solid("Red", 16, 16, [1.0, 0.0, 0.0, 1.0]),
60 solid("Hdr", 16, 16, [2.5, 2.5, 2.5, 1.0]),
61 solid("Gray", 16, 16, [0.18, 0.18, 0.18, 1.0]),
62 tool("Lin", "OCIOColorSpace", "Mid", "SourceSpace = Input { Value = \"sRGB - Texture\", }, OutputSpace = Input { Value = \"Linear Rec.709 (sRGB)\", },"),
63 tool("Ap1", "OCIOColorSpace", "Red", "SourceSpace = Input { Value = \"Linear Rec.709 (sRGB)\", }, OutputSpace = Input { Value = \"ACEScg\", },"),
64 tool("Log", "OCIOColorSpace", "Gray", "SourceSpace = Input { Value = \"Linear\", }, OutputSpace = Input { Value = \"ARRI LogC3\", },"),
65 tool("Back", "OCIOColorSpace", "Log", "SourceSpace = Input { Value = \"ARRI LogC3\", }, OutputSpace = Input { Value = \"Linear\", },"),
66 tool("Id", "OCIOColorSpace", "Hdr", "SourceSpace = Input { Value = \"ACEScg\", }, OutputSpace = Input { Value = \"ACEScg\", },"),
67 tool("Hdr2", "OCIOColorSpace", "Hdr", "SourceSpace = Input { Value = \"Linear\", }, OutputSpace = Input { Value = \"ACEScct\", },"),
68 tool("Off", "OCIOColorSpace", "Mid", "SourceSpace = Input { Value = \"sRGB\", }, Blend = Input { Value = 0, },"),
69 solid("Lft", 32, 16, [0.5, 0.5, 0.5, 1.0]),
70 left_half_mask().to_owned(),
71 tool("Masked", "OCIOColorSpace", "Lft", "SourceSpace = Input { Value = \"sRGB\", }, EffectMask = Input { SourceOp = \"M\", Source = \"Mask\", },"),
72 tool("MaskedInv", "OCIOColorSpace", "Lft", "SourceSpace = Input { Value = \"sRGB\", }, ApplyMaskInverted = Input { Value = 1, }, EffectMask = Input { SourceOp = \"M\", Source = \"Mask\", },"),
73 ].concat());
74 let Some((_, lin)) = render(&c, "Lin", 0.0) else { return };
75 near(px(&lin, 16, 3, 3)[0], 0.21404, 2e-3, "sRGB 0.5 -> linear");
76 let (_, ap1) = render(&c, "Ap1", 0.0).unwrap();
77 let p = px(&ap1, 16, 3, 3);
78 near(p[0], 0.6131, 0.01, "Rec.709 red -> ACEScg R");
79 near(p[1], 0.0702, 0.01, "Rec.709 red -> ACEScg G");
80 near(p[2], 0.0206, 0.01, "Rec.709 red -> ACEScg B");
81 let (_, log) = render(&c, "Log", 0.0).unwrap();
82 near(px(&log, 16, 3, 3)[0], 0.391, 0.01, "LogC3 of 18% gray");
83 let (_, back) = render(&c, "Back", 0.0).unwrap();
84 near(px(&back, 16, 3, 3)[0], 0.18, 2e-3, "LogC3 round trip");
85 let (_, id) = render(&c, "Id", 0.0).unwrap();
86 assert_eq!(px(&id, 16, 3, 3), [2.5, 2.5, 2.5, 1.0], "identity keeps HDR exactly");
87 let (_, cct) = render(&c, "Hdr2", 0.0).unwrap();
88 assert!(px(&cct, 16, 3, 3)[0] > 0.5, "ACEScct encodes HDR without clamping");
89 let (_, off) = render(&c, "Off", 0.0).unwrap();
90 assert_eq!(px(&off, 16, 3, 3), [0.5, 0.5, 0.5, 1.0], "Blend=0 passes through");
91 let (_, masked) = render(&c, "Masked", 0.0).unwrap();
92 near(px(&masked, 32, 4, 8)[0], 0.21404, 2e-3, "inside mask converted");
93 near(px(&masked, 32, 28, 8)[0], 0.5, 1e-4, "outside mask untouched");
94 let (_, inv) = render(&c, "MaskedInv", 0.0).unwrap();
95 near(px(&inv, 32, 4, 8)[0], 0.5, 1e-4, "ApplyMaskInverted keeps inside");
96 near(px(&inv, 32, 28, 8)[0], 0.21404, 2e-3, "ApplyMaskInverted converts outside");
97}
98
99#[test]
100fn ocio_display_applies_exposure_curve_gamma_and_view() {
101 let c = comp(&[
102 solid("Gray", 16, 16, [0.18, 0.18, 0.18, 1.0]),
103 solid("Hdr", 16, 16, [4.0, 4.0, 4.0, 1.0]),
104 tool("Def", "OCIODisplay", "Gray", ""),
105 tool("Ev1", "OCIODisplay", "Gray", "Exposure = Input { Value = 1, },"),
106 tool("G2", "OCIODisplay", "Gray", "Gamma = Input { Value = 2, },"),
107 tool("P2", "OCIODisplay", "Gray", "DisplayDevice = Input { Value = \"Rec.709\", },"),
108 tool("Raw", "OCIODisplay", "Hdr", ""),
109 tool("Aces", "OCIODisplay", "Hdr", "ViewTransform = Input { Value = \"ACES\", },"),
110 tool("Srgb", "OCIODisplay", "Gray", "InputSpace = Input { Value = \"sRGB\", },"),
111 tool("Off", "OCIODisplay", "Gray", "Blend = Input { Value = 0, },"),
112 ].concat());
113 let Some((_, def)) = render(&c, "Def", 0.0) else { return };
114 near(px(&def, 16, 4, 4)[0], srgb_oetf(0.18), 2e-3, "sRGB display of 18% gray");
115 let (_, ev) = render(&c, "Ev1", 0.0).unwrap();
116 near(px(&ev, 16, 4, 4)[0], srgb_oetf(0.36), 2e-3, "+1 stop doubles linear before the curve");
117 let (_, g2) = render(&c, "G2", 0.0).unwrap();
118 near(px(&g2, 16, 4, 4)[0], srgb_oetf(0.18).sqrt(), 2e-3, "post gamma");
119 let (_, p2) = render(&c, "P2", 0.0).unwrap();
120 near(px(&p2, 16, 4, 4)[0], 0.18_f32.powf(1.0 / 2.4), 2e-3, "Rec.709 display uses gamma 2.4");
121 let (_, raw) = render(&c, "Raw", 0.0).unwrap();
122 assert!(px(&raw, 16, 4, 4)[0] > 1.0, "no view transform keeps HDR above 1");
123 let (_, aces) = render(&c, "Aces", 0.0).unwrap();
124 let a = px(&aces, 16, 4, 4)[0];
125 assert!(a > 0.5 && a < 1.0, "ACES view rolls HDR into range: {a}");
126 let (_, srgb_in) = render(&c, "Srgb", 0.0).unwrap();
127 assert!(px(&srgb_in, 16, 4, 4)[0] < px(&def, 16, 4, 4)[0], "sRGB-encoded input decodes before the display curve");
128 let (_, off) = render(&c, "Off", 0.0).unwrap();
129 assert_eq!(px(&off, 16, 4, 4)[0], 0.18);
130}
131
132const POW2_LUT: &str = "LUT_1D_SIZE 5\\n0 0 0\\n0.0625 0.0625 0.0625\\n0.25 0.25 0.25\\n0.5625 0.5625 0.5625\\n1 1 1\\n";
133const SWAP_RB_CLF: &str = "<ProcessList><LUT3D inBitDepth=\\\"32f\\\" outBitDepth=\\\"32f\\\"><Array dim=\\\"2 2 2 3\\\">\
134 0 0 0 1 0 0 0 1 0 1 1 0 0 0 1 1 0 1 0 1 1 1 1 1</Array></LUT3D></ProcessList>";
135const DOUBLE_CDL: &str = "<ASC_CDL id=\\\"a\\\"><SOPNode><Slope>2 2 2</Slope><Offset>0 0 0</Offset><Power>1 1 1</Power></SOPNode>\
136 <SATNode><Saturation>1</Saturation></SATNode></ASC_CDL>";
137
138#[test]
139fn ocio_file_transform_luts_cdl_clf_direction_and_interpolation() {
140 let file = |spec: &str, extra: &str| format!("File = Input {{ Value = \"{spec}\", }}, {extra}");
141 let c = comp(&[
142 solid("Src", 16, 16, [0.5, 0.5, 0.5, 1.0]),
143 solid("Low", 16, 16, [0.3, 0.3, 0.3, 1.0]),
144 solid("Rgb", 16, 16, [0.2, 0.4, 0.6, 1.0]),
145 tool("Fwd", "OCIOFileTransform", "Src", &file(POW2_LUT, "")),
146 tool("Inv", "OCIOFileTransform", "Fwd", &file(POW2_LUT, "Direction = Input { Value = \"Inverse\", },")),
147 tool("Lin", "OCIOFileTransform", "Low", &file(POW2_LUT, "Interpolation = Input { Value = \"Linear\", },")),
148 tool("Near", "OCIOFileTransform", "Low", &file(POW2_LUT, "Interpolation = Input { Value = \"Nearest\", },")),
149 tool("Clf", "OCIOFileTransform", "Rgb", &file(SWAP_RB_CLF, "")),
150 tool("Cdl", "OCIOFileTransform", "Low", &file(DOUBLE_CDL, "")),
151 tool("CdlInv", "OCIOFileTransform", "Low", &file(DOUBLE_CDL, "Direction = Input { Value = \"Inverse\", },")),
152 tool("Empty", "OCIOFileTransform", "Rgb", &file("", "")),
153 tool("Missing", "OCIOFileTransform", "Rgb", &file("/nonexistent/grade.cube", "")),
154 tool("Off", "OCIOFileTransform", "Src", &file(POW2_LUT, "Blend = Input { Value = 0, },")),
155 ].concat());
156 let Some((_, fwd)) = render(&c, "Fwd", 0.0) else { return };
157 near(px(&fwd, 16, 3, 3)[0], 0.25, 2e-3, "1D .cube forward");
158 let (_, inv) = render(&c, "Inv", 0.0).unwrap();
159 near(px(&inv, 16, 3, 3)[0], 0.5, 5e-3, "Inverse undoes the forward LUT");
160 let (_, lin) = render(&c, "Lin", 0.0).unwrap();
161 near(px(&lin, 16, 3, 3)[0], 0.1, 2e-3, "Linear interpolation between nodes");
162 let (_, near_px) = render(&c, "Near", 0.0).unwrap();
163 near(px(&near_px, 16, 3, 3)[0], 0.0625, 2e-3, "Nearest snaps to the closest node");
164 let (_, clf) = render(&c, "Clf", 0.0).unwrap();
165 let p = px(&clf, 16, 3, 3);
166 near(p[0], 0.6, 2e-3, "CLF LUT3D B-fastest order: R<-B");
167 near(p[1], 0.4, 2e-3, "CLF LUT3D G");
168 near(p[2], 0.2, 2e-3, "CLF LUT3D B<-R");
169 let (_, cdl) = render(&c, "Cdl", 0.0).unwrap();
170 near(px(&cdl, 16, 3, 3)[0], 0.6, 2e-3, "CDL slope 2");
171 let (_, cdl_inv) = render(&c, "CdlInv", 0.0).unwrap();
172 near(px(&cdl_inv, 16, 3, 3)[0], 0.15, 2e-3, "CDL inverse");
173 for name in ["Empty", "Missing"] {
174 let (_, same) = render(&c, name, 0.0).unwrap();
175 assert_eq!(px(&same, 16, 3, 3), [0.2, 0.4, 0.6, 1.0], "{name} passes through");
176 }
177 let (_, off) = render(&c, "Off", 0.0).unwrap();
178 assert_eq!(px(&off, 16, 3, 3)[0], 0.5);
179}
180
181#[test]
182fn layer_muxer_regex_and_remover_route_aux_without_touching_rgba() {
183 let c = comp(&[
184 solid("Plain", 16, 16, [0.2, 0.4, 0.6, 1.0]),
185 solid("Other", 16, 16, [0.9, 0.1, 0.1, 1.0]),
186 with_z("Deep", "Other"),
187 "Mux = LayerMuxer { Inputs = { Background = Input { SourceOp = \"Plain\", Source = \"Output\", }, Foreground1 = Input { SourceOp = \"Deep\", Source = \"Output\", }, }, },".to_owned(),
188 "MuxMain = LayerMuxer { Inputs = { Background = Input { SourceOp = \"Deep\", Source = \"Output\", }, Input1 = Input { SourceOp = \"Plain\", Source = \"Output\", }, }, },".to_owned(),
189 "MuxEmpty = LayerMuxer { },".to_owned(),
190 tool("KeepAll", "LayerRegex", "Deep", ""),
191 tool("KeepZ", "LayerRegex", "Deep", "Pattern = Input { Value = \"^z$\", },"),
192 tool("KeepNormal", "LayerRegex", "Deep", "Pattern = Input { Value = \"^normal$\", },"),
193 tool("InvZ", "LayerRegex", "Deep", "Pattern = Input { Value = \"^z$\", }, Invert = Input { Value = 1, },"),
194 tool("KillZ", "LayerRemover", "Deep", "KillZ = Input { Value = 1, },"),
195 tool("KillByName", "LayerRemover", "Deep", "Layers = Input { Value = \"^z$|^normal$\", },"),
196 tool("KillNormal", "LayerRemover", "Deep", "KillNormal = Input { Value = 1, },"),
197 ].concat());
198 let has_z = |t: &CachedTexture| t.aux.as_ref().is_some_and(|a| a.z.is_some());
199 let Some((plain, plain_px)) = render(&c, "Plain", 0.0) else { return };
200 assert!(!has_z(&plain));
201
202 let (mux, mux_px) = render(&c, "Mux", 0.0).unwrap();
203 assert!(has_z(&mux), "Background lacks Z so the foreground's Z is routed in");
204 assert_eq!(mux_px, plain_px, "main RGBA comes from the Background");
205 let (mux_main, mux_main_px) = render(&c, "MuxMain", 0.0).unwrap();
206 let (deep, deep_px) = render(&c, "Deep", 0.0).unwrap();
207 assert!(has_z(&mux_main) && mux_main.content_hash == deep.content_hash, "nothing to route: input returned untouched");
208 assert_eq!(mux_main_px, deep_px);
209 assert_eq!(render(&c, "MuxEmpty", 0.0).unwrap().0.size, [1, 1]);
210
211 for (name, expect_z) in [("KeepAll", true), ("KeepZ", true), ("KeepNormal", false), ("InvZ", false), ("KillZ", false), ("KillByName", false), ("KillNormal", true)] {
212 let (t, p) = render(&c, name, 0.0).unwrap();
213 assert_eq!(has_z(&t), expect_z, "{name} Z presence");
214 assert_eq!(p, deep_px, "{name} leaves RGBA lossless");
215 }
216}
217
218#[test]
219fn swizzler_remaps_channels_secondary_luminance_z_and_mask() {
220 let c = comp(&[
221 solid("Src", 32, 16, [0.2, 0.4, 0.6, 0.5]),
222 solid("Sec", 32, 16, [0.9, 0.8, 0.7, 0.1]),
223 solid("Hdr", 32, 16, [2.5, 2.5, 2.5, 1.0]),
224 with_z("Deep", "Src"),
225 left_half_mask().to_owned(),
226 tool("Swap", "Swizzler", "Src", "Red = Input { Value = \"Blue\", }, Blue = Input { Value = \"Red\", }, Green = Input { Value = \"Zero\", }, Alpha = Input { Value = \"One\", },"),
227 tool("Id", "Swizzler", "Src", ""),
228 tool("Inv", "Swizzler", "Src", "Red = Input { Value = \"InvertedRed\", },"),
229 tool("Luma", "Swizzler", "Hdr", "Red = Input { Value = \"Luminance\", },"),
230 tool("Z", "Swizzler", "Deep", "Green = Input { Value = \"Z\", },"),
231 "Sw2 = Swizzler { Inputs = { Input = Input { SourceOp = \"Src\", Source = \"Output\", }, Input2 = Input { SourceOp = \"Sec\", Source = \"Output\", }, \
232 Red = Input { Value = \"SecondaryRed\", }, Alpha = Input { Value = \"SecondaryAlpha\", }, }, },".to_owned(),
233 tool("Masked", "Swizzler", "Src", "Red = Input { Value = \"Blue\", }, Blue = Input { Value = \"Red\", }, EffectMask = Input { SourceOp = \"M\", Source = \"Mask\", },"),
234 tool("MaskedInv", "Swizzler", "Src", "Red = Input { Value = \"Blue\", }, Blue = Input { Value = \"Red\", }, ApplyMaskInverted = Input { Value = 1, }, EffectMask = Input { SourceOp = \"M\", Source = \"Mask\", },"),
235 tool("Mult", "Swizzler", "Src", "Red = Input { Value = \"Blue\", }, MultiplyByMask = Input { Value = 1, }, EffectMask = Input { SourceOp = \"M\", Source = \"Mask\", },"),
236 ].concat());
237 let Some((_, swap)) = render(&c, "Swap", 0.0) else { return };
238 assert_eq!(px(&swap, 32, 5, 5), [0.6, 0.0, 0.2, 1.0]);
239 let (_, id) = render(&c, "Id", 0.0).unwrap();
240 assert_eq!(px(&id, 32, 5, 5), [0.2, 0.4, 0.6, 0.5], "default selectors are identity");
241 let (_, inv) = render(&c, "Inv", 0.0).unwrap();
242 near(px(&inv, 32, 5, 5)[0], 0.8, 1e-6, "inverted red");
243 let (_, luma) = render(&c, "Luma", 0.0).unwrap();
244 near(px(&luma, 32, 5, 5)[0], 2.5, 1e-3, "luminance of HDR gray stays above 1");
245 let (_, z) = render(&c, "Z", 0.0).unwrap();
246 assert!(px(&z, 32, 5, 5)[1] != 0.4, "Z aux replaces green");
247 let (_, sw2) = render(&c, "Sw2", 0.0).unwrap();
248 let p = px(&sw2, 32, 5, 5);
249 near(p[0], 0.9, 1e-5, "secondary red");
250 near(p[3], 0.1, 1e-5, "secondary alpha");
251 near(p[1], 0.4, 1e-6, "untouched green");
252 let (_, masked) = render(&c, "Masked", 0.0).unwrap();
253 assert_eq!(px(&masked, 32, 4, 8)[0], 0.6, "inside mask swapped");
254 assert_eq!(px(&masked, 32, 28, 8)[0], 0.2, "outside mask untouched");
255 let (_, inverted) = render(&c, "MaskedInv", 0.0).unwrap();
256 assert_eq!(px(&inverted, 32, 4, 8)[0], 0.2);
257 assert_eq!(px(&inverted, 32, 28, 8)[0], 0.6);
258 let (_, mult) = render(&c, "Mult", 0.0).unwrap();
259 assert_eq!(px(&mult, 32, 28, 8), [0.0; 4], "MultiplyByMask zeroes outside the mask");
260}
261
262#[test]
263fn metadata_tools_and_dummy_are_lossless_passthroughs() {
264 let c = comp(&[
265 solid("Src", 16, 16, [0.2, 0.4, 0.6, 1.0]),
266 with_z("Deep", "Src"),
267 tool("Copy", "Fuse.FLS_CopyMetadata", "Deep", ""),
268 tool("Tc", "Fuse.SetMetaDataTC", "Deep", "TimeCode = Input { Value = \"01:00:00:00\", }, FPS = Input { Value = 24, }, DropFrame = Input { Value = 0, },"),
269 tool("Dum", "Dummy", "Deep", ""),
270 "Free = Dummy { }, FreeCopy = Fuse.FLS_CopyMetadata { }, FreeTc = Fuse.SetMetaDataTC { },".to_owned(),
271 ].concat());
272 let Some((deep, deep_px)) = render(&c, "Deep", 0.0) else { return };
273 for name in ["Copy", "Tc", "Dum"] {
274 let (t, p) = render(&c, name, 0.0).unwrap();
275 assert_eq!(p, deep_px, "{name} pixels");
276 assert_eq!((t.origin, t.canvas_size, t.depth), (deep.origin, deep.canvas_size, deep.depth), "{name} DoD/depth");
277 assert!(t.aux.as_ref().is_some_and(|a| a.z.is_some()), "{name} keeps aux.z");
278 }
279 for name in ["Free", "FreeCopy", "FreeTc"] {
280 assert_eq!(render(&c, name, 0.0).unwrap().0.size, [1, 1], "{name} disconnected");
281 }
282}
283
284#[test]
285fn multi_text_renders_each_layer_and_matches_text_plus_for_one() {
286 let c = comp(r#"
287 Mt = MultiText { Inputs = {
288 Width = Input { Value = 128, }, Height = Input { Value = 64, },
289 StyledText1 = Input { Value = "II", }, Center1 = Input { Value = { 0.25, 0.5 }, }, Size1 = Input { Value = 0.4, },
290 Red1 = Input { Value = 1, }, Green1 = Input { Value = 0, }, Blue1 = Input { Value = 0, },
291 Text2 = Input { Value = "II", }, Center2 = Input { Value = { 0.75, 0.5 }, }, Size2 = Input { Value = 0.4, },
292 Red2 = Input { Value = 0, }, Green2 = Input { Value = 1, }, Blue2 = Input { Value = 0, },
293 }, },
294 One = MultiText { Inputs = {
295 Width = Input { Value = 128, }, Height = Input { Value = 64, },
296 StyledText = Input { Value = "HI", }, Size = Input { Value = 0.25, },
297 }, },
298 Tp = TextPlus { Inputs = {
299 Width = Input { Value = 128, }, Height = Input { Value = 64, },
300 StyledText = Input { Value = "HI", }, Size = Input { Value = 0.25, },
301 }, },
302 Empty = MultiText { Inputs = { Width = Input { Value = 128, }, Height = Input { Value = 64, }, }, },
303 "#);
304 let Some((mt, mt_px)) = render(&c, "Mt", 0.0) else { return };
305 assert_eq!(mt.canvas_size, [128, 64]);
306 let count = |range: std::ops::Range<u32>, pred: &dyn Fn([f32; 4]) -> bool| {
307 range.flat_map(|x| (0..64).map(move |y| (x, y))).filter(|&(x, y)| pred(px(&mt_px, mt.size[0], x, y))).count()
308 };
309 assert!(count(0..64, &|p| p[0] > 0.8 && p[1] < 0.2 && p[3] > 0.8) > 10, "layer 1 renders red text on the left");
310 assert!(count(64..128, &|p| p[1] > 0.8 && p[0] < 0.2 && p[3] > 0.8) > 10, "layer 2 renders green text on the right");
311 assert_eq!(count(0..64, &|p| p[1] > 0.5 && p[3] > 0.5), 0, "layers stay on their own side");
312 let (one, one_px) = render(&c, "One", 0.0).unwrap();
313 let (tp, tp_px) = render(&c, "Tp", 0.0).unwrap();
314 assert_eq!((one.size, one.origin), (tp.size, tp.origin));
315 assert_eq!(one_px, tp_px, "a single layer is exactly TextPlus");
316 let (_, empty_px) = render(&c, "Empty", 0.0).unwrap();
317 assert!(empty_px.iter().all(|&v| v == 0.0), "no text renders transparent");
318}
319
320fn row_ramp() -> String {
321 "Ramp = Background { Inputs = { Width = Input { Value = 16, }, Height = Input { Value = 16, }, Depth = Input { Value = 4, }, \
322 Type = Input { Value = FuID { \"Vertical\" }, }, TopLeftRed = Input { Value = 0, }, TopLeftAlpha = Input { Value = 1, }, \
323 BottomLeftRed = Input { Value = 1, }, BottomLeftAlpha = Input { Value = 1, }, }, },"
324 .to_owned()
325}
326
327#[test]
328fn fields_deinterlace_interlace_swap_and_strip() {
329 let fields = |name: &str, extra: &str| tool(name, "Fields", "Ramp", extra);
330 let c = comp(&[
331 row_ramp(),
332 solid("Second", 16, 16, [0.0, 1.0, 0.0, 1.0]),
333 fields("Dup", "Operation = Input { Value = \"Deinterlace\", }, Method = Input { Value = \"Duplicate\", },"),
334 fields("Interp", "Operation = Input { Value = \"Deinterlace\", }, Method = Input { Value = \"Interpolate\", },"),
335 fields("Blend", "Operation = Input { Value = \"Deinterlace\", }, Method = Input { Value = \"Blend\", },"),
336 fields("Even", "Operation = Input { Value = \"Deinterlace\", }, Method = Input { Value = \"Duplicate\", }, FieldOrder = Input { Value = \"Even\", },"),
337 fields("Swap", "Operation = Input { Value = \"SwapFields\", },"),
338 fields("Strip", "Operation = Input { Value = \"StripFields\", }, Method = Input { Value = \"Blend\", },"),
339 "Weave = Fields { Inputs = { Input = Input { SourceOp = \"Ramp\", Source = \"Output\", }, Input2 = Input { SourceOp = \"Second\", Source = \"Output\", }, \
340 Operation = Input { Value = \"Interlace\", }, }, },".to_owned(),
341 fields("Off", "Operation = Input { Value = \"SwapFields\", }, Blend = Input { Value = 0, },"),
342 ].concat());
343 let Some((_, ramp)) = render(&c, "Ramp", 0.0) else { return };
344 let r = |buf: &[f32], y: u32| px(buf, 16, 2, y)[0];
345 assert!(r(&ramp, 3) > r(&ramp, 2) + 0.01, "the ramp must vary per row for the parity checks to mean anything");
346 let (_, dup) = render(&c, "Dup", 0.0).unwrap();
347 near(r(&dup, 2), r(&ramp, 2), 1e-6, "dominant (odd) lines kept");
348 near(r(&dup, 3), r(&ramp, 2), 1e-6, "Duplicate copies the dominant line above");
349 let (_, interp) = render(&c, "Interp", 0.0).unwrap();
350 near(r(&interp, 3), (r(&ramp, 2) + r(&ramp, 4)) / 2.0, 1e-6, "Interpolate averages neighbours");
351 let (_, blend) = render(&c, "Blend", 0.0).unwrap();
352 near(r(&blend, 3), (r(&ramp, 3) + (r(&ramp, 2) + r(&ramp, 4)) / 2.0) / 2.0, 1e-6, "Blend mixes with the interpolation");
353 let (_, even) = render(&c, "Even", 0.0).unwrap();
354 near(r(&even, 2), r(&ramp, 1), 1e-6, "Even field order keeps odd rows and rebuilds even ones");
355 let (_, swap) = render(&c, "Swap", 0.0).unwrap();
356 near(r(&swap, 2), r(&ramp, 3), 1e-6, "SwapFields exchanges line pairs");
357 near(r(&swap, 3), r(&ramp, 2), 1e-6, "SwapFields exchanges line pairs");
358 let (_, strip) = render(&c, "Strip", 0.0).unwrap();
359 near(r(&strip, 3), r(&ramp, 2), 1e-6, "StripFields drops the second field");
360 let (_, weave) = render(&c, "Weave", 0.0).unwrap();
361 near(r(&weave, 2), r(&ramp, 2), 1e-6, "Interlace keeps the dominant line from Input");
362 assert_eq!(px(&weave, 16, 2, 3), [0.0, 1.0, 0.0, 1.0], "Interlace takes the other lines from Input2");
363 let (_, off) = render(&c, "Off", 0.0).unwrap();
364 assert_eq!(off, ramp);
365}
366
367#[test]
368fn chromatic_aberration_removal_rescales_channels_radially() {
369 let c = comp(&[
370 "Ramp = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 16, }, Depth = Input { Value = 4, }, \
371 Type = Input { Value = FuID { \"Horizontal\" }, }, TopLeftRed = Input { Value = 0, }, TopLeftGreen = Input { Value = 0, }, TopLeftBlue = Input { Value = 0, }, TopLeftAlpha = Input { Value = 1, }, \
372 TopRightRed = Input { Value = 1, }, TopRightGreen = Input { Value = 1, }, TopRightBlue = Input { Value = 1, }, TopRightAlpha = Input { Value = 1, }, }, },".to_owned(),
373 "Edge = Background { Inputs = { Width = Input { Value = 16, }, Height = Input { Value = 16, }, Depth = Input { Value = 4, }, \
374 TopLeftRed = Input { Value = 1, }, TopLeftGreen = Input { Value = 0, }, TopLeftBlue = Input { Value = 0, }, TopLeftAlpha = Input { Value = 1, }, }, },".to_owned(),
375 tool("Def", "ChromaticAberrationRemoval", "Ramp", ""),
376 tool("Red", "ChromaticAberrationRemoval", "Ramp", "RedScale = Input { Value = 1.1, },"),
377 tool("Blue", "ChromaticAberrationRemoval", "Ramp", "BlueYellow = Input { Value = 10, },"),
378 tool("Off", "ChromaticAberrationRemoval", "Ramp", "RedScale = Input { Value = 1.1, }, Blend = Input { Value = 0, },"),
379 tool("Defr", "ChromaticAberrationRemoval", "Edge", "Defringe = Input { Value = 1, },"),
380 ].concat());
381 let Some((_, ramp)) = render(&c, "Ramp", 0.0) else { return };
382 let (_, def) = render(&c, "Def", 0.0).unwrap();
383 assert_eq!(def, ramp, "default is identity");
384 let (_, red) = render(&c, "Red", 0.0).unwrap();
385 let (a, b) = (px(&ramp, 64, 48, 8), px(&red, 64, 48, 8));
386 assert!(b[0] > a[0] + 0.01, "red sampled farther from the centre: {} vs {}", b[0], a[0]);
387 near(b[1], a[1], 1e-5, "green untouched");
388 near(px(&red, 64, 32, 8)[0], px(&ramp, 64, 32, 8)[0], 0.02, "centre pixel stays put");
389 let (_, blue) = render(&c, "Blue", 0.0).unwrap();
390 assert!(px(&blue, 64, 48, 8)[2] > a[2] + 0.01, "BlueYellow shifts blue");
391 let (_, off) = render(&c, "Off", 0.0).unwrap();
392 assert_eq!(off, ramp);
393 let (_, defr) = render(&c, "Defr", 0.0).unwrap();
394 assert_eq!(px(&defr, 16, 8, 8), [1.0, 0.0, 0.0, 1.0], "defringe leaves flat colour alone");
395}
396
397#[test]
398fn key_stretcher_keeps_intro_and_outro_and_stretches_the_middle() {
399 let keys = (0..=10).map(|f| format!("[{f}] = {{ {}, Flags = {{ StepIn = true }} }},", f as f32 / 10.0)).collect::<String>();
400 let stretch = |name: &str, mode: &str| {
401 format!(
402 "{name} = KeyStretcher {{ Inputs = {{ Input = Input {{ SourceOp = \"Clip\", Source = \"Output\", }}, \
403 SourceStart = Input {{ Value = 2, }}, SourceEnd = Input {{ Value = 4, }}, StretchStart = Input {{ Value = 2, }}, \
404 StretchEnd = Input {{ Value = 6, }}, StretchMode = Input {{ Value = \"{mode}\", }}, }}, }},"
405 )
406 };
407 let c = comp(&[
408 "Clip = Background { Inputs = { Width = Input { Value = 4, }, Height = Input { Value = 4, }, Depth = Input { Value = 4, }, \
409 TopLeftRed = Input { SourceOp = \"Ramp\", Source = \"Value\", }, TopLeftAlpha = Input { Value = 1, }, }, },".to_owned(),
410 format!("Ramp = BezierSpline {{ KeyFrames = {{ {keys} }} }},"),
411 stretch("Loop", "Loop"),
412 stretch("Hold", "Hold"),
413 stretch("Ping", "PingPong"),
414 stretch("Scale", "Scale"),
415 "Ident = KeyStretcher { Inputs = { Input = Input { SourceOp = \"Clip\", Source = \"Output\", }, }, },".to_owned(),
416 "Cycle = KeyStretcher { Inputs = { Input = Input { SourceOp = \"Cycle\", Source = \"Output\", }, }, },".to_owned(),
417 "Free = KeyStretcher { },".to_owned(),
418 ].concat());
419 let value = |name: &str, t: f64| render(&c, name, t).map(|(_, p)| px(&p, 4, 1, 1)[0]);
420 let Some(intro) = value("Loop", 1.0) else { return };
421 near(intro, 0.1, 1e-3, "intro plays unstretched");
422 near(value("Loop", 5.0).unwrap(), 0.3, 1e-3, "Loop wraps the middle over the source range");
423 near(value("Loop", 8.0).unwrap(), 0.6, 1e-3, "outro continues from SourceEnd");
424 near(value("Hold", 5.0).unwrap(), 0.4, 1e-3, "Hold clamps at SourceEnd");
425 near(value("Ping", 5.0).unwrap(), 0.3, 1e-3, "PingPong folds back");
426 near(value("Scale", 4.0).unwrap(), 0.3, 1e-3, "Scale maps linearly");
427 for t in [0.0, 3.0, 10.0] {
428 near(value("Ident", t).unwrap(), t as f32 / 10.0, 1e-3, "defaults are the identity");
429 }
430 assert_eq!(render(&c, "Cycle", 3.0).unwrap().0.size, [1, 1], "self-referencing input terminates");
431 assert_eq!(render(&c, "Free", 0.0).unwrap().0.size, [1, 1]);
432}