rust-j2k
A pure-Rust JPEG 2000 codec, built GRIB2-decode-first and aimed, over time, at the same coverage as OpenJPEG (the ISO/IEC reference implementation), with no C dependency, so it cross-compiles cleanly to every target. As of 2026 there is no production-grade pure-Rust JPEG 2000 codec; this fills that gap.
Install
Or add it to Cargo.toml:
[]
= "0.3"
The crate has no runtime dependencies and no C toolchain requirement. Minimum supported Rust version is 1.87.
Usage
The public surface is small: bytes of a raw JPEG 2000 codestream in, a decoded
Image out — decode, or decode_with to set options such as a resolution
reduction. An Image is the image area on the reference grid plus one
Component per codestream component, each on its own sample grid.
use decode;
Decoding never panics on malformed input; every failure is a typed Error. The
Unsupported variant specifically marks valid JPEG 2000 that falls outside the
currently decoded subset (see below), so you can tell "broken file" from "not
yet supported":
use ;
match decode
samples are raw integer values. For GRIB2 the caller un-scales them with the
template's reference value and binary/decimal scale factors; this crate stops at
the codestream's integer output and does not interpret the GRIB2 fields.
What it decodes
| Decoded | Not yet (returns Error::Unsupported) |
|---|---|
| Raw codestream (Annex A) | JP2 file format / boxes |
| Integer components, signed or unsigned, up to 32 bits, with sub-sampling | Depths past the i32 container (33–38 bits) |
| Both color transforms: reversible (RCT) and irreversible (ICT) | Part 2 extensions (array MCT, …) |
| Reversible 5/3 (lossless) and irreversible 9/7 (lossy) wavelets | HTJ2K (Part 15) block coding |
| Multiple tiles and tile-parts | — |
| Multiple quality layers; all five progression orders and POC changes | — |
| Precinct partitions; SOP/EPH delimiters; PPM/PPT packed headers | — |
| All six code-block coding styles; region of interest (maxshift) | — |
| Resolution-reduced decode (each level halves the output) | — |
Anything outside the decoded set is rejected cleanly, not half-decoded. The long-run target is OpenJPEG-level coverage (full Part 1, the JP2 file format, HTJ2K, an encoder, and the later parts); we get there by widening this same engine outward. 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 under src/, and each module's docs cite the ISO section
it owns.
Status
All 23 entries of the ISO/IEC 15444-4 conformance suite decode within their
class-1 error bounds, gated in CI against committed reference images. The GRIB2
§5.40 (grid_jpeg) path — the crate's original target — is gated bit-exact
against the OpenJPEG/eccodes oracle.
Next comes the JP2 file format, then HTJ2K and an encoder, widening this same engine. The plan is in docs/roadmap.md.
Correctness
Two harnesses set the bar. tests/conformance_part4.rs grades the vendored
ISO/IEC 15444-4 conformance corpus against its class-1 error bounds — the
standard's own definition of a compliant decoder. tests/conformance.rs decodes
a corpus of real and synthetic codestreams and compares against a trusted
decoder (OpenJPEG, or eccodes for the GRIB2-sourced files): bit-exact for the
reversible path, within a stated tolerance for lossy. The full strategy —
oracle cross-check, conformance suite, golden vectors, fuzzing — is in
docs/correctness.md.
Development
cargo test is self-contained: it grades a decode against the committed
expected.json snapshots and needs no external tools. CI runs the same way.
A reference toolchain is only needed to (re)generate those snapshots and to run
the extra gates — OpenJPEG and eccodes (oracles), cargo-deny (license/advisory
gate), and cargo-fuzz (robustness). Install it in one step and regenerate an
oracle with:
These tools are for oracle generation only; the test suite and CI never use them. Versions, the macOS path, and the GRIB2 sample-mapping note are in docs/development.md. See CONTRIBUTING.md to get started and docs/ for the design docs.
Contributing
Contributions are welcome. Start with CONTRIBUTING.md for the workflow, the pure-Rust / no-C rule, and the quality gates every PR must pass.
Security
This crate decodes untrusted binary input. To report a vulnerability, see SECURITY.md. It also covers the robustness contract (no panics on malformed input) and the fuzzing that backs it.
Why this exists
It is the decoder the fieldglass GRIB2 reader needs for template 5.40, kept in its own crate so the codec is reusable and testable on its own, and so the fieldglass bundle stays free of a C JPEG 2000 dependency.
License
Dual-licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual-licensed as above, without any additional terms or conditions.