From 57347238e4738960c736b07399265a2937245463 Mon Sep 17 00:00:00 2001 From: PurpleMyst Date: Sun, 5 Jul 2020 13:37:48 +0200 Subject: [PATCH] feat: add -o argument --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 24 +++++++++++++++--------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d519f11..5cf000f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -548,7 +548,7 @@ dependencies = [ [[package]] name = "themis" -version = "1.0.0" +version = "1.2.0" dependencies = [ "anyhow", "image", diff --git a/Cargo.toml b/Cargo.toml index 3653216..eb0bf47 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "themis" -version = "1.1.0" +version = "1.2.0" authors = ["PurpleMyst "] license = "GPL-3.0-only" edition = "2018" diff --git a/src/main.rs b/src/main.rs index 2c0f787..a35a53b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -88,6 +88,10 @@ struct Opt { #[structopt(parse(from_os_str))] tiles_directory: PathBuf, + /// Where to save the finished mosaic + #[structopt(short, long, parse(from_os_str), default_value = "mosaic.png")] + output: PathBuf, + /// The side length that the image to turn into to a mosaic will be resized to #[structopt(short, long, default_value = "128")] mosaic_size: u32, @@ -103,6 +107,7 @@ fn main() -> Result<()> { tiles_directory, mosaic_size, keep_aspect_ratio, + output, } = Opt::from_args(); let possible_tiles = load_images(tiles_directory)?; @@ -114,25 +119,26 @@ fn main() -> Result<()> { }; // For every unique pixel in the image, find its most appropiate tile - let unique_pixels = image.pixels().collect::>(); + let unique_pixels = image + .pixels() + .map(|(_x, _y, pixel)| pixel) + .collect::>(); let pbar = make_pbar("pixels", unique_pixels.len() as _); let tiles = unique_pixels .into_par_iter() .progress_with(pbar) - .filter_map(|(x, y, pixel)| { - let pixel = pick_image_for_pixel(pixel, &possible_tiles)?; - Some(((x, y), pixel)) + .filter_map(|pixel| { + let tile = pick_image_for_pixel(pixel, &possible_tiles)?; + Some((pixel, tile)) }) .collect::>(); // Apply the mapping previously calculated and save the mosaic let mut mosaic = DynamicImage::new_rgba8(image.width() * TILE_SIDE, image.height() * TILE_SIDE); - for y in 0..image.height() { - for x in 0..image.width() { - mosaic.copy_from(*tiles.get(&(x, y)).unwrap(), x * TILE_SIDE, y * TILE_SIDE)?; - } + for (x, y, pixel) in image.pixels() { + mosaic.copy_from(&**tiles.get(&pixel).unwrap(), x * TILE_SIDE, y * TILE_SIDE)?; } - mosaic.save("mosaic.png")?; + mosaic.save(output)?; Ok(()) }