JFLib

img_dds#

DDS and BC1..BC7 block compression -- crate root lib/img_dds/lib.rs

img_dds -- DirectDraw Surface reader / writer and the BC1..BC7 block codecs
                        [src/img/dds.s, bc_dec.s, bc_enc.s, bc6h_enc.s, bc7_enc.s]

Modules:
  read     DDS reader: probe, decode (headers, DX10, legacy pixel formats, mipmaps,
           cube maps as a 4 x 3 cross, arrays, volumes)                     [dds.s CD]
  write    DDS writer: encode (legacy / DX10 headers, BC and raw formats, mip chains,
           cube crosses)                                                    [dds.s CB]
  dec      BC1..BC7 block decoders, decode_to / decode                      [bc_dec.s]
  enc      BC1..BC5 block encoders, the surface driver (block rows on the thread pool)
           and the float pixel-set helpers of the BC6H / BC7 encoders       [bc_enc.s]
  bc6h     BC6H block encoder                                               [bc6h_enc.s]
  bc7      BC7 block encoder                                                [bc7_enc.s]
  tables   literal tables of the BC codecs

  CODEC    the codec descriptor (dds_codec): probe, decode, encode

img_dds API#

img.inc (BC / DDS parts)

/// block formats of the BC codecs (BCF_*); 1..7 equal DDSF_BC1..BC7
BCF_BC1: u32 = 1
BCF_BC2: u32 = 2
BCF_BC3: u32 = 3
BCF_BC4: u32 = 4  // BC4 unsigned (ATI1 / BC4U)
BCF_BC5: u32 = 5  // BC5 unsigned (ATI2 / BC5U)
BCF_BC6H: u32 = 6  // BC6H UF16
BCF_BC7: u32 = 7
BCF_BC4S: u32 = 8  // BC4 signed
BCF_BC5S: u32 = 9  // BC5 signed
BCF_BC6HS: u32 = 10  // BC6H SF16
BCF_COUNT: u32 = 11
/// encoder configuration (cfg of encode_block / encode_to)
BCQ_LEVEL: u32 = 3  // mask: 0 default (= normal), BCQ_FAST, BCQ_NORMAL, BCQ_SLOW
BCQ_FAST: u32 = 1  // BC7: mode 6 only; BC1..5: one refinement
BCQ_NORMAL: u32 = 2  // BC7: modes 1, 3, 4, 5, 6, 7
BCQ_SLOW: u32 = 3  // BC7: all modes / rotations / p-bits
BCQ_UNIFORM: u32 = 4  // BC1..BC3 colour: uniform RGB error (else perceptual)
BCQ_ATHR_SHIFT: u32 = 8  // BC1: bits 8..16 = alpha threshold + 1 (0 = default 128)
/// more ImgOpt::ddsfmt values (DDSF_BC1 .. DDSF_R8G8 are in img_core)
DDSF_BC4S: u32 = 17  // BC4 signed (legacy FourCC "BC4S")
DDSF_BC5S: u32 = 18  // BC5 signed ("BC5S")
DDSF_BC6HS: u32 = 19  // BC6H SF16 (DX10)
DDSF_R8: u32 = 20  // DXGI R8_UNORM (grey, DX10)
DDSF_BGR8: u32 = 21  // legacy 24-bit R8G8B8
DDSF_COUNT: u32 = 22
/// more ImgOpt::mipfilter values (0 = MIPF_FANT)
MIPF_FANT: u32 = 5  // area average
MIPF_LANCZOS: u32 = 6  // Lanczos-3
/// ImgOpt::flags: uniform error metric for BC1..BC3 colour
IOF_DDS_UNIFORM: u32 = 0x10000
  • static CODEC: Codec the codec descriptor [dds_codec]

