From 1163ee275228f180b175124d0da16d0a61130f0f Mon Sep 17 00:00:00 2001 From: ashley Date: Wed, 13 Sep 2023 23:01:17 -0400 Subject: [PATCH] Changes to tileset to allow error throwing per-overstuffed-palette --- src/reskit/cli/evaluator.rs | 2 +- src/reskit/level/system.rs | 11 +++++++++-- src/reskit/tileset.rs | 30 ++++++++++++++++-------------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/reskit/cli/evaluator.rs b/src/reskit/cli/evaluator.rs index 66d6654..3688f6a 100644 --- a/src/reskit/cli/evaluator.rs +++ b/src/reskit/cli/evaluator.rs @@ -19,7 +19,7 @@ pub fn run_command() -> Result<(), Box> { TileOrder::Tile => "tile", TileOrder::Sprite => "sprite" } - ), + )?, 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() { return Err( "no input files (see `reskit soundtrack --help` for more info)" )?; diff --git a/src/reskit/level/system.rs b/src/reskit/level/system.rs index c1c1ca0..927c99e 100644 --- a/src/reskit/level/system.rs +++ b/src/reskit/level/system.rs @@ -21,16 +21,17 @@ pub fn get_tiles( tilemap: &TiledTilemap ) -> Result<(Vec, Vec), Box Result<(Vec, Vec), Box u32 { +pub fn color_to_palette( r: u16, g: u16, b: u16, palette: &mut [u16; 16] ) -> Result> { let final_val = ( ( r & 0x00F0 ) >> 4 ) | ( 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? for i in 0..palette.len() { 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() { if palette[ i ] == 0 { palette[ i ] = final_val; - return i as u32; + return Ok( i as u32 ); } } - utility::print_error( "image contains greater than 15 colours, exiting..." ); - exit( 3 ); + utility::print_error( "attempted to insert greater than 15 colours in a palette" ); + 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> { let ( max_x, max_y ) = image.dimensions(); if x >= max_x || y >= max_y { - return 0; + return Ok( 0 ); } let pixel = image.get_pixel( x, y ); @@ -115,7 +115,7 @@ pub fn output_inc( output_filename: &str, palette: [u16; 16], body: Vec ) -> Ok( () ) } -pub fn image_to_tiles( img: &DynamicImage, palette: &mut [u16; 16], tile_order: &str ) -> Vec { +pub fn image_to_tiles( img: &DynamicImage, palette: &mut [u16; 16], tile_order: &str ) -> Result, Box> { 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_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; 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; } @@ -157,7 +157,7 @@ pub fn image_to_tiles( img: &DynamicImage, palette: &mut [u16; 16], tile_order: let mut series: u32 = 0; 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; } @@ -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> { let img = image::open( image_filename ); if let Ok( img ) = img { 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 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 { utility::print_error( format!( "could not open filename {}", image_filename ).as_str() ); } + + Ok( () ) } \ No newline at end of file