Skip to main content

DecodeOptions

Struct DecodeOptions 

Source
#[non_exhaustive]
pub struct DecodeOptions { pub resolution_reduction: u8, pub raw_channels: bool, pub color: ColorOutput, pub packet_work_limit: WorkLimit, pub code_block_work_limit: WorkLimit, pub sample_limit: WorkLimit, pub strict: bool, }
Expand description

Options for decode_with. decode is decode_with at DecodeOptions::default.

Start from DecodeOptions::default and set what differs with its with_* method, or the field directly. The struct is #[non_exhaustive], so a new option is not a breaking change.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§resolution_reduction: u8

How many of the finest resolution levels to discard. Each level about halves the output in both axes, so 1 decodes a half-size image, 2 a quarter-size, and so on: the wavelet pyramid’s own lower resolutions, not a resample. The exact size depends on the image origin; Image and ComponentInfo::width give the formula. Every component must keep at least one resolution: a reduction that consumes some component’s whole pyramid is rejected as Error::InvalidOptions. Header::max_reduction gives the largest the main header allows.

§raw_channels: bool

Return a JP2 file’s codestream components as coded, without applying the file’s palette (pclr + cmap) or channel definitions (cdef): the file decodes exactly as its codestream does on its own, in SIZ order, and Jp2Metadata::palette_applied is false. It means no color conversion either, so it cannot be combined with ColorOutput::Srgb (Error::InvalidOptions). Each index plane is reported on Jp2Metadata::channels as ChannelType::Unspecified with Association::None, since it is not a color until it goes through the palette; every other channel is labeled as a file without a cdef is (see Jp2Metadata::channels), whatever the cdef box says. The boxes’ layout is still checked, so a malformed pclr, cmap or cdef is still Error::FileFormat; the palette mapping and column depths, and the channel definitions’ entries, are not.

The metadata carries what the decode left out, so a caller can rebuild the default decode, in order, from the raw one:

  1. Expand the index planes. For each entry of Jp2Metadata::channel_mapping, in order, take the raw channel component; a MappingKind::Direct entry is used as it is, and a MappingKind::Palette entry looks each sample up as an index into that column of Jp2Metadata::palette (an index past the table reads its last entry, a negative one its first). Each channel takes its bit depth and sign from its palette column: column column for a palette entry, and column i for a direct entry at position i, as OpenJPEG has it. Without a palette the raw channels are used as they are.
  2. Put the channels in color order with Jp2Metadata::channel_definitions, by the rule documented there (each swap looks up where the entry’s channel has moved to, not its original position). Without a cdef the order from step 1 stands.

The result is the channels of the default decode. No effect on a raw codestream. Default false.

§color: ColorOutput

What color space the color channels come out in. Default ColorOutput::AsCoded.

§packet_work_limit: WorkLimit

How much work reading the packet headers may take, against the bound described on decode. Default WorkLimit::Default.

This also bounds the walk of a Psot = 0 tile that read_header_with describes, so read_header_with takes the same option.

§code_block_work_limit: WorkLimit

How much work decoding the code-blocks may take, against the bound described on decode. Default WorkLimit::Default.

§sample_limit: WorkLimit

How many samples the decoded image may hold, against the limit described on decode: 2^28 samples (1 GiB as i32) by default, summed over the output channels at resolution_reduction. Default WorkLimit::Default.

Raised(n) allows n times 2^28 samples. Unlimited lifts the limit: the decode allocates what the header declares, 4 bytes per sample for the output, and about 1.35 to 3.2 times that in all at its peak (see the sample limit), so a small hostile file can ask for many gigabytes. If the allocation fails the process aborts, as Rust’s allocation failures do. Only an image too large for this target to address (isize::MAX bytes of samples) is still refused, with Error::Limit. Use it only for files from a source you trust.

read_header_with checks the same limit, so it refuses what the decode would.

§strict: bool

Refuse missing data with an Error (true, the default), or decode what is there and say what is missing on Image::report (false). It is the same switch as OpenJPEG’s strict mode (opj_decoder_set_strict_mode, opj_decompress -allow-partial) and GDAL’s STRICT option.

