shithub: pokecrystal

Download patch

ref: 59a27d5907874a43662b1b95b84f8558abae0473
parent: 7ba068d45b6e1744402ab910f2ded4e9e32429c6
author: yenatch <[email protected]>
date: Sun Aug 20 09:34:26 EDT 2017

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

--- a/Makefile
+++ b/Makefile
@@ -138,16 +138,16 @@
 
 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
--- 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 @@
 	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 @@
 		{"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 @@
 		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,7 +221,26 @@
 	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);
 	argc -= optind;
@@ -241,12 +260,13 @@
 		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);