From d931cd7a615815701a00855592eaf5c3797f9c9f Mon Sep 17 00:00:00 2001 From: jaby Date: Sun, 25 Sep 2022 18:46:15 +0200 Subject: [PATCH] Allow writing clut data --- .../src/images/reduced_tim/color_full16.rs | 8 ++++ .../src/images/reduced_tim/mod.rs | 38 ++++++++++++++++--- .../src/images/reduced_tim/types.rs | 2 + 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/color_full16.rs b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/color_full16.rs index 45cace9f..96b375a2 100644 --- a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/color_full16.rs +++ b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/color_full16.rs @@ -24,6 +24,10 @@ impl PSXImageConverter for RgbImage { fn height(&self) -> u16 { self.height } + + fn get_palette(&self) -> Option> { + None + } } impl std::iter::Iterator for RgbImage { @@ -62,6 +66,10 @@ impl PSXImageConverter for RgbaImage { fn height(&self) -> u16 { self.height } + + fn get_palette(&self) -> Option> { + None + } } impl std::iter::Iterator for RgbaImage { 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 1483ec7a..aaf80f67 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 @@ -15,19 +15,45 @@ pub enum ColorType{ Full16, } +pub enum ClutAlignment { + None, + Linear, + Block +} + #[derive(Args)] pub struct Arguments { #[clap(arg_enum, value_parser)] color_depth: ColorType } +fn encode(image: T, clut_align: ClutAlignment, mut output: Output) -> Result<(), Error> { + let width = image.width(); + let height = image.height(); + let palette = image.get_palette(); + let (pal_width, pal_height) = { + if let Some(palette) = &palette { + match clut_align { + ClutAlignment::None | + ClutAlignment::Linear => (palette.len() as u16, 1u16), + ClutAlignment::Block => (16u16, (palette.len()/16) as u16), + } + } -fn encode(image: T, mut output: Output) -> Result<(), Error> { - let width = image.width(); - let height = image.height(); - let header = Header::encode(width, height, 0, 0).ok_or(Error::from_callback(|| {format!("Image size {}, {} needs to be even", width, height)}))?; + else { + (0u16, 0u16) + } + }; + + let header = Header::encode(width, height, pal_width, pal_height).ok_or(Error::from_callback(|| {format!("Image size {}, {} needs to be even", width, height)}))?; tool_helper::raw::write_generic(&mut output, header)?; + if let Some(palette) = palette { + for color in palette { + tool_helper::raw::write_generic(&mut output, color)?; + } + } + for color in image { tool_helper::raw::write_generic(&mut output, color)?; } @@ -39,8 +65,8 @@ fn convert_full16(input: Input, output: Output) -> Result<(), Error> { match ImageReader::new(Cursor::new(tool_helper::input_to_vec(input)?)).with_guessed_format()?.decode() { Ok(image) => { match image { - DynamicImage::ImageRgb8(image) => encode(RgbImage::new(image), output), - DynamicImage::ImageRgba8(image) => encode(RgbaImage::new(image), output), + DynamicImage::ImageRgb8(image) => encode(RgbImage::new(image), ClutAlignment::None, output), + DynamicImage::ImageRgba8(image) => encode(RgbaImage::new(image), ClutAlignment::None, output), _ => Err(Error::from_str("Only RGB and RGBA images are supported for 16bit encoding")) } diff --git a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/types.rs b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/types.rs index c6732a6b..c0f77afa 100644 --- a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/types.rs +++ b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/types.rs @@ -119,4 +119,6 @@ impl Header { pub trait PSXImageConverter: std::iter::Iterator { fn width(&self) -> u16; fn height(&self) -> u16; + + fn get_palette(&self) -> Option>; } \ No newline at end of file