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//! # Status
29//!
30//! Phase 1 complete: the GRIB2 §5.40 decode path is implemented end to end and
31//! passes the conformance gate (bit-exact 5/3, within tolerance 9/7). See each
32//! module's docs for the ISO §reference and what it owns. Correctness is defined
33//! by the conformance harness in `tests/` (cross-check against OpenJPEG /
34//! eccodes), not by self-consistency. Later phases widen this same engine toward
35//! general Part 1; see `docs/roadmap.md` and `docs/scope.md`.
36
37pub mod codestream;
38pub mod dwt;
39pub mod error;
40pub mod image;
41pub mod quant;
42pub mod tier1;
43pub mod tier2;
44
45pub use error::{Error, Result};
46pub use image::Image;
47
48/// Decode a JPEG 2000 **codestream** (Annex A, no JP2 wrapper) into a single
49/// integer-component [`Image`].
50///
51/// This is the whole public surface for the GRIB2 use case: the §7 data
52/// section of a `grid_jpeg` message is exactly such a codestream.
53pub fn decode(codestream: &[u8]) -> Result<Image> {
54 let cs = codestream::parse(codestream)?;
55
56 // Tier-2: parse packets into per-code-block coded segments.
57 let coded = tier2::decode_packets(&cs)?;
58 // Tier-1: MQ + EBCOT bit-plane decode each code block into subband coeffs.
59 let coeffs = tier1::decode_code_blocks(&cs.header, &coded)?;
60 // Dequantize, then invert the DWT per resolution level into samples.
61 let dequant = quant::dequantize(&cs.header, coeffs)?;
62 let samples = dwt::inverse(&cs.header, dequant)?;
63
64 image::assemble(&cs.header, samples)
65}