Browse Source

Fixed clap destroying the default palette

master v0.2.0
parent
commit
c4f029f38a
  1. 2
      Cargo.lock
  2. 2
      Cargo.toml
  3. 56
      src/main.rs

2
Cargo.lock generated

@ -69,7 +69,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "charify" name = "charify"
version = "0.1.0" version = "0.2.0"
dependencies = [ dependencies = [
"clap", "clap",
"image", "image",

2
Cargo.toml

@ -1,6 +1,6 @@
[package] [package]
name = "charify" name = "charify"
version = "0.1.0" version = "0.2.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

56
src/main.rs

@ -17,9 +17,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
use clap::Arg;
use std::io::Write; use std::io::Write;
use std::process::exit; use std::process::exit;
use clap::Arg;
/// returns a character from a character set that corresponds to given brightness /// returns a character from a character set that corresponds to given brightness
fn get_char(charset: &[char], brightness: &u8) -> char { fn get_char(charset: &[char], brightness: &u8) -> char {
@ -38,12 +38,11 @@ fn charify(charset: &[char], brightness_map: &image::GrayImage) -> Vec<char> {
return character_map; return character_map;
} }
fn main() { fn main() {
let mut charset: Vec<char> = vec![' ', '░', '▒', '▓', '█']; let mut charset: Vec<char> = vec![' ', '░', '▒', '▓', '█'];
let matches = clap::Command::new("charify") let matches = clap::Command::new("charify")
.version("0.1") .version("0.2")
.author("Kasyanov Nikolay Alexeyevich (Unbewohnte)") .author("Kasyanov Nikolay Alexeyevich (Unbewohnte)")
.arg( .arg(
Arg::new("image") Arg::new("image")
@ -52,7 +51,7 @@ fn main() {
.required(true) .required(true)
.long("image") .long("image")
.short('i') .short('i')
.index(1) .index(1),
) )
.arg( .arg(
Arg::new("destination") Arg::new("destination")
@ -61,7 +60,7 @@ fn main() {
.required(true) .required(true)
.long("destination") .long("destination")
.short('d') .short('d')
.index(2) .index(2),
) )
.arg( .arg(
Arg::new("new_dimensions") Arg::new("new_dimensions")
@ -69,7 +68,7 @@ fn main() {
.takes_value(true) .takes_value(true)
.required(false) .required(false)
.long("new_dimensions") .long("new_dimensions")
.short('r') .short('r'),
) )
.arg( .arg(
Arg::new("charset") Arg::new("charset")
@ -78,7 +77,7 @@ fn main() {
.required(false) .required(false)
.long("charset") .long("charset")
.short('c') .short('c')
.default_value(&*format!("{:?}", charset)) .default_value(" ░▒▓█"),
) )
.get_matches(); .get_matches();
@ -123,15 +122,13 @@ fn main() {
} }
} }
match destination_file_path.parent() { match destination_file_path.parent() {
Some(path) => { Some(path) => match std::fs::create_dir_all(path) {
match std::fs::create_dir_all(path) { Ok(_) => {}
Ok(_) => {} Err(e) => {
Err(e) => { eprintln!("[ERROR] could not create \"{}\": {}", path.display(), e);
eprintln!("[ERROR] could not create \"{}\": {}", path.display(), e); exit(1);
exit(1);
}
} }
} },
None => {} None => {}
} }
@ -148,19 +145,20 @@ fn main() {
// work with new dimensions if present // work with new dimensions if present
match matches.value_of("new_dimensions") { match matches.value_of("new_dimensions") {
Some(new_dimensions) => { Some(new_dimensions) => match new_dimensions.split_once('x') {
match new_dimensions.split_once('x') { Some((nw_str, nh_str)) => {
Some((nw_str, nh_str)) => { let new_width: u32 = nw_str.parse::<u32>().unwrap();
let new_width: u32 = nw_str.parse::<u32>().unwrap(); let new_height: u32 = nh_str.parse::<u32>().unwrap();
let new_height: u32 = nh_str.parse::<u32>().unwrap();
source_image = image::imageops::resize( source_image = image::imageops::resize(
&source_image, new_width, new_height, &source_image,
image::imageops::FilterType::Lanczos3); new_width,
} new_height,
None => {} image::imageops::FilterType::Lanczos3,
);
} }
} None => {}
},
None => {} None => {}
} }
@ -179,7 +177,11 @@ fn main() {
let character_map = charify(&charset, &source_image); let character_map = charify(&charset, &source_image);
for y in 0..source_image.height() { for y in 0..source_image.height() {
for x in 0..source_image.width() { for x in 0..source_image.width() {
match write!(destination_file, "{}", character_map[(y * source_image.width() + x) as usize]) { match write!(
destination_file,
"{}",
character_map[(y * source_image.width() + x) as usize] as char
) {
Ok(_) => {} Ok(_) => {}
Err(e) => { Err(e) => {
eprintln!("[ERROR] error writing to destination file: {}", e) eprintln!("[ERROR] error writing to destination file: {}", e)

Loading…
Cancel
Save