Skip to content

zune-image (Rust) ​

A Rust workspace of small, separately published image codecs (zune-jpeg, zune-png, zune-inflate, zune-heic, zune-qoi, zune-ppm, zune-hdr, zune-bmp, zune-psd, zune-farbfeld, a JPEG XL encoder) over one shared core crate, plus a planar Image type and an operations crate.

FieldValue
LanguageRust (root package rust-version = "1.88.0"; crate requirements vary, including 1.89.0 for zune-heic)
LicenseMIT OR Apache-2.0 OR Zlib (zune-image manifest)
Repositoryetemesi254/zune-image
Revision read2f9086b07cdf5a36621ce18f79d310f99b751de9 (October 24, 2026, branch dev; zune-core 0.5.3, zune-jpeg 0.5.16-rc3, zune-png 0.5.2, zune-image 0.5.0)
Documentationdocs.rs/zune-image · docs.rs/zune-jpeg · docs.rs/zune-core · the in-tree docs/ notes
CategoryGeneric libraries: codec family with a shared core and a planar image model
TierA
Last reviewedNovember 15, 2026

IMPORTANT

What was run. Claims marked verified come from a scratch crate built with rustc 1.91.1 against the pinned sources of zune-core, zune-jpeg, zune-png, zune-inflate and zune-image (exported with git archive, outside this repository), run on the workspace's own test-images or generated inputs. Everything else was read in the source at the pinned revision. No performance number in this page was measured here; the ones quoted are the project's own published results and are labelled as such. Verification on November 14 repeated the options, reader, JPEG and corrupt-PNG probes, and added DNL, generated APNG, HDR, one-thread rayon and zero-length Channel probes under /tmp/zune-verifier-20261114/ on x86_64 Linux. These probes were rebuilt from a fresh pinned export and repeated on November 15 under /tmp/zune-verifier-20261115/, with matching results. The scratch crate also depends on zune-hdr; these probes are not CI tests. An independent November 15 pass rebuilt and reran those probes under /tmp/zune-independent-xAwTtpq4/ and added an operation-invariant probe (cargo run --offline --bin invariants in its probe/ directory).


Overview ​

What it solves ​

zune-image exists because its author wanted decoders that are as fast as the C reference libraries without their memory-safety record, and that can be used one at a time. Formats have separately versioned crates, with codec-specific fuzz targets and benchmarks for several of them; a program that needs only JPEG depends on zune-jpeg and zune-core and nothing else. The zune-image crate then ties the codecs into one Image value, a codec registry, metadata (EXIF, ICC, transfer function, gain map), colour and depth conversion and a Pipeline; zune-imageprocs adds about fifty operations (blur, resize, CLAHE, median, bilateral, SSIM, premultiply, transpose and others). Bindings exist for C (zune-capi), Python (zune-python) and WebAssembly (zune-wasm), and zune-bin is a command-line tool.

The format coverage is uneven by design: what the project writes itself it decodes (JPEG including progressive and arithmetic coding, PNG and APNG, HEIC with its own HEVC intra decoder, BMP, QOI, PPM/PFM, Radiance HDR, PSD, farbfeld, and a zune-gif crate not integrated into zune-image). JPEG XL decoding, WebP, JPEG 2000, JPEG encoding, colour management and EXIF parsing are delegated to other crates through zune-image features (jxl-oxide, image-webp, hayro-jpeg2000, jpeg-encoder, moxcms, kamadak-exif) (zune-image/Cargo.toml).

Design philosophy ​

The root README orders the goals explicitly (README.md):

"The set of codecs aim to have the following features in order of priority:

  • Performance: Performance should be on par with or better than reference libraries. For example, zune-jpeg should easily replace libjpeg-turbo without any noticeable speed loss.
  • Safety: Minimal unsafe code, with the sole exception of SIMD intrinsics which currently require unsafe.
  • Robustness: All decoders should be fuzz tested and found bugs fixed promptly."

and states the safety rule as a workspace policy (README.md):

"This workspace allows only 1 type of unsafe: platform specific intrinsics (e.g. SIMD), and only where speed really matters. All other types are explicitly forbidden."

Three consequences shape the code:

  1. Performance comes first, then safety. The defaults pick the fast, lenient path: SIMD on, checksums off, truncated files decoded best-effort (see Hostile input).
  2. Codecs are leaf crates with one shared vocabulary. zune-core holds the byte reader and writer traits, ColorSpace, BitDepth, DecoderOptions and EncoderOptions. Most in-tree codecs depend only on this core; PNG also needs zune-inflate, and HEIC needs zune-isobmff. Most support no_std, but HDR currently requires std (PNG manifest, HEIC manifest, HDR).
  3. Processing wants planes. zune-image stores each channel as its own buffer, because "this library aims to be a processing library" (deinterleave.rs); the image layer splits interleaved multi-component codec output into planes.

The safety rule is a goal more than an invariant. Several codec crates enforce it with #![forbid(unsafe_code)] (zune-ppm, zune-hdr, zune-jpegxl, zune-isobmff; zune-jpeg when built without its x86 and neon features; zune-heic uses deny), but the zune-image crate's Channel is a hand-written untyped allocation and the parallel region helpers build slices from raw pointers (see Allocation and ownership). Neither is a SIMD intrinsic. HEIC also explicitly allows unsafe for its VideoToolbox FFI (lib.rs).


