dds: 16-bit input and output (R16, R16G16, R16G16B16A16; BC4/BC5 fitted to 16-bit values)

encode16 writes 16-bit pixels uncompressed as R16_UNORM, R16G16_UNORM or
R16G16B16A16_UNORM with the DX10 header, and fits BC4/BC5 endpoints and
indices against the 16-bit values; BC1-BC3 round to 8 bits first.
Texture::decode16 reads these formats (and legacy FourCC 36, wide channel
masks, BC4/BC5 interpolated at 16 bits) without losing precision.
This commit is contained in:
vangel76
2026-11-16 21:53:12 +01:00
parent 136c34ae4c
commit f200f1c91d
3 changed files with 545 additions and 46 deletions
+14 -4
View File
@@ -11,22 +11,32 @@ craft-dds = { git = "https://github.com/storytold/craft-libs", rev = "<commit>"
| | Read | Write |
|---|---|---|
| Uncompressed | 8, 16, 24 or 32 bits per pixel described by channel masks, RGB or luminance, with or without alpha; padded rows (`DDSD_PITCH`) | BGRA, BGRX, luminance, luminance with alpha |
| Uncompressed | 8, 16, 24 or 32 bits per pixel described by channel masks, RGB or luminance, with or without alpha; padded rows (`DDSD_PITCH`); 16 bits per channel: `R16_UNORM`, `R16G16_UNORM`, `R16G16B16A16_UNORM` (DXGI 56, 35, 11 and their typeless forms; legacy FourCC 36) | BGRA, BGRX, luminance, luminance with alpha; from 16-bit pixels `R16_UNORM`, `R16G16_UNORM`, `R16G16B16A16_UNORM` |
| Block-compressed | BC1, BC2, BC3 (DXT1, DXT3, DXT5); unsigned BC4 (ATI1, BC4U, DXGI 79 and 80) and BC5 (ATI2, BC5U, DXGI 82 and 83) | BC1, BC2, BC3, BC4, BC5 |
| Headers | legacy and DX10 | legacy; DX10 for BC4 and BC5 |
| Headers | legacy and DX10 | legacy; DX10 for BC4, BC5 and the 16-bit formats |
| Mip levels | the top level only | optional full chain, box-filtered |
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
the other DXGI formats (BC6H, BC7, float, signed or integer 16-bit) are refused with `DdsError::Unsupported` rather than read
in part.
Pixels are 8 bits per channel, interleaved and top row first, in one of four `Layout`s (`Gray`,
Pixels are 8 bits per channel (`encode`, `Texture::decode`) or 16 bits per channel (`encode16`,
`Texture::decode16`), interleaved and top row first, in one of four `Layout`s (`Gray`,
`GrayA`, `Rgb`, `Rgba`). BC1 to BC3 are written from RGBA, BC4 from gray and BC5 from RGB
(`DdsCompression::layout` names it). BC4 reads back as gray. BC5 stores red and green only, so it
reads back as RGB with blue 0: for a tangent-space normal map, X and Y, with Z left to the
renderer. ATI2 files are read as Microsoft's tools read them (red in the first block), not with
the two blocks swapped as FFmpeg does.
From 16-bit pixels, uncompressed textures keep 16 bits per channel: gray as `R16_UNORM`, gray
and alpha (or any two channels, such as a normal map's X and Y) as `R16G16_UNORM`, RGB (alpha
65535) and RGBA as `R16G16B16A16_UNORM`, all with the DX10 header; they read back as gray, gray
and alpha, and RGBA. BC4 and BC5 keep 8-bit endpoints, as the formats store them, but pick them
and the indices against the 16-bit values, so a smooth height or normal map has less error than
when its 8-bit rounding is compressed. BC1 to BC3 round the pixels to 8 bits first.
`Texture::decode` rounds 16-bit formats to 8 bits; `Texture::decode16` returns them exactly,
interpolates BC4 and BC5 at 16 bits, and scales every 8-bit format by 257.
```rust
use craft_dds::{DdsCompression, DdsOptions, Layout, Texture};
+119 -1
View File
@@ -4,7 +4,9 @@
//! knows about DDS headers, so other containers of the same blocks can use it.
//!
//! BC4 is one block of 16 single-channel values, the same block BC3 stores its alpha in; BC5 is
//! two of them, red then green. [`value_block`] and [`decode_values`] serve all three.
//! two of them, red then green. [`value_block`] and [`decode_values`] serve all three;
//! [`value_block16`] and [`decode_values16`] are the same blocks for 16-bit values (BC4 and BC5
//! from 16-bit sources).
//!
//! The encoder fits a block's colours with two RGB565 endpoints along their principal axis,
//! refines them by least squares, and scores every candidate against the exact palette the
@@ -69,6 +71,26 @@ pub(crate) fn decode_colour(block: &[u8], one_bit_alpha: bool) -> Block {
std::array::from_fn(|t| palette[(indices >> (2 * t)) as usize & 3])
}
/// [`alpha_palette`] at 16 bits: the same eight values before they are rounded to 8 bits, as a
/// decoder that interpolates at a higher precision shows them (endpoints scaled by 257, so 255
/// is 65535).
pub(crate) fn alpha_palette16(a0: u8, a1: u8) -> [u16; 8] {
let (a0, a1) = (u32::from(a0) * 257, u32::from(a1) * 257);
let mut palette = [a0, a1, 0, 0, 0, 0, 0, 65535];
if a0 > a1 {
for (k, v) in palette.iter_mut().enumerate().skip(2) {
let k = k as u32;
*v = ((8 - k) * a0 + (k - 1) * a1 + 3) / 7;
}
} else {
for (k, v) in palette.iter_mut().enumerate().take(6).skip(2) {
let k = k as u32;
*v = ((6 - k) * a0 + (k - 1) * a1 + 2) / 5;
}
}
palette.map(|v| v as u16)
}
/// BC2's alpha: four explicit bits per texel.
pub(crate) fn decode_bc2_alpha(block: &[u8]) -> [u8; 16] {
let bits = u64::from_le_bytes(std::array::from_fn(|i| block.get(i).copied().unwrap_or(0)));
@@ -84,6 +106,14 @@ pub(crate) fn decode_values(block: &[u8]) -> [u8; 16] {
std::array::from_fn(|t| palette[((bits >> (3 * t)) & 7) as usize])
}
/// [`decode_values`] at 16 bits, from [`alpha_palette16`].
pub(crate) fn decode_values16(block: &[u8]) -> [u16; 16] {
let byte = |i: usize| block.get(i).copied().unwrap_or(0);
let palette = alpha_palette16(byte(0), byte(1));
let bits = u64::from_le_bytes([byte(2), byte(3), byte(4), byte(5), byte(6), byte(7), 0, 0]);
std::array::from_fn(|t| palette[((bits >> (3 * t)) & 7) as usize])
}
/// A BC1 block. Texels under half opacity become BC1's transparent black, which needs the
/// three-colour mode; an opaque block takes whichever mode fits its colours better.
pub(crate) fn encode_bc1(block: &Block, inside: Mask) -> [u8; 8] {
@@ -369,6 +399,64 @@ fn value_fit(values: &[u8; 16], inside: Mask, a0: u8, a1: u8) -> ([u8; 8], u32)
([a0, a1, b[0], b[1], b[2], b[3], b[4], b[5]], error)
}
/// [`value_block`] for 16-bit values: the endpoints are still 8 bits, but each end of each mode
/// tries the 8-bit levels just below and above the 16-bit extreme, and every candidate is scored
/// against the 16-bit palette ([`alpha_palette16`]). A block that spans less than one 8-bit level
/// is flat from 8-bit values but gets the interpolated steps between two adjacent levels here.
pub(crate) fn value_block16(values: &[u16; 16], inside: Mask) -> [u8; 8] {
let mut all: Option<(u16, u16)> = None;
let mut mid = all;
let widen = |r: Option<(u16, u16)>, v: u16| Some(r.map_or((v, v), |(lo, hi)| (lo.min(v), hi.max(v))));
for (t, &v) in values.iter().enumerate() {
if inside >> t & 1 != 0 {
all = widen(all, v);
if !matches!(v, 0 | 65535) {
mid = widen(mid, v);
}
}
}
let (lo, hi) = all.unwrap_or((0, 0));
let (mid_lo, mid_hi) = mid.unwrap_or((0, 0));
let (down, up) = (|v: u16| (v / 257) as u8, |v: u16| v.div_ceil(257) as u8);
// The eight-value mode's pairs first (a0 the high end), then the six-value mode's: on a tie
// the eight-value mode wins, as in `value_block`. Each pair's palette follows its order, so a
// pair that lands in the other mode is still scored as the decoder reads it.
let candidates = [
(up(hi), down(lo)),
(up(hi), up(lo)),
(down(hi), down(lo)),
(down(hi), up(lo)),
(down(mid_lo), down(mid_hi)),
(down(mid_lo), up(mid_hi)),
(up(mid_lo), down(mid_hi)),
(up(mid_lo), up(mid_hi)),
];
let mut best: Option<([u8; 8], u64)> = None;
for (a0, a1) in candidates {
let fit = value_fit16(values, inside, a0, a1);
if best.is_none_or(|(_, e)| fit.1 < e) {
best = Some(fit);
}
}
best.map_or([0; 8], |(bytes, _)| bytes)
}
fn value_fit16(values: &[u16; 16], inside: Mask, a0: u8, a1: u8) -> ([u8; 8], u64) {
let palette = alpha_palette16(a0, a1);
let mut bits = 0u64;
let mut error = 0u64;
for (t, &v) in values.iter().enumerate() {
if inside >> t & 1 == 0 {
continue;
}
let (i, d) = nearest(&palette, |&p| u32::from(p.abs_diff(v)).pow(2));
error += u64::from(d);
bits |= (i as u64) << (3 * t);
}
let b = bits.to_le_bytes();
([a0, a1, b[0], b[1], b[2], b[3], b[4], b[5]], error)
}
#[cfg(test)]
mod tests {
use super::*;
@@ -484,6 +572,36 @@ mod tests {
assert_eq!(nearest::<u8>(&[], |_| 7), (0, 0));
}
#[test]
fn value_blocks_decode_the_frozen_vectors_at_16_bits() {
// Eight-value mode (200 > 50): index 2 is (6 * 200 + 50) / 7 of 255 at 16 bits, which
// the 8-bit palette rounds to 179.
let bits: u64 = (0..16).fold(0, |b, t| b | 2 << (3 * t));
let b = bits.to_le_bytes();
let block = [200, 50, b[0], b[1], b[2], b[3], b[4], b[5]];
assert_eq!(decode_values16(&block), [((6 * 200 + 50) * 257 + 3) / 7; 16]);
assert_eq!(decode_values(&block), [179; 16]);
// Six-value mode's indices 6 and 7 are 0 and 65535.
let bits: u64 = (0..16).fold(0, |b, t| b | (6 + (t % 2)) << (3 * t));
let b = bits.to_le_bytes();
let got = decode_values16(&[10, 250, b[0], b[1], b[2], b[3], b[4], b[5]]);
assert_eq!(got, std::array::from_fn(|t| if t % 2 == 0 { 0 } else { 65535 }));
}
#[test]
fn value_blocks_from_16_bits_step_between_two_8_bit_levels() {
// A ramp across 0.8 of one 8-bit level: from 8 bits it is one or two flat values; from 16
// bits the endpoints straddle it and the steps between them follow the ramp.
let values: [u16; 16] = std::array::from_fn(|t| 25_700 + (t as u16) * 13);
let error = |got: [u16; 16]| values.iter().zip(got).map(|(a, b)| u64::from(a.abs_diff(b)).pow(2)).sum::<u64>();
let ours = error(decode_values16(&value_block16(&values, u16::MAX)));
let from_8_bits = error(decode_values16(&value_block(&values.map(|v| ((u32::from(v) + 128) / 257) as u8), u16::MAX)));
assert!(ours * 4 < from_8_bits, "{ours} vs {from_8_bits} from 8 bits");
// Values outside the image don't count.
let solid = value_block16(&std::array::from_fn(|t| if t == 0 { 1000 } else { 60_000 }), 1);
assert!(decode_values16(&solid)[0].abs_diff(1000) < 129);
}
#[test]
fn texels_outside_the_image_are_ignored() {
// Only the top-left texel is inside: its colour alone decides the endpoints.
+412 -41
View File
@@ -2,7 +2,8 @@
//! "DDS_HEADER", "DDS_PIXELFORMAT", "DDS_HEADER_DXT10").
//!
//! Reading: the top mip level of a 2D texture, either uncompressed (8 to 32 bits per pixel
//! described by channel masks, RGB or luminance) or block-compressed BC1/BC2/BC3
//! described by channel masks, RGB or luminance, or 16 bits per channel: `R16_UNORM`,
//! `R16G16_UNORM`, `R16G16B16A16_UNORM`) or block-compressed BC1/BC2/BC3
//! (DXT1/DXT3/DXT5) or unsigned BC4/BC5 (ATI1/ATI2), with or without the DX10 header. BC4 opens
//! as gray and BC5 as RGB with blue 0: BC5 stores only red and green, and a normal map's Z is
//! left for the caller to reconstruct. Cubemaps, texture arrays and volume textures are refused
@@ -14,7 +15,12 @@
//! legacy FourCCs are vendor names that programs read differently (ATI2's two blocks are swapped
//! by some). BC6H, BC7 and sRGB formats aren't written.
//!
//! Pixels are 8 bits per channel, interleaved, top row first, in one of the [`Layout`]s. The
//! Pixels are 8 bits per channel ([`encode`], [`Texture::decode`]) or 16 bits per channel
//! ([`encode16`], [`Texture::decode16`]), interleaved, top row first, in one of the [`Layout`]s.
//! Uncompressed 16-bit pixels are written with the DX10 header as `R16_UNORM` (gray),
//! `R16G16_UNORM` (gray and alpha, or any two channels such as a normal map's X and Y) or
//! `R16G16B16A16_UNORM` (RGB, with alpha 65535, and RGBA). BC4 and BC5 fit their 8-bit endpoints
//! and their indices to the 16-bit values; BC1 to BC3 round the pixels to 8 bits first. The
//! crate has no dependencies; the `parallel` feature encodes and decodes rows of blocks (and mip
//! levels) on several threads with rayon.
//!
@@ -38,7 +44,7 @@ use std::fmt;
use bc::{Block, Mask};
/// The channels of a pixel, 8 bits each, in this order.
/// The channels of a pixel, 8 or 16 bits each, in this order.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Layout {
Gray,
@@ -187,6 +193,8 @@ const DDSCAPS2_CUBEMAP_FACES: u32 = 0xfc00;
const DDSCAPS2_VOLUME: u32 = 0x20_0000;
const DIMENSION_TEXTURE2D: u32 = 3;
const MISC_TEXTURECUBE: u32 = 0x4;
/// The legacy FourCC (a number, not text) of 16-bit RGBA.
const D3DFMT_A16B16G16R16: u32 = 36;
fn bad(reason: impl fmt::Display) -> DdsError {
DdsError::Malformed(reason.to_string())
@@ -242,6 +250,12 @@ impl Field {
let max = u64::from(self.max);
((v * 255 + max / 2) / max) as u8
}
fn get16(self, pixel: u32) -> u16 {
let v = u64::from((pixel >> self.shift) & self.max);
let max = u64::from(self.max);
((v * 65535 + max / 2) / max) as u16
}
}
#[derive(Clone, Copy)]
@@ -255,6 +269,8 @@ enum Encoding {
Bc1,
Bc2,
Bc3,
/// 16 bits per channel, little-endian: gray, gray and alpha (or red and green), or RGBA.
Unorm16 { channels: usize },
/// One value block: gray.
Bc4,
/// Two value blocks: red, then green.
@@ -282,6 +298,8 @@ impl Encoding {
Encoding::Masks { gray: true, fields, .. } if fields[3].is_some() => Layout::GrayA,
Encoding::Masks { gray: true, .. } => Layout::Gray,
Encoding::Masks { fields, .. } if fields[3].is_none() => Layout::Rgb,
Encoding::Unorm16 { channels: 1 } => Layout::Gray,
Encoding::Unorm16 { channels: 2 } => Layout::GrayA,
Encoding::Bc4 => Layout::Gray,
Encoding::Bc5 => Layout::Rgb,
_ => Layout::Rgba,
@@ -291,7 +309,7 @@ impl Encoding {
/// Bytes per 4×4 block; 0 for uncompressed pixels.
fn block_size(&self) -> usize {
match self {
Encoding::Masks { .. } => 0,
Encoding::Masks { .. } | Encoding::Unorm16 { .. } => 0,
Encoding::Bc1 | Encoding::Bc4 => 8,
Encoding::Bc2 | Encoding::Bc3 | Encoding::Bc5 => 16,
}
@@ -301,9 +319,14 @@ impl Encoding {
fn level_layout(&self, width: u32, height: u32, pitch: Option<u32>) -> Result<(usize, usize), DdsError> {
let overflow = || bad("pixel data size overflow");
let (w, h) = (usize::try_from(width).map_err(|_| overflow())?, usize::try_from(height).map_err(|_| overflow())?);
let (row_bytes, rows) = match self {
Encoding::Masks { bytes, .. } => {
let packed = w.checked_mul(*bytes).ok_or_else(overflow)?;
let pixel_bytes = match self {
Encoding::Masks { bytes, .. } => Some(*bytes),
Encoding::Unorm16 { channels } => Some(2 * channels),
_ => None,
};
let (row_bytes, rows) = match pixel_bytes {
Some(bytes) => {
let packed = w.checked_mul(bytes).ok_or_else(overflow)?;
let row_bytes = match pitch {
Some(pitch) => usize::try_from(pitch).map_err(|_| overflow())?,
None => packed,
@@ -313,9 +336,7 @@ impl Encoding {
}
(row_bytes, h)
}
Encoding::Bc1 | Encoding::Bc2 | Encoding::Bc3 | Encoding::Bc4 | Encoding::Bc5 => {
(w.div_ceil(4).checked_mul(self.block_size()).ok_or_else(overflow)?, h.div_ceil(4))
}
None => (w.div_ceil(4).checked_mul(self.block_size()).ok_or_else(overflow)?, h.div_ceil(4)),
};
let size = row_bytes.checked_mul(rows).ok_or_else(overflow)?;
Ok((row_bytes, size))
@@ -327,6 +348,10 @@ fn legacy_encoding(b: &[u8]) -> Result<Encoding, DdsError> {
let (flags, fourcc, bits) = (le32(b, 80)?, le32(b, 84)?, le32(b, 88)?);
let alpha = if flags & DDPF_ALPHAPIXELS != 0 { le32(b, 104)? } else { 0 };
if flags & DDPF_FOURCC != 0 {
// D3DFMT_A16B16G16R16, the legacy name of R16G16B16A16_UNORM (DirectXTex writes it so).
if fourcc == D3DFMT_A16B16G16R16 {
return Ok(Encoding::Unorm16 { channels: 4 });
}
return match &fourcc.to_le_bytes() {
b"DXT1" => Ok(Encoding::Bc1),
b"DXT3" => Ok(Encoding::Bc2),
@@ -364,7 +389,7 @@ fn dx10_encoding(b: &[u8]) -> Result<Encoding, DdsError> {
}
// 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) {
if le32(b, HEADER + 16)? & 0x7 == 2 && matches!(dxgi, 9 | 11 | 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];
@@ -372,6 +397,10 @@ fn dx10_encoding(b: &[u8]) -> Result<Encoding, DdsError> {
const BGRX: [u32; 4] = [0xff_0000, 0xff00, 0xff, 0];
match dxgi {
27..=29 => Encoding::masks(32, RGBA, false),
// Typeless, then UNORM, as for BC4 and BC5 below.
9 | 11 => Ok(Encoding::Unorm16 { channels: 4 }),
33 | 35 => Ok(Encoding::Unorm16 { channels: 2 }),
53 | 56 => Ok(Encoding::Unorm16 { channels: 1 }),
87 | 90 | 91 => Encoding::masks(32, BGRA, false),
88 | 92 | 93 => Encoding::masks(32, BGRX, false),
70..=72 => Ok(Encoding::Bc1),
@@ -382,7 +411,7 @@ fn dx10_encoding(b: &[u8]) -> Result<Encoding, DdsError> {
79 | 80 => Ok(Encoding::Bc4),
82 | 83 => Ok(Encoding::Bc5),
81 | 84 => Err(unsupported(format!("signed (SNORM) BC4 and BC5 textures are not supported (DXGI format {dxgi})"))),
_ => Err(unsupported(format!("DXGI format {dxgi} is not supported (BC1 to BC5 and 8-bit RGBA or BGRA are)"))),
_ => Err(unsupported(format!("DXGI format {dxgi} is not supported (BC1 to BC5, 8-bit RGBA or BGRA and 16-bit R, RG or RGBA are)"))),
}
}
@@ -448,6 +477,65 @@ impl<'a> Texture<'a> {
/// Decode with an explicit per-buffer output byte budget; allocation remains fallible.
pub fn decode_with_limit(&self, max_bytes: u64) -> Result<Vec<u8>, DdsError> {
let encoding = self.encoding;
let (data, row_pitch, total) = self.top_level(1, max_bytes)?;
let (w, channels) = (self.width as usize, encoding.layout().channels());
let row_size = w * channels;
let mut pixels = alloc_zeroed(total)?;
match encoding {
Encoding::Masks { bytes, gray, fields } => decode_masks(Masks { bytes, gray, fields }, data, row_pitch, &mut pixels, row_size),
// Each 16-bit sample rounded to the nearest 8-bit level.
Encoding::Unorm16 { .. } => rows(&mut pixels, row_size, |y, dst| {
let src = data.get(y * row_pitch..).unwrap_or_default();
for (d, s) in dst.iter_mut().zip(src.as_chunks::<2>().0) {
*d = to8(u16::from_le_bytes(*s));
}
}),
Encoding::Bc1 | Encoding::Bc2 | Encoding::Bc3 | Encoding::Bc4 | Encoding::Bc5 => {
decode_blocks(encoding, data, (w, channels), &mut pixels)
}
}
Ok(pixels)
}
/// The top mip level's pixels in [`Texture::layout`] at 16 bits per channel, bounded by
/// [`DEFAULT_MAX_DECODE_BYTES`]. 16-bit formats come back exactly, as do channel masks wider
/// than 8 bits; BC4 and BC5 values are interpolated at 16 bits; every other format is its
/// 8-bit value times 257 (so 255 is 65535).
pub fn decode16(&self) -> Result<Vec<u16>, DdsError> {
self.decode16_with_limit(DEFAULT_MAX_DECODE_BYTES)
}
/// [`Texture::decode16`] with an explicit per-buffer output byte budget (two bytes a sample).
pub fn decode16_with_limit(&self, max_bytes: u64) -> Result<Vec<u16>, DdsError> {
let encoding = self.encoding;
let (data, row_pitch, total) = self.top_level(2, max_bytes)?;
let (w, channels) = (self.width as usize, encoding.layout().channels());
let row_size = w * channels;
let mut pixels = alloc_zeroed(total / 2)?;
match encoding {
Encoding::Masks { bytes, gray, fields } => decode_masks16(Masks { bytes, gray, fields }, data, row_pitch, &mut pixels, row_size),
Encoding::Unorm16 { .. } => rows(&mut pixels, row_size, |y, dst| {
let src = data.get(y * row_pitch..).unwrap_or_default();
for (d, s) in dst.iter_mut().zip(src.as_chunks::<2>().0) {
*d = u16::from_le_bytes(*s);
}
}),
Encoding::Bc4 | Encoding::Bc5 => decode_value_blocks16(encoding, data, (w, channels), &mut pixels),
Encoding::Bc1 | Encoding::Bc2 | Encoding::Bc3 => {
// The limit was checked above for the 16-bit buffer, which is twice this one.
let narrow = self.decode_with_limit(u64::MAX)?;
for (d, v) in pixels.iter_mut().zip(narrow) {
*d = u16::from(v) * 257;
}
}
}
Ok(pixels)
}
/// The top level's stored bytes and row stride, and the decoded size in bytes at
/// `sample_bytes` a sample, checked against `max_bytes` before the stored data is.
fn top_level(&self, sample_bytes: usize, max_bytes: u64) -> Result<(&'a [u8], usize, usize), DdsError> {
let (b, flags, encoding) = (self.bytes, self.flags, self.encoding);
let (width, height) = (self.width, self.height);
// The same header field is a total level size for BC, but a row stride when DDSD_PITCH is set.
@@ -455,23 +543,21 @@ impl<'a> Texture<'a> {
let (row_pitch, size) = encoding.level_layout(width, height, pitch)?;
let (w, h) = (width as usize, height as usize);
let channels = encoding.layout().channels();
let row_size = w.checked_mul(channels).ok_or_else(|| bad("pixel size overflow"))?;
let row_size = w.checked_mul(channels).and_then(|n| n.checked_mul(sample_bytes)).ok_or_else(|| bad("pixel size overflow"))?;
let total = row_size.checked_mul(h).ok_or_else(|| bad("pixel size overflow"))?;
if total as u64 > max_bytes {
return Err(DdsError::LimitExceeded { required: total, limit: max_bytes });
}
let data = b.get(self.start..).and_then(|d| d.get(..size)).ok_or_else(|| bad(format!("truncated pixel data: {size} bytes needed")))?;
let mut pixels = alloc_zeroed(total)?;
match encoding {
Encoding::Masks { bytes, gray, fields } => decode_masks(Masks { bytes, gray, fields }, data, row_pitch, &mut pixels, row_size),
Encoding::Bc1 | Encoding::Bc2 | Encoding::Bc3 | Encoding::Bc4 | Encoding::Bc5 => {
decode_blocks(encoding, data, (w, channels), &mut pixels)
}
}
Ok(pixels)
Ok((data, row_pitch, total))
}
}
/// A 16-bit sample rounded to the nearest 8-bit level.
fn to8(v: u16) -> u8 {
((u32::from(v) + 128) / 257) as u8
}
/// An uncompressed pixel format: bytes per pixel, luminance or RGB, and the red (or
/// luminance), green, blue and alpha fields.
struct Masks {
@@ -531,6 +617,51 @@ fn decode_masks(m: Masks, data: &[u8], row_pitch: usize, pixels: &mut [u8], row_
});
}
/// [`decode_masks`] at 16 bits: every field scaled to 0..=65535, so fields wider than 8 bits
/// (luminance or red and green of 16 bits) keep their precision.
fn decode_masks16(m: Masks, data: &[u8], row_pitch: usize, pixels: &mut [u16], row_size: usize) {
let Masks { bytes, gray, fields } = m;
let channels = if gray { 1 + usize::from(fields[3].is_some()) } else { 3 + usize::from(fields[3].is_some()) };
rows(pixels, row_size, |y, dst_row| {
let src_row = data.get(y * row_pitch..).unwrap_or_default();
for (src, dst) in src_row.chunks_exact(bytes).zip(dst_row.chunks_exact_mut(channels)) {
let pixel = src.iter().rev().fold(0u32, |v, &byte| v << 8 | u32::from(byte));
let channel = |i: usize| fields[i].map_or(0, |f| f.get16(pixel));
let values = if gray { [channel(0), channel(3), 0, 0] } else { [channel(0), channel(1), channel(2), channel(3)] };
dst.copy_from_slice(&values[..channels]);
}
});
}
/// BC4 or BC5 blocks into 16-bit `pixels` of `width` × `channels` samples a row: gray, or red and
/// green with blue 0, interpolated at 16 bits.
fn decode_value_blocks16(encoding: Encoding, data: &[u8], (width, channels): (usize, usize), pixels: &mut [u16]) {
let block_size = encoding.block_size().max(1);
let blocks_x = width.div_ceil(4);
let row_size = width * channels;
rows(pixels, row_size.saturating_mul(4), |by, band| {
let src = data.get(by * blocks_x * block_size..).unwrap_or_default();
for (bx, block) in src.chunks_exact(block_size).take(blocks_x).enumerate() {
let red = bc::decode_values16(block);
let green = if matches!(encoding, Encoding::Bc5) { bc::decode_values16(block.get(8..).unwrap_or_default()) } else { [0; 16] };
let x0 = bx * 4;
for (ty, line) in band.chunks_exact_mut(row_size).enumerate() {
let dst = line.get_mut(x0 * channels..).unwrap_or_default();
for (tx, d) in dst.chunks_exact_mut(channels).take(4).enumerate() {
let t = ty * 4 + tx;
for (c, v) in d.iter_mut().enumerate() {
*v = match c {
0 => red[t],
1 => green[t],
_ => 0,
};
}
}
}
}
});
}
/// Block-compressed data into `pixels` of `width` × `channels` bytes a row, one row of blocks
/// per task. Blocks wholly inside the image copy whole rows of texels; only those on the right
/// edge check each texel.
@@ -593,19 +724,47 @@ const DDSCAPS_TEXTURE: u32 = 0x1000;
const DDSCAPS_MIPMAP: u32 = 0x40_0000;
const DXGI_BC4_UNORM: u32 = 80;
const DXGI_BC5_UNORM: u32 = 83;
const DXGI_R16G16B16A16_UNORM: u32 = 11;
const DXGI_R16G16_UNORM: u32 = 35;
const DXGI_R16_UNORM: u32 = 56;
fn encode_err(reason: impl fmt::Display) -> DdsError {
DdsError::Encode(reason.to_string())
}
/// A zeroed buffer of `len` bytes, or [`DdsError::Alloc`] when it can't be reserved.
fn alloc_zeroed(len: usize) -> Result<Vec<u8>, DdsError> {
/// A zeroed buffer of `len` samples, or [`DdsError::Alloc`] (in bytes) when it can't be reserved.
fn alloc_zeroed<T: Sample>(len: usize) -> Result<Vec<T>, DdsError> {
let mut v = Vec::new();
v.try_reserve_exact(len).map_err(|_| DdsError::Alloc(len))?;
v.resize(len, 0);
v.try_reserve_exact(len).map_err(|_| DdsError::Alloc(len.saturating_mul(size_of::<T>())))?;
v.resize(len, T::default());
Ok(v)
}
/// A sample of 8 or 16 bits: what the mip chain averages and the buffers hold.
trait Sample: Copy + Default + Send + Sync {
fn wide(self) -> u32;
/// An average already within the sample's range.
fn from_average(v: u32) -> Self;
}
impl Sample for u8 {
fn wide(self) -> u32 {
u32::from(self)
}
fn from_average(v: u32) -> u8 {
v as u8
}
}
impl Sample for u16 {
fn wide(self) -> u32 {
u32::from(self)
}
fn from_average(v: u32) -> u16 {
v as u16
}
}
/// How a written texture stores its pixels.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Store {
@@ -622,6 +781,14 @@ enum Store {
Bc3,
Bc4,
Bc5,
/// 16-bit gray (`R16_UNORM`).
R16,
/// 16-bit gray and alpha, or any two channels (`R16G16_UNORM`).
Rg16,
/// 16-bit RGB with alpha 65535 (`R16G16B16A16_UNORM`).
Rgb16,
/// 16-bit RGBA (`R16G16B16A16_UNORM`).
Rgba16,
}
impl Store {
@@ -642,12 +809,24 @@ impl Store {
})
}
/// [`Store::new`] for 16-bit pixels: uncompressed ones keep 16 bits per channel.
fn new16(compression: DdsCompression, layout: Layout) -> Result<Store, DdsError> {
Ok(match (compression, layout) {
(DdsCompression::None, Layout::Gray) => Store::R16,
(DdsCompression::None, Layout::GrayA) => Store::Rg16,
(DdsCompression::None, Layout::Rgb) => Store::Rgb16,
(DdsCompression::None, Layout::Rgba) => Store::Rgba16,
_ => Store::new(compression, layout)?,
})
}
/// Bytes per pixel, or per 4×4 block for a block compression.
fn unit(self) -> usize {
match self {
Store::Luminance => 1,
Store::LuminanceAlpha => 2,
Store::Bgrx | Store::Bgra => 4,
Store::LuminanceAlpha | Store::R16 => 2,
Store::Bgrx | Store::Bgra | Store::Rg16 => 4,
Store::Rgb16 | Store::Rgba16 => 8,
Store::Bc1 | Store::Bc4 => 8,
Store::Bc2 | Store::Bc3 | Store::Bc5 => 16,
}
@@ -662,6 +841,9 @@ impl Store {
match self {
Store::Bc4 => Some(DXGI_BC4_UNORM),
Store::Bc5 => Some(DXGI_BC5_UNORM),
Store::R16 => Some(DXGI_R16_UNORM),
Store::Rg16 => Some(DXGI_R16G16_UNORM),
Store::Rgb16 | Store::Rgba16 => Some(DXGI_R16G16B16A16_UNORM),
_ => None,
}
}
@@ -686,7 +868,7 @@ impl Store {
Store::Bc1 => (DDPF_FOURCC, *b"DXT1", 0, [0; 4]),
Store::Bc2 => (DDPF_FOURCC, *b"DXT3", 0, [0; 4]),
Store::Bc3 => (DDPF_FOURCC, *b"DXT5", 0, [0; 4]),
Store::Bc4 | Store::Bc5 => (DDPF_FOURCC, *b"DX10", 0, [0; 4]),
Store::Bc4 | Store::Bc5 | Store::R16 | Store::Rg16 | Store::Rgb16 | Store::Rgba16 => (DDPF_FOURCC, *b"DX10", 0, [0; 4]),
}
}
}
@@ -708,11 +890,40 @@ pub fn encode(width: u32, height: u32, layout: Layout, pixels: &[u8], options: D
return Err(encode_err("texture dimensions must be nonzero"));
}
let store = Store::new(options.compression, layout)?;
encode_levels((width, height), layout, pixels, store, options.mipmaps, write_level)
}
/// [`encode`] for 16-bit pixels. Uncompressed, they keep 16 bits per channel: gray as
/// `R16_UNORM`, gray and alpha (or any two channels, such as a normal map's X and Y) as
/// `R16G16_UNORM`, RGB (with alpha 65535) and RGBA as `R16G16B16A16_UNORM`, all with the DX10
/// header. BC4 and BC5 pick their endpoints and indices against the 16-bit values; BC1 to BC3
/// round the pixels to 8 bits and write what [`encode`] writes from them.
pub fn encode16(width: u32, height: u32, layout: Layout, pixels: &[u16], options: DdsOptions) -> Result<Vec<u8>, DdsError> {
if width == 0 || height == 0 {
return Err(encode_err("texture dimensions must be nonzero"));
}
let store = Store::new16(options.compression, layout)?;
if matches!(store, Store::Bc1 | Store::Bc2 | Store::Bc3) {
let mut narrow = alloc_zeroed(pixels.len())?;
for (d, &v) in narrow.iter_mut().zip(pixels) {
*d = to8(v);
}
return encode(width, height, layout, &narrow, options);
}
encode_levels((width, height), layout, pixels, store, options.mipmaps, write_level16)
}
/// A level writer: one level of `width` × `height` pixels (in the image's layout) into `dst`.
type WriteLevel<T> = fn(Store, &[T], (usize, usize), &mut [u8]) -> Result<(), DdsError>;
/// The header and every level of `pixels`, each written by `write`.
fn encode_levels<T: Sample>((width, height): (u32, u32), layout: Layout, pixels: &[T], store: Store, mipmaps: bool, write: WriteLevel<T>) -> Result<Vec<u8>, DdsError> {
let expected = (width as usize).checked_mul(height as usize).and_then(|n| n.checked_mul(layout.channels()));
if expected != Some(pixels.len()) {
return Err(encode_err(format!("{} bytes of pixels for a {width}x{height} {layout:?} texture", pixels.len())));
let unit = if size_of::<T>() == 1 { "bytes" } else { "16-bit samples" };
return Err(encode_err(format!("{} {unit} of pixels for a {width}x{height} {layout:?} texture", pixels.len())));
}
let levels = if options.mipmaps { mip_levels(width, height) } else { 1 };
let levels = if mipmaps { mip_levels(width, height) } else { 1 };
let top = (width as usize, height as usize);
// Every level's size, checked before anything is allocated.
let mut sizes = Vec::new();
@@ -724,7 +935,7 @@ pub fn encode(width: u32, height: u32, layout: Layout, pixels: &[u8], options: D
let head = if store.dxgi().is_some() { HEADER + DX10_HEADER } else { HEADER };
let total = sizes.iter().try_fold(head, |total, &s| total.checked_add(s)).ok_or_else(|| encode_err("texture size overflow"))?;
let header = header(width, height, store, levels, sizes.first().copied().unwrap_or(0))?;
let mut out = alloc_zeroed(total)?;
let mut out = alloc_zeroed::<u8>(total)?;
let (Some(dst), Some(src)) = (out.get_mut(..head), header.get(..head)) else {
return Err(encode_err("output buffer too small"));
};
@@ -735,7 +946,7 @@ pub fn encode(width: u32, height: u32, layout: Layout, pixels: &[u8], options: D
let (mut size, mut at) = (top, head);
for (i, &bytes) in sizes.iter().enumerate() {
let dst = out.get_mut(at..at + bytes).ok_or_else(|| encode_err("output buffer too small"))?;
write_level(store, &level, size, dst)?;
write(store, &level, size, dst)?;
at += bytes;
if i + 1 < sizes.len() {
let next = half(size);
@@ -777,7 +988,8 @@ fn header(width: u32, height: u32, store: Store, levels: u32, top: usize) -> Res
}
put(108, DDSCAPS_TEXTURE | if mipmapped { DDSCAPS_COMPLEX | DDSCAPS_MIPMAP } else { 0 });
if let Some(dxgi) = store.dxgi() {
// One 2D texture, no misc flags, alpha mode unknown (the formats have no alpha).
// One 2D texture, no misc flags, alpha mode unknown (straight for the one format with
// alpha, RGBA16, as readers take it).
put(HEADER, dxgi);
put(HEADER + 4, DIMENSION_TEXTURE2D);
put(HEADER + 12, 1);
@@ -788,7 +1000,7 @@ fn header(width: u32, height: u32, store: Store, levels: u32, top: usize) -> Res
/// Runs `f` on each `len`-byte row of `out` with its index, in parallel with the `parallel`
/// feature.
fn rows(out: &mut [u8], len: usize, f: impl Fn(usize, &mut [u8]) + Send + Sync) {
fn rows<T: Send>(out: &mut [T], len: usize, f: impl Fn(usize, &mut [T]) + Send + Sync) {
#[cfg(feature = "parallel")]
{
use rayon::prelude::*;
@@ -804,6 +1016,8 @@ fn rows(out: &mut [u8], len: usize, f: impl Fn(usize, &mut [u8]) + Send + Sync)
fn write_level(store: Store, pixels: &[u8], (width, height): (usize, usize), dst: &mut [u8]) -> Result<(), DdsError> {
let row_bytes = store.row_bytes(width).ok_or_else(|| encode_err("texture size overflow"))?;
match store {
// 16-bit stores are written by `write_level16`.
Store::R16 | Store::Rg16 | Store::Rgb16 | Store::Rgba16 => return Err(encode_err("16-bit storage needs 16-bit pixels")),
Store::Luminance | Store::LuminanceAlpha => {
// The image's gray (and alpha) samples, in the same order.
let src = pixels.get(..dst.len()).ok_or_else(|| encode_err("the level is smaller than its size"))?;
@@ -851,10 +1065,44 @@ fn write_level(store: Store, pixels: &[u8], (width, height): (usize, usize), dst
Ok(())
}
/// [`write_level`] for 16-bit pixels: little-endian samples, or BC4/BC5 blocks fitted to them.
fn write_level16(store: Store, pixels: &[u16], (width, height): (usize, usize), dst: &mut [u8]) -> Result<(), DdsError> {
let row_bytes = store.row_bytes(width).ok_or_else(|| encode_err("texture size overflow"))?;
match store {
Store::R16 | Store::Rg16 | Store::Rgba16 => {
// The image's samples, in the same order.
for (d, s) in dst.as_chunks_mut::<2>().0.iter_mut().zip(pixels) {
*d = s.to_le_bytes();
}
}
Store::Rgb16 => rows(dst, row_bytes, |y, row| {
let src = pixels.get(y * width * 3..).unwrap_or_default();
for (d, s) in row.as_chunks_mut::<8>().0.iter_mut().zip(src.as_chunks::<3>().0) {
let [r, g, b, a] = [s[0], s[1], s[2], u16::MAX].map(u16::to_le_bytes);
*d = [r[0], r[1], g[0], g[1], b[0], b[1], a[0], a[1]];
}
}),
Store::Bc4 | Store::Bc5 => {
let channels = if store == Store::Bc4 { 1 } else { 3 };
rows(dst, row_bytes, |by, row| {
for (bx, d) in row.chunks_exact_mut(store.unit()).enumerate() {
for (c, d) in d.as_chunks_mut::<8>().0.iter_mut().enumerate() {
let (values, inside) = gather_channel(pixels, (width, height), channels, c, (bx, by));
*d = bc::value_block16(&values, inside);
}
}
});
}
// `encode16` rounds BC1 to BC3 to 8 bits; the 8-bit stores take 8-bit pixels.
_ => return Err(encode_err("8-bit storage needs 8-bit pixels")),
}
Ok(())
}
/// Channel `c` of block (`bx`, `by`) of pixels with `channels` channels, and which of its texels
/// lie inside the image.
fn gather_channel(pixels: &[u8], (width, height): (usize, usize), channels: usize, c: usize, (bx, by): (usize, usize)) -> ([u8; 16], Mask) {
let mut values = [0u8; 16];
fn gather_channel<T: Sample>(pixels: &[T], (width, height): (usize, usize), channels: usize, c: usize, (bx, by): (usize, usize)) -> ([T; 16], Mask) {
let mut values = [T::default(); 16];
let mut inside: Mask = 0;
for (t, v) in values.iter_mut().enumerate() {
let (x, y) = (bx * 4 + t % 4, by * 4 + t / 4);
@@ -902,7 +1150,7 @@ fn gather(texels: &[[u8; 4]], (width, height): (usize, usize), bx: usize, by: us
/// The next mip level: each pixel the average of the 2×2 pixels it covers, or of 2×3, 3×2 or
/// 3×3 along the last column and row of an odd-sized level, so no pixel is dropped.
fn downsample(src: &[u8], (w, h): (usize, usize), (nw, nh): (usize, usize), channels: usize) -> Result<Vec<u8>, DdsError> {
fn downsample<T: Sample>(src: &[T], (w, h): (usize, usize), (nw, nh): (usize, usize), channels: usize) -> Result<Vec<T>, DdsError> {
let len = nw.checked_mul(nh).and_then(|n| n.checked_mul(channels)).ok_or_else(|| encode_err("mip level size overflow"))?;
let mut dst = alloc_zeroed(len)?;
let span = |i: usize, n: usize, size: usize| 2 * i..=if i + 1 == n { size.saturating_sub(1) } else { 2 * i + 1 };
@@ -919,8 +1167,8 @@ fn downsample(src: &[u8], (w, h): (usize, usize), (nw, nh): (usize, usize), chan
let interior = nw.saturating_sub(1);
for ((px, a), b) in row.chunks_exact_mut(ch).zip(r0.chunks_exact(2 * ch)).zip(r1.chunks_exact(2 * ch)).take(interior) {
for (c, v) in px.iter_mut().enumerate() {
let sum = u32::from(a[c]) + u32::from(a[ch + c]) + u32::from(b[c]) + u32::from(b[ch + c]);
*v = ((sum + 2) / 4) as u8;
let sum = a[c].wide() + a[ch + c].wide() + b[c].wide() + b[ch + c].wide();
*v = T::from_average((sum + 2) / 4);
}
}
edge = interior;
@@ -932,12 +1180,12 @@ fn downsample(src: &[u8], (w, h): (usize, usize), (nw, nh): (usize, usize), chan
for sy in ys.clone() {
for sx in xs.clone() {
if let Some(&s) = src.get((sy * w + sx) * channels + c) {
sum += u32::from(s);
sum += s.wide();
n += 1;
}
}
}
*v = ((sum + n / 2) / n.max(1)) as u8;
*v = T::from_average((sum + n / 2) / n.max(1));
}
}
});
@@ -980,4 +1228,127 @@ mod tests {
assert!(matches!(texture.decode(), Err(DdsError::Malformed(_))));
assert_eq!(Texture::parse(&file).and_then(|t| t.decode()), Ok(vec![7; 64]));
}
/// Samples spread over the whole 16-bit range, `channels` a pixel.
fn spread16(width: usize, height: usize, channels: usize) -> Vec<u16> {
(0..width * height * channels).map(|i| (i as u32 * 7919 % 65536) as u16).collect()
}
#[test]
fn uncompressed_16_bit_pixels_round_trip_exactly() {
let (w, h) = (7, 5);
for (layout, dxgi, read_as, pixel_bytes) in [(Layout::Gray, 56, Layout::Gray, 2), (Layout::GrayA, 35, Layout::GrayA, 4), (Layout::Rgb, 11, Layout::Rgba, 8), (Layout::Rgba, 11, Layout::Rgba, 8)] {
let pixels = spread16(w, h, layout.channels());
let want: Vec<u16> = if layout == Layout::Rgb { pixels.chunks(3).flat_map(|p| [p[0], p[1], p[2], 65535]).collect() } else { pixels.clone() };
for mipmaps in [false, true] {
let file = encode16(w as u32, h as u32, layout, &pixels, DdsOptions { compression: DdsCompression::None, mipmaps }).unwrap();
assert_eq!(le32(&file, HEADER).unwrap(), dxgi, "{layout:?}");
// 7×5, then 3×2 and 1×1.
let samples = if mipmaps { 7 * 5 + 3 * 2 + 1 } else { 7 * 5 };
assert_eq!(file.len(), HEADER + DX10_HEADER + samples * pixel_bytes);
let texture = Texture::parse(&file).unwrap();
assert_eq!(texture.layout(), read_as);
assert_eq!(texture.decode16().unwrap(), want);
assert_eq!(texture.decode().unwrap(), want.iter().map(|&v| to8(v)).collect::<Vec<_>>());
if layout == Layout::Rgba {
let mut premultiplied = file.clone();
premultiplied[HEADER + 16] = 2;
assert!(matches!(Texture::parse(&premultiplied), Err(DdsError::Unsupported(_))));
}
}
}
}
#[test]
fn mip_levels_of_16_bit_pixels_average_at_16_bits() {
// 1000 to 1003 average to 1002 (rounded), which 8 bits can't hold.
let file = encode16(2, 2, Layout::Gray, &[1000, 1001, 1002, 1003], DdsOptions { compression: DdsCompression::None, mipmaps: true }).unwrap();
assert_eq!(le32(&file, 28).unwrap(), 2);
assert_eq!(file[file.len() - 2..], 1002u16.to_le_bytes());
}
#[test]
fn legacy_16_bit_textures_decode_at_16_bits() {
// D3DFMT_A16B16G16R16: the legacy header's numeric FourCC 36, the same pixels as the
// DX10 file.
let pixels = spread16(3, 2, 4);
let file = encode16(3, 2, Layout::Rgba, &pixels, DdsOptions::default()).unwrap();
let mut legacy = file[..HEADER].to_vec();
legacy[84..88].copy_from_slice(&36u32.to_le_bytes());
legacy.extend_from_slice(&file[HEADER + DX10_HEADER..]);
let texture = Texture::parse(&legacy).unwrap();
assert_eq!((texture.layout(), texture.decode16().unwrap()), (Layout::Rgba, pixels));
// L16: a 16-bit luminance mask keeps its 16 bits through `decode16`.
let mut l16 = encode(2, 1, Layout::Gray, &[0, 0], DdsOptions::default()).unwrap();
l16[20..24].copy_from_slice(&4u32.to_le_bytes());
l16[88..92].copy_from_slice(&16u32.to_le_bytes());
l16[92..96].copy_from_slice(&0xffffu32.to_le_bytes());
l16.truncate(HEADER);
l16.extend([300u16, 65000].iter().flat_map(|v| v.to_le_bytes()));
let texture = Texture::parse(&l16).unwrap();
assert_eq!(texture.decode16().unwrap(), [300, 65000]);
assert_eq!(texture.decode().unwrap(), [1, 253]);
}
#[test]
fn bc4_and_bc5_from_16_bits_beat_the_8_bit_rounded_source() {
// Smooth ramps, about half an 8-bit level across a block, as in height and normal maps.
// 62×61 leaves partial blocks on the right and bottom edges.
let (w, h) = (62usize, 61usize);
let at = |i: usize| (i % w, i / w);
let height: Vec<u16> = (0..w * h).map(|i| (20_000 + at(i).0 * 29 + at(i).1 * 11) as u16).collect();
let normal: Vec<u16> = (0..w * h).flat_map(|i| [(10_000 + at(i).0 * 23 + at(i).1 * 7) as u16, (40_000 + at(i).1 * 27 + at(i).0 * 5) as u16, 65535]).collect();
for (compression, layout, pixels) in [(DdsCompression::Bc4, Layout::Gray, &height), (DdsCompression::Bc5, Layout::Rgb, &normal)] {
let options = DdsOptions { compression, mipmaps: true };
let rounded: Vec<u8> = pixels.iter().map(|&v| to8(v)).collect();
let from16 = Texture::parse(&encode16(w as u32, h as u32, layout, pixels, options).unwrap()).unwrap();
let from8 = Texture::parse(&encode(w as u32, h as u32, layout, &rounded, options).unwrap()).unwrap();
let channels = layout.channels();
// Root-mean-square error of the stored channels (BC5: red and green), in 16-bit units.
let rms = |got: &[u16]| {
let stored = pixels.iter().zip(got).enumerate().filter(|(i, _)| i % channels < 2);
let (sum, n) = stored.fold((0f64, 0f64), |(sum, n), (_, (&a, &b))| (sum + (f64::from(a) - f64::from(b)).powi(2), n + 1.0));
(sum / n).sqrt()
};
let (ours, theirs) = (rms(&from16.decode16().unwrap()), rms(&from8.decode16().unwrap()));
eprintln!("{compression:?}: RMS error {ours:.1} from 16 bits, {theirs:.1} from the 8-bit rounded source (16-bit units)");
assert!(ours * 2.0 < theirs, "{compression:?}: {ours} vs {theirs}");
// `decode` and `decode16` read the same blocks: 8 bits are the 16-bit values rounded,
// give or take the 8-bit palette's own rounding.
for texture in [&from16, &from8] {
let (narrow, wide) = (texture.decode().unwrap(), texture.decode16().unwrap());
assert!(narrow.iter().zip(&wide).all(|(&a, &b)| a.abs_diff(to8(b)) <= 1));
}
}
}
#[test]
fn bc1_to_bc3_from_16_bits_write_what_the_rounded_pixels_write() {
let pixels = spread16(6, 5, 4);
let rounded: Vec<u8> = pixels.iter().map(|&v| to8(v)).collect();
for compression in [DdsCompression::Bc1, DdsCompression::Bc2, DdsCompression::Bc3] {
for mipmaps in [false, true] {
let options = DdsOptions { compression, mipmaps };
assert_eq!(encode16(6, 5, Layout::Rgba, &pixels, options), encode(6, 5, Layout::Rgba, &rounded, options));
}
}
// `encode16` takes the layouts `encode` takes, and checks the buffer size the same way.
for compression in DdsCompression::ALL {
for layout in [Layout::Gray, Layout::GrayA, Layout::Rgb, Layout::Rgba] {
let options = DdsOptions { compression, mipmaps: false };
let n = layout.channels();
assert_eq!(encode16(1, 1, layout, &[9; 4][..n], options).is_ok(), encode(1, 1, layout, &[9; 4][..n], options).is_ok(), "{compression:?} from {layout:?}");
}
}
assert!(matches!(encode16(2, 2, Layout::Rgb, &[0; 11], DdsOptions::default()), Err(DdsError::Encode(_))));
assert!(matches!(encode16(0, 2, Layout::Rgb, &[], DdsOptions::default()), Err(DdsError::Encode(_))));
}
#[test]
fn decode16_checks_its_budget_in_bytes() {
let file = encode16(4, 4, Layout::Gray, &[7; 16], DdsOptions::default()).unwrap();
let texture = Texture::parse(&file).unwrap();
assert!(matches!(texture.decode16_with_limit(31), Err(DdsError::LimitExceeded { required: 32, limit: 31 })));
assert_eq!(texture.decode16_with_limit(32).unwrap(), [7; 16]);
}
}