diff --git a/benches/reductions.rs b/benches/reductions.rs index be9b6e06..ada197a2 100644 --- a/benches/reductions.rs +++ b/benches/reductions.rs @@ -12,7 +12,7 @@ fn reductions_16_to_8_bits(b: &mut Bencher) { let input = test::black_box(PathBuf::from("tests/files/rgb_16_should_be_rgb_8.png")); let png = PngData::new(&input, false).unwrap(); - b.iter(|| bit_depth::reduce_bit_depth(&png.raw, 1)); + b.iter(|| bit_depth::reduced_bit_depth_16_to_8(&png.raw)); } #[bench] @@ -22,7 +22,7 @@ fn reductions_8_to_4_bits(b: &mut Bencher) { )); let png = PngData::new(&input, false).unwrap(); - b.iter(|| bit_depth::reduce_bit_depth(&png.raw, 1)); + b.iter(|| bit_depth::reduced_bit_depth_8_or_less(&png.raw, 1)); } #[bench] @@ -32,7 +32,7 @@ fn reductions_8_to_2_bits(b: &mut Bencher) { )); let png = PngData::new(&input, false).unwrap(); - b.iter(|| bit_depth::reduce_bit_depth(&png.raw, 1)); + b.iter(|| bit_depth::reduced_bit_depth_8_or_less(&png.raw, 1)); } #[bench] @@ -42,7 +42,7 @@ fn reductions_8_to_1_bits(b: &mut Bencher) { )); let png = PngData::new(&input, false).unwrap(); - b.iter(|| bit_depth::reduce_bit_depth(&png.raw, 1)); + b.iter(|| bit_depth::reduced_bit_depth_8_or_less(&png.raw, 1)); } #[bench] @@ -52,7 +52,7 @@ fn reductions_4_to_2_bits(b: &mut Bencher) { )); let png = PngData::new(&input, false).unwrap(); - b.iter(|| bit_depth::reduce_bit_depth(&png.raw, 1)); + b.iter(|| bit_depth::reduced_bit_depth_8_or_less(&png.raw, 1)); } #[bench] @@ -62,7 +62,7 @@ fn reductions_4_to_1_bits(b: &mut Bencher) { )); let png = PngData::new(&input, false).unwrap(); - b.iter(|| bit_depth::reduce_bit_depth(&png.raw, 1)); + b.iter(|| bit_depth::reduced_bit_depth_8_or_less(&png.raw, 1)); } #[bench] @@ -72,7 +72,7 @@ fn reductions_2_to_1_bits(b: &mut Bencher) { )); let png = PngData::new(&input, false).unwrap(); - b.iter(|| bit_depth::reduce_bit_depth(&png.raw, 1)); + b.iter(|| bit_depth::reduced_bit_depth_8_or_less(&png.raw, 1)); } #[bench] @@ -82,7 +82,7 @@ fn reductions_grayscale_8_to_4_bits(b: &mut Bencher) { )); let png = PngData::new(&input, false).unwrap(); - b.iter(|| bit_depth::reduce_bit_depth(&png.raw, 1)); + b.iter(|| bit_depth::reduced_bit_depth_8_or_less(&png.raw, 1)); } #[bench] @@ -92,7 +92,7 @@ fn reductions_grayscale_8_to_2_bits(b: &mut Bencher) { )); let png = PngData::new(&input, false).unwrap(); - b.iter(|| bit_depth::reduce_bit_depth(&png.raw, 1)); + b.iter(|| bit_depth::reduced_bit_depth_8_or_less(&png.raw, 1)); } #[bench] @@ -102,7 +102,7 @@ fn reductions_grayscale_8_to_1_bits(b: &mut Bencher) { )); let png = PngData::new(&input, false).unwrap(); - b.iter(|| bit_depth::reduce_bit_depth(&png.raw, 1)); + b.iter(|| bit_depth::reduced_bit_depth_8_or_less(&png.raw, 1)); } #[bench] @@ -112,7 +112,7 @@ fn reductions_grayscale_4_to_2_bits(b: &mut Bencher) { )); let png = PngData::new(&input, false).unwrap(); - b.iter(|| bit_depth::reduce_bit_depth(&png.raw, 1)); + b.iter(|| bit_depth::reduced_bit_depth_8_or_less(&png.raw, 1)); } #[bench] @@ -122,7 +122,7 @@ fn reductions_grayscale_4_to_1_bits(b: &mut Bencher) { )); let png = PngData::new(&input, false).unwrap(); - b.iter(|| bit_depth::reduce_bit_depth(&png.raw, 1)); + b.iter(|| bit_depth::reduced_bit_depth_8_or_less(&png.raw, 1)); } #[bench] @@ -132,7 +132,7 @@ fn reductions_grayscale_2_to_1_bits(b: &mut Bencher) { )); let png = PngData::new(&input, false).unwrap(); - b.iter(|| bit_depth::reduce_bit_depth(&png.raw, 1)); + b.iter(|| bit_depth::reduced_bit_depth_8_or_less(&png.raw, 1)); } #[bench] diff --git a/src/lib.rs b/src/lib.rs index d331c6dc..895786c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -686,19 +686,9 @@ fn perform_reductions( } if opts.bit_depth_reduction { - if let Some(reduced) = reduce_bit_depth(&png, 1) { - let previous = png.clone(); - let bits = reduced.ihdr.bit_depth; + if let Some(reduced) = reduced_bit_depth_16_to_8(&png) { png = Arc::new(reduced); eval.try_image(png.clone()); - if (bits == BitDepth::One || bits == BitDepth::Two) - && previous.ihdr.bit_depth != BitDepth::Four - { - // Also try 16-color mode for all lower bits images, since that may compress better - if let Some(reduced) = reduce_bit_depth(&previous, 4) { - eval.try_image(Arc::new(reduced)); - } - } reduction_occurred = true; } if deadline.passed() { @@ -719,6 +709,24 @@ fn perform_reductions( } } + if opts.bit_depth_reduction { + if let Some(reduced) = reduced_bit_depth_8_or_less(&png, 1) { + let previous = png; + png = Arc::new(reduced); + eval.try_image(png.clone()); + if png.ihdr.bit_depth < BitDepth::Four && previous.ihdr.bit_depth == BitDepth::Eight { + // Also try 16-color mode for all lower bits images, since that may compress better + if let Some(reduced) = reduced_bit_depth_8_or_less(&previous, 4) { + eval.try_image(Arc::new(reduced)); + } + } + reduction_occurred = true; + } + if deadline.passed() { + return; + } + } + if reduction_occurred { eval.set_baseline(baseline); } diff --git a/src/reduction/bit_depth.rs b/src/reduction/bit_depth.rs index 948de473..9213c88c 100644 --- a/src/reduction/bit_depth.rs +++ b/src/reduction/bit_depth.rs @@ -2,13 +2,10 @@ use crate::colors::{BitDepth, ColorType}; use crate::headers::IhdrData; use crate::png::PngImage; -/// Attempt to reduce the bit depth of the image, returning the reduced image if successful +/// Attempt to reduce a 16-bit image to 8-bit, returning the reduced image if successful #[must_use] -pub fn reduce_bit_depth(png: &PngImage, minimum_bits: usize) -> Option { +pub fn reduced_bit_depth_16_to_8(png: &PngImage) -> Option { if png.ihdr.bit_depth != BitDepth::Sixteen { - if png.channels_per_pixel() == 1 { - return reduce_bit_depth_8_or_less(png, minimum_bits); - } return None; } @@ -29,11 +26,12 @@ pub fn reduce_bit_depth(png: &PngImage, minimum_bits: usize) -> Option }) } +/// Attempt to reduce an 8/4/2-bit image to a lower bit depth, returning the reduced image if successful #[must_use] -pub fn reduce_bit_depth_8_or_less(png: &PngImage, mut minimum_bits: usize) -> Option { +pub fn reduced_bit_depth_8_or_less(png: &PngImage, mut minimum_bits: usize) -> Option { assert!((1..8).contains(&minimum_bits)); let bit_depth = png.ihdr.bit_depth as usize; - if minimum_bits >= bit_depth || bit_depth > 8 { + if minimum_bits >= bit_depth || bit_depth > 8 || png.channels_per_pixel() != 1 { return None; } // Calculate the current number of pixels per byte diff --git a/src/reduction/mod.rs b/src/reduction/mod.rs index 8154189c..354d52f9 100644 --- a/src/reduction/mod.rs +++ b/src/reduction/mod.rs @@ -8,12 +8,12 @@ use std::borrow::Cow; pub mod alpha; use crate::alpha::reduced_alpha_channel; pub mod bit_depth; -use crate::bit_depth::reduce_bit_depth_8_or_less; pub mod color; use crate::color::*; pub(crate) use crate::alpha::cleaned_alpha_channel; -pub(crate) use crate::bit_depth::reduce_bit_depth; +pub(crate) use crate::bit_depth::reduced_bit_depth_16_to_8; +pub(crate) use crate::bit_depth::reduced_bit_depth_8_or_less; /// Attempt to reduce the number of colors in the palette /// Returns `None` if palette hasn't changed @@ -196,7 +196,6 @@ pub fn reduce_color_type( grayscale_reduction: bool, optimize_alpha: bool, ) -> Option { - let was_single_channel = png.channels_per_pixel() == 1; let mut reduced = Cow::Borrowed(png); // Go down one step at a time - maybe not the most efficient, but it's safe @@ -233,13 +232,6 @@ pub fn reduce_color_type( } } - // Some conversions will allow us to perform bit depth reduction that wasn't possible before - if !was_single_channel && reduced.channels_per_pixel() == 1 { - if let Some(r) = reduce_bit_depth_8_or_less(&reduced, 1) { - reduced = Cow::Owned(r); - } - } - match reduced { Cow::Owned(r) => Some(r), _ => None, diff --git a/tests/files/grayscale_16_should_be_grayscale_1.png b/tests/files/grayscale_16_should_be_grayscale_1.png new file mode 100644 index 00000000..66fe12e3 Binary files /dev/null and b/tests/files/grayscale_16_should_be_grayscale_1.png differ diff --git a/tests/reduction.rs b/tests/reduction.rs index 040375af..a7fe5e39 100644 --- a/tests/reduction.rs +++ b/tests/reduction.rs @@ -712,6 +712,18 @@ fn grayscale_16_should_be_grayscale_8() { ); } +#[test] +fn grayscale_16_should_be_grayscale_1() { + test_it_converts( + "tests/files/grayscale_16_should_be_grayscale_1.png", + false, + GRAYSCALE, + BitDepth::Sixteen, + GRAYSCALE, + BitDepth::One, + ); +} + #[test] fn grayscale_8_should_be_grayscale_8() { test_it_converts(