gifenc: improve no-compression mode by keeping code lengths the same
authorKostya Shishkov <kostya.shishkov@gmail.com>
Mon, 11 Sep 2023 10:08:52 +0000 (12:08 +0200)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Mon, 11 Sep 2023 10:08:52 +0000 (12:08 +0200)
nihav-commonfmt/src/codecs/gifenc.rs

index 6bb9263055606df4ffd1832160a3b915c1c8f256..3055accb806cff98e8b629faaaae0eadfa463b30 100644 (file)
@@ -134,7 +134,12 @@ impl LZWEncoder {
 
         match self.level {
             CompressionLevel::None => {
+                let sym_limit = 1 << (clr_bits + 1);
                 for &b in src.iter() {
+                    if self.dict.cur_size >= sym_limit {
+                        bw.write(u32::from(self.dict.clear_code), self.dict.bit_len);
+                        self.dict.reset();
+                    }
                     bw.write(u32::from(b), self.dict.bit_len);
                     self.dict.add(usize::from(b), 0);
                 }
@@ -636,7 +641,7 @@ mod test {
         let enc_options = &[
                 NAOption { name: "compr", value: NAValue::String("none".to_string()) },
             ];
-        test_gif_encoder_single("none.gif", enc_options, &[0x2767a289, 0xdef9ad30, 0xca4c289b, 0x1fd0ec19]);
+        test_gif_encoder_single("none.gif", enc_options, &[0x32900cff, 0xef979bb0, 0x2d0355e8, 0x424bddee]);
     }
     #[test]
     fn test_gif_single_fast() {