From 4d62f9f7f9f1844e70a10437783cc2e5ee58914c Mon Sep 17 00:00:00 2001 From: Josh Holmer Date: Tue, 21 Nov 2017 12:37:31 -0500 Subject: [PATCH] Fix failure to optimize on certain grayscale images Closes #89 --- CHANGELOG.md | 1 + src/reduction.rs | 6 +++++- tests/files/issue-89.png | Bin 0 -> 120 bytes tests/regression.rs | 17 +++++++++++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/files/issue-89.png diff --git a/CHANGELOG.md b/CHANGELOG.md index ec66ca37..bdb6af92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ### Version 0.18.1 (unreleased) - Bump `rayon` to 0.9 + - Fix failure to optimize on certain grayscale images ([#89](https://github.com/shssoichiro/oxipng/issues/89)) ### Version 0.18.0 - Bump `itertools` to 0.7 diff --git a/src/reduction.rs b/src/reduction.rs index 38f7acb1..a4344f35 100644 --- a/src/reduction.rs +++ b/src/reduction.rs @@ -10,7 +10,11 @@ pub fn reduce_bit_depth_8_or_less(png: &mut PngData) -> bool { for line in png.scan_lines() { let bit_vec = BitVec::from_bytes(&line.data); for (i, bit) in bit_vec.iter().enumerate() { - let bit_index = bit_depth - (i % bit_depth); + let bit_index = if png.ihdr_data.color_type == ColorType::Indexed { + bit_depth - (i % bit_depth) + } else { + i % bit_depth + }; if bit && bit_index > allowed_bits { allowed_bits = bit_index.next_power_of_two(); if allowed_bits == bit_depth { diff --git a/tests/files/issue-89.png b/tests/files/issue-89.png new file mode 100644 index 0000000000000000000000000000000000000000..ed56380a3ca70409ff2e7595d8732e4d8ffd1bd4 GIT binary patch literal 120 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`_MR?|Ar*7p_89UpC~z=8{NKMW zM7CSX{gA4U?YXl&h4+^*miuu#XbDFZy>;NRE~rx5vgF`_w=9 literal 0 HcmV?d00001 diff --git a/tests/regression.rs b/tests/regression.rs index e0e2ff2b..02b5498c 100644 --- a/tests/regression.rs +++ b/tests/regression.rs @@ -308,3 +308,20 @@ fn issue_82() { BitDepth::Four, ); } + +#[test] +fn issue_89() { + let input = PathBuf::from("tests/files/issue-89.png"); + let opts = get_opts(&input); + let output = opts.out_file.clone(); + + test_it_converts( + &input, + &output, + &opts, + ColorType::RGBA, + BitDepth::Eight, + ColorType::Grayscale, + BitDepth::Eight, + ); +}