Replace unsafe code with raw trait
This commit is contained in:
parent
513275fba8
commit
a50e0ec544
|
@ -60,22 +60,22 @@ fn encode<T: PSXImageConverter>(image: T, color_depth: ColorType, clut_align: Cl
|
||||||
|
|
||||||
let header = Header::encode(width, height, pal_width, pal_height).ok_or(Error::from_callback(|| {format!("Image size (width: {}, height: {}) needs to be even", width, height)}))?;
|
let header = Header::encode(width, height, pal_width, pal_height).ok_or(Error::from_callback(|| {format!("Image size (width: {}, height: {}) needs to be even", width, height)}))?;
|
||||||
|
|
||||||
tool_helper::raw::write_generic(output, header)?;
|
tool_helper::raw::write_raw(output, &header)?;
|
||||||
if let Some(palette) = palette {
|
if let Some(palette) = palette {
|
||||||
let mut color_count = pal_width*pal_height;
|
let mut color_count = pal_width*pal_height;
|
||||||
for color in palette {
|
for color in palette {
|
||||||
tool_helper::raw::write_generic(output, color.get_raw())?;
|
tool_helper::raw::write_raw(output, color)?;
|
||||||
color_count -= 1;
|
color_count -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
while color_count > 0 {
|
while color_count > 0 {
|
||||||
tool_helper::raw::write_generic(output, PSXColor::black().get_raw())?;
|
tool_helper::raw::write_raw(output, &PSXColor::black())?;
|
||||||
color_count -= 1;
|
color_count -= 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for color in image {
|
for color in image {
|
||||||
tool_helper::raw::write_generic(output, color.get_raw())?;
|
tool_helper::raw::write_raw(output, &color)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use tool_helper::{*, bits::BitRange};
|
use tool_helper::{*, bits::BitRange, raw::RawConversion};
|
||||||
|
|
||||||
#[repr(packed(1))]
|
#[repr(packed(1))]
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
@ -102,10 +102,6 @@ impl Color {
|
||||||
Color{value}
|
Color{value}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn get_raw(&self) -> u16 {
|
|
||||||
self.value.to_le()
|
|
||||||
}
|
|
||||||
|
|
||||||
const fn new(stp: u8, red: u8, green: u8, blue: u8) -> Color {
|
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,
|
let value = set_member_value!(set_member_value!(set_member_value!(set_member_value!(0,
|
||||||
stp, 0, u16),
|
stp, 0, u16),
|
||||||
|
@ -122,6 +118,12 @@ impl Color {
|
||||||
make_member_getter_setter!(blue, Self::COLOR_SHIFT, u16);
|
make_member_getter_setter!(blue, Self::COLOR_SHIFT, u16);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl RawConversion<2> for Color {
|
||||||
|
fn convert_to_raw(&self) -> [u8; 2] {
|
||||||
|
self.value.to_le_bytes()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
impl Header {
|
impl Header {
|
||||||
const TEX_WIDTH_BIT_RANGE: BitRange = BitRange::from_to(0, 8);
|
const TEX_WIDTH_BIT_RANGE: BitRange = BitRange::from_to(0, 8);
|
||||||
|
@ -146,6 +148,12 @@ impl Header {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl RawConversion<4> for Header {
|
||||||
|
fn convert_to_raw(&self) -> [u8; 4] {
|
||||||
|
self.value.to_le_bytes()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub trait PSXImageConverter: std::iter::Iterator<Item = Color> {
|
pub trait PSXImageConverter: std::iter::Iterator<Item = Color> {
|
||||||
fn width(&self) -> u16;
|
fn width(&self) -> u16;
|
||||||
fn height(&self) -> u16;
|
fn height(&self) -> u16;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "tool_helper"
|
name = "tool_helper"
|
||||||
version = "0.8.1"
|
version = "0.9.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
use super::{Error, Write};
|
use super::{Error, Write};
|
||||||
|
|
||||||
pub fn write_generic<T>(output: &mut dyn Write, value: T) -> Result<usize, Error> {
|
pub trait RawConversion<const COUNT:usize> {
|
||||||
let raw = unsafe {std::slice::from_raw_parts(&value as *const _ as *const u8, std::mem::size_of_val(&value))};
|
fn convert_to_raw(&self) -> [u8; COUNT];
|
||||||
|
}
|
||||||
Error::try_or_new(output.write(raw))
|
|
||||||
|
pub fn write_raw<T:RawConversion<COUNT>, const COUNT:usize>(output: &mut dyn Write, value: &T) -> Result<usize, Error> {
|
||||||
|
Error::try_or_new(output.write(&value.convert_to_raw()))
|
||||||
}
|
}
|
Loading…
Reference in New Issue