mod detect;
mod equirect;
pub(crate) use detect::detect;
pub(crate) use equirect::load_equirect;
#[cfg(test)]
pub(crate) mod tests_support {
pub(crate) fn panorama_glb_bytes() -> Vec<u8> {
super::detect::test_fixtures::panorama_glb()
}
pub(crate) fn ordinary_scene_glb_bytes() -> Vec<u8> {
super::detect::test_fixtures::ordinary_scene_glb()
}
}
use crate::import::gltf_source::GltfDoc;
pub fn file_is_panorama_sphere(path: &str) -> bool {
GltfDoc::parse_file(path)
.ok()
.map(|doc| detect(&doc).is_ok())
.unwrap_or(false)
}
pub(crate) fn load_panorama_file(path: &str) -> Result<crate::codec::hdr::HdrImage, String> {
let doc = GltfDoc::parse_file(path)?;
let panorama = detect(&doc).map_err(|e| format!("'{}': {}", path, e))?;
load_equirect(&doc, path, panorama.image_index)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::import::panorama::detect::test_fixtures::{ordinary_scene_glb, panorama_glb};
fn write(dir: &tempfile::TempDir, name: &str, bytes: &[u8]) -> String {
let path = dir.path().join(name);
std::fs::write(&path, bytes).expect("write glb");
path.to_string_lossy().into_owned()
}
#[test]
fn a_panorama_glb_on_disk_is_recognised_and_decodes() {
let dir = tempfile::tempdir().unwrap();
let path = write(&dir, "sky.glb", &panorama_glb());
assert!(file_is_panorama_sphere(&path));
let image = load_panorama_file(&path).expect("decode");
assert_eq!((image.width, image.height), (4, 2));
}
#[test]
fn an_ordinary_scene_glb_on_disk_is_not_a_panorama() {
let dir = tempfile::tempdir().unwrap();
let path = write(&dir, "scene.glb", &ordinary_scene_glb());
assert!(!file_is_panorama_sphere(&path));
let err = load_panorama_file(&path).unwrap_err();
assert!(err.contains("scene.glb"), "got: {err}");
}
#[test]
fn an_unparseable_file_is_not_a_panorama() {
let dir = tempfile::tempdir().unwrap();
let path = write(&dir, "junk.glb", b"not a glb at all");
assert!(!file_is_panorama_sphere(&path));
assert!(load_panorama_file(&path).is_err());
}
}