Skip to content

Commit

Permalink
Use best_possible for all strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
andrews05 committed Nov 26, 2024
1 parent f6842f2 commit c80d273
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/png/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,10 @@ impl PngImage {
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
// Calculate the best possible result for this pass
best_possible = match filter {
RowFilter::Entropy => ilog2i(line.data.len() as u32 + 1) as i32,
RowFilter::Bigrams => 1,
RowFilter::BigEnt => ilog2i(line.data.len() as u32) as i32,
_ => 0,
}
Expand Down Expand Up @@ -379,18 +380,18 @@ impl PngImage {
RowFilter::MinSum => {
// MSAD algorithm mentioned in libpng reference docs
// http://www.libpng.org/pub/png/book/chapter09.html
let mut best_size = usize::MAX;
let mut best_size = i32::MAX;
for f in try_filters {
f.filter_line(bpp, &mut line_data, &prev_line, &mut f_buf, alpha_bytes);
let size = f_buf.iter().fold(0, |acc, &x| {
let signed = x as i8;
acc + signed.unsigned_abs() as usize
acc + signed.unsigned_abs() as i32
});
if size < best_size {
best_size = size;
std::mem::swap(&mut best_line, &mut f_buf);
best_line_raw.clone_from(&line_data);
if size == 0 {
if size == best_possible {
// Best possible result
break;
}
Expand Down Expand Up @@ -427,20 +428,20 @@ impl PngImage {
RowFilter::Bigrams => {
// Count distinct bigrams, from pngwolf
// https://bjoern.hoehrmann.de/pngwolf/
let mut best_size = usize::MAX;
let mut best_size = i32::MAX;
for f in try_filters {
f.filter_line(bpp, &mut line_data, &prev_line, &mut f_buf, alpha_bytes);
let mut set = bitarr![0; 0x10000];
for pair in f_buf.windows(2) {
let bigram = (pair[0] as usize) << 8 | pair[1] as usize;
set.set(bigram, true);
}
let size = set.count_ones();
let size = set.count_ones() as i32;
if size < best_size {
best_size = size;
std::mem::swap(&mut best_line, &mut f_buf);
best_line_raw.clone_from(&line_data);
if size == 1 {
if size == best_possible {
// Best possible result
break;
}
Expand Down

0 comments on commit c80d273

Please sign in to comment.