Skip to content

Commit c80d273

Browse files
committed
Use best_possible for all strategies
1 parent f6842f2 commit c80d273

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/png/mod.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,10 @@ impl PngImage {
345345
for line in self.scan_lines(false) {
346346
if prev_pass != line.pass || line.data.len() != prev_line.len() {
347347
prev_line = vec![0; line.data.len()];
348-
// Calculate the best possible entropy for this pass
348+
// Calculate the best possible result for this pass
349349
best_possible = match filter {
350350
RowFilter::Entropy => ilog2i(line.data.len() as u32 + 1) as i32,
351+
RowFilter::Bigrams => 1,
351352
RowFilter::BigEnt => ilog2i(line.data.len() as u32) as i32,
352353
_ => 0,
353354
}
@@ -379,18 +380,18 @@ impl PngImage {
379380
RowFilter::MinSum => {
380381
// MSAD algorithm mentioned in libpng reference docs
381382
// http://www.libpng.org/pub/png/book/chapter09.html
382-
let mut best_size = usize::MAX;
383+
let mut best_size = i32::MAX;
383384
for f in try_filters {
384385
f.filter_line(bpp, &mut line_data, &prev_line, &mut f_buf, alpha_bytes);
385386
let size = f_buf.iter().fold(0, |acc, &x| {
386387
let signed = x as i8;
387-
acc + signed.unsigned_abs() as usize
388+
acc + signed.unsigned_abs() as i32
388389
});
389390
if size < best_size {
390391
best_size = size;
391392
std::mem::swap(&mut best_line, &mut f_buf);
392393
best_line_raw.clone_from(&line_data);
393-
if size == 0 {
394+
if size == best_possible {
394395
// Best possible result
395396
break;
396397
}
@@ -427,20 +428,20 @@ impl PngImage {
427428
RowFilter::Bigrams => {
428429
// Count distinct bigrams, from pngwolf
429430
// https://bjoern.hoehrmann.de/pngwolf/
430-
let mut best_size = usize::MAX;
431+
let mut best_size = i32::MAX;
431432
for f in try_filters {
432433
f.filter_line(bpp, &mut line_data, &prev_line, &mut f_buf, alpha_bytes);
433434
let mut set = bitarr![0; 0x10000];
434435
for pair in f_buf.windows(2) {
435436
let bigram = (pair[0] as usize) << 8 | pair[1] as usize;
436437
set.set(bigram, true);
437438
}
438-
let size = set.count_ones();
439+
let size = set.count_ones() as i32;
439440
if size < best_size {
440441
best_size = size;
441442
std::mem::swap(&mut best_line, &mut f_buf);
442443
best_line_raw.clone_from(&line_data);
443-
if size == 1 {
444+
if size == best_possible {
444445
// Best possible result
445446
break;
446447
}

0 commit comments

Comments
 (0)