From 4917d0727039a9438ebdecb812427f60594e620a Mon Sep 17 00:00:00 2001 From: Jaby Date: Tue, 27 Dec 2022 20:58:43 +0100 Subject: [PATCH] Make jaby_engine_fconv output lz4 files on request --- src/Tools/Tests/Test.mk | 6 +-- src/Tools/jaby_engine_fconv/Cargo.toml | 2 +- .../src/images/reduced_tim/mod.rs | 20 +++++----- src/Tools/jaby_engine_fconv/src/main.rs | 39 +++++++++++++++++-- src/Tools/tool_helper/src/raw.rs | 4 +- 5 files changed, 51 insertions(+), 20 deletions(-) diff --git a/src/Tools/Tests/Test.mk b/src/Tools/Tests/Test.mk index becb8b7a..cfde7ba9 100644 --- a/src/Tools/Tests/Test.mk +++ b/src/Tools/Tests/Test.mk @@ -1,10 +1,10 @@ export PATH := $(HOME)/.cargo/bin/:$(PATH) test_cpp_out: always - @echo "Planschbecken" | ./../cpp_out/target/x86_64-unknown-linux-musl/release/cpp_out --name Dino -o "Test_Planschbecken.cpp" - @echo "Planschbecken" | ./../cpp_out/target/x86_64-unknown-linux-musl/release/cpp_out --name Dino -o "Test_Planschbecken.hpp" + @echo "Planschbecken" | ./../cpp_out/target/x86_64-unknown-linux-gnu/release/cpp_out --name Dino -o "Test_Planschbecken.cpp" + @echo "Planschbecken" | ./../cpp_out/target/x86_64-unknown-linux-gnu/release/cpp_out --name Dino -o "Test_Planschbecken.hpp" test_jaby_engine_fconv: always # @./../jaby_engine_fconv/target/x86_64-unknown-linux-musl/release/jaby_engine_fconv -o Test_Planschi.bin Test_PNG.PNG simple-tim full16 - @./../jaby_engine_fconv/target/x86_64-unknown-linux-musl/release/jaby_engine_fconv -o Test_Planschi.bin Test_PNG_pal.PNG simple-tim clut8 + @./../jaby_engine_fconv/target/x86_64-unknown-linux-gnu/release/jaby_engine_fconv --lz4 -o Test_Planschi.bin ../../Library/ressources/Splash.PNG simple-tim full16 always: ; \ No newline at end of file diff --git a/src/Tools/jaby_engine_fconv/Cargo.toml b/src/Tools/jaby_engine_fconv/Cargo.toml index 12a3cf27..1f992c77 100644 --- a/src/Tools/jaby_engine_fconv/Cargo.toml +++ b/src/Tools/jaby_engine_fconv/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "jaby_engine_fconv" -version = "0.1.0" +version = "0.1.2" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs index fdf18049..3e4c6715 100644 --- a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs +++ b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs @@ -2,8 +2,8 @@ use clap::{Args, ValueEnum}; use image::{DynamicImage, io::Reader as ImageReader}; use color_clut::IndexedImage; use color_full16::{RgbaImage, RgbImage}; -use std::io::Cursor; -use tool_helper::{Error, Input, Output}; +use std::io::{Cursor, Write}; +use tool_helper::{Error, Input}; use types::{Header, Color as PSXColor, PSXImageConverter}; mod types; @@ -33,7 +33,7 @@ pub struct Arguments { clut_align: ClutAlignment } -fn encode(image: T, clut_align: ClutAlignment, mut output: Output) -> Result<(), Error> { +fn encode(image: T, clut_align: ClutAlignment, output: &mut dyn Write) -> Result<(), Error> { let width = image.width(); let height = image.height(); let palette = image.get_palette(); @@ -53,28 +53,28 @@ fn encode(image: T, clut_align: ClutAlignment, mut output: let header = Header::encode(width, height, pal_width, pal_height).ok_or(Error::from_callback(|| {format!("Image size (width: {}, height: {}) needs to be even", width, height)}))?; - tool_helper::raw::write_generic(&mut output, header)?; + tool_helper::raw::write_generic(output, header)?; if let Some(palette) = palette { let mut color_count = pal_width*pal_height; for color in palette { - tool_helper::raw::write_generic(&mut output, color)?; + tool_helper::raw::write_generic(output, color)?; color_count -= 1; } while color_count > 0 { - tool_helper::raw::write_generic(&mut output, PSXColor::black())?; + tool_helper::raw::write_generic(output, PSXColor::black())?; color_count -= 1; } } for color in image { - tool_helper::raw::write_generic(&mut output, color)?; + tool_helper::raw::write_generic(output, color)?; } Ok(()) } -fn convert_full16(input: Input, output: Output) -> Result<(), Error> { +fn convert_full16(input: Input, output: &mut dyn Write) -> Result<(), Error> { match ImageReader::new(Cursor::new(tool_helper::input_to_vec(input)?)).with_guessed_format()?.decode() { Ok(image) => { match image { @@ -88,7 +88,7 @@ fn convert_full16(input: Input, output: Output) -> Result<(), Error> { } } -fn convert_palette_based(input: Input, output: Output, color_type: ColorType, clut_align: ClutAlignment) -> Result<(), Error> { +fn convert_palette_based(input: Input, output: &mut dyn Write, color_type: ColorType, clut_align: ClutAlignment) -> Result<(), Error> { match png::Decoder::new(input).read_info() { Ok(reader) => { let output_type = { @@ -104,7 +104,7 @@ fn convert_palette_based(input: Input, output: Output, color_type: ColorType, cl } } -pub fn convert(args: Arguments, input: Input, output: Output) -> Result<(), Error> { +pub fn convert(args: Arguments, input: Input, output: &mut dyn Write) -> Result<(), Error> { match args.color_depth { ColorType::Full16 => convert_full16(input, output), _ => convert_palette_based(input, output, args.color_depth, args.clut_align), diff --git a/src/Tools/jaby_engine_fconv/src/main.rs b/src/Tools/jaby_engine_fconv/src/main.rs index 98bb6036..fdcf55ed 100644 --- a/src/Tools/jaby_engine_fconv/src/main.rs +++ b/src/Tools/jaby_engine_fconv/src/main.rs @@ -6,6 +6,9 @@ use tool_helper::Error; #[derive(Parser)] #[clap(about = "Converts files to various JabyEngine related file formats", long_about = None)] struct CommandLine { + #[clap(long="lz4", default_value_t=false)] + compress_lz4: bool, + #[clap(short='o')] output_file: Option, @@ -24,14 +27,42 @@ enum SubCommands { fn run_main() -> Result<(), Error> { match CommandLine::try_parse() { Ok(cmd) => { - let input = tool_helper::open_input(cmd.input_file)?; - let output = tool_helper::open_output(cmd.output_file)?; + let input = tool_helper::open_input(cmd.input_file)?; + let mut buffer = Vec::::new(); + let mut output_file = tool_helper::open_output(cmd.output_file)?; + let dst_buffer = { + if cmd.compress_lz4 { + &mut buffer as &mut dyn std::io::Write + } + + else { + &mut output_file as &mut dyn std::io::Write + } + }; match cmd.sub_command { - SubCommands::SimpleTIM(args) => reduced_tim::convert(args, input, output), + SubCommands::SimpleTIM(args) => { + reduced_tim::convert(args, input, dst_buffer)?; + } } + + // We encoded the file to a temporary buffer and now need to write it + if cmd.compress_lz4 { + println!("Buffer-Size: {} ({} Sectors)", buffer.len(), (buffer.len() + 2047)/2048); + let buffer = tool_helper::compress::lz4(&buffer, 16)?; + println!("New buffer-Size: {} ({} Sectors)", buffer.len(), (buffer.len() + 2047)/2048); + + output_file.write(&buffer)?; + } + + Ok(()) }, - Err(error) => Err(Error::from_error(error)) + Err(error) => Err({ + let mut error = Error::from_error(error); + + error.exit_code = 0; + error + }) } } diff --git a/src/Tools/tool_helper/src/raw.rs b/src/Tools/tool_helper/src/raw.rs index 7ae1dd1e..3350213a 100644 --- a/src/Tools/tool_helper/src/raw.rs +++ b/src/Tools/tool_helper/src/raw.rs @@ -1,6 +1,6 @@ -use super::{Error, Output}; +use super::{Error, Write}; -pub fn write_generic(output: &mut Output, value: T) -> Result { +pub fn write_generic(output: &mut dyn Write, value: T) -> Result { let raw = unsafe {std::slice::from_raw_parts(&value as *const _ as *const u8, std::mem::size_of_val(&value))}; Error::try_or_new(output.write(raw))