From 2398053c61edf60bc2f0e5f456709ff993087712 Mon Sep 17 00:00:00 2001 From: Jaby Date: Sat, 15 Feb 2025 17:33:34 +0100 Subject: [PATCH] Further improvements --- src/Tools/tim_tool/src/gui/file_tab.rs | 15 ++-- src/Tools/tim_tool/src/gui/mod.rs | 5 ++ src/Tools/tim_tool/src/logic/mod.rs | 48 ++++++------- src/Tools/tim_tool/src/logic/tim.rs | 95 +++++++++++++------------- src/Tools/tim_tool/src/main.rs | 31 +++------ 5 files changed, 92 insertions(+), 102 deletions(-) diff --git a/src/Tools/tim_tool/src/gui/file_tab.rs b/src/Tools/tim_tool/src/gui/file_tab.rs index 32c09761..02b1c9c3 100644 --- a/src/Tools/tim_tool/src/gui/file_tab.rs +++ b/src/Tools/tim_tool/src/gui/file_tab.rs @@ -1,6 +1,7 @@ use crate::MainWindow; -use super::{GUIElements, GUIElementsRef, MainWindowRef}; +use super::{GUIElements, GUIElementsRef, MainWindowRef, display_error}; use slint::Image; +use tool_helper::Error; pub struct FileTab { main_window: MainWindowRef @@ -38,21 +39,25 @@ impl FileTab { self.main_window.borrow().get_file_tab_image_name().to_string() } - pub fn on_browse_file(&self, gui_elements: GUIElementsRef, mut function: impl FnMut(&mut GUIElements, &MainWindow) + 'static) { + pub fn on_browse_file(&self, gui_elements: GUIElementsRef, mut function: impl FnMut(&mut GUIElements, &MainWindow) -> Result<(), Error> + 'static) { let main_window_cloned = self.main_window.clone(); let gui_cloned = gui_elements.clone(); self.main_window.borrow().on_file_tab_browse_convert_image(move || { - function(&mut gui_cloned.borrow_mut(), &main_window_cloned.borrow()); + if let Err(error) = function(&mut gui_cloned.borrow_mut(), &main_window_cloned.borrow()) { + display_error("Loadind file failed", &error.to_string()); + } }); } - pub fn on_add_image(&self, gui_elements: GUIElementsRef, mut function: impl FnMut(&mut GUIElements, &MainWindow) + 'static) { + pub fn on_add_image(&self, gui_elements: GUIElementsRef, mut function: impl FnMut(&mut GUIElements, &MainWindow) -> Result<(), Error> + 'static) { let main_window_cloned = self.main_window.clone(); let gui_cloned = gui_elements.clone(); self.main_window.borrow().on_file_tab_add_convert_image(move || { - function(&mut gui_cloned.borrow_mut(), &main_window_cloned.borrow()); + if let Err(error) = function(&mut gui_cloned.borrow_mut(), &main_window_cloned.borrow()) { + display_error("Adding file failed", &error.to_string()); + } }); } } \ No newline at end of file diff --git a/src/Tools/tim_tool/src/gui/mod.rs b/src/Tools/tim_tool/src/gui/mod.rs index 163db6ce..5be11d69 100644 --- a/src/Tools/tim_tool/src/gui/mod.rs +++ b/src/Tools/tim_tool/src/gui/mod.rs @@ -3,6 +3,7 @@ mod gui_elements; mod main_tab; use crate::MainWindow; +use rfd::MessageDialog; use std::{cell::RefCell, rc::Rc}; use slint::{Rgba8Pixel, SharedPixelBuffer}; use tiny_skia::{Rect, Transform}; @@ -15,6 +16,10 @@ pub const VRAM_HEIGHT:usize = 512; type MainWindowRef = Rc>; type GUIElementsRef = Rc>; +fn display_error(title: &str, text: &String) { + MessageDialog::new().set_title(title).set_level(rfd::MessageLevel::Error).set_description(text).show(); +} + fn create_vram_bg(rect_width: u32, rect_height: u32) -> Option> { let mut pixel_buffer = SharedPixelBuffer::::new(VRAM_WIDTH as u32, VRAM_HEIGHT as u32); let width = pixel_buffer.width(); diff --git a/src/Tools/tim_tool/src/logic/mod.rs b/src/Tools/tim_tool/src/logic/mod.rs index a6b0d99f..aba1c083 100644 --- a/src/Tools/tim_tool/src/logic/mod.rs +++ b/src/Tools/tim_tool/src/logic/mod.rs @@ -1,45 +1,37 @@ pub mod tim; + +use std::path::PathBuf; use slint::Image; use tim::TIMInfo; +use tool_helper::Error; pub struct Logic { - unadded_tim: UnaddedTIM, + unadded_tim: Option, } impl Logic { pub fn new() -> Logic { - Logic{unadded_tim: UnaddedTIM::empty()} + Logic{unadded_tim: None} } - pub fn set_unadded_tim(&mut self, info: TIMInfo, image: Image) { - self.unadded_tim = UnaddedTIM::new(info, image); + pub fn set_unadded_tim(&mut self, path: &PathBuf) -> Result { + let tim_info = TIMInfo::from_image(path)?; + let image = tim_info.get_slint_images(); + + self.unadded_tim = Some(tim_info); + Ok(image) } - pub fn add_unadded_tim_as(&mut self, _name: &String) -> Image { - let _info = self.unadded_tim.take_info(); - self.unadded_tim.take_image() - } -} + pub fn add_unadded_tim_as(&mut self, _name: &String) -> Result { + if let Some(unadded_tim) = &self.unadded_tim { + let image = unadded_tim.get_slint_images(); -struct UnaddedTIM { - info: TIMInfo, - image: Image, -} + self.unadded_tim = None; + Ok(image) + } -impl UnaddedTIM { - pub fn new(info: TIMInfo, image: Image) -> UnaddedTIM { - UnaddedTIM{info, image} - } - - pub fn take_info(&mut self) -> TIMInfo { - std::mem::take(&mut self.info) - } - - pub fn take_image(&mut self) -> Image { - std::mem::take(&mut self.image) - } - - fn empty() -> UnaddedTIM { - UnaddedTIM{info: TIMInfo::default(), image: Image::default()} + else { + Err(Error::from_str("No data found for loaded image")) + } } } \ No newline at end of file diff --git a/src/Tools/tim_tool/src/logic/tim.rs b/src/Tools/tim_tool/src/logic/tim.rs index 5487e39e..bfa96882 100644 --- a/src/Tools/tim_tool/src/logic/tim.rs +++ b/src/Tools/tim_tool/src/logic/tim.rs @@ -1,4 +1,4 @@ -use std::{default, fmt::format, fs::File, path::PathBuf}; +use std::{fs::File, path::PathBuf}; use slint::{Rgba8Pixel, SharedPixelBuffer}; use tool_helper::Error; @@ -7,57 +7,56 @@ pub struct TIMInfo { palette: Option>, } -impl std::default::Default for TIMInfo { - fn default() -> Self { - TIMInfo{image_data: SharedPixelBuffer::new(1, 1), palette: None} - } -} - -pub fn load_image(path: &PathBuf) -> Result<(slint::Image, TIMInfo), Error> { - fn make_color(iter: &mut dyn std::iter::Iterator) -> Option { - Some(Rgba8Pixel::new( - *iter.next()?, *iter.next()?, *iter.next()?, - 0xFF)) - } - - let mut reader = png::Decoder::new(File::open(path)?).read_info().or_else(|error| {Err(Error::from_error(error))})?; - let info = reader.info().clone(); +impl TIMInfo { + pub fn from_image(path: &PathBuf) -> Result { + fn make_color(iter: &mut dyn std::iter::Iterator) -> Option { + Some(Rgba8Pixel::new( + *iter.next()?, *iter.next()?, *iter.next()?, + 0xFF)) + } - let mut buffer = vec![0; reader.output_buffer_size()]; - let frame_info = reader.next_frame(&mut buffer).or_else(|error| {Err(Error::from_error(error))})?; - let bit_depth = frame_info.bit_depth; - let mut image_data = SharedPixelBuffer::new(frame_info.width, frame_info.height); - - if info.color_type == png::ColorType::Indexed { - let palette = info.palette.ok_or(Error::from_str("Found indexed PNG without palette"))?; - let mut palette_colors = Vec::new(); - let mut iter = palette.iter(); - - while let Some(color) = make_color(&mut iter) { - palette_colors.push(color); - } - - let mut dst_pixels = image_data.make_mut_slice(); - for byte in buffer.into_iter() { - match bit_depth { - png::BitDepth::Four => { - dst_pixels[0] = palette_colors[(byte >> 4) as usize]; - dst_pixels[1] = palette_colors[(byte & 0xF) as usize]; - dst_pixels = &mut dst_pixels[2..]; - }, - png::BitDepth::Eight => { - dst_pixels[0] = palette_colors[byte as usize]; - dst_pixels = &mut dst_pixels[1..]; - }, - _ => {return Err(Error::from_str("Only 4 and 8bit color depth are supported"));} + let mut reader = png::Decoder::new(File::open(path)?).read_info().or_else(|error| {Err(Error::from_error(error))})?; + let info = reader.info().clone(); + + let mut buffer = vec![0; reader.output_buffer_size()]; + let frame_info = reader.next_frame(&mut buffer).or_else(|error| {Err(Error::from_error(error))})?; + let bit_depth = frame_info.bit_depth; + let mut image_data = SharedPixelBuffer::new(frame_info.width, frame_info.height); + + if info.color_type == png::ColorType::Indexed { + let palette = info.palette.ok_or(Error::from_str("Found indexed PNG without palette"))?; + let mut palette_colors = Vec::new(); + let mut iter = palette.iter(); + + while let Some(color) = make_color(&mut iter) { + palette_colors.push(color); + } + + let mut dst_pixels = image_data.make_mut_slice(); + for byte in buffer.into_iter() { + match bit_depth { + png::BitDepth::Four => { + dst_pixels[0] = palette_colors[(byte >> 4) as usize]; + dst_pixels[1] = palette_colors[(byte & 0xF) as usize]; + dst_pixels = &mut dst_pixels[2..]; + }, + png::BitDepth::Eight => { + dst_pixels[0] = palette_colors[byte as usize]; + dst_pixels = &mut dst_pixels[1..]; + }, + _ => {return Err(Error::from_str("Only 4 and 8bit color depth are supported"));} + } } - } - let image = slint::Image::from_rgba8_premultiplied(image_data.clone()); - Ok((image, TIMInfo{image_data, palette: Some(palette_colors)})) + Ok(TIMInfo{image_data, palette: Some(palette_colors)}) + } + + else { + Err(Error::not_implemented("Support for non indexed images")) + } } - else { - Err(Error::not_implemented("Support for non indexed images")) + pub fn get_slint_images(&self) -> slint::Image { + slint::Image::from_rgba8_premultiplied(self.image_data.clone()) } } \ No newline at end of file diff --git a/src/Tools/tim_tool/src/main.rs b/src/Tools/tim_tool/src/main.rs index aa2c5882..086a6cbf 100644 --- a/src/Tools/tim_tool/src/main.rs +++ b/src/Tools/tim_tool/src/main.rs @@ -3,10 +3,11 @@ mod gui; use gui::{GUIElements, VRAM_WIDTH, VRAM_HEIGHT}; -use rfd::{FileDialog, MessageDialog}; +use rfd::FileDialog; use std::{cell::RefCell, rc::Rc}; use slint::SharedString; -use tim_tool::logic::{tim::load_image, Logic}; +use tim_tool::logic::Logic; +use tool_helper::Error; slint::include_modules!(); @@ -41,11 +42,7 @@ fn setup_file_tab(gui_elements_ref: Rc>, logic_ref: Rc Result<(), Error> { let file = FileDialog::new() .add_filter("PNG image (.png)", &["png"]) .set_title("PNG image file") @@ -59,25 +56,16 @@ fn setup_file_tab(gui_elements_ref: Rc>, logic_ref: Rc (image, info), - Err(error) => { - file_tab.clear_load(); - show_error_message(error.to_string()); - return; - } - }; + let image = logic.borrow_mut().set_unadded_tim(&file)?; let img_size = image.size(); if img_size.width > VRAM_WIDTH as u32 || img_size.height > VRAM_HEIGHT as u32 { - file_tab.clear_load(); - show_error_message(format!("Image size ({}; {}) is to big for VRAM ({}, {})", img_size.width, img_size.height, VRAM_WIDTH, VRAM_HEIGHT)); - return; + return Err(Error::from_text(format!("Image size ({}; {}) is to big for VRAM ({}, {})", img_size.width, img_size.height, VRAM_WIDTH, VRAM_HEIGHT))); } - - logic.borrow_mut().set_unadded_tim(info, image.clone()); file_tab.update_new_load(file_name, image); } + + Ok(()) }); let logic = logic_ref.clone(); @@ -86,9 +74,10 @@ fn setup_file_tab(gui_elements_ref: Rc>, logic_ref: Rc