Introduce PSXImageConverter

This commit is contained in:
jaby 2022-09-22 20:09:19 +02:00
parent bbb2e2688e
commit a52207061f
3 changed files with 68 additions and 3 deletions

View File

@ -0,0 +1,53 @@
use super::types::{Header, PSXImageConverter};
use tool_helper::{Error, Output};
pub struct RgbImage {
image: image::RgbImage
}
impl RgbImage {
pub fn new(image: image::RgbImage) -> RgbImage {
RgbImage{image}
}
}
impl PSXImageConverter for RgbImage {
fn width(&self) -> u32 {
image::RgbImage::width(&self.image)
}
fn height(&self) -> u32 {
image::RgbImage::height(&self.image)
}
}
pub struct RgbaImage {
image: image::RgbaImage
}
impl RgbaImage {
pub fn new(image: image::RgbaImage) -> RgbaImage {
RgbaImage{image}
}
}
impl PSXImageConverter for RgbaImage {
fn width(&self) -> u32 {
image::RgbaImage::width(&self.image)
}
fn height(&self) -> u32 {
image::RgbaImage::height(&self.image)
}
}
pub fn encode<T: PSXImageConverter>(image: T, _: Output) -> Result<(), Error> {
let width = image.width();
let height = image.height();
if !Header::can_encode(width, height) {
return Err(Error::from_text(format!("Image size {}, {} needs to be even", width, height)));
}
Ok(())
}

View File

@ -1,9 +1,11 @@
use clap::{Args, ValueEnum};
use image::io::Reader as ImageReader;
use image::{DynamicImage, io::Reader as ImageReader};
use full16_encoder::{RgbaImage, RgbImage};
use std::io::Cursor;
use tool_helper::{Error, Input, Output};
mod types;
mod full16_encoder;
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
pub enum ColorDepth {
@ -18,11 +20,16 @@ pub struct Arguments {
color_depth: ColorDepth
}
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) => {
println!("Blubb!!");
Ok(())
match image {
DynamicImage::ImageRgb8(image) => full16_encoder::encode(RgbImage::new(image), output),
DynamicImage::ImageRgba8(image) => full16_encoder::encode(RgbaImage::new(image), output),
_ => Err(Error::from_text("Only RGB and RGBA images are supported for 16bit encoding".to_owned()))
}
},
Err(error) => Err(Error::from_error(error))
}

View File

@ -94,4 +94,9 @@ impl Header {
pub fn can_encode(width: u32, height: u32) -> bool {
width & 1 == 0 && height & 1 == 0
}
}
pub trait PSXImageConverter {
fn width(&self) -> u32;
fn height(&self) -> u32;
}