rust_j2k/lib.rs
1//! Pure-Rust JPEG 2000 codec, built GRIB2-decode-first toward OpenJPEG-level
2//! coverage. No C dependency, so it cross-compiles cleanly to every target.
3//!
4//! JPEG 2000 (ISO/IEC 15444-1 and later parts) is a large standard. The first
5//! deliverable decodes the slice GRIB2 §5.40 (`grid_jpeg`) needs, which keeps
6//! the initial surface tractable while exercising the whole pipeline:
7//!
8//! - the **raw codestream** (Annex A), not yet the JP2 file format (no boxes);
9//! - a **single component** (one scalar grid), so no multi-component or color
10//! transform (MCT) yet;
11//! - **integer** samples, signed or unsigned, up to 32 bits;
12//! - **both** the reversible 5/3 (lossless) and irreversible 9/7 (lossy)
13//! wavelet paths (the 9/7 path is graded by re-encoding a real grid with
14//! OpenJPEG, since no operational GRIB2 producer ships lossy 9/7).
15//!
16//! Multi-component/color, JP2 boxes, HTJ2K, and an encoder are later-phase
17//! work, not permanent non-goals. See `docs/roadmap.md` and `docs/scope.md`.
18//!
19//! # Pipeline
20//!
21//! ```text
22//! bytes ─▶ codestream ─▶ tier-2 ─▶ tier-1 ─▶ dequant ─▶ inverse DWT ─▶ Image
23//! (markers) (packets) (MQ+EBCOT) (quant) (5/3 | 9/7)
24//! ```
25//!
26//! Each stage is a module below. [`decode`] wires them together.
27//!
28//! # Example
29//!
30//! The entire public surface is [`decode`]: codestream bytes in, an [`Image`]
31//! out. Malformed input never panics; it comes back as a typed [`Error`].
32//!
33//! ```
34//! use rust_j2k::{decode, Error};
35//!
36//! // In real use these are the bytes of a `.j2k` codestream, or the GRIB2 §7
37//! // data section of a `grid_jpeg` message. Invalid input is rejected cleanly:
38//! match decode(b"not a codestream") {
39//! Ok(image) => println!("decoded {}x{}", image.width, image.height),
40//! Err(Error::Unsupported(what)) => println!("outside the decoded subset: {what}"),
41//! Err(e) => println!("decode failed: {e}"),
42//! }
43//! ```
44//!
45//! # Status
46//!
47//! The GRIB2 §5.40 decode path described above is implemented end to end and
48//! passes the conformance gate (bit-exact 5/3, within tolerance 9/7). Anything
49//! outside that subset is rejected with [`Error::Unsupported`], never
50//! half-decoded. See each module's docs for the ISO §reference and what it owns.
51//! Correctness is defined by the conformance harness in `tests/` (cross-check
52//! against OpenJPEG / eccodes), not by self-consistency. The plan for widening
53//! this same engine toward general Part 1 is in `docs/roadmap.md`; the feature
54//! map is in `docs/scope.md`.
55#![warn(missing_docs)]
56
57// The pipeline modules are crate-internal: the public API is `decode`, `Image`,
58// `Error`, and `Result`. Keeping the stages private lets each one evolve freely
59// (the roadmap widens all of them) without churning the crate's committed
60// surface, and keeps the docs.rs page to what a caller can actually use.
61pub(crate) mod codestream;
62pub(crate) mod dwt;
63pub(crate) mod error;
64pub(crate) mod image;
65pub(crate) mod quant;
66pub(crate) mod tier1;
67pub(crate) mod tier2;
68
69pub use error::{Error, Result};
70pub use image::Image;
71
72/// Decode a JPEG 2000 **codestream** (Annex A, no JP2 wrapper) into a single
73/// integer-component [`Image`].
74///
75/// This is the whole public surface for the GRIB2 use case: the §7 data
76/// section of a `grid_jpeg` message is exactly such a codestream.
77pub fn decode(codestream: &[u8]) -> Result<Image> {
78 let cs = codestream::parse(codestream)?;
79
80 // Tier-2: parse packets into per-code-block coded segments.
81 let coded = tier2::decode_packets(&cs)?;
82 // Tier-1: MQ + EBCOT bit-plane decode each code block into subband coeffs.
83 let coeffs = tier1::decode_code_blocks(&cs.header, &coded)?;
84 // Dequantize, then invert the DWT per resolution level into samples.
85 let dequant = quant::dequantize(&cs.header, coeffs)?;
86 let samples = dwt::inverse(&cs.header, dequant)?;
87
88 image::assemble(&cs.header, samples)
89}