pub fn decode(bytes: &[u8]) -> Result<Image>Expand description
Decode a JPEG 2000 codestream (Annex A) or JP2 file (Annex I)
into an Image: the reference-grid image area plus one Component per
output channel, each on its own sample grid. For a raw codestream that is
one per component of the SIZ marker, in SIZ order. What a JPEG 2000
file holds
explains the codestream’s parts. A JP2 file can change the channels:
its palette and channel definitions (below), and, through decode_with,
the sRGB output option.
The input kind is detected from its first bytes. Input that opens with the
SOC marker (FF 4F) is a codestream. Otherwise, input whose bytes 4..8
are jP (the signature box’s type) is a JP2 file, and anything else goes
to the codestream parser, which rejects it for its missing SOC. A JP2 file
decodes its codestream (the first jp2c box) and reports the file’s header
on Image::jp2, which is None for a raw codestream.
A JP2 file with a palette (pclr and cmap) decodes to the palette’s
channels, one per palette column, so it can have more components than its
codestream. A pclr without a cmap is ignored. A file with channel
definitions (cdef) moves each color channel to its color’s position
(color 1 first) and labels every channel on Jp2Metadata::channels.
DecodeOptions::raw_channels skips both and returns the codestream’s
components as coded.
use rust_j2k::{decode, ColorSpace};
// A `.jp2` file decodes the same way as a raw codestream; the format is
// detected from the first bytes.
let bytes = std::fs::read("photo.jp2")?;
let image = decode(&bytes)?;
if let Some(jp2) = &image.jp2 {
if jp2.color == ColorSpace::Srgb {
println!("{} sRGB channels", image.components.len());
}
}This is the whole public surface for the GRIB2 use case: the §7 data
section of a grid_jpeg message is a raw codestream. That subset is
single-component, so its images carry one component; a codestream declaring
more decodes each component onto its own grid, independently.
A JPH file (a JP2-family file holding an HTJ2K codestream) decodes as a
JP2 file does. A malformed box structure is Error::FileFormat. A file
whose File Type box has neither jp2 nor jph as its brand or in its
compatibility list is valid JPEG 2000 but is rejected with
Error::Unsupported. Anything else outside the decoded subset is
rejected the same way, never half-decoded.
Three limits bound a decode: the size of the image, and the work of
reading the packet headers and decoding the code-blocks for the file’s
size. Past any of them the decode fails with Error::Limit. OpenJPEG has none of
them.
- The decoded image may hold at most 2^29 samples (2 GiB
as
i32), summed over its output channels at the decode’sresolution_reduction: every component, a palette’s expanded channels, and the channels sRGB output adds. That admits a 10980 x 10980 Sentinel-2 true-color image (three components), or an RGB image of up to about 179 MP. A larger image is refused from its header alone, before any tile is read. Under a window or a tile, only the tiles read count (seeDecodeOptions::sample_limit). Peak memory is about 1.35 times the output for a square tile, about 2.7 GiB at the limit, and more for a tall, narrow tile or while a palette expands (see the sample limit). This is for the one-shot functions; anIncrementalDecoderkeeps per-tile state and can hold up to about three times as much. - Reading the packet headers of every tile may take at most 2^24 steps, or 32 per byte of tile data when that is more. A step is a packet read, a run of code-blocks a header skips without reading a bit, or a position a progression order sweeps; the tier-2 work bound gives the full count. A valid file of many layers that each include a few scattered code-blocks can exceed it.
- Decoding the code-blocks may take at most 2^26 sample-passes (passes run times code-block samples), or 2^15 per byte of tile data when that is more. A tile past it is refused before any of its code-blocks is decoded. On the slowest data the bound comes to about 0.5 s, or 0.25 ms a byte (see the tier-1 work bound).
decode_with can raise or lift each of the three, for a file from a
source you trust: see DecodeOptions::sample_limit,
DecodeOptions::packet_work_limit and
DecodeOptions::code_block_work_limit. An Error::Limit from one
of them names the option, as LimitDetail::kind, and the
WorkLimit to retry with, as LimitDetail::retry_with; its message
names both too. WorkLimit shows a retry and how far to trust the
factor. The guards with no option are listed on Error::Limit.
Decoding is strict by default: damaged or truncated data is an
Error, not a partial image. With DecodeOptions::strict off,
missing data and leftover bytes give an image with an Image::report
of what is missing. A flipped byte in entropy-coded data can go unnoticed,
since the format has no checksum, and damage can show up as any variant
(see the crate docs).