diff options
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | gifed/src/block/indexedimage.rs | 6 | ||||
-rw-r--r-- | gifed/src/block/packed.rs | 2 | ||||
-rw-r--r-- | gifed/src/block/palette.rs | 16 | ||||
-rw-r--r-- | gifed/src/color.rs | 6 | ||||
-rw-r--r-- | gifed/src/gif.rs | 18 | ||||
-rw-r--r-- | gifed/src/lzw.rs | 10 | ||||
-rw-r--r-- | gifed/src/reader/mod.rs | 11 |
8 files changed, 39 insertions, 31 deletions
diff --git a/Cargo.toml b/Cargo.toml index 948388b..c3f707d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,2 +1,3 @@ [workspace] members = ["gifed", "gifprobe", "gaudio", "gifcheck"] +resolver = "2" diff --git a/gifed/src/block/indexedimage.rs b/gifed/src/block/indexedimage.rs index 1868382..03c2119 100644 --- a/gifed/src/block/indexedimage.rs +++ b/gifed/src/block/indexedimage.rs @@ -17,7 +17,7 @@ impl IndexedImage { } pub fn top(&self) -> u16 { - self.image_descriptor.left + self.image_descriptor.top } pub fn width(&self) -> u16 { @@ -86,7 +86,7 @@ impl CompressedImage { } pub fn top(&self) -> u16 { - self.image_descriptor.left + self.image_descriptor.top } pub fn width(&self) -> u16 { @@ -126,7 +126,7 @@ impl CompressedImage { blocks, } = self; - let data: Vec<u8> = blocks.into_iter().map(<_>::into_iter).flatten().collect(); + let data: Vec<u8> = blocks.into_iter().flat_map(<_>::into_iter).collect(); //TODO: remove unwrap let mut decompressor = weezl::decode::Decoder::new(weezl::BitOrder::Msb, lzw_code_size); diff --git a/gifed/src/block/packed.rs b/gifed/src/block/packed.rs index 455f43a..0752e39 100644 --- a/gifed/src/block/packed.rs +++ b/gifed/src/block/packed.rs @@ -1,3 +1,5 @@ +#![allow(clippy::unusual_byte_groupings)] + #[derive(Clone, Copy, Debug, PartialEq)] pub struct GraphicPacked { pub raw: u8, diff --git a/gifed/src/block/palette.rs b/gifed/src/block/palette.rs index d5cc696..534cfeb 100644 --- a/gifed/src/block/palette.rs +++ b/gifed/src/block/palette.rs @@ -35,6 +35,10 @@ impl Palette { self.table.len() } + pub fn is_empty(&self) -> bool { + self.len() > 0 + } + /// Returns the number of items that the decoder *thinks* is in the palette. /// This is 2^(n + 1) where n = [Palette::packed_len] pub fn computed_len(&self) -> usize { @@ -47,7 +51,7 @@ impl Palette { } pub fn get(&self, index: u8) -> Option<Color> { - self.table.get(index as usize).map(|v| v.clone()) + self.table.get(index as usize).copied() } pub fn from_color<C: AsRef<Color>>(&self, color: C) -> Option<u8> { @@ -64,7 +68,7 @@ impl Palette { //TODO: gen- better docs pub fn padding(&self) -> usize { let comp = self.computed_len(); - (comp as usize - self.len()) * 3 + (comp - self.len()) * 3 } pub fn as_bytes(&self) -> Vec<u8> { @@ -77,6 +81,12 @@ impl Palette { } } +impl Default for Palette { + fn default() -> Self { + Self::new() + } +} + impl Deref for Palette { type Target = [Color]; @@ -97,7 +107,7 @@ impl TryFrom<&[u8]> for Palette { fn try_from(value: &[u8]) -> Result<Self, Self::Error> { if value.len() % 3 != 0 { - return Err(()); + Err(()) } else { Ok(Self { table: value diff --git a/gifed/src/color.rs b/gifed/src/color.rs index e1b727a..9f2bbe5 100644 --- a/gifed/src/color.rs +++ b/gifed/src/color.rs @@ -37,8 +37,8 @@ impl From<(u8, u8, u8)> for Color { } } -impl Into<[u8; 3]> for Color { - fn into(self) -> [u8; 3] { - [self.r, self.g, self.b] +impl From<Color> for [u8; 3] { + fn from(val: Color) -> Self { + [val.r, val.g, val.b] } } diff --git a/gifed/src/gif.rs b/gifed/src/gif.rs index 09b052e..1c71d13 100644 --- a/gifed/src/gif.rs +++ b/gifed/src/gif.rs @@ -23,7 +23,7 @@ impl Gif { pub fn as_bytes(&self) -> Vec<u8> { let mut out = vec![]; - out.extend_from_slice(&self.header.as_bytes()); + out.extend_from_slice(self.header.as_bytes()); out.extend_from_slice(&self.screen_descriptor.as_bytes()); if let Some(gct) = &self.global_color_table { @@ -44,7 +44,7 @@ impl Gif { File::create(path.as_ref())?.write_all(&self.as_bytes()) } - pub fn images<'a>(&'a self) -> ImageIterator<'a> { + pub fn images(&self) -> ImageIterator<'_> { ImageIterator { gif: self, block_index: 0, @@ -65,15 +65,14 @@ impl<'a> Iterator for ImageIterator<'a> { let img = loop { match self.gif.blocks.get(self.block_index) { - Some(block) => match block { - Block::CompressedImage(img) => { + Some(block) => { + if let Block::CompressedImage(img) = block { // Step over this image so we don't hit it next time self.block_index += 1; break img; } - _ => (), - }, + } None => return None, } @@ -81,7 +80,7 @@ impl<'a> Iterator for ImageIterator<'a> { }; Some(Image { - compressed: &img, + compressed: img, global_palette: self.gif.global_color_table.as_ref(), blocks: &self.gif.blocks[starting_block..self.block_index], }) @@ -123,8 +122,7 @@ impl<'a> Image<'a> { pub fn transparent_index(&self) -> Option<u8> { self.graphic_control() - .map(|gce| gce.transparent_index()) - .flatten() + .and_then(|gce| gce.transparent_index()) } pub fn frame_control(&self) -> Option<FrameControl> { @@ -193,7 +191,7 @@ pub enum FrameControl { } #[cfg(test)] -pub mod gif { +pub mod gif_test { use std::convert::TryInto; use std::io::Write; diff --git a/gifed/src/lzw.rs b/gifed/src/lzw.rs index 9fdfcdd..c5ad5b3 100644 --- a/gifed/src/lzw.rs +++ b/gifed/src/lzw.rs @@ -18,7 +18,7 @@ impl LZW { let mut next_code = eoi + 1; let mut code_size = minimum_size + 1; - let mut iter = indicies.into_iter(); + let mut iter = indicies.iter(); let mut out = BitStream::new(); let mut buffer = vec![*iter.next().unwrap()]; @@ -54,7 +54,7 @@ impl LZW { } } - if buffer.len() > 0 { + if !buffer.is_empty() { match dictionary.get(&buffer) { Some(&code) => out.push_bits(code_size, code), None => { @@ -116,7 +116,7 @@ impl BitStream { loop { if new_index >= 8 { self.formed.push(current32 as u8); - current32 = current32 >> 8; + current32 >>= 8; new_index -= 8; } else { self.current = current32 as u8; @@ -155,7 +155,7 @@ mod bitstream_test { for byte in &bsvec { print!("{:b} ", byte); } - println!(""); + println!(); assert_eq!(bsvec, vec![0b1001_1111, 0b0000_0001]); } @@ -171,7 +171,7 @@ mod bitstream_test { for byte in &bsvec { print!("{:b} ", byte); } - println!(""); + println!(); assert_eq!(bsvec, vec![0b0000_0011, 0b0001_0000]); } diff --git a/gifed/src/reader/mod.rs b/gifed/src/reader/mod.rs index aed2142..763f34e 100644 --- a/gifed/src/reader/mod.rs +++ b/gifed/src/reader/mod.rs @@ -22,7 +22,7 @@ pub struct Decoder<R: Read> { impl Decoder<BufReader<File>> { pub fn file<P: AsRef<Path>>(path: P) -> Result<Self, DecodeError> { - let file = File::open(path).map_err(|e| DecodeError::IoError(e))?; + let file = File::open(path).map_err(DecodeError::IoError)?; let buffreader = BufReader::new(file); Ok(Decoder::new(buffreader)) } @@ -61,11 +61,8 @@ impl<R: Read> Decoder<R> { let mut decoder = self.read()?; let mut blocks = vec![]; - loop { - match decoder.block()? { - Some(block) => blocks.push(block.block), - None => break, - } + while let Some(block) = decoder.block()? { + blocks.push(block.block) } Ok(Gif { @@ -283,7 +280,7 @@ impl<R: Read> SmartReader<R> { } pub fn read_palette(&mut self, count: usize) -> Result<Palette, DecodeError> { - let mut buf = vec![0; count as usize * 3]; + let mut buf = vec![0; count * 3]; self.read_exact(&mut buf)?; Ok(Palette::try_from(buf.as_slice()).unwrap()) |