Skip to content
Snippets Groups Projects
Verified Commit 31609c42 authored by Louis's avatar Louis :fire:
Browse files

Apply clippy warning changes

parent cf193e1b
No related branches found
No related tags found
No related merge requests found
hard_tabs = true
group_imports = "StdExternalCrate"
use_field_init_shorthand = true
use_try_shorthand = true
\ No newline at end of file
......@@ -16,10 +16,8 @@ use crate::{load_image, Format};
#[clap(about, long_about = None)]
pub struct Args {
/// The path to the spritesheet file
// #[clap(short = 'i', long = "in")]
in_path: String,
/// The path to the output file
// #[clap(short = 'o', long = "out")]
out_path: String,
/// Force Crunch to read the input file as a specific format
......
......@@ -71,8 +71,6 @@ pub fn extrude(
pix.0[3].to_u8().unwrap(),
]);
// log::info!
new_image.put_pixel(
pad_x + (sprite.tx * space_x) + img_x + x,
pad_y + (sprite.ty * space_y) + img_y + y,
......@@ -82,7 +80,5 @@ pub fn extrude(
}
}
log::info!("{}", new_image.width() * new_image.height());
Ok(new_image)
}
use clap::ArgEnum;
use image::imageops::FilterType;
use image::{imageops, GenericImage, ImageBuffer, Pixel};
use image::{imageops, GenericImage, Pixel};
use serde::{Deserialize, Serialize};
use crate::TypedOutputFormat;
#[derive(Copy, Clone, Serialize, Deserialize, ArgEnum, Debug)]
pub enum FlipDirection {
#[serde(rename = "vertical")]
......@@ -13,10 +14,7 @@ pub enum FlipDirection {
Both,
}
pub fn flip<T: GenericImage>(
image: &T,
dir: FlipDirection,
) -> anyhow::Result<ImageBuffer<T::Pixel, Vec<<T::Pixel as Pixel>::Subpixel>>>
pub fn flip<T: GenericImage>(image: &T, dir: FlipDirection) -> anyhow::Result<TypedOutputFormat<T>>
where
T::Pixel: 'static,
<T::Pixel as Pixel>::Subpixel: 'static,
......
......@@ -16,18 +16,17 @@ pub type Palette = Vec<BasicRgba>;
pub fn palette(image: &impl GenericImage) -> anyhow::Result<Palette> {
let mut colours = HashSet::new();
for (_, _, pixel) in image.pixels().into_iter() {
for (_, _, pixel) in image.pixels() {
let pixel = pixel.to_rgba();
colours.insert(Rgba::from([
pixel.0[0].to_u8().unwrap(),
pixel.0[1].to_u8().unwrap(),
pixel.0[2].to_u8().unwrap(),
pixel.0[3].to_u8().unwrap(),
// &pixel.0.map(|i| i.to_u8().unwrap())
]));
}
Ok(colours.iter().map(|c| BasicRgba::from(c)).collect())
Ok(colours.iter().map(BasicRgba::from).collect())
}
struct HexStringValue(String);
......@@ -44,7 +43,7 @@ impl HexString for Rgba<u8> {
}
impl<T: HexString + Clone> HexString for &T {
fn as_hex_string(&self) -> HexStringValue {
self.clone().as_hex_string()
(*self).clone().as_hex_string()
}
}
impl UpperHex for HexStringValue {
......@@ -63,11 +62,9 @@ pub fn write_palette<T: AsRef<Path>>(
format: PaletteFormat,
outpath: T,
) -> anyhow::Result<()> {
#[allow(clippy::redundant_clone)]
let mut sorted = colours.clone();
sorted.sort_by(|pa, pb| {
// format!("{:X}", pa.as_hex_string())
// .cmp(&format!("{:X}", pb.as_hex_string()))
let hue_a = pa.hue();
let hue_b = pb.hue();
......@@ -83,7 +80,7 @@ pub fn write_palette<T: AsRef<Path>>(
});
match format {
PaletteFormat::PNG => {
PaletteFormat::Png => {
let num_colours = sorted.len();
let image_width = min(16, num_colours);
let image_height = if num_colours % 16 > 0 {
......@@ -103,7 +100,7 @@ pub fn write_palette<T: AsRef<Path>>(
out_image.save(outpath)?;
}
PaletteFormat::TXT => {
PaletteFormat::Txt => {
let mut file = std::fs::File::create(outpath)?;
for colour in sorted.iter() {
let line = format!("#{:X}\n", colour);
......@@ -144,13 +141,13 @@ pub fn calculate_mapping(from: &Palette, to: &Palette) -> ColourMapping {
match closest {
Some(idx) => match to_palette_vectors.get(&idx) {
Some(col) => {
out_map.insert(colour.clone(), *col.clone());
out_map.insert(*colour, **col);
}
None => {
println!("No matching vec for {} with col {:?}", idx, &colour);
out_map.insert(
colour.clone(),
*colour,
BasicRgba {
r: 0,
g: 0,
......@@ -163,7 +160,7 @@ pub fn calculate_mapping(from: &Palette, to: &Palette) -> ColourMapping {
None => {
println!("No closest for {:?}", &colour);
out_map.insert(
colour.clone(),
*colour,
BasicRgba {
r: 0,
g: 0,
......@@ -175,7 +172,5 @@ pub fn calculate_mapping(from: &Palette, to: &Palette) -> ColourMapping {
}
}
// println!("{:?}", &out_map);
out_map
}
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::cli_args::CrunchCommand;
use crate::utils::Pipeline;
use crate::{commands, load_image};
#[derive(Error, Debug)]
......@@ -22,7 +20,7 @@ pub enum PipelineType {
Pipeline {
input_path: String,
output_path: String,
actions: Vec<crate::cli_args::CrunchCommand>,
actions: Vec<CrunchCommand>,
},
Ref {
input_path: String,
......@@ -33,7 +31,7 @@ pub enum PipelineType {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PipelineRef {
pub actions: Vec<crate::cli_args::CrunchCommand>,
pub actions: Vec<CrunchCommand>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
......@@ -52,7 +50,7 @@ pub fn execute_pipeline<IN: ToString, OUT: ToString>(
}
let file_contents = std::fs::read(file_path.to_string())?;
let mut pipeline_data: PipelineFile = if path.ends_with(".toml") {
let pipeline_data: PipelineFile = if path.ends_with(".toml") {
toml::from_slice(&file_contents)?
} else {
serde_json::from_slice(&file_contents)?
......@@ -73,7 +71,7 @@ pub fn execute_pipeline<IN: ToString, OUT: ToString>(
reference,
} => pipeline_data
.refs
.get(reference)
.get(reference.as_str())
.map(|value| (input_path, output_path, value.actions.clone())),
})
.for_each(|(input_path, output_path, actions)| {
......@@ -86,8 +84,6 @@ pub fn execute_pipeline<IN: ToString, OUT: ToString>(
};
let mut count = 1;
let total = actions.len();
for step in actions {
match step {
CrunchCommand::Extrude {
......@@ -218,23 +214,21 @@ pub fn execute_pipeline<IN: ToString, OUT: ToString>(
let mut outer_target_path = PathBuf::from(output_path);
outer_target_path.pop();
match std::fs::create_dir(&outer_target_path) {
Err(e) => match e.kind() {
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);
return;
}
}
});
......
......@@ -10,7 +10,7 @@ pub fn remap_image(
mappings: ColourMapping,
) -> anyhow::Result<OutputFormat> {
let mut output = new_image(image.width(), image.height());
for (x, y, pixel) in image.pixels().into_iter() {
for (x, y, pixel) in image.pixels() {
let pixel = pixel.to_rgba();
if pixel.0[3].to_u8().unwrap() == 0 {
......@@ -32,7 +32,7 @@ pub fn remap_image(
y,
Rgba::from(BasicRgba {
a: basic.a,
..mapped_data.clone()
..*mapped_data
}),
),
None => output.put_pixel(x, y, Rgba::from(BasicRgba::transparent())),
......
use clap::ArgEnum;
use image::imageops::FilterType;
use image::{imageops, GenericImage, ImageBuffer, Pixel};
use image::{imageops, GenericImage, Pixel};
use serde::{Deserialize, Serialize};
use crate::TypedOutputFormat;
#[derive(Copy, Clone, Serialize, Deserialize, ArgEnum, Debug)]
pub enum RotateDegree {
#[serde(rename = "90")]
......@@ -16,7 +17,7 @@ pub enum RotateDegree {
pub fn rotate<T: GenericImage>(
image: &T,
degree: RotateDegree,
) -> anyhow::Result<ImageBuffer<T::Pixel, Vec<<T::Pixel as Pixel>::Subpixel>>>
) -> anyhow::Result<TypedOutputFormat<T>>
where
T::Pixel: 'static,
<T::Pixel as Pixel>::Subpixel: 'static,
......
use image::imageops::FilterType;
use image::{imageops, GenericImage, ImageBuffer, Pixel};
use image::{imageops, GenericImage, Pixel};
pub fn rescale<T: GenericImage>(
image: &T,
factor: f32,
) -> anyhow::Result<ImageBuffer<T::Pixel, Vec<<T::Pixel as Pixel>::Subpixel>>>
use crate::TypedOutputFormat;
pub fn rescale<T: GenericImage>(image: &T, factor: f32) -> anyhow::Result<TypedOutputFormat<T>>
where
T::Pixel: 'static,
<T::Pixel as Pixel>::Subpixel: 'static,
......
......@@ -10,39 +10,33 @@ use thiserror::Error;
Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ArgEnum, Debug, Serialize, Deserialize, Default,
)]
pub enum PaletteFormat {
TXT,
Txt,
#[default]
PNG,
Png,
}
// impl Default for PaletteFormat {
// fn default() -> Self {
// Self::PNG
// }
// }
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ArgEnum, Debug)]
pub enum Format {
PNG,
JPG,
GIF,
ICO,
TGA,
TIFF,
BMP,
Png,
Jpg,
Gif,
Ico,
Tga,
Tiff,
Bmp,
}
impl Format {
pub fn as_image_format(&self) -> ImageFormat {
use Format::*;
match self {
PNG => ImageFormat::Png,
JPG => ImageFormat::Jpeg,
GIF => ImageFormat::Gif,
ICO => ImageFormat::Ico,
TGA => ImageFormat::Tga,
TIFF => ImageFormat::Tiff,
BMP => ImageFormat::Bmp,
Png => ImageFormat::Png,
Jpg => ImageFormat::Jpeg,
Gif => ImageFormat::Gif,
Ico => ImageFormat::Ico,
Tga => ImageFormat::Tga,
Tiff => ImageFormat::Tiff,
Bmp => ImageFormat::Bmp,
}
}
}
......
......@@ -25,6 +25,9 @@ struct SpriteData<'a, T: GenericImage> {
}
pub type OutputFormat = image::ImageBuffer<Rgba<u8>, Vec<u8>>;
#[allow(type_alias_bounds)]
pub type TypedOutputFormat<T: image::GenericImage> =
image::ImageBuffer<T::Pixel, Vec<<T::Pixel as image::Pixel>::Subpixel>>;
fn main() -> anyhow::Result<(), anyhow::Error> {
env_logger::Builder::from_env("LOG_LEVEL").init();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment