From 1398cd62ef5fa3004ed00b54bf4837fb54e1458d Mon Sep 17 00:00:00 2001 From: Jaby Date: Fri, 23 Sep 2022 22:01:52 +0200 Subject: [PATCH] Write clut16 --- .../{full16_encoder.rs => color_full16.rs} | 13 +---------- .../src/images/reduced_tim/mod.rs | 22 +++++++++++++++---- src/Tools/tool_helper/src/lib.rs | 1 + src/Tools/tool_helper/src/raw.rs | 7 ++++++ 4 files changed, 27 insertions(+), 16 deletions(-) rename src/Tools/jaby_engine_fconv/src/images/reduced_tim/{full16_encoder.rs => color_full16.rs} (81%) create mode 100644 src/Tools/tool_helper/src/raw.rs diff --git a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/full16_encoder.rs b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/color_full16.rs similarity index 81% rename from src/Tools/jaby_engine_fconv/src/images/reduced_tim/full16_encoder.rs rename to src/Tools/jaby_engine_fconv/src/images/reduced_tim/color_full16.rs index 326c58af..45cace9f 100644 --- a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/full16_encoder.rs +++ b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/color_full16.rs @@ -1,5 +1,4 @@ -use super::types::{Color, Header, PSXImageConverter}; -use tool_helper::{Error, Output}; +use super::types::{Color, PSXImageConverter}; pub struct RgbImage { raw_data: std::vec::IntoIter, @@ -82,14 +81,4 @@ impl std::iter::Iterator for RgbaImage { } ) } -} - -pub fn encode(image: T, _: 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)})); - - for _ in image {} - - Ok(()) } \ No newline at end of file diff --git a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs index 5b37cf8f..3de7a171 100644 --- a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs +++ b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs @@ -1,11 +1,12 @@ use clap::{Args, ValueEnum}; use image::{DynamicImage, io::Reader as ImageReader}; -use full16_encoder::{RgbaImage, RgbImage}; +use color_full16::{RgbaImage, RgbImage}; use std::io::Cursor; use tool_helper::{Error, Input, Output}; +use types::{Header, PSXImageConverter}; mod types; -mod full16_encoder; +mod color_full16; #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] pub enum ColorDepth { @@ -21,12 +22,25 @@ pub struct Arguments { } +fn encode(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)}))?; + + tool_helper::raw::write_generic(&mut output, header)?; + for color in image { + tool_helper::raw::write_generic(&mut output, color)?; + } + + Ok(()) +} + 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) => full16_encoder::encode(RgbImage::new(image), output), - DynamicImage::ImageRgba8(image) => full16_encoder::encode(RgbaImage::new(image), output), + DynamicImage::ImageRgb8(image) => encode(RgbImage::new(image), output), + DynamicImage::ImageRgba8(image) => encode(RgbaImage::new(image), output), _ => Err(Error::from_text("Only RGB and RGBA images are supported for 16bit encoding".to_owned())) } diff --git a/src/Tools/tool_helper/src/lib.rs b/src/Tools/tool_helper/src/lib.rs index 16eee015..904a8475 100644 --- a/src/Tools/tool_helper/src/lib.rs +++ b/src/Tools/tool_helper/src/lib.rs @@ -1,6 +1,7 @@ use std::{boxed::Box, io::{Read, Write}, path::PathBuf}; pub mod bits; +pub mod raw; pub type Output = Box; pub type Input = Box; diff --git a/src/Tools/tool_helper/src/raw.rs b/src/Tools/tool_helper/src/raw.rs new file mode 100644 index 00000000..7ae1dd1e --- /dev/null +++ b/src/Tools/tool_helper/src/raw.rs @@ -0,0 +1,7 @@ +use super::{Error, Output}; + +pub fn write_generic(output: &mut Output, value: T) -> Result { + let raw = unsafe {std::slice::from_raw_parts(&value as *const _ as *const u8, std::mem::size_of_val(&value))}; + + Error::try_or_new(output.write(raw)) +} \ No newline at end of file