* image-codecs: wrap a clipboard DIB as a BMP (#778)
Add bmp_probe::dib_to_bmp (prepend the 14-byte file header, pixel offset
from the header with saturating arithmetic, try_reserve for the output)
and bmp_probe::dib_layout (V4/V5 alpha-mask BI_RGB becomes BI_BITFIELDS,
mask copy after the header). Both are ported from WordCraft's
crates/geom/src/picture.rs so it can drop its local copies. Tests compare
byte-for-byte with the originals over crafted DIBs and every prefix, and
decode wrapped results.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* image-codecs: fix DIB offsets for V2/V3 bitfields and ambiguous mask copies (#778)
- dib_to_bmp no longer adds 12 appended-mask bytes to the pixel offset for 52- and
56-byte BI_BITFIELDS headers, which carry their masks inside the header; the
decoder reads the pixels at bfOffBits, so the offset was 12 bytes too far.
- dib_layout treats 12 bytes after the header that equal the masks as an existing
copy only when more bytes follow than the pixels need, so an exact-length DIB
whose pixels equal the masks gets its copy inserted.
- dib_layout reserves the 12 bytes before changing compression or masks, so a
failed reservation returns the DIB unchanged; the mask bytes are a [u8; 12]
array, not a heap copy.
The differential tests against the WordCraft originals now exclude exactly the
52/56-byte bitfields offset and the exact-length payload cases, and assert the new
behaviour for them.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
png_probe::pixel_model reads width, height, bit depth, colour type and tRNS
presence from IHDR and the chunk headers before IDAT without inflating iCCP;
PixelModel::bytes_per_pixel and has_alpha give a caller's pixel size and alpha.
webp_probe::has_alpha reads the VP8X alpha flag, the VP8L alpha_is_used bit,
and treats plain VP8 as opaque. Differential tests compare against WordCraft's
png_chunks as an oracle.
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
A conservative pre-decode coefficient budget for hostile JPEG headers: the
larger of JpegInfo::held_coefficients and a whole-picture estimate over every
progressive frame header (SOF2/6/10/14, any precision, sampling clamped to
1..=4), with the costliest frame charged at the first frame's size when the
header can't be followed. held_coefficients is unchanged.
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
craft-testkit's hostile hammer reads CRAFT_HOSTILE_DIVISOR=N: the unmodified
seed, every N-th truncation and 1/N of the rounds. hostile::divisor() gives the
same value to sweeps written without the hammer; craft-image-codecs' TIFF zoo
and malformed-header sweeps use it, with a different offset per input so the
inputs together still reach every position. Proptests without their own case
count read proptest's PROPTEST_CASES.
The Workspace gate and the media, audio, video and PDF workflows set
CRAFT_HOSTILE_DIVISOR=8 and PROPTEST_CASES=64. The longest of these tests
(craft-media's MXF hostile input, 1,310 s on Windows; craft-pdf's crypt
proptests, up to 257 s; the image-codecs sweeps, 140-162 s) drop to about an
eighth or a quarter. nightly.yml runs the whole workspace on Linux without
either variable, so every case still runs once a day.
The gate's test-input setup moves to scripts/ci-test-inputs.sh, shared by both
workflows; it uses a corpus that is already present.
* Expose supported BMP header recognition
Port the BMP routing check from LightCraft commit 304180a500d1b3f87c8c841f269d305161d6c8b9, preserving minimum length and DIB allowlist.
* Test BMP routing boundaries and unsupported DIB sizes
---------
Co-authored-by: Brandon Thomas <echelon@gmail.com>
camera_raw_mode read a missing SubIFDs offset as 0, so it walked the TIFF
header as the pointer array and could classify a malformed TIFF as RAW.
Skip the entry instead, matching LightCraft's classifier.
* image-codecs: read a raster's resolution from its headers (resolution_probe)
file_resolution(bytes) gives the pixels per inch a JPEG, PNG, WebP, BMP or
classic TIFF records without decoding pixels. JPEG uses the decoder's marker
scan (Photoshop, XMP, EXIF, then JFIF), PNG reads pHYs as the decoder does and
falls back to eXIf, WebP its EXIF chunk, TIFF its IFD0 tags; BMP reads the
info header's pixels per metre. Values outside 1-100000 ppi count as none.
DesignCraft (crates/images/src/resolution.rs) and FigCraft
(crates/format/src/density.rs) each carry their own marker and chunk walkers
for this; they can switch to the shared probe.
Part of #778 and #785.
* image-codecs: resolution_probe reads only a whole nine-byte pHYs, and a zero one leaves eXIf
A pHYs chunk that declares any other length, or is cut off before its
CRC, records nothing, and its data is no longer read from the bytes
after it. A zero pixels-per-metre count records nothing either, as in the
decoder, so the eXIf resolution still answers.
A BMP stores its resolution in biXPelsPerMeter and biYPelsPerMeter
(BITMAPINFOHEADER and later). The image crate writes 0 there and nothing
read the fields, so a 300 ppi image saved as BMP reopened at 72 ppi and an
opened BMP's resolution was lost (storytold/photocraft#13630).
decode now reads the fields into Metadata::dpi, encode writes
Metadata::dpi into them whatever embed_metadata says, and
caps(Format::Bmp).dpi is true. encode_bmp_indexed_with_dpi is the palette
writer with a resolution; encode_bmp_indexed keeps its signature and output.
An Exif signature inside a COM segment was taken for the EXIF APP1, and the copy was checked
only against the end of the file, so a replacement longer than the APP1 overwrote the next
marker. Walk the header segments to the first EXIF APP1 and copy only within its declared length.
On panic = "abort" targets (wasm32) the catch_unwind fallback can't run, so
frames whose first component is sampled below the frame's largest factors
go to craft-jpeg before zune-jpeg is called.
zune-jpeg allocates its whole output before reading a scan, so
decode_jpeg_inks now applies decode's scan-data guard (one byte for every
2048 pixels, counted on scan data, not APP padding) first. The comparison
test's noise is computed in u64, so it doesn't overflow on 32-bit targets.
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.
zune-jpeg 0.5 asserts in its upsampler on some subsampled four-component
frames whose first component isn't the largest. decode now catches that
panic and reads the file with craft-jpeg, as it reads arithmetic-coded and
12-bit files. Refs #778.
A jpeg_inks module for apps that keep a CMYK JPEG's inks the way zune-jpeg
readers gave them: every four-component file taken as Adobe-inverted, YCCK
turned into CMYK as libjpeg does (truncating), only the 8-bit Huffman frames
zune-jpeg decodes (arithmetic-coded and 12-bit files are refused), within
Limits. Also the frame's component count and the assembled ICC profile from
the headers alone. Refs #778.
An ignored test beside the encode timings: the best of five decodes of
the same texture written with each compression. It uses only the API
the reader had before, so it runs on the old code for comparison.