Struct Decoder
pub struct Decoder { /* private fields */ }
The state for decoding data with an LZW algorithm.
The same structure can be utilized with streams as well as your own buffers and driver logic. It may even be possible to mix them if you are sufficiently careful not to lose or skip any already decode data in the process.
This is a sans-IO implementation, meaning that it only contains the state of the decoder and
the caller will provide buffers for input and output data when calling the basic
decode_bytes method. Nevertheless, a number of adapters are provided in the into_*
methods for decoding with a particular style of common IO.
decodefor decoding once without any IO-loop.into_asyncfor decoding with thefuturestraits for asynchronous IO.into_streamfor decoding with the standardiotraits.into_vecfor in-memory decoding.
Implementations
impl Decoder
fn new(order: BitOrder, size: u8) -> SelfCreate a new decoder with the specified bit order and symbol size.
The algorithm for dynamically increasing the code symbol bit width is compatible with the original specification. In particular you will need to specify an
Lsbbit oder to decode the data portion of a compressedgifimage.Panics
The
sizeneeds to be in the interval0..=12.fn with_tiff_size_switch(order: BitOrder, size: u8) -> SelfCreate a TIFF compatible decoder with the specified bit order and symbol size.
The algorithm for dynamically increasing the code symbol bit width is compatible with the TIFF specification, which is a misinterpretation of the original algorithm for increasing the code size. It switches one symbol sooner.
Panics
The
sizeneeds to be in the interval0..=12.fn decode_bytes(&mut self, inp: &[u8], out: &mut [u8]) -> BufferResultDecode some bytes from
inpand write result toout.This will consume a prefix of the input buffer and write decoded output into a prefix of the output buffer. See the respective fields of the return value for the count of consumed and written bytes. For the next call You should have adjusted the inputs accordingly.
The call will try to decode and write as many bytes of output as available. It will be much more optimized (and avoid intermediate buffering) if it is allowed to write a large contiguous chunk at once.
See
into_streamfor high-level functions (that are only available with thestdfeature).fn decode(&mut self, data: &[u8]) -> Result<Vec<u8>, LzwError>Decode a single chunk of lzw encoded data.
This method requires the data to contain an end marker, and returns an error otherwise.
This is a convenience wrapper around
into_vec. Use theinto_vecadapter to customize buffer size, to supply an existing vector, to control whether an end marker is required, or to preserve partial data in the case of a decoding error.Example
use ; // Encoded that was created with an encoder. let data = b"\x80\x04\x81\x94l\x1b\x06\xf0\xb0 \x1d\xc6\xf1\xc8l\x19 \x10"; let decoded = new .decode .unwrap; assert_eq!;fn into_stream<W: Write>(&mut self, writer: W) -> IntoStream<'_, W>Construct a decoder into a writer.
fn into_vec<'lt>(&'lt mut self, vec: &'lt mut Vec<u8>) -> IntoVec<'lt>Construct a decoder into a vector.
All decoded data is appended and the vector is not cleared.
Compared to
into_streamthis interface allows a high-level access to decoding without requires thestd-feature. Also, it can make full use of the extra buffer control that the special target exposes.fn has_ended(&self) -> boolCheck if the decoding has finished.
No more output is produced beyond the end code that marked the finish of the stream. The decoder may have read additional bytes, including padding bits beyond the last code word but also excess bytes provided.
fn reset(&mut self)Reset all internal state.
This produce a decoder as if just constructed with
newbut taking slightly less work. In particular it will not deallocate any internal allocations. It will also avoid some duplicate setup work.
Auto Trait Implementations
impl !RefUnwindSafe for Decoder
impl !Sync for Decoder
impl !UnwindSafe for Decoder
impl Freeze for Decoder
impl Send for Decoder
impl Unpin for Decoder
impl UnsafeUnpin for Decoder
Blanket Implementations
impl<T> Any for Decoder
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Decoder
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Decoder
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> From<T> for Decoder
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into<U> for Decoder
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Decoder
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for Decoder
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>