From dfb2a41a193130b6fd8f2f2db19cb6a6ff7f8c3d Mon Sep 17 00:00:00 2001 From: HoLLy Date: Fri, 27 Oct 2023 22:04:22 +0200 Subject: [PATCH] Create options struct for extract_sprites_with_texture_atlas --- data-prepper/src/ryza3/extract_images/mod.rs | 38 ++++++++---------- data-prepper/src/sophie/extract_images/mod.rs | 21 +++++----- data-prepper/src/utils/images/mod.rs | 40 ++++++++++++++----- 3 files changed, 55 insertions(+), 44 deletions(-) diff --git a/data-prepper/src/ryza3/extract_images/mod.rs b/data-prepper/src/ryza3/extract_images/mod.rs index 2882f93..922d281 100644 --- a/data-prepper/src/ryza3/extract_images/mod.rs +++ b/data-prepper/src/ryza3/extract_images/mod.rs @@ -7,11 +7,9 @@ pub use extract_maps::MapInfoList; use tracing::info; use crate::extract_images::{Args, Category}; -use crate::utils::images::extract_prefixed_with_texture_atlas; +use crate::utils::images::{extract_sprites_with_texture_atlas, ExtractSpritesOptions}; use crate::utils::PakIndex; -const PATH_ITEMS: &str = "items"; -const PATH_ENEMIES: &str = "enemies"; const PATH_MAPS: &str = "maps"; pub fn extract_images( @@ -22,28 +20,26 @@ pub fn extract_images( ) -> anyhow::Result<()> { if category.is_none() || category == Some(Category::Monsters) { info!("Extracting monster portraits"); - const MONSTER_PATTERN: &str = r"\data\x64\res_cmn\ui\neo\neo_a24_monster_l_*.g1t"; - extract_prefixed_with_texture_atlas( - args, - pak_index, - MONSTER_PATTERN, - output_directory, - PATH_ENEMIES, - ) - .context("extract monster portraits")?; + let options = ExtractSpritesOptions { + pattern: r"\data\x64\res_cmn\ui\neo\neo_a24_monster_l_*.g1t", + subdirectory: "enemies", + sprite_dimensions: (512, 512), + texture_atlas_dimensions: (64, 64), + }; + extract_sprites_with_texture_atlas(args, pak_index, output_directory, options) + .context("extract monster portraits")?; } if category.is_none() || category == Some(Category::Items) { info!("Extracting item icons"); - const ITEM_PATTERN: &str = r"\data\x64\res_cmn\ui\neo\neo_a24_item_l_*.g1t"; - extract_prefixed_with_texture_atlas( - args, - pak_index, - ITEM_PATTERN, - output_directory, - PATH_ITEMS, - ) - .context("extract item icons")?; + let options = ExtractSpritesOptions { + pattern: r"\data\x64\res_cmn\ui\neo\neo_a24_item_l_*.g1t", + subdirectory: "items", + sprite_dimensions: (512, 512), + texture_atlas_dimensions: (64, 64), + }; + extract_sprites_with_texture_atlas(args, pak_index, output_directory, options) + .context("extract item icons")?; } if category.is_none() || category == Some(Category::Maps) { diff --git a/data-prepper/src/sophie/extract_images/mod.rs b/data-prepper/src/sophie/extract_images/mod.rs index 199c8c2..46f47b3 100644 --- a/data-prepper/src/sophie/extract_images/mod.rs +++ b/data-prepper/src/sophie/extract_images/mod.rs @@ -4,11 +4,9 @@ use anyhow::Context; use tracing::info; use crate::extract_images::{Args, Category}; -use crate::utils::images::extract_prefixed_with_texture_atlas; +use crate::utils::images::{extract_sprites_with_texture_atlas, ExtractSpritesOptions}; use crate::utils::PakIndex; -const PATH_ITEMS: &str = "items"; - pub fn extract_images( args: &Args, pak_index: &mut PakIndex, @@ -17,15 +15,14 @@ pub fn extract_images( ) -> anyhow::Result<()> { if category.is_none() || category == Some(Category::Items) { info!("Extracting item icons"); - const ITEM_PATTERN: &str = r"\Data\Win32\ui_JP\a17_item_l_*.g1t"; - extract_prefixed_with_texture_atlas( - args, - pak_index, - ITEM_PATTERN, - output_directory, - PATH_ITEMS, - ) - .context("extract item icons")?; + let options = ExtractSpritesOptions { + pattern: r"\Data\Win32\ui_JP\a17_item_l_*.g1t", + subdirectory: "items", + sprite_dimensions: (512, 512), + texture_atlas_dimensions: (64, 64), + }; + extract_sprites_with_texture_atlas(args, pak_index, output_directory, options) + .context("extract item icons")?; } Ok(()) diff --git a/data-prepper/src/utils/images/mod.rs b/data-prepper/src/utils/images/mod.rs index 90008ec..03ec8a8 100644 --- a/data-prepper/src/utils/images/mod.rs +++ b/data-prepper/src/utils/images/mod.rs @@ -11,14 +11,25 @@ use crate::extract_images::Args; pub mod rgba8_image; pub mod texture_atlas; -pub fn extract_prefixed_with_texture_atlas( +pub struct ExtractSpritesOptions { + /// The pattern used to find file names + pub pattern: &'static str, + /// The subdirectory in the output directory to put the individual images in. This is also the + /// name of the texture atlas file. + pub subdirectory: &'static str, + /// The size of each individual input image. + pub sprite_dimensions: (u32, u32), + /// The size of each item in the texture atlas + pub texture_atlas_dimensions: (u32, u32), +} + +pub fn extract_sprites_with_texture_atlas( args: &Args, pak_index: &mut PakIndex, - pattern: &'static str, output_directory: &Path, - subdirectory: &'static str, + options: ExtractSpritesOptions, ) -> anyhow::Result<()> { - let image_output_folder = output_directory.join(subdirectory); + let image_output_folder = output_directory.join(options.subdirectory); if !args.dont_write_images { debug!("Creating image output directory"); std::fs::create_dir_all(&image_output_folder).context("create image output directory")?; @@ -26,16 +37,21 @@ pub fn extract_prefixed_with_texture_atlas( let mut entries: Vec<_> = pak_index .iter_entries() - .filter_map(|e| match_pattern::(pattern, e.get_file_name()).map(|num| (e, num))) + .filter_map(|e| { + match_pattern::(options.pattern, e.get_file_name()).map(|num| (e, num)) + }) .map(|(f, num)| (f.get_file_name().to_string(), num)) .collect(); entries.sort_by_key(|(_, num)| *num); // create texture atlas - let mut texture_atlas = - UniformTextureAtlas::new_with_scaling((512, 512), (64, 64), entries.len()) - .context("create texture atlas")?; + let mut texture_atlas = UniformTextureAtlas::new_with_scaling( + options.sprite_dimensions, + options.texture_atlas_dimensions, + entries.len(), + ) + .context("create texture atlas")?; for (entry, num) in entries { let mut file = pak_index @@ -47,11 +63,13 @@ pub fn extract_prefixed_with_texture_atlas( let g1t = gust_g1t::GustG1t::read(&mut file).context("read g1t")?; let texture = &g1t.textures[0]; - if texture.width != 512 && texture.height != 512 { + if (texture.width, texture.height) != options.sprite_dimensions { bail!( - "Texture {entry} has invalid size {}x{}, expected 512x512", + "Texture {entry} has invalid size {}x{}, expected {}x{}", texture.width, texture.height, + options.sprite_dimensions.0, + options.sprite_dimensions.1, ); } @@ -73,7 +91,7 @@ pub fn extract_prefixed_with_texture_atlas( let atlas_directory = output_directory.join("texture-atlasses"); std::fs::create_dir_all(&atlas_directory).context("create atlas directory")?; crate::extract::write_data_to_file( - &atlas_directory.join(format!("{subdirectory}.json")), + &atlas_directory.join(format!("{}.json", options.subdirectory)), &texture_atlas.create_info(), ) .context("write texture atlas info")?;