Tweaks to interlacing and format display (#476)

This commit is contained in:
andrews05 2022-12-13 03:54:35 +13:00 committed by GitHub
parent 815f12df46
commit 2599b9fe82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 143 additions and 111 deletions

View file

@ -3,7 +3,7 @@
extern crate oxipng; extern crate oxipng;
extern crate test; extern crate test;
use oxipng::internal_tests::*; use oxipng::{internal_tests::*, Interlacing};
use std::path::PathBuf; use std::path::PathBuf;
use test::Bencher; use test::Bencher;
@ -12,7 +12,7 @@ fn interlacing_16_bits(b: &mut Bencher) {
let input = test::black_box(PathBuf::from("tests/files/rgb_16_should_be_rgb_16.png")); let input = test::black_box(PathBuf::from("tests/files/rgb_16_should_be_rgb_16.png"));
let png = PngData::new(&input, false).unwrap(); let png = PngData::new(&input, false).unwrap();
b.iter(|| png.raw.change_interlacing(1)); b.iter(|| png.raw.change_interlacing(Interlacing::Adam7));
} }
#[bench] #[bench]
@ -20,7 +20,7 @@ fn interlacing_8_bits(b: &mut Bencher) {
let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_rgb_8.png")); let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_rgb_8.png"));
let png = PngData::new(&input, false).unwrap(); let png = PngData::new(&input, false).unwrap();
b.iter(|| png.raw.change_interlacing(1)); b.iter(|| png.raw.change_interlacing(Interlacing::Adam7));
} }
#[bench] #[bench]
@ -30,7 +30,7 @@ fn interlacing_4_bits(b: &mut Bencher) {
)); ));
let png = PngData::new(&input, false).unwrap(); let png = PngData::new(&input, false).unwrap();
b.iter(|| png.raw.change_interlacing(1)); b.iter(|| png.raw.change_interlacing(Interlacing::Adam7));
} }
#[bench] #[bench]
@ -40,7 +40,7 @@ fn interlacing_2_bits(b: &mut Bencher) {
)); ));
let png = PngData::new(&input, false).unwrap(); let png = PngData::new(&input, false).unwrap();
b.iter(|| png.raw.change_interlacing(1)); b.iter(|| png.raw.change_interlacing(Interlacing::Adam7));
} }
#[bench] #[bench]
@ -50,7 +50,7 @@ fn interlacing_1_bits(b: &mut Bencher) {
)); ));
let png = PngData::new(&input, false).unwrap(); let png = PngData::new(&input, false).unwrap();
b.iter(|| png.raw.change_interlacing(1)); b.iter(|| png.raw.change_interlacing(Interlacing::Adam7));
} }
#[bench] #[bench]
@ -60,7 +60,7 @@ fn deinterlacing_16_bits(b: &mut Bencher) {
)); ));
let png = PngData::new(&input, false).unwrap(); let png = PngData::new(&input, false).unwrap();
b.iter(|| png.raw.change_interlacing(0)); b.iter(|| png.raw.change_interlacing(Interlacing::None));
} }
#[bench] #[bench]
@ -70,7 +70,7 @@ fn deinterlacing_8_bits(b: &mut Bencher) {
)); ));
let png = PngData::new(&input, false).unwrap(); let png = PngData::new(&input, false).unwrap();
b.iter(|| png.raw.change_interlacing(0)); b.iter(|| png.raw.change_interlacing(Interlacing::None));
} }
#[bench] #[bench]
@ -80,7 +80,7 @@ fn deinterlacing_4_bits(b: &mut Bencher) {
)); ));
let png = PngData::new(&input, false).unwrap(); let png = PngData::new(&input, false).unwrap();
b.iter(|| png.raw.change_interlacing(0)); b.iter(|| png.raw.change_interlacing(Interlacing::None));
} }
#[bench] #[bench]
@ -90,7 +90,7 @@ fn deinterlacing_2_bits(b: &mut Bencher) {
)); ));
let png = PngData::new(&input, false).unwrap(); let png = PngData::new(&input, false).unwrap();
b.iter(|| png.raw.change_interlacing(0)); b.iter(|| png.raw.change_interlacing(Interlacing::None));
} }
#[bench] #[bench]
@ -100,5 +100,5 @@ fn deinterlacing_1_bits(b: &mut Bencher) {
)); ));
let png = PngData::new(&input, false).unwrap(); let png = PngData::new(&input, false).unwrap();
b.iter(|| png.raw.change_interlacing(0)); b.iter(|| png.raw.change_interlacing(Interlacing::None));
} }

View file

