//! Calibration for [`wai::jpegai_pipeline::lef_filter`] — the LEF adaptive-sharpness //! post-filter. Runs on the reference fixture (input luma, the 16×16 scale grid, the //! three thresholds/magnitudes) and compares against the reference `y_want`. Float //! arithmetic ⇒ machine-precision match expected. //! //! env: WAI_JPEGAI_LEF_FIXTURE = lef_fixture.json //! { y_in:[f64], y_want:[f64], sigma:[f64], sh, sw, thr:[f64;3], mag:[f64;3], h, w } #![cfg(feature = "neural_int")] use wai::jpegai_pipeline::lef_filter; #[test] fn lef_filter_matches_reference() { let path = match std::env::var("WAI_JPEGAI_LEF_FIXTURE") { Ok(p) if std::path::Path::new(&p).exists() => p, _ => { eprintln!("skip: set WAI_JPEGAI_LEF_FIXTURE to lef_fixture.json"); return; } }; let v: serde_json::Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap(); let f32v = |k: &str| v[k].as_array().unwrap().iter().map(|x| x.as_f64().unwrap() as f32).collect::>(); let y_in = f32v("y_in"); let y_want = f32v("y_want"); let sigma = f32v("sigma"); let thr_v = f32v("thr"); let mag_v = f32v("mag"); let g = |k: &str| v[k].as_u64().unwrap() as usize; let (sh, sw, h, w) = (g("sh"), g("sw"), g("h"), g("w")); let got = lef_filter(&y_in, &sigma, sh, sw, [thr_v[0], thr_v[1], thr_v[2]], [mag_v[0], mag_v[1], mag_v[2]], h, w); assert_eq!(got.len(), y_want.len(), "output size mismatch"); let mut maxd = 0f32; let mut mse = 0f64; for i in 0..got.len() { let d = (got[i] - y_want[i]).abs(); maxd = maxd.max(d); mse += (d as f64) * (d as f64); } mse /= got.len() as f64; let sig: f64 = y_want.iter().map(|&x| (x as f64) * (x as f64)).sum::() / got.len() as f64; let snr = if mse > 0.0 { 10.0 * (sig / mse).log10() } else { f64::INFINITY }; eprintln!("lef_filter vs reference: max|Δ| = {maxd:.6} SNR = {snr:.2} dB"); assert!(maxd < 1e-2, "LEF filter diverged from the reference (max|Δ| = {maxd})"); }