Improve output for out-of-order processing

This commit is contained in:
Andrew 2023-07-02 17:22:04 +12:00 committed by Josh Holmer
parent bde1d113e4
commit 5b38f6161f
3 changed files with 23 additions and 6 deletions

View file

@ -453,13 +453,15 @@ pub fn optimize(input: &InFile, output: &OutFile, opts: &Options) -> PngResult<(
// Run the optimizer on the decoded PNG. // Run the optimizer on the decoded PNG.
let mut optimized_output = optimize_png(&mut png, &in_data, opts, deadline)?; let mut optimized_output = optimize_png(&mut png, &in_data, opts, deadline)?;
let in_length = in_data.len();
if is_fully_optimized(in_data.len(), optimized_output.len(), opts) { if is_fully_optimized(in_data.len(), optimized_output.len(), opts) {
info!("File already optimized");
match (output, input) { match (output, input) {
// if p is None, it also means same as the input path // if p is None, it also means same as the input path
(OutFile::Path(ref p), InFile::Path(ref input_path)) (OutFile::Path(ref p), InFile::Path(ref input_path))
if p.as_ref().map_or(true, |p| p == input_path) => if p.as_ref().map_or(true, |p| p == input_path) =>
{ {
info!("{}: Could not optimize further, no change written", input);
return Ok(()); return Ok(());
} }
_ => { _ => {
@ -468,8 +470,22 @@ pub fn optimize(input: &InFile, output: &OutFile, opts: &Options) -> PngResult<(
} }
} }
let savings = if in_length >= optimized_output.len() {
format!(
"{} bytes ({:.2}% smaller)",
optimized_output.len(),
(in_length - optimized_output.len()) as f64 / in_length as f64 * 100_f64
)
} else {
format!(
"{} bytes ({:.2}% larger)",
optimized_output.len(),
(optimized_output.len() - in_length) as f64 / in_length as f64 * 100_f64
)
};
if opts.pretend { if opts.pretend {
info!("Running in pretend mode, no output"); info!("{}: Running in pretend mode, no output", savings);
return Ok(()); return Ok(());
} }
@ -516,7 +532,7 @@ pub fn optimize(input: &InFile, output: &OutFile, opts: &Options) -> PngResult<(
if let Some(metadata_input) = &opt_metadata_preserved { if let Some(metadata_input) = &opt_metadata_preserved {
copy_times(metadata_input, output_path)?; copy_times(metadata_input, output_path)?;
} }
info!("Output: {}", output_path.display()); info!("{}: {}", savings, output_path.display());
} }
} }
Ok(()) Ok(())
@ -606,14 +622,14 @@ fn optimize_png(
); );
} }
if file_original_size >= output.len() { if file_original_size >= output.len() {
info!( debug!(
" file size = {} bytes ({} bytes = {:.2}% decrease)", " file size = {} bytes ({} bytes = {:.2}% decrease)",
output.len(), output.len(),
file_original_size - output.len(), file_original_size - output.len(),
(file_original_size - output.len()) as f64 / file_original_size as f64 * 100_f64 (file_original_size - output.len()) as f64 / file_original_size as f64 * 100_f64
); );
} else { } else {
info!( debug!(
" file size = {} bytes ({} bytes = {:.2}% increase)", " file size = {} bytes ({} bytes = {:.2}% increase)",
output.len(), output.len(),
output.len() - file_original_size, output.len() - file_original_size,

View file

@ -328,7 +328,7 @@ Heuristic filter selection strategies:
// We don't really want to return an error code for those files. // We don't really want to return an error code for those files.
Ok(_) => true, Ok(_) => true,
Err(e) => { Err(e) => {
error!("{}", e); error!("{}: {}", input, e);
false false
} }
} }

View file

@ -188,6 +188,7 @@ fn verbose_mode() {
"Found better combination:", "Found better combination:",
" zc = 11 f = None ", " zc = 11 f = None ",
" IDAT size = ", " IDAT size = ",
" file size = ",
]; ];
assert_eq!(logs.len(), expected_prefixes.len()); assert_eq!(logs.len(), expected_prefixes.len());
for (i, log) in logs.into_iter().enumerate() { for (i, log) in logs.into_iter().enumerate() {