@ -1,6 +1,7 @@
use crate::colors::{BitDepth, ColorType}; use crate::colors::{BitDepth, ColorType};
use crate::deflate::crc32; use crate::deflate::crc32;
use crate::error::PngError; use crate::error::PngError;
use crate::interlace::Interlacing;
use crate::PngResult; use crate::PngResult;
use indexmap::IndexSet; use indexmap::IndexSet;
use std::io; use std::io;
@ -21,8 +22,8 @@ pub struct IhdrData {
pub compression: u8, pub compression: u8,
/// The filter mode used for this image (currently only 0 is valid) /// The filter mode used for this image (currently only 0 is valid)
pub filter: u8, pub filter: u8,
/// The interlacing mode of the image (0 = None, 1 = Adam7) /// The interlacing mode of the image
pub interlaced: u8, pub interlaced: Interlacing,
} }
impl IhdrData { impl IhdrData {
@ -44,7 +45,7 @@ impl IhdrData {
(((w / 8) * bpp as usize) + ((w & 7) * bpp as usize + 7) / 8) * h (((w / 8) * bpp as usize) + ((w & 7) * bpp as usize + 7) / 8) * h
} }
if self.interlaced == 0 { if self.interlaced == Interlacing::None {
bitmap_size(bpp, w, h) + h bitmap_size(bpp, w, h) + h
} else { } else {
let mut size = bitmap_size(bpp, (w + 7) >> 3, (h + 7) >> 3) + ((h + 7) >> 3); let mut size = bitmap_size(bpp, (w + 7) >> 3, (h + 7) >> 3) + ((h + 7) >> 3);
@ -167,7 +168,7 @@ pub fn parse_ihdr_header(byte_data: &[u8]) -> PngResult<IhdrData> {
height: read_be_u32(&mut rdr).map_err(|_| PngError::TruncatedData)?, height: read_be_u32(&mut rdr).map_err(|_| PngError::TruncatedData)?,
compression: byte_data[10], compression: byte_data[10],
filter: byte_data[11], filter: byte_data[11],
interlaced, interlaced: interlaced.try_into()?,
}) })
} }

View file

@ -1,7 +1,42 @@
use std::fmt::Display;
use crate::headers::IhdrData; use crate::headers::IhdrData;
use crate::png::PngImage; use crate::png::PngImage;
use crate::PngError;
use bitvec::prelude::*; use bitvec::prelude::*;
#[repr(u8)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Interlacing {
None,
Adam7,
}
impl TryFrom<u8> for Interlacing {
type Error = PngError;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::None),
1 => Ok(Self::Adam7),
_ => Err(PngError::new("Unexpected interlacing in header")),
}
}
}
impl Display for Interlacing {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match *self {
Self::None => "non-interlaced",
Self::Adam7 => "interlaced",
}
)
}
}
#[must_use] #[must_use]
pub fn interlace_image(png: &PngImage) -> PngImage { pub fn interlace_image(png: &PngImage) -> PngImage {
let mut passes: Vec<BitVec<u8, Msb0>> = vec![BitVec::new(); 7]; let mut passes: Vec<BitVec<u8, Msb0>> = vec![BitVec::new(); 7];
@ -85,7 +120,7 @@ pub fn interlace_image(png: &PngImage) -> PngImage {
PngImage { PngImage {
data: output, data: output,
ihdr: IhdrData { ihdr: IhdrData {
interlaced: 1, interlaced: Interlacing::Adam7,
..png.ihdr ..png.ihdr
}, },
aux_headers: png.aux_headers.clone(), aux_headers: png.aux_headers.clone(),
@ -101,7 +136,7 @@ pub fn deinterlace_image(png: &PngImage) -> PngImage {
_ => deinterlace_bits(png), _ => deinterlace_bits(png),
}, },
ihdr: IhdrData { ihdr: IhdrData {
interlaced: 0, interlaced: Interlacing::None,
..png.ihdr ..png.ihdr
}, },
aux_headers: png.aux_headers.clone(), aux_headers: png.aux_headers.clone(),

View file

@ -46,6 +46,7 @@ pub use crate::deflate::Deflaters;
pub use crate::error::PngError; pub use crate::error::PngError;
pub use crate::filters::RowFilter; pub use crate::filters::RowFilter;
pub use crate::headers::Headers; pub use crate::headers::Headers;
pub use crate::interlace::Interlacing;
pub use indexmap::{indexset, IndexMap, IndexSet}; pub use indexmap::{indexset, IndexMap, IndexSet};
mod atomicmin; mod atomicmin;
@ -157,7 +158,7 @@ pub struct Options {
/// `Some(x)` will change the file to interlacing mode `x`. /// `Some(x)` will change the file to interlacing mode `x`.
/// ///
/// Default: `None` /// Default: `None`
pub interlace: Option<u8>, pub interlace: Option<Interlacing>,
/// Whether to allow transparent pixels to be altered to improve compression. /// Whether to allow transparent pixels to be altered to improve compression.
pub optimize_alpha: bool, pub optimize_alpha: bool,
/// Whether to attempt bit depth reduction /// Whether to attempt bit depth reduction
@ -469,20 +470,7 @@ fn optimize_png(
" {}x{} pixels, PNG format", " {}x{} pixels, PNG format",
png.raw.ihdr.width, png.raw.ihdr.height png.raw.ihdr.width, png.raw.ihdr.height
); );
if let Some(ref palette) = png.raw.palette { report_format(" ", &png.raw);
info!(
" {} bits/pixel, {} colors in palette",
png.raw.ihdr.bit_depth,
palette.len()
);
} else {
info!(
" {}x{} bits/pixel, {:?}",
png.raw.channels_per_pixel(),
png.raw.ihdr.bit_depth,
png.raw.ihdr.color_type
);
}
info!(" IDAT size = {} bytes", idat_original_size); info!(" IDAT size = {} bytes", idat_original_size);
info!(" File size = {} bytes", file_original_size); info!(" File size = {} bytes", file_original_size);
@ -490,7 +478,16 @@ fn optimize_png(
perform_strip(png, opts); perform_strip(png, opts);
let stripped_png = png.clone(); let stripped_png = png.clone();
// If alpha optimization is enabled, first perform a black alpha reduction // Interlacing is not part of the evaluator trials but must be done first to evaluate the rest correctly
let mut reduction_occurred = false;
if let Some(interlacing) = opts.interlace {
if let Some(reduced) = png.raw.change_interlacing(interlacing) {
png.raw = Arc::new(reduced);
reduction_occurred = true;
}
}
// If alpha optimization is enabled, perform a black alpha reduction before evaluating reductions
// This can allow reductions from alpha to indexed which may not have been possible otherwise // This can allow reductions from alpha to indexed which may not have been possible otherwise
if opts.optimize_alpha { if opts.optimize_alpha {
if let Some(reduced) = cleaned_alpha_channel(&png.raw) { if let Some(reduced) = cleaned_alpha_channel(&png.raw) {
@ -510,13 +507,18 @@ fn optimize_png(
false, false,
); );
perform_reductions(png.raw.clone(), opts, &deadline, &eval); perform_reductions(png.raw.clone(), opts, &deadline, &eval);
let (reduction_occurred, mut eval_filter) = if let Some(result) = eval.get_best_candidate() { let mut eval_filter = if let Some(result) = eval.get_best_candidate() {
*png = result.image; *png = result.image;
(result.is_reduction, Some(result.filter)) reduction_occurred = true;
Some(result.filter)
} else { } else {
(false, None) None
}; };
if reduction_occurred {
report_format("Reducing image to ", &png.raw);
}
if opts.idat_recoding || reduction_occurred { if opts.idat_recoding || reduction_occurred {
let mut filters = opts.filter.clone(); let mut filters = opts.filter.clone();
let fast_eval = opts.fast_evaluation && (filters.len() > 1 || eval_filter.is_some()); let fast_eval = opts.fast_evaluation && (filters.len() > 1 || eval_filter.is_some());
@ -686,27 +688,13 @@ fn perform_reductions(
eval: &Evaluator, eval: &Evaluator,
) { ) {
// The eval baseline will be set from the original png only if we attempt any reductions // The eval baseline will be set from the original png only if we attempt any reductions
let mut baseline = Some(png.clone()); let baseline = png.clone();
let mut reduction_occurred = false; let mut reduction_occurred = false;
// must be done first to evaluate rest with the correct interlacing
if let Some(interlacing) = opts.interlace {
if let Some(reduced) = png.change_interlacing(interlacing) {
png = Arc::new(reduced);
eval.try_image(png.clone());
// If we're interlacing, we have to accept a possible file size increase
baseline = None;
}
if deadline.passed() {
return;
}
}
if opts.palette_reduction { if opts.palette_reduction {
if let Some(reduced) = reduced_palette(&png, opts.optimize_alpha) { if let Some(reduced) = reduced_palette(&png, opts.optimize_alpha) {
png = Arc::new(reduced); png = Arc::new(reduced);
eval.try_image(png.clone()); eval.try_image(png.clone());
report_reduction(&png);
reduction_occurred = true; reduction_occurred = true;
} }
if deadline.passed() { if deadline.passed() {
@ -728,7 +716,6 @@ fn perform_reductions(
eval.try_image(Arc::new(reduced)); eval.try_image(Arc::new(reduced));
} }
} }
report_reduction(&png);
reduction_occurred = true; reduction_occurred = true;
} }
if deadline.passed() { if deadline.passed() {
@ -742,7 +729,6 @@ fn perform_reductions(
{ {
png = Arc::new(reduced); png = Arc::new(reduced);
eval.try_image(png.clone()); eval.try_image(png.clone());
report_reduction(&png);
reduction_occurred = true; reduction_occurred = true;
} }
if deadline.passed() { if deadline.passed() {
@ -750,10 +736,8 @@ fn perform_reductions(
} }
} }
if let Some(baseline) = baseline { if reduction_occurred {
if reduction_occurred { eval.set_baseline(baseline);
eval.set_baseline(baseline);
}
} }
} }
@ -845,20 +829,24 @@ impl Deadline {
} }
} }
/// Display the status of the image data after a reduction has taken place /// Display the format of the image data
fn report_reduction(png: &PngImage) { fn report_format(prefix: &str, png: &PngImage) {
if let Some(ref palette) = png.palette { if let Some(ref palette) = png.palette {
info!( info!(
"Reducing image to {} bits/pixel, {} colors in palette", "{}{} bits/pixel, {} colors in palette ({})",
prefix,
png.ihdr.bit_depth, png.ihdr.bit_depth,
palette.len() palette.len(),
png.ihdr.interlaced
); );
} else { } else {
info!( info!(
"Reducing image to {}x{} bits/pixel, {}", "{}{}x{} bits/pixel, {} ({})",
prefix,
png.channels_per_pixel(), png.channels_per_pixel(),
png.ihdr.bit_depth, png.ihdr.bit_depth,
png.ihdr.color_type png.ihdr.color_type,
png.ihdr.interlaced
); );
} }
} }

View file

@ -393,13 +393,13 @@ fn parse_opts_into_struct(
}; };
if let Some(x) = matches.value_of("interlace") { if let Some(x) = matches.value_of("interlace") {
opts.interlace = x.parse::<u8>().ok(); opts.interlace = x.parse::<u8>().unwrap().try_into().ok();
} }
if let Some(x) = matches.value_of("filters") { if let Some(x) = matches.value_of("filters") {
opts.filter.clear(); opts.filter.clear();
for f in parse_numeric_range_opts(x, 0, RowFilter::LAST).unwrap() { for f in parse_numeric_range_opts(x, 0, RowFilter::LAST).unwrap() {
opts.filter.insert(RowFilter::try_from(f).unwrap()); opts.filter.insert(f.try_into().unwrap());
} }
} }

View file

@ -3,7 +3,7 @@ use crate::deflate;
use crate::error::PngError; use crate::error::PngError;
use crate::filters::*; use crate::filters::*;
use crate::headers::*; use crate::headers::*;
use crate::interlace::{deinterlace_image, interlace_image}; use crate::interlace::{deinterlace_image, interlace_image, Interlacing};
use bitvec::bitarr; use bitvec::bitarr;
use indexmap::IndexMap; use indexmap::IndexMap;
use libdeflater::{CompressionLvl, Compressor}; use libdeflater::{CompressionLvl, Compressor};
@ -187,7 +187,7 @@ impl PngData {
.ok(); .ok();
ihdr_data.write_all(&[0]).ok(); // Compression -- deflate ihdr_data.write_all(&[0]).ok(); // Compression -- deflate
ihdr_data.write_all(&[0]).ok(); // Filter method -- 5-way adaptive filtering ihdr_data.write_all(&[0]).ok(); // Filter method -- 5-way adaptive filtering
ihdr_data.write_all(&[self.raw.ihdr.interlaced]).ok(); ihdr_data.write_all(&[self.raw.ihdr.interlaced as u8]).ok();
write_png_block(b"IHDR", &ihdr_data, &mut output); write_png_block(b"IHDR", &ihdr_data, &mut output);
// Ancillary headers // Ancillary headers
for (key, header) in self for (key, header) in self
@ -258,12 +258,12 @@ impl PngImage {
/// Assumes that the data has already been de-filtered /// Assumes that the data has already been de-filtered
#[inline] #[inline]
#[must_use] #[must_use]
pub fn change_interlacing(&self, interlace: u8) -> Option<PngImage> { pub fn change_interlacing(&self, interlace: Interlacing) -> Option<PngImage> {
if interlace == self.ihdr.interlaced { if interlace == self.ihdr.interlaced {
return None; return None;
} }
Some(if interlace == 1 { Some(if interlace == Interlacing::Adam7 {
// Convert progressive to interlaced data // Convert progressive to interlaced data
interlace_image(self) interlace_image(self)
} else { } else {

View file

@ -1,3 +1,4 @@
use crate::interlace::Interlacing;
use crate::png::PngImage; use crate::png::PngImage;
/// An iterator over the scan lines of a PNG image /// An iterator over the scan lines of a PNG image
@ -79,7 +80,7 @@ impl ScanLineRanges {
width: png.ihdr.width, width: png.ihdr.width,
height: png.ihdr.height, height: png.ihdr.height,
left: png.data.len(), left: png.data.len(),
pass: if png.ihdr.interlaced == 1 { pass: if png.ihdr.interlaced == Interlacing::Adam7 {
Some((1, 0)) Some((1, 0))
} else { } else {
None None

View file

@ -1,5 +1,5 @@
use indexmap::IndexSet; use indexmap::IndexSet;
use oxipng::{internal_tests::*, RowFilter}; use oxipng::{internal_tests::*, Interlacing, RowFilter};
use oxipng::{InFile, OutFile}; use oxipng::{InFile, OutFile};
#[cfg(feature = "filetime")] #[cfg(feature = "filetime")]
use std::cell::RefCell; use std::cell::RefCell;
@ -324,11 +324,11 @@ fn strip_headers_none() {
fn interlacing_0_to_1() { fn interlacing_0_to_1() {
let input = PathBuf::from("tests/files/interlacing_0_to_1.png"); let input = PathBuf::from("tests/files/interlacing_0_to_1.png");
let (output, mut opts) = get_opts(&input); let (output, mut opts) = get_opts(&input);
opts.interlace = Some(1); opts.interlace = Some(Interlacing::Adam7);
let png = PngData::new(&input, opts.fix_errors).unwrap(); let png = PngData::new(&input, opts.fix_errors).unwrap();
assert_eq!(png.raw.ihdr.interlaced, 0); assert_eq!(png.raw.ihdr.interlaced, Interlacing::None);
match oxipng::optimize(&InFile::Path(input), &output, &opts) { match oxipng::optimize(&InFile::Path(input), &output, &opts) {
Ok(_) => (), Ok(_) => (),
@ -345,7 +345,7 @@ fn interlacing_0_to_1() {
} }
}; };
assert_eq!(png.raw.ihdr.interlaced, 1); assert_eq!(png.raw.ihdr.interlaced, Interlacing::Adam7);
remove_file(output).ok(); remove_file(output).ok();
} }
@ -354,11 +354,11 @@ fn interlacing_0_to_1() {
fn interlacing_1_to_0() { fn interlacing_1_to_0() {
let input = PathBuf::from("tests/files/interlacing_1_to_0.png"); let input = PathBuf::from("tests/files/interlacing_1_to_0.png");
let (output, mut opts) = get_opts(&input); let (output, mut opts) = get_opts(&input);
opts.interlace = Some(0); opts.interlace = Some(Interlacing::None);
let png = PngData::new(&input, opts.fix_errors).unwrap(); let png = PngData::new(&input, opts.fix_errors).unwrap();
assert_eq!(png.raw.ihdr.interlaced, 1); assert_eq!(png.raw.ihdr.interlaced, Interlacing::Adam7);
match oxipng::optimize(&InFile::Path(input), &output, &opts) { match oxipng::optimize(&InFile::Path(input), &output, &opts) {
Ok(_) => (), Ok(_) => (),
@ -375,7 +375,7 @@ fn interlacing_1_to_0() {
} }
}; };
assert_eq!(png.raw.ihdr.interlaced, 0); assert_eq!(png.raw.ihdr.interlaced, Interlacing::None);
remove_file(output).ok(); remove_file(output).ok();
} }
@ -384,11 +384,11 @@ fn interlacing_1_to_0() {
fn interlacing_0_to_1_small_files() { fn interlacing_0_to_1_small_files() {
let input = PathBuf::from("tests/files/interlacing_0_to_1_small_files.png"); let input = PathBuf::from("tests/files/interlacing_0_to_1_small_files.png");
let (output, mut opts) = get_opts(&input); let (output, mut opts) = get_opts(&input);
opts.interlace = Some(1); opts.interlace = Some(Interlacing::Adam7);
let png = PngData::new(&input, opts.fix_errors).unwrap(); let png = PngData::new(&input, opts.fix_errors).unwrap();
assert_eq!(png.raw.ihdr.interlaced, 0); assert_eq!(png.raw.ihdr.interlaced, Interlacing::None);
assert_eq!(png.raw.ihdr.color_type, ColorType::Indexed); assert_eq!(png.raw.ihdr.color_type, ColorType::Indexed);
assert_eq!(png.raw.ihdr.bit_depth, BitDepth::Eight); assert_eq!(png.raw.ihdr.bit_depth, BitDepth::Eight);
@ -407,7 +407,7 @@ fn interlacing_0_to_1_small_files() {
} }
}; };
assert_eq!(png.raw.ihdr.interlaced, 1); assert_eq!(png.raw.ihdr.interlaced, Interlacing::Adam7);
assert_eq!(png.raw.ihdr.color_type, ColorType::Indexed); assert_eq!(png.raw.ihdr.color_type, ColorType::Indexed);
assert_eq!(png.raw.ihdr.bit_depth, BitDepth::One); assert_eq!(png.raw.ihdr.bit_depth, BitDepth::One);
@ -418,11 +418,11 @@ fn interlacing_0_to_1_small_files() {
fn interlacing_1_to_0_small_files() { fn interlacing_1_to_0_small_files() {
let input = PathBuf::from("tests/files/interlacing_1_to_0_small_files.png"); let input = PathBuf::from("tests/files/interlacing_1_to_0_small_files.png");
let (output, mut opts) = get_opts(&input); let (output, mut opts) = get_opts(&input);
opts.interlace = Some(0); opts.interlace = Some(Interlacing::None);
let png = PngData::new(&input, opts.fix_errors).unwrap(); let png = PngData::new(&input, opts.fix_errors).unwrap();
assert_eq!(png.raw.ihdr.interlaced, 1); assert_eq!(png.raw.ihdr.interlaced, Interlacing::Adam7);
assert_eq!(png.raw.ihdr.color_type, ColorType::Indexed); assert_eq!(png.raw.ihdr.color_type, ColorType::Indexed);
assert_eq!(png.raw.ihdr.bit_depth, BitDepth::Eight); assert_eq!(png.raw.ihdr.bit_depth, BitDepth::Eight);
@ -441,7 +441,7 @@ fn interlacing_1_to_0_small_files() {
} }
}; };
assert_eq!(png.raw.ihdr.interlaced, 0); assert_eq!(png.raw.ihdr.interlaced, Interlacing::None);
assert_eq!(png.raw.ihdr.color_type, ColorType::Indexed); assert_eq!(png.raw.ihdr.color_type, ColorType::Indexed);
// the depth can't be asserted reliably, because on such small file different zlib implementations pick different depth as the best // the depth can't be asserted reliably, because on such small file different zlib implementations pick different depth as the best
@ -452,14 +452,14 @@ fn interlacing_1_to_0_small_files() {
fn interlaced_0_to_1_other_filter_mode() { fn interlaced_0_to_1_other_filter_mode() {
let input = PathBuf::from("tests/files/interlaced_0_to_1_other_filter_mode.png"); let input = PathBuf::from("tests/files/interlaced_0_to_1_other_filter_mode.png");
let (output, mut opts) = get_opts(&input); let (output, mut opts) = get_opts(&input);
opts.interlace = Some(1); opts.interlace = Some(Interlacing::Adam7);
let mut filter = IndexSet::new(); let mut filter = IndexSet::new();
filter.insert(RowFilter::Paeth); filter.insert(RowFilter::Paeth);
opts.filter = filter; opts.filter = filter;
let png = PngData::new(&input, opts.fix_errors).unwrap(); let png = PngData::new(&input, opts.fix_errors).unwrap();
assert_eq!(png.raw.ihdr.interlaced, 0); assert_eq!(png.raw.ihdr.interlaced, Interlacing::None);
match oxipng::optimize(&InFile::Path(input), &output, &opts) { match oxipng::optimize(&InFile::Path(input), &output, &opts) {
Ok(_) => (), Ok(_) => (),
@ -476,7 +476,7 @@ fn interlaced_0_to_1_other_filter_mode() {
} }
}; };
assert_eq!(png.raw.ihdr.interlaced, 1); assert_eq!(png.raw.ihdr.interlaced, Interlacing::Adam7);
remove_file(output).ok(); remove_file(output).ok();
} }

View file

@ -1,5 +1,5 @@
use indexmap::IndexSet; use indexmap::IndexSet;
use oxipng::{internal_tests::*, RowFilter}; use oxipng::{internal_tests::*, Interlacing, RowFilter};
use oxipng::{InFile, OutFile}; use oxipng::{InFile, OutFile};
use std::fs::remove_file; use std::fs::remove_file;
use std::path::Path; use std::path::Path;
@ -33,7 +33,7 @@ fn test_it_converts(
assert_eq!(png.raw.ihdr.color_type, color_type_in); assert_eq!(png.raw.ihdr.color_type, color_type_in);
assert_eq!(png.raw.ihdr.bit_depth, bit_depth_in); assert_eq!(png.raw.ihdr.bit_depth, bit_depth_in);
assert_eq!(png.raw.ihdr.interlaced, 1); assert_eq!(png.raw.ihdr.interlaced, Interlacing::Adam7);
match oxipng::optimize(&InFile::Path(input), &output, &opts) { match oxipng::optimize(&InFile::Path(input), &output, &opts) {
Ok(_) => (), Ok(_) => (),

View file

@ -1,5 +1,5 @@
use indexmap::IndexSet; use indexmap::IndexSet;
use oxipng::{internal_tests::*, RowFilter}; use oxipng::{internal_tests::*, Interlacing, RowFilter};
use oxipng::{InFile, OutFile}; use oxipng::{InFile, OutFile};
use std::fs::remove_file; use std::fs::remove_file;
use std::path::Path; use std::path::Path;
@ -22,7 +22,7 @@ fn get_opts(input: &Path) -> (OutFile, oxipng::Options) {
fn test_it_converts( fn test_it_converts(
input: &str, input: &str,
interlace: u8, interlace: Interlacing,
color_type_in: ColorType, color_type_in: ColorType,
bit_depth_in: BitDepth, bit_depth_in: BitDepth,
color_type_out: ColorType, color_type_out: ColorType,
@ -34,7 +34,14 @@ fn test_it_converts(
opts.interlace = Some(interlace); opts.interlace = Some(interlace);
assert_eq!(png.raw.ihdr.color_type, color_type_in); assert_eq!(png.raw.ihdr.color_type, color_type_in);
assert_eq!(png.raw.ihdr.bit_depth, bit_depth_in); assert_eq!(png.raw.ihdr.bit_depth, bit_depth_in);
assert_eq!(png.raw.ihdr.interlaced, if interlace == 1 { 0 } else { 1 }); assert_eq!(
png.raw.ihdr.interlaced,
if interlace == Interlacing::Adam7 {
Interlacing::None
} else {
Interlacing::Adam7
}
);
match oxipng::optimize(&InFile::Path(input), &output, &opts) { match oxipng::optimize(&InFile::Path(input), &output, &opts) {
Ok(_) => (), Ok(_) => (),
@ -61,7 +68,7 @@ fn test_it_converts(
fn deinterlace_rgb_16() { fn deinterlace_rgb_16() {
test_it_converts( test_it_converts(
"tests/files/interlaced_rgb_16_should_be_rgb_16.png", "tests/files/interlaced_rgb_16_should_be_rgb_16.png",
0, Interlacing::None,
ColorType::RGB, ColorType::RGB,
BitDepth::Sixteen, BitDepth::Sixteen,
ColorType::RGB, ColorType::RGB,
@ -73,7 +80,7 @@ fn deinterlace_rgb_16() {
fn deinterlace_rgb_8() { fn deinterlace_rgb_8() {
test_it_converts( test_it_converts(
"tests/files/interlaced_rgb_8_should_be_rgb_8.png", "tests/files/interlaced_rgb_8_should_be_rgb_8.png",
0, Interlacing::None,
ColorType::RGB, ColorType::RGB,
BitDepth::Eight, BitDepth::Eight,
ColorType::RGB, ColorType::RGB,
@ -85,7 +92,7 @@ fn deinterlace_rgb_8() {
fn deinterlace_palette_8() { fn deinterlace_palette_8() {
test_it_converts( test_it_converts(
"tests/files/interlaced_palette_8_should_be_palette_8.png", "tests/files/interlaced_palette_8_should_be_palette_8.png",
0, Interlacing::None,
ColorType::Indexed, ColorType::Indexed,
BitDepth::Eight, BitDepth::Eight,
ColorType::Indexed, ColorType::Indexed,
@ -97,7 +104,7 @@ fn deinterlace_palette_8() {
fn deinterlace_palette_4() { fn deinterlace_palette_4() {
test_it_converts( test_it_converts(
"tests/files/interlaced_palette_4_should_be_palette_4.png", "tests/files/interlaced_palette_4_should_be_palette_4.png",
0, Interlacing::None,
ColorType::Indexed, ColorType::Indexed,
BitDepth::Four, BitDepth::Four,
ColorType::Indexed, ColorType::Indexed,
@ -109,7 +116,7 @@ fn deinterlace_palette_4() {
fn deinterlace_palette_2() { fn deinterlace_palette_2() {
test_it_converts( test_it_converts(
"tests/files/interlaced_palette_2_should_be_palette_2.png", "tests/files/interlaced_palette_2_should_be_palette_2.png",
0, Interlacing::None,
ColorType::Indexed, ColorType::Indexed,
BitDepth::Two, BitDepth::Two,
ColorType::Indexed, ColorType::Indexed,
@ -121,7 +128,7 @@ fn deinterlace_palette_2() {
fn deinterlace_palette_1() { fn deinterlace_palette_1() {
test_it_converts( test_it_converts(
"tests/files/interlaced_palette_1_should_be_palette_1.png", "tests/files/interlaced_palette_1_should_be_palette_1.png",
0, Interlacing::None,
ColorType::Indexed, ColorType::Indexed,
BitDepth::One, BitDepth::One,
ColorType::Indexed, ColorType::Indexed,
@ -133,7 +140,7 @@ fn deinterlace_palette_1() {
fn interlace_rgb_16() { fn interlace_rgb_16() {
test_it_converts( test_it_converts(
"tests/files/rgb_16_should_be_rgb_16.png", "tests/files/rgb_16_should_be_rgb_16.png",
1, Interlacing::Adam7,
ColorType::RGB, ColorType::RGB,
BitDepth::Sixteen, BitDepth::Sixteen,
ColorType::RGB, ColorType::RGB,
@ -145,7 +152,7 @@ fn interlace_rgb_16() {
fn interlace_rgb_8() { fn interlace_rgb_8() {
test_it_converts( test_it_converts(
"tests/files/rgb_8_should_be_rgb_8.png", "tests/files/rgb_8_should_be_rgb_8.png",
1, Interlacing::Adam7,
ColorType::RGB, ColorType::RGB,
BitDepth::Eight, BitDepth::Eight,
ColorType::RGB, ColorType::RGB,
@ -157,7 +164,7 @@ fn interlace_rgb_8() {
fn interlace_palette_8() { fn interlace_palette_8() {
test_it_converts( test_it_converts(
"tests/files/palette_8_should_be_palette_8.png", "tests/files/palette_8_should_be_palette_8.png",
1, Interlacing::Adam7,
ColorType::Indexed, ColorType::Indexed,
BitDepth::Eight, BitDepth::Eight,
ColorType::Indexed, ColorType::Indexed,
@ -169,7 +176,7 @@ fn interlace_palette_8() {
fn interlace_palette_4() { fn interlace_palette_4() {
test_it_converts( test_it_converts(
"tests/files/palette_4_should_be_palette_4.png", "tests/files/palette_4_should_be_palette_4.png",
1, Interlacing::Adam7,
ColorType::Indexed, ColorType::Indexed,
BitDepth::Four, BitDepth::Four,
ColorType::Indexed, ColorType::Indexed,
@ -181,7 +188,7 @@ fn interlace_palette_4() {
fn interlace_palette_2() { fn interlace_palette_2() {
test_it_converts( test_it_converts(
"tests/files/palette_2_should_be_palette_2.png", "tests/files/palette_2_should_be_palette_2.png",
1, Interlacing::Adam7,
ColorType::Indexed, ColorType::Indexed,
BitDepth::Two, BitDepth::Two,
ColorType::Indexed, ColorType::Indexed,
@ -193,7 +200,7 @@ fn interlace_palette_2() {
fn interlace_palette_1() { fn interlace_palette_1() {
test_it_converts( test_it_converts(
"tests/files/palette_1_should_be_palette_1.png", "tests/files/palette_1_should_be_palette_1.png",
1, Interlacing::Adam7,
ColorType::Indexed, ColorType::Indexed,
BitDepth::One, BitDepth::One,
ColorType::Indexed, ColorType::Indexed,

View file

@ -1,5 +1,5 @@
use indexmap::IndexSet; use indexmap::IndexSet;
use oxipng::{internal_tests::*, RowFilter}; use oxipng::{internal_tests::*, Interlacing, RowFilter};
use oxipng::{InFile, OutFile}; use oxipng::{InFile, OutFile};
use std::fs::remove_file; use std::fs::remove_file;
use std::path::Path; use std::path::Path;
@ -35,7 +35,7 @@ fn test_it_converts(
assert_eq!(png.raw.ihdr.color_type, color_type_in); assert_eq!(png.raw.ihdr.color_type, color_type_in);
assert_eq!(png.raw.ihdr.bit_depth, bit_depth_in, "test file is broken"); assert_eq!(png.raw.ihdr.bit_depth, bit_depth_in, "test file is broken");
assert_eq!(png.raw.ihdr.interlaced, 0); assert_eq!(png.raw.ihdr.interlaced, Interlacing::None);
match oxipng::optimize(&InFile::Path(input), &output, &opts) { match oxipng::optimize(&InFile::Path(input), &output, &opts) {
Ok(_) => (), Ok(_) => (),

View file

@ -1,5 +1,5 @@
use indexmap::IndexSet; use indexmap::IndexSet;
use oxipng::{internal_tests::*, RowFilter}; use oxipng::{internal_tests::*, Interlacing, RowFilter};
use oxipng::{InFile, OutFile}; use oxipng::{InFile, OutFile};
use std::fs::remove_file; use std::fs::remove_file;
use std::path::Path; use std::path::Path;
@ -92,11 +92,11 @@ fn issue_29() {
fn issue_42() { fn issue_42() {
let input = PathBuf::from("tests/files/issue_42.png"); let input = PathBuf::from("tests/files/issue_42.png");
let (output, mut opts) = get_opts(&input); let (output, mut opts) = get_opts(&input);
opts.interlace = Some(1); opts.interlace = Some(Interlacing::Adam7);
let png = PngData::new(&input, opts.fix_errors).unwrap(); let png = PngData::new(&input, opts.fix_errors).unwrap();
assert_eq!(png.raw.ihdr.interlaced, 0); assert_eq!(png.raw.ihdr.interlaced, Interlacing::None);
assert_eq!(png.raw.ihdr.color_type, ColorType::GrayscaleAlpha); assert_eq!(png.raw.ihdr.color_type, ColorType::GrayscaleAlpha);
assert_eq!(png.raw.ihdr.bit_depth, BitDepth::Eight); assert_eq!(png.raw.ihdr.bit_depth, BitDepth::Eight);
@ -115,7 +115,7 @@ fn issue_42() {
} }
}; };
assert_eq!(png.raw.ihdr.interlaced, 1); assert_eq!(png.raw.ihdr.interlaced, Interlacing::Adam7);
assert_eq!(png.raw.ihdr.color_type, ColorType::GrayscaleAlpha); assert_eq!(png.raw.ihdr.color_type, ColorType::GrayscaleAlpha);
assert_eq!(png.raw.ihdr.bit_depth, BitDepth::Eight); assert_eq!(png.raw.ihdr.bit_depth, BitDepth::Eight);
@ -311,7 +311,7 @@ fn issue_92_filter_5() {
fn issue_113() { fn issue_113() {
let input = "tests/files/issue-113.png"; let input = "tests/files/issue-113.png";
let (output, mut opts) = get_opts(Path::new(input)); let (output, mut opts) = get_opts(Path::new(input));
opts.interlace = Some(1); opts.interlace = Some(Interlacing::Adam7);
opts.optimize_alpha = true; opts.optimize_alpha = true;
test_it_converts( test_it_converts(
input, input,
@ -440,7 +440,7 @@ fn issue_175() {
fn issue_182() { fn issue_182() {
let input = "tests/files/issue-182.png"; let input = "tests/files/issue-182.png";
let (output, mut opts) = get_opts(Path::new(input)); let (output, mut opts) = get_opts(Path::new(input));
opts.interlace = Some(0); opts.interlace = Some(Interlacing::Adam7);
test_it_converts( test_it_converts(
input, input,