2022-10-03 18:13:59 +00:00
|
|
|
use clap::arg;
|
2022-10-03 18:50:48 +00:00
|
|
|
use clap::builder::Str;
|
2022-10-03 18:13:59 +00:00
|
|
|
use image::{GenericImageView, DynamicImage};
|
|
|
|
use colored::{ColoredString, Colorize};
|
|
|
|
use crate::args::{
|
|
|
|
args::Arguments,
|
|
|
|
enums::Mode
|
|
|
|
};
|
|
|
|
|
2022-10-03 18:50:48 +00:00
|
|
|
pub fn generate_ascii(image: DynamicImage, args: &Arguments) -> Vec<ColoredString> {
|
2022-10-03 18:13:59 +00:00
|
|
|
let characters = args.characters.chars().collect::<Vec<char>>();
|
|
|
|
trace!("Characters: {:?}, length: {}", characters, characters.len());
|
|
|
|
let mut output = Vec::new();
|
|
|
|
let (width, height) = image.dimensions();
|
|
|
|
|
|
|
|
for y in 0..height {
|
|
|
|
for x in 0..width {
|
|
|
|
if y % (args.scale * 2) == 0 && x % args.scale == 0 {
|
2022-10-03 18:50:48 +00:00
|
|
|
output.push(get_character(
|
|
|
|
image.get_pixel(x, y),
|
|
|
|
&characters, args.mode,
|
|
|
|
&args.background
|
|
|
|
));
|
2022-10-03 18:13:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add a new line at the end of each row
|
|
|
|
if y % (args.scale * 2) == 0 {
|
|
|
|
output.push("\n".into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 18:50:48 +00:00
|
|
|
output
|
2022-10-03 18:13:59 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 18:50:48 +00:00
|
|
|
fn get_character(
|
|
|
|
pixel: image::Rgba<u8>,
|
|
|
|
characters: &Vec<char>, mode: Mode,
|
|
|
|
background: &Option<String>
|
|
|
|
) -> ColoredString {
|
2022-10-03 18:13:59 +00:00
|
|
|
let intent = if pixel[3] == 0 { 0 } else { pixel[0] / 3 + pixel[1] / 3 + pixel[2] / 3 };
|
|
|
|
|
|
|
|
let ch = characters[(intent / (32 + 7 - (7 + (characters.len() - 7)) as u8)) as usize];
|
|
|
|
|
|
|
|
let ch = String::from(ch);
|
|
|
|
|
2022-10-03 18:50:48 +00:00
|
|
|
let ch = match mode {
|
2022-10-03 18:13:59 +00:00
|
|
|
Mode::NormalAscii => ColoredString::from(&*ch),
|
|
|
|
Mode::COLORED => {
|
|
|
|
ch.to_string()
|
|
|
|
.truecolor(pixel[0], pixel[1], pixel[2])
|
|
|
|
}
|
2022-10-03 18:50:48 +00:00
|
|
|
};
|
2022-10-03 18:13:59 +00:00
|
|
|
|
2022-10-03 18:50:48 +00:00
|
|
|
match background {
|
|
|
|
Some(bg) => ch.on_color(bg.to_string()),
|
|
|
|
None => ch
|
2022-10-03 18:13:59 +00:00
|
|
|
}
|
|
|
|
}
|