Make jaby_engine_fconv output lz4 files on request
This commit is contained in:
@@ -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
|
||||
|
@@ -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<T: PSXImageConverter>(image: T, clut_align: ClutAlignment, mut output: Output) -> Result<(), Error> {
|
||||
fn encode<T: PSXImageConverter>(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<T: PSXImageConverter>(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),
|
||||
|
@@ -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<PathBuf>,
|
||||
|
||||
@@ -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::<u8>::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
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user