pub struct IncrementalDecoder { /* private fields */ }Expand description
A decoder for a file that arrives a piece at a time, from a network or a slow disk.
feed appends bytes as they come in, and
refine decodes what has come in so far. Each image
refine returns is the image decode_with gives with
strict off on the bytes fed so far, with its
report saying what those bytes do not hold yet: the
quality layers and resolutions that are complete, and the tiles still
missing. bytes_decoded is how many bytes that
image is the decode of. Once the input ends, finish
decodes all of it under the decoder’s options.
refine does not have to decode every time it is called, so a caller
can call it after every read, however small. It decodes at least each
time the bytes fed have doubled since it last decoded or tried to, and
may decode more often; when it does not decode, it returns Ok(None).
The first refine called with at least one byte fed tries; one called
with none fed returns Ok(None) and is not a try. A try that a work
bound refuses counts, so the next refine waits for twice its bytes.
The decoder keeps each tile’s packet walk (what it has read of the
tile’s packet headers so far) between refines, so a refine reads only
the packets that have come in since the last one. Over all
the refines, the packet headers are read about once, as one decode reads
them; a packet the data cuts is read again by each refine (the packets
a refine reads
again).
Each refine is held to the packet bound of a decode of its bytes on its
own: it is charged the packet visits that decode would spend, and a
refine that decode is refused for returns Ok(None).
The code-block work is done again by each refine that decodes, so the
code-block work every refine has spent is held to the bound one decode
of the bytes fed has, raised as the options say. A refine the rest of
that bound cannot cover returns Ok(None), so a file whose code-blocks
cost a lot may give no image before finish, which is a decode of its
own, under its own bound. Each refine that returns an image also builds
the whole image, as one decode does: it allocates the samples and runs
the inverse wavelet and color transforms over them, outside the work
bounds. Under the doubling cadence a file of n bytes gives at most
about log2(n) images, so a large image fed in small pieces costs up to
that many reconstructions.
Between calls the decoder keeps the bytes fed and the tiles’ packet
walks. The walks kept hold at most twice what one decode holds at its
peak: twice the image’s samples, plus twice the packet walk of the
largest tile. A tile whose walk would pass that is walked from the start
by each refine instead. Each refine also holds what decode_with
holds for the same bytes, so the decoder holds at most about three
times one decode’s peak memory, plus the bytes fed.
That memory follows the image size the file declares, not the bytes fed:
a file of 1228 bytes that declares a 16384 x 16384 image holds about
640 MB between calls, until the decoder is dropped. A server that keeps
many slow streams open should check the size that
read_header reports before it feeds the rest of
a file, or drop the decoders it is not refining.
use rust_j2k::{DecodeOptions, IncrementalDecoder};
let file = std::fs::read("image.j2k")?;
let mut decoder = IncrementalDecoder::new(DecodeOptions::default())?;
// The file arrives 500 bytes at a time.
for piece in file.chunks(500) {
decoder.feed(piece);
if let Some(image) = decoder.refine()? {
// Show the image of what has arrived so far. Its report says
// what is still missing.
println!(
"{} bytes: {}x{}, complete: {}",
decoder.bytes_decoded(),
image.width,
image.height,
image.report.is_complete()
);
}
}
// The input has ended: decode all of it, under the decoder's options
// (strict here).
let image = decoder.finish()?;
assert!(image.report.is_complete());An error from refine is the error decode_with gives with strict
off on the same bytes, such as a marker segment with an invalid field,
or an Error::Limit that does not depend on how many bytes there are:
an image past the sample limit, too many precincts or code-blocks, too
many POC volumes, or too many bit-planes. More bytes do not undo
these, so once refine returns an error, every later refine returns
the same error without decoding. Two refusals are not final while bytes
may still come, so refine returns Ok(None) for them and only
finish reports them: a main header cut short (Error::Incomplete),
and a refusal by the packet or code-block work bound, which more bytes
raise.
Implementations§
Source§impl IncrementalDecoder
impl IncrementalDecoder
Sourcepub fn new(options: DecodeOptions) -> Result<Self>
pub fn new(options: DecodeOptions) -> Result<Self>
A decoder with no bytes fed yet, which decodes under options.
Options that are invalid whatever the input, which
decode_with refuses before it reads a byte, are
Error::InvalidOptions here. Options that do not fit the file,
such as a reduction past its levels, are found by the first decode
that reads its header.
Sourcepub fn bytes_decoded(&self) -> usize
pub fn bytes_decoded(&self) -> usize
How many bytes the last image refine returned is
the decode of, or 0 before it has returned one.
Sourcepub fn refine(&mut self) -> Result<Option<Image>>
pub fn refine(&mut self) -> Result<Option<Image>>
Decode the bytes fed so far. This decodes at least each time they have doubled since it last decoded or tried to, and may decode more often.
The image is the one decode_with gives with
strict off on all the bytes fed, and its
report lists what they do not hold yet.
Ok(None) means there is no new image yet: too few bytes have come
in since the last decode, the main header is not whole, or a work
bound refused the decode. Any error is the one decode_with gives
on those bytes, and more bytes will not clear it: each later call
returns it again.
Sourcepub fn finish(&self) -> Result<Image>
pub fn finish(&self) -> Result<Image>
The input has ended: decode all of it under the decoder’s options,
as decode_with does.
With strict on, the default, a file cut
short is refused as decode_with refuses it. With strict off, it
gives the image the bytes hold, with a report of what is missing. In
both cases, a file that ends before its main header is whole, which
refine waits on, is truncated now:
Error::FileFormat when it ends inside the boxes of a JP2 file
before its codestream, and Error::Codestream when it ends inside
the codestream’s main header.
This is a decode of its own, also after a refine has returned an
error: under strict options it may refuse the bytes for an earlier
fault, such as the data ending, than the one refine found.
finish borrows the decoder and keeps nothing from the call, so it
can be called again and gives the same result. When it refuses a
work limit, into_bytes gives the bytes back to
decode with the limit raised.
Sourcepub fn into_bytes(self) -> Vec<u8> ⓘ
pub fn into_bytes(self) -> Vec<u8> ⓘ
Consumes the decoder and returns the bytes fed.
After finish refuses a work limit, decode these
with decode_with, or feed them to a new decoder, under options
with the limit raised.