Files
craft-libs/crates/raster-codecs/src/dds.rs
T
.CooCoo a19d58c5f6 dds: read R16G16 as red and green, and open 16-bit textures at 16 bits (#2124)
R16G16_UNORM stores a red and a green channel
(https://learn.microsoft.com/en-us/windows/win32/api/dxgiformat/ne-dxgiformat-dxgi_format),
but craft-dds read it as gray and alpha, so a two-channel normal map
opened with its green channel as transparency. It now reads as RGB with
blue 0, as BC5 and G16R16 channel masks already do. encode16 still
writes two-channel 16-bit pixels as R16G16; they read back as red and
green.

craft-image-codecs decoded every DDS at 8 bits. Texture::bits_per_channel
gives the widest stored channel, and textures wider than 8 bits (R16,
R16G16, R16G16B16A16, legacy FourCC 36, and channel masks with a 10- or
16-bit field) now open as U16 through decode16. craft-raster-codecs'
decode_levels does the same for every mip level.
2026-11-18 15:31:45 -05:00

154 lines
6.8 KiB
Rust

//! DDS compatibility facade; parsing, mip layout and BC block codecs live in craft-dds.
use crate::{CodecError, Image, Limits, SampleType, bridge};
use craft_image_codecs::Format;
pub fn looks_like_dds(bytes: &[u8]) -> bool {
craft_image_codecs::detect(bytes) == Some(Format::Dds)
}
pub fn has_additional_images(bytes: &[u8]) -> Result<bool, CodecError> {
craft_image_codecs::has_unretained_source_data(bytes).map_err(bridge::from_shared_error)
}
pub fn decode(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError> {
bridge::format(Format::Dds, bytes, limits)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum DdsCompression {
#[default]
None,
Bc1,
Bc2,
Bc3,
}
impl DdsCompression {
pub const ALL: [Self; 4] = [Self::None, Self::Bc1, Self::Bc2, Self::Bc3];
pub fn name(self) -> &'static str {
match self {
Self::None => "none",
Self::Bc1 => "bc1",
Self::Bc2 => "bc2",
Self::Bc3 => "bc3",
}
}
pub fn from_name(name: &str) -> Option<Self> {
match name.to_ascii_lowercase().as_str() {
"none" => Some(Self::None),
"bc1" | "dxt1" => Some(Self::Bc1),
"bc2" | "dxt3" => Some(Self::Bc2),
"bc3" | "dxt5" => Some(Self::Bc3),
_ => None,
}
}
pub fn is_block(self) -> bool {
self != Self::None
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct DdsOptions {
pub compression: DdsCompression,
pub mipmaps: bool,
}
pub fn encode(image: &Image, options: DdsOptions) -> Result<Vec<u8>, CodecError> {
if image.sample_type() != SampleType::U8 {
return Err(CodecError::encode("DDS", "DDS is written at 8 bits per channel"));
}
// BC storage encodes RGBA blocks. The generic image API can plan a
// conversion from other layouts, but this compatibility facade accepts
// only layouts its former writer stored directly.
let allowed = if options.compression.is_block() {
image.layout() == crate::ChannelLayout::Rgba
} else {
matches!(image.layout(), crate::ChannelLayout::Gray | crate::ChannelLayout::GrayA | crate::ChannelLayout::Rgb | crate::ChannelLayout::Rgba)
};
if !allowed {
return Err(CodecError::encode("DDS", format!("{:?} pixels can't be written as `{}`", image.layout(), options.compression.name())));
}
let compression = match options.compression {
DdsCompression::None => craft_image_codecs::DdsCompression::None,
DdsCompression::Bc1 => craft_image_codecs::DdsCompression::Bc1,
DdsCompression::Bc2 => craft_image_codecs::DdsCompression::Bc2,
DdsCompression::Bc3 => craft_image_codecs::DdsCompression::Bc3,
};
let opts =
craft_image_codecs::EncodeOptions { dds: craft_image_codecs::DdsOptions { compression, mipmaps: options.mipmaps }, ..Default::default() };
bridge::encode(image, Format::Dds, &opts)
}
/// Metadata for the supported 2D DDS texture, available without pixel data.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DdsInfo {
pub width: u32,
pub height: u32,
pub levels: u32,
pub alpha: bool,
/// `None` for a shared decoder compression not exposed by this legacy writer API.
pub compression: Option<DdsCompression>,
}
fn dds_error(error: craft_dds::DdsError) -> CodecError {
match error {
craft_dds::DdsError::Malformed(reason) => CodecError::malformed("DDS", reason),
craft_dds::DdsError::Unsupported(reason) => CodecError::unsupported("DDS", reason),
craft_dds::DdsError::Encode(reason) => CodecError::encode("DDS", reason),
craft_dds::DdsError::Alloc(len) => CodecError::LimitExceeded(format!("cannot allocate {len} bytes for DDS")),
craft_dds::DdsError::LimitExceeded { required, limit } => {
CodecError::LimitExceeded(format!("decoded DDS requires {required} bytes, exceeding limit {limit}"))
}
other => CodecError::malformed("DDS", other),
}
}
fn layout(texture: &craft_dds::Texture<'_>) -> crate::ChannelLayout {
match texture.layout() {
craft_dds::Layout::Gray => crate::ChannelLayout::Gray,
craft_dds::Layout::GrayA => crate::ChannelLayout::GrayA,
craft_dds::Layout::Rgb => crate::ChannelLayout::Rgb,
craft_dds::Layout::Rgba => crate::ChannelLayout::Rgba,
}
}
pub fn info(bytes: &[u8]) -> Result<DdsInfo, CodecError> {
let texture = craft_dds::Texture::parse(bytes).map_err(dds_error)?;
let compression = match texture.compression() {
craft_dds::DdsCompression::None => Some(DdsCompression::None),
craft_dds::DdsCompression::Bc1 => Some(DdsCompression::Bc1),
craft_dds::DdsCompression::Bc2 => Some(DdsCompression::Bc2),
craft_dds::DdsCompression::Bc3 => Some(DdsCompression::Bc3),
_ => None,
};
let alpha = matches!(texture.layout(), craft_dds::Layout::GrayA | craft_dds::Layout::Rgba);
Ok(DdsInfo { width: texture.width(), height: texture.height(), levels: texture.levels(), alpha, compression })
}
/// Decode the declared mip chain, charging all retained pixels to one output-byte budget.
/// Cancellation is checked before each level; no partial chain is returned. Channels wider than
/// 8 bits decode at 16 bits, as [`decode`] opens the top level.
pub fn decode_levels(bytes: &[u8], limits: &Limits, cancel: &dyn Fn() -> bool) -> Result<Vec<Image>, CodecError> {
let texture = craft_dds::Texture::parse(bytes).map_err(dds_error)?;
let wide = texture.bits_per_channel() > 8;
let mut images = Vec::new();
images.try_reserve_exact(texture.levels() as usize).map_err(|_| CodecError::LimitExceeded("cannot allocate DDS mip list".into()))?;
let mut remaining = limits.max_alloc;
for i in 0..texture.levels() {
if cancel() {
return Err(CodecError::malformed("DDS", "DDS import cancelled"));
}
let level = texture.mip_level(i).map_err(dds_error)?;
let layout = layout(&level);
let level_limits = Limits { max_alloc: remaining, ..*limits };
let exceeded = || CodecError::LimitExceeded("DDS mip chain exceeds max_alloc".into());
if wide {
level_limits.check(level.width(), level.height(), layout, SampleType::U16)?;
let pixels = level.decode16_with_limit(remaining).map_err(dds_error)?;
remaining = remaining.checked_sub(pixels.len() as u64 * 2).ok_or_else(exceeded)?;
images.push(Image::from_u16(level.width(), level.height(), layout, &pixels)?);
} else {
level_limits.check(level.width(), level.height(), layout, SampleType::U8)?;
let pixels = level.decode_with_limit(remaining).map_err(dds_error)?;
remaining = remaining.checked_sub(pixels.len() as u64).ok_or_else(exceeded)?;
images.push(Image::from_u8(level.width(), level.height(), layout, pixels)?);
}
}
Ok(images)
}