//! `file.exportTextures` / `export.plan` with `colorSpace`. use serde_json::{Value, json}; use texturecraft_engine::geom::primitives; use texturecraft_engine::texel::ColorSpace; use texturecraft_engine::{Session, execute}; fn session() -> Session { let mut s = Session::new_project("t", primitives::builtin("plane").unwrap(), "builtin:plane", None, 128).unwrap(); execute(&mut s, "layer.add.fill", &json!({ "color": [0.8, 0.2, 0.1] })).unwrap(); s } fn export(s: &mut Session, tag: &str, extra: Value) -> (Value, std::path::PathBuf) { let dir = std::env::temp_dir().join(format!("tc_export_cs_{tag}_{}", std::process::id())); let _ = std::fs::remove_dir_all(&dir); let mut p = json!({ "dir": dir.display().to_string(), "preset": "glTF PBR Metal Roughness", "format": "exr", "bitDepth": 32, "padding": "infinite" }); for (k, v) in extra.as_object().unwrap() { p[k] = v.clone(); } (execute(s, "file.exportTextures", &p).unwrap(), dir) } fn first_pixel(path: &std::path::Path) -> [f32; 3] { let img = image::open(path).unwrap().to_rgb32f(); // The plane's UV layout covers the whole image; read its middle. let px = img.get_pixel(img.width() / 2, img.height() / 2); [px[0], px[1], px[2]] } #[test] fn colour_maps_are_written_in_the_chosen_space() { let mut s = session(); let (plain, dir_plain) = export(&mut s, "plain", json!({})); let (aces, dir_aces) = export(&mut s, "aces", json!({ "colorSpace": "ACEScg" })); let base = |r: &Value| std::path::PathBuf::from(r["files"].as_array().unwrap().iter().map(|f| f.as_str().unwrap()).find(|f| f.contains("BaseColor")).unwrap()); let (a, b) = (first_pixel(&base(&plain)), first_pixel(&base(&aces))); let want = ColorSpace::AcesCg.from_linear_rec709(a); assert!((0..3).all(|k| (b[k] - want[k]).abs() < 1e-3), "{b:?} vs {want:?}"); assert!((0..3).any(|k| (a[k] - b[k]).abs() > 0.01), "the colour actually changed: {a:?} vs {b:?}"); for d in [dir_plain, dir_aces] { let _ = std::fs::remove_dir_all(d); } } #[test] fn the_plan_reports_the_space_and_clamping_and_bad_names_are_errors() { let mut s = session(); let plan = execute(&mut s, "export.plan", &json!({ "preset": "glTF PBR Metal Roughness", "colorSpace": "ACEScg" })).unwrap(); assert!(plan.as_array().unwrap().iter().any(|f| f["warnings"].as_array().is_some_and(|w| !w.is_empty())), "8-bit ACEScg warns: {plan}"); let float = execute(&mut s, "export.plan", &json!({ "preset": "glTF PBR Metal Roughness", "colorSpace": "ACEScg", "format": "exr" })).unwrap(); assert!(float.as_array().unwrap().iter().all(|f| f["warnings"].as_array().is_some_and(Vec::is_empty)), "{float}"); for bad in ["Display P3", "", "Rec.2020"] { assert!(execute(&mut s, "export.plan", &json!({ "preset": "glTF PBR Metal Roughness", "colorSpace": bad })).is_err(), "{bad:?}"); } }