rebased to use custom struct over String

This commit is contained in:
Djairo Hougee 2023-02-08 17:24:17 +01:00
parent b00ef93c1c
commit 135274c9c5
4 changed files with 53 additions and 26 deletions

View File

@ -1,50 +1,48 @@
use std::process::exit; use std::process::exit;
use image::{DynamicImage, GenericImageView, Rgba}; use image::{DynamicImage, GenericImageView, Rgba};
use log::error; 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>)) -> u8 { fn get_color(pixel: (u32, u32, Rgba<u8>)) -> u8 {
let vec = pixel.2.0; let vec = pixel.2.0;
//luminosity method of getting lightness //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 (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<String> { fn to_ascii(char_map: String, image: DynamicImage) -> Vec<Vec<Ascii>> {
let l = char_map.len() as f32; let l = char_map.len() as f32;
let mut str = String::new(); let mut str: Vec<Ascii> = Vec::new();
let mut out: Vec<String> = 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(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 { if pixel.0 == image.width()-1 {
out.push(str); out.push(str);
str = String::new(); str = Vec::new();
} }
} }
out out
} }
pub fn to_simple_ascii(image: DynamicImage) -> Vec<String> { pub fn to_simple_ascii(image: DynamicImage) -> Vec<Vec<Ascii>> {
to_ascii(" .:-=+*#%@".to_owned(), image) to_ascii(" .:-=+*#%@".to_owned(), image)
} }
pub fn to_complex_ascii(image: DynamicImage) -> Vec<String> { pub fn to_complex_ascii(image: DynamicImage) -> Vec<Vec<Ascii>> {
to_ascii(" .'`^\",:;Il!i><~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$".to_owned(), image) to_ascii(" .'`^\",:;Il!i><~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$".to_owned(), image)
} }
pub fn to_braille_ascii(image: DynamicImage) -> Vec<String> { pub fn to_braille_ascii(image: DynamicImage) -> Vec<Vec<Ascii>> {
//todo: figure out braille symbols //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<String> { pub fn to_custom_ascii(char_map: String, image: DynamicImage) -> Vec<Vec<Ascii>> {
if char_map.is_empty() { if char_map.is_empty() {
error!("Custom map can not be empty!"); error!("Custom map can not be empty!");
exit(1); exit(1);
} }
to_ascii(char_map, image) to_ascii(char_map, image)
} }
//todo: replace Vec<String> with a custom struct containing rgb information as well as character (for coloured output)

View File

@ -3,11 +3,13 @@ use crate::ascii_manipulation::*;
use crate::cli::Cli; use crate::cli::Cli;
use crate::output::*; use crate::output::*;
use crate::image_manipulation::*; use crate::image_manipulation::*;
use crate::model_rgb_ascii::Ascii;
mod cli; mod cli;
mod image_manipulation; mod image_manipulation;
mod ascii_manipulation; mod ascii_manipulation;
mod output; mod output;
mod model_rgb_ascii;
//todo: general //todo: general
/* Documentation /* Documentation
@ -28,7 +30,7 @@ fn main() {
img = resize_image(img, cli.full, cli.width); img = resize_image(img, cli.full, cli.width);
//converting to ASCII //converting to ASCII
let out: Vec<String>; let out: Vec<Vec<Ascii>>;
if cli.braille { if cli.braille {
out = to_braille_ascii(img); out = to_braille_ascii(img);
} else if cli.complex { } else if cli.complex {
@ -45,11 +47,4 @@ fn main() {
} else { } else {
print_terminal(out, cli.colour); print_terminal(out, cli.colour);
} }
//todo:
/* function that converts image to braille (if -b)
* something about printing in colour
* output to file
*/
} }

29
src/model_rgb_ascii.rs Normal file
View File

@ -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)
}
}

View File

@ -1,11 +1,16 @@
use std::path::PathBuf; use std::path::PathBuf;
use crate::model_rgb_ascii::Ascii;
pub fn print_terminal(ascii: Vec<String>, in_colour: bool) { //todo: take into consideration the in_colour flag
for line in ascii { pub fn print_terminal(art: Vec<Vec<Ascii>>, in_colour: bool) {
println!("{}", line); for line in art {
for ascii in line {
print!("{}", ascii.char)
}
println!();
} }
} }
pub fn print_file(ascii: Vec<String>, out: PathBuf) { pub fn print_file(ascii: Vec<Vec<Ascii>>, out: PathBuf) {
//todo: this //todo: this
} }