Compare commits

..
10 changed files with 68 additions and 154 deletions

No files matched your search

Executable → Regular
View File
File mode changed.
Executable → Regular
+1 -1
View File
@@ -11,5 +11,5 @@ dotenvy = "0.15.*"
log = "0.4.*"
clap = { version = "4.1.*", features = ["derive"] }
image = { version = "0.24.*", features = ["webp-encoder"] }
#termion = "2.0.*"
crossterm = "0.26.*"
Executable → Regular
View File
File mode changed.
Executable → Regular
View File
File mode changed.
+12 -40
View File
@@ -1,9 +1,8 @@
//! This module contains all functions related to converting images to ASCII.
use crate::image_manipulation::get_terminal_size;
use crate::model_rgb_ascii::Ascii;
use std::process::exit;
use image::{DynamicImage, GenericImageView, Rgba};
use log::error;
use std::process::exit;
use crate::model_rgb_ascii::Ascii;
/// This constant is used to calculate braille values.
const BRAILLE_TABLE: [[u8; 2]; 4] = [[1u8, 8u8], [2u8, 16u8], [4u8, 32u8], [64u8, 128u8]];
@@ -23,25 +22,6 @@ 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
}
/// 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.
///
/// arguments:
@@ -57,11 +37,11 @@ fn to_ascii(char_map: String, image: DynamicImage) -> Vec<Vec<Ascii>> {
for pixel in image.pixels() {
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);
str.push(ascii);
if pixel.0 == image.width() - 1 {
if pixel.0 == image.width()-1 {
out.push(str);
str = Vec::new();
}
@@ -92,10 +72,7 @@ pub fn to_simple_ascii(image: DynamicImage) -> Vec<Vec<Ascii>> {
/// returns:
/// Vec<Vec<Ascii>> containing the converted image
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.
@@ -132,24 +109,19 @@ pub fn to_braille_ascii(image: DynamicImage, threshold: u8) -> Vec<Vec<Ascii>> {
let mut braille_value: u32 = 10240;
for nx in 0..2 {
for ny in 0..4 {
let pixel = image.get_pixel(x + nx, y + ny);
r = r + pixel[0] as u16;
g = g + pixel[1] as u16;
b = b + pixel[2] as u16;
let pixel = image.get_pixel(x+nx, y+ny);
r = r+pixel[0] as u16;
g = g+pixel[1] as u16;
b = b+pixel[2] as u16;
if get_color(pixel) >= threshold {
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);
str = Vec::new();
}
out
}
}
+14 -21
View File
@@ -1,22 +1,17 @@
//! This module contains all functions related to the CLI part of the program.
//!
//! It manages and validates flags, mostly.
use clap::Parser;
use log::{debug, error};
use std::ffi::OsString;
use std::path::PathBuf;
use std::process::exit;
use clap::Parser;
use log::{debug, error};
/// Convert an image to ASCII art and print it to the terminal.
///
/// Configuration is done using flags.
#[derive(Parser)]
pub struct Cli {
/// Center the output in terminal.
///
/// Incompatible with '--output'
#[arg(short = 'm', long)]
pub center: bool,
/// Use a larger range of characters
///
/// Takes priority over '--map'
@@ -35,7 +30,7 @@ pub struct Cli {
/// Display ASCII art in grayscale
///
/// incompatible with '--color'
#[arg(short = 'g', long)]
#[arg(short= 'g', long)]
pub grayscale: bool,
/// Use braille characters instead of ASCII
///
@@ -71,12 +66,12 @@ pub struct Cli {
pub output: Option<PathBuf>,
}
impl Cli {
/// This function prints all parts of Cli.
///
/// used for debugging.
pub fn debug_print(&self) {
debug!("center: {}", self.center);
debug!("complex: {}", self.complex);
debug!("colour: {}", self.colour);
debug!("background: {}", self.background);
@@ -133,9 +128,7 @@ impl Cli {
/// This function initializes logging (dependent on cli.debug flag)
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();
self.debug_print();
}
@@ -151,14 +144,14 @@ impl Cli {
fn is_image(path: &PathBuf) -> bool {
let ext = path.extension();
match ext {
Some(ext) => vec![
OsString::from("png"),
OsString::from("jpg"),
OsString::from("jpeg"),
OsString::from("webp"),
]
.contains(&ext.to_ascii_lowercase()),
None => false,
Some(ext) => {
vec![OsString::from("png"),
OsString::from("jpg"),
OsString::from("jpeg"),
OsString::from("webp")]
.contains(&ext.to_ascii_lowercase())
}
None => false
}
}
@@ -177,4 +170,4 @@ fn file_exists(path: &PathBuf) -> bool {
false
}
}
}
}
+12 -15
View File
@@ -1,19 +1,21 @@
//! This module contains all functions related to image manipulation.
use crossterm::terminal;
use image::imageops::FilterType;
use image::DynamicImage;
use log::{debug, error};
use std::path::PathBuf;
use std::process::exit;
use std::path::PathBuf;
use image::DynamicImage;
use image::imageops::FilterType;
/// This function returns the (width, length) of stdout
///
/// returns:
/// (u32, u32) representing the (width, length) of stdout
pub(crate) fn get_terminal_size() -> (u32, u32) {
fn get_terminal_size() -> (u32, u32) {
let size = terminal::size();
match size {
Ok(size) => (size.0 as u32, size.1 as u32),
Ok(size) => {
(size.0 as u32, size.1 as u32)
}
Err(e) => {
error!("Failed to get terminal size: {}", e.to_string());
exit(1);
@@ -33,27 +35,22 @@ pub(crate) fn get_terminal_size() -> (u32, u32) {
///
/// returns:
/// 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.
let (mut w, mut h) = (1, 1);
let (mut w, mut h) = (1,1);
let (max_w, max_h) = get_terminal_size();
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;
} 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 {
h = max_h - 1;
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;
w = max_w-1;
h = (img.height() as f32 * w as f32 / img.width() as f32 * 0.5) as u32;
}
}
+7 -12
View File
@@ -1,17 +1,18 @@
//! Driver module.
#![feature(file_create_new)]
use clap::Parser;
use crate::ascii_manipulation::*;
use crate::cli::Cli;
use crate::output::*;
use crate::image_manipulation::*;
use crate::model_rgb_ascii::Ascii;
use crate::output::*;
use clap::Parser;
mod ascii_manipulation;
mod cli;
mod image_manipulation;
mod model_rgb_ascii;
mod ascii_manipulation;
mod output;
mod model_rgb_ascii;
/// This is the main function.
///
@@ -44,16 +45,10 @@ fn main() {
if let Some(output) = cli.output {
print_file(out, output);
} else {
let prefix;
if cli.center {
prefix = calc_margin(&out);
} else {
prefix = 0;
}
if cli.background && (cli.colour || cli.grayscale) {
print_terminal_background(out, cli.colour, cli.grayscale, prefix);
print_terminal_background(out, cli.colour, cli.grayscale);
} else {
print_terminal(out, cli.colour, cli.grayscale, prefix);
print_terminal(out, cli.colour, cli.grayscale);
}
}
}
+1 -1
View File
@@ -48,4 +48,4 @@ impl Ascii {
col_depth: (r as f32 * 0.3 + g as f32 * 0.59 + b as f32 * 0.11) as u8,
}
}
}
}
+21 -64
View File
@@ -1,12 +1,12 @@
//! 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::io::{stdout, Write};
use std::path::PathBuf;
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.
///
@@ -14,34 +14,19 @@ use std::process::exit;
/// art: Vec<Vec<Ascii>> - vector of vectors representing the art
/// in_colour: bool - whether to print in colour or as hard white
/// grayscale: bool - whether to print grayscale
/// 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);
pub fn print_terminal(art: Vec<Vec<Ascii>>, in_colour: bool, grayscale: bool) {
for line in art {
let _ = execute!(stdout(), Print(prefix_str.clone()));
for ascii in line {
if in_colour {
let _ = execute!(
stdout(),
SetForegroundColor(Color::Rgb {
r: ascii.rgb[0],
g: ascii.rgb[1],
b: ascii.rgb[2]
}),
let _ = execute!(stdout(),
SetForegroundColor(Color::Rgb {r: ascii.rgb[0], g: ascii.rgb[1], b: ascii.rgb[2]}),
Print(ascii.char),
ResetColor
);
ResetColor);
} else if grayscale {
let _ = execute!(
stdout(),
SetForegroundColor(Color::Rgb {
r: ascii.col_depth,
g: ascii.col_depth,
b: ascii.col_depth
}),
let _ = execute!(stdout(),
SetForegroundColor(Color::Rgb {r: ascii.col_depth, g: ascii.col_depth, b: ascii.col_depth}),
Print(ascii.char),
ResetColor
);
ResetColor);
} else {
print!("{}", ascii.char);
}
@@ -56,49 +41,21 @@ pub fn print_terminal(art: Vec<Vec<Ascii>>, in_colour: bool, grayscale: bool, pr
/// art: Vec<Vec<Ascii>> - vector of vectors representing the art
/// in_colour: bool - whether to print in colour
/// grayscale: bool - whether to print grayscale
/// 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);
pub fn print_terminal_background(art: Vec<Vec<Ascii>>, in_colour: bool, grayscale: bool) {
for line in art {
let _ = execute!(stdout(), Print(prefix_str.clone()));
for ascii in line {
if in_colour {
let _ = execute!(
stdout(),
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]
}),
let _ = execute!(stdout(),
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),
ResetColor
);
ResetColor);
} else if grayscale {
let _ = execute!(
stdout(),
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
}),
let _ = execute!(stdout(),
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),
ResetColor
);
ResetColor);
} else {
error!("This should be unreachable!");
exit(1);
@@ -137,4 +94,4 @@ pub fn print_file(art: Vec<Vec<Ascii>>, out: PathBuf) {
error!("Failed to write to file: {}", e.to_string());
exit(1);
}
}
}
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.