From e3a26b77a482d78d34a5461752cda28e4520ae4a Mon Sep 17 00:00:00 2001 From: Josh Holmer Date: Sun, 10 Sep 2017 22:58:03 -0400 Subject: [PATCH] Fix a bug in reducing palettes with a bit depth of two Closes #80 --- CHANGELOG.md | 1 + src/png.rs | 10 +++++----- tests/files/issue-80.png | Bin 0 -> 1059 bytes tests/regression.rs | 17 +++++++++++++++++ 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 tests/files/issue-80.png diff --git a/CHANGELOG.md b/CHANGELOG.md index 0aac5423..ee3258c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ trials for optimizing the alpha channel, using the previously mentioned fast heuristic. This option will make optimization of images with transparency somewhat slower, but may improve compression. + - Fixed a bug in reducing palettes for images with bit depth of two([#80](https://github.com/shssoichiro/oxipng/issues/80)) - Code cleanup ### Version 0.16.3 diff --git a/src/png.rs b/src/png.rs index 0c0f19c4..5449d8a3 100644 --- a/src/png.rs +++ b/src/png.rs @@ -543,7 +543,7 @@ impl PngData { for (i, bit) in bitvec.iter().enumerate() { let mod_i = i % 4; if bit { - current += 2u8.pow(3u32 - mod_i as u32); + current += 1u8 << (3 - mod_i); } if mod_i == 3 { seen.insert(current); @@ -557,7 +557,7 @@ impl PngData { for (i, bit) in bitvec.iter().enumerate() { let mod_i = i % 2; if bit { - current += 2u8.pow(1u32 - mod_i as u32); + current += 1u8 << (1 - mod_i); } if mod_i == 1 { seen.insert(current); @@ -649,17 +649,17 @@ impl PngData { new_byte |= if let Some(new_idx) = index_map.get(&one) { *new_idx << 6 } else { - one << 6 + one }; new_byte |= if let Some(new_idx) = index_map.get(&two) { *new_idx << 4 } else { - two << 4 + two }; new_byte |= if let Some(new_idx) = index_map.get(&three) { *new_idx << 2 } else { - three << 2 + three }; new_byte |= if let Some(new_idx) = index_map.get(&four) { *new_idx diff --git a/tests/files/issue-80.png b/tests/files/issue-80.png new file mode 100644 index 0000000000000000000000000000000000000000..15c819142205eca2a3924c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i0dVN-jzTQVd20hUU5k7P`iUAw~vP zh9*`<#@YtPRt5%(8}_rIXvob^$xN%nt>MyL_FX^?{9qe0i%as0N;32F7z~Y!jUBpF RwgROXJYD@<);T3K0RVSA!*&1w literal 0 HcmV?d00001 diff --git a/tests/regression.rs b/tests/regression.rs index d8cf6d65..aaa63820 100644 --- a/tests/regression.rs +++ b/tests/regression.rs @@ -274,3 +274,20 @@ fn issue_60() { BitDepth::Eight, ); } + +#[test] +fn issue_80() { + let input = PathBuf::from("tests/files/issue-80.png"); + let opts = get_opts(&input); + let output = opts.out_file.clone(); + + test_it_converts( + &input, + &output, + &opts, + ColorType::Indexed, + BitDepth::Two, + ColorType::Indexed, + BitDepth::One, + ); +}