Support project conversion #27

Merged
jaby merged 9 commits from topic/jb/psyfileconv-tim-prj into main 2025-04-16 20:19:59 +00:00
6 changed files with 57 additions and 37 deletions
Showing only changes of commit 3500a288bc - Show all commits

View File

@ -36,7 +36,8 @@ fn modify_palette(mut image: IndexedImage, clut_align: ClutAlignment, semi_trans
image
}
fn encode<T: PSXImageConverter>(header_conv: &mut dyn HeaderEncoder, image: T, tex_pos: Point, clut_pos: Point, color_depth: ColorType, clut_align: ClutAlignment, output: &mut dyn Write) -> Result<(), Error> {
fn encode<T: PSXImageConverter>(header_conv: &mut dyn HeaderEncoder, image: T, tex_pos: Point, clut_pos: Point, color_depth: ColorType, clut_align: ClutAlignment, output: &mut dyn Write) -> Result<(u16, u16), Error> {
let image_size = (image.width(), image.height());
let (width, height) = {
fn return_error(clut_type: u32, div: u32, width: u16, height: u16) -> Result<(u16, u16), Error> {
return Err(Error::from_callback(|| {format!("CLUT {} images require a width divideable by {} (found width: {}/{}={}, height: {})", clut_type, div, width, div, (width as f32/div as f32), height)}));
@ -115,10 +116,10 @@ fn encode<T: PSXImageConverter>(header_conv: &mut dyn HeaderEncoder, image: T, t
tool_helper::raw::write_raw(output, &color)?;
}
Ok(())
Ok(image_size)
}
fn convert_full16(header_conv: &mut dyn HeaderEncoder, input: Input, output: &mut dyn Write, tex_pos: Point) -> Result<(), Error> {
fn convert_full16(header_conv: &mut dyn HeaderEncoder, input: Input, output: &mut dyn Write, tex_pos: Point) -> Result<(u16, u16), Error> {
match ImageReader::new(Cursor::new(tool_helper::input_to_vec(input)?)).with_guessed_format()?.decode() {
Ok(image) => {
match image {
@ -132,7 +133,7 @@ fn convert_full16(header_conv: &mut dyn HeaderEncoder, input: Input, output: &mu
}
}
fn convert_palette_based(header_conv: &mut dyn HeaderEncoder, input: Input, output: &mut dyn Write, tex_pos: Point, clut_pos: Point, color_type: ColorType, clut_align: ClutAlignment, semi_transparent: bool, transparent_palette: bool) -> Result<(), Error> {
fn convert_palette_based(header_conv: &mut dyn HeaderEncoder, input: Input, output: &mut dyn Write, tex_pos: Point, clut_pos: Point, color_type: ColorType, clut_align: ClutAlignment, semi_transparent: bool, transparent_palette: bool) -> Result<(u16, u16), Error> {
match png::Decoder::new(input).read_info() {
Ok(reader) => {
let output_type = {

View File

@ -7,7 +7,7 @@ use tool_helper::{Error, Input};
pub type Arguments = super::args::Arguments;
pub fn convert(args: Arguments, input: Input, output: &mut dyn Write) -> Result<(), Error> {
pub fn convert(args: Arguments, input: Input, output: &mut dyn Write) -> Result<(u16, u16), Error> {
let mut header_conv = Header::default();
match args.color_depth {
ColorType::Full16 => super::convert_full16(&mut header_conv, input, output, Point::default()),

View File

@ -18,7 +18,7 @@ pub struct Arguments {
pub(crate) tex_pos: Point,
}
pub fn convert(args: Arguments, input: Input, output: &mut dyn Write) -> Result<(), Error> {
pub fn convert(args: Arguments, input: Input, output: &mut dyn Write) -> Result<(u16, u16), Error> {
let global_args = args.global;
let mut header_conv = Header::default();

View File

@ -20,7 +20,7 @@ pub enum SubCommands {
Project(project::Arguments),
}
pub fn run_subcommand(compress_lz4: bool, input_path: Option<PathBuf>, output_path: Option<PathBuf>, sub_command: SubCommands) -> Result<(), Error> {
pub fn run_subcommand(compress_lz4: bool, input_path: Option<PathBuf>, output_path: Option<PathBuf>, sub_command: SubCommands) -> Result<Option<(u16, u16)>, Error> {
let mut input = tool_helper::open_input(&input_path)?;
let mut buffer = Vec::<u8>::new();
let mut output_file = tool_helper::open_output(&output_path)?;
@ -34,34 +34,52 @@ pub fn run_subcommand(compress_lz4: bool, input_path: Option<PathBuf>, output_pa
}
};
let cmd_result: Result<(), Error> = {
let cmd_result: Result<Option<(u16, u16)>, Error> = {
match sub_command {
SubCommands::Nothing => nothing::copy(&mut input, dst_buffer),
SubCommands::SimpleTIM(args) => reduced_tim::convert(args, input, dst_buffer),
SubCommands::TIM(args) => tim::convert(args, input, dst_buffer),
SubCommands::VAG(args) => audio::vag::convert(args, &output_path, input, dst_buffer),
SubCommands::XA(args) => audio::xa::convert(args, input, dst_buffer),
SubCommands::Project(args) => project::run_project(input, args, input_path, &output_path),
SubCommands::Nothing => {
nothing::copy(&mut input, dst_buffer)?;
Ok(None)
},
SubCommands::SimpleTIM(args) => {
Ok(Some(reduced_tim::convert(args, input, dst_buffer)?))
},
SubCommands::TIM(args) => {
Ok(Some(tim::convert(args, input, dst_buffer)?))
},
SubCommands::VAG(args) => {
audio::vag::convert(args, &output_path, input, dst_buffer)?;
Ok(None)
}
SubCommands::XA(args) => {
audio::xa::convert(args, input, dst_buffer)?;
Ok(None)
}
SubCommands::Project(args) => {
project::run_project(input, args, input_path, &output_path)?;
Ok(None)
},
}
};
if let Err(cmd_error) = cmd_result {
if let Some(file_path) = output_path {
let _result = std::fs::remove_file(file_path);
match cmd_result {
Ok(cmd_result) => {
// We encoded the file to a temporary buffer and now need to write it
if compress_lz4 {
let buffer = tool_helper::compress::psx_default::lz4(&buffer)?;
output_file.write(&buffer)?;
}
Ok(cmd_result)
}
Err(cmd_error) => {
if let Some(file_path) = output_path {
let _result = std::fs::remove_file(file_path);
}
else {
tool_helper::print_warning("Open stream detected! Incomplete file can not be deleted".to_owned());
else {
tool_helper::print_warning("Open stream detected! Incomplete file can not be deleted".to_owned());
}
Err(cmd_error)
}
return Err(cmd_error);
}
// We encoded the file to a temporary buffer and now need to write it
if compress_lz4 {
let buffer = tool_helper::compress::psx_default::lz4(&buffer)?;
output_file.write(&buffer)?;
}
Ok(())
}

View File

@ -17,7 +17,7 @@ impl CppHeader {
Ok(CppHeader{file_out})
}
pub fn push_job(&mut self, job: &Job) -> Result<(), Error> {
pub fn push_job(&mut self, job: &Job, size: (u16, u16)) -> Result<(), Error> {
let name = job.name.replace('.', "_");
let (clut_x, clut_y) = if let Some(palette_rect) = &job.palette_rect {(palette_rect.pos.x, palette_rect.pos.y)} else {(0, 0)};
@ -25,7 +25,7 @@ impl CppHeader {
writeln!(self.file_out, "\tusing namespace JabyEngine;")?;
writeln!(self.file_out, "")?;
writeln!(self.file_out, "\tstatic constexpr auto tim = SimpleTIM::create({}, {}, {}, {});", job.image_pos.x, job.image_pos.y, clut_x, clut_y)?;
writeln!(self.file_out, "\tstatic constexpr auto size = GPU::SizeI16::create({}, {});", 0, 0)?;
writeln!(self.file_out, "\tstatic constexpr auto size = GPU::SizeI16::create({}, {});", size.0, size.1)?;
writeln!(self.file_out, "}}")?;
Ok(())

View File

@ -25,20 +25,21 @@ pub fn run_project(input: Input, args: Arguments, input_path: Option<PathBuf>, o
let project = serde_json::from_reader::<Input, Project>(input).map_err(|error|{Error::from_text(format!("Reading project failed: {}", error))})?;
let mut hpp_out = CppHeader::new(&args.hpp_out_path)?;
for job in project.jobs {
hpp_out.push_job(&job)?;
for mut job in project.jobs {
let args = to_arguments(&job)?;
let output_path = {
let mut path = output_path.clone();
path.push(job.name);
path.push(&job.name);
path.set_extension("img");
path
};
let sub_command = crate::SubCommands::SimpleTIM(args);
crate::run_subcommand(job.settings.compress, Some(Job::create_file_path(job.file_path, &location_path)), Some(output_path), sub_command)?;
let sub_command = crate::SubCommands::SimpleTIM(args);
let org_img_size = crate::run_subcommand(job.settings.compress, Some(Job::create_file_path(job.file_path, &location_path)), Some(output_path), sub_command)?;
job.file_path = PathBuf::new();
hpp_out.push_job(&job, org_img_size.unwrap_or((0, 0)))?;
}
Ok(())