Skip to content

Commit

Permalink
Break when best possible filter result found
Browse files Browse the repository at this point in the history
  • Loading branch information
andrews05 committed Nov 26, 2024
1 parent 490a469 commit 4846650
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/png/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,16 @@ impl PngImage {
let mut prev_line = Vec::new();
let mut prev_pass: Option<u8> = None;
let mut f_buf = Vec::new();
let mut best_possible = 0;
for line in self.scan_lines(false) {
if prev_pass != line.pass || line.data.len() != prev_line.len() {
prev_line = vec![0; line.data.len()];
// Calculate the best possible entropy for this pass
best_possible = match filter {
RowFilter::Entropy => ilog2i(line.data.len() as u32 + 1) as i32,
RowFilter::BigEnt => ilog2i(line.data.len() as u32) as i32,
_ => 0,
}
}
// Alpha optimisation may alter the line data, so we need a mutable copy of it
let mut line_data = line.data.to_vec();
Expand Down Expand Up @@ -383,6 +390,10 @@ impl PngImage {
best_size = size;
std::mem::swap(&mut best_line, &mut f_buf);
best_line_raw.clone_from(&line_data);
if size == 0 {
// Best possible result
break;
}
}
}
}
Expand All @@ -406,6 +417,10 @@ impl PngImage {
best_size = size;
std::mem::swap(&mut best_line, &mut f_buf);
best_line_raw.clone_from(&line_data);
if size == best_possible {
// Best possible result
break;
}
}
}
}
Expand All @@ -425,6 +440,10 @@ impl PngImage {
best_size = size;
std::mem::swap(&mut best_line, &mut f_buf);
best_line_raw.clone_from(&line_data);
if size == 1 {
// Best possible result
break;
}
}
}
}
Expand All @@ -445,6 +464,10 @@ impl PngImage {
best_size = size;
std::mem::swap(&mut best_line, &mut f_buf);
best_line_raw.clone_from(&line_data);
if size == best_possible {
// Best possible result
break;
}
}
}
}
Expand Down

0 comments on commit 4846650

Please sign in to comment.