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

Add 'info' command to extract image info

parent 22c74f3a
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,7 @@ use image::ImageFormat;
use serde::{Deserialize, Serialize};
use crate::commands::{
Atlas, Extract, Extrude, Flip, Palette, Pipeline, Reduce, Remap, Rotate, Scale, Split,
Atlas, Extract, Extrude, Flip, Info, Palette, Pipeline, Reduce, Remap, Rotate, Scale, Split,
};
use crate::load_image;
......@@ -50,6 +50,9 @@ pub enum Args {
#[clap(name = "extract")]
#[serde(alias = "extract")]
Extract(Extract),
#[clap(name = "info")]
#[serde(alias = "info")]
Info(Info),
}
impl Args {
......@@ -120,6 +123,15 @@ impl Args {
let image_data = load_image(&extract.input, None)?;
extract.run(&image_data)
}
Args::Info(info) => {
let image_data = load_image(&info.input, None)?;
let output = info.run(&image_data)?;
{
let file = std::fs::File::create(&info.output)?;
serde_json::to_writer_pretty(file, &output)?;
}
Ok(())
}
}
}
}
use crate::utils::TypedOutputFormat;
use clap::{Parser, ValueEnum};
use image::{imageops, GenericImage, ImageFormat, Pixel};
use serde::{Deserialize, Serialize};
/// Extract Information About An Image
#[derive(Debug, Clone, Parser, Serialize, Deserialize)]
#[clap(author, version = "0.7.0")]
pub struct Info {
/// The path to the image file
#[serde(default)]
pub input: String,
/// The path to write the flipped image
#[serde(default)]
pub output: String,
/// The size of each tile in the spritesheet. Assumed to be square tiles
#[clap(short, long, default_value = None)]
#[serde(default)]
pub tile_size: Option<u32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImageInfo {
image_width: u32,
image_height: u32,
image_format: String,
tile_width: Option<u32>,
tile_height: Option<u32>,
rows: Option<u32>,
columns: Option<u32>,
}
impl Info {
pub fn run<T: GenericImage>(&self, image: &T) -> anyhow::Result<ImageInfo>
where
T::Pixel: 'static,
<T::Pixel as Pixel>::Subpixel: 'static,
{
let format = ImageFormat::from_path(&self.input)?;
let image_width = image.width();
let image_height = image.height();
let mut image_info = ImageInfo {
image_width,
image_height,
image_format: format!("{:?}", format).to_lowercase(),
tile_width: None,
tile_height: None,
rows: None,
columns: None,
};
if let Some(tile_size) = self.tile_size {
image_info.tile_width = Some(tile_size);
image_info.tile_height = Some(tile_size);
image_info.rows = Some(image_info.image_height / tile_size);
image_info.columns = Some(image_info.image_width / tile_size);
}
Ok(image_info)
}
}
......@@ -2,6 +2,7 @@ mod atlas;
mod extract;
mod extrude;
mod flip;
mod info;
mod palette;
mod pipeline;
mod reduce;
......@@ -14,6 +15,7 @@ pub use atlas::Atlas;
pub use extract::Extract;
pub use extrude::Extrude;
pub use flip::Flip;
pub use info::Info;
pub use palette::Palette;
pub use pipeline::Pipeline;
pub use reduce::Reduce;
......
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