mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-30 10:15:00 +01:00
Merge storytold main into next (17 Nov sync)
storytold main 83cb1133dd (291 commits) into the integration head bab1a99b85; 47 conflicted files.
- Domain test files (metafile, prepress, pdf plates, settings store, heif, media-testkit, atomic): tests next cut stay cut; tests new on main are kept, as trials where the crate's test target runs trials.
- atomic: main's group-bits test and Faulty::set_group go to src/tests.rs. audio: main's GSM and SBC oracles join tests/main.rs. cms constructors, pdf knockout_backdrop and testing_pdf join their crates' one test binary; timeline's preflight tests become src/preflight_tests.rs trials.
- update: main's changes to detect, install (linux, macos) and the tests land in next's split modules. The app's own offer is inspected on a thread of its own, picked up by tick's polling; settle_for_tests settles offers too. Later on a ready update drops the close it asked for. updater_pin_tests.rs stays deleted; its new pin test is in tests/it/update.rs.
- craft-color: main added a different crate at crates/color (the colour tags of compositor and display frames); it is next's craft-color module `tags`, re-exported at the root.
- Old crates/ui (combo, docking, testing, README) and keymap's ui/view.rs stay deleted; main's new crates/ui/src/testing files are dropped with them.
- New crates in the craft facade: ai, control, dds, frame, ipc, media-source, palette, press-print, score, ssip. testkit and xtask are listed outside it.
- Cargo.lock: the union of both locks, resolved by cargo metadata. Workflow: main's nextest Windows leg, with next's --keep-going clippy.
This commit is contained in:
commit
ac3dfd1f53
581 files changed
+77998
-3002
No files matched your search
@@ -1,557 +1,60 @@
|
||||
//! DirectDraw Surface (DDS) textures, after Microsoft's public DDS documentation ("DDS",
|
||||
//! "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
|
||||
//! (DXT1/DXT3/DXT5), with or without the DX10 header. Cubemaps, texture arrays and volume
|
||||
//! textures are refused rather than flattened to one face or slice.
|
||||
//!
|
||||
//! Writing: a 2D texture, uncompressed (BGRA or BGRX, luminance with or without alpha) or
|
||||
//! BC1/BC2/BC3, optionally with its full mip chain. The legacy header describes all of these,
|
||||
//! so no DX10 header is written; formats only DXGI can name (BC4–BC7, sRGB) aren't written.
|
||||
//! 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 std::borrow::Cow;
|
||||
|
||||
use super::dds_bc::{self, Block, Mask};
|
||||
use crate::SampleType;
|
||||
use crate::error::CodecError;
|
||||
use crate::fidelity::Plan;
|
||||
use crate::format::Format;
|
||||
use crate::image::{ChannelLayout, Image};
|
||||
use crate::options::{DdsCompression, EncodeOptions, Limits};
|
||||
use crate::options::{EncodeOptions, Limits};
|
||||
use craft_dds::{DdsError, Layout, Texture};
|
||||
|
||||
/// `DDS ` plus the 124-byte DDS_HEADER.
|
||||
const HEADER: usize = 128;
|
||||
/// DDS_HEADER_DXT10, present when the FourCC is `DX10`.
|
||||
const DX10_HEADER: usize = 20;
|
||||
|
||||
const DDSD_DEPTH: u32 = 0x80_0000;
|
||||
const DDPF_ALPHAPIXELS: u32 = 0x1;
|
||||
const DDPF_FOURCC: u32 = 0x4;
|
||||
const DDPF_RGB: u32 = 0x40;
|
||||
const DDPF_LUMINANCE: u32 = 0x2_0000;
|
||||
const DDSCAPS2_CUBEMAP: u32 = 0x200;
|
||||
const DDSCAPS2_CUBEMAP_FACES: u32 = 0xfc00;
|
||||
const DDSCAPS2_VOLUME: u32 = 0x20_0000;
|
||||
const DIMENSION_TEXTURE2D: u32 = 3;
|
||||
const MISC_TEXTURECUBE: u32 = 0x4;
|
||||
|
||||
fn bad(reason: impl std::fmt::Display) -> CodecError {
|
||||
CodecError::malformed(Format::Dds, reason)
|
||||
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")),
|
||||
DdsError::LimitExceeded { required, limit } => {
|
||||
CodecError::LimitExceeded(format!("decoded texture requires {required} bytes, exceeding limit {limit}"))
|
||||
}
|
||||
other => CodecError::malformed(Format::Dds, other),
|
||||
}
|
||||
}
|
||||
fn unsupported(reason: impl Into<String>) -> CodecError {
|
||||
CodecError::unsupported(Format::Dds, reason)
|
||||
}
|
||||
fn le32(b: &[u8], at: usize) -> Result<u32, CodecError> {
|
||||
let bytes = b.get(at..at + 4).and_then(|s| <[u8; 4]>::try_from(s).ok()).ok_or_else(|| bad("truncated header"))?;
|
||||
Ok(u32::from_le_bytes(bytes))
|
||||
|
||||
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> {
|
||||
let depth = le32(bytes, 24)?;
|
||||
let mips = le32(bytes, 28)?;
|
||||
let faces = (le32(bytes, 112)? & DDSCAPS2_CUBEMAP_FACES).count_ones();
|
||||
let dx10 = le32(bytes, 80)? & DDPF_FOURCC != 0 && bytes.get(84..88) == Some(b"DX10");
|
||||
let (cube, array) = if dx10 { (le32(bytes, 136)? & MISC_TEXTURECUBE != 0, le32(bytes, 140)?) } else { (false, 0) };
|
||||
Ok(depth > 1 || mips > 1 || faces > 1 || cube || array > 1)
|
||||
}
|
||||
|
||||
/// A FourCC as text when it is printable, else as the number legacy writers store there.
|
||||
fn fourcc_name(fourcc: u32) -> String {
|
||||
let bytes = fourcc.to_le_bytes();
|
||||
if bytes.iter().all(|c| c.is_ascii_graphic() || *c == b' ') { bytes.iter().map(|&c| char::from(c)).collect() } else { fourcc.to_string() }
|
||||
}
|
||||
|
||||
/// One channel of an uncompressed pixel: where it sits and its largest value.
|
||||
#[derive(Clone, Copy)]
|
||||
struct Field {
|
||||
shift: u32,
|
||||
max: u32,
|
||||
}
|
||||
|
||||
impl Field {
|
||||
/// `None` for an empty mask; an error for one with gaps or wider than the pixel.
|
||||
fn new(mask: u32, bits: u32) -> Result<Option<Field>, CodecError> {
|
||||
if mask == 0 {
|
||||
return Ok(None);
|
||||
}
|
||||
let shift = mask.trailing_zeros();
|
||||
let max = mask >> shift;
|
||||
if max & max.wrapping_add(1) != 0 || (bits < 32 && mask >> bits != 0) {
|
||||
return Err(bad(format!("channel mask {mask:#x} is not a contiguous field of a {bits}-bit pixel")));
|
||||
}
|
||||
Ok(Some(Field { shift, max }))
|
||||
}
|
||||
|
||||
fn get(self, pixel: u32) -> u8 {
|
||||
let v = u64::from((pixel >> self.shift) & self.max);
|
||||
let max = u64::from(self.max);
|
||||
((v * 255 + max / 2) / max) as u8
|
||||
}
|
||||
}
|
||||
|
||||
enum Encoding {
|
||||
/// Uncompressed: bytes per pixel, then red (or luminance), green, blue and alpha.
|
||||
Masks {
|
||||
bytes: usize,
|
||||
gray: bool,
|
||||
fields: [Option<Field>; 4],
|
||||
},
|
||||
Bc1,
|
||||
Bc2,
|
||||
Bc3,
|
||||
}
|
||||
|
||||
impl Encoding {
|
||||
fn masks(bits: u32, masks: [u32; 4], gray: bool) -> Result<Encoding, CodecError> {
|
||||
if !matches!(bits, 8 | 16 | 24 | 32) {
|
||||
return Err(bad(format!("{bits} bits per pixel is not 8, 16, 24 or 32")));
|
||||
}
|
||||
let mut fields = [None; 4];
|
||||
for (field, mask) in fields.iter_mut().zip(masks) {
|
||||
*field = Field::new(mask, bits)?;
|
||||
}
|
||||
let colour = if gray { fields[0].is_some() } else { fields[..3].iter().any(Option::is_some) };
|
||||
if !colour {
|
||||
return Err(bad("the pixel format has no colour channel masks"));
|
||||
}
|
||||
Ok(Encoding::Masks { bytes: bits as usize / 8, gray, fields })
|
||||
}
|
||||
|
||||
fn layout(&self) -> ChannelLayout {
|
||||
match self {
|
||||
Encoding::Masks { gray: true, fields, .. } if fields[3].is_some() => ChannelLayout::GrayA,
|
||||
Encoding::Masks { gray: true, .. } => ChannelLayout::Gray,
|
||||
Encoding::Masks { fields, .. } if fields[3].is_none() => ChannelLayout::Rgb,
|
||||
_ => ChannelLayout::Rgba,
|
||||
}
|
||||
}
|
||||
|
||||
/// Stored row stride and byte size of the top mip level (block rows for BC).
|
||||
fn level_layout(&self, width: u32, height: u32, pitch: Option<u32>) -> Result<(usize, usize), CodecError> {
|
||||
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 row_bytes = match pitch {
|
||||
Some(pitch) => usize::try_from(pitch).map_err(|_| overflow())?,
|
||||
None => packed,
|
||||
};
|
||||
if row_bytes < packed {
|
||||
return Err(bad(format!("row pitch {row_bytes} is smaller than the {packed}-byte pixel row")));
|
||||
}
|
||||
(row_bytes, h)
|
||||
}
|
||||
Encoding::Bc1 | Encoding::Bc2 | Encoding::Bc3 => {
|
||||
let block = if matches!(self, Encoding::Bc1) { 8 } else { 16 };
|
||||
(w.div_ceil(4).checked_mul(block).ok_or_else(overflow)?, h.div_ceil(4))
|
||||
}
|
||||
};
|
||||
let size = row_bytes.checked_mul(rows).ok_or_else(overflow)?;
|
||||
Ok((row_bytes, size))
|
||||
}
|
||||
}
|
||||
|
||||
/// The legacy pixel format: a FourCC, or channel masks.
|
||||
fn legacy_encoding(b: &[u8]) -> Result<Encoding, CodecError> {
|
||||
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 {
|
||||
return match &fourcc.to_le_bytes() {
|
||||
b"DXT1" => Ok(Encoding::Bc1),
|
||||
b"DXT3" => Ok(Encoding::Bc2),
|
||||
b"DXT5" => Ok(Encoding::Bc3),
|
||||
b"DXT2" | b"DXT4" => Err(unsupported("premultiplied-alpha DXT2/DXT4 textures are not supported")),
|
||||
_ => Err(unsupported(format!("compression `{}` is not supported (DXT1, DXT3 and DXT5 are)", fourcc_name(fourcc)))),
|
||||
};
|
||||
}
|
||||
if flags & DDPF_RGB != 0 {
|
||||
return Encoding::masks(bits, [le32(b, 92)?, le32(b, 96)?, le32(b, 100)?, alpha], false);
|
||||
}
|
||||
if flags & DDPF_LUMINANCE != 0 {
|
||||
return Encoding::masks(bits, [le32(b, 92)?, 0, 0, alpha], true);
|
||||
}
|
||||
Err(unsupported(format!("pixel format flags {flags:#x} are not RGB, luminance or a supported compression")))
|
||||
}
|
||||
|
||||
/// The DX10 header's DXGI format, refusing anything but a single 2D texture.
|
||||
fn dx10_encoding(b: &[u8]) -> Result<Encoding, CodecError> {
|
||||
let (dxgi, dimension, misc, array) = (le32(b, HEADER)?, le32(b, HEADER + 4)?, le32(b, HEADER + 8)?, le32(b, HEADER + 12)?);
|
||||
if misc & MISC_TEXTURECUBE != 0 {
|
||||
return Err(unsupported("cubemaps are not supported"));
|
||||
}
|
||||
if dimension != DIMENSION_TEXTURE2D {
|
||||
return Err(unsupported(format!("only 2D textures are supported (resource dimension {dimension})")));
|
||||
}
|
||||
if array > 1 {
|
||||
return Err(unsupported(format!("texture arrays are not supported ({array} textures)")));
|
||||
}
|
||||
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];
|
||||
match dxgi {
|
||||
27..=29 => Encoding::masks(32, RGBA, false),
|
||||
87 | 90 | 91 => Encoding::masks(32, BGRA, false),
|
||||
88 | 92 | 93 => Encoding::masks(32, BGRX, false),
|
||||
70..=72 => Ok(Encoding::Bc1),
|
||||
73..=75 => Ok(Encoding::Bc2),
|
||||
76..=78 => Ok(Encoding::Bc3),
|
||||
_ => Err(unsupported(format!("DXGI format {dxgi} is not supported (BC1, BC2, BC3 and 8-bit RGBA or BGRA are)"))),
|
||||
}
|
||||
craft_dds::has_additional_images(bytes).map_err(error)
|
||||
}
|
||||
|
||||
pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result<Image, CodecError> {
|
||||
if !b.starts_with(b"DDS ") {
|
||||
return Err(bad("missing the `DDS ` signature"));
|
||||
}
|
||||
if le32(b, 4)? != 124 {
|
||||
return Err(bad("header size is not 124"));
|
||||
}
|
||||
let (flags, height, width, depth, caps2) = (le32(b, 8)?, le32(b, 12)?, le32(b, 16)?, le32(b, 24)?, le32(b, 112)?);
|
||||
if caps2 & DDSCAPS2_CUBEMAP != 0 {
|
||||
return Err(unsupported("cubemaps are not supported"));
|
||||
}
|
||||
if caps2 & DDSCAPS2_VOLUME != 0 || (flags & DDSD_DEPTH != 0 && depth > 1) {
|
||||
return Err(unsupported("volume textures are not supported"));
|
||||
}
|
||||
let dx10 = le32(b, 80)? & DDPF_FOURCC != 0 && &le32(b, 84)?.to_le_bytes() == b"DX10";
|
||||
let (encoding, start) = if dx10 { (dx10_encoding(b)?, HEADER + DX10_HEADER) } else { (legacy_encoding(b)?, HEADER) };
|
||||
let layout = encoding.layout();
|
||||
limits.check(width, height, layout, SampleType::U8)?;
|
||||
// The same header field is a total level size for BC, but a row stride when DDSD_PITCH is set.
|
||||
let pitch = if flags & DDSD_PITCH != 0 { Some(le32(b, 20)?) } else { None };
|
||||
let (row_pitch, size) = encoding.level_layout(width, height, pitch)?;
|
||||
let data = b.get(start..).and_then(|d| d.get(..size)).ok_or_else(|| bad(format!("truncated pixel data: {size} bytes needed")))?;
|
||||
let (w, h) = (width as usize, height as usize);
|
||||
let channels = layout.channels();
|
||||
let row_size = w.checked_mul(channels).ok_or_else(|| bad("pixel size overflow"))?;
|
||||
let total = row_size.checked_mul(h).ok_or_else(|| bad("pixel size overflow"))?;
|
||||
let mut pixels = vec![0u8; total];
|
||||
match encoding {
|
||||
Encoding::Masks { bytes, gray, fields } => {
|
||||
for (src_row, dst_row) in data.chunks_exact(row_pitch).zip(pixels.chunks_exact_mut(row_size)) {
|
||||
// Stop at the output width, leaving any stored row padding unread.
|
||||
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.get(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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Encoding::Bc1 | Encoding::Bc2 | Encoding::Bc3 => {
|
||||
let block_size = if matches!(encoding, Encoding::Bc1) { 8 } else { 16 };
|
||||
let blocks_x = w.div_ceil(4);
|
||||
for (i, block) in data.chunks_exact(block_size).enumerate() {
|
||||
let texels = match encoding {
|
||||
Encoding::Bc1 => bc1(block, true),
|
||||
Encoding::Bc2 => with_alpha(bc1(block.get(8..).unwrap_or_default(), false), bc2_alpha(block)),
|
||||
_ => with_alpha(bc1(block.get(8..).unwrap_or_default(), false), bc3_alpha(block)),
|
||||
};
|
||||
let (bx, by) = ((i % blocks_x) * 4, (i / blocks_x) * 4);
|
||||
for (t, texel) in texels.iter().enumerate() {
|
||||
let (x, y) = (bx + t % 4, by + t / 4);
|
||||
if x < w && y < h {
|
||||
let at = (y * w + x) * 4;
|
||||
if let Some(dst) = pixels.get_mut(at..at + 4) {
|
||||
dst.copy_from_slice(texel);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Image::from_u8(width, height, layout, pixels)
|
||||
}
|
||||
|
||||
/// A BC1 colour block. BC2 and BC3 colour blocks always have four colours; only BC1's
|
||||
/// `c0 <= c1` order selects three colours and transparent black.
|
||||
fn bc1(block: &[u8], one_bit_alpha: bool) -> [[u8; 4]; 16] {
|
||||
let byte = |i: usize| block.get(i).copied().unwrap_or(0);
|
||||
let (c0, c1) = (u16::from_le_bytes([byte(0), byte(1)]), u16::from_le_bytes([byte(2), byte(3)]));
|
||||
let palette = dds_bc::colour_palette(c0, c1, c0 > c1 || !one_bit_alpha);
|
||||
let indices = u32::from_le_bytes([byte(4), byte(5), byte(6), byte(7)]);
|
||||
std::array::from_fn(|t| palette[(indices >> (2 * t)) as usize & 3])
|
||||
}
|
||||
|
||||
/// BC2: four explicit bits of alpha per texel.
|
||||
fn bc2_alpha(block: &[u8]) -> [u8; 16] {
|
||||
let bits = u64::from_le_bytes(std::array::from_fn(|i| block.get(i).copied().unwrap_or(0)));
|
||||
std::array::from_fn(|t| ((bits >> (4 * t)) & 0xf) as u8 * 17)
|
||||
}
|
||||
|
||||
/// BC3: two alpha endpoints and a 3-bit index per texel into eight values, or into six plus
|
||||
/// 0 and 255 when `a0 <= a1`.
|
||||
fn bc3_alpha(block: &[u8]) -> [u8; 16] {
|
||||
let byte = |i: usize| block.get(i).copied().unwrap_or(0);
|
||||
let palette = dds_bc::alpha_palette(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])
|
||||
}
|
||||
|
||||
fn with_alpha(mut texels: [[u8; 4]; 16], alpha: [u8; 16]) -> [[u8; 4]; 16] {
|
||||
for (texel, a) in texels.iter_mut().zip(alpha) {
|
||||
texel[3] = a;
|
||||
}
|
||||
texels
|
||||
}
|
||||
|
||||
const DDSD_CAPS: u32 = 0x1;
|
||||
const DDSD_HEIGHT: u32 = 0x2;
|
||||
const DDSD_WIDTH: u32 = 0x4;
|
||||
const DDSD_PITCH: u32 = 0x8;
|
||||
const DDSD_PIXELFORMAT: u32 = 0x1000;
|
||||
const DDSD_MIPMAPCOUNT: u32 = 0x2_0000;
|
||||
const DDSD_LINEARSIZE: u32 = 0x8_0000;
|
||||
const DDSCAPS_COMPLEX: u32 = 0x8;
|
||||
const DDSCAPS_TEXTURE: u32 = 0x1000;
|
||||
const DDSCAPS_MIPMAP: u32 = 0x40_0000;
|
||||
|
||||
fn encode_err(reason: impl std::fmt::Display) -> CodecError {
|
||||
CodecError::encode(Format::Dds, reason)
|
||||
}
|
||||
|
||||
/// How a written texture stores its pixels.
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum Store {
|
||||
/// 8-bit luminance.
|
||||
Luminance,
|
||||
/// 8-bit luminance, then 8-bit alpha.
|
||||
LuminanceAlpha,
|
||||
/// Blue, green, red and an unused byte (written as 255, so a reader that takes it for
|
||||
/// alpha still sees an opaque texture).
|
||||
Bgrx,
|
||||
Bgra,
|
||||
Bc1,
|
||||
Bc2,
|
||||
Bc3,
|
||||
}
|
||||
|
||||
impl Store {
|
||||
/// The storage for an image of `layout`, as [`crate::fidelity::plan`] chose it.
|
||||
fn new(compression: DdsCompression, layout: ChannelLayout) -> Result<Store, CodecError> {
|
||||
Ok(match (compression, layout) {
|
||||
(DdsCompression::None, ChannelLayout::Gray) => Store::Luminance,
|
||||
(DdsCompression::None, ChannelLayout::GrayA) => Store::LuminanceAlpha,
|
||||
(DdsCompression::None, ChannelLayout::Rgb) => Store::Bgrx,
|
||||
(DdsCompression::None, ChannelLayout::Rgba) => Store::Bgra,
|
||||
(DdsCompression::Bc1, ChannelLayout::Rgba) => Store::Bc1,
|
||||
(DdsCompression::Bc2, ChannelLayout::Rgba) => Store::Bc2,
|
||||
(DdsCompression::Bc3, ChannelLayout::Rgba) => Store::Bc3,
|
||||
(_, layout) => return Err(encode_err(format!("{layout:?} pixels can't be written as `{}`", compression.name()))),
|
||||
})
|
||||
}
|
||||
|
||||
/// 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::Bc1 => 8,
|
||||
Store::Bc2 | Store::Bc3 => 16,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_block(self) -> bool {
|
||||
matches!(self, Store::Bc1 | Store::Bc2 | Store::Bc3)
|
||||
}
|
||||
|
||||
/// Bytes of one row of pixels (or of 4×4 blocks) of a level `width` wide.
|
||||
fn row_bytes(self, width: usize) -> Option<usize> {
|
||||
if self.is_block() { width.div_ceil(4).checked_mul(self.unit()) } else { width.checked_mul(self.unit()) }
|
||||
}
|
||||
|
||||
fn level_bytes(self, width: usize, height: usize) -> Option<usize> {
|
||||
let rows = if self.is_block() { height.div_ceil(4) } else { height };
|
||||
self.row_bytes(width)?.checked_mul(rows)
|
||||
}
|
||||
|
||||
/// DDS_PIXELFORMAT: flags, FourCC, bits per pixel and the red, green, blue and alpha masks.
|
||||
fn pixel_format(self) -> (u32, [u8; 4], u32, [u32; 4]) {
|
||||
match self {
|
||||
Store::Luminance => (DDPF_LUMINANCE, [0; 4], 8, [0xff, 0, 0, 0]),
|
||||
Store::LuminanceAlpha => (DDPF_LUMINANCE | DDPF_ALPHAPIXELS, [0; 4], 16, [0xff, 0, 0, 0xff00]),
|
||||
Store::Bgrx => (DDPF_RGB, [0; 4], 32, [0xff_0000, 0xff00, 0xff, 0]),
|
||||
Store::Bgra => (DDPF_RGB | DDPF_ALPHAPIXELS, [0; 4], 32, [0xff_0000, 0xff00, 0xff, 0xff00_0000]),
|
||||
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]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Levels in a full mip chain: the larger side halves down to 1.
|
||||
fn mip_levels(width: u32, height: u32) -> u32 {
|
||||
32 - width.max(height).max(1).leading_zeros()
|
||||
}
|
||||
|
||||
fn half((w, h): (usize, usize)) -> (usize, usize) {
|
||||
((w / 2).max(1), (h / 2).max(1))
|
||||
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_with_limit(limits.max_alloc).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(|| encode_err("the image was not converted to 8 bits per channel"))?;
|
||||
let store = Store::new(opts.dds.compression, img.layout())?;
|
||||
let (width, height) = img.dimensions();
|
||||
let levels = if opts.dds.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();
|
||||
let mut size = top;
|
||||
for _ in 0..levels {
|
||||
sizes.push(store.level_bytes(size.0, size.1).ok_or_else(|| encode_err("texture size overflow"))?);
|
||||
size = half(size);
|
||||
}
|
||||
let total = sizes.iter().try_fold(HEADER, |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 = crate::options::alloc_zeroed::<u8>(total)?;
|
||||
out.get_mut(..HEADER).ok_or_else(|| encode_err("output buffer too small"))?.copy_from_slice(&header);
|
||||
|
||||
let channels = img.layout().channels();
|
||||
let mut level = Cow::Borrowed(pixels);
|
||||
let (mut size, mut at) = (top, HEADER);
|
||||
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)?;
|
||||
at += bytes;
|
||||
if i + 1 < sizes.len() {
|
||||
let next = half(size);
|
||||
level = Cow::Owned(downsample(&level, size, next, channels)?);
|
||||
size = next;
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// The `DDS ` signature and DDS_HEADER. `top` is the top level's size in bytes.
|
||||
fn header(width: u32, height: u32, store: Store, levels: u32, top: usize) -> Result<[u8; HEADER], CodecError> {
|
||||
let mut b = [0u8; HEADER];
|
||||
let mut put = |at: usize, v: u32| {
|
||||
if let Some(d) = b.get_mut(at..at + 4) {
|
||||
d.copy_from_slice(&v.to_le_bytes());
|
||||
}
|
||||
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()))),
|
||||
};
|
||||
// Uncompressed: the pitch of one row. Compressed: the whole top level (its "linear size").
|
||||
let (size_flag, pitch_or_size) = if store.is_block() { (DDSD_LINEARSIZE, Some(top)) } else { (DDSD_PITCH, store.row_bytes(width as usize)) };
|
||||
let pitch_or_size = pitch_or_size
|
||||
.and_then(|v| u32::try_from(v).ok())
|
||||
.ok_or_else(|| encode_err(format!("a {width}x{height} level is too large for a DDS header")))?;
|
||||
let mipmapped = levels > 1;
|
||||
put(4, 124);
|
||||
put(8, DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | size_flag | if mipmapped { DDSD_MIPMAPCOUNT } else { 0 });
|
||||
put(12, height);
|
||||
put(16, width);
|
||||
put(20, pitch_or_size);
|
||||
put(28, levels);
|
||||
let (flags, fourcc, bits, masks) = store.pixel_format();
|
||||
put(76, 32);
|
||||
put(80, flags);
|
||||
put(84, u32::from_le_bytes(fourcc));
|
||||
put(88, bits);
|
||||
for (i, mask) in masks.into_iter().enumerate() {
|
||||
put(92 + 4 * i, mask);
|
||||
}
|
||||
put(108, DDSCAPS_TEXTURE | if mipmapped { DDSCAPS_COMPLEX | DDSCAPS_MIPMAP } else { 0 });
|
||||
b[..4].copy_from_slice(b"DDS ");
|
||||
Ok(b)
|
||||
}
|
||||
|
||||
/// Runs `f` on each `len`-byte row of `out` with its index, in parallel on native targets.
|
||||
fn rows(out: &mut [u8], len: usize, f: impl Fn(usize, &mut [u8]) + Send + Sync) {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
use rayon::prelude::*;
|
||||
out.par_chunks_mut(len.max(1)).enumerate().for_each(|(i, row)| f(i, row));
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
for (i, row) in out.chunks_mut(len.max(1)).enumerate() {
|
||||
f(i, row);
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes one level of `width` × `height` pixels (in the image's layout) into `dst`.
|
||||
fn write_level(store: Store, pixels: &[u8], (width, height): (usize, usize), dst: &mut [u8]) -> Result<(), CodecError> {
|
||||
let row_bytes = store.row_bytes(width).ok_or_else(|| encode_err("texture size overflow"))?;
|
||||
match store {
|
||||
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"))?;
|
||||
dst.copy_from_slice(src);
|
||||
}
|
||||
Store::Bgrx | Store::Bgra => {
|
||||
let channels = if store == Store::Bgra { 4 } else { 3 };
|
||||
rows(dst, row_bytes, |y, row| {
|
||||
let src = pixels.get(y * width * channels..).unwrap_or_default();
|
||||
for (d, s) in row.as_chunks_mut::<4>().0.iter_mut().zip(src.chunks_exact(channels)) {
|
||||
let byte = |i: usize| s.get(i).copied().unwrap_or(255);
|
||||
*d = [byte(2), byte(1), byte(0), byte(3)];
|
||||
}
|
||||
});
|
||||
}
|
||||
Store::Bc1 | Store::Bc2 | Store::Bc3 => {
|
||||
let (texels, _) = pixels.as_chunks::<4>();
|
||||
rows(dst, row_bytes, |by, row| {
|
||||
for (bx, d) in row.chunks_exact_mut(store.unit()).enumerate() {
|
||||
let (block, inside) = gather(texels, (width, height), bx, by);
|
||||
let encoded: &[u8] = match store {
|
||||
Store::Bc1 => &dds_bc::encode_bc1(&block, inside),
|
||||
Store::Bc2 => &dds_bc::encode_bc2(&block, inside),
|
||||
_ => &dds_bc::encode_bc3(&block, inside),
|
||||
};
|
||||
for (d, s) in d.iter_mut().zip(encoded) {
|
||||
*d = *s;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Block (`bx`, `by`)'s texels, and which of them lie inside the image.
|
||||
fn gather(texels: &[[u8; 4]], (width, height): (usize, usize), bx: usize, by: usize) -> (Block, Mask) {
|
||||
let mut block = [[0u8; 4]; 16];
|
||||
let mut inside: Mask = 0;
|
||||
for (t, texel) in block.iter_mut().enumerate() {
|
||||
let (x, y) = (bx * 4 + t % 4, by * 4 + t / 4);
|
||||
if x < width
|
||||
&& y < height
|
||||
&& let Some(p) = texels.get(y * width + x)
|
||||
{
|
||||
*texel = *p;
|
||||
inside |= 1 << t;
|
||||
}
|
||||
}
|
||||
(block, inside)
|
||||
}
|
||||
|
||||
/// 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>, CodecError> {
|
||||
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 = crate::options::alloc_zeroed::<u8>(len)?;
|
||||
let span = |i: usize, n: usize, size: usize| 2 * i..=if i + 1 == n { size.saturating_sub(1) } else { 2 * i + 1 };
|
||||
rows(&mut dst, nw * channels, |y, row| {
|
||||
let ys = span(y, nh, h);
|
||||
for (x, px) in row.chunks_exact_mut(channels.max(1)).enumerate() {
|
||||
let xs = span(x, nw, w);
|
||||
for (c, v) in px.iter_mut().enumerate() {
|
||||
let (mut sum, mut n) = (0u32, 0u32);
|
||||
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);
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
*v = ((sum + n / 2) / n.max(1)) as u8;
|
||||
}
|
||||
}
|
||||
});
|
||||
Ok(dst)
|
||||
let (width, height) = img.dimensions();
|
||||
craft_dds::encode(width, height, layout, pixels, opts.dds).map_err(error)
|
||||
}
|
||||
@@ -1,369 +0,0 @@
|
||||
//! BC1, BC2 and BC3 (DXT1, DXT3, DXT5) blocks, after Microsoft's public block compression
|
||||
//! documentation ("Block Compression (Direct3D 10)" and "Texture Block Compression in Direct3D
|
||||
//! 11"): the palettes the DDS reader decodes with, and the block encoder the DDS writer uses.
|
||||
//!
|
||||
//! 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
|
||||
//! reader rebuilds, so the indices it picks are the best ones for what a decoder shows.
|
||||
|
||||
/// The 16 texels of a 4×4 block, row by row, as RGBA.
|
||||
pub(crate) type Block = [[u8; 4]; 16];
|
||||
|
||||
/// Bit `t` set for each texel `t` of a block that counts: edge blocks of a texture whose size is
|
||||
/// not a multiple of four have texels outside the image.
|
||||
pub(crate) type Mask = u16;
|
||||
|
||||
pub(crate) fn rgb565(c: u16) -> [u8; 3] {
|
||||
let scale = |v: u16, max: u16| ((u32::from(v) * 255 + u32::from(max) / 2) / u32::from(max)) as u8;
|
||||
[scale(c >> 11, 31), scale((c >> 5) & 63, 63), scale(c & 31, 31)]
|
||||
}
|
||||
|
||||
/// The colours a colour block's indices select. Four-colour mode (BC1 with `c0 > c1`, and every
|
||||
/// BC2 and BC3 block) adds two colours at thirds; three-colour mode adds the midpoint and
|
||||
/// transparent black.
|
||||
pub(crate) fn colour_palette(c0: u16, c1: u16, four: bool) -> [[u8; 4]; 4] {
|
||||
let (a, b) = (rgb565(c0), rgb565(c1));
|
||||
let mix = |wa: u32, wb: u32| {
|
||||
let d = wa + wb;
|
||||
let at = |i: usize| ((wa * u32::from(a[i]) + wb * u32::from(b[i]) + d / 2) / d) as u8;
|
||||
[at(0), at(1), at(2), 255]
|
||||
};
|
||||
if four {
|
||||
[[a[0], a[1], a[2], 255], [b[0], b[1], b[2], 255], mix(2, 1), mix(1, 2)]
|
||||
} else {
|
||||
[[a[0], a[1], a[2], 255], [b[0], b[1], b[2], 255], mix(1, 1), [0; 4]]
|
||||
}
|
||||
}
|
||||
|
||||
/// A BC3 alpha block's eight values: six interpolated at sevenths when `a0 > a1`, else four
|
||||
/// interpolated at fifths followed by 0 and 255.
|
||||
pub(crate) fn alpha_palette(a0: u8, a1: u8) -> [u8; 8] {
|
||||
let (a0, a1) = (u32::from(a0), u32::from(a1));
|
||||
let mut palette = [a0, a1, 0, 0, 0, 0, 0, 255];
|
||||
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 u8)
|
||||
}
|
||||
|
||||
/// 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] {
|
||||
let transparent = select(block, inside, |t| t[3] < 128);
|
||||
colour_block(block, inside & !transparent, transparent, true)
|
||||
}
|
||||
|
||||
/// A BC2 block: four bits of alpha per texel, then a four-colour block.
|
||||
pub(crate) fn encode_bc2(block: &Block, inside: Mask) -> [u8; 16] {
|
||||
let bits = block.iter().enumerate().fold(0u64, |bits, (t, texel)| bits | ((u64::from(texel[3]) * 15 + 127) / 255) << (4 * t));
|
||||
join(bits.to_le_bytes(), colour_block(block, inside, 0, false))
|
||||
}
|
||||
|
||||
/// A BC3 block: an interpolated alpha block, then a four-colour block.
|
||||
pub(crate) fn encode_bc3(block: &Block, inside: Mask) -> [u8; 16] {
|
||||
join(alpha_block(block, inside), colour_block(block, inside, 0, false))
|
||||
}
|
||||
|
||||
fn join(a: [u8; 8], b: [u8; 8]) -> [u8; 16] {
|
||||
std::array::from_fn(|i| if i < 8 { a.get(i) } else { b.get(i - 8) }.copied().unwrap_or(0))
|
||||
}
|
||||
|
||||
/// The texels of `inside` for which `f` holds.
|
||||
fn select(block: &Block, inside: Mask, f: impl Fn(&[u8; 4]) -> bool) -> Mask {
|
||||
block.iter().enumerate().filter(|(t, texel)| inside >> t & 1 != 0 && f(texel)).fold(0, |m, (t, _)| m | 1 << t)
|
||||
}
|
||||
|
||||
fn rgb_of(texel: &[u8; 4]) -> [f32; 3] {
|
||||
[f32::from(texel[0]), f32::from(texel[1]), f32::from(texel[2])]
|
||||
}
|
||||
|
||||
/// RGB565 of a colour, rounding each channel to the nearest level.
|
||||
fn to565(c: [f32; 3]) -> u16 {
|
||||
let q = |v: f32, max: f32| (v.clamp(0.0, 255.0) * max / 255.0).round() as u16;
|
||||
q(c[0], 31.0) << 11 | q(c[1], 63.0) << 5 | q(c[2], 31.0)
|
||||
}
|
||||
|
||||
fn distance(a: &[u8; 4], b: &[u8; 4]) -> u32 {
|
||||
(0..3).map(|i| u32::from(a[i].abs_diff(b[i])).pow(2)).sum()
|
||||
}
|
||||
|
||||
/// A colour block (endpoints, then 2-bit indices) for the `opaque` texels' colours, with the
|
||||
/// `transparent` ones on BC1's transparent index. `bc1` allows the three-colour mode; BC2 and
|
||||
/// BC3 always decode four colours.
|
||||
fn colour_block(block: &Block, opaque: Mask, transparent: Mask, bc1: bool) -> [u8; 8] {
|
||||
let colours: Vec<[f32; 3]> = block.iter().enumerate().filter(|(t, _)| opaque >> t & 1 != 0).map(|(_, texel)| rgb_of(texel)).collect();
|
||||
if colours.is_empty() {
|
||||
// Nothing visible: every texel on the transparent index (or index 0 of black).
|
||||
let indices = if transparent != 0 { u32::MAX } else { 0 };
|
||||
return pack(0, 0, indices);
|
||||
}
|
||||
let (lo, hi) = principal_endpoints(&colours);
|
||||
let mut candidates = vec![(to565(lo), to565(hi))];
|
||||
let modes: &[bool] = if transparent != 0 {
|
||||
&[false]
|
||||
} else if bc1 {
|
||||
&[true, false]
|
||||
} else {
|
||||
&[true]
|
||||
};
|
||||
let mut best: Option<(u32, [u8; 8])> = None;
|
||||
// Each refinement starts from the best block so far: its indices give the least-squares
|
||||
// endpoints, which become one more candidate.
|
||||
for round in 0..3 {
|
||||
for &(e0, e1) in &candidates {
|
||||
for &four in modes {
|
||||
let (bytes, error) = fit(block, opaque, transparent, e0, e1, four);
|
||||
if best.is_none_or(|(e, _)| error < e) {
|
||||
best = Some((error, bytes));
|
||||
}
|
||||
}
|
||||
}
|
||||
let Some((error, bytes)) = best else { break };
|
||||
if error == 0 || round == 2 {
|
||||
break;
|
||||
}
|
||||
candidates = least_squares(block, opaque, &bytes).into_iter().collect();
|
||||
if candidates.is_empty() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
best.map_or([0; 8], |(_, bytes)| bytes)
|
||||
}
|
||||
|
||||
/// Orders endpoints `e0`, `e1` for the mode (`c0 > c1` for four colours, `c0 <= c1` for three)
|
||||
/// and picks each texel's nearest palette colour. Returns the block and its squared error.
|
||||
fn fit(block: &Block, opaque: Mask, transparent: Mask, e0: u16, e1: u16, four: bool) -> ([u8; 8], u32) {
|
||||
let (c0, c1) = if four == (e0 >= e1) { (e0, e1) } else { (e1, e0) };
|
||||
// Equal endpoints can't be in four-colour order; the palette is then one colour either way.
|
||||
let palette = colour_palette(c0, c1, four && c0 > c1);
|
||||
let choices = if four && c0 > c1 { 4 } else { 3 };
|
||||
let mut indices = 0u32;
|
||||
let mut error = 0u32;
|
||||
for (t, texel) in block.iter().enumerate() {
|
||||
let index = if transparent >> t & 1 != 0 {
|
||||
3
|
||||
} else if opaque >> t & 1 != 0 {
|
||||
let (i, d) = palette.iter().take(choices).enumerate().map(|(i, p)| (i, distance(texel, p))).min_by_key(|&(_, d)| d).unwrap_or((0, 0));
|
||||
error += d;
|
||||
i as u32
|
||||
} else {
|
||||
0
|
||||
};
|
||||
indices |= index << (2 * t);
|
||||
}
|
||||
(pack(c0, c1, indices), error)
|
||||
}
|
||||
|
||||
fn pack(c0: u16, c1: u16, indices: u32) -> [u8; 8] {
|
||||
let (c0, c1, i) = (c0.to_le_bytes(), c1.to_le_bytes(), indices.to_le_bytes());
|
||||
[c0[0], c0[1], c1[0], c1[1], i[0], i[1], i[2], i[3]]
|
||||
}
|
||||
|
||||
/// The ends of the colours' spread along their principal axis (the covariance's dominant
|
||||
/// eigenvector, by power iteration), clamped to the RGB cube.
|
||||
fn principal_endpoints(colours: &[[f32; 3]]) -> ([f32; 3], [f32; 3]) {
|
||||
let n = colours.len().max(1) as f32;
|
||||
let mean = [0, 1, 2].map(|i| colours.iter().map(|c| c[i]).sum::<f32>() / n);
|
||||
let mut cov = [[0f32; 3]; 3];
|
||||
for c in colours {
|
||||
let d = [c[0] - mean[0], c[1] - mean[1], c[2] - mean[2]];
|
||||
for (i, row) in cov.iter_mut().enumerate() {
|
||||
for (j, v) in row.iter_mut().enumerate() {
|
||||
*v += d[i] * d[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
let times = |a: [f32; 3]| [0, 1, 2].map(|i| cov[i][0] * a[0] + cov[i][1] * a[1] + cov[i][2] * a[2]);
|
||||
let iterate = |start: usize| {
|
||||
let mut axis = [0f32; 3];
|
||||
axis[start] = 1.0;
|
||||
for _ in 0..8 {
|
||||
let next = times(axis);
|
||||
let len = next.iter().map(|v| v * v).sum::<f32>().sqrt();
|
||||
if !len.is_normal() {
|
||||
break;
|
||||
}
|
||||
axis = next.map(|v| v / len);
|
||||
}
|
||||
axis
|
||||
};
|
||||
// From each channel's axis, keeping the one the colours spread along most: a channel that
|
||||
// varies on its own is already an eigenvector, so a single start there would never leave it
|
||||
// even when the others vary more together.
|
||||
let spread = |a: [f32; 3]| times(a).iter().zip(a).map(|(x, y)| x * y).sum::<f32>();
|
||||
let axis = [0, 1, 2].map(iterate).into_iter().max_by(|&a, &b| spread(a).total_cmp(&spread(b))).unwrap_or([1.0, 0.0, 0.0]);
|
||||
let along = |c: &[f32; 3]| (c[0] - mean[0]) * axis[0] + (c[1] - mean[1]) * axis[1] + (c[2] - mean[2]) * axis[2];
|
||||
let (lo, hi) = colours.iter().map(along).fold((f32::MAX, f32::MIN), |(lo, hi), t| (lo.min(t), hi.max(t)));
|
||||
let at = |t: f32| [0, 1, 2].map(|i| (mean[i] + t * axis[i]).clamp(0.0, 255.0));
|
||||
if lo > hi { (mean, mean) } else { (at(lo), at(hi)) }
|
||||
}
|
||||
|
||||
/// The endpoints that minimise the squared error for the indices `bytes` already chose: each
|
||||
/// texel's index fixes its weights between the endpoints, giving 2×2 normal equations per
|
||||
/// channel. `None` when the indices don't constrain both endpoints.
|
||||
fn least_squares(block: &Block, opaque: Mask, bytes: &[u8; 8]) -> Option<(u16, u16)> {
|
||||
let (c0, c1) = (u16::from_le_bytes([bytes[0], bytes[1]]), u16::from_le_bytes([bytes[2], bytes[3]]));
|
||||
let indices = u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]);
|
||||
let four = c0 > c1;
|
||||
// How much of c0 each index holds.
|
||||
let weight = |i: u32| match (four, i) {
|
||||
(_, 0) => Some(1.0f32),
|
||||
(_, 1) => Some(0.0),
|
||||
(true, 2) => Some(2.0 / 3.0),
|
||||
(true, _) => Some(1.0 / 3.0),
|
||||
(false, 2) => Some(0.5),
|
||||
(false, _) => None,
|
||||
};
|
||||
let (mut aa, mut ab, mut bb) = (0f32, 0f32, 0f32);
|
||||
let (mut ax, mut bx) = ([0f32; 3], [0f32; 3]);
|
||||
for (t, texel) in block.iter().enumerate() {
|
||||
if opaque >> t & 1 == 0 {
|
||||
continue;
|
||||
}
|
||||
let Some(a) = weight(indices >> (2 * t) & 3) else { continue };
|
||||
let b = 1.0 - a;
|
||||
let c = rgb_of(texel);
|
||||
aa += a * a;
|
||||
ab += a * b;
|
||||
bb += b * b;
|
||||
for ((x0, x1), v) in ax.iter_mut().zip(bx.iter_mut()).zip(c) {
|
||||
*x0 += a * v;
|
||||
*x1 += b * v;
|
||||
}
|
||||
}
|
||||
let det = aa * bb - ab * ab;
|
||||
if det.abs() < 1e-3 {
|
||||
return None;
|
||||
}
|
||||
let e0 = [0, 1, 2].map(|i| (ax[i] * bb - bx[i] * ab) / det);
|
||||
let e1 = [0, 1, 2].map(|i| (bx[i] * aa - ax[i] * ab) / det);
|
||||
Some((to565(e0), to565(e1)))
|
||||
}
|
||||
|
||||
/// A BC3 alpha block: whichever of the two modes (eight values between the extremes, or six
|
||||
/// between the extremes other than 0 and 255, plus exact 0 and 255) fits better.
|
||||
fn alpha_block(block: &Block, inside: Mask) -> [u8; 8] {
|
||||
let alphas = || block.iter().enumerate().filter(move |(t, _)| inside >> t & 1 != 0).map(|(_, texel)| texel[3]);
|
||||
let range =
|
||||
|values: &mut dyn Iterator<Item = u8>| values.fold(None, |r: Option<(u8, u8)>, a| Some(r.map_or((a, a), |(lo, hi)| (lo.min(a), hi.max(a)))));
|
||||
let (lo, hi) = range(&mut alphas()).unwrap_or((0, 0));
|
||||
// Eight values need a0 > a1; equal ends fall to the six-value mode, which starts with a0 too.
|
||||
let eight = alpha_fit(block, inside, hi, lo);
|
||||
let (mid_lo, mid_hi) = range(&mut alphas().filter(|a| !matches!(a, 0 | 255))).unwrap_or((0, 0));
|
||||
let six = alpha_fit(block, inside, mid_lo, mid_hi);
|
||||
if six.1 < eight.1 { six.0 } else { eight.0 }
|
||||
}
|
||||
|
||||
fn alpha_fit(block: &Block, inside: Mask, a0: u8, a1: u8) -> ([u8; 8], u32) {
|
||||
let palette = alpha_palette(a0, a1);
|
||||
let mut bits = 0u64;
|
||||
let mut error = 0u32;
|
||||
for (t, texel) in block.iter().enumerate() {
|
||||
if inside >> t & 1 == 0 {
|
||||
continue;
|
||||
}
|
||||
let (i, d) = palette.iter().enumerate().map(|(i, &p)| (i, u32::from(p.abs_diff(texel[3])).pow(2))).min_by_key(|&(_, d)| d).unwrap_or((0, 0));
|
||||
error += 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::*;
|
||||
|
||||
fn decode_colour(bytes: &[u8; 8], bc1: bool) -> Block {
|
||||
let (c0, c1) = (u16::from_le_bytes([bytes[0], bytes[1]]), u16::from_le_bytes([bytes[2], bytes[3]]));
|
||||
let palette = colour_palette(c0, c1, c0 > c1 || !bc1);
|
||||
let indices = u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]);
|
||||
std::array::from_fn(|t| palette[(indices >> (2 * t)) as usize & 3])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn solid_blocks_land_on_the_nearest_565_colour() {
|
||||
for colour in [[0, 0, 0, 255], [255, 255, 255, 255], [12, 200, 77, 255]] {
|
||||
let block = [colour; 16];
|
||||
let out = decode_colour(&encode_bc1(&block, u16::MAX), true);
|
||||
for texel in out {
|
||||
assert!(texel.iter().zip(colour).all(|(a, b)| a.abs_diff(b) <= 4), "{texel:?} for {colour:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_colour_blocks_are_exact_when_the_colours_are_565() {
|
||||
let (a, b) = ([255, 0, 0, 255], [0, 0, 255, 255]);
|
||||
let block: Block = std::array::from_fn(|t| if t % 3 == 0 { a } else { b });
|
||||
for bc1 in [true, false] {
|
||||
let bytes = if bc1 { encode_bc1(&block, u16::MAX) } else { colour_block(&block, u16::MAX, 0, false) };
|
||||
assert_eq!(decode_colour(&bytes, bc1), block);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bc1_punches_through_texels_under_half_alpha() {
|
||||
let block: Block = std::array::from_fn(|t| if t < 8 { [200, 100, 50, 255] } else { [9, 9, 9, 127] });
|
||||
let bytes = encode_bc1(&block, u16::MAX);
|
||||
let (c0, c1) = (u16::from_le_bytes([bytes[0], bytes[1]]), u16::from_le_bytes([bytes[2], bytes[3]]));
|
||||
assert!(c0 <= c1, "three-colour mode");
|
||||
let out = decode_colour(&bytes, true);
|
||||
assert!(out[..8].iter().all(|t| t[3] == 255) && out[8..].iter().all(|t| *t == [0; 4]), "{out:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn four_colour_blocks_keep_c0_above_c1() {
|
||||
// A ramp needs all four colours; BC2 and BC3 blocks must not rely on the order, but
|
||||
// older decoders read c0 <= c1 as three colours, so the encoder never writes that.
|
||||
let block: Block = std::array::from_fn(|t| [(t * 16) as u8, 0, 0, 255]);
|
||||
let bytes = colour_block(&block, u16::MAX, 0, false);
|
||||
assert!(u16::from_le_bytes([bytes[0], bytes[1]]) > u16::from_le_bytes([bytes[2], bytes[3]]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_blocks_pick_the_mode_that_keeps_0_and_255() {
|
||||
// 0, 255 and a cluster in between: the six-value mode stores 0 and 255 exactly.
|
||||
let block: Block = std::array::from_fn(|t| [0, 0, 0, [0, 255, 100, 110][t % 4]]);
|
||||
let bytes = alpha_block(&block, u16::MAX);
|
||||
assert!(bytes[0] <= bytes[1], "six-value mode");
|
||||
let palette = alpha_palette(bytes[0], bytes[1]);
|
||||
let bits = u64::from_le_bytes([bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7], 0, 0]);
|
||||
for (t, texel) in block.iter().enumerate() {
|
||||
let got = palette[(bits >> (3 * t)) as usize & 7];
|
||||
assert!(got.abs_diff(texel[3]) <= 2, "texel {t}: {got} for {}", texel[3]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_principal_axis_is_found_when_one_channel_varies_on_its_own() {
|
||||
// Red alternates on its own (variance 64); green and blue alternate together (variance 49
|
||||
// each, 98 along their shared axis), uncorrelated with red. Starting on red alone would
|
||||
// keep red's axis and flatten green and blue; the block must do better than its bounding box.
|
||||
let block: Block = std::array::from_fn(|t| {
|
||||
let gb = if (t / 2) % 2 == 0 { 121 } else { 135 };
|
||||
[if t % 2 == 0 { 120 } else { 136 }, gb, gb, 255]
|
||||
});
|
||||
let error = |bytes: &[u8; 8]| decode_colour(bytes, false).iter().zip(&block).map(|(a, b)| distance(a, b)).sum::<u32>();
|
||||
let (_, bounding_box) = fit(&block, u16::MAX, 0, to565([120.0, 121.0, 121.0]), to565([136.0, 135.0, 135.0]), true);
|
||||
let ours = error(&colour_block(&block, u16::MAX, 0, false));
|
||||
assert!(ours < bounding_box, "{ours} vs the bounding box's {bounding_box}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn texels_outside_the_image_are_ignored() {
|
||||
// Only the top-left texel is inside: its colour alone decides the endpoints.
|
||||
let mut block: Block = [[0, 255, 0, 255]; 16];
|
||||
block[0] = [255, 0, 255, 255];
|
||||
let out = decode_colour(&encode_bc1(&block, 1), true);
|
||||
assert_eq!(out[0], [255, 0, 255, 255]);
|
||||
}
|
||||
}
|
||||
@@ -207,8 +207,10 @@ fn camera_raw_mode(b: &[u8], legacy: bool) -> bool {
|
||||
}
|
||||
TAG_SUB_IFDS if count == 1 => queue.extend(value(e)),
|
||||
TAG_SUB_IFDS => {
|
||||
// More than one offset: they are stored at the entry's offset.
|
||||
let at = u32_at(e.saturating_add(8)).unwrap_or(0) as usize;
|
||||
// More than one offset: they are stored at the entry's offset. A cut-off
|
||||
// offset field means no array, not one at the start of the file.
|
||||
let Some(at) = u32_at(e.saturating_add(8)) else { continue };
|
||||
let at = at as usize;
|
||||
for k in 0..count.min(8) as usize {
|
||||
queue.extend(u32_at(at.saturating_add(4 * k)));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user