Struct Decoder

Source
pub struct Decoder<R: BitReader> { /* private fields */ }
Expand description

Decoder for a LZW compressed stream (this algorithm is used for GIF files).

The maximum supported code size is 16 bits. The decoder assumes two special code word to be present in the stream:

  • CLEAR_CODE == 1 << min_code_size
  • END_CODE == CLEAR_CODE + 1

Furthermore the decoder expects the stream to start with a CLEAR_CODE. This corresponds to the implementation needed for en- and decoding GIF and TIFF files.

Implementations§

Source§

impl<R> Decoder<R>
where R: BitReader,

Source

pub fn new(reader: R, min_code_size: u8) -> Decoder<R>

Creates a new LZW decoder.

Examples found in repository?
examples/lzw-decompress.rs (line 10)
7fn main() {
8    match (|| -> io::Result<()> {
9        let mut decoder = 
10            lzw::Decoder::new(lzw::LsbReader::new(), 8)
11        ;
12        let stdout = io::stdout();
13        let mut stdout = BufWriter::new(stdout.lock());
14        let stdin = io::stdin();
15        let mut stdin = stdin.lock();
16        loop {
17            let len = {
18                let buf = try!(stdin.fill_buf());
19                if buf.len() == 0 {
20                    break
21                }
22                let (len, bytes) = try!(decoder.decode_bytes(buf));
23                try!(stdout.write_all(bytes));
24                len
25            };
26            stdin.consume(len);
27        }
28        Ok(())
29    })() {
30        Ok(()) => (),
31        Err(err) => { let _ = write!(io::stderr(), "{}", err); }
32    }
33    
34}
Source

pub fn decode_bytes(&mut self, bytes: &[u8]) -> Result<(usize, &[u8])>

Tries to obtain and decode a code word from bytes.

Returns the number of bytes that have been consumed from bytes. An empty slice does not indicate EOF.

Examples found in repository?
examples/lzw-decompress.rs (line 22)
7fn main() {
8    match (|| -> io::Result<()> {
9        let mut decoder = 
10            lzw::Decoder::new(lzw::LsbReader::new(), 8)
11        ;
12        let stdout = io::stdout();
13        let mut stdout = BufWriter::new(stdout.lock());
14        let stdin = io::stdin();
15        let mut stdin = stdin.lock();
16        loop {
17            let len = {
18                let buf = try!(stdin.fill_buf());
19                if buf.len() == 0 {
20                    break
21                }
22                let (len, bytes) = try!(decoder.decode_bytes(buf));
23                try!(stdout.write_all(bytes));
24                len
25            };
26            stdin.consume(len);
27        }
28        Ok(())
29    })() {
30        Ok(()) => (),
31        Err(err) => { let _ = write!(io::stderr(), "{}", err); }
32    }
33    
34}

Trait Implementations§

Source§

impl<R: Debug + BitReader> Debug for Decoder<R>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<R> Freeze for Decoder<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for Decoder<R>
where R: RefUnwindSafe,

§

impl<R> Send for Decoder<R>
where R: Send,

§

impl<R> Sync for Decoder<R>
where R: Sync,

§

impl<R> Unpin for Decoder<R>
where R: Unpin,

§

impl<R> UnwindSafe for Decoder<R>
where R: UnwindSafe,

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.