Skip to content

Commit

Permalink
Avoid searching entire pixel buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Jan 16, 2024
1 parent bf39633 commit 4ce5e09
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,17 @@ impl<W: Write> Encoder<W> {
///
/// The first byte is the minimum code size, followed by LZW data.
fn lzw_encode(data: &[u8], buffer: &mut Vec<u8>) {
let min_code_size = match flag_size(1 + data.iter().copied().max().unwrap_or(0) as usize) + 1 {
let mut max_byte = 0;
for &byte in data {
if byte > max_byte {
max_byte = byte;
// code size is the same after that
if byte > 128 {
break;
}
}
}
let min_code_size = match flag_size(1 + max_byte as usize) + 1 {
1 => 2, // As per gif spec: The minimal code size has to be >= 2
n => n,
};
Expand Down

0 comments on commit 4ce5e09

Please sign in to comment.