Small refactoring and optimization to tools/gfx.c

This commit is contained in:
Rangi 2021-09-21 17:16:14 -04:00
parent 314c5fc9ab
commit 2691c9f5c8

View File

@ -1,7 +1,7 @@
#include "common.h" #include "common.h"
static void usage(void) { void usage(void) {
fprintf(stderr, "Usage: gfx [-h|--help] [--trim-whitespace] [--remove-whitespace] [--interleave] [--remove-duplicates [--keep-whitespace]] [--remove-xflip] [--remove-yflip] [--preserve indexes] [-p|--png filename.png] [-d|--depth depth] [-o outfile] infile\n"); fprintf(stderr, "Usage: gfx [-h|--help] [--trim-whitespace] [--remove-whitespace] [--interleave] [--remove-duplicates [--keep-whitespace]] [--remove-xflip] [--remove-yflip] [--preserve indexes] [-d|--depth depth] [-p|--png filename.png] [-o|--out outfile] infile\n");
} }
struct Options { struct Options {
@ -14,8 +14,8 @@ struct Options {
bool remove_yflip; bool remove_yflip;
int *preserved; int *preserved;
int num_preserved; int num_preserved;
char *png_file;
int depth; int depth;
char *png_file;
char *outfile; char *outfile;
}; };
@ -33,6 +33,7 @@ void parse_args(int argc, char *argv[]) {
{"preserve", required_argument, 0, 'r'}, {"preserve", required_argument, 0, 'r'},
{"png", required_argument, 0, 'p'}, {"png", required_argument, 0, 'p'},
{"depth", required_argument, 0, 'd'}, {"depth", required_argument, 0, 'd'},
{"out", required_argument, 0, 'o'},
{"help", no_argument, 0, 'h'}, {"help", no_argument, 0, 'h'},
{0} {0}
}; };
@ -68,12 +69,12 @@ void parse_args(int argc, char *argv[]) {
case 'd': case 'd':
options.depth = strtoul(optarg, NULL, 0); options.depth = strtoul(optarg, NULL, 0);
break; break;
case 'o':
options.outfile = optarg;
break;
case 'p': case 'p':
options.png_file = optarg; options.png_file = optarg;
break; break;
case 'o':
options.outfile = optarg;
break;
case 'h': case 'h':
usage(); usage();
exit(0); exit(0);
@ -157,6 +158,7 @@ bool tile_exists(const uint8_t *tile, const uint8_t *tiles, int tile_size, int n
for (int j = 0; j < tile_size; j++) { for (int j = 0; j < tile_size; j++) {
if (tile[j] != tiles[i * tile_size + j]) { if (tile[j] != tiles[i * tile_size + j]) {
match = false; match = false;
break;
} }
} }
if (match) { if (match) {
@ -280,7 +282,7 @@ int main(int argc, char *argv[]) {
} }
if (options.interleave) { if (options.interleave) {
if (!options.png_file) { if (!options.png_file) {
error_exit("interleave: need --png to infer dimensions"); error_exit("--interleave needs --png to infer dimensions");
} }
int width = read_png_width_verbose(options.png_file); int width = read_png_width_verbose(options.png_file);
interleave(&graphic, width); interleave(&graphic, width);