slightly improve printing and explanations

This commit is contained in:
Noa Aarts 2026-01-26 20:30:40 +01:00
parent 5a6f88ca4b
commit f9fea83ed4
Signed by: noa
GPG key ID: 1850932741EFF672

View file

@ -62,7 +62,8 @@ fn encode_standard(pixels: &[[u8; 4]], width: u32, height: u32) -> Vec<u8> {
w
}
/// Simple program to optimise palette based PNGs
/// Simple program to optimise palette based PNGs and substitute certain rgb values
/// Assumes that there is only alpha 0 and alpha 255
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
@ -79,14 +80,14 @@ struct Args {
fn parse_rgb(hex: String) -> Result<[u8; 4], String> {
if hex.len() != 6 {
return Err("expected rrggbb".into());
return Err("Expected the format rrggbb".into());
}
let bytes: Vec<u8> = (0..6)
.step_by(2)
.map(|i| u8::from_str_radix(&hex[i..i + 2], 16))
.collect::<Result<_, _>>()
.map_err(|_| "invalid hex color")?;
.map_err(|_| "Invalid hex color")?;
Ok([bytes[0], bytes[1], bytes[2], 0xff])
}
@ -109,9 +110,9 @@ fn main() {
.map(|[a, b]| (*a, *b))
.collect();
if asubs_len == 0 {
println!("no substitutions selected, only re-encoding")
println!("No substitutions selected, only re-encoding")
} else {
println!("substitutions to use: {:?}", pairs);
println!("Using substitutions: {:?}", pairs);
}
let decoder = PngDecoder::new(BufReader::new(File::open(in_path).unwrap())).unwrap();
@ -127,8 +128,11 @@ fn main() {
let plte_data = encode_plte(image_data.as_slice(), width, height);
let std_data = encode_standard(image_data.as_slice(), width, height);
println!("plte data has length {}", plte_data.len(),);
println!("std data has length {}", std_data.len(),);
println!(
"Palette data has length {}, standard has {}",
plte_data.len(),
std_data.len()
);
let mut file = File::create(out_path).unwrap();
if plte_data.len() <= std_data.len() {