diff --git a/src/ascii_manipulation.rs b/src/ascii_manipulation.rs index 515bc05..6cf4cbf 100644 --- a/src/ascii_manipulation.rs +++ b/src/ascii_manipulation.rs @@ -15,7 +15,8 @@ fn to_ascii(char_map: String, image: DynamicImage) -> Vec> { 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) + 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(Ascii::new(ch, pixel.2[0], pixel.2[1], pixel.2[2])); if pixel.0 == image.width()-1 { diff --git a/src/image_manipulation.rs b/src/image_manipulation.rs index 6b20531..627e40f 100644 --- a/src/image_manipulation.rs +++ b/src/image_manipulation.rs @@ -1,5 +1,5 @@ use termion::terminal_size; -use log::{debug, error, trace}; +use log::{debug, error}; use std::process::exit; use std::path::PathBuf; use image::DynamicImage; @@ -20,20 +20,20 @@ fn get_terminal_size() -> (u32, u32) { pub fn resize_image(img: DynamicImage, full: bool, opt_w: Option) -> DynamicImage { let (mut w, mut h) = (1,1); + let (max_w, max_h) = get_terminal_size(); if full { - w = get_terminal_size().0; + w = max_w-1; h = (img.height() as f32 * w as f32 / img.width() as f32 * 0.5) as u32; } else if let Some(act_w) = opt_w { w = act_w; h = (img.height() as f32 * w as f32 / img.width() as f32 * 0.5) as u32; } else { - let (max_w, max_h) = get_terminal_size(); - if max_h*max_w/2 > img.height()*img.width() { - h = max_h; - w = (img.width() as f32 * h as f32 / img.height() as f32 * 2.0) as u32; - } else { - w = max_w; - h = (img.height() as f32 * w as f32 / img.height() as f32 * 0.5) as u32; + h = max_h-1; + w = (img.width() as f32 * h as f32 / img.height() as f32 * 2.0) as u32; + + if w >= max_w { + w = max_w-1; + h = (img.height() as f32 * w as f32 / img.width() as f32 * 0.5) as u32; } } debug!("Resizing image to (w|h): {} | {}", w, h); diff --git a/src/main.rs b/src/main.rs index 5a4dfc9..f373371 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,6 @@ mod model_rgb_ascii; //todo: general /* Documentation * Readme - * https://stackoverflow.com/questions/69981449/how-do-i-print-colored-text-to-the-terminal-in-rust */ fn main() { diff --git a/src/model_rgb_ascii.rs b/src/model_rgb_ascii.rs index 8170473..a2546bc 100644 --- a/src/model_rgb_ascii.rs +++ b/src/model_rgb_ascii.rs @@ -1,29 +1,13 @@ - -pub struct RGB { - pub r: u8, - pub g: u8, - pub b: u8, -} - pub struct Ascii { pub char: char, - pub rgb: RGB, + pub rgb: [u8; 3], } - impl Ascii { - fn _new(char: char, r: u8, g: u8, b: u8) -> Self { + pub fn new(ch: u8, r: u8, g: u8, b: u8) -> Self { Ascii { - char, - rgb: RGB { - r, - g, - b, - }, + char: char::from(ch), + 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 29bde72..b15b46d 100644 --- a/src/output.rs +++ b/src/output.rs @@ -10,7 +10,7 @@ pub fn print_terminal(art: Vec>, in_colour: bool) { for line in art { for ascii in line { if in_colour { - print!("{}{}", color::Fg(color::Rgb(ascii.rgb.r, ascii.rgb.g, ascii.rgb.b)), ascii.char); + print!("{}{}", color::Fg(color::Rgb(ascii.rgb[0], ascii.rgb[1], ascii.rgb[2])), ascii.char); } else { print!("{}", ascii.char); }