diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c9c7dc2..d5a1b061 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +### Version 2.1.2 + - Fix issue with PNG to Indexed reduction on images with transparency pixel ([#129](https://github.com/shssoichiro/oxipng/issues/129)) + ### Version 2.1.1 - More fixes for alpha optimization on interlaced images ([#133](https://github.com/shssoichiro/oxipng/issues/133)) diff --git a/src/reduction/color.rs b/src/reduction/color.rs index 432c100d..a1be2552 100644 --- a/src/reduction/color.rs +++ b/src/reduction/color.rs @@ -169,6 +169,13 @@ pub fn reduce_rgb_to_palette(png: &mut PngData) -> bool { } let mut reduced = Vec::with_capacity(png.raw_data.len()); let mut palette = Vec::with_capacity(256); + if let Some(ref trns) = png.transparency_pixel { + assert_eq!(trns.len(), 6); + if trns[0] != trns[1] || trns[2] != trns[3] || trns[4] != trns[5] { + return false; + } + palette.push(vec![trns[0], trns[2], trns[4]]); + } let bpp: usize = (3 * png.ihdr_data.bit_depth.as_u8() as usize) >> 3; for line in png.scan_lines() { reduced.push(line.filter); @@ -227,6 +234,10 @@ pub fn reduce_rgb_to_palette(png: &mut PngData) -> bool { png.raw_data = reduced; png.palette = Some(color_palette); png.ihdr_data.color_type = ColorType::Indexed; + if png.transparency_pixel.is_some() { + png.transparency_pixel = None; + png.transparency_palette = Some(vec![0]); + }; true } @@ -263,6 +274,13 @@ pub fn reduce_rgb_to_grayscale(png: &mut PngData) -> bool { } } } + if let Some(ref mut trns) = png.transparency_pixel { + assert_eq!(trns.len(), 6); + if trns[0..2] != trns[2..4] || trns[2..4] != trns[4..6] { + return false; + } + *trns = trns[0..2].to_owned(); + } if let Some(sbit_header) = png.aux_headers.get_mut(&"sBIT".to_string()) { assert_eq!(sbit_header.len(), 3); sbit_header.truncate(1); diff --git a/tests/files/issue-129.png b/tests/files/issue-129.png new file mode 100644 index 00000000..1cf61e01 Binary files /dev/null and b/tests/files/issue-129.png differ diff --git a/tests/regression.rs b/tests/regression.rs index b09d3bf3..5a1855a8 100644 --- a/tests/regression.rs +++ b/tests/regression.rs @@ -410,6 +410,19 @@ fn issue_113_down() { ); } +#[test] +fn issue_129() { + let input = "tests/files/issue-129.png"; + test_it_converts( + input, + None, + ColorType::RGB, + BitDepth::Eight, + ColorType::Indexed, + BitDepth::Eight, + ); +} + #[test] fn issue_133_black() { let input = "tests/files/issue-133.png";