Skip to content

Commit

Permalink
Try to pick tRNS value that's valid at low depth (#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrews05 authored Jul 8, 2023
1 parent 75a0f0d commit b846a2e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/reduction/alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions tests/reduction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit b846a2e

Please sign in to comment.