diff --git a/Cargo.lock b/Cargo.lock index c171ba4..0970069 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -901,6 +901,16 @@ dependencies = [ "objc2-app-kit 0.3.2", ] +[[package]] +name = "craft-texture" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=726ad0f8290bedb6ced60fa06200a6b574f9990d#726ad0f8290bedb6ced60fa06200a6b574f9990d" +dependencies = [ + "bytemuck", + "dds", + "thiserror 2.0.21", +] + [[package]] name = "craft-texture" version = "0.1.0" @@ -2712,7 +2722,7 @@ dependencies = [ name = "materialcraft-engine" version = "0.4.1" dependencies = [ - "craft-texture", + "craft-texture 0.1.0 (git+https://github.com/storytold/craft-libs?rev=92fb3fa8ac2981c4c31961cfce7f7fdfbb03540d)", "materialcraft-core", "materialcraft-eval", "materialcraft-formats", @@ -2728,7 +2738,7 @@ dependencies = [ name = "materialcraft-eval" version = "0.4.1" dependencies = [ - "craft-texture", + "craft-texture 0.1.0 (git+https://github.com/storytold/craft-libs?rev=92fb3fa8ac2981c4c31961cfce7f7fdfbb03540d)", "half", "materialcraft-core", "materialcraft-nodes", @@ -2741,10 +2751,10 @@ name = "materialcraft-formats" version = "0.4.1" dependencies = [ "base64 0.22.1", - "bytemuck", "craft-fonts-build", "craft-mcmat", - "craft-texture", + "craft-texture 0.1.0 (git+https://github.com/storytold/craft-libs?rev=726ad0f8290bedb6ced60fa06200a6b574f9990d)", + "craft-texture 0.1.0 (git+https://github.com/storytold/craft-libs?rev=92fb3fa8ac2981c4c31961cfce7f7fdfbb03540d)", "craft-xml", "dds", "flate2", @@ -2778,7 +2788,7 @@ dependencies = [ name = "materialcraft-nodes" version = "0.4.1" dependencies = [ - "craft-texture", + "craft-texture 0.1.0 (git+https://github.com/storytold/craft-libs?rev=92fb3fa8ac2981c4c31961cfce7f7fdfbb03540d)", "materialcraft-core", "rayon", ] @@ -2787,7 +2797,7 @@ dependencies = [ name = "materialcraft-render" version = "0.4.1" dependencies = [ - "craft-texture", + "craft-texture 0.1.0 (git+https://github.com/storytold/craft-libs?rev=92fb3fa8ac2981c4c31961cfce7f7fdfbb03540d)", "materialcraft-core", "materialcraft-geom", "rayon", diff --git a/Cargo.toml b/Cargo.toml index 5029ee7..0bbf502 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,7 @@ rust-version = "1.95" repository = "https://github.com/storytold/materialcraft" [workspace.dependencies] +craft-texture-dds = { package = "craft-texture", version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "726ad0f8290bedb6ced60fa06200a6b574f9990d", features = ["dds"] } # Shared XML escaping; SBS model and serialization policy stay app-local. craft-xml = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "b6d9feed23ceae249b40593ed84a668343091627" } resvg = { version = "0.47", default-features = false } diff --git a/crates/formats/Cargo.toml b/crates/formats/Cargo.toml index e81fba6..cd5d333 100644 --- a/crates/formats/Cargo.toml +++ b/crates/formats/Cargo.toml @@ -18,8 +18,7 @@ materialcraft-nodes = { workspace = true } gltf = { workspace = true } tobj = { workspace = true } image = { workspace = true } -dds = { workspace = true } -bytemuck = { workspace = true } +craft-texture-dds = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } thiserror = { workspace = true } @@ -33,3 +32,6 @@ resvg = { workspace = true } flate2 = { workspace = true } craft-fonts-build = { workspace = true } sha2 = { workspace = true } + +[dev-dependencies] +dds = { workspace = true } diff --git a/crates/formats/src/dds_image.rs b/crates/formats/src/dds_image.rs index d993b24..84248a6 100644 --- a/crates/formats/src/dds_image.rs +++ b/crates/formats/src/dds_image.rs @@ -3,17 +3,14 @@ use craft_texture::{Image, Kind, Limits, MAX_SIDE, srgb_to_linear}; pub(crate) fn decode(bytes: &[u8], linear: bool) -> Result { let error = |message: String| FormatError::Image(message); - let mut decoder = dds::Decoder::new(std::io::Cursor::new(bytes)).map_err(|e| error(e.to_string()))?; - let size = decoder.main_size(); - if size.width == 0 || size.height == 0 || size.width > MAX_SIDE || size.height > MAX_SIDE { - return Err(error(format!("DDS dimensions must be 1..={MAX_SIDE}"))); - } - - let mut data = vec![0.0_f32; size.width as usize * size.height as usize * 4]; - let view = - dds::ImageViewMut::new(bytemuck::cast_slice_mut(&mut data), size, dds::ColorFormat::RGBA_F32).ok_or_else(|| error("Invalid DDS dimensions".into()))?; - decoder.read_surface(view).map_err(|e| error(e.to_string()))?; - if !linear && decoder.format().precision() != dds::Precision::F32 { + let mut limits = craft_texture_dds::Limits::default(); + limits.max_side = MAX_SIDE; + let decoded = craft_texture_dds::dds::decode_first(bytes, &limits).map_err(|e| match e { + craft_texture_dds::dds::DecodeError::Image(craft_texture_dds::ImageError::BadSize(_, _)) => error(format!("DDS dimensions must be 1..={MAX_SIDE}")), + e => error(e.to_string()), + })?; + let mut data = decoded.rgba; + if !linear && !decoded.source_precision_is_f32 { for pixel in data.as_chunks_mut::<4>().0 { for value in pixel.iter_mut().take(3) { *value = srgb_to_linear(*value); @@ -21,5 +18,5 @@ pub(crate) fn decode(bytes: &[u8], linear: bool) -> Result { } } - Image::from_data(size.width, size.height, Kind::Color, data, &Limits::default()).map_err(|e| error(e.to_string())) + Image::from_data(decoded.width, decoded.height, Kind::Color, data, &Limits::default()).map_err(|e| error(e.to_string())) } diff --git a/crates/formats/tests/bitmap_imports.rs b/crates/formats/tests/bitmap_imports.rs index 1d8e11f..bd03b29 100644 --- a/crates/formats/tests/bitmap_imports.rs +++ b/crates/formats/tests/bitmap_imports.rs @@ -72,3 +72,24 @@ fn dds_supports_compressed_uncompressed_and_float_textures() { assert!(decode(&bytes[..bytes.len() / 2], true).is_err()); } } + +#[test] +fn shared_dds_keeps_rgb_transfer_alpha_and_float_range() { + let mut bytes = Vec::new(); + dds::header::Header::new_image(1, 1, dds::Format::R8G8B8A8_UNORM).write(&mut bytes).unwrap(); + bytes.extend_from_slice(&[128, 64, 255, 32]); + let raw = decode(&bytes, true).unwrap(); + let converted = decode(&bytes, false).unwrap(); + for i in 0..3 { + assert!((converted.data()[i] - craft_texture::srgb_to_linear(raw.data()[i])).abs() < 1e-6); + } + assert_eq!(converted.data()[3], raw.data()[3]); + let mut floats = Vec::new(); + dds::header::Header::new_image(1, 1, dds::Format::R32G32B32A32_FLOAT).write(&mut floats).unwrap(); + for v in [-0.5_f32, 2.0, 0.25, 0.75] { + floats.extend_from_slice(&v.to_le_bytes()); + } + for linear in [false, true] { + assert_eq!(decode(&floats, linear).unwrap().data(), &[-0.5, 2.0, 0.25, 0.75]); + } +}