From b1a71b4e38c4be9caece84ff9b0405436a814bad Mon Sep 17 00:00:00 2001 From: Jaby Date: Sun, 25 Sep 2022 19:32:58 +0200 Subject: [PATCH] Start reading in indexed PNG file --- .../src/images/reduced_tim/color_clut.rs | 52 +++++++++++++++++++ .../src/images/reduced_tim/mod.rs | 37 ++++--------- src/Tools/tool_helper/src/lib.rs | 5 ++ 3 files changed, 68 insertions(+), 26 deletions(-) create mode 100644 src/Tools/jaby_engine_fconv/src/images/reduced_tim/color_clut.rs diff --git a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/color_clut.rs b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/color_clut.rs new file mode 100644 index 00000000..95536848 --- /dev/null +++ b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/color_clut.rs @@ -0,0 +1,52 @@ +use tool_helper::Error; +use super::types::Color; + +pub enum OutputType { + TwoIndex, + OneIndex, +} + +pub struct IndexedImage { + palette: Vec, + data: Vec, + bit_depth: png::BitDepth, + output_type: OutputType, + width: u16, + height: u16, +} + +impl IndexedImage { + pub fn new(mut reader: png::Reader, output_type: OutputType) -> Result { + let action_name = "Creating IndexedImage"; + + let mut buffer = vec![0; reader.output_buffer_size()]; + match reader.next_frame(&mut buffer) { + Ok(info) => { + let width = info.width as u16; + let height = info.height as u16; + let bit_depth = info.bit_depth; + + if info.color_type != png::ColorType::Indexed { + return Err(Error::from_str("PNG file must be indexed").with_action(action_name)); + } + + if bit_depth != png::BitDepth::Four && bit_depth != png::BitDepth::Eight { + Err(Error::from_str("Only 4 and 8bit color depth are supported").with_action(action_name)) + } + + else { + + Ok(IndexedImage{palette: Self::make_palette(), data: buffer, bit_depth, output_type, width, height}) + } + } + + Err(err) => { + Err(Error::from_error(err).with_action(action_name)) + } + } + } + + fn make_palette() -> Vec { + Vec::new() + } +} \ 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 aaf80f67..e3087994 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,13 @@ use clap::{Args, ValueEnum}; use image::{DynamicImage, io::Reader as ImageReader}; +use color_clut::IndexedImage; use color_full16::{RgbaImage, RgbImage}; use std::io::Cursor; use tool_helper::{Error, Input, Output}; use types::{Header, PSXImageConverter}; mod types; +mod color_clut; mod color_full16; #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] @@ -75,34 +77,17 @@ fn convert_full16(input: Input, output: Output) -> Result<(), Error> { } } -fn convert_palette_based(input: Input, _: Output, _: ColorType) -> Result<(), Error> { +fn convert_palette_based(input: Input, _: Output, color_type: ColorType) -> Result<(), Error> { match png::Decoder::new(input).read_info() { - Ok(mut reader) => { - let mut buf = vec![0; reader.output_buffer_size()]; - - loop { - match reader.next_frame(&mut buf) { - Ok(_) => { - println!("Frame: {}", buf.len()); - for byte in &buf { - print!("[{}] ", byte); - } - println!(""); - } - Err(err) => { - println!("Wuff: {}", err); - break; - } + Ok(reader) => { + let output_type = { + match color_type { + ColorType::Clut4 => color_clut::OutputType::TwoIndex, + ColorType::Clut8 => color_clut::OutputType::OneIndex, + _ => return Err(Error::from_str("ColorType not supported")) } - } - - let info = reader.info(); - println!("picture: {} - {}, ({:?}, {:?}) ", info.width, info.height, info.color_type, info.bit_depth); - if let Some(pal) = &info.palette { - println!("Size of PAL: {}", pal.len()); - } - - + }; + let _image = IndexedImage::new(reader, output_type)?; Err(Error::from_str("Under construction")) }, diff --git a/src/Tools/tool_helper/src/lib.rs b/src/Tools/tool_helper/src/lib.rs index bd5ef2d7..7ad5ad19 100644 --- a/src/Tools/tool_helper/src/lib.rs +++ b/src/Tools/tool_helper/src/lib.rs @@ -49,6 +49,11 @@ impl Error { pub fn ok_or_new(option: Option, error_text: F) -> Result where F: Fn () -> String{ Ok(option.ok_or(Error::from_callback(error_text))?) } + + pub fn with_action(mut self, action: &str) -> Error { + self.action = action.to_owned(); + self + } } impl std::fmt::Display for Error {