Fix RGB to Indexed reduction if transparency pixel exists

Closes #129
This commit is contained in:
Josh Holmer 2018-08-11 23:33:55 -04:00
parent 7895e42687
commit bb1737928e
4 changed files with 34 additions and 0 deletions

View file

@ -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))

View file

@ -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);

BIN
tests/files/issue-129.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

View file

@ -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";