diff --git a/src/colors.rs b/src/colors.rs index 32999849..77ee9818 100644 --- a/src/colors.rs +++ b/src/colors.rs @@ -1,5 +1,5 @@ use rgb::{RGB16, RGBA8}; -use std::fmt; +use std::{fmt, fmt::Display}; use crate::PngError; @@ -27,20 +27,18 @@ pub enum ColorType { RGBA, } -impl fmt::Display for ColorType { +impl Display for ColorType { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!( - f, - "{}", - match *self { - ColorType::Grayscale { .. } => "Grayscale", - ColorType::RGB { .. } => "RGB", - ColorType::Indexed { .. } => "Indexed", - ColorType::GrayscaleAlpha => "Grayscale + Alpha", - ColorType::RGBA => "RGB + Alpha", + match self { + ColorType::Grayscale { .. } => Display::fmt("Grayscale", f), + ColorType::RGB { .. } => Display::fmt("RGB", f), + ColorType::Indexed { palette } => { + Display::fmt(&format!("Indexed ({} colors)", palette.len()), f) } - ) + ColorType::GrayscaleAlpha => Display::fmt("Grayscale + Alpha", f), + ColorType::RGBA => Display::fmt("RGB + Alpha", f), + } } } @@ -109,9 +107,9 @@ impl TryFrom for BitDepth { } } -impl fmt::Display for BitDepth { +impl Display for BitDepth { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", *self as u8) + Display::fmt(&(*self as u8).to_string(), f) } } diff --git a/src/filters.rs b/src/filters.rs index d43c2833..1fec39e6 100644 --- a/src/filters.rs +++ b/src/filters.rs @@ -1,4 +1,5 @@ -use std::{fmt::Display, mem::transmute}; +use std::mem::transmute; +use std::{fmt, fmt::Display}; use crate::error::PngError; @@ -31,11 +32,9 @@ impl TryFrom for RowFilter { } impl Display for RowFilter { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{:8}", - match *self { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + Display::fmt( + match self { Self::None => "None", Self::Sub => "Sub", Self::Up => "Up", @@ -46,7 +45,8 @@ impl Display for RowFilter { Self::Bigrams => "Bigrams", Self::BigEnt => "BigEnt", Self::Brute => "Brute", - } + }, + f, ) } } diff --git a/src/interlace.rs b/src/interlace.rs index 88c146ff..0fd63f21 100644 --- a/src/interlace.rs +++ b/src/interlace.rs @@ -1,4 +1,4 @@ -use std::fmt::Display; +use std::{fmt, fmt::Display}; use crate::headers::IhdrData; use crate::png::PngImage; @@ -25,14 +25,13 @@ impl TryFrom for Interlacing { } impl Display for Interlacing { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{}", - match *self { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + Display::fmt( + match self { Self::None => "non-interlaced", Self::Adam7 => "interlaced", - } + }, + f, ) } } diff --git a/src/lib.rs b/src/lib.rs index 22a457bc..d331c6dc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,7 @@ extern crate rayon; mod rayon; use crate::atomicmin::AtomicMin; -use crate::colors::{BitDepth, ColorType}; +use crate::colors::BitDepth; use crate::deflate::{crc32, inflate}; use crate::evaluate::Evaluator; use crate::png::PngData; @@ -615,7 +615,7 @@ fn optimize_png( png.idat_data = idat_data; debug!("Found better combination:"); debug!( - " zc = {} f = {} {} bytes", + " zc = {} f = {:8} {} bytes", opts.compression, opts.filter, png.idat_data.len() @@ -747,7 +747,7 @@ fn perform_trial( Ok(n) => { let bytes = n.len(); trace!( - " zc = {} f = {} {} bytes", + " zc = {} f = {:8} {} bytes", trial.compression, trial.filter, bytes @@ -756,7 +756,7 @@ fn perform_trial( } Err(PngError::DeflatedDataTooLong(bytes)) => { trace!( - " zc = {} f = {} >{} bytes", + " zc = {} f = {:8} >{} bytes", trial.compression, trial.filter, bytes, @@ -818,24 +818,10 @@ impl Deadline { /// Display the format of the image data fn report_format(prefix: &str, png: &PngImage) { - if let ColorType::Indexed { palette } = &png.ihdr.color_type { - debug!( - "{}{} bits/pixel, {} colors in palette ({})", - prefix, - png.ihdr.bit_depth, - palette.len(), - png.ihdr.interlaced - ); - } else { - debug!( - "{}{}x{} bits/pixel, {} ({})", - prefix, - png.channels_per_pixel(), - png.ihdr.bit_depth, - png.ihdr.color_type, - png.ihdr.interlaced - ); - } + debug!( + "{}{}-bit {}, {}", + prefix, png.ihdr.bit_depth, png.ihdr.color_type, png.ihdr.interlaced + ); } /// Strip headers from the `PngData` object, as requested by the passed `Options` diff --git a/tests/flags.rs b/tests/flags.rs index 281efb00..3690f21a 100644 --- a/tests/flags.rs +++ b/tests/flags.rs @@ -182,7 +182,7 @@ fn verbose_mode() { let logs: Vec<_> = receiver.into_iter().collect(); let expected_prefixes = [ " 500x400 pixels, PNG format", - " 3x8 bits/pixel, RGB (non-interlaced)", + " 8-bit RGB, non-interlaced", " IDAT size = 113794 bytes", " File size = 114708 bytes", "Trying: 1 filters",