How it works ​

Workspace layout ​

CrateRole
zune-coreZByteReaderTrait, ZReader, ZCursor, ZByteWriterTrait, ColorSpace, BitDepth, DecoderOptions, log
zune-inflateDEFLATE/zlib/gzip decoder (one-shot and streaming); DEFLATE/zlib encoder
zune-jpegbaseline, progressive and (feature arith) arithmetic JPEG; scanline, raw-plane and incremental APIs; AVX2 / NEON
zune-pngPNG and APNG decoding over streaming inflate; experimental encoder; SSE4 / NEON filters
zune-heicHEIF container (via zune-isobmff) and its own HEVC intra decoder; SSE4.1 / NEON transforms; VideoToolbox
zune-qoi …zune-qoi, zune-ppm, zune-hdr, zune-bmp, zune-psd, zune-farbfeld, zune-gif (0.5.0-rc0, not wired)
zune-jpegxla JPEG XL encoder only; decoding goes through jxl-oxide
zune-imageImage / Frame / Channel, ImageMetadata, DecoderTrait, EncoderTrait, OperationsTrait, registry
zune-imageprocsthe operations, each an OperationsTrait implementation

The byte reader ​

The in-tree decoders are generic over T: ZByteReaderTrait. The trait is the minimum a decoder needs from its input (traits.rs):

rust
// crates/zune-core/src/bytestream/traits.rs (abridged)
pub trait ZByteReaderTrait {
    fn read_byte_no_error(&mut self) -> u8;
    fn read_exact_bytes(&mut self, buf: &mut [u8]) -> Result<(), ZByteIoError>;
    fn read_bytes(&mut self, buf: &mut [u8]) -> Result<usize, ZByteIoError>;
    fn peek_bytes(&mut self, buf: &mut [u8]) -> Result<usize, ZByteIoError>;
    fn peek_exact_bytes(&mut self, buf: &mut [u8]) -> Result<(), ZByteIoError>;
    fn z_seek(&mut self, from: ZSeekFrom) -> Result<u64, ZByteIoError>;
    fn is_eof(&mut self) -> Result<bool, ZByteIoError>;
    fn z_position(&mut self) -> Result<u64, ZByteIoError>;
    fn read_remaining(&mut self, sink: &mut alloc::vec::Vec<u8>) -> Result<usize, ZByteIoError>;
}

It has two implementations: ZCursor<T: AsRef<[u8]>> for memory (works under no_std) and a blanket implementation for any std::io::BufRead + Seek. The trait documentation records the cost of having only those two: without specialisation the blanket implementation cannot be overridden, so "functions like read_byte_no_error are slower than they should be for Cursor". ZReader<T> wraps a source and adds endian-aware reads, skip, rewind and peek_at (reader.rs).

Two contracts carry the rest of the design. read_byte_no_error returns 0 past the end instead of failing (no_std_readers.rs), which keeps the entropy decoders' hot loops branch-light. An exact read that reports NotEnoughBytes rewinds, making that error retryable (reader.rs); this guarantee does not cover every I/O error:

"Retry contract: on NotEnoughBytes, the stream position is already rewound to where the failed read began, so the caller can append more data and retry the same operation without repositioning."

Verified: over ZCursor::new([7u8]), read_u8 returns 7 then 0, and read_u8_err then returns an error. A custom BufRead + Seek reader that yields one byte and then std::io::ErrorKind::UnexpectedEof reports recoverable EOF but remains at byte 1: the blanket implementation forwards that I/O error without rewinding (std_readers.rs).

Options as the policy carrier ​

DecoderOptions is one Copy struct shared by the in-tree decoders (decoder.rs): max_width, max_height (default 1 << 14 each), max_scans (progressive JPEG, default 100), deflate_limit (default 1 << 30), hevc_max_mdat_size (default 1 << 24), num_threads, endianness and a private DecoderFlags bag of booleans (checksums, strictness, per-ISA SIMD switches, PNG alpha and 16-to-8 stripping, animated decoding, VideoToolbox). Some options' documentation names the decoders that respect them ("Respected by: jpeg"). The use_avx2 style getters fold the user's flag, the unsafe master switch and CPU detection into one answer (decoder.rs): is_x86_feature_detected! under std, the compile-time target_feature otherwise.

The image model ​

zune-image stores an image as nested owned buffers (image.rs, frame.rs):

rust
// crates/zune-image/src/image.rs and frame.rs
pub struct Image {
    pub(crate) background_color: Color,
    pub(crate) frames: Vec<Frame>,       // more than one: animated
    pub(crate) metadata: ImageMetadata,  // width, height, colorspace, depth, ...
}
pub struct Frame {
    pub(crate) channels: Vec<Channel>,   // one plane per component
    pub(crate) numerator: usize,         // frame duration as a fraction
    pub(crate) denominator: usize,
}

A Channel is an untyped, 64-byte-aligned byte allocation that remembers the TypeId it was created for (channel.rs):

rust
// crates/zune-image/src/channel.rs
pub struct Channel {
    ptr: *mut u8,
    length: usize,     // in bytes, not elements
    capacity: usize,
    type_id: TypeId,   // u8, u16 or f32 in practice
    size_of: usize,
    layout: Layout,
}

