From 135274c9c54f97c4af6bdca272a4e54925b4609f Mon Sep 17 00:00:00 2001 From: djairoh Date: Wed, 8 Feb 2023 17:24:17 +0100 Subject: [PATCH] rebased to use custom struct over String --- src/ascii_manipulation.rs | 26 ++++++++++++-------------- src/main.rs | 11 +++-------- src/model_rgb_ascii.rs | 29 +++++++++++++++++++++++++++++ src/output.rs | 13 +++++++++---- 4 files changed, 53 insertions(+), 26 deletions(-) create mode 100644 src/model_rgb_ascii.rs diff --git a/src/ascii_manipulation.rs b/src/ascii_manipulation.rs index 9250597..515bc05 100644 --- a/src/ascii_manipulation.rs +++ b/src/ascii_manipulation.rs @@ -1,50 +1,48 @@ use std::process::exit; use image::{DynamicImage, GenericImageView, Rgba}; use log::error; +use crate::model_rgb_ascii::Ascii; -//todo: consider how to take care of the a channel => do we want to render that as background? fn get_color(pixel: (u32, u32, Rgba)) -> u8 { let vec = pixel.2.0; //luminosity method of getting lightness (vec[0] as f32 * 0.3 + vec[1] as f32 * 0.59 + vec[2] as f32 * 0.11) as u8 } -fn to_ascii(char_map: String, image: DynamicImage) -> Vec { +fn to_ascii(char_map: String, image: DynamicImage) -> Vec> { let l = char_map.len() as f32; - let mut str = String::new(); - let mut out: Vec = Vec::new(); + let mut str: Vec = Vec::new(); + let mut out: Vec> = Vec::new(); for pixel in image.pixels() { let ch = char_map.as_bytes()[((get_color(pixel) as f32-1.0)/255f32 * l) as usize]; //fixme: might break with non-ASCII char_map (ie braille chars, possibly) - str.push(char::from(ch)); //fixme: this is finicky and also very unsafe + str.push(Ascii::new(ch, pixel.2[0], pixel.2[1], pixel.2[2])); if pixel.0 == image.width()-1 { out.push(str); - str = String::new(); + str = Vec::new(); } } out } -pub fn to_simple_ascii(image: DynamicImage) -> Vec { +pub fn to_simple_ascii(image: DynamicImage) -> Vec> { to_ascii(" .:-=+*#%@".to_owned(), image) } -pub fn to_complex_ascii(image: DynamicImage) -> Vec { +pub fn to_complex_ascii(image: DynamicImage) -> Vec> { to_ascii(" .'`^\",:;Il!i><~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$".to_owned(), image) } -pub fn to_braille_ascii(image: DynamicImage) -> Vec { +pub fn to_braille_ascii(image: DynamicImage) -> Vec> { //todo: figure out braille symbols - vec!["not implemented".to_owned()] + vec![vec![Ascii::new(0, 0, 0, 0)]] } -pub fn to_custom_ascii(char_map: String, image: DynamicImage) -> Vec { +pub fn to_custom_ascii(char_map: String, image: DynamicImage) -> Vec> { if char_map.is_empty() { error!("Custom map can not be empty!"); exit(1); } to_ascii(char_map, image) -} - -//todo: replace Vec with a custom struct containing rgb information as well as character (for coloured output) \ No newline at end of file +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index ace5433..5e3f4bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,11 +3,13 @@ use crate::ascii_manipulation::*; use crate::cli::Cli; use crate::output::*; use crate::image_manipulation::*; +use crate::model_rgb_ascii::Ascii; mod cli; mod image_manipulation; mod ascii_manipulation; mod output; +mod model_rgb_ascii; //todo: general /* Documentation @@ -28,7 +30,7 @@ fn main() { img = resize_image(img, cli.full, cli.width); //converting to ASCII - let out: Vec; + let out: Vec>; if cli.braille { out = to_braille_ascii(img); } else if cli.complex { @@ -45,11 +47,4 @@ fn main() { } else { print_terminal(out, cli.colour); } - - //todo: - /* function that converts image to braille (if -b) - * something about printing in colour - * output to file - */ - } diff --git a/src/model_rgb_ascii.rs b/src/model_rgb_ascii.rs new file mode 100644 index 0000000..8170473 --- /dev/null +++ b/src/model_rgb_ascii.rs @@ -0,0 +1,29 @@ + +pub struct RGB { + pub r: u8, + pub g: u8, + pub b: u8, +} + +pub struct Ascii { + pub char: char, + pub rgb: RGB, +} + + +impl Ascii { + fn _new(char: char, r: u8, g: u8, b: u8) -> Self { + Ascii { + char, + rgb: RGB { + r, + g, + b, + }, + } + } + + pub fn new(ch: u8, r: u8, g: u8, b: u8) -> Self { + Self::_new(char::from(ch), r, g, b) + } +} \ No newline at end of file diff --git a/src/output.rs b/src/output.rs index 49d305f..21fa397 100644 --- a/src/output.rs +++ b/src/output.rs @@ -1,11 +1,16 @@ use std::path::PathBuf; +use crate::model_rgb_ascii::Ascii; -pub fn print_terminal(ascii: Vec, in_colour: bool) { - for line in ascii { - println!("{}", line); +//todo: take into consideration the in_colour flag +pub fn print_terminal(art: Vec>, in_colour: bool) { + for line in art { + for ascii in line { + print!("{}", ascii.char) + } + println!(); } } -pub fn print_file(ascii: Vec, out: PathBuf) { +pub fn print_file(ascii: Vec>, out: PathBuf) { //todo: this } \ No newline at end of file