Use rgbgfx and c tools instead of the submodule.

This commit is contained in:
yenatch 2018-03-10 18:01:54 -05:00
parent 37ded1d150
commit f75049feef
33 changed files with 1006 additions and 40 deletions

View File

@ -1,20 +1,13 @@
PYTHON := python
MD5 := md5sum -c --quiet
2bpp := $(PYTHON) extras/pokemontools/gfx.py 2bpp
1bpp := $(PYTHON) extras/pokemontools/gfx.py 1bpp
pic := $(PYTHON) extras/pokemontools/pic.py compress
includes := $(PYTHON) extras/pokemontools/scan_includes.py
MD5 := md5sum -c
pokered_obj := audio_red.o main_red.o text_red.o wram_red.o
pokeblue_obj := audio_blue.o main_blue.o text_blue.o wram_blue.o
.SUFFIXES:
.SUFFIXES: .asm .o .gbc .png .2bpp .1bpp .pic
.SECONDEXPANSION:
# Suppress annoying intermediate file deletion messages.
.PRECIOUS: %.2bpp
.PHONY: all clean red blue compare
.PRECIOUS:
.SECONDARY:
.PHONY: all clean red blue compare tools
roms := pokered.gbc pokeblue.gbc
@ -29,14 +22,26 @@ compare: red blue
clean:
rm -f $(roms) $(pokered_obj) $(pokeblue_obj) $(roms:.gbc=.sym)
find . \( -iname '*.1bpp' -o -iname '*.2bpp' -o -iname '*.pic' \) -exec rm {} +
#$(MAKE) clean -C tools/
tools:
$(MAKE) -C tools/
# Build tools when building the rom.
# This has to happen before the rules are processed, since that's when scan_includes is run.
ifeq (,$(filter clean tools,$(MAKECMDGOALS)))
$(info $(shell $(MAKE) -C tools))
endif
%.asm: ;
%_red.o: dep = $(shell $(includes) $(@D)/$*.asm)
%_red.o: dep = $(shell tools/scan_includes $(@D)/$*.asm)
$(pokered_obj): %_red.o: %.asm $$(dep)
rgbasm -D _RED -h -o $@ $*.asm
%_blue.o: dep = $(shell $(includes) $(@D)/$*.asm)
%_blue.o: dep = $(shell tools/scan_includes $(@D)/$*.asm)
$(pokeblue_obj): %_blue.o: %.asm $$(dep)
rgbasm -D _BLUE -h -o $@ $*.asm
@ -48,7 +53,26 @@ pokeblue_opt = -jsv -k 01 -l 0x33 -m 0x13 -p 0 -r 03 -t "POKEMON BLUE"
rgbfix $($*_opt) $@
sort $*.sym -o $*.sym
%.png: ;
%.2bpp: %.png ; @$(2bpp) $<
%.1bpp: %.png ; @$(1bpp) $<
%.pic: %.2bpp ; @$(pic) $<
gfx/blue/intro_purin_1.2bpp: rgbgfx += -h
gfx/blue/intro_purin_2.2bpp: rgbgfx += -h
gfx/blue/intro_purin_3.2bpp: rgbgfx += -h
gfx/red/intro_nido_1.2bpp: rgbgfx += -h
gfx/red/intro_nido_2.2bpp: rgbgfx += -h
gfx/red/intro_nido_3.2bpp: rgbgfx += -h
gfx/game_boy.2bpp: tools/gfx += --remove-duplicates
gfx/theend.2bpp: tools/gfx += --interleave --png=$<
gfx/tilesets/%.2bpp: tools/gfx += --trim-whitespace
%.png: ;
%.2bpp: %.png
rgbgfx $(rgbgfx) -o $@ $<
$(if $(tools/gfx),\
tools/gfx $(tools/gfx) -o $@ $@)
%.1bpp: %.png
rgbgfx -d1 $(rgbgfx) -o $@ $<
$(if $(tools/gfx),\
tools/gfx $(tools/gfx) -d1 -o $@ $@)
%.pic: %.2bpp
tools/pkmncompress $< $@

View File

