ft: added centering flag

This commit is contained in:
2026-04-12 11:47:11 +02:00
parent 6693d8917c
commit 1c56b07915
7 changed files with 151 additions and 65 deletions

0
src/a
View File

View File

@@ -1,8 +1,9 @@
//! This module contains all functions related to converting images to ASCII. //! This module contains all functions related to converting images to ASCII.
use std::process::exit; use crate::image_manipulation::get_terminal_size;
use crate::model_rgb_ascii::Ascii;
use image::{DynamicImage, GenericImageView, Rgba}; use image::{DynamicImage, GenericImageView, Rgba};
use log::error; use log::error;
use crate::model_rgb_ascii::Ascii; use std::process::exit;
/// This constant is used to calculate braille values. /// This constant is used to calculate braille values.
const BRAILLE_TABLE: [[u8; 2]; 4] = [[1u8, 8u8], [2u8, 16u8], [4u8, 32u8], [64u8, 128u8]]; const BRAILLE_TABLE: [[u8; 2]; 4] = [[1u8, 8u8], [2u8, 16u8], [4u8, 32u8], [64u8, 128u8]];
@@ -22,6 +23,25 @@ fn get_color(pixel: Rgba<u8>) -> u8 {
(pixel[0] as f32 * 0.3 + pixel[1] as f32 * 0.59 + pixel[2] as f32 * 0.11) as u8 (pixel[0] as f32 * 0.3 + pixel[1] as f32 * 0.59 + pixel[2] as f32 * 0.11) as u8
} }
/// This function returns the empty space on either side of the ascii, relative to the terminal
/// width.
///
/// arguments:
/// ascii: &Vec<Vec<Ascii>> - ascii image to calculate margins for
///
/// returns:
/// u32 representing the size of one margin (i.e. the empty space that should be prefixed to center
/// the ascii)
pub fn calc_margin(ascii: &Vec<Vec<Ascii>>) -> u32 {
let (max_w, _) = get_terminal_size();
let w = ascii[0].len() as u32; // Assume all lines have same length.
if w < max_w {
return (max_w - w) / 2 as u32;
} else {
return 0;
}
}
/// This function converts a given image into ASCII. /// This function converts a given image into ASCII.
/// ///
/// arguments: /// arguments:
@@ -37,11 +57,11 @@ fn to_ascii(char_map: String, image: DynamicImage) -> Vec<Vec<Ascii>> {
for pixel in image.pixels() { for pixel in image.pixels() {
let mut ascii = Ascii::new(0, pixel.2[0], pixel.2[1], pixel.2[2]); let mut ascii = Ascii::new(0, pixel.2[0], pixel.2[1], pixel.2[2]);
let ch = char_map.as_bytes()[((ascii.col_depth as f32-1.0)/255f32 * l) as usize]; let ch = char_map.as_bytes()[((ascii.col_depth as f32 - 1.0) / 255f32 * l) as usize];
ascii.char = char::from(ch); ascii.char = char::from(ch);
str.push(ascii); str.push(ascii);
if pixel.0 == image.width()-1 { if pixel.0 == image.width() - 1 {
out.push(str); out.push(str);
str = Vec::new(); str = Vec::new();
} }
@@ -72,7 +92,10 @@ pub fn to_simple_ascii(image: DynamicImage) -> Vec<Vec<Ascii>> {
/// returns: /// returns:
/// Vec<Vec<Ascii>> containing the converted image /// Vec<Vec<Ascii>> containing the converted image
pub fn to_complex_ascii(image: DynamicImage) -> Vec<Vec<Ascii>> { 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,
)
} }
/// This wrapper function converts an image to custom ASCII. /// This wrapper function converts an image to custom ASCII.
@@ -109,17 +132,22 @@ pub fn to_braille_ascii(image: DynamicImage, threshold: u8) -> Vec<Vec<Ascii>> {
let mut braille_value: u32 = 10240; let mut braille_value: u32 = 10240;
for nx in 0..2 { for nx in 0..2 {
for ny in 0..4 { for ny in 0..4 {
let pixel = image.get_pixel(x+nx, y+ny); let pixel = image.get_pixel(x + nx, y + ny);
r = r+pixel[0] as u16; r = r + pixel[0] as u16;
g = g+pixel[1] as u16; g = g + pixel[1] as u16;
b = b+pixel[2] as u16; b = b + pixel[2] as u16;
if get_color(pixel) >= threshold { if get_color(pixel) >= threshold {
braille_value += BRAILLE_TABLE[ny as usize][nx as usize] as u32; braille_value += BRAILLE_TABLE[ny as usize][nx as usize] as u32;
} }
} }
} }
str.push(Ascii::new_with_char(char::from_u32(braille_value).unwrap(), (r/8) as u8, (g/8) as u8, (b/8) as u8)); str.push(Ascii::new_with_char(
} char::from_u32(braille_value).unwrap(),
(r / 8) as u8,
(g / 8) as u8,
(b / 8) as u8,
));
}
out.push(str); out.push(str);
str = Vec::new(); str = Vec::new();
} }

