Add output mode arguments + refactor

master
ne0ndrag0n 2021-02-27 17:15:48 -05:00
parent 9fd5ac3e0b
commit 66b7ed745b
2 changed files with 33 additions and 17 deletions

View File

@ -3,7 +3,7 @@ extern crate clap;
#[macro_use] #[macro_use]
extern crate colour; extern crate colour;
use clap::{App, SubCommand}; use clap::{App, Arg, SubCommand};
mod reskit; mod reskit;
use reskit::utility; use reskit::utility;
@ -19,6 +19,14 @@ fn main() {
.about( "Generate a Sega Megadrive VDP tileset + palette from a 15-colour image" ) .about( "Generate a Sega Megadrive VDP tileset + palette from a 15-colour image" )
.arg_from_usage( "-i, --input=<FILE> 'Specify input image'" ) .arg_from_usage( "-i, --input=<FILE> 'Specify input image'" )
.arg_from_usage( "-o, --output=<FILE> 'Specify output file'") .arg_from_usage( "-o, --output=<FILE> '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(); .get_matches();
@ -27,7 +35,7 @@ fn main() {
// Get input and output filenames // Get input and output filenames
if let Some( input_filename ) = matches.value_of( "input" ) { if let Some( input_filename ) = matches.value_of( "input" ) {
if let Some( output_filename ) = matches.value_of( "output" ) { 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 { } else {
utility::print_error( "expected: output_filename (-o,--output)" ); utility::print_error( "expected: output_filename (-o,--output)" );
} }

View File

@ -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 ) 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<u8> ) {
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 ); let img = image::open( image_filename );
if let Ok( img ) = img { if let Ok( img ) = img {
let ( mut max_x, mut max_y ) = img.dimensions(); 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(); if output_mode == "bin" {
for i in 0..palette.len() { output_bin( image_filename, output_filename, palette, body );
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 { } 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 { } 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() );