slightly improve printing and explanations
This commit is contained in:
parent
5a6f88ca4b
commit
f9fea83ed4
1 changed files with 11 additions and 7 deletions
18
src/main.rs
18
src/main.rs
|
|
@ -62,7 +62,8 @@ fn encode_standard(pixels: &[[u8; 4]], width: u32, height: u32) -> Vec<u8> {
|
||||||
w
|
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)]
|
#[derive(Parser, Debug)]
|
||||||
#[command(version, about, long_about = None)]
|
#[command(version, about, long_about = None)]
|
||||||
struct Args {
|
struct Args {
|
||||||
|
|
@ -79,14 +80,14 @@ struct Args {
|
||||||
|
|
||||||
fn parse_rgb(hex: String) -> Result<[u8; 4], String> {
|
fn parse_rgb(hex: String) -> Result<[u8; 4], String> {
|
||||||
if hex.len() != 6 {
|
if hex.len() != 6 {
|
||||||
return Err("expected rrggbb".into());
|
return Err("Expected the format rrggbb".into());
|
||||||
}
|
}
|
||||||
|
|
||||||
let bytes: Vec<u8> = (0..6)
|
let bytes: Vec<u8> = (0..6)
|
||||||
.step_by(2)
|
.step_by(2)
|
||||||
.map(|i| u8::from_str_radix(&hex[i..i + 2], 16))
|
.map(|i| u8::from_str_radix(&hex[i..i + 2], 16))
|
||||||
.collect::<Result<_, _>>()
|
.collect::<Result<_, _>>()
|
||||||
.map_err(|_| "invalid hex color")?;
|
.map_err(|_| "Invalid hex color")?;
|
||||||
|
|
||||||
Ok([bytes[0], bytes[1], bytes[2], 0xff])
|
Ok([bytes[0], bytes[1], bytes[2], 0xff])
|
||||||
}
|
}
|
||||||
|
|
@ -109,9 +110,9 @@ fn main() {
|
||||||
.map(|[a, b]| (*a, *b))
|
.map(|[a, b]| (*a, *b))
|
||||||
.collect();
|
.collect();
|
||||||
if asubs_len == 0 {
|
if asubs_len == 0 {
|
||||||
println!("no substitutions selected, only re-encoding")
|
println!("No substitutions selected, only re-encoding")
|
||||||
} else {
|
} else {
|
||||||
println!("substitutions to use: {:?}", pairs);
|
println!("Using substitutions: {:?}", pairs);
|
||||||
}
|
}
|
||||||
|
|
||||||
let decoder = PngDecoder::new(BufReader::new(File::open(in_path).unwrap())).unwrap();
|
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 plte_data = encode_plte(image_data.as_slice(), width, height);
|
||||||
let std_data = encode_standard(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!(
|
||||||
println!("std data has length {}", std_data.len(),);
|
"Palette data has length {}, standard has {}",
|
||||||
|
plte_data.len(),
|
||||||
|
std_data.len()
|
||||||
|
);
|
||||||
|
|
||||||
let mut file = File::create(out_path).unwrap();
|
let mut file = File::create(out_path).unwrap();
|
||||||
if plte_data.len() <= std_data.len() {
|
if plte_data.len() <= std_data.len() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue