Try to pick tRNS value that's valid at low depth (#520)

This commit is contained in:
andrews05 2023-07-08 12:56:15 +12:00 committed by GitHub
parent 75a0f0de95
commit b846a2e909
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 1 deletions

View file

@ -57,8 +57,16 @@ pub fn reduced_alpha_channel(png: &PngImage, optimize_alpha: bool) -> Option<Png
}
let transparency_pixel = if has_transparency {
// For grayscale, start by checking 4 specific values in the hope that we may reduce depth
let unused = match png.ihdr.color_type {
ColorType::GrayscaleAlpha => [0x00, 0xFF, 0x55, 0xAA]
.into_iter()
.find(|&v| !used_colors[v as usize]),
_ => None,
}
.or_else(|| used_colors.iter().position(|&u| !u).map(|v| v as u8));
// If no unused color was found we will have to fail here
Some(used_colors.iter().position(|b| !*b)? as u8)
Some(unused?)
} else {
None
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

View file

@ -916,6 +916,18 @@ fn grayscale_alpha_8_should_be_grayscale_trns_8() {
);
}
#[test]
fn grayscale_alpha_8_should_be_grayscale_trns_1() {
test_it_converts(
"tests/files/grayscale_alpha_8_should_be_grayscale_trns_1.png",
true,
GRAYSCALE_ALPHA,
BitDepth::Eight,
GRAYSCALE,
BitDepth::One,
);
}
#[test]
fn small_files() {
test_it_converts(