1use super::*;
2use crate::Engine;
3
4fn eval_px(comp: &Comp, eng: &mut Engine, name: &str, t: f64) -> (Vec<u8>, crate::cache::EvalStats) {
5 let (tex, stats) = eng.evaluate(comp, comp.find_node(name).unwrap(), t);
6 (eng.readback_rgba(&tex).unwrap(), stats)
7}
8
9#[test]
10fn test_upstream_infinite_edge_mode_propagates_through_point_ops_and_disabled_dirblur() {
11 let src = r#"Composition {
12 CurrentTime = 3050,
13 RenderRange = { 3000, 3100 },
14 GlobalRange = { 3000, 3100 },
15 Tools = ordered() {
16 BG1 = Background {
17 Inputs = {
18 Width = Input { Value = 64, },
19 Height = Input { Value = 64, },
20 TopLeftRed = Input { Value = 0.2, },
21 TopLeftAlpha = Input { Value = 1.0, },
22 },
23 },
24 FG1 = Background {
25 Inputs = {
26 Width = Input { Value = 64, },
27 Height = Input { Value = 64, },
28 Type = Input { Value = FuID { "Horizontal" }, },
29 TopLeftRed = Input { Value = 1.0, },
30 TopLeftGreen = Input { Value = 0.0, },
31 TopLeftBlue = Input { Value = 0.0, },
32 TopRightRed = Input { Value = 0.0, },
33 TopRightGreen = Input { Value = 0.0, },
34 TopRightBlue = Input { Value = 1.0, },
35 },
36 },
37 Merge23 = Merge {
38 Inputs = {
39 Background = Input { SourceOp = "BG1", Source = "Output", },
40 Foreground = Input { SourceOp = "FG1", Source = "Output", },
41 Edges = Input { Value = 3, },
42 },
43 },
44 CC1 = ColorCorrector {
45 Inputs = {
46 Input = Input { SourceOp = "Merge23", Source = "Output", },
47 },
48 },
49 BG2 = Background {
50 Inputs = {
51 Width = Input { Value = 64, },
52 Height = Input { Value = 64, },
53 TopLeftAlpha = Input { Value = 1.0, },
54 },
55 },
56 CC_BG = ColorCorrector {
57 Inputs = {
58 Input = Input { SourceOp = "BG2", Source = "Output", },
59 },
60 },
61 Merge24 = Merge {
62 Inputs = {
63 Background = Input { SourceOp = "CC_BG", Source = "Output", },
64 Foreground = Input { SourceOp = "CC1", Source = "Output", },
65 },
66 },
67 Infrared = ColorCurves {
68 Inputs = {
69 Input = Input { SourceOp = "Merge24", Source = "Output", },
70 },
71 },
72 CC2 = ColorCorrector {
73 Inputs = {
74 Input = Input { SourceOp = "Infrared", Source = "Output", },
75 },
76 },
77 DirBlur = DirectionalBlur {
78 Inputs = {
79 Blend = Input { Value = 0.0, },
80 Length = Input { Value = 0.05, },
81 Input = Input { SourceOp = "CC2", Source = "Output", },
82 },
83 },
84 DirBlurActive = DirectionalBlur {
85 Inputs = {
86 Blend = Input { Value = 1.0, },
87 Length = Input { Value = 0.05, },
88 Input = Input { SourceOp = "CC2", Source = "Output", },
89 },
90 },
91 XF = Transform {
92 Inputs = {
93 Center = Input { Value = { 0.75, 0.5 }, },
94 Edges = Input { Value = 2, },
95 Input = Input { SourceOp = "DirBlur", Source = "Output", },
96 },
97 },
98 },
99 }"#;
100 let comp = Comp::from_fusion_str(src).expect("parse mirror propagation comp");
101 let expr = ExprEngine::new();
102 let dir_blur_id = comp.find_node("DirBlur").expect("DirBlur");
103 let dir_blur_active_id = comp.find_node("DirBlurActive").expect("DirBlurActive");
104 assert_eq!(
105 upstream_infinite_edge_mode(&comp, dir_blur_id, 3050.0, &expr),
106 Some(3.0),
107 "Merge(Edges=3) should propagate through point ops, Merge(BG=CC(Background)), and DirectionalBlur(Blend=0)"
108 );
109 assert_eq!(
110 upstream_infinite_edge_mode(&comp, dir_blur_active_id, 3050.0, &expr),
111 None,
112 "Active DirectionalBlur (Blend=1) should stop RoI edge-mode propagation"
113 );
114
115 let Ok(mut eng) = Engine::new_headless() else {
116 return;
117 };
118 let xf_id = comp.find_node("XF").expect("XF");
119 let (tex, _) = eng.evaluate(&comp, xf_id, 3050.0);
120 let rgba = eng.readback_rgba(&tex).expect("readback XF");
121 // Center.x = 0.75 shifts image right by 16px: x=0..16 samples mirrored x=16..0
122 // rather than duplicating x=0! On a red->blue gradient, x=2 should be noticeably
123 // bluer than x=15 (which sits right next to the red left edge).
124 let px = |x: usize| -> [u8; 4] {
125 let i = (32 * 64 + x) * 4;
126 [rgba[i], rgba[i + 1], rgba[i + 2], rgba[i + 3]]
127 };
128 let far_left = px(2);
129 let seam = px(15);
130 assert!(
131 far_left[2] > seam[2] + 20,
132 "expected mirrored left edge (far_left={far_left:?} bluer than seam={seam:?}), not Duplicate streak"
133 );
134}
135
136#[test]
137fn test_merge_mirror_edges_phase_with_cropped_fg_and_offset() {
138 // Reproduces crazy_3050's Merge23(Edges=3, Center={0.125,0.5}, Size=0.75, cropped FG) ->
139 // CC1 -> Merge24(Edges=0) -> ColorCurves -> CC2 -> DirBlur(Blend=0) -> Transform(Edges=2, Center={0.6875,0.5}).
140 // Shifting Transform right by 24px (0.1875 * 128) pulls u < 0 from upstream:
141 // FG is a 32x128 Red->Blue horizontal gradient centered at x=16 with Size=0.75 (24px wide -> x=[4..28]).
142 // Mirroring FG across its own left edge x=4 means canvas x=-4 (output x=20) samples near Red (x=12),
143 // while output x=8 (canvas x=-16) reflects past x=4..28 to Blue, NOT BG1 green or canvas x=0 mirror!
144 let src = r#"Composition {
145 CurrentTime = 3050,
146 RenderRange = { 3000, 3100 },
147 GlobalRange = { 3000, 3100 },
148 Tools = ordered() {
149 BG1 = Background {
150 Inputs = {
151 Width = Input { Value = 128, },
152 Height = Input { Value = 128, },
153 TopLeftRed = Input { Value = 0.0, },
154 TopLeftGreen = Input { Value = 1.0, },
155 TopLeftBlue = Input { Value = 0.0, },
156 TopLeftAlpha = Input { Value = 1.0, },
157 },
158 },
159 FG1 = Background {
160 Inputs = {
161 Width = Input { Value = 32, },
162 Height = Input { Value = 128, },
163 Type = Input { Value = FuID { "Horizontal" }, },
164 TopLeftRed = Input { Value = 1.0, },
165 TopLeftGreen = Input { Value = 0.0, },
166 TopLeftBlue = Input { Value = 0.0, },
167 TopRightRed = Input { Value = 0.0, },
168 TopRightGreen = Input { Value = 0.0, },
169 TopRightBlue = Input { Value = 1.0, },
170 TopLeftAlpha = Input { Value = 1.0, },
171 },
172 },
173 Merge23 = Merge {
174 Inputs = {
175 Background = Input { SourceOp = "BG1", Source = "Output", },
176 Foreground = Input { SourceOp = "FG1", Source = "Output", },
177 Center = Input { Value = { 0.125, 0.5 }, },
178 Size = Input { Value = 0.75, },
179 Edges = Input { Value = 3, },
180 },
181 },
182 CC1 = ColorCorrector {
183 Inputs = {
184 Input = Input { SourceOp = "Merge23", Source = "Output", },
185 },
186 },
187 BG2 = Background {
188 Inputs = {
189 Width = Input { Value = 128, },
190 Height = Input { Value = 128, },
191 TopLeftAlpha = Input { Value = 1.0, },
192 },
193 },
194 Merge24 = Merge {
195 Inputs = {
196 Background = Input { SourceOp = "BG2", Source = "Output", },
197 Foreground = Input { SourceOp = "CC1", Source = "Output", },
198 },
199 },
200 DirBlur = DirectionalBlur {
201 Inputs = {
202 Blend = Input { Value = 0.0, },
203 Length = Input { Value = 0.05, },
204 Input = Input { SourceOp = "Merge24", Source = "Output", },
205 },
206 },
207 XF = Transform {
208 Inputs = {
209 Center = Input { Value = { 0.6875, 0.5 }, },
210 Edges = Input { Value = 2, },
211 Input = Input { SourceOp = "DirBlur", Source = "Output", },
212 },
213 },
214 },
215 }"#;
216 let comp = Comp::from_fusion_str(src).expect("parse offset Merge(Edges=3) comp");
217 let Ok(mut eng) = Engine::new_headless() else {
218 return;
219 };
220 let xf_id = comp.find_node("XF").expect("XF");
221 let (tex, _) = eng.evaluate(&comp, xf_id, 3050.0);
222 let rgba = eng.readback_rgba(&tex).expect("readback XF");
223 let px = |x: usize| -> [u8; 4] {
224 let i = (64 * 128 + x) * 4;
225 [rgba[i], rgba[i + 1], rgba[i + 2], rgba[i + 3]]
226 };
227 // Output x=20 corresponds to canvas x=-4 (mirrored around FG left edge x=4 -> canvas x=12, red-dominant).
228 // Output x=4 corresponds to canvas x=-20 (mirrors onto far side of FG -> blue-dominant).
229 // Crucially, green from BG1 must NOT appear at x < 24 (canvas x < 0)!
230 let out_near_left_fg = px(20);
231 let out_far_mirrored = px(4);
232 assert!(
233 out_near_left_fg[1] < 20 && out_far_mirrored[1] < 20,
234 "BG1 green must be transparent outside [0,W] (got near={out_near_left_fg:?}, far={out_far_mirrored:?})"
235 );
236 assert!(
237 out_far_mirrored[2] > out_near_left_fg[2] + 60,
238 "cropped FG Mirror phase should reflect across FG bounds x=4 (far={out_far_mirrored:?} bluer than near={out_near_left_fg:?})"
239 );
240}
241
242#[test]
243fn transform_multiply_by_mask_zeroes_outside_effect_mask() {
244 let src = |mbm: u8| {
245 format!(
246 r#"Composition {{ CurrentTime = 0, RenderRange = {{ 0, 0 }}, GlobalRange = {{ 0, 0 }}, Tools = ordered() {{
247 BG = Background {{ Inputs = {{ Width = Input {{ Value = 64, }}, Height = Input {{ Value = 64, }},
248 TopLeftRed = Input {{ Value = 1, }}, TopLeftAlpha = Input {{ Value = 1, }}, }}, }},
249 Rect = RectangleMask {{ Inputs = {{ Width = Input {{ Value = 0.25, }}, Height = Input {{ Value = 0.25, }},
250 MaskWidth = Input {{ Value = 64, }}, MaskHeight = Input {{ Value = 64, }}, }}, }},
251 XF = Transform {{ Inputs = {{ MultiplyByMask = Input {{ Value = {mbm}, }},
252 Input = Input {{ SourceOp = "BG", Source = "Output", }},
253 EffectMask = Input {{ SourceOp = "Rect", Source = "Mask", }}, }}, }},
254 }} }}"#
255 )
256 };
257 if Engine::new_headless().is_err() {
258 return;
259 }
260 let px = |mbm: u8, x: usize, y: usize| {
261 let mut eng = Engine::new_headless().unwrap();
262 let comp = Comp::from_fusion_str(&src(mbm)).unwrap();
263 let (tex, _) = eng.evaluate(&comp, comp.find_node("XF").unwrap(), 0.0);
264 let rgba = eng.readback_rgba(&tex).unwrap();
265 let i = (y * 64 + x) * 4;
266 [rgba[i], rgba[i + 3]]
267 };
268 assert_eq!(px(0, 2, 2), [255, 255], "effect mask only blends; identity transform is unchanged");
269 assert_eq!(px(1, 2, 2), [0, 0], "outside the mask is zeroed");
270 assert_eq!(px(1, 32, 32), [255, 255], "inside the mask is kept");
271}
272
273#[test]
274fn transform_invert_transform_shifts_the_opposite_way() {
275 let src = |invert: u8| {
276 format!(
277 r#"Composition {{ CurrentTime = 0, RenderRange = {{ 0, 0 }}, GlobalRange = {{ 0, 0 }}, Tools = ordered() {{
278 BG = Background {{ Inputs = {{ Width = Input {{ Value = 64, }}, Height = Input {{ Value = 64, }},
279 Type = Input {{ Value = FuID {{ "Horizontal" }}, }},
280 TopLeftRed = Input {{ Value = 1, }}, TopLeftGreen = Input {{ Value = 0, }}, TopLeftBlue = Input {{ Value = 0, }}, TopLeftAlpha = Input {{ Value = 1, }},
281 TopRightRed = Input {{ Value = 0, }}, TopRightGreen = Input {{ Value = 1, }}, TopRightBlue = Input {{ Value = 0, }}, TopRightAlpha = Input {{ Value = 1, }}, }}, }},
282 XF = Transform {{ Inputs = {{ InvertTransform = Input {{ Value = {invert}, }},
283 Center = Input {{ Value = {{ 0.75, 0.5 }}, }}, Input = Input {{ SourceOp = "BG", Source = "Output", }}, }}, }},
284 }} }}"#
285 )
286 };
287 if Engine::new_headless().is_err() {
288 return;
289 }
290 let mid_row = |invert: u8| {
291 let mut eng = Engine::new_headless().unwrap();
292 let comp = Comp::from_fusion_str(&src(invert)).unwrap();
293 let (tex, _) = eng.evaluate(&comp, comp.find_node("XF").unwrap(), 0.0);
294 let rgba = eng.readback_rgba(&tex).unwrap();
295 let i = (32 * 64 + 32) * 4;
296 [rgba[i], rgba[i + 1]]
297 };
298 let [r, g] = mid_row(0);
299 assert!(r > g + 60, "Center +0.25 moves the left (red) side under the middle: {r} {g}");
300 let [r, g] = mid_row(1);
301 assert!(g > r + 60, "InvertTransform pulls the right (green) side under the middle: {r} {g}");
302}
303
304#[test]
305fn transform_dod_preserved_through_bc_into_merge() {
306 // 64x64 bright red FG is translated left off-canvas by Transform (Center.x = -0.25 -> shift = -0.75 * 64 = -48px),
307 // passed through BrightnessContrast (unmasked), then merged back onto a 128x64 canvas at Center.x = 0.75.
308 // Without Transform DoD + unmasked BC DoD preservation, x in [-48..0] would be clipped at x=0!
309 let src = r#"Composition {
310 CurrentTime = 0,
311 RenderRange = { 0, 0 },
312 GlobalRange = { 0, 0 },
313 Tools = ordered() {
314 BG = Background {
315 Inputs = {
316 Width = Input { Value = 128, },
317 Height = Input { Value = 64, },
318 TopLeftAlpha = Input { Value = 1.0, },
319 },
320 },
321 FG = Background {
322 Inputs = {
323 Width = Input { Value = 64, },
324 Height = Input { Value = 64, },
325 TopLeftRed = Input { Value = 1.0, },
326 TopLeftGreen = Input { Value = 0.0, },
327 TopLeftBlue = Input { Value = 0.0, },
328 TopLeftAlpha = Input { Value = 1.0, },
329 },
330 },
331 XF = Transform {
332 Inputs = {
333 Input = Input { SourceOp = "FG", Source = "Output", },
334 Center = Input { Value = { -0.25, 0.5 }, },
335 },
336 },
337 BC = BrightnessContrast {
338 Inputs = {
339 Input = Input { SourceOp = "XF", Source = "Output", },
340 Gain = Input { Value = 0.8, },
341 Lift = Input { Value = -0.2, },
342 Gamma = Input { Value = 1.5, },
343 },
344 },
345 M = Merge {
346 Inputs = {
347 Background = Input { SourceOp = "BG", Source = "Output", },
348 Foreground = Input { SourceOp = "BC", Source = "Output", },
349 Center = Input { Value = { 0.75, 0.5 }, },
350 },
351 },
352 },
353 }"#;
354 let comp = Comp::from_fusion_str(src).expect("parse Transform DoD comp");
355 let Ok(mut eng) = Engine::new_headless() else {
356 return;
357 };
358 let (tex, _) = eng.evaluate(&comp, comp.find_node("M").unwrap(), 0.0);
359 let rgba = eng.readback_rgba(&tex).unwrap();
360 // Merge places BC's 64x64 logical center (32) at 0.75 * 128 = 96, so x=0 of BC lands at 64.
361 // The shifted red square spans x in [-48..16) in BC, which lands at [16..80) on BG!
362 // At x=32 (left of BC's x=0 at 64), red pixels must survive!
363 let i = (32 * 128 + 32) * 4;
364 assert!(rgba[i] > 150 && rgba[i + 1] == 0, "expected off-canvas shifted FG via DoD at x=32, got {:?}", &rgba[i..i + 4]);
365}
366
367#[test]
368fn effect_mask_channel_remap_is_applied_and_cached() {
369 let src = r#"Composition { Tools = ordered() {
370 BG = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, TopLeftRed = Input { Value = 1, }, TopLeftAlpha = Input { Value = 1, }, }, },
371 M = RectangleMask { Inputs = { MaskWidth = Input { Value = 64, }, MaskHeight = Input { Value = 64, }, Width = Input { Value = 1, }, Height = Input { Value = 1, }, }, },
372 Plain = Transform { Inputs = { Input = Input { SourceOp = "BG", Source = "Output", }, EffectMask = Input { SourceOp = "M", Source = "Mask", }, MultiplyByMask = Input { Value = 1, }, }, },
373 Half = Transform { Inputs = { Input = Input { SourceOp = "BG", Source = "Output", }, EffectMask = Input { SourceOp = "M", Source = "Mask", }, MultiplyByMask = Input { Value = 1, }, MaskHigh = Input { Value = 2, }, }, },
374 }, }"#;
375 let comp = Comp::from_fusion_str(src).expect("parse comp");
376 let Ok(mut eng) = Engine::new_headless() else { return };
377 let center = (32 * 64 + 32) * 4 + 3;
378 assert_eq!(eval_px(&comp, &mut eng, "Plain", 0.0).0[center], 255);
379 let (half, _) = eval_px(&comp, &mut eng, "Half", 0.0);
380 assert!((half[center] as i32 - 128).abs() <= 2, "MaskHigh=2 halves mask coverage: {}", half[center]);
381 let (_, stats) = eval_px(&comp, &mut eng, "Half", 1.0);
382 assert_eq!(stats.nodes_evaluated, 0, "remapped mask and Transform come from the cache on a static frame");
383}
384
385#[test]
386fn transform_and_corner_positioner_process_skip_and_bilinear_offsets() {
387 let src = r#"Composition { Tools = ordered() {
388 BG = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, },
389 TopLeftRed = Input { Value = 0.8, }, TopLeftGreen = Input { Value = 0.6, }, TopLeftBlue = Input { Value = 0.4, }, TopLeftAlpha = Input { Value = 1.0, }, }, },
390 Mask = RectangleMask { Inputs = { MaskWidth = Input { Value = 64, }, MaskHeight = Input { Value = 64, },
391 Center = Input { Value = { 0.5, 0.5 }, }, Width = Input { Value = 1.0, }, Height = Input { Value = 1.0, }, }, },
392 XF = Transform { Inputs = {
393 Input = Input { SourceOp = "BG", Source = "Output", },
394 EffectMask = Input { SourceOp = "Mask", Source = "Mask", },
395 Center = Input { Value = { 1.1, 0.5 }, },
396 MultiplyByMask = Input { Value = 1, },
397 ProcessGreen = Input { Value = 0, },
398 }, },
399 CP = CornerPositioner { Inputs = {
400 Input = Input { SourceOp = "BG", Source = "Output", },
401 EffectMask = Input { SourceOp = "Mask", Source = "Mask", },
402 MappingType = Input { Value = 0, },
403 ["TopRight.X"] = Input { Value = 0.8, },
404 MultiplyByMask = Input { Value = 1, },
405 ProcessRed = Input { Value = 0, },
406 }, },
407 } }"#;
408 let comp = Comp::from_fusion_str(src).expect("parse xf/cp comp");
409 let Ok(mut eng) = Engine::new_headless() else { return };
410 let px_xf = eval_px(&comp, &mut eng, "XF", 0.0).0;
411 let c = (32 * 64 + 32) * 4;
412 // Center shifted to 0.95 shifts translated image off-center (R/B become 0), while ProcessGreen=0 keeps source green (~153).
413 assert_eq!((px_xf[c], px_xf[c + 1]), (0, 153), "Transform ProcessGreen=0 keeps source green: {:?}", &px_xf[c..c + 4]);
414 let px_cp = eval_px(&comp, &mut eng, "CP", 0.0).0;
415 assert_eq!(px_cp[c], 0, "CornerPositioner MultiplyByMask=1 & ProcessRed=0 clears red to 0: {:?}", &px_cp[c..c + 4]);
416 assert!(px_cp[c + 1] > 100 && px_cp[c + 3] == 255, "CornerPositioner MappingType=0 keeps image in quad interior: {:?}", &px_cp[c..c + 4]);
417}
418
419#[test]
420fn crop_odd_offset_truncation_and_transform_ewa_minification() {
421 let src = r#"Composition { Tools = ordered() {
422 BG = Background { Inputs = { Width = Input { Value = 32, }, Height = Input { Value = 32, },
423 TopLeftRed = Input { Value = 0, }, TopLeftAlpha = Input { Value = 1, },
424 TopRightRed = Input { Value = 1, }, TopRightAlpha = Input { Value = 1, },
425 Type = Input { Value = FuID { "Horizontal" }, }, }, },
426 CR = Crop { Inputs = { Input = Input { SourceOp = "BG", Source = "Output", },
427 XOffset = Input { Value = -3, }, YOffset = Input { Value = 0, },
428 XSize = Input { Value = 32, }, YSize = Input { Value = 32, },
429 ClippingMode = Input { Value = FuID { "None" }, }, }, },
430 XF = Transform { Inputs = { Input = Input { SourceOp = "BG", Source = "Output", },
431 InvertTransform = Input { Value = 1, }, XSize = Input { Value = 2.0, }, YSize = Input { Value = 4.0, },
432 UseSizeAndAspect = Input { Value = 0, }, Angle = Input { Value = 35.0, }, }, },
433 } }"#;
434 let comp = Comp::from_fusion_str(src).expect("parse crop+ewa comp");
435 let Ok(mut eng) = Engine::new_headless() else { return };
436 let cr_px = eval_px(&comp, &mut eng, "CR", 0.0).0;
437 // XOffset = -3 shifts BG right by 3px, so x=0..2 are transparent and x=3 is 0..; centered odd coords truncate.
438 assert_eq!(cr_px[(16 * 32 + 2) * 4 + 3], 0);
439 assert_eq!(cr_px[(16 * 32 + 3) * 4 + 3], 255);
440
441 let xf_px = eval_px(&comp, &mut eng, "XF", 0.0).0;
442 // Corner pixel near minifying border should receive smooth EWA alpha rather than hard 0/255 aliasing.
443 let mut partial = 0usize;
444 for chunk in xf_px.chunks_exact(4) {
445 if chunk[3] > 0 && chunk[3] < 255 {
446 partial += 1;
447 }
448 }
449 assert!(partial >= 8, "EWA minification should produce smooth anti-aliased edge pixels (got {partial})");
450}