ft: arg parsing
This commit is contained in:
1 parent
9f722b1040
commit
14093f7a61
2 files changed
+104
-31
No files matched your search
+34
-31
@@ -1,11 +1,15 @@
|
||||
mod cli;
|
||||
|
||||
use clap::Parser;
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
env,
|
||||
num::ParseIntError,
|
||||
};
|
||||
|
||||
use dotenv::dotenv;
|
||||
use image::{DynamicImage, GenericImageView, ImageReader, Rgb};
|
||||
use image::{DynamicImage, GenericImageView, Rgb};
|
||||
|
||||
use crate::cli::Cli;
|
||||
|
||||
// TODO (medium priority): replace hashset with another hashmap<u8, Vec<(u32, u32)>>;
|
||||
// that way we can track pixel locations for colours.
|
||||
@@ -45,7 +49,7 @@ fn extract_colours_set(img: DynamicImage) -> HashSet<Rgb<u8>> {
|
||||
out.into_iter().map(|c| Rgb([c[0], c[1], c[2]])).collect()
|
||||
}
|
||||
|
||||
fn closest_colour(colour: Colour, all: &ColourSet) -> Option<Colour> {
|
||||
fn closest_colour(colour: &Colour, all: &ColourSet) -> Option<Colour> {
|
||||
// use redmean to calculate the distance between colours
|
||||
// inevitably O(len(all))
|
||||
let mut closest: Option<Colour> = None;
|
||||
@@ -71,31 +75,23 @@ fn closest_colour(colour: Colour, all: &ColourSet) -> Option<Colour> {
|
||||
return closest;
|
||||
}
|
||||
|
||||
fn map_image_list(img_file: &str, args: &Vec<String>) -> HashMap<Colour, Colour> {
|
||||
let img = ImageReader::open(img_file).unwrap().decode().unwrap(); // FIXME: unwraps
|
||||
|
||||
let all_cols = extract_colours(img);
|
||||
fn map_image_list(all_cols: &ColourSet, cols: Vec<Colour>) -> HashMap<Colour, Colour> {
|
||||
let mut output_cols: HashMap<Colour, Colour> = HashMap::new();
|
||||
|
||||
args.iter().skip(2).for_each(|c_str| {
|
||||
let c = decode_hex(c_str).unwrap(); // FIXME: unwraps
|
||||
if let Some(col) = closest_colour(c, &all_cols) {
|
||||
for c in cols {
|
||||
if let Some(col) = closest_colour(&c, &all_cols) {
|
||||
output_cols.insert(c, col);
|
||||
}
|
||||
});
|
||||
}
|
||||
output_cols
|
||||
}
|
||||
|
||||
fn map_image_image(img_file_1: &str, img_file_2: &str) -> HashMap<Colour, Colour> {
|
||||
let img_1 = ImageReader::open(img_file_1).unwrap().decode().unwrap();
|
||||
let img_2 = ImageReader::open(img_file_2).unwrap().decode().unwrap();
|
||||
|
||||
let img_1_cols = extract_colours_set(img_1);
|
||||
let img_2_cols = extract_colours(img_2);
|
||||
fn map_image_image(img_1_cols: &ColourSet, img_2: DynamicImage) -> HashMap<Colour, Colour> {
|
||||
let img_2_cols = extract_colours_set(img_2);
|
||||
|
||||
let mut output_cols: HashMap<Colour, Colour> = HashMap::new();
|
||||
img_1_cols.iter().for_each(|c| {
|
||||
if let Some(col) = closest_colour(*c, &img_2_cols) {
|
||||
img_2_cols.iter().for_each(|c| {
|
||||
if let Some(col) = closest_colour(c, &img_1_cols) {
|
||||
output_cols.insert(*c, col);
|
||||
}
|
||||
});
|
||||
@@ -103,22 +99,29 @@ fn map_image_image(img_file_1: &str, img_file_2: &str) -> HashMap<Colour, Colour
|
||||
output_cols
|
||||
}
|
||||
|
||||
fn encode_hex(c: &Colour) -> String {
|
||||
format!("#{:02x}{:02x}{:02x}", c[0], c[1], c[2])
|
||||
}
|
||||
|
||||
fn pretty_print(h: HashMap<Colour, Colour>) {
|
||||
h.iter()
|
||||
.for_each(|(k, v)| println!("{}: {}", encode_hex(k), encode_hex(v)))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
dotenv().ok();
|
||||
pretty_env_logger::init();
|
||||
let cli = Cli::parse();
|
||||
|
||||
// TODO (high priority): proper arg parsing
|
||||
let args: Vec<String> = env::args().collect();
|
||||
if args.len() > 2 {
|
||||
// list of colours and image
|
||||
// let out_cols = map_image_list(&args[1], &args);
|
||||
let out_cols = map_image_image(&args[1], &args[2]);
|
||||
let set = extract_colours(cli.image);
|
||||
|
||||
out_cols.iter().for_each(|(k, v)| {
|
||||
println!(
|
||||
"({}, {}, {}): ({}, {}, {})",
|
||||
k[0], k[1], k[2], v[0], v[1], v[2]
|
||||
)
|
||||
})
|
||||
if let Some(cs) = cli.colours {
|
||||
let colours = map_image_list(&set, cs);
|
||||
pretty_print(colours);
|
||||
}
|
||||
|
||||
if let Some(i) = cli.second_image {
|
||||
let colours = map_image_image(&set, i);
|
||||
pretty_print(colours);
|
||||
}
|
||||
}
|
||||