Skip to main content

Crate rust_j2k

Crate rust_j2k 

Source
Expand description

Pure-Rust JPEG 2000 codec, built GRIB2-decode-first toward OpenJPEG-level coverage. No C dependency, so it cross-compiles cleanly to every target.

The decoder reads a raw codestream (ISO/IEC 15444-1 Annex A, not yet the JP2 file format) into integer samples: one grid per component, signed up to 32 bits, unsigned up to 31 (an i32 container), each with its own bit depth, sign, and sub-sampling. Both wavelet paths decode — the reversible 5/3 bit-exactly, the irreversible 9/7 within the standard’s compliance bounds — as do tiling and tile-parts, precincts, quality layers, all five progression orders and POC changes between them, packed packet headers (PPM/PPT), every Part 1 code-block coding style, region of interest (maxshift), non-zero canvas offsets, resolution-reduced decoding, and the reversible (RCT) and irreversible (ICT) colour transforms.

JP2 boxes, HTJ2K, and an encoder are later work, not permanent non-goals. See docs/roadmap.md and docs/scope.md.

§Pipeline

bytes ─▶ codestream ─▶ tier-2 ─▶ tier-1 ─▶ dequant ─▶ inverse DWT ─▶ Image
        (markers)     (packets)  (MQ+EBCOT) (quant)    (5/3 | 9/7)

Each stage is a module below. decode wires them together.

§Example

The entire public surface is decode: codestream bytes in, an Image out. An Image is the image area on the reference grid plus one Component per codestream component; a sub-sampled component holds fewer samples than that area, so size sample buffers from the component, not from the image. Malformed input never panics; it comes back as a typed Error.

use rust_j2k::{decode, Error};

// In real use these are the bytes of a `.j2k` codestream, or the GRIB2 §7
// data section of a `grid_jpeg` message. Invalid input is rejected cleanly:
match decode(b"not a codestream") {
    Ok(image) => {
        let first = image.component(0).expect("at least one component");
        println!(
            "image area {}x{}, first component {}x{}",
            image.width, image.height, first.width, first.height,
        );
    }
    Err(Error::Unsupported(what)) => println!("outside the decoded subset: {what}"),
    Err(e) => println!("decode failed: {e}"),
}

§Status

All 23 entries of the ISO/IEC 15444-4 conformance suite decode within their class-1 error bounds, and that gate runs in tests/ against committed oracle snapshots, with no external tools. Correctness is defined by cross-checking a reference decoder (OpenJPEG / eccodes), not by self-consistency. A codestream using a feature outside the decoded set — a JP2 wrapper, HTJ2K, a Part 2 extension — is rejected with Error::Unsupported, never half-decoded. GRIB2 §5.40 (grid_jpeg) payloads, the crate’s original target, are raw codestreams and decode directly. See each module’s docs for the ISO §reference and what it owns; the feature map is in docs/scope.md.

Structs§

Component
One decoded component: its own sample grid, its declared depth and sign, and the sub-sampling that maps it onto the image’s reference grid.
DecodeOptions
Options for decode_with. The default decodes at full resolution — decode is exactly that.
Image
A decoded image: the image area on the reference grid, plus its components.

Enums§

Error
A decode failure, tagged by the class of fault.

Functions§

decode
Decode a JPEG 2000 codestream (Annex A, no JP2 wrapper) into an Image: the reference-grid image area plus one Component per SIZ component, each on its own sample grid.
decode_with
Decode a JPEG 2000 codestream like decode, governed by options.

Type Aliases§

Result
Crate result alias.