false relaxes these two things and no others; any other relaxation is a separate option:

  • The input may end anywhere after the first SOT marker, which ends the main header: inside a tile-part, its header or its packets, with or without the EOC marker. Tiles with no tile-part are filled with 0, and packets that were never read (cut off, or left out by the POC volumes) add nothing to their code-blocks. A tile-part count (TNsot) that disagrees with the tile-parts found is taken as advisory.
  • Bytes left after a tile’s last packet are skipped rather than refused, and the tile is reported as possibly wrong.

Everything else is refused as a strict decode refuses it: a damaged packet header, coded data that fails to decode, a marker fault, tile-parts out of order or with a gap, and every Error::Limit. The work bounds and the sample limit hold as they do when strict, counted on the data that is there. A missing tile is not walked, but each of its tile-components is charged against the packet work bound, as are the packets after the end of the data, so a short input cannot ask for unbounded work.

For a codestream as it streams in: a prefix that holds every packet of the resolutions a reduction keeps decodes at that reduction to the same samples as the whole codestream, and the report’s complete_reduction says which reduction that is.

use rust_j2k::{DecodeOptions, Error, decode_with, read_header};

let bytes = include_bytes!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/tests/fixtures/jpeg2000_regular_latlon.j2k"
));
let prefix = &bytes[..bytes.len() - 20];
// Strict, the default: a cut codestream is an error.
assert!(matches!(
    decode_with(prefix, &DecodeOptions::default()),
    Err(Error::Codestream(_))
));
// Not strict: the image, and what it lacks.
let options = DecodeOptions::default().with_strict(false);
let image = decode_with(prefix, &options).expect("decodes");
assert!(!image.report.is_complete());
// The coarsest level a decode may ask for, and the finest the data holds.
let max = read_header(prefix).expect("the main header is there").max_reduction;
if let Some(r) = image.report.complete_reduction.filter(|&r| r <= max) {
    let coarse = decode_with(prefix, &options.with_resolution_reduction(r)).expect("decodes");
    assert!(coarse.report.is_complete());
}

Implementations§

Source§

impl DecodeOptions

Source

pub fn with_resolution_reduction(self, levels: u8) -> Self

These options with resolution_reduction set to levels.

use rust_j2k::DecodeOptions;

let options = DecodeOptions::default().with_resolution_reduction(1);
assert_eq!(options.resolution_reduction, 1);
Source

pub fn with_raw_channels(self, raw: bool) -> Self

These options with raw_channels set to raw.

use rust_j2k::DecodeOptions;

let options = DecodeOptions::default().with_raw_channels(true);
assert!(options.raw_channels);
Source

pub fn with_color(self, color: ColorOutput) -> Self

These options with color set to color.

use rust_j2k::{ColorOutput, DecodeOptions};

let options = DecodeOptions::default().with_color(ColorOutput::Srgb);
assert_eq!(options.color, ColorOutput::Srgb);
Source

pub fn with_packet_work_limit(self, limit: WorkLimit) -> Self

These options with packet_work_limit set to limit.

use rust_j2k::{DecodeOptions, WorkLimit};

let options = DecodeOptions::default().with_packet_work_limit(WorkLimit::Raised(4));
assert_eq!(options.packet_work_limit, WorkLimit::Raised(4));
Source

pub fn with_code_block_work_limit(self, limit: WorkLimit) -> Self

These options with code_block_work_limit set to limit.

use rust_j2k::{DecodeOptions, WorkLimit};

let options = DecodeOptions::default().with_code_block_work_limit(WorkLimit::Unlimited);
assert_eq!(options.code_block_work_limit, WorkLimit::Unlimited);
Source

pub fn with_sample_limit(self, limit: WorkLimit) -> Self

These options with sample_limit set to limit.

use rust_j2k::{DecodeOptions, WorkLimit};

// Up to 3 x 2^28 samples, enough for a 16384 x 16384 RGB image.
let options = DecodeOptions::default().with_sample_limit(WorkLimit::Raised(3));
assert_eq!(options.sample_limit, WorkLimit::Raised(3));
Source

pub fn with_strict(self, strict: bool) -> Self

These options with strict set to strict.

use rust_j2k::DecodeOptions;

assert!(DecodeOptions::default().strict);
let options = DecodeOptions::default().with_strict(false);
assert!(!options.strict);

Trait Implementations§

Source§

impl Clone for DecodeOptions

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 DecodeOptions

Source§

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

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

impl Default for DecodeOptions

Source§

fn default() -> Self

Full resolution, components as the file’s boxes shape them, color as coded, the default limits, and strict.

Auto Trait Implementations§

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, 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.