Introduce PSXImageConverter
This commit is contained in:
parent
bbb2e2688e
commit
a52207061f
|
@ -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(())
|
||||||
|
}
|
|
@ -1,9 +1,11 @@
|
||||||
use clap::{Args, ValueEnum};
|
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 std::io::Cursor;
|
||||||
use tool_helper::{Error, Input, Output};
|
use tool_helper::{Error, Input, Output};
|
||||||
|
|
||||||
mod types;
|
mod types;
|
||||||
|
mod full16_encoder;
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
|
||||||
pub enum ColorDepth {
|
pub enum ColorDepth {
|
||||||
|
@ -18,11 +20,16 @@ pub struct Arguments {
|
||||||
color_depth: ColorDepth
|
color_depth: ColorDepth
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn convert_full16(input: Input, output: Output) -> Result<(), Error> {
|
fn convert_full16(input: Input, output: Output) -> Result<(), Error> {
|
||||||
match ImageReader::new(Cursor::new(tool_helper::input_to_vec(input)?)).with_guessed_format()?.decode() {
|
match ImageReader::new(Cursor::new(tool_helper::input_to_vec(input)?)).with_guessed_format()?.decode() {
|
||||||
Ok(image) => {
|
Ok(image) => {
|
||||||
println!("Blubb!!");
|
match image {
|
||||||
Ok(())
|
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))
|
Err(error) => Err(Error::from_error(error))
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,4 +94,9 @@ impl Header {
|
||||||
pub fn can_encode(width: u32, height: u32) -> bool {
|
pub fn can_encode(width: u32, height: u32) -> bool {
|
||||||
width & 1 == 0 && height & 1 == 0
|
width & 1 == 0 && height & 1 == 0
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait PSXImageConverter {
|
||||||
|
fn width(&self) -> u32;
|
||||||
|
fn height(&self) -> u32;
|
||||||
}
|
}
|
Loading…
Reference in New Issue