rebased to use custom struct over String

This commit is contained in:
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 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>)) -> 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<String> {
fn to_ascii(char_map: String, image: DynamicImage) -> Vec<Vec<Ascii>> {
let l = char_map.len() as f32;
let mut str = String::new();
let mut out: Vec<String> = Vec::new();
let mut str: Vec<Ascii> = Vec::new();
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)
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<String> {
pub fn to_simple_ascii(image: DynamicImage) -> Vec<Vec<Ascii>> {
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)
}
pub fn to_braille_ascii(image: DynamicImage) -> Vec<String> {
pub fn to_braille_ascii(image: DynamicImage) -> Vec<Vec<Ascii>> {
//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() {
error!("Custom map can not be empty!");
exit(1);
}
to_ascii(char_map, image)
}
//todo: replace Vec<String> with a custom struct containing rgb information as well as character (for coloured output)
}