Compare commits

..

No commits in common. "2af28b72c88e6af8a045f38d9d8a52f262654c8a" and "2398053c61edf60bc2f0e5f456709ff993087712" have entirely different histories.

6 changed files with 38 additions and 78 deletions

View File

@ -2,11 +2,10 @@ use crate::{gui::{VRAM_HEIGHT, VRAM_WIDTH}, MainWindow, VRAMImage};
use super::{GUIElementsRef, MainWindowRef}; use super::{GUIElementsRef, MainWindowRef};
use slint::Model; use slint::Model;
use std::{rc::Rc, sync::{Arc, Mutex}}; use std::rc::Rc;
pub struct MainTab { pub struct MainTab {
main_window: MainWindowRef, main_window: MainWindowRef,
mtx: Arc<Mutex<i32>>,
vram_file_list: Rc<slint::VecModel<slint::StandardListViewItem>>, vram_file_list: Rc<slint::VecModel<slint::StandardListViewItem>>,
vram_image_list: Rc<slint::VecModel<VRAMImage>>, vram_image_list: Rc<slint::VecModel<VRAMImage>>,
} }
@ -20,39 +19,23 @@ impl MainTab {
main_window.borrow().set_main_tab_vram_file_list(vram_file_list.clone().into()); main_window.borrow().set_main_tab_vram_file_list(vram_file_list.clone().into());
main_window.borrow().set_main_tab_vram_images(vram_image_list.clone().into()); main_window.borrow().set_main_tab_vram_images(vram_image_list.clone().into());
MainTab{main_window, mtx: Arc::new(Mutex::new(0)), vram_file_list, vram_image_list} MainTab{main_window, vram_file_list, vram_image_list}
} }
pub fn add_new_vram_file(&mut self, file_name: &String, image: slint::Image, image_palette: Option<slint::Image>) { pub fn add_new_vram_file(&mut self, file_name: &String, image: slint::Image) {
let add_new_image = |file_name: &String, image: slint::Image| { let vram_image = VRAMImage{
let vram_image = VRAMImage{ img: image,
img: image, x: 0,
x: 0, y: 0
y: 0,
palette_count: 0,
is_palette: false,
};
self.vram_file_list.push(slint::StandardListViewItem::from(file_name.as_str()));
self.vram_image_list.push(vram_image);
}; };
let _lock = self.mtx.lock().unwrap(); self.vram_file_list.push(slint::StandardListViewItem::from(file_name.as_str()));
add_new_image(file_name, image); self.vram_image_list.push(vram_image);
} }
pub fn remove_vram_file(&mut self, idx: usize) -> bool { pub fn remove_vram_file(&mut self, idx: usize) {
let _lock = self.mtx.lock().unwrap();
if let Some(element) = self.vram_image_list.iter().skip(idx).next() {
if element.is_palette {
return false;
}
}
self.vram_file_list.remove(idx); self.vram_file_list.remove(idx);
self.vram_image_list.remove(idx); self.vram_image_list.remove(idx);
return true;
} }
pub fn move_vram_image(&mut self, idx: usize, dx: i32, dy: i32) { pub fn move_vram_image(&mut self, idx: usize, dx: i32, dy: i32) {

View File

@ -16,11 +16,7 @@ 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>>;
pub fn display_information(title: impl Into<String>, text: impl Into<String>) { fn display_error(title: &str, text: &String) {
MessageDialog::new().set_title(title).set_level(rfd::MessageLevel::Info).set_description(text).show();
}
pub fn display_error(title: impl Into<String>, text: impl Into<String>) {
MessageDialog::new().set_title(title).set_level(rfd::MessageLevel::Error).set_description(text).show(); MessageDialog::new().set_title(title).set_level(rfd::MessageLevel::Error).set_description(text).show();
} }

View File

@ -14,7 +14,7 @@ impl Logic {
Logic{unadded_tim: None} Logic{unadded_tim: None}
} }
pub fn set_unadded_tim(&mut self, path: &PathBuf) -> Result<(Image, Option<Image>), Error> { pub fn set_unadded_tim(&mut self, path: &PathBuf) -> Result<Image, Error> {
let tim_info = TIMInfo::from_image(path)?; let tim_info = TIMInfo::from_image(path)?;
let image = tim_info.get_slint_images(); let image = tim_info.get_slint_images();
@ -22,7 +22,7 @@ impl Logic {
Ok(image) Ok(image)
} }
pub fn add_unadded_tim_as(&mut self, _name: &String) -> Result<(Image, Option<Image>), Error> { pub fn add_unadded_tim_as(&mut self, _name: &String) -> Result<Image, Error> {
if let Some(unadded_tim) = &self.unadded_tim { if let Some(unadded_tim) = &self.unadded_tim {
let image = unadded_tim.get_slint_images(); let image = unadded_tim.get_slint_images();

View File

@ -9,31 +9,30 @@ pub struct TIMInfo {
impl TIMInfo { impl TIMInfo {
pub fn from_image(path: &PathBuf) -> Result<TIMInfo, Error> { pub fn from_image(path: &PathBuf) -> Result<TIMInfo, Error> {
fn make_color(iter: &mut dyn std::iter::Iterator<Item = &u8>, load_alpha: bool) -> Option<Rgba8Pixel> { fn make_color(iter: &mut dyn std::iter::Iterator<Item = &u8>) -> Option<Rgba8Pixel> {
Some(Rgba8Pixel::new( Some(Rgba8Pixel::new(
*iter.next()?, *iter.next()?, *iter.next()?, *iter.next()?, *iter.next()?, *iter.next()?,
if load_alpha {*iter.next()?} else {0xFF})) 0xFF))
} }
let mut reader = png::Decoder::new(File::open(path)?).read_info().or_else(|error| {Err(Error::from_error(error))})?; 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 info = reader.info().clone();
let mut buffer = vec![0; reader.output_buffer_size()]; 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 frame_info = reader.next_frame(&mut buffer).or_else(|error| {Err(Error::from_error(error))})?;
let bytes_per_pixel = info.bytes_per_pixel(); let bit_depth = frame_info.bit_depth;
let bit_depth = frame_info.bit_depth; let mut image_data = SharedPixelBuffer::new(frame_info.width, frame_info.height);
let mut image_data = SharedPixelBuffer::new(frame_info.width, frame_info.height);
let mut dst_pixels = image_data.make_mut_slice();
if info.color_type == png::ColorType::Indexed { if info.color_type == png::ColorType::Indexed {
let palette = info.palette.ok_or(Error::from_str("Found indexed PNG without palette"))?; let palette = info.palette.ok_or(Error::from_str("Found indexed PNG without palette"))?;
let mut palette_colors = Vec::new(); let mut palette_colors = Vec::new();
let mut iter = palette.iter(); let mut iter = palette.iter();
while let Some(color) = make_color(&mut iter, false) { while let Some(color) = make_color(&mut iter) {
palette_colors.push(color); palette_colors.push(color);
} }
let mut dst_pixels = image_data.make_mut_slice();
for byte in buffer.into_iter() { for byte in buffer.into_iter() {
match bit_depth { match bit_depth {
png::BitDepth::Four => { png::BitDepth::Four => {
@ -45,32 +44,19 @@ impl TIMInfo {
dst_pixels[0] = palette_colors[byte as usize]; dst_pixels[0] = palette_colors[byte as usize];
dst_pixels = &mut dst_pixels[1..]; dst_pixels = &mut dst_pixels[1..];
}, },
_ => {return Err(Error::from_str("Only 4 and 8bit color depth are supported for indexed color images"));} _ => {return Err(Error::from_str("Only 4 and 8bit color depth are supported"));}
} }
} }
Ok(TIMInfo{image_data, palette: Some(palette_colors)}) Ok(TIMInfo{image_data, palette: Some(palette_colors)})
} }
else { else {
if bytes_per_pixel != 3 && bytes_per_pixel != 4 { Err(Error::not_implemented("Support for non indexed images"))
return Err(Error::from_text(format!("Image has {} bytes per pixel, but only 3 and 4 bytes per pixel are supported", bytes_per_pixel)));
}
let mut byte_iter = buffer.iter();
while let Some(color) = make_color(&mut byte_iter, bytes_per_pixel == 4) {
match bit_depth {
png::BitDepth::Eight => {
dst_pixels[0] = color;
dst_pixels = &mut dst_pixels[1..];
}
_ => {return Err(Error::from_str("Only 8bit color depth are supported for direct color images"));}
}
}
Ok(TIMInfo{image_data, palette: None})
} }
} }
pub fn get_slint_images(&self) -> (slint::Image, Option<slint::Image>) { pub fn get_slint_images(&self) -> slint::Image {
(slint::Image::from_rgba8_premultiplied(self.image_data.clone()), None) slint::Image::from_rgba8_premultiplied(self.image_data.clone())
} }
} }

View File

@ -2,7 +2,7 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
mod gui; mod gui;
use gui::{GUIElements, VRAM_WIDTH, VRAM_HEIGHT, display_information}; use gui::{GUIElements, VRAM_WIDTH, VRAM_HEIGHT};
use rfd::FileDialog; use rfd::FileDialog;
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
use slint::SharedString; use slint::SharedString;
@ -33,9 +33,7 @@ fn setup_main_tab(gui_elements_ref: Rc<RefCell<GUIElements>>) {
gui_elements.main_tab.on_remove_file(gui_elements_ref.clone(), move |main_tab, _main_window, idx| { gui_elements.main_tab.on_remove_file(gui_elements_ref.clone(), move |main_tab, _main_window, idx| {
if idx >= 0 { if idx >= 0 {
if !main_tab.remove_vram_file(idx as usize) { main_tab.remove_vram_file(idx as usize);
display_information("Removing VRAM file", "Can not remove palette. Delete image instead");
}
} }
}); });
} }
@ -58,7 +56,7 @@ 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, palette_image) = logic.borrow_mut().set_unadded_tim(&file)?; let image = logic.borrow_mut().set_unadded_tim(&file)?;
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 {
@ -75,10 +73,9 @@ fn setup_file_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefC
let main_tab = &mut gui_elements.main_tab; let main_tab = &mut gui_elements.main_tab;
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();
let (image, palette_image) = 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)?);
main_tab.add_new_vram_file(&file_name, image, palette_image);
file_tab.clear_load(); file_tab.clear_load();
main_window.invoke_change_to_main(); main_window.invoke_change_to_main();
Ok(()) Ok(())

View File

@ -2,11 +2,9 @@ import { VRAMArea } from "../vram-components.slint";
import { Button, ComboBox, GroupBox, StandardListView } from "std-widgets.slint"; import { Button, ComboBox, GroupBox, StandardListView } from "std-widgets.slint";
struct VRAMImage { struct VRAMImage {
img: image, img: image,
x: int, x: int,
y: int, y: int,
palette_count: int,
is_palette: bool,
} }
export component MainTab inherits Rectangle { export component MainTab inherits Rectangle {