Skip to main content

Error

Enum Error 

Source
#[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:

VariantA message for a user
Codestream, Marker, FileFormatThe file is damaged, truncated or not a JPEG 2000 image.
UnsupportedThe file uses a JPEG 2000 feature this decoder does not read.
LimitThe 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.
InvalidOptionsThe 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).
InconsistentAn 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
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

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§

Source§

impl Clone for Error

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Error

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Error

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Error

Source§

impl Error for Error

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl PartialEq for Error

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Error

Auto Trait Implementations§

§

impl Forget for Error

§

impl Freeze for Error

§

impl Move for Error

§

impl RefUnwindSafe for Error

§

impl Send for Error

§

impl Sync for Error

§

impl Unpin for Error

§

impl UnsafeUnpin for Error

§

impl UnwindSafe for Error

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.