add charset arg and fix charset indexing bug

This commit is contained in:
markichnich 2024-03-17 20:56:51 +01:00
parent 26b3301a04
commit 60d97109ea
2 changed files with 17 additions and 4 deletions

View File

@ -42,4 +42,14 @@ pub fn new() -> Command {
.value_parser(value_parser!(f64)) .value_parser(value_parser!(f64))
.default_value("2.3"), .default_value("2.3"),
) )
.arg(
Arg::new("charset")
.short('c')
.long("charset")
.help("Character set to use in ascending order of pixel value")
.action(ArgAction::Set)
.value_name("STRING")
.value_parser(value_parser!(String))
.default_value(" .:-=+*%#@"),
)
} }

View File

@ -3,14 +3,17 @@ use std::{fmt, fs::File, io, path::PathBuf};
mod cli; mod cli;
const SYMBOLS: &[char] = &[' ', '.', ':', '-', '=', '+', '*', '#', '%', '@'];
fn main() -> Result<(), &'static str> { fn main() -> Result<(), &'static str> {
let matches = cli::new().get_matches(); let matches = cli::new().get_matches();
let input_file: &PathBuf = matches.get_one("input").unwrap(); let input_file: &PathBuf = matches.get_one("input").unwrap();
let x_stretch: f64 = *matches.get_one("x-stretch").unwrap(); let x_stretch: f64 = *matches.get_one("x-stretch").unwrap();
let size: u32 = *matches.get_one("size").unwrap(); let size: u32 = *matches.get_one("size").unwrap();
let symbols = matches
.get_one::<String>("charset")
.unwrap()
.chars()
.collect::<Vec<_>>();
let img = image::open(input_file).unwrap(); let img = image::open(input_file).unwrap();
let (w, h) = img.dimensions(); let (w, h) = img.dimensions();
@ -26,9 +29,9 @@ fn main() -> Result<(), &'static str> {
.map(|(_, _, color)| { .map(|(_, _, color)| {
let sum: u16 = color.0.iter().take(3).map(|&v| v as u16).sum::<u16>(); let sum: u16 = color.0.iter().take(3).map(|&v| v as u16).sum::<u16>();
let normalized = sum as f64 / (3.0 * 255.0); let normalized = sum as f64 / (3.0 * 255.0);
let idx = (normalized * SYMBOLS.len() as f64).round() as usize; let idx = (normalized * symbols.len() as f64) as usize;
SYMBOLS[idx] symbols[idx]
}) })
.collect::<Vec<_>>() .collect::<Vec<_>>()
.chunks(w as usize) .chunks(w as usize)