Commit Graph
6 Commits
Author SHA1 Message Date
FredApps 808df4f186 dds: validate public boundaries and bound fallible decoding 2026-11-16 18:01:22 +01:00
vangel76 39e1d41af7 DDS: refuse premultiplied-alpha DX10 textures
The DX10 header's alpha mode (miscFlags2, low three bits) was never
read, so RGBA, BGRA, BC2 and BC3 files marked premultiplied were read
as straight alpha. They are refused as Unsupported, like the legacy
DXT2/DXT4 FourCCs. Alpha modes 0, 1, 3 and 4 and the formats without
alpha are unchanged.
2026-11-16 01:34:20 +01:00
vangel76 f31c93cf59 DDS: read and write BC4 and BC5 (grayscale data and normal maps)
BC4 is one block of 16 single-channel values and BC5 two of them, the
block BC3 already stores its alpha in, so the alpha block encoder and
decoder now serve all three (BC3's output stays the same).

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

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

DdsCompression is now #[non_exhaustive] and ALL has six entries.
2026-11-16 00:00:28 +01:00
vangel76 203f225562 DDS: stop the principal-axis iteration once the axis is a fixed point
A power-iteration step that gives back the same axis bit for bit gives
it back on every later step, so stopping there keeps the output. Kept
only if the timings show a gain.
2026-11-16 00:00:28 +01:00
vangel76 e22ccf3511 DDS: encode and decode faster with the same bytes
Encoding:
- colour blocks keep their colours and endpoint candidates on the stack
  instead of in per-block vectors (the idea of storytold/photocraft#10669)
- a refinement round whose endpoint pair was already fitted ends the
  search, which could not change the block any more
- a block of one colour skips the principal-axis search, whose result
  for it is that colour at both ends
- the alpha block gathers its 16 values once, finds both ranges in one
  pass and picks indices in a plain loop with the same tie-break
- the nearest palette colour is found the same way
- interior blocks are gathered as four rows of four texels, and
  interior mip pixels average their 2x2 source pixels directly

Decoding:
- block-compressed data is decoded one row of blocks per task, copying
  whole texel rows and checking bounds only on the right edge
- uncompressed rows are decoded one per task; when every channel is a
  whole byte (RGBA, BGRA, BGRX, luminance), pixels are a byte shuffle

The f32 arithmetic and its order are unchanged; the digest oracle pins
the output.
2026-11-16 00:00:28 +01:00
vangel76 a98aa0b462 Move the DDS reader and writer into a craft-dds crate without dependencies
craft-dds holds the header parsing, the uncompressed and BC1-BC3
readers and writers and the block encoder, with its own pixel layout
and error types and rayon behind a `parallel` feature. It parses the
header first (Texture::parse) so a caller can check the size against
its limits before anything is allocated.

craft-image-codecs reads and writes DDS through it, re-exports its
DdsCompression and DdsOptions and maps its errors onto CodecError with
the same messages, so its API and output stay the same. Apps that only
need DDS can depend on craft-dds without the other codecs and their
licences.
2026-11-16 00:00:28 +01:00