@ -266,5 +266,5 @@ INCLUDE "data/credits_order.asm"
INCLUDE "text/credits_text.asm"
TheEndGfx:
INCBIN "gfx/theend.interleave.2bpp"
INCBIN "gfx/theend.2bpp"
TheEndGfxEnd:

View File

@ -443,14 +443,14 @@ FightIntroBackMonEnd:
FightIntroFrontMon:
IF DEF(_RED)
INCBIN "gfx/red/intro_nido_1.6x6.2bpp"
INCBIN "gfx/red/intro_nido_2.6x6.2bpp"
INCBIN "gfx/red/intro_nido_3.6x6.2bpp"
INCBIN "gfx/red/intro_nido_1.2bpp"
INCBIN "gfx/red/intro_nido_2.2bpp"
INCBIN "gfx/red/intro_nido_3.2bpp"
ENDC
IF DEF(_BLUE)
INCBIN "gfx/blue/intro_purin_1.6x6.2bpp"
INCBIN "gfx/blue/intro_purin_2.6x6.2bpp"
INCBIN "gfx/blue/intro_purin_3.6x6.2bpp"
INCBIN "gfx/blue/intro_purin_1.2bpp"
INCBIN "gfx/blue/intro_purin_2.2bpp"
INCBIN "gfx/blue/intro_purin_3.2bpp"
ENDC
FightIntroFrontMonEnd:

View File

Before

Width:  |  Height:  |  Size: 366 B

After

Width:  |  Height:  |  Size: 366 B

View File

Before

Width:  |  Height:  |  Size: 360 B

After

Width:  |  Height:  |  Size: 360 B

View File

Before

Width:  |  Height:  |  Size: 416 B

After

Width:  |  Height:  |  Size: 416 B

View File

Before

Width:  |  Height:  |  Size: 246 B

After

Width:  |  Height:  |  Size: 246 B

View File

Before

Width:  |  Height:  |  Size: 462 B

After

Width:  |  Height:  |  Size: 462 B

View File

Before

Width:  |  Height:  |  Size: 449 B

After

Width:  |  Height:  |  Size: 449 B

View File

Before

Width:  |  Height:  |  Size: 495 B

After

Width:  |  Height:  |  Size: 495 B

View File

Before

Width:  |  Height:  |  Size: 139 B

After

Width:  |  Height:  |  Size: 139 B

View File

Before

Width:  |  Height:  |  Size: 1022 B

After

Width:  |  Height:  |  Size: 1022 B

View File

Before

Width:  |  Height:  |  Size: 928 B

After

Width:  |  Height:  |  Size: 928 B

View File

Before

Width:  |  Height:  |  Size: 899 B

After

Width:  |  Height:  |  Size: 899 B

View File

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

Before

Width:  |  Height:  |  Size: 1.0 KiB

After

Width:  |  Height:  |  Size: 1.0 KiB

View File

Before

Width:  |  Height:  |  Size: 966 B

After

Width:  |  Height:  |  Size: 966 B

View File

Before

Width:  |  Height:  |  Size: 960 B

After

Width:  |  Height:  |  Size: 960 B

View File

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

Before

Width:  |  Height:  |  Size: 1011 B

After

Width:  |  Height:  |  Size: 1011 B

View File

Before

Width:  |  Height:  |  Size: 845 B

After

Width:  |  Height:  |  Size: 845 B

View File

Before

Width:  |  Height:  |  Size: 865 B

After

Width:  |  Height:  |  Size: 865 B

View File

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

Before

Width:  |  Height:  |  Size: 272 B

After

Width:  |  Height:  |  Size: 272 B

View File

