diff --git a/src/main.rs b/src/main.rs index f81422b..782491c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ extern crate clap; #[macro_use] extern crate colour; -use clap::{App, SubCommand}; +use clap::{App, Arg, SubCommand}; mod reskit; use reskit::utility; @@ -19,6 +19,14 @@ fn main() { .about( "Generate a Sega Megadrive VDP tileset + palette from a 15-colour image" ) .arg_from_usage( "-i, --input= 'Specify input image'" ) .arg_from_usage( "-o, --output= 'Specify output file'") + .arg( + Arg::with_name( "FORMAT" ) + .short( "f" ) + .long( "format" ) + .help( "Specify output format for tileset (valid FORMAT options: bin, inc)") + .default_value( "bin" ) + .takes_value( true ) + ) ) .get_matches(); @@ -27,7 +35,7 @@ fn main() { // Get input and output filenames if let Some( input_filename ) = matches.value_of( "input" ) { if let Some( output_filename ) = matches.value_of( "output" ) { - return tileset::generate( input_filename, output_filename ); + return tileset::generate( input_filename, output_filename, matches.value_of( "FORMAT" ).unwrap() ); } else { utility::print_error( "expected: output_filename (-o,--output)" ); } diff --git a/src/reskit/tileset.rs b/src/reskit/tileset.rs index 12980ed..ffc5179 100644 --- a/src/reskit/tileset.rs +++ b/src/reskit/tileset.rs @@ -40,7 +40,26 @@ fn get_pixel( image: &DynamicImage, palette: &mut [u16; 16], x: u32, y: u32 ) -> color_to_palette( pixel[ 0 ].into(), pixel[ 1 ].into(), pixel[ 2 ].into(), palette ) } -pub fn generate( image_filename: &str, output_filename: &str ) { +fn output_bin( image_filename: &str, output_filename: &str, palette: [u16; 16], body: Vec ) { + let mut output_palette: Vec< u8 > = Vec::new(); + for i in 0..palette.len() { + let bytes = palette[ i ].to_be_bytes(); + for i in 0..2 { + output_palette.push( bytes[ i ] ); + } + } + + let output_try = File::create( output_filename ); + if let Ok( mut output_file ) = output_try { + output_file.write( &output_palette ).unwrap(); + output_file.write( &body ).unwrap(); + utility::print_good( format!( "converted file {}", image_filename ).as_str() ); + } else { + utility::print_error( format!( "could not open filename for output {}", output_filename ).as_str() ); + } +} + +pub fn generate( image_filename: &str, output_filename: &str, output_mode: &str ) { let img = image::open( image_filename ); if let Ok( img ) = img { let ( mut max_x, mut max_y ) = img.dimensions(); @@ -68,21 +87,10 @@ pub fn generate( image_filename: &str, output_filename: &str ) { } } - let mut output_palette: Vec< u8 > = Vec::new(); - for i in 0..palette.len() { - let bytes = palette[ i ].to_be_bytes(); - for i in 0..2 { - output_palette.push( bytes[ i ] ); - } - } - - let output_try = File::create( output_filename ); - if let Ok( mut output_file ) = output_try { - output_file.write( &output_palette ).unwrap(); - output_file.write( &body ).unwrap(); - utility::print_good( format!( "converted file {}", image_filename ).as_str() ); + if output_mode == "bin" { + output_bin( image_filename, output_filename, palette, body ); } else { - utility::print_error( format!( "could not open filename for output {}", output_filename ).as_str() ); + utility::print_error( format!( "invalid output mode {}", output_mode ).as_str() ); } } else { utility::print_error( format!( "could not open filename {}", image_filename ).as_str() );