Revert "Break when best possible filter result found"

This reverts commit 1efacace9f.
This commit is contained in:
Andrew 2024-11-27 19:23:53 +13:00
parent 1efacace9f
commit 7c89dc17fc

View file

@ -341,17 +341,9 @@ impl PngImage {
let mut prev_line = Vec::new(); let mut prev_line = Vec::new();
let mut prev_pass: Option<u8> = None; let mut prev_pass: Option<u8> = None;
let mut f_buf = Vec::new(); let mut f_buf = Vec::new();
let mut best_possible = 0;
for line in self.scan_lines(false) { for line in self.scan_lines(false) {
if prev_pass != line.pass || line.data.len() != prev_line.len() { if prev_pass != line.pass || line.data.len() != prev_line.len() {
prev_line = vec![0; line.data.len()]; prev_line = vec![0; line.data.len()];
// 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,
}
} }
// Alpha optimisation may alter the line data, so we need a mutable copy of it // Alpha optimisation may alter the line data, so we need a mutable copy of it
let mut line_data = line.data.to_vec(); let mut line_data = line.data.to_vec();
@ -380,21 +372,17 @@ impl PngImage {
RowFilter::MinSum => { RowFilter::MinSum => {
// MSAD algorithm mentioned in libpng reference docs // MSAD algorithm mentioned in libpng reference docs
// http://www.libpng.org/pub/png/book/chapter09.html // http://www.libpng.org/pub/png/book/chapter09.html
let mut best_size = i32::MAX; let mut best_size = usize::MAX;
for f in try_filters { for f in try_filters {
f.filter_line(bpp, &mut line_data, &prev_line, &mut f_buf, alpha_bytes); f.filter_line(bpp, &mut line_data, &prev_line, &mut f_buf, alpha_bytes);
let size = f_buf.iter().fold(0, |acc, &x| { let size = f_buf.iter().fold(0, |acc, &x| {
let signed = x as i8; let signed = x as i8;
acc + signed.unsigned_abs() as i32 acc + signed.unsigned_abs() as usize
}); });
if size < best_size { if size < best_size {
best_size = size; best_size = size;
std::mem::swap(&mut best_line, &mut f_buf); std::mem::swap(&mut best_line, &mut f_buf);
best_line_raw.clone_from(&line_data); best_line_raw.clone_from(&line_data);
if size == best_possible {
// Best possible result
break;
}
} }
} }
} }
@ -418,17 +406,13 @@ impl PngImage {
best_size = size; best_size = size;
std::mem::swap(&mut best_line, &mut f_buf); std::mem::swap(&mut best_line, &mut f_buf);
best_line_raw.clone_from(&line_data); best_line_raw.clone_from(&line_data);
if size == best_possible {
// Best possible result
break;
}
} }
} }
} }
RowFilter::Bigrams => { RowFilter::Bigrams => {
// Count distinct bigrams, from pngwolf // Count distinct bigrams, from pngwolf
// https://bjoern.hoehrmann.de/pngwolf/ // https://bjoern.hoehrmann.de/pngwolf/
let mut best_size = i32::MAX; let mut best_size = usize::MAX;
for f in try_filters { for f in try_filters {
f.filter_line(bpp, &mut line_data, &prev_line, &mut f_buf, alpha_bytes); f.filter_line(bpp, &mut line_data, &prev_line, &mut f_buf, alpha_bytes);
let mut set = bitarr![0; 0x10000]; let mut set = bitarr![0; 0x10000];
@ -436,15 +420,11 @@ impl PngImage {
let bigram = (pair[0] as usize) << 8 | pair[1] as usize; let bigram = (pair[0] as usize) << 8 | pair[1] as usize;
set.set(bigram, true); set.set(bigram, true);
} }
let size = set.count_ones() as i32; let size = set.count_ones();
if size < best_size { if size < best_size {
best_size = size; best_size = size;
std::mem::swap(&mut best_line, &mut f_buf); std::mem::swap(&mut best_line, &mut f_buf);
best_line_raw.clone_from(&line_data); best_line_raw.clone_from(&line_data);
if size == best_possible {
// Best possible result
break;
}
} }
} }
} }
@ -465,10 +445,6 @@ impl PngImage {
best_size = size; best_size = size;
std::mem::swap(&mut best_line, &mut f_buf); std::mem::swap(&mut best_line, &mut f_buf);
best_line_raw.clone_from(&line_data); best_line_raw.clone_from(&line_data);
if size == best_possible {
// Best possible result
break;
}
} }
} }
} }