summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorRangi <remy.oukaour+rangi42@gmail.com>2020-06-14 19:00:34 -0400
committerRangi <remy.oukaour+rangi42@gmail.com>2020-06-14 19:00:34 -0400
commitb6b573152cb86ff47cac3f6cae95fa1edcd3a64f (patch)
tree5eb78ee9ae0676fa79343eb074272281ff3cce91 /tools
parent1521f52e37741a9220a1362443df6d0462e582d3 (diff)
Derive Pokémon frontpic dimensions using tools/png_dimensions
Diffstat (limited to 'tools')
-rw-r--r--tools/Makefile1
-rw-r--r--tools/png_dimensions.c57
2 files changed, 58 insertions, 0 deletions
diff --git a/tools/Makefile b/tools/Makefile
index a86675d3..797ee2c9 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -5,6 +5,7 @@ CFLAGS := -O3 -std=c99 -Wall -Wextra -Wno-missing-field-initializers
tools := \
lzcomp \
+ png_dimensions \
scan_includes \
gfx
all: $(tools)
diff --git a/tools/png_dimensions.c b/tools/png_dimensions.c
new file mode 100644
index 00000000..900485bd
--- /dev/null
+++ b/tools/png_dimensions.c
@@ -0,0 +1,57 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+void usage(void) {
+ fprintf(stderr, "Usage: png_dimensions infile outfile\n");
+ exit(1);
+}
+
+void output_dimensions(char* png_filename, char* out_filename) {
+ FILE* f;
+ int width, height;
+ int i;
+ uint8_t bytes[4];
+ uint8_t output;
+
+ f = fopen(png_filename, "rb");
+ if (f == NULL) {
+ fprintf(stderr, "failed to open file %s\n", png_filename);
+ exit(1);
+ }
+
+ // width
+ fseek(f, 16, SEEK_SET);
+ int size = fread(bytes, 1, 4, f);
+ fclose(f);
+ if (size != 4) {
+ fprintf(stderr, "failed to read at offset 0x10 in file %s\n", png_filename);
+ exit(1);
+ }
+
+ width = 0;
+ for (i = 0; i < 4; i++) {
+ width |= bytes[i] << (8 * (3 - i));
+ }
+ width >>= 3;
+ height = width;
+
+ output = width & 0xf;
+ output |= (height & 0xf) << 4;
+
+ f = fopen(out_filename, "wb");
+ if (f == NULL) {
+ fprintf(stderr, "failed to open file %s\n", out_filename);
+ exit(1);
+ }
+ fwrite(&output, 1, 1, f);
+ fclose(f);
+}
+
+int main(int argc, char* argv[]) {
+ if (argc < 3) {
+ usage();
+ }
+ output_dimensions(argv[1], argv[2]);
+ return 0;
+}