tools/gfx: Replace --width with --png

This commit is contained in:
yenatch 2017-08-20 13:34:26 -04:00
parent 7ba068d45b
commit 59a27d5907
2 changed files with 35 additions and 15 deletions

View File

@ -138,16 +138,16 @@ gfx/mail/0b9cfe.1bpp: tools/gfx += --remove-whitespace
gfx/pokedex/%.2bpp: tools/gfx += --trim-whitespace gfx/pokedex/%.2bpp: tools/gfx += --trim-whitespace
gfx/title/crystal.2bpp: tools/gfx += --interleave --width=48 gfx/title/crystal.2bpp: tools/gfx += --interleave --png=$<
gfx/title/old_fg.2bpp: tools/gfx += --interleave --width=64 gfx/title/old_fg.2bpp: tools/gfx += --interleave --png=$<
gfx/title/logo.2bpp: rgbgfx += -x 4 gfx/title/logo.2bpp: rgbgfx += -x 4
gfx/trade/ball.2bpp: tools/gfx += --remove-whitespace gfx/trade/ball.2bpp: tools/gfx += --remove-whitespace
gfx/slots_2.2bpp: tools/gfx += --interleave --width=16 gfx/slots_2.2bpp: tools/gfx += --interleave --png=$<
gfx/slots_3.2bpp: tools/gfx += --interleave --width=24 --remove-duplicates --keep-whitespace --remove-xflip gfx/slots_3.2bpp: tools/gfx += --interleave --png=$< --remove-duplicates --keep-whitespace --remove-xflip
gfx/slots_3a.2bpp: tools/gfx += --interleave --width=16 gfx/slots_3a.2bpp: tools/gfx += --interleave --png=$<
gfx/slots_3b.2bpp: tools/gfx += --interleave --width=24 --remove-duplicates --keep-whitespace --remove-xflip gfx/slots_3b.2bpp: tools/gfx += --interleave --png=$< --remove-duplicates --keep-whitespace --remove-xflip
gfx/fx/angels.2bpp: tools/gfx += --trim-whitespace gfx/fx/angels.2bpp: tools/gfx += --trim-whitespace
gfx/fx/beam.2bpp: tools/gfx += --remove-xflip --remove-yflip --remove-whitespace gfx/fx/beam.2bpp: tools/gfx += --remove-xflip --remove-yflip --remove-whitespace

View File

@ -8,7 +8,7 @@
#include "common.h" #include "common.h"
static void usage(void) { 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) { static void error(char *message) {
@ -23,11 +23,11 @@ struct Options {
char *outfile; char *outfile;
int depth; int depth;
int interleave; int interleave;
int width;
int remove_duplicates; int remove_duplicates;
int keep_whitespace; int keep_whitespace;
int remove_xflip; int remove_xflip;
int remove_yflip; int remove_yflip;
char *png_file;
}; };
struct Options Options = { struct Options Options = {
@ -43,13 +43,13 @@ void get_args(int argc, char *argv[]) {
{"keep-whitespace", no_argument, &Options.keep_whitespace, 1}, {"keep-whitespace", no_argument, &Options.keep_whitespace, 1},
{"remove-xflip", no_argument, &Options.remove_xflip, 1}, {"remove-xflip", no_argument, &Options.remove_xflip, 1},
{"remove-yflip", no_argument, &Options.remove_yflip, 1}, {"remove-yflip", no_argument, &Options.remove_yflip, 1},
{"width", required_argument, 0, 'w'}, {"png", required_argument, 0, 'p'},
{"depth", required_argument, 0, 'd'}, {"depth", required_argument, 0, 'd'},
{"help", no_argument, 0, 'h'}, {"help", no_argument, 0, 'h'},
{0} {0}
}; };
for (int opt = 0; opt != -1;) { 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': case 'h':
Options.help = true; Options.help = true;
break; break;
@ -59,8 +59,8 @@ void get_args(int argc, char *argv[]) {
case 'd': case 'd':
Options.depth = strtoul(optarg, NULL, 0); Options.depth = strtoul(optarg, NULL, 0);
break; break;
case 'w': case 'p':
Options.width = strtoul(optarg, NULL, 0); Options.png_file = optarg;
break; break;
case 0: case 0:
case -1: case -1:
@ -221,6 +221,25 @@ void interleave(struct Graphic *graphic, int width) {
free(interleaved); 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[]) { int main(int argc, char *argv[]) {
get_args(argc, argv); get_args(argc, argv);
@ -241,12 +260,13 @@ int main(int argc, char *argv[]) {
trim_whitespace(&graphic); trim_whitespace(&graphic);
} }
if (Options.interleave) { if (Options.interleave) {
if (!Options.width) { if (!Options.png_file) {
error("interleave: must set --width to a nonzero value"); error("interleave: need --png to infer dimensions");
usage(); usage();
exit(1); exit(1);
} }
interleave(&graphic, Options.width); int width = png_get_width(Options.png_file);
interleave(&graphic, width);
} }
if (Options.remove_duplicates) { if (Options.remove_duplicates) {
remove_duplicates(&graphic); remove_duplicates(&graphic);