Further improvements
This commit is contained in:
parent
4f0103e8fa
commit
2398053c61
|
@ -1,6 +1,7 @@
|
||||||
use crate::MainWindow;
|
use crate::MainWindow;
|
||||||
use super::{GUIElements, GUIElementsRef, MainWindowRef};
|
use super::{GUIElements, GUIElementsRef, MainWindowRef, display_error};
|
||||||
use slint::Image;
|
use slint::Image;
|
||||||
|
use tool_helper::Error;
|
||||||
|
|
||||||
pub struct FileTab {
|
pub struct FileTab {
|
||||||
main_window: MainWindowRef
|
main_window: MainWindowRef
|
||||||
|
@ -38,21 +39,25 @@ impl FileTab {
|
||||||
self.main_window.borrow().get_file_tab_image_name().to_string()
|
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 main_window_cloned = self.main_window.clone();
|
||||||
let gui_cloned = gui_elements.clone();
|
let gui_cloned = gui_elements.clone();
|
||||||
|
|
||||||
self.main_window.borrow().on_file_tab_browse_convert_image(move || {
|
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 main_window_cloned = self.main_window.clone();
|
||||||
let gui_cloned = gui_elements.clone();
|
let gui_cloned = gui_elements.clone();
|
||||||
|
|
||||||
self.main_window.borrow().on_file_tab_add_convert_image(move || {
|
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());
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -3,6 +3,7 @@ mod gui_elements;
|
||||||
mod main_tab;
|
mod main_tab;
|
||||||
|
|
||||||
use crate::MainWindow;
|
use crate::MainWindow;
|
||||||
|
use rfd::MessageDialog;
|
||||||
use std::{cell::RefCell, rc::Rc};
|
use std::{cell::RefCell, rc::Rc};
|
||||||
use slint::{Rgba8Pixel, SharedPixelBuffer};
|
use slint::{Rgba8Pixel, SharedPixelBuffer};
|
||||||
use tiny_skia::{Rect, Transform};
|
use tiny_skia::{Rect, Transform};
|
||||||
|
@ -15,6 +16,10 @@ pub const VRAM_HEIGHT:usize = 512;
|
||||||
type MainWindowRef = Rc<RefCell<MainWindow>>;
|
type MainWindowRef = Rc<RefCell<MainWindow>>;
|
||||||
type GUIElementsRef = Rc<RefCell<GUIElements>>;
|
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>> {
|
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 mut pixel_buffer = SharedPixelBuffer::<Rgba8Pixel>::new(VRAM_WIDTH as u32, VRAM_HEIGHT as u32);
|
||||||
let width = pixel_buffer.width();
|
let width = pixel_buffer.width();
|
||||||
|
|
|
@ -1,45 +1,37 @@
|
||||||
pub mod tim;
|
pub mod tim;
|
||||||
|
|
||||||
|
use std::path::PathBuf;
|
||||||
use slint::Image;
|
use slint::Image;
|
||||||
use tim::TIMInfo;
|
use tim::TIMInfo;
|
||||||
|
use tool_helper::Error;
|
||||||
|
|
||||||
pub struct Logic {
|
pub struct Logic {
|
||||||
unadded_tim: UnaddedTIM,
|
unadded_tim: Option<TIMInfo>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Logic {
|
impl Logic {
|
||||||
pub fn new() -> 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) {
|
pub fn set_unadded_tim(&mut self, path: &PathBuf) -> Result<Image, Error> {
|
||||||
self.unadded_tim = UnaddedTIM::new(info, image);
|
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 {
|
pub fn add_unadded_tim_as(&mut self, _name: &String) -> Result<Image, Error> {
|
||||||
let _info = self.unadded_tim.take_info();
|
if let Some(unadded_tim) = &self.unadded_tim {
|
||||||
self.unadded_tim.take_image()
|
let image = unadded_tim.get_slint_images();
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct UnaddedTIM {
|
self.unadded_tim = None;
|
||||||
info: TIMInfo,
|
Ok(image)
|
||||||
image: Image,
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl UnaddedTIM {
|
else {
|
||||||
pub fn new(info: TIMInfo, image: Image) -> UnaddedTIM {
|
Err(Error::from_str("No data found for loaded image"))
|
||||||
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()}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{default, fmt::format, fs::File, path::PathBuf};
|
use std::{fs::File, path::PathBuf};
|
||||||
use slint::{Rgba8Pixel, SharedPixelBuffer};
|
use slint::{Rgba8Pixel, SharedPixelBuffer};
|
||||||
use tool_helper::Error;
|
use tool_helper::Error;
|
||||||
|
|
||||||
|
@ -7,57 +7,56 @@ pub struct TIMInfo {
|
||||||
palette: Option<Vec<Rgba8Pixel>>,
|
palette: Option<Vec<Rgba8Pixel>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::default::Default for TIMInfo {
|
impl TIMInfo {
|
||||||
fn default() -> Self {
|
pub fn from_image(path: &PathBuf) -> Result<TIMInfo, Error> {
|
||||||
TIMInfo{image_data: SharedPixelBuffer::new(1, 1), palette: None}
|
fn make_color(iter: &mut dyn std::iter::Iterator<Item = &u8>) -> Option<Rgba8Pixel> {
|
||||||
}
|
Some(Rgba8Pixel::new(
|
||||||
}
|
*iter.next()?, *iter.next()?, *iter.next()?,
|
||||||
|
0xFF))
|
||||||
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 mut reader = png::Decoder::new(File::open(path)?).read_info().or_else(|error| {Err(Error::from_error(error))})?;
|
||||||
let frame_info = reader.next_frame(&mut buffer).or_else(|error| {Err(Error::from_error(error))})?;
|
let info = reader.info().clone();
|
||||||
let bit_depth = frame_info.bit_depth;
|
|
||||||
let mut image_data = SharedPixelBuffer::new(frame_info.width, frame_info.height);
|
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))})?;
|
||||||
if info.color_type == png::ColorType::Indexed {
|
let bit_depth = frame_info.bit_depth;
|
||||||
let palette = info.palette.ok_or(Error::from_str("Found indexed PNG without palette"))?;
|
let mut image_data = SharedPixelBuffer::new(frame_info.width, frame_info.height);
|
||||||
let mut palette_colors = Vec::new();
|
|
||||||
let mut iter = palette.iter();
|
if info.color_type == png::ColorType::Indexed {
|
||||||
|
let palette = info.palette.ok_or(Error::from_str("Found indexed PNG without palette"))?;
|
||||||
while let Some(color) = make_color(&mut iter) {
|
let mut palette_colors = Vec::new();
|
||||||
palette_colors.push(color);
|
let mut iter = palette.iter();
|
||||||
}
|
|
||||||
|
while let Some(color) = make_color(&mut iter) {
|
||||||
let mut dst_pixels = image_data.make_mut_slice();
|
palette_colors.push(color);
|
||||||
for byte in buffer.into_iter() {
|
}
|
||||||
match bit_depth {
|
|
||||||
png::BitDepth::Four => {
|
let mut dst_pixels = image_data.make_mut_slice();
|
||||||
dst_pixels[0] = palette_colors[(byte >> 4) as usize];
|
for byte in buffer.into_iter() {
|
||||||
dst_pixels[1] = palette_colors[(byte & 0xF) as usize];
|
match bit_depth {
|
||||||
dst_pixels = &mut dst_pixels[2..];
|
png::BitDepth::Four => {
|
||||||
},
|
dst_pixels[0] = palette_colors[(byte >> 4) as usize];
|
||||||
png::BitDepth::Eight => {
|
dst_pixels[1] = palette_colors[(byte & 0xF) as usize];
|
||||||
dst_pixels[0] = palette_colors[byte as usize];
|
dst_pixels = &mut dst_pixels[2..];
|
||||||
dst_pixels = &mut dst_pixels[1..];
|
},
|
||||||
},
|
png::BitDepth::Eight => {
|
||||||
_ => {return Err(Error::from_str("Only 4 and 8bit color depth are supported"));}
|
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(TIMInfo{image_data, palette: Some(palette_colors)})
|
||||||
Ok((image, TIMInfo{image_data, palette: Some(palette_colors)}))
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
Err(Error::not_implemented("Support for non indexed images"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
pub fn get_slint_images(&self) -> slint::Image {
|
||||||
Err(Error::not_implemented("Support for non indexed images"))
|
slint::Image::from_rgba8_premultiplied(self.image_data.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -3,10 +3,11 @@
|
||||||
|
|
||||||
mod gui;
|
mod gui;
|
||||||
use gui::{GUIElements, VRAM_WIDTH, VRAM_HEIGHT};
|
use gui::{GUIElements, VRAM_WIDTH, VRAM_HEIGHT};
|
||||||
use rfd::{FileDialog, MessageDialog};
|
use rfd::FileDialog;
|
||||||
use std::{cell::RefCell, rc::Rc};
|
use std::{cell::RefCell, rc::Rc};
|
||||||
use slint::SharedString;
|
use slint::SharedString;
|
||||||
use tim_tool::logic::{tim::load_image, Logic};
|
use tim_tool::logic::Logic;
|
||||||
|
use tool_helper::Error;
|
||||||
|
|
||||||
slint::include_modules!();
|
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 gui_elements = gui_elements_ref.borrow();
|
||||||
|
|
||||||
let logic = logic_ref.clone();
|
let logic = logic_ref.clone();
|
||||||
gui_elements.file_tab.on_browse_file(gui_elements_ref.clone(), move |gui_elements, main_window| {
|
gui_elements.file_tab.on_browse_file(gui_elements_ref.clone(), move |gui_elements, main_window| -> Result<(), Error> {
|
||||||
fn show_error_message(text: String) {
|
|
||||||
MessageDialog::new().set_title("Loading Image failed").set_level(rfd::MessageLevel::Error).set_description(text).show();
|
|
||||||
}
|
|
||||||
|
|
||||||
let file = FileDialog::new()
|
let file = FileDialog::new()
|
||||||
.add_filter("PNG image (.png)", &["png"])
|
.add_filter("PNG image (.png)", &["png"])
|
||||||
.set_title("PNG image file")
|
.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_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 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) {
|
let image = logic.borrow_mut().set_unadded_tim(&file)?;
|
||||||
Ok((image, info)) => (image, info),
|
|
||||||
Err(error) => {
|
|
||||||
file_tab.clear_load();
|
|
||||||
show_error_message(error.to_string());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let img_size = image.size();
|
let img_size = image.size();
|
||||||
if img_size.width > VRAM_WIDTH as u32 || img_size.height > VRAM_HEIGHT as u32 {
|
if img_size.width > VRAM_WIDTH as u32 || img_size.height > VRAM_HEIGHT as u32 {
|
||||||
file_tab.clear_load();
|
return Err(Error::from_text(format!("Image size ({}; {}) is to big for VRAM ({}, {})", img_size.width, img_size.height, VRAM_WIDTH, VRAM_HEIGHT)));
|
||||||
show_error_message(format!("Image size ({}; {}) is to big for VRAM ({}, {})", img_size.width, img_size.height, VRAM_WIDTH, VRAM_HEIGHT));
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logic.borrow_mut().set_unadded_tim(info, image.clone());
|
|
||||||
file_tab.update_new_load(file_name, image);
|
file_tab.update_new_load(file_name, image);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
});
|
});
|
||||||
|
|
||||||
let logic = logic_ref.clone();
|
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_tab = &gui_elements.file_tab;
|
||||||
|
|
||||||
let file_name = file_tab.get_file_name();
|
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();
|
file_tab.clear_load();
|
||||||
main_window.invoke_change_to_main();
|
main_window.invoke_change_to_main();
|
||||||
|
Ok(())
|
||||||
});
|
});
|
||||||
}
|
}
|
Loading…
Reference in New Issue