Add Header basics

This commit is contained in:
jaby 2022-09-21 21:52:21 +02:00
parent 355153176f
commit c82438d297
4 changed files with 100 additions and 55 deletions

View File

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

View File

@ -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 {

View File

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

View File

@ -31,4 +31,5 @@ macro_rules! create_bit_functions {
};
}
create_bit_functions!(u16);
create_bit_functions!(u16);
create_bit_functions!(u32);