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,
impl<R> Decoder<R>where
R: BitReader,
Sourcepub fn new(reader: R, min_code_size: u8) -> Decoder<R>
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}
Sourcepub fn decode_bytes(&mut self, bytes: &[u8]) -> Result<(usize, &[u8])>
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more