@ -895,7 +895,7 @@ INCLUDE "engine/battle/trainer_ai.asm"
INCLUDE "engine/battle/draw_hud_pokeball_gfx.asm"
TradingAnimationGraphics:
INCBIN "gfx/game_boy.norepeat.2bpp"
INCBIN "gfx/game_boy.2bpp"
INCBIN "gfx/link_cable.2bpp"
TradingAnimationGraphicsEnd:
@ -1846,23 +1846,24 @@ INCLUDE "engine/hidden_object_functions18.asm"
SECTION "bank19",ROMX,BANK[$19]
Overworld_GFX: INCBIN "gfx/tilesets/overworld.t2.2bpp"
Overworld_GFX: INCBIN "gfx/tilesets/overworld.2bpp"
Overworld_Block: INCBIN "gfx/blocksets/overworld.bst"
RedsHouse1_GFX:
RedsHouse2_GFX: INCBIN "gfx/tilesets/reds_house.t7.2bpp"
RedsHouse2_GFX: INCBIN "gfx/tilesets/reds_house.2bpp"
ds 16
RedsHouse1_Block:
RedsHouse2_Block: INCBIN "gfx/blocksets/reds_house.bst"
House_GFX: INCBIN "gfx/tilesets/house.t2.2bpp"
House_GFX: INCBIN "gfx/tilesets/house.2bpp"
House_Block: INCBIN "gfx/blocksets/house.bst"
Mansion_GFX: INCBIN "gfx/tilesets/mansion.t2.2bpp"
Mansion_GFX: INCBIN "gfx/tilesets/mansion.2bpp"
Mansion_Block: INCBIN "gfx/blocksets/mansion.bst"
ShipPort_GFX: INCBIN "gfx/tilesets/ship_port.t2.2bpp"
ShipPort_GFX: INCBIN "gfx/tilesets/ship_port.2bpp"
ShipPort_Block: INCBIN "gfx/blocksets/ship_port.bst"
Interior_GFX: INCBIN "gfx/tilesets/interior.t1.2bpp"
Interior_GFX: INCBIN "gfx/tilesets/interior.2bpp"
Interior_Block: INCBIN "gfx/blocksets/interior.bst"
Plateau_GFX: INCBIN "gfx/tilesets/plateau.t10.2bpp"
Plateau_GFX: INCBIN "gfx/tilesets/plateau.2bpp"
Plateau_Block: INCBIN "gfx/blocksets/plateau.bst"
@ -1891,7 +1892,7 @@ Pokecenter_Block: INCBIN "gfx/blocksets/pokecenter.bst"
ForestGate_GFX:
Museum_GFX:
Gate_GFX: INCBIN "gfx/tilesets/gate.t1.2bpp"
Gate_GFX: INCBIN "gfx/tilesets/gate.2bpp"
ForestGate_Block:
Museum_Block:
Gate_Block: INCBIN "gfx/blocksets/gate.bst"
@ -1904,19 +1905,19 @@ Facility_Block: INCBIN "gfx/blocksets/facility.bst"
SECTION "bank1B",ROMX,BANK[$1B]
Cemetery_GFX: INCBIN "gfx/tilesets/cemetery.t4.2bpp"
Cemetery_GFX: INCBIN "gfx/tilesets/cemetery.2bpp"
Cemetery_Block: INCBIN "gfx/blocksets/cemetery.bst"
Cavern_GFX: INCBIN "gfx/tilesets/cavern.t14.2bpp"
Cavern_GFX: INCBIN "gfx/tilesets/cavern.2bpp"
Cavern_Block: INCBIN "gfx/blocksets/cavern.bst"
Lobby_GFX: INCBIN "gfx/tilesets/lobby.t2.2bpp"
Lobby_GFX: INCBIN "gfx/tilesets/lobby.2bpp"
Lobby_Block: INCBIN "gfx/blocksets/lobby.bst"
Ship_GFX: INCBIN "gfx/tilesets/ship.t6.2bpp"
Ship_GFX: INCBIN "gfx/tilesets/ship.2bpp"
Ship_Block: INCBIN "gfx/blocksets/ship.bst"
Lab_GFX: INCBIN "gfx/tilesets/lab.t4.2bpp"
Lab_GFX: INCBIN "gfx/tilesets/lab.2bpp"
Lab_Block: INCBIN "gfx/blocksets/lab.bst"
Club_GFX: INCBIN "gfx/tilesets/club.t5.2bpp"
Club_GFX: INCBIN "gfx/tilesets/club.2bpp"
Club_Block: INCBIN "gfx/blocksets/club.bst"
Underground_GFX: INCBIN "gfx/tilesets/underground.t7.2bpp"
Underground_GFX: INCBIN "gfx/tilesets/underground.2bpp"
Underground_Block: INCBIN "gfx/blocksets/underground.bst"

3
tools/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
scan_includes
gfx
pkmncompress

