summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordannye <33dannye@gmail.com>2018-05-30 04:18:51 -0500
committerdannye <33dannye@gmail.com>2018-05-30 04:18:51 -0500
commit6ef9138b8a6c41c66c15e6b98a06f81002d22718 (patch)
tree9a1ccce423e1159304f310b0d28059c59a879855
parent943a9c74e4bd2bd754d09a15f82b8338f1e8c3e7 (diff)
Add more graphics
add tools/gfx.c for trimming whitespace from pngs
-rw-r--r--Makefile12
-rw-r--r--gfx/15_puzzle.pngbin0 -> 409 bytes
-rw-r--r--gfx/attack_animations.pngbin0 -> 5822 bytes
-rw-r--r--gfx/emotion_bubbles.pngbin0 -> 149 bytes
-rw-r--r--gfx/fire_notes.pngbin0 -> 228 bytes
-rw-r--r--gfx/gameboy.pngbin0 -> 427 bytes
-rw-r--r--gfx/gamefreak_logo.pngbin0 -> 270 bytes
-rw-r--r--gfx/gamefreak_logo_oam.pngbin0 -> 143 bytes
-rw-r--r--gfx/gfx_44745.pngbin0 -> 346 bytes
-rw-r--r--gfx/gfx_84bf.pngbin0 -> 140 bytes
-rw-r--r--gfx/intro_forest.pngbin0 -> 630 bytes
-rw-r--r--gfx/intro_fushigibana.pngbin0 -> 610 bytes
-rw-r--r--gfx/intro_kamekkusu.pngbin0 -> 548 bytes
-rw-r--r--gfx/intro_purin_pika.pngbin0 -> 1388 bytes
-rw-r--r--gfx/intro_rizado_1.pngbin0 -> 772 bytes
-rw-r--r--gfx/intro_rizado_2.pngbin0 -> 796 bytes
-rw-r--r--gfx/intro_rizado_3.pngbin0 -> 872 bytes
-rw-r--r--gfx/intro_rizado_flames.pngbin0 -> 229 bytes
-rw-r--r--gfx/intro_underwater.pngbin0 -> 1157 bytes
-rw-r--r--gfx/intro_water_pokemon.pngbin0 -> 1288 bytes
-rw-r--r--gfx/leader_faces_names.pngbin0 -> 317 bytes
-rw-r--r--gfx/ledge_hopping_shadow.pngbin0 -> 84 bytes
-rw-r--r--gfx/mail.pngbin0 -> 135 bytes
-rw-r--r--gfx/matches.pngbin0 -> 445 bytes
-rw-r--r--gfx/mon_party_sprites.pngbin0 -> 2202 bytes
-rw-r--r--gfx/picross.pngbin0 -> 336 bytes
-rw-r--r--gfx/picross_cursor.pngbin0 -> 77 bytes
-rw-r--r--gfx/pokedex_buttons.pngbin0 -> 541 bytes
-rw-r--r--gfx/pokedex_cursors.pngbin0 -> 113 bytes
-rw-r--r--gfx/pokedex_pokeball.pngbin0 -> 89 bytes
-rw-r--r--gfx/pokegear.pngbin0 -> 420 bytes
-rw-r--r--gfx/poker.pngbin0 -> 1124 bytes
-rw-r--r--gfx/sgb_border.pngbin0 -> 589 bytes
-rw-r--r--gfx/sgb_border_alt.pngbin0 -> 608 bytes
-rw-r--r--gfx/slot_machine.pngbin0 -> 469 bytes
-rw-r--r--gfx/slot_machine_2.pngbin0 -> 1278 bytes
-rw-r--r--gfx/titlescreen.pngbin0 -> 1511 bytes
-rw-r--r--gfx/trainer_card.pngbin0 -> 619 bytes
-rw-r--r--gfx/unused_leader_name.pngbin0 -> 92 bytes
-rw-r--r--main.asm82
-rw-r--r--shim.sym15
-rw-r--r--tools/Makefile3
-rwxr-xr-xtools/common.h40
-rwxr-xr-xtools/gfx.c296
44 files changed, 432 insertions, 16 deletions
diff --git a/Makefile b/Makefile
index 2069e0a..344662b 100644
--- a/Makefile
+++ b/Makefile
@@ -44,11 +44,23 @@ clean:
rm -rf $(ROMS) $(OBJS) $(ROMS:.gb=.sym) build/* shim.asm
find . \( -iname '*.1bpp' -o -iname '*.2bpp' -o -iname '*.pic' -o -iname '*.pcm' \) -exec rm {} +
+gfx/sgb_border_alt.2bpp: tools/gfx += --trim-whitespace
+gfx/sgb_border.2bpp: tools/gfx += --trim-whitespace
+gfx/titlescreen.2bpp: tools/gfx += --trim-whitespace
+gfx/leader_faces_names.2bpp: tools/gfx += --trim-whitespace
+gfx/slot_machine.2bpp: tools/gfx += --trim-whitespace
+gfx/poker.2bpp: tools/gfx += --trim-whitespace
+gfx/intro_purin_pika.2bpp: tools/gfx += --trim-whitespace
+
%.2bpp: %.png
rgbgfx -o $@ $<
+ $(if $(tools/gfx),\
+ tools/gfx $(tools/gfx) -o $@ $@)
%.1bpp: %.png
rgbgfx -d1 -o $@ $<
+ $(if $(tools/gfx),\
+ tools/gfx $(tools/gfx) -d1 -o $@ $@)
%.tilemap: %.png
rgbgfx -t $@ $<
diff --git a/gfx/15_puzzle.png b/gfx/15_puzzle.png
new file mode 100644
index 0000000..70c1078
--- /dev/null
+++ b/gfx/15_puzzle.png
Binary files differ
diff --git a/gfx/attack_animations.png b/gfx/attack_animations.png
new file mode 100644
index 0000000..96e60c2
--- /dev/null
+++ b/gfx/attack_animations.png
Binary files differ
diff --git a/gfx/emotion_bubbles.png b/gfx/emotion_bubbles.png
new file mode 100644
index 0000000..31deacf
--- /dev/null
+++ b/gfx/emotion_bubbles.png
Binary files differ
diff --git a/gfx/fire_notes.png b/gfx/fire_notes.png
new file mode 100644
index 0000000..5e5fcd0
--- /dev/null
+++ b/gfx/fire_notes.png
Binary files differ
diff --git a/gfx/gameboy.png b/gfx/gameboy.png
new file mode 100644
index 0000000..0788ba4
--- /dev/null
+++ b/gfx/gameboy.png
Binary files differ
diff --git a/gfx/gamefreak_logo.png b/gfx/gamefreak_logo.png
new file mode 100644
index 0000000..8883d51
--- /dev/null
+++ b/gfx/gamefreak_logo.png
Binary files differ
diff --git a/gfx/gamefreak_logo_oam.png b/gfx/gamefreak_logo_oam.png
new file mode 100644
index 0000000..3341412
--- /dev/null
+++ b/gfx/gamefreak_logo_oam.png
Binary files differ
diff --git a/gfx/gfx_44745.png b/gfx/gfx_44745.png
new file mode 100644
index 0000000..5b35c7a
--- /dev/null
+++ b/gfx/gfx_44745.png
Binary files differ
diff --git a/gfx/gfx_84bf.png b/gfx/gfx_84bf.png
new file mode 100644
index 0000000..709f347
--- /dev/null
+++ b/gfx/gfx_84bf.png
Binary files differ
diff --git a/gfx/intro_forest.png b/gfx/intro_forest.png
new file mode 100644
index 0000000..2f97b65
--- /dev/null
+++ b/gfx/intro_forest.png
Binary files differ
diff --git a/gfx/intro_fushigibana.png b/gfx/intro_fushigibana.png
new file mode 100644
index 0000000..784a1c0
--- /dev/null
+++ b/gfx/intro_fushigibana.png
Binary files differ
diff --git a/gfx/intro_kamekkusu.png b/gfx/intro_kamekkusu.png
new file mode 100644
index 0000000..255ca08
--- /dev/null
+++ b/gfx/intro_kamekkusu.png
Binary files differ
diff --git a/gfx/intro_purin_pika.png b/gfx/intro_purin_pika.png
new file mode 100644
index 0000000..eab3c2d
--- /dev/null
+++ b/gfx/intro_purin_pika.png
Binary files differ
diff --git a/gfx/intro_rizado_1.png b/gfx/intro_rizado_1.png
new file mode 100644
index 0000000..0aa062c
--- /dev/null
+++ b/gfx/intro_rizado_1.png
Binary files differ
diff --git a/gfx/intro_rizado_2.png b/gfx/intro_rizado_2.png
new file mode 100644
index 0000000..a3e7ff3
--- /dev/null
+++ b/gfx/intro_rizado_2.png
Binary files differ
diff --git a/gfx/intro_rizado_3.png b/gfx/intro_rizado_3.png
new file mode 100644
index 0000000..be9e22a
--- /dev/null
+++ b/gfx/intro_rizado_3.png
Binary files differ
diff --git a/gfx/intro_rizado_flames.png b/gfx/intro_rizado_flames.png
new file mode 100644
index 0000000..2b99a62
--- /dev/null
+++ b/gfx/intro_rizado_flames.png
Binary files differ
diff --git a/gfx/intro_underwater.png b/gfx/intro_underwater.png
new file mode 100644
index 0000000..fc446fd
--- /dev/null
+++ b/gfx/intro_underwater.png
Binary files differ
diff --git a/gfx/intro_water_pokemon.png b/gfx/intro_water_pokemon.png
new file mode 100644
index 0000000..f7caa9b
--- /dev/null
+++ b/gfx/intro_water_pokemon.png
Binary files differ
diff --git a/gfx/leader_faces_names.png b/gfx/leader_faces_names.png
new file mode 100644
index 0000000..dceb21f
--- /dev/null
+++ b/gfx/leader_faces_names.png
Binary files differ
diff --git a/gfx/ledge_hopping_shadow.png b/gfx/ledge_hopping_shadow.png
new file mode 100644
index 0000000..ba3dab5
--- /dev/null
+++ b/gfx/ledge_hopping_shadow.png
Binary files differ
diff --git a/gfx/mail.png b/gfx/mail.png
new file mode 100644
index 0000000..7ad25ed
--- /dev/null
+++ b/gfx/mail.png
Binary files differ
diff --git a/gfx/matches.png b/gfx/matches.png
new file mode 100644
index 0000000..e0f1d32
--- /dev/null
+++ b/gfx/matches.png
Binary files differ
diff --git a/gfx/mon_party_sprites.png b/gfx/mon_party_sprites.png
new file mode 100644
index 0000000..24f970d
--- /dev/null
+++ b/gfx/mon_party_sprites.png
Binary files differ
diff --git a/gfx/picross.png b/gfx/picross.png
new file mode 100644
index 0000000..37f7e62
--- /dev/null
+++ b/gfx/picross.png
Binary files differ
diff --git a/gfx/picross_cursor.png b/gfx/picross_cursor.png
new file mode 100644
index 0000000..efee734
--- /dev/null
+++ b/gfx/picross_cursor.png
Binary files differ
diff --git a/gfx/pokedex_buttons.png b/gfx/pokedex_buttons.png
new file mode 100644
index 0000000..f6669f5
--- /dev/null
+++ b/gfx/pokedex_buttons.png
Binary files differ
diff --git a/gfx/pokedex_cursors.png b/gfx/pokedex_cursors.png
new file mode 100644
index 0000000..265a41f
--- /dev/null
+++ b/gfx/pokedex_cursors.png
Binary files differ
diff --git a/gfx/pokedex_pokeball.png b/gfx/pokedex_pokeball.png
new file mode 100644
index 0000000..8c198cb
--- /dev/null
+++ b/gfx/pokedex_pokeball.png
Binary files differ
diff --git a/gfx/pokegear.png b/gfx/pokegear.png
new file mode 100644
index 0000000..2c8d3e9
--- /dev/null
+++ b/gfx/pokegear.png
Binary files differ
diff --git a/gfx/poker.png b/gfx/poker.png
new file mode 100644
index 0000000..00218f7
--- /dev/null
+++ b/gfx/poker.png
Binary files differ
diff --git a/gfx/sgb_border.png b/gfx/sgb_border.png
new file mode 100644
index 0000000..2fda111
--- /dev/null
+++ b/gfx/sgb_border.png
Binary files differ
diff --git a/gfx/sgb_border_alt.png b/gfx/sgb_border_alt.png
new file mode 100644
index 0000000..054ab48
--- /dev/null
+++ b/gfx/sgb_border_alt.png
Binary files differ
diff --git a/gfx/slot_machine.png b/gfx/slot_machine.png
new file mode 100644
index 0000000..44993f6
--- /dev/null
+++ b/gfx/slot_machine.png
Binary files differ
diff --git a/gfx/slot_machine_2.png b/gfx/slot_machine_2.png
new file mode 100644
index 0000000..cae4177
--- /dev/null
+++ b/gfx/slot_machine_2.png
Binary files differ
diff --git a/gfx/titlescreen.png b/gfx/titlescreen.png
new file mode 100644
index 0000000..1b6ba5d
--- /dev/null
+++ b/gfx/titlescreen.png
Binary files differ
diff --git a/gfx/trainer_card.png b/gfx/trainer_card.png
new file mode 100644
index 0000000..8fa51d1
--- /dev/null
+++ b/gfx/trainer_card.png
Binary files differ
diff --git a/gfx/unused_leader_name.png b/gfx/unused_leader_name.png
new file mode 100644
index 0000000..daff9f5
--- /dev/null
+++ b/gfx/unused_leader_name.png
Binary files differ
diff --git a/main.asm b/main.asm
index e68b601..58da46e 100644
--- a/main.asm
+++ b/main.asm
@@ -6,6 +6,10 @@ INCLUDE "engine/link/place_waiting_text.asm"
SECTION "Title screen", ROMX[$5D8C],BANK[$01]
INCLUDE "engine/title.asm"
+SECTION "Title Screen Sprites", ROMX[$5EB8], BANK[$01]
+TitleFlameNoteGfx::
+ INCBIN "gfx/fire_notes.2bpp"
+
SECTION "Predef pointers", ROMX[$62B0],BANK[$01]
INCLUDE "engine/predef.asm"
@@ -35,9 +39,38 @@ MainMenuStrings: ; 01:5428
SECTION "Nest Icon", ROMX[$4A0F], BANK[$02]
INCBIN "gfx/mon_nest_icon.1bpp"
+SECTION "Bank 2 Misc GFX", ROMX[$44bf], BANK[$02]
+ INCBIN "gfx/gfx_84bf.2bpp"
+ INCBIN "gfx/ledge_hopping_shadow.2bpp"
+ INCBIN "gfx/emotion_bubbles.2bpp"
+
+SECTION "Pokegear GFX", ROMX[$4F32], BANK[$02]
+ INCBIN "gfx/pokegear.2bpp"
+
+SECTION "Unused SGB Border GFX", ROMX[$62CC], BANK[$02]
+ INCBIN "gfx/sgb_border_alt.2bpp"
+
+SECTION "SGB Border GFX", ROMX[$6B1C], BANK[$02]
+ INCBIN "gfx/sgb_border.2bpp"
+
+SECTION "Title Screen GFX", ROMX[$47CF], BANK[$04]
+ INCBIN "gfx/titlescreen.2bpp"
+
+SECTION "Mail Icon GFX", ROMX[$5BB1], BANK[$04]
+ INCBIN "gfx/mail.2bpp"
+
SECTION "TM/HM Moves", ROMX[$52D1],BANK[$04]
INCLUDE "data/moves/tmhm_moves.asm"
+SECTION "Trainer Card GFX", ROMX[$7171], BANK[$04]
+ INCBIN "gfx/trainer_card.2bpp"
+ INCBIN "gfx/leader_faces_names.2bpp"
+SECTION "Unused Leader", ROMX[$7BA3], BANK[$04]
+ INCBIN "gfx/unused_leader_name.2bpp"
+
+SECTION "Gameboy GFX", ROMX[$5641], BANK[$0A]
+ INCBIN "gfx/gameboy.2bpp"
+
SECTION "Type Matchups", ROMX[$506D],BANK[$0D]
INCLUDE "data/types/type_matchups.asm"
@@ -49,6 +82,12 @@ INCLUDE "data/pokemon/dex_order_alpha.asm"
SECTION "Evolutions and Attacks", ROMX[$6493],BANK[$10]
INCLUDE "data/pokemon/evos_attacks.asm"
+SECTION "Pokedex GFX", ROMX[$40D5], BANK[$11]
+ INCBIN "gfx/pokedex_buttons.2bpp"
+ INCBIN "gfx/pokedex_pokeball.2bpp"
+ INCBIN "gfx/pokedex_cursors.2bpp"
+ INCBIN "gfx/gfx_44745.2bpp"
+
SECTION "Base Data", ROMX[$4F10],BANK[$14]
INCLUDE "data/pokemon/base_stats.asm"
@@ -61,6 +100,49 @@ SECTION "Annon Pic Ptrs and Pics", ROMX[$4d6a], BANK[$1f]
INCLUDE "gfx/pokemon/annon_pic_ptrs.asm"
INCLUDE "gfx/pokemon/annon_pics.asm"
+SECTION "Attack Animation GFX", ROMX[$4000], BANK[$21]
+ INCBIN "gfx/attack_animations.2bpp"
+
+SECTION "Pokemon Party Sprites", ROMX[$60CC], BANK[$23]
+ INCBIN "gfx/mon_party_sprites.2bpp"
+
+SECTION "Slot Machine GFX", ROMX[$4FDB], BANK[$24]
+ INCBIN "gfx/slot_machine.2bpp"
+ INCBIN "gfx/slot_machine_2.2bpp"
+
+SECTION "Poker GFX", ROMX[$5403], BANK[$38]
+ INCBIN "gfx/poker.2bpp"
+
+SECTION "15 Puzzle GFX", ROMX[$5F93], BANK[$38]
+ INCBIN "gfx/15_puzzle.2bpp"
+
+SECTION "Matches GFX", ROMX[$6606], BANK[$38]
+ INCBIN "gfx/matches.2bpp"
+
+SECTION "Picross GFX", ROMX[$75B7], BANK[$38]
+ INCBIN "gfx/picross.2bpp"
+ INCBIN "gfx/picross_cursor.2bpp"
+
+SECTION "Gamefreak Logo GFX", ROMX[$41FF], BANK[$39]
+ INCBIN "gfx/gamefreak_logo.1bpp"
+ INCBIN "gfx/gamefreak_logo_oam.2bpp"
+
+SECTION "Intro Underwater GFX", ROMX[$4ADF], BANK[$39]
+ INCBIN "gfx/intro_underwater.2bpp"
+
+SECTION "Intro Water Mon and Forest GFX", ROMX[$55EF], BANK[$39]
+ INCBIN "gfx/intro_water_pokemon.2bpp"
+ INCBIN "gfx/intro_forest.2bpp"
+
+SECTION "Intro Mon", ROMX[$626F], BANK[$39]
+ INCBIN "gfx/intro_purin_pika.2bpp"
+ INCBIN "gfx/intro_rizado_1.2bpp"
+ INCBIN "gfx/intro_rizado_2.2bpp"
+ INCBIN "gfx/intro_rizado_3.2bpp"
+ INCBIN "gfx/intro_rizado_flames.2bpp"
+ INCBIN "gfx/intro_kamekkusu.2bpp"
+ INCBIN "gfx/intro_fushigibana.2bpp"
+
SECTION "Misc GFX", ROMX[$4162], BANK[$3E]
FontExtraGfx::
INCBIN "gfx/font_extra.2bpp"
diff --git a/shim.sym b/shim.sym
index 7a1d48c..b75eddb 100644
--- a/shim.sym
+++ b/shim.sym
@@ -60,8 +60,6 @@
00:3E32 Unk_3E32 ; What the f*** is this???
01:53cc MainMenu
-01:5eb8 TitleFlameGfx
-01:5f38 TitleNoteGfx
01:6FEC ItemNames
02:4856 DecompTownMapTilemap
@@ -74,15 +72,7 @@
03:4791 DebugWarp
03:479f DebugWarp.Destinations
-04:47cf CopyrightGfx
-04:495f VersionGfx
-04:49ef PocketMonstersGfx
-04:4adf TitleHoOhGfx
-04:4def LogoGfx
04:5677 DoPlayerMovement
-04:5bb1 MailIconGfx
-04:7171 ProfileGfx
-04:73b1 ProfileBadgeGfx
0e:4b78 LoadTrainerClassName
0e:4ba0 LoadTrainerClassName.got_name
@@ -91,13 +81,8 @@
10:52A1 MoveNames
-11:40d5 DexGfx
-11:46d5 DexSearchGfx
-
14:6D75 PokemonNames
-21:4000 MoveEffectGfx
-
23:4349 OverworldFadeOut
23:60cc MenuMonIconGfx
diff --git a/tools/Makefile b/tools/Makefile
index 9dbf8bc..accee0f 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -5,7 +5,8 @@ CFLAGS := -O3 -std=c99 -Wall -Wextra
tools := \
scan_includes \
- pkmncompress
+ pkmncompress \
+ gfx
all: $(tools)
@:
diff --git a/tools/common.h b/tools/common.h
new file mode 100755
index 0000000..4da0b2e
--- /dev/null
+++ b/tools/common.h
@@ -0,0 +1,40 @@
+#ifndef GUARD_COMMON_H
+#define GUARD_COMMON_H
+
+int __getopt_long_i__;
+#define getopt_long(c, v, s, l) getopt_long(c, v, s, l, &__getopt_long_i__)
+
+FILE *fopen_verbose(char *filename, char *mode) {
+ FILE *f = fopen(filename, mode);
+ if (!f) {
+ fprintf(stderr, "Could not open file: \"%s\"\n", filename);
+ }
+ return f;
+}
+
+uint8_t *read_u8(char *filename, int *size) {
+ FILE *f = fopen_verbose(filename, "rb");
+ if (!f) {
+ exit(1);
+ }
+ fseek(f, 0, SEEK_END);
+ *size = ftell(f);
+ rewind(f);
+ uint8_t *data = malloc(*size);
+ if (*size != (int)fread(data, 1, *size, f)) {
+ fprintf(stderr, "Could not read file: \"%s\"\n", filename);
+ exit(1);
+ }
+ fclose(f);
+ return data;
+}
+
+void write_u8(char *filename, uint8_t *data, int size) {
+ FILE *f = fopen_verbose(filename, "wb");
+ if (f) {
+ fwrite(data, 1, size, f);
+ fclose(f);
+ }
+}
+
+#endif // GUARD_COMMON_H
diff --git a/tools/gfx.c b/tools/gfx.c
new file mode 100755
index 0000000..8c4066a
--- /dev/null
+++ b/tools/gfx.c
@@ -0,0 +1,296 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <getopt.h>
+#include <string.h>
+#include <stdint.h>
+
+#include "common.h"
+
+static void usage(void) {
+ 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) {
+ fputs(message, stderr);
+ fputs("\n", stderr);
+}
+
+struct Options {
+ int trim_whitespace;
+ int remove_whitespace;
+ int help;
+ char *outfile;
+ int depth;
+ int interleave;
+ int remove_duplicates;
+ int keep_whitespace;
+ int remove_xflip;
+ int remove_yflip;
+ char *png_file;
+};
+
+struct Options Options = {
+ .depth = 2,
+};
+
+void get_args(int argc, char *argv[]) {
+ struct option long_options[] = {
+ {"remove-whitespace", no_argument, &Options.remove_whitespace, 1},
+ {"trim-whitespace", no_argument, &Options.trim_whitespace, 1},
+ {"interleave", no_argument, &Options.interleave, 1},
+ {"remove-duplicates", no_argument, &Options.remove_duplicates, 1},
+ {"keep-whitespace", no_argument, &Options.keep_whitespace, 1},
+ {"remove-xflip", no_argument, &Options.remove_xflip, 1},
+ {"remove-yflip", no_argument, &Options.remove_yflip, 1},
+ {"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:p:", long_options)) {
+ case 'h':
+ Options.help = true;
+ break;
+ case 'o':
+ Options.outfile = optarg;
+ break;
+ case 'd':
+ Options.depth = strtoul(optarg, NULL, 0);
+ break;
+ case 'p':
+ Options.png_file = optarg;
+ break;
+ case 0:
+ case -1:
+ break;
+ default:
+ usage();
+ exit(1);
+ break;
+ }
+ }
+}
+
+struct Graphic {
+ int size;
+ uint8_t *data;
+};
+
+bool is_whitespace(uint8_t *tile, int tile_size) {
+ uint8_t WHITESPACE = 0;
+ for (int i = 0; i < tile_size; i++) {
+ if (tile[i] != WHITESPACE) {
+ return false;
+ }
+ }
+ return true;
+}
+
+void trim_whitespace(struct Graphic *graphic) {
+ int tile_size = Options.depth * 8;
+ for (int i = graphic->size - tile_size; i > 0; i -= tile_size) {
+ if (is_whitespace(&graphic->data[i], tile_size)) {
+ graphic->size = i;
+ } else {
+ break;
+ }
+ }
+}
+
+void remove_whitespace(struct Graphic *graphic) {
+ int tile_size = Options.depth * 8;
+ if (Options.interleave) tile_size *= 2;
+ int i = 0;
+ for (int j = 0; i < graphic->size && j < graphic->size; i += tile_size, j += tile_size) {
+ while (is_whitespace(&graphic->data[j], tile_size)) {
+ j += tile_size;
+ }
+ if (j >= graphic->size) {
+ break;
+ }
+ if (j > i) {
+ memcpy(&graphic->data[i], &graphic->data[j], tile_size);
+ }
+ }
+ graphic->size = i;
+}
+
+bool tile_exists(uint8_t *tile, uint8_t *tiles, int tile_size, int num_tiles) {
+ for (int i = 0; i < num_tiles; i++) {
+ bool match = true;
+ for (int j = 0; j < tile_size; j++) {
+ if (tile[j] != tiles[i * tile_size + j]) {
+ match = false;
+ }
+ }
+ if (match) {
+ return true;
+ }
+ }
+ return false;
+}
+
+void remove_duplicates(struct Graphic *graphic) {
+ int tile_size = Options.depth * 8;
+ if (Options.interleave) tile_size *= 2;
+ int num_tiles = 0;
+ for (int i = 0, j = 0; i < graphic->size && j < graphic->size; i += tile_size, j += tile_size) {
+ while (tile_exists(&graphic->data[j], graphic->data, tile_size, num_tiles)) {
+ if (Options.keep_whitespace && is_whitespace(&graphic->data[j], tile_size)) {
+ break;
+ }
+ j += tile_size;
+ }
+ if (j >= graphic->size) {
+ break;
+ }
+ if (j > i) {
+ memcpy(&graphic->data[i], &graphic->data[j], tile_size);
+ }
+ num_tiles++;
+ }
+ graphic->size = num_tiles * tile_size;
+}
+
+bool flip_exists(uint8_t *tile, uint8_t *tiles, int tile_size, int num_tiles, bool xflip, bool yflip) {
+ uint8_t *flip = calloc(tile_size, 1);
+ int half_size = tile_size / 2;
+ for (int i = 0; i < tile_size; i++) {
+ int byte = i;
+ if (yflip) {
+ byte = tile_size - 1 - (i ^ 1);
+ if (Options.interleave && i < half_size) {
+ byte = half_size - 1 - (i ^ 1);
+ }
+ }
+ if (xflip) {
+ for (int bit = 0; bit < 8; bit++) {
+ flip[byte] |= ((tile[i] >> bit) & 1) << (7 - bit);
+ }
+ } else {
+ flip[byte] = tile[i];
+ }
+ }
+ if (tile_exists(flip, tiles, tile_size, num_tiles)) {
+ return true;
+ }
+ return false;
+}
+
+void remove_flip(struct Graphic *graphic, bool xflip, bool yflip) {
+ int tile_size = Options.depth * 8;
+ if (Options.interleave) tile_size *= 2;
+ int num_tiles = 0;
+ for (int i = 0, j = 0; i < graphic->size && j < graphic->size; i += tile_size, j += tile_size) {
+ while (flip_exists(&graphic->data[j], graphic->data, tile_size, num_tiles, xflip, yflip)) {
+ if (Options.keep_whitespace && is_whitespace(&graphic->data[j], tile_size)) {
+ break;
+ }
+ j += tile_size;
+ }
+ if (j >= graphic->size) {
+ break;
+ }
+ if (j > i) {
+ memcpy(&graphic->data[i], &graphic->data[j], tile_size);
+ }
+ num_tiles++;
+ }
+ graphic->size = num_tiles * tile_size;
+}
+
+void interleave(struct Graphic *graphic, int width) {
+ int tile_size = Options.depth * 8;
+ int width_tiles = width / 8;
+ int num_tiles = graphic->size / tile_size;
+ uint8_t *interleaved = malloc(graphic->size);
+ for (int i = 0; i < num_tiles; i++) {
+ int tile = i * 2;
+ int row = i / width_tiles;
+ tile -= width_tiles * row;
+ if (row % 2) {
+ tile -= width_tiles;
+ tile += 1;
+ }
+ memcpy(&interleaved[tile * tile_size], &graphic->data[i * tile_size], tile_size);
+ }
+ graphic->size = num_tiles * tile_size;
+ memcpy(graphic->data, interleaved, graphic->size);
+ 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);
+ size_t size = 4;
+ size_t result = fread(bytes, 1, size, f);
+ fclose(f);
+ if (result != size) {
+ fprintf(stderr, "Could not read file at offset 0x%x: \"%s\"\n", OFFSET_WIDTH, filename);
+ exit(1);
+ }
+
+ 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;
+ argv += optind;
+ if (Options.help) {
+ usage();
+ return 0;
+ }
+ if (argc < 1) {
+ usage();
+ exit(1);
+ }
+ char *infile = argv[0];
+ struct Graphic graphic;
+ graphic.data = read_u8(infile, &graphic.size);
+ if (Options.trim_whitespace) {
+ trim_whitespace(&graphic);
+ }
+ if (Options.interleave) {
+ if (!Options.png_file) {
+ error("interleave: need --png to infer dimensions");
+ usage();
+ exit(1);
+ }
+ int width = png_get_width(Options.png_file);
+ interleave(&graphic, width);
+ }
+ if (Options.remove_duplicates) {
+ remove_duplicates(&graphic);
+ }
+ if (Options.remove_xflip) {
+ remove_flip(&graphic, true, false);
+ }
+ if (Options.remove_yflip) {
+ remove_flip(&graphic, false, true);
+ }
+ if (Options.remove_xflip && Options.remove_yflip) {
+ remove_flip(&graphic, true, true);
+ }
+ if (Options.remove_whitespace) {
+ remove_whitespace(&graphic);
+ }
+ if (Options.outfile) {
+ write_u8(Options.outfile, graphic.data, graphic.size);
+ }
+ free(graphic.data);
+ return 0;
+}