Changes to tileset to allow error throwing per-overstuffed-palette
parent
5d8f7294ef
commit
1163ee2752
|
@ -19,7 +19,7 @@ pub fn run_command() -> Result<(), Box<dyn Error>> {
|
|||
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)" )?;
|
||||
|
|
|
@ -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_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 );
|
||||
|
||||
// Fake palette (see below)
|
||||
let mut fake: [u16; 16] = [ 0; 16 ];
|
||||
let selected_pal = tilemap.tileset.palettes[ tile_index as usize ];
|
||||
|
||||
let tile_bin = image_to_tiles(
|
||||
&tile,
|
||||
{
|
||||
// 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 {
|
||||
&mut system_pals[ selected_pal as usize ]
|
||||
} else {
|
||||
|
@ -42,7 +43,13 @@ pub fn get_tiles( tilemap: &TiledTilemap ) -> Result<(Vec<u8>, Vec<u8>), Box<dyn
|
|||
"tile"
|
||||
);
|
||||
|
||||
all_tiles.extend( tile_bin );
|
||||
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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
use crate::reskit::utility;
|
||||
use std::{process::exit, error::Error};
|
||||
use std::error::Error;
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
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 =
|
||||
( ( 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<u32, Box<dyn Error>> {
|
||||
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<u8> ) ->
|
|||
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();
|
||||
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<dyn Error>> {
|
||||
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( () )
|
||||
}
|
Loading…
Reference in New Issue