diff --git a/crates/dds/README.md b/crates/dds/README.md index 720ec9ba7..e3c3f4956 100644 --- a/crates/dds/README.md +++ b/crates/dds/README.md @@ -16,7 +16,7 @@ craft-dds = { git = "https://github.com/storytold/craft-libs", rev = "" | Headers | legacy and DX10 | legacy; DX10 for BC4 and BC5 | | Mip levels | the top level only | optional full chain, box-filtered | -Cubemaps, texture arrays, volume textures, premultiplied DXT2/DXT4, signed (SNORM) BC4/BC5 and +Cubemaps, texture arrays, volume textures, premultiplied-alpha textures (DXT2/DXT4, or DX10 alpha mode 2), signed (SNORM) BC4/BC5 and the other DXGI formats (BC6H, BC7, float) are refused with `DdsError::Unsupported` rather than read in part. diff --git a/crates/dds/src/lib.rs b/crates/dds/src/lib.rs index 5c088052a..2d026a660 100644 --- a/crates/dds/src/lib.rs +++ b/crates/dds/src/lib.rs @@ -359,6 +359,11 @@ fn dx10_encoding(b: &[u8]) -> Result { if array > 1 { return Err(unsupported(format!("texture arrays are not supported ({array} textures)"))); } + // miscFlags2 bits 0-2 are the alpha mode; 2 is premultiplied. Only the formats with a real + // alpha channel are affected, and the legacy path refuses their DXT2/DXT4 equivalents too. + if le32(b, HEADER + 16)? & 0x7 == 2 && matches!(dxgi, 27..=29 | 73..=78 | 87 | 90 | 91) { + return Err(unsupported(format!("premultiplied-alpha DX10 textures are not supported (DXGI format {dxgi})"))); + } const RGBA: [u32; 4] = [0xff, 0xff00, 0xff_0000, 0xff00_0000]; const BGRA: [u32; 4] = [0xff_0000, 0xff00, 0xff, 0xff00_0000]; const BGRX: [u32; 4] = [0xff_0000, 0xff00, 0xff, 0]; diff --git a/crates/image-codecs/README.md b/crates/image-codecs/README.md index 1bdf1418d..11ed6b40c 100644 --- a/crates/image-codecs/README.md +++ b/crates/image-codecs/README.md @@ -100,7 +100,7 @@ embedded table (and a PNG's transparent entry) so `photocraft-io` can open them map's Z is not reconstructed). ATI2 is read as Microsoft reads it, red in the first block. Uncompressed row padding follows the header's `DDSD_PITCH` stride; without that flag, rows are tightly packed. Everything opens as U8; the other mip levels are ignored. Cubemaps, texture - arrays, volume textures, premultiplied DXT2/DXT4, signed (SNORM) BC4/BC5 and other formats + arrays, volume textures, premultiplied-alpha textures (DXT2/DXT4, or DX10 alpha mode 2), signed (SNORM) BC4/BC5 and other formats (BC6H, BC7, float) are refused with `CodecError::Unsupported`. Writing (`EncodeOptions::dds`) makes a 2D texture: uncompressed by default (BGRA, BGRX, or luminance with or without alpha, so it reads back exactly), BC1/BC2/BC3 (from RGBA) with the legacy header, or BC4 (from gray, for diff --git a/crates/image-codecs/tests/it/dds.rs b/crates/image-codecs/tests/it/dds.rs index ad927019a..d174255b2 100644 --- a/crates/image-codecs/tests/it/dds.rs +++ b/crates/image-codecs/tests/it/dds.rs @@ -327,6 +327,35 @@ const BC4_200: [u8; 8] = [0xc8, 0x32, 0, 0, 0, 0, 0, 0]; /// A value block of 250 everywhere (six-value mode, a0 = 10 <= a1 = 250, every index 1). const VALUES_250: [u8; 8] = [0x0a, 0xfa, 0x49, 0x92, 0x24, 0x49, 0x92, 0x24]; +/// A DX10 file of one 1x1 level of `dxgi` with DDS_ALPHA_MODE `alpha_mode` (miscFlags2) and zeroed pixel data. +fn dx10_alpha_mode(dxgi: u32, alpha_mode: u32) -> Vec { + let mut b = dx10(1, 1, dxgi, 3, 0, 1); + let n = b.len(); + b[n - 4..].copy_from_slice(&alpha_mode.to_le_bytes()); + b.extend([0u8; 16]); + b +} + +#[test] +fn refuses_premultiplied_alpha_dx10_textures() { + // RGBA, BGRA (typeless, UNORM, sRGB) and BC2/BC3: alpha mode 2 is premultiplied, like DXT2/DXT4. + for dxgi in [27, 28, 29, 87, 90, 91, 73, 74, 75, 76, 77, 78] { + match decode_as(Format::Dds, &dx10_alpha_mode(dxgi, 2)) { + Err(CodecError::Unsupported { reason, .. }) => assert!(reason.contains("premultiplied"), "{dxgi}: {reason}"), + other => panic!("DXGI {dxgi}: {:?}", other.map(|_| ())), + } + } + // Straight, unknown, opaque and custom alpha modes, and formats without alpha, still decode. + for dxgi in [28, 87, 74, 77] { + for mode in [0, 1, 3, 4] { + assert!(decode_as(Format::Dds, &dx10_alpha_mode(dxgi, mode)).is_ok(), "{dxgi} mode {mode}"); + } + } + for dxgi in [88, 71, 80, 83] { + assert!(decode_as(Format::Dds, &dx10_alpha_mode(dxgi, 2)).is_ok(), "{dxgi}"); + } +} + #[test] fn decodes_bc4_as_gray_from_every_name() { // 5×3: two blocks across, one down; the second block's texels outside the image are dropped.