From 82301cc8a14c4772c7f6bb6013ccb914dd6b013e Mon Sep 17 00:00:00 2001 From: Jaby Date: Wed, 21 Sep 2022 21:52:21 +0200 Subject: [PATCH] Add Header basics --- .../src/images/reduced_tim/color.rs | 53 ---------- .../src/images/reduced_tim/mod.rs | 2 +- .../src/images/reduced_tim/types.rs | 97 +++++++++++++++++++ src/Tools/tool_helper/src/bits.rs | 3 +- 4 files changed, 100 insertions(+), 55 deletions(-) delete mode 100644 src/Tools/jaby_engine_fconv/src/images/reduced_tim/color.rs create mode 100644 src/Tools/jaby_engine_fconv/src/images/reduced_tim/types.rs diff --git a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/color.rs b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/color.rs deleted file mode 100644 index f9feae0f..00000000 --- a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/color.rs +++ /dev/null @@ -1,53 +0,0 @@ -use tool_helper::{*, bits::BitRange}; - -#[repr(packed(1))] -#[allow(dead_code)] -struct Color { - value: u16 -} - -macro_rules! set_member_value { - ($dst:expr, $color:ident, $shift:expr) => { - paste::item! { - bits::set_value_u16($dst, ($color >> $shift) as u16, &Self::[< $color:upper _BIT_RANGE >]) - } - }; -} - -macro_rules! make_member_getter_setter { - ($color:ident, $shift:expr) => { - paste::item! { - pub fn [< set_ $color >](&mut self, $color: u8) { - self.value = set_member_value!(self.value, $color, $shift); - } - - pub fn [< get_ $color >](&self) -> u8 { - (bits::get_value_u16(self.value, &Self::[< $color:upper _BIT_RANGE >]) as u8) << $shift - } - } - }; -} - -#[allow(dead_code)] -impl Color { - const COLOR_SHIFT: u16 = 3; - const STP_BIT_RANGE: BitRange = BitRange::from_to(15, 15); - const RED_BIT_RANGE: BitRange = BitRange::from_to(0, 4); - const GREEN_BIT_RANGE: BitRange = BitRange::from_to(5, 9); - const BLUE_BIT_RANGE: BitRange = BitRange::from_to(10, 14); - - pub const fn new(stp: u8, red: u8, green: u8, blue: u8) -> Color { - let value = set_member_value!(set_member_value!(set_member_value!(set_member_value!(0, - stp, 0), - red, Self::COLOR_SHIFT), - green, Self::COLOR_SHIFT), - blue, Self::COLOR_SHIFT); - - Color{value} - } - - make_member_getter_setter!(stp, 0); - make_member_getter_setter!(red, Self::COLOR_SHIFT); - make_member_getter_setter!(green, Self::COLOR_SHIFT); - make_member_getter_setter!(blue, Self::COLOR_SHIFT); -} \ No newline at end of file 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 8048850c..951b2306 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 @@ -3,7 +3,7 @@ use image::io::Reader as ImageReader; use std::io::Cursor; use tool_helper::{Error, Input, Output}; -mod color; +mod types; #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] pub enum ColorDepth { 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 new file mode 100644 index 00000000..b7b6e726 --- /dev/null +++ b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/types.rs @@ -0,0 +1,97 @@ +use tool_helper::{*, bits::BitRange}; + +#[repr(packed(1))] +#[allow(dead_code)] +pub struct Color { + value: u16 +} + +#[repr(packed(1))] +#[allow(dead_code)] +pub struct Header { + value: u32 +} + +macro_rules! set_member_value { + ($dst:expr, $color:ident, $shift:expr, $bits:ident) => { + paste::item! { + bits::[< set_value_ $bits >]($dst, ($color >> $shift) as $bits, &Self::[< $color:upper _BIT_RANGE >]) + } + }; +} + +macro_rules! make_member_getter_setter { + ($color:ident, $shift:expr, $bits:ident) => { + paste::item! { + pub fn [< set_ $color >](&mut self, $color: u8) { + self.value = set_member_value!(self.value, $color, $shift, $bits); + } + + pub fn [< get_ $color >](&self) -> u8 { + (bits::[< get_value_ $bits >](self.value, &Self::[< $color:upper _BIT_RANGE >]) as u8) << $shift + } + } + }; +} + +#[allow(dead_code)] +impl Color { + const COLOR_SHIFT: u16 = 3; + const STP_BIT_RANGE: BitRange = BitRange::from_to(15, 15); + const RED_BIT_RANGE: BitRange = BitRange::from_to(0, 4); + const GREEN_BIT_RANGE: BitRange = BitRange::from_to(5, 9); + const BLUE_BIT_RANGE: BitRange = BitRange::from_to(10, 14); + + pub const fn transparent() -> Color { + Color::new(0, 0, 0, 0) + } + + pub const fn non_transparent(red: u8, green: u8, blue: u8) -> Color { + Color::new(0, red, green, blue) + } + + pub const fn semi_transparent(red: u8, green: u8, blue: u8) -> Color { + Color::new(1, red, green, blue) + } + + pub const fn black() -> Color { + Color::new(1, 0, 0, 0) + } + + const fn new(stp: u8, red: u8, green: u8, blue: u8) -> Color { + let value = set_member_value!(set_member_value!(set_member_value!(set_member_value!(0, + stp, 0, u16), + red, Self::COLOR_SHIFT, u16), + green, Self::COLOR_SHIFT, u16), + blue, Self::COLOR_SHIFT, u16); + + Color{value} + } + + make_member_getter_setter!(stp, 0, u16); + make_member_getter_setter!(red, Self::COLOR_SHIFT, u16); + make_member_getter_setter!(green, Self::COLOR_SHIFT, u16); + make_member_getter_setter!(blue, Self::COLOR_SHIFT, u16); +} + +#[allow(dead_code)] +impl Header { + const TEX_WIDTH_BIT_RANGE: BitRange = BitRange::from_to(0, 8); + const TEX_HEIGHT_BIT_RANGE: BitRange = BitRange::from_to(9, 16); + const CLUT_WIDTH_BIT_RANGE: BitRange = BitRange::from_to(17, 22); + const CLUT_HEIGHT_BIT_RANGE: BitRange = BitRange::from_to(23, 31); + + pub fn new(tex_width: u16, tex_height: u16, clut_width: u16, clut_height: u16) -> Header { + let value = set_member_value!(set_member_value!(set_member_value!(set_member_value!(0, + tex_width, 1, u32), + tex_height, 1, u32), + clut_width, 4, u32), + clut_height, 0, u32); + + Header{value} + } + + pub fn can_encode(width: u32, height: u32) -> bool { + width & 1 == 0 && height & 1 == 0 + } +} \ No newline at end of file diff --git a/src/Tools/tool_helper/src/bits.rs b/src/Tools/tool_helper/src/bits.rs index 4e15df85..47665de7 100644 --- a/src/Tools/tool_helper/src/bits.rs +++ b/src/Tools/tool_helper/src/bits.rs @@ -31,4 +31,5 @@ macro_rules! create_bit_functions { }; } -create_bit_functions!(u16); \ No newline at end of file +create_bit_functions!(u16); +create_bit_functions!(u32); \ No newline at end of file