diff --git a/src/cli_args.rs b/src/cli_args.rs index 1177319a0a006dd9feca9256d195cb22e6d76c18..717b77ccafea07f0f8338543e498594e0ec64b7d 100644 --- a/src/cli_args.rs +++ b/src/cli_args.rs @@ -1,10 +1,10 @@ -use clap::{Parser, Subcommand}; +use clap::Parser; use serde::{Deserialize, Serialize}; // use crate::commands::{calculate_mapping, execute_pipeline, extrude, flip, palette, remap_image, rescale, rotate, write_palette, FlipDirection, RotateDegree, Rotate}; use crate::commands::{Extrude, Flip, Palette, Pipeline, Remap, Rotate, Scale}; -use crate::format::PaletteFormat; -use crate::{load_image, Format}; + +use crate::load_image; /// Crunch is a set of utilities for quickly and easily processing a batch of files, either directly /// or by defining pipelines diff --git a/src/commands/extrude.rs b/src/commands/extrude.rs index 4aa0a4776fcb5df3b82e5d005f50f7e9d77461c0..6559cea8667aaa1b5b49176a565b111ab5b3c528 100644 --- a/src/commands/extrude.rs +++ b/src/commands/extrude.rs @@ -1,4 +1,4 @@ -use crate::utils::{OutputFormat, RgbaOutputFormat, SpriteData}; +use crate::utils::{RgbaOutputFormat, SpriteData}; use clap::Parser; use image::{GenericImage, GenericImageView, Pixel, Rgba, RgbaImage}; use num_traits::cast::ToPrimitive; diff --git a/src/commands/palette.rs b/src/commands/palette.rs index 60c90037adc92ce12f10934e693126c9e23dc04c..167b541b2057f81aa534c8aa1687cc2caeb0b61a 100644 --- a/src/commands/palette.rs +++ b/src/commands/palette.rs @@ -2,9 +2,8 @@ use clap::Parser; use std::cmp::{min, Ordering}; use std::collections::hash_map::RandomState; use std::collections::{HashMap, HashSet}; -use std::fmt::{Formatter, LowerHex, UpperHex}; + use std::io::Write; -use std::path::Path; use deltae::{Delta, LabValue, DE2000}; use image::{GenericImage, Pixel, Rgba}; diff --git a/src/commands/pipeline.rs b/src/commands/pipeline.rs index a538fc50bb968cc1a8cc708b7c32d71955308933..db801120cc5092ae9fb4e4d7ad822588efc6855f 100644 --- a/src/commands/pipeline.rs +++ b/src/commands/pipeline.rs @@ -92,86 +92,80 @@ impl Pipeline { log::debug!("Expanding pipeline file into targets"); let base_path = PathBuf::from(path.parent().unwrap()); - get_targets(base_path.clone(), &pipeline_data).for_each( - |(input_path, output_path, actions)| { - match make_paths(&output_path) { + get_targets(base_path, &pipeline_data).for_each(|(input_path, output_path, actions)| { + match make_paths(&output_path) { + Ok(_) => {} + Err(e) => { + log::error!("Failed to create target directory {}; {}", &output_path, e); + return; + } + } + + if actions.is_empty() { + match std::fs::copy(&input_path, &output_path) { Ok(_) => {} Err(e) => { - log::error!("Failed to create target directory {}; {}", &output_path, e); - return; + log::error!("Failed to copy {} to {}; {}", input_path, output_path, e); } - } + }; + return; + } - if actions.is_empty() { - match std::fs::copy(&input_path, &output_path) { - Ok(_) => {} - Err(e) => { - log::error!("Failed to copy {} to {}; {}", input_path, output_path, e); - } - }; - return; - } + let mut file = result!(load_image(&input_path, None)); - let mut file = result!(load_image(&input_path, None)); - - log::debug!( - "Loaded {}, Executing {} actions", - &input_path, - actions.len() - ); - - let mut count = 1; - for step in actions { - match step { - Args::Rotate(rotate) => { - file = result!(rotate.run(&file)); - } - Args::Extrude(extrude) => { - file = result!(extrude.run(&file)); - } - Args::Scale(scale) => { - file = result!(scale.run(&file)); - } - Args::Flip(flip) => { - file = result!(flip.run(&file)); - } - Args::Remap(remap) => { - let palette = result!(load_image(&remap.palette, None)); - let image_palette = result!(Palette::extract_from(&file)); - let target_palette = result!(Palette::extract_from(&palette)); - - let mappings = - Palette::calculate_mapping(&image_palette, &target_palette); - file = result!(Remap::remap_image(file, mappings)); - } - _ => {} + log::debug!( + "Loaded {}, Executing {} actions", + &input_path, + actions.len() + ); + + for step in actions { + match step { + Args::Rotate(rotate) => { + file = result!(rotate.run(&file)); + } + Args::Extrude(extrude) => { + file = result!(extrude.run(&file)); } + Args::Scale(scale) => { + file = result!(scale.run(&file)); + } + Args::Flip(flip) => { + file = result!(flip.run(&file)); + } + Args::Remap(remap) => { + let palette = result!(load_image(&remap.palette, None)); + let image_palette = result!(Palette::extract_from(&file)); + let target_palette = result!(Palette::extract_from(&palette)); - count += 1; + let mappings = Palette::calculate_mapping(&image_palette, &target_palette); + file = result!(Remap::remap_image(file, mappings)); + } + _ => {} } + } - let mut outer_target_path = PathBuf::from(&output_path); - outer_target_path.pop(); - - if let Err(e) = std::fs::create_dir(&outer_target_path) { - match e.kind() { - std::io::ErrorKind::AlreadyExists => { /* This is fine */ } - _ => log::error!( - "Failed to create containing directory {}; {}", - outer_target_path.to_string_lossy(), - e - ), - } + let mut outer_target_path = PathBuf::from(&output_path); + outer_target_path.pop(); + + if let Err(e) = std::fs::create_dir(&outer_target_path) { + match e.kind() { + std::io::ErrorKind::AlreadyExists => { /* This is fine */ } + _ => log::error!( + "Failed to create containing directory {}; {}", + outer_target_path.to_string_lossy(), + e + ), } + } - match file.save(&output_path) { - Ok(_) => {} - Err(e) => { - log::error!("Failed to save to {}; {}", output_path, e); - } + match file.save(&output_path) { + Ok(_) => {} + Err(e) => { + log::error!("Failed to save to {}; {}", output_path, e); } - }, - ); + } + }); Ok(()) } @@ -211,7 +205,7 @@ fn get_targets( ( join(&base_path, &input_path), join(&base_path, &output_path), - (*value).actions.clone(), + value.actions.clone(), ) }) .collect(), @@ -223,7 +217,7 @@ fn get_targets( .refs .get(reference.as_str()) .iter() - .map(|value| (*value).actions.clone()) + .map(|value| value.actions.clone()) .flat_map(|actions| { let mut paths = Vec::new(); let target_path = join(&base_path, pattern); diff --git a/src/commands/remap.rs b/src/commands/remap.rs index 342999a172a5984969b8efc141c1f97cc59173ef..3dd30839d063bb943153e9c62ce1767cc4a92eca 100644 --- a/src/commands/remap.rs +++ b/src/commands/remap.rs @@ -4,7 +4,7 @@ use image::{GenericImage, Pixel, Rgba}; use num_traits::ToPrimitive; use serde::{Deserialize, Serialize}; -use crate::utils::{new_image, BasicRgba, OutputFormat, TypedOutputFormat}; +use crate::utils::{new_image, BasicRgba, OutputFormat}; /// Convert the colour space of an image to that of a given palette file #[derive(Debug, Clone, Parser, Serialize, Deserialize)] diff --git a/src/main.rs b/src/main.rs index 84689c5a8a19941421cc6eed9ac5c68a0678ffe3..3338e4e4ed30d62290ae5d2435006edd93aa7c43 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,10 +4,9 @@ mod format; mod utils; use clap::Parser; -use image::{GenericImage, Rgba, SubImage}; use crate::cli_args::Args; -use crate::format::{load_image, Format}; +use crate::format::load_image; fn main() -> anyhow::Result<(), anyhow::Error> { env_logger::Builder::from_env("LOG_LEVEL").init(); diff --git a/src/utils.rs b/src/utils.rs index e5c952b57a2491e26586a01729a2db514d157097..d29ee7cc4daeb9067fb08c1379ff42b072bae38f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -5,7 +5,6 @@ use deltae::LabValue; use glam::Vec3; use image::{GenericImage, GenericImageView, Rgb, Rgba, RgbaImage, SubImage}; use lab::Lab; -use serde::{Deserialize, Serialize}; #[derive(Clone, Copy)] pub struct SpriteData<'a, T: GenericImage> {