fix clippy warnings
[nihav.git] / nihav-core / src / compr / deflate.rs
index e3f1f13bf002adf8da28b7426ec2529095b62350..9277731d2301b02fc212137b637f4c53aa095441 100644 (file)
@@ -210,7 +210,7 @@ impl<'a, S: Copy> CodebookReader<S> for CurrentSource<'a> {
         let mut lut_bits = cb.lut_bits;
         let orig_br = self.br;
         while esc {
-            let lut_idx = (self.peek(lut_bits) as usize) + (idx as usize);
+            let lut_idx = (self.peek(lut_bits) as usize) + idx;
             if cb.table[lut_idx] == TABLE_FILL_VALUE { return Err(CodebookError::InvalidCode); }
             let bits = cb.table[lut_idx] & 0x7F;
             esc  = (cb.table[lut_idx] & 0x80) != 0;
@@ -221,7 +221,7 @@ impl<'a, S: Copy> CodebookReader<S> for CurrentSource<'a> {
                 self.refill();
                 return Err(CodebookError::MemoryError);
             }
-            self.skip(skip_bits as u32).unwrap();
+            self.skip(skip_bits).unwrap();
             lut_bits = bits as u8;
         }
         Ok(cb.syms[idx])
@@ -1583,25 +1583,25 @@ fn gen_tree(codes: &mut [u16], lens: &mut [u8], num_codes: &mut usize, stats: &m
         *num_codes = 0;
         return;
     }
-    while tot_w > (1 << max_bits) {
-        for w in stats.iter_mut() {
-            *w = (*w + 1) >> 1;
-        }
-        tot_w = 0;
-        for &w in stats.iter() {
-            tot_w += w;
+    loop {
+        let mut tree = Tree::new();
+        for (sym, &w) in stats.iter().enumerate() {
+            tree.insert(Node{ sym: sym as u16, w: w as u16, idx0: 64000, idx1: 64000 });
         }
-    }
-    let mut tree = Tree::new();
-    for (sym, &w) in stats.iter().enumerate() {
-        tree.insert(Node{ sym: sym as u16, w: w as u16, idx0: 64000, idx1: 64000 });
-    }
-    tree.trim();
-    tree.build();
+        tree.trim();
+        tree.build();
 
-    for n in tree.nodes[..tree.nnodes].iter() {
-        if n.sym != NODE_SYM {
-            lens[n.sym as usize] = n.w as u8;
+        for n in tree.nodes[..tree.nnodes].iter() {
+            if n.sym != NODE_SYM {
+                lens[n.sym as usize] = n.w as u8;
+            }
+        }
+        if !lens.iter().any(|&x| x > max_bits) {
+            break;
+        } else {
+            for w in stats.iter_mut() {
+                *w = (*w + 1) >> 1;
+            }
         }
     }
     lengths_to_codes16(lens, codes);
@@ -1918,22 +1918,19 @@ impl LZParse for OptimalParser {
 }
 
 ///! Deflate compression mode.
-#[derive(Clone,Copy,Debug,PartialEq)]
+#[derive(Clone,Copy,Debug,PartialEq,Default)]
 pub enum DeflateMode {
     ///! No compression.
     NoCompr,
     ///! Fast compression.
     Fast,
     ///! Still fast but better compression.
+    #[default]
     Better,
     ///! Slow but the best compression.
     Best,
 }
 
-impl Default for DeflateMode {
-    fn default() -> Self { DeflateMode::Better }
-}
-
 pub const DEFLATE_MODE_DESCRIPTION: &str = "Deflate compression level.";
 ///! Deflate option for no compression.
 pub const DEFLATE_MODE_NONE: &str = "none";