The module header calls it "analogous to C/C++ void * but comes with some safety measures". reinterpret_as::<T>() and reinterpret_as_mut::<T>() check the stored TypeId and the alignment, then hand out &[T] or &mut [T] (channel.rs). The intended plane size is width * height samples, with no stride, offset or sub-view. confirm_invariants checks the channel count and non-alpha plane lengths, but passes true to channels_ref and therefore skips alpha-plane lengths (traits.rs, frame.rs). Verified: a no-op operation's execute accepts a 2×2 RGBA image with plane lengths [4, 4, 4, 1]; shortening the red plane instead produces a dimension-mismatch error.

Operations ​

An operation implements OperationsTrait (traits.rs): a name, supported_colorspaces, supported_types (a slice of BitType) and execute_impl. The provided execute checks the colour space and bit type against those lists, runs confirm_invariants, calls execute_impl, and checks the invariants again. Inside, an operation matches the run-time bit type and monomorphises its kernel; Brighten is typical (brighten.rs):

rust
// crates/zune-imageprocs/src/brighten.rs (abridged)
match depth.bit_type() {
    BitType::U8 => image.par_process_regions::<u8, _>(true, |region| {
        for channel in region.channels.iter_mut() {
            brighten(channel, self.value, /* max */ 255);
        }
    })?,
    BitType::U16 => image.par_process_regions::<u16, _>(true, |region| { /* ... */ })?,
    BitType::F32 => image.par_process_regions::<f32, _>(true, |region| { /* ... */ })?,
    _ => return Err(ImageErrors::ImageOperationNotImplemented(self.name(), depth.bit_type())),
}

par_process_regions cuts each frame into horizontal bands of whole rows (about four bands per rayon thread) and gives each band a PlanarRegionMut holding one &mut [T] per channel (planar_regions.rs). Because the planes are separate and unpadded, a point operation is a loop over a plain slice, and premultiplication is a zip of a colour plane with the alpha plane (premul_alpha.rs).

Codec entry points ​

Decoders follow a naming convention rather than a common trait: new(source), new_with_options(source, options), decode_headers() and an allocating decode(). The exact surface and sample type vary: JPEG and PNG expose byte-buffer decoding, HDR's decode_into takes &mut [f32] (HDR decoder), farbfeld's takes &mut [u16] (farbfeld decoder), and PPM's decode returns a tagged DecodingResult (PPM decoder). zune-image adapts each into DecoderTrait (decode() -> Result<Image, ImageErrors>, read_headers, dimensions), and a process-wide RwLocked registry maps an ImageFormat (including ImageFormat::Custom(&str)) to a factory (codecs.rs).

zune-jpeg goes further than the shared shape. It offers a pull-style scanline session that writes converted rows into caller storage with a caller-chosen row stride (lib.rs); a raw session whose layout() returns one PlaneInfo per component with sampling factors, logical size, stride and byte_size, and decode_into_planes_strided for subsampled YCbCr planes (decoder.rs, decoder.rs); an incremental mode (I/O and codec architecture); and cooperative cancellation through a CancelCheck trait with a default polling interval of 1,024 MCUs (also checked at header boundaries) (cancel.rs).


Analysis ​

1. Pixel and memory model ​

Two models coexist. The codecs hand out interleaved samples, as byte buffers or typed arrays depending on the codec. JPEG can select an output ColorSpace (RGB, RGBA, BGRA, Luma, YCbCr, CMYK, …); PNG's byte output supports a configurable 16-bit byte order. zune-image holds planar images (concepts: interleaved, planar and semi-planar): Image → Vec<Frame> → Vec<Channel>, each channel a dense width * height array of u8, u16 or f32. Multi-component interleaved output must be deinterleaved (SSE2, SSE4.1 and AVX2 kernels in deinterleave/), and Frame::flatten re-interleaves for encoding. The delegated JPEG XL adapter instead copies already-planar f32 channels (jpeg_xl.rs).

