small change to image resizing
This commit is contained in:
parent
5f01bfa485
commit
7d33d72401
|
|
@ -15,7 +15,8 @@ fn to_ascii(char_map: String, image: DynamicImage) -> Vec<Vec<Ascii>> {
|
|||
let mut out: Vec<Vec<Ascii>> = 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 {
|
||||
|
|
|
|||
|
|
@ -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<u32>) -> 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);
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
@ -10,7 +10,7 @@ pub fn print_terminal(art: Vec<Vec<Ascii>>, 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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue