From a926325a4dc864668d478223d5bb2348c3a1ebc8 Mon Sep 17 00:00:00 2001 From: Jaby Date: Wed, 21 Sep 2022 20:43:08 +0200 Subject: [PATCH] Basic Color support --- src/Tools/Tests/Test.mk | 2 +- src/Tools/jaby_engine_fconv/Cargo.toml | 1 + .../src/images/reduced_tim/color.rs | 53 +++++++++++++++++++ .../src/images/reduced_tim/mod.rs | 19 +++++-- src/Tools/tool_helper/src/bits.rs | 10 ++-- 5 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 src/Tools/jaby_engine_fconv/src/images/reduced_tim/color.rs diff --git a/src/Tools/Tests/Test.mk b/src/Tools/Tests/Test.mk index 39bf8afb..08406ceb 100644 --- a/src/Tools/Tests/Test.mk +++ b/src/Tools/Tests/Test.mk @@ -7,6 +7,6 @@ test_cpp_out: always test_jaby_engine_fconv: always @cargo build --manifest-path ../jaby_engine_fconv/Cargo.toml --release - @./../jaby_engine_fconv/target/release/jaby_engine_fconv -o Test_Planschi.bin Test_PNG.PNG simple-tim full16 + @./../jaby_engine_fconv/target/release/jaby_engine_fconv -o Test_Planschi.bin Test_PNG2.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 61ed5822..068865d5 100644 --- a/src/Tools/jaby_engine_fconv/Cargo.toml +++ b/src/Tools/jaby_engine_fconv/Cargo.toml @@ -8,4 +8,5 @@ edition = "2021" [dependencies] clap = {version = "*", features = ["derive"]} image = "*" +paste = "*" tool_helper = {path = "../tool_helper"} \ No newline at end of file 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 new file mode 100644 index 00000000..f9feae0f --- /dev/null +++ b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/color.rs @@ -0,0 +1,53 @@ +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 5ed6b0a4..8048850c 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,6 +3,8 @@ use image::io::Reader as ImageReader; use std::io::Cursor; use tool_helper::{Error, Input, Output}; +mod color; + #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] pub enum ColorDepth { Clut4, @@ -16,8 +18,19 @@ pub struct Arguments { color_depth: ColorDepth } -pub fn convert(_args: Arguments, input: Input, _output: Output) -> Result<(), Error> { - ImageReader::new(Cursor::new(tool_helper::input_to_vec(input)?)); +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) => { + + Ok(()) + }, + Err(error) => Err(Error::from_error(error)) + } +} - Err(Error::not_implemented("convert")) +pub fn convert(args: Arguments, input: Input, output: Output) -> Result<(), Error> { + match args.color_depth { + ColorDepth::Full16 => convert_full16(input, output), + _ => Err(Error::not_implemented("Converting anything else then full16")), + } } \ 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 2e5b53ee..4e15df85 100644 --- a/src/Tools/tool_helper/src/bits.rs +++ b/src/Tools/tool_helper/src/bits.rs @@ -12,17 +12,21 @@ impl BitRange { macro_rules! create_bit_functions { ($type_val:ty) => { paste::item! { - fn [< get_mask_ $type_val >](range: &BitRange) -> $type_val { + const fn [< get_mask_ $type_val >](range: &BitRange) -> $type_val { (1 << range.len) - 1 } - pub fn [< clear_value_ $type_val >](dst: $type_val, range: &BitRange) -> $type_val { + pub const fn [< clear_value_ $type_val >](dst: $type_val, range: &BitRange) -> $type_val { dst & !([< get_mask_ $type_val >](range) << (range.start as $type_val)) } - pub fn [< set_value_ $type_val >](dst: $type_val, value: $type_val, range: &BitRange) -> $type_val { + pub const fn [< set_value_ $type_val >](dst: $type_val, value: $type_val, range: &BitRange) -> $type_val { [< clear_value_ $type_val >](dst, range) | ((value & [< get_mask_ $type_val >](range)) << range.start) } + + pub const fn [< get_value_ $type_val >](src: $type_val, range: &BitRange) -> $type_val { + (src & [< get_mask_ $type_val >](range)) >> range.start + } } }; }