From 974d0adf5a2f617130bad003a5ef93877d3dcfb6 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 4 May 2023 13:25:34 +1200 Subject: [PATCH] Allow grayscale reduction from 16 to 4 or less --- benches/reductions.rs | 26 +++++++-------- src/lib.rs | 30 +++++++++++------- src/reduction/bit_depth.rs | 12 +++---- src/reduction/mod.rs | 12 ++----- .../grayscale_16_should_be_grayscale_1.png | Bin 0 -> 2483 bytes tests/reduction.rs | 12 +++++++ 6 files changed, 51 insertions(+), 41 deletions(-) create mode 100644 tests/files/grayscale_16_should_be_grayscale_1.png 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 0000000000000000000000000000000000000000..66fe12e3c8fb10bd19e2602e4d8955a43931533f GIT binary patch literal 2483 zcmd5+Z%i9y7=Mof?KBBbl0Iu?fjG94z_)C3Z#(Au(iNB?L|UGvD`mE#>I6AHDXy zd+vFEzvs{UJkPz>e6W6tzC;fI*wWbGYykjUforf3DuA(JTPpy$4ZPbUd0YpWcEN9A zJA^jQ6!r%Z0NBC|6#bmU;$gop5Msi$1{uLX8L0^aE<>ajYYiS(Gwu*X4!4*{6KSyP zaU8da9bTrzxnH5fw_3wdNeVIq(cRr`>NcAMv6G-`YHA3QCTQ9S5yntYKw`tjKxk*e zNYajz3$=@UP~wFEj_k5+!ZE4VU_g#iS0YX>%&&P0gp{;kf&|(jC=*F!qQPWS@vUQp z+28|767PM0FSL`O$%m1M(S{;m9D+{}pAWL_oSm}CL8YtKGD@JE`7r16IC(!82*K>_ zRV1m9)H@lOt!vE6^~|)LG$uR-#vyX7B#3T7@Y!W2e)Nhrivn&onN75fP@_}^TtWx$ z?QyaaXQxS6SqlCwZqmw-G(#)&G>nQZyXO=(WigVJk+QmJlA)@YDwurAc><$iyI^Z} zuo9c}ZZ?utMzYdPnHkE;RFV}G&7gxoR@PzD?4aT?EnG(uxrk>_B`BR8lUWEbf=HQuupmc5!L;YCpBu29pr)zsF1r>1W!R2zY2 zEp7G^gDq>}WZU)d7?WQKyPn>@wGMtbzUMrRUY83z1!W!3Vh;lilrZ=MSP96JWdPc+ z(v;kVJ*i-FAcg|)4jzF^#AsRG0p16#ii{aLuuWZ!xYRpQ*sf?Jow0$_#V9Im_{#O8w?0J;7}gyH zRb%_^Fuki))vV*As|y$NL2>?(ncvSXSyw*T@s8tsrXCJXe`tHJESug5Tzad1Xe5g% zi0&I()`e67Y3`aU-l(is)hAjtw%o6VW^$qZoICVr&)JRbxM4uSg~_ip^?f=2Ij@mD i-uahD9Is@U>d4lah4S91c;F@~Y2&_w&X4PkocsrUJHIpl literal 0 HcmV?d00001 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(