Basic Color support

This commit is contained in:
jaby 2022-09-21 20:43:08 +02:00
parent 090d0d09a1
commit 355153176f
5 changed files with 78 additions and 7 deletions

View File

@ -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: ;

View File

@ -8,4 +8,5 @@ edition = "2021"
[dependencies]
clap = {version = "*", features = ["derive"]}
image = "*"
paste = "*"
tool_helper = {path = "../tool_helper"}

View File

@ -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);
}

View File

@ -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")),
}
}

View File

@ -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
}
}
};
}