small change to image resizing

This commit is contained in:
Djairo Hougee 2023-02-08 19:08:59 +01:00
parent 5f01bfa485
commit 7d33d72401
5 changed files with 16 additions and 32 deletions

View File

@ -15,7 +15,8 @@ fn to_ascii(char_map: String, image: DynamicImage) -> Vec<Vec<Ascii>> {
let mut out: Vec<Vec<Ascii>> = Vec::new(); let mut out: Vec<Vec<Ascii>> = Vec::new();
for pixel in image.pixels() { 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])); str.push(Ascii::new(ch, pixel.2[0], pixel.2[1], pixel.2[2]));
if pixel.0 == image.width()-1 { if pixel.0 == image.width()-1 {

View File

@ -1,5 +1,5 @@
use termion::terminal_size; use termion::terminal_size;
use log::{debug, error, trace}; use log::{debug, error};
use std::process::exit; use std::process::exit;
use std::path::PathBuf; use std::path::PathBuf;
use image::DynamicImage; use image::DynamicImage;
@ -20,20 +20,20 @@ fn get_terminal_size() -> (u32, u32) {
pub fn resize_image(img: DynamicImage, full: bool, opt_w: Option<u32>) -> DynamicImage { pub fn resize_image(img: DynamicImage, full: bool, opt_w: Option<u32>) -> DynamicImage {
let (mut w, mut h) = (1,1); let (mut w, mut h) = (1,1);
let (max_w, max_h) = get_terminal_size();
if full { 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; h = (img.height() as f32 * w as f32 / img.width() as f32 * 0.5) as u32;
} else if let Some(act_w) = opt_w { } else if let Some(act_w) = opt_w {
w = act_w; w = act_w;
h = (img.height() as f32 * w as f32 / img.width() as f32 * 0.5) as u32; h = (img.height() as f32 * w as f32 / img.width() as f32 * 0.5) as u32;
} else { } else {
let (max_w, max_h) = get_terminal_size(); h = max_h-1;
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; w = (img.width() as f32 * h as f32 / img.height() as f32 * 2.0) as u32;
} else {
w = max_w; if w >= max_w {
h = (img.height() as f32 * w as f32 / img.height() as f32 * 0.5) as u32; 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); debug!("Resizing image to (w|h): {} | {}", w, h);

View File

@ -16,7 +16,6 @@ mod model_rgb_ascii;
//todo: general //todo: general
/* Documentation /* Documentation
* Readme * Readme
* https://stackoverflow.com/questions/69981449/how-do-i-print-colored-text-to-the-terminal-in-rust
*/ */
fn main() { fn main() {

View File

@ -1,29 +1,13 @@
pub struct RGB {
pub r: u8,
pub g: u8,
pub b: u8,
}
pub struct Ascii { pub struct Ascii {
pub char: char, pub char: char,
pub rgb: RGB, pub rgb: [u8; 3],
} }
impl Ascii { 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 { pub fn new(ch: u8, r: u8, g: u8, b: u8) -> Self {
Self::_new(char::from(ch), r, g, b) Ascii {
char: char::from(ch),
rgb: [r, g, b],
}
} }
} }

View File

@ -10,7 +10,7 @@ pub fn print_terminal(art: Vec<Vec<Ascii>>, in_colour: bool) {
for line in art { for line in art {
for ascii in line { for ascii in line {
if in_colour { 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 { } else {
print!("{}", ascii.char); print!("{}", ascii.char);
} }