shithub: rgbds

Download patch

ref: c3c31138ddbd8680d4e67957e387f2816798a71b
parent: 330a39596c51d8412a5a344054df24f00318f68f
author: Anthony J. Bentley <[email protected]>
date: Thu Jan 28 23:30:53 EST 2016

Add rgbgfx.

--- a/LICENSE
+++ b/LICENSE
@@ -18,6 +18,8 @@
 rgbfix was rewritten from scratch by Anthony J. Bentley, and is released
 under the ISC license; see the source file for the text of the license.
 
+rgbgfx was written by stag019, and is released under the ISC license.
+
 The UTF-8 decoder in src/asm/charmap.c was written by Björn Höhrmann and is
 released under the MIT license. The remainder of charmap.c was written by
 stag019, and is released under the ISC license.
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
-.POSIX:
-
+PKG_CONFIG =	pkg-config
 WARNFLAGS =	-Wall -Werror=implicit
-REALCFLAGS =	${CFLAGS} ${WARNFLAGS} -Iinclude -g \
+PNGFLAGS !=	${PKG_CONFIG} --cflags libpng
+REALCFLAGS =	${CFLAGS} ${WARNFLAGS} ${PNGFLAGS} -Iinclude -g \
 		-std=c99 -D_POSIX_C_SOURCE=200809L
 
 # User-defined variables
@@ -9,6 +9,7 @@
 BINPREFIX =	${PREFIX}/bin
 MANPREFIX =	${PREFIX}/man
 Q =		@
+PKG_CONFIG =	pkg-config
 
 rgbasm_obj = \
 	src/asm/asmy.o \
@@ -42,13 +43,20 @@
 	src/fix/main.o \
 	src/extern/err.o
 
-all: rgbasm rgblink rgbfix
+rgbgfx_obj = \
+	src/gfx/gb.o \
+	src/gfx/main.o \
+	src/gfx/png.o \
+	src/extern/err.o
 
+all: rgbasm rgblink rgbfix rgbgfx
+
 clean:
 	$Qrm -rf rgbds.html
 	$Qrm -rf rgbasm rgbasm.exe ${rgbasm_obj} rgbasm.html
 	$Qrm -rf rgblink rgblink.exe ${rgblink_obj} rgblink.html
 	$Qrm -rf rgbfix rgbfix.exe ${rgbfix_obj} rgbfix.html
+	$Qrm -rf rgbgfx rgbgfx.exe ${rgbgfx_obj} rgbgfx.html
 	$Qrm -rf src/asm/asmy.c src/asm/asmy.h
 
 install: all
@@ -56,11 +64,13 @@
 	$Qinstall -s -m 555 rgbasm ${BINPREFIX}/rgbasm
 	$Qinstall -s -m 555 rgbfix ${BINPREFIX}/rgbfix
 	$Qinstall -s -m 555 rgblink ${BINPREFIX}/rgblink
+	$Qinstall -s -m 555 rgbgfx ${BINPREFIX}/rgbgfx
 	$Qmkdir -p ${MANPREFIX}/man1 ${MANPREFIX}/man7
 	$Qinstall -m 444 src/rgbds.7 ${MANPREFIX}/man7/rgbds.7
 	$Qinstall -m 444 src/asm/rgbasm.1 ${MANPREFIX}/man1/rgbasm.1
 	$Qinstall -m 444 src/fix/rgbfix.1 ${MANPREFIX}/man1/rgbfix.1
 	$Qinstall -m 444 src/link/rgblink.1 ${MANPREFIX}/man1/rgblink.1
+	$Qinstall -m 444 src/gfx/rgbgfx.1 ${MANPREFIX}/man1/rgbgfx.1
 
 rgbasm: ${rgbasm_obj}
 	$Q${CC} ${REALCFLAGS} -o $@ ${rgbasm_obj} -lm
@@ -71,6 +81,9 @@
 rgbfix: ${rgbfix_obj}
 	$Q${CC} ${REALCFLAGS} -o $@ ${rgbfix_obj}
 
+rgbgfx: ${rgbgfx_obj}
+	$Q${CC} `${PKG_CONFIG} --libs libpng` ${REALCFLAGS} -o $@ ${rgbgfx_obj}
+
 .y.c:
 	$Q${YACC} -d ${YFLAGS} -o $@ $<
 
