rust-j2k decodes JPEG 2000 images in pure Rust, for programs that need to read
them without linking a C library. It reads .jp2 and .jph files, bare
.j2k and .j2c streams, and the JPEG 2000 grids inside GRIB2 weather files,
and gives you each channel's values as whole numbers at the file's full depth.
It has no dependencies, so it cross-compiles without a C toolchain. Its output
is checked against OpenJPEG, an official JPEG 2000
reference implementation, and against the standard's own conformance tests.
cargo add rust-j2kIt needs Rust 1.87 or newer.
The crate follows Semantic Versioning. Within a minor version, the types, the error variants and limit kinds, what each means, and the variant each fault or wait gives do not change in a way that breaks your code. A fix that brings the output closer to the standard or OpenJPEG can ship in a patch release. The minimum Rust version rises only in a minor release. Message text and which fault is reported first can change in any release. A default limit can be raised in any release, and lowered only in a minor one or as a security fix. docs/stability.md lists what is covered and what is not.
Pass the bytes to decode and you get back an Image, with one Component
for each channel. Start here:
use rust_j2k::decode;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// A .jp2, .j2k or .j2c file, or the JPEG 2000 bytes of a GRIB2 grid.
let bytes = std::fs::read("YOUR_FILE.jp2")?;
let image = decode(&bytes)?;
println!("{}x{}, {} channels", image.width, image.height, image.components.len());
let channel = image.component(0).expect("an image has at least one channel");
println!(
"channel 0 is {}x{}, {}-bit, signed: {}",
channel.width, channel.height, channel.bit_depth, channel.signed,
);
// `samples` holds width × height numbers, row by row.
let first = channel.sample(0, 0).expect("0,0 is inside the grid");
println!("first value: {first}");
Ok(())
}The values are the image's samples. Unsigned ones run from 0 to 2^bit_depth − 1, and signed ones are centered on 0.
To try it on a file, see the
examples:
decode writes a PGM or PPM, info prints a file's header, thumbnail
decodes at a smaller size or a window, stream decodes a file fed in
chunks, untrusted decodes a file from a source you don't trust, and
grib2 decodes and scales a GRIB2 grid.
To show a .jp2 image, decode it with
DecodeOptions::default().with_color(ColorOutput::Srgb) through decode_with,
then scale each value to 8 bits; the ColorOutput::Srgb
docs
show how. Check image.jp2 afterwards. If a profile couldn't be applied, the
values come back as stored and jp2.color is still
ColorSpace::RestrictedIcc.
decode_with takes the other options too,
such as a smaller image. To learn the size and channels without decoding, call
read_header. The API docs cover each one.
A bare .j2k or .j2c stream has no boxes, so image.jp2 is None and the
file says nothing about its color space. One channel is usually gray, and two
are gray plus transparency; for three or more, you have to know what they
mean.
read_header reads the size, the image area and the tile grid without
decoding. Pass a rectangle or a tile number to decode_with to decode only
that part. Tiles and the reference grid are explained in What a JPEG 2000
file holds.
use rust_j2k::{DecodeOptions, Region, Window, decode_with, read_header};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let bytes = std::fs::read("YOUR_FILE.jp2")?;
let header = read_header(&bytes)?;
println!("{}x{}, {} tiles", header.width, header.height, header.tiles.count());
// The image's top-left 256 × 256. The image area can start away from
// the grid's origin, so place the window from it.
let area = header.image_area;
let (x1, y1) = (area.x0.saturating_add(256), area.y0.saturating_add(256));
let window = Region::Window(Window::new(area.x0, area.y0, x1, y1));
let part = decode_with(&bytes, &DecodeOptions::default().with_region(window))?;
println!("window: {}x{}", part.width, part.height);
// The last tile. Tiles are numbered row by row from 0, and
// `header.tiles.tile(n)` gives tile n's rectangle.
let last = Region::Tile(header.tiles.count() - 1);
let tile = decode_with(&bytes, &DecodeOptions::default().with_region(last))?;
println!("last tile: {}x{}", tile.width, tile.height);
Ok(())
}The rectangle is on the reference grid. It must overlap the image area, or
decode_with returns Error::InvalidOptions; the part of it outside the
image area is cut off.
A JPEG 2000 image is stored as one compressed stream of bytes, called a
codestream. A .j2k or .j2c file is just that stream, and so is the JPEG
2000 part of a GRIB2 grid. A .jp2 file wraps the stream in labeled sections,
called boxes. The boxes say what the colors mean, and they can carry a palette,
the capture and display resolution, or XML metadata.
The image has one or more channels: one for a grayscale picture or a weather
grid, three for RGB, and sometimes one more for transparency. Each channel is
its own grid of whole numbers, often 8 bits deep and sometimes 12 or 16. A
channel can be stored at a lower resolution than the image, for example a
color channel at half the width. This is called sub-sampling. That channel's
grid is smaller than the image, and its x_sampling and y_sampling say by
how much.
The image sits on a reference grid, the coordinate space of the whole codestream. The image area can start away from the grid's origin, and each channel's grid is the image area divided by its sub-sampling. The image is cut into rectangular tiles, numbered row by row from 0, each coded on its own. A file with no tiling is one tile.
The API docs use the standard's names for the parts of a codestream:
- A marker is a two-byte code that names the header segment after it,
or marks a boundary. The main header, before any tile, holds
SIZ(the image size, tile size and channels) andCOD(how the image is coded), among others. - Each tile is stored as one or more tile-parts. Each tile-part starts
with an
SOTmarker, which gives its tile and its length (Psot; 0 means "to the end of the codestream"), and can give the tile's number of tile-parts (TNsot; 0 means not declared). The firstSOTmarker ends the main header. - A tile's coded data is a series of packets. Each packet header says which code-blocks, small blocks of the image's wavelet coefficients, it adds data to. Reading those headers is the packet walk.
- The
EOCmarker ends the codestream.
- It reads every coding feature of the core standard (ISO/IEC 15444-1, "Part 1"): lossless and lossy compression, images cut into tiles, quality layers, every order the data can be stored in, and regions coded at higher quality. docs/scope.md lists every feature.
- It reads HTJ2K (ISO/IEC 15444-15, "Part 15"), the high-throughput
variant, including files that mix HT and Part 1 code-blocks, and
.jphfiles, the HTJ2K version of.jp2. You don't need to know whether a file uses it: the same call and options decode either kind. - It reads the
.jp2file format: palettes, channel labels, the color space, the resolution, and the XML and UUID metadata boxes. It applies the palette and puts the color channels in order, and reports the rest onImage::jp2. - It can convert to sRGB if you ask (
ColorOutput::Srgb). It converts sYCC, and the restricted ICC profiles JP2 allows (gray, or RGB defined by a matrix and curves), giving the same samples as OpenJPEG with LittleCMS. - It can decode a smaller image. Each resolution level you drop about
halves the width and height (
DecodeOptions::with_resolution_reduction). - It can decode fewer quality layers, for a coarser image at full size in
less time (
DecodeOptions::with_max_quality_layers). - It can decode part of an image: a rectangle of it, one tile, or some of
its components (
DecodeOptions::with_region,with_components). Only the tiles the rectangle reaches are decoded, and in them only the code-blocks it needs. - It can decode a codestream that is cut short and tell you what is
missing (
DecodeOptions::with_strict(false)), and decode a file while it arrives (IncrementalDecoder). - It reads whole numbers up to 32 bits: unsigned up to 31 bits and signed up to 32, with or without sub-sampling.
- It doesn't encode. It reads JPEG 2000 but can't write it. An encoder is
planned
(docs/roadmap.md).
To write JPEG 2000 today, use OpenJPEG's
opj_compress. - It doesn't give you display pixels. You get each channel as its own
Vec<i32>at the file's depth and in the file's color space, not 8-bit RGBA. To show an image, ask for sRGB and scale each value to 8 bits yourself (see Decode a file). - It doesn't read the Part 2 extensions, or an HTJ2K codestream that
needs more than one HT coding pass set per code-block. These are refused with
Error::Unsupported. - It doesn't open JPX, JPM or MJ2 files unless they say they can also be
read as JP2 or JPH. Those files are refused with
Error::Unsupported. - It doesn't read unsigned 32-bit channels or channels deeper than 32 bits,
because they don't fit an
i32. The standard allows up to 38. These are refused withError::Unsupported. - It doesn't convert every color space to sRGB. CMYK, CIELab, e-sYCC, and
ICC profiles that work through a lookup table or through Lab, are refused with
Error::Unsupportedwhen you ask for sRGB. Decoding without the option still gives you the values as stored. docs/srgb-output.md lists every case. - It doesn't read GRIB2 files. You find the JPEG 2000 bytes in the message and turn the decoded numbers into physical values yourself (see GRIB2 grids).
- It can't always detect damage. JPEG 2000 has no checksum, so a flipped byte in the compressed data can decode to a wrong image with no error. If that matters, check the file against a hash you keep.
A GRIB2 message that uses data representation template 5.40 stores its grid as
a JPEG 2000 codestream in section 7, the data section. Pass decode the bytes
of section 7 after its 5-byte header. You get one channel of whole numbers X,
in the grid's scanning order. To get the physical values Y, take the reference
value R, the binary scale factor E and the decimal scale factor D from section
5, and compute Y = (R + X × 2^E) / 10^D (WMO regulation 92.9.4, note 4). If the
message has a bitmap in section 6, the values fill only the points it marks.
The grib2 example takes the codestream and R, E and D, and prints the
first few scaled values.
If section 5 gives 0 bits per value (octet 20), the field is constant and
section 7 may hold no codestream at all. Don't call decode then: every point
has the value R, which is how eccodes and NCEP's g2clib read it.
Every failure comes back as an Error. With the default limits, decoding never
panics or hangs, whatever the input, and needs at most about 6.4 GiB of memory
for an input of up to 64 MiB, or about 102 bytes per byte of input past that,
plus up to about 3.2 bytes for each byte of JP2 metadata boxes. If the machine
can't supply that, the allocation fails and the process aborts, as in any Rust
program. Those figures cover the one-shot decode functions;
IncrementalDecoder keeps per-tile state and can hold one decode's peak plus
the packet walks it keeps, at most twice the image's samples plus twice the walk
of the largest tile walked so far, plus up to twice the bytes fed and a table of
24 bytes per tile index up to the highest seen, about 1.5 MiB for 65535 tiles
(see docs/incremental-decoding.md).
Error::Unsupported usually means the file is valid JPEG 2000 that uses
something from What it doesn't do. Error::Limit means
the file asks for more memory or work than the limits allow (see Large
files). A damaged header can show up as either one.
Error::InvalidOptions means your options are invalid, or don't fit this
file (such as a reduction past its levels).
Error::Inconsistent is a bug in this decoder; please report it. The other
variants mean the input is damaged or isn't JPEG 2000. The untrusted
example shows one way to handle these: it prints a limit refusal's details,
and decodes a cut-short file again with strict off.
use rust_j2k::{Error, decode};
fn check(bytes: &[u8]) {
match decode(bytes) {
Ok(image) => println!("decoded {} channels", image.components.len()),
Err(Error::Unsupported(what)) => eprintln!("not decoded: {what}"),
Err(Error::Limit(limit)) => eprintln!("past a limit: {limit}"),
Err(e) => eprintln!("damaged or not JPEG 2000: {e}"),
}
}By default a damaged or cut-off file is an error, never a partial image. What is refused, and where this decoder is stricter than OpenJPEG, is in docs/openjpeg-departures.md.
To decode what a cut-short codestream holds, as opj_decompress -allow-partial
does, turn strict off. The codestream's header, everything before the first
tile, must have arrived. Only missing data and extra bytes after a tile's last
packet are let through; any other damage is still an error. Image::report
says what was missing, and its complete_reduction is the finest resolution
the data already gives in full.
For a file that arrives in pieces, such as a download, use
IncrementalDecoder. Feed it each piece and call refine. From time to time
it returns an image of what has come in so far (Refined::Image); otherwise
it says why it waits (Refined::Waiting), such as for more bytes or for the
rest of the main header. Each refine goes on from where
the last one stopped reading packets;
docs/incremental-decoding.md
lists the packets it reads again. Each image refine returns is decoded with
strict off and comes with a report of what is missing. finish decodes
under the options you gave the decoder, so it is strict unless you turned
strict off:
use rust_j2k::{DecodeOptions, IncrementalDecoder, Refined};
// `pieces`: the parts of the file, in the order they arrive.
fn show_as_it_arrives(pieces: &[&[u8]]) -> Result<(), rust_j2k::Error> {
let mut decoder = IncrementalDecoder::new(DecodeOptions::default())?;
for piece in pieces {
decoder.feed(piece);
if let Refined::Image(image) = decoder.refine()? {
// Show `image`. Its report says what is still missing.
for entry in &image.report.entries {
eprintln!("{entry}");
}
}
}
// The input has ended: decode all of it.
let image = decoder.finish()?;
println!("{}x{}", image.width, image.height);
Ok(())
}A valid file can be refused with Error::Limit when it is very large or very
costly to decode. The limits protect you from a small hostile file that asks
for gigabytes of memory or minutes of work. By default an image may hold 2^29
values (2 GiB as i32): a 10980 × 10980 three-band satellite image, or an RGB
image of about 179 megapixels. Past 64 MiB of input the limit is 8 values per
byte of the file. For a ceiling that does not grow with the file, set
sample_limit to SampleLimit::AtMost(n): at most n values, whatever the
file's size or padding.
Three limits can be raised: sample_limit, packet_work_limit and
code_block_work_limit on DecodeOptions. For a file you trust, set them to
SampleLimit::Unlimited and WorkLimit::Unlimited. The process then
aborts if memory runs out, instead of returning an error. Damage can also
look like a limit, since one flipped bit in the header can declare an
absurd size, so don't lift the limits on that evidence alone. The error says
which limit refused the file and what to raise it to, as data:
LimitDetail::kind and LimitDetail::retry_with. A code-block work or tile
cap refusal also gives the tile, the amount it needs and the amount's unit
(LimitDetail::refused_at), on every target. DecodeOptions::raised_for
sets that limit for you:
use rust_j2k::{DecodeOptions, Error, Image, LimitKind, Raise, SampleLimit, WorkLimit, decode_with};
/// For files you trust. Each retry decodes again and can ask for more, and a
/// 100-byte file can name a factor of 8 or more. This loop raises a limit to
/// at most `MAX_FACTOR` times the default. For a small file, 4 x 2^29 samples
/// is 8 GiB of output and up to about 26 GiB of sample buffers, plus a tile's
/// code-block and precinct records, and each of the three bounds can reach 4
/// times its default, and the decode time grows with them. The loop makes at
/// most five decodes, so up to about 17 times one default decode's work in
/// all. Pick the factor you can afford.
fn decode_raising_limits(bytes: &[u8]) -> Result<Image, Error> {
const MAX_FACTOR: u32 = 4;
let mut options = DecodeOptions::default();
// A few retries; each one can ask for more.
for _ in 0..4 {
let limit = match decode_with(bytes, &options) {
Err(Error::Limit(limit)) => limit,
result => return result,
};
let Some(Raise::Raised(factor)) = limit.retry_with() else {
// No option raises this limit, or it gave no factor.
return Err(Error::Limit(limit));
};
if factor <= MAX_FACTOR {
options = options.raised_for(&limit).expect("it gave a factor");
} else {
// Past the maximum: one last try at the maximum, for the limit
// that refused.
let most = WorkLimit::Raised(MAX_FACTOR);
let raised = match limit.kind() {
LimitKind::PacketWork => options.clone().with_packet_work_limit(most),
LimitKind::CodeBlockWork => options.clone().with_code_block_work_limit(most),
// The samples, and the caps on a tile's code-blocks and
// precincts, which the sample limit sets.
LimitKind::Samples | LimitKind::TileCodeBlocks | LimitKind::TilePrecincts => {
options.clone().with_sample_limit(SampleLimit::Raised(MAX_FACTOR))
}
_ => return Err(Error::Limit(limit)),
};
// Already at the maximum: the same options would refuse again.
if raised == options {
return Err(Error::Limit(limit));
}
options = raised;
return decode_with(bytes, &options);
}
}
decode_with(bytes, &options)
}For files from outside, don't retry: set sample_limit to
SampleLimit::AtMost(n) and run each decode under a timeout (see Running
a decode service).
A retry can be refused again, by the same limit or another, so the loop stops
after a few. The WorkLimit
docs say how
far to trust the factor.
It decodes all 23 test codestreams of the JPEG 2000 conformance suite (ISO/IEC 15444-4) within the error the standard allows, the suite's nine JP2 files within theirs, and its 41 HTJ2K codestreams and 20 JPH files within theirs. Lossless images, including GRIB2 grids written by eccodes, match OpenJPEG bit for bit. The details are in docs/correctness.md, and the places where this decoder and OpenJPEG differ on purpose are in docs/openjpeg-departures.md. The test suite needs no external tools.
The speed and memory of this decoder next to OpenJPEG, Grok, OpenJPH and hayro-jpeg2000 are in docs/performance.md.
To contribute, start with CONTRIBUTING.md. It has the workflow and the checks every pull request must pass. To report a crash, a hang or another vulnerability, see SECURITY.md.
Dual-licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
src/jp2/lcms.rs is a Rust port of parts of LittleCMS 2.14, which is MIT
licensed. Its copyright and license notice is at the top of that file.
src/jp2/math.rs and src/jp2/math_data.rs are a Rust port of pow and
log from Arm's optimized-routines (commit 2ee4b0d), used under the MIT
license. Its copyright and license notice is at the top of math.rs.
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.