Skip to content

Commit

Permalink
Create options struct for extract_sprites_with_texture_atlas
Browse files Browse the repository at this point in the history
  • Loading branch information
holly-hacker committed Oct 27, 2023
1 parent ed5ec28 commit dfb2a41
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 44 deletions.
38 changes: 17 additions & 21 deletions data-prepper/src/ryza3/extract_images/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) {
Expand Down
21 changes: 9 additions & 12 deletions data-prepper/src/sophie/extract_images/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(())
Expand Down
40 changes: 29 additions & 11 deletions data-prepper/src/utils/images/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,47 @@ 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")?;
}

let mut entries: Vec<_> = pak_index
.iter_entries()
.filter_map(|e| match_pattern::<usize>(pattern, e.get_file_name()).map(|num| (e, num)))
.filter_map(|e| {
match_pattern::<usize>(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
Expand All @@ -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,
);
}

Expand All @@ -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")?;
Expand Down

0 comments on commit dfb2a41

Please sign in to comment.