View File

@@ -1,17 +1,22 @@
//! This module contains all functions related to the CLI part of the program. //! This module contains all functions related to the CLI part of the program.
//! //!
//! It manages and validates flags, mostly. //! It manages and validates flags, mostly.
use clap::Parser;
use log::{debug, error};
use std::ffi::OsString; use std::ffi::OsString;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::exit; use std::process::exit;
use clap::Parser;
use log::{debug, error};
/// Convert an image to ASCII art and print it to the terminal. /// Convert an image to ASCII art and print it to the terminal.
/// ///
/// Configuration is done using flags. /// Configuration is done using flags.
#[derive(Parser)] #[derive(Parser)]
pub struct Cli { pub struct Cli {
/// Center the output in terminal.
///
/// Incompatible with '--output'
#[arg(short = 'm', long)]
pub center: bool,
/// Use a larger range of characters /// Use a larger range of characters
/// ///
/// Takes priority over '--map' /// Takes priority over '--map'
@@ -30,7 +35,7 @@ pub struct Cli {
/// Display ASCII art in grayscale /// Display ASCII art in grayscale
/// ///
/// incompatible with '--color' /// incompatible with '--color'
#[arg(short= 'g', long)] #[arg(short = 'g', long)]
pub grayscale: bool, pub grayscale: bool,
/// Use braille characters instead of ASCII /// Use braille characters instead of ASCII
/// ///
@@ -66,12 +71,12 @@ pub struct Cli {
pub output: Option<PathBuf>, pub output: Option<PathBuf>,
} }
impl Cli { impl Cli {
/// This function prints all parts of Cli. /// This function prints all parts of Cli.
/// ///
/// used for debugging. /// used for debugging.
pub fn debug_print(&self) { pub fn debug_print(&self) {
debug!("center: {}", self.center);
debug!("complex: {}", self.complex); debug!("complex: {}", self.complex);
debug!("colour: {}", self.colour); debug!("colour: {}", self.colour);
debug!("background: {}", self.background); debug!("background: {}", self.background);
@@ -128,7 +133,9 @@ impl Cli {
/// This function initializes logging (dependent on cli.debug flag) /// This function initializes logging (dependent on cli.debug flag)
pub fn init(&self) { pub fn init(&self) {
if self.debug {std::env::set_var("RUST_LOG", "trace")} if self.debug {
std::env::set_var("RUST_LOG", "trace")
}
env_logger::init(); env_logger::init();
self.debug_print(); self.debug_print();
} }
@@ -144,14 +151,14 @@ impl Cli {
fn is_image(path: &PathBuf) -> bool { fn is_image(path: &PathBuf) -> bool {
let ext = path.extension(); let ext = path.extension();
match ext { match ext {
Some(ext) => { Some(ext) => vec![
vec![OsString::from("png"), OsString::from("png"),
OsString::from("jpg"), OsString::from("jpg"),
OsString::from("jpeg"), OsString::from("jpeg"),
OsString::from("webp")] OsString::from("webp"),
.contains(&ext.to_ascii_lowercase()) ]
} .contains(&ext.to_ascii_lowercase()),
None => false None => false,
} }
} }

View File

@@ -1,21 +1,19 @@
//! This module contains all functions related to image manipulation. //! This module contains all functions related to image manipulation.
use crossterm::terminal; use crossterm::terminal;
use log::{debug, error};
use std::process::exit;
use std::path::PathBuf;
use image::DynamicImage;
use image::imageops::FilterType; use image::imageops::FilterType;
use image::DynamicImage;
use log::{debug, error};
use std::path::PathBuf;
use std::process::exit;
/// This function returns the (width, length) of stdout /// This function returns the (width, length) of stdout
/// ///
/// returns: /// returns:
/// (u32, u32) representing the (width, length) of stdout /// (u32, u32) representing the (width, length) of stdout
fn get_terminal_size() -> (u32, u32) { pub(crate) fn get_terminal_size() -> (u32, u32) {
let size = terminal::size(); let size = terminal::size();
match size { match size {
Ok(size) => { Ok(size) => (size.0 as u32, size.1 as u32),
(size.0 as u32, size.1 as u32)
}
Err(e) => { Err(e) => {
error!("Failed to get terminal size: {}", e.to_string()); error!("Failed to get terminal size: {}", e.to_string());
exit(1); exit(1);
@@ -35,22 +33,27 @@ fn get_terminal_size() -> (u32, u32) {
/// ///
/// returns: /// returns:
/// DynamicImage, resized /// DynamicImage, resized
pub fn resize_image(img: DynamicImage, full: bool, braille: bool, opt_w: Option<u32>) -> DynamicImage { pub fn resize_image(
img: DynamicImage,
full: bool,
braille: bool,
opt_w: Option<u32>,
) -> DynamicImage {
//compiler complains that these v values are never read; this is true, however they are necessary because otherwise the program simply will not compile. //compiler complains that these v values are never read; this is true, however they are necessary because otherwise the program simply will not compile.
let (mut w, mut h) = (1,1); let (mut w, mut h) = (1, 1);
let (max_w, max_h) = get_terminal_size(); let (max_w, max_h) = get_terminal_size();
if full { if full {
w = max_w-1; 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 {
h = max_h-1; h = max_h - 1;
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;
if w >= max_w { if w >= max_w {
w = max_w-1; 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;
} }
} }

View File

@@ -1,5 +1,4 @@
//! Driver module. //! Driver module.
#![feature(file_create_new)]
use crate::ascii_manipulation::*; use crate::ascii_manipulation::*;
use crate::cli::Cli; use crate::cli::Cli;
@@ -45,10 +44,16 @@ fn main() {
if let Some(output) = cli.output { if let Some(output) = cli.output {
print_file(out, output); print_file(out, output);
} else { } else {
if cli.background && (cli.colour || cli.grayscale) { let prefix;
print_terminal_background(out, cli.colour, cli.grayscale); if cli.center {
prefix = calc_margin(&out);
} else { } else {
print_terminal(out, cli.colour, cli.grayscale); prefix = 0;
}
if cli.background && (cli.colour || cli.grayscale) {
print_terminal_background(out, cli.colour, cli.grayscale, prefix);
} else {
print_terminal(out, cli.colour, cli.grayscale, prefix);
} }
} }
} }

View File

@@ -1,12 +1,12 @@
//! This module contains functions related to output. //! This module contains functions related to output.
use crate::model_rgb_ascii::Ascii;
use crossterm::execute;
use crossterm::style::{Color, Print, ResetColor, SetBackgroundColor, SetForegroundColor};
use log::error;
use std::fs::File; use std::fs::File;
use std::io::{stdout, Write}; use std::io::{stdout, Write};
use std::path::PathBuf; use std::path::PathBuf;
use std::process::exit; use std::process::exit;
use crossterm::execute;
use crossterm::style::{Color, SetForegroundColor, SetBackgroundColor, Print, ResetColor};
use log::error;
use crate::model_rgb_ascii::Ascii;
/// This function prints ASCII art to stdout. /// This function prints ASCII art to stdout.
/// ///
@@ -14,19 +14,34 @@ use crate::model_rgb_ascii::Ascii;
/// art: Vec<Vec<Ascii>> - vector of vectors representing the art /// art: Vec<Vec<Ascii>> - vector of vectors representing the art
/// in_colour: bool - whether to print in colour or as hard white /// in_colour: bool - whether to print in colour or as hard white
/// grayscale: bool - whether to print grayscale /// grayscale: bool - whether to print grayscale
pub fn print_terminal(art: Vec<Vec<Ascii>>, in_colour: bool, grayscale: bool) { /// prefix: u32 - number of empty spaces to prepend to each line
pub fn print_terminal(art: Vec<Vec<Ascii>>, in_colour: bool, grayscale: bool, prefix: u32) {
let prefix_str = " ".repeat(prefix as usize);
for line in art { for line in art {
let _ = execute!(stdout(), Print(prefix_str.clone()));
for ascii in line { for ascii in line {
if in_colour { if in_colour {
let _ = execute!(stdout(), let _ = execute!(
SetForegroundColor(Color::Rgb {r: ascii.rgb[0], g: ascii.rgb[1], b: ascii.rgb[2]}), stdout(),
SetForegroundColor(Color::Rgb {
r: ascii.rgb[0],
g: ascii.rgb[1],
b: ascii.rgb[2]
}),
Print(ascii.char), Print(ascii.char),
ResetColor); ResetColor
);
} else if grayscale { } else if grayscale {
let _ = execute!(stdout(), let _ = execute!(
SetForegroundColor(Color::Rgb {r: ascii.col_depth, g: ascii.col_depth, b: ascii.col_depth}), stdout(),
SetForegroundColor(Color::Rgb {
r: ascii.col_depth,
g: ascii.col_depth,
b: ascii.col_depth
}),
Print(ascii.char), Print(ascii.char),
ResetColor); ResetColor
);
} else { } else {
print!("{}", ascii.char); print!("{}", ascii.char);
} }
@@ -41,21 +56,49 @@ pub fn print_terminal(art: Vec<Vec<Ascii>>, in_colour: bool, grayscale: bool) {
/// art: Vec<Vec<Ascii>> - vector of vectors representing the art /// art: Vec<Vec<Ascii>> - vector of vectors representing the art
/// in_colour: bool - whether to print in colour /// in_colour: bool - whether to print in colour
/// grayscale: bool - whether to print grayscale /// grayscale: bool - whether to print grayscale
pub fn print_terminal_background(art: Vec<Vec<Ascii>>, in_colour: bool, grayscale: bool) { /// prefix: u32 - number of empty spaces to prepend to each line
pub fn print_terminal_background(
art: Vec<Vec<Ascii>>,
in_colour: bool,
grayscale: bool,
prefix: u32,
) {
let prefix_str = " ".repeat(prefix as usize);
for line in art { for line in art {
let _ = execute!(stdout(), Print(prefix_str.clone()));
for ascii in line { for ascii in line {
if in_colour { if in_colour {
let _ = execute!(stdout(), let _ = execute!(
SetBackgroundColor(Color::Rgb {r: ascii.rgb[0], g: ascii.rgb[1], b: ascii.rgb[2]}), stdout(),
SetForegroundColor(Color::Rgb {r: 255-ascii.rgb[0], g: 255-ascii.rgb[1], b: 255-ascii.rgb[2]}), SetBackgroundColor(Color::Rgb {
r: ascii.rgb[0],
g: ascii.rgb[1],
b: ascii.rgb[2]
}),
SetForegroundColor(Color::Rgb {
r: 255 - ascii.rgb[0],
g: 255 - ascii.rgb[1],
b: 255 - ascii.rgb[2]
}),
Print(ascii.char), Print(ascii.char),
ResetColor); ResetColor
);
} else if grayscale { } else if grayscale {
let _ = execute!(stdout(), let _ = execute!(
SetBackgroundColor(Color::Rgb {r: ascii.col_depth, g: ascii.col_depth, b: ascii.col_depth}), stdout(),
SetForegroundColor(Color::Rgb {r: 255-ascii.col_depth, g: 255-ascii.col_depth, b: 255-ascii.col_depth}), SetBackgroundColor(Color::Rgb {
r: ascii.col_depth,
g: ascii.col_depth,
b: ascii.col_depth
}),
SetForegroundColor(Color::Rgb {
r: 255 - ascii.col_depth,
g: 255 - ascii.col_depth,
b: 255 - ascii.col_depth
}),
Print(ascii.char), Print(ascii.char),
ResetColor); ResetColor
);
} else { } else {
error!("This should be unreachable!"); error!("This should be unreachable!");
exit(1); exit(1);