Further improvements

This commit is contained in:
Jaby 2025-02-15 17:33:34 +01:00
parent 4f0103e8fa
commit 2398053c61
5 changed files with 92 additions and 102 deletions

View File

@ -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());
}
});
}
}

View File

@ -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<RefCell<MainWindow>>;
type GUIElementsRef = Rc<RefCell<GUIElements>>;
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<SharedPixelBuffer<Rgba8Pixel>> {
let mut pixel_buffer = SharedPixelBuffer::<Rgba8Pixel>::new(VRAM_WIDTH as u32, VRAM_HEIGHT as u32);
let width = pixel_buffer.width();

View File

@ -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<TIMInfo>,
}
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<Image, Error> {
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()
}
}
struct UnaddedTIM {
info: TIMInfo,
image: 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()}
pub fn add_unadded_tim_as(&mut self, _name: &String) -> Result<Image, Error> {
if let Some(unadded_tim) = &self.unadded_tim {
let image = unadded_tim.get_slint_images();
self.unadded_tim = None;
Ok(image)
}
else {
Err(Error::from_str("No data found for loaded image"))
}
}
}

View File

@ -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<Vec<Rgba8Pixel>>,
}
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<Item = &u8>) -> Option<Rgba8Pixel> {
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();
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);
impl TIMInfo {
pub fn from_image(path: &PathBuf) -> Result<TIMInfo, Error> {
fn make_color(iter: &mut dyn std::iter::Iterator<Item = &u8>) -> Option<Rgba8Pixel> {
Some(Rgba8Pixel::new(
*iter.next()?, *iter.next()?, *iter.next()?,
0xFF))
}
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"));}
}
}
Ok(TIMInfo{image_data, palette: Some(palette_colors)})
}
let image = slint::Image::from_rgba8_premultiplied(image_data.clone());
Ok((image, 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())
}
}

View File

@ -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<RefCell<GUIElements>>, logic_ref: Rc<RefC
let gui_elements = gui_elements_ref.borrow();
let logic = logic_ref.clone();
gui_elements.file_tab.on_browse_file(gui_elements_ref.clone(), move |gui_elements, main_window| {
fn show_error_message(text: String) {
MessageDialog::new().set_title("Loading Image failed").set_level(rfd::MessageLevel::Error).set_description(text).show();
}
gui_elements.file_tab.on_browse_file(gui_elements_ref.clone(), move |gui_elements, main_window| -> 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<RefCell<GUIElements>>, logic_ref: Rc<RefC
let file_tab = &gui_elements.file_tab;
let file_name = if let Some(name) = file.file_name() {Some(name.to_string_lossy().to_string())} else {None};
let (image, info) = match load_image(&file) {
Ok((image, info)) => (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<RefCell<GUIElements>>, logic_ref: Rc<RefC
let file_tab = &gui_elements.file_tab;
let file_name = file_tab.get_file_name();
main_tab.add_new_vram_file(&file_name, logic.borrow_mut().add_unadded_tim_as(&file_name));
main_tab.add_new_vram_file(&file_name, logic.borrow_mut().add_unadded_tim_as(&file_name)?);
file_tab.clear_load();
main_window.invoke_change_to_main();
Ok(())
});
}