Files
craft-libs/crates/image-codecs/src/codecs/dds.rs
T
vangel76 f31c93cf59 DDS: read and write BC4 and BC5 (grayscale data and normal maps)
BC4 is one block of 16 single-channel values and BC5 two of them, the
block BC3 already stores its alpha in, so the alpha block encoder and
decoder now serve all three (BC3's output stays the same).

Reading: ATI1 and BC4U, and DXGI 79 (typeless) and 80, open as gray;
ATI2 and BC5U, and DXGI 82 (typeless, as Pillow writes it) and 83,
open as RGB with blue 0. ATI2 follows Microsoft's mapping, red in the
first block. Signed BC4S/BC5S and DXGI 81/84 are refused, naming SNORM.

Writing: DdsCompression::Bc4 (bc4, ati1) from gray and Bc5 (bc5, ati2)
from RGB, with the DX10 header (BC4_UNORM, BC5_UNORM, one 2D texture),
mip chains included. The encode plan converts other layouts to gray
and RGB, so the fidelity warnings report colour to gray and dropped
alpha; BC5's dropped blue is documented under LossyCompression.

DdsCompression is now #[non_exhaustive] and ALL has six entries.
2026-11-16 00:00:28 +01:00

58 lines
2.6 KiB
Rust

//! DirectDraw Surface (DDS) textures through [`craft_dds`]: its errors and pixel layouts mapped
//! onto this crate's, and the decoder's size checked against [`Limits`] before it allocates.
use crate::SampleType;
use crate::error::CodecError;
use crate::fidelity::Plan;
use crate::format::Format;
use crate::image::{ChannelLayout, Image};
use crate::options::{EncodeOptions, Limits};
use craft_dds::{DdsError, Layout, Texture};
fn error(e: DdsError) -> CodecError {
match e {
DdsError::Malformed(reason) => CodecError::malformed(Format::Dds, reason),
DdsError::Unsupported(reason) => CodecError::unsupported(Format::Dds, reason),
DdsError::Encode(reason) => CodecError::encode(Format::Dds, reason),
DdsError::Alloc(len) => CodecError::LimitExceeded(format!("cannot allocate {len} elements for the image")),
other => CodecError::malformed(Format::Dds, other),
}
}
pub(crate) fn channel_layout(layout: Layout) -> ChannelLayout {
match layout {
Layout::Gray => ChannelLayout::Gray,
Layout::GrayA => ChannelLayout::GrayA,
Layout::Rgb => ChannelLayout::Rgb,
Layout::Rgba => ChannelLayout::Rgba,
}
}
/// Header-declared extra subresources cannot survive a single-image rewrite, even without presence flags.
pub(crate) fn has_additional_images(bytes: &[u8]) -> Result<bool, CodecError> {
craft_dds::has_additional_images(bytes).map_err(error)
}
pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result<Image, CodecError> {
let texture = Texture::parse(b).map_err(error)?;
let layout = channel_layout(texture.layout());
limits.check(texture.width(), texture.height(), layout, SampleType::U8)?;
let pixels = texture.decode().map_err(error)?;
Image::from_u8(texture.width(), texture.height(), layout, pixels)
}
pub(crate) fn encode(image: &Image, plan: Plan, opts: &EncodeOptions) -> Result<Vec<u8>, CodecError> {
let img = image.converted(plan.layout, SampleType::U8);
let pixels = img.as_u8().ok_or_else(|| CodecError::encode(Format::Dds, "the image was not converted to 8 bits per channel"))?;
let layout = match img.layout() {
ChannelLayout::Gray => Layout::Gray,
ChannelLayout::GrayA => Layout::GrayA,
ChannelLayout::Rgb => Layout::Rgb,
ChannelLayout::Rgba => Layout::Rgba,
// `fidelity::plan` only picks the four layouts above for DDS.
other => return Err(CodecError::encode(Format::Dds, format!("{other:?} pixels can't be written as `{}`", opts.dds.compression.name()))),
};
let (width, height) = img.dimensions();
craft_dds::encode(width, height, layout, pixels, opts.dds).map_err(error)
}