Tweak reporting format

This commit is contained in:
Andrew 2023-05-06 11:40:48 +12:00
parent 0452f35a3c
commit 59f88e705d
5 changed files with 34 additions and 51 deletions

View file

@ -1,5 +1,5 @@
use rgb::{RGB16, RGBA8}; use rgb::{RGB16, RGBA8};
use std::fmt; use std::{fmt, fmt::Display};
use crate::PngError; use crate::PngError;
@ -27,20 +27,18 @@ pub enum ColorType {
RGBA, RGBA,
} }
impl fmt::Display for ColorType { impl Display for ColorType {
#[inline] #[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!( match self {
f, ColorType::Grayscale { .. } => Display::fmt("Grayscale", f),
"{}", ColorType::RGB { .. } => Display::fmt("RGB", f),
match *self { ColorType::Indexed { palette } => {
ColorType::Grayscale { .. } => "Grayscale", Display::fmt(&format!("Indexed ({} colors)", palette.len()), f)
ColorType::RGB { .. } => "RGB",
ColorType::Indexed { .. } => "Indexed",
ColorType::GrayscaleAlpha => "Grayscale + Alpha",
ColorType::RGBA => "RGB + Alpha",
} }
) ColorType::GrayscaleAlpha => Display::fmt("Grayscale + Alpha", f),
ColorType::RGBA => Display::fmt("RGB + Alpha", f),
}
} }
} }
@ -109,9 +107,9 @@ impl TryFrom<u8> for BitDepth {
} }
} }
impl fmt::Display for BitDepth { impl Display for BitDepth {
#[inline] #[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", *self as u8) Display::fmt(&(*self as u8).to_string(), f)
} }
} }

View file

@ -1,4 +1,5 @@
use std::{fmt::Display, mem::transmute}; use std::mem::transmute;
use std::{fmt, fmt::Display};
use crate::error::PngError; use crate::error::PngError;
@ -31,11 +32,9 @@ impl TryFrom<u8> for RowFilter {
} }
impl Display for RowFilter { impl Display for RowFilter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!( Display::fmt(
f, match self {
"{:8}",
match *self {
Self::None => "None", Self::None => "None",
Self::Sub => "Sub", Self::Sub => "Sub",
Self::Up => "Up", Self::Up => "Up",
@ -46,7 +45,8 @@ impl Display for RowFilter {
Self::Bigrams => "Bigrams", Self::Bigrams => "Bigrams",
Self::BigEnt => "BigEnt", Self::BigEnt => "BigEnt",
Self::Brute => "Brute", Self::Brute => "Brute",
} },
f,
) )
} }
} }

View file

@ -1,4 +1,4 @@
use std::fmt::Display; use std::{fmt, fmt::Display};
use crate::headers::IhdrData; use crate::headers::IhdrData;
use crate::png::PngImage; use crate::png::PngImage;
@ -25,14 +25,13 @@ impl TryFrom<u8> for Interlacing {
} }
impl Display for Interlacing { impl Display for Interlacing {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!( Display::fmt(
f, match self {
"{}",
match *self {
Self::None => "non-interlaced", Self::None => "non-interlaced",
Self::Adam7 => "interlaced", Self::Adam7 => "interlaced",
} },
f,
) )
} }
} }

View file

@ -25,7 +25,7 @@ extern crate rayon;
mod rayon; mod rayon;
use crate::atomicmin::AtomicMin; use crate::atomicmin::AtomicMin;
use crate::colors::{BitDepth, ColorType}; use crate::colors::BitDepth;
use crate::deflate::{crc32, inflate}; use crate::deflate::{crc32, inflate};
use crate::evaluate::Evaluator; use crate::evaluate::Evaluator;
use crate::png::PngData; use crate::png::PngData;
@ -615,7 +615,7 @@ fn optimize_png(
png.idat_data = idat_data; png.idat_data = idat_data;
debug!("Found better combination:"); debug!("Found better combination:");
debug!( debug!(
" zc = {} f = {} {} bytes", " zc = {} f = {:8} {} bytes",
opts.compression, opts.compression,
opts.filter, opts.filter,
png.idat_data.len() png.idat_data.len()
@ -747,7 +747,7 @@ fn perform_trial(
Ok(n) => { Ok(n) => {
let bytes = n.len(); let bytes = n.len();
trace!( trace!(
" zc = {} f = {} {} bytes", " zc = {} f = {:8} {} bytes",
trial.compression, trial.compression,
trial.filter, trial.filter,
bytes bytes
@ -756,7 +756,7 @@ fn perform_trial(
} }
Err(PngError::DeflatedDataTooLong(bytes)) => { Err(PngError::DeflatedDataTooLong(bytes)) => {
trace!( trace!(
" zc = {} f = {} >{} bytes", " zc = {} f = {:8} >{} bytes",
trial.compression, trial.compression,
trial.filter, trial.filter,
bytes, bytes,
@ -818,24 +818,10 @@ impl Deadline {
/// Display the format of the image data /// Display the format of the image data
fn report_format(prefix: &str, png: &PngImage) { fn report_format(prefix: &str, png: &PngImage) {
if let ColorType::Indexed { palette } = &png.ihdr.color_type { debug!(
debug!( "{}{}-bit {}, {}",
"{}{} bits/pixel, {} colors in palette ({})", prefix, png.ihdr.bit_depth, png.ihdr.color_type, png.ihdr.interlaced
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
);
}
} }
/// Strip headers from the `PngData` object, as requested by the passed `Options` /// Strip headers from the `PngData` object, as requested by the passed `Options`

View file

@ -182,7 +182,7 @@ fn verbose_mode() {
let logs: Vec<_> = receiver.into_iter().collect(); let logs: Vec<_> = receiver.into_iter().collect();
let expected_prefixes = [ let expected_prefixes = [
" 500x400 pixels, PNG format", " 500x400 pixels, PNG format",
" 3x8 bits/pixel, RGB (non-interlaced)", " 8-bit RGB, non-interlaced",
" IDAT size = 113794 bytes", " IDAT size = 113794 bytes",
" File size = 114708 bytes", " File size = 114708 bytes",
"Trying: 1 filters", "Trying: 1 filters",