changeset 96:3d10542703a9

New utility for converting map files to PPM format images
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 17 Dec 2006 10:03:35 +0000
parents 0d2b25e342df
children c094c3637841
files map2ppm.c
diffstat 1 files changed, 173 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/map2ppm.c	Sun Dec 17 10:03:35 2006 +0000
@@ -0,0 +1,173 @@
+/*
+ * Convert BatMUD ASCII map to PPM image file
+ * Programmed by Matti 'ccr' Hämäläinen (Ggr Pupunen)
+ * (C) Copyright 2006 Tecnic Software productions (TNSP)
+ */
+#include "maputils.h"
+#include "th_args.h"
+#include "th_string.h"
+
+
+char	*srcFilename = NULL,
+	*destFilename = NULL;
+
+BOOL	optUseOldFormat = FALSE,
+	optInputIsDiff = FALSE;
+
+	
+/* Arguments
+ */
+optarg_t optList[] = {
+	{ 0, '?', "help",	"Show this help", OPT_NONE },
+	{ 1, 'v', "verbose",	"Be more verbose", OPT_NONE },
+	{ 2, 'q', "quiet",	"Be quiet", OPT_NONE },
+	{ 3, 'd', "input-diff",	"Input is a diff", OPT_NONE },
+	{ 4, 'O', "old-format",	"Input using old symbols/colors", OPT_NONE },
+	{ 5, 'o', "output",	"Output filename", OPT_ARGREQ },
+};
+
+const int optListN = (sizeof(optList) / sizeof(optarg_t));
+
+
+void argShowHelp(void)
+{
+	th_args_help(stderr, optList, optListN, th_prog_name,
+	"[options] <input mapfile>");
+}
+
+
+BOOL argHandleOpt(const int optN, char *optArg, char *currArg)
+{
+	switch (optN) {
+	case 0:
+		argShowHelp();
+		exit(0);
+		break;
+
+	case 1:
+		th_verbosityLevel++;
+		break;
+	
+	case 2:
+		th_verbosityLevel = -1;
+		break;
+	
+	case 3:
+		optInputIsDiff = TRUE;
+		THMSG(2, "Input is a 'diff', handling it as such.\n");
+		break;
+		
+	case 4:
+		optUseOldFormat = TRUE;
+		THMSG(2, "Input is using old map symbols/colors.\n");
+		break;
+	
+	case 5:
+		destFilename = optArg;
+		THMSG(2, "Output file set to '%s'.\n", destFilename);
+		break;
+	
+	default:
+		THERR("Unknown option '%s'.\n", currArg);
+		return FALSE;
+	}
+	
+	return TRUE;
+}
+
+
+BOOL argHandleFile(char *currArg)
+{
+	if (!srcFilename)
+		srcFilename = currArg;
+	else {
+		THERR("Too many input map files specified!\n");
+		return FALSE;
+	}
+
+	return TRUE;
+}
+
+
+/* Main program
+ */
+int main(int argc, char *argv[])
+{
+	FILE *outFile;
+	mapblock_t *map;
+	int x, y, i;
+	size_t s;
+	char *d, tmpStr[64];
+
+	/* Initialize */
+	th_init("map2ppm", "ASCII map to PPM image converter", "0.1", NULL, NULL);
+	th_verbosityLevel = 1;
+	
+	/* Parse arguments */
+	if (!th_args_process(argc, argv, optList, optListN,
+		argHandleOpt, argHandleFile, TRUE))
+		exit(1);
+	
+	if (srcFilename == NULL) {
+		THERR("Nothing to do. (try --help)\n");
+		exit(0);
+	}
+	
+	/* Read input file */
+	THMSG(1, "Reading map file '%s'\n", srcFilename);
+	
+	if ((map = parseFile(srcFilename, optInputIsDiff)) == NULL) {
+		THERR("Error reading map file '%s'!\n",
+			srcFilename);
+		exit(1);
+	}
+	
+	/* Open output file */
+	if (destFilename == NULL)
+		outFile = stdout;
+	else if ((outFile = fopen(destFilename, "wb")) == NULL) {
+		THERR("Error opening output file '%s'!\n",
+			destFilename);
+		exit(1);
+	}
+
+	
+	/* Write header for 24-bit PPM */
+	THMSG(1, "Outputting 24-bit PPM image of %dx%d ...\n",
+		map->w, map->h);
+	
+	snprintf(tmpStr, sizeof(tmpStr),
+		"P6\n%d %d\n255\n",
+		map->w, map->h);
+	
+	fputs(tmpStr, outFile);
+	
+	s = th_strlen(tmpStr);
+	d = map->d;
+	for (y = 0; y < map->h; y++)
+	for (x = 0; x < map->w; x++) {
+		int qr, qg, qb, c;
+		
+		c = mcGetColor(*d, optUseOldFormat);
+		if (c >= 0 && c < nmapColors) {
+			sscanf(mapColors[c].css, "#%2x%2x%2x",
+				&qr, &qg, &qb);
+		} else {
+			/* Unknown symbol -> */
+			qr = qb = qg = 255;
+		}
+		
+		fputc(qr, outFile);
+		fputc(qg, outFile);
+		fputc(qb, outFile);
+		d++;
+		s += 3;
+	}
+	
+	fclose(outFile);
+	
+	THMSG(1, "Done. Image size %d bytes.\n", s);
+	
+	exit(0);
+	return 0;
+}