What the planar model leaves out is as instructive as what it has:

  • No stride and no views. A plane is exactly width * height samples; a crop is a copy (crop.rs), and nothing borrows a window of a larger image. Contrast mir's Slice (mir-algorithm) and Boost.GIL's views (Boost.GIL).
  • No subsampled planes. All channels share one size, so 4:2:0 YCbCr cannot be represented; ColorSpace::YCbCr means full-resolution planes. zune-jpeg's own raw API, by contrast, returns subsampled planes with explicit strides and 8-sample padding (PlaneInfo), so the codec layer has the richer model and the image layer discards it.
  • No N-D extent. ColorSpace::MultiBand(NonZeroU32) allows any number of channels (the README's "up to 4 billion bands"), but an image stays 2-D. Many operations assume at most four (MAX_CHANNELS = 4); the region helpers assert at most 16 (planar_regions.rs).
  • Depth is three storage cases. BitDepth is Eight, Sixteen, Float32 or Unknown (bit_depth.rs); there is no separate 10- or 12-bit tag, and sub-byte PNG expands to u8. Float32 is documented as "range is 0.0 to 1.0", but the HDR decoder can return values above 1.0 (decoder.rs).

Planes are 64-byte aligned (MIN_ALIGNMENT, chosen "for AVX-512" (channel.rs)), so every kernel may assume an aligned start; with no padding, row starts after the first need not retain that alignment.

2. Genericity mechanism ​

Genericity is run-time tags plus monomorphised kernels. The element type is not a type parameter of Image: it is BitDepth in ImageMetadata, checked against the TypeId in each Channel. Each operation matches on BitType and instantiates a generic kernel for u8, u16 and f32, bounded by ZuneInts<T> (a depth() and max_value() per sample type) and bytemuck::Pod. Capability is declared as data: supported_colorspaces() and supported_types() return static slices, and execute refuses an image outside them, except that a MultiBand image is let through with a warning and treated as colour without alpha (traits.rs).

Codecs are generic over the reader (JpegDecoder<T: ZByteReaderTrait>), so concrete in-memory and stream readers can compile separately without virtual I/O calls. Encoders take T: ZByteWriterTrait. At the zune-image boundary everything is erased again: the registry boxes its reader as dyn BoxableReader and returns Box<dyn DecoderTrait> (codecs.rs). Pipeline instead stores Box<dyn IntoImage> and Box<dyn OperationsTrait> (pipelines.rs), erasing the input and operation types.

Some declared capability is unused. OperationsTrait::alpha_state() documents that it lets an operation choose the alpha state "the image will be converted into before carrying out an operation" (traits.rs), but the provided execute does not read it; operation_color_values() (gamma versus linear) is likewise declared by operations such as Brighten and never consulted by execute. A search of the pinned Rust sources finds hook definitions and overrides but no calls, including in the bindings; this does not establish how external consumers use the public trait.

3. Colour and metadata ​

zune-core has the vocabulary: ColorSpace (component order and model, including HSL, HSV, YCCK and MultiBand), ColorCharacteristics (sRGB, Rec.709, gamma 2.2/2.8, linear, PQ, HLG and other CICP transfer characteristics, with an Unknown(u8) fallback), ColorPrimaries and RenderingIntent (colorspace.rs, colorspace.rs). ImageMetadata carries the per-image state (metadata.rs): color_trc, default_gamma, is_linear, color_primaries (from cHRM), color_standard (the CICP primaries code), max_cll, icc_chunk, exif (parsed by kamadak-exif), an AlphaState (premultiplied or not) and a GainMapImage field. That field defaults to None; the JPEG adapter's Ultra HDR path decodes and applies the gain map rather than assigning it to the field (jpeg.rs).

The mapping from files is partial. The PNG adapter keeps the CICP transfer function and primaries but drops matrix_coefficients and video_full_range_flag, which zune-png's CicpInfo does read (png.rs, decoder.rs); see concepts: CICP. XMP and IPTC are exposed by zune-jpeg (xmp(), iptc()) but not stored in ImageMetadata. No colour management happens in the core: ICC profiles travel as bytes, and conversion between profiles is the cms feature of zune-imageprocs over moxcms. Ultra HDR handling (uhdr feature) comes from gainforge. The colour conversions in core_filters/colorspace/ work on code values; the HSL and HSV ones are documented as matching Python's colorsys. Compare the typed colour model in color (TurkeyMan) and JuliaImages.

4. I/O and codec architecture ​

The baseline is pull over a seekable reader: the decoder calls read_* and z_seek on its source. Since 0.5 that source may be a file through BufReader rather than a whole-file slice; the migration notes say the earlier design, which "accepted byte slices that represent the whole data as contiguous", was "not ideal ... increasing memory e.g on massive files" (lib.rs). Seeking is required, so a forward-only stream must be buffered.

On top of the pull reader, zune-jpeg builds retry-based incremental decoding (concepts: incremental, progressive and suspendable decoding), documented in its crate root (lib.rs):

"JpegDecoder can be retried on the same decoder when the underlying reader can see more bytes later. Call set_incremental_mode(true) before scan decoding to make scan EOF recoverable in non-strict mode. Callers should treat DecodeErrors::is_recoverable_eof() as the signal to feed more input and retry; any other error is a hard decode failure."

Suspension is by checkpoint, not by coroutine: baseline Huffman scans record "stream position, next MCU row/column, restart countdown, SOS parameters, DC predictors, and bitstream state", and a retry resumes from the last checkpoint. decoded_scanlines() reports the stable prefix of the caller's buffer; progressive images instead report a replaceable full-frame preview built from committed scans. Verified: on a 40% prefix (509,687 of 1,274,218 bytes) of the 7680×4320 baseline speed_bench.jpg, incremental decode_into returned a recoverable EOF with decoded_scanlines() == Some(1864) of 4320. The project's own Darwin benchmark reports 47.58 ms with incremental mode against 84.38 ms without (benchmark-results.md). Its "first retry" benchmark measures decoder setup, headers, output allocation, an initial decode at 80% visibility and a retry with all input visible, using a different fixture, speed_bench_hv_subsampling.jpg (benchmark code). These are combined times, not isolated retry latency.

zune-png decodes IDAT through zune_inflate::StreamingDecoder row by row into the output, so the compressed stream is never concatenated (streaming.rs); it has no comparable public retry contract. The other decoders are whole-image. Only zune-jpeg has the scanline and cancellation APIs; they are not part of DecoderTrait, so zune-image users get whole-image decodes. The project's guide to adding a decoder is in docs/AddingNewDecoder.md. Wuffs is the comparison point for suspendable decoding in this catalog (wave 3).

5. Hostile input and safety ​

The hostile-input defences are memory safety by language, explicit limits, and fuzzing.

Limits. The in-tree decoders check declared max_width and max_height before allocating pixel output (zune-jpeg headers.rs, zune-pngreaders.rs, and the BMP, GIF, HDR, HEIC, PPM, PSD, QOI and farbfeld decoders). The limits are per axis, not per pixel or per byte: at the defaults a legal header may request 16384 × 16384 × 4 samples, 1 GiB at 8 bits or 4 GiB as f32 in zune-image. Verified: with set_max_width(100) the 7680-wide JPEG fails in decode_headers with "Image width 7680 greater than width limit 100". Other budgets are format-specific: max_scans for progressive and multi-scan JPEG (mcu.rs), deflate_limit for text and ICC chunks, hevc_max_mdat_size because the mdat box is read into memory. A JPEG with a DNL marker (height unknown until entropy decoding) makes decode() allocate for max_height rows up front (decoder.rs). Verified: the bundled 8192-wide dnl_image.jpg returned 184,909,824 bytes, but retained capacities of 196,608,000 and 245,760,000 bytes with max_height set to 8000 and 10000 respectively. APNG has no configurable frame-count budget; zune-image composites each frame into a full-canvas copy (png.rs), so retained pixel storage grows as frames × canvas. Verified: generated 1×1 8-bit greyscale APNGs with 2, 20 and 200 frames retained 2, 20 and 200 plane bytes respectively, in addition to frame and channel overhead.

Leniency is the default. DecoderOptions::default() uses decoder_error_tolerance_mode(), which returns fast_options(): checksums off, non-conformance tolerated, SIMD flags enabled (decoder.rs, decoder.rs). A truncated JPEG decodes "with best-effort output" unless strict or incremental mode is set. Verified: the 40% prefix above decodes with decode() to Ok with a full 99,532,800-byte buffer by default, and fails with a recoverable EOF under set_strict_mode(true).

Documentation and behaviour disagree in three places. All three were verified against the pinned sources:

  • DecoderOptions::new_safe() is documented as the variant that "disables platform specific intrinsics" (decoder.rs), but it returns default(), whose use_unsafe() and use_avx2() were true on this AVX2 host.
  • png_set_confirm_crc(true) has no effect on decoding: a PNG with a corrupted IHDR CRC and one with a corrupted IDAT CRC both decode successfully with it set. calc_crc is called only from the encoder's chunk writers (crc.rs).
  • inflate_set_confirm_adler(true) does not reach image data: with a corrupted zlib Adler-32 the PNG still decodes. The check exists only in the one-shot DeflateDecoder (decoder.rs), which zune-png uses for iCCP and zTXt; those paths use DeflateOptions' own default checksum policy without consulting this flag (readers.rs). IDAT uses the streaming decoder.

strict_mode() ORs jpg_error_on_non_conformance, png_confirm_crc and inflate_confirm_adler (decoder.rs), so in PNG it means less than it says. On the unsafe side, set_use_unsafe(false) disables option-controlled dispatch such as JPEG's IDCT. It is not a workspace-wide SIMD prohibition: PNG's NEON Sub/Average filters and HEIC's transform dispatch detect CPU features directly (de_filter.rs, idct.rs). zune-jpeg without its x86 and neon features is forbid(unsafe_code) (lib.rs). Counting matching lines with git grep unsafe 2f9086b07 -- crates/zune-jpeg/src | wc -l gives 101; the corresponding counts are 78 for zune-image/src, 55 for zune-imageprocs/src and 25 for zune-core/src, and zero for zune-inflate, zune-qoi and zune-bmp. These include comments and identifiers such as use_unsafe; they do not count unsafe blocks or measure the size of the trusted code.

Fuzzing has per-format GitHub Actions triggered by pushes and pull requests to main or dev that touch the corresponding crate, and by a daily schedule. Budgets differ: JPEG uses 600 s per target nightly and 90 s per change, whereas BMP uses 120 s on both (fuzz_jpeg.yml, fuzz_bmp.yml). The JPEG targets cover the plain decode, the incremental API, the other entry points, and a differential IDCT target (see Testing). The plain target uses default options, so it fuzzes the lenient, SIMD-enabled configuration that users get (decode_buffer.rs). The hostile-input papers put this kind of fuzzing in context.

6. Allocation and ownership ​

Codecs allocate their own scratch (Vec) and expose caller-buffer decoding (decode_into) or return owned decoded samples (decode); the alloc crate is required even under no_std (zune-core/src/lib.rs). There is no allocator parameter. Encoders write to any ZByteWriterTrait, which has a reserve_capacity hint, so output can go to a file, a Vec or a fixed slice.

zune-image owns everything: Image owns Frames owns Channels, and Clone deep-copies (threaded per channel for large frames when threads is enabled). There are no borrowed images and no reference counting. Channel manages its memory by hand with alloc_zeroed, realloc and dealloc at 64-byte alignment (channel.rs). Source inspection shows two contract problems: alloc passes a caller-supplied size straight to alloc_zeroed, and a size of zero is reachable (the new_with_bit_type(0, BitType::U8) documentation example), whereas the GlobalAlloc contract says "layout must have non-zero size. Attempting to allocate for a zero-sized layout will result in undefined behavior" (GlobalAlloc); and neither alloc nor realloc checks for a null return. Verified: creating, reading and dropping the zero-length channel returned normally with an empty slice on this system allocator. The allocation still violates the documented contract. This is unsafe outside SIMD, which the workspace policy forbids. The region helpers also build &mut [T] from ThreadSafePtr raw pointers to give each rayon task a disjoint band (planar_regions.rs).

7. CPU parallelism and SIMD ​

SIMD is hand-written per ISA and chosen at run time: AVX2 IDCT and colour conversion in zune-jpeg (#[target_feature(enable = "avx2")], avx2.rs), NEON versions for AArch64, SSE4 and NEON PNG filters, SSE2/SSE4.1/AVX2 deinterleaving and greyscale conversion in zune-image, SSE4.1 and NEON transposition and x86-64/AArch64 Gaussian blur in zune-imageprocs, SSE4.1 and NEON HEVC transforms in zune-heic. JPEG's IDCT dispatch uses a function pointer selected from the options (idct.rs); HEIC instead checks CPU features at its transform entry points (idct.rs):

rust
// crates/zune-jpeg/src/idct.rs (abridged)
pub fn choose_idct_func(options: &DecoderOptions) -> IDCTPtr {
    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    #[cfg(feature = "x86")]
    if options.use_avx2() {
        return |a, b, c| unsafe { avx2::idct_avx2(a, b, c) };
    }
    #[cfg(target_arch = "aarch64")]
    #[cfg(feature = "neon")]
    if options.use_neon() {
        return |a, b, c| unsafe { neon::idct_neon(a, b, c) };
    }
    return idct_int;
}

portable_simd (nightly std::simd) backs alternative paths (premultiply, PNG filters, JPEG upsampling, HEVC IDCT) behind opt-in features; the README expects the unsafe count to "reduce when portable-simd becomes mainstream". Where no intrinsics exist the code relies on plain loops over planes, which planar storage makes easy to auto-vectorise. The project's own Darwin run measures the JPEG intrinsics against none at 54.81 ms versus 55.44 ms (benchmark-results.md), a difference of about 1% on that machine; the README's "should easily replace libjpeg-turbo without any noticeable speed loss" sits beside rows in the same file where mozjpeg (a libjpeg-turbo derivative) is 1.1 to 1.34 times faster on baseline JPEG and zune-jpeg is faster on two progressive cases. These numbers are the project's, not measured here.

Threads. zune-image and zune-imageprocs depend on rayon unconditionally (the image crate's threads feature also gates threaded frame cloning and enables threading in jxl-oxide and zune-jpegxl; zune-image/Cargo.toml, zune-imageprocs/Cargo.toml, Frame::clone). Operations parallelise over row bands (above); zune-heic decodes grid tiles and wavefront CTU rows in parallel. The codecs themselves are single-threaded except HEIC and JPEG XL. Verified: a 2×2 par_process_regions probe succeeded with RAYON_NUM_THREADS=1.

8. GPU offload and interop ​

There is no GPU compute. crates/README.md lists zune-opencl ("Experimental OpenCL bindings"), but the directory does not exist at this revision. Apart from that list, its only source-text reference is a commented-out dependency in zune-bin/Cargo.toml. The one hardware path is decode offload: zune-heic can hand HEVC to Apple VideoToolbox, on by default on macOS (hvec_use_apple_videotoolbox()). Software decoding is a separately selected path; hardware errors propagate without an automatic software retry (decoder.rs). For GPU upload the useful artefacts are zune-jpeg's caller-stride scanline output and its strided raw YCbCr planes, which could write directly into a mapped staging buffer; zune-image's dense planes need re-interleaving or per-plane upload. Not applicable beyond that, because the project's goal is CPU decoding speed.

9. Temporal, animation and containers ​

An animated image is Image with several Frames, each with a duration numerator / denominator (seconds) (frame.rs); is_animated() is "more than one frame" (image.rs). APNG is decoded by zune-png frame by frame (more_frames, frame_info) and composited by ApngContext honouring DisposeOp and BlendOp (apng.rs); the zune-image adapter stores each composited canvas as a full frame (png.rs). JPEG XL animation comes through jxl-oxide, but the adapter reads and discards render.duration() and constructs frames with the Frame::new default of 1 / 1 (jpeg_xl.rs, frame.rs). GIF has a crate that zune-image does not use. Loop count, frame offsets and blend operators are not kept once frames are composited, so an animation cannot be re-encoded losslessly as deltas. Containers are handled per codec; zune-isobmff parses generic ISOBMFF box headers, with payload interpretation left to the HEIF decoder (lib.rs). There is no video, and no timestamp model beyond per-frame durations.

10. Testing and conformance ​

Four mechanisms, each tied to a source:

  • Hash regression corpus. tests/tests/{format}.json lists files in test-images/ with an xxhash of the decoded output; the harness decodes each and compares (tests/src/png.rs). The guide tells contributors to take the hash from a failing run once they "have ensured the output is correct(matches other libraries)" (AddingATest.md), so the oracle is a manual comparison frozen into a hash.
  • Differential tests against other decoders. zune-png's tests decode with the png crate and compare (test_basic.rs); zune-heic runs a CI job that requires RGB byte-identical to libheif 1.17 and YCbCr byte-identical to libde265, pinning the runner image because "zune-heic's YCbCr -> RGB follows libheif 1.17 exactly" (heic_conformance.yml). PngSuite is vendored under crates/zune-png/tests/png_suite/.
  • SIMD against scalar. The fuzz_idct target feeds random coefficient blocks to the dispatched IDCT and the scalar idct_int and asserts equal output, with each input coefficient clamped to −255…255 (fuzz_idct.rs); unit tests in idct.rs do the same on fixed inputs.
  • Build-mode coverage. A CI job builds zune-core, zune-inflate, zune-jpeg (with and without arith), zune-png, zune-ppm and zune-qoi for x86_64-unknown-uefi with --no-default-features; it also builds zune-farbfeld, zune-psd, zune-bmp, zune-isobmff and zune-heic (no_std.yml).

Benchmarks use criterion against mozjpeg, png, spng (libspng), libdeflater, flate2, rapid-qoi, libvips, fast_image_resize, libblur and image (benchmarks/Cargo.toml). The checksum findings in Hostile input show what the suite does not cover: the strict-mode flags have no test that a corrupt checksum is rejected.


Lessons for Sparkles ​

  • Q9. The workspace is evidence for one crate per codec over a tiny shared core: zune-core holds only the reader and writer traits, colour space, depth, options and errors. Most in-tree codecs depend on it alone; PNG adds zune-inflate and HEIC adds zune-isobmff. That lets a consumer take one codec without the image model. Most codecs support no_std, and codec-specific fuzz targets are separate. It also shows the price: the shared surface is a shape convention (decode_headers, decode_into), not a trait, and the image layer re-wraps each codec by hand. Where Sparkles' two PNG decoders and the draft sparkles:image stand is in the existing-code baseline (G1, decision 6).
  • Q1. The planar, stride-free Image is the model to avoid for interchange and the model to keep in mind for point kernels. It forces deinterleaving of interleaved multi-component input and a copy on every crop, and it cannot hold 4:2:0, while zune-jpeg's raw PlaneInfo (sampling factors, stride, byte_size) shows the codec already has the richer description. A Sparkles view with per-plane strides and subsampling (existing-code baseline, G2 and decision 2) can express both. In D that view would use size_t extents and strides, which zune-image's usize matches.
  • Q2. Run-time BitDepth plus a match that instantiates a kernel per sample type is the same erased-plus-typed split as the baseline's decision 2C. Two details transfer: capability as static data (supported_types, supported_colorspaces) checked by one provided execute, and invariant checks before and after operations. The latter need to cover every plane: zune's checks skip alpha lengths, as the probe above demonstrates. In D both map onto design by introspection (a kernel declares what it supports; the shell checks), with static foreach over the sample types replacing the hand-written match. The unused alpha_state hook shows that a capability nobody consults decays; Sparkles' rule to audit callers rather than definitions applies.
  • Q4. ZByteReaderTrait's rewind-on-NotEnoughBytes contract plus zune-jpeg's scalar checkpoints is a working form of resumable decoding without coroutines: the decoder re-runs from a saved position with saved entropy state, and the caller owns the output buffer. The decoder still owns scratch and coefficient buffers, including persistent full-frame coefficients for progressive images (decoder.rs). That is close to the push parser with explicit state that the baseline recommends (decision 5A) and that sparkles:event-horizon could drive (event-horizon baseline). What does not transfer: the reader requires Seek, the fallback without checkpoints re-decodes from the start, and the facility is JPEG-only. A Sparkles design would make resumability part of every codec's contract, not a per-codec addition.
  • Q4. The caller-stride scanline API and CancelCheck (a Fn() -> bool polled at a default interval of 1,024 MCUs) are separable features that fit Sparkles' conventions directly: a @nogc nothrow callback or a delegate-free template parameter, and an output range or caller slice for rows (wired baseline for the header side).
  • Q5. ImageMetadata shows the minimum fields one project found it needed (transfer function, primaries, CICP primaries, MaxCLL, ICC bytes, EXIF, alpha state, an optional gain-map field) and also how a descriptor goes stale when an adapter drops fields: PNG's CICP matrix and range are read and discarded. A Sparkles descriptor (existing-code baseline, G4, decision 3B) should be filled by the codec, not by glue, so nothing read is lost.
  • Q4, Q8. Lenient defaults with strict opt-in match the baseline's decision 1B for PNG, but zune shows the failure mode: the strict flags exist, are documented, and do nothing for PNG CRC and IDAT Adler-32 (verified). An opt-in strict mode is only real with a test per flag that feeds a corrupt file and expects rejection. Under Expected-returning @nogc nothrow code that test is a two-line checkRejects-style unittest.
  • Q4. Per-axis limits are not a resource budget: 16384 × 16384 passes, DNL JPEGs allocate for the maximum height, and APNG frames multiply the canvas. The hostile-input papers call resource safety an open problem; a Sparkles probe returning sizes the caller checks before allocating can budget the caller-owned output. Internal scratch and coefficients still need limits.
  • Q7, Q3. JPEG's runtime ISA dispatch by function pointer chosen per decoder, with a scalar reference and a fuzz target asserting SIMD equals scalar, is the pattern to copy. Sparkles' dispatch is currently package-visible and repeated per use (shaders and SIMD baseline, G5); zune centralises the decision in DecoderOptions, which is also how a test forces the scalar path. The project's own numbers (intrinsics about 1% faster than none for JPEG on one Darwin machine) are a caution that the gain must be measured per kernel and per platform before it justifies @trusted code.
  • Q8. Three oracle forms appear: frozen output hashes, byte-identity against a reference library supplied by a fixed OS release (Ubuntu 24.04's libheif and libde265, without exact package-version pins), and SIMD-against- scalar differential fuzzing. The second is the strongest and matches the png_oracle.d precedent; the first is the weakest, because the hash is only as correct as the manual check that produced it (existing-code baseline, G11 and decision 9).
  • Build modes. The no_std UEFI job is the Rust analogue of Sparkles' betterC and wasm extractions, and it covers the core, inflate and a selection of codec/container crates, not the image layer (which needs std and rayon). The same split would suit Sparkles: codecs @nogc nothrow and betterC-clean, the image layer free to use the runtime. zune-hdr requires std; its source comments cite floating-point floor and exp2 as the reason (lib.rs). D's math functions would need a separate betterC check.
  • Safety rule. The policy restricting unsafe to SIMD did not hold for the container type, where a hand-rolled untyped allocation was written for flexibility. The Sparkles equivalents are sparkles.base.buffer (UniqueBuffer, HeapBuffer) holding a typed element and dip1000 scope slices handed to kernels, which give the same per-plane slices without a @trusted allocator in the image layer.

Strengths ​

  • Independent, small codec crates with one shared vocabulary; a consumer pays only for the formats it uses, and most in-tree codecs support no_std.
  • zune-jpeg exposes whole-image decoding, caller-buffer decoding, caller-stride scanlines, strided subsampled planes, incremental retry with previews, and cooperative cancellation.
  • JPEG's run-time SIMD dispatch, off switch and differential fuzz target keep its fast path checkable against the scalar one.
  • Continuous per-format fuzzing in CI, daily and on every change, with a corpus in the repository.
  • CI compares HEIC output byte-for-byte against libheif and libde265.
  • Planar storage makes point and per-channel operations short, data-parallel loops and lets operations parallelise over row bands without coordination.
  • The image model supports u8, u16 and f32; operations declare which sample types they accept.

Weaknesses ​

  • Defaults favour speed over strictness, and the strict flags for PNG CRC and IDAT Adler-32 are no-ops at this revision; new_safe() leaves intrinsics enabled despite its doc comment.
  • Limits are per axis; there is no combined decoded-pixel, output-byte or frame budget, and DNL JPEG and APNG paths allocate in proportion to the maximum or the frame count.
  • The Image model has no strides, views or subsampled planes, so crops copy and the codecs' richer layouts are lost at the image boundary.
  • Operation invariant checks skip alpha-plane lengths and accept a malformed RGBA image in the no-op probe above.
  • Channel is unsafe, hand-managed untyped memory, contrary to the workspace's own rule, with a reachable zero-size allocation and no null check.
  • Several formats the README presents as supported are delegated (JPEG XL decode, WebP, JPEG 2000, JPEG encode, colour management).
  • Incremental decoding, scanlines and cancellation are JPEG-only and invisible through DecoderTrait.
  • rayon is unconditional in zune-image; disabling threads does not remove its parallel region processing. The image layer requires std. This does not rule out using a one-thread rayon pool.

Key design decisions and trade-offs ​

DecisionRationaleTrade-off
One crate per codec over zune-corePay-per-format dependencies, no_std codecs, per-codec fuzzing and versionsShared behaviour is convention, not a trait; the image layer adapts each codec by hand
Pull reader with Seek, rewind on NotEnoughBytesFiles need not be in memory; short-input failures can be retriedForward-only streams must be buffered; resumption needs per-codec checkpoint state
read_byte_no_error returns 0 past EOFBranch-light entropy loopsTruncation becomes silent zeros unless the codec checks separately
Lenient, SIMD-on defaults; strictness opt-inDecode as many real-world files as possible, as fast as possiblePNG checksum flags are no-ops; the plain JPEG decode fuzz target uses the lenient mode
Per-axis max_width / max_height checked before allocationSimple, uniform across decodersNot a memory budget; allocation size still depends on component count, depth, DNL and frame count
Planar Image of dense width * height planesSimple per-channel kernels, easy band parallelism, depth-agnostic opsDeinterleave on decode, interleave on encode, copy on crop; no subsampling or views
Untyped Channel with run-time TypeIdOne image type for u8, u16, f32; type erasure for bindings and pipelineHand-written unsafe allocation; type errors become run-time ChannelErrors
Run-time BitType match, monomorphised kernelsStatic speed per sample type without a type parameter on ImageEvery operation repeats the match; unsupported combinations are errors, not compile failures
JPEG IDCT function-pointer dispatch chosen per decoderCPU detection at selection; options can force the scalar pathIndirect call per block; HEIC uses a different dispatch policy
Delegate secondary formats and colour management to other cratesShip coverage without writing every codecSafety, limits and fuzzing guarantees differ per delegated crate

Sources ​