16
tools/Makefile Normal file
View File

@ -0,0 +1,16 @@
.PHONY: all clean
CC := gcc
CFLAGS := -O3 -std=c99 -Wall -Wextra
tools := scan_includes gfx pkmncompress
all: $(tools)
@:
clean:
rm -f $(tools)
gfx: common.h
%: %.c
$(CC) $(CFLAGS) -o $@ $<

40
tools/common.h Normal file
View File

@ -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

296
tools/gfx.c Normal file
View File

@ -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;
}

451
tools/pkmncompress.c Normal file
View File

@ -0,0 +1,451 @@
/*
* Copyright © 2013 stag019 <stag019@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
typedef uint8_t u8;
u8 *compressed = NULL;
int xrows = 0;
int xwidth = 0;
int curbit = 0;
int curbyte = 0;
void writebit(int bit)
{
if (++curbit == 8)
{
curbyte++;
curbit = 0;
}
compressed[curbyte] |= bit << (7 - curbit);
}
void method_1(u8 *RAM)
{
int i;
int j;
int nibble_1;
int nibble_2 = 0;
int code_1;
int code_2;
int table;
static int method_1[2][0x10] = {
{0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4, 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9, 0x8},
{0x8, 0x9, 0xB, 0xA, 0xE, 0xF, 0xD, 0xC, 0x4, 0x5, 0x7, 0x6, 0x2, 0x3, 0x1, 0x0}
};
for (i = 0; i < xrows * xwidth * 8; i++)
{
j = i / xrows;
j += i % xrows * xwidth * 8;
if (!(i % xrows))
{
nibble_2 = 0;
}
nibble_1 = (RAM[j] >> 4) & 0x0F;
table = 0;
if (nibble_2 & 1)
{
table = 1;
}
code_1 = method_1[table][nibble_1];
nibble_2 = RAM[j] & 0x0F;
table = 0;
if (nibble_1 & 1)
{
table = 1;
}
code_2 = method_1[table][nibble_2];
RAM[j] = (code_1 << 4) | code_2;
}
}
// "Get the previous power of 2. Deriving the bitcount from that seems to be faster on average than using the lookup table."
void RLE(int nums)
{
int v;
int j;
int bitcount;
int number;
bitcount = -1;
v = ++nums;
v++;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v -= v >> 1;
v--;
number = nums - v;
while(v) {
v >>= 1;
bitcount++;
}
for(j = 0; j < bitcount; j++) {
writebit(1);
}
writebit(0);
for(j = bitcount; j >= 0; j--) {
writebit((number >> j) & 1);
}
}
void RLE_old(int nums)
{
int search;
int i;
int j;
int bitcount;
int number;
static int RLE[0x10] = {0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF};
bitcount = -1;
search = ++nums;
while (search > 0)
{
for (i = 0; i < 0xF; i++)
{
if (RLE[i] == search)
{
bitcount = i;
break;
}
}
if (bitcount != -1)
{
break;
}
search--;
}
number = nums - RLE[bitcount];
for (j = 0; j < bitcount; j++)
{
writebit(1);
}
writebit(0);
for (j = bitcount; j >= 0; j--)
{
writebit((number >> j) & 1);
}
}
void data_packet(u8 *bitgroups, int bgi)
{
int i;
for (i = 0; i < bgi; i++)
{
writebit((bitgroups[i] >> 1) & 1);
writebit(bitgroups[i] & 1);
}
}
int interpret_compress(u8 *RAM_1, u8 *RAM_2, int interpretation, int switchram)
{
u8 *_1_RAM;
u8 *_2_RAM;
int i;
int ram;
int type;
int nums;
u8 *bitgroups;
int x;
int y;
int byte;
int bit;
int bitgroup;
int bgi = 0;
int ram_size = xrows * xwidth * 8;
_1_RAM = (u8 *)calloc(ram_size, 1);
_2_RAM = (u8 *)calloc(ram_size, 1);
if (switchram)
{
memcpy(_1_RAM, RAM_2, ram_size);
memcpy(_2_RAM, RAM_1, ram_size);
}
else
{
memcpy(_1_RAM, RAM_1, ram_size);
memcpy(_2_RAM, RAM_2, ram_size);
}
switch(interpretation)
{
case 1:
method_1(_1_RAM);
method_1(_2_RAM);
break;
case 2:
case 3:
for (i = 0; i < xrows * xwidth * 8; i++)
{
_2_RAM[i] ^= _1_RAM[i];
}
method_1(_1_RAM);
break;
}
if (interpretation == 3)
{
method_1(_2_RAM);
}
curbit = 7;
curbyte = 0;
compressed = (u8 *)calloc(0x310, 1);
compressed[0] = (xrows << 4) | xwidth;
writebit(switchram);
for (ram = 0; ram < 2; ram++)
{
type = 0;
nums = 0;
bitgroups = (u8 *)calloc(0x1000, 1);
for (x = 0; x < xwidth; x++)
{
for (bit = 0; bit < 8; bit += 2)
{
byte = x * xrows * 8;
for (y=0; y < xrows * 8; y++)
{
if (ram)
{
bitgroup = (_2_RAM[byte] >> (6 - bit)) & 3;
}
else
{
bitgroup = (_1_RAM[byte] >> (6 - bit)) & 3;
}
if (!bitgroup)
{
if (!type)
{
writebit(0);
}
else if (type == 1)
{
nums++;
}
else
{
data_packet(bitgroups, bgi);
writebit(0);
writebit(0);
}
type = 1;
free(bitgroups);
bitgroups = (u8 *)calloc(0x1000, 1);
bgi = 0;
}
else
{
if (!type)
{
writebit(1);
}
else if (type == 1)
{
RLE(nums);
}
type = -1;
bitgroups[bgi++] = bitgroup;
nums = 0;
}
byte++;
}
}
}
if (type == 1)
{
RLE(nums);
}
else
{
data_packet(bitgroups, bgi);
}
if (!ram)
{
if (interpretation < 2)
{
writebit(0);
}
else
{
writebit(1);
writebit(interpretation - 2);
}
}
}
free(bitgroups);
free(_1_RAM);
free(_2_RAM);
return (curbyte + 1) * 8 + curbit;
}
int compress(u8 *data, int width, int height)
{
u8 *RAM_1;
u8 *RAM_2;
int i;
int mode;
int order;
int newsize;
int compressedsize;
int size = -1;
u8 *current = NULL;
int ram_size;
xrows = height;
xwidth = width;
ram_size = xrows * xwidth * 8;
RAM_1 = (u8 *)calloc(ram_size, 1);
RAM_2 = (u8 *)calloc(ram_size, 1);
for (i = 0; i < xrows * xwidth * 8; i++)
{
RAM_1[i] = data[(i << 1)];
RAM_2[i] = data[(i << 1) | 1];
}
for (mode = 1; mode < 4; mode++)
{
for (order = 0; order < 2; order++)
{
if (!(mode == 1 && order == 0))
{
newsize = interpret_compress(RAM_1, RAM_2, mode, order);
if (size == -1 || newsize < size)
{
if (current != NULL)
{
free(current);
}
current = (u8 *)calloc(0x310, 1);
memcpy(current, compressed, newsize / 8);
free(compressed);
size = newsize;
}
}
}
}
compressed = (u8 *)calloc(0x310, 1);
compressedsize = size / 8;
memcpy(compressed, current, compressedsize);
free(current);
free(RAM_1);
free(RAM_2);
return compressedsize;
}
uint8_t *transpose_tiles(uint8_t *data, int width, int height)
{
int i;
int j;
int tile_size = 0x10;
int size = width * height * tile_size;
u8 *transposed = calloc(size, 1);
for (i = 0; i < size; i++)
{
j = (i / 0x10) * width * 0x10;
j = (j % size) + 0x10 * (j / size) + (i % 0x10);
transposed[j] = data[i];
}
free(data);
return transposed;
}
int main(int argc, char *argv[])
{
int width = 0;
int height = 0;
int transpose = 1;
if (argc != 3)
{
fputs("Usage: pkmncompress infile.2bpp outfile.pic\n", stderr);
return EXIT_FAILURE;
}
char *infile = argv[1];
char *outfile = argv[2];
FILE *f = fopen(infile, "rb");
if (!f) {
fprintf(stderr, "failed to open for reading: '%s'\n", infile);
return EXIT_FAILURE;
}
fseek(f, 0, SEEK_END);
int filesize = ftell(f);
for (int i = 0; i < 32; i++) {
width = i;
height = i;
if (width * height * 16 >= filesize) {
break;
}
}
if (width * height * 16 < filesize) {
fprintf(stderr, "file too big: '%s' (%x)\n", infile, filesize);
return EXIT_FAILURE;
}
if (width * height * 16 > filesize) {
fprintf(stderr, "wrong filesize for '%s' (%x). must be a square image of 16-byte tiles\n", infile, filesize);
return EXIT_FAILURE;
}
u8 *data = (u8 *)calloc(filesize, 1);
fseek(f, 0, SEEK_SET);
int size = fread(data, 1, filesize, f);
fclose(f);
if (size != filesize) {
fprintf(stderr, "failed to read: '%s'\n", infile);
return EXIT_FAILURE;
}
if (transpose) {
data = transpose_tiles(data, width, height);
}
int compressed_size = compress(data, width, height);
free(data);
f = fopen(outfile, "wb");
if (!f) {
fprintf(stderr, "failed to open for writing: '%s'\n", outfile);
return EXIT_FAILURE;
}
fwrite(compressed, 1, compressed_size, f);
fclose(f);
free(compressed);
return EXIT_SUCCESS;
}

135
tools/scan_includes.c Normal file
View File

@ -0,0 +1,135 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <getopt.h>
void usage(void) {
printf("Usage: scan_includes [-h] [-s] filename\n"
"-h, --help\n"
" Print usage and exit\n"
"-s, --strict\n"
" Fail if a file cannot be read\n");
}
struct Options {
bool help;
bool strict;
};
struct Options Options = {0};
void scan_file(char* filename) {
FILE *f = fopen(filename, "rb");
if (!f) {
if (Options.strict) {
fprintf(stderr, "Could not open file: '%s'\n", filename);
exit(1);
} else {
return;
}
}
fseek(f, 0, SEEK_END);
long size = ftell(f);
rewind(f);
char *buffer = malloc(size + 1);
char *orig = buffer;
size = fread(buffer, 1, size, f);
buffer[size] = '\0';
fclose(f);
for (; buffer && (buffer - orig < size); buffer++) {
bool is_include = false;
bool is_incbin = false;
switch (*buffer) {
case ';':
buffer = strchr(buffer, '\n');
if (!buffer) {
fprintf(stderr, "%s: no newline at end of file\n", filename);
break;
}
break;
case '"':
buffer++;
buffer = strchr(buffer, '"');
if (!buffer) {
fprintf(stderr, "%s: unterminated string\n", filename);
break;
}
buffer++;
break;
case 'i':
case 'I':
if ((strncmp(buffer, "INCBIN", 6) == 0) || (strncmp(buffer, "incbin", 6) == 0)) {
is_incbin = true;
} else if ((strncmp(buffer, "INCLUDE", 7) == 0) || (strncmp(buffer, "include", 7) == 0)) {
is_include = true;
}
if (is_incbin || is_include) {
buffer = strchr(buffer, '"');
if (!buffer) {
break;
}
buffer++;
int length = strcspn(buffer, "\"");
char *include = malloc(length + 1);
strncpy(include, buffer, length);
include[length] = '\0';
printf("%s ", include);
if (is_include) {
scan_file(include);
}
free(include);
buffer = strchr(buffer, '"');
}
break;
}
if (!buffer) {
break;
}
}
free(orig);
}
int main(int argc, char* argv[]) {
int i = 0;
struct option long_options[] = {
{"strict", no_argument, 0, 's'},
{"help", no_argument, 0, 'h'},
{0}
};
int opt = -1;
while ((opt = getopt_long(argc, argv, "sh", long_options, &i)) != -1) {
switch (opt) {
case 's':
Options.strict = true;
break;
case 'h':
Options.help = true;
break;
default:
usage();
exit(1);
break;
}
}
argc -= optind;
argv += optind;
if (Options.help) {
usage();
return 0;
}
if (argc < 1) {
usage();
exit(1);
}
scan_file(argv[0]);
return 0;
}