Allow writing clut data
This commit is contained in:
parent
c6d1726a70
commit
d931cd7a61
|
@ -24,6 +24,10 @@ impl PSXImageConverter for RgbImage {
|
|||
fn height(&self) -> u16 {
|
||||
self.height
|
||||
}
|
||||
|
||||
fn get_palette(&self) -> Option<Vec<Color>> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl std::iter::Iterator for RgbImage {
|
||||
|
@ -62,6 +66,10 @@ impl PSXImageConverter for RgbaImage {
|
|||
fn height(&self) -> u16 {
|
||||
self.height
|
||||
}
|
||||
|
||||
fn get_palette(&self) -> Option<Vec<Color>> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl std::iter::Iterator for RgbaImage {
|
||||
|
|
|
@ -15,19 +15,45 @@ pub enum ColorType{
|
|||
Full16,
|
||||
}
|
||||
|
||||
pub enum ClutAlignment {
|
||||
None,
|
||||
Linear,
|
||||
Block
|
||||
}
|
||||
|
||||
#[derive(Args)]
|
||||
pub struct Arguments {
|
||||
#[clap(arg_enum, value_parser)]
|
||||
color_depth: ColorType
|
||||
}
|
||||
|
||||
fn encode<T: PSXImageConverter>(image: T, clut_align: ClutAlignment, mut output: Output) -> Result<(), Error> {
|
||||
let width = image.width();
|
||||
let height = image.height();
|
||||
let palette = image.get_palette();
|
||||
let (pal_width, pal_height) = {
|
||||
if let Some(palette) = &palette {
|
||||
match clut_align {
|
||||
ClutAlignment::None |
|
||||
ClutAlignment::Linear => (palette.len() as u16, 1u16),
|
||||
ClutAlignment::Block => (16u16, (palette.len()/16) as u16),
|
||||
}
|
||||
}
|
||||
|
||||
fn encode<T: PSXImageConverter>(image: T, mut output: Output) -> Result<(), Error> {
|
||||
let width = image.width();
|
||||
let height = image.height();
|
||||
let header = Header::encode(width, height, 0, 0).ok_or(Error::from_callback(|| {format!("Image size {}, {} needs to be even", width, height)}))?;
|
||||
else {
|
||||
(0u16, 0u16)
|
||||
}
|
||||
};
|
||||
|
||||
let header = Header::encode(width, height, pal_width, pal_height).ok_or(Error::from_callback(|| {format!("Image size {}, {} needs to be even", width, height)}))?;
|
||||
|
||||
tool_helper::raw::write_generic(&mut output, header)?;
|
||||
if let Some(palette) = palette {
|
||||
for color in palette {
|
||||
tool_helper::raw::write_generic(&mut output, color)?;
|
||||
}
|
||||
}
|
||||
|
||||
for color in image {
|
||||
tool_helper::raw::write_generic(&mut output, color)?;
|
||||
}
|
||||
|
@ -39,8 +65,8 @@ 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) => {
|
||||
match image {
|
||||
DynamicImage::ImageRgb8(image) => encode(RgbImage::new(image), output),
|
||||
DynamicImage::ImageRgba8(image) => encode(RgbaImage::new(image), output),
|
||||
DynamicImage::ImageRgb8(image) => encode(RgbImage::new(image), ClutAlignment::None, output),
|
||||
DynamicImage::ImageRgba8(image) => encode(RgbaImage::new(image), ClutAlignment::None, output),
|
||||
|
||||
_ => Err(Error::from_str("Only RGB and RGBA images are supported for 16bit encoding"))
|
||||
}
|
||||
|
|
|
@ -119,4 +119,6 @@ impl Header {
|
|||
pub trait PSXImageConverter: std::iter::Iterator<Item = Color> {
|
||||
fn width(&self) -> u16;
|
||||
fn height(&self) -> u16;
|
||||
|
||||
fn get_palette(&self) -> Option<Vec<Color>>;
|
||||
}
|
Loading…
Reference in New Issue