Compare commits

...

4 Commits

Author SHA1 Message Date
Jaby 2af28b72c8 Prepare for palette 2025-02-16 16:11:40 +01:00
Jaby 6ddca9e79f Add palette flag that prevents deleting 2025-02-16 15:49:25 +01:00
Jaby ac6a46134e Fix for indexed images not loading anymore 2025-02-16 15:37:00 +01:00
Jaby 7a7b710ca4 Support direct color PNG images 2025-02-16 15:27:07 +01:00
6 changed files with 78 additions and 38 deletions

View File

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

View File

@ -16,7 +16,11 @@ pub const VRAM_HEIGHT:usize = 512;
type MainWindowRef = Rc<RefCell<MainWindow>>;
type GUIElementsRef = Rc<RefCell<GUIElements>>;
fn display_error(title: &str, text: &String) {
pub fn display_information(title: impl Into<String>, text: impl Into<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();
}

View File

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

View File

@ -9,10 +9,10 @@ pub struct TIMInfo {
impl TIMInfo {
pub fn from_image(path: &PathBuf) -> Result<TIMInfo, Error> {
fn make_color(iter: &mut dyn std::iter::Iterator<Item = &u8>) -> Option<Rgba8Pixel> {
fn make_color(iter: &mut dyn std::iter::Iterator<Item = &u8>, load_alpha: bool) -> Option<Rgba8Pixel> {
Some(Rgba8Pixel::new(
*iter.next()?, *iter.next()?, *iter.next()?,
0xFF))
if load_alpha {*iter.next()?} else {0xFF}))
}
let mut reader = png::Decoder::new(File::open(path)?).read_info().or_else(|error| {Err(Error::from_error(error))})?;
@ -20,19 +20,20 @@ impl TIMInfo {
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 bytes_per_pixel = info.bytes_per_pixel();
let bit_depth = frame_info.bit_depth;
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 {
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) {
while let Some(color) = make_color(&mut iter, false) {
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 => {
@ -44,19 +45,32 @@ impl TIMInfo {
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"));}
_ => {return Err(Error::from_str("Only 4 and 8bit color depth are supported for indexed color images"));}
}
}
Ok(TIMInfo{image_data, palette: Some(palette_colors)})
}
else {
Err(Error::not_implemented("Support for non indexed images"))
if bytes_per_pixel != 3 && bytes_per_pixel != 4 {
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 {
slint::Image::from_rgba8_premultiplied(self.image_data.clone())
pub fn get_slint_images(&self) -> (slint::Image, Option<slint::Image>) {
(slint::Image::from_rgba8_premultiplied(self.image_data.clone()), None)
}
}

View File

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

View File

@ -5,6 +5,8 @@ struct VRAMImage {
img: image,
x: int,
y: int,
palette_count: int,
is_palette: bool,
}
export component MainTab inherits Rectangle {