From 918c52eed8acfbaf19851a475807bbe9f15c7647 Mon Sep 17 00:00:00 2001 From: Josh Holmer Date: Sun, 2 Dec 2018 00:12:30 -0500 Subject: [PATCH] Fix more nonstandard sBIT headers Closes #153 --- CHANGELOG.md | 3 +++ src/reduction/alpha.rs | 2 +- src/reduction/color.rs | 9 ++++++--- tests/regression.rs | 12 ++++++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 632d027b..185914fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +### Version 2.1.8 + - Fix non-standard sBIT headers in other code locations ([#153](https://github.com/shssoichiro/oxipng/issues/153)) + ### Version 2.1.7 - 80x faster palette reduction ([#150](https://github.com/shssoichiro/oxipng/pull/150)) - Optimize RGB to palette conversion ([#148](https://github.com/shssoichiro/oxipng/pull/148)) diff --git a/src/reduction/alpha.rs b/src/reduction/alpha.rs index be82bd8e..0380c697 100644 --- a/src/reduction/alpha.rs +++ b/src/reduction/alpha.rs @@ -31,7 +31,7 @@ pub fn reduce_alpha_channel(png: &mut PngData, channels: u8) -> Option> if let Some(sbit_header) = png.aux_headers.get_mut(b"sBIT") { // Some programs save the sBIT header as RGB even if the image is RGBA. // Only remove the alpha channel if it's actually there. - if sbit_header.len() == channels as usize { + if sbit_header.len() == 4 { sbit_header.pop(); } } diff --git a/src/reduction/color.rs b/src/reduction/color.rs index f0327f48..87cec441 100644 --- a/src/reduction/color.rs +++ b/src/reduction/color.rs @@ -60,7 +60,7 @@ pub fn reduce_rgba_to_grayscale_alpha(png: &mut PngData) -> bool { } if let Some(sbit_header) = png.aux_headers.get_mut(b"sBIT") { - assert_eq!(sbit_header.len(), 4); + assert!(sbit_header.len() >= 3); sbit_header.remove(1); sbit_header.remove(1); } @@ -170,8 +170,11 @@ pub fn reduce_color_to_palette(png: &mut PngData) -> bool { } if let Some(sbit_header) = png.aux_headers.get_mut(b"sBIT") { - assert_eq!(sbit_header.len(), 4); - sbit_header.pop(); + // Some programs save the sBIT header as RGB even if the image is RGBA. + // Only remove the alpha channel if it's actually there. + if sbit_header.len() == 4 { + sbit_header.pop(); + } } let mut palette_vec = vec![RGBA8::new(0, 0, 0, 0); palette.len()]; diff --git a/tests/regression.rs b/tests/regression.rs index 3a344963..c2ebfd07 100644 --- a/tests/regression.rs +++ b/tests/regression.rs @@ -560,3 +560,15 @@ fn issue_141() { BitDepth::Eight, ); } + +#[test] +fn issue_153() { + test_it_converts( + "tests/files/issue-153.png", + None, + ColorType::RGBA, + BitDepth::Eight, + ColorType::Indexed, + BitDepth::Eight, + ); +}