#[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
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.resolution_reduction: u8How 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: boolReturn 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:
- Expand the index planes. For each entry of
Jp2Metadata::channel_mapping, in order, take the raw channelcomponent; aMappingKind::Directentry is used as it is, and aMappingKind::Paletteentry looks each sample up as an index into that column ofJp2Metadata::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: columncolumnfor a palette entry, and columnifor a direct entry at positioni, as OpenJPEG has it. Without a palette the raw channels are used as they are. - 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 acdefthe order from step 1 stands.
The result is the channels of the default decode. No effect on a raw
codestream. Default false.
color: ColorOutputWhat color space the color channels come out in. Default
ColorOutput::AsCoded.
packet_work_limit: WorkLimitHow 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: WorkLimitHow much work decoding the code-blocks may take, against the bound
described on decode. Default WorkLimit::Default.
sample_limit: WorkLimitHow 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: boolRefuse 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
SOTmarker, which ends the main header: inside a tile-part, its header or its packets, with or without theEOCmarker. Tiles with no tile-part are filled with 0, and packets that were never read (cut off, or left out by thePOCvolumes) 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
impl DecodeOptions
Sourcepub fn with_resolution_reduction(self, levels: u8) -> Self
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);Sourcepub fn with_raw_channels(self, raw: bool) -> Self
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);Sourcepub fn with_color(self, color: ColorOutput) -> Self
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);Sourcepub fn with_packet_work_limit(self, limit: WorkLimit) -> Self
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));Sourcepub fn with_code_block_work_limit(self, limit: WorkLimit) -> Self
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);Sourcepub fn with_sample_limit(self, limit: WorkLimit) -> Self
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));Sourcepub fn with_strict(self, strict: bool) -> Self
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);