Simplify reduce_color_type

This commit is contained in:
Andrew 2022-12-15 13:41:08 +13:00
parent 7ba1f9bbac
commit e3d82db96b

View file

@ -199,48 +199,40 @@ pub fn reduce_color_type(
// Go down one step at a time // Go down one step at a time
// Maybe not the most efficient, but it's safe // Maybe not the most efficient, but it's safe
if reduced.ihdr.color_type == ColorType::RGBA { if grayscale_reduction && matches!(reduced.ihdr.color_type, ColorType::RGBA | ColorType::RGB) {
if let Some(r) = if grayscale_reduction { if let Some(r) = reduce_rgb_to_grayscale(&reduced) {
reduce_rgb_to_grayscale(&reduced)
} else {
None
}
.or_else(|| reduced_alpha_channel(&reduced, optimize_alpha))
{
reduced = Cow::Owned(r); reduced = Cow::Owned(r);
} else if let Some(r) = reduce_to_palette(&reduced) { should_reduce_bit_depth = reduced.ihdr.color_type == ColorType::Grayscale;
reduced = Cow::Owned(r);
should_reduce_bit_depth = true;
} }
} }
// Attempt grayscale alpha reduction before palette, as grayscale will typically be smaller than indexed
if reduced.ihdr.color_type == ColorType::GrayscaleAlpha { if reduced.ihdr.color_type == ColorType::GrayscaleAlpha {
if let Some(r) = if let Some(r) = reduced_alpha_channel(&reduced, optimize_alpha) {
reduced_alpha_channel(&reduced, optimize_alpha).or_else(|| reduce_to_palette(&reduced))
{
reduced = Cow::Owned(r); reduced = Cow::Owned(r);
should_reduce_bit_depth = true; should_reduce_bit_depth = true;
} }
} }
if reduced.ihdr.color_type == ColorType::RGB { if matches!(
if let Some(r) = if grayscale_reduction { reduced.ihdr.color_type,
reduce_rgb_to_grayscale(&reduced) ColorType::RGBA | ColorType::RGB | ColorType::GrayscaleAlpha
} else { ) {
None if let Some(r) = reduce_to_palette(&reduced) {
}
.or_else(|| reduce_to_palette(&reduced))
{
reduced = Cow::Owned(r); reduced = Cow::Owned(r);
should_reduce_bit_depth = true; should_reduce_bit_depth = true;
// Make sure that palette gets sorted. Ideally, this should be done within reduce_to_palette.
if let Some(r) = reduced_palette(&reduced, optimize_alpha) {
reduced = Cow::Owned(r);
}
} }
} }
//Make sure that palette gets sorted. Ideally, this should be done within reduced_color_to_palette. // Attempt RGBA alpha reduction after palette, so it can be skipped if palette was successful
if should_reduce_bit_depth && reduced.ihdr.color_type == ColorType::Indexed { if reduced.ihdr.color_type == ColorType::RGBA {
if let Some(r) = reduced_palette(&reduced, optimize_alpha) { if let Some(r) = reduced_alpha_channel(&reduced, optimize_alpha) {
reduced = Cow::Owned(r); reduced = Cow::Owned(r);
should_reduce_bit_depth = true;
} }
} }