#[non_exhaustive]pub enum Error {
Codestream(String),
Marker(String),
FileFormat(String),
Unsupported(String),
Limit(String),
InvalidOptions(String),
Inconsistent(String),
}Expand description
A decode failure, tagged by the class of fault.
Match on the variant to decide what to do; the String inside is a
diagnostic for a log. Its wording can change between releases, so do not
parse it or compare it. Input that is not JPEG 2000 at all, empty input
and plain text included, is Error::Codestream.
The variant also says what to tell a user:
| Variant | A message for a user |
|---|---|
Codestream, Marker, FileFormat | The file is damaged, truncated or not a JPEG 2000 image. |
Unsupported | The file uses a JPEG 2000 feature this decoder does not read. |
Limit | The file is larger or costlier than the decoder allows by default. If the message names a DecodeOptions limit, it can be retried with a higher WorkLimit. |
InvalidOptions | The decode options are invalid, for any file (WorkLimit::Raised(0), sRGB output with raw_channels) or for this one (a reduction past the file’s levels). |
Inconsistent | An internal error; please file a bug. |
Damage does not always show as the first row. A flipped bit in a header
field can read as an absurd image size (Limit), an unknown marker or a
feature the decoder does not read (Unsupported), so a corrupt file can
come back as any of those. Do not retry a Limit with
WorkLimit::Unlimited unless the source
is trusted.
use rust_j2k::{Error, decode};
fn explain(error: &Error) -> &'static str {
match error {
Error::Codestream(_) | Error::Marker(_) | Error::FileFormat(_) => {
"the file is damaged, truncated or not a JPEG 2000 image"
}
Error::Unsupported(_) => "the file uses a feature this decoder does not read",
Error::Limit(_) => "the file is larger or costlier than the decoder allows",
Error::InvalidOptions(_) => "the decode options do not fit this file",
// The enum is `#[non_exhaustive]`: a later version can add variants.
_ => "the file could not be decoded",
}
}
let error = decode(b"").unwrap_err();
assert!(matches!(error, Error::Codestream(_)));
assert_eq!(
explain(&error),
"the file is damaged, truncated or not a JPEG 2000 image"
);Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Codestream(String)
Not a JPEG 2000 codestream, or its structure is broken: a truncated or
missing segment, lost sync, or corrupt packet / coded data. Any input
that is not JPEG 2000 at all (empty input, text, another image
format) is this variant, since it has no SOC marker.
Marker(String)
A marker segment has an invalid field: a reserved value, a bad length or an out-of-range value.
FileFormat(String)
The JP2 box structure or a box field is malformed: a truncated box, a missing or misplaced required box, an invalid field value, or a box that disagrees with the codestream.
Unsupported(String)
A valid-but-not-decoded feature: an HTJ2K codestream, a Part 2 extension, a JP2 box this decoder cannot apply, a file that is not JP2-compatible. A newer version of the decoder may accept the same input.
Limit(String)
The input exceeded one of the decoder’s resource guards: a hostile or
absurd declaration (code-block count, precinct count, bit-plane depth)
past a cap, an image of more samples than the sample limit allows, or
work past the bound for the file’s size (see decode).
It is not a missing feature (that is Error::Unsupported), and the
file may be valid.
Three of these can be changed, each with a WorkLimit:
the sample limit
(DecodeOptions::sample_limit),
which holds the image area and the samples that palette expansion, sYCC
chroma replication and a monochrome ICC conversion produce; reading the
packet headers
(DecodeOptions::packet_work_limit);
and decoding the code-blocks
(DecodeOptions::code_block_work_limit).
The message of one of them names the option and a WorkLimit::Raised
factor; the factor is a hint (see WorkLimit), not
a measure of the file.
The other guards have no option, so a file past one of them is refused
whatever the options. A tile may hold at most 2^19 code-blocks and
2^18 precincts, and 32 progression order change (POC) volumes; a
code-block may have at most 30 bit-planes; and under a sample_limit
of WorkLimit::Unlimited, an image too large for the target to
address is still refused. WorkLimit has an
example that retries.
InvalidOptions(String)
The caller passed DecodeOptions that are
invalid for this codestream, or that are invalid whatever the input
(sRGB output with raw_channels, or a limit of WorkLimit::Raised(0)).
The input itself may be fine.
Inconsistent(String)
A decoder invariant was violated (declared geometry and decoded state disagreed where they never should); please file a bug.
Trait Implementations§
impl Eq for Error
Source§impl Error for Error
impl Error for Error
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()