diff --git a/crates/engine/src/display_cmds.rs b/crates/engine/src/display_cmds.rs index 486a94b..f8dd1e7 100644 --- a/crates/engine/src/display_cmds.rs +++ b/crates/engine/src/display_cmds.rs @@ -74,7 +74,7 @@ fn export_geometry(session: &mut Session, parameters: &J) -> Result { .ok_or_else(|| bad(COMMAND, "`path` is required"))?; DisplaySnapshot::valid_geometry(COMMAND, &mesh)?; - let text = materialcraft_formats::mesh::obj_text(&mesh); + let text = materialcraft_formats::mesh::obj_text(&mesh)?; materialcraft_formats::write_atomic(&path, text.as_bytes())?; Ok(json!({"path": path.display().to_string(), "triangles": mesh.triangle_count()})) } diff --git a/crates/engine/tests/it/display_commands.rs b/crates/engine/tests/it/display_commands.rs index e4d19be..4093217 100644 --- a/crates/engine/tests/it/display_commands.rs +++ b/crates/engine/tests/it/display_commands.rs @@ -55,7 +55,7 @@ fn geometry_command_writes_only_the_captured_mesh() { session.custom_mesh = Some(("newer".into(), Arc::new(expected))); execute(&mut session, "view.3d.exportGeometry", &json!({"path": saved})).unwrap(); - assert_eq!(std::fs::read_to_string(&saved).unwrap(), materialcraft_engine::formats::mesh::obj_text(&mesh)); + assert_eq!(std::fs::read_to_string(&saved).unwrap(), materialcraft_engine::formats::mesh::obj_text(&mesh).unwrap()); assert!(session.finish_display_handoff().is_none()); assert_eq!(Arc::strong_count(&mesh), 1, "the command retained a mesh after writing it"); std::fs::remove_file(saved).unwrap(); diff --git a/crates/formats/src/image.rs b/crates/formats/src/image.rs index c91f1e9..6d417dc 100644 --- a/crates/formats/src/image.rs +++ b/crates/formats/src/image.rs @@ -145,4 +145,21 @@ mod transfer_tests { } } } + + #[test] + fn raster_encode_preserves_defaults_and_rejects_invalid_inputs() { + let color = Image::filled(2, 2, Kind::Color, [0.5, 0.25, 0.75, 1.0]); + let png = encode(&color, "png", Depth::U8, true).unwrap(); + for chunk in [b"iCCP", b"eXIf", b"tEXt", b"iTXt", b"zTXt"] { + assert!(!png.windows(4).any(|w| w == chunk), "unexpected PNG metadata chunk {chunk:?}"); + } + + let jpeg = encode(&color, "jpg", Depth::U8, true).unwrap(); + assert!(!jpeg.windows(4).any(|w| w == b"Exif" || w == b"ICC_")); + let decoded = decode(&jpeg, false).unwrap(); + assert_eq!((decoded.width, decoded.height), (2, 2)); + + assert!(encode(&color, "psd", Depth::U8, true).is_err()); + assert!(decode(b"not an image", false).is_err()); + } } diff --git a/crates/formats/src/mesh.rs b/crates/formats/src/mesh.rs index 740e5d1..f5efab7 100644 --- a/crates/formats/src/mesh.rs +++ b/crates/formats/src/mesh.rs @@ -62,7 +62,7 @@ fn finish(positions: Vec, normals: Option>, uvs: Vec, indi } /// `mesh` as Wavefront OBJ text: positions, texture coordinates, normals and triangles -pub fn obj_text(mesh: &Mesh) -> String { +pub fn obj_text(mesh: &Mesh) -> Result { let (textured, shaded) = (mesh.uvs.len() == mesh.positions.len(), mesh.normals.len() == mesh.positions.len()); let corners = mesh .indices @@ -84,7 +84,7 @@ pub fn obj_text(mesh: &Mesh) -> String { ..craft_mesh::Mesh::default() }; let scene = craft_mesh::Scene::from_meshes(vec![shared], Vec::new()); - craft_mesh::obj::write(&scene, None, &limits()).unwrap_or_default() + craft_mesh::obj::write(&scene, None, &limits()).map_err(|e| read_error("OBJ", e)) } /// Parse a mesh file by extension. @@ -257,7 +257,7 @@ mod tests { fn a_mesh_written_as_obj_loads_back_as_it_was() { let mesh = materialcraft_geom::primitives::torus(1.0, 0.35, 12, 6); - let loaded = load_mesh("torus.obj", obj_text(&mesh).as_bytes()).unwrap(); + let loaded = load_mesh("torus.obj", obj_text(&mesh).unwrap().as_bytes()).unwrap(); assert_eq!((loaded.triangle_count(), loaded.vertex_count()), (mesh.triangle_count(), mesh.vertex_count())); @@ -590,4 +590,36 @@ mod tests { assert!(check_vertex_bound(0, 3 * MAX_TRIANGLES).is_ok()); assert!(check_vertex_bound(0, 3 * MAX_TRIANGLES + 1).is_err()); } + + #[test] + fn obj_text_preserves_material_and_attribute_parity() { + let positions = vec![Vec3::new(0.0, 0.0, 0.0), Vec3::new(1.0, 0.0, 0.0), Vec3::new(0.0, 1.0, 0.0)]; + let normals = vec![Vec3::Z; 3]; + let uvs = vec![Vec2::new(0.0, 0.25), Vec2::new(1.0, 0.25), Vec2::new(0.0, 0.75)]; + let mesh = Mesh::from_parts(positions.clone(), Some(normals.clone()), uvs, vec![0, 1, 2], "material").unwrap(); + let text = obj_text(&mesh).unwrap(); + + assert!(!text.contains("mtllib") && !text.contains("usemtl")); + assert!(!text.contains("\no ") && !text.contains("\ng ") && !text.contains("\ns ")); + assert!(text.contains("vt 0 0.75\n"), "OBJ flips v: {text}"); + assert!(text.contains("f 1/1/1 2/2/2 3/3/3\n"), "{text}"); + + let without_uvs = Mesh { positions: positions.clone(), normals, indices: vec![0, 1, 2], ..Mesh::default() }; + let no_uv_text = obj_text(&without_uvs).unwrap(); + assert!(!no_uv_text.contains("vt ")); + assert!(no_uv_text.contains("f 1//1 2//2 3//3\n"), "{no_uv_text}"); + + let positions_only = Mesh { positions, indices: vec![0, 1, 2], ..Mesh::default() }; + let pos_only_text = obj_text(&positions_only).unwrap(); + assert!(!pos_only_text.contains("vt ") && !pos_only_text.contains("vn ")); + assert!(pos_only_text.contains("f 1 2 3\n"), "{pos_only_text}"); + } + + #[test] + fn obj_text_propagates_shared_writer_refusals() { + assert!(obj_text(&Mesh::default()).is_err()); + + let invalid = Mesh { positions: vec![Vec3::ZERO, Vec3::X, Vec3::Y], indices: vec![0, 1, 99], ..Mesh::default() }; + assert!(obj_text(&invalid).is_err()); + } } diff --git a/crates/ui-egui/src/native_dialogs.test.rs b/crates/ui-egui/src/native_dialogs.test.rs index bb0c90c..50d18a3 100644 --- a/crates/ui-egui/src/native_dialogs.test.rs +++ b/crates/ui-egui/src/native_dialogs.test.rs @@ -355,7 +355,7 @@ fn geometry_dialog_exports_the_mesh_captured_before_a_scene_change() { let target = path.clone(); answer_all(&mut app, move || Answer::Paths(vec![target.clone()])); - assert_eq!(std::fs::read_to_string(&path).unwrap(), materialcraft_engine::formats::mesh::obj_text(&mesh)); + assert_eq!(std::fs::read_to_string(&path).unwrap(), materialcraft_engine::formats::mesh::obj_text(&mesh).unwrap()); assert_eq!(std::sync::Arc::strong_count(&mesh), 1, "the answered dialog retained the mesh"); assert!(app.session.finish_display_handoff().is_none()); std::fs::remove_file(path).unwrap(); @@ -375,7 +375,7 @@ fn export_dialogs_keep_native_non_utf8_destinations() { ask(&mut app, FileDialog::save(), Then::ExportGeometry(mesh.clone())); let target = path.clone(); answer_all(&mut app, move || Answer::Paths(vec![target.clone()])); - assert_eq!(std::fs::read_to_string(&path).unwrap(), materialcraft_engine::formats::mesh::obj_text(&mesh)); + assert_eq!(std::fs::read_to_string(&path).unwrap(), materialcraft_engine::formats::mesh::obj_text(&mesh).unwrap()); } else { ask(&mut app, FileDialog::save(), Then::SaveRender(Box::new((1, 1, std::sync::Arc::new(vec![5, 19, 77, 255]))))); let target = path.clone();