Allow grayscale reduction from 16 to 4 or less

This commit is contained in:
Andrew 2023-05-04 13:25:34 +12:00
parent d6cc8a003b
commit 974d0adf5a
6 changed files with 51 additions and 41 deletions

View file

@ -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 input = test::black_box(PathBuf::from("tests/files/rgb_16_should_be_rgb_8.png"));
let png = PngData::new(&input, false).unwrap(); 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] #[bench]
@ -22,7 +22,7 @@ fn reductions_8_to_4_bits(b: &mut Bencher) {
)); ));
let png = PngData::new(&input, false).unwrap(); 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] #[bench]
@ -32,7 +32,7 @@ fn reductions_8_to_2_bits(b: &mut Bencher) {
)); ));
let png = PngData::new(&input, false).unwrap(); 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] #[bench]
@ -42,7 +42,7 @@ fn reductions_8_to_1_bits(b: &mut Bencher) {
)); ));
let png = PngData::new(&input, false).unwrap(); 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] #[bench]
@ -52,7 +52,7 @@ fn reductions_4_to_2_bits(b: &mut Bencher) {
)); ));
let png = PngData::new(&input, false).unwrap(); 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] #[bench]
@ -62,7 +62,7 @@ fn reductions_4_to_1_bits(b: &mut Bencher) {
)); ));
let png = PngData::new(&input, false).unwrap(); 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] #[bench]
@ -72,7 +72,7 @@ fn reductions_2_to_1_bits(b: &mut Bencher) {
)); ));
let png = PngData::new(&input, false).unwrap(); 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] #[bench]
@ -82,7 +82,7 @@ fn reductions_grayscale_8_to_4_bits(b: &mut Bencher) {
)); ));
let png = PngData::new(&input, false).unwrap(); 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] #[bench]
@ -92,7 +92,7 @@ fn reductions_grayscale_8_to_2_bits(b: &mut Bencher) {
)); ));
let png = PngData::new(&input, false).unwrap(); 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] #[bench]
@ -102,7 +102,7 @@ fn reductions_grayscale_8_to_1_bits(b: &mut Bencher) {
)); ));
let png = PngData::new(&input, false).unwrap(); 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] #[bench]
@ -112,7 +112,7 @@ fn reductions_grayscale_4_to_2_bits(b: &mut Bencher) {
)); ));
let png = PngData::new(&input, false).unwrap(); 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] #[bench]
@ -122,7 +122,7 @@ fn reductions_grayscale_4_to_1_bits(b: &mut Bencher) {
)); ));
let png = PngData::new(&input, false).unwrap(); 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] #[bench]
@ -132,7 +132,7 @@ fn reductions_grayscale_2_to_1_bits(b: &mut Bencher) {
)); ));
let png = PngData::new(&input, false).unwrap(); 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] #[bench]

View file

@ -686,19 +686,9 @@ fn perform_reductions(
} }
if opts.bit_depth_reduction { if opts.bit_depth_reduction {
if let Some(reduced) = reduce_bit_depth(&png, 1) { if let Some(reduced) = reduced_bit_depth_16_to_8(&png) {
let previous = png.clone();
let bits = reduced.ihdr.bit_depth;
png = Arc::new(reduced); png = Arc::new(reduced);
eval.try_image(png.clone()); 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; reduction_occurred = true;
} }
if deadline.passed() { 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 { if reduction_occurred {
eval.set_baseline(baseline); eval.set_baseline(baseline);
} }

View file

@ -2,13 +2,10 @@ use crate::colors::{BitDepth, ColorType};
use crate::headers::IhdrData; use crate::headers::IhdrData;
use crate::png::PngImage; 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] #[must_use]
pub fn reduce_bit_depth(png: &PngImage, minimum_bits: usize) -> Option<PngImage> { pub fn reduced_bit_depth_16_to_8(png: &PngImage) -> Option<PngImage> {
if png.ihdr.bit_depth != BitDepth::Sixteen { 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; return None;
} }
@ -29,11 +26,12 @@ pub fn reduce_bit_depth(png: &PngImage, minimum_bits: usize) -> Option<PngImage>
}) })
} }
/// Attempt to reduce an 8/4/2-bit image to a lower bit depth, returning the reduced image if successful
#[must_use] #[must_use]
pub fn reduce_bit_depth_8_or_less(png: &PngImage, mut minimum_bits: usize) -> Option<PngImage> { pub fn reduced_bit_depth_8_or_less(png: &PngImage, mut minimum_bits: usize) -> Option<PngImage> {
assert!((1..8).contains(&minimum_bits)); assert!((1..8).contains(&minimum_bits));
let bit_depth = png.ihdr.bit_depth as usize; 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; return None;
} }
// Calculate the current number of pixels per byte // Calculate the current number of pixels per byte

View file

@ -8,12 +8,12 @@ use std::borrow::Cow;
pub mod alpha; pub mod alpha;
use crate::alpha::reduced_alpha_channel; use crate::alpha::reduced_alpha_channel;
pub mod bit_depth; pub mod bit_depth;
use crate::bit_depth::reduce_bit_depth_8_or_less;
pub mod color; pub mod color;
use crate::color::*; use crate::color::*;
pub(crate) use crate::alpha::cleaned_alpha_channel; 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 /// Attempt to reduce the number of colors in the palette
/// Returns `None` if palette hasn't changed /// Returns `None` if palette hasn't changed
@ -196,7 +196,6 @@ pub fn reduce_color_type(
grayscale_reduction: bool, grayscale_reduction: bool,
optimize_alpha: bool, optimize_alpha: bool,
) -> Option<PngImage> { ) -> Option<PngImage> {
let was_single_channel = png.channels_per_pixel() == 1;
let mut reduced = Cow::Borrowed(png); let mut reduced = Cow::Borrowed(png);
// Go down one step at a time - maybe not the most efficient, but it's safe // 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 { match reduced {
Cow::Owned(r) => Some(r), Cow::Owned(r) => Some(r),
_ => None, _ => None,

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

View file

@ -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] #[test]
fn grayscale_8_should_be_grayscale_8() { fn grayscale_8_should_be_grayscale_8() {
test_it_converts( test_it_converts(