From lib/img_dds/dec.rs (img_dds::dec):

  • fn block_bytes(fmt: u32) -> usize bytes of one block of bcfmt (0 = unknown format) [bc_block_bytes]

  • fn decode_block(fmt: u32, src: &[u8], dst: &mut [u8], stride: usize) -> i32 decode one 4x4 block (8 / 16 bytes at src) into 4 rows of 4 straight BGRA pixels at dst, rows stride bytes apart -> 0 / ERR_UNSUPPORTED [bc_decode_block]

  • fn decode_to(src: &[u8], fmt: u32, px: &mut [u8], stride: usize, w: u32, h: u32, cancel: Option<&AtomicU32>) -> i32 decode ceil(w / 4) x ceil(h / 4) blocks (raster order) into the 32-bit pixels at px (row y at y * stride, w x h pixels) -> 0 / ERR_TRUNC / ERR_CANCEL / ERR_UNSUPPORTED; the cancel flag is polled once per block row [bc_decode_to]

  • fn decode(src: &[u8], w: u32, h: u32, fmt: u32) -> Result<Surface, i32> decode a whole BCn image (blocks in raster order) into a new FMT_ARGB surface (w, h 1..65535, at most IMG_MAXPX_DEF pixels: ERR_DIMS / ERR_LIMIT; ERR_TRUNC, ERR_UNSUPPORTED) [bc_decode]

  • fn bc6h_decode_half(src: &[u8], signed: bool, dst: &mut [u16; 64]) decode one BC6H block (signed: SF16, else UF16) into 16 x (r, g, b, a) IEEE halves (a = 1.0); reserved modes -> 0 [bc6h_decode_half]

From lib/img_dds/enc.rs (img_dds::enc):

impl Scratch

  • fn new() -> Box<Scratch>

  • fn encoded_size(w: u32, h: u32, fmt: u32) -> u64 bytes of ceil(w / 4) x ceil(h / 4) blocks (0 = unknown format) [bc_encoded_size]

  • fn encode_block(fmt: u32, px: &[u8; 64], dst: &mut [u8], cfg: u32) -> i32 encode one block (16 straight BGRA pixels) into 8 / 16 bytes at dst -> 0 / ERR_UNSUPPORTED [bc_encode_block]

  • fn encode_to(sf: &Surface, fmt: u32, cfg: u32, dst: &mut [u8], cancel: Option<&AtomicU32>) -> i32 encode the whole surface into dst (encoded_size bytes, blocks in raster order) -> 0 / ERR_DIMS / ERR_UNSUPPORTED / ERR_CANCEL [bc_encode_to]

  • fn encode(sf: &Surface, fmt: u32, cfg: u32) -> Result<Vec<u8>, i32> encode_to into a new buffer -> Ok(blocks) / Err(ERR_DIMS / ERR_UNSUPPORTED / ERR_NOMEM / ERR_CANCEL) [bc_encode]

From lib/img_dds/read.rs (img_dds::read):

  • fn probe(data: &[u8], meta: &mut ImgMeta) -> bool recognised (meta: size, depth, model, alpha, mips): the header is valid (the pixel format may still be unsupported) [dds_probe]

  • fn decode(data: &[u8], sf: &mut Surface, dec: &ImgDec, meta: &mut ImgMeta) -> i32 decode mip dec.frame (see the module header) [dds_decode]

From lib/img_dds/write.rs (img_dds::write):

  • fn encode(sf: &Surface, opt: &ImgOpt, out: &mut Vec<u8>) -> i32 the DDS file of sf (see the module header) [dds_encode]

img_dds modules#

The codec's building blocks (public for the codec test app codectest; see their source headers):

ModuleContent
img_dds::bc6hBC6H block encoder
img_dds::bc7BC7 block encoder
img_dds::decBC1..BC7 block decoders (DDS reader; verification helper of the encoders)
img_dds::encBCn block encoders BC1..BC5, the surface driver (block rows in parallel on the thread pool) and the float "pixel set" helpers shared with bc7 / bc6h
img_dds::readDirectDraw Surface reader
img_dds::tablesliteral tables of the BC codecs (extracted from the assembly by tools/gen_dds.py)
img_dds::writeDirectDraw Surface writer

Source: docs/API.md in the JFLib repository.