DDS: refuse premultiplied-alpha DX10 textures

The DX10 header's alpha mode (miscFlags2, low three bits) was never
read, so RGBA, BGRA, BC2 and BC3 files marked premultiplied were read
as straight alpha. They are refused as Unsupported, like the legacy
DXT2/DXT4 FourCCs. Alpha modes 0, 1, 3 and 4 and the formats without
alpha are unchanged.
This commit is contained in:
vangel76 committed 2026-11-16 01:34:20 +01:00
1 parent c27333683a
commit 39e1d41af7
4 files changed
+36 -2

No files matched your search

+1 -1
View File
@@ -16,7 +16,7 @@ craft-dds = { git = "https://github.com/storytold/craft-libs", rev = "<commit>"
| 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.
+5
View File
@@ -359,6 +359,11 @@ fn dx10_encoding(b: &[u8]) -> Result<Encoding, DdsError> {
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];
+1 -1
View File
@@ -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
+29
View File
@@ -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<u8> {
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.