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 00000000..ed56380a Binary files /dev/null and b/tests/files/issue-89.png differ 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, + ); +}