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

Add numeric file sort option to atlas builder

parent f5ca59c7
No related branches found
No related tags found
No related merge requests found
Pipeline #604 passed with stages
in 3 minutes
......@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [0.8.2] - 2024-04-28
### Added
- Added numeric file name sort option to atlas builder
## [0.8.1] - 2024-04-28
### Added
......
......@@ -233,7 +233,7 @@ dependencies = [
[[package]]
name = "crunch-cli"
version = "0.8.1"
version = "0.8.2"
dependencies = [
"anyhow",
"clap",
......
[package]
name = "crunch-cli"
version = "0.8.1"
version = "0.8.2"
edition = "2021"
homepage = "https://microhacks.lcr.app/crunch/"
......
......@@ -4,6 +4,7 @@ use etagere::{AllocId, AtlasAllocator, Rectangle, Size};
use image::{image_dimensions, GenericImage, Rgba, RgbaImage};
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::collections::HashMap;
use std::fmt::Display;
use std::fs::File;
......@@ -16,7 +17,7 @@ fn default_max_size() -> usize {
/// Given a set of images, create a single atlas image and metadata file containing all of the original
/// set
#[derive(Parser, Serialize, Deserialize, Clone, Debug)]
#[clap(author, version = "0.8.1")]
#[clap(author, version = "0.8.2")]
pub struct Atlas {
/// A pattern evaluating to one or more image files
#[serde(default)]
......@@ -36,6 +37,13 @@ pub struct Atlas {
/// be padded to the correct size before specifying an area
#[clap(short = 'a', long = "area")]
pub area: Option<usize>,
/// Perform a numeric sort on the names of files being combined.
///
/// With this flag enabled, the files "104", "5", "2045" would be ordered as "5", "104", "2045"
/// Without this flag, those same files would be ordered by OS determination, typically ""104", "2045", "5"
#[serde(default)]
#[clap(short = 'n')]
pub numeric: bool,
}
#[derive(Copy, Clone, Hash, Eq, PartialEq)]
......@@ -108,9 +116,44 @@ impl Atlas {
let pattern = glob::glob(self.glob.as_str())?;
let mut builder = AtlasBuilder::new((self.max_frame_width, self.max_frame_height));
let mut pattern = pattern.filter_map(Result::ok).collect::<Vec<PathBuf>>();
if self.numeric {
pattern.sort_by(|a, b| {
let a_num: isize = match a
.file_stem()
.and_then(|s| s.to_str())
.and_then(|s| s.parse().ok())
{
Some(v) => v,
_ => return Ordering::Equal,
};
let b_num: isize = match b
.file_stem()
.and_then(|s| s.to_str())
.and_then(|s| s.parse().ok())
{
Some(v) => v,
_ => return Ordering::Equal,
};
a_num.cmp(&b_num)
});
} else {
pattern.sort_by(|a, b| {
let a_num = match a.file_stem().and_then(|s| s.to_str()) {
Some(v) => v,
_ => return Ordering::Equal,
};
let b_num = match b.file_stem().and_then(|s| s.to_str()) {
Some(v) => v,
_ => return Ordering::Equal,
};
a_num.cmp(b_num)
})
}
let page_content_map: HashMap<usize, Vec<(PathBuf, Rectangle)>> = pattern
.into_iter()
.filter_map(Result::ok)
.flat_map(|path| {
if let Some(fixed) = &self.area {
Ok((*fixed as u32, *fixed as u32, path))
......
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