| ... | ... | @@ -0,0 +1,139 @@ |
| 1 | //! FastNoise parameter transfer curves, checked pixel-for-pixel against a same-lattice baseline. |
| 2 | //! The curves were measured from Fusion 21.1 sweeps where only one input changes. |
| 3 | |
| 4 | use engine::{Engine, ExportOptions, export_saver_png_sequence}; |
| 5 | use model::Comp; |
| 6 | |
| 7 | const SIZE: u32 = 96; |
| 8 | const LINEAR_GRADIENT: &str = "Gradient { Colors = { [0] = { 0, 0, 0, 1 }, [1] = { 1, 1, 1, 1 } } }"; |
| 9 | |
| 10 | fn render(tag: &str, overrides: &[(&str, &str)]) -> Vec<[f32; 4]> { |
| 11 | let mut inputs: Vec<(&str, &str)> = vec![ |
| 12 | ("Detail", "1"), |
| 13 | ("XScale", "4"), |
| 14 | ("Color1Alpha", "1"), |
| 15 | ]; |
| 16 | for (key, value) in overrides { |
| 17 | inputs.retain(|(k, _)| k != key); |
| 18 | inputs.push((key, value)); |
| 19 | } |
| 20 | let inputs: String = inputs.iter().map(|(k, v)| format!("\t\t\t\t{k} = Input {{ Value = {v}, }},\n")).collect(); |
| 21 | |
| 22 | let dir = std::env::temp_dir().join(format!("fission-fast-noise-{}-{tag}", std::process::id())); |
| 23 | let _ = std::fs::remove_dir_all(&dir); |
| 24 | let path = dir.join("out_0000.png"); |
| 25 | let path = path.to_string_lossy().replace('\\', "\\\\"); |
| 26 | let src = format!( |
| 27 | r#"Composition {{ |
| 28 | 	RenderRange = {{ 0, 0 }}, |
| 29 | 	Tools = ordered() {{ |
| 30 | 		Noise = FastNoise {{ |
| 31 | 			Inputs = {{ |
| 32 | 				Width = Input {{ Value = {SIZE}, }}, |
| 33 | 				Height = Input {{ Value = {SIZE}, }}, |
| 34 | {inputs}			}}, |
| 35 | 		}}, |
| 36 | 		Black = Background {{ |
| 37 | 			Inputs = {{ |
| 38 | 				Width = Input {{ Value = {SIZE}, }}, |
| 39 | 				Height = Input {{ Value = {SIZE}, }}, |
| 40 | 				TopLeftAlpha = Input {{ Value = 1, }}, |
| 41 | 			}}, |
| 42 | 		}}, |
| 43 | 		Over = Merge {{ |
| 44 | 			Inputs = {{ |
| 45 | 				Background = Input {{ SourceOp = "Black", Source = "Output", }}, |
| 46 | 				Foreground = Input {{ SourceOp = "Noise", Source = "Output", }}, |
| 47 | 			}}, |
| 48 | 		}}, |
| 49 | 		Saver1 = Saver {{ |
| 50 | 			Inputs = {{ |
| 51 | 				Clip = Input {{ Value = Clip {{ Filename = "{path}", FormatID = "PNGFormat", Saving = true, }}, }}, |
| 52 | 				Input = Input {{ SourceOp = "Over", Source = "Output", }}, |
| 53 | 			}}, |
| 54 | 		}}, |
| 55 | 	}}, |
| 56 | 	Prefs = {{ Comp = {{ FrameFormat = {{ Width = {SIZE}, Height = {SIZE}, Rate = 24, }}, }}, }}, |
| 57 | }}"# |
| 58 | ); |
| 59 | let comp = Comp::from_fusion_str(&src).expect("parse FastNoise comp"); |
| 60 | let mut engine = Engine::new_headless().expect("gpu"); |
| 61 | export_saver_png_sequence(&mut engine, &comp, &ExportOptions::default(), |_| {}).expect("export"); |
| 62 | let img = image::open(dir.join("out_0000.png")).expect("decode").to_rgba8(); |
| 63 | let _ = std::fs::remove_dir_all(&dir); |
| 64 | img.pixels().map(|p| p.0.map(|c| c as f32 / 255.0)).collect() |
| 65 | } |
| 66 | |
| 67 | /// Asserts `out == expected(base)` per pixel on pixels where `valid(base)` holds, and that enough pixels qualify. |
| 68 | fn assert_transfer(base: &[[f32; 4]], out: &[[f32; 4]], valid: impl Fn(f32) -> bool, expected: impl Fn(f32) -> f32) { |
| 69 | let mut checked = 0; |
| 70 | for (b, o) in base.iter().zip(out) { |
| 71 | if !valid(b[0]) { |
| 72 | continue; |
| 73 | } |
| 74 | checked += 1; |
| 75 | let want = expected(b[0]); |
| 76 | assert!((o[0] - want).abs() <= 4.0 / 255.0, "base {:.3}: got {:.3}, want {want:.3}", b[0], o[0]); |
| 77 | } |
| 78 | assert!(checked > base.len() / 4, "only {checked} of {} pixels exercised", base.len()); |
| 79 | } |
| 80 | |
| 81 | fn std_dev(px: &[[f32; 4]]) -> f32 { |
| 82 | let mean = px.iter().map(|p| p[0]).sum::<f32>() / px.len() as f32; |
| 83 | (px.iter().map(|p| (p[0] - mean).powi(2)).sum::<f32>() / px.len() as f32).sqrt() |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn brightness_adds_half_its_value() { |
| 88 | let base = render("b0", &[]); |
| 89 | let bright = render("b1", &[("Brightness", "0.2")]); |
| 90 | assert_transfer(&base, &bright, |a| a < 0.85, |a| a + 0.1); |
| 91 | } |
| 92 | |
| 93 | #[test] |
| 94 | fn contrast_pivots_on_half() { |
| 95 | let base = render("c0", &[]); |
| 96 | let contrast = render("c1", &[("Contrast", "2")]); |
| 97 | assert_transfer(&base, &contrast, |a| (0.3..0.7).contains(&a), |a| (a - 0.5) * 2.0 + 0.5); |
| 98 | } |
| 99 | |
| 100 | #[test] |
| 101 | fn discontinuous_folds_noise_and_scales_contrast_from_zero() { |
| 102 | let base = render("d0", &[]); |
| 103 | let disc = render("d1", &[("Discontinuous", "1")]); |
| 104 | assert_transfer(&base, &disc, |_| true, |a| 2.0 * (a - 0.5).abs()); |
| 105 | let disc_contrast = render("d2", &[("Discontinuous", "1"), ("Contrast", "0.5")]); |
| 106 | assert_transfer(&base, &disc_contrast, |_| true, |a| (a - 0.5).abs()); |
| 107 | } |
| 108 | |
| 109 | #[test] |
| 110 | fn inverted_runs_before_brightness() { |
| 111 | let base = render("i0", &[]); |
| 112 | let inverted = render("i1", &[("Inverted", "1"), ("Brightness", "0.2")]); |
| 113 | assert_transfer(&base, &inverted, |a| a > 0.1, |a| 1.0 - a + 0.1); |
| 114 | } |
| 115 | |
| 116 | #[test] |
| 117 | fn gradient_with_discontinuous_shifts_lookup_by_quarter() { |
| 118 | let base = render("g0", &[]); |
| 119 | let plain = render("g1", &[("Type", "1"), ("Gradient", LINEAR_GRADIENT)]); |
| 120 | assert_transfer(&base, &plain, |_| true, |a| a); |
| 121 | let disc = render("g2", &[("Type", "1"), ("Gradient", LINEAR_GRADIENT), ("Discontinuous", "1")]); |
| 122 | assert_transfer(&base, &disc, |_| true, |a| (2.0 * (a - 0.5).abs() + 0.25).min(1.0)); |
| 123 | } |
| 124 | |
| 125 | #[test] |
| 126 | fn detail_octaves_sum_unnormalized() { |
| 127 | let one = std_dev(&render("o1", &[("XScale", "8")])); |
| 128 | let three = std_dev(&render("o3", &[("XScale", "8"), ("Detail", "3")])); |
| 129 | // sqrt(1 + 1/4 + 1/16) = 1.146 for independent octaves at amplitudes 1, 1/2, 1/4 |
| 130 | assert!((1.05..1.25).contains(&(three / one)), "std ratio {}", three / one); |
| 131 | } |
| 132 | |
| 133 | #[test] |
| 134 | fn colors_are_not_premultiplied_by_alpha() { |
| 135 | let px = render("a0", &[("Color1Alpha", "0.5"), ("Color1Red", "1"), ("Color2Alpha", "0.5")]); |
| 136 | // red is 1 at both ends of the ramp, so a premultiplied output would land near 0.5 |
| 137 | assert!(px.iter().all(|p| p[0] > 0.99), "min red {}", px.iter().map(|p| p[0]).fold(1.0, f32::min)); |
| 138 | assert!(std_dev(&px.iter().map(|p| [p[1], 0.0, 0.0, 0.0]).collect::<Vec<_>>()) > 0.05); |
| 139 | } |