Support scaling image depended on encoding

This commit is contained in:
Jaby 2025-03-06 21:56:52 +01:00
parent 454fa43492
commit 8ead2a205d
6 changed files with 89 additions and 28 deletions

View File

@ -2,6 +2,7 @@ use crate::MainWindow;
use super::{GUIElements, GUIElementsRef, MainWindowRef, display_error}; use super::{GUIElements, GUIElementsRef, MainWindowRef, display_error};
use slint::{Image, SharedString}; use slint::{Image, SharedString};
use std::rc::Rc; use std::rc::Rc;
use tim_tool::logic::tim::Encoding;
use tool_helper::Error; use tool_helper::Error;
pub struct FileTab { pub struct FileTab {
@ -10,6 +11,10 @@ pub struct FileTab {
} }
impl FileTab { impl FileTab {
const FOUR_BIT_NAME:&str = "4 bit";
const EIGHT_BIT_NAME:&str = "8 bit";
const FULL_COLOR_NAME:&str = "Full color";
fn update_encoding_options(&self, width: u32, height: u32, has_palette: bool) -> Result<(), Error> { fn update_encoding_options(&self, width: u32, height: u32, has_palette: bool) -> Result<(), Error> {
self.encoding_options.clear(); self.encoding_options.clear();
@ -22,17 +27,17 @@ impl FileTab {
} }
if has_4bit { if has_4bit {
self.encoding_options.push(SharedString::from(format!("4 bit ({}/{})", width/4, height))); self.encoding_options.push(SharedString::from(format!("{} ({}/{})", Self::FOUR_BIT_NAME, width/4, height)));
} }
if has_8bit { if has_8bit {
self.encoding_options.push(SharedString::from(format!("8 bit ({}/{})", width/2, height))); self.encoding_options.push(SharedString::from(format!("{} ({}/{})", Self::EIGHT_BIT_NAME, width/2, height)));
} }
Ok(()) Ok(())
} }
else { else {
self.encoding_options.push(SharedString::from(format!("16 bit ({}/{})", width, height))); self.encoding_options.push(SharedString::from(format!("{} ({}/{})", Self::FULL_COLOR_NAME, width, height)));
Ok(()) Ok(())
} }
} }
@ -102,6 +107,26 @@ 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 get_encoding(&self) -> Result<Encoding, Error> {
let selected_encoding = self.main_window.borrow().get_file_tab_selected_encoding();
if selected_encoding.starts_with(Self::FOUR_BIT_NAME) {
Ok(Encoding::FourBit)
}
else if selected_encoding.starts_with(Self::EIGHT_BIT_NAME) {
Ok(Encoding::EightBit)
}
else if selected_encoding.starts_with(Self::FULL_COLOR_NAME) {
Ok(Encoding::FullColor)
}
else {
Err(Error::from_str("Failed to obtain encoding"))
}
}
pub fn on_browse_file(&self, gui_elements: GUIElementsRef, mut function: impl FnMut(&mut GUIElements, &MainWindow) -> Result<(), Error> + '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();

View File

@ -2,7 +2,7 @@ pub mod tim;
use std::path::PathBuf; use std::path::PathBuf;
use slint::Image; use slint::Image;
use tim::TIMInfo; use tim::{Encoding, TIMInfo};
use tool_helper::Error; use tool_helper::Error;
pub struct Logic { pub struct Logic {
@ -16,18 +16,18 @@ impl Logic {
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, Option<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(Encoding::FullColor);
self.unadded_tim = Some(tim_info); self.unadded_tim = Some(tim_info);
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, encoding: Encoding) -> Result<(Image, Option<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, palette) = unadded_tim.get_slint_images(encoding);
self.unadded_tim = None; self.unadded_tim = None;
Ok(image) Ok((image, palette))
} }
else { else {

View File

@ -2,6 +2,12 @@ use std::{fs::File, path::PathBuf};
use slint::{Rgba8Pixel, SharedPixelBuffer}; use slint::{Rgba8Pixel, SharedPixelBuffer};
use tool_helper::Error; use tool_helper::Error;
pub enum Encoding {
FourBit,
EightBit,
FullColor,
}
pub struct TIMInfo { pub struct TIMInfo {
image_data: SharedPixelBuffer<Rgba8Pixel>, image_data: SharedPixelBuffer<Rgba8Pixel>,
palette: Option<PaletteInfo>, palette: Option<PaletteInfo>,
@ -86,8 +92,28 @@ impl TIMInfo {
} }
} }
pub fn get_slint_images(&self) -> (slint::Image, Option<slint::Image>) { pub fn get_slint_images(&self, encoding: Encoding) -> (slint::Image, Option<slint::Image>) {
(slint::Image::from_rgba8_premultiplied(self.image_data.clone()), if let Some(palette) = &self.palette { fn scaled_copy(src: &SharedPixelBuffer<Rgba8Pixel>, factor: u32) -> SharedPixelBuffer<Rgba8Pixel> {
let mut image_data = SharedPixelBuffer::<Rgba8Pixel>::new(src.width()/factor, src.height());
let mut dst_pixel = image_data.make_mut_slice();
let src_pixel = src.as_slice();
for (idx, pixel) in src_pixel.into_iter().enumerate() {
if idx%factor as usize == 0 {
dst_pixel[0] = *pixel;
dst_pixel = &mut dst_pixel[1..];
}
}
image_data
}
let image_data = match encoding {
Encoding::FourBit => scaled_copy(&self.image_data, 4),
Encoding::EightBit => scaled_copy(&self.image_data, 2),
Encoding::FullColor => self.image_data.clone()
};
(slint::Image::from_rgba8_premultiplied(image_data), if let Some(palette) = &self.palette {
Some(palette.get_image()) Some(palette.get_image())
} else { } else {
None None

View File

@ -84,7 +84,8 @@ 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();
let (image, palette_image) = logic.borrow_mut().add_unadded_tim_as(&file_name)?; let encoding = file_tab.get_encoding()?;
let (image, palette_image) = logic.borrow_mut().add_unadded_tim_as(&file_name, encoding)?;
main_tab.add_new_vram_file(&file_name, image, palette_image); main_tab.add_new_vram_file(&file_name, image, palette_image);
file_tab.clear_load(); file_tab.clear_load();

View File

@ -22,6 +22,7 @@ export component MainWindow inherits Window {
in-out property file_tab-palette_width <=> file_tab.conv-palette_width; in-out property file_tab-palette_width <=> file_tab.conv-palette_width;
in-out property file_tab-palette_height <=> file_tab.conv-palette_height; in-out property file_tab-palette_height <=> file_tab.conv-palette_height;
in-out property file_tab-palette_visible <=> file_tab.conv-palette_enable; in-out property file_tab-palette_visible <=> file_tab.conv-palette_enable;
in-out property file_tab-selected_encoding <=> file_tab.conv-selected_encoding;
in-out property file_tab-image_name <=> file_tab.conv-image_name; in-out property file_tab-image_name <=> file_tab.conv-image_name;
in-out property file_tab-enable <=> file_tab.conv-enable_view; in-out property file_tab-enable <=> file_tab.conv-enable_view;
callback file_tab-update_palette_size <=> file_tab.conv-image_update_palette_size; callback file_tab-update_palette_size <=> file_tab.conv-image_update_palette_size;

View File

@ -21,6 +21,7 @@ component ConvertImageWidget inherits Rectangle {
in-out property <image> palette_data; in-out property <image> palette_data;
in-out property <int> palette_width: 0; in-out property <int> palette_width: 0;
in-out property <int> palette_height: 0; in-out property <int> palette_height: 0;
in-out property <string> selected_encoding;
in-out property <bool> enable_view: false; in-out property <bool> enable_view: false;
in-out property <bool> palette_visible: false; in-out property <bool> palette_visible: false;
@ -94,6 +95,11 @@ component ConvertImageWidget inherits Rectangle {
ComboBox { ComboBox {
model: root.encoding_options; model: root.encoding_options;
enabled: root.enable_view; enabled: root.enable_view;
current-value: root.encoding_options[0];
selected(current-value) => {
root.selected_encoding = current-value;
}
} }
} }
} }
@ -205,6 +211,7 @@ export component FileTab inherits Rectangle {
in-out property <image> conv-palette_data; in-out property <image> conv-palette_data;
in-out property <int> conv-palette_width; in-out property <int> conv-palette_width;
in-out property <int> conv-palette_height; in-out property <int> conv-palette_height;
in-out property <string> conv-selected_encoding;
in-out property <bool> conv-palette_enable; in-out property <bool> conv-palette_enable;
in-out property <bool> conv-enable_view; in-out property <bool> conv-enable_view;
@ -253,6 +260,7 @@ export component FileTab inherits Rectangle {
palette_width <=> root.conv-palette_width; palette_width <=> root.conv-palette_width;
palette_height <=> root.conv-palette_height; palette_height <=> root.conv-palette_height;
palette_visible <=> root.conv-palette_enable; palette_visible <=> root.conv-palette_enable;
selected_encoding <=> root.conv-selected_encoding;
image_name <=> root.conv-image_name; image_name <=> root.conv-image_name;
enable_view <=> root.conv-enable_view; enable_view <=> root.conv-enable_view;