Skip to main content

read_header

Function read_header 

Source
pub fn read_header(bytes: &[u8]) -> Result<Header>
Expand description

Read a JPEG 2000 codestream’s or JP2 file’s Header without decoding any tile data: the image area, each output channel’s sample format, and, for a JP2 file, the same Jp2Metadata that decode would put on Image::jp2.

This is read_header_with at the default DecodeOptions, so a fault in what it reads is the same Error that decode returns for that input. Use read_header_with to read the header for other options.

A readable header does not mean decode will succeed. The tile data is not read (except the packet headers of the last tile of a JP2 file whose tile-part has Psot = 0, described on read_header_with), so it can still be damaged, unsupported, or past a work bound (Error::Limit).

use rust_j2k::{read_header, Error};

// In real use these are a codestream's or JP2 file's bytes; the tile data
// may be missing. Invalid input is the same typed error `decode` gives.
match read_header(b"not a codestream") {
    Ok(header) => println!("{}x{}, {} channels", header.width, header.height, header.components.len()),
    Err(Error::Codestream(why)) => println!("malformed: {why}"),
    Err(e) => println!("refused: {e}"),
}