mirror of
https://github.com/eRgo35/ascii.git
synced 2026-02-04 12:46:09 +01:00
color
This commit is contained in:
82
src/main.rs
82
src/main.rs
@@ -1,85 +1,19 @@
|
||||
use std::env;
|
||||
use image;
|
||||
use image::GenericImageView;
|
||||
|
||||
fn parse_args() -> Vec<String> {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
|
||||
if args.len() < 2 {
|
||||
println!("Usage: {} <image_file>", args[0]);
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
args
|
||||
}
|
||||
|
||||
fn load_image(file_name: &str) -> image::DynamicImage {
|
||||
let img = image::open(file_name).expect("File not found!");
|
||||
println!("Image loaded: {file_name}");
|
||||
|
||||
img
|
||||
}
|
||||
|
||||
fn print_size(img: &image::DynamicImage) {
|
||||
let (width, height) = img.dimensions();
|
||||
println!("Image dimensions: {width}x{height}");
|
||||
}
|
||||
|
||||
fn select_char(brightness: &u8) -> &str {
|
||||
match brightness {
|
||||
0..=25 => " ",
|
||||
26..=50 => "..",
|
||||
51..=75 => "::",
|
||||
76..=100 => "--",
|
||||
101..=125 => "==",
|
||||
126..=150 => "++",
|
||||
151..=175 => "**",
|
||||
176..=200 => "##",
|
||||
201..=225 => "%%",
|
||||
226..=255 => "@@",
|
||||
}
|
||||
}
|
||||
|
||||
fn to_ascii(img: &image::DynamicImage) -> String {
|
||||
let (width, height) = img.dimensions();
|
||||
|
||||
let mut ascii_img = String::new();
|
||||
|
||||
for y in 0..height {
|
||||
for x in 0..width {
|
||||
let pixel = img.get_pixel(x, y);
|
||||
|
||||
let red = pixel[0] as u16;
|
||||
let green = pixel[1] as u16;
|
||||
let blue = pixel[2] as u16;
|
||||
|
||||
let brightness = ((red + green + blue) / 3) as u8;
|
||||
|
||||
ascii_img.push_str(select_char(&brightness));
|
||||
}
|
||||
ascii_img.push('\n');
|
||||
}
|
||||
|
||||
ascii_img
|
||||
}
|
||||
|
||||
fn resize_image(img: &image::DynamicImage, nwidth: u32, nheight: u32) -> image::DynamicImage {
|
||||
img.resize(nwidth, nheight, image::imageops::FilterType::Lanczos3)
|
||||
}
|
||||
use clap::Parser;
|
||||
mod libs;
|
||||
|
||||
fn main() {
|
||||
println!("ASCII Generator\n----------------");
|
||||
|
||||
let args: Vec<String> = parse_args();
|
||||
let args = libs::args::Args::parse();
|
||||
|
||||
let img = load_image(&args[1]);
|
||||
print_size(&img);
|
||||
let img = libs::image::load_image(&args.image);
|
||||
libs::image::print_size(&img);
|
||||
|
||||
let resized_img = resize_image(&img, 80, 25);
|
||||
let resized_img = libs::image::resize_image(&img, args.width, args.height);
|
||||
println!("Image resized");
|
||||
print_size(&resized_img);
|
||||
libs::image::print_size(&resized_img);
|
||||
|
||||
let ascii_img = to_ascii(&resized_img);
|
||||
let ascii_img = libs::ascii::to_ascii(&resized_img, &args);
|
||||
println!("ASCII image created");
|
||||
|
||||
println!("{ascii_img}");
|
||||
|
||||
Reference in New Issue
Block a user