Expand description
Full-spec PNG codec with byte-exact decode parity to libspng’s
SPNG_FMT_RGBA8 path (the blazediff reference decoder configuration:
tRNS applied, adler32 ignored, CRCs unverified) and round-trip-verified
encoding.
- Decode: every format spng accepts — bit depths 1/2/4/8/16, all five color types, palette + tRNS, gray/RGB tRNS color-keying, Adam7 interlacing — decoded to RGBA8 byte-identically to spng, with the same accept/reject behavior on malformed inputs. One deliberate exception: a gray tRNS key is masked to the bit depth as the PNG spec requires, which spng does not do (see the README).
- Decode formats:
decode_withtargets any spngSPNG_FMT_*(RGBA8/RGBA16/RGB8/GA8/GA16/G8/nativePNG/RAW) with optional tRNS / gamma / sBIT transforms, each byte-identical tospng_decode_image— seeDecodeFormat/DecodeOptions. - Encode: all PNG color type / bit depth combinations, optional
Adam7, real deflate levels (libdeflate) plus a stored-block level 0;
verified by
decode(encode(x)) == xand spng cross-decode. - Fast: single-threaded and SIMD-first — whole-buffer libdeflate inflate, in-place sequential defiltering, autovectorizable row expansion, and NEON-accelerated encode-filter kernels.
- Metadata:
decode_with_metadata/encode_with_metadatacapture and emit every ancillary chunk spng exposes (text, color management, timing, physical dimensions, EXIF, suggested palettes, explicit palette/transparency) plus unknown-chunk passthrough — verified field by field againstspng_get_*. The plaindecodestays metadata-free for the pixel hot path.
let bytes = std::fs::read("image.png").unwrap();
let image = blazediff_png::decode(&bytes).unwrap();
let png = blazediff_png::encode(&image, &blazediff_png::EncodeOptions::default()).unwrap();Structs§
- Chrm
- Chromaticities (
cHRM), stored as the rawchrm_intform: each field is the spec’s value × 100000. - Decode
Options - Decode configuration: the output format plus the optional sample
transforms, mirroring spng’s
SPNG_DECODE_TRNS / _GAMMA / _USE_SBIT. - Decoded
- Decoded pixels plus the layout needed to interpret
data. Returned bycrate::decode_with. - Decoded
Png - An image plus all of its non-pixel chunk data, from
decode_with_metadata. - Encode
Options - PNG encode tuning. Mirrors the knobs spng exposes via
spng_set_optionthat the libdeflate backend can honor: - Filter
Set - Bitmask over the five PNG row filters the adaptive heuristic may choose
from, mirroring spng’s
SPNG_IMG_FILTER_CHOICE. The encoder picks the lowest sum-of-absolute-differences filter within the set for each row; a singleton set forces that filter with no scan. Compose with|: - Iccp
- Embedded ICC profile (
iCCP);profileis the decompressed bytes. - Image
- Decoded image: RGBA8, 4 bytes per pixel, row-major.
- Image16
- True 16-bit image: RGBA16 host-order, 4
u16channels per pixel, row-major. The input type forencode16, and the natural pairing withdecode_withatDecodeFormat::Rgba16. - Image
Ref - Borrowed RGBA8 view, the input for the streaming encode entry points
(
encode_ref/encode_to). Lets a caller encode straight from an existing buffer without handing ownership to anImage. - Metadata
- All non-pixel chunk data for one image.
Defaultis “no metadata”. - Offs
- Image offset (
oFFs). - Palette
- Explicit palette (
PLTE) for encoding: entry order is preserved verbatim, overriding the encoder’s first-seen auto-derivation. - Phys
- Physical pixel dimensions (
pHYs). - Sbit
- Significant bits (
sBIT). Only the channels relevant to the image’s color type are meaningful; the rest are zero (matching spng’s struct). - Splt
- Suggested palette (
sPLT). Multiple are allowed per image. - Splt
Entry - One suggested-palette entry (
sPLT). Samples are 16-bit regardless of the chunk’s sample depth (8-bit entries are stored as-is in the low byte). - Text
- A textual annotation (
tEXt/zTXt/iTXt).textis always the decompressed content;language_tag/translated_keywordareiTXtonly (empty otherwise). - Time
- Last-modification time (
tIME), UTC. - Unknown
Chunk - An ancillary chunk the codec does not model, preserved verbatim.
Enums§
- Bkgd
- Background color (
bKGD), tagged by the image’s color type. - Color
Mode - Decode
Format - Output pixel layout for
crate::decode_with, one variant per spngSPNG_FMT_*.Rgba8is the historical default (crate::decode). - Filter
- Location
- Position of an unknown chunk relative to the critical chunks, mirroring
spng’s
enum spng_location. - PngError
- Text
Kind - Which textual chunk a
Textentry came from / is written as. - Trns
- Transparency (
tRNS), tagged by color type.
Functions§
- decode
- Decode a PNG to RGBA8, byte-identical to libspng’s FMT_RGBA8 + tRNS output for every input spng accepts, rejecting the inputs spng rejects. The exception is a gray tRNS key with bits above the bit depth, which is masked per the PNG spec.
- decode_
with - Decode a PNG to an arbitrary
DecodeFormatwith optional tRNS / gamma / sBIT transforms, byte-identical to libspng’sspng_decode_image(fmt, flags).decodeis theRgba8+ tRNS special case. - decode_
with_ metadata - Decode a PNG to RGBA8 and capture every ancillary chunk spng exposes
(text, color management, timing, physical dimensions, EXIF, suggested
palettes, and unknown chunks), with the same accept/reject behavior as
decode. Heavier thandecode— use that when only pixels are needed. - encode
- Encode an RGBA8 image to PNG. The requested color mode must represent
the image losslessly (
decode(encode(x)) == xalways holds);EncodeOptions::defaultauto-picks the smallest lossless mode. - encode16
- Encode a true 16-bit
Image16to PNG, carrying full 16-bit precision (unlikeencode, which byte-replicates an 8-bit source). The color mode must be a 16-bit one —Gray16/GrayAlpha16/Rgb16/Rgba16orAuto;EncodeOptions::defaultauto-picks the smallest lossless 16-bit mode. - encode16_
with_ metadata encode16with caller-supplied ancillary chunks, mirroringencode_with_metadata.- encode_
ref encodefrom a borrowedImageRef— same output, noImageownership required. The hot path (Rgba8+Filter::None+ level 0, non-interlaced, no metadata) is written straight from the borrowed rows with no intermediate raw or zlib buffer.- encode_
to - Stream a PNG encode straight into
outinstead of returning aVec. For the stored RGBA8 hot path this keeps peak memory at roughly the input size: the deflate stored blocks are written directly to the sink with no full-size intermediate buffer. Other modes build aVecand write it through. Wrap aFilein aBufWriter— the stored path issues many small writes. - encode_
with_ metadata - Encode an RGBA8 image to PNG, emitting the supplied metadata chunks in
spec-valid positions. When
meta.paletteis set and the resolved color mode is indexed, that palette (andmeta.transparency) is used verbatim instead of the auto-derived one.