@@ -91,6 +104,7 @@
 	$Qmv rgbasm rgbasm.exe
 	$Qmv rgblink rgblink.exe
 	$Qmv rgbfix rgbfix.exe
+	$Qmv rgbgfx rgbgfx.exe
 
 # Below is a target for the project maintainer to easily create web manuals.
 # It relies on mandoc: http://mdocml.bsd.lv
@@ -105,3 +119,5 @@
 		rgbfix.html
 	$Qmandoc ${MANDOC} src/link/rgblink.1 | sed s/OpenBSD/General/ > \
 		rgblink.html
+	$Qmandoc ${MANDOC} src/gfx/rgbgfx.1 | sed s/OpenBSD/General/ > \
+		rgbgfx.html
--- a/README
+++ b/README
@@ -8,6 +8,7 @@
   - rgbasm  (assembler)
   - rgblink (linker)
   - rgbfix  (checksum/header fixer)
+  - rgbgfx  (PNG‐to‐Game Boy graphics converter)
 
 rgbds-linux is a fork of the original RGBDS which aims to make the programs
 more like other UNIX tools.
--- /dev/null
+++ b/include/gfx/gb.h
@@ -1,0 +1,30 @@
+/*
+ * Copyright © 2013 stag019 <[email protected]>
+ *
+ * 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.
+ */
+
+#ifndef RGBDS_GFX_GB_H
+#define RGBDS_GFX_GB_H
+
+#include <stdint.h>
+#include "gfx/main.h"
+
+void png_to_gb(struct PNGImage png, struct GBImage *gb);
+void output_file(struct Options opts, struct GBImage gb);
+int get_tile_index(uint8_t *tile, uint8_t **tiles, int num_tiles, int tile_size);
+void create_tilemap(struct Options opts, struct GBImage *gb, struct Tilemap *tilemap);
+void output_tilemap_file(struct Options opts, struct Tilemap tilemap);
+void output_palette_file(struct Options opts, struct PNGImage png);
+
+#endif
--- /dev/null
+++ b/include/gfx/main.h
@@ -1,0 +1,75 @@
+/*
+ * Copyright © 2013 stag019 <[email protected]>
+ *
+ * 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.
+ */
+
+#ifndef RGBDS_GFX_MAIN_H
+#define RGBDS_GFX_MAIN_H
+
+#include <png.h>
+#include <stdbool.h>
+#include <stdint.h>
+
+#include "extern/err.h"
+
+struct Options {
+	bool debug;
+	bool verbose;
+	bool hardfix;
+	bool fix;
+	bool horizontal;
+	bool unique;
+	int trim;
+	char *mapfile;
+	bool mapout;
+	char *palfile;
+	bool palout;
+	char *outfile;
+	char *infile;
+};
+
+struct PNGImage {
+	png_struct *png;
+	png_info *info;
+	png_byte **data;
+	int width;
+	int height;
+	png_byte depth;
+	png_byte type;
+	bool horizontal;
+	int trim;
+	char *mapfile;
+	bool mapout;
+	char *palfile;
+	bool palout;
+};
+
+struct GBImage {
+	uint8_t *data;
+	int size;
+	bool horizontal;
+	int trim;
+};
+
+struct Tilemap {
+	uint8_t *data;
+	int size;
+};
+
+int depth, colors;
+
+#include "gfx/png.h"
+#include "gfx/gb.h"
+
+#endif
--- /dev/null
+++ b/include/gfx/png.h
@@ -1,0 +1,28 @@
+/*
+ * Copyright © 2013 stag019 <[email protected]>
+ *
+ * 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.
+ */
+
+#ifndef RGBDS_GFX_PNG_H
+#define RGBDS_GFX_PNG_H
+
+#include "gfx/main.h"
+
+void input_png_file(struct Options opts, struct PNGImage *img);
+void get_text(struct PNGImage *png);
+void set_text(struct PNGImage *png);
+void output_png_file(struct Options opts, struct PNGImage *png);
+void free_png_data(struct PNGImage *png);
+
+#endif
--- /dev/null
+++ b/src/gfx/gb.c
@@ -1,0 +1,203 @@
+/*
+ * Copyright © 2013 stag019 <[email protected]>
+ *
+ * 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 <stdio.h>
+#include <stdlib.h>
+
+#include "gfx/main.h"
+
+void
+transpose_tiles(struct GBImage *gb, int width)
+{
+	uint8_t *newdata;
+	int i;
+	int newbyte;
+
+	newdata = calloc(gb->size, 1);
+	for (i = 0; i < gb->size; i++) {
+		newbyte = i / (8 * depth) * width * 8 * depth;
+		newbyte = newbyte % gb->size + 8 * depth * (newbyte / gb->size) + i % (8 * depth);
+		newdata[newbyte] = gb->data[i];
+	}
+
+	free(gb->data);
+
+	gb->data = newdata;
+}
+
+void
+png_to_gb(struct PNGImage png, struct GBImage *gb)
+{
+	int x, y, byte;
+	png_byte index;
+
+	for (y = 0; y < png.height; y++) {
+		for (x = 0; x < png.width; x++) {
+			index = png.data[y][x];
+			index &= (1 << depth) - 1;
+
+			if (!gb->horizontal) {
+				byte = y * depth + x / 8 * png.height / 8 * 8 * depth;
+			} else {
+				byte = y * depth + x / 8 * png.height / 8 * 8 * depth;
+			}
+			gb->data[byte] |= (index & 1) << (7 - x % 8);
+			if (depth == 2) {
+				gb->data[byte + 1] |= (index >> 1) << (7 - x % 8);
+			}
+		}
+	}
+
+	if (!gb->horizontal) {
+		transpose_tiles(gb, png.width / 8);
+	}
+}
+
+void
+output_file(struct Options opts, struct GBImage gb)
+{
+	FILE *f;
+
+	f = fopen(opts.outfile, "wb");
+	if (!f) {
+		err(1, "Opening output file '%s' failed", opts.outfile);
+	}
+	fwrite(gb.data, 1, gb.size - gb.trim * 8 * depth, f);
+
+	fclose(f);
+}
+
+int
+get_tile_index(uint8_t *tile, uint8_t **tiles, int num_tiles, int tile_size)
+{
+	int i, j;
+	for (i = 0; i < num_tiles; i++) {
+		for (j = 0; j < tile_size; j++) {
+			if (tile[j] != tiles[i][j]) {
+				break;
+			}
+		}
+		if (j >= tile_size) {
+			return i;
+		}
+	}
+	return -1;
+}
+
+void
+create_tilemap(struct Options opts, struct GBImage *gb, struct Tilemap *tilemap)
+{
+	int i, j;
+	int gb_i;
+	int tile_size;
+	int max_tiles;
+	int num_tiles;
+	int index;
+	int gb_size;
+	uint8_t *tile;
+	uint8_t **tiles;
+
+	tile_size = sizeof(uint8_t) * depth * 8;
+	gb_size = gb->size - (gb->trim * tile_size);
+	max_tiles = gb_size / tile_size;
+	tiles = malloc(sizeof(uint8_t*) * max_tiles);
+	num_tiles = 0;
+
+	tilemap->data = malloc(sizeof(uint8_t) * max_tiles);
+	tilemap->size = 0;
+
+	gb_i = 0;
+	while (gb_i < gb_size) {
+		tile = malloc(tile_size);
+		for (i = 0; i < tile_size; i++) {
+			tile[i] = gb->data[gb_i];
+			gb_i++;
+		}
+		if (opts.unique) {
+			index = get_tile_index(tile, tiles, num_tiles, tile_size);
+			if (index < 0) {
+				index = num_tiles;
+				tiles[num_tiles] = tile;
+				num_tiles++;
+			}
+		} else {
+			index = num_tiles;
+			tiles[num_tiles] = tile;
+			num_tiles++;
+		}
+		tilemap->data[tilemap->size] = index;
+		tilemap->size++;
+	}
+
+	if (opts.unique) {
+		free(gb->data);
+		gb->data = malloc(tile_size * num_tiles);
+		for (i = 0; i < num_tiles; i++) {
+			tile = tiles[i];
+			for (j = 0; j < tile_size; j++) {
+				gb->data[i * tile_size + j] = tile[j];
+			}
+		}
+		gb->size = i * tile_size;
+	}
+
+	for (i = 0; i < num_tiles; i++) {
+		free(tiles[i]);
+	}
+	free(tiles);
+}
+
+void
+output_tilemap_file(struct Options opts, struct Tilemap tilemap)
+{
+	FILE *f;
+
+	f = fopen(opts.mapfile, "wb");
+	if (!f) {
+		err(1, "Opening tilemap file '%s' failed", opts.mapfile);
+	}
+
+	fwrite(tilemap.data, 1, tilemap.size, f);
+	fclose(f);
+
+	if (opts.mapout) {
+		free(opts.mapfile);
+	}
+}
+
+void
+output_palette_file(struct Options opts, struct PNGImage png)
+{
+	FILE *f;
+	int i, colors, color;
+	png_color *palette;
+
+	if (png_get_PLTE(png.png, png.info, &palette, &colors)) {
+		f = fopen(opts.palfile, "wb");
+		if (!f) {
+			err(1, "Opening palette file '%s' failed", opts.palfile);
+		}
+		for (i = 0; i < colors; i++) {
+			color = palette[i].blue >> 3 << 10 | palette[i].green >> 3 << 5 | palette[i].red >> 3;
+			fwrite(&color, 2, 1, f);
+		}
+		fclose(f);
+	}
+
+	if (opts.palout) {
+		free(opts.palfile);
+	}
+}
--- /dev/null
+++ b/src/gfx/main.c
@@ -1,0 +1,255 @@
+/*
+ * Copyright © 2013 stag019 <[email protected]>
+ *
+ * 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 <getopt.h>
+#include <stdlib.h>
+#include <string.h>
+#include "gfx/main.h"
+
+char *progname;
+
+static void
+usage(void)
+{
+	printf(
+"usage: rgbgfx [-DFfhPTuv] [-d #] [-o outfile] [-p palfile] [-t mapfile]\n"
+"[-x #] infile\n");
+	exit(1);
+}
+
+int
+main(int argc, char *argv[])
+{
+	int ch, size;
+	struct Options opts = {0};
+	struct PNGImage png = {0};
+	struct GBImage gb = {0};
+	struct Tilemap tilemap = {0};
+	char *ext;
+	const char *errmsg = "Warning: The PNG's %s setting is not the same as the setting defined on the command line.";
+
+	progname = argv[0];
+
+	if (argc == 1) {
+		usage();
+	}
+
+	opts.mapfile = "";
+	opts.palfile = "";
+	opts.outfile = "";
+
+	depth = 2;
+
+	while((ch = getopt(argc, argv, "DvFfd:hx:Tt:uPp:o:")) != -1) {
+		switch(ch) {
+		case 'D':
+			opts.debug = true;
+			break;
+		case 'v':
+			opts.verbose = true;
+			break;
+		case 'F':
+			opts.hardfix = true;
+		case 'f':
+			opts.fix = true;
+			break;
+		case 'd':
+			depth = strtoul(optarg, NULL, 0);
+			break;
+		case 'h':
+			opts.horizontal = true;
+			break;
+		case 'x':
+			opts.trim = strtoul(optarg, NULL, 0);
+			break;
+		case 'T':
+			opts.mapout = true;
+			break;
+		case 't':
+			opts.mapfile = optarg;
+			break;
+		case 'u':
+			opts.unique = true;
+			break;
+		case 'P':
+			opts.palout = true;
+			break;
+		case 'p':
+			opts.palfile = optarg;
+			break;
+		case 'o':
+			opts.outfile = optarg;
+			break;
+		default:
+			usage();
+		}
+	}
+	argc -= optind;
+	argv += optind;
+
+	if (argc == 0) {
+		usage();
+	}
+
+	opts.infile = argv[argc - 1];
+
+	if (depth != 1 && depth != 2) {
+		errx(1, "Depth option must be either 1 or 2.");
+	}
+	colors = 1 << depth;
+
+	input_png_file(opts, &png);
+
+	png.mapfile = "";
+	png.palfile = "";
+
+	get_text(&png);
+
+	if (png.horizontal != opts.horizontal) {
+		if (opts.verbose) {
+			warnx(errmsg, "horizontal");
+		}
+		if (opts.hardfix) {
+			png.horizontal = opts.horizontal;
+		}
+	}
+	if (png.horizontal) {
+		opts.horizontal = png.horizontal;
+	}
+
+	if (png.trim != opts.trim) {
+		if (opts.verbose) {
+			warnx(errmsg, "trim");
+		}
+		if (opts.hardfix) {
+			png.trim = opts.trim;
+		}
+	}
+	if (png.trim) {
+		opts.trim = png.trim;
+	}
+	if (opts.trim > png.width / 8 - 1) {
+		errx(1, "Trim (%i) for input png file '%s' too large (max: %i)", opts.trim, opts.infile, png.width / 8 - 1);
+	}
+
+	if (strcmp(png.mapfile, opts.mapfile) != 0) {
+		if (opts.verbose) {
+			warnx(errmsg, "tilemap file");
+		}
+		if (opts.hardfix) {
+			png.mapfile = opts.mapfile;
+		}
+	}
+	if (!*opts.mapfile) {
+		opts.mapfile = png.mapfile;
+	}
+
+	if (png.mapout != opts.mapout) {
+		if (opts.verbose) {
+			warnx(errmsg, "tilemap file");
+		}
+		if (opts.hardfix) {
+			png.mapout = opts.mapout;
+		}
+	}
+	if (png.mapout) {
+		opts.mapout = png.mapout;
+	}
+
+	if (strcmp(png.palfile, opts.palfile) != 0) {
+		if (opts.verbose) {
+			warnx(errmsg, "palette file");
+		}
+		if (opts.hardfix) {
+			png.palfile = opts.palfile;
+		}
+	}
+	if (!*opts.palfile) {
+		opts.palfile = png.palfile;
+	}
+
+	if (png.palout != opts.palout) {
+		if (opts.verbose) {
+			warnx(errmsg, "palette file");
+		}
+		if (opts.hardfix) {
+			png.palout = opts.palout;
+		}
+	}
+	if (png.palout) {
+		opts.palout = png.palout;
+	}
+
+	if (!*opts.mapfile && opts.mapout) {
+		if ((ext = strrchr(opts.infile, '.')) != NULL) {
+			size = ext - opts.infile + 9;
+			opts.mapfile = malloc(size);
+			strncpy(opts.mapfile, opts.infile, size);
+			*strrchr(opts.mapfile, '.') = '\0';
+			strcat(opts.mapfile, ".tilemap");
+		} else {
+			opts.mapfile = malloc(strlen(opts.infile) + 9);
+			strcpy(opts.mapfile, opts.infile);
+			strcat(opts.mapfile, ".tilemap");
+		}
+	}
+
+	if (!*opts.palfile && opts.palout) {
+		if ((ext = strrchr(opts.infile, '.')) != NULL) {
+			size = ext - opts.infile + 5;
+			opts.palfile = malloc(size);
+			strncpy(opts.palfile, opts.infile, size);
+			*strrchr(opts.palfile, '.') = '\0';
+			strcat(opts.palfile, ".pal");
+		} else {
+			opts.palfile = malloc(strlen(opts.infile) + 5);
+			strcpy(opts.palfile, opts.infile);
+			strcat(opts.palfile, ".pal");
+		}
+	}
+
+	gb.size = png.width * png.height * depth / 8;
+	gb.data = calloc(gb.size, 1);
+	gb.trim = opts.trim;
+	gb.horizontal = opts.horizontal;
+
+	if (*opts.outfile || *opts.mapfile) {
+		png_to_gb(png, &gb);
+		create_tilemap(opts, &gb, &tilemap);
+	}
+
+	if (*opts.outfile) {
+		output_file(opts, gb);
+	}
+
+	if (*opts.mapfile) {
+		output_tilemap_file(opts, tilemap);
+	}
+
+	if (*opts.palfile) {
+		output_palette_file(opts, png);
+	}
+
+	if (opts.fix || opts.debug) {
+		set_text(&png);
+		output_png_file(opts, &png);
+	}
+
+	free_png_data(&png);
+	free(gb.data);
+
+	return 0;
+}
--- /dev/null
+++ b/src/gfx/png.c
@@ -1,0 +1,335 @@
+/*
+ * Copyright © 2013 stag019 <[email protected]>
+ *
+ * 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 <string.h>
+#include "gfx/main.h"
+
+void
+input_png_file(struct Options opts, struct PNGImage *img)
+{
+	FILE *f;
+	int i, y, num_trans;
+	bool has_palette = false;
+	png_byte *trans_alpha;
+	png_color_16 *trans_values;
+	bool *full_alpha;
+	png_color *palette;
+
+	f = fopen(opts.infile, "rb");
+	if (!f) {
+		err(1, "Opening input png file '%s' failed", opts.infile);
+	}
+
+	img->png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+	if (!img->png) {
+		errx(1, "Creating png structure failed");
+	}
+
+	img->info = png_create_info_struct(img->png);
+	if (!img->info) {
+		errx(1, "Creating png info structure failed");
+	}
+
+	/* Better error handling here? */
+	if (setjmp(png_jmpbuf(img->png))) {
+		exit(1);
+	}
+
+	png_init_io(img->png, f);
+
+	png_read_info(img->png, img->info);
+
+	img->width  = png_get_image_width(img->png, img->info);
+	img->height = png_get_image_height(img->png, img->info);
+	img->depth  = png_get_bit_depth(img->png, img->info);
+	img->type   = png_get_color_type(img->png, img->info);
+
+	if (img->type & PNG_COLOR_MASK_ALPHA) {
+		png_set_strip_alpha(img->png);
+	}
+
+	if (img->depth != depth) {
+		if (opts.verbose) {
+			warnx("Image bit depth is not %i (is %i).", depth,
+			    img->depth);
+		}
+	}
+
+	if (img->type == PNG_COLOR_TYPE_GRAY) {
+		if (img->depth < 8) {
+			png_set_expand_gray_1_2_4_to_8(img->png);
+		}
+		png_set_gray_to_rgb(img->png);
+	} else {
+		if (img->depth < 8) {
+			png_set_expand_gray_1_2_4_to_8(img->png);
+		}
+		has_palette = png_get_PLTE(img->png, img->info, &palette,
+		    &colors);
+	}
+
+	if (png_get_tRNS(img->png, img->info, &trans_alpha, &num_trans,
+	    &trans_values)) {
+		if (img->type == PNG_COLOR_TYPE_PALETTE) {
+			full_alpha = malloc(sizeof(bool) * num_trans);
+
+			for (i = 0; i < num_trans; i++) {
+				if (trans_alpha[i] > 0) {
+					full_alpha[i] = false;
+				} else {
+					full_alpha[i] = true;
+				}
+			}
+
+			for (i = 0; i < num_trans; i++) {
+				if (full_alpha[i]) {
+					palette[i].red   = 0xFF;
+					palette[i].green = 0x00;
+					palette[i].blue  = 0xFF;
+					/*
+					 * Set to the lightest color in the
+					 * palette.
+					 */
+				}
+			}
+
+			free(full_alpha);
+		} else {
+			/* Set to the lightest color in the image. */
+		}
+
+		png_free_data(img->png, img->info, PNG_FREE_TRNS, -1);
+	}
+
+	if (has_palette) {
+		/* Make sure palette only has the amount of colors you want. */
+	} else {
+		/*
+		 * Eventually when this copies colors from the image itself,
+		 * make sure order is lightest to darkest.
+		 */
+		palette = malloc(sizeof(png_color) * colors);
+
+		if (strcmp(opts.infile, "rgb.png") == 0) {
+			palette[0].red   = 0xFF;
+			palette[0].green = 0xEF;
+			palette[0].blue  = 0xFF;
+
+			palette[1].red   = 0xF7;
+			palette[1].green = 0xF7;
+			palette[1].blue  = 0x8C;
+
+			palette[2].red   = 0x94;
+			palette[2].green = 0x94;
+			palette[2].blue  = 0xC6;
+
+			palette[3].red   = 0x39;
+			palette[3].green = 0x39;
+			palette[3].blue  = 0x84;
+		} else {
+			palette[0].red   = 0xFF;
+			palette[0].green = 0xFF;
+			palette[0].blue  = 0xFF;
+
+			palette[1].red   = 0xA9;
+			palette[1].green = 0xA9;
+			palette[1].blue  = 0xA9;
+
+			palette[2].red   = 0x55;
+			palette[2].green = 0x55;
+			palette[2].blue  = 0x55;
+
+			palette[3].red   = 0x00;
+			palette[3].green = 0x00;
+			palette[3].blue  = 0x00;
+		}
+	}
+
+	/*
+	 * Also unfortunately, this sets it at 8 bit, and I can't find any
+	 * option to reduce to 2 or 1 bit.
+	 */
+	png_set_quantize(img->png, palette, colors, colors, NULL, 1);
+
+	if (!has_palette) {
+		png_set_PLTE(img->png, img->info, palette, colors);
+		free(palette);
+	}
+
+	/*
+	 * If other useless chunks exist (sRGB, bKGD, pHYs, gAMA, cHRM, iCCP,
+	 * etc.) offer to remove?
+	 */
+
+	png_read_update_info(img->png, img->info);
+
+	img->data = malloc(sizeof(png_byte *) * img->height);
+	for (y = 0; y < img->height; y++) {
+		img->data[y] = malloc(png_get_rowbytes(img->png, img->info));
+	}
+
+	png_read_image(img->png, img->data);
+	png_read_end(img->png, img->info);
+
+	fclose(f);
+}
+
+void
+get_text(struct PNGImage *png)
+{
+	png_text *text;
+	int i, numtxts, numremoved;
+
+	png_get_text(png->png, png->info, &text, &numtxts);
+	for (i = 0; i < numtxts; i++) {
+		if (strcmp(text[i].key, "h") == 0 && !*text[i].text) {
+			png->horizontal = true;
+			png_free_data(png->png, png->info, PNG_FREE_TEXT, i);
+		} else if (strcmp(text[i].key, "x") == 0) {
+			png->trim = strtoul(text[i].text, NULL, 0);
+			png_free_data(png->png, png->info, PNG_FREE_TEXT, i);
+		} else if (strcmp(text[i].key, "t") == 0) {
+			png->mapfile = text[i].text;
+			png_free_data(png->png, png->info, PNG_FREE_TEXT, i);
+		} else if (strcmp(text[i].key, "T") == 0 && !*text[i].text) {
+			png->mapout = true;
+			png_free_data(png->png, png->info, PNG_FREE_TEXT, i);
+		} else if (strcmp(text[i].key, "p") == 0) {
+			png->palfile = text[i].text;
+			png_free_data(png->png, png->info, PNG_FREE_TEXT, i);
+		} else if (strcmp(text[i].key, "P") == 0 && !*text[i].text) {
+			png->palout = true;
+			png_free_data(png->png, png->info, PNG_FREE_TEXT, i);
+		}
+	}
+
+	/* TODO: Remove this and simply change the warning function not to warn instead. */
+	for (i = 0, numremoved = 0; i < numtxts; i++) {
+		if (text[i].key == NULL) {
+			numremoved++;
+		}
+		text[i].key = text[i + numremoved].key;
+		text[i].text = text[i + numremoved].text;
+		text[i].compression = text[i + numremoved].compression;
+	}
+	png_set_text(png->png, png->info, text, numtxts - numremoved);
+}
+
+void
+set_text(struct PNGImage *png)
+{
+	png_text *text;
+	char buffer[3];
+
+	text = malloc(sizeof(png_text));
+
+	if (png->horizontal) {
+		text[0].key = "h";
+		text[0].text = "";
+		text[0].compression = PNG_TEXT_COMPRESSION_NONE;
+		png_set_text(png->png, png->info, text, 1);
+	}
+	if (png->trim) {
+		text[0].key = "x";
+		snprintf(buffer, 3, "%d", png->trim);
+		text[0].text = buffer;
+		text[0].compression = PNG_TEXT_COMPRESSION_NONE;
+		png_set_text(png->png, png->info, text, 1);
+	}
+	if (*png->mapfile) {
+		text[0].key = "t";
+		text[0].text = "";
+		text[0].compression = PNG_TEXT_COMPRESSION_NONE;
+		png_set_text(png->png, png->info, text, 1);
+	}
+	if (png->mapout) {
+		text[0].key = "T";
+		text[0].text = "";
+		text[0].compression = PNG_TEXT_COMPRESSION_NONE;
+		png_set_text(png->png, png->info, text, 1);
+	}
+	if (*png->palfile) {
+		text[0].key = "p";
+		text[0].text = "";
+		text[0].compression = PNG_TEXT_COMPRESSION_NONE;
+		png_set_text(png->png, png->info, text, 1);
+	}
+	if (png->palout) {
+		text[0].key = "P";
+		text[0].text = "";
+		text[0].compression = PNG_TEXT_COMPRESSION_NONE;
+		png_set_text(png->png, png->info, text, 1);
+	}
+
+	free(text);
+}
+
+void
+output_png_file(struct Options opts, struct PNGImage *png)
+{
+	FILE *f;
+	char *outfile;
+	png_struct *img;
+
+	/* Variable outfile is for debugging purposes. Eventually, opts.infile will be used directly. */
+	if (opts.debug) {
+		outfile = malloc(strlen(opts.infile) + 5);
+		strcpy(outfile, opts.infile);
+		strcat(outfile, ".out");
+	} else {
+		outfile = opts.infile;
+	}
+
+	f = fopen(outfile, "wb");
+	if (!f) {
+		err(1, "Opening output png file '%s' failed", outfile);
+	}
+
+	img = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+	if (!img) {
+		errx(1, "Creating png structure failed");
+	}
+
+	/* Better error handling here? */
+	if (setjmp(png_jmpbuf(img))) {
+		exit(1);
+	}
+
+	png_init_io(img, f);
+
+	png_write_info(img, png->info);
+	
+	png_write_image(img, png->data);
+	png_write_end(img, NULL);
+
+	fclose(f);
+
+	if (opts.debug) {
+		free(outfile);
+	}
+}
+
+void
+free_png_data(struct PNGImage *png)
+{
+	int y;
+
+	for (y = 0; y < png->height; y++) {
+		free(png->data[y]);
+	}
+	free(png->data);
+}
--- /dev/null
+++ b/src/gfx/rgbgfx.1
@@ -1,0 +1,90 @@
+.Dd $Mdocdate$
+.Dt RGBGFX 1
+.Os RGBDS Manual
+.Sh NAME
+.Nm rgbgfx
+.Nd Game Boy graphics converter
+.Sh SYNOPSIS
+.Nm rgbgfx
+.Op Fl DfFhPTv
+.Op Fl o Ar outfile
+.Op Fl d Ar depth
+.Op Fl p Ar palfile
+.Op Fl t Ar mapfile
+.Op Fl x Ar tiles
+.Ar file
+.Sh DESCRIPTION
+The
+.Nm
+program converts PNG images into the Nintendo Game Boy's planar tile format.
+The arguments are as follows:
+.Bl -tag -width Ds
+.It Fl D
+Debug features are enabled.
+.It Fl f
+Fix the input PNG file to be a correctly indexed image.
+.It Fl F
+Same as
+.Fl f ,
+but additionally, the input PNG file is fixed to have its parameters match the
+command line's parameters.
+.It Fl d Ar depth
+The bitdepth of the output image (either 1 or 2).
+By default, the bitdepth is 2 (two bits per pixel).
+.It Fl h
+Lay out tiles horizontally rather than vertically.
+.It Fl o Ar outfile
+The name of the output file.
+.It Fl p Ar palfile
+Raw bytes (8 bytes for two bits per pixel, 4 bytes for one bit per pixel)
+containing the RGB15 values in the little-endian byte order and then ordered
+from lightest to darkest.
+.It Fl P
+Same as
+.Fl p ,
+but the pallete file output name is made by taking the input filename,
+removing the file extension, and appending
+.Pa .pal .
+.It Fl t Ar mapfile
+If any tiles are the same, don't place the repeat tiles in the output file, and
+make a tilemap file.
+.It Fl T
+Same as
+.Fl t ,
+but the tilemap file output name is made by taking the input filename,
+removing the file extension, and appending
+.Pa .tilemap .
+.It Fl u
+Truncate repeated tiles. Useful with tilemaps.
+.It Fl v
+Verbose.
+Print errors when the command line parameters and the parameters in
+the PNG file don't match.
+.It Fl x Ar tiles
+Trim the end of the output file by this many tiles.
+.El
+.Sh EXAMPLES
+The following will take a PNG file with a bitdepth of 1, 2, or 8, and output
+planar 2bpp data:
+.Pp
+.D1 $ rgbgfx -o out.2bpp in.png
+.Pp
+The following creates a planar 2bpp file with only unique tiles, and its tilemap
+.Pa out.tilemap :
+.Pp
+.D1 $ rgbgfx -T -u -o out.2bpp in.png
+.Pp
+The following will do nothing:
+.Pp
+.D1 $ rgbgfx in.png
+.Sh SEE ALSO
+.Xr rgbds 7 ,
+.Xr rgbasm 1 ,
+.Xr rgblink 1 ,
+.Xr rgbfix 1 ,
+.Xr gbz80 7
+.Sh HISTORY
+.Nm
+was created by
+.An stag019
+to be included in RGBDS.