Changes to tileset to allow error throwing per-overstuffed-palette

stinkhead7ds
Ashley N. 2023-09-13 23:01:17 -04:00
parent 5d8f7294ef
commit 1163ee2752
3 changed files with 26 additions and 17 deletions

View File

@ -19,7 +19,7 @@ pub fn run_command() -> Result<(), Box<dyn Error>> {
TileOrder::Tile => "tile", TileOrder::Tile => "tile",
TileOrder::Sprite => "sprite" TileOrder::Sprite => "sprite"
} }
), )?,
Tools::Soundtrack { input_files, output_directory, input_format: _, output_format: _, source_file_format: _, source_file_output_directory, artifact_output_directory } => { Tools::Soundtrack { input_files, output_directory, input_format: _, output_format: _, source_file_format: _, source_file_output_directory, artifact_output_directory } => {
if input_files.is_empty() { if input_files.is_empty() {
return Err( "no input files (see `reskit soundtrack --help` for more info)" )?; return Err( "no input files (see `reskit soundtrack --help` for more info)" )?;

View File

@ -21,16 +21,17 @@ pub fn get_tiles( tilemap: &TiledTilemap ) -> Result<(Vec<u8>, Vec<u8>), Box<dyn
for tile_y in 0..tiles_height { for tile_y in 0..tiles_height {
for tile_x in 0..tiles_width { for tile_x in 0..tiles_width {
let tile_index = ( tile_y * tiles_width ) + tile_x;
let tile = tilemap.tileset.image.clone().crop( tile_x * 8, tile_y * 8, 8, 8 ); let tile = tilemap.tileset.image.clone().crop( tile_x * 8, tile_y * 8, 8, 8 );
// Fake palette (see below) // Fake palette (see below)
let mut fake: [u16; 16] = [ 0; 16 ]; let mut fake: [u16; 16] = [ 0; 16 ];
let selected_pal = tilemap.tileset.palettes[ tile_index as usize ];
let tile_bin = image_to_tiles( let tile_bin = image_to_tiles(
&tile, &tile,
{ {
// Determine if palette is used here or it is a dummy palette // Determine if palette is used here or it is a dummy palette
let selected_pal = tilemap.tileset.palettes[ ( ( tile_y * tiles_width ) + tile_x ) as usize ];
if let Some( selected_pal ) = selected_pal { if let Some( selected_pal ) = selected_pal {
&mut system_pals[ selected_pal as usize ] &mut system_pals[ selected_pal as usize ]
} else { } else {
@ -42,9 +43,15 @@ pub fn get_tiles( tilemap: &TiledTilemap ) -> Result<(Vec<u8>, Vec<u8>), Box<dyn
"tile" "tile"
); );
if let Err( _ ) = tile_bin {
return Err( format!( "palette {:?} full (try moving tile {} to another palette)", selected_pal, tile_index ) )?
}
if let Ok( tile_bin ) = tile_bin {
all_tiles.extend( tile_bin ); all_tiles.extend( tile_bin );
} }
} }
}
// Define result and write palettes // Define result and write palettes
let mut palettes: Vec<u8> = Vec::new(); let mut palettes: Vec<u8> = Vec::new();

View File

@ -1,11 +1,11 @@
use crate::reskit::utility; use crate::reskit::utility;
use std::{process::exit, error::Error}; use std::error::Error;
use std::fs; use std::fs;
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
use image::{ GenericImageView, DynamicImage }; use image::{ GenericImageView, DynamicImage };
pub fn color_to_palette( r: u16, g: u16, b: u16, palette: &mut [u16; 16] ) -> u32 { pub fn color_to_palette( r: u16, g: u16, b: u16, palette: &mut [u16; 16] ) -> Result<u32, Box<dyn Error>> {
let final_val = let final_val =
( ( r & 0x00F0 ) >> 4 ) | ( ( r & 0x00F0 ) >> 4 ) |
( g & 0x00F0 ) | ( g & 0x00F0 ) |
@ -14,7 +14,7 @@ pub fn color_to_palette( r: u16, g: u16, b: u16, palette: &mut [u16; 16] ) -> u3
// Does the color already exist? // Does the color already exist?
for i in 0..palette.len() { for i in 0..palette.len() {
if palette[ i ] == final_val { if palette[ i ] == final_val {
return i as u32; return Ok( i as u32 );
} }
} }
@ -22,19 +22,19 @@ pub fn color_to_palette( r: u16, g: u16, b: u16, palette: &mut [u16; 16] ) -> u3
for i in 1..palette.len() { for i in 1..palette.len() {
if palette[ i ] == 0 { if palette[ i ] == 0 {
palette[ i ] = final_val; palette[ i ] = final_val;
return i as u32; return Ok( i as u32 );
} }
} }
utility::print_error( "image contains greater than 15 colours, exiting..." ); utility::print_error( "attempted to insert greater than 15 colours in a palette" );
exit( 3 ); Err( "no more room in this palette!" )?
} }
fn get_pixel( image: &DynamicImage, palette: &mut [u16; 16], x: u32, y: u32 ) -> u32 { fn get_pixel( image: &DynamicImage, palette: &mut [u16; 16], x: u32, y: u32 ) -> Result<u32, Box<dyn Error>> {
let ( max_x, max_y ) = image.dimensions(); let ( max_x, max_y ) = image.dimensions();
if x >= max_x || y >= max_y { if x >= max_x || y >= max_y {
return 0; return Ok( 0 );
} }
let pixel = image.get_pixel( x, y ); let pixel = image.get_pixel( x, y );
@ -115,7 +115,7 @@ pub fn output_inc( output_filename: &str, palette: [u16; 16], body: Vec<u8> ) ->
Ok( () ) Ok( () )
} }
pub fn image_to_tiles( img: &DynamicImage, palette: &mut [u16; 16], tile_order: &str ) -> Vec<u8> { pub fn image_to_tiles( img: &DynamicImage, palette: &mut [u16; 16], tile_order: &str ) -> Result<Vec<u8>, Box<dyn Error>> {
let ( mut max_x, mut max_y ) = img.dimensions(); let ( mut max_x, mut max_y ) = img.dimensions();
if max_x % 8 != 0 { max_x = ( 8 * ( max_x / 8 ) ) + ( 8 - ( max_x % 8 ) ); } if max_x % 8 != 0 { max_x = ( 8 * ( max_x / 8 ) ) + ( 8 - ( max_x % 8 ) ); }
if max_y % 8 != 0 { max_y = ( 8 * ( max_y / 8 ) ) + ( 8 - ( max_y % 8 ) ); } if max_y % 8 != 0 { max_y = ( 8 * ( max_y / 8 ) ) + ( 8 - ( max_y % 8 ) ); }
@ -134,7 +134,7 @@ pub fn image_to_tiles( img: &DynamicImage, palette: &mut [u16; 16], tile_order:
let mut series: u32 = 0; let mut series: u32 = 0;
for cell_x in 0..8 { for cell_x in 0..8 {
let nibble: u32 = get_pixel( &img, palette, cell_x + x, cell_y + y ) << ( ( 7 - cell_x ) * 4 ); let nibble: u32 = get_pixel( &img, palette, cell_x + x, cell_y + y )? << ( ( 7 - cell_x ) * 4 );
series = series | nibble; series = series | nibble;
} }
@ -157,7 +157,7 @@ pub fn image_to_tiles( img: &DynamicImage, palette: &mut [u16; 16], tile_order:
let mut series: u32 = 0; let mut series: u32 = 0;
for cell_x in 0..8 { for cell_x in 0..8 {
let nibble: u32 = get_pixel( &img, palette, cell_x + x, cell_y + y ) << ( ( 7 - cell_x ) * 4 ); let nibble: u32 = get_pixel( &img, palette, cell_x + x, cell_y + y )? << ( ( 7 - cell_x ) * 4 );
series = series | nibble; series = series | nibble;
} }
@ -170,14 +170,14 @@ pub fn image_to_tiles( img: &DynamicImage, palette: &mut [u16; 16], tile_order:
} }
} }
body Ok( body )
} }
pub fn generate( image_filename: &str, output_filename: &str, output_mode: &str, tile_order: &str ) { pub fn generate( image_filename: &str, output_filename: &str, output_mode: &str, tile_order: &str ) -> Result<(), Box<dyn Error>> {
let img = image::open( image_filename ); let img = image::open( image_filename );
if let Ok( img ) = img { if let Ok( img ) = img {
let mut palette: [u16; 16] = [ 0; 16 ]; let mut palette: [u16; 16] = [ 0; 16 ];
let body = image_to_tiles( &img, &mut palette, tile_order ); let body = image_to_tiles( &img, &mut palette, tile_order )?;
if output_mode == "bin" { if output_mode == "bin" {
if let Err( err ) = output_bin( output_filename, palette, body ) { if let Err( err ) = output_bin( output_filename, palette, body ) {
@ -197,4 +197,6 @@ pub fn generate( image_filename: &str, output_filename: &str, output_mode: &str,
} else { } else {
utility::print_error( format!( "could not open filename {}", image_filename ).as_str() ); utility::print_error( format!( "could not open filename {}", image_filename ).as_str() );
} }
Ok( () )
} }