From b180ca5f566a0ee2a4c101cf5fab70cd8fe2216b Mon Sep 17 00:00:00 2001 From: Anas Elgarhy Date: Mon, 3 Oct 2022 20:50:48 +0200 Subject: [PATCH] =?UTF-8?q?Add=20the=20background=20option=20=F0=9F=A5=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/args.rs | 3 +++ src/ascii_processor.rs | 54 +++++++++++++++--------------------------- src/main.rs | 17 ++++--------- 3 files changed, 26 insertions(+), 48 deletions(-) diff --git a/src/args.rs b/src/args.rs index 2a16eb8..4e0d1b5 100644 --- a/src/args.rs +++ b/src/args.rs @@ -19,6 +19,9 @@ pub mod args { /// The output scale (1 is the original size) #[arg(short, long, default_value="4")] pub scale: u32, + /// The background color to use + #[arg(short, long, default_value=None)] + pub background: Option, /// The output file to write to (if output_method is file) #[arg(short, long, default_value="ascii_image.txt")] pub output: String, diff --git a/src/ascii_processor.rs b/src/ascii_processor.rs index e5467e2..a6ae85f 100644 --- a/src/ascii_processor.rs +++ b/src/ascii_processor.rs @@ -1,4 +1,5 @@ use clap::arg; +use clap::builder::Str; use image::{GenericImageView, DynamicImage}; use colored::{ColoredString, Colorize}; use crate::args::{ @@ -6,7 +7,7 @@ use crate::args::{ enums::Mode }; -pub fn generate_ascii(image: DynamicImage, args: &Arguments) -> Result, error::ASCIIProcessingError> { +pub fn generate_ascii(image: DynamicImage, args: &Arguments) -> Vec { let characters = args.characters.chars().collect::>(); trace!("Characters: {:?}, length: {}", characters, characters.len()); let mut output = Vec::new(); @@ -15,7 +16,11 @@ pub fn generate_ascii(image: DynamicImage, args: &Arguments) -> Result Result, characters: &Vec, mode: Mode) -> ColoredString { +fn get_character( + pixel: image::Rgba, + characters: &Vec, mode: Mode, + background: &Option +) -> ColoredString { 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); - match mode { + let ch = match mode { Mode::NormalAscii => ColoredString::from(&*ch), Mode::COLORED => { ch.to_string() .truecolor(pixel[0], pixel[1], pixel[2]) } - } -} - -mod error { - use std::error::Error; - use std::fmt::{Debug, Display, Formatter}; - - #[derive(Debug)] - pub struct ASCIIProcessingError { - message: String, - } - - impl ASCIIProcessingError { - pub fn new(message: String) -> Self { - ASCIIProcessingError { - message - } - } - } - - impl Display for ASCIIProcessingError { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.message) - } - } - - impl Error for ASCIIProcessingError { - fn description(&self) -> &str { - &self.message - } + }; + + match background { + Some(bg) => ch.on_color(bg.to_string()), + None => ch } } diff --git a/src/main.rs b/src/main.rs index ddba124..de3ba00 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use std::io::Write; use clap::Parser; use image::GenericImageView; -use colored::Colorize; + extern crate pretty_env_logger; #[macro_use] @@ -12,7 +12,7 @@ mod ascii_processor; use crate::args::{ args::Arguments, - enums::{Mode, OutputMethod}, + enums::OutputMethod, }; use crate::ascii_processor::generate_ascii; @@ -52,17 +52,8 @@ fn main() { trace!("Image dimensions: {:?}", image.dimensions()); // Process the image - let output = match generate_ascii(image, &arguments) { - Ok(out) => { - info!("Successfully processed image"); - out - } - Err(e) => { - error!("Failed to process image: {:?}", e); - eprintln!("Failed to process image: {:?}", e); - std::process::exit(1); - } - }; + let output = generate_ascii(image, &arguments); + info!("Successfully processed image"); // Output the image info!("Outputting image");