From 59a27d5907874a43662b1b95b84f8558abae0473 Mon Sep 17 00:00:00 2001 From: yenatch Date: Sun, 20 Aug 2017 13:34:26 -0400 Subject: [PATCH] tools/gfx: Replace --width with --png --- Makefile | 12 ++++++------ tools/gfx.c | 38 +++++++++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index aa3d73c17..208af0aed 100644 --- a/Makefile +++ b/Makefile @@ -138,16 +138,16 @@ gfx/mail/0b9cfe.1bpp: tools/gfx += --remove-whitespace gfx/pokedex/%.2bpp: tools/gfx += --trim-whitespace -gfx/title/crystal.2bpp: tools/gfx += --interleave --width=48 -gfx/title/old_fg.2bpp: tools/gfx += --interleave --width=64 +gfx/title/crystal.2bpp: tools/gfx += --interleave --png=$< +gfx/title/old_fg.2bpp: tools/gfx += --interleave --png=$< gfx/title/logo.2bpp: rgbgfx += -x 4 gfx/trade/ball.2bpp: tools/gfx += --remove-whitespace -gfx/slots_2.2bpp: tools/gfx += --interleave --width=16 -gfx/slots_3.2bpp: tools/gfx += --interleave --width=24 --remove-duplicates --keep-whitespace --remove-xflip -gfx/slots_3a.2bpp: tools/gfx += --interleave --width=16 -gfx/slots_3b.2bpp: tools/gfx += --interleave --width=24 --remove-duplicates --keep-whitespace --remove-xflip +gfx/slots_2.2bpp: tools/gfx += --interleave --png=$< +gfx/slots_3.2bpp: tools/gfx += --interleave --png=$< --remove-duplicates --keep-whitespace --remove-xflip +gfx/slots_3a.2bpp: tools/gfx += --interleave --png=$< +gfx/slots_3b.2bpp: tools/gfx += --interleave --png=$< --remove-duplicates --keep-whitespace --remove-xflip gfx/fx/angels.2bpp: tools/gfx += --trim-whitespace gfx/fx/beam.2bpp: tools/gfx += --remove-xflip --remove-yflip --remove-whitespace diff --git a/tools/gfx.c b/tools/gfx.c index 207dcb6c2..3e5624e95 100644 --- a/tools/gfx.c +++ b/tools/gfx.c @@ -8,7 +8,7 @@ #include "common.h" static void usage(void) { - fprintf(stderr, "Usage: gfx [--trim-whitespace] [--remove-whitespace] [--interleave] [--remove-duplicates [--keep-whitespace]] [--remove-xflip] [--remove-yflip] [-w width] [-d depth] [-h] [-o outfile] infile\n"); + fprintf(stderr, "Usage: gfx [--trim-whitespace] [--remove-whitespace] [--interleave] [--remove-duplicates [--keep-whitespace]] [--remove-xflip] [--remove-yflip] [--png filename] [-d depth] [-h] [-o outfile] infile\n"); } static void error(char *message) { @@ -23,11 +23,11 @@ struct Options { char *outfile; int depth; int interleave; - int width; int remove_duplicates; int keep_whitespace; int remove_xflip; int remove_yflip; + char *png_file; }; struct Options Options = { @@ -43,13 +43,13 @@ void get_args(int argc, char *argv[]) { {"keep-whitespace", no_argument, &Options.keep_whitespace, 1}, {"remove-xflip", no_argument, &Options.remove_xflip, 1}, {"remove-yflip", no_argument, &Options.remove_yflip, 1}, - {"width", required_argument, 0, 'w'}, + {"png", required_argument, 0, 'p'}, {"depth", required_argument, 0, 'd'}, {"help", no_argument, 0, 'h'}, {0} }; for (int opt = 0; opt != -1;) { - switch (opt = getopt_long(argc, argv, "ho:d:", long_options)) { + switch (opt = getopt_long(argc, argv, "ho:d:p:", long_options)) { case 'h': Options.help = true; break; @@ -59,8 +59,8 @@ void get_args(int argc, char *argv[]) { case 'd': Options.depth = strtoul(optarg, NULL, 0); break; - case 'w': - Options.width = strtoul(optarg, NULL, 0); + case 'p': + Options.png_file = optarg; break; case 0: case -1: @@ -221,6 +221,25 @@ void interleave(struct Graphic *graphic, int width) { free(interleaved); } +int png_get_width(char *filename) { + FILE *f = fopen_verbose(filename, "rb"); + if (!f) { + exit(1); + } + + const int OFFSET_WIDTH = 16; + uint8_t bytes[4]; + fseek(f, OFFSET_WIDTH, SEEK_SET); + fread(bytes, 1, 4, f); + fclose(f); + + int width = 0; + for (int i = 0; i < 4; i++) { + width |= bytes[i] << (8 * (3 - i)); + } + return width; +} + int main(int argc, char *argv[]) { get_args(argc, argv); @@ -241,12 +260,13 @@ int main(int argc, char *argv[]) { trim_whitespace(&graphic); } if (Options.interleave) { - if (!Options.width) { - error("interleave: must set --width to a nonzero value"); + if (!Options.png_file) { + error("interleave: need --png to infer dimensions"); usage(); exit(1); } - interleave(&graphic, Options.width); + int width = png_get_width(Options.png_file); + interleave(&graphic, width); } if (Options.remove_duplicates) { remove